annotate edena_ovl_wrapper.py @ 0:60609a9cef3b draft

Uploaded
author crs4
date Mon, 09 Sep 2013 05:44:31 -0400
parents
children cd6cc6d76708
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
60609a9cef3b Uploaded
crs4
parents:
diff changeset
1 # -*- coding: utf-8 -*-
60609a9cef3b Uploaded
crs4
parents:
diff changeset
2 """
60609a9cef3b Uploaded
crs4
parents:
diff changeset
3 Edena (overlapping)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
4 version 0.2.1 (andrea.pinna@crs4.it)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
5 """
60609a9cef3b Uploaded
crs4
parents:
diff changeset
6
60609a9cef3b Uploaded
crs4
parents:
diff changeset
7 import optparse
60609a9cef3b Uploaded
crs4
parents:
diff changeset
8 import shutil
60609a9cef3b Uploaded
crs4
parents:
diff changeset
9 import subprocess
60609a9cef3b Uploaded
crs4
parents:
diff changeset
10 import sys
60609a9cef3b Uploaded
crs4
parents:
diff changeset
11
60609a9cef3b Uploaded
crs4
parents:
diff changeset
12 def __main__():
60609a9cef3b Uploaded
crs4
parents:
diff changeset
13 # load arguments
60609a9cef3b Uploaded
crs4
parents:
diff changeset
14 print 'Parsing Edena (overlapping) input options...'
60609a9cef3b Uploaded
crs4
parents:
diff changeset
15 parser = optparse.OptionParser()
60609a9cef3b Uploaded
crs4
parents:
diff changeset
16 parser.add_option('--unpaired_input', dest='unpaired_input', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
17 parser.add_option('--dr_pair_1', dest='dr_pair_1', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
18 parser.add_option('--dr_pair_2', dest='dr_pair_2', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
19 parser.add_option('--rd_pair_1', dest='rd_pair_1', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
20 parser.add_option('--rd_pair_2', dest='rd_pair_2', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
21 parser.add_option('--nThreads', dest='nThreads', type='int', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
22 parser.add_option('--minOlap', dest='minOlap', type='int', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
23 parser.add_option('--readsTruncation', dest='readsTruncation', type='int', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
24 parser.add_option('--output', dest='output', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
25 parser.add_option('--logfile', dest='logfile', help='')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
26 (options, args) = parser.parse_args()
60609a9cef3b Uploaded
crs4
parents:
diff changeset
27 if len(args) > 0:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
28 parser.error('Wrong number of arguments')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
29
60609a9cef3b Uploaded
crs4
parents:
diff changeset
30 # build Edena (overlapping) command to be executed
60609a9cef3b Uploaded
crs4
parents:
diff changeset
31 # unpaired input(s)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
32 if options.unpaired_input:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
33 unpaired_inputs = options.unpaired_input.split('+')[0:-1]
60609a9cef3b Uploaded
crs4
parents:
diff changeset
34 unpaired_input = '-r'
60609a9cef3b Uploaded
crs4
parents:
diff changeset
35 for item in unpaired_inputs:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
36 unpaired_input += ' %s' % (item)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
37 else:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
38 unpaired_input = ''
60609a9cef3b Uploaded
crs4
parents:
diff changeset
39 # direct-reverse paired-end files
60609a9cef3b Uploaded
crs4
parents:
diff changeset
40 if options.dr_pair_1 and options.dr_pair_2:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
41 dr_pairs_1 = options.dr_pair_1.split('+')[0:-1]
60609a9cef3b Uploaded
crs4
parents:
diff changeset
42 dr_pairs_2 = options.dr_pair_2.split('+')[0:-1]
60609a9cef3b Uploaded
crs4
parents:
diff changeset
43 dr_pairs = '-DRpairs'
60609a9cef3b Uploaded
crs4
parents:
diff changeset
44 for i in xrange(len(dr_pairs_1)):
60609a9cef3b Uploaded
crs4
parents:
diff changeset
45 dr_pairs += ' %s %s' % (dr_pairs_1[i], dr_pairs_2[i])
60609a9cef3b Uploaded
crs4
parents:
diff changeset
46 else:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
47 dr_pairs = ''
60609a9cef3b Uploaded
crs4
parents:
diff changeset
48 # reverse-direct paired-end files
60609a9cef3b Uploaded
crs4
parents:
diff changeset
49 if options.rd_pair_1 and options.rd_pair_2:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
50 rd_pairs_1 = options.rd_pair_1.split('+')[0:-1]
60609a9cef3b Uploaded
crs4
parents:
diff changeset
51 rd_pairs_2 = options.rd_pair_2.split('+')[0:-1]
60609a9cef3b Uploaded
crs4
parents:
diff changeset
52 rd_pairs = '-RDpairs'
60609a9cef3b Uploaded
crs4
parents:
diff changeset
53 for i in xrange(len(rd_pairs_1)):
60609a9cef3b Uploaded
crs4
parents:
diff changeset
54 rd_pairs += ' %s %s' % (rd_pairs_1[i], rd_pairs_2[i])
60609a9cef3b Uploaded
crs4
parents:
diff changeset
55 else:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
56 rd_pairs = ''
60609a9cef3b Uploaded
crs4
parents:
diff changeset
57 # nThreads
60609a9cef3b Uploaded
crs4
parents:
diff changeset
58 if options.nThreads is not None:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
59 nThreads = '-nThreads %d' % (options.nThreads)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
60 else:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
61 nThreads = ''
60609a9cef3b Uploaded
crs4
parents:
diff changeset
62 # minimum overlap
60609a9cef3b Uploaded
crs4
parents:
diff changeset
63 if options.minOlap is not None:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
64 minOlap = '-M %d' % (options.minOlap)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
65 else:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
66 minOlap = ''
60609a9cef3b Uploaded
crs4
parents:
diff changeset
67 # 3' end reads truncation
60609a9cef3b Uploaded
crs4
parents:
diff changeset
68 if options.readsTruncation is not None:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
69 readsTruncation = '-t %d' % (options.readsTruncation)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
70 else:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
71 readsTruncation = ''
60609a9cef3b Uploaded
crs4
parents:
diff changeset
72 # output file(s)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
73 output = options.output
60609a9cef3b Uploaded
crs4
parents:
diff changeset
74 logfile = options.logfile
60609a9cef3b Uploaded
crs4
parents:
diff changeset
75
60609a9cef3b Uploaded
crs4
parents:
diff changeset
76 # Build Edena (overlapping) command
60609a9cef3b Uploaded
crs4
parents:
diff changeset
77 cmd = 'edena %s %s %s %s %s %s -p galaxy_output' % (unpaired_input, dr_pairs, rd_pairs, nThreads, minOlap, readsTruncation)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
78 print '\nEdena (overlapping) command to be executed: \n %s' % ( cmd )
60609a9cef3b Uploaded
crs4
parents:
diff changeset
79
60609a9cef3b Uploaded
crs4
parents:
diff changeset
80 # Execution of Edena
60609a9cef3b Uploaded
crs4
parents:
diff changeset
81 print 'Executing Edena (overlapping)...'
60609a9cef3b Uploaded
crs4
parents:
diff changeset
82 if logfile:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
83 log = open(logfile, 'w')
60609a9cef3b Uploaded
crs4
parents:
diff changeset
84 else:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
85 log = sys.stdout
60609a9cef3b Uploaded
crs4
parents:
diff changeset
86 try:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
87 subprocess.check_call(cmd, stdout=log, stderr=subprocess.STDOUT, shell=True) # need to redirect stderr because edena writes some logging info there (e.g. "Computing overlaps >=30...")
60609a9cef3b Uploaded
crs4
parents:
diff changeset
88 finally:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
89 if log != sys.stdout:
60609a9cef3b Uploaded
crs4
parents:
diff changeset
90 log.close()
60609a9cef3b Uploaded
crs4
parents:
diff changeset
91 print 'Edena (overlapping) executed!'
60609a9cef3b Uploaded
crs4
parents:
diff changeset
92
60609a9cef3b Uploaded
crs4
parents:
diff changeset
93 shutil.move( "galaxy_output.ovl", output)
60609a9cef3b Uploaded
crs4
parents:
diff changeset
94
60609a9cef3b Uploaded
crs4
parents:
diff changeset
95
60609a9cef3b Uploaded
crs4
parents:
diff changeset
96 if __name__ == "__main__":
60609a9cef3b Uploaded
crs4
parents:
diff changeset
97 __main__()