samtools-sort(1)
π samtools sort π
okay bestie, this is samtools-sort(1) β the manual page for samtools sort, and we are about to get your alignments sorted sorted. literally. π«Ά
πΈ NAME πΈ
samtools sort β sorts SAM/BAM/CRAM files. periodt. β¨
β¨ SYNOPSIS β¨
samtools sort [options] [in.sam|in.bam|in.cram]
π DESCRIPTION π
Sort alignments by leftmost coordinates, by read name when -n or -N are used, by tag contents with -t, or by a minimiser-based collation order with -M. An appropriate @HD SO sort order header tag will be added, or an existing one updated, if necessary β along with the @HD SS sub-sort header tag where appropriate. π«
The sorted output is written to standard output by default, or to the specified file (out.bam) when -o is used. This command will also create temporary files tmpprefix.%d.bam as needed when the entire alignment data cannot fit into memory (as controlled via the -m option). π
Consider using samtools collate instead if you need name collated data without a full lexicographical sort. iykyk.
π¨ note that if the sorted output file is to be indexed withsamtools index, the default coordinate sort must be used. Thus the-n,-Nand-toptions are incompatible withsamtools index. no cap, this matters.
When sorting by minimiser (-M), the sort order for unplaced data is defined by the whole-read minimiser value and the offset into the read that this minimiser was observed. This produces small clusters (contig-like, but unaligned) and helps to improve compression with LZ algorithms. This can be improved by supplying a known reference to build a minimiser index (-I and -w options). π§
π OPTIONS π
-l INTβ set the desired compression level for the final output file, ranging from0(uncompressed) or1(fastest but minimal compression) to9(best compression but slowest to write), similarly togzip(1)'s compression level setting. If-lis not used, the default compression level will apply.-uβ set the compression level to0, for uncompressed output. This is a synonym for-l 0.-m INTβ approximately the maximum required memory per thread, specified either in bytes or with aK,M, orGsuffix. [768 MiB] To prevent sort from creating a huge number of temporary files, it enforces a minimum value of1Mfor this setting.-nβ sort by read names (i.e., theQNAMEfield) using an alpha-numeric ordering, rather than by chromosomal coordinates. The alpha-numeric or βnaturalβ sort order detects runs of digits in the strings and sorts these numerically. Hence"a7b"appears before"a12b". Note this is not suitable where hexadecimal values are in use. Sets the header sub-sort (@HD SS) tag toqueryname:natural.-Nβ sort by read names (i.e., theQNAMEfield) using the lexicographical ordering, rather than by chromosomal coordinates. Unlike-nno detection of numeric components is used, instead relying purely on the ASCII value of each character. Hence"x12"comes before"x7"as"1"is before"7"in ASCII. This is a more appropriate name sort order where all digits in names are already zero-padded and/or hexadecimal values are being used. Sets the header sub-sort (@HD SS) tag toqueryname:lexicographical.-t TAGβ sort first by the value in the alignment tagTAG, then by position or name (if also using-nor-N).-Mβ sort unmapped reads (those in chromosome"*") by their sequence minimiser (Schleimer et al., 2003; Roberts et al., 2004), also reverse complementing as appropriate. This has the effect of collating some similar data together, improving the compressibility of the unmapped sequence. The minimiser kmer size is adjusted using the-Koption. Note data compressed in this manner may need to be name collated prior to conversion back to fastq. Mapped sequences are sorted by chromosome and position. Files with at least one aligned record (being placed at a position on a chromosome) use the sort order"coordinate"with a sub-sort of"coordinate:minhash". Files entirely consisting of unaligned data use sort order"unsorted"with sub-sort"unsorted:minhash".-Rβ do not use reverse strand with minimiser sort (only compatible with-M).-K INTβ sets the kmer size to be used in the-Moption.[20]-I FILEβ build a minimiser index overFILE. The per-read minimisers produced by-Mare no longer sorted by their numeric value, but by the reference coordinate this minimiser was found to come from (if found in the index). This further improves compression due to improved sequence similarity between sequences, albeit with a small CPU cost of building and querying the index. Specifying-Iautomatically implies-M.-w INTβ specifies the window size for building the minimiser index on the file specified in-I. This defaults to100. It may be better to set this closer to50for short-read data sets (at a higher CPU and memory cost), or for more speed up to1000for long-read data sets.-Hβ squashes base homopolymers down to a single base pair before constructing the minimiser. This is useful for instruments where the primary source of error is in the length of homopolymer.-o FILEβ write the final sorted output toFILE, rather than to standard output.-O FORMATβ write the final output assam,bam, orcram. By default, samtools tries to select a format based on the-ofilename extension; if output is to standard output or no format can be deduced,bamis selected.-T PREFIXβ write temporary files toPREFIX.nnnn.bam, or if the specifiedPREFIXis an existing directory, toPREFIX/samtools.mmm.mmm.tmp.nnnn.bam, wheremmmis unique to this invocation of the sort command. By default, any temporary files are written alongside the output file, asout.bam.tmp.nnnn.bam, or if output is to standard output, in the current directory assamtools.mmm.mmm.tmp.nnnn.bam.-@ INTβ set number of sorting and compression threads. By default, operation is single-threaded.--no-PGβ do not add a@PGline to the header of the output file.--template-coordinateβ sorts by template-coordinate, whereby the sort order (@HD SO) isunsorted, the group order (GO) isquery, and the sub-sort (SS) istemplate-coordinate.
π§ Ordering Rules π§
The following rules are used for ordering records.
If option -t is in use, records are first sorted by the value of the given alignment tag, and then by position or name (if using -n or -N). For example, "-t RG" will make read group the primary sort key. The rules for ordering by tag are:
- Records that do not have the tag are sorted before ones that do.
- If the types of the tags are different, they will be sorted so that single character tags (type A) come before array tags (type B), then string tags (types H and Z), then numeric tags (types f and i).
- Numeric tags (types f and i) are compared by value. Note that comparisons of floating-point values are subject to issues of rounding and precision.
- String tags (types H and Z) are compared based on the binary contents of the tag using the C
strcmp(3)function. - Character tags (type A) are compared by binary character value.
- No attempt is made to compare tags of other types β notably type B array values will not be compared.
When the -n or -N option is present, records are sorted by name. Historically samtools has used a βnaturalβ ordering β i.e. sections consisting of digits are compared numerically while all other sections are compared based on their binary representation. This means βa1β will come before βb1β and βa9β will come before βa10β. However this alpha-numeric sort can be confused by runs of hexadecimal digits. The newer -N option adds a simpler lexicographical based name collation which does not attempt any numeric comparisons and may be more appropriate for some data sets. Note care must be taken when using samtools merge to ensure all files are using the same collation order. Records with the same name will be ordered according to the values of the READ1 and READ2 flags (see samtools flags). When that flag is also equal, ties are resolved with primary alignments first, then SUPPLEMENTARY, SECONDARY, and finally SUPPLEMENTARY plus SECONDARY. Any remaining ties are reported in the same order as the input data. π
When the --template-coordinate option is in use, the reads are sorted by:
- The earlier unclipped 5' coordinate of the template.
- The higher unclipped 5' coordinate of the template.
- The library (from the read group).
- The cellular barcode (
CBtag, if present). - The molecular identifier (
MItag if present). - The read name.
- If unpaired, or if
R1has the lower coordinates of the pair.
When none of the above options are in use, reads are sorted by reference (according to the order of the @SQ header records), then by position in the reference, and then by the REVERSE flag.
π Note π
Historically samtools sort also accepted a less flexible way of specifying the final and temporary output filenames:
samtools sort [-f] [-o] in.bam out.prefix
This has now been removed. The previous out.prefix argument (and -f option, if any) should be changed to an appropriate combination of -T PREFIX and -o FILE. The previous -o option should be removed, as output defaults to standard output. π
π AUTHOR π
Written by Heng Li from the Sanger Institute with numerous subsequent modifications.
π SEE ALSO π
Samtools website: <http://www.htslib.org/>
mwah π that's the whole page, bestie β samtools-1.24, dated 9 July 2026, slay. π
π¦ where this page comes from
- epel 10: samtools 1.24-2.el10_3 Β· MIT
- nixpkgs unstable: samtools 1.24
- Copyright (C) 2008-2011, 2013-2020, 2022-2024 Genome Research Ltd
- copyright (C) 2010, 2011 Broad Institute
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms