annotate tools/next_gen_conversion/solid_to_fastq.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -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 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Converts SOLiD data to Sanger FASTQ format.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 usage: %prog [options]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 -i, --input1=i: Forward reads file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 -q, --input2=q: Forward qual file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 -I, --input3=I: Reverse reads file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 -Q, --input4=Q: Reverse qual file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 -o, --output1=o: Forward output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 -r, --output2=r: Reverse output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 usage: %prog forward_reads_file forwards_qual_file reverse_reads_file(or_None) reverse_qual_file(or_None) output_file ouptut_id output_dir
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 import os, sys, tempfile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 sys.stderr.write( "%s\n" % msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 def replaceNeg1(fin, fout):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 line = fin.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 while line.strip():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 fout.write(line.replace('-1', '1'))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 line = fin.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 fout.seek(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 return fout
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 #Parse Command Line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 # common temp file setup
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 tmpf = tempfile.NamedTemporaryFile() #forward reads
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 tmpqf = tempfile.NamedTemporaryFile()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 tmpqf = replaceNeg1(file(options.input2,'r'), tmpqf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 # if paired-end data (have reverse input files)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 if options.input3 != "None" and options.input4 != "None":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 tmpr = tempfile.NamedTemporaryFile() #reverse reads
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 # replace the -1 in the qualities file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 tmpqr = tempfile.NamedTemporaryFile()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 tmpqr = replaceNeg1(file(options.input4,'r'), tmpqr)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 cmd1 = "%s/bwa_solid2fastq_modified.pl 'yes' %s %s %s %s %s %s 2>&1" %(os.path.split(sys.argv[0])[0], tmpf.name, tmpr.name, options.input1, tmpqf.name, options.input3, tmpqr.name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 os.system(cmd1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 os.system('gunzip -c %s >> %s' %(tmpf.name,options.output1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 os.system('gunzip -c %s >> %s' %(tmpr.name,options.output2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 except Exception, eq:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 stop_err("Error converting data to fastq format.\n" + str(eq))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 tmpr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 tmpqr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 # if single-end data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 cmd1 = "%s/bwa_solid2fastq_modified.pl 'no' %s %s %s %s %s %s 2>&1" % (os.path.split(sys.argv[0])[0], tmpf.name, None, options.input1, tmpqf.name, None, None)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 os.system(cmd1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 os.system('gunzip -c %s >> %s' % (tmpf.name, options.output1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 except Exception, eq:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 stop_err("Error converting data to fastq format.\n" + str(eq))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 tmpqf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 tmpf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 sys.stdout.write('converted SOLiD data')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 if __name__=="__main__": __main__()