forked from EBISPOT/gwas-sumstats-harmoniser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
231 lines (208 loc) · 10.6 KB
/
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
222
223
224
225
226
227
228
229
230
231
# Configure --------------------------------------------------------------------
#configfile: "config.yaml"
#singularity: "docker://ebispot/gwas-sumstats-harmoniser:latest"
rule get_vcf_files:
output:
expand("{local}homo_sapiens-chr{{chromosome}}.vcf.gz", local=config["local_resources"])
params:
remote_location=config["remote_vcf_location"],
local_resources=config["local_resources"]
shell:
"mkdir -p {params.local_resources}; "
"wget -P {params.local_resources} {params.remote_location}/homo_sapiens-chr{wildcards.chromosome}.vcf.gz"
rule get_tbi_files:
output:
expand("{local}homo_sapiens-chr{{chromosome}}.vcf.gz.tbi", local=config["local_resources"])
params:
local_resources=config["local_resources"]
shell:
"tabix -p vcf {params.local_resources}/homo_sapiens-chr{wildcards.chromosome}.vcf.gz"
rule make_parquet_refs:
input:
expand("{local}homo_sapiens-chr{{chromosome}}.vcf.gz", local=config["local_resources"])
output:
expand("{local}homo_sapiens-chr{{chromosome}}.parquet", local=config["local_resources"])
params:
local_resources=config["local_resources"],
repo_path=config["repo_path"]
shell:
"python {params.repo_path}/harmoniser/vcf2parquet.py -f {params.local_resources}homo_sapiens-chr{wildcards.chromosome}.vcf.gz"
rule map_to_build:
input:
expand("{local}homo_sapiens-chr{chromosome}.parquet", chromosome=config["chromosomes"], local=config["local_resources"]),
in_ss="{ss_file}.tsv"
output:
expand("{{ss_file}}/{chromosome}.merged", chromosome=config["chromosomes"])
params:
local_resources=config["local_resources"],
to_build=config["desired_build"],
repo_path=config["repo_path"]
resources:
mem_mb = lambda wildcards, attempt: attempt * 28000
shell:
"filename={wildcards.ss_file}; "
"from_build=$(echo -n $filename | tail -c 2); "
"mkdir -p {wildcards.ss_file}; "
"python {params.repo_path}/harmoniser/map_to_build.py -f {input.in_ss} "
"-from_build $from_build "
"-to_build {params.to_build} "
"-vcf '{params.local_resources}/homo_sapiens-chr*.parquet'"
rule ten_percent_generate_strand_counts:
input:
expand("{local}homo_sapiens-chr{{chromosome}}.vcf.gz", local=config["local_resources"]),
expand("{local}homo_sapiens-chr{{chromosome}}.vcf.gz.tbi", local=config["local_resources"]),
in_ss="{ss_file}/{chromosome}.merged"
output:
"{ss_file}/10percent.{chromosome}.merged.sc"
params:
local_resources=config["local_resources"],
repo_path=config["repo_path"]
shell:
"select=$[$(wc -l < {input.in_ss})]; "
"if [ $[$select/10] -gt 100 ]; then n=$[$select/10]; else n=$select; fi; "
"(head -n 1 {input.in_ss}; sed '1d' {input.in_ss} | shuf -n $n)>{wildcards.ss_file}/10percent.{wildcards.chromosome}.merged;"
"python {params.repo_path}/harmoniser/genetics-sumstat-harmoniser/sumstat_harmoniser/main.py --sumstats {wildcards.ss_file}/10percent.{wildcards.chromosome}.merged "
"--vcf {params.local_resources}homo_sapiens-chr{wildcards.chromosome}.vcf.gz "
"--chrom_col chromosome "
"--pos_col base_pair_location "
"--effAl_col effect_allele "
"--otherAl_col other_allele "
"--rsid_col variant_id "
"--strand_counts {wildcards.ss_file}/10percent.{wildcards.chromosome}.merged.sc"
rule ten_percent_summarise_strand_counts:
input:
expand("{{ss_file}}/10percent.{chromosome}.merged.sc", chromosome=config["chromosomes"])
output:
"{ss_file}/10_percent_total_strand_count.tsv"
params:
repo_path=config["repo_path"]
shell:
"python {params.repo_path}/harmoniser/sum_strand_counts_10percent.py -i {wildcards.ss_file} -o {wildcards.ss_file} -config {params.repo_path}/config.yaml"
rule generate_strand_counts:
input:
expand("{local}homo_sapiens-chr{{chromosome}}.vcf.gz", local=config["local_resources"]),
expand("{local}homo_sapiens-chr{{chromosome}}.vcf.gz.tbi", local=config["local_resources"]),
in_ss="{ss_file}/{chromosome}.merged",
sc_sum="{ss_file}/10_percent_total_strand_count.tsv",
ten_ss="{ss_file}/10percent.{chromosome}.merged.sc",
output:
"{ss_file}/full.{chromosome}.merged.sc"
params:
local_resources=config["local_resources"],
repo_path=config["repo_path"]
shell:
"if [ `grep -c rerun {input.sc_sum}` == '0' ];then rename.ul 10percent full {input.ten_ss}; else "
"python {params.repo_path}/harmoniser/genetics-sumstat-harmoniser/sumstat_harmoniser/main.py --sumstats {input.in_ss} "
"--vcf {params.local_resources}homo_sapiens-chr{wildcards.chromosome}.vcf.gz "
"--chrom_col chromosome "
"--pos_col base_pair_location "
"--effAl_col effect_allele "
"--otherAl_col other_allele "
"--rsid_col variant_id "
"--strand_counts {wildcards.ss_file}/full.{wildcards.chromosome}.merged.sc; fi"
rule summarise_strand_counts:
input:
expand("{{ss_file}}/full.{chromosome}.merged.sc", chromosome=config["chromosomes"]),
sc_sum="{ss_file}/10_percent_total_strand_count.tsv"
output:
"{ss_file}/total_strand_count.tsv"
params:
repo_path=config["repo_path"]
shell:
"if [ `grep -c rerun {input.sc_sum}` == '0' ];then cp {input.sc_sum} {wildcards.ss_file}/total_strand_count.tsv; else "
"python {params.repo_path}/harmoniser/sum_strand_counts.py -i {wildcards.ss_file} -o {wildcards.ss_file} -config {params.repo_path}/config.yaml;fi"
rule log_palindromic:
input:
expand("{local}homo_sapiens-chr22.vcf.gz", local=config["local_resources"]),
sc_sum="{ss_file}/total_strand_count.tsv"
output:
"{ss_file}/running.log"
params:
local_resources=config["local_resources"]
shell:
"palin_mode=$(grep palin_mode {wildcards.ss_file}/total_strand_count.tsv | cut -f2);"
"ratio=$(grep ratio {wildcards.ss_file}/total_strand_count.tsv);"
"echo The direction of palindromic SNPs infers as $palin_mode by $ratio > {wildcards.ss_file}/running.log;"
"echo reference: >> {wildcards.ss_file}/running.log;"
"echo '################################################################' >> {wildcards.ss_file}/running.log;"
"echo $(less {params.local_resources}homo_sapiens-chr22.vcf.gz | head -n 1000 | grep ^# | grep source) >> {wildcards.ss_file}/running.log;"
"echo $(less {params.local_resources}homo_sapiens-chr22.vcf.gz | head -n 1000 | grep ^# | grep reference) >> {wildcards.ss_file}/running.log;"
"echo $(less {params.local_resources}homo_sapiens-chr22.vcf.gz | head -n 1000 | grep ^# | grep dbSNP | sed 's/INFO=<//g' | sed 's/>//g' )>> {wildcards.ss_file}/running.log;"
"echo '################################################################' >> {wildcards.ss_file}/running.log;"
rule harmonisation:
input:
expand("{local}homo_sapiens-chr{{chromosome}}.vcf.gz", local=config["local_resources"]),
expand("{local}homo_sapiens-chr{{chromosome}}.vcf.gz.tbi", local=config["local_resources"]),
in_ss="{ss_file}/{chromosome}.merged",
sc_sum="{ss_file}/total_strand_count.tsv",
log="{ss_file}/running.log"
output:
"{ss_file}/{chromosome}.merged.hm"
params:
local_resources=config["local_resources"],
repo_path=config["repo_path"]
shell:
"palin_mode=$(grep palin_mode {input.sc_sum} | cut -f2 );"
"python {params.repo_path}/harmoniser/genetics-sumstat-harmoniser/sumstat_harmoniser/main.py --sumstats {input.in_ss} "
"--vcf {params.local_resources}homo_sapiens-chr{wildcards.chromosome}.vcf.gz "
"--hm_sumstats {wildcards.ss_file}/{wildcards.chromosome}.merged.hm "
"--hm_statfile {wildcards.ss_file}/{wildcards.chromosome}.merged.log.tsv.gz "
"--chrom_col chromosome "
"--pos_col base_pair_location "
"--effAl_col effect_allele "
"--otherAl_col other_allele "
"--rsid_col variant_id "
"--beta_col beta "
"--or_col odds_ratio "
"--or_col_lower ci_lower "
"--or_col_upper ci_upper "
"--eaf_col effect_allele_frequency "
"--na_rep_in NA "
"--na_rep_out NA "
"--palin_mode $palin_mode"
rule concatenate_chr_splits:
input:
expand("{{ss_file}}/{chromosome}.merged.hm", chromosome=config["chromosomes"])
output:
"{ss_file}/harmonised.tsv"
params:
repo_path=config["repo_path"]
shell:
"{params.repo_path}/harmoniser/cat_chroms.sh {wildcards.ss_file}"
rule get_variation_tables:
output:
expand("{local}variation.txt.gz", local=config["local_resources"]),
expand("{local}variation_synonym.txt.gz", local=config["local_resources"])
params:
remote_location=config["remote_ensembl_variation"],
local_resources=config["local_resources"]
shell:
"wget -P {params.local_resources} {params.remote_location}variation.txt.gz; "
"wget -P {params.local_resources} {params.remote_location}variation_synonym.txt.gz"
rule make_local_synonyms_table:
input:
expand("{local}variation.txt.gz", local=config["local_resources"]),
expand("{local}variation_synonym.txt.gz", local=config["local_resources"])
output:
expand("{local}rsID.sql", local=config["local_resources"])
params:
remote_location=config["remote_ensembl_variation"],
local_resources=config["local_resources"],
repo_path=config["repo_path"]
resources:
mem_mb = lambda wildcards, attempt: attempt * 12000
shell:
"python {params.repo_path}/harmoniser/make_synonym_table.py -f {params.local_resources}variation.txt.gz -id_col 0 -name_col 2 -db {params.local_resources}rsID.sql; "
"python {params.repo_path}/harmoniser/make_synonym_table.py -f {params.local_resources}variation_synonym.txt.gz -id_col 1 -name_col 4 -db {params.local_resources}rsID.sql; "
"python {params.repo_path}/harmoniser/make_synonym_table.py -index -db {params.local_resources}/rsID.sql"
rule qc:
input:
in_ss="{ss_file}/harmonised.tsv",
db=expand("{local}rsID.sql", local=config["local_resources"]) if config["local_synonyms"] else []
output:
"{ss_file}/harmonised.qc.tsv"
params:
local_resources=config["local_resources"],
repo_path=config["repo_path"]
shell:
"python {params.repo_path}/harmoniser/basic_qc.py -f {input.in_ss} -d {wildcards.ss_file}/ --log {wildcards.ss_file}/report.txt -db {input.db}"