This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Extract Unaligned Sequences From Bam Files Obtainend From Bwa

Hi everyone, I aligned my RNA sequences against 16s and 23s using BWA. How can I extract my unmapped reads (the ones that really matters) into a fastq or fasta file? I want these sequences to run against bacterial genome and get gene counts.

I already tried using

samtools view -f 4 sample.bam > sample.unmapped.sam,

but it seems that the unmapped.sam is missing something because when I try convert it to bam, samtools show me the following error:

$ samtools view -S -b S_2_unmapped.sam > S_2_unmapped.BAM
[samopen] no @SQ lines in the header.
[sam_read1] missing header? Abort!

So, I am wondering if there is a way to get the unaligned files right from the first bam file ( the one containing aligned and unaligned files) and make my fatsq or fasta file to continue the analysis.

Thanks to those available to help me.

P.S I am a biologist.

bam fasta fastq gene counts

2 answers

When a SAM file is created for viewing (with the view command) then it is meant for visual inspection and as such it is missing for example the header information.

To generate the header you need to pass the -h flag. But the right solution is to keep the output as BAM file in the first place by giving it the -b flag on the first view

samtools view -b -f 4 sample.bam > sample.unmapped.bam

But can I use the BAM file as the imput for BWA alignment. I want to align these sequences against a bacterial genome for gene count using BWA. I have only seen the fa and fq files as the imput.

Thanks

make sure to add these as comments to the original answer and not as a new answer to you post

Log in to answer this question.