forked from grenaud/schmutzi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbam2makeSchmutzi.pl
executable file
·242 lines (183 loc) · 10.5 KB
/
bam2makeSchmutzi.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use POSIX;
use Cwd 'abs_path';
sub fileExists{
my ($exeFile) = @_;
return ( -e $exeFile);
}
my $threads="2";
my $library="double";
#my $dir = getcwd;
#my $abs_path = abs_path($dir);
my @arraycwd=split("/",abs_path($0));
pop(@arraycwd);
my $pathdir = join("/",@arraycwd);
my $skipContDeam=0;
my $skipPred=0;
my $subsample=-1;
my $nice="";
#my $installDirToFastaHaplogrep="/home/gabriel/scripts/fasta2haplogrep/";
my $installDirToFastaHaplogrep = $pathdir;
my $installDir = $pathdir;#"/home/gabriel/projects/schmutzi";
my $iterations=5;
print STDERR " ~~~~ Please read carefully ~~~~\n";
print STDERR "using script path: ".$installDir."/projects/schmutzi/schmutzi.pl"."\n";
print STDERR "using haplogred : ".$installDirToFastaHaplogrep."/fasta2haplogrep.py"."\n";
print STDERR "if these paths are incorrect, change them in the Perl script\n";
print STDERR "Also, fasta2haplogrep.py uses haplogrep-cmd.jar, please hard code the path in this script\n";
my $usage= "\n\n usage:\t".$0." <options> [bam file1] [bam file2]...\n\n".
" Options:\n".
"\t--skippred\t\t\t\tSkip contamination estimate using prediction of the contaminant (useful for low cont. samples)\n".
"\t--nodeam [cont]\t\t\t\tSkip contamination based on deamination, useful for UDG treated\n".
"\t\t\t\t\t\tuse [cont] as prior instead, must be between 0.01 and 0.99\n".
"\t--single\t\t\t\tUse single stranded library damage (just C->T on both ends)\n".
"\t--subsample [XXX]\t\t\tSubsample the BAM file down to XXX, 100-300 depending on how difficult the target is are good\n".
"\t--threads [num]\t\t\tUse [num] of threads (default $threads)\n".
"\t--iterations [num]\t\t\tMaximum number of iterations (default $iterations)\n".
"\t--nice\t\t\t\t\tUse nice\n\n\n";
if($#ARGV == -1){
die $usage;
}
my $nodeamContPrior;
my @arrayOfTargets;
my @arrayOfTargetsClean;
my $stringToPrint="";
my $i=0;
foreach my $filebam (@ARGV){
$i++;
if($filebam =~ /^--/){
# if($filebam eq "--nodeam"){
# $skipContDeam=1;
# next;
# }
if($filebam eq "--skippred"){
$skipPred=1;
next;
}
if($filebam eq "--nice"){
$nice=" nice -n 19 ";
next;
}
if($filebam eq "--single"){
$library="single";
next;
}
if($filebam eq "--nodeam"){
if($ARGV[$i] !~ /^0.\d+$/){
die "Wrong --nodeam parameter ".$ARGV[$i]."\n";
}
$skipContDeam = 1;
$nodeamContPrior= $ARGV[$i];
next;
}
if($filebam eq "--threads"){
if($ARGV[$i] !~ /^\d+$/){
die "Wrong --threads parameter ".$ARGV[$i]."\n";
}
$threads=$ARGV[$i];
next;
}
if($filebam eq "--subsample"){
if($ARGV[$i] !~ /^\d+$/){
die "Wrong --subsample parameter ".$ARGV[$i]."\n";
}
$subsample=$ARGV[$i];
next;
}
if($filebam eq "--iterations"){
if($ARGV[$i] !~ /^\d+$/){
die "Wrong --iterations parameter ".$ARGV[$i]."\n";
}
$iterations=$ARGV[$i];
next;
}
die "Unknown option ".$filebam."\n";
}
if($skipContDeam == 1){
if($nodeamContPrior<0.01 or $nodeamContPrior>0.99){
die "The contamination prior must be between 0.01 and 0.99 found: ".$nodeamContPrior."\n";
}
}
if($filebam !~ /.bam$/){
next;
}
print STDERR "Found bam file $filebam\n";
my @filea = split("/",$filebam);
my $filebamname=$filea[$#filea]; #file name without path
my $name = substr($filebamname,0,-4);
my $outprefix = substr($filebam,0,-4);#path without extension
#my $prefixFilename =
if( $subsample != -1){
push(@arrayOfTargets, $outprefix."_sub.bam");
push(@arrayOfTargetsClean,$outprefix."_sub.bam");
$stringToPrint.="".$outprefix."_sub.bam:\n\t".$installDir."/subSampleBAM ".$subsample." ".$outprefix.".bam ".$outprefix."_sub.bam\n\n";
$outprefix = $outprefix."_sub";
}
push(@arrayOfTargets, $outprefix.".bai");
push(@arrayOfTargetsClean,$outprefix.".bai");
$stringToPrint.="".$outprefix.".bai:";
if( $subsample != -1){
$stringToPrint.=" ".$outprefix.".bam";
}
$stringToPrint.="\n\tsamtools index ".$outprefix.".bam\n\n";
push(@arrayOfTargets, $outprefix.".cont.est");
push(@arrayOfTargetsClean,$outprefix.".cont*");
push(@arrayOfTargetsClean,$outprefix.".deam.config");
push(@arrayOfTargetsClean,$outprefix.".endo.5p.prof");
push(@arrayOfTargetsClean,$outprefix.".endo.3p.prof");
if($skipContDeam == 1){
$stringToPrint.= "".$outprefix.".cont.est: ".$outprefix.".bai\n\techo -e \"0.5\t0.49\t0.51\" > ".$outprefix.".cont.est\n\t/opt/schmutzi/bam2prof -5p ".$outprefix.".endo.5p.prof -3p ".$outprefix.".endo.3p.prof ".$outprefix.".bam\n\techo -e \"library\\t".$library."\\noutputPrefix\\t".$outprefix."\\nreferenceFasta\\t\\ncontPriorKnow\\t-1\\ntextGraph\\tPosterior probability for contamination\\\\\\nusing deamination patterns\\nsplitPos\\t\\nuseLength\\t0\\nsplitDeam\\t0\" > ".$outprefix.".deam.config\n\n";
}else{
$stringToPrint.= "".$outprefix.".cont.est: ".$outprefix.".bai\n\tif ".$installDir."/contDeam.pl --library ".$library." --lengthDeam 30 --out ".$outprefix." ".$outprefix.".bam; then echo \"command contDeam finished\"; else echo -e \"0.5\t0.49\t0.51\" > ".$outprefix.".cont.est; fi\n\n";
}
if (!$skipPred ) {
push(@arrayOfTargets, $outprefix."_wtpred_final.cont.est");
push(@arrayOfTargetsClean,$outprefix."_wtpred*");
$stringToPrint.= "".$outprefix."_wtpred_final.cont.est: ".$outprefix.".cont.est\n\tif $nice ".$installDir."/schmutzi.pl ";
if($skipContDeam==1){
$stringToPrint.= " --contprior ".$nodeamContPrior." ";
}
$stringToPrint.=" --iterations $iterations -t $threads --uselength --ref ".$installDir."/refs/human_MT.fa --out ".$outprefix."_wtpred ".$outprefix." ".$installDir."/alleleFreqMT/eurasian/freqs/ ".$outprefix.".bam; then echo \"command with pred finished\"; else echo \"command with pred stopped\"; fi\n\n";
}
push(@arrayOfTargets, $outprefix."_nopred_final.cont.est");
push(@arrayOfTargetsClean,$outprefix."_nopred*");
$stringToPrint.= "".$outprefix."_nopred_final.cont.est: ".$outprefix.".cont.est\n\tif $nice ".$installDir."/schmutzi.pl ";
if($skipContDeam==1){
$stringToPrint.= " --contprior ".$nodeamContPrior." ";
}
$stringToPrint.= " --iterations $iterations --notusepredC -t $threads --uselength --ref ".$installDir."/refs/human_MT.fa --out ".$outprefix."_nopred ".$outprefix." ".$installDir."/alleleFreqMT/eurasian/freqs/ ".$outprefix.".bam; then echo \"command with pred finished\"; else echo \"command with pred stopped\"; fi\n\n";
my @typeOfTargets = ("wtpred","nopred");
if($skipPred){
@typeOfTargets = ("nopred");
}
for (my $q=10;$q<=50;$q+=20) {
foreach my $type (@typeOfTargets) {
push(@arrayOfTargets, $outprefix."_".$type."_final_endo.q".$q.".fa");
push(@arrayOfTargetsClean,$outprefix."_".$type."_final_endo.q".$q.".fa");
$stringToPrint.="".$outprefix."_".$type."_final_endo.q".$q.".fa: ".$outprefix."_".$type."_final.cont.est\n\t".
"if [ -e ".$outprefix."_".$type."_final_endo.log ]; then tail -n+2 ".$outprefix."_".$type."_final_endo.log |cut -f 6| awk 'BEGIN{sum=0;}{ sum+=\$\$1; }END{print sum/NR}' > ".$outprefix."_".$type."_final_endo.cov; ".$installDir."//log2fasta -name ".$name."q".$q."w -q ".$q." -indel ".$q." ".$outprefix."_".$type."_final_endo.log > ".$outprefix."_".$type."_final_endo.q".$q.".fa; else ".
"if [ -e ".$outprefix."_".$type."_5_endo.log ]; then tail -n+2 ".$outprefix."_".$type."_5_endo.log |cut -f 6| awk 'BEGIN{sum=0;}{ sum+=\$\$1; }END{print sum/NR}' > ".$outprefix."_".$type."_final_endo.cov; ".$installDir."/log2fasta -name ".$name."q".$q."w -q ".$q." -indel ".$q." ".$outprefix."_".$type."_5_endo.log > ".$outprefix."_".$type."_final_endo.q".$q.".fa; else ".
"if [ -e ".$outprefix."_".$type."_4_endo.log ]; then tail -n+2 ".$outprefix."_".$type."_4_endo.log |cut -f 6| awk 'BEGIN{sum=0;}{ sum+=\$\$1; }END{print sum/NR}' > ".$outprefix."_".$type."_final_endo.cov; ".$installDir."/log2fasta -name ".$name."q".$q."w -q ".$q." -indel ".$q." ".$outprefix."_".$type."_4_endo.log > ".$outprefix."_".$type."_final_endo.q".$q.".fa; else ".
"if [ -e ".$outprefix."_".$type."_3_endo.log ]; then tail -n+2 ".$outprefix."_".$type."_3_endo.log |cut -f 6| awk 'BEGIN{sum=0;}{ sum+=\$\$1; }END{print sum/NR}' > ".$outprefix."_".$type."_final_endo.cov; ".$installDir."/log2fasta -name ".$name."q".$q."w -q ".$q." -indel ".$q." ".$outprefix."_".$type."_3_endo.log > ".$outprefix."_".$type."_final_endo.q".$q.".fa; else ".
"if [ -e ".$outprefix."_".$type."_2_endo.log ]; then tail -n+2 ".$outprefix."_".$type."_2_endo.log |cut -f 6| awk 'BEGIN{sum=0;}{ sum+=\$\$1; }END{print sum/NR}' > ".$outprefix."_".$type."_final_endo.cov; ".$installDir."/log2fasta -name ".$name."q".$q."w -q ".$q." -indel ".$q." ".$outprefix."_".$type."_2_final_endo.log > ".$outprefix."_".$type."_final_endo.q".$q.".fa; else ".
"if [ -e ".$outprefix."_".$type."_1_endo.log ]; then tail -n+2 ".$outprefix."_".$type."_1_endo.log |cut -f 6| awk 'BEGIN{sum=0;}{ sum+=\$\$1; }END{print sum/NR}' > ".$outprefix."_".$type."_final_endo.cov; ".$installDir."/log2fasta -name ".$name."q".$q."w -q ".$q." -indel ".$q." ".$outprefix."_".$type."_1_endo.log > ".$outprefix."_".$type."_final_endo.q".$q.".fa; else echo \"error\"; fi; fi; fi; fi; fi; fi;\n\n";
}
}
my @arrayOfTargetsHSD;
for (my $q=10;$q<=50;$q+=20) {
foreach my $type (@typeOfTargets){
push(@arrayOfTargets, $outprefix."_".$type."_final_endo.q".$q.".hsd");
push(@arrayOfTargetsClean, $outprefix."_".$type."_final_endo.q".$q.".hsd");
push(@arrayOfTargetsHSD, $outprefix."_".$type."_final_endo.q".$q.".hsd");
$stringToPrint.="".$outprefix."_".$type."_final_endo.q".$q.".hsd: ".$outprefix."_".$type."_final_endo.q".$q.".fa\n\tpython ".$installDirToFastaHaplogrep."/fasta2haplogrep.py ".$outprefix."_".$type."_final_endo.q".$q.".fa > ".$outprefix."_".$type."_final_endo.q".$q.".hsd\n\n";
}
}
push(@arrayOfTargets, $name."_results.txt");
push(@arrayOfTargetsClean, $name."_results.txt");
$stringToPrint.="".$name."_results.txt: ".join(" ",@arrayOfTargetsHSD)."\n\t".$installDir."/parseSchmutzi.pl ".$outprefix." ".$name." > ".$name."_results.txt\n\n";
#python ".$installDirToFastaHaplogrep."/fasta2haplogrep.py ".$name."_".$type."_final_endo.q".$q.".fa > ".$name."_".$type."_final_endo.q".$q.".hsd\n\n";
}
print "SHELL := /bin/bash\n\nall:\t".join(" ",@arrayOfTargets)."\n\nclean:\n\trm -vf ".join(" ",@arrayOfTargetsClean)."\n\n".$stringToPrint."\n";