libStatGen Software  1
PrintRefPositions.cpp
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 //////////////////////////////////////////////////////////////////////////
19 #include "SamFile.h"
20 
21 void printRefPositions(std::string inFile, std::string indexFile,
22  std::string rname, int startPosition,
23  int endPosition)
24 {
25  SamFileHeader header;
26  // Open the bam file for reading and read the header.
27  SamFile samIn(inFile.c_str(), SamFile::READ, &header);
28 
29  // Open the bam index file for reading.
30  samIn.ReadBamIndex(indexFile.c_str());
31 
32  // Set the section to be read.
33  samIn.SetReadSection(rname.c_str(), startPosition, endPosition);
34 
35  SamRecord record;
36  // Keep reading BAM records until they aren't anymore.
37  while(samIn.ReadRecord(header, record))
38  {
39  // Print the reference positions associated with this read.
40  std::cout << "Read " << samIn.GetCurrentRecordCount() << ":";
41  Cigar* cigar = record.getCigarInfo();
42  for(int i = 0; i < record.getReadLength(); i++)
43  {
44  int refPos =
45  cigar->getRefPosition(i, record.get1BasedPosition());
46  if(refPos != Cigar::INDEX_NA)
47  {
48  std::cout << " " << refPos;
49  }
50  }
51  std::cout << "\n";
52  }
53 }
This class represents the CIGAR without any methods to set the cigar (see CigarRoller for that).
Definition: Cigar.h:84
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
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
This class allows a user to get/set the fields in a SAM/BAM Header.
Definition: SamFileHeader.h:35
Allows the user to easily read/write a SAM/BAM file.
Definition: SamFile.h:36
@ READ
open for reading.
Definition: SamFile.h:40
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
Definition: SamRecord.h:52
int32_t get1BasedPosition()
Get the 1-based(SAM) leftmost position (POS) of the record.
Definition: SamRecord.cpp:1312
Cigar * getCigarInfo()
Returns a pointer to the Cigar object associated with this record.
Definition: SamRecord.cpp:1836
int32_t getReadLength()
Get the length of the read.
Definition: SamRecord.cpp:1391