BAM文件转换成fastq

1、使用samtools

## 将BAM文件按照read name进行排序

samtools sort -n ${SAMPLE}.bam -@ 20 -o ${SAMPLE}_sorted.bam

## 将排序以后BAM转为fastq

samtools fastq -@ 20 ${SAMPLE}_sorted.bam -1 ${SAMPLE}_R1.fastq.gz -2 ${SAMPLE}_R2.fastq.gz -0 /dev/null -s /dev/null -n

rm ${SAMPLE}_sorted.bam

此外,也可以用samtools里面的bam2fq命令

samtools bam2fq ${SAMPLE}_sorted.bam > ${SAMPLE}.fastq            ## paired-end reads: '/1' or '/2' is added to the end of read names

cat ${SAMPLE}.fastq  | grep '^@.*/1$' -A 3 --no-group-separator > ${SAMPLE}_R1.fastq

cat ${SAMPLE}.fastq  | grep '^@.*/2$' -A 3 --no-group-separator > ${SAMPLE}_R2.fastq

gzip ${SAMPLE}_R1.fastq

gzip ${SAMPLE}_R2.fastq

2、使用bedtools

bedtools bamtofastq -i ${SAMPLE}_sorted.bam -fq ${SAMPLE}_R1.fq.gz -fq2 ${SAMPLE}_R2.fq.gz

3、使用picard

java -Xmx2g -jar Picard/SamToFastq.jar I=${SAMPLE}_sorted.bam F=${SAMPLE}_R1.fastq F2=${SAMPLE}_R2.fastq

Converting BAM to fastq

bam文件转为fastq - 生物信息文件夹

你可能感兴趣的:(BAM文件转换成fastq)