-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
294 lines (265 loc) · 10.7 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import json
f = open("params.json")
params_dict = json.load(f)
f.close()
sensible_defaults = params_dict["sensible_defaults"]
# Input files
covariate_file = params_dict["covariate_file"]
genotype_file = params_dict["genotype_file"]
phenotype_file = params_dict["phenotype_file"]
population_name = params_dict["population_name"]
genotype_name = params_dict["genotype_name"]
covariate_name = params_dict["covariate_name"]
phenotype_name = params_dict["phenotype_name"]
model_type = params_dict["model_type"]
# Additional constants
chromosomes = params_dict["chromosomes"]
chunks = params_dict["chunks"]
window_size = params_dict["window_size"]
qsub_array = params_dict["qsub_array"]
def readable_bp_number(bp):
if ((bp % 1e6) == 0):
return f"{int(bp/1e6)}Mbase"
elif ((bp % 1e3) == 0):
return f"{int(bp/1e3)}kbase"
else:
return f"{bp}base"
model = f"{readable_bp_number(window_size)}_{model_type}"
if (sensible_defaults):
out_covariate_dir = f"data/{population_name}/{genotype_name}_{phenotype_name}_{covariate_name}/covariates/"
out_ped_dir = f"data/{population_name}/{genotype_name}/ped/"
out_grm_dir = f"data/{population_name}/{genotype_name}/grm/"
out_phenotype_dir = f"data/{population_name}/{phenotype_name}/"
out_greml_intermediate_dir = f"data/{population_name}/{genotype_name}_{phenotype_name}_{covariate_name}/greml_intermediate/{model}/"
out_hsq_dir = f"data/{population_name}/{genotype_name}_{phenotype_name}_{covariate_name}/hsq/{model}/"
out_results_dir = f"data/{population_name}/{genotype_name}_{phenotype_name}_{covariate_name}/results/"
else: # Manually select output directories
out_covariate_dir = "data/tri1_sQTL_covariates/"
out_ped_dir = "data/tri1_sQTL_ped/"
out_grm_dir = "data/tri1_sQTL_grm/"
out_phenotype_dir = "data/tri1_sQTL_phenotype/"
out_greml_intermediate_dir = "data/tri1_sQTL_greml_intermediate/"
out_hsq_dir = "data/tri1_sQTL_hsq/"
out_results_dir = "data/tri1_results/"
# Making output directories
import os
def makedirs(path):
if not os.path.exists(path):
os.makedirs(path)
makedirs(out_covariate_dir)
makedirs(out_ped_dir)
makedirs(out_grm_dir)
makedirs(out_phenotype_dir)
makedirs(out_greml_intermediate_dir)
makedirs(out_hsq_dir)
makedirs(out_results_dir)
# Functions
# Rules
rule all:
input:
out_results_dir + model + ".csv"
# Processing genotype files
rule vcf_to_bed:
input:
genotype_file
output:
out_ped_dir + "genotype.bed",
out_ped_dir + "genotype.bim",
out_ped_dir + "genotype.fam",
out_ped_dir + "genotype.log",
out_ped_dir + "genotype.nosex"
shell:
"plink "
"--vcf " + genotype_file + " "
"--recode --const-fid --make-bed --mac 1 "
"--out " + out_ped_dir + "genotype"
rule chr_grm:
input:
out_ped_dir + "genotype.bed",
out_ped_dir + "genotype.bim",
out_ped_dir + "genotype.fam",
out_ped_dir + "genotype.log",
out_ped_dir + "genotype.nosex"
output:
out_grm_dir + "genotype{chrom}.grm.N.bin",
out_grm_dir + "genotype{chrom}.grm.bin",
out_grm_dir + "genotype{chrom}.grm.id",
out_grm_dir + "genotype{chrom}.log"
shell:
"gcta64 "
"--bfile " + out_ped_dir + "genotype" + " "
"--make-grm-bin --make-grm-alg 0 --chr {wildcards.chrom} "
"--out " + out_grm_dir + "genotype{wildcards.chrom}"
rule whole_grm:
input:
expand(out_grm_dir + "genotype{chrom}.grm.N.bin", chrom=chromosomes),
expand(out_grm_dir + "genotype{chrom}.grm.bin", chrom=chromosomes),
expand(out_grm_dir + "genotype{chrom}.grm.id", chrom=chromosomes),
expand(out_grm_dir + "genotype{chrom}.log", chrom=chromosomes)
output:
chr_list=out_grm_dir + "all_chrs_list.txt",
grm_N=out_grm_dir + "complete.grm.N.bin",
grm_bin=out_grm_dir + "complete.grm.bin",
grm_id=out_grm_dir + "complete.grm.id",
grm_log=out_grm_dir + "complete.log"
shell:
"echo > {output.chr_list} && " + \
" ".join(["echo '" + out_grm_dir + "genotype" + str(chrom) + "' >> {output.chr_list} && " for chrom in chromosomes]) + \
"gcta64 "
"--make-grm-bin "
"--mgrm {output.chr_list} "
"--out " + out_grm_dir + "complete"
rule unrelated_grm:
input:
out_grm_dir + "complete.grm.N.bin",
out_grm_dir + "complete.grm.bin",
out_grm_dir + "complete.grm.id",
out_grm_dir + "complete.log",
output:
out_grm_dir + "unrelated.grm.N.bin",
out_grm_dir + "unrelated.grm.bin",
out_grm_dir + "unrelated.grm.id",
out_grm_dir + "unrelated.log"
shell:
"gcta64 "
"--grm " + out_grm_dir + "complete "
"--grm-cutoff 0.05 --make-grm-bin "
"--out " + out_grm_dir + "unrelated"
rule excluded_samples_list:
input:
complete=out_grm_dir + "complete.grm.id",
unrelated=out_grm_dir + "unrelated.grm.id",
output:
out_grm_dir + "excluded_samples.txt"
shell:
"diff "
"--new-line-format=%L "
"--unchanged-line-format='' "
"{input.complete} "
"{input.unrelated} | "
r"sed 's/\t/ /g' > "
"{output} || :"
# Processing phenotype files
rule bed_to_pheno:
input:
phenotype_file
output:
out_phenotype_dir + "phenotype.txt",
out_phenotype_dir + "phenotype_info.txt"
script:
"scripts/bed_to_phenotype.jl"
checkpoint chunk_phenotype_info:
input:
out_phenotype_dir + "phenotype_info.txt"
output:
expand(out_phenotype_dir + "phenotype_info.txt_{chunk}", chunk = [str(chunk).rjust(7, '0') for chunk in [*range(0, chunks)]])
shell:
"split -n l/" + str(chunks) + " -a 7 -d {input} " + out_phenotype_dir + "phenotype_info.txt_"
# Splitting covariate files
rule split_covariate:
input:
covariate_file
output:
out_covariate_dir + "quant_cov.txt",
out_covariate_dir + "qual_cov.txt"
script:
"scripts/fetal_scripts/process_covariates.jl"
# Checkpoint before running GREML
# Running GREML
if (model_type == "window"):
greml_script = "scripts/run_window.jl"
compile_script = "scripts/hsq_window.jl"
elif (model_type == "cis"):
greml_script = "scripts/run_cis_greml_wrapper.jl"
compile_script = "scripts/hsq_cis.jl"
if qsub_array:
rule greml:
input:
genotypebed=out_ped_dir + "genotype.bed",
genotypebim=out_ped_dir + "genotype.bim",
genotypefam=out_ped_dir + "genotype.fam",
genotypelog=out_ped_dir + "genotype.log",
genotypenosex=out_ped_dir + "genotype.nosex",
excludedsamples=out_grm_dir + "excluded_samples.txt",
phenotype=out_phenotype_dir + "phenotype.txt",
phenotypeinfo=expand(out_phenotype_dir + "phenotype_info.txt_{chunk}", chunk = [str(chunk).rjust(7, '0') for chunk in [*range(0, chunks)]]),
quantcov=out_covariate_dir + "quant_cov.txt",
qualcov=out_covariate_dir + "qual_cov.txt",
grmN=expand(out_grm_dir + "genotype{chrom}.grm.N.bin", chrom = chromosomes),
grmbin=expand(out_grm_dir + "genotype{chrom}.grm.bin", chrom = chromosomes),
grmid=expand(out_grm_dir + "genotype{chrom}.grm.id", chrom = chromosomes),
grmlog=expand(out_grm_dir + "genotype{chrom}.log", chrom = chromosomes)
output:
expand(out_greml_intermediate_dir + ".{chunk}_chunk.done", chunk = [str(chunk).rjust(7, '0') for chunk in [*range(0, chunks)]])
params:
genotypeprefix=out_ped_dir + "genotype",
grmprefix=out_grm_dir + "genotype",
outgremlintermediatedir=out_greml_intermediate_dir,
outhsqdir=out_hsq_dir,
windowsize = window_size
run:
with open(out_greml_intermediate_dir + "snakemake_info.json", "w") as outfile:
json.dump({
"input": {
"excludedsamples": input.excludedsamples,
"phenotype": input.phenotype,
"phenotypeinfo": input.phenotypeinfo,
"quantcov": input.quantcov,
"qualcov": input.qualcov
},
"output": output,
"params": {
"genotypeprefix": params.genotypeprefix,
"grmprefix": params.grmprefix,
"outgremlintermediatedir": params.outgremlintermediatedir,
"outhsqdir": params.outhsqdir,
"windowsize": params.windowsize
}
},
outfile)
shell("qsub "
"-N qsub_greml "
"-o logs/joblog.qsub_greml.\$TASK_ID.log "
"-e logs/joblog.qsub_greml.\$TASK_ID.err "
"-t 1:{chunks} "
"scripts/run_cis_qsub.sh " + out_greml_intermediate_dir + "snakemake_info.json")
shell("while [[ -n $(qstat | grep qsub_greml ) ]]; do "
"sleep 300; "
"done")
shell("rm " + out_greml_intermediate_dir + "snakemake_info.json")
else:
rule greml:
input:
genotypebed=out_ped_dir + "genotype.bed",
genotypebim=out_ped_dir + "genotype.bim",
genotypefam=out_ped_dir + "genotype.fam",
genotypelog=out_ped_dir + "genotype.log",
genotypenosex=out_ped_dir + "genotype.nosex",
excludedsamples=out_grm_dir + "excluded_samples.txt",
phenotype=out_phenotype_dir + "phenotype.txt",
phenotypeinfo=out_phenotype_dir + "phenotype_info.txt_{chunk}",
quantcov=out_covariate_dir + "quant_cov.txt",
qualcov=out_covariate_dir + "qual_cov.txt",
grmN=expand(out_grm_dir + "genotype{chrom}.grm.N.bin", chrom = chromosomes),
grmbin=expand(out_grm_dir + "genotype{chrom}.grm.bin", chrom = chromosomes),
grmid=expand(out_grm_dir + "genotype{chrom}.grm.id", chrom = chromosomes),
grmlog=expand(out_grm_dir + "genotype{chrom}.log", chrom = chromosomes)
output:
out_greml_intermediate_dir + ".{chunk}_chunk.done"
params:
genotypeprefix=out_ped_dir + "genotype",
grmprefix=out_grm_dir + "genotype",
outgremlintermediatedir=out_greml_intermediate_dir,
outhsqdir=out_hsq_dir,
windowsize = window_size
script:
greml_script
rule compile_greml:
input:
expand(out_greml_intermediate_dir + ".{chunk}_chunk.done", chunk = [str(chunk).rjust(7, '0') for chunk in [*range(0, chunks)]])
output:
out_results_dir + model + ".csv"
params:
outhsqdir=out_hsq_dir
script:
compile_script