comparison deseq/differential_expression_analysis_pipeline_for_rnaseq_data-a03838a6eb54/DiffExpAnal/fastq_groomer_parallel.py @ 10:6e573fd3c41b draft

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