annotate redup.pl @ 0:df1e7c7dd9cb draft default tip

Initial uploaded of files
author jgarbe
date Wed, 27 Nov 2013 14:39:56 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
1 #!/usr/bin/perl -w
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
2
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
3 ###########################################################
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
4 # redup.pl
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
5 # John Garbe
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
6 # August 2012
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
7 #
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
8 # Remove exact duplicate reads from paired-end fastq files, printing out
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
9 # unique reads to .unique files. Prints out top N most duplicated sequences
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
10 #
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
11 ###########################################################
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
12
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
13 =head1 NAME
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
14
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
15 redup.pl - Remove exact duplicate reads from paired-end fastq files, printing out unique reads to .unique files. Prints out top N most duplicated sequences in fasta format
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
16
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
17 =head1 SYNOPSIS
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
18
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
19 redup.pl [-n N] sample1_R1.fastq sample1_R2.fastq > topdups.fasta
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
20
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
21 =head1 DESCRIPTION
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
22
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
23 This script removes duplicate paired-end reads from the input files sample1_R1.fastq and sample1_R2.fastq and prints out unique reads to the files sample1_R1.fastq.unique and sample2_R2.fastq.unique. Reads must have the exact same sequence to be called duplicates, quality scores are ignored. The top N (default 20) most duplicated sequences are printed out in fasta format, making it convenient for using BLAST to identify them.
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
24
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
25 =cut
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
26
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
27 use Getopt::Std;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
28
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
29 # determine number of most duplicated sequences to return
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
30 $opt_n = 20;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
31 $ok = getopts('n:');
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
32 $k = $opt_n - 1; # subtract one to work with Perl's zero-based arrays
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
33
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
34 die "USAGE: redup.pl [-n N] sample1_R1.fastq sample1_R2.fastq\n" unless ((($#ARGV == 1) || ($#ARGV == 3)) && $ok);
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
35
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
36 # open up input files
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
37 open F1, "<$ARGV[0]" or die "cannot open $ARGV[0]\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
38 open F2, "<$ARGV[1]" or die "cannot open $ARGV[1]\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
39
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
40 # open up output files
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
41 open O1, ">$ARGV[0].unique" or die "cannot open $ARGV[0].unique\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
42 open O2, ">$ARGV[1].unique" or die "cannot open $ARGV[1].unique\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
43
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
44 # open up output files
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
45 if ($#ARGV < 3) {
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
46 open O1, ">$ARGV[0].unique" or die "cannot open $ARGV[0].unique\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
47 open O2, ">$ARGV[1].unique" or die "cannot open $ARGV[1].unique\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
48 } else {
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
49 open O1, ">$ARGV[2]" or die "cannot open $ARGV[2]\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
50 open O2, ">$ARGV[3]" or die "cannot open $ARGV[3]\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
51 }
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
52
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
53
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
54 # run through the input files
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
55 $readcount = 0;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
56 $dupcount = 0;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
57 while ($f1line1 = <F1>) {
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
58 $readcount++;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
59 if ($readcount % 1000000 == 0) { # print progress update
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
60 print STDERR "$readcount reads processed, $dupcount duplicates removed\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
61 }
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
62 $f1line2 = <F1>;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
63 $f1line3 = <F1>;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
64 $f1line4 = <F1>;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
65
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
66 $f2line1 = <F2>;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
67 $f2line2 = <F2>;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
68 $f2line3 = <F2>;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
69 $f2line4 = <F2>;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
70
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
71 $combo = $f1line2 . $f2line2; # concatenate seqs from reads 1 and 2
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
72 if (defined($seen{$combo})) { # if seq is a dup skip it
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
73 $seen{$combo}++;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
74 $dupcount++;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
75 next;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
76 } else { # seq is not a dup, print reads back out
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
77 $seen{$combo} = 1;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
78 print O1 "$f1line1";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
79 print O1 "$f1line2";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
80 print O1 "$f1line3";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
81 print O1 "$f1line4";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
82
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
83 print O2 "$f2line1";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
84 print O2 "$f2line2";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
85 print O2 "$f2line3";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
86 print O2 "$f2line4";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
87 }
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
88 }
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
89
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
90 close O1;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
91 close O2;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
92 print STDERR "$readcount reads processed, $dupcount duplicates removed\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
93
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
94 print STDERR "Identifying most frequent sequences (kill program to skip this step)\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
95
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
96 ### print out the top k most duplicated reads in fasta format
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
97 # It would be better to use a proper partial sort algorithm here
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
98
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
99 # initialize first k elements
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
100 for $i (0..$k) {
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
101 $result[$i][0] = "seed";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
102 $result[$i][1] = -1;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
103 }
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
104
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
105 # find reads with more duplicates than least duplicated sequence in results
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
106 for $key (keys %seen) {
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
107
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
108 if ($seen{$key} > $result[$#result][1]) {
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
109 push @result, [ ( $key, $seen{$key} ) ];
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
110 @result = sort { $b->[1] <=> $a->[1] } @result;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
111 $#result = $k;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
112 }
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
113 }
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
114
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
115 # print out the top k most duplicated reads in fasta format
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
116 for $i (0..$k) {
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
117 next if ($result[$i][1] < 0);
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
118 $rank = $i + 1;
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
119 print ">Rank:$rank:Count:$result[$i][1]\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
120 ($r1, $r2) = split /\n/, $result[$i][0];
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
121 print $r1 . "N" . "$r2\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
122 }
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
123
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
124 # print out total again in case it was missed
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
125 print STDERR "$readcount reads processed, $dupcount duplicates removed\n";
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
126
df1e7c7dd9cb Initial uploaded of files
jgarbe
parents:
diff changeset
127 exit;