Main Content

bamsort

Sort BAM files

Since R2019b

Description

example

sortedFile = bamsort(inFile) sorts a BAM file inFile and returns the name of the sorted BAM file sortedFile. The function sorts the alignment records by the reference sequence name first, and then by position within the reference.

example

bamsort(inFile,outFile) sorts inFile and produces a sorted BAM file named outFile.

Examples

collapse all

Sort a sample BAM file. The sorted file has the same base name as the input file, but with the extension ".sorted.bam". By default, the sorted file is saved in the current directory.

sortedFile = bamsort("ex1.bam")
sortedFile = 
"ex1.sorted.bam"

You can change the name of output file by specifying it as the second input.

bamsort("ex1.bam","sortedEx1.bam")
ans = 
"sortedEx1.bam"

You can also save the output file to an existing directory by providing the file path information.

mkdir("./OutputEx1BAM");
bamsort("ex1.bam","./OutputEx1BAM/sortedEx1.bam")
ans = 
"./OutputEx1BAM/sortedEx1.bam"

Input Arguments

collapse all

Name of the input BAM file to sort, specified as a string or character vector. You can specify a file name or a path and file name. The file name must have the extension .bam.

Example: "./InputData/ex1.bam"

Data Types: char | string

Name of the output BAM file, specified as a string or character vector. You can specify a file name or a path and file name. The file name must have the extension .bam. The file is saved in the current directory by default unless you specify the path information. If you specify the file path, the listed directories must exist before you run the function.

Example: "./OutputData/ex1Sorted.bam"

Data Types: char | string

Output Arguments

collapse all

Name of the output BAM file, returned as a string. sortedFile has the same base name as inFile but with the extension .sorted.bam. The file is saved in the current directory by default.

Version History

Introduced in R2019b