-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
163 lines (148 loc) · 5.16 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
import os
import pandas as pd
from snakemake.utils import validate
configfile: 'config.yaml'
validate(config, 'schemas/config.schema.yaml')
traits = pd.read_table(config['traits']).set_index('name', drop=False)
validate(traits, 'schemas/traits.schema.yaml')
def get_variants():
if config['symlink_data']:
return os.path.join(
config['symlink_dir'],
os.path.basename(config['variants']))
return config['variants']
def get_plink_prefix(trait=False):
if 'plink_prefix' not in config or not config['plink_prefix']:
# Make a PLINK prefix based on the VCF file
variant_fname = get_variants()
if variant_fname.endswith('.gz'):
variant_fname = os.path.splitext(variant_fname)[0]
prefix = os.path.splitext(variant_fname)[0]
if not trait:
return '{prefix}_plink'.format(prefix=prefix)
return 'results/{{trait}}/plink/{prefix}_plink'.format(
prefix=os.path.basename(prefix))
# A PLINK prefix has been supplied
if not trait:
return config['plink_prefix']
return 'results/{{trait}}/plink/{prefix}'.format(
prefix=os.path.basename(config['plink_prefix']))
def get_plink_files(trait=False, extensions=['fam', 'bed', 'bim']):
if not trait:
return expand('{prefix}.{ext}',
prefix=get_plink_prefix(trait), ext=extensions)
return expand('results/{{trait}}/plink/{prefix}.{ext}',
prefix=os.path.basename(get_plink_prefix(trait)), ext=extensions)
localrules: all, link_variants, link_plink, link_relatedness,
setup_plink_traits, cluster_config
rule all:
input:
expand('results/{trait}/{trait}.assoc.txt', trait=traits.name)
rule cluster_config:
output: directory( \
'{home}/.config/snakemake/{profile_name}' \
.format(home=os.path.expanduser('~'), \
profile_name=config['cluster']['profile_name']))
params:
url=config['cluster']['cookiecutter_url'],
profile_name=config['cluster']['profile_name']
conda: 'envs/cluster_config.yaml'
shell: 'bash scripts/cluster_config.sh {params.url} {params.profile_name}'
rule gemma:
input:
plink=get_plink_files(trait=True),
relatedness_matrix='{prefix}.relmat.cXX.txt'.format(
prefix=get_plink_prefix(trait=True))
output:
'results/{trait}/{trait}.assoc.txt'
params:
plink_prefix=os.path.abspath(get_plink_prefix(trait=True)),
output_prefix='{trait}'
threads: 1
conda: 'envs/gemma.yaml'
shell:
'''
cd $(dirname {output})
gemma \
-bfile {params.plink_prefix} \
-k {params.plink_prefix}.relmat.cXX.txt \
-lmm 4 \
-o {params.output_prefix}
mv output/* .
rmdir output
'''
rule link_relatedness:
input:
'{prefix}.relmat.cXX.txt'.format(
prefix=get_plink_prefix())
output:
'{prefix}.relmat.cXX.txt'.format(
prefix=get_plink_prefix(trait=True))
shell: 'ln -sr {input} {output}'
rule gemma_relatedness:
input:
plink=get_plink_files(trait=True)
output:
relatedness_matrix='{prefix}.relmat.cXX.txt'.format(
prefix=get_plink_prefix(trait=True))
params:
plink_prefix=os.path.abspath(get_plink_prefix(trait=True)),
output_prefix='{prefix}.relmat'.format(
prefix=os.path.basename(get_plink_prefix(trait=True)))
threads: 1
conda: 'envs/gemma.yaml'
shell:
'''
cd $(dirname {output.relatedness_matrix})
gemma \
-bfile {params.plink_prefix} \
-gk 1 \
-o {params.output_prefix}
mv output/* .
rmdir output
'''
rule setup_plink_traits:
input:
fam=get_plink_files(extensions='fam'),
trait=lambda wildcards: traits.filename[wildcards.trait]
output:
fam=get_plink_files(trait=True, extensions='fam')
shell:
'''
paste -d' ' <(cut -f1-5 -d' ' {input.fam}) <(tail -n+2 {input.trait} | cut -f2) > {output.fam}
'''
rule link_plink:
input:
plink=get_plink_files(extensions=['bim', 'bed'])
output:
plink=get_plink_files(trait=True, extensions=['bim', 'bed'])
params:
output_directory=os.path.dirname(get_plink_prefix(trait=True))
shell: 'ln -sr {input.plink} {params.output_directory}'
rule plink:
input:
vcf=get_variants()
output:
plink=get_plink_files()
params:
plink_prefix=get_plink_prefix(),
hwe_p=config['snp_filtering']['hwe_p'],
maf=config['snp_filtering']['maf'],
allow_extra_chr='--allow-extra-chr' if config['plink']['allow_extra_chr'] else '',
family_id_flags=config['plink']['family_id_flags']
conda: 'envs/plink.yaml'
shell:
'''
plink \
--make-bed \
{params.allow_extra_chr} \
{params.family_id_flags} \
--maf {params.maf} \
--hwe {params.hwe_p} \
--out {params.plink_prefix} \
--vcf {input.vcf}
'''
rule link_variants:
input: config['variants']
output: get_variants()
shell: 'ln -sr {input} {output}'