-
Notifications
You must be signed in to change notification settings - Fork 3
/
create-biolayout-file.sh
executable file
·310 lines (267 loc) · 7.67 KB
/
create-biolayout-file.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#! /bin/bash
SCRIPT_NAME=$(basename $0)
DIR_NAME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo_timestamp()
{
date +"%R:%S $*"
}
while getopts b:t:g:o:c:d:n:p:l:uh ARG
do
case ${ARG} in
(b) UNSORTED_BAM_FILE=$(readlink -f "$OPTARG");;
(t) CHROMOSOME_LENGTH_FILE=$(readlink -f "$OPTARG");;
(g) GTF_FILE=$(readlink -f "$OPTARG");;
(o) OUTPUT_DIRECTORY=$(readlink -f "$OPTARG");;
(c) CACHE_DIRECTORY=$(readlink -f "$OPTARG");;
(d) GENE_LIST="$OPTARG";;
(n) GENE_LIST=$(cat "$OPTARG");;
(p) PERCENTAGE="$OPTARG";;
(l) COVERAGE="$OPTARG";;
(u) UNIQUIFY="-u";;
(h)
echo "${SCRIPT_NAME}"
echo " One of -d or -n must be specified in addition to all the other options"
echo " -b <file> The unsorted BAM file"
echo " -t <file> The chromosome length file"
echo " -g <file> The GTF file"
echo " -o <directory> The directory in which to place the output"
echo " -c <directory> The directory in which to cache intermediate files"
echo " -d \"GENE1, GENE2, ..., GENEN\" A list of genes to examine"
echo " -n <file> A file containing a list of genes to examine"
echo " -p <value> The percentage similarity value (default 85)"
echo " -l <value> The percentage coverage value (default 55)"
echo " -u discard redundant reads"
exit 0;;
(*)
exit 1;;
esac
done
if [ ! "${UNSORTED_BAM_FILE}" ] || [ ! -e "${UNSORTED_BAM_FILE}" ];
then
echo "BAM file not supplied or not found; use -b option"
exit 1;
fi
if [ ! "${CHROMOSOME_LENGTH_FILE}" ] || [ ! -e "${CHROMOSOME_LENGTH_FILE}" ];
then
echo "Chromosome length file not supplied or not found; use -t option"
exit 1;
fi
if [ ! "${GTF_FILE}" ] || [ ! -e "${GTF_FILE}" ];
then
echo "GTF file not supplied or not found; use -g option"
exit 1;
fi
if [ ! "${OUTPUT_DIRECTORY}" ];
then
OUTPUT_DIRECTORY=$(readlink -f output)
fi
if [ ! "${CACHE_DIRECTORY}" ];
then
CACHE_DIRECTORY=$(readlink -f cache)
fi
if [ ! "${GENE_LIST}" ];
then
echo "Gene list is empty; use -d or -n options"
exit 1
fi
if [ ! "${PERCENTAGE}" ];
then
PERCENTAGE="85"
fi
if [ ! "${COVERAGE}" ];
then
COVERAGE="55"
fi
mkdir -p ${OUTPUT_DIRECTORY}
EXITCODE="$?"
if [ "$EXITCODE" != 0 ];
then
echo_timestamp "Cannot create ${OUTPUT_DIRECTORY}"
exit $EXITCODE
fi
echo_timestamp "Computing valid gene names..."
VALID_GENES=$(perl -pe 's/.*gene_name "([^"]+)".*/\1/' ${GTF_FILE} | \
tr '[:lower:]' '[:upper:]' | sort | uniq)
GENE_LIST=$(echo "$GENE_LIST" | tr '[:lower:]' '[:upper:]' | perl -pe 's/\s*,\s*|\s+/ /g')
for GENE in ${GENE_LIST}
do
echo ${VALID_GENES} | grep -q "\(^\|.*\s\+\)${GENE}\($\|.*\s\+\)"
if [ "$?" != 0 ];
then
echo_timestamp "Gene ${GENE} is not present in ${GTF_FILE}."
continue
exit 1
fi
done
NUM_CORES=$(nproc)
echo_timestamp "Using ${NUM_CORES} cores..."
echo_timestamp "Computing hash..."
INPUT_HASH=$(cat ${UNSORTED_BAM_FILE} ${CHROMOSOME_LENGTH_FILE} ${GTF_FILE} | md5sum)
INPUT_HASH=${INPUT_HASH%% *}
echo_timestamp "BAM file: ${UNSORTED_BAM_FILE}"
echo_timestamp "Chromosome length file: ${CHROMOSOME_LENGTH_FILE}"
echo_timestamp "GTF file: ${GTF_FILE}"
echo_timestamp "Output directory: ${OUTPUT_DIRECTORY}"
echo_timestamp "Gene list: ${GENE_LIST}"
echo_timestamp "Input hash: ${INPUT_HASH}"
SAMTOOLS=samtools
R_SCRIPT=Rscript
COMPUTE_DATA=0
HASH_DIRECTORY="${CACHE_DIRECTORY}/${INPUT_HASH}"
CACHE_LOCK="${HASH_DIRECTORY}/lock"
takeCacheLock()
{
if ( set -o noclobber; echo_timestamp "$$" > "$CACHE_LOCK") 2> /dev/null;
then
trap 'rm -f "$CACHE_LOCK"; exit $?' INT TERM EXIT
echo_timestamp "Aquired lock ${CACHE_LOCK}..."
return 1
else
return 0
fi
}
takeCacheLockBlocking()
{
echo_timestamp "Waiting for ${CACHE_LOCK}..."
takeCacheLock
while (( "$?" == "0" ))
do
sleep 30
takeCacheLock
done
}
waitForCacheToComplete()
{
takeCacheLockBlocking
releaseCacheLock
}
releaseCacheLock()
{
rm -f "$CACHE_LOCK"
trap - INT TERM EXIT
echo_timestamp "Released lock ${CACHE_LOCK}..."
}
cacheIsLocked()
{
takeCacheLock
if [ "$?" == "1" ];
then
return 1
releaseCacheLock
else
return 0
fi
}
echo_timestamp Checking if ${HASH_DIRECTORY} exists...
if [ ! -d "${HASH_DIRECTORY}" ];
then
COMPUTE_DATA=1
mkdir -p ${HASH_DIRECTORY}
EXITCODE="$?"
if [ "$EXITCODE" != 0 ];
then
echo_timestamp "Cannot create ${HASH_DIRECTORY}"
exit $EXITCODE
fi
takeCacheLock
else
if [ cacheIsLocked ];
then
waitForCacheToComplete
fi
if [ ! -e "${HASH_DIRECTORY}/valid" ]
then
# There is a possible race condition where two or more processes
# manage to get here simultaneously, but the chances of it happening
# are fairly small
COMPUTE_DATA=1
takeCacheLock
if [ "$?" == "0" ];
then
echo_timestamp "!!! Probable race condition encountered, exiting."
exit 1
fi
fi
fi
BASENAME_BAM_FILE=$(basename ${UNSORTED_BAM_FILE})
NO_EXT_BAM_FILE="${BASENAME_BAM_FILE%.*}"
SAMTOOLS_SORTED_BAM_FILE="${HASH_DIRECTORY}/${NO_EXT_BAM_FILE}-sorted"
SORTED_BAM_FILE="${SAMTOOLS_SORTED_BAM_FILE}.bam"
GRANGES_FILE="${HASH_DIRECTORY}/${NO_EXT_BAM_FILE}-GRanges.RData"
GTF_ANNOTATION_FILE="${HASH_DIRECTORY}/${NO_EXT_BAM_FILE}-ensembl_gtfannotation.RData"
if [ "${COMPUTE_DATA}" == "1" ];
then
echo_timestamp "Sorting ${UNSORTED_BAM_FILE}..."
${SAMTOOLS} sort ${UNSORTED_BAM_FILE} ${SAMTOOLS_SORTED_BAM_FILE}
EXITCODE="$?"
if [ "$EXITCODE" != 0 ];
then
echo_timestamp "samtools sort step failed"
exit $EXITCODE
fi
${R_SCRIPT} ${DIR_NAME}/grangesscript.R -b "${SORTED_BAM_FILE}" -t "${CHROMOSOME_LENGTH_FILE}" \
-o "${GRANGES_FILE}"
EXITCODE="$?"
if [ "$EXITCODE" != 0 ];
then
echo_timestamp "grangesscript.R failed"
exit $EXITCODE
fi
${R_SCRIPT} ${DIR_NAME}/grangesscript_gtf.R -g "${GTF_FILE}" -t "${CHROMOSOME_LENGTH_FILE}" \
-o "${GTF_ANNOTATION_FILE}"
EXITCODE="$?"
if [ "$EXITCODE" != 0 ];
then
echo_timestamp "grangesscript_gtf.R failed"
exit $EXITCODE
fi
touch "${HASH_DIRECTORY}/valid"
releaseCacheLock
else
echo_timestamp "Using cached data..."
fi
# Make a symlink in the job directory to the cached data
ln -s ${HASH_DIRECTORY} ${OUTPUT_DIRECTORY}/${INPUT_HASH}
R2R_OUTPUT_DIR="${OUTPUT_DIRECTORY}/r2r_output"
for GENE in ${GENE_LIST}
do
if [ ! -e "${HASH_DIRECTORY}/${GENE}.tab" ];
then
takeCacheLockBlocking
echo_timestamp "Writing ${GENE}.tab"
${R_SCRIPT} ${DIR_NAME}/findoverlaps.R -g "${GRANGES_FILE}" -e "${GTF_ANNOTATION_FILE}" -d "${GENE}" \
-p "${HASH_DIRECTORY}/"
releaseCacheLock
EXITCODE="$?"
if [ "$EXITCODE" != 0 ];
then
echo_timestamp "findoverlaps.R failed"
exit $EXITCODE
fi
else
echo_timestamp "Using cached ${GENE}.tab"
fi
echo_timestamp "Writing ${GENE}.fasta"
${DIR_NAME}/tab-to-fasta.sh ${UNIQUIFY} -t "${HASH_DIRECTORY}/${GENE}.tab" > \
"${OUTPUT_DIRECTORY}/${GENE}.fasta"
echo_timestamp "Writing ${GENE}.nodeclass"
${DIR_NAME}/tab-to-nodeclass.sh ${UNIQUIFY} -e -t "${HASH_DIRECTORY}/${GENE}.tab" > \
"${OUTPUT_DIRECTORY}/${GENE}.nodeclass"
rm -rf "${R2R_OUTPUT_DIR}"
${DIR_NAME}/read2read.py -p ${PERCENTAGE} -l ${COVERAGE} -a ${NUM_CORES} \
"${OUTPUT_DIRECTORY}/${GENE}.fasta" "${R2R_OUTPUT_DIR}"
EXITCODE="$?"
if [ "$EXITCODE" != 0 ];
then
echo_timestamp "read2read.py failed"
continue
exit $EXITCODE
fi
OUTPUT_FILE_NAME="${GENE}-s${PERCENTAGE}-c${COVERAGE}${UNIQUIFY}.layout"
cat "${R2R_OUTPUT_DIR}/${GENE}_pairwise.txt" \
"${OUTPUT_DIRECTORY}/${GENE}.nodeclass" > \
"${OUTPUT_DIRECTORY}/${OUTPUT_FILE_NAME}"
echo "Finished running job!"
done
BASE_DIRECTORY_NAME=$(basename ${OUTPUT_DIRECTORY})
zip -j ${OUTPUT_DIRECTORY}/${BASE_DIRECTORY_NAME}.zip ${OUTPUT_DIRECTORY}/*.layout