forked from albertwcheng/albert-bioinformatics-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbam2Bed.sh
executable file
·31 lines (23 loc) · 1.06 KB
/
bam2Bed.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
echo please use bamtools convert --format bed -in <bamfile> -out <bedfile> for conversion instead
exit
if [[ $# != 2 ]]; then
echo $0 bamfile bedfile
exit
fi
:<<'COMMENT'
1 QNAME String [!-?A-~]f1,255g Query template NAME2 FLAG Int [0,216-1] bitwise FLAG3 RNAME String \*|[!-()+-<>-~][!-~]* Reference sequence NAME4 POS Int [0,229-1] 1-based leftmost mapping POSition5 MAPQ Int [0,28-1] MAPping Quality6 CIGAR String \*|([0-9]+[MIDNSHPX=])+ CIGAR string7 RNEXT String \*|=|[!-()+-<>-~][!-~]* Ref. name of the mate/next fragment8 PNEXT Int [0,229-1] Position of the mate/next fragment9 TLEN Int [-229+1,229-1] observed Template LENgth10 SEQ String \*|[A-Za-z=.]+ fragment SEQuence11 QUAL String [!-~]+ ASCII of Phred-scaled base QUALity+33
COMMENT
bamfile=$1
bedfile=$2
samtools view $bamfile | awk '($1!~/\@/ && $1!~/\#/){chrom=3; pos=$4; samFlag=$2; if(!( and(samFlag,0x4))){ if(and(samFlag,0x10)){strand="-"}else{strand="+";}start0=pos-1; seq=$10; lseq=length(seq); end1=start0+lseq; printf("%s\t%d\t%d\t%s\n",chrom,start0,end1,strand); }}' > bedfile