-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmMinION.py
350 lines (268 loc) · 13.7 KB
/
mMinION.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from sqlalchemy.ext.declarative import declarative_base
import vcf
import logging
import json
import argparse
from lib.common import Common
import os
from lib.common import MetaClass
class mMinION(Common):
__metaclass__ = MetaClass
base = declarative_base()
def __init__(self,samplename,fastq,output,sangerresults):
self.output = output
self.samplename = samplename
self.fastq = fastq
self.sangerresults = sangerresults
with open('/Users/mattparker/PycharmProjects/mMinION/conf/config.json') as json_data_file:
self.config = json.load(json_data_file)
# set up logger
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.INFO)
# complete setup of logger
handler = logging.FileHandler(self.output + "/pipeline.log", 'w')
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
self.logger.addHandler(handler)
self.logger.addHandler(console)
def correction_trimming(self,corrected_error_rate,min_read_length):
#correct reads
command = [self.config["software"]["canu"],"-correct","-p",self.samplename,"-d",self.output,"genomeSize=16.5k overlapper=mhap utgReAlign=true stopOnReadQuality=false -nanopore-raw",self.fastq]
self.run_command(" ".join(command))
#trim reads
corrected = self.output + "/" + self.samplename + ".correctedReads.fasta.gz"
command = [self.config["software"]["canu"], "-trim","-p",self.samplename,"-d",self.output,"genomeSize=16.5k overlapper=mhap utgReAlign=true stopOnReadQuality=false -nanopore-corrected",corrected]
self.run_command(" ".join(command))
trimmed = self.output + "/" + self.samplename + ".trimmedReads.fasta.gz"
return trimmed
def trim_adapters(self,fastqs):
out_files = []
fastq = fastqs.split(",")
for i in fastq:
output = self.output + "/" + os.path.basename(i).replace("fastq","trimmed.fastq.gz")
out_files.append(output)
command = [self.config["software"]["porechop"], "-i", i, "-o",output]
self.run_command(" ".join(command))
return out_files
def mapping(self,fastq):
rg = r'"@RG\tID:'+self.samplename+r'\tSM:mMinION"'
outfile = self.output + "/" + self.samplename + ".sorted.bam"
print self.config["software"]
command = [self.config["software"]["minimap2"], "--MD -ax map-ont", '-R',rg, self.config["reference"]["genome"], fastq,"|",self.config["software"]["samtools"],"sort","-","|",self.config["software"]["samtools"],"view","-bh","-",">",outfile]
self.run_command(" ".join(command))
command = [self.config["software"]["samtools"],"index",outfile]
self.run_command(" ".join(command))
return outfile
def filter(self,bam):
#samtools view -h N4.trimmed.sorted.bam | awk 'length($10) > 1000 || $1 ~ /^@/' | samtools view -bS > N4.trimmed.sorted.filtered.bam
outfile = self.output + "/" + self.samplename + ".trimed.sorted.filtered.bam"
command = [self.config["software"]["samtools"],"view","-h",bam,"| awk 'length($10) > 200 || $1 ~ /^@/' |",self.config["software"]["samtools"],"view -bS >",outfile]
self.run_command(" ".join(command))
command = [self.config["software"]["samtools"], "index", outfile]
self.run_command(" ".join(command))
return outfile
def sv(self,bam):
#run nanosv
outfile = self.output + "/" + self.samplename + ".sorted.bam.vcf"
command = [self.config["software"]["nanosv"],"-o",outfile,"-s",self.config["software"]["samtools"],"-b",self.config["reference"]["bed"],"-t","6",bam]
self.run_command(" ".join(command))
#filter calls which overlap the uncovered region around 0kb
return outfile
def transform_sv(self, sv):
vcf_reader = vcf.Reader(open(sv, 'r'))
out = []
for record in vcf_reader:
print record
print record.ALT[0]
print type(record.ALT[0])
if str(record.ALT[0]) == "<INS>":
print record.INFO["END"]
alt = str(int(record.POS) + int(record.INFO["END"]))
else:
alt = str(''.join(i for i in str(record.ALT[0]) if i.isdigit()))
if record.QUAL < 200:
color = "lgrey"
else:
color = "red"
if "LowQual" in record.FILTER:
color = "vvlgrey"
line = [record.CHROM, str(record.POS), str(record.POS), record.CHROM, alt, alt,
"color=" + color + ",thickness=5p"]
out.append("\t".join(line))
links_file = self.output + "/" + self.samplename + ".sorted.bam.sv.circos"
circos_out = open(links_file, "w")
circos_out.write("\n".join(out))
circos_out.close()
return links_file
def sniffles_sv(self,bam):
outfile = self.samplename + ".sorted.bam.sniffles.vcf"
bam_path = os.path.dirname(bam)
bam = os.path.basename(bam)
self.docker_command(image=self.config["software"]["sniffles_image"],executable=self.config["software"]["sniffles_path"],data_dir=bam_path,output_dir=self.output,arguments="-q 30 --allelefreq 0.01 -v /output/"+outfile+" -m /data/"+bam)
return self.output + "/" + self.samplename + ".sorted.bam.sniffles.vcf"
def transform_sniffles_sv(self, sv):
vcf_reader = vcf.Reader(open(sv, 'r'))
red_out = []
grey_out = []
for record in vcf_reader:
if str(record.ALT[0]) == '<DEL>':
if "IMPRECISE" in record.INFO:
if record.INFO["IMPRECISE"] == True:
color = "vvlgrey"
if "PRECISE" in record.INFO:
if record.INFO["PRECISE"] == True:
color = "red"
alt = str(record.INFO["END"])
# print record.FORMAT
# #reference reads
# reference_reads = record.samples[0]["DR"]
# #variant reads
# variant_reads = record.samples[0]["DV"]
# total_reads = reference_reads+variant_reads
#
#
# print record.samples[0]["GT"]
#
# if reference_reads == 0:
# color="vvlgrey"
# else:
# color = "red"
# print total_reads
# print self.median_coverage
# print (total_reads / float(self.median_coverage))
# if (total_reads / float(self.median_coverage)) < 0.1:
# print "low_cov"
# color="vvlgrey"
#
line = [record.CHROM, str(record.POS), str(record.POS), record.CHROM, alt, alt,
"color=" + color + ",thickness=5p"]
if color == "vvlgrey":
grey_out.append("\t".join(line))
elif color == "red":
red_out.append("\t".join(line))
links_file = self.output + "/" + self.samplename + ".sorted.bam.sv.circos"
circos_out = open(links_file, "w")
circos_out.write("\n".join(grey_out))
circos_out.write("\n")
circos_out.write("\n".join(red_out))
circos_out.write("\n")
circos_out.close()
return links_file
def parse_sanger_svs(self):
sanger_file = self.output + "/" + self.samplename + ".labels.circos"
sanger_out = open(sanger_file, "w")
print self.sangerresults
with open ("/Users/mattparker/PycharmProjects/mMinION/conf/labels.txt", "rb") as l:
for line in l:
sanger_out.write(line)
with open(self.sangerresults, "rb") as f:
for line in f:
sample, miseqid, barcode, deletion_status, deletion_start, deletion_end, mutation, mutation_frequency = line.rstrip().split(
"\t")
if self.samplename in line:
if deletion_start != "NA":
sanger_out.write(str("MT ")+str(deletion_start)+str(" ")+str(deletion_start)+str(" SANGER_1 link_color=blue\n"))
sanger_out.write(str("MT ") + str(deletion_end) + str(" ") + str(deletion_end) + str(" SANGER_2 link_color=blue\n"))
sanger_out.close
return sanger_file
def call_variants(self,mpileup):
snp_outfile = mpileup + ".snp.vcf"
command = ["java","-jar",self.config["software"]["varscan"],"mpileup2snp",mpileup," --min-var-freq 0.01 --output-vcf 1 --strand-filter 0",">",snp_outfile]
self.run_command(" ".join(command))
#do indels not follow VCF spec? process them so they do
indel_outfile = mpileup + ".indel.vcf"
command = ["java", "-jar", self.config["software"]["varscan"], "mpileup2indel",
mpileup," --output-vcf 1 --strand-filter 0", ">", indel_outfile]
self.run_command(" ".join(command))
bgzip_snps = self.bgzip_tabix(snp_outfile)
bgzip_indels = self.bgzip_tabix(indel_outfile)
combined = mpileup + ".snp.indel.vcf"
command = [self.config["software"]["bcftools"],"merge","--force-samples",bgzip_snps,bgzip_indels,">",combined]
self.run_command(" ".join(command))
return combined
def annotate_variants(self,vcf):
outfile = self.output + "/" + self.samplename + ".sorted.bam.mpileup.snp.indel.annotated.vcf"
command = [self.config["software"]["vcfanno"],self.config["reference"]["vcfanno"],vcf,">",outfile]
self.run_command(" ".join(command))
return outfile
def prepare_ciros(self,links,coverage,sanger):
#replace lines in conf with actual locations
print self.max_coverage
conf_file = self.output + "/" + self.samplename + ".circos.conf"
circos_out = open(conf_file, "w")
with open("/Users/mattparker/PycharmProjects/mMinION/conf/circos.conf", "rb") as f:
for line in f:
line = line.replace("{{LINKS}}",links)
line = line.replace("{{COVERAGE}}",coverage)
line = line.replace("{{MAX}}", str(self.max_coverage))
line = line.replace("{{LABELS}}",sanger)
circos_out.write(line)
circos_out.close()
command = ["cp","/Users/mattparker/PycharmProjects/mMinION/conf/ticks.conf",self.output+"/ticks.conf"]
self.run_command(" ".join(command))
command = ["cp","/Users/mattparker/PycharmProjects/mMinION/conf/ideogram.conf",self.output+"/ideogram.conf"]
self.run_command(" ".join(command))
command = ["cp", "/Users/mattparker/PycharmProjects/mMinION/conf/karyotype.txt", self.output + "/karyotype.txt"]
self.run_command(" ".join(command))
return conf_file
def main():
parser = argparse.ArgumentParser(description='mMinION - Run fastq file for a sample through mitochondraial analysis pipeline')
parser.add_argument('--samplename',help='The name of the sample being processed - files will take this name')
parser.add_argument('--fastq',help='Full path to the Fastq file')
parser.add_argument('--trimmed_fastq', help='Full path to the Fastq file')
parser.add_argument('--outdir',help='The output directory for all files generated')
parser.add_argument('--bam',help='Provide bam file to skip mapping')
parser.add_argument('--coverage',help='Provide samtools depth output to skip this step')
parser.add_argument('--sv',help='Provide SV calls to skip SV calling')
parser.add_argument('--variants', help='Provide SNV/INDEL calls to skip calling')
parser.add_argument('--correctedErrorRate')
parser.add_argument('--minReadLength')
parser.add_argument('--sangerresults')
parser.add_argument('--correction', action='store_true', help='Turn on read correction & trimming with canu. WARNING! This is computationally intensive')
args = parser.parse_args()
if not os.path.exists(args.outdir):
os.makedirs(args.outdir)
m = mMinION(args.samplename,args.fastq,args.outdir,args.sangerresults)
if args.bam:
bam = m.filter(args.bam)
else:
if args.correction:
trimmed_fastq = m.correction_trimming(corrected_error_rate=args.correctedErrorRate,min_read_length=args.minReadLength)
bam = m.mapping(trimmed_fastq)
else:
if args.trimmed_fastq:
trimmed_fastq = args.trimmed_fastq.split(",")
else:
trimmed_fastq = m.trim_adapters(args.fastq)
fastq = " ".join(trimmed_fastq)
temp_bam = m.mapping(fastq)
bam = m.filter(temp_bam)
if args.coverage:
coverage = args.coverage
else:
coverage = m.coverage(bam)
circos_coverage = m.transform_coverage(coverage)
if args.sv:
sv = args.sv
else:
sv = m.sv(bam)
if args.variants:
variants = args.variants
else:
pileup = m.mpileup(bam)
raw_variants = m.call_variants(pileup)
variants = m.annotate_variants(raw_variants)
links = m.transform_sniffles_sv(sv)
sanger = m.parse_sanger_svs()
circos_conf = m.prepare_ciros(links, circos_coverage,sanger)
circos = m.circos(circos_conf)
print circos_conf
#preprocessing steps
#convert to fastq
#poretools fastq /Volumes/Untitled/20180206_1708_Multiplex_Exp/uploaded/fast5/pass/ > ~/Documents/Projects/Wood/DATA/MINION/PORETOOLS_FASTQ/20180206_1708_Multiplex_Exp.fastq
#porechop -i PORETOOLS_FASTQ/20180206_1708_Multiplex_Exp.fastq -b ~/Documents/Projects/Wood/DATA/MINION/PORETOOLS_FASTQ/ --barcode_threshold 85 -t 16
if __name__ == '__main__':
main()