PrintRefPositions.cpp
/*
* Copyright (C) 2010 Regents of the University of Michigan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//////////////////////////////////////////////////////////////////////////
#include "SamFile.h"
void printRefPositions(std::string inFile, std::string indexFile,
std::string rname, int startPosition,
int endPosition)
{
SamFileHeader header;
// Open the bam file for reading and read the header.
SamFile samIn(inFile.c_str(), SamFile::READ, &header);
// Open the bam index file for reading.
samIn.ReadBamIndex(indexFile.c_str());
// Set the section to be read.
samIn.SetReadSection(rname.c_str(), startPosition, endPosition);
SamRecord record;
// Keep reading BAM records until they aren't anymore.
while(samIn.ReadRecord(header, record))
{
// Print the reference positions associated with this read.
std::cout << "Read " << samIn.GetCurrentRecordCount() << ":";
Cigar* cigar = record.getCigarInfo();
for(int i = 0; i < record.getReadLength(); i++)
{
int refPos =
cigar->getRefPosition(i, record.get1BasedPosition());
if(refPos != Cigar::INDEX_NA)
{
std::cout << " " << refPos;
}
}
std::cout << "\n";
}
}
Cigar
This class represents the CIGAR without any methods to set the cigar (see CigarRoller for that).
Definition: Cigar.h:84
Cigar::INDEX_NA
static const int32_t INDEX_NA
Value associated with an index that is not applicable/does not exist, used for converting between que...
Definition: Cigar.h:492
Cigar::getRefPosition
int32_t getRefPosition(int32_t queryIndex, int32_t queryStartPos)
Return the reference position associated with the specified query index or INDEX_NA based on this cig...
Definition: Cigar.cpp:217
SamFileHeader
This class allows a user to get/set the fields in a SAM/BAM Header.
Definition: SamFileHeader.h:35
SamFile
Allows the user to easily read/write a SAM/BAM file.
Definition: SamFile.h:36
SamFile::READ
@ READ
open for reading.
Definition: SamFile.h:40
SamRecord
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
Definition: SamRecord.h:52
SamRecord::get1BasedPosition
int32_t get1BasedPosition()
Get the 1-based(SAM) leftmost position (POS) of the record.
Definition: SamRecord.cpp:1312
SamRecord::getCigarInfo
Cigar * getCigarInfo()
Returns a pointer to the Cigar object associated with this record.
Definition: SamRecord.cpp:1836
SamRecord::getReadLength
int32_t getReadLength()
Get the length of the read.
Definition: SamRecord.cpp:1391