Skip to content

Commit ab353f8

Browse files
authored
Temporarily fix high --normalisation issue until maptide refactor (#157)
1 parent a60ede0 commit ab353f8

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed

artic/align_trim.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ def find_primer(bed, pos, direction, chrom, threshold=35):
3939
primer_distances = [
4040
(abs(p["start"] - pos), p["start"] - pos, p)
4141
for p in bed
42-
if (p["direction"] == direction) and (pos >= (p["start"] - threshold)) and chrom == p["chrom"]
42+
if (p["direction"] == direction)
43+
and (pos >= (p["start"] - threshold))
44+
and chrom == p["chrom"]
4345
]
4446

4547
else:
4648
primer_distances = [
4749
(abs(p["end"] - pos), p["end"] - pos, p)
4850
for p in bed
49-
if (p["direction"] == direction) and (pos <= (p["end"] + threshold)) and chrom == p["chrom"]
51+
if (p["direction"] == direction)
52+
and (pos <= (p["end"] + threshold))
53+
and chrom == p["chrom"]
5054
]
5155

5256
if not primer_distances:
@@ -206,9 +210,21 @@ def handle_segment(
206210

207211
# locate the nearest primers to this alignment segment
208212
# p1 = find_primer(bed, segment.reference_start, "+", segment.reference_name, args.primer_match_threshold)
209-
p1 = find_primer(bed=bed, pos=segment.reference_start, direction="+", chrom=segment.reference_name, threshold=args.primer_match_threshold)
213+
p1 = find_primer(
214+
bed=bed,
215+
pos=segment.reference_start,
216+
direction="+",
217+
chrom=segment.reference_name,
218+
threshold=args.primer_match_threshold,
219+
)
210220
# p2 = find_primer(bed, segment.reference_end, "-", segment.reference_name, args.primer_match_threshold)
211-
p2 = find_primer(bed=bed, pos=segment.reference_end, direction="-", chrom=segment.reference_name, threshold=args.primer_match_threshold)
221+
p2 = find_primer(
222+
bed=bed,
223+
pos=segment.reference_end,
224+
direction="-",
225+
chrom=segment.reference_name,
226+
threshold=args.primer_match_threshold,
227+
)
212228

213229
if not p1 or not p2:
214230
if args.verbose:
@@ -361,7 +377,9 @@ def generate_amplicons(bed: list):
361377
for chrom, amplicons_dict in amplicons.items():
362378
for amplicon in amplicons_dict:
363379
if not all([x in amplicons_dict[amplicon] for x in ["p_start", "p_end"]]):
364-
raise ValueError(f"Primer scheme for amplicon {amplicon} for reference {chrom} is incomplete")
380+
raise ValueError(
381+
f"Primer scheme for amplicon {amplicon} for reference {chrom} is incomplete"
382+
)
365383

366384
# Check if primer runs accross reference start / end -> circular virus
367385
amplicons_dict[amplicon]["circular"] = (
@@ -408,7 +426,9 @@ def normalise(trimmed_segments: dict, normalise: int, bed: list, verbose: bool =
408426
(amplicons[chrom][amplicon]["length"],), normalise, dtype=int
409427
)
410428

411-
amplicon_depth = np.zeros((amplicons[chrom][amplicon]["length"],), dtype=int)
429+
amplicon_depth = np.zeros(
430+
(amplicons[chrom][amplicon]["length"],), dtype=int
431+
)
412432

413433
if not segments:
414434
if verbose:
@@ -425,12 +445,16 @@ def normalise(trimmed_segments: dict, normalise: int, bed: list, verbose: bool =
425445
for segment in segments:
426446
test_depths = np.copy(amplicon_depth)
427447

428-
relative_start = segment.reference_start - amplicons[chrom][amplicon]["p_start"]
448+
relative_start = (
449+
segment.reference_start - amplicons[chrom][amplicon]["p_start"]
450+
)
429451

430452
if relative_start < 0:
431453
relative_start = 0
432454

433-
relative_end = segment.reference_end - amplicons[chrom][amplicon]["p_start"]
455+
relative_end = (
456+
segment.reference_end - amplicons[chrom][amplicon]["p_start"]
457+
)
434458

435459
test_depths[relative_start:relative_end] += 1
436460

@@ -442,7 +466,7 @@ def normalise(trimmed_segments: dict, normalise: int, bed: list, verbose: bool =
442466
output_segments.append(segment)
443467

444468
mean_depths[(chrom, amplicon)] = np.mean(amplicon_depth)
445-
469+
446470
return output_segments, mean_depths
447471

448472

@@ -519,7 +543,9 @@ def go(args):
519543
trimmed_segments[trimmed_segment.reference_name].setdefault(amplicon, [])
520544

521545
if trimmed_segment:
522-
trimmed_segments[trimmed_segment.reference_name][amplicon].append(trimmed_segment)
546+
trimmed_segments[trimmed_segment.reference_name][amplicon].append(
547+
trimmed_segment
548+
)
523549

524550
# normalise if requested
525551
if args.normalise:
@@ -536,10 +562,12 @@ def go(args):
536562

537563
for output_segment in output_segments:
538564
outfile.write(output_segment)
565+
539566
else:
540-
for amplicon, segments in trimmed_segments.items():
541-
for segment in segments:
542-
outfile.write(segment)
567+
for chrom, amplicon_dict in trimmed_segments.items():
568+
for amplicon, segments in amplicon_dict.items():
569+
for segment in segments:
570+
outfile.write(segment)
543571

544572
# close up the file handles
545573
infile.close()

artic/make_depth_mask.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ def collect_depths(bamfile, refName, minDepth, ignoreDeletions, warnRGcov):
6565

6666
# generate the pileup
6767
for pileupcolumn in bamFile.pileup(
68-
refName, start=0, stop=bamFile.get_reference_length(refName), max_depth=10000, truncate=False, min_base_quality=0
68+
refName,
69+
start=0,
70+
stop=bamFile.get_reference_length(refName),
71+
max_depth=100000000,
72+
truncate=False,
73+
min_base_quality=0,
6974
):
70-
7175
# process the pileup column
7276
for pileupread in pileupcolumn.pileups:
7377

@@ -78,13 +82,16 @@ def collect_depths(bamfile, refName, minDepth, ignoreDeletions, warnRGcov):
7882
# process the pileup read
7983
if pileupread.is_refskip:
8084
continue
85+
8186
if pileupread.is_del:
8287
if not ignoreDeletions:
8388
depths[pileupcolumn.pos] += 1
8489
rgDepths[rg][pileupcolumn.pos] += 1
90+
8591
elif not pileupread.is_del:
8692
depths[pileupcolumn.pos] += 1
8793
rgDepths[rg][pileupcolumn.pos] += 1
94+
8895
else:
8996
raise Exception("unhandled pileup read encountered")
9097

artic/minion.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ def run(parser, args):
4444
)
4545
raise SystemExit(1)
4646

47+
if args.normalise > 10000:
48+
print(
49+
colored.red(
50+
"--normalise appears to be an aribtrarily high value, if you wish to not normalise your BAM files set '--normalise 0' to disable normalisation entirely"
51+
),
52+
file=sys.stderr,
53+
)
54+
raise SystemExit(1)
55+
4756
if args.model_dir:
4857
model_path = args.model_dir
4958
else:
@@ -169,7 +178,7 @@ def run(parser, args):
169178
cmds.append(f"samtools index {args.sample}.{p}.trimmed.rg.sorted.bam")
170179

171180
cmds.append(
172-
f"run_clair3.sh --enable_long_indel --chunk_size=10000 --haploid_precise --no_phasing_for_fa --bam_fn='{args.sample}.{p}.trimmed.rg.sorted.bam' --ref_fn='{ref}' --output='{args.sample}_rg_{p}' --threads='{args.threads}' --platform='ont' --model_path='{full_model_path}' --include_all_ctgs --min_coverage='{args.min_depth}'"
181+
f"run_clair3.sh --enable_long_indel --chunk_size=10000 --haploid_precise --no_phasing_for_fa --bam_fn='{args.sample}.{p}.trimmed.rg.sorted.bam' --ref_fn='{ref}' --output='{args.sample}_rg_{p}' --threads='{args.threads}' --platform='ont' --model_path='{full_model_path}' --include_all_ctgs"
173182
)
174183

175184
cmds.append(
@@ -199,7 +208,7 @@ def run(parser, args):
199208

200209
# 9) get the depth of coverage for each readgroup, create a coverage mask and plots, and add failed variants to the coverage mask (artic_mask must be run before bcftools consensus)
201210
cmds.append(
202-
f"artic_make_depth_mask --store-rg-depths {ref} {args.sample}.primertrimmed.rg.sorted.bam {args.sample}.coverage_mask.txt"
211+
f"artic_make_depth_mask --depth {args.min_depth} --store-rg-depths {ref} {args.sample}.primertrimmed.rg.sorted.bam {args.sample}.coverage_mask.txt"
203212
)
204213

205214
cmds.append(

artic/pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def init_pipeline_parser():
102102
"--min-depth",
103103
type=int,
104104
default=20,
105-
help="Minimum depth required to call a variant (default: %(default)d)",
105+
help="Minimum coverage required for a position to be included in the consensus sequence (default: %(default)d)",
106106
)
107107
parser_minion.add_argument(
108108
"--threads",

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = artic
3-
version = 1.6.0
3+
version = 1.6.1
44
author = Nick Loman
55
author_email = [email protected]
66
maintainer = Sam Wilkinson

0 commit comments

Comments
 (0)