-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shakefile
169 lines (150 loc) · 4.94 KB
/
Shakefile
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
#### modify config files with sample sheets ####
configfile: "config.yaml"
### import other jobs ###
import pathlib
import os, getpass, shutil, re, psutil
import pandas
#### target rules ####
rule all:
input:
expand("{sample}/yield.tab", sample=config["samples"]),
expand("{sample}/kraken2.tab", sample=config["samples"]),
expand("{sample}/shovill", sample=config["samples"]),
expand("filtered_contigs/{sample}.fna", sample=config["samples"]),
"denovo.tab",
"mlst.tab",
expand("{sample}/abricate.tab", sample=config["samples"]),
"resistome.tab",
expand("{sample}/vfdb.tab", sample=config["samples"]),
"virulome.tab",
expand("{sample}/plasmid.tab", sample=config["samples"]),
"plasmid.tab",
"seq_data.tab",
"species_identification.tab"
rule fq:
input:
r1= "input/{sample}_R1.fastq.gz",
r2= "input/{sample}_R2.fastq.gz"
output:
"{sample}/yield.tab"
shell:
"fq {input.r1} {input.r2} > {output}"
rule kraken:
input:
r1 = "input/{sample}_R1.fastq.gz",
r2 = "input/{sample}_R2.fastq.gz"
output:
"{sample}/kraken2.tab"
threads: 4
shell:
"kraken2 --threads {threads} --memory-mapping --db /scratch/kraken/k2_pluspf_20220607/ --report {output} --paired {input.r1} {input.r2}"
rule shovill:
input:
r1 = "input/{sample}_R1.fastq.gz",
r2 = "input/{sample}_R2.fastq.gz"
output:
directory("{sample}/shovill")
threads: 16
shell:
"shovill --force --cpus {threads} -R1 {input.r1} -R2 {input.r2} --outdir {output}"
rule remove_small:
input:
"{sample}/shovill"
output:
"filtered_contigs/{sample}.fna"
shell:
"seqtk seq -L 1000 {input}/contigs.fa > {output}"
rule assembly_quality:
input:
expand("filtered_contigs/{sample}.fna", sample=config["samples"])
output:
"denovo.tab"
shell:
"fa -e -t {input} > {output}"
rule mlst:
input:
expand("filtered_contigs/{sample}.fna", sample=config["samples"])
output:
"mlst.tab"
shell:
"mlst {input} > {output}"
rule abricate:
input:
"filtered_contigs/{sample}.fna"
output:
"{sample}/abricate.tab"
shell:
"abricate --db ncbi {input} > {output}"
rule abricate_sum:
input:
expand("{sample}/abricate.tab", sample=config["samples"])
output:
"resistome.tab"
shell:
"abricate --summary {input} > {output}"
rule vfdb:
input:
"filtered_contigs/{sample}.fna"
output:
"{sample}/vfdb.tab"
shell:
"abricate --db vfdb {input} > {output}"
rule vfdb_sum:
input:
expand("{sample}/vfdb.tab", sample=config["samples"])
output:
"virulome.tab"
shell:
"abricate --summary {input} > {output}"
rule plasmid:
input:
"filtered_contigs/{sample}.fna"
output:
"{sample}/plasmid.tab"
shell:
"abricate --db plasmidfinder {input} > {output}"
rule plasmid_sum:
input:
expand("{sample}/plasmid.tab", sample=config["samples"])
output:
"plasmid.tab"
shell:
"abricate --summary {input} > {output}"
rule seq_data:
input:
expand("{sample}/yield.tab", sample=config["samples"])
output:
"seq_data.tab"
shell:
"compile_seq_data.sh {input} > {output}"
rule combine_kraken:
input:
expand("{sample}/kraken2.tab", sample = config["samples"])
output:
"species_identification.tab"
run:
import pandas, pathlib, subprocess
kfiles = f"{input}".split()
id_table = pandas.DataFrame()
for k in kfiles:
kraken = pathlib.Path(k)
df = pandas.read_csv(kraken, sep = "\t", header =None, names = ['percentage', 'frag1', 'frag2','code','taxon','name'])
df['percentage'] = df['percentage'].apply(lambda x:float(x.strip('%')) if isinstance(x, str) == True else float(x))
df = df.sort_values(by = ['percentage'], ascending = False)
df = df[df['code'].isin(['U','S'])]
df = df.reset_index(drop = True)
tempdf = pandas.DataFrame()
d = {'Isolate': f"{kraken.parts[0]}",
'#1 Match': df.loc[0,'name'].strip(), '%1': df.loc[0,'percentage'],
'#2 Match': df.loc[1,'name'].strip(), '%2': df.loc[1,'percentage'],
'#3 Match': df.loc[2,'name'].strip(), '%3': df.loc[2,'percentage']
}
tempdf = pandas.DataFrame(data = d, index= [0])
if id_table.empty:
id_table = tempdf
else:
id_table = id_table.append(tempdf, sort = True)
cols_list = ['Isolate', '#1 Match', '%1', '#2 Match', '%2', '#3 Match', '%3']
id_table = id_table.reindex(cols_list, axis = 'columns')
id_table.to_csv(f"{output}", sep = "\t", index = False)
subprocess.run("sed -i 's/%[0-9]/%/g' {output}", shell=True)