本文整理汇总了C++中BamWriter::Open方法的典型用法代码示例。如果您正苦于以下问题:C++ BamWriter::Open方法的具体用法?C++ BamWriter::Open怎么用?C++ BamWriter::Open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BamWriter
的用法示例。
在下文中一共展示了BamWriter::Open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char* argv[])
{
vector<string> bamFile;
for(int i=1; i<argc; ++i)
bamFile.push_back(argv[i]);
string outFile = "combined.bam";
BamMultiReader reader;
reader.Open(bamFile);
SamHeader header = reader.GetHeader();
RefVector refs = reader.GetReferenceData();
BamWriter writer;
writer.SetNumThreads(8);
writer.Open(outFile, header, refs);
assert(writer.IsOpen());
BamAlignment al;
size_t numReads = 0;
while(reader.GetNextAlignment(al)){
writer.SaveAlignment(al);
if(++numReads % 10000 == 0)
cerr << setw(12) << numReads << '\r';
}
cerr << setw(12) << numReads << endl;
cerr << "done!" << endl;
}
示例2: IntersectBamPE
void BedIntersectPE::IntersectBamPE(string bamFile) {
// load the "B" bed file into a map so
// that we can easily compare "A" to it for overlaps
_bedB->loadBedFileIntoMap();
// open the BAM file
BamReader reader;
BamWriter writer;
reader.Open(bamFile);
// get header & reference information
string bamHeader = reader.GetHeaderText();
RefVector refs = reader.GetReferenceData();
// open a BAM output to stdout if we are writing BAM
if (_bamOutput == true) {
// set compression mode
BamWriter::CompressionMode compressionMode = BamWriter::Compressed;
if ( _isUncompressedBam ) compressionMode = BamWriter::Uncompressed;
writer.SetCompressionMode(compressionMode);
// open our BAM writer
writer.Open("stdout", bamHeader, refs);
}
// track the previous and current sequence
// names so that we can identify blocks of
// alignments for a given read ID.
string prevName, currName;
prevName = currName = "";
vector<BamAlignment> alignments; // vector of BAM alignments for a given ID in a BAM file.
alignments.reserve(100);
_bedA->bedType = 10; // it's a full BEDPE given it's BAM
// rip through the BAM file and convert each mapped entry to BEDPE
BamAlignment bam1, bam2;
while (reader.GetNextAlignment(bam1)) {
// the alignment must be paired
if (bam1.IsPaired() == true) {
// grab the second alignment for the pair.
reader.GetNextAlignment(bam2);
// require that the alignments are from the same query
if (bam1.Name == bam2.Name) {
ProcessBamBlock(bam1, bam2, refs, writer);
}
else {
cerr << "*****ERROR: -bedpe requires BAM to be sorted or grouped by query name. " << endl;
exit(1);
}
}
}
// close up
reader.Close();
if (_bamOutput == true) {
writer.Close();
}
}
示例3: while
bool RevertTool::RevertToolPrivate::Run(void) {
// opens the BAM file without checking for indexes
BamReader reader;
if ( !reader.Open(m_settings->InputFilename) ) {
cerr << "Could not open input BAM file... quitting." << endl;
return false;
}
// get BAM file metadata
const string& headerText = reader.GetHeaderText();
const RefVector& references = reader.GetReferenceData();
// open writer
BamWriter writer;
bool writeUncompressed = ( m_settings->OutputFilename == Options::StandardOut() && !m_settings->IsForceCompression );
if ( !writer.Open(m_settings->OutputFilename, headerText, references, writeUncompressed) ) {
cerr << "Could not open " << m_settings->OutputFilename << " for writing." << endl;
return false;
}
// plow through file, reverting alignments
BamAlignment al;
while ( reader.GetNextAlignment(al) ) {
RevertAlignment(al);
writer.SaveAlignment(al);
}
// clean and exit
reader.Close();
writer.Close();
return true;
}
示例4: main
int main (int argc, char *argv[]) {
if( (argc== 1) ||
(argc== 2 && string(argv[1]) == "-h") ||
(argc== 2 && string(argv[1]) == "-help") ||
(argc== 2 && string(argv[1]) == "--help") ){
cout<<"Usage:setAsUnpaired [in bam] [outbam]"<<endl<<"this program takes flags all paired sequences as singles"<<endl;
return 1;
}
string bamfiletopen = string(argv[1]);
string bamFileOUT = string(argv[2]);
BamReader reader;
BamWriter writer;
if ( !reader.Open(bamfiletopen) ) {
cerr << "Could not open input BAM files." << endl;
return 1;
}
const SamHeader header = reader.GetHeader();
const RefVector references = reader.GetReferenceData();
if ( !writer.Open(bamFileOUT,header,references) ) {
cerr << "Could not open output BAM file "<<bamFileOUT << endl;
return 1;
}
BamAlignment al;
while ( reader.GetNextAlignment(al) ) {
if(al.IsMapped()){
cerr << "Cannot yet handle mapped reads " << endl;
return 1;
}
al.SetIsPaired (false);
writer.SaveAlignment(al);
} //while al
reader.Close();
writer.Close();
return 0;
}
示例5: while
// merges sorted temp BAM files into single sorted output BAM file
bool SortTool::SortToolPrivate::MergeSortedRuns(void) {
// open up multi reader for all of our temp files
// this might get broken up if we do a multi-pass system later ??
BamMultiReader multiReader;
if ( !multiReader.Open(m_tempFilenames) ) {
cerr << "bamtools sort ERROR: could not open BamMultiReader for merging temp files... Aborting."
<< endl;
return false;
}
// set sort order for merge
if ( m_settings->IsSortingByName )
multiReader.SetSortOrder(BamMultiReader::SortedByReadName);
else
multiReader.SetSortOrder(BamMultiReader::SortedByPosition);
// open writer for our completely sorted output BAM file
BamWriter mergedWriter;
if ( !mergedWriter.Open(m_settings->OutputBamFilename, m_headerText, m_references) ) {
cerr << "bamtools sort ERROR: could not open " << m_settings->OutputBamFilename
<< " for writing... Aborting." << endl;
multiReader.Close();
return false;
}
// while data available in temp files
BamAlignment al;
while ( multiReader.GetNextAlignmentCore(al) )
mergedWriter.SaveAlignment(al);
// close readers
multiReader.Close();
mergedWriter.Close();
// delete all temp files
vector<string>::const_iterator tempIter = m_tempFilenames.begin();
vector<string>::const_iterator tempEnd = m_tempFilenames.end();
for ( ; tempIter != tempEnd; ++tempIter ) {
const string& tempFilename = (*tempIter);
remove(tempFilename.c_str());
}
return true;
}
示例6:
bool SortTool::SortToolPrivate::WriteTempFile(const vector<BamAlignment>& buffer,
const string& tempFilename)
{
// open temp file for writing
BamWriter tempWriter;
if ( !tempWriter.Open(tempFilename, m_headerText, m_references) ) {
cerr << "bamtools sort ERROR: could not open " << tempFilename
<< " for writing." << endl;
return false;
}
// write data
vector<BamAlignment>::const_iterator buffIter = buffer.begin();
vector<BamAlignment>::const_iterator buffEnd = buffer.end();
for ( ; buffIter != buffEnd; ++buffIter ) {
const BamAlignment& al = (*buffIter);
tempWriter.SaveAlignment(al);
}
// close temp file & return success
tempWriter.Close();
return true;
}
示例7: while
bool CompressTool::CompressToolPrivate::Run(void) {
// ------------------------------------
// initialize conversion input/output
// set to default input if none provided
if ( !m_settings->HasInput )
m_settings->InputFilename = Options::StandardIn();
if ( !m_settings->HasOutput )
m_settings->InputFilename = Options::StandardOut();
SamReader reader;
reader.Open(m_settings->InputFilename);
BamWriter writer;
writer.Open( m_settings->OutputFilename, reader.GetHeader(), reader.GetReferenceData());
int alignment_ct = 0;
while(true) {
BamAlignment alignment;
if(!reader.GetNextAlignment(alignment))
break;
writer.SaveAlignment(alignment);
alignment_ct++;
//progress indicator
//if(alignment_ct % 500000 == 0)
// cerr << ".";
}
reader.Close();
writer.Close();
return true;
}
示例8: 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;
//.........这里部分代码省略.........
示例9: 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);
}
}
}
}
//.........这里部分代码省略.........
示例10: main
int main (int argc, char * argv[])
{
vector<string> inputFilenames;
string combinedOutFilename, alignmentsOutFilename;
try
{
TCLAP::CmdLine cmd("Program description", ' ', VERSION);
TCLAP::ValueArg<string> combinedOutputArg("o", "out",
"Combined output filename (BAM format)", true, "", "combined.bam", cmd);
TCLAP::ValueArg<int> minInsertArg("n", "min-insert",
"Minimum insert size", false, DEFAULT_MIN_GAP, "min insert size", cmd);
TCLAP::ValueArg<int> maxInsertArg("x", "max-insert",
"Maximum insert size", false, DEFAULT_MAX_GAP, "max insert size", cmd);
TCLAP::MultiArg<string> inputArgs("b", "bam",
"Input BAM file", true,
"input.bam", cmd);
cmd.parse(argc, argv);
combinedOutFilename = combinedOutputArg.getValue();
MIN_GAP = minInsertArg.getValue();
MAX_GAP = maxInsertArg.getValue();
inputFilenames = inputArgs.getValue();
} catch (TCLAP::ArgException &e) {
cerr << "Error: " << e.error() << " " << e.argId() << endl;
}
// TODO require that alignments are sorted by name
BamMultiReader reader;
reader.Open(inputFilenames);
if (!ValidOut.Open(combinedOutFilename, reader.GetHeader(),
reader.GetReferenceData()))
{
cerr << ValidOut.GetErrorString() << endl;
return 1;
}
string current, prev;
char mateID;
Group group;
set<string> references;
Alignment a;
while (reader.GetNextAlignment(a))
{
parseID(a.Name, current, mateID);
if (current.compare(prev) && prev.size() > 0)
{
processGroup(group, references);
group.clear();
references.clear();
}
references.insert(a.RefName);
GroupKey key;
key.refID = a.RefName;
key.mateID = mateID;
key.rev = a.IsReverseStrand();
group.insert( std::make_pair( key, a ) );
prev = current;
}
processGroup(group, references);
}
示例11: main
int main (int argc, char *argv[]) {
int length=0;
const string usage=string(string(argv[0])+" [options] input.bam out.bam"+"\n\n"+
"This program takes a BAM file as input and produces\n"+
"another where the first l bases have been cut\n"+
"\n"+
"Options:\n"+
"\t"+"-l" +"\n\t\t"+"Cut this length (Default "+stringify(length)+")"+"\n"
);
// "\t"+"-m , --mapped" +"\n\t\t"+"For an mapped bam file"+"\n");
if( (argc== 1) ||
(argc== 2 && string(argv[1]) == "-h") ||
(argc== 2 && string(argv[1]) == "-help") ||
(argc== 2 && string(argv[1]) == "--help") ) {
cout<<"Usage:"<<endl;
cout<<usage<<endl;
cout<<""<<endl;
return 1;
}
for(int i=1; i<(argc-2); i++) { //all but the last arg
// if(strcmp(argv[i],"-m") == 0 || strcmp(argv[i],"--mapped") == 0 ){
// mapped=true;
// continue;
// }
if(string(argv[i]) == "-l" ) {
length=destringify<int>(string(argv[i+1]));
i++;
continue;
}
cerr<<"Unknown option "<<argv[i] <<" exiting"<<endl;
return 1;
}
if(argc < 3) {
cerr<<"Error: Must specify the input and output BAM files";
return 1;
}
string inbamFile =argv[argc-2];
string outbamFile=argv[argc-1];
// if(!mapped && !unmapped){
// cerr << "Please specify whether you reads are mapped or unmapped" << endl;
// return 1;
// }
// if(mapped && unmapped){
// cerr << "Please specify either mapped or unmapped but not both" << endl;
// return 1;
// }
BamReader reader;
if ( !reader.Open(inbamFile) ) {
cerr << "Could not open input BAM files." << endl;
return 1;
}
vector<RefData> testRefData=reader.GetReferenceData();
SamHeader myHeader = reader.GetHeader();
const RefVector references = reader.GetReferenceData();
string pID = "cutStart";
string pName = "cutStart";
string pCommandLine = "";
for(int i=0; i<(argc); i++) {
pCommandLine += (string(argv[i])+" ");
}
putProgramInHeader(&myHeader,pID,pName,pCommandLine,returnGitHubVersion(string(argv[0]),"."));
BamWriter writer;
if ( !writer.Open(outbamFile, myHeader, references) ) {
cerr << "Could not open output BAM file" << endl;
return 1;
}
BamAlignment al;
// BamAlignment al2;
// bool al2Null=true;
while ( reader.GetNextAlignment(al) ) {
int lengthAl=MIN(al.QueryBases.size(),length);
al.QueryBases = al.QueryBases.substr(lengthAl);
al.Qualities = al.Qualities.substr(lengthAl);
writer.SaveAlignment(al);
}// while ( reader.GetNextAlignment(al) ) {
reader.Close();
//.........这里部分代码省略.........
示例12: main
int main (int argc, char *argv[]) {
// bool mapped =false;
// bool unmapped=false;
int bpToDecrease5=1;
int bpToDecrease3=2;
const string usage=string(string(argv[0])+" [options] input.bam out.bam"+"\n\n"+
"\tThis program takes a BAM file as input and produces\n"+
"\tanother where the putative deaminated bases have\n"+
"\ta base quality score of "+intStringify(baseQualForDeam)+"\n"+
"\tgiven an "+intStringify(offset)+" offset \n"+
"\n"+
"\tOptions:\n"+
"\t\t"+"-n5" +"\t\t\t"+"Decrease the nth bases surrounding the 5' ends (Default:"+stringify(bpToDecrease5)+") "+"\n"+
"\t\t"+"-n3" +"\t\t\t"+"Decrease the nth bases surrounding the 3' ends (Default:"+stringify(bpToDecrease3)+") "+"\n"
);
// "\t"+"-u , --unmapped" +"\n\t\t"+"For an unmapped bam file"+"\n"+
// "\t"+"-m , --mapped" +"\n\t\t"+"For an mapped bam file"+"\n");
if( (argc== 1) ||
(argc== 2 && string(argv[1]) == "-h") ||
(argc== 2 && string(argv[1]) == "-help") ||
(argc== 2 && string(argv[1]) == "--help") ){
cout<<"Usage:"<<endl;
cout<<usage<<endl;
cout<<""<<endl;
return 1;
}
for(int i=1;i<(argc-2);i++){ //all but the last arg
if( string(argv[i]) == "-n5" ){
bpToDecrease5 = destringify<int>(argv[i+1]);
i++;
continue;
}
if( string(argv[i]) == "-n3" ){
bpToDecrease3 = destringify<int>(argv[i+1]);
i++;
continue;
}
cerr<<"Unknown option "<<argv[i] <<" exiting"<<endl;
return 1;
}
if(argc < 3){
cerr<<"Error: Must specify the input and output BAM files";
return 1;
}
string inbamFile =argv[argc-2];
string outbamFile=argv[argc-1];
if(inbamFile == outbamFile){
cerr<<"Input and output files are the same"<<endl;
return 1;
}
// if(!mapped && !unmapped){
// cerr << "Please specify whether you reads are mapped or unmapped" << endl;
// return 1;
// }
// if(mapped && unmapped){
// cerr << "Please specify either mapped or unmapped but not both" << endl;
// return 1;
// }
BamReader reader;
if ( !reader.Open(inbamFile) ) {
cerr << "Could not open input BAM files." << endl;
return 1;
}
vector<RefData> testRefData=reader.GetReferenceData();
SamHeader header = reader.GetHeader();
string pID = "decrQualDeaminated";
string pName = "decrQualDeaminated";
string pCommandLine = "";
for(int i=0;i<(argc);i++){
pCommandLine += (string(argv[i])+" ");
}
putProgramInHeader(&header,pID,pName,pCommandLine);
const RefVector references = reader.GetReferenceData();
BamWriter writer;
if ( !writer.Open(outbamFile, header, references) ) {
cerr << "Could not open output BAM file" << endl;
return 1;
}
BamAlignment al;
// BamAlignment al2;
//.........这里部分代码省略.........
示例13: 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
示例14: IntersectBam
void BedIntersect::IntersectBam(string bamFile) {
// load the "B" bed file into a map so
// that we can easily compare "A" to it for overlaps
_bedB->loadBedFileIntoMap();
// open the BAM file
BamReader reader;
BamWriter writer;
reader.Open(bamFile);
// get header & reference information
string header = reader.GetHeaderText();
RefVector refs = reader.GetReferenceData();
// open a BAM output to stdout if we are writing BAM
if (_bamOutput == true) {
// open our BAM writer
writer.Open("stdout", header, refs, _isUncompressedBam);
}
vector<BED> hits;
// reserve some space
hits.reserve(100);
_bedA->bedType = 6;
BamAlignment bam;
// get each set of alignments for each pair.
while (reader.GetNextAlignment(bam)) {
if (bam.IsMapped()) {
BED a;
a.chrom = refs.at(bam.RefID).RefName;
a.start = bam.Position;
a.end = bam.GetEndPosition(false);
// build the name field from the BAM alignment.
a.name = bam.Name;
if (bam.IsFirstMate()) a.name += "/1";
if (bam.IsSecondMate()) a.name += "/2";
a.score = ToString(bam.MapQuality);
a.strand = "+";
if (bam.IsReverseStrand()) a.strand = "-";
if (_bamOutput == true) {
bool overlapsFound = false;
// treat the BAM alignment as a single "block"
if (_obeySplits == false) {
overlapsFound = FindOneOrMoreOverlap(a);
}
// split the BAM alignment into discrete blocks and
// look for overlaps only within each block.
else {
bool overlapFoundForBlock;
bedVector bedBlocks; // vec to store the discrete BED "blocks" from a
// we don't want to split on "D" ops, hence the "false"
getBamBlocks(bam, refs, bedBlocks, false);
vector<BED>::const_iterator bedItr = bedBlocks.begin();
vector<BED>::const_iterator bedEnd = bedBlocks.end();
for (; bedItr != bedEnd; ++bedItr) {
overlapFoundForBlock = FindOneOrMoreOverlap(a);
if (overlapFoundForBlock == true)
overlapsFound = true;
}
}
if (overlapsFound == true) {
if (_noHit == false)
writer.SaveAlignment(bam);
}
else {
if (_noHit == true) {
writer.SaveAlignment(bam);
}
}
}
else {
// treat the BAM alignment as a single BED "block"
if (_obeySplits == false) {
FindOverlaps(a, hits);
hits.clear();
}
// split the BAM alignment into discrete BED blocks and
// look for overlaps only within each block.
else {
bedVector bedBlocks; // vec to store the discrete BED "blocks" from a
getBamBlocks(bam, refs, bedBlocks, false);
vector<BED>::const_iterator bedItr = bedBlocks.begin();
vector<BED>::const_iterator bedEnd = bedBlocks.end();
for (; bedItr != bedEnd; ++bedItr) {
FindOverlaps(*bedItr, hits);
hits.clear();
}
}
}
}
}
//.........这里部分代码省略.........
示例15: main
int main (int argc, const char *argv[])
{
printf ("------------- bamrealignment --------------\n");
OptArgs opts;
opts.ParseCmdLine(argc, argv);
vector<int> score_vals(4);
string input_bam = opts.GetFirstString ('i', "input", "");
string output_bam = opts.GetFirstString ('o', "output", "");
opts.GetOption(score_vals, "4,-6,-5,-2", 's', "scores");
int clipping = opts.GetFirstInt ('c', "clipping", 2);
bool anchors = opts.GetFirstBoolean ('a', "anchors", true);
int bandwidth = opts.GetFirstInt ('b', "bandwidth", 10);
bool verbose = opts.GetFirstBoolean ('v', "verbose", false);
bool debug = opts.GetFirstBoolean ('d', "debug", false);
int format = opts.GetFirstInt ('f', "format", 1);
int num_threads = opts.GetFirstInt ('t', "threads", 8);
string log_fname = opts.GetFirstString ('l', "log", "");
if (input_bam.empty() or output_bam.empty())
return PrintHelp();
opts.CheckNoLeftovers();
std::ofstream logf;
if (log_fname.size ())
{
logf.open (log_fname.c_str ());
if (!logf.is_open ())
{
fprintf (stderr, "bamrealignment: Failed to open log file %s\n", log_fname.c_str());
return 1;
}
}
BamReader reader;
if (!reader.Open(input_bam)) {
fprintf(stderr, "bamrealignment: Failed to open input file %s\n", input_bam.c_str());
return 1;
}
SamHeader header = reader.GetHeader();
RefVector refs = reader.GetReferenceData();
BamWriter writer;
writer.SetNumThreads(num_threads);
if (format == 1)
writer.SetCompressionMode(BamWriter::Uncompressed);
else
writer.SetCompressionMode(BamWriter::Compressed);
if (!writer.Open(output_bam, header, refs)) {
fprintf(stderr, "bamrealignment: Failed to open output file %s\n", output_bam.c_str());
return 1;
}
// The meat starts here ------------------------------------
if (verbose)
cout << "Verbose option is activated, each alignment will print to screen." << endl
<< " After a read hit RETURN to continue to the next one," << endl
<< " or press q RETURN to quit the program," << endl
<< " or press s Return to silence verbose," << endl
<< " or press c RETURN to continue printing without further prompt." << endl << endl;
unsigned int readcounter = 0;
unsigned int mapped_readcounter = 0;
unsigned int realigned_readcounter = 0;
unsigned int modified_alignment_readcounter = 0;
unsigned int pos_update_readcounter = 0;
unsigned int failed_clip_realigned_readcount = 0;
unsigned int already_perfect_readcount = 0;
unsigned int bad_md_tag_readcount = 0;
unsigned int error_recreate_ref_readcount = 0;
unsigned int error_clip_anchor_readcount = 0;
unsigned int error_sw_readcount = 0;
unsigned int error_unclip_readcount = 0;
unsigned int start_position_shift;
int orig_position;
int new_position;
string md_tag, new_md_tag, input = "x";
vector<CigarOp> new_cigar_data;
vector<MDelement> new_md_data;
bool position_shift = false;
time_t start_time = time(NULL);
Realigner aligner;
aligner.verbose_ = verbose;
aligner.debug_ = debug;
if (!aligner.SetScores(score_vals))
cout << "bamrealignment: Four scores need to be provided: match, mismatch, gap open, gap extend score!" << endl;
aligner.SetAlignmentBandwidth(bandwidth);
//.........这里部分代码省略.........