本文整理汇总了C++中BamMultiReader::GetHeaderText方法的典型用法代码示例。如果您正苦于以下问题:C++ BamMultiReader::GetHeaderText方法的具体用法?C++ BamMultiReader::GetHeaderText怎么用?C++ BamMultiReader::GetHeaderText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BamMultiReader
的用法示例。
在下文中一共展示了BamMultiReader::GetHeaderText方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: filelist
bool HeaderTool::HeaderToolPrivate::Run(void) {
// set to default input if none provided
if ( !m_settings->HasInput && !m_settings->HasInputFilelist )
m_settings->InputFiles.push_back(Options::StandardIn());
// add files in the filelist to the input file list
if ( m_settings->HasInputFilelist ) {
ifstream filelist(m_settings->InputFilelist.c_str(), ios::in);
if ( !filelist.is_open() ) {
cerr << "bamtools header ERROR: could not open input BAM file list... Aborting." << endl;
return false;
}
string line;
while ( getline(filelist, line) )
m_settings->InputFiles.push_back(line);
}
// attemp to open BAM files
BamMultiReader reader;
if ( !reader.Open(m_settings->InputFiles) ) {
cerr << "bamtools header ERROR: could not open BAM file(s) for reading... Aborting." << endl;
return false;
}
// dump (merged) header contents to stdout
cout << reader.GetHeaderText() << endl;
// clean up & exit
reader.Close();
return true;
}
示例2: if
//.........这里部分代码省略.........
cerr << "bamtools convert ERROR: could not locate index file(s)... Aborting." << endl;
return false;
}
}
// retrieve reference data
m_references = reader.GetReferenceData();
// set region if specified
BamRegion region;
if ( m_settings->HasRegion ) {
if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
if ( reader.HasIndexes() ) {
if ( !reader.SetRegion(region) ) {
cerr << "bamtools convert ERROR: set region failed. Check that REGION describes a valid range" << endl;
reader.Close();
return false;
}
}
} else {
cerr << "bamtools convert ERROR: could not parse REGION: " << m_settings->Region << endl;
cerr << "Check that REGION is in valid format (see documentation) and that the coordinates are valid"
<< endl;
reader.Close();
return false;
}
}
// if output file given
ofstream outFile;
if ( m_settings->HasOutput ) {
// open output file stream
outFile.open(m_settings->OutputFilename.c_str());
if ( !outFile ) {
cerr << "bamtools convert ERROR: could not open " << m_settings->OutputFilename
<< " for output" << endl;
return false;
}
// set m_out to file's streambuf
m_out.rdbuf(outFile.rdbuf());
}
// -------------------------------------
// do conversion based on format
bool convertedOk = true;
// pileup is special case
// conversion not done per alignment, like the other formats
if ( m_settings->Format == FORMAT_PILEUP )
convertedOk = RunPileupConversion(&reader);
// all other formats
else {
bool formatError = false;
// set function pointer to proper conversion method
void (BamTools::ConvertTool::ConvertToolPrivate::*pFunction)(const BamAlignment&) = 0;
if ( m_settings->Format == FORMAT_BED ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintBed;
else if ( m_settings->Format == FORMAT_FASTA ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintFasta;
else if ( m_settings->Format == FORMAT_FASTQ ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintFastq;
else if ( m_settings->Format == FORMAT_JSON ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintJson;
else if ( m_settings->Format == FORMAT_SAM ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintSam;
else if ( m_settings->Format == FORMAT_YAML ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintYaml;
else {
cerr << "bamtools convert ERROR: unrecognized format: " << m_settings->Format << endl;
cerr << "Please see documentation for list of supported formats " << endl;
formatError = true;
convertedOk = false;
}
// if format selected ok
if ( !formatError ) {
// if SAM format & not omitting header, print SAM header first
if ( (m_settings->Format == FORMAT_SAM) && !m_settings->IsOmittingSamHeader )
m_out << reader.GetHeaderText();
// iterate through file, doing conversion
BamAlignment a;
while ( reader.GetNextAlignment(a) )
(this->*pFunction)(a);
// set flag for successful conversion
convertedOk = true;
}
}
// ------------------------
// clean up & exit
reader.Close();
if ( m_settings->HasOutput )
outFile.close();
return convertedOk;
}
示例3: filelist
bool RandomTool::RandomToolPrivate::Run(void) {
// set to default stdin if no input files provided
if ( !m_settings->HasInput && !m_settings->HasInputFilelist )
m_settings->InputFiles.push_back(Options::StandardIn());
// add files in the filelist to the input file list
if ( m_settings->HasInputFilelist ) {
ifstream filelist(m_settings->InputFilelist.c_str(), ios::in);
if ( !filelist.is_open() ) {
cerr << "bamtools random ERROR: could not open input BAM file list... Aborting." << endl;
return false;
}
string line;
while ( getline(filelist, line) )
m_settings->InputFiles.push_back(line);
}
// open our reader
BamMultiReader reader;
if ( !reader.Open(m_settings->InputFiles) ) {
cerr << "bamtools random ERROR: could not open input BAM file(s)... Aborting." << endl;
return false;
}
// look up index files for all BAM files
reader.LocateIndexes();
// make sure index data is available
if ( !reader.HasIndexes() ) {
cerr << "bamtools random ERROR: could not load index data for all input BAM file(s)... Aborting." << endl;
reader.Close();
return false;
}
// get BamReader metadata
const string headerText = reader.GetHeaderText();
const RefVector references = reader.GetReferenceData();
if ( references.empty() ) {
cerr << "bamtools random ERROR: no reference data available... Aborting." << endl;
reader.Close();
return false;
}
// determine compression mode for BamWriter
bool writeUncompressed = ( m_settings->OutputFilename == Options::StandardOut() &&
!m_settings->IsForceCompression );
BamWriter::CompressionMode compressionMode = BamWriter::Compressed;
if ( writeUncompressed ) compressionMode = BamWriter::Uncompressed;
// open BamWriter
BamWriter writer;
writer.SetCompressionMode(compressionMode);
if ( !writer.Open(m_settings->OutputFilename, headerText, references) ) {
cerr << "bamtools random ERROR: could not open " << m_settings->OutputFilename
<< " for writing... Aborting." << endl;
reader.Close();
return false;
}
// if user specified a REGION constraint, attempt to parse REGION string
BamRegion region;
if ( m_settings->HasRegion && !Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
cerr << "bamtools random ERROR: could not parse REGION: " << m_settings->Region << endl;
cerr << "Check that REGION is in valid format (see documentation) and that the coordinates are valid"
<< endl;
reader.Close();
writer.Close();
return false;
}
// seed our random number generator
srand( time(NULL) );
// grab random alignments
BamAlignment al;
unsigned int i = 0;
while ( i < m_settings->AlignmentCount ) {
int randomRefId = 0;
int randomPosition = 0;
// use REGION constraints to select random refId & position
if ( m_settings->HasRegion ) {
// select a random refId
randomRefId = getRandomInt(region.LeftRefID, region.RightRefID);
// select a random position based on randomRefId
const int lowerBoundPosition = ( (randomRefId == region.LeftRefID)
? region.LeftPosition
: 0 );
const int upperBoundPosition = ( (randomRefId == region.RightRefID)
? region.RightPosition
: (references.at(randomRefId).RefLength - 1) );
randomPosition = getRandomInt(lowerBoundPosition, upperBoundPosition);
}
//.........这里部分代码省略.........
示例4: main
int main ( int argc, char *argv[] ) {
struct parameters *param = 0;
param = interface(param, argc, argv);
//bam input and generate index if not yet
//-------------------------------------------------------------------------------------------------------+
// BAM input (file or filenames?) |
//-------------------------------------------------------------------------------------------------------+
char *fof = param->mapping_f;
FILE *IN=NULL;
char linefof[5000];
int filecount=0;
vector <string> fnames;
if (strchr(fof,' ')!=NULL) {
char *ptr;
ptr=strtok(fof," ");
while (ptr!=NULL) {
fnames.push_back(ptr);
filecount++;
ptr=strtok(NULL," ");
}
} else {
IN=fopen(fof,"rt");
if (IN!=NULL) {
long linecount=0;
while (fgets(linefof,5000-1,IN)!=NULL) {
linecount++;
if (linefof[0]!='#' && linefof[0]!='\n') {
char *ptr=strchr(linefof,'\n');
if (ptr!=NULL && ptr[0]=='\n') {
ptr[0]='\0';
}
FILE *dummy=NULL;
dummy=fopen(linefof,"rt");
if (dummy!=NULL) { // seems to be a file of filenames...
fclose(dummy);
fnames.push_back(linefof);
filecount++;
} else if (filecount==0 || linecount>=1000-1) { // seems to be a single file
fnames.push_back(fof);
filecount++;
break;
}
}
}
fclose(IN);
}
} //file or file name decided and stored in vector "fnames"
cerr << "the input mapping files are:" << endl;
vector <string>::iterator fit = fnames.begin();
for(; fit != fnames.end(); fit++) {
cerr << *fit << endl;
}
//-------------------------------------------------------------------------------------------------------+
// end of file or filenames |
//-------------------------------------------------------------------------------------------------------+
// open the BAM file(s)
BamMultiReader reader;
reader.Open(fnames);
// get header & reference information
string header = reader.GetHeaderText();
RefVector refs = reader.GetReferenceData();
// attempt to open BamWriter
BamWriter writer;
string outputBam = param->writer;
if ( outputBam != "" ) {
if ( !writer.Open(param->writer, header, refs) ) {
cerr << "Could not open output BAM file" << endl;
exit(0);
}
}
BamAlignment bam;
while (reader.GetNextAlignment(bam)) { //change RG
string rg = "RG";
string rgType = "Z";
string rgValue = "1";
bam.EditTag(rg,rgType,rgValue);
writer.SaveAlignment(bam);
} // read a bam
return 0;
} //main
示例5: if
bool ConvertTool::ConvertToolPrivate::Run(void) {
// ------------------------------------
// initialize conversion input/output
// set to default input if none provided
if ( !m_settings->HasInput )
m_settings->InputFiles.push_back(Options::StandardIn());
// open input files
BamMultiReader reader;
if ( !m_settings->HasInput ) { // don't attempt to open index for stdin
if ( !reader.Open(m_settings->InputFiles, false) ) {
cerr << "Could not open input files" << endl;
return false;
}
} else {
if ( !reader.Open(m_settings->InputFiles, true) ) {
if ( !reader.Open(m_settings->InputFiles, false) ) {
cerr << "Could not open input files" << endl;
return false;
} else {
cerr << "Opened reader without index file, jumping is disabled." << endl;
}
}
}
m_references = reader.GetReferenceData();
// set region if specified
BamRegion region;
if ( m_settings->HasRegion ) {
if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
if ( !reader.SetRegion(region) ) {
cerr << "Could not set BamReader region to REGION: " << m_settings->Region << endl;
return false;
}
} else {
cerr << "Could not parse REGION: " << m_settings->Region << endl;
return false;
}
}
// if output file given
ofstream outFile;
if ( m_settings->HasOutput ) {
// open output file stream
outFile.open(m_settings->OutputFilename.c_str());
if ( !outFile ) {
cerr << "Could not open " << m_settings->OutputFilename << " for output" << endl;
return false;
}
// set m_out to file's streambuf
m_out.rdbuf(outFile.rdbuf());
}
// -------------------------------------
// do conversion based on format
bool convertedOk = true;
// pileup is special case
// conversion not done per alignment, like the other formats
if ( m_settings->Format == FORMAT_PILEUP )
convertedOk = RunPileupConversion(&reader);
// all other formats
else {
bool formatError = false;
// set function pointer to proper conversion method
void (BamTools::ConvertTool::ConvertToolPrivate::*pFunction)(const BamAlignment&) = 0;
if ( m_settings->Format == FORMAT_BED ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintBed;
else if ( m_settings->Format == FORMAT_BEDGRAPH ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintBedGraph;
else if ( m_settings->Format == FORMAT_FASTA ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintFasta;
else if ( m_settings->Format == FORMAT_FASTQ ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintFastq;
else if ( m_settings->Format == FORMAT_JSON ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintJson;
else if ( m_settings->Format == FORMAT_SAM ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintSam;
else if ( m_settings->Format == FORMAT_WIGGLE ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintWiggle;
else if ( m_settings->Format == FORMAT_YAML ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintYaml;
else {
cerr << "Unrecognized format: " << m_settings->Format << endl;
cerr << "Please see help|README (?) for details on supported formats " << endl;
formatError = true;
convertedOk = false;
}
// if format selected ok
if ( !formatError ) {
// if SAM format & not omitting header, print SAM header first
if ( (m_settings->Format == FORMAT_SAM) && !m_settings->IsOmittingSamHeader )
m_out << reader.GetHeaderText();
// iterate through file, doing conversion
BamAlignment a;
while ( reader.GetNextAlignment(a) )
(this->*pFunction)(a);
//.........这里部分代码省略.........
示例6: filelist
bool FilterTool::FilterToolPrivate::Run(void) {
// set to default input if none provided
if ( !m_settings->HasInput && !m_settings->HasInputFilelist )
m_settings->InputFiles.push_back(Options::StandardIn());
// add files in the filelist to the input file list
if ( m_settings->HasInputFilelist ) {
ifstream filelist(m_settings->InputFilelist.c_str(), ios::in);
if ( !filelist.is_open() ) {
cerr << "bamtools filter ERROR: could not open input BAM file list... Aborting." << endl;
return false;
}
string line;
while ( getline(filelist, line) )
m_settings->InputFiles.push_back(line);
}
// initialize defined properties & user-specified filters
// quit if failed
if ( !SetupFilters() )
return false;
// open reader without index
BamMultiReader reader;
if ( !reader.Open(m_settings->InputFiles) ) {
cerr << "bamtools filter ERROR: could not open input files for reading." << endl;
return false;
}
// retrieve reader header & reference data
const string headerText = reader.GetHeaderText();
filterToolReferences = reader.GetReferenceData();
// determine compression mode for BamWriter
bool writeUncompressed = ( m_settings->OutputFilename == Options::StandardOut() &&
!m_settings->IsForceCompression );
BamWriter::CompressionMode compressionMode = BamWriter::Compressed;
if ( writeUncompressed ) compressionMode = BamWriter::Uncompressed;
// open BamWriter
BamWriter writer;
writer.SetCompressionMode(compressionMode);
if ( !writer.Open(m_settings->OutputFilename, headerText, filterToolReferences) ) {
cerr << "bamtools filter ERROR: could not open " << m_settings->OutputFilename << " for writing." << endl;
reader.Close();
return false;
}
// if no region specified, filter entire file
BamAlignment al;
if ( !m_settings->HasRegion ) {
while ( reader.GetNextAlignment(al) ) {
if ( CheckAlignment(al) )
writer.SaveAlignment(al);
}
}
// otherwise attempt to use region as constraint
else {
// if region string parses OK
BamRegion region;
if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
// attempt to find index files
reader.LocateIndexes();
// if index data available for all BAM files, we can use SetRegion
if ( reader.HasIndexes() ) {
// attempt to use SetRegion(), if failed report error
if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
cerr << "bamtools filter ERROR: set region failed. Check that REGION describes a valid range" << endl;
reader.Close();
return false;
}
// everything checks out, just iterate through specified region, filtering alignments
while ( reader.GetNextAlignment(al) )
if ( CheckAlignment(al) )
writer.SaveAlignment(al);
}
// no index data available, we have to iterate through until we
// find overlapping alignments
else {
while ( reader.GetNextAlignment(al) ) {
if ( (al.RefID >= region.LeftRefID) && ((al.Position + al.Length) >= region.LeftPosition) &&
(al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) )
{
if ( CheckAlignment(al) )
writer.SaveAlignment(al);
}
}
}
}
//.........这里部分代码省略.........
示例7: file
bool MergeTool::MergeToolPrivate::Run(void) {
// set to default input if none provided
if ( !m_settings->HasInputBamFilename )
m_settings->InputFiles.push_back(Options::StandardIn());
// opens the BAM files (by default without checking for indexes)
BamMultiReader reader;
if ( !reader.Open(m_settings->InputFiles) ) {
cerr << "bamtools merge ERROR: could not open input BAM file(s)... Aborting." << endl;
return false;
}
// retrieve header & reference dictionary info
std::string mergedHeader = reader.GetHeaderText();
RefVector references = reader.GetReferenceData();
// determine compression mode for BamWriter
bool writeUncompressed = ( m_settings->OutputFilename == Options::StandardOut() &&
!m_settings->IsForceCompression );
BamWriter::CompressionMode compressionMode = BamWriter::Compressed;
if ( writeUncompressed ) compressionMode = BamWriter::Uncompressed;
// open BamWriter
BamWriter writer;
writer.SetCompressionMode(compressionMode);
if ( !writer.Open(m_settings->OutputFilename, mergedHeader, references) ) {
cerr << "bamtools merge ERROR: could not open "
<< m_settings->OutputFilename << " for writing." << endl;
reader.Close();
return false;
}
// if no region specified, store entire contents of file(s)
if ( !m_settings->HasRegion ) {
BamAlignment al;
while ( reader.GetNextAlignmentCore(al) )
writer.SaveAlignment(al);
}
// otherwise attempt to use region as constraint
else {
// if region string parses OK
BamRegion region;
if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
// attempt to find index files
reader.LocateIndexes();
// if index data available for all BAM files, we can use SetRegion
if ( reader.HasIndexes() ) {
// attempt to use SetRegion(), if failed report error
if ( !reader.SetRegion(region.LeftRefID,
region.LeftPosition,
region.RightRefID,
region.RightPosition) )
{
cerr << "bamtools merge ERROR: set region failed. Check that REGION describes a valid range"
<< endl;
reader.Close();
return false;
}
// everything checks out, just iterate through specified region, storing alignments
BamAlignment al;
while ( reader.GetNextAlignmentCore(al) )
writer.SaveAlignment(al);
}
// no index data available, we have to iterate through until we
// find overlapping alignments
else {
BamAlignment al;
while ( reader.GetNextAlignmentCore(al) ) {
if ( (al.RefID >= region.LeftRefID) && ( (al.Position + al.Length) >= region.LeftPosition ) &&
(al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) )
{
writer.SaveAlignment(al);
}
}
}
}
// error parsing REGION string
else {
cerr << "bamtools merge ERROR: could not parse REGION - " << m_settings->Region << endl;
cerr << "Check that REGION is in valid format (see documentation) and that the coordinates are valid"
<< endl;
reader.Close();
writer.Close();
return false;
}
}
// clean & exit
reader.Close();
writer.Close();
return true;
//.........这里部分代码省略.........
示例8: main
int main ( int argc, char *argv[] ) {
struct parameters *param = 0;
param = interface(param, argc, argv);
//region file input (the region file should be sorted as the same way as the bam file)
ifstream region_f;
region_f.open(param->region_f, ios_base::in); // the region file is opened
//bam input and generate index if not yet
//-------------------------------------------------------------------------------------------------------+
// BAM input (file or filenames?) |
//-------------------------------------------------------------------------------------------------------+
char *fof = param->mapping_f;
FILE *IN=NULL;
char linefof[5000];
int filecount=0;
vector <string> fnames;
if (strchr(fof,' ')!=NULL) {
char *ptr;
ptr=strtok(fof," ");
while (ptr!=NULL) {
fnames.push_back(ptr);
filecount++;
ptr=strtok(NULL," ");
}
} else {
IN=fopen(fof,"rt");
if (IN!=NULL) {
long linecount=0;
while (fgets(linefof,5000-1,IN)!=NULL) {
linecount++;
if (linefof[0]!='#' && linefof[0]!='\n') {
char *ptr=strchr(linefof,'\n');
if (ptr!=NULL && ptr[0]=='\n') {
ptr[0]='\0';
}
FILE *dummy=NULL;
dummy=fopen(linefof,"rt");
if (dummy!=NULL) { // seems to be a file of filenames...
fclose(dummy);
fnames.push_back(linefof);
filecount++;
} else if (filecount==0 || linecount>=1000-1) { // seems to be a single file
fnames.push_back(fof);
filecount++;
break;
}
}
}
fclose(IN);
}
} //file or file name decided and stored in vector "fnames"
cerr << "the input mapping files are:" << endl;
vector <string>::iterator fit = fnames.begin();
for(; fit != fnames.end(); fit++) {
cerr << *fit << endl;
}
//-------------------------------------------------------------------------------------------------------+
// end of file or filenames |
//-------------------------------------------------------------------------------------------------------+
// open the BAM file(s)
BamMultiReader reader;
reader.Open(fnames);
// get header & reference information
string header = reader.GetHeaderText();
RefVector refs = reader.GetReferenceData();
if ( ! reader.LocateIndexes() ) // opens any existing index files that match our BAM files
reader.CreateIndexes(); // creates index files for BAM files that still lack one
// locus bias
struct lb empty_profile = {0,0,0,0};
vector <struct lb> locus_b(1000, empty_profile);
// output locus bias file
string locus_bias_set = param->lbias;
ofstream locus_bias;
if ( locus_bias_set != "" ) {
locus_bias.open(param->lbias);
if ( !locus_bias ) {
cerr << "can not open locus_bias file.\n";
exit(0);
}
}
//should decide which chromosome
string line;
string old_chr = "SRP";
string type = param->type;
//whether do some position-level pile-up stuff
bool posc = false;
ofstream posc_f;
ofstream chrmap_f;
//.........这里部分代码省略.........
示例9: Run
int MergeTool::Run(int argc, char* argv[]) {
// parse command line arguments
Options::Parse(argc, argv, 1);
// set to default input if none provided
if ( !m_settings->HasInputBamFilename )
m_settings->InputFiles.push_back(Options::StandardIn());
// opens the BAM files (by default without checking for indexes)
BamMultiReader reader;
if ( !reader.Open(m_settings->InputFiles, false, true) ) {
cerr << "ERROR: Could not open input BAM file(s)... Aborting." << endl;
return 1;
}
// retrieve header & reference dictionary info
std::string mergedHeader = reader.GetHeaderText();
RefVector references = reader.GetReferenceData();
// open writer
BamWriter writer;
bool writeUncompressed = ( m_settings->OutputFilename == Options::StandardOut() && !m_settings->IsForceCompression );
if ( !writer.Open(m_settings->OutputFilename, mergedHeader, references, writeUncompressed) ) {
cerr << "ERROR: Could not open BAM file " << m_settings->OutputFilename << " for writing... Aborting." << endl;
reader.Close();
return 1;
}
// if no region specified, store entire contents of file(s)
if ( !m_settings->HasRegion ) {
BamAlignment al;
while ( reader.GetNextAlignmentCore(al) )
writer.SaveAlignment(al);
}
// otherwise attempt to use region as constraint
else {
// if region string parses OK
BamRegion region;
if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
// attempt to re-open reader with index files
reader.Close();
bool openedOK = reader.Open(m_settings->InputFiles, true, true );
// if error
if ( !openedOK ) {
cerr << "ERROR: Could not open input BAM file(s)... Aborting." << endl;
return 1;
}
// if index data available, we can use SetRegion
if ( reader.IsIndexLoaded() ) {
// attempt to use SetRegion(), if failed report error
if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
cerr << "ERROR: Region requested, but could not set BamReader region to REGION: " << m_settings->Region << " Aborting." << endl;
reader.Close();
return 1;
}
// everything checks out, just iterate through specified region, storing alignments
BamAlignment al;
while ( reader.GetNextAlignmentCore(al) )
writer.SaveAlignment(al);
}
// no index data available, we have to iterate through until we
// find overlapping alignments
else {
BamAlignment al;
while ( reader.GetNextAlignmentCore(al) ) {
if ( (al.RefID >= region.LeftRefID) && ( (al.Position + al.Length) >= region.LeftPosition ) &&
(al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) )
{
writer.SaveAlignment(al);
}
}
}
}
// error parsing REGION string
else {
cerr << "ERROR: Could not parse REGION - " << m_settings->Region << endl;
cerr << "Be sure REGION is in valid format (see README) and that coordinates are valid for selected references" << endl;
reader.Close();
writer.Close();
return 1;
}
}
// clean & exit
reader.Close();
writer.Close();
return 0;
}