annotate fastq_combiner.py @ 1:02c665f7ac2b draft

planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
author devteam
date Tue, 13 Oct 2015 12:42:26 -0400
parents 82f441097692
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
1 #Dan Blankenberg
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
2 import sys, os, shutil
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
3 from galaxy_utils.sequence.fastq import fastqWriter, fastqSequencingRead, fastqCombiner, fastqFakeFastaScoreReader
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
4 from galaxy_utils.sequence.fasta import fastaReader, fastaNamedReader
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
5
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
6 def main():
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
7 #Read command line arguments
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
8 fasta_filename = sys.argv[1]
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
9 fasta_type = sys.argv[2] or 'fasta' #should always be fasta or csfasta? what if txt?
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
10 qual_filename = sys.argv[3]
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
11 qual_type = sys.argv[4] or 'qualsanger' #qual454 qualsolid
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
12 output_filename = sys.argv[5]
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
13 force_quality_encoding = sys.argv[6]
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
14 if force_quality_encoding == 'None':
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
15 force_quality_encoding = None
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
16
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
17 format = 'sanger'
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
18 if fasta_type == 'csfasta' or qual_type == 'qualsolid':
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
19 format = 'cssanger'
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
20 elif qual_type == 'qualsolexa':
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
21 format = 'solexa'
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
22 elif qual_type == 'qualillumina':
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
23 format = 'illumina'
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
24
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
25 out = fastqWriter( open( output_filename, 'wb' ), format = format, force_quality_encoding = force_quality_encoding )
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
26 if qual_filename == 'None':
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
27 qual_input = fastqFakeFastaScoreReader( format, quality_encoding = force_quality_encoding )
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
28 else:
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
29 qual_input = fastaNamedReader( open( qual_filename, 'rb' ) )
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
30
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
31 fastq_combiner = fastqCombiner( format )
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
32 i = None
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
33 skip_count = 0
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
34 for i, sequence in enumerate( fastaReader( open( fasta_filename, 'rb' ) ) ):
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
35 quality = qual_input.get( sequence )
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
36 if quality:
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
37 fastq_read = fastq_combiner.combine( sequence, quality )
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
38 out.write( fastq_read )
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
39 else:
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
40 skip_count += 1
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
41 out.close()
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
42 if i is None:
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
43 print "Your file contains no valid FASTA sequences."
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
44 else:
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
45 print qual_input.has_data()
82f441097692 Imported from capsule None
devteam
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 )
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
47
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
48 if __name__ == "__main__":
82f441097692 Imported from capsule None
devteam
parents:
diff changeset
49 main()