Read data from Sequence Alignment/Map (SAM) file
SAMStruct = samread(File)
[SAMStruct, HeaderStruct]= samread(File)
... = samread(File,'ParameterName',ParameterValue)
SAMStruct = samread(File) reads a SAM-formatted file and returns the data in a MATLAB® array of structures.
[SAMStruct, HeaderStruct]= samread(File) returns the alignment and header data in two separate variables.
... = samread(File,'ParameterName',ParameterValue) accepts one or more comma-separated parameter name/value pairs. Specify ParameterName inside single quotes.
Either of the following:
Controls the reading of the optional tags in addition to the first 11 fields for each alignment in the SAM-formatted file. Choices are true (default) or false.
String specifying the read group ID for which to read alignment records from. Default is to read records from all groups.
Scalar or vector that controls the reading of a single sequence entry or block of sequence entries from a SAM-formatted file containing multiple sequences. Enter a scalar N, to read the Nth entry in the file. Enter a 1-by-2 vector [M1, M2], to read a block of entries starting at the M1 entry and ending at the M2 entry. To read all remaining entries in the file starting at the M1 entry, enter a positive value for M1 and enter Inf for M2.
An N-by-1 array of structures containing sequence alignment and mapping information from a SAM-formatted file, where N is the number of alignment records stored in the SAM-formatted file. Each structure contains the following fields.
Structure containing header information for the SAM-formatted file in the following fields.
* — These structures and their fields appear in the output structure only if they are present in the SAM file. The information in these structures depends on the information present in the SAM file.
Read the header information and the alignment data from the ex1.sam file included with Bioinformatics Toolbox™, and then return the information in two separate variables:
[data header] = samread('ex1.sam');
Read a block of entries, excluding the tags, from the ex1.sam file, and then return the information in an array of structures:
% Read entries 5 through 10 and do not include the tags data = samread('ex1.sam','blockread', [5 10], 'tags', false);
Use the saminfo function to investigate the size and content of a SAM-formatted file before using the samread function to read the file contents into a MATLAB array of structures.
If your SAM-formatted file is too large to read using available memory, try one of the following:
Use the BlockRead parameter with the samread function to read a subset of entries.
Create a BioIndexedFile object from the SAM-formatted file, then access the entries using methods of the BioIndexedFile class.
Use the SAMStruct output argument that samread returns to create a BioMap object, which lets you explore, access, filter, and manipulate all or a subset of the data, before doing subsequent analyses or viewing the data.
 Li, H., Handsaker, B., Wysoker, A., Fennell, T., Ruan, J., Homer, N., Marth, G., Goncalo, A., and Durbin, R. (2009). The Sequence Alignment/Map format and SAMtools. Bioinformatics 25, 16, 2078–2079.