-
Notifications
You must be signed in to change notification settings - Fork 3
/
eval_assembly.smk
150 lines (127 loc) · 5.79 KB
/
eval_assembly.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
# Load parameters from config file
include: "rules/load_config.smk"
assembly_dir = "/".join([project_dir, "results/assembly"])
metaquast_dir = "/".join([project_dir, "results/metaquast"])
assemblers = ["spades", "metaspades", "tadpole", "abyss",
"megahit", "ray", "idba", "vicuna", "iva", "savage", "virgena"] # "haploflow", "pehaplo", "quasirecomb",
metaquast_criteria = ["num_contigs", "Largest_contig", "Genome_fraction",
"Duplication_ratio", "Largest_alignment", "LGA50",
"NGA50", "num_mismatches_per_100_kbp"]
# Get current working directory
# cwd = os.getcwd()
# Samples to corresponding TM or TM mixture folders
# ruleorder: metaspades > megahit > tadpole > abyss > spades > ray > idba > savage_full_ref
def make_mix():
return ["{}/{}".format(sample.split("-")[0], sample) for sample in sample_list]
def get_assembly_ref(wc):
if wc.sample.startswith('TA'):
if wc.sample.endswith('-1-0'):
ref_list = [tb_ref]
elif wc.sample.endswith('-0-1'):
ref_list = [ad_ref]
else:
ref_list = [tb_ref, ad_ref]
else:
if wc.sample.endswith('-1-0'):
ref_list = [tb_ref]
elif wc.sample.endswith('-0-1'):
ref_list = [merlin_ref]
else:
ref_list = [tb_ref, merlin_ref]
return ','.join(ref_list)
onsuccess:
print("The assembly evaluation is done!")
# shell("mail -s 'The assembly evaluation is done' youremail@provider.com")
rule all:
input:
metaquast_report = expand(metaquast_dir + "/{strain_sample}/report.html",
strain_sample=make_mix()),
all_sample_metaquast_table = results_dir + "/final_tables/all_sample_metaquast.tsv",
figure = results_dir + "/final_figures/assembly_metaquast_evaluation.pdf"
# expand("{assemblyDir}/{assembler}/{sample}/contig.done",
# assemblyDir=assembly_dir, sample=sample_list, assembler=assemblers),
# expand(metaquast_dir + "/summary_for_figure/{mix}.{criteria}.merged.tsv",
# mix=["TM", "TA"], criteria=metaquast_criteria),
# results_dir + "/final_figures/assembly_metaquast_evaluation.pdf",
# results_dir + "/final_tables/assembly_metaquast_ranking.tsv",
# results_dir + "/final_tables/assembly_metaquast_score.tsv",
# results_dir + "/final_tables/assembly_metaquast_scaled.tsv"
# Build index for reference
include: "rules/index.smk"
# If not run on reads, copy the resulting scaffolds provided within the software for benchmarking
if not run_on_reads:
# Extract assembly
if not os.path.exists(cd + "/data/assembly"):
shell("tar -xzvf {} -C {}".format(cd + "/data/assembly.tar.gz",
cd + "/data/"))
rule cp_assembly:
input: cd + \
"/data/assembly/{assembler}/{sample}.{assembler}.scaffolds.fa"
output: assembly_dir + "/{assembler}/{sample}.{assembler}.scaffolds.fa"
shell:
"""
cp {input} {output}
"""
else:
# Remove remaining Phix reads
include: "rules/decontamination.smk"
# Run all consensus assembly tools
include: "rules/assembly.smk"
# Evaluate assemblies using metaquast
rule metaquast:
input:
scaffolds = lambda wc: expand(assembly_dir + "/{assembler}/{{sample}}.{assembler}.scaffolds.fa",
assembler=assemblers),
ref_fai = lambda wc: [tb_ref + ".fai", ad_ref + ".fai"] if \
wc.mix == "TA" else [tb_ref + ".fai", merlin_ref + ".fai"]
output:
reports = metaquast_dir + "/{mix}/{sample, [A-Z]+-[0-9\-]+}/report.html",
tsv_report = metaquast_dir + "/{mix}/{sample, [A-Z]+-[0-9\-]+}/combined_reference/report.tsv"
conda:
"config/conda_env.yaml"
threads: threads
params:
metaquast_outdir = metaquast_dir + "/{mix}/{sample}",
ref = get_assembly_ref
# ref = lambda wc: ",".join(
# [tb_ref, ad_ref]) if wc.mix == "TA" else ",".join([tb_ref, merlin_ref])
shell:
"""
metaquast.py --unique-mapping -o {params.metaquast_outdir} -R {params.ref} {input.scaffolds} -t {threads}
"""
# Summarize all evaluations
rule summarize:
input:
expand(metaquast_dir + "/{strain_sample}/report.html",
strain_sample=make_mix())
output:
metaquast_dir + "/summary_for_figure/{mix}.{criteria}.merged.tsv"
# conda:
# "config/conda_env.yaml"
params:
input_files = metaquast_dir + "/{mix}/*/summary/TSV/{criteria}.tsv",
joiner = cd + '/program/join_tsv.py'
shell:
"""
python {params.joiner} {params.input_files}|sed '1s/\.scaffolds//g' |csvtk transpose -Tt -|\
awk 'NR==1{{print}}$1!="Assemblies"{{print}}'|sed '1s/\.GFP\|\.BAC//g' > {output}
"""
# Visualize the evaluation
rule visualize:
input:
individual_ref_reports = expand(metaquast_dir + "/summary_for_figure/{mix}.{criteria}.merged.tsv",
mix=["TA", "TM"], criteria=metaquast_criteria),
combined_ref_reports = expand("{metaquastDir}/{strain_sample}/combined_reference/report.tsv", metaquastDir=metaquast_dir,
assembler=assemblers, strain_sample=make_mix())
output:
figure = results_dir + "/final_figures/assembly_metaquast_evaluation.pdf",
all_sample_table = results_dir + "/final_tables/all_sample_metaquast.tsv",
table = results_dir + "/final_tables/assembly_metaquast_ranking.tsv",
radarplot_table = results_dir + "/final_tables/assembly_metaquast_scaled.tsv",
score = results_dir + "/final_tables/assembly_metaquast_score.tsv"
conda:
"config/conda_env.yaml"
params:
input_dir = metaquast_dir + "/summary_for_figure"
script:
"scripts/metaquast_visualize.R"