LibStatGen: BAM: Difference between revisions

From Genome Analysis Wiki
Jump to navigationJump to search
No edit summary
Ppwhite (talk | contribs)
 
(67 intermediate revisions by one other user not shown)
Line 1: Line 1:
[[Category:C++]]
[[Category:libStatGen]]
[[Category:libStatGen BAM]]
= SAM/BAM File=
= SAM/BAM File=


== Reading/Writing SAM/BAM Files ==
See the github history: https://github.com/statgen/libStatGen/commits/master/bam for a list of the most recent updates to the BAM classes.
The SamFile class allows a user to easily read/write a SAM/BAM file.
 
This methods found in this class are:
[[BAM Review Action Items|Old BAM Review Action Items]]
{| class="wikitable" style="width:100%" border="1"
 
|+ style="font-size:150%"|'''SamFile Class Methods'''
== Read & Write BAM/SAM Library Software ==
! width=""|Method Name
 
! width=""|Description
The software reads the beginning of files opened for reading to determine if it is SAM/BAM.  To determine the format (SAM/BAM) of files open for writing, the software checks the output file's extension.  If the extension is "bam" it writes a BAM file, otherwise it writes a SAM file.
 
The library is found in statgen/lib/bam.
 
=== BAM/SAM Classes ===
{| style="margin: 1em 1em 1em 0; background-color: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;" border="1"
|-style="background: #f2f2f2; text-align: center;"
! Class Name !!  Description
|-
| <code>[[C++ Class: SamFile|SamFile]]</code>
| Class used for reading/writing SAM/BAM files and their headers and records.
|-
| <code>[[C++ Class: SamFileHeader|SamFileHeader]]</code>
| Class used for storing the header. Allows access for getting and setting header values when both reading & writing SAM/BAM files.
|-
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classSamHeaderRecord.html SamHeaderRecord]</code>
| Class used for storing the tag/value pairs within a given header line.
|-
| <code>[[C++ Class: SamRecord|SamRecord]]</code>
| Class used for storing a SAM/BAM Record. Allows access for getting and setting record values when both reading & writing SAM/BAM files.
|-
|-
| bool OpenForRead(const char* filename)
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classSamStatus.html SamStatus]</code>
| Opens the specified file for reading.
| Status value used by the BAM classes for returning the status of the operations.
Determines if it is a BAM/SAM file by reading the beginning of the file.
Returns true if successfully opened reading, false if not.
|-
|-
| bool OpenForWrite(const char * filename)
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classSamValidator.html SamValidator]</code>
| bool: true if successfully opened, false if not.
| Validates a SAM/BAM Record.
Opens as BAM file if the specified filename ends in .bam. Otherwise it is opened as a SAM file.
Returns true if successfully opened for writing, false if not.
|-
|-
| bool ReadHeader(SamFileHeader& header)
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classSamValidationError.html SamValidationError]</code>
| Reads the header section from the file and stores it in the passed in header.
| Validation Error Information for a SamRecord.
Returns true if successfully read, false if not.
|-
|-  
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classSamValidationErrors.html SamValidationErrors]</code>
| bool WriteHeader(const SamFileHeader& header)
| Container for ValidationErrors.
| Writes the specified header into the file.
|-
Returns true if successfully written, false if not.
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classPileup.html Pileup]</code>
|-  
| Template for doing pileups.
| bool ReadRecord(SamFileHeader& header, SamRecord& record)
|-
| Reads the next record from the file and stores it in the passed in record.
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classPileupElement.html PileupElement]</code>
Returns true if successfully read, false if not.
| Base class that can be used for the elements stored in a Pileup.
|-  
|-
| bool WriteRecord(SamFileHeader& header, SamRecord& record)
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classErrorHandler.html ErrorHandler]</code>
| Writes the specified record into the file.
| Class for handling errors based on the error handling type.
Returns true if successfully written, false if not.
|-
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classPosList.html PosList]</code>
| Store refID/position, but does not store values < 0.
|-
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classSamFilter.html SamFilter]</code>
| Class for filtering a SAM/BAM record.
|-
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classSamFlag.html SamFlag]</code>
| Class for getting information from a SAM/BAM flag.
|-
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classSamReferenceInfo.html SamReferenceInfo]</code>
| Class for tracking the reference information mapping between the reference ids and the reference names.
|-
| <code>[http://csg.sph.umich.edu//mktrost/doxygen/current/classSamTags.html SamTags]</code>
| Class for parsing/creating/operating on SAM/BAM record tags.
|}
|}


=== Usage Example ===
== FAQs ==
The following example reads in a sam/bam file and writes it out as a sam/bam file.  The file format of the input sam/bam is determined by the SamFile class based on reading the type from the file.  The file format of the output sam/bam file is determined by the SamFile class based on the extension of the output file.  A ".bam" extension indicates a BAM file.  All other extensions indicate SAM files.
[[SAM/BAM Classes FAQs]]
<pre>
int main(int argc, char ** argv)
{
  if(argc != 3)
  {
      printf("./bam <inputFile> <outputFile.sam/bam>\n");
      exit(-1);
  }
 
 
  SamFile samIn;
     
  samIn.OpenForRead(argv[1]);
 
  SamFile samOut;
 
  samOut.OpenForWrite(argv[2]);


  // Read the sam header.
== Usage Examples ==
  SamFileHeader samHeader;
[[Sam Library Usage Examples]]
  samIn.ReadHeader(samHeader);


  samOut.WriteHeader(samHeader);


  // Read the first sam record.
== Programs ==
  SamRecord samRecord;


  // Keep reading records until it fails.
BamUtil contains a set of programs that uses this library to operate on SAM & BAM files. It includes tools for converting between SAM & BAM and validating the files. See [[BamUtil]] for more information and a description of all the tools.
  int recordCount = 0;
  while (samIn.ReadRecord(samHeader, samRecord) == true)
  {
      recordCount++;
      samOut.WriteRecord(samHeader, samRecord);
  }
  printf("RecordCount = %d\n", recordCount);
}
</pre>

Latest revision as of 10:55, 2 February 2017


SAM/BAM File

See the github history: https://github.com/statgen/libStatGen/commits/master/bam for a list of the most recent updates to the BAM classes.

Old BAM Review Action Items

Read & Write BAM/SAM Library Software

The software reads the beginning of files opened for reading to determine if it is SAM/BAM. To determine the format (SAM/BAM) of files open for writing, the software checks the output file's extension. If the extension is "bam" it writes a BAM file, otherwise it writes a SAM file.

The library is found in statgen/lib/bam.

BAM/SAM Classes

Class Name Description
SamFile Class used for reading/writing SAM/BAM files and their headers and records.
SamFileHeader Class used for storing the header. Allows access for getting and setting header values when both reading & writing SAM/BAM files.
SamHeaderRecord Class used for storing the tag/value pairs within a given header line.
SamRecord Class used for storing a SAM/BAM Record. Allows access for getting and setting record values when both reading & writing SAM/BAM files.
SamStatus Status value used by the BAM classes for returning the status of the operations.
SamValidator Validates a SAM/BAM Record.
SamValidationError Validation Error Information for a SamRecord.
SamValidationErrors Container for ValidationErrors.
Pileup Template for doing pileups.
PileupElement Base class that can be used for the elements stored in a Pileup.
ErrorHandler Class for handling errors based on the error handling type.
PosList Store refID/position, but does not store values < 0.
SamFilter Class for filtering a SAM/BAM record.
SamFlag Class for getting information from a SAM/BAM flag.
SamReferenceInfo Class for tracking the reference information mapping between the reference ids and the reference names.
SamTags Class for parsing/creating/operating on SAM/BAM record tags.

FAQs

SAM/BAM Classes FAQs

Usage Examples

Sam Library Usage Examples


Programs

BamUtil contains a set of programs that uses this library to operate on SAM & BAM files. It includes tools for converting between SAM & BAM and validating the files. See BamUtil for more information and a description of all the tools.