annotate tools/fastq/fastq_combiner.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
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 fastqWriter, fastqSequencingRead, fastqCombiner, fastqFakeFastaScoreReader
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 from galaxy_utils.sequence.fasta import fastaReader, fastaNamedReader
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 #Read command line arguments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 fasta_filename = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 fasta_type = sys.argv[2] or 'fasta' #should always be fasta or csfasta? what if txt?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 qual_filename = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 qual_type = sys.argv[4] or 'qualsanger' #qual454 qualsolid
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 output_filename = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 force_quality_encoding = sys.argv[6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 if force_quality_encoding == 'None':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 force_quality_encoding = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 format = 'sanger'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 if fasta_type == 'csfasta' or qual_type == 'qualsolid':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 format = 'cssanger'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 elif qual_type == 'qualsolexa':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 format = 'solexa'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 elif qual_type == 'qualillumina':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 format = 'illumina'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 out = fastqWriter( open( output_filename, 'wb' ), format = format, force_quality_encoding = force_quality_encoding )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 if qual_filename == 'None':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 qual_input = fastqFakeFastaScoreReader( format, quality_encoding = force_quality_encoding )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 qual_input = fastaNamedReader( open( qual_filename, 'rb' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 fastq_combiner = fastqCombiner( format )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 i = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 skip_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 for i, sequence in enumerate( fastaReader( open( fasta_filename, 'rb' ) ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 quality = qual_input.get( sequence )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if quality:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 fastq_read = fastq_combiner.combine( sequence, quality )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 out.write( fastq_read )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 skip_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 if i is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 print "Your file contains no valid FASTA sequences."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 print qual_input.has_data()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 print 'Combined %s of %s sequences with quality scores (%.2f%%).' % ( i - skip_count + 1, i + 1, float( i - skip_count + 1 ) / float( i + 1 ) * 100.0 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 main()