-
Notifications
You must be signed in to change notification settings - Fork 1
/
gbtquick.py
executable file
·375 lines (337 loc) · 13.2 KB
/
gbtquick.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python3
import sys
import re
import argparse
import logging
import json
import matplotlib as mpl
mpl.use("Agg") # Plot without X-server
import matplotlib.pyplot as plt
from collections import defaultdict
from statistics import mean,stdev,median
parser = argparse.ArgumentParser(description="""
Report covstats and plot blobplots from (meta)genome assembly files without
read-mapping, by parsing coverage info directly from headers
""")
# Basic inputs
parser.add_argument("-a","--assembler", type=str,
help="Assembler used, valid options 'spades', 'flye', 'megahit', 'mapping'")
parser.add_argument("-f", "--fasta", type=str,
help="Assembly Fasta file")
parser.add_argument("-i", "--info", type=str,
help="assembly_info file from Flye")
# Prodigal params
parser.add_argument("--cds", action="store_true",
help="Run Prodigal to predict CDS and calculate CDS density")
parser.add_argument("--prodigal_gff", type=str, default=None,
help="Output GFF3 from Prodigal already computed")
# Plot outputs
parser.add_argument("--plot", action="store_true",
help="Plot figure of blobplot")
parser.add_argument("--plot_fmt", type=str, default="png",
help="Format of plot image, either png or pdf")
parser.add_argument("--plot_width", type=int, default=10,
help="Width of plot, in inches")
parser.add_argument("--plot_height", type=int, default=7,
help="Height of plot, in inches")
# Output params
parser.add_argument("-o", "--out", type=str, default="test",
help="Output filename prefix")
parser.add_argument("--dump", action="store_true",
help="Dump data as json for troubleshooting")
args = parser.parse_args()
logging.basicConfig(format='[%(asctime)s] %(message)s', level=logging.INFO)
def fasta_to_gccount(filename):
"""Parse Fasta file and get GC base count per contig
Parameters
----------
filename : str
Path to Fasta file
Returns
-------
dict
dict of ints (GC base counts) per contig, keyed by str (contig name)
"""
# counter = 0
gccount = defaultdict(int)
with open(filename, "r") as fh:
rname = ""
for line in fh:
line = line.rstrip() # Remove trailing whitespace
if (re.match(">",line)):
# counter += 1
# if (counter % 50000 == 0):
# fhlog.write(str(counter)+ " entries parsed\n")
rheader = line[1:] # Remove initial >
# Strip anything after first whitespace
rname = re.match("\S+", rheader)[0]
else:
gccount[rname] += sum([line.count(char)
for char in ["g", "c", "G", "C", "S"]])
return(gccount)
def parse_spades_assembly(assem):
"""Parse SPAdes assembly, get coverage from header and report covstats
Parameters
----------
assem : str
Path to SPAdes scaffolds.fasta file
Returns
-------
dict
dict of dicts, keyed by str (statistic type), each dict contains stats
values keyed by str (contig name)
"""
covstats = defaultdict(dict)
logging.info(f"Parsing SPAdes scaffolds assembly file {assem}")
with open(assem, "r") as fh:
for line in fh:
if re.match(r"^>", line):
line = line.rstrip()
rname = line[1:] # Strip > character from head
rcap = re.match(r"NODE_(\d+)_length_(\d+)_cov_([\d.]+)", rname)
if rcap:
covstats[rname]["Length"] = int(rcap.group(2))
covstats[rname]["Avg_fold"] = float(rcap.group(3))
else:
logging.warn(f"Invalid SPAdes header format {rname}")
logging.info(f"Parsing SPAdes scaffolds assembly file {assem} for GC content")
gccount = fasta_to_gccount(assem)
# Divide raw GC count by contig length to get GC frac
for rname in covstats:
if gccount[rname]:
covstats[rname]["Ref_GC"] = float(gccount[rname] / covstats[rname]["Length"])
else:
covstats[rname]["Ref_GC"] = 0.0
return(covstats)
def parse_megahit_assembly(assem):
"""Parse Megahit assembly, get coverage from header and report covstats
Parameters
----------
assem : str
Path to Megahit contigs.fa file
Returns
-------
dict
dict of dicts, keyed by str (statistic type), each dict contains stats
values keyed by str (contig name)
"""
covstats = defaultdict(dict)
logging.info(f"Parsing Megahit contig assembly file {assem}")
with open(assem, "r") as fh:
for line in fh:
if re.match(r"^>", line):
line = line.rstrip()
rname = line[1:] # Strip > character from head
rcap = re.match(r"(\w+) flag=(\S+) multi=([\d.]+) len=(\d+)", rname)
if rcap:
rname = rcap.group(1) # contig name until first whitespace
covstats[rname]["Avg_fold"] = float(rcap.group(3))
covstats[rname]["Length"] = int(rcap.group(4))
else:
logging.warn(f"Invalid SPAdes header format {rname}")
logging.info(f"Parsing SPAdes scaffolds assembly file {assem} for GC content")
gccount = fasta_to_gccount(assem)
# Divide raw GC count by contig length to get GC frac
for rname in covstats:
if gccount[rname]:
covstats[rname]["Ref_GC"] = float(gccount[rname] / covstats[rname]["Length"])
else:
covstats[rname]["Ref_GC"] = 0.0
return(covstats)
def parse_flye_assembly(info, assem):
"""Parse Flye assembly, get coverage from header and report covstats
Parameters
----------
info : str
Path to Flye assembly_info file
assem : str
Path to Flye assembly Fasta file
Returns
-------
dict
dict of dicts, keyed by str (statistic type), each dict contains stats
values keyed by str (contig name)
"""
covstats = defaultdict(dict)
logging.info(f"Parsing Flye assembly_info file {info}")
with open(info, "r") as fh:
for line in fh:
if not re.match(r"^#", line): # skip comments/headers
splitline = line.split(sep="\t")
# seqname length cov circ repeat mult alt_group graph_path
rname = splitline[0]
covstats[rname]["Length"] = int(splitline[1])
covstats[rname]["Avg_fold"] = float(splitline[2])
covstats[rname]["Circular"] = str(splitline[3])
covstats[rname]["Repeat"] = str(splitline[4])
logging.info(f"Parsing Flye assembly Fasta file {assem} for GC content")
gccount = fasta_to_gccount(assem)
# Divide raw GC count by contig length to get GC frac
for rname in covstats:
if gccount[rname]:
covstats[rname]["Ref_GC"] = float(gccount[rname] / covstats[rname]["Length"])
else:
covstats[rname]["Ref_GC"] = 0.0
return(covstats)
def run_prodigal(fasta, filename):
"""Run Prodigal to predict CDS
Parameters
----------
fasta : str
Path to Fasta file of genome assembly
filename : str
Path to write Prodigal GFF file
"""
import os
# retval = subprocess.run(["prodigal", "-q -f gff", "-i", fasta, "-o", filename])
retval = os.system(f"prodigal -i {fasta} -f gff -o {filename} -q")
if retval != 0:
logging.warning("Prodigal did not terminate successfully")
return(retval)
def parse_prodigal_gff(filename):
"""Parse GFF output from Prodigal and calculate CDS density per contig
Parameters
----------
filename : str
Path to Prodigal GFF output file
Returns
-------
defaultdict
dict of total CDS lengths keyed by contig name
"""
# dict of total CDS length keyed by contig name
cdslength = defaultdict(int)
with open(filename, "r") as fh:
for line in fh:
line = line.rstrip()
if not re.match(r"#", line):
fields = line.split("\t")
if fields[2] == "CDS":
cdslength[fields[0]] += int(fields[4]) - int(fields[3])
return(cdslength)
def covstats_to_tsv(d, filename):
"""Convert covstats dict to TSV format
Headers for all columns will be written
"""
# scaffold Length Avg_fold Ref_GC Circular Repeat Cds_dens
headings = ["Length", "Avg_fold", "Ref_GC", "Circular", "Repeat", "CDS_dens"]
out = []
heading_counter = defaultdict(int)
for scaffold in d:
outl = [scaffold]
for heading in headings:
if heading in d[scaffold]:
heading_counter[heading] += 1
outl.append(str(d[scaffold][heading]))
out.append(outl)
# Sanity check
for heading in heading_counter:
if heading_counter[heading] != len(d):
logging.warn("Data lengths do not match number of scaffolds")
# Write header
header = ["Scaffold"]
for heading in headings:
if heading in heading_counter:
header.append(heading)
# Write file
with open(filename, "w") as fh:
fh.write("\t".join(header) + "\n")
for line in out:
fh.write("\t".join(line) + "\n")
def covstats_to_plot(covstats, filename, fmt, name=None,
width=10, height=7, color_field=None):
"""Produce blobplot from covstats
Parameters
----------
covstats : dict
Covstats dict produced by parse_.._assembly
color_field : str
Field to use for color of plot characters
filename : str
Path to write file
fmt : str
Image format, either 'pdf' or 'png'
name : str
Name of library to be included in plot title
width : int
Width of plot in inches
height : int
Height of plot in inches
"""
minContigLen = 1000
lens = [covstats[contig]["Length"] for contig in covstats if covstats[contig]["Length"] > minContigLen]
covs = [covstats[contig]["Avg_fold"] for contig in covstats if covstats[contig]["Length"] > minContigLen]
gcs = [covstats[contig]["Ref_GC"] for contig in covstats if covstats[contig]["Length"] > minContigLen]
if color_field:
try:
cols = [covstats[contig][color_field] for contig in covstats if covstats[contig]["Length"] > minContigLen]
except:
logging.warn(f"Field {color_field} not found in statistics")
# Scaling factor for plot points
scale_factor = max(lens) / (width * plt.rcParams["figure.dpi"])
fig, ax = plt.subplots(figsize=(width, height))
if color_field:
scatter = ax.scatter(x=gcs, y=covs,
s=[i/scale_factor for i in lens],
c=cols,
alpha=0.1)
fig.colorbar(scatter, label=color_field)
else:
scatter = ax.scatter(x=gcs, y=covs,
s=[i/scale_factor for i in lens],
alpha=0.1)
plt.yscale('symlog')
plt.xlabel("GC fraction")
plt.ylabel("Coverage")
if name:
plt.title(f"Blobplot for library {name}")
# Invert scaling factor for legend to portray correct values
invs = dict(prop="sizes", num=5, func=lambda s: s*scale_factor)
legend = ax.legend(*scatter.legend_elements(**invs, alpha=0.2))
plt.savefig(filename, format=fmt)
# main
if args.assembler == "flye":
if not args.info:
logging.warn("Flye assembly_info file not specified")
covstats = parse_flye_assembly(args.info, args.fasta)
elif args.assembler == "spades":
logging.info(f"Parsing SPAdes assembly file {args.fasta}")
covstats = parse_spades_assembly(args.fasta)
elif args.assembler == "megahit":
logging.info(f"Parsing Megahit assembly file {args.fasta}")
covstats = parse_megahit_assembly(args.fasta)
else:
logging.warn(f"Invalid assembler {args.assembler} specified")
if args.cds:
if args.prodigal_gff:
logging.info(f"Prodigal output already supplied, skipping Prodigal")
else:
logging.info(f"Running Prodigal to predict CDSs")
args.prodigal_gff = f"{args.out}.prodigal.gff"
run_prodigal(args.fasta, args.prodigal_gff)
if args.prodigal_gff:
logging.info(f"Calculating CDS density from Prodigal output {args.prodigal_gff}")
cdslengths = parse_prodigal_gff(args.prodigal_gff)
for contig in covstats:
if contig in cdslengths:
covstats[contig]["CDS_dens"] = float(cdslengths[contig] / covstats[contig]["Length"])
else:
covstats[contig]["CDS_dens"] = 0.0
if args.plot:
logging.info(f"Drawing blobplot to file {args.out}.blobplot.{args.plot_fmt}")
if args.prodigal_gff or args.cds:
covstats_to_plot(covstats, f"{args.out}.blobplot.{args.plot_fmt}",
fmt=args.plot_fmt, width=args.plot_width,
height=args.plot_height, color_field="CDS_dens",
name=args.out)
else:
covstats_to_plot(covstats, f"{args.out}.blobplot.{args.plot_fmt}",
fmt=args.plot_fmt, width=args.plot_width,
height=args.plot_height,
name=args.out)
if args.dump:
logging.info("Dumping data to {args.out}.dump.json for troubleshooting")
with open(f"{args.out}.dump.json","w") as fh:
json.dump(covstats, fh, indent=4)
if covstats:
covstats_to_tsv(covstats,f"{args.out}.covstats.tsv")