pybedtools.bedtool.BedTool.sort

BedTool.sort(*args, **kwargs)[source]

Wraps bedtools sort.

Note that chromosomes are sorted lexograpically, so chr12 will come before chr9.

Example usage:

>>> a = pybedtools.BedTool('''
... chr9 300 400
... chr1 100 200
... chr1 1 50
... chr12 1 100
... chr9 500 600
... ''', from_string=True)
>>> print(a.sort()) 
chr1    1       50
chr1    100     200
chr12   1       100
chr9    300     400
chr9    500     600

For convenience, the file or stream this BedTool points to is implicitly passed as the -i argument to sortBed

There are two alternatives for supplying a genome. Use g="genome.filename" if you have a genome’s chrom sizes saved as a file. This is the what BEDTools expects when using it from the command line. Alternatively, use the genome="assembly.name" (for example, genome="hg19") to use chrom sizes for that assembly without having to manage a separate file. The genome argument triggers a call pybedtools.chromsizes, so see that method for more details.

Original BEDTools help::

Tool:    bedtools sort (aka sortBed)
Version: v2.31.1
Summary: Sorts a feature file in various and useful ways.

Usage:   bedtools sort [OPTIONS] -i <bed/gff/vcf>

Options: 
        -sizeA                  Sort by feature size in ascending order.
        -sizeD                  Sort by feature size in descending order.
        -chrThenSizeA           Sort by chrom (asc), then feature size (asc).
        -chrThenSizeD           Sort by chrom (asc), then feature size (desc).
        -chrThenScoreA          Sort by chrom (asc), then score (asc).
        -chrThenScoreD          Sort by chrom (asc), then score (desc).
        -g (names.txt)  Sort according to the chromosomes declared in "genome.txt"
        -faidx (names.txt)      Sort according to the chromosomes declared in "names.txt"
        -header Print the header from the A file prior to results.