-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntroot_run_pipeline.smk
176 lines (149 loc) · 7.17 KB
/
ntroot_run_pipeline.smk
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
#!/usr/bin/env snakemake -s
# Snakefile for ntRoot pipeline
import os
import shutil
onsuccess:
shutil.rmtree(".snakemake", ignore_errors=True)
# Read parameters from config or set default values
reference=config["reference"]
draft_base = os.path.basename(os.path.realpath(reference))
reads_prefix_full=config["reads"] if "reads" in config else ""
reads_prefix=os.path.basename(reads_prefix_full)
k=config["kmer"] if "kmer" in config else None
genomes = config["genomes"] if "genomes" in config else ""
genome_prefix = ".".join([os.path.basename(os.path.realpath(genome)).removesuffix(".fa").removesuffix(".fasta").removesuffix(".fna") for genome in genomes])
# Common parameters
t = config["threads"] if "threads" in config else 4
b = config["b_prefix"] + "_" if "b_prefix" in config and config["b_prefix"] != "" else ""
# ntEdit parameters
z = config["z_param"] if "z_param" in config else 100
v = config["verbose"] if "verbose" in config else 0
j = config["j_param"] if "j_param" in config else 3
Y = config["Y_param"] if "Y_param" in config else 0.55
l = config["l_vcf"] if "l_vcf" in config else ""
# Ancestry inference parameters
tile_size = config["tile_size"] if "tile_size" in config else 5000000
# Third-party VCF parameters
input_vcf = config["input_vcf"] if "input_vcf" in config else None
input_vcf_basename = os.path.basename(os.path.realpath(input_vcf)) if input_vcf else "None"
strip_info = config["strip_info"] if "strip_info" in config else None
# time command
mac_time_command = "command time -l -o"
linux_time_command = "command time -v -o"
time_command = mac_time_command if os.uname().sysname == "Darwin" else linux_time_command
rule all:
input: f"{reads_prefix}_ntedit_k{k}_variants.vcf_ancestry-predictions_tile{tile_size}.tsv"
rule ntroot_genome:
input: f"{genome_prefix}_ntedit_k{k}_variants.vcf_ancestry-predictions_tile{tile_size}.tsv"
rule ntroot_reads:
input: f"{reads_prefix}_ntedit_k{k}_variants.vcf_ancestry-predictions_tile{tile_size}.tsv"
rule ntroot_genome_lai:
input: f"{genome_prefix}_ntedit_k{k}_variants.vcf_ancestry-predictions-tile-resolution_tile{tile_size}.tsv"
rule ntroot_reads_lai:
input: f"{reads_prefix}_ntedit_k{k}_variants.vcf_ancestry-predictions-tile-resolution_tile{tile_size}.tsv"
rule ntroot_input_vcf:
input: f"{input_vcf_basename}.cross-ref.vcf_ancestry-predictions_tile{tile_size}.tsv"
rule ntroot_input_vcf_lai:
input: f"{input_vcf_basename}.cross-ref.vcf_ancestry-predictions-tile-resolution_tile{tile_size}.tsv"
rule ntedit_reads:
input:
reference = reference
output:
out_vcf = f"{reads_prefix}_ntedit_k{k}_variants.vcf",
out_fa = temp(f"{reads_prefix}_ntedit_k{k}_edited.fa"),
out_changes = temp(f"{reads_prefix}_ntedit_k{k}_changes.tsv"),
out_bf = temp(f"{reads_prefix}_k{k}.bf")
params:
benchmark = f"{time_command} ntedit_snv_k{k}.time",
params = f"-k {k} -t {t} -z {z} -j {j} -Y {Y} --solid ",
vcf_input = f"-l {l}" if l else ""
shell:
"{params.benchmark} run-ntedit snv --reference {reference} --reads {reads_prefix_full} {params.params} "
"{params.vcf_input}"
rule ntedit_genome:
input:
reference = reference,
genomes = genomes
output:
out_vcf = f"{genome_prefix}_ntedit_k{k}_variants.vcf",
out_fa = temp(f"{genome_prefix}_ntedit_k{k}_edited.fa"),
out_changes = temp(f"{genome_prefix}_ntedit_k{k}_changes.tsv"),
out_bf = temp(f"{genome_prefix}_k{k}.bf")
params:
benchmark = f"{time_command} ntedit_snv_k{k}.time",
params = f"-k {k} -t {t} -z {z} -j {j} -Y {Y}",
vcf_input = f"-l {l}" if l else ""
shell:
"{params.benchmark} run-ntedit snv --reference {reference} --genome {input.genomes} {params.params} "
" {params.vcf_input}"
rule samtools_faidx:
input: reference = reference
output: out_fai = f"{draft_base}.fai"
params:
benchmark = f"{time_command} samtools_faidx_{draft_base}.time"
run:
if input.reference.endswith(".gz"):
shell("{params.benchmark} gunzip -c {input.reference} |samtools faidx -o {output.out_fai} -")
else:
shell("{params.benchmark} samtools faidx -o {output.out_fai} {input.reference}")
rule ancestry_prediction:
input:
vcf = "{vcf}"
output:
predictions = "{vcf}_ancestry-predictions_tile{tile_size}.tsv"
params:
benchmark = f"{time_command} ancestry_prediction_tile{tile_size}.time" if input_vcf_basename else f"{time_command} ancestry_prediction_k{k}_tile{tile_size}.time",
tile_size = tile_size,
verbosity = v
shell:
"{params.benchmark} ntRootAncestryPredictor.pl -f {input.vcf} -t {params.tile_size} -v {params.verbosity}"
rule ancestry_prediction_lai:
input:
vcf = "{vcf}",
ref_fai = f"{draft_base}.fai"
output:
lai_output = "{vcf}_ancestry-predictions-tile-resolution_tile{tile_size}.tsv"
params:
benchmark = f"{time_command} ancestry_prediction_tile{tile_size}.time" if input_vcf_basename else f"{time_command} ancestry_prediction_k{k}_tile{tile_size}.time",
tile_size = tile_size,
verbosity = v
shell:
"{params.benchmark} ntRootAncestryPredictor.pl -f {input.vcf} -t {params.tile_size} -v {params.verbosity} -r 1 -i {input.ref_fai}"
rule sort_vcf_input:
input: vcf = f"{input_vcf}"
output: vcf_sorted = temp(f"{input_vcf_basename}_sorted.vcf")
params:
benchmark = f"{time_command} sort_vcf_{input_vcf_basename}.time",
cat_cmd = "gunzip -c" if f"{input_vcf}".endswith(".gz") else "cat"
shell:
"""{params.benchmark} sh -c '(echo "##fileformat=VCFv4.2" ; {params.cat_cmd} {input.vcf} |grep -v "^#" |sort -k1,1 -k2,2n) > {output.vcf_sorted}'"""
rule sort_vcf_l:
input: vcf = l
output: temp(f"{os.path.basename(os.path.realpath(l))}_sorted.tmp.vcf")
params:
benchmark = f"{time_command} sort_vcf_l.time",
cat_cmd = "gunzip -c" if f"{l}".endswith(".gz") else "cat"
shell:
"""{params.benchmark} sh -c "(echo '##fileformat=VCFv4.2' ; {params.cat_cmd} {input.vcf} | awk '\$5 !~ /^</' | grep -v '^#' |sort -k1,1 -k2,2n) > {output}" """
rule bedtools_intersect:
input:
sorted_vcf = f"{input_vcf_basename}_sorted.vcf",
sorted_ref_vars = f"{os.path.basename(os.path.realpath(l))}_sorted.tmp.vcf"
output:
bedtools = temp(f"{input_vcf_basename}.bedtools-intersect.bed")
params:
benchmark = f"{time_command} bedtools_intersect_{input_vcf_basename}.time"
shell:
"{params.benchmark} bedtools intersect -loj -sorted -a {input.sorted_vcf} -b {input.sorted_ref_vars} > {output.bedtools}"
rule cross_reference_vcf:
input:
vcf = f"{input_vcf}",
ref_vars = l,
bedtools = f"{input_vcf_basename}.bedtools-intersect.bed"
output: f"{input_vcf_basename}.cross-ref.vcf"
params:
benchmark = f"{time_command} cross_reference_vcf_{input_vcf_basename}.time",
prefix=f"{input_vcf_basename}.cross-ref",
strip = "--strip" if strip_info else ""
shell:
"{params.benchmark} ntroot_cross_reference_vcf.py -b {input.bedtools} --vcf {input.vcf} --vcf_l {input.ref_vars} -p {params.prefix} {params.strip}"