Created
February 14, 2018 14:15
-
-
Save tkrahn/97688343c5a1389e62b15cf0de6c227e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Extracting paired fastq files directly from a BAM file | |
# https://gist.github.com/darencard/72ddd9e6c08aaff5ff64ca512a04a6dd | |
# This script requires a lot of disk space! (about 7 x BAM file size) | |
YSEQID="99999" | |
original_bam="${YSEQID}.bam" | |
threads=80 | |
echo "Splitting BAM file..." | |
samtools view -b -f 1 -F 12 ${original_bam} > lib_002_map_map.bam & | |
# R1 unmapped, R2 mapped | |
samtools view -b -f 4 -F 264 ${original_bam} > lib_002_unmap_map.bam & | |
# R1 mapped, R2 unmapped | |
samtools view -b -f 8 -F 260 ${original_bam} > lib_002_map_unmap.bam & | |
# R1 & R2 unmapped | |
samtools view -b -f 12 -F 256 ${original_bam} > lib_002_unmap_unmap.bam & | |
wait | |
echo "Merging unmapped BAM files..." | |
# Merge the unmapped reads | |
samtools merge -@ ${threads} lib_002_unmapped.bam lib_002_unmap_map.bam lib_002_map_unmap.bam lib_002_unmap_unmap.bam | |
#rm -f lib_002_unmap_map.bam | |
#rm -f lib_002_map_unmap.bam | |
#rm -f lib_002_unmap_unmap.bam | |
# Sort BAM files by read name | |
echo "Sorting mapped BAM files by read name..." | |
samtools sort -@ ${threads} -n -o lib_002_mapped.sort.bam lib_002_map_map.bam | |
echo "Sorting unmapped BAM files by Read name..." | |
samtools sort -@ ${threads} -n -o lib_002_unmapped.sort.bam lib_002_unmapped.bam | |
#rm -f lib_002_map_map.bam | |
#rm -f lib_002_unmapped.bam | |
echo "Extracting fastq files from sorted BAM files..." | |
bamToFastq -i lib_002_mapped.sort.bam -fq lib_002_mapped.1.fastq -fq2 lib_002_mapped.2.fastq & | |
bamToFastq -i lib_002_unmapped.sort.bam -fq lib_002_unmapped.1.fastq -fq2 lib_002_unmapped.2.fastq & | |
wait | |
#rm -f lib_002_mapped.sort.bam | |
#rm -f lib_002_unmapped.sort.bam | |
echo "Merging and compressing of the paired end read fastq files..." | |
cat lib_002_mapped.1.fastq lib_002_unmapped.1.fastq | pigz - >> ../fastq/${YSEQID}_R1.fastq.gz & | |
cat lib_002_mapped.2.fastq lib_002_unmapped.2.fastq | pigz - >> ../fastq/${YSEQID}_R2.fastq.gz & | |
wait | |
echo "Conversion completed. Deleting unused files..." | |
#rm -f lib_002_mapped.1.fastq lib_002_unmapped.1.fastq lib_002_mapped.2.fastq lib_002_unmapped.2.fastq | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment