annotate SMART/DiffExpAnal/fastq_groomer_parallel.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
1 import sys, os, optparse, random
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
2 from galaxy_utils.sequence.fastq import fastqReader, fastqVerboseErrorReader, fastqAggregator, fastqWriter
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 def stop_err(msg):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 sys.stderr.write("%s\n" % msg)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 sys.exit()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8 def main():
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 input_filename = sys.argv[1] #a txt file
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 input_type = sys.argv[2]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 output_filename = sys.argv[3] #a txt file
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 output_type = sys.argv[4]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 force_quality_encoding = sys.argv[5]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 summarize_input = sys.argv[6] == 'summarize_input'
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 pairedEnd_input = sys.argv[7]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 if pairedEnd_input == 'None':
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 pairedEnd_input = None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 output_pairedEndFileName = sys.argv[8]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 if force_quality_encoding == 'None':
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 force_quality_encoding = None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 #Parse the input txt file and read a list of fastq files
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 file = open(input_filename, "r")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27 lines = file.readlines()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 inputFileNames = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 outGroomerNames = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 resDirName = os.path.dirname(output_filename) + "/"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 #Write output txt file and define all output groomer file names
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32 outFile = open(output_filename, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 for line in lines:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 tab = line.split()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 inputFileNames.append(tab[1])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 outGroomerName = resDirName + tab[0] + '_outGroomer_%s.fastq' % random.randrange(0, 10000)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 outGroomerNames.append(outGroomerName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 outFile.write(tab[0] + '\t' + outGroomerName + '\n')
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 outFile.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 file.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 if pairedEnd_input != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 inPairedFile = open(pairedEnd_input, "r")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 lines = inPairedFile.readlines()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 inputPairedEndFileNames = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 outGroomerPairedEndNames = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 outPairedEndFile = open(output_pairedEndFileName, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 for line in lines:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 tab = line.split()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 inputPairedEndFileNames.append(tab[1])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 outGroomerPairedEndName = resDirName + tab[0] + '_outGroomer_pairedEnd_%s.fastq' % random.randrange(0, 10000)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 outGroomerPairedEndNames.append(outGroomerPairedEndName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 outPairedEndFile.write(tab[0] + '\t' + outGroomerPairedEndName + '\n')
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 outPairedEndFile.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 inPairedFile.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 # Write output file
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 aggregator = fastqAggregator()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 for i in range(len(outGroomerNames)):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 out = fastqWriter( open( outGroomerNames[i], 'wb' ), format = output_type, force_quality_encoding = force_quality_encoding )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 read_count = None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 if summarize_input:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 reader = fastqVerboseErrorReader
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 reader = fastqReader
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 for read_count, fastq_read in enumerate( reader( open( inputFileNames[i] ), format = input_type, apply_galaxy_conventions = True ) ):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 if summarize_input:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 aggregator.consume_read( fastq_read )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 out.write( fastq_read )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 out.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 if read_count is not None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 print "Groomed %i %s reads into %s reads." % ( read_count + 1, input_type, output_type )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 if input_type != output_type and 'solexa' in [ input_type, output_type ]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 print "Converted between Solexa and PHRED scores."
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 if summarize_input:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 print "Based upon quality and sequence, the input data is valid for: %s" % ( ", ".join( aggregator.get_valid_formats() ) or "None" )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 ascii_range = aggregator.get_ascii_range()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 decimal_range = aggregator.get_decimal_range()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 print "Input ASCII range: %s(%i) - %s(%i)" % ( repr( ascii_range[0] ), ord( ascii_range[0] ), repr( ascii_range[1] ), ord( ascii_range[1] ) ) #print using repr, since \x00 (null) causes info truncation in galaxy when printed
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 print "Input decimal range: %i - %i" % ( decimal_range[0], decimal_range[1] )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 print "No valid FASTQ reads were provided."
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 # Write output pairedEnd file
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87 if pairedEnd_input != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 aggregator = fastqAggregator()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 for i in range(len(outGroomerPairedEndNames)):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 outPair = fastqWriter(open(outGroomerPairedEndNames[i], 'wb'), format = output_type, force_quality_encoding = force_quality_encoding)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 read_count = None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 if summarize_input:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 reader = fastqVerboseErrorReader
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95 reader = fastqReader
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 for read_count, fastq_reader in enumerate(reader(open(inputPairedEndFileNames[i]), format=input_type, apply_galaxy_conventions=True)):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 if summarize_input:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 aggregator.consume_read(fastq_read)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 outPair.write(fastq_read)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 outPair.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 if read_count is not None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 print "Groomed %i %s reads into %s reads." % ( read_count + 1, input_type, output_type )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104 if input_type != output_type and 'solexa' in [ input_type, output_type ]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 print "Converted between Solexa and PHRED scores."
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 if summarize_input:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107 print "Based upon quality and sequence, the input data is valid for: %s" % ( ", ".join( aggregator.get_valid_formats() ) or "None" )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 ascii_range = aggregator.get_ascii_range()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 decimal_range = aggregator.get_decimal_range()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 print "Input ASCII range: %s(%i) - %s(%i)" % ( repr( ascii_range[0] ), ord( ascii_range[0] ), repr( ascii_range[1] ), ord( ascii_range[1] ) ) #print using repr, since \x00 (null) causes info truncation in galaxy when printed
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 print "Input decimal range: %i - %i" % ( decimal_range[0], decimal_range[1] )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113 print "No valid paired-end FASTQ reads were provided."
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 if __name__ == "__main__": main()