Virulence 12.1 KB
Newer Older
Laura Denies's avatar
Laura Denies committed
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
#Virulence

import glob
import os

configfile: "config.yaml"
PROJECT=config["project"]
INPUT=config["input_file"]

#HMM scan
rule run_HMM_vir:
    input:
        hmm=config["vir_hmm_file"],
        renamed="{OUTDIR}/{project}/splitted/{input_file}/{file_i}.faa"
    output: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}/{file_i}.hmmscan"
    message: "Run HMM scan on {input[1]} to generate {output}"
    params: outdir="{OUTDIR}"
    threads: 12
    shell: """
        {config[hmmscan_tool]} --cpu {threads} --noali --notextw --tblout {output} {input[0]} {input[1]}
        """

# Adjust HMM results to correct format

rule HMM_correct_format_vir:
    input: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}/{file_i}.hmmscan"
    output: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}/{file_i}.hmm.csv"
    message: "Adjust {input} to correct format: {output}"
    params: outdir="{OUTDIR}"
    shell: """
        sed '/^#/ d' {input} | sed 's/ \+/\t/g' > {output}
        """

def aggregate_hmm(wildcards):
        checkpoint_output = checkpoints.splitting.get(**wildcards).output.splits
        return expand("{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}/{file_i}.hmm.csv",
                OUTDIR=wildcards.OUTDIR,
                project=wildcards.project,
                input_file=wildcards.input_file,
                file_i=glob_wildcards(os.path.join(checkpoint_output, "{i}.faa")).i)

rule HMM_correct_format_2_vir:
    input: aggregate_hmm
    output: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}.Input_HMM_R.csv"
    params: outdir="{OUTDIR}"
    shell: """
        cut -f 1,3,5,6 {input} |uniq > {output}
        """

rule HMM_R_VIR:
        input:
                hmmresults="{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}.Input_HMM_R.csv",
                positive="databases/models_and_domains/positive_domains.tsv",
                negative="databases/models_and_domains/negative_domains.tsv",
                shared="databases/models_and_domains/shared_domains.tsv",
                ID="{OUTDIR}/{project}/renamed/{input_file}_translation.tsv"
        output: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}.hmm_results.csv"
58
        conda: "../../envs/R.yaml"
59
        script: "../../scripts/hmm.R"
Laura Denies's avatar
Laura Denies committed
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


## Give pathogenicity prediction
rule HMM_VIR_nonpath:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}.hmm_results.csv"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_non_path.txt")
        shell: """
                awk '$2 == "True" && $3 == "False"  && $4 == "False"' {input} | awk '$5 = "Non-Pathogenic"' | sed 's/ /\t/g' > {output}
                """

rule HMM_VIR_pathogenic:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}.hmm_results.csv"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_pathogenic.txt")
        shell: """
                awk '$3 == "True"' {input} | awk '$5 = "Pathogenic"' | sed 's/ /\t/g' > {output}
                """

rule HMM_VIR_unclassified:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}.hmm_results.csv"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_unclassified.txt")
        shell: """
                awk '$3 == "False"  && $4 == "True"' {input} | awk '$5 = "Unclassified"' | sed 's/ /\t/g' > {output}
                """
rule HMM_VIR_report:
        input:
                non_pathogenic= "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_non_path.txt",
                pathogenic= "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_pathogenic.txt",
                unclassified= "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_unclassified.txt"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_prediction_intermediate.tsv")
        shell: """
                cat {input[0]} {input[1]} {input[2]} | sort > {output}
                """

rule HMM_VIR_finalformat:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_prediction_intermediate.tsv"
        output: "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_prediction.tsv"
        shell:
                "cut -f 1,5 {input}  > {output}"


##########################
#  Virulence classifier  #
##########################
rule AAC:
	input: "{OUTDIR}/{project}/splitted/{input_file}/{file_i}.faa"
	output: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_AAC.txt"
106
	conda: "../../envs/Biopython.yaml"
Laura Denies's avatar
Laura Denies committed
107
108
109
110
111
	shell: "python scripts/AAC.py --file {input} --out {output}"

rule DPC:
        input: "{OUTDIR}/{project}/splitted/{input_file}/{file_i}.faa"
        output: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_DPC.txt"
112
        conda: "../../envs/Biopython.yaml"
Laura Denies's avatar
Laura Denies committed
113
114
115
116
117
        shell: "python scripts/DPC.py --file {input} --out {output}"

rule CTDC:
        input: "{OUTDIR}/{project}/splitted/{input_file}/{file_i}.faa"
        output: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_CTDC.txt"
118
        conda: "../../envs/Biopython.yaml"
Laura Denies's avatar
Laura Denies committed
119
120
121
122
123
        shell: "python scripts/CTDC.py --file {input} --out {output}"

rule CTDT:
        input: "{OUTDIR}/{project}/splitted/{input_file}/{file_i}.faa"
        output: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_CTDT.txt"
124
        conda: "../../envs/Biopython.yaml"
Laura Denies's avatar
Laura Denies committed
125
126
127
128
129
        shell: "python scripts/CTDT.py --file {input} --out {output}"

rule CTDD:
        input: "{OUTDIR}/{project}/splitted/{input_file}/{file_i}.faa"
        output: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_CTDD.txt"
130
        conda: "../../envs/Biopython.yaml"
Laura Denies's avatar
Laura Denies committed
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
        shell: "python scripts/CTDD.py --file {input} --out {output}"

rule join_matrix:
	input:
		AAC="{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_AAC.txt",
		DPC="{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_DPC.txt",
		CTDC="{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_CTDC.txt",
		CTDT="{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_CTDT.txt",
		CTDD="{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_CTDD.txt"
	output: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_matrix.tsv"
	shell: """
		xjoin() {{
			local f
    			local srt="sort -k 1b,1"

    			if [ "$#" -lt 2 ]; then
            			echo "xjoin: need at least 2 files" >&2
            			return 1
    			elif [ "$#" -lt 3 ]; then
            			join -t $'\t' <($srt "$1") <($srt "$2")
    			else
            			f=$1
            			shift
            			join -t $'\t' <($srt "$f") <(xjoin "$@")
    			fi
		}}
			
		xjoin {input[0]} {input[1]} {input[2]} {input[3]} {input[4]} | sort -k 1n,1 > {output}
		"""

rule classifier:
	input: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_matrix.tsv"
	output: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_classifier_prediction.tsv"
164
	conda: "../../envs/Biopython.yaml"
Laura Denies's avatar
Laura Denies committed
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
	shell: "python scripts/virulence_prediction.py {input} {output}"

def aggregate_classifier(wildcards):
        checkpoint_output = checkpoints.splitting.get(**wildcards).output.splits
        return expand("{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}/{file_i}_classifier_prediction.tsv",
                OUTDIR=wildcards.OUTDIR,
                project=wildcards.project,
                input_file=wildcards.input_file,
                file_i=glob_wildcards(os.path.join(checkpoint_output, "{i}.faa")).i)

rule format_classifier:
        input: aggregate_classifier 
        output: temp("{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}_classifier_results_format.tsv")
        shell: """
                sed 's/"//g' {input} | cut -f2,3 >{output}
                """

rule format_classifier_2:
	input: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}_classifier_results_format.tsv"
	output: "{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}_classifier_results_formatted.tsv"
	shell: """
		awk '{{$1=sprintf("%010d", $1)}}1' {input} | sed 's/ /\t/g' > {output}
		"""

##############################
# Merge Virulence Prediction #
##############################

rule merge_virulence:
        input:
                hmm= "{OUTDIR}/{project}/VIRULENCE/HMM_virulence/{input_file}_hmm_prediction.tsv",
                classifier="{OUTDIR}/{project}/VIRULENCE/classifier_virulence/{input_file}_classifier_results_formatted.tsv"
	output: "{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_Virulence_prediction.tsv"
        shell:
                "join -t $'\t' <(sort {input[0]}) <(sort {input[1]}) >{output};"
                "sed -i $'1 i\\\ ID\tHMM_prediction\tmodel_prediction' {output}"

rule virulence_nonpath:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_Virulence_prediction.tsv"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_non_path_1.tsv")
        shell: """
                awk '$2 == "Non-Pathogenic" && $3 == "negative"' {input} | awk '$4 = "Non-Pathogenic"' | sed 's/ /\t/g' > {output}
                """

rule virulence_nonpath2:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_Virulence_prediction.tsv"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_non_path_2.tsv")
        shell: """
                awk '$2 == "Unclassified" && $3 == "negative"' {input} | awk '$4 = "Non-Pathogenic"' | sed 's/ /\t/g' > {output}
                """

rule virulence_path:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_Virulence_prediction.tsv"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_pathogenic_1.tsv")
        shell: """
                awk '$2 == "Pathogenic" && $3 == "pathogenic"' {input} | awk '$4 = "Pathogenic"' | sed 's/ /\t/g' > {output}
                """

rule virulence_path2:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_Virulence_prediction.tsv"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_pathogenic_2.tsv")
        shell: """
                awk '$2 == "Unclassified" && $3 == "pathogenic"' {input} | awk '$4 = "Pathogenic"' | sed 's/ /\t/g' > {output}
                """

rule virulence_unclassified1:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_Virulence_prediction.tsv"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_unclassified_1.tsv")
        shell: """
                awk '$2 == "Pathogenic" && $3 == "negative"' {input} | awk '$4 = "-"' | sed 's/ /\t/g' > {output}
                """

rule virulence_unclassified2:
        input: "{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_Virulence_prediction.tsv"
        output: temp("{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_unclassified_2.tsv")
        shell: """
                awk '$2 == "Non-Pathogenic" && $3 == "pathogenic"' {input} | awk '$4 = "-"' | sed 's/ /\t/g' > {output}
                """

rule merge_combined:
        input:
                Nonpath_1="{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_non_path_1.tsv",
                Nonpath_2="{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_non_path_2.tsv",
                Pathogenic_1="{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_pathogenic_1.tsv",
                Pathogenic_2="{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_pathogenic_2.tsv",
                Unclassified_1="{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_unclassified_1.tsv",
                Unclassified_2="{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_prediction_unclassified_2.tsv"
        output: "{OUTDIR}/{project}/VIRULENCE/HMM_classifier_virulence/{input_file}_virulence_final_prediction.tsv"
        shell: "cat {input[0]} {input[1]} {input[2]} {input[3]} {input[4]} {input[5]} > {output}"