Tools‎ > ‎SAMtools‎ > ‎

Converting BAM to fastq


samtools bam2fq SAMPLE.bam > SAMPLE.fastq

paired-end reads:   '/1' or '/2' is added to the end of read names

How to split a single .fastq file of paired-end reads into two separated files?
# extracting reads ending with '/1' or '/2'
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


converting a SAMPLE.bam file into paired end SAMPLE_r1.fastq and SAMPLE_r2.fastq files
java -Xmx2g -jar Picard/SamToFastq.jar I=SAMPLE.bam F=SAMPLE_r1.fastq F2=SAMPLE_r2.fastq
 F2   to get two files for paired-end reads (R1 and R2)
 -Xmx2g   allows a maximum use of 2GB memory for the JVM



bedtools bamtofastq -i input.bam -fq output.fastq

paired-end reads:
samtools sort -n input.bam -o input_sorted.bam   # sort reads by identifier-name (-n)
bedtools bamtofastq -i input_sorted.bam -fq output_r1.fastq -fq2 output_r2.fastq