samtools reheader [-iP] [-c CMD | in.header.sam ] in.bam
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.
By default this command outputs the BAM or CRAM file to standard output (stdout), but for CRAM format files it has the option to perform an in-place edit, both reading and writing to the same file. No validity checking is performed on the header, nor that it is suitable to use with the sequence data itself.
Do not add a @PG line to the header of the output file.
Perform the header edit in-place, if possible. This only works on CRAM files and only if there is sufficient room to store the new header. The amount of space available will differ for each CRAM file.
Allow the header from in.bam to be processed by external CMD and read back the result. When used in this manner, the external header file in.header.sam has to be omitted.
CMD must take the original header through stdin in SAM format and output the modified header to stdout. CMD is passed to the system's command shell. Care should be taken to ensure the command is quoted correctly to avoid unwanted shell expansions (for example of $ variables).
CMD must return an exit status of zero.
samtools reheader -c 'grep -v ^@CO' in.bam
samtools reheader -c 'perl -pe "s/^(@SQ.*)(\tSN:)(\d+|X|Y|MT)(\s|\$)/\$1Chr\$2\$3/"' in.bam
samtools reheader -c 'perl -pe "s/^(@SQ.*)(\tSN:)Chr/\$1\$2/"' in.bam
Written by Heng Li with modifications by James Bonfield and Valeriu Ohan, all from the Sanger Institute.
Samtools website: <http://www.htslib.org/>
Copyright © 2023 Genome Research Limited (reg no. 2742969) is a charity registered in England with number 1021457. Terms and conditions.