-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuk.Snakefile
221 lines (183 loc) · 6.5 KB
/
euk.Snakefile
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
#-*- coding: utf-8
# This workflow details our scripts for skin MAG generation
# This workflow is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# MAG Snakemake workflow is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with MAG Snakemake workflow. If not, see <https://www.gnu.org/licenses/>.
#'''
# This is a basic framework for recovery and basic quality control of MAGs
# To visualize the pipeline: snakemake --dag | dot -Tpng > dag.png
#'''
__maintainer__ = "Sara Kashaf"
__email__ = "[email protected]"
import os
from os.path import join
import sys
import glob
import pandas as pd
import csv
outfiles_all=[]
#collect all the eukaryotic bins using glob
EUKS,=glob_wildcards("eukcc_output/eukcc_{euks}/eukcc.tsv")
EUKS_filt,=glob_wildcards("QC_fasta/fasta_comp_cont/{euks}")
#QC euks
outfiles_all.append(expand(join("QC_fasta/all_tsv_parsed/{bins}_eukcc_parsed.csv"),bins=EUKS))
#dereplicate
outfiles_all.append("dRep/data_tables/Sdb.csv")
#dnadiff
outfiles_all.append("MAG_genbank/dnadiff_summary.tsv")
rule all:
input: outfiles_all
rule parse_tsv:
input:
tsv="eukcc_output/eukcc_{euks}/eukcc.tsv",
output:
tsv="QC_fasta/all_tsv/{euks}_eukcc.tsv",
priority: 1000
shell:"""
scp {input.tsv} {output.tsv}
"""
rule make_csv:
input: "QC_fasta/all_tsv/{euks}_eukcc.tsv"
output: "QC_fasta/all_tsv_parsed/{euks}_eukcc_parsed.csv"
params:
run="{euks}",
qc="QC_fasta/eukcc_summ.tsv",
f="fastas/{euks}",
odir="QC_fasta/fasta_comp_cont"
run:
os.system("mkdir -p " + str(params.odir))
infile=str(input)
outfile=str(output)
qc=str(params.qc)
with open(infile) as inp:
LoL=[x.strip().split('\t') for x in inp]
row1=LoL[1]
completeness=LoL[1][0]
contamination=LoL[1][1]
run=str(params.run)
g = open(outfile, "w")
if float(completeness)>50 and float(contamination)<5:
g.write(run+','+completeness+','+contamination+'\n')
g.close()
h = open(params.qc, "a")
h.write(run+','+completeness+','+contamination+'\n')
h.close()
f = str(params.f)
odir=str(params.odir)
os.system("scp "+f+ " "+str(odir))
else:
g.write("")
g.close()
rule make_genome_info:
input: "QC_fasta/eukcc_summ.tsv"
output: "QC_fasta/eukcc_metrics.csv"
shell: "echo -e 'genome,completeness,contamination' | cat - {input} > {output}"
rule dereplicate:
input:
parsed=expand("QC_fasta/all_tsv_parsed/{euks}_eukcc_parsed.csv",euks=EUKS),
genome_info="QC_fasta/eukcc_metrics.csv"
output: "dRep/data_tables/Sdb.csv"
params:
infolder="QC_fasta/fasta_comp_cont",
outfolder="dRep"
threads: 20
shell:"""
dRep dereplicate -p {threads} \
{params.outfolder} -g {params.infolder}/*.fa \
-pa 0.9 -sa 0.95 -nc 0.3 \
-cm larger \
--genomeInfo {input.genome_info} \
-comp 50 -con 5
"""
rule mash_dist:
input:
bins="QC_fasta/fasta_comp_cont/{euks}",
db="/hps/research1/finn/saary/projects/2020_eukmash_db/output/latest/mash/genbank-fungi.msh"
output:
join("MAG_genbank/mashdist/{euks}.tab"),
threads: 1
shell:
"mash dist -p {threads} {input.db} {input.bins} > {output}"
rule best_mash:
input:
mashdist=join(
"MAG_genbank/mashdist/{euks}.tab"
),
output: "MAG_genbank/best_mash/{euks}.tab"
threads: 1
shell:
"""
sort -gk3 {input.mashdist}|sed -n 1p >{output}
"""
rule dnadiff:
input:
bestmash="MAG_genbank/best_mash/{euks}.tab"
output: "MAG_genbank/dnadiff/{euks}.report"
params:
outdir=join("MAG_genbank/dnadiff"),
bins="{euks}",
genomesdir="MAG_genbank/dnadiff/genomes"
shell:
"""
mkdir -p {params.genomesdir}
while read col1 col2 rem
do
f="$(basename -- ${{col1}})"
echo {params.genomesdir}/${{f%%.gz}}
if [ ! -f {params.genomesdir}/${{f%%.gz}} ]; then
echo "copying file over"
scp ${{col1}} {params.genomesdir}
gunzip {params.genomesdir}/${{f}}
fi
echo 'dnadiff ${{col1}} ${{col2}} -p ${{col1%%.fasta}}_${{col2%%.fa}}_'
dnadiff {params.genomesdir}/${{f%%.gz}} ${{col2}} -p {params.outdir}/{params.bins}
done < {input.bestmash}
"""
rule parse_dnadiff:
input:
dnadiff=join("MAG_genbank/dnadiff/{euks}.report")
output:
join(
"MAG_genbank/dnadiff_parsed/{euks}.tsv"
),
run:
outfile = str(output)
f = open(input.dnadiff)
data = f.read()
first_line = data.split("\n", 1)[0]
a = first_line.split(" ")
ref = a[0]
quer = a[1]
with open(outfile, "w") as outf:
path_dna = input.dnadiff
base = os.path.basename(path_dna)
base = base.split(".report")[0]
with open(path_dna) as f:
for line in f:
if "TotalBases" in line:
cols = line.split()
lenref = int(cols[1])
lenquer = int(cols[2])
if "AlignedBases" in line:
cols = line.split()
aliref = cols[1].split("(")[-1].split("%")[0]
alique = cols[2].split("(")[-1].split("%")[0]
if "AvgIdentity" in line:
cols = line.split()
ident = float(cols[1])
line = "%s\t%s\t%i\t%.2f\t%i\t%.2f\t%.2f" % (ref, quer, lenref, float(aliref), lenquer, float(alique), float(ident))
outf.writelines(line + "\n")
rule aggregate_dnadiff:
input: expand("MAG_genbank/dnadiff_parsed/{euks}.tsv",euks=EUKS_filt)
output: "MAG_genbank/dnadiff_summary.tsv"
shell:
"""
cat {input}>{output}
"""