PageRenderTime 18ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/next_gen_conversion/solid_to_fastq.py

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