Skip to content

Instantly share code, notes, and snippets.

@tkrahn
Created April 13, 2022 16:11
Show Gist options
  • Save tkrahn/c1623884b48a95913882eb8c836e2642 to your computer and use it in GitHub Desktop.
Save tkrahn/c1623884b48a95913882eb8c836e2642 to your computer and use it in GitHub Desktop.
Simple way to create FastQ files from a paired end BAM file
#!/bin/bash
clear
echo "Recovering FASTQ reads from a BAM file"
YSEQ_ID=${PWD##*/}
NUM_THREADS=$(getconf _NPROCESSORS_ONLN)
echo "We can use ${NUM_THREADS} threads."
mkdir fastq
READS_1="fastq/${YSEQ_ID}_R1.fastq"
READS_2="fastq/${YSEQ_ID}_R2.fastq"
READS_U="fastq/${YSEQ_ID}_unpaired.fastq"
READS_0="fastq/${YSEQ_ID}_other.fastq"
BAMFILE="${YSEQ_ID}_original.bam"
# Sort the BAM file by read names (-n)
samtools sort -@ ${NUM_THREADS} -n -T ${YSEQ_ID}_tmp -o ${YSEQ_ID}_name_sorted.bam ${BAMFILE}
# Split in different fastq.gz files
samtools fastq -@ ${NUM_THREADS} -1 ${READS_1}.gz -2 ${READS_2}.gz -s ${READS_U}.gz -0 ${READS_0}.gz ${YSEQ_ID}_name_sorted.bam
# Old bbmap version
# samtools bamshuf -@ ${NUM_THREADS} -Ou ${BAMFILE} ${YSEQ_ID}_shuf | \
# samtools bam2fq -@ ${NUM_THREADS} - | \
# bash /genomes/0/bbmap/repair.sh -Xmx64g in=stdin.fq out=${READS_1}.gz out2=${READS_2}.gz outsingle=${READS_U}.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment