annotate edge_pro.py @ 0:7af33315bc5e draft

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