-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCAtools.sh
More file actions
219 lines (173 loc) · 6.51 KB
/
GCAtools.sh
File metadata and controls
219 lines (173 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env bash
# GCAtools
# Copyright (C) Yumin Huang
# Contact: [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
set -e
if [ -z "$1" ]; then
echo "The arguments is empty!"
exit 1
fi
gettime() {
echo -e `date '+%Y-%m-%d %H:%M:%S ... '`
}
dont_stat_chromosome_group=""
stat_confidence=""
count_collinearity=""
Usage () {
echo ""
echo "GCAtools"
echo "Usage: GCAtools -f <clusterfolder> [options]"
echo ""
echo "Description:"
echo " Core workflow of GCAtools"
echo ""
echo "Options:"
echo " -h Show this message and exit"
#echo " -l <str> Prefix name of first file"
echo " -f <dir> Folder of cluster files"
echo " -t <str> Type of cluster files (dna or pep) [default dna]"
echo " -d <dir> Output file in <dir> [default ./GCAout]"
echo " -n <int> Number of threads [default 36]"
echo ""
echo "Author: Yumin Huang; [email protected]"
echo ""
exit 1
}
while getopts "hf:t:d:rcs:e:n:b:m" opt
do
case $opt in
h)
Usage
exit 1
;;
f)
fname=$OPTARG
;;
t)
ty=$OPTARG
;;
d)
directory=$OPTARG
;;
r)
dont_stat_chromosome_group="-r"
;;
c)
stat_confidence="-c"
;;
s)
fa_str=$OPTARG
;;
e)
evalue=$OPTARG
;;
n)
num_threads=$OPTARG
;;
b)
score_threshold=$OPTARG
;;
m)
count_collinearity="-m"
;;
?)
echo "Unknow argument!"
exit 1
;;
esac
done
logo () {
echo ""
echo " ==============================="
echo " || ||"
echo " || GCAtools ||"
echo " || Version: v0.0.1 ||"
echo " || ||"
echo " ==============================="
echo ""
}
logo
script_dir=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
based=$(echo ${directory-./GCAout})
rm -rf ${based}
echo "Total $(ls "${fname}"|wc -l) fasta files detected in ${fname}..."
echo `gettime`"generate MSA..."
mkdir -p ${based}/msas/
mkdir -p ${based}/logs/
ls -1 $fname | parallel --joblog ${based}/logs/parallel.msa -j ${num_threads-36} ''"${script_dir}"'/bin/muscle-linux-x86.v5.2 -align '"${fname}"'/{} -output '"${based}/msas/{}"'' 2>${based}/logs/logs.msas || true
# 检查并报告 `parallel` 的退出状态
if [ $? -ne 0 ]; then
echo "Warning: parallel command failed."
fi
echo `gettime`"muscle MSA done..."
cp ${based}/logs/parallel.msa ${based}/logs/parallel.rawmsa
rd=1
while true; do
failed_count=$(awk '$7 != 0 || $8 != 0' <(tail -n+2 "${based}/logs/parallel.msa") | wc -l)
if [ "$failed_count" -eq 0 ]; then
echo "All files successed in MSA processings..."
break
else
echo "Round ${rd}: Total ${failed_count} fasta files failed in muscle MSA processings..."
echo "Preformed MSA by MAFFT..."
awk '$7 != 0 || $8 != 0' ${based}/logs/parallel.msa | awk -F '\t' 'NR>1{print $9}'|awk '{print $(NF-2)}'|while read f; do basename=$(basename "$f"); echo "$basename";done | parallel --joblog ${based}/logs/parallel.mafft -j ${num_threads-36} 'file=$(basename {}); mafft --maxiterate 1000 --ep 0 --genafpair '"${fname}"'/${file} > '"${based}"'/msas/${file}' 2>>${based}/logs/logs.msas
cat ${based}/logs/parallel.msa ${based}/logs/parallel.mafft > ${based}/logs/parallel.rawmsa
mv ${based}/logs/parallel.mafft ${based}/logs/parallel.msa
((rd++))
#echo "All files successed in MSA processings..."
fi
done
rm ${based}/logs/parallel.msa
echo `gettime`"build graph..."
if [ "$ty" == "pep" ]; then
PanPA build_gfa -d ${based}/msas/ -c ${num_threads-36} -o ${based}/graphs
else
mkdir -p ${based}/graphs
ls ${based}/msas/ | parallel --joblog ${based}/logs/parallel.construct -j ${num_threads-36} 'ii=$(echo {} | sed "s/fasta/gfa/g"); '"${script_dir}"'/bin/vg construct -m 999999999 -M '"${based}"'/msas/{} | '"${script_dir}"'/bin/vg convert -f - > '"${based}"'/graphs/${ii}'
fi
echo `gettime`"calculate graph complexity..."
echo "id segments links arcs mrank totalseg avgseg s mdegree avgdegree" > ${based}/graphs.stat; for i in `ls ${based}/graphs/`;do gfatools stat ${based}/graphs/${i} | awk -F ': ' 'BEGIN{print "'$i'"}{print $2}'|xargs -n 10 |awk '{sub(/\.gfa$/, "", $1); print}' >> ${based}/graphs.stat;done 2>${based}/logs/gfatools.log
python3 ${script_dir}/scripts/parse_alignlen.py ${based}/msas/ ${based}/graphs.length
mkdir -p ${based}/bubble
python3 ${script_dir}/scripts/parse_bubble.py ${based}/graphs ${based}/bubble/graphs.bubble ${based}/bubble/graphs.bubble.log > ${based}/logs/logs.bubble
paste ${based}/graphs.stat ${based}/graphs.length <(awk '{sub(/\.gfa$/, "", $1); print}' ${based}/bubble/graphs.bubble) |sed 's/[[:space:]]\+/\t/g' | awk '{if($1==$11&&$1==$15){print}else{print "Error: Inconsistent values found in line " NR ": " $0;exit 1;}}' | cut -f1-10,12-14,16- > ${based}/graphs.combined
# 检查awk的退出状态,如果不为0则终止脚本
if [ $? -ne 0 ]; then
echo "Script terminated due to inconsistent values."
exit 1
fi
awk -F "\t" 'BEGIN { OFS="\t" }
function harmonic_series(n) {
if (n <= 0) return 0
sum = 0
for (i = 1; i <= n; i++) {
sum += 1 / i
}
return sum
}
NR == 1 {
print $0, "indicatorL", "indicatorB"
}
NR > 1 {
h = harmonic_series($13)
indicatorL = $3 / ($11 * h)
indicatorB = $17 / $11
print $0, indicatorL, indicatorB
}' ${based}/graphs.combined > ${based}/graphs.complexity.txt
echo "处理完成,结果已保存到 ${based}/graphs.complexity.txt"