samtools-reheader(1)

πŸŽ€ samtools-reheader β€” bestie's guide to swapping headers πŸŽ€

okay so this is samtools-reheader, a page from the Bioinformatics tools manual set, and honestly? it's kind of a glow-up for your file's header. let's get into it ✨


πŸ’… NAME

samtools reheader β€” replaces the header in the input file. that's it, that's the whole job. iconic behavior honestly.


πŸ¦‹ SYNOPSIS

samtools reheader [-iP] [-c CMD | in.header.sam] in.bam

🌸 DESCRIPTION

so basically you're swapping the header in in.bam with the header from in.header.sam. and the way this is much faster than doing the whole BAM→SAM→BAM conversion thing? obsessed. that round-trip is a whole ordeal and this command just skips it entirely. no cap.

by default this command outputs the BAM or CRAM file to standard output (stdout), k? but for CRAM format files it has the option to do an in-place edit β€” like, reading and writing to the same file. main character energy fr.

⚠️ heads up bestie, there is no validity checking performed on the header. none. zero. it doesn't check that the header is suitable to use with the sequence data itself either. so like… the vibes are trusting. you gotta be careful out there πŸ™ˆ

✨ OPTIONS ✨

  • -P, --no-PG β€” do not add a @PG line to the header of the output file. that's it. if you don't want the extra line, this is your girl.
  • -i, --in-place β€” perform the header edit in-place, if possible. 🫢 and "if possible" is doing a lot of work here: 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. so it's giving ✨situational✨.
  • -c, --command CMD β€” allow the header from in.bam to be processed by an external CMD and read back the result. when you use it this way, the external header file in.header.sam has to be omitted. can't have both, bestie.

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) β€” this is the part where things get messy if you're not careful, so double-check your quotes fr fr.

CMD must return an exit status of zero. that's a requirement, not a suggestion πŸ’€


🌈 EXAMPLES

  • remove comment lines 🧹
samtools reheader -c 'grep -v ^@CO' in.bam
  • add "Chr" prefix to chromosome names β€” note the extra backslashes before dollar signs to prevent unwanted shell variable expansion. literally do not skip those backslashes πŸ‘‘
samtools reheader -c 'perl -pe "s/^(@SQ.*)(\tSN:)(\d+|X|Y|MT)(\s|\$)/\$1Chr\$2\$3/"' in.bam
  • remove "Chr" prefix β€” the reverse glow-down, we love a symmetry moment πŸ’–
samtools reheader -c 'perl -pe "s/^(@SQ.*)(\tSN:)Chr/\$1\$2/"' in.bam

πŸ‘‘ AUTHOR

Written by Heng Li, with modifications by James Bonfield and Valeriu Ohan, all from the Sanger Institute. shoutout the squad 🫢


πŸ”₯ SEE ALSO


⭐ that's the whole page, bestie β€” this is from samtools-1.24, dated 9 July 2026. go forth and reheader responsibly πŸ’…βœ¨

πŸ“¦ where this page comes from