-
Notifications
You must be signed in to change notification settings - Fork 4
/
rvhaplo.sh
526 lines (504 loc) · 12 KB
/
rvhaplo.sh
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#! /bin/bash
### required arguments
file_sam=""
file_ref=""
### optional arguments
file_path='./result'
prefix="rvhaplo"
mq=0
thread=8
error_rate=0.1
signi_level=0.05
cond_pro=0.65
fre_snv=0.8
num_read_1=10
num_read_2=5
gap=15
smallest_snv=20
only_snv=0
ovlap_read=5
weight_read=0.85
mcl_inflation=2
lar_cluster=50
ovlap_cluster=10
depth=5
weight_cluster=0.8
abundance=0.005
s_pos=1
e_pos=10000000000
sub_graph=1
function help_info() {
echo "Usage: $0 -i alignment.sam -r ref_genome.fasta [options]"
echo ""
echo "RVHaplo: Reconstructing viral haplotypes using long reads"
echo ""
echo "Author: Dehan CAI"
echo "Date: May 2022"
echo "Version 2: Support mutli-thread processing; Use a C package of MCL; Cost less memory "
echo ""
echo " -i | --input: alignment file (sam)"
echo " -r | --refernece: reference genome (fasta)"
echo ""
echo " Options:"
echo " -o | --out: Path where to output the results. (default:./result)"
echo " -p | --prefix STR: Prefix of output file. (default: rvhaplo)"
echo " -t | --thread INT: Number of CPU cores for multiprocessing. (default:8)"
echo " -e | --error_rate FLOAT: Sequencing error rate. (default: 0.1)"
echo " -mq | --map_qual INT: Smallest mapping quality for reads . (default:0)"
echo " -s | --signi_level FLOAT: Significance level for binomial tests. (default: 0.05)"
echo " -c | --cond_pro FLOAT: A threshold of the maximum conditional probability for SNV sites. (default: 0.65)"
echo " -f | --fre_snv FLOAT: The most dominant base' frequency at a to-be-verified site should >= fre_snv. (default: 0.80)"
echo " -n1 | --num_read_1 INT: Minimum # of reads for calculating the conditional probability given one conditional site. (default:10)"
echo " -n2 | --num_read_2 INT: Minimum # of reads for calculating the conditional probability given more than one conditional sites. (default: 5)"
echo " -g | --gap INT: Minimum length of gap between SNV sites for calculating the conditional probability. (default:15)"
echo " -ss | --smallest_snv INT: Minimum # of SNV sites for haplotype construction. (default:20)"
echo " -os | --only_snv (0 or 1) : Only output the SNV sites without running the haplotype reconstruction part. (default: 0)"
echo " -or | --overlap_read INT: Minimum length of overlap for creating edges between two read in the read graph. (default: 5)"
echo " -wr | --weight_read FLOAT: Minimum weights of edges in the read graph. (default:0.85)"
echo " -sg | --sub_graph INT: Number of subgraphs to run MCL (default:1)"
echo " -m | --mcl_inflation FLOAT: Inflation of MCL algorithm. (default:2)"
echo " -l | --lar_cluster INT: A threshold for seperating clusters into two groups based on sizes of clusters. (default:50)"
echo " -oc | --overlap_cluster INT: A parameter related to the minimum overlap between consensus sequences. (default:10) "
echo " -d | --depth INT: Depth limitation for consensus sequences generated from clusters. (default:5) "
echo " -wc | --weight_cluster FLOAT: Minimum weights between clusters in the hierarchical clustering. (default: 0.8)"
echo " -sp | --start_pos INT: Starting position for generating consensus sequences (default: 1)"
echo " -ep | --end_pos INT: Ending position for generating consensus sequences. (default: 1e10)"
echo " -a | --abundance FLOAT: A threshold for filtering low-abundance haplotypes. (default: 0.005)"
echo " -h | --help : Print help message."
echo ""
echo " For further details of above arguments, please refer to https://github.com/dhcai21/RVHaplo "
echo ""
exit 0
}
if [[ "$1" == "" ]];then
help_info
exit 1
fi
while [[ "$1" != "" ]]; do
case "$1" in
-h | --help ) ## print help message
help_info
exit 1
;;
-i | --input ) ### input sam file
case "$2" in
"" )
echo "Error: no sam file as input"
exit 1
;;
*)
file_sam="$2"
if [[ "${file_sam:0:1}" == "-" ]]
then
echo "Error: no sam file as input"
exit 1
fi
shift 2
;;
esac
;;
-r | --ref_genome) ### input reference genome
case "$2" in
"")
echo "Error: no fasta file as input"
exit 1
;;
*)
file_ref="$2"
if [[ ""${file_ref:0:1}"" == "-" ]]
then
echo "Error: no fasta file as input"
exit 1
fi
shift 2
;;
esac
;;
-o | --out ) ### output path
case "$2" in
"" )
echo "Error: no output path"
exit 1
;;
*)
file_path="$2"
if [[ "${file_sam:0:1}" == "-" ]]
then
echo "Error: no output path"
exit 1
fi
shift 2
;;
esac
;;
-p | --prefix ) ### prefix
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
prefix="$2"
shift 2
;;
esac
;;
-mq | --map_qual ) ### mapping quality
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
mq="$2"
shift 2
;;
esac
;;
-t | --thread ) ### threads
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
thread="$2"
shift 2
;;
esac
;;
-e | --error_rate ) ### error_rate
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
error_rate="$2"
shift 2
;;
esac
;;
-s | --signi_level ) ### significance_level
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
signi_level="$2"
shift 2
;;
esac
;;
-c | --cond_pro ) ### conditional_probability
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
cond_pro="$2"
shift 2
;;
esac
;;
-f | --fre_snv ) ### determine the set of to-be-verified SNV sites
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
fre_snv="$2"
shift 2
;;
esac
;;
-n1 | --num_read_1 ) ### number of reads for p(ai|aj)
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
num_read_1="$2"
shift 2
;;
esac
;;
-n2 | --num_read_2 ) ### number of reads for p(ai|aj1,aj2,...,ajp)
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
num_read_2="$2"
shift 2
;;
esac
;;
-g | --gap ) ### Minimum distance between SNV sites
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
gap="$2"
shift 2
;;
esac
;;
-ss | --smallest_snv ) ### Minimum number of SNV sites for haplotype reconstruction
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
smallest_snv="$2"
shift 2
;;
esac
;;
-os | --only_snv ) ### Only output the SNV sites without running the haplotype reconstruction part.
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
only_snv="$2"
shift 2
;;
esac
;;
-or | --ovlap_read ) ### overlap_read
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
ovlap_read="$2"
shift 2
;;
esac
;;
-wr | --weight_read ) ### weight_read
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
weight_read="$2"
shift 2
;;
esac
;;
-sg | --sub_graph ) ### Number of subgraphs
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
sub_graph="$2"
shift 2
;;
esac
;;
-m | --mcl_inflation ) ### inflation of MCL
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
mcl_inflation="$2"
shift 2
;;
esac
;;
-oc | --ovlap_cluster ) ### overlap_cluster
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
ovlap_cluster="$2"
shift 2
;;
esac
;;
-wc | --weight_cluster ) ### weight_cluster
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
weight_cluster="$2"
shift 2
;;
esac
;;
-d | --depth ) ### depth limitation
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
depth="$2"
shift 2
;;
esac
;;
-l | --lar_cluster ) ### large cluster size
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
lar_cluster="$2"
shift 2
;;
esac
;;
-sp | --start_pos ) ### start_pos
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
s_pos="$2"
shift 2
;;
esac
;;
-ep | --end_pos ) ### end_pos
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
e_pos="$2"
shift 2
;;
esac
;;
-a | --abundance ) ### smallest abundance
case "$2" in
"" )
echo "Error: no input for $1"
exit 1
;;
*)
abundance="$2"
shift 2
;;
esac
;;
*)
echo "Error: unknow parameter $1"
exit 1
esac
done
if [[ "$file_sam" == "" ]];then
echo "Error: no sam file input"
exit 1
fi
if [[ "$file_ref" == "" ]];then
echo "Error: no reference genome input"
exit 1
fi
if [[ ${file_path:0-1:1} == "/" ]];then
path_len=`expr ${#file_path}-1`
file_prefix=$file_path$prefix
file_path=${file_path:0:path_len}
else
file_prefix=$file_path"/"$prefix
fi
########## count nucleotide occurrence ##########
echo "count nucleotide occurrence"
if [[ "$file_path" != "." ]];then
rm -rf $file_path
mkdir -p $file_path
fi
rm -rf $file_path"/alignment"
mkdir -p $file_path"/alignment"
file_len=`expr ${#file_sam}-4`
unique_sam=$file_path"/alignment/"$prefix".sam"
samtools view -h -F 0x900 -q $mq $file_sam > $unique_sam
file_bam=$file_path"/alignment/"$prefix".bam"
samtools view -b -S $unique_sam > $file_bam
rm $unique_sam
file_bam_sorted=$file_path"/alignment/"$prefix"_sorted.bam"
samtools sort $file_bam -o $file_bam_sorted
samtools index $file_bam_sorted
file_acgt=$file_prefix"_acgt.txt"
python ./src/count_frequency.py $file_bam_sorted $file_acgt
########## two binomial tests ##########
echo "SNV detection"
file_snv=$file_prefix"_snv.txt"
python ./src/two_binomial.py $error_rate $signi_level $file_acgt $file_snv $thread $s_pos $e_pos
## judge number of detected SNV sites
size="$(wc -l <"$file_snv")"
#size="${size:0-1:1}"
if (( $size != 0 ));then
python ./src/out_haplotypes.py $file_prefix"_clusters.pickle" $file_bam_sorted $file_path $file_acgt 1 $file_prefix"_consensus.fasta" $s_pos $e_pos
python ./src/extract_reads.py $file_path $prefix 1
python ./src/run_medaka.py $file_path $prefix 1
exit 0
fi
## maximum conditional probability and construct reads graph
python ./src/mcp_read_graph.py $file_bam_sorted $file_snv $cond_pro $smallest_snv $num_read_1 $num_read_2 $gap \
$weight_read $ovlap_read $file_prefix $fre_snv $thread $only_snv $sub_graph
## judge number of detected SNV sites
size="$(wc -l <"$file_snv")"
#size="${size:0-1:1}"
if (( $size != 0 ));then
python ./src/out_haplotypes.py $file_prefix"_clusters.pickle" $file_bam_sorted $file_path $file_acgt 1 $file_prefix"_consensus.fasta" $s_pos $e_pos
python ./src/extract_reads.py $file_path $prefix 1
python ./src/run_medaka.py $file_path $prefix 1
exit 0
fi
if (( $only_snv != 0 ));then
exit 0
fi
## check the number of reads with overlaps
size="$(wc -l <"$file_prefix"_reads_graph.txt)"
#size="${size:0-1:1}"
if (( $size == 0 ));then
echo "No enough reads with overlaps"
exit 0
fi
# MCL clustering
echo "MCL clustering"
python ./src/run_mcl.py "$file_prefix" "$thread" "$mcl_inflation" "$sub_graph"
## hierarchical clustering
echo "hierarchical clustering"
python ./src/hierarchical_cluster.py $file_prefix"_matrix.pickle" $lar_cluster $depth \
$ovlap_cluster $weight_cluster $abundance $file_prefix
## reconstruct haplotypes
rm -rf $file_path"/clusters"
mkdir -p $file_path"/clusters"
echo "haplotypes reconstruction"
python ./src/out_haplotypes.py $file_prefix"_clusters.pickle" $file_bam_sorted $file_path $file_acgt x $file_prefix"_consensus.fasta" $s_pos $e_pos
echo "haplotypes polishment (medaka)"
python ./src/extract_reads.py $file_path $prefix x
python ./src/run_medaka.py $file_path $prefix x
rm $file_prefix"_matrix.pickle"
rm $file_prefix"_reads_cluster.txt"
rm $file_prefix"_clusters.pickle"
rm -rf $file_path/medaka
echo "complete reconstructing haplotypes"
exit 0