NextPyper 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nextpyper/__init__.py +0 -0
- nextpyper/__main__.py +429 -0
- nextpyper/config/config.yaml +80 -0
- nextpyper/config/rich_config.yaml +54 -0
- nextpyper/config/system_config.yaml +10 -0
- nextpyper/data/blosum62.csv +32 -0
- nextpyper/data/cps_seqids.csv +674 -0
- nextpyper/data/mock/mock_args.yaml +17 -0
- nextpyper/data/mock/probes.fasta +56 -0
- nextpyper/data/mock/reads/sample1_R1.fastq +20 -0
- nextpyper/data/mock/reads/sample1_R2.fastq +20 -0
- nextpyper/data/mock/reads/sample2_R1.fastq +20 -0
- nextpyper/data/mock/reads/sample2_R2.fastq +20 -0
- nextpyper/data/mock/reads/sample3_R1.fastq +20 -0
- nextpyper/data/mock/reads/sample3_R2.fastq +20 -0
- nextpyper/data/mock/sample.tsv +4 -0
- nextpyper/nextpyper.CITATION +9 -0
- nextpyper/nextpyper.LICENSE +19 -0
- nextpyper/nextpyper.VERSION +1 -0
- nextpyper/src/divergence_estimation.py +131 -0
- nextpyper/src/diversity.py +93 -0
- nextpyper/src/exon_discovery.py +294 -0
- nextpyper/src/exon_intron.py +197 -0
- nextpyper/src/fix_headers.py +107 -0
- nextpyper/src/gfa2fasta.py +71 -0
- nextpyper/src/gfa_graph.py +1245 -0
- nextpyper/src/gff_parser.py +344 -0
- nextpyper/src/graph_utils.py +201 -0
- nextpyper/src/homolog_filtering.py +411 -0
- nextpyper/src/miniprot.py +1008 -0
- nextpyper/src/multi_seq_probes.py +292 -0
- nextpyper/src/run_orfipy.py +493 -0
- nextpyper/src/sample_table.py +63 -0
- nextpyper/src/seeds_collection.py +387 -0
- nextpyper/src/summarize_results.py +507 -0
- nextpyper/src/union_find.py +65 -0
- nextpyper/src/var_asm_parser.py +229 -0
- nextpyper/test/.gitkeep +0 -0
- nextpyper/workflow/Snakefile +62 -0
- nextpyper/workflow/envs/alignment.yaml +7 -0
- nextpyper/workflow/envs/assembly.yaml +8 -0
- nextpyper/workflow/envs/clustering.yaml +9 -0
- nextpyper/workflow/envs/matching.yaml +8 -0
- nextpyper/workflow/envs/preprocessing.yaml +11 -0
- nextpyper/workflow/envs/saute.yaml +7 -0
- nextpyper/workflow/envs/translating.yaml +8 -0
- nextpyper/workflow/rules/0_preprocessing/0.1_reads.smk +90 -0
- nextpyper/workflow/rules/0_preprocessing/0.2_probes.smk +16 -0
- nextpyper/workflow/rules/1_seeds_assembly/1.1_assembly_spades.smk +48 -0
- nextpyper/workflow/rules/1_seeds_assembly/1.2_seeds_extension.smk +90 -0
- nextpyper/workflow/rules/1_seeds_assembly/1.3_seeds_filtering.smk +64 -0
- nextpyper/workflow/rules/1_seeds_assembly/1.4_seeds_clustering.smk +73 -0
- nextpyper/workflow/rules/2_homologs_assembly/2.1_saute_assembly.smk +228 -0
- nextpyper/workflow/rules/3_homolog_prospection/3.1_allele_collapsing.smk +23 -0
- nextpyper/workflow/rules/3_homolog_prospection/3.2_homologs_filtering.smk +76 -0
- nextpyper/workflow/rules/3_homolog_prospection/3.3_region_separation.smk +189 -0
- nextpyper/workflow/rules/common.smk +160 -0
- nextpyper/workflow/schemas/probes.yaml +12 -0
- nextpyper/workflow/schemas/sample_table.yaml +29 -0
- nextpyper/workflow/scripts/bench_busco.py +572 -0
- nextpyper/workflow/scripts/categories_bench.sh +92 -0
- nextpyper/workflow/scripts/end_recs_parser.py +146 -0
- nextpyper/workflow/scripts/tribble_stats.py +131 -0
- nextpyper-0.1.0.dist-info/METADATA +231 -0
- nextpyper-0.1.0.dist-info/RECORD +69 -0
- nextpyper-0.1.0.dist-info/WHEEL +5 -0
- nextpyper-0.1.0.dist-info/entry_points.txt +3 -0
- nextpyper-0.1.0.dist-info/licenses/nextpyper/nextpyper.LICENSE +19 -0
- nextpyper-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
#
|
|
5
|
+
# Copyright (C) 2024
|
|
6
|
+
# Simón Villanueva CORRALES: simon.corrales@ibot.cas.cz
|
|
7
|
+
# Yann J.K. BERTRAND: yjk_bertrand@ybertrand.org
|
|
8
|
+
#
|
|
9
|
+
# All rights reserved.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
"""Parse saute assembly and do multiple operations on them, including collapsing alleles,
|
|
13
|
+
limiting the number of variants per component, and splitting into well behaved "normal"
|
|
14
|
+
and tribble components."""
|
|
15
|
+
|
|
16
|
+
__version__ = "0.1"
|
|
17
|
+
|
|
18
|
+
# =======================================================================================
|
|
19
|
+
# IMPORTS
|
|
20
|
+
# =======================================================================================
|
|
21
|
+
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
import sys
|
|
24
|
+
|
|
25
|
+
import polars as pl
|
|
26
|
+
from Bio import SeqIO
|
|
27
|
+
from Bio.SeqRecord import SeqRecord
|
|
28
|
+
from more_itertools import one
|
|
29
|
+
|
|
30
|
+
# =======================================================================================
|
|
31
|
+
# CONSTANTS
|
|
32
|
+
# =======================================================================================
|
|
33
|
+
|
|
34
|
+
FIELDS = {
|
|
35
|
+
"seed": str,
|
|
36
|
+
"probe": str,
|
|
37
|
+
"seed_id": int,
|
|
38
|
+
"seed_len": int,
|
|
39
|
+
"cov": float,
|
|
40
|
+
"comp": int,
|
|
41
|
+
"ctg1": int,
|
|
42
|
+
"ctg2": int,
|
|
43
|
+
"kmers": int,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
TARGET_PAT = r"^Contig_(?P<seed>.*?)-(?P<probe>.*?)_EDGE_(?P<seed_id>\d+)_length_(?P<seed_len>\d+)_cov_(?P<cov>[\w.]+):(?P<comp>\d+):(?P<ctg1>\d+):(?P<ctg2>\d+):(?P<kmers>\d+)$"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# =======================================================================================
|
|
50
|
+
# FUNCTIONS
|
|
51
|
+
# =======================================================================================
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def query2df(recs: list[SeqRecord], pat: str, schema: dict[str, str]) -> pl.DataFrame:
|
|
55
|
+
recs_ids, lens = zip(*map(lambda rec: (rec.id, len(rec)), recs))
|
|
56
|
+
return (
|
|
57
|
+
pl.DataFrame(data={"query": recs_ids, "len": lens})
|
|
58
|
+
.with_columns(pl.col("query").str.extract_groups(pat).struct.unnest())
|
|
59
|
+
.cast(schema)
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def collapse_alleles_df(df: pl.DataFrame) -> pl.DataFrame:
|
|
64
|
+
"""Group the sequences from the same component with the same length and pick the one
|
|
65
|
+
with the highest kmer count. It is assumed that these sequences are different alleles.
|
|
66
|
+
|
|
67
|
+
Return the dataframe with the collapsed alleles."""
|
|
68
|
+
|
|
69
|
+
return df.group_by("probe", "seed", "seed_id", "comp", "len").agg(
|
|
70
|
+
pl.col("query").gather(pl.col("kmers").arg_max()).first(),
|
|
71
|
+
pl.col("kmers").max(),
|
|
72
|
+
count=pl.len(),
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def collapse_variants_df(df: pl.DataFrame, max_vars: int = 1) -> pl.DataFrame:
|
|
77
|
+
"""Given a dataframe with already collapsed alleles, further collapse the variants
|
|
78
|
+
in the same component.
|
|
79
|
+
|
|
80
|
+
Return a DataFrame with the collapsed variants
|
|
81
|
+
"""
|
|
82
|
+
return (
|
|
83
|
+
df.group_by("probe", "seed", "seed_id", "comp")
|
|
84
|
+
.agg(
|
|
85
|
+
pl.all().sort_by("kmers", descending=True).head(max_vars),
|
|
86
|
+
var_count=pl.len(),
|
|
87
|
+
allele_count=pl.sum("count"),
|
|
88
|
+
)
|
|
89
|
+
.explode("len", "query", "kmers", "count")
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def collapse_records(recs: list[SeqRecord], df: pl.DataFrame) -> list[SeqRecord]:
|
|
94
|
+
"""Group the sequences from the same component with the same length and pick the one
|
|
95
|
+
with the highest kmer count. It is assumed that these sequences are different alleles.
|
|
96
|
+
|
|
97
|
+
Return a reduced set of records with the selected alleles."""
|
|
98
|
+
|
|
99
|
+
return [rec for rec in recs if rec.id in set(df["query"])]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def split_tribble_probes(
|
|
103
|
+
df: pl.DataFrame, tribble_limit: int
|
|
104
|
+
) -> tuple[list[SeqRecord], list[SeqRecord]]:
|
|
105
|
+
|
|
106
|
+
allele_df = collapse_alleles_df(df)
|
|
107
|
+
var_df = collapse_variants_df(allele_df)
|
|
108
|
+
|
|
109
|
+
tribble_probes = (
|
|
110
|
+
var_df.filter(pl.col("var_count") >= tribble_limit).select("probe").unique()
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
print(f"Total probes: {var_df["probe"].n_unique()}. Setting {tribble_limit=}")
|
|
114
|
+
print(f"Normal probes: {var_df["probe"].n_unique() - len(tribble_probes)}")
|
|
115
|
+
print(f"Tribble probes: {len(tribble_probes)}")
|
|
116
|
+
|
|
117
|
+
normal_df = df.join(tribble_probes, on="probe", how="anti")
|
|
118
|
+
tribble_df = df.join(tribble_probes, on="probe")
|
|
119
|
+
|
|
120
|
+
return normal_df, tribble_df
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def snakemake_call(snakemake):
|
|
124
|
+
with open(snakemake.log[0], "w") as outlog:
|
|
125
|
+
sys.stdout = sys.stderr = outlog
|
|
126
|
+
|
|
127
|
+
records_path = Path(snakemake.input[0])
|
|
128
|
+
out_path = Path(snakemake.output.normal)
|
|
129
|
+
tribbles_path = snakemake.output.get("tribbles")
|
|
130
|
+
tribble_stats_path = snakemake.output.get("tribble_stats")
|
|
131
|
+
|
|
132
|
+
# Mandatory parameters
|
|
133
|
+
pattern = snakemake.params.get("pattern", TARGET_PAT)
|
|
134
|
+
mode = snakemake.params.get("mode", "collapse")
|
|
135
|
+
|
|
136
|
+
# Optional parameters (required according to mode)
|
|
137
|
+
empty_ok = snakemake.params.get("empty_ok", False)
|
|
138
|
+
collapse_vars = snakemake.params.get("collapse_vars", False)
|
|
139
|
+
max_vars = one(snakemake.params.get("max_vars", [10]))
|
|
140
|
+
|
|
141
|
+
recs = list(SeqIO.parse(records_path, "fasta"))
|
|
142
|
+
|
|
143
|
+
print(f"Starting sequences: {len(recs)}")
|
|
144
|
+
print(f"{mode=}")
|
|
145
|
+
|
|
146
|
+
# If the input is empty and we allow for it, just touch the output
|
|
147
|
+
if empty_ok and len(recs) == 0:
|
|
148
|
+
print("Exiting without error due to empty input.")
|
|
149
|
+
out_path.touch()
|
|
150
|
+
sys.exit(0)
|
|
151
|
+
|
|
152
|
+
df = query2df(recs, pattern, FIELDS)
|
|
153
|
+
|
|
154
|
+
match mode:
|
|
155
|
+
case "collapse":
|
|
156
|
+
print(f"{collapse_vars=}")
|
|
157
|
+
allele_df = collapse_alleles_df(df)
|
|
158
|
+
print(f"Sequences after allele collapsing: {len(allele_df)}")
|
|
159
|
+
|
|
160
|
+
if collapse_vars:
|
|
161
|
+
var_df = collapse_variants_df(allele_df, max_vars)
|
|
162
|
+
new_recs = collapse_records(recs, var_df)
|
|
163
|
+
print(f"Sequences after variant collapsing: {len(var_df)}")
|
|
164
|
+
else:
|
|
165
|
+
new_recs = collapse_records(recs, allele_df)
|
|
166
|
+
|
|
167
|
+
SeqIO.write(new_recs, out_path, "fasta")
|
|
168
|
+
|
|
169
|
+
case "split" | "cap":
|
|
170
|
+
if mode == "split" and tribbles_path is None:
|
|
171
|
+
raise ValueError(f"For {mode=}, two outputs have to be specified.")
|
|
172
|
+
|
|
173
|
+
print(f"Splitting into normal and tribble sequence sets ({max_vars=})")
|
|
174
|
+
normal_df, tribble_df = split_tribble_probes(df, max_vars)
|
|
175
|
+
|
|
176
|
+
print(f"Sequences in normal set: {len(normal_df)}")
|
|
177
|
+
print(f"Sequences in tribble set: {len(tribble_df)}")
|
|
178
|
+
tribble_allele_df = collapse_alleles_df(tribble_df)
|
|
179
|
+
print(
|
|
180
|
+
f"Sequences in tribble set (allele collapsed): {len(tribble_allele_df)}"
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
if mode == "cap":
|
|
184
|
+
tribble_var_df = collapse_variants_df(tribble_allele_df, max_vars)
|
|
185
|
+
print(
|
|
186
|
+
f"Sequences in tribble set (after capping to {max_vars=}): {len(tribble_var_df)}"
|
|
187
|
+
)
|
|
188
|
+
if tribble_stats_path is not None:
|
|
189
|
+
tribble_allele_df.select(
|
|
190
|
+
"probe", "seed", "seed_id", "comp"
|
|
191
|
+
).unique().write_csv(tribble_stats_path, separator="\t")
|
|
192
|
+
|
|
193
|
+
final_df = normal_df.select("query").vstack(
|
|
194
|
+
tribble_var_df.select("query")
|
|
195
|
+
)
|
|
196
|
+
final_recs = collapse_records(recs, final_df)
|
|
197
|
+
|
|
198
|
+
SeqIO.write(final_recs, out_path, "fasta")
|
|
199
|
+
else:
|
|
200
|
+
|
|
201
|
+
normal_recs = collapse_records(recs, normal_df)
|
|
202
|
+
tribble_recs = collapse_records(recs, tribble_allele_df)
|
|
203
|
+
|
|
204
|
+
SeqIO.write(normal_recs, out_path, "fasta")
|
|
205
|
+
SeqIO.write(tribble_recs, Path(tribbles_path), "fasta")
|
|
206
|
+
|
|
207
|
+
case _:
|
|
208
|
+
raise NotImplementedError(f"{mode=} not implemented.")
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def main():
|
|
212
|
+
if len(sys.argv) != 3:
|
|
213
|
+
print("Usage: python var_asm_parser.py <saute_asm.fasta> <output.fasta>")
|
|
214
|
+
sys.exit(1)
|
|
215
|
+
|
|
216
|
+
records_path = Path(sys.argv[1])
|
|
217
|
+
out_path = Path(sys.argv[2])
|
|
218
|
+
|
|
219
|
+
recs = list(SeqIO.parse(records_path, "fasta"))
|
|
220
|
+
df = query2df(recs, TARGET_PAT, FIELDS)
|
|
221
|
+
new_recs = collapse_records(recs, df)
|
|
222
|
+
SeqIO.write(new_recs, out_path, "fasta")
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
if __name__ == "__main__":
|
|
226
|
+
if "snakemake" in globals():
|
|
227
|
+
snakemake_call(snakemake)
|
|
228
|
+
else:
|
|
229
|
+
main()
|
nextpyper/test/.gitkeep
ADDED
|
File without changes
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Update default config with runtime config
|
|
2
|
+
configfile: Path(workflow.basedir) / "../config/config.yaml"
|
|
3
|
+
configfile: Path(workflow.basedir) / "../config/system_config.yaml"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
config.update(config["nextpyper"]) # convenience if using namespaced config
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Rules files
|
|
10
|
+
include: Path(workflow.basedir) / "rules" / "common.smk"
|
|
11
|
+
include: Path(workflow.basedir) / "rules/0_preprocessing/0.1_reads.smk"
|
|
12
|
+
include: Path(workflow.basedir) / "rules/0_preprocessing/0.2_probes.smk"
|
|
13
|
+
include: Path(workflow.basedir) / "rules/1_seeds_assembly/1.1_assembly_spades.smk"
|
|
14
|
+
include: Path(workflow.basedir) / "rules/1_seeds_assembly/1.2_seeds_extension.smk"
|
|
15
|
+
include: Path(workflow.basedir) / "rules/1_seeds_assembly/1.3_seeds_filtering.smk"
|
|
16
|
+
include: Path(workflow.basedir) / "rules/1_seeds_assembly/1.4_seeds_clustering.smk"
|
|
17
|
+
include: Path(workflow.basedir) / "rules/2_homologs_assembly/2.1_saute_assembly.smk"
|
|
18
|
+
include: Path(workflow.basedir) / "rules/3_homolog_prospection/3.1_allele_collapsing.smk"
|
|
19
|
+
include: Path(workflow.basedir) / "rules/3_homolog_prospection/3.2_homologs_filtering.smk"
|
|
20
|
+
include: Path(workflow.basedir) / "rules/3_homolog_prospection/3.3_region_separation.smk"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## List of outputs
|
|
24
|
+
targets = []
|
|
25
|
+
|
|
26
|
+
targets.append(
|
|
27
|
+
expand("<workdir>/3_homolog_prospection/33_alns/{probes}", probes=probes_list)
|
|
28
|
+
)
|
|
29
|
+
targets.append(
|
|
30
|
+
expand(
|
|
31
|
+
"<workdir>/3_homolog_prospection/34_collating_homologs/341_coverage/{kind}/{sample}.metabat",
|
|
32
|
+
sample=sample_list,
|
|
33
|
+
kind=("exons", "genetigs", "supercontigs"),
|
|
34
|
+
)
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# Target rules
|
|
39
|
+
rule all:
|
|
40
|
+
input:
|
|
41
|
+
targets,
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
onsuccess:
|
|
45
|
+
# Make final output directory and link the main results
|
|
46
|
+
(outdir / "outputs").mkdir(exist_ok=True)
|
|
47
|
+
shell(
|
|
48
|
+
f"ln -sf ../workflow/3_homolog_prospection/33_alns {outdir / 'outputs/alignments'}"
|
|
49
|
+
)
|
|
50
|
+
shell(
|
|
51
|
+
f"ln -sf ../workflow/3_homolog_prospection/32_region_separation/321_output/3211_scfs {outdir / 'outputs/seqs_per_locus'}"
|
|
52
|
+
)
|
|
53
|
+
shell(
|
|
54
|
+
f"ln -sf ../workflow/3_homolog_prospection/34_collating_homologs/340_per_sample {outdir / 'outputs/seqs_per_sample'}"
|
|
55
|
+
)
|
|
56
|
+
print("Workflow finished, without error")
|
|
57
|
+
shell(f"cp {{log}} {outdir/ 'execution.log'}")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
onerror:
|
|
61
|
+
print("An error occurred")
|
|
62
|
+
shell(f"cp {{log}} {outdir/ 'execution.log'}")
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
pathvars:
|
|
2
|
+
stage="0_preprocessing",
|
|
3
|
+
results="<workdir>/<stage>",
|
|
4
|
+
|
|
5
|
+
rule fastp_pe:
|
|
6
|
+
input:
|
|
7
|
+
in1=lookup(query="sample=='{sample}'", cols="path_forward", within=sample_table),
|
|
8
|
+
in2=lookup(query="sample=='{sample}'", cols="path_reverse", within=sample_table),
|
|
9
|
+
output:
|
|
10
|
+
trim1="<results>/01_trimming/{sample}_R1.fastq.gz",
|
|
11
|
+
trim2="<results>/01_trimming/{sample}_R2.fastq.gz",
|
|
12
|
+
html="<logs>/<stage>/01_trimming_fastp/{sample}.html",
|
|
13
|
+
json="<logs>/<stage>/01_trimming_fastp/{sample}.json",
|
|
14
|
+
log:
|
|
15
|
+
"<logs>/<stage>/01_trimming_fastp/{sample}.log",
|
|
16
|
+
params:
|
|
17
|
+
trim_qual=lookup("preprocessing/trim_qual", within=pipeline),
|
|
18
|
+
trim_min_len=lookup("preprocessing/min_len", within=pipeline),
|
|
19
|
+
extra="--trim_poly_g --trim_poly_x --low_complexity_filter --cut_tail",
|
|
20
|
+
threads: 4
|
|
21
|
+
conda:
|
|
22
|
+
"../../envs/preprocessing.yaml"
|
|
23
|
+
shell:
|
|
24
|
+
"""fastp --thread {threads} {params.extra} \
|
|
25
|
+
--cut_mean_quality {params.trim_qual} \
|
|
26
|
+
--length_required {params.trim_min_len} \
|
|
27
|
+
--in1 {input.in1} --in2 {input.in2} \
|
|
28
|
+
--out1 {output.trim1} --out2 {output.trim2} \
|
|
29
|
+
--html {output.html} \
|
|
30
|
+
--json {output.json} 2> {log}
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
checkpoint prepare_cps:
|
|
35
|
+
output:
|
|
36
|
+
"<results>/ref_cps.fasta",
|
|
37
|
+
log:
|
|
38
|
+
"<logs>/<stage>/cps.log",
|
|
39
|
+
params:
|
|
40
|
+
cp_refs_map=cp_refs_map,
|
|
41
|
+
custom=lookup("args/custom_cps", within=config),
|
|
42
|
+
retries: 5
|
|
43
|
+
run:
|
|
44
|
+
out_cps = Path(output[0])
|
|
45
|
+
custom_cps = params.custom
|
|
46
|
+
|
|
47
|
+
with open(log[0], "w") as outlog:
|
|
48
|
+
sys.stdout = sys.stderr = outlog
|
|
49
|
+
cps = []
|
|
50
|
+
|
|
51
|
+
if use_ref_cps and seqids:
|
|
52
|
+
handle = Entrez.efetch(
|
|
53
|
+
db="nuccore", id=seqids, rettype="fasta", retmode="text"
|
|
54
|
+
)
|
|
55
|
+
cps += list(SeqIO.parse(handle, "fasta"))
|
|
56
|
+
|
|
57
|
+
# If custom cps are given add them to the downloaded ones
|
|
58
|
+
if custom_cps:
|
|
59
|
+
cps += list(SeqIO.parse(custom_cps, "fasta"))
|
|
60
|
+
|
|
61
|
+
SeqIO.write(cps, out_cps, "fasta")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
rule reads_cp_cleaning:
|
|
65
|
+
input:
|
|
66
|
+
ref="<results>/ref_cps.fasta",
|
|
67
|
+
trim1="<results>/01_trimming/{sample}_R1.fastq.gz",
|
|
68
|
+
trim2="<results>/01_trimming/{sample}_R2.fastq.gz",
|
|
69
|
+
output:
|
|
70
|
+
clean1="<results>/02_cleaning/{sample}_R1.fastq.gz",
|
|
71
|
+
clean2="<results>/02_cleaning/{sample}_R2.fastq.gz",
|
|
72
|
+
log:
|
|
73
|
+
"<logs>/<stage>/02_cleaning/{sample}.log",
|
|
74
|
+
conda:
|
|
75
|
+
"../../envs/preprocessing.yaml"
|
|
76
|
+
threads: 4
|
|
77
|
+
run:
|
|
78
|
+
if Path(input.ref).stat().st_size > 0:
|
|
79
|
+
shell(
|
|
80
|
+
"""minimap2 -t {threads} -ax sr {input.ref} {input.trim1} {input.trim2} 2> {log} | \
|
|
81
|
+
samtools view -u -e 'flag & 4 || flag & 8' 2> {log} | \
|
|
82
|
+
samtools fastq -1 {output.clean1} -2 {output.clean2} 2> {log} """
|
|
83
|
+
)
|
|
84
|
+
else:
|
|
85
|
+
Path(output.clean1).symlink_to(
|
|
86
|
+
Path(input.trim1).relative_to(Path(output.clean1).parent, walk_up=True)
|
|
87
|
+
)
|
|
88
|
+
Path(output.clean2).symlink_to(
|
|
89
|
+
Path(input.trim2).relative_to(Path(output.clean2).parent, walk_up=True)
|
|
90
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
rule translate_probes:
|
|
2
|
+
input:
|
|
3
|
+
probes_path.resolve(),
|
|
4
|
+
output:
|
|
5
|
+
"<results>/longest_cds.fasta",
|
|
6
|
+
params:
|
|
7
|
+
translated_prop=lookup("probes_translation/translated_prop", within=pipeline),
|
|
8
|
+
stop_per_1Kbp=lookup("probes_translation/stop_per_1Kbp", within=pipeline),
|
|
9
|
+
min_exon_length=lookup("probes_translation/min_exon_length", within=pipeline),
|
|
10
|
+
log:
|
|
11
|
+
"<logs>/<stage>/translated_cds.log",
|
|
12
|
+
threads: 1
|
|
13
|
+
conda:
|
|
14
|
+
"../../envs/translating.yaml"
|
|
15
|
+
script:
|
|
16
|
+
"../../../src/run_orfipy.py"
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
pathvars:
|
|
2
|
+
stage="1_assembly/10_raw_assembly",
|
|
3
|
+
results="<workdir>/<stage>",
|
|
4
|
+
|
|
5
|
+
def select_asm_kmer(wildcards, input):
|
|
6
|
+
spades_k = lookup("spades/k", within=pipeline)
|
|
7
|
+
if spades_k == "auto":
|
|
8
|
+
summary = json.loads(Path(input.json).read_text())
|
|
9
|
+
k_mid = int(summary["summary"]["after_filtering"]["read2_mean_length"]) // 2
|
|
10
|
+
if k_mid >= 117:
|
|
11
|
+
return "21,45,107,127"
|
|
12
|
+
elif k_mid % 2 == 0:
|
|
13
|
+
return f"21,45,{k_mid-9},{k_mid+11}"
|
|
14
|
+
else:
|
|
15
|
+
return f"21,45,{k_mid-10},{k_mid+10}"
|
|
16
|
+
else:
|
|
17
|
+
return spades_k
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
rule spades_assembly:
|
|
21
|
+
input:
|
|
22
|
+
in1="<workdir>/0_preprocessing/02_cleaning/{sample}_R1.fastq.gz",
|
|
23
|
+
in2="<workdir>/0_preprocessing/02_cleaning/{sample}_R2.fastq.gz",
|
|
24
|
+
json="<logs>/0_preprocessing/01_trimming_fastp/{sample}.json",
|
|
25
|
+
output:
|
|
26
|
+
graph="<results>/100_spades/{sample}/assembly_graph_with_scaffolds.gfa",
|
|
27
|
+
params:
|
|
28
|
+
k=select_asm_kmer,
|
|
29
|
+
out_dir=subpath(output.graph, parent=True),
|
|
30
|
+
extra="--meta --only-assembler",
|
|
31
|
+
log:
|
|
32
|
+
"<logs>/<stage>/100_spades/{sample}.log",
|
|
33
|
+
threads: 4
|
|
34
|
+
shadow:
|
|
35
|
+
"minimal"
|
|
36
|
+
conda:
|
|
37
|
+
"../../envs/assembly.yaml"
|
|
38
|
+
shell:
|
|
39
|
+
"spades.py -t {threads} {params.extra} -k {params.k} -1 {input.in1} -2 {input.in2} -o {params.out_dir} > {log} 2>&1"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
rule make_assembly_scaffolds:
|
|
43
|
+
input:
|
|
44
|
+
"<results>/100_spades/{sample}/assembly_graph_with_scaffolds.gfa",
|
|
45
|
+
output:
|
|
46
|
+
"<results>/101_scaffolds/{sample}.fasta",
|
|
47
|
+
script:
|
|
48
|
+
"../../../src/gfa2fasta.py"
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
pathvars:
|
|
2
|
+
stage="1_assembly/11_extension",
|
|
3
|
+
results="<workdir>/<stage>",
|
|
4
|
+
|
|
5
|
+
rule make_diamond_probes_db:
|
|
6
|
+
input:
|
|
7
|
+
"<workdir>/0_preprocessing/longest_cds.fasta",
|
|
8
|
+
output:
|
|
9
|
+
"<workdir>/1_assembly/probes.dmnd",
|
|
10
|
+
log:
|
|
11
|
+
"<logs>/1_assembly/diamond_makedb.log",
|
|
12
|
+
params:
|
|
13
|
+
subpath(output[0], strip_suffix=".dmnd"),
|
|
14
|
+
conda:
|
|
15
|
+
"../../envs/matching.yaml"
|
|
16
|
+
shell:
|
|
17
|
+
"diamond makedb --db {params} --in {input} > {log} 2>&1"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
rule raw_assembly_to_probes_matching:
|
|
21
|
+
input:
|
|
22
|
+
probes="<workdir>/1_assembly/probes.dmnd",
|
|
23
|
+
query="<workdir>/1_assembly/10_raw_assembly/101_scaffolds/{sample}.fasta",
|
|
24
|
+
output:
|
|
25
|
+
"<results>/110_raw_matching_tables/{sample}.tsv",
|
|
26
|
+
params:
|
|
27
|
+
fields=lookup("diamond_matching/fields", within=pipeline),
|
|
28
|
+
sensitivity=lookup("diamond_matching/sensitivity", within=pipeline),
|
|
29
|
+
evalue=lookup("diamond_matching/evalue", within=pipeline),
|
|
30
|
+
max_hsps=lookup("diamond_matching/max_hsps", within=pipeline),
|
|
31
|
+
gapopen=lookup("diamond_matching/gapopen", within=pipeline),
|
|
32
|
+
frameshift=lookup("diamond_matching/frameshift", within=pipeline),
|
|
33
|
+
min_orf_len=lookup("diamond_matching/min_orf_len", within=pipeline),
|
|
34
|
+
log:
|
|
35
|
+
"<logs>/<stage>/110_raw_matching_diamond/{sample}.log",
|
|
36
|
+
threads: 4
|
|
37
|
+
conda:
|
|
38
|
+
"../../envs/matching.yaml"
|
|
39
|
+
shell:
|
|
40
|
+
"""
|
|
41
|
+
diamond blastx \
|
|
42
|
+
--threads {threads} \
|
|
43
|
+
--db {input.probes} --query {input.query} --out {output} \
|
|
44
|
+
{params.sensitivity} \
|
|
45
|
+
--evalue {params.evalue} \
|
|
46
|
+
--max-hsps {params.max_hsps} \
|
|
47
|
+
--min-orf {params.min_orf_len} \
|
|
48
|
+
--gapopen {params.gapopen} \
|
|
49
|
+
--frameshift {params.frameshift} \
|
|
50
|
+
--outfmt 6 {params.fields} > {log} 2>&1
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def read_insert_size(stats: Path) -> int:
|
|
55
|
+
insert_hist = json.loads(stats.read_text())["insert_size"]["histogram"]
|
|
56
|
+
return last(filter(itemgetter(1), enumerate(insert_hist)))[0]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def get_max_intron_size(wildcards, input):
|
|
60
|
+
"""Parametrize the maximum intron size that can be bridged during extension
|
|
61
|
+
by taking twice the maximum observed insert size of the data."""
|
|
62
|
+
|
|
63
|
+
return read_insert_size(Path(input.stats)) * 2
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
rule extend_paths:
|
|
67
|
+
input:
|
|
68
|
+
graph="<workdir>/1_assembly/10_raw_assembly/100_spades/{sample}/assembly_graph_with_scaffolds.gfa",
|
|
69
|
+
table="<results>/110_raw_matching_tables/{sample}.tsv",
|
|
70
|
+
stats="<logs>/0_preprocessing/01_trimming_fastp/{sample}.json",
|
|
71
|
+
output:
|
|
72
|
+
ext_table="<results>/111_paths_extension/1110_tables/{sample}.tsv",
|
|
73
|
+
ext_seqs="<results>/111_paths_extension/1111_sequences/{sample}.fasta",
|
|
74
|
+
params:
|
|
75
|
+
filter_low_dp_comps=lookup("filter_low_dp_comps", within=scf_ext),
|
|
76
|
+
floor_len=lookup("floor_len_extension", within=scf_ext),
|
|
77
|
+
ceil_len=lookup("ceiling_len_extension", within=scf_ext),
|
|
78
|
+
plen_scaling=lookup("probe_len_scaling", within=scf_ext),
|
|
79
|
+
max_extensions=lookup("max_extensions", within=scf_ext),
|
|
80
|
+
min_idt=lookup("scf_min_idt", within=seeds_filt_params),
|
|
81
|
+
max_intron_size=(
|
|
82
|
+
get_max_intron_size
|
|
83
|
+
if (size := lookup("max_intron_size", within=scf_ext)) == "auto"
|
|
84
|
+
else size
|
|
85
|
+
),
|
|
86
|
+
probe_pattern=lambda wildcards: probe_pattern,
|
|
87
|
+
log:
|
|
88
|
+
"<logs>/<stage>/111_path_extension/{sample}.log",
|
|
89
|
+
script:
|
|
90
|
+
"../../../src/gfa_graph.py"
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
pathvars:
|
|
2
|
+
stage="1_assembly/12_filtering",
|
|
3
|
+
results="<workdir>/<stage>",
|
|
4
|
+
inter="1_assembly/11_extension/111_paths_extension/1111_sequences"
|
|
5
|
+
|
|
6
|
+
use rule raw_assembly_to_probes_matching as seeds_to_probes_matching with:
|
|
7
|
+
input:
|
|
8
|
+
probes="<workdir>/1_assembly/probes.dmnd",
|
|
9
|
+
query="<workdir>/<inter>/{sample}.fasta",
|
|
10
|
+
output:
|
|
11
|
+
"<results>/120_matching_tables/{sample}.tsv",
|
|
12
|
+
log:
|
|
13
|
+
"<logs>/<stage>/120_matching_diamond/{sample}.log",
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
rule seeds_coverage:
|
|
17
|
+
input:
|
|
18
|
+
scfs="<workdir>/<inter>/{sample}.fasta",
|
|
19
|
+
clean1="<workdir>/0_preprocessing/02_cleaning/{sample}_R1.fastq.gz",
|
|
20
|
+
clean2="<workdir>/0_preprocessing/02_cleaning/{sample}_R2.fastq.gz",
|
|
21
|
+
output:
|
|
22
|
+
counts="<results>/121_coverage/{sample}.counts",
|
|
23
|
+
metabat="<results>/121_coverage/{sample}.metabat",
|
|
24
|
+
params:
|
|
25
|
+
extra="--proper-pairs-only --exclude-supplementary",
|
|
26
|
+
log:
|
|
27
|
+
"<logs>/<stage>/121_coverage/{sample}.log",
|
|
28
|
+
threads: 4
|
|
29
|
+
shadow:
|
|
30
|
+
"shallow"
|
|
31
|
+
conda:
|
|
32
|
+
"../../envs/preprocessing.yaml"
|
|
33
|
+
shell:
|
|
34
|
+
"""
|
|
35
|
+
minimap2 -t {threads} -ax sr {input.scfs} {input.clean1} {input.clean2} 2> {log} | \
|
|
36
|
+
samtools sort -u -@ {threads} > tmp_{wildcards.sample}.bam 2>> {log}
|
|
37
|
+
coverm contig -m count {params.extra} -b tmp_{wildcards.sample}.bam > {output.counts} 2>> {log}
|
|
38
|
+
coverm contig -m metabat {params.extra} -b tmp_{wildcards.sample}.bam > {output.metabat} 2>> {log}
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
rule seeds_filtering:
|
|
43
|
+
input:
|
|
44
|
+
scfs="<workdir>/<inter>/{sample}.fasta",
|
|
45
|
+
hits="<results>/120_matching_tables/{sample}.tsv",
|
|
46
|
+
covs="<results>/121_coverage/{sample}.metabat",
|
|
47
|
+
output:
|
|
48
|
+
metrics="<results>/122_filt_seeds/1220_summary_tables/{sample}.tsv",
|
|
49
|
+
scfs="<results>/122_filt_seeds/1221_filtered_scfs/{sample}.fasta",
|
|
50
|
+
log:
|
|
51
|
+
"<logs>/<stage>/122_filt_seeds/{sample}.log",
|
|
52
|
+
params:
|
|
53
|
+
min_cov=lookup("scf_min_cov", within=seeds_filt_params),
|
|
54
|
+
min_idt=lookup("scf_min_idt", within=seeds_filt_params),
|
|
55
|
+
max_gc=lookup("max_gc", within=seeds_filt_params),
|
|
56
|
+
min_gc=lookup("min_gc", within=seeds_filt_params),
|
|
57
|
+
cov_threshold=lookup("cov_threshold", within=seeds_filt_params),
|
|
58
|
+
cov_dynamic_filt=lookup("cov_dynamic_filt", within=seeds_filt_params),
|
|
59
|
+
separate_probes=lambda wildcards: False,
|
|
60
|
+
tag_scfs=lambda wildcards: False,
|
|
61
|
+
qpat=lambda wildcards: SEED_PAT,
|
|
62
|
+
tpat=lambda wildcards: probe_pattern,
|
|
63
|
+
script:
|
|
64
|
+
"../../../src/homolog_filtering.py"
|