-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathextractOriginalBam2RegionalBam2.sh
executable file
·66 lines (48 loc) · 1.33 KB
/
extractOriginalBam2RegionalBam2.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
#########################################################################
# File Name: extractOriginalBam2RegionalBam2.sh
# Author: C.J. Liu
# Mail: samliu@hust.edu.cn
# Created Time: Tue 06 Dec 2016 03:34:19 PM CST
#########################################################################
#!/bin/bash
index='/data/hg38/genomeBuild/hg38.fasta'
tumorDir='/WXS_RAW/tumor'
[ $# -eq 0 ] && echo "Error: Input regional file" && exit 1
region=$1
[ ! -f "$region" ] && echo "Error: $region does not exist or not regional file" && exit 1
allBams=(`find $tumorDir -name "*bam" -type f `)
outputDir=`readlink -f $region`
outputDir=`dirname $outputDir`
extractedBams=(`ls $outputDir`)
unExtractedBams=()
for bam in ${allBams[@]}
do
name=${bam%%.*}
bname=`basename $name`
[[ "${extractedBams[@]}" =~ "${bname}" ]] || unExtractedBams+=($bam)
done
echo ${#unExtractedBams[@]}
# Make fifo file.
tmp_fifo='/tmp/$$.fifo'
mkfifo $tmp_fifo
exec 6<>$tmp_fifo
rm -rf $tmp_fifo
for (( i=0; i<30; i++ ))
do
echo ""
done >&6
for bam in "${unExtractedBams[@]}"
do
read -u6
{
name=`basename $bam`
name=${name%.bam}.extracted.bam
cmd="samtools view -b -L $region $bam -o ${outputDir}/${name}"
echo "Notice - Running the command : $cmd"
eval $cmd
echo "Success - $bam was extracted to ${outputDir}/${name}!"
echo "" >&6
}&
done
wait
exec 6>&-