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