annotate fastq_paired_end_joiner.py @ 0:2793d1d765b9 draft

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