samtools-split(1)

πŸ’… samtools-split(1) β€” bestie edition πŸŽ€

ok so this is a page from the Bioinformatics tools manual set, and the star of the show is samtools split. buckle up, it's actually such a clean little tool πŸ¦‹


✨ NAME ✨

samtools split β€” it splits a file by read group. that's it, that's the vibe πŸ’«


🌸 SYNOPSIS 🌸

samtools split [options] merged.sam|merged.bam|merged.cram

πŸ‘‘ DESCRIPTION πŸ‘‘

so the whole thing splits a file by read group (or by a tag you pick), and it produces one or more output files that all match a common prefix β€” and by default that prefix is based on the input filename. cute, right? πŸŽ€

unless you use the -d option, the file gets split according to the @RG tags listed in the header. and listen β€” records with no RG tag, or with an RG tag that's undefined in the header, will make the program exit with an error 😭 unless you use the -u option. no cap.

RG values defined in the header but with no records? they still produce an output file that only contains a header. main character energy tbh πŸ‘‘

if you use the -d TAG option, the file gets split on the value in the given aux tag instead. only string (type Z) and integer (type i in SAM, plus the equivalents in BAM/CRAM) tags are currently supported β€” that's the limit, bestie. and unless you use -u, the program exits with an error if it finds a record without the given tag πŸ’€

⚠️ heads up: splitting on a tag with high cardinality can literally create a huge number of output files. to prevent that whole situation, use the -M option to set a limit on the number of splits made. protecting you from yourself, iconic behavior 🫢

using -d RG behaves in a similar way to the default (i.e. without -d) β€” it opens an output file for each @RG line in the header. but unlike the default, new output files will be opened for any RG tags found in the alignment records irrespective of whether they have a matching header @RG line. wild, I know ✨

the -u option lets you specify the output filename for any records with a missing or unrecognised tag. and this option will always write out a file, even if there are no records. always. periodt πŸ’…

output format defaults to BAM. so for SAM or CRAM, either set the format with --output-fmt or use -f to set the file extension, like -f %*_%#.sam. easy 🌈


πŸ’– OPTIONS πŸ’–

  • -u FILE1 β€” put reads with no tag or an unrecognised tag into FILE1 πŸ™ˆ
  • -h FILE2 β€” use the header from FILE2 when writing the file given in the -u option. this header completely replaces the one from the input file 😳 it must be compatible with the input file header, which means it must have the same number of references listed in the @SQ lines, and the references must be in the same order and have the same lengths. no mixing and matching, bestie.
  • -f STRING β€” output filename format string (see the expansions below) β€” default is "%*_%#.%." ⭐
  • -d TAG β€” split reads by TAG value into distinct files. only the TAG key has to be supplied with the option. the value of the TAG has to be a string (i.e. key:Z:value) or an integer (key:i:value) 🌸

using this option changes the default filename format string to "%*_%!.%", so that tag values appear in the output file names. and yes, you can override that with the -f option πŸ‘‘

  • -p NUMBER β€” pad numeric values in %# and %! format expansions to this many digits using leading zeros. for %!, only integer tag values get padded β€” string tag values are left unchanged, even if the value only includes digits. rude but fair πŸ’€
  • -M, --max-split NUM β€” limit the number of files created by the -d option to NUM (default 100). this prevents accidents where trying to split on a tag with high cardinality could result in the creation of a very large number of output files 😭 once the file limit is reached, any tag values not already seen will be treated as unmatched, and the program will exit with an error unless the -u option is in use.

if desired, the limit can be removed using -M -1 β€” although in practice the number of outputs will still be restricted by system limits on the number of files that can be open at once. so like, you can try 🫢

if splitting by read group, and the read group count in the header is higher than the requested limit, then the limit will be raised to match. it's very accommodating like that ✨

  • -v β€” verbose output. she talks more πŸ“£
  • --no-PG β€” do not add a @PG line to the header of the output file πŸ™…β€β™€οΈ
  • Format string expansions: these are the little magic bits you can drop into your filename format πŸ’«
    • %% β€” %
    • %* β€” basename
    • %# β€” index (of @RG in the header, or count of TAG values seen so far)
    • %! β€” @RG ID or TAG value
    • %. β€” output format filename extension
  • -@, --threads INT β€” number of input/output compression threads to use in addition to the main thread. default is [0] 🧚

⭐ AUTHOR ⭐

written by Martin Pollard from the Sanger Institute. legend behavior, honestly πŸ‘


πŸ¦‹ SEE ALSO πŸ¦‹

samtools(1), samtools-addreplacerg(1)

Samtools website: <http://www.htslib.org/> β€” go poke around, it's giving resources πŸ’œ


and that's the whole page, bestie!! this was samtools-split(1) from samtools-1.24, dated 9 July 2026 β€” screenshot it, cite it, love it πŸ’–πŸŽ€βœ¨

πŸ“¦ where this page comes from