0
|
1 #!/usr/bin/perl
|
7
|
2 #V1.0.0
|
0
|
3 use strict;
|
|
4 use Getopt::Long;
|
|
5
|
|
6 #
|
|
7 # Filter a pileup file on forward/reverse presence and %read having the variant
|
|
8 # The error code
|
|
9 # 1 : multiple variant type detected insertion/deletion/mutation
|
|
10 # 1i : inconsistency in insertion
|
|
11 # 1d : inconsistency in deletion
|
|
12 # 1m : inconsistency in mutation
|
|
13 # 2 : insufficient depth
|
|
14 # 3 : insufficient variant frequency
|
|
15 # 4 : variant position not covered by forward and reverse reads
|
|
16 # 5 : variant with other variant in neighbourhood
|
|
17 # 6 : too much depth
|
|
18 # 8 : parsing error (couldn't parse the mpileup line correctly)
|
|
19 # 9 : parsing error (couldn't parse the readbase string correctly)
|
|
20
|
|
21
|
|
22 my $inputfile;
|
|
23 my $logfile;
|
|
24 my $MIN_DISTANCE=0;
|
|
25 my $MIN_VARIANTFREQUENCY=0;
|
|
26 my $MIN_FORWARDREVERSE=0;
|
|
27 my $MIN_DEPTH=0;
|
|
28 my $MAX_DEPTH=500;
|
|
29 my $VERBOSE=0;
|
|
30 my $ONLY_UNFILTERED_VARIANT="OFF";
|
|
31 my $DO_STAT="NO";
|
|
32
|
7
|
33 my $nb_variant_checked=0;
|
|
34 my $nb_variant_selected=0;
|
|
35
|
0
|
36
|
|
37 my $STAT_MIN_DEPTH_MIN = 2;
|
|
38 my $STAT_MIN_DEPTH_MAX = 10;
|
|
39 my $STAT_MIN_DEPTH_STEP = 2;
|
|
40 my $STAT_MAX_DEPTH_MIN = 100;
|
|
41 my $STAT_MAX_DEPTH_MAX = 200;
|
|
42 my $STAT_MAX_DEPTH_STEP = 100;
|
|
43 my $STAT_FREQ_MIN = 0.8;
|
|
44 my $STAT_FREQ_MAX = 1;
|
|
45 my $STAT_FREQ_STEP = 0.1;
|
|
46 my $STAT_DIST_MIN = 0;
|
|
47 my $STAT_DIST_MAX = 50;
|
|
48 my $STAT_DIST_STEP = 50;
|
|
49
|
|
50 GetOptions (
|
|
51 "input_file=s" => \$inputfile,
|
|
52 "log_file=s" => \$logfile,
|
|
53 "min_depth=i" => \$MIN_DEPTH,
|
|
54 "max_depth=i" => \$MAX_DEPTH,
|
|
55 "min_frequency=f" => \$MIN_VARIANTFREQUENCY,
|
|
56 "min_distance=i" => \$MIN_DISTANCE,
|
|
57 "min_forward_and_reverse=i" => \$MIN_FORWARDREVERSE,
|
|
58 "variant_only=s" => \$ONLY_UNFILTERED_VARIANT,
|
|
59 "v=i" => \$VERBOSE,
|
|
60 "do_stat=s" => \$DO_STAT,
|
|
61 "stat_min_depth_min=i" => \$STAT_MIN_DEPTH_MIN,
|
|
62 "stat_min_depth_max=i" => \$STAT_MIN_DEPTH_MAX,
|
|
63 "stat_min_depth_step=i" => \$STAT_MIN_DEPTH_STEP,
|
|
64 "stat_max_depth_min=i" => \$STAT_MAX_DEPTH_MIN,
|
|
65 "stat_max_depth_max=i" => \$STAT_MAX_DEPTH_MAX,
|
|
66 "stat_max_depth_step=i" => \$STAT_MAX_DEPTH_STEP,
|
|
67 "stat_freq_min=f" => \$STAT_FREQ_MIN,
|
|
68 "stat_freq_max=f" => \$STAT_FREQ_MAX,
|
|
69 "stat_freq_step=f" => \$STAT_FREQ_STEP,
|
|
70 "stat_dist_min=i" => \$STAT_DIST_MIN,
|
|
71 "stat_dist_max=i" => \$STAT_DIST_MAX,
|
|
72 "stat_dist_step=i" => \$STAT_DIST_STEP
|
|
73 ) or die("Error in command line arguments\n");
|
|
74
|
|
75 open(IF, $inputfile) or die("Can't open $inputfile\n");
|
|
76
|
|
77 my @tbl_line;
|
|
78 my %USR_PARAM;
|
|
79 $USR_PARAM{"min_depth"} = $MIN_DEPTH;
|
|
80 $USR_PARAM{"max_depth"} = $MAX_DEPTH;
|
|
81 $USR_PARAM{"min_freq"} = $MIN_VARIANTFREQUENCY;
|
|
82 $USR_PARAM{"min_dist"} = $MIN_DISTANCE;
|
|
83 $USR_PARAM{"min_fr"} = $MIN_FORWARDREVERSE;
|
|
84
|
|
85
|
|
86
|
|
87 #Extraction des variants
|
|
88 my $nb_line=0;
|
|
89 while (my $line=<IF>){
|
7
|
90 $nb_variant_checked++;
|
0
|
91 $nb_line++;
|
|
92 if (($nb_line % 1000000 == 0)&&($VERBOSE==1)){
|
|
93 print "$nb_line\n";
|
|
94 }
|
|
95 my $error_code=0;
|
|
96 if ($line=~/(.*?)\s+(\d+)\s+([ATGCN])\s+(\d+)\s+(.*?)\s+(.*?)$/){
|
|
97 my $current_chromosome = $1;
|
|
98 my $current_position = $2;
|
|
99 my $current_refbase = $3;
|
|
100 my $current_coverage = $4;
|
|
101 my $current_readbase_string = $5;
|
|
102 my $current_quality_string = $6;
|
|
103
|
|
104 #Suppression of mPileUp special character
|
|
105 $current_readbase_string =~ s/\$//g; #the read start at this position
|
|
106 $current_readbase_string =~ s/\^.//g; #the read end at this position followed by quality char
|
|
107
|
|
108 if ($current_readbase_string =~ /[ATGCNatgcn\d]/){
|
|
109 my %variant;
|
|
110 $variant{"line"} = $line;
|
|
111 $variant{"chr"} = $current_chromosome;
|
|
112 $variant{"pos"} = $current_position;
|
|
113 $variant{"refbase"} = $current_refbase;
|
|
114 $variant{"coverage"} = $current_coverage;
|
|
115 $variant{"readbase"} = $current_readbase_string;
|
|
116 $variant{"quality"} = $current_quality_string;
|
|
117 push(@tbl_line,\%variant);
|
|
118
|
|
119 if ($ONLY_UNFILTERED_VARIANT eq "ON"){
|
|
120 print $line;
|
|
121 }
|
|
122
|
|
123 }
|
|
124 else {
|
|
125 #Position with no variant
|
|
126 }
|
|
127
|
|
128 }
|
|
129 else {
|
|
130 #Error Parsing
|
|
131 print STDERR "$line #8";
|
|
132 }
|
|
133 }
|
|
134 close(IF);
|
|
135
|
|
136 if ($ONLY_UNFILTERED_VARIANT eq "ON"){
|
|
137 exit(0);
|
|
138 }
|
|
139
|
|
140 ####Checking the distance between variant and other filter
|
|
141
|
|
142
|
|
143 my @error;
|
|
144 for (my $i=0;$i<=$#tbl_line;$i++){
|
|
145 # print "ligne : $tbl_line[$i]\n";
|
|
146 my $before="";
|
|
147 my $after="";
|
|
148 my %line = %{$tbl_line[$i]};
|
|
149
|
|
150 if ($tbl_line[$i-1]){
|
|
151 $before = $tbl_line[$i-1];
|
|
152 }
|
|
153 if ($tbl_line[$i+1]){
|
|
154 $after = $tbl_line[$i+1];
|
|
155 }
|
|
156 my $error_code = check_error($tbl_line[$i],$before,$after,\%USR_PARAM);
|
|
157 if ($error_code == 0){
|
|
158 print $line{"line"};
|
7
|
159 $nb_variant_selected++;
|
0
|
160 }
|
|
161 else {
|
|
162 push(@error,$error_code,"\t",$line{"line"});
|
|
163 }
|
|
164 }
|
|
165
|
|
166 ### LOG
|
|
167 open(LF,">$logfile") or die ("Can't open $logfile\n");
|
7
|
168 print LF "\n####\t MPileup filtering \n";
|
|
169 print LF "Variant checked :\t$nb_variant_checked\n";
|
|
170 if ($DO_STAT eq "NO"){
|
|
171 print LF "Variant selected :\t$nb_variant_selected\n";
|
|
172 }
|
|
173 elsif ($DO_STAT eq "YES"){
|
0
|
174 for (my $idx_min_depth=$STAT_MIN_DEPTH_MIN;$idx_min_depth<=$STAT_MIN_DEPTH_MAX;$idx_min_depth = $idx_min_depth + $STAT_MIN_DEPTH_STEP ){
|
|
175 for (my $idx_max_depth=$STAT_MAX_DEPTH_MIN;$idx_max_depth<=$STAT_MAX_DEPTH_MAX;$idx_max_depth = $idx_max_depth + $STAT_MAX_DEPTH_STEP ){
|
|
176 for (my $idx_freq = $STAT_FREQ_MIN;$idx_freq<=$STAT_FREQ_MAX;$idx_freq= $idx_freq+$STAT_FREQ_STEP){
|
|
177 for (my $idx_dist=$STAT_DIST_MIN;$idx_dist<=$STAT_DIST_MAX;$idx_dist = $idx_dist + $STAT_DIST_STEP){
|
|
178 for (my $idx_fr=0;$idx_fr<=1;$idx_fr++){
|
|
179 my %stat_param;
|
|
180 $stat_param{"min_depth"}=$idx_min_depth;
|
|
181 $stat_param{"max_depth"}=$idx_max_depth;
|
|
182 $stat_param{"min_freq"}=$idx_freq;
|
|
183 $stat_param{"min_fr"}=$idx_fr;
|
|
184 $stat_param{"min_dist"}=$idx_dist;
|
|
185
|
|
186 print LF "#SNP = ",&test_check(\@tbl_line,\%stat_param),"\tdepth (min/max) = ",$stat_param{"min_depth"}," / ",$stat_param{"max_depth"},"\tmin_dist=",$stat_param{"min_dist"},"\tmin_freq=",$stat_param{"min_freq"},"\tmin_forwardreverse = ",$stat_param{"min_fr"},"\n";
|
|
187 }
|
|
188 }
|
|
189 }
|
|
190 print "\n";
|
|
191 }
|
|
192 }
|
|
193 }
|
|
194
|
|
195
|
7
|
196 #for (my $i=0;$i<=$#error;$i++){
|
|
197 # print LF $error[$i];
|
|
198 #}
|
0
|
199 close (LF);
|
|
200
|
|
201
|
|
202
|
|
203
|
|
204 sub test_check{
|
|
205 my $ref_tbl_line = shift;
|
|
206 my $ref_param = shift;
|
|
207 my @tbl_line = @$ref_tbl_line;
|
|
208 my %param = %$ref_param;
|
|
209 my $nb=0;
|
|
210
|
|
211 for (my $i=0;$i<=$#tbl_line;$i++){
|
|
212 my $before="";
|
|
213 my $after="";
|
|
214 my %line = %{$tbl_line[$i]};
|
|
215
|
|
216 if ($tbl_line[$i-1]){
|
|
217 $before = $tbl_line[$i-1];
|
|
218 }
|
|
219 if ($tbl_line[$i+1]){
|
|
220 $after = $tbl_line[$i+1];
|
|
221 }
|
|
222 my $error_code = check_error($tbl_line[$i],$before,$after,\%param);
|
|
223 if ($error_code == 0){
|
|
224 $nb++;
|
|
225 }
|
|
226 }
|
|
227
|
|
228 return $nb;
|
|
229 }
|
|
230
|
|
231 sub check_error{
|
|
232 my $refline = shift;
|
|
233 my %line = %$refline;
|
|
234 my $refbefore = shift;
|
|
235 my $refafter = shift;
|
|
236 my $refparam = shift;
|
|
237 my %param = %$refparam;
|
|
238
|
|
239
|
|
240 my $current_chromosome = $line{"chr"};
|
|
241 my $current_position = $line{"pos"};
|
|
242 my $current_refbase = $line{"refbase"};
|
|
243 my $current_coverage = $line{"coverage"};
|
|
244 my $current_readbase_string = $line{"readbase"};
|
|
245
|
|
246
|
|
247 my $min_depth = $param{"min_depth"};
|
|
248 my $max_depth = $param{"max_depth"};
|
|
249 my $min_variant_frequency = $param{"min_freq"};
|
|
250 my $min_forward_reverse = $param{"min_fr"};
|
|
251 my $min_dist = $param{"min_dist"};
|
|
252
|
|
253 #Verification of neightbourhood
|
|
254 if ($refbefore){
|
|
255 my %compareline = %$refbefore;
|
|
256 my $compare_chromosome = $compareline{"chr"};
|
|
257 my $compare_position = $compareline{"pos"};
|
|
258 my $compare_refbase = $compareline{"refbase"};
|
|
259 my $compare_coverage = $compareline{"coverage"};
|
|
260 my $compare_readbase_string = $compareline{"readbase"};
|
|
261
|
|
262 if (($current_chromosome eq $compare_chromosome )&&($compare_position + $min_dist >= $current_position)){
|
|
263 return 5;
|
|
264 }
|
|
265 }
|
|
266
|
|
267 if ($refafter){
|
|
268 my %compareline = %$refafter;
|
|
269 my $compare_chromosome = $compareline{"chr"};
|
|
270 my $compare_position = $compareline{"pos"};
|
|
271 my $compare_refbase = $compareline{"refbase"};
|
|
272 my $compare_coverage = $compareline{"coverage"};
|
|
273 my $compare_readbase_string = $compareline{"readbase"};
|
|
274
|
|
275 if (($current_chromosome eq $compare_chromosome )&&($current_position + $min_dist >= $compare_position)){
|
|
276 return 5;
|
|
277 }
|
|
278 }
|
|
279
|
|
280
|
|
281
|
|
282
|
|
283 #Extraction of insertions
|
|
284
|
|
285 ##################################################################
|
|
286 # my @IN = $current_readbase_string =~ m/\+[0-9]+[ACGTNacgtn]+/g;
|
|
287 # my @DEL = $current_readbase_string =~ m/\-[0-9]+[ACGTNacgtn]+/g;
|
|
288 # print "IN : @IN\n";
|
|
289 # print "DEL :@DEL\n";
|
|
290 #$current_readbase_string=~s/[\+\-][0-9]+[ACGTNacgtn]+//g;
|
|
291 ##################################################################
|
|
292 #!!! marche pas : exemple .+1Ct. correspond a . / +1C / t /. mais le match de l'expression vire +1Ct
|
|
293 ##################################################################
|
|
294
|
|
295 # => parcours de boucle
|
|
296 my @readbase = split(//,$current_readbase_string);
|
|
297 my $cleaned_readbase_string="";
|
|
298 my @IN;
|
|
299 my @DEL;
|
|
300 my $current_IN="";
|
|
301 my $current_DEL="";
|
|
302 my $current_size=0;
|
|
303
|
|
304 for (my $i=0;$i<=$#readbase;$i++){
|
|
305 if ($readbase[$i] eq "+"){
|
|
306 #Ouverture de IN
|
|
307 $current_IN="+";
|
|
308
|
|
309 #Recuperation de la taille
|
|
310 my $sub = substr $current_readbase_string,$i;
|
|
311 if ($sub=~/^\+(\d+)/){
|
|
312 $current_size = $1;
|
|
313 }
|
|
314 my $remaining_size = $current_size;
|
|
315 while (($remaining_size>0)&&($i<=$#readbase)){
|
|
316 $i++;
|
|
317 $current_IN.=$readbase[$i];
|
|
318 if ($readbase[$i]=~ /[ATGCNatgcn]/){
|
|
319 $remaining_size--;
|
|
320 }
|
|
321 }
|
|
322 push(@IN,$current_IN);
|
|
323 }
|
|
324 elsif ($readbase[$i] eq "-"){
|
|
325 #Ouverture de DEL
|
|
326 $current_DEL="-";
|
|
327
|
|
328 #Recuperation de la taille
|
|
329 my $sub = substr $current_readbase_string,$i;
|
|
330 if ($sub=~/^\-(\d+)/){
|
|
331 $current_size = $1;
|
|
332 }
|
|
333 my $remaining_size = $current_size;
|
|
334 while (($remaining_size>0)&&($i<=$#readbase)){
|
|
335 $i++;
|
|
336 $current_DEL.=$readbase[$i];
|
|
337 if ($readbase[$i]=~ /[ATGCNatgcn]/){
|
|
338 $remaining_size--;
|
|
339 }
|
|
340 }
|
|
341 push(@DEL,$current_DEL);
|
|
342
|
|
343 }
|
|
344 else {
|
|
345 #Ajout a la string
|
|
346 $cleaned_readbase_string .= $readbase[$i];
|
|
347 }
|
|
348 }
|
|
349
|
|
350
|
|
351 # print "IN : @IN\n";
|
|
352 # print "DEL :@DEL\n";
|
|
353 # print "$cleaned_readbase_string\n";
|
|
354
|
|
355 my @current_readbase_array = split(//,$cleaned_readbase_string);
|
|
356
|
|
357 #Filtering : error detection
|
|
358
|
|
359 if ($#current_readbase_array+1 != $current_coverage){
|
|
360 return 9;
|
|
361 #parsing error (couldn't parse the readbase string correctly)
|
|
362 }
|
|
363 elsif ($current_coverage<$min_depth){
|
|
364 return 2;
|
|
365 # 2 : insufficient depth
|
|
366 }
|
|
367 elsif ($current_coverage>$max_depth){
|
|
368 return 6;
|
|
369 # 6 : too much depth
|
|
370 }
|
|
371 else {
|
|
372 if ($#IN>=0){
|
|
373 if (($cleaned_readbase_string=~/[ACGTNacgtn]/)){
|
|
374 return 1;
|
|
375 # 1 : variant type overload (multiple variant type detected insertion/deletion/mutation)
|
|
376 }
|
|
377 else {
|
|
378 ########## TEST de coherence des insertions ################
|
|
379 # for (my $i=0;$i<=$#IN;$i++){
|
|
380 # if (uc($IN[0]) ne uc($IN[$i])){
|
|
381 # print uc($IN[0]),"\n";
|
|
382 # print uc($IN[$i]),"\n";
|
|
383 # return "1i";
|
|
384 # }
|
|
385 # }
|
|
386 ###########################################################
|
|
387
|
|
388 if($#IN+1 < $current_coverage*$min_variant_frequency ){
|
|
389 return 3;
|
|
390 # 3 : insufficient variant frequency
|
|
391 }
|
|
392 }
|
|
393 }
|
|
394 elsif ($#DEL>=0){
|
|
395 if (($cleaned_readbase_string=~/[ACGTNacgtn]/)){
|
|
396 return 1;
|
|
397 # 1 : variant type overload (multiple variant type detected insertion/deletion/mutation)
|
|
398 }
|
|
399 else {
|
|
400 ########## TEST de coherence des deletions ################
|
|
401 # for (my $i=0;$i<=$#DEL;$i++){
|
|
402 # if (uc($DEL[0]) ne uc($DEL[$i])){
|
|
403 # print uc($DEL[0]),"\n";
|
|
404 # print uc($DEL[$i]),"\n";
|
|
405 # return "1d";
|
|
406 # }
|
|
407 # }
|
|
408 ###########################################################
|
|
409
|
|
410 if($#DEL+1 < $current_coverage*$min_variant_frequency){
|
|
411 return 3;
|
|
412 # 3 : insufficient variant frequency
|
|
413 }
|
|
414 }
|
|
415 }
|
|
416 else {
|
|
417 my $nbA=0;
|
|
418 $nbA++ while ($current_readbase_string =~ m/A/g);
|
|
419 my $nbC=0;
|
|
420 $nbC++ while ($current_readbase_string =~ m/C/g);
|
|
421 my $nbT=0;
|
|
422 $nbT++ while ($current_readbase_string =~ m/T/g);
|
|
423 my $nbG=0;
|
|
424 $nbG++ while ($current_readbase_string =~ m/G/g);
|
|
425 my $nbN=0;
|
|
426 $nbN++ while ($current_readbase_string =~ m/N/g);
|
|
427 my $nba=0;
|
|
428 $nba++ while ($current_readbase_string =~ m/a/g);
|
|
429 my $nbc=0;
|
|
430 $nbc++ while ($current_readbase_string =~ m/c/g);
|
|
431 my $nbt=0;
|
|
432 $nbt++ while ($current_readbase_string =~ m/t/g);
|
|
433 my $nbg=0;
|
|
434 $nbg++ while ($current_readbase_string =~ m/g/g);
|
|
435 my $nbn=0;
|
|
436 $nbn++ while ($current_readbase_string =~ m/n/g);
|
|
437
|
|
438 if (($nbA+$nba>0)&&($nbT+$nbt+$nbG+$nbg+$nbC+$nbc+$nbN+$nbn>0)){
|
|
439 return "1m";
|
|
440 }
|
|
441 if (($nbT+$nbt>0)&&($nbA+$nba+$nbG+$nbg+$nbC+$nbc+$nbN+$nbn>0)){
|
|
442 return "1m";
|
|
443 }
|
|
444 if (($nbG+$nbg>0)&&($nbA+$nba+$nbT+$nbt+$nbC+$nbc+$nbN+$nbn>0)){
|
|
445 return "1m";
|
|
446 }
|
|
447 if (($nbC+$nbc>0)&&($nbA+$nba+$nbT+$nbt+$nbG+$nbg+$nbN+$nbn>0)){
|
|
448 return "1m";
|
|
449 }
|
|
450 if (($nbN+$nbn>0)&&($nbA+$nba+$nbT+$nbt+$nbG+$nbg+$nbC+$nbc>0)){
|
|
451 return "1m";
|
|
452 }
|
|
453
|
|
454 if ($nbA+$nba >= $current_coverage*$min_variant_frequency){
|
|
455 if (($nbA<$min_forward_reverse)||($nba<$min_forward_reverse)){
|
|
456 return 4;
|
|
457 # 4 : variant position not covered by forward and reverse reads
|
|
458 }
|
|
459 }
|
|
460 elsif ($nbT+$nbt >= $current_coverage*$min_variant_frequency){
|
|
461 if (($nbT<$min_forward_reverse)||($nbt<$min_forward_reverse)){
|
|
462 return 4;
|
|
463 # 4 : variant position not covered by forward and reverse reads
|
|
464 }
|
|
465 }
|
|
466 elsif ($nbG+$nbg >= $current_coverage*$min_variant_frequency){
|
|
467 if (($nbG<$min_forward_reverse)||($nbg<$min_forward_reverse)){
|
|
468 return 4;
|
|
469 # 4 : variant position not covered by forward and reverse reads
|
|
470 }
|
|
471 }
|
|
472 elsif ($nbC+$nbc >= $current_coverage*$min_variant_frequency){
|
|
473 if (($nbC<$min_forward_reverse)||($nbc<$min_forward_reverse)){
|
|
474 return 4;
|
|
475 # 4 : variant position not covered by forward and reverse reads
|
|
476 }
|
|
477 }
|
|
478 elsif ($nbN+$nbn >= $current_coverage*$min_variant_frequency){
|
|
479 if (($nbN<$min_forward_reverse)||($nbn<$min_forward_reverse)){
|
|
480 return 4;
|
|
481 # 4 : variant position not covered by forward and reverse reads
|
|
482 }
|
|
483 }
|
|
484 else {
|
|
485 return 3;
|
|
486 # 3 : insufficient variant frequency
|
|
487 }
|
|
488 }
|
|
489 }
|
|
490
|
|
491 return 0;
|
|
492 }
|