-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-ancestry.sh
executable file
·281 lines (226 loc) · 8.96 KB
/
01-ancestry.sh
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
#!/bin/bash
# strict stop if there are any errors
set -e
# get environmental variables
source config.env
# create results directory
mkdir -p ${results_dir}/01
# log everything from this script to a logfile in the results director
exec &> >(tee ${results_dir}/01/logfile)
# Inputs:
# - Cleaned genotype data
# Processes:
# - Generate PCs (aware of relatedness if necessary)
# - Generate sparse GRM (for family data)
# Outputs:
# - Sparse GRM for each ancestry
# - PCs for each ancestry
# - mbfile for fastGWA
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
echo "There is no method for ${1}."
echo "Please run:"
echo "./01-check_data [arg]"
echo "where arg is an optional argument that can be one of:"
printf '%s\n' ${@:2}
return 1
}
arg="all"
declare -a sections=('all' 'relateds' 'pcs' 'grm' 'keeplists')
if [ -n "${1}" ]; then
arg="${1}"
containsElement ${1} ${sections[@]}
fi
section_message () {
echo "-----------------------------------------------"
echo ""
echo "$1 section"
echo ""
echo "to run this part on its own type:"
echo "$ ./01-check_data.sh $1"
echo ""
echo "-----------------------------------------------"
echo ""
echo ""
}
# If family data
## get list of relateds and list of unrelateds
## generate pcs in unrelateds and project to relateds
## nobody is removed
# If not family data
## get list of relateds and list of unrelateds
## use list of unrelateds as keeplist going forwards
if [ "$arg" = "relateds" ] || [ "$arg" = "all" ]
then
section_message "relateds"
echo "Get relateds and unrelateds"
bin/king \
-b ${genotype_processed_dir}/scratch/indep.bed \
--unrelated \
--degree 3 \
--cpus ${env_threads} \
--prefix ${genotype_processed_dir}/scratch/king
cp ${genotype_processed_dir}/scratch/kingunrelated.txt ${genotype_processed_dir}/kingunrelated.txt
bin/plink2 \
--threads ${env_threads} \
--bfile ${genotype_processed_dir}/scratch/indep \
--keep ${genotype_processed_dir}/scratch/kingunrelated.txt \
--make-bed \
--out ${genotype_processed_dir}/scratch/indep_unrelated
if [ "${env_family_data}" = "true" ]
then
bin/plink2 \
--threads ${env_threads} \
--bfile ${genotype_processed_dir}/scratch/indep \
--remove ${genotype_processed_dir}/scratch/kingunrelated.txt \
--make-bed \
--out ${genotype_processed_dir}/scratch/indep_related
fi
fi
if [ "$arg" = "pcs" ] || [ "$arg" = "all" ]
then
section_message "pcs"
echo "Generate PCs"
if test -f "${genotype_processed_dir}/pcs.txt"; then
echo "pcafile already provided"
Rscript resources/genotypes/genetic_outliers.r \
${genotype_processed_dir}/pcs.txt \
${env_pca_sd} \
${env_n_pcs} \
${genotype_processed_dir}/genetic_outliers.txt \
${results_dir}/01/pcaplot.png
n_outliers=`wc -l ${genotype_processed_dir}/genetic_outliers.txt | awk '{ print $1 }'`
if [ "${n_outliers}" != "0" ]; then
echo "WARNING: there are $n_outliers genetic outliers based on the user-provided PCs"
echo "We recommend one of the following"
echo "- changing the env_pca_sd threshold"
echo "- removing those outliers in ${genotype_processed_dir}/genetic_outliers.txt and recalculatingg the PCs"
echo "- delete the ${genotype_processed_dir}/pcs.txt file and allow the pipeline to calculate the PCs and remove outliers itself"
exit 1
fi
echo "Success - PCs already calculated and no outliers detected"
if [ ! "$arg" = "all" ]; then
exit 0
fi
fi
pcs_unrelated () {
bin/flashpca \
--bfile ${genotype_processed_dir}/scratch/indep_unrelated \
--ndim ${env_n_pcs} \
--outpc ${genotype_processed_dir}/scratch/fastpca_pcs_unrelated.txt \
--outload ${genotype_processed_dir}/scratch/fastpca_loadings.txt \
--outmeans ${genotype_processed_dir}/scratch/fastpca_meansd.txt \
--numthreads ${env_threads} \
--outval ${genotype_processed_dir}/scratch/fastpca_eigenvalues.txt \
--outvec ${genotype_processed_dir}/scratch/fastpca_eigenvectors.txt \
--outpve ${genotype_processed_dir}/scratch/fastpca_pve.txt
}
pcs_related () {
bin/flashpca \
--bfile ${genotype_processed_dir}/scratch/indep_related \
--project \
--inmeansd ${genotype_processed_dir}/scratch/fastpca_meansd.txt \
--outproj ${genotype_processed_dir}/scratch/fastpca_pcs_related.txt \
--inload ${genotype_processed_dir}/scratch/fastpca_loadings.txt \
--numthreads ${env_threads}
}
if [ "${env_family_data}" = "true" ]
then
pcs_unrelated
pcs_related
sed -i 1d ${genotype_processed_dir}/scratch/fastpca_pcs_related.txt
cat ${genotype_processed_dir}/scratch/fastpca_pcs_unrelated.txt ${genotype_processed_dir}/scratch/fastpca_pcs_related.txt > ${genotype_processed_dir}/pcs.txt
else
pcs_unrelated
cp ${genotype_processed_dir}/scratch/fastpca_pcs_unrelated.txt ${genotype_processed_dir}/pcs.txt
fi
echo "Check PCs e.g. by plotting them"
Rscript resources/genotypes/genetic_outliers.r \
${genotype_processed_dir}/pcs.txt \
${env_pca_sd} \
${env_n_pcs} \
${genotype_processed_dir}/genetic_outliers.txt \
${results_dir}/01/pcaplot.png
n_outliers=`wc -l ${genotype_processed_dir}/genetic_outliers.txt | awk '{ print $1 }'`
if [ "${n_outliers}" = "0" ]
then
echo "No genetic outliers detected"
else
echo "Remove genetic outliers from data"
echo "Found ${n_outliers}. Removing them and recalculating PCs"
bin/plink2 \
--threads ${env_threads} \
--bfile ${genotype_processed_dir}/scratch/indep_unrelated \
--remove ${genotype_processed_dir}/genetic_outliers.txt \
--make-bed \
--out ${genotype_processed_dir}/scratch/indep_unrelated
if [ "${env_family_data}" = "true" ]
then
bin/plink2 \
--threads ${env_threads} \
--bfile ${genotype_processed_dir}/scratch/indep_related \
--remove ${genotype_processed_dir}/genetic_outliers.txt \
--make-bed \
--out ${genotype_processed_dir}/scratch/indep_related
pcs_unrelated
pcs_related
sed -i 1d ${genotype_processed_dir}/scratch/fastpca_pcs_related.txt
cat ${genotype_processed_dir}/scratch/fastpca_pcs_unrelated.txt ${genotype_processed_dir}/scratch/fastpca_pcs_related.txt > ${genotype_processed_dir}/pcs.txt
else
pcs_unrelated
cp ${genotype_processed_dir}/scratch/fastpca_pcs_unrelated.txt ${genotype_processed_dir}/pcs.txt
fi
mv ${results_dir}/01/pcaplot.png ${results_dir}/01/pcaplot_round1.png
Rscript resources/genotypes/genetic_outliers.r \
${genotype_processed_dir}/pcs.txt \
${env_pca_sd} \
${env_n_pcs} \
${genotype_processed_dir}/genetic_outliers.txt \
${results_dir}/01/pcaplot.png
fi
fi
if [ "$arg" = "grm" ] || [ "$arg" = "all" ]
then
echo "Generate sparse GRM"
section_message "grm"
if [ "${env_family_data}" = "true" ]
then
bin/king \
-b ${genotype_processed_dir}/scratch/indep.bed \
--related \
--degree 3 \
--cpus ${env_threads} \
--prefix ${genotype_processed_dir}/scratch/king
awk '{ print $1, $3, $14 }' ${genotype_processed_dir}/scratch/king.kin0 | grep -v "4th" | sed 1d > ${genotype_processed_dir}/scratch/king.kin0.formatted
Rscript resources/genotypes/pedFAM.R \
${genotype_processed_dir}/scratch/indep.fam \
${genotype_processed_dir}/scratch/king.kin0.formatted \
${genotype_processed_dir}/sparsegrm
fi
fi
if [ "$arg" = "keeplists" ] || [ "$arg" = "all" ]
then
section_message "keeplists"
echo "Final keep lists"
# Unrelateds
# +kingunrelated.txt
# -genetic_outliers.txt
cat ${genotype_processed_dir}/kingunrelated.txt | \
grep -vw -f ${genotype_processed_dir}/genetic_outliers.txt > \
${genotype_processed_dir}/unrelated_keep.txt
nunrelated=$(cat ${genotype_processed_dir}/unrelated_keep.txt | wc -l)
echo "N Unrelated: ${nunrelated}"
# Relateds
# +fam file (everyone)
# -genetic_outliers.txt
if [ "${env_family_data}" = "true" ]
then
awk '{ print $1"\t"$2 }' ${genotype_processed_dir}/scratch/indep.fam | \
grep -vw -f ${genotype_processed_dir}/genetic_outliers.txt > \
${genotype_processed_dir}/related_keep.txt
fi
nrelated=$(cat ${genotype_processed_dir}/related_keep.txt | wc -l)
echo "N Related: ${nrelated}"
fi
echo "Successfully generated PCs etc!"