forked from OpenGenomics/muse-tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmuse.py
executable file
·260 lines (226 loc) · 9.93 KB
/
muse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
import sys
import os
import re
import string
import shutil
import logging
import subprocess
import tempfile
from multiprocessing import Pool
from multiprocessing import cpu_count
from argparse import ArgumentParser
from datetime import datetime
def which(cmd):
cmd = ["which",cmd]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
res = p.stdout.readline().rstrip()
if len(res) == 0: return None
return res
def fai_chunk(path, blocksize):
seq_map = {}
with open( path ) as handle:
for line in handle:
tmp = line.split("\t")
seq_map[tmp[0]] = long(tmp[1])
for seq in seq_map:
l = seq_map[seq]
for i in xrange(1, l, blocksize):
yield (seq, i, min(i+blocksize-1, l))
def cmd_caller(cmd):
logging.info("RUNNING: %s" % (cmd))
print "running", cmd
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if len(stderr):
print stderr
return p.returncode
def cmds_runner(cmds, cpus):
p = Pool(cpus)
values = p.map(cmd_caller, cmds, 1)
return values
def call_cmd_iter(muse, ref_seq, block_size, tumor_bam, normal_bam, contamination, output_base):
contamination_str = ""
if contamination is not None:
contamination_str = "-p %s" % (contamination)
template = string.Template("${MUSE} call -f ${REF_SEQ} ${CONTAMINATION} -r '${INTERVAL}' ${TUMOR_BAM} ${NORMAL_BAM} -O ${OUTPUT_BASE}.${BLOCK_NUM}")
for i, block in enumerate(fai_chunk( ref_seq + ".fai", block_size ) ):
cmd = template.substitute(
dict(
REF_SEQ=ref_seq,
CONTAMINATION=contamination_str,
BLOCK_NUM=i,
INTERVAL="%s:%s-%s" % (block[0], block[1], block[2]) ),
MUSE=muse,
TUMOR_BAM=tumor_bam,
NORMAL_BAM=normal_bam,
OUTPUT_BASE=output_base
)
yield cmd, "%s.%s.MuSE.txt" % (output_base, i)
def get_run_id_from_sm_in_bam(bam):
# retrieve the @RG from BAM header
try:
header = subprocess.check_output(['samtools', 'view', '-H', bam])
except Exception as e:
sys.exit('\n%s: Retrieve BAM header failed: %s' % (e, bam))
# get @RG
header_array = header.decode('utf-8').rstrip().split('\n')
sm = set()
for line in header_array:
if not line.startswith("@RG"): continue
rg_array = line.rstrip().split('\t')[1:]
for element in rg_array:
if not element.startswith('SM'): continue
value = element.replace("SM:","")
value = "".join([ c if re.match(r"[a-zA-Z0-9\-_]", c) else "_" for c in value ])
sm.add(value)
if not len(sm) == 1: sys.exit("\nDo not support multiple different SM entries, or no SM: %s:" % ", ".join(list(sm)))
return sm.pop()
def execute(cmd):
print "RUNNING...\n", cmd, "\n"
process = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
while True:
nextline = process.stdout.readline()
if nextline == '' and process.poll() is not None:
break
sys.stdout.write(nextline)
sys.stdout.flush()
stderr = process.communicate()[1]
if process.returncode != 0:
print "[ERROR] command:", cmd, "exited with code:", process.returncode
print stderr
raise RuntimeError
return process.returncode
def run_muse(args):
if args.run_id is None:
sm = get_run_id_from_sm_in_bam(args.tumor_bam)
else:
sm = args.run_id
reg = re.compile('^[a-zA-Z0-9_-]+$')
if not reg.match(sm):
sys.exit('\nrun-id contains invalid character: %s\n' % sm)
else:
print "run-id:", sm
dateString = datetime.now().strftime("%Y%m%d")
output_vcf = '.'.join([sm, args.muse.replace(".", "-"), dateString, "somatic", "snv_mnv", "vcf"])
mode_flag = ""
if args.muse.endswith("MuSEv1.0rc"):
args.p = None
if args.mode == "wgs":
mode_flag = "-G"
else:
mode_flag = "-E"
if not os.path.exists(args.muse):
args.muse = which(args.muse)
workdir = os.path.abspath(tempfile.mkdtemp(dir=args.workdir, prefix="muse_work_"))
if not os.path.exists(args.f + ".fai"):
new_ref = os.path.join(workdir, "ref_genome.fasta")
os.symlink(os.path.abspath(args.f),new_ref)
subprocess.check_call( ["/usr/bin/samtools", "faidx", new_ref] )
args.f = new_ref
if args.normal_bam_index is None:
if not os.path.exists(args.normal_bam + ".bai"):
new_bam = os.path.join(os.path.abspath(workdir), "normal.bam")
os.symlink(os.path.abspath(args.normal_bam),new_bam)
subprocess.check_call( ["/usr/bin/samtools", "index", new_bam] )
args.normal_bam = new_bam
else:
new_bam = os.path.join(os.path.abspath(workdir), "normal.bam")
os.symlink(os.path.abspath(args.normal_bam), new_bam)
os.symlink(os.path.abspath(args.normal_bam_index), new_bam + ".bai")
args.normal_bam = new_bam
if args.tumor_bam_index is None:
if not os.path.exists(args.tumor_bam + ".bai"):
new_bam = os.path.join(os.path.abspath(workdir), "tumor.bam")
os.symlink(os.path.abspath(args.tumor_bam),new_bam)
subprocess.check_call( ["/usr/bin/samtools", "index", new_bam] )
args.tumor_bam = new_bam
else:
new_bam = os.path.join(workdir, "tumor.bam")
os.symlink(os.path.abspath(args.tumor_bam), new_bam)
os.symlink(os.path.abspath(args.tumor_bam_index), new_bam + ".bai")
args.tumor_bam = new_bam
cmds = list(call_cmd_iter(ref_seq=args.f,
muse=args.muse,
block_size=args.b,
tumor_bam=args.tumor_bam,
normal_bam=args.normal_bam,
contamination=args.p,
output_base=os.path.join(workdir, "output.file"))
)
rvals = cmds_runner(list(a[0] for a in cmds), args.cpus)
if any(rvals):
raise Exception("MuSE CALL failed")
#check if rvals is ok
first = True
merge = os.path.join(workdir, "merge.output")
with open(merge, "w") as ohandle:
for cmd, out in cmds:
with open(out) as handle:
for line in handle:
if first or not line.startswith("#"):
ohandle.write(line)
first = False
if not args.no_clean:
os.unlink(out)
dbsnp_file = None
if args.D:
new_dbsnp = os.path.join(workdir, "db_snp.vcf.gz")
os.symlink(args.D,new_dbsnp)
#subprocess.check_call( ["/usr/bin/bgzip", new_dbsnp] )
subprocess.check_call( ["/usr/bin/tabix", "-p", "vcf", new_dbsnp ])
dbsnp_file = new_dbsnp
sump_template = string.Template("${MUSE} sump -I ${MERGE} -O ${OUTPUT} -D ${DBSNP} ${MODE}")
else:
sump_template = string.Template("${MUSE} sump -I ${MERGE} -O ${OUTPUT} ${MODE}")
tmp_out = os.path.join(workdir, "tmp.vcf")
sump_cmd = sump_template.substitute( dict (
MUSE=args.muse,
MERGE=merge,
OUTPUT=tmp_out,
DBSNP=dbsnp_file,
MODE=mode_flag
))
cmd_caller(sump_cmd)
if args.muse.endswith("MuSEv0.9.9.5"):
subprocess.check_call( ["/opt/bin/vcf_reformat.py", tmp_out, "-o", output_vcf,
"-b", "TUMOR", args.tumor_bam, "-b", "NORMAL", args.normal_bam] )
else:
shutil.copy(tmp_out, output_vcf)
# gzip and generate tbi file for vcf
execute("/usr/bin/bgzip -c {0} > {0}.gz".format(output_vcf))
execute("/usr/bin/tabix -p vcf {0}.gz".format(output_vcf))
execute("cat {0}.gz | md5sum | cut -b 1-33 > {0}.gz.md5".format(output_vcf))
execute("cat {0}.gz.tbi | md5sum | cut -b 1-33 > {0}.gz.tbi.md5".format(output_vcf))
if not args.no_clean:
shutil.rmtree(workdir)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-m", "--muse", help="Which Copy of MuSE", choices=["MuSEv0.9.9.5", "MuSEv1.0rc"], default="MuSEv0.9.9.5")
parser.add_argument("-f", help="faidx indexed reference sequence file", required=True)
#parser.add_argument("-r", help="single region (chr:pos-pos) where somatic mutations are called")
#parser.add_argument("-l", help="list of regions (chr:pos-pos or BED), one region per line")
parser.add_argument("-p", type=float, help="normal data contamination rate [0.050]", default=0.05)
parser.add_argument("-b", type=long, help="Parallel Block Size", default=50000000)
parser.add_argument("--run-id", dest="run_id", type=str, help="The output vcf file will be named following \
the convention: \
<run_id>.<workflowName>.<dateString>.somatic.snv_mnv.vcf.gz \
Otherwise the output vcf file will be named automatically \
following the pattern: \
<SM>.<workflowName>.<dateString>.somatic.snv_mnv.vcf.gz \
where SM is extracted from the @RG line in the tumor BAM header.")
parser.add_argument("-D", help="""dbSNP vcf file that should be bgzip compressed, \
tabix indexed and based on the same reference genome used in 'MuSE call'""")
parser.add_argument("-n", "--cpus", type=int, default=cpu_count())
parser.add_argument("-w", "--workdir", default="/tmp")
parser.add_argument("--no-clean", action="store_true", default=False)
parser.add_argument("--mode", choices=["wgs", "wxs"], default="wgs")
parser.add_argument("--tumor-bam", dest="tumor_bam", required=True)
parser.add_argument("--tumor-bam-index", dest="tumor_bam_index", default=None)
parser.add_argument("--normal-bam", dest="normal_bam", required=True)
parser.add_argument("--normal-bam-index", dest="normal_bam_index", default=None)
args = parser.parse_args()
run_muse(args)