annotate tools/fastq/fastq_paired_end_joiner.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 import sys, os, shutil
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 from galaxy_utils.sequence.fastq import fastqReader, fastqNamedReader, fastqWriter, fastqJoiner
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 #Read command line arguments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 input1_filename = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 input1_type = sys.argv[2] or 'sanger'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 input2_filename = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 input2_type = sys.argv[4] or 'sanger'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 output_filename = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 if input1_type != input2_type:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 print "WARNING: You are trying to join files of two different types: %s and %s." % ( input1_type, input2_type )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 input2 = fastqNamedReader( open( input2_filename, 'rb' ), input2_type )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 joiner = fastqJoiner( input1_type )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 out = fastqWriter( open( output_filename, 'wb' ), format = input1_type )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 i = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 skip_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 for i, fastq_read in enumerate( fastqReader( open( input1_filename, 'rb' ), format = input1_type ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 identifier = joiner.get_paired_identifier( fastq_read )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 fastq_paired = input2.get( identifier )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 if fastq_paired is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 skip_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 out.write( joiner.join( fastq_read, fastq_paired ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 if i is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 print "Your file contains no valid FASTQ reads."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 print input2.has_data()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 print 'Joined %s of %s read pairs (%.2f%%).' % ( i - skip_count + 1, i + 1, float( i - skip_count + 1 ) / float( i + 1 ) * 100.0 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 main()