libStatGen Software  1
PrintRefPositions.cpp
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 //////////////////////////////////////////////////////////////////////////
00019 #include "SamFile.h"
00020 
00021 void printRefPositions(std::string inFile, std::string indexFile,
00022                        std::string rname, int startPosition, 
00023                        int endPosition)
00024 {
00025     SamFileHeader header;
00026     // Open the bam file for reading and read the header.
00027     SamFile samIn(inFile.c_str(), SamFile::READ, &header);
00028 
00029     // Open the bam index file for reading.
00030     samIn.ReadBamIndex(indexFile.c_str());
00031 
00032     // Set the section to be read.
00033     samIn.SetReadSection(rname.c_str(), startPosition, endPosition);
00034 
00035     SamRecord record;
00036     // Keep reading BAM records until they aren't anymore.
00037     while(samIn.ReadRecord(header, record))
00038     {
00039         // Print the reference positions associated with this read.
00040         std::cout << "Read " << samIn.GetCurrentRecordCount() << ":";
00041         Cigar* cigar = record.getCigarInfo();
00042         for(int i = 0; i < record.getReadLength(); i++)
00043         {
00044             int refPos = 
00045                 cigar->getRefPosition(i, record.get1BasedPosition());
00046             if(refPos != Cigar::INDEX_NA)
00047             {
00048                 std::cout << "  " << refPos;
00049             }
00050         }
00051         std::cout << "\n";
00052     }
00053 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends