+++ /dev/null
-// ***************************************************************************\r
-// BamTrimMain.cpp (c) 2009 Derek Barnett\r
-// Marth Lab, Department of Biology, Boston College\r
-// All rights reserved.\r
-// ---------------------------------------------------------------------------\r
-// Last modified: 15 July 2009 (DB)\r
-// ---------------------------------------------------------------------------\r
-// Basic example of reading/writing BAM files. Pulls alignments overlapping \r
-// the range specified by user from one BAM file and writes to a new BAM file.\r
-// ***************************************************************************\r
-\r
-// Std C/C++ includes\r
-#include <cstdlib>\r
-#include <iostream>\r
-#include <string>\r
-using namespace std;\r
-\r
-// BamTools includes\r
-#include "BamReader.h"\r
-#include "BamWriter.h"\r
-using namespace BamTools;\r
-\r
-int main(int argc, char* argv[]) {\r
-\r
- // validate argument count\r
- if( argc != 7 ) {\r
- cerr << "USAGE: " << argv[0] << " <input BAM file> <input BAM index file> <output BAM file> <reference name> <leftBound> <rightBound> " << endl;\r
- exit(1);\r
- }\r
-\r
- // store arguments\r
- string inBamFilename = argv[1];\r
- string indexFilename = argv[2];\r
- string outBamFilename = argv[3];\r
- string referenceName = argv[4];\r
- string leftBound_str = argv[5];\r
- string rightBound_str = argv[6];\r
-\r
- // open our BAM reader\r
- BamReader reader;\r
- reader.Open(inBamFilename, indexFilename);\r
-\r
- // get header & reference information\r
- string header = reader.GetHeaderText();\r
- RefVector references = reader.GetReferenceData();\r
- \r
- // open our BAM writer\r
- BamWriter writer;\r
- writer.Open(outBamFilename, header, references);\r
-\r
- // get reference ID from name\r
- int refID = 0;\r
- RefVector::const_iterator refIter = references.begin();\r
- RefVector::const_iterator refEnd = references.end();\r
- for ( ; refIter != refEnd; ++refIter ) {\r
- if ( (*refIter).RefName == referenceName ) { break; }\r
- ++refID;\r
- }\r
- \r
- // validate ID\r
- if ( refIter == refEnd ) {\r
- cerr << "Reference: " << referenceName << " not found." << endl;\r
- exit(1);\r
- }\r
-\r
- // convert boundary arguments to numeric values\r
- int leftBound = (int) atoi( leftBound_str.c_str() );\r
- int rightBound = (int) atoi( rightBound_str.c_str() );\r
- \r
- // attempt jump to range of interest\r
- if ( reader.Jump(refID, leftBound) ) {\r
- \r
- // while data exists and alignment begin before right bound\r
- BamAlignment bAlignment;\r
- while ( reader.GetNextAlignment(bAlignment) && (bAlignment.Position <= rightBound) ) {\r
- // save alignment to archive\r
- writer.SaveAlignment(bAlignment);\r
- }\r
- } \r
- \r
- // if jump failed\r
- else {\r
- cerr << "Could not jump to ref:pos " << referenceName << ":" << leftBound << endl;\r
- exit(1);\r
- }\r
-\r
- // clean up and exit\r
- reader.Close();\r
- writer.Close(); \r
- return 0;\r
-}
\ No newline at end of file