/
impute2Imputation.ftl
213 lines (160 loc) · 4.85 KB
/
impute2Imputation.ftl
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
#MOLGENIS nodes=1 cores=1 mem=8
known_haps_g="${known_haps_g}"
m="${m}"
h="${h}"
l="${l}"
additonalImpute2Param="${additonalImpute2Param}"
chr="${chr}"
fromChrPos="${fromChrPos}"
toChrPos="${toChrPos}"
imputationIntermediatesFolder="${imputationIntermediatesFolder}"
impute2Bin="${impute2Bin}"
aditionalArgs="${additonalImpute2Param}"
${stage} impute/${impute2version}
<#noparse>
tmpOutput="${imputationIntermediatesFolder}/~chr${chr}_${fromChrPos}-${toChrPos}"
finalOutput="${imputationIntermediatesFolder}/chr${chr}_${fromChrPos}-${toChrPos}"
echo "known_haps_g: ${known_haps_g}";
echo "chr: ${chr}"
echo "fromChrPos: ${fromChrPos}"
echo "toChrPos: ${toChrPos}"
echo "interMediFolder: ${imputationIntermediatesFolder}"
echo "tmpOutput: ${tmpOutput}"
echo "finalOutput: ${finalOutput}"
alloutputsexist \
"${finalOutput}" \
"${finalOutput}_info" \
"${finalOutput}_info_by_sample" \
"${finalOutput}_summary" \
"${finalOutput}_warnings"
startTime=$(date +%s)
echo "tmpOutput: ${tmpOutput}"
getFile $known_haps_g
inputs $known_haps_g
getFile $m
inputs $m
# $h can be multiple files. Here we will check each file and do a getFile, if needed, for each file
for refH in $h
do
echo "Reference haplotype file: ${refH}"
getFile $refH
inputs $refH
done
# $l can be multiple files. Here we will check each file and do a getFile, if needed, for each file
for refL in $l
do
echo "Reference legend file: ${refL}"
getFile $refL
inputs $refL
done
# DECLARE POSSIBLE VALUES FOR additonalImpute2Param HERE
impute2FileArg[0]="-sample_g_ref"
impute2FileArg[1]="-exclude_samples_g"
impute2FileArg[2]="-exclude_snps_g"
impute2FileArg[3]="-sample_g"
# This function test if element is in array
# http://stackoverflow.com/questions/3685970/bash-check-if-an-array-contains-a-value
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 1; done
return 0
}
aditionalArgsArray=($aditionalArgs)
# Loop over all aditional args. If arg is encounterd that requeres file then do inputs and getFile on next element
for (( i=0; i<${#aditionalArgsArray[@]}; i++ ));
do
currentArg=${aditionalArgsArray[$i]}
containsElement $currentArg ${impute2FileArg[@]}
if [[ $? -eq 1 ]];
then
i=`expr $i + 1`
file=${aditionalArgsArray[$i]}
echo "File for this argument: $currentArg will get and is requered for this script to start $file"
inputs $file
get $file
echo "Found additional Impute2 file: $file"
fi
done
mkdir -p $imputationIntermediatesFolder
$impute2Bin \
-known_haps_g $known_haps_g \
-m $m \
-h $h \
-l $l \
-int $fromChrPos $toChrPos \
-o $tmpOutput \
-use_prephased_g \
$additonalImpute2Param
#Get return code from last program call
returnCode=$?
echo "returnCode impute2: ${returnCode}"
if [ $returnCode -eq 0 ]
then
#If there are no SNPs in this bin we will create empty files
if [ ! -f ${tmpOutput}_info ]
then
echo "Impute2 did not output files. Usually this means there where no SNPs in this region so, generate empty files"
echo "Touching file: ${tmpOutput}"
echo "Touching file: ${tmpOutput}_info"
echo "Touching file: ${tmpOutput}_info_by_sample"
touch ${tmpOutput}
touch ${tmpOutput}_info
touch ${tmpOutput}_info_by_sample
fi
echo -e "\nMoving temp files to final files\n\n"
for tempFile in ${tmpOutput}* ; do
finalFile=`echo $tempFile | sed -e "s/~//g"`
echo "Moving temp file: ${tempFile} to ${finalFile}"
mv $tempFile $finalFile
putFile $finalFile
done
elif [ `grep "ERROR: There are no type 2 SNPs after applying the command-line settings for this run" ${tmpOutput}_summary | wc -l | awk '{print $1}'` == 1 ]
then
if [ ! -f ${tmpOutput}_info ]
then
echo "Impute2 found no type 2 SNPs in this region. We now create empty output"
echo "Touching file: ${tmpOutput}"
echo "Touching file: ${tmpOutput}_info"
echo "Touching file: ${tmpOutput}_info_by_sample"
touch ${tmpOutput}
touch ${tmpOutput}_info
touch ${tmpOutput}_info_by_sample
fi
echo -e "\nMoving temp files to final files\n\n"
for tempFile in ${tmpOutput}* ; do
finalFile=`echo $tempFile | sed -e "s/~//g"`
echo "Moving temp file: ${tempFile} to ${finalFile}"
mv $tempFile $finalFile
putFile $finalFile
done
else
echo -e "\nNon zero return code not making files final. Existing temp files are kept for debugging purposes\n\n"
#Return non zero return code
exit 1
fi
endTime=$(date +%s)
#Source: http://stackoverflow.com/questions/12199631/convert-seconds-to-hours-minutes-seconds-in-bash
num=$endTime-$startTime
min=0
hour=0
day=0
if((num>59));then
((sec=num%60))
((num=num/60))
if((num>59));then
((min=num%60))
((num=num/60))
if((num>23));then
((hour=num%24))
((day=num/24))
else
((hour=num))
fi
else
((min=num))
fi
else
((sec=num))
fi
echo "Running time: ${day} days ${hour} hours ${min} mins ${sec} secs"
</#noparse>