This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Converting Bam To Fastq

Any suggestions on good programs or scripts to convert a BAM file back to a fastq? I have found some scripts but wanted to ask for advice before I go too far down the wrong path.

next-gen-sequencing fastq

Do you know the rules for identifying mate pairs? I used bowtie to align some paired end reads into a sam format file and was able to use various picard methods to process it. I made a file with duplicates removed and SamToFastq throws an exception:

Exception in thread "main" net.sf.picard.PicardException: Illegal mate state: HWUSI-EAS614_1:1:12:4379:1523:0:2:1
at net.sf.picard.sam.SamToFastq.assertPairedMates(SamToFastq.java:231)
at net.sf.picard.sam.SamToFastq.doPaired(SamToFastq.java:140)
at net.sf.picard.sam.SamToFastq.doWork(SamToFastq.java:95)
at net.sf.picard.cmdline.CommandLineProgram.instanceMain(CommandLineProgram.java:150)
at net.sf.picard.sam.SamToFastq.main(SamToFastq.java:87)

I assume because the read ID is not HWUSI-EAS614_1:1:12:4379:1523/2

Have you any had any problems like this?

Thanks

Mike

I have not. Maybe you should ask this as an independent question to have more people view and potentially offer assistance.

Mike, Did you find a way around it? Im facing the same problem.

Thanks, Teja

Teja, please ask your problem as a separate question for detailed response.

5 answers

Use SamToFastq

UPDATE 2023: use samtools collate piped into samtools fastq

hu ? I wonder why someone flagged this as negative ???!! :-/

I moved this comment here as it's adds to Pierre's answer and is not a unique answer of its own.

There is a bam2fastx utility bundled with tophat:

bam2fastx [--fasta|-a|--fastq|-q] <in.bam>

Works really really well for me:

bam2fastx --fastq -M -o S1_mapped.fastq -N S1.sort.grp.md.bam

Here it adds the /1 and /2 suffix for paired reads and doesn't complain if mate is missing (some of my reads are single ended and don't have a mate). Bam2Fastq would complain about missing mates and wouldn't export single ended reads unless extra precautions were taken.

There is also one in the Hydra package. It's called bamToFastq.

Here is another tool that saves the transition into sam format and converts bam directly into fastq: bam2fastq

Bedtools - for converting bam to fastq http://bedtools.readthedocs.io/en/latest/content/tools/bamtofastq.html

Log in to answer this question.