-
Notifications
You must be signed in to change notification settings - Fork 1
/
assign_contigs.py
606 lines (501 loc) · 21.1 KB
/
assign_contigs.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib import pyplot as plt
from matplotlib import patches
import pandas as pd
import numpy as np
import subprocess
import argparse
import pathlib
# Local imports.
import fasta
import agp
def setup():
parser = argparse.ArgumentParser(
description="Run nucmer to find contig chromosome assignments."
)
parser.add_argument(
"-r",
"--reference",
required=True,
type=pathlib.Path,
help="Reference genome sequence.",
)
parser.add_argument(
"-a",
"--assembly",
required=True,
type=pathlib.Path,
help="Assembly file.",
)
parser.add_argument(
"-s",
"--scaffolds",
required=True,
type=pathlib.Path,
help="Scaffolder AGP file.",
)
parser.add_argument(
"-t",
"--threads",
default="1",
help="Number of threads to use."
)
parser.add_argument(
"-l",
"--min-length",
default=1000,
help="Minimum alignment length to be considered. (bp)",
)
parser.add_argument(
"-c",
"--min-coverage",
default=0.2,
help="Minimum alignment percent to be considered. (0 - 1)",
)
return parser.parse_args()
def run_mummer(reference: pathlib.Path, assembly: pathlib.Path, threads: int):
"""
MUMmer produces a coordinates file which describes the alignment of
sequences. We parse this file and produce a pandas dataframe.
Args:
filename: The file path to the MUMmer show-coords coordinate file.
Returns:
A pandas dataframe containing all the columns of the MUMmer output.
"""
# Run nucmer.
print("Running MUMmer.")
subprocess.call(["nucmer", "-t", threads, reference, assembly])
# Run delta-filter.
with open("filter.delta", "w") as outfile:
subprocess.call(["delta-filter", "-1", "out.delta"], stdout=outfile)
# Run show-coords.
with open("coordinates.txt", "w") as outfile:
subprocess.call(
["show-coords", "-c", "-l", "-r", "-d", "filter.delta"],
stdout=outfile,
)
# Read the MUMmer output.
alignment = read_mummer("coordinates.txt")
# Clean up.
pathlib.Path.unlink(pathlib.Path("out.delta"))
pathlib.Path.unlink(pathlib.Path("filter.delta"))
pathlib.Path.unlink(pathlib.Path("coordinates.txt"))
return alignment
def read_mummer(filename: pathlib.Path) -> pd.DataFrame:
"""
MUMmer produces a coordinates file which describes the alignment of
sequences. We parse this file and produce a pandas dataframe.
Args:
filename: The file path to the MUMmer show-coords coordinate file.
Returns:
A pandas dataframe containing all the columns of the MUMmer output.
"""
with open(filename) as infile:
lines = infile.readlines()
# All the fields.
alignment = {
"reference_name": [],
"query_name": [],
"reference_start": [],
"reference_end": [],
"query_start": [],
"query_end": [],
"reference_alignment_length": [],
"query_alignment_length": [],
"percent_identity": [],
"reference_length": [],
"query_length": [],
"reference_coverage": [],
"query_coverage": [],
"reference_orientation": [],
"query_orientation": [],
}
for line in lines[5:]:
split = line.split()
# Where the fields show up in the file.
alignment["reference_start"].append(int(split[0]))
alignment["reference_end"].append(int(split[1]))
alignment["query_start"].append(int(split[3]))
alignment["query_end"].append(int(split[4]))
alignment["reference_alignment_length"].append(int(split[6]))
alignment["query_alignment_length"].append(int(split[7]))
alignment["percent_identity"].append(float(split[9]))
alignment["reference_length"].append(int(split[11]))
alignment["query_length"].append(int(split[12]))
alignment["reference_coverage"].append(float(split[14]))
alignment["query_coverage"].append(float(split[15]))
alignment["reference_orientation"].append(int(split[17]))
alignment["query_orientation"].append(int(split[18]))
alignment["reference_name"].append(split[19])
alignment["query_name"].append(split[20])
alignment = pd.DataFrame(alignment)
alignment.set_index(["query_name", "reference_name"], inplace=True)
alignment.sort_index(level=["query_name", "reference_name"], inplace=True)
return alignment
def remove_alignments(group, min_length: int=1000, min_coverage: float=0.2):
"""
Since MUMmer aligns sections of a sequence, it can produce multiple regions
of alignment for a given sequence. We filter out low alignments using this
function.
Args:
group: A pandas group_by group.
min_length: The minimum length a sequence needs to align to a reference,
by summing all of its constituent alignments.
min_coverage: The minimum percent of the query sequence that needs to
have aligned to the reference sequence. This is to prevent long
sequences which meet the min_length requirement from passing.
Returns:
True or false, which helps with the filtering function used by pandas.
"""
alignment_length = group["query_alignment_length"].sum()
alignment_coverage = alignment_length / group["query_length"].iloc[0]
if (alignment_coverage > min_coverage) and (alignment_length > min_length):
return True
else:
return False
def get_assignments(df: pd.DataFrame, reference_lengths: dict, assembly_lengths: dict) -> tuple:
"""
Creates an AGP compatible dictionary for how contigs ought to be scaffolded
based on their alignments to a reference genome.
Args:
df: The pandas dataframe with the alignments between the assembly and
reference genome.
reference_lengths: A dictionary containing sequence lengths for the
reference genome.
assembly_lengths: A dictionary containing sequence lengths for the
assembly.
Returns:
A dictionary which describes the optimal scaffolding of contigs based
on their alignments. It is used to create an AGP file. Also returned is
the number of reference chromosomes which were skipped due to low
alignments.
"""
# Keep a list of contigs in the assembly.
remaining_contigs = list(assembly_lengths.keys())
skipped = 0
assembly = {}
for scaffold_name in reference_lengths:
# If reference scaffold has no alignments.
if scaffold_name not in df.index.get_level_values("reference_name").unique():
skipped += 1
continue
# Get the dataframe pertaining to a particular scaffold.
segment = df.xs(scaffold_name, level="reference_name").sort_values(
"mean_weighted_center"
)[["orientation", "query_length"]]
# Get rid of duplicate alignments.
segment = segment.loc[~segment.index.duplicated()]
# Add each contig.
assembly[scaffold_name] = {}
for contig in segment.itertuples():
assembly[scaffold_name][contig.Index] = {
"start": 1,
"end": contig.query_length,
"orientation": contig.orientation,
}
# Remove from remaining contigs.
remaining_contigs.remove(contig.Index)
# Add unscaffolded contigs.
i = 1
for contig in remaining_contigs:
scaffold_name = f"unscaffolded_{i}"
assembly[scaffold_name] = {
contig: {
"start": 1,
"end": assembly_lengths[contig],
"orientation": "+",
}
}
i += 1
return assembly, skipped
def plot(df: pd.DataFrame, reference: dict, assembly: dict, filename: pathlib.Path):
"""
Plot how contigs are scaffolded on the reference genome compared to how they
are scaffolded in the assembly.
Args:
df: The pandas dataframe with the alignments between the assembly and
reference genome.
reference: An AGP compatible dictionary showing ideal scaffolding.
assembly: An AGP compatible dictionary showing actual scaffolding.
"""
# Assign scaffold names to the dataframe since they get lost during the
# MUMmer alignment.
for scaffold_name in assembly:
for query_name in assembly[scaffold_name]:
if query_name in df.index.get_level_values("query_name").unique():
df.loc[query_name, "scaffold_name"] = scaffold_name
# Compute the center of each scaffold so that they can be ordered by how
# they appear on a chromosome.
df["scaffold_weighted_center"] = df.groupby(by=["reference_name", "scaffold_name"])[
"mean_weighted_center"
].transform(np.mean)
df.sort_values(["reference_name", "scaffold_weighted_center"], inplace=True)
# Create a pdf.
with PdfPages(filename) as pdf:
# For each reference chromosome -
for reference_name in reference:
i = 0
ci = 0
minimum = 0
reference_maximum = reference[reference_name]
maximum = reference_maximum
# Rotate five colors.
light_colors = [
"#64b0e4",
"#ff9e49",
"#6bd66b",
"#e36869",
"#a782c9",
]
dark_colors = [
"#1f77b4",
"#ff7f0e",
"#2ca02c",
"#d62728",
"#9467bd",
]
# If reference scaffold has alignments.
if reference_name in df.index.get_level_values("reference_name").unique():
# Extract all the query sequences associated with it.
section = df.xs(reference_name, level="reference_name")
# Set figure size.
num_contigs = len(section.index.get_level_values("query_name").unique())
height = num_contigs * 0.2 + 1
if height > 100:
height = 100
plt.figure(figsize=(8, height), dpi=100)
# For each scaffold.
for scaffold_name, scaffold in section.groupby(
"scaffold_name", sort=False
):
j = 0
# For each contig in the scaffold.
for query_name in assembly[scaffold_name]:
# If the contig has been aligned.
if (
query_name
in section.index.get_level_values("query_name").unique()
):
contig = df.xs(query_name, level="query_name")
# Get the start and end of the query coordinates.
min_query = contig[["query_start", "query_end"]].min().min()
max_query = contig[["query_start", "query_end"]].max().max()
query_length = contig["query_length"][0]
# Get the start and end of the reference coordinates.
reference_start = contig["reference_start"].min()
reference_end = contig["reference_end"].max()
reference_length = reference_end - reference_start
# Calculate how much of the query was unmapped.
preceeding_start = reference_start - min_query
proceeding_length = query_length - max_query
proceeding_end = reference_end + proceeding_length
# Keep track of the global minumum coordinate.
if preceeding_start < minimum:
minimum = preceeding_start
# Keep track of the global maximum coordinate.
if proceeding_end > maximum:
maximum = proceeding_end
# Unaligned segment of contig before main alignment.
preceeding_segment = patches.Rectangle(
xy=(preceeding_start, i),
width=min_query,
height=0.75,
facecolor=light_colors[ci % 5],
edgecolor="black",
)
# Aligned segment of the contig.
main_segment = patches.Rectangle(
xy=(reference_start, i),
width=reference_length,
height=0.75,
facecolor=dark_colors[ci % 5],
edgecolor="black",
)
# Unaligned segment of the contig after main alignment.
proceeding_segment = patches.Rectangle(
xy=(reference_end, i),
width=proceeding_length,
height=0.75,
facecolor=light_colors[ci % 5],
edgecolor="black",
)
# Add the rectangle patches.
plt.gca().add_patch(preceeding_segment)
plt.gca().add_patch(main_segment)
plt.gca().add_patch(proceeding_segment)
# Draw arrow for orientation.
arrowprops = {
"width": 1,
"shrink": 0.1,
"facecolor": "black",
"headwidth": 7,
"headlength": 7,
}
if contig["orientation"].iloc[0] == "+":
xy = (reference_end, i + 0.375)
xytext = (reference_start, i + 0.375)
else:
xy = (reference_start, i + 0.375)
xytext = (reference_end, i + 0.375)
plt.annotate(
text="",
xy=xy,
xytext=xytext,
arrowprops=arrowprops,
)
# Add scaffold label.
if j == 0:
x = (
reference_end
+ proceeding_length
+ (reference_maximum * 0.01)
)
text = plt.text(
x=x,
y=i + 0.375,
s=scaffold_name,
va="center",
)
plt.gcf().canvas.draw()
text_end = (
text.get_window_extent()
.transformed(plt.gca().transData.inverted())
.x1
)
if text_end > maximum:
maximum = text_end
# Place the next contig below.
i -= 1
# Count contigs in scaffold.
j += 1
# Count color changes.
ci += 1
else:
plt.figure(figsize=(8, 2), dpi=100)
# Add the reference sequence.
main_segment = patches.Rectangle(
xy=(0, i),
width=reference_maximum,
height=0.75,
facecolor="gray",
edgecolor="black",
)
plt.gca().add_patch(main_segment)
plt.axis("off")
# Add a 5% margin on the figure.
plt.xlim([minimum - (maximum * 0.05), maximum + (maximum * 0.05)])
plt.ylim([i, 2])
plt.title(reference_name)
plt.tight_layout()
pdf.savefig()
plt.close()
# Be sure to have the nucmer, delta-filter, and show-coords, in your path.
def main(
reference,
assembly,
scaffold_file,
outdir,
threads,
min_length,
min_coverage,
):
alignment = run_mummer(reference=reference, assembly=assembly, threads=threads)
# If something goes wrong in the MUMmer process, stop the program.
if alignment is None:
return
contig_lengths = fasta.lengths(assembly)
# Count the number of aligned contigs.
aligned_contigs = len(alignment.index.get_level_values("query_name").unique())
# Filter out contigs with low alignment coverage or low alignment lengths.
filtered = alignment.groupby(by=["query_name", "reference_name"]).filter(
remove_alignments, min_length=min_length, min_coverage=min_coverage
)
filtered_contigs = len(filtered.index.get_level_values("query_name").unique())
# Add orientation.
filtered["signed_query_alignment_length"] = (
filtered["query_alignment_length"] * filtered["query_orientation"]
)
filtered["orientation"] = np.where(
filtered.groupby(by=["query_name", "reference_name"])[
"signed_query_alignment_length"
].transform(np.mean)
> 0,
"+",
"-",
)
# Add total alignment length for each contig and reference pair.
filtered["total_reference_alignment_length"] = filtered.groupby(
by=["query_name", "reference_name"]
)["reference_alignment_length"].transform(sum)
# Find the center of each group of alignments weighted by the length of the
# alignment.
filtered["weighted_center"] = filtered[["reference_start", "reference_end"]].mean(
axis=1
) * (
filtered["reference_alignment_length"]
/ filtered["total_reference_alignment_length"]
)
filtered["mean_weighted_center"] = filtered.groupby(
by=["query_name", "reference_name"]
)["weighted_center"].transform(sum)
# Only keep the query reference pair with the largest total alignment.
filtered = filtered.loc[
filtered.groupby(by="query_name")["total_reference_alignment_length"].idxmax()
]
# Sort by center of contig alignments.
filtered.sort_values(["reference_name", "mean_weighted_center"], inplace=True)
# Write the AGP file.
print("Writing AGP.")
reference_lengths = fasta.lengths(reference)
scaffolds, skipped = get_assignments(
df=filtered,
reference_lengths=reference_lengths,
assembly_lengths=contig_lengths,
)
stem = "_".join(assembly.stem.split("_")[:-1])
agp_file = outdir.joinpath(f"{stem}_reference.agp")
agp.write(scaffolds, agp_file)
# Plot alignments.
print("Plotting alignments.")
scaffold_file = agp.read(scaffold_file)
filename = outdir.joinpath(f"{stem}_alignment.pdf")
plot(filtered, reference_lengths, scaffold_file, filename)
# Alignment statistics.
if len(filtered) > 0:
minimum_alignment = (
filtered["total_reference_alignment_length"] / filtered["query_length"]
).min() * 100
average_alignment = (
filtered.groupby(by="query_name")
.first()["total_reference_alignment_length"]
.sum()
/ filtered.groupby(by="query_name").first()["query_length"].sum()
* 100
)
print()
print(" Contig Statistics ")
print("-----------------------------")
print(f" Starting: {len(contig_lengths):>6}")
print(f" Missing Alignments: {len(contig_lengths) - aligned_contigs:>6}")
print(f" Low Alignments: {aligned_contigs - filtered_contigs:>6}")
print(f" Missing Reference: {skipped:>6}")
print(f" Minimum Alignment: {minimum_alignment:>5.2f}%")
print(f" Average Alignment: {average_alignment:>5.2f}%")
if len(filtered) == 0:
print("\nWarning, there are no alignments.")
return
if average_alignment < 50:
print("\nWarning, the average query alignment coverage is low!")
print("The assembly does not match the reference well.")
return agp_file
if __name__ == "__main__":
arguments = setup()
main(
reference=arguments.reference,
assembly=arguments.assembly,
scaffold_file=arguments.scaffolds,
threads=arguments.threads,
min_length=arguments.min_length,
min_coverage=arguments.min_coverage,
outdir=pathlib.Path.cwd(),
)