annotate resync.pl @ 0:751f4938cf0d

Uploaded
author jjohnson
date Tue, 05 Feb 2013 15:23:18 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
1 #!/usr/bin/perl -w
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
2
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
3 ###########################################################
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
4 # resync.pl
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
5 # John Garbe
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
6 # June 2012
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
7 #
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
8 # Resynchronize a pair of paired-end fastq files. Read in two unsynchronized
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
9 # fastq files and write out two synchronized fastq files. The
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
10 # synchronized files have properly paired reads, with singelton reads removed.
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
11 #
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
12 #
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
13 ###########################################################
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
14
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
15 die "USAGE: resync.pl sample1_R1.fastq sample1_R2.fastq [sample1_R1_synced.fastq] [sample1_R2_synced.fastq]\n" unless ($#ARGV >= 1 && $#ARGV <= 3);
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
16
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
17 # open up input files
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
18 open F1, "<$ARGV[0]" or die "cannot open $ARGV[0]\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
19 open F2, "<$ARGV[1]" or die "cannot open $ARGV[1]\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
20
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
21 # open up output files
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
22 if ($#ARGV > 1) {
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
23 open O1, ">$ARGV[2]" or die "cannot open $ARGV[2]\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
24 } else {
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
25 open O1, ">$ARGV[0].out" or die "cannot open $ARGV[0].out\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
26 }
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
27 if ($#ARGV == 3) {
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
28 open O2, ">$ARGV[3]" or die "cannot open $ARGV[3]\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
29 } else {
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
30 open O2, ">$ARGV[1].out" or die "cannot open $ARGV[1].out\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
31 }
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
32
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
33 $readid = `head -n 1 $ARGV[0]`;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
34 chomp $readid;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
35 if ($readid =~ /^@\S+\/[12]$/) { # @ at the start of the line followed by non-whitespace, a /, a 1 or 2, the end of the line
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
36 $id_type = 1;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
37 print STDERR "Casava 1.7 read id style\n"; # TESTING
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
38 } elsif ($readid =~ /^@\S+\W[12]\S+$/) { # @ at the start of the line followed by non-whitspace, a space, a 1 or 2, non-whitespace
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
39 $id_type = 2;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
40 print STDERR "Casava 1.8 read id style\n"; # TESTING
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
41 } else {
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
42 print STDERR "Cannot determine read id style\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
43 print STDOUT "Unknwon id style: $readid\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
44 exit 1;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
45 }
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
46
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
47
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
48 # read in the first fastq file and store in a hash
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
49 $f1readcount = 0;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
50 while ($f1line1 = <F1>) {
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
51 $f1readcount++;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
52 if ($f1readcount % 1000000 == 0) { # print progress update
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
53 print STDERR "$f1readcount reads processed in first file\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
54 }
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
55 $f1line2 = <F1>;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
56 $f1line3 = <F1>;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
57 $f1line4 = <F1>;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
58
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
59 ($id, $junk) = split /\//, $f1line1;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
60 $r1{$id} = $f1line1 . $f1line2 . $f1line3 . $f1line4;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
61 }
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
62
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
63 # read in the second fastq file, printing out both read pairs if available, otherwise tossing the reads
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
64 $f2readcount = 0;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
65 $f1missed = $f1readcount;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
66 $f2missed = 0;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
67 while ($f2line1 = <F2>) {
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
68 $f2readcount++;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
69 if ($f2readcount % 1000000 == 0) { # print progress update
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
70 print STDERR "$f2readcount reads processed in second file\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
71 }
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
72 $f2line2 = <F2>;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
73 $f2line3 = <F2>;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
74 $f2line4 = <F2>;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
75
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
76 ($id, $junk) = split /\//, $f2line1;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
77 if (defined($r1{$id})) {
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
78 $f1missed--;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
79 print O1 "$r1{$id}";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
80 print O2 "$f2line1";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
81 print O2 "$f2line2";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
82 print O2 "$f2line3";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
83 print O2 "$f2line4";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
84 } else {
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
85 $f2missed++;
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
86 }
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
87 }
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
88
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
89 print "$f1readcount reads in $ARGV[0], $f1missed without mate\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
90 print "$f2readcount reads in $ARGV[1], $f2missed without mate\n";
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
91
751f4938cf0d Uploaded
jjohnson
parents:
diff changeset
92