forked from Danko-Lab/RunOnBamToBigWig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunOnBamToBigWig.bsh
474 lines (413 loc) · 17.7 KB
/
RunOnBamToBigWig.bsh
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/bash
#
while test $# -gt 0; do
case "$1" in
-h|--help)
echo ""
echo "Takes bam files from Run-On sequencing data as input and writes"
echo "bigWig files as output to the user-assigned output-dir."
echo ""
echo "Requirements in current working directory:"
echo "samtools, bedtools, and bedGraphToBigWig."
echo ""
echo "bash RunOnBamToBigWig.bsh [options]"
echo ""
echo "options:"
echo ""
echo "To get help:"
echo "-h, --help Show this brief help menu."
echo ""
echo "Required options:"
echo "-SE, --SEQ=SE Bam file from Single-end sequencing."
echo "-PE, --SEQ=PE Bam file from Paired-end sequencing."
echo "-c, --chrom-info=PATH Location of the chromInfo table."
echo ""
echo "I/O options:"
echo "-I, --bam=PREFIX.bam Input bam file. If not specified, will take"
echo " *.bam in the current working directory as input"
echo "-T, --tmp=PATH Path to a temporary storage directory."
echo "-O, --output-dir=DIR Specify a directory to store output in."
echo ""
echo "Required options for SE"
echo "-G, --SE_READ=RNA_5prime Single-end sequencing from 5' end of"
echo " nascent RNA, like GRO-seq."
echo "-P, --SE_READ=RNA_3prime Single-end sequencing from 3' end of"
echo " nascent RNA, like PRO-seq."
echo ""
echo "Options for PE"
echo "--RNA5=R1_5prime Specify the location of the 5' end of RNA"
echo " [default: R1_5prime]."
echo "--RNA3=R2_5prime Specify the location of the 3' end of RNA"
echo " [default: R2_5prime]."
echo " Available options: R1_5prime: the 5' end of R1 reads"
echo " R2_5prime: the 5' end of R2 reads"
echo "-5, --map5=TRUE Report the 5' end of RNA [default on, --map5=TRUE]."
echo "-3, --map5=FALSE Report the 3' end of RNA,"
echo " only available for PE [default off, --map5=TRUE]."
echo "-s, --opposite-strand=TRUE"
echo " Enable this option if the RNA are at the different strand"
echo " as the reads set at RNA5 [default: disable]."
echo ""
echo "Optional operations:"
echo "--thread=1 Number of threads can be used [default: 1]"
echo ""
exit 0
;;
-SE)
export SEQ="SE"
shift
;;
-PE)
export SEQ="PE"
shift
;;
-c)
shift
if test $# -gt 0; then
export CHINFO=$1
else
echo "no chromInfo specified"
exit 1
fi
shift
;;
--chrom-info*)
export CHINFO=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-I)
shift
if test $# -gt 0; then
export BAM_INPUT=$1
else
echo "no input bam file specified."
exit 1
fi
shift
;;
--bam*)
export BAM_INPUT=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-T)
shift
if test $# -gt 0; then
export TMPDIR=$1
else
echo "no temp folder specified."
exit 1
fi
shift
;;
--tmp*)
export TMPDIR=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-O)
shift
if test $# -gt 0; then
export OUTPUT=$1
else
echo "no output dir specified."
exit 1
fi
shift
;;
--output-dir*)
export OUTPUT=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
--thread*)
export thread=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
--RNA5*) # report location of the 5 prime end of RNA
# acce
export RNA5=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
--RNA3*) # report location of the 5 prime end of RNA
# acce
export RNA3=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-5)
export MAP5="TRUE"
shift
;;
--map5*)
export MAP5=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-3)
export MAP5="FALSE"
shift
;;
-s)
export OPP="TRUE"
shift
;;
--opposite-strand*)
export OPP=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-G)
export SE_OUTPUT="G"
export RNA5="R1_5prime"
export OPP="FALSE"
#export MAP5="TRUE"
export SE_READ="RNA_5prime"
shift
;;
-P)
export SE_OUTPUT="P"
export RNA3="R1_5prime"
export OPP="TRUE"
#export MAP5="TRUE"
export SE_READ="RNA_3prime"
shift
;;
--SE_READ*)
export SE_READ=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
*)
break
;;
esac
done
## CHECK ARGUMENTS.
if [ -z "$CHINFO" ]; then
echo "--chrom-info is required."
echo " use bash RunOnBamToBigWig.bsh --help."
exit 1
fi
if [ -z "$SEQ" ]; then
echo "Please specify the input data is SE or PE."
echo " use bash RunOnBamToBigWig.bsh --help."
exit 1
fi
## INPUT & Parameters
if [ -z "$BAM_INPUT" ]; then
echo "No input files specified. Using *.bam"
BAM_INPUT=`ls *.bam`
fi
if [[ "$SEQ" == "PE" ]] ; then
# PE
if [ -z "$SE_OUTPUT" ]; then
:
else
echo "-G and -P can only be used with -SE."
echo " use bash RunOnBamToBigWig.bsh --help."
exit 1
fi
# SE
elif [[ "$SEQ" == "SE" ]] ; then
if [[ "$SE_READ" == "RNA_5prime" ]] ; then
SE_OUTPUT="G"
RNA5="R1_5prime"
OPP="FALSE"
#MAP5="TRUE"
elif [[ "$SE_READ" == "RNA_3prime" ]] ; then
SE_OUTPUT="P"
RNA3="R1_5prime"
OPP="FALSE"
#MAP5="TRUE"
fi
if [ -z "$SE_OUTPUT" ] ; then
echo "Please specify output format for SE [-G or -P]"
exit 1
fi
else
echo "Please specify the input data is SE or PE."
echo " use bash RunOnBamToBigWig.bsh --help."
exit 1
fi
# Check input file number
if [[ ${#BAM_INPUT[@]} == 0 ]]; then # if the length of array is 0
echo "#####################################"
echo " No files is in the correct format."
echo "#####################################"
exit 1
fi
if [ -z "$OUTPUT" ]; then
now=$(date +"%m_%d_%Y")
OUTPUT=./My_output-${now}
echo No output path specified. Using ${OUTPUT}
fi
if [ ! -d $OUTPUT ]; then
mkdir $OUTPUT
fi
if [ -z "$TMPDIR" ]; then
TMPDIR="./"
fi
if [ ! -d $TMPDIR ]; then
mkdir $TMPDIR
fi
# bash generate random 32 character alphanumeric string (upper and lowercase).
tmp=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1`
TMPDIR=$TMPDIR/$tmp
if [ -z "$thread" ]; then
thread=1
fi
if [[ "$SEQ" == "PE" ]]; then
if [[ -z "$RNA5" && -z "$RNA3" ]]; then
RNA5="R1_5prime"
fi
if [[ "$RNA3" == "R1_5prime" ]]; then
RNA5="R2_5prime"
elif [[ "$RNA3" == "R2_5prime" ]]; then
RNA5="R1_5prime"
fi
if [[ "$RNA5" == "R1_5prime" || "$RNA5" == "R2_5prime" ]] ; then
:
else
echo "--RNA5 and --RNA3 value can only be R1_5prime or R2_5prime."
echo " use bash RunOnBamToBigWig.bsh --help."
exit 1
fi
fi
if [ -z "$OPP" ]; then
OPP="FALSE"
fi
if [ -z "$MAP5" ]; then
MAP5="TRUE"
fi
if [[ "${MAP5}" == "FALSE" && "$SEQ" == "SE" ]] ; then
echo "For single-end (SE), can only report the 5 prime end of reads (--map5=TRUE)"
exit 1
fi
if [ "${MAP5}" == "TRUE" ] ; then
:
elif [ "${MAP5}" == "FALSE" ] ; then
:
else
echo "--map5 value can only be TRUE or FALSE."
echo " use bash RunOnBamToBigWig.bsh --help."
exit 1
fi
## Check all the bioinformatics tools can be called from current working directory.
for tool in samtools bedtools bedGraphToBigWig sort-bed
do command -v ${tool} >/dev/null 2>&1 || { echo >&2 ${tool}" is required. Please make sure you can call the bioinformatics tools from your current working directoryb. Aborting."; exit 1; }
done
exec > >(tee ${OUTPUT}/RunOnBamToBigWig.log)
exec 2>&1
## Print out
echo " "
echo "Processing ..."
echo "SEQ $SEQ"
if [[ "$SEQ" == "SE" ]]; then
echo "SE_OUTPUT $SE_OUTPUT"
echo "SE_READ $SE_READ"
fi
if [[ "$SEQ" == "PE" ]]; then
echo "Location of 5' of RNA $RNA5"
echo "Location of 3' of RNA $RNA3"
fi
echo "Report 5' ends $MAP5"
echo "Report opposite strand $OPP"
echo ""
echo "Input files/ paths:"
echo "chromInfo $CHINFO"
i=1
for name in ${BAM_INPUT[@]}
do
echo "input file $i ${name}"
let "i++"
done
echo "temp folder $TMPDIR"
echo "output-dir $OUTPUT"
echo " "
echo "Optional operations:"
echo "number of threads $thread"
mkdir ${TMPDIR}
#############################################
## Write out the bigWigs.
echo " "
if [[ "$SEQ" == "SE" ]] ; then
for f in ${BAM_INPUT[@]}
do
j=`echo $f | awk -F"/" '{print $NF}' | rev | cut -d \. -f 2- |rev `
echo $j > ${OUTPUT}/${j}.align.log
echo "Sorting $f"
samtools sort -n -@ ${thread} $f > ${TMPDIR}/${j}.sort.bam
# in SE, MAP5 alwasys TRUE
#if [[ "${RNA5}" == "R1_5prime" && "${OPP}" == "FALSE" ]] ; then ## report The 5 prime end of the RNA. #like GRO-seq
if [[ "$SE_OUTPUT" == "G" ]] ; then
bedtools bamtobed -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/kill.warnings| awk 'BEGIN{OFS="\t"} ($5 > 0){print $0}' | \
awk 'BEGIN{OFS="\t"} ($6 == "+") {print $1,$2,$2+1,$4,$5,$6}; ($6 == "-") {print $1,$3-1,$3,$4,$5,$6}' | gzip > ${TMPDIR}/$j.bed.gz
#elif [[ "${RNA3}" == "R1_5prime" && "${OPP}" == "TRUE" ]] ; then
elif [[ "$SE_OUTPUT" == "P" ]] ; then #like PRO-seq
bedtools bamtobed -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/kill.warnings| awk 'BEGIN{OFS="\t"} ($5 > 0){print $0}' | \
awk 'BEGIN{OFS="\t"} ($6 == "+") {print $1,$2,$2+1,$4,$5,"-"}; ($6 == "-") {print $1,$3-1,$3,$4,$5,"+"}' | gzip > ${TMPDIR}/$j.bed.gz
fi
echo 'Number of mappable reads:' >> ${OUTPUT}/${j}.align.log
echo `zcat ${TMPDIR}/$j.bed.gz | grep "" -c` >> ${OUTPUT}/${j}.align.log
## Remove rRNA and reverse the strand (PRO-seq).
zcat ${TMPDIR}/$j.bed.gz | grep "rRNA\|chrM" -v | grep "_" -v | sort-bed - | gzip > ${TMPDIR}/$j.nr.rs.bed.gz
echo 'Number of mappable reads (excluding rRNA):' >> ${OUTPUT}/${j}.align.log
echo `zcat ${TMPDIR}/$j.nr.rs.bed.gz | grep "" -c` >> ${OUTPUT}/${j}.align.log
## Convert to bedGraph ... Can't gzip these, unfortunately.
bedtools genomecov -bg -i ${TMPDIR}/$j.nr.rs.bed.gz -g ${CHINFO} -strand + > ${TMPDIR}/$j\_plus.bedGraph
bedtools genomecov -bg -i ${TMPDIR}/$j.nr.rs.bed.gz -g ${CHINFO} -strand - > ${TMPDIR}/$j\_minus.noinv.bedGraph
## Invert minus strand.
cat ${TMPDIR}/$j\_minus.noinv.bedGraph | awk 'BEGIN{OFS="\t"} {print $1,$2,$3,-1*$4}' > ${TMPDIR}/$j\_minus.bedGraph ## Invert read counts on the minus strand.
## Then to bigWig
echo "Writing bigWigs:"
bedGraphToBigWig ${TMPDIR}/$j\_plus.bedGraph ${CHINFO} ${OUTPUT}/$j\_plus.bw
bedGraphToBigWig ${TMPDIR}/$j\_minus.bedGraph ${CHINFO} ${OUTPUT}/$j\_minus.bw
done
elif [[ "$SEQ" == "PE" ]] ; then
for f in ${BAM_INPUT[@]}
do
j=`echo $f | awk -F"/" '{print $NF}' | rev | cut -d \. -f 2- |rev `
echo $j > ${OUTPUT}/${j}.align.log
echo "Sorting $f"
samtools sort -n -@ ${thread} $f > ${TMPDIR}/${j}.sort.bam
if [ "${RNA5}" == "R1_5prime" ] ; then
if [ "${OPP}" == "FALSE" ] ; then
if [ "${MAP5}" == "TRUE" ] ; then ## report The 5' end of the RNA. Danko lab leChRO-Seq protocol is on the 5' of _R1 readl, same strand of R1 ($9)
bedtools bamtobed -bedpe -mate1 -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/kill.warnings | awk 'BEGIN{OFS="\t"} ($9 == "+") {print $1,$2,$2+1,$7,$8,$9}; ($9 == "-") {print $1,$3-1,$3,$7,$8,$9}' | gzip > ${TMPDIR}/$j.bed.gz
else ## report The 3' end of the RNA. Danko lab leChRO-Seq protocol is on the 5 prime of _R2 read, opposite strand of R2 (R2 strand $10, R1 strand $9)
bedtools bamtobed -bedpe -mate1 -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/kill.warnings | awk 'BEGIN{OFS="\t"} ($10 == "-") {print $1,$6-1,$6,$7,$8,$9}; ($10 == "+") {print $1,$5,$5+1,$7,$8,$9}' | gzip > ${TMPDIR}/$j.bed.gz
fi
elif [ "${OPP}" == "TRUE" ] ; then
if [ "${MAP5}" == "TRUE" ] ; then ## report The 5' end of the RNA.
bedtools bamtobed -bedpe -mate1 -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/kill.warnings | awk 'BEGIN{OFS="\t"} ($9 == "+") {print $1,$2,$2+1,$7,$8,$10}; ($9 == "-") {print $1,$3-1,$3,$7,$8,$10}' | gzip > ${TMPDIR}/$j.bed.gz
else ## report The 3' end of the RNA.
bedtools bamtobed -bedpe -mate1 -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/kill.warnings | awk 'BEGIN{OFS="\t"} ($10 == "-") {print $1,$6-1,$6,$7,$8,$10}; ($10 == "+") {print $1,$5,$5+1,$7,$8,$10}' | gzip > ${TMPDIR}/$j.bed.gz
fi
fi
elif [ "${RNA5}" == "R2_5prime" ] ; then
if [ "${OPP}" == "FALSE" ] ; then
if [ "${MAP5}" == "TRUE" ] ; then #report the 5 prime end of RNA, in Engreitz data is 5 prime end of R2, same strand
bedtools bamtobed -bedpe -mate1 -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/${j}.kill.warnings | awk 'BEGIN{OFS="\t"} ($10 == "+") {print $1,$5,$5+1,$7,$8,$10}; ($10 == "-") {print $1,$6-1,$6,$7,$8,$10}'|gzip > ${TMPDIR}/${j}.bed.gz
else ## report the 3-prime end of the RNA, in Engreitz data is the 5' end of R1 read, but opposite strand
bedtools bamtobed -bedpe -mate1 -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/${j}.kill.warnings | awk 'BEGIN{OFS="\t"} ($9 == "+") {print $1,$2,$2+1,$7,$8,$10}; ($9 == "-") {print $1,$3-1,$3,$7,$8,$10}' |gzip > ${TMPDIR}/${j}.bed.gz
fi
elif [ "${OPP}" == "TRUE" ] ; then
if [ "${MAP5}" == "TRUE" ] ; then #report the 5 prime end of RNA, in Engreitz data is 5 prime end of R2, same strand
bedtools bamtobed -bedpe -mate1 -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/${j}.kill.warnings | awk 'BEGIN{OFS="\t"} ($10 == "+") {print $1,$5,$5+1,$7,$8,$9}; ($10 == "-") {print $1,$6-1,$6,$7,$8,$9}'|gzip > ${TMPDIR}/${j}.bed.gz
else ## report the 3-prime end of the RNA, in Engreitz data is the 5' end of R1 read, but opposite strand
bedtools bamtobed -bedpe -mate1 -i ${TMPDIR}/${j}.sort.bam 2> ${TMPDIR}/${j}.kill.warnings | awk 'BEGIN{OFS="\t"} ($9 == "+") {print $1,$2,$2+1,$7,$8,$9}; ($9 == "-") {print $1,$3-1,$3,$7,$8,$9}' |gzip > ${TMPDIR}/${j}.bed.gz
fi
fi
fi
echo 'Number of mappable reads:' >> ${OUTPUT}/${j}.align.log
echo `zcat ${TMPDIR}/$j.bed.gz | grep "" -c` >> ${OUTPUT}/${j}.align.log
## Remove rRNA and reverse the strand (PRO-seq).
zcat ${TMPDIR}/$j.bed.gz | grep "rRNA\|chrM" -v | grep "_" -v | sort-bed - | gzip > ${TMPDIR}/$j.nr.rs.bed.gz
echo 'Number of mappable reads (excluding rRNA):' >> ${OUTPUT}/${j}.align.log
echo `zcat ${TMPDIR}/$j.nr.rs.bed.gz | grep "" -c` >> ${OUTPUT}/${j}.align.log
## Convert to bedGraph ... Can't gzip these, unfortunately.
bedtools genomecov -bg -i ${TMPDIR}/$j.nr.rs.bed.gz -g ${CHINFO} -strand + > ${TMPDIR}/$j\_plus.bedGraph
bedtools genomecov -bg -i ${TMPDIR}/$j.nr.rs.bed.gz -g ${CHINFO} -strand - > ${TMPDIR}/$j\_minus.noinv.bedGraph
## Invert minus strand.
cat ${TMPDIR}/$j\_minus.noinv.bedGraph | awk 'BEGIN{OFS="\t"} {print $1,$2,$3,-1*$4}' > ${TMPDIR}/$j\_minus.bedGraph ## Invert read counts on the minus strand.
## Then to bigWig
echo "Writing bigWigs:"
bedGraphToBigWig ${TMPDIR}/$j\_plus.bedGraph ${CHINFO} ${OUTPUT}/$j\_plus.bw
bedGraphToBigWig ${TMPDIR}/$j\_minus.bedGraph ${CHINFO} ${OUTPUT}/$j\_minus.bw
done
fi
rm ${TMPDIR} -r