bam文件按照比对到参考基因组正负链拆分

针对双端reads

/TJPROJ6/RNA_SH/personal_dir/zhangxin/jiaoben/get_forward_reverse_from_bam_for_paired.sh <in.bam> <prefix>

####代码
#!/usr/bin/bash

if [ ! $# -eq 2 ];then
        echo
        echo "Split paired-end reads according to the positive and negative strands based on their alignment to the reference genome"
        echo
        echo "Please note the input format of the file: for example ...."
        echo
        echo "get_forward_reverse_from_bam_for_paired.sh <in.bam> <prefix>"
        echo
        exit 1
fi

#对双端reads 按正负链拆分
###### forward strand
samtools view -b -f 128 -F 16 $1 > $1.fwd1.bam
samtools view -b -f 80 $1 > $1.fwd2.bam
samtools merge -f $2_forward.bam $1.fwd1.bam $1.fwd2.bam
samtools index $2_forward.bam

###### reverse strand
samtools view -b -f 144 $1 > $1.rev1.bam
samtools view -b -f 64 -F 16 $1 > $1.rev2.bam
samtools merge -f $2_reverse.bam $1.rev1.bam $1.rev2.bam
samtools index $2_reverse.bam

针对单端数据

/TJPROJ6/RNA_SH/personal_dir/zhangxin/jiaoben/get_forward_reverse_from_bam_for_single.sh <in.bam> <prefix>

####代码
#!/usr/bin/bash

if [ ! $# -eq 2 ];then
        echo
        echo "Split single reads according to the positive and negative strands based on their alignment to the reference genome"
        echo
        echo "Please note the input format of the file: for example ...."
        echo
        echo "get_forward_reverse_from_bam_for_single.sh <in.bam> <prefix>"
        echo
        exit 1
fi
###### forward strand
samtools view -bF 20 $1 > $2_forward.bam
samtools index $2_forward.bam
###### reverse strand
samtools view -bf 16 $1 > $2_reverse.bam
samtools index $2_reverse.bam