Question: How to keep the header when I'm extracting a specific region from a bam file
1
gravatar for vasilislenis
4.3 years ago by
vasilislenis100
United Kingdom
vasilislenis100 wrote:

Hello, 

 

I have used bowtie2 to map some newly sequenced reads to the sheep reference genome.

After that with samtools I converted the sam file to bam file. I'm interested for the reads that are aligned in the mitochondria, so I extracted the particular region from the bam fie. So far everything seems ok, except the fact that the new bam file has "invalid BAM binary header". How can I keep the header to the new bam file?

Thank you very much in advance,

Vasilis.

 

 

 

 

ADD COMMENTlink modified 4.3 years ago by Pierre Lindenbaum117k • written 4.3 years ago by vasilislenis100
1

Can you show the commands used ? How did you extract the region of interest ?

ADD REPLYlink written 4.3 years ago by geek_y9.3k

I used:

samtools view -h B2_ACAGTG_L001_PE_bwt.sorted.bam chrM > B2_ACAGTG_L001_PE_bwt.sorted.chrM.sam

and after that I converted again into bam.

samtools view -bS B2_ACAGTG_L001_PE_bwt.sorted.chrM.sam > B2_ACAGTG_L001_PE_bwt.sorted.chrM.bam

ADD REPLYlink written 4.3 years ago by vasilislenis100
2

Just do

samtools view -b B2_ACAGTG_L001_PE_bwt.sorted.bam chrM > B2_ACAGTG_L001_PE_bwt.sorted.chrM.bam
ADD REPLYlink modified 4.3 years ago • written 4.3 years ago by geek_y9.3k

Yes, it works!

Thank you very much

ADD REPLYlink written 4.3 years ago by vasilislenis100
1
gravatar for Pierre Lindenbaum
4.3 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum117k wrote:

try to generate a BAM file, not a SAM file. for SAM, by default samtools-view ignores the header.

samtools view  -b -o new.bam old.bam MT
ADD COMMENTlink written 4.3 years ago by Pierre Lindenbaum117k

Do you mean at the mapping stage to produce directly a bam file, and after that whatever i want to extract it will keep the header?

ADD REPLYlink written 4.3 years ago by vasilislenis100
2

using option '-b' for binary output always implies writing the header at the output, so Pierre's answer is all you need.

ADD REPLYlink written 4.3 years ago by Jorge Amigo11k

Thank you very much for the explanation, it works!

 

ADD REPLYlink written 4.3 years ago by vasilislenis100
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1303 users visited in the last hour