0
|
1 # -*- coding: utf-8 -*-
|
|
2 """
|
|
3 Wrapper for EDGE - Gene expression in Prokaryotes
|
|
4 Author: Paolo Uva paolo dot uva at crs4 dot it
|
|
5 Date: March 18, 2013
|
|
6 """
|
|
7
|
|
8 import glob
|
|
9 import optparse
|
|
10 import os
|
|
11 import shutil
|
|
12 import subprocess
|
|
13 import tempfile
|
|
14 import sys
|
|
15
|
|
16 def __main__():
|
|
17 # Parse Command Line
|
|
18 parser = optparse.OptionParser()
|
|
19 # Input
|
|
20 parser.add_option('-g', '--genome', dest="genome")
|
|
21 parser.add_option('-p', '--ptt', dest="ptt")
|
|
22 parser.add_option('-r', '--rnt', dest="rnt")
|
|
23 parser.add_option('-u', '--input1', dest="input1")
|
|
24 parser.add_option('-v', '--input2', dest="input2")
|
|
25 parser.add_option('-t', '--num-threads', dest="num_threads", type='int')
|
|
26 parser.add_option('-m', '--minInsertSize', dest="minInsertSize", type='int')
|
|
27 parser.add_option('-M', '--maxInsertSize', dest="maxInsertSize", type='int')
|
|
28 parser.add_option('-w', '--window', dest="window", type='int')
|
|
29 parser.add_option('-i', '--utrSize', dest="utrSize", type='int')
|
|
30 parser.add_option('-x', '--similarity', dest="similarity", type='float')
|
|
31 parser.add_option('-c', '--minCoverage', dest="minCoverage", type='int')
|
|
32 parser.add_option('-l', '--readLength', dest="readLength", type='int')
|
|
33 # Output
|
|
34 parser.add_option('--out-aln', dest="out_aln")
|
|
35 parser.add_option('--out-rpkm', dest="out_rpkm")
|
|
36 parser.add_option('--out-log', dest="out_log")
|
|
37 (options, args) = parser.parse_args()
|
|
38 if len(args) > 0:
|
|
39 parser.error('Wrong number of arguments')
|
|
40
|
|
41 # Build command
|
|
42 if options.input2:
|
|
43 input2_flag = '-v %s' % (options.input2)
|
|
44 else:
|
|
45 input2_flag = ''
|
|
46 if options.num_threads is not None:
|
|
47 num_threads_flag = '-t %d' % options.num_threads
|
|
48 else:
|
|
49 num_threads_flag = ''
|
|
50 if options.minInsertSize is not None:
|
|
51 minInsertSize_flag = '-m %d' % options.minInsertSize
|
|
52 else:
|
|
53 minInsertSize_flag = ''
|
|
54 if options.maxInsertSize is not None:
|
|
55 maxInsertSize_flag = '-M %d' % options.maxInsertSize
|
|
56 else:
|
|
57 maxInsertSize_flag = ''
|
|
58 if options.window is not None:
|
|
59 window_flag = '-w %d' % options.window
|
|
60 else:
|
|
61 window_flag = ''
|
|
62 if options.utrSize is not None:
|
|
63 utrSize_flag = '-i %d' % options.utrSize
|
|
64 else:
|
|
65 utrSize_flag = ''
|
|
66 if options.similarity is not None:
|
|
67 similarity_flag = '-x %s' % options.similarity
|
|
68 else:
|
|
69 similarity_flag = ''
|
|
70 if options.readLength is not None:
|
|
71 readLength_flag = '-l %d' % (options.readLength)
|
|
72 else:
|
|
73 readLength_flag = ''
|
|
74 if options.minCoverage is not None:
|
|
75 minCoverage_flag = '-c %d' % options.minCoverage
|
|
76 else:
|
|
77 minCoverage_flag = ''
|
|
78
|
|
79 wd = tempfile.mkdtemp()
|
|
80 try:
|
|
81 prefix = os.path.join(wd, 'out')
|
|
82 cl = 'edge.pl -g %s -p %s -r %s -u %s -o %s %s %s %s %s %s %s %s %s %s' % (options.genome, options.ptt, options.rnt, options.input1, prefix, input2_flag, num_threads_flag, minInsertSize_flag, maxInsertSize_flag, window_flag, utrSize_flag, similarity_flag, readLength_flag, minCoverage_flag)
|
|
83 print cl
|
|
84
|
|
85 if options.out_log:
|
|
86 sout = open(options.out_log, 'w')
|
|
87 else:
|
|
88 sout = sys.stdout
|
|
89 try:
|
|
90 subprocess.check_call(cl, stdout=sout, stderr=subprocess.STDOUT, shell=True) # need to redirect stderr because edge.pl calls bowtie2 and count which write some logging info there
|
|
91 finally:
|
|
92 if sout != sys.stdout:
|
|
93 sout.close()
|
|
94
|
|
95 # Move alignment file
|
|
96 shutil.move(prefix + '.alignments', options.out_aln)
|
|
97 # Concatenate multiple RPKM files together
|
|
98 with open(options.out_rpkm, 'wb') as destination:
|
|
99 for filename in glob.iglob(prefix + '.rpkm_*'):
|
|
100 with open(filename, 'rb') as source:
|
|
101 shutil.copyfileobj(source, destination)
|
|
102 finally:
|
|
103 shutil.rmtree(wd)
|
|
104
|
|
105
|
|
106 if __name__ == "__main__":
|
|
107 __main__()
|