This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Fast BAM header editing

I would like to change some information in the BAM header (specifically, chromosome size information). Is there a smarter/faster way to do this without extracting to an intermediate SAM file and remaking the BAM file from the edited SAM file?

bam

Thanks all. Will just reheader the old way.

3 answers

You can pipe/stream the process without explicitly writing a temp file to disk.

For better solutions, unfortunately BAM file is not defined to have a fixed size header block at the beginning. Otherwise, we would be able to swap the header directly without touching the reads.

In the context of java/htsjdk you could create a custom SamReaderFactory that would change the dictionary by changing CUSTOM_READER_FACTORY https://www.javadoc.io/doc/com.github.samtools/htsjdk/2.5.1/htsjdk/samtools/Defaults.html#CUSTOM_READER_FACTORY with -Dsamjdk.custom_reader . I can elaborate if needed.

See samtools reheader

http://www.htslib.org/doc/samtools-reheader.html

quote from the docs " Replace the header in in.bam with the header in in.header.sam. This command is much faster than replacing the header with a BAM->SAM->BAM conversion. "

Log in to answer this question.