Sort alignments by leftmost coordinates, by read name when -n or -N are used, by tag contents with -t, or 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.
Note that if the sorted output file is to be indexed with samtools index, the default coordinate sort must be used. Thus the -n, -N, -t and -M options are incompatible with samtools index.
When sorting by minimisier (-M), the sort order 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).
Set the desired compression level for the final output file, ranging from 0 (uncompressed) or 1 (fastest but minimal compression) to 9 (best compression but slowest to write), similarly to gzip(1)'s compression level setting.
If -l is not used, the default compression level will apply.
Set the compression level to 0, for uncompressed output. This is a synonym for -l 0.
Approximately the maximum required memory per thread, specified either in bytes or with a K, M, or G suffix. [768 MiB]
To prevent sort from creating a huge number of temporary files, it enforces a minimum value of 1M for this setting.
Sort by read names (i.e., the QNAME field) 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 to queryname:natural.
Sort by read names (i.e., the QNAME field) using the lexicographical ordering, rather than by chromosomal coordinates. Unlike -n no 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 to queryname:lexicographical.
Sort first by the value in the alignment tag TAG, then by position or name (if also using -n or -N).
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 -K option. 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.
Do not use reverse strand with minimiser sort (only compatible with -M).
Sets the kmer size to be used in the -M option. [20]
Build a minimiser index over FILE. The per-read minimisers produced by -M are 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 -I automatically implies -M.
Specifies the window size for building the minimiser index on the file specified in -I. This defaults to 100. It may be better to set this closer to 50 for short-read data sets (at a higher CPU and memory cost), or for more speed up to 1000 for long-read data sets.
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.
Write the final sorted output to FILE, rather than to standard output.
Write the final output as sam, bam, or cram.
By default, samtools tries to select a format based on the -o filename extension; if output is to standard output or no format can be deduced, bam is selected.
Write temporary files to PREFIX.nnnn.bam, or if the specified PREFIX is an existing directory, to PREFIX/samtools.mmm.mmm.tmp.nnnn.bam, where mmm is unique to this invocation of the sort command.
By default, any temporary files are written alongside the output file, as out.bam.tmp.nnnn.bam, or if output is to standard output, in the current directory as samtools.mmm.mmm.tmp.nnnn.bam.
Set number of sorting and compression threads. By default, operation is single-threaded.
Do not add a @PG line to the header of the output file.
Sorts by template-coordinate, whereby the sort order (@HD SO) is unsorted, the group order (GO) is query, and the sub-sort (SS) is template-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:
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 molecular identifier (MI tag if present).
The read name.
If unpaired, or if R1 has 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.
Written by Heng Li from the Sanger Institute with numerous subsequent modifications.
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.