本文整理汇总了C++中SamFile::WriteRecord方法的典型用法代码示例。如果您正苦于以下问题:C++ SamFile::WriteRecord方法的具体用法?C++ SamFile::WriteRecord怎么用?C++ SamFile::WriteRecord使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SamFile
的用法示例。
在下文中一共展示了SamFile::WriteRecord方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: modifyTags
void modify::modifyTags()
{
assert(samIn.OpenForRead(myFilename.c_str()));
// Read the sam header.
assert(samIn.ReadHeader(samHeader));
SamFile samOut;
SamFile bamOut;
std::string inputType = myFilename.substr(myFilename.find_last_of('.'));
std::string outFileBase = "results/updateTagFrom";
if(inputType == ".bam")
{
outFileBase += "Bam";
}
else
{
outFileBase += "Sam";
}
std::string outFile = outFileBase + ".sam";
assert(samOut.OpenForWrite(outFile.c_str()));
outFile = outFileBase + ".bam";
assert(bamOut.OpenForWrite(outFile.c_str()));
assert(samOut.WriteHeader(samHeader));
assert(bamOut.WriteHeader(samHeader));
int count = 0;
// Read the records.
while(samIn.ReadRecord(samHeader, samRecord))
{
if(count == 0)
{
assert(samRecord.rmTag("MD", 'Z'));
}
else if(count == 2)
{
assert(samRecord.rmTags("XT:A;MD:Z;AB:c;NM:i"));
}
else if(count == 4)
{
assert(samRecord.rmTags("MD:Z,AB:c,NM:i"));
}
assert(bamOut.WriteRecord(samHeader, samRecord));
assert(samOut.WriteRecord(samHeader, samRecord));
++count;
}
}
示例2: execute
//.........这里部分代码省略.........
Logger::gLogger->writeLog("Sorting the indices of %d duplicated records",
myDupList.size());
// sort the indices of duplicate records
std::sort(myDupList.begin(), myDupList.end(),
std::less<uint32_t> ());
// get ready to write the output file by making a second pass
// through the input file
samIn.OpenForRead(inFile.c_str());
samIn.ReadHeader(header);
SamFile samOut;
samOut.OpenForWrite(outFile.c_str());
samOut.WriteHeader(header);
// If we are recalibrating, output the model information.
if(myDoRecab)
{
myRecab.modelFitPrediction(outFile);
}
// an iterator to run through the duplicate indices
int currentDupIndex = 0;
bool moreDups = !myDupList.empty();
// let the user know what we're doing
Logger::gLogger->writeLog("\nWriting %s", outFile.c_str());
// count the duplicate records as a check
uint32_t singleDuplicates(0), pairedDuplicates(0);
// start reading records and writing them out
SamRecord record;
while(samIn.ReadRecord(header, record))
{
uint32_t currentIndex = samIn.GetCurrentRecordCount();
bool foundDup = moreDups &&
(currentIndex == myDupList[currentDupIndex]);
// modify the duplicate flag and write out the record,
// if it's appropriate
int flag = record.getFlag();
if (foundDup)
{
// this record is a duplicate, so mark it.
record.setFlag( flag | 0x400 );
currentDupIndex++;
// increment duplicate counters to verify we found them all
if ( ( ( flag & 0x0001 ) == 0 ) || ( flag & 0x0008 ) )
{ // unpaired or mate unmapped
singleDuplicates++;
}
else
{
pairedDuplicates++;
}
// recalibrate if necessary.
if(myDoRecab)
{
myRecab.processReadApplyTable(record);
}
// write the record if we are not removing duplicates
if (!removeFlag ) samOut.WriteRecord(header, record);
}
else
{
if(myForceFlag)
{
// this is not a duplicate we've identified but we want to
// remove any duplicate marking
record.setFlag( flag & 0xfffffbff ); // unmark duplicate
}
// Not a duplicate, so recalibrate if necessary.
if(myDoRecab)
{
myRecab.processReadApplyTable(record);
}
samOut.WriteRecord(header, record);
}
// Let the user know we're still here
if (verboseFlag && (currentIndex % 100000 == 0)) {
Logger::gLogger->writeLog("recordCount=%u", currentIndex);
}
}
// We're done. Close the files and print triumphant messages.
samIn.Close();
samOut.Close();
Logger::gLogger->writeLog("Successfully %s %u unpaired and %u paired duplicate reads",
removeFlag ? "removed" : "marked" ,
singleDuplicates,
pairedDuplicates/2);
Logger::gLogger->writeLog("\nDedup_LowMem complete!");
return 0;
}
示例3: execute
//.........这里部分代码省略.........
}
if(outFile == "")
{
usage();
inputParameters.Status();
// In file was not specified but it is mandatory.
std::cerr << "--out is a mandatory argument, "
<< "but was not specified" << std::endl;
return(-1);
}
if(params)
{
inputParameters.Status();
}
// Open the input file for reading.
SamFile samIn;
samIn.OpenForRead(inFile);
// Open the output file for writing.
SamFile samOut;
samOut.OpenForWrite(outFile);
// Read the sam header.
SamFileHeader samHeader;
samIn.ReadHeader(samHeader);
// Write the sam header.
samOut.WriteHeader(samHeader);
SamRecord samRecord;
// Set returnStatus to success. It will be changed to the
// failure reason if any of the writes or updates fail.
SamStatus::Status returnStatus = SamStatus::SUCCESS;
// Keep reading records until ReadRecord returns false.
while(samIn.ReadRecord(samHeader, samRecord))
{
// Update the cigar & position.
if(cigar)
{
if(!updateCigar(samRecord))
{
// Failed to update the cigar & position.
fprintf(stderr, "%s\n", samIn.GetStatusMessage());
returnStatus = samIn.GetStatus();
}
}
if(qual)
{
if(!updateQual(samRecord))
{
// Failed to update the quality.
fprintf(stderr, "%s\n", samIn.GetStatusMessage());
returnStatus = samIn.GetStatus();
}
}
if(rmBQ)
{
if(!removeBQ(samRecord))
{
// Failed to remove BQ.
fprintf(stderr, "%s\n", samIn.GetStatusMessage());
returnStatus = samIn.GetStatus();
}
}
if(rmTags != "")
{
if(!samRecord.rmTags(rmTags.c_str()))
{
// Failed to remove the specified tags.
fprintf(stderr, "%s\n", samIn.GetStatusMessage());
returnStatus = samIn.GetStatus();
}
}
// Successfully read a record from the file, so write it.
if(!samOut.WriteRecord(samHeader, samRecord))
{
// Failed to write a record.
fprintf(stderr, "%s\n", samOut.GetStatusMessage());
returnStatus = samOut.GetStatus();
}
}
std::cerr << std::endl << "Number of records read = " <<
samIn.GetCurrentRecordCount() << std::endl;
std::cerr << "Number of records written = " <<
samOut.GetCurrentRecordCount() << std::endl;
// Since the reads were successful, return the status based
// on the status of the writes. If any failed, return
// their failure status.
return(returnStatus);
}
示例4: execute
//.........这里部分代码省略.........
refPtr = new GenomeSequence(refFile);
}
SamRecord::SequenceTranslation translation;
if((useBases) && (refPtr != NULL))
{
translation = SamRecord::BASES;
}
else if((useEquals) && (refPtr != NULL))
{
translation = SamRecord::EQUAL;
}
else
{
useOrigSeq = true;
translation = SamRecord::NONE;
}
if(params)
{
inputParameters.Status();
}
// Open the input file for reading.
SamFile samIn;
if(recover) samIn.setAttemptRecovery(true);
samIn.OpenForRead(inFile);
// Open the output file for writing.
SamFile samOut;
samOut.OpenForWrite(outFile);
samOut.SetWriteSequenceTranslation(translation);
samOut.SetReference(refPtr);
// Read the sam header.
SamFileHeader samHeader;
samIn.ReadHeader(samHeader);
// Write the sam header.
samOut.WriteHeader(samHeader);
SamRecord samRecord;
// Set returnStatus to success. It will be changed
// to the failure reason if any of the writes fail.
SamStatus::Status returnStatus = SamStatus::SUCCESS;
while(1) {
try {
// Keep reading records until ReadRecord returns false.
while(samIn.ReadRecord(samHeader, samRecord))
{
// left shift if necessary.
if(lshift)
{
samRecord.shiftIndelsLeft();
}
// Successfully read a record from the file, so write it.
if(!samOut.WriteRecord(samHeader, samRecord))
{
// Failed to write a record.
fprintf(stderr, "%s\n", samOut.GetStatusMessage());
returnStatus = samOut.GetStatus();
}
}
break;
} catch (std::runtime_error e) {
std::cerr << "Caught runtime error: " << e.what() << "\n";
if(!recover) {
std::cerr << "Corrupted BAM file detected - consider using --recover option.\n";
break;
}
std::cerr << "Attempting to resync at next good BGZF block and BAM record.\n";
// XXX need to resync SamFile stream here
bool rc = samIn.attemptRecoverySync(checkSignature, SIGNATURE_LENGTH);
if(rc) {
std::cerr << "Successful resync - some data lost.\n";
continue; // succeeded
}
std::cerr << "Failed to re-sync on data stream.\n";
break; // failed to resync
}
}
std::cerr << std::endl << "Number of records read = " <<
samIn.GetCurrentRecordCount() << std::endl;
std::cerr << "Number of records written = " <<
samOut.GetCurrentRecordCount() << std::endl;
if(refPtr != NULL)
{
delete(refPtr);
}
// Since the reads were successful, return the status based
// on the status of the writes. If any failed, return
// their failure status.
return(returnStatus);
}
示例5: main
//.........这里部分代码省略.........
else if ( static_cast<int>(fastaFile.vnSequenceLengths[i]) != atoi(pSamHeaderRecord->getTagValue("LN")) ) {
gpLogger->error("SequenceLength is not identical between fasta and input BAM file");
}
else {
if ( !sAS.empty() )
samHeader.setSQTag("AS",sAS.c_str(),fastaFile.vsSequenceNames[i].c_str());
samHeader.setSQTag("M5",fastaFile.vsMD5sums[i].c_str(),fastaFile.vsSequenceNames[i].c_str());
if ( !sUR.empty() )
samHeader.setSQTag("UR",sUR.c_str(),fastaFile.vsSequenceNames[i].c_str());
if ( !sSP.empty() )
samHeader.setSQTag("SP",sSP.c_str(),fastaFile.vsSequenceNames[i].c_str());
}
}
gpLogger->write_log("Finished checking the consistency of SQ tags");
}
else {
gpLogger->write_log("Skipped checking the consistency of SQ tags");
}
// go over the headers again,
// assuming order of HD, SQ, RG, PG, and put proper tags at the end of the original tags
gpLogger->write_log("Creating the header of new output file");
//SamFileHeader outHeader;
samHeader.resetHeaderRecordIter();
for(unsigned int i=0; i < vsHDHeaders.size(); ++i) {
samHeader.addHeaderLine(vsHDHeaders[i].c_str());
}
/*
for(int i=0; i < fastaFile.vsSequenceNames.size(); ++i) {
std::string s("@SQ\tSN:");
char buf[1024];
s += fastaFile.vsSequenceNames[i];
sprintf(buf,"\tLN:%d",fastaFile.vnSequenceLengths[i]);
s += buf;
if ( !sAS.empty() ) {
sprintf(buf,"\tAS:%s",sAS.c_str());
s += buf;
}
if ( !sUR.empty() ) {
sprintf(buf,"\tUR:%s",sUR.c_str());
s += buf;
}
sprintf(buf,"\tM5:%s",fastaFile.vsMD5sums[i].c_str());
s += buf;
if ( !sSP.empty() ) {
sprintf(buf,"\tSP:%s",sSP.c_str());
s += buf;
}
outHeader.addHeaderLine(s.c_str());
}*/
for(unsigned int i=0; i < vsRGHeaders.size(); ++i) {
samHeader.addHeaderLine(vsRGHeaders[i].c_str());
}
for(unsigned int i=0; i < vsPGHeaders.size(); ++i) {
samHeader.addHeaderLine(vsPGHeaders[i].c_str());
}
samOut.WriteHeader(samHeader);
gpLogger->write_log("Adding %d HD, %d RG, and %d PG headers",vsHDHeaders.size(), vsRGHeaders.size(), vsPGHeaders.size());
gpLogger->write_log("Finished writing output headers");
// parse RG tag and get RG ID to append
std::string sRGID;
if ( ! vsRGHeaders.empty() ) {
std::vector<std::string> tokens;
FastaFile::tokenizeString( vsRGHeaders[0].c_str(), tokens );
for(unsigned int i=0; i < tokens.size(); ++i) {
if ( tokens[i].find("ID:") == 0 ) {
sRGID = tokens[i].substr(3);
}
}
}
gpLogger->write_log("Writing output BAM file");
SamRecord samRecord;
while (samIn.ReadRecord(samHeader, samRecord) == true) {
if ( !sRGID.empty() ) {
if ( samRecord.addTag("RG",'Z',sRGID.c_str()) == false ) {
gpLogger->error("Failed to add a RG tag %s",sRGID.c_str());
}
// temporary code added
if ( strncmp(samRecord.getReadName(),"seqcore_",8) == 0 ) {
char buf[1024];
sprintf(buf,"UM%s",samRecord.getReadName()+8);
samRecord.setReadName(buf);
}
}
samOut.WriteRecord(samHeader, samRecord);
//if ( samIn.GetCurrentRecordCount() == 1000 ) break;
}
samOut.Close();
gpLogger->write_log("Successfully written %d records",samIn.GetCurrentRecordCount());
delete gpLogger;
return 0;
}
示例6: process
bool BamProcessor::process ()
{
if (!infile_.ReadHeader (sam_header_))
ers << "Unable to read SAM header" << Throw;
else
info << "Header read" << std::endl;
if (outfile_.IsOpen ())
{
if (!outfile_.WriteHeader (sam_header_))
ers << "Unable to write header data" << Throw;
else
info << "Header written" << std::endl;
}
// set up signal handlers
sighandler_t sighandler_int, sighandler_term, sighandler_hup;
// set INT handler to int_handler if interrupting is not disabled allready
if ((sighandler_int = signal (SIGINT, int_handler)) == SIG_IGN)
signal (SIGINT, SIG_IGN), sighandler_int = NULL;
// set HUP handler to nothing
sighandler_hup = signal (SIGHUP, SIG_IGN);
// set TERM handler to int_handler if terminating is not disabled allready
if ((sighandler_term = signal (SIGTERM, int_handler)) == SIG_IGN)
signal (SIGTERM, SIG_IGN), sighandler_term = NULL;
begtime_ = time (NULL);
while (!infile_.IsEOF () && !interrupted)
{
if (limit_ && proc_cnt_ >= limit_)
{
info << limit_ << " records processed. Limit reached." << std::endl;
break;
}
if (read_cnt_ == skip_)
timer_.mark ();
infile_.ReadRecord (sam_header_, rec_);
++ read_cnt_;
if (read_cnt_-1 >= skip_)
{
if (!processRecord ())
++ fail_cnt_;
++ proc_cnt_;
if (outfile_.IsOpen ())
outfile_.WriteRecord (sam_header_, rec_);
}
if (timer_ ())
{
info << "\r" << read_cnt_;
if (proc_cnt_ != read_cnt_)
info << " rd " << proc_cnt_;
info << " pr ";
if (realigned_cnt_ != proc_cnt_)
info << realigned_cnt_ << " al (" << (double (realigned_cnt_) * 100 / proc_cnt_) << "%) ";
info << modified_cnt_ << " mod (" << (double (modified_cnt_) * 100 / proc_cnt_) << "%) ";
if (pos_adjusted_cnt_)
info << pos_adjusted_cnt_ << " sh (" << (double (pos_adjusted_cnt_) * 100 / modified_cnt_) << "% mod) ";
info << "in " << timer_.tot_elapsed () << " sec (" << std::setprecision (3) << std::fixed << timer_.speed () << " r/s)" << std::flush;
}
}
if (interrupted)
{
errlog << "\nProcessing interrupted by ";
switch (signal_received)
{
case SIGTERM:
errlog << "TERM signal";
break;
case SIGINT:
errlog << "user's request";
break;
default:
errlog << "receipt of signal " << signal_received;
}
errlog << std::endl;
}
// restore signal handlers
if (sighandler_term)
signal (SIGTERM, sighandler_term);
if (sighandler_int)
signal (SIGINT, sighandler_int);
if (sighandler_hup)
signal (SIGHUP, sighandler_hup);
return 0;
}
示例7: execute
//.........这里部分代码省略.........
// Keep reading records until ReadRecord returns false.
while(samIn.ReadRecord(samHeader, samRecord)) {
// Successfully read a record from the file, so write it.
strcpy(seq,samRecord.getSequence());
strcpy(qual,samRecord.getQuality());
// Number of bases to trim from the left/right,
// set based on ignoreStrand flag and strand info.
int trimLeft = numTrimBaseL;
int trimRight = numTrimBaseR;
if(!ignoreStrand)
{
if(SamFlag::isReverse(samRecord.getFlag()))
{
// We are reversing the reverse reads,
// so swap the left & right trim counts.
trimRight = numTrimBaseL;
trimLeft = numTrimBaseR;
}
}
len = strlen(seq);
// Do not trim if sequence is '*'
if ( strcmp(seq, "*") != 0 ) {
bool qualValue = true;
if(strcmp(qual, "*") == 0)
{
qualValue = false;
}
int qualLen = strlen(qual);
if ( (qualLen != len) && qualValue ) {
fprintf(stderr,"ERROR: Sequence and Quality have different length\n");
return(-1);
}
if ( len < (trimLeft + trimRight) ) {
// Read Length is less than the total number of bases to trim,
// so trim the entire read.
for(i=0; i < len; ++i) {
seq[i] = 'N';
if ( qualValue ) {
qual[i] = '!';
}
}
}
else
{
// Read Length is larger than the total number of bases to trim,
// so trim from the left, then from the right.
for(i=0; i < trimLeft; ++i)
{
// Trim the bases from the left.
seq[i] = 'N';
if ( qualValue )
{
qual[i] = '!';
}
}
for(i = 0; i < trimRight; i++)
{
seq[len-i-1] = 'N';
if(qualValue)
{
qual[len-i-1] = '!';
}
}
}
samRecord.setSequence(seq);
samRecord.setQuality(qual);
}
if(!samOut.WriteRecord(samHeader, samRecord)) {
// Failed to write a record.
fprintf(stderr, "Failure in writing record %s\n", samOut.GetStatusMessage());
return(-1);
}
}
if(samIn.GetStatus() != SamStatus::NO_MORE_RECS)
{
// Failed to read a record.
fprintf(stderr, "%s\n", samIn.GetStatusMessage());
}
std::cerr << std::endl << "Number of records read = " <<
samIn.GetCurrentRecordCount() << std::endl;
std::cerr << "Number of records written = " <<
samOut.GetCurrentRecordCount() << std::endl;
if(samIn.GetStatus() != SamStatus::NO_MORE_RECS)
{
// Failed reading a record.
return(samIn.GetStatus());
}
// Since the reads were successful, return the status based
samIn.Close();
samOut.Close();
return 0;
}
示例8: execute
//.........这里部分代码省略.........
}
if(myRefName.Length() != 0 && bed.Length() != 0)
{
std::cerr << "Can't specify both refName and bed" << std::endl;
inputParameters.Status();
return(-1);
}
if(!bed.IsEmpty())
{
myBedFile = ifopen(bed, "r");
}
if(params)
{
inputParameters.Status();
}
// Open the file for reading.
mySamIn.OpenForRead(inFile);
// Open the output file for writing.
SamFile samOut;
samOut.OpenForWrite(outFile);
// Open the bam index file for reading if a region was specified.
if((myRefName.Length() != 0) || (myRefID != UNSET_REF) || (myBedFile != NULL))
{
mySamIn.ReadBamIndex(indexFile);
}
// Read & write the sam header.
mySamIn.ReadHeader(mySamHeader);
samOut.WriteHeader(mySamHeader);
// Read the sam records.
SamRecord samRecord;
// Track the status.
int numSectionRecords = 0;
// Set returnStatus to success. It will be changed
// to the failure reason if any of the writes fail.
SamStatus::Status returnStatus = SamStatus::SUCCESS;
while(getNextSection())
{
// Keep reading records until they aren't anymore.
while(mySamIn.ReadRecord(mySamHeader, samRecord))
{
if(!readName.IsEmpty())
{
// Check for readname.
if(strcmp(samRecord.getReadName(), readName.c_str()) != 0)
{
// not a matching read name, so continue to the next record.
continue;
}
}
// Check to see if the read has already been processed.
if(myPrevEnd != UNSPECIFIED_INT)
{
// Because we already know that the bed was sorted,
// we know that the previous section started before
// this one, so if the previous end is greater than
// this record's end position we know that it
// was already written in the previous section.
// Note: can't be equal to the previous end since
// the end range was exclusive, while
// get0BasedAlignmentEnd is inclusive.
// myPrevEnd is reset by getNextSection when a new
// chromosome is hit.
if(samRecord.get0BasedAlignmentEnd() < myPrevEnd)
{
// This record was already written.
continue;
}
}
// Shift left if applicable.
if(lshift)
{
samRecord.shiftIndelsLeft();
}
// Successfully read a record from the file, so write it.
samOut.WriteRecord(mySamHeader, samRecord);
++numSectionRecords;
}
myWroteReg = true;
}
if(myBedFile != NULL)
{
ifclose(myBedFile);
}
std::cerr << "Wrote " << outFile << " with " << numSectionRecords
<< " records.\n";
return(returnStatus);
}