Skip to content

Commit 7a131a3

Browse files
nikhillordzappo
authored andcommitted
Delly cmo wrapper (#71)
* on local * Added delly * Fixed delly handling of version * More modifications: - fixed bugs - automatically pull the exclusion file based on genome - give the user the option to specify a file/use all regions * Increment version * Fixed a simple bug in delly command generation * Removed artifact file
1 parent 5f7ae19 commit 7a131a3

File tree

3 files changed

+244
-2
lines changed

3 files changed

+244
-2
lines changed

bin/cmo_delly

+241
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
#!/opt/common/CentOS_6-dev/python/python-2.7.10/bin/python
2+
3+
import argparse, os, sys, re, subprocess,itertools
4+
import cmo
5+
6+
class Delly:
7+
def __init__(self,version="default"):
8+
try:
9+
self.Delly_image=cmo.util.programs["delly"][version]
10+
self.version=version
11+
except KeyError, e:
12+
print >>sys.stderr, "Cannot find specified version of delly in configuration file: %s" % version
13+
sys.exit(1)
14+
self.default_args = {}
15+
16+
def delly_cmd(self, command, default_args_override={}, command_specific_args={}):
17+
options = ''
18+
input_files = ''
19+
normal_bam = ''
20+
tumor_bam = ''
21+
genome = ''
22+
cmd_option = ''
23+
cmd = ''
24+
25+
for arg, value in command_specific_args.items():
26+
if arg == 'genome':
27+
genome = ' -g ' + value
28+
elif arg == 'cmd':
29+
cmd_option = value
30+
elif arg == 'version':
31+
continue
32+
elif arg == 'i':
33+
if command == 'merge':
34+
input_files = ' '.join(value)
35+
else:
36+
input_files = value
37+
elif arg == 'normal_bam':
38+
normal_bam = value
39+
elif arg == 'tumor_bam':
40+
tumor_bam = value
41+
elif arg == 'genome_exc':
42+
options = options + ' -x ' + value
43+
else:
44+
#Do not include the false booleans
45+
if type(value) == bool:
46+
if value == False:
47+
continue
48+
elif value == True:
49+
options = options + ' -' + arg
50+
elif value != None:
51+
options = options + ' -' + arg + ' ' + str(value)
52+
if normal_bam and tumor_bam:
53+
input_files = tumor_bam + " " + normal_bam
54+
cmd = [self.Delly_image,cmd_option,options,genome,input_files]
55+
# elif value==True:
56+
# cmd = cmd + [arg + "=" + str(value)]
57+
# elif value != None and value!=False:
58+
# cmd = cmd + [arg + "=" + value]
59+
print >>sys.stderr, " ".join(cmd)
60+
return " ".join(cmd)
61+
62+
def delly_cmd_help(self, command):
63+
cmd = [self.Delly_image, command]
64+
return " ".join(cmd)
65+
66+
def find_sub_command_options(self, sub_command):
67+
cmd = self.delly_cmd_help(sub_command)
68+
delly_output = subprocess.Popen(cmd,stderr=subprocess.PIPE,stdout=subprocess.PIPE,shell=True).communicate()
69+
#look for "Unrecognized command" command, and return the delly help instead of a parsed dict of args
70+
delly_stdout = delly_output[0]
71+
delly_stderr = delly_output[1]
72+
if re.search("Unrecognized command", delly_stderr):
73+
return (None, delly_stderr)
74+
75+
if sub_command == 'help':
76+
return(None, delly_stdout)
77+
78+
valid_tuple_args = []
79+
valid_list_args = []
80+
new_short_option = None;
81+
new_long_option= None
82+
new_description = '';
83+
#print delly_stdout
84+
headers=['Generic options','Discovery options','Genotyping options','Usage','Overlap options','Somatic options','Germline options']
85+
86+
for line in delly_stdout.split("\n"):
87+
arg_short_option = ''
88+
arg_long_option = ''
89+
arg_default = ''
90+
arg_description = ''
91+
arg_type = str
92+
#Check if boolean
93+
m= re.search("\s+(-.) \[ (--\S+) \] (?:arg)? \(?=?([^)]*)\)? +([\S\s]+)$", line)
94+
if m:
95+
arg_short_option = m.group(1)
96+
arg_long_option = m.group(2)
97+
arg_default_str = m.group(3).strip()
98+
arg_description = m.group(4).strip()
99+
100+
#There is no default value
101+
if '=' not in line:
102+
arg_description = arg_default_str + " " + arg_description
103+
arg_default_str = ''
104+
arg_default = None
105+
else:
106+
arg_default_str = arg_default_str.replace('"','')
107+
arg_default = arg_default_str
108+
109+
if 'arg' not in line:
110+
arg_type = bool
111+
elif arg_default_str != '':
112+
try:
113+
arg_default = float(arg_default_str)
114+
arg_type = float
115+
if arg_default.is_integer():
116+
arg_default = int(arg_default)
117+
arg_type = int
118+
except:
119+
pass
120+
121+
list_elem = [arg_short_option, arg_long_option, arg_description, arg_default,arg_type]
122+
valid_list_args.append(list_elem)
123+
else:
124+
# Full description goes to the next line
125+
m= re.search("\s+(-.) \[ (--\S+) \] (?:arg)? \(?=?([^)]*)\)?", line)
126+
if m:
127+
arg_short_option = m.group(1)
128+
arg_long_option = m.group(2)
129+
arg_default_str = m.group(3).strip()
130+
arg_default_str = arg_default_str.replace('"','')
131+
arg_description = ""
132+
if 'arg' not in line:
133+
arg_type = bool
134+
elif arg_default_str != '':
135+
try:
136+
arg_default = float(arg_default_str)
137+
arg_type = float
138+
if arg_default.is_integer():
139+
arg_default = int(arg_default)
140+
arg_type = int
141+
except:
142+
pass
143+
list_elem = [arg_short_option, arg_long_option, arg_description, arg_default,arg_type]
144+
valid_list_args.append(list_elem)
145+
elif len(valid_list_args) != 0:
146+
if not any(single_header in line for single_header in headers):
147+
non_whitespace_line = " " + line.strip()
148+
arg_description = arg_description + non_whitespace_line
149+
valid_list_args[-1][2] = valid_list_args[-1][2] + arg_description
150+
#print valid_list_args
151+
for single_arg in valid_list_args:
152+
tuple_elem = tuple(single_arg)
153+
valid_tuple_args.append(tuple_elem)
154+
155+
#print valid_list_args
156+
#print arg_description
157+
158+
#valid_args.append((new_short_option, new_long_option, new_description))
159+
# for (short, longa, desc) in valid_args:
160+
# print "%s, %s, %s" % (short, longa, desc[0:50])
161+
# sys.exit(1)
162+
return (valid_tuple_args, None)
163+
#return None
164+
165+
166+
167+
if __name__ =='__main__':
168+
list_of_args = sys.argv
169+
preparser = argparse.ArgumentParser(description="run delly", add_help=False)
170+
preparser.add_argument("--version", choices=cmo.util.programs['delly'].keys(), default="default",required=False)
171+
preparser.add_argument("--cmd",required=True,help='delly command, use "--cmd help" for a list of delly commands')
172+
try:
173+
options, _ = preparser.parse_known_args()
174+
except:
175+
preparser.print_help()
176+
exit()
177+
delly_cmd = options.cmd
178+
delly_version = options.version
179+
180+
if '--generate_cwl_tool' in list_of_args:
181+
base_command = ' '.join([sys.argv[0],'--version',delly_version,'--cmd',delly_cmd])
182+
sys.argv = [sys.argv[0], sys.argv[-1],'-b',base_command]
183+
184+
#partially parse options to get version/subcommand and add more options
185+
parser = argparse.ArgumentParser(add_help= True, parents=[preparser], formatter_class=argparse.ArgumentDefaultsHelpFormatter)
186+
187+
#time for monkey business
188+
delly_helper = Delly(version=delly_version)
189+
(sub_command_options, error_msg) = delly_helper.find_sub_command_options(delly_cmd)
190+
if sub_command_options == None:
191+
print >>sys.stderr, error_msg
192+
sys.exit(1)
193+
#ok it was a valid subcommand, add the options we found and the genome option
194+
195+
arg_required = False
196+
required_list = ['-g']
197+
ignore_list = ['-?','-g','-x']
198+
for arg, long_arg, description, default, arg_type in sub_command_options:
199+
if arg not in ignore_list:
200+
if arg in required_list:
201+
arg_required = True
202+
if arg_type == bool:
203+
parser.add_argument("-" + arg, long_arg, action="store_true", help=description, required=arg_required)
204+
elif default == None:
205+
parser.add_argument("-" + arg, long_arg, action="store", help=description, required=arg_required, type=arg_type)
206+
else:
207+
parser.add_argument("-" + arg, long_arg, action="store", help=description, default=default, required=arg_required, type=arg_type)
208+
if arg == '-g':
209+
parser.add_argument("-" + arg,long_arg,help=description,required=arg_required, choices=cmo.util.genomes.keys(), type=arg_type)
210+
if arg == '-x':
211+
parser.add_argument("-" + arg,"--exclude_file", help=description,required=arg_required, type=arg_type)
212+
if delly_cmd == 'call':
213+
parser.add_argument("--normal_bam", action="store", help="Sorted normal bam", required=True)
214+
parser.add_argument("--tumor_bam", action="store", help="Sorted tumor bam", required=True)
215+
elif delly_cmd == 'filter':
216+
parser.add_argument("--i","--input", action="store", help="Input file (.bcf)", required=True)
217+
else:
218+
parser.add_argument("--i","--input", action="store", help="Input files (.bcf)", nargs="+", required=True)
219+
parser.add_argument("--all_regions", help="include regions marked in this genome", action="store_true",default=False)
220+
cmo.util.add_logging_options(parser)
221+
args = parser.parse_args()
222+
command_specific_args = vars(args)
223+
if args.all_regions and args.x:
224+
print >>sys.stderr, "You cannot use all_regions when specifying an exclude file"
225+
sys.exit(1)
226+
if 'g' in args and args.g:
227+
command_specific_args['genome']=cmo.util.genomes[args.g]['fasta']
228+
if not args.all_regions and not args.x:
229+
if 'delly' not in cmo.util.genomes[args.g]:
230+
print >>sys.stderr, "exclude file not included with the " + args.g + " genome. Please use --all_regions"
231+
sys.exit(1)
232+
command_specific_args['genome_exc'] = cmo.util.genomes[args.g]['delly']
233+
del command_specific_args['g']
234+
del command_specific_args['all_regions']
235+
default_args_override = {}
236+
stdout = args.stdout
237+
stderr = args.stderr
238+
cmo.util.remove_logging_options_from_dict(command_specific_args)
239+
#default_args is handled a little crappily, but not a big deal for now
240+
cmo.util.call_cmd(delly_helper.delly_cmd(options.cmd, default_args_override=default_args_override, command_specific_args=command_specific_args), stdout=stdout, stderr=stderr)
241+

cmo/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55

66

7-
__version__ = '1.9.0'
7+
__version__ = '1.9.2'
88

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def get_version():
120120
'bin/cmo_index',
121121
'bin/cmo_fillout',
122122
'bin/cmo_file_of_files',
123-
'bin/cmo_igv_plot'
123+
'bin/cmo_igv_plot',
124+
'bin/cmo_delly'
124125
#'bin/cmo_hotspot3d'
125126
],
126127
zip_safe=False)

0 commit comments

Comments
 (0)