0
|
1 #!/usr/bin/perl
|
10
|
2 #v1.1.0 manage empty files
|
|
3 #v1.0.4 bug correction, last read not considered
|
8
|
4 #v1.0.3 support rapsodyn header (.... 1:... / .... 2:...)
|
|
5 #V1.0.2 added auto type detection
|
7
|
6 #V1.0.1 added log, option parameters
|
0
|
7 use strict;
|
|
8 use warnings;
|
7
|
9 use Getopt::Long;
|
0
|
10
|
7
|
11 my $read1_file;
|
|
12 my $read2_file;
|
|
13 my $log_file;
|
|
14 my $output1_file;
|
|
15 my $output2_file;
|
0
|
16
|
7
|
17 my $TYPE="sanger";
|
|
18 my $MIN_LENGTH=30;
|
|
19 my $MIN_QUALITY=30;
|
|
20
|
|
21 my $VERBOSE = "OFF";
|
0
|
22
|
7
|
23 GetOptions (
|
|
24 "read1_file=s" => \$read1_file,
|
|
25 "read2_file=s" => \$read2_file,
|
|
26 "log_file=s" => \$log_file,
|
|
27 "output1_file=s" => \$output1_file,
|
|
28 "output2_file=s" => \$output2_file,
|
|
29 "type=s" => \$TYPE,
|
|
30 "min_length=i" => \$MIN_LENGTH,
|
|
31 "min_quality=i" => \$MIN_QUALITY,
|
|
32 "verbose=s" => \$VERBOSE
|
|
33 ) or die("Error in command line arguments\n");
|
0
|
34
|
|
35
|
7
|
36 my $nb_read1=0;
|
|
37 my $nb_base_read1=0;
|
|
38 my $nb_read2=0;
|
|
39 my $nb_base_read2=0;
|
0
|
40
|
7
|
41 my $nb_read1_t=0;
|
|
42 my $nb_base_read1_t=0;
|
|
43 my $nb_read2_t=0;
|
|
44 my $nb_base_read2_t=0;
|
|
45
|
|
46 my $nb_base_current_t=0;
|
|
47
|
|
48 open(READ1, $read1_file) or die ("Can't open $read1_file\n");
|
|
49 open(READ2, $read2_file) or die ("Can't open $read2_file\n");
|
|
50 open(OUT1, ">$output1_file") or die ("Can't open $output1_file\n");
|
|
51 open(OUT2, ">$output2_file") or die ("Can't open $output2_file\n");
|
8
|
52 open (LF,">$log_file") or die("Can't open $log_file\n");
|
|
53
|
10
|
54 if (( -z READ1)&&( -z READ2)){
|
|
55 exit(0);
|
|
56 }
|
|
57 elsif (( -z READ1)||( -z READ2)){
|
|
58 print STDERR "One empty File\n";
|
|
59 exit(0);
|
|
60 }
|
|
61
|
0
|
62
|
|
63 my $error1=0;
|
|
64 my $error2=0;
|
|
65 my $error3=0;
|
|
66 my $error4=0;
|
|
67 my $error5=0;
|
|
68 my $error6=0;
|
|
69 my $error7=0;
|
|
70 my $error8=0;
|
|
71 my $error9=0;
|
|
72 my $error10=0;
|
|
73
|
8
|
74 my $auto_type="";
|
|
75 my %qual;
|
|
76 if ($TYPE eq "auto"){
|
|
77 my $compt=0;
|
|
78 open(DETECT, $read1_file) or die ("Can't open $read1_file\n");
|
|
79 while (my $ligne1_r1 =<DETECT>){
|
|
80 my $ligne2_r1 =<DETECT>;
|
|
81 my $ligne3_r1 =<DETECT>;
|
|
82 my $ligne4_r1 =<DETECT>;
|
|
83 $compt++;
|
|
84 if ($ligne4_r1 =~ /^(.*)\s*$/i){
|
|
85 my $qual = $1;
|
|
86 my @q = split(//,$qual);
|
|
87 for (my $i=0;$i<=$#q;$i++){
|
|
88 my $num = ord($q[$i]);
|
|
89 if ($qual{$num}){
|
|
90 $qual{$num}++;
|
|
91 }
|
|
92 else {
|
|
93 $qual{$num} = 1;
|
|
94 }
|
|
95 #range sanger / illumina 1.8+ : 33->94
|
|
96 #range illumina 1.3->1.7 : 64->105
|
|
97 if ($num > 94){$auto_type = "illumina";last;}
|
|
98 if ($num < 64){$auto_type = "sanger";last;}
|
|
99 }
|
|
100 }
|
|
101 else {
|
|
102 print STDERR "Error in format detection : quality not recognized\n$ligne4_r1";
|
|
103 exit(0);
|
|
104 }
|
|
105
|
|
106 if ($auto_type ne ""){
|
|
107 last;
|
|
108 }
|
|
109
|
|
110 }
|
|
111 close (DETECT);
|
|
112 if ($auto_type eq ""){
|
|
113 print STDERR "Error in format detection : type not recognized parsing read1\n";
|
|
114 foreach my $key (sort {$a <=> $b} keys %qual){
|
|
115 print "$key\t:\t",$qual{$key},"\n";
|
|
116 }
|
|
117 exit(0);
|
|
118 }
|
|
119 else {
|
|
120 $TYPE = $auto_type;
|
|
121 }
|
|
122 }
|
|
123
|
|
124
|
|
125
|
10
|
126 my $compt=0;
|
0
|
127 while (my $ligne1_r1 =<READ1>){
|
|
128 my $ligne2_r1 =<READ1>;
|
|
129 my $ligne3_r1 =<READ1>;
|
|
130 my $ligne4_r1 =<READ1>;
|
|
131 my $ligne1_r2 =<READ2>;
|
|
132 my $ligne2_r2 =<READ2>;
|
|
133 my $ligne3_r2 =<READ2>;
|
|
134 my $ligne4_r2 =<READ2>;
|
10
|
135 # chomp($ligne1_r1);
|
|
136 # chomp($ligne2_r1);
|
|
137 # chomp($ligne3_r1);
|
|
138 # chomp($ligne4_r1);
|
|
139 # chomp($ligne2_r1);
|
|
140
|
|
141 $compt++;
|
7
|
142 $nb_read1++;
|
|
143 $nb_read2++;
|
0
|
144
|
|
145 #@ 1 sec
|
|
146 if ((!$ligne1_r1)||(!$ligne2_r1)||(!$ligne3_r1)||(!$ligne4_r1)||(!$ligne1_r2)||(!$ligne2_r2)||(!$ligne3_r2)||(!$ligne4_r2)){
|
|
147 if ($VERBOSE eq "ON"){
|
|
148 print "Error in file format";
|
|
149 if ($ligne1_r1){print $ligne1_r1;}
|
|
150 if ($ligne2_r1){print $ligne2_r1;}
|
|
151 if ($ligne3_r1){print $ligne3_r1;}
|
|
152 if ($ligne4_r1){print $ligne4_r1;}
|
|
153 if ($ligne1_r2){print $ligne1_r2;}
|
|
154 if ($ligne2_r2){print $ligne2_r2;}
|
|
155 if ($ligne3_r2){print $ligne3_r2;}
|
|
156 if ($ligne4_r2){print $ligne4_r2;}
|
|
157 print "\n";
|
|
158 }
|
|
159 $error1++;
|
|
160 }
|
|
161 elsif(($ligne1_r1 !~/^\@/)||($ligne1_r2 !~/^\@/)||($ligne3_r1 !~/^\+/)||($ligne3_r2 !~/^\+/)){
|
|
162 if ($VERBOSE eq "ON"){
|
|
163 print "Error in header : format\n";
|
|
164 print $ligne1_r1;
|
|
165 print $ligne2_r1;
|
|
166 print $ligne3_r1;
|
|
167 print $ligne4_r1;
|
|
168 print $ligne1_r2;
|
|
169 print $ligne2_r2;
|
|
170 print $ligne3_r2;
|
|
171 print $ligne4_r2;
|
|
172 print "\n";
|
|
173 }
|
|
174 $error2++;
|
|
175 }
|
|
176 #@ 1 - 2 sec
|
|
177 else {
|
|
178
|
10
|
179 my $length_seq1 = length(chomp($ligne2_r1));
|
|
180 my $length_qual1 =length(chomp($ligne4_r1));
|
0
|
181 my $seq1;
|
|
182 my $qual1;
|
|
183
|
10
|
184 my $length_seq2 = length(chomp($ligne2_r2));
|
|
185 my $length_qual2 =length(chomp($ligne4_r2));
|
0
|
186 my $seq2;
|
|
187 my $qual2;
|
|
188 my $header1="";
|
|
189 my $header2="";
|
|
190 my $repheader1="";
|
|
191 my $repheader2="";
|
7
|
192
|
0
|
193
|
8
|
194 if ($ligne1_r1 =~/^\@(.*?)[\s\/]/){
|
0
|
195 $header1 = $1;
|
|
196 }
|
|
197
|
8
|
198 if ($ligne3_r1 =~/^\+(.*?)[\s\/]/){
|
0
|
199 $repheader1 = $1;
|
|
200 }
|
|
201
|
8
|
202 if ($ligne1_r2 =~/^\@(.*?)[\s\/]/){
|
0
|
203 $header2 = $1;
|
|
204 }
|
|
205
|
8
|
206 if ($ligne3_r2 =~/^\+(.*?)[\s\/]/){
|
0
|
207 $repheader2 = $1;
|
|
208 }
|
|
209 #@ 2 sec
|
|
210
|
|
211 ### Verification de la coherence sequence /qualité @ 1 sec
|
|
212 if (($TYPE eq "illumina")&&((!$header1)||(!$header2)||(!$repheader1)||(!$repheader2))){
|
|
213 if ($VERBOSE eq "ON"){
|
|
214 print "Error in header : empty\n";
|
|
215 print $ligne1_r1;
|
|
216 print $ligne2_r1;
|
|
217 print $ligne3_r1;
|
|
218 print $ligne4_r1;
|
|
219 print $ligne1_r2;
|
|
220 print $ligne2_r2;
|
|
221 print $ligne3_r2;
|
|
222 print $ligne4_r2;
|
|
223 print "\n";
|
|
224 }
|
|
225 $error3++;
|
|
226 }
|
|
227 elsif (($TYPE eq "sanger")&&((!$header1)||(!$header2))){
|
|
228 if ($VERBOSE eq "ON"){
|
10
|
229 print "Error in header ref : empty\n";
|
0
|
230 print $ligne1_r1;
|
|
231 print $ligne2_r1;
|
|
232 print $ligne3_r1;
|
|
233 print $ligne4_r1;
|
|
234 print $ligne1_r2;
|
|
235 print $ligne2_r2;
|
|
236 print $ligne3_r2;
|
|
237 print $ligne4_r2;
|
|
238 print "\n";
|
|
239 }
|
|
240 $error3++;
|
|
241 }
|
|
242 elsif (($TYPE eq "illumina")&&(($header1 ne $repheader1)||($header2 ne $repheader2)||($header1 ne $header2))){
|
|
243 if ($VERBOSE eq "ON"){
|
|
244 print "Error in header : different\n";
|
|
245 print $ligne1_r1;
|
|
246 print $ligne2_r1;
|
|
247 print $ligne3_r1;
|
|
248 print $ligne4_r1;
|
|
249 print $ligne1_r2;
|
|
250 print $ligne2_r2;
|
|
251 print $ligne3_r2;
|
|
252 print $ligne4_r2;
|
|
253 print "\n";
|
|
254 }
|
|
255 $error4++;
|
|
256 }
|
|
257 elsif (($TYPE eq "sanger")&&($header1 ne $header2)){
|
|
258 if ($VERBOSE eq "ON"){
|
|
259 print "Error in header : different\n";
|
|
260 print $ligne1_r1;
|
|
261 print $ligne2_r1;
|
|
262 print $ligne3_r1;
|
|
263 print $ligne4_r1;
|
|
264 print $ligne1_r2;
|
|
265 print $ligne2_r2;
|
|
266 print $ligne3_r2;
|
|
267 print $ligne4_r2;
|
|
268 print "\n";
|
|
269 }
|
|
270 $error4++;
|
|
271 }
|
|
272 elsif (($length_seq1 != $length_qual1)||($length_seq2 != $length_qual2)){
|
|
273 if ($VERBOSE eq "ON"){
|
|
274 print "Error in seq/qual length\n";
|
10
|
275 print "$length_seq1 / $length_qual1 \t $length_seq2 / $length_qual2\n";
|
0
|
276 print $ligne1_r1;
|
|
277 print $ligne2_r1;
|
|
278 print $ligne3_r1;
|
|
279 print $ligne4_r1;
|
10
|
280 print "\n";
|
0
|
281 print $ligne1_r2;
|
|
282 print $ligne2_r2;
|
|
283 print $ligne3_r2;
|
|
284 print $ligne4_r2;
|
|
285 print "\n";
|
|
286 }
|
|
287 $error5++;
|
|
288 }
|
|
289 #@ 1 - 2 sec
|
|
290 else {
|
10
|
291 #print "TEST : $compt\n";
|
0
|
292 ### Parsing sequence & qualité
|
|
293 if ($ligne2_r1 =~ /^([ATGCNX]+)\s*$/i){
|
|
294 $seq1 = $1;
|
7
|
295 $nb_base_read1 += length($seq1);
|
0
|
296 }
|
|
297 if ($ligne2_r2 =~ /^([ATGCNX]+)\s*$/i){
|
|
298 $seq2 = $1;
|
7
|
299 $nb_base_read2 += length($seq2);
|
0
|
300 }
|
|
301 if ($ligne4_r1 =~ /^(.*)\s*$/i){
|
|
302 $qual1 = $1;
|
|
303 }
|
|
304 if ($ligne4_r2 =~ /^(.*)\s*$/i){
|
|
305 $qual2 = $1;
|
|
306 }
|
|
307 #@ 2 sec
|
|
308 ### Verification du parsing et de la coherence sequence /qualité (n°2)
|
|
309 if ((!$seq1)||(!$seq2)||(!$qual1)||(!$qual2)){
|
|
310 if ($VERBOSE eq "ON"){
|
|
311 print "Error parsing seq / quality \n";
|
|
312 print $ligne1_r1;
|
|
313 print $ligne2_r1;
|
|
314 print $ligne3_r1;
|
|
315 print $ligne4_r1;
|
|
316 print $ligne1_r2;
|
|
317 print $ligne2_r2;
|
|
318 print $ligne3_r2;
|
|
319 print $ligne4_r2;
|
|
320 print "\n";
|
|
321 }
|
|
322 $error6++;
|
|
323 }
|
|
324 elsif ((length($seq1) != length($qual1))||(length($seq2) != length($qual2))){
|
|
325 if ($VERBOSE eq "ON"){
|
|
326 print "Error in seq/qual length after parsing\n";
|
|
327 print $ligne1_r1;
|
|
328 print $ligne2_r1;
|
|
329 print $ligne3_r1;
|
|
330 print $ligne4_r1;
|
|
331 print $ligne1_r2;
|
|
332 print $ligne2_r2;
|
|
333 print $ligne3_r2;
|
|
334 print $ligne4_r2;
|
|
335 print "\n";
|
|
336 }
|
|
337 $error7++;
|
|
338 }
|
|
339 #@ <1 sec
|
|
340 else {
|
|
341 my $fastq_lines_r1="";
|
|
342 my $fastq_lines_r2="";
|
7
|
343 my $nb_base_current_read1_t = 0;
|
|
344 my $nb_base_current_read2_t = 0;
|
|
345
|
0
|
346 $fastq_lines_r1 = &grooming_and_trimming($ligne1_r1,$seq1,$qual1);
|
7
|
347 $nb_base_current_read1_t = $nb_base_current_t;
|
0
|
348 if ($fastq_lines_r1){
|
|
349 $fastq_lines_r2 = &grooming_and_trimming($ligne1_r2,$seq2,$qual2);
|
7
|
350 $nb_base_current_read2_t = $nb_base_current_t;
|
0
|
351 }
|
|
352 if ($fastq_lines_r2){
|
|
353 print OUT1 $fastq_lines_r1;
|
|
354 print OUT2 $fastq_lines_r2;
|
7
|
355
|
|
356 $nb_read1_t++;
|
|
357 $nb_read2_t++;
|
|
358 $nb_base_read1_t += $nb_base_current_read1_t;
|
|
359 $nb_base_read2_t += $nb_base_current_read2_t;
|
|
360
|
|
361
|
0
|
362 }
|
|
363 }
|
|
364 }
|
|
365
|
7
|
366
|
0
|
367 #@ 7 sec
|
|
368 }
|
|
369 }
|
|
370
|
|
371 close (READ1);
|
|
372 close (READ2);
|
|
373 close (OUT1);
|
|
374 close (OUT2);
|
|
375
|
7
|
376 print LF "\n####\t Fastq preparation \n";
|
8
|
377 print LF "Fastq format : $TYPE\n";
|
7
|
378 print LF "## Before preparation\n";
|
|
379 print LF "#Read1 :\t$nb_read1\t#Base :\t$nb_base_read1\n";
|
|
380 print LF "#Read2 :\t$nb_read2\t#Base :\t$nb_base_read2\n";
|
|
381 print LF "## After preparation\n";
|
|
382 print LF "#Read1 :\t$nb_read1_t\t#Base :\t$nb_base_read1_t\n";
|
|
383 print LF "#Read2 :\t$nb_read2_t\t#Base :\t$nb_base_read2_t\n";
|
|
384 close (LF);
|
|
385
|
0
|
386
|
|
387 sub grooming_and_trimming{
|
|
388 my $header = shift;
|
|
389 my $seq = shift;
|
|
390 my $quality = shift;
|
|
391 my $quality_converted="";
|
5
|
392 my $quality_ori=$quality;
|
0
|
393
|
5
|
394 my $lengthseq = length($seq);
|
|
395 my $startTrim = 0;
|
|
396 my $stopTrim = length($quality)-1;
|
|
397 my $startnoN = $startTrim;
|
|
398 my $stopnoN = $stopTrim;
|
0
|
399
|
|
400
|
|
401 my $chercheN = $seq;
|
5
|
402 my @bad_position_N;
|
|
403 my @bad_position_Q;
|
0
|
404 my $current_index = index($chercheN,"N");
|
|
405 my $abs_index = $current_index;
|
|
406 while ($current_index >=0){
|
5
|
407 push (@bad_position_N,$abs_index);
|
0
|
408
|
|
409 if ($current_index<length($seq)){
|
|
410 $chercheN = substr($chercheN,$current_index+1);
|
|
411 $current_index = index($chercheN,"N");
|
5
|
412 $abs_index = $current_index + $bad_position_N[$#bad_position_N]+1;
|
0
|
413 }
|
|
414 else {
|
|
415 last;
|
|
416 }
|
|
417 }
|
5
|
418
|
|
419 my @q = split(//,$quality);
|
|
420 for (my $i=0;$i<=$#q;$i++){
|
|
421 my $chr = $q[$i];
|
|
422 my $num = ord($q[$i]);
|
|
423 if ($TYPE eq "illumina"){
|
|
424 $num = $num - 31; # 31 comme la difference entre la plage sanger (33-> 93 / 0->60) et illumina (64->104 / 0->40)
|
|
425 $quality_converted .= chr($num);
|
|
426 }
|
|
427
|
|
428 if ($num < $MIN_QUALITY + 33){ #33 comme le départ de la plage sanger
|
|
429 push(@bad_position_Q,$i);
|
|
430 }
|
|
431 }
|
|
432 if ($quality_converted){$quality = $quality_converted;}
|
0
|
433
|
5
|
434 my @bad_position = (@bad_position_N, @bad_position_Q);
|
0
|
435
|
|
436 if ($#bad_position>=0){
|
5
|
437 @bad_position = sort {$a <=> $b} @bad_position;
|
|
438 my %coord=%{&extract_longer_string_coordinates_from_bad_position(0,$stopTrim,\@bad_position)};
|
|
439 $startTrim = $coord{"start"};
|
|
440 $stopTrim = $coord{"stop"};
|
|
441 #print "$startTrim .. $stopTrim\n";
|
0
|
442
|
5
|
443 }
|
|
444 my $lengthTrim = $stopTrim - $startTrim +1;
|
7
|
445
|
|
446 #if ($stats_length{$lengthTrim}){
|
|
447 # $stats_length{$lengthTrim} = 1;
|
|
448 #}
|
|
449 #else {
|
|
450 # $stats_length{$lengthTrim}++;
|
|
451 #}
|
5
|
452 my $fastq_lines="";
|
|
453
|
|
454 # if ($header =~ /GA8\-EAS671_0005\:3\:1\:1043\:4432/){
|
|
455 # print "HEAD:\t$header";
|
|
456 # print "SEQ:\n$seq\n";
|
|
457 # print "$quality_ori\n";
|
|
458 # print "$quality\n";
|
|
459 # for (my $i=0;$i<=$#bad_position;$i++){
|
|
460 # print $bad_position[$i]."(".$q[$bad_position[$i]]." : ".ord($q[$bad_position[$i]]).")"."\t";
|
|
461 # }
|
|
462 # print "\n";
|
|
463 # print "$startTrim .. $stopTrim / $lengthTrim \n";
|
|
464 # print $fastq_lines;
|
|
465 # print "\n";
|
|
466 # }
|
|
467
|
7
|
468 #for (my $i=$startTrim;$i<=$stopTrim;$i++){
|
|
469 # if ($stats_quality{ord($q{$i])}){
|
|
470 # $stats_quality{ord($q{$i])}=1;
|
|
471 # }
|
|
472 # else {
|
|
473 # $stats_quality{ord($q{$i])}++;
|
|
474 # }
|
|
475 #}
|
|
476
|
5
|
477 if ($lengthTrim >= $MIN_LENGTH){
|
|
478 $fastq_lines .= $header;
|
7
|
479 my $new_seq = substr($seq,$startTrim,$lengthTrim);
|
|
480 $nb_base_current_t = length($new_seq);
|
|
481 $fastq_lines .= $new_seq."\n";
|
5
|
482 $fastq_lines .= "+\n";
|
7
|
483 my $new_q = substr($quality,$startTrim,$lengthTrim);
|
|
484 $fastq_lines .= $new_q."\n";
|
5
|
485 return $fastq_lines;
|
0
|
486
|
|
487 }
|
|
488 else {
|
5
|
489 #print "Insufficient length after trimming\n";
|
0
|
490 return "";
|
|
491 }
|
|
492 }
|
|
493
|
|
494 sub extract_longer_string_coordinates_from_bad_position{
|
|
495 my $start=shift;
|
|
496 my $stop =shift;
|
|
497 my $refbad = shift;
|
|
498 my @bad_position = @$refbad;
|
|
499 my %coord;
|
|
500
|
|
501 my $current_start = $start;
|
|
502 my $current_stop = $bad_position[0]-1;
|
|
503 if ($current_stop < $start){$current_stop = $start;}
|
|
504
|
|
505
|
|
506 #debut -> premier N
|
|
507 my $current_length = $current_stop - $current_start +1;
|
|
508 my $test_length;
|
|
509
|
|
510 #entre les N
|
|
511 for (my $i=1;$i<=$#bad_position;$i++){
|
|
512 $test_length = $bad_position[$i]+1-$bad_position[$i-1]-1;
|
|
513 if ( $test_length > $current_length){
|
|
514 $current_start = $bad_position[$i-1]+1;
|
|
515 $current_stop = $bad_position[$i]-1;
|
|
516 $current_length = $current_stop - $current_start +1;
|
|
517 }
|
|
518 }
|
|
519
|
|
520 #dernier N -> fin
|
|
521 $test_length = $stop-$bad_position[$#bad_position]+1;
|
|
522 if ( $test_length > $current_length){
|
|
523 $current_start = $bad_position[$#bad_position]+1;
|
|
524 if ($current_start > $stop){$current_start=$stop;}
|
|
525 $current_stop = $stop;
|
|
526 }
|
|
527 $coord{"start"}=$current_start;
|
|
528 $coord{"stop"}= $current_stop;
|
|
529 $coord{"lenght"}=$current_stop-$current_start+1;
|
|
530
|
|
531 return \%coord;
|
|
532 }
|