From: barnett Date: Tue, 8 Dec 2009 18:57:41 +0000 (+0000) Subject: Fixed some signed/unsigned int issues X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=0d00f380644c78a321e8891562bf165317af10b4;p=bamtools.git Fixed some signed/unsigned int issues git-svn-id: svn+ssh://gene.bc.edu/home/subversion/Derek/BamTools/trunk@32 9efb377e-2e27-44b9-b91a-ec4abb80ed8b --- diff --git a/BamAux.h b/BamAux.h index d5510d1..eec5556 100644 --- a/BamAux.h +++ b/BamAux.h @@ -230,9 +230,9 @@ struct CigarOp { struct RefData { // data members - std::string RefName; // Name of reference sequence - unsigned int RefLength; // Length of reference sequence - bool RefHasAlignments; // True if BAM file contains alignments mapped to reference sequence + std::string RefName; // Name of reference sequence + int RefLength; // Length of reference sequence + bool RefHasAlignments; // True if BAM file contains alignments mapped to reference sequence // constructor RefData(void) : RefLength(0) diff --git a/BamReader.cpp b/BamReader.cpp index 386a485..8dc2eb0 100644 --- a/BamReader.cpp +++ b/BamReader.cpp @@ -40,9 +40,9 @@ struct BamReader::BamReaderPrivate { string IndexFilename; // user-specified region values - bool IsRegionSpecified; - int CurrentRefID; - unsigned int CurrentLeft; + bool IsRegionSpecified; + int CurrentRefID; + int CurrentLeft; // BAM character constants const char* DNA_LOOKUP; @@ -60,7 +60,7 @@ struct BamReader::BamReaderPrivate { // flie operations void Close(void); - bool Jump(int refID, unsigned int position = 0); + bool Jump(int refID, int position = 0); void Open(const string& filename, const string& indexFilename = ""); bool Rewind(void); @@ -83,11 +83,11 @@ struct BamReader::BamReaderPrivate { // *** reading alignments and auxiliary data *** // // calculate bins that overlap region ( left to reference end for now ) - int BinsFromRegion(int, unsigned int, uint16_t[MAX_BIN]); + int BinsFromRegion(int refID, int left, uint16_t[MAX_BIN]); // calculates alignment end position based on starting position and provided CIGAR operations - unsigned int CalculateAlignmentEnd(const unsigned int& position, const std::vector& cigarData); + int CalculateAlignmentEnd(const int& position, const std::vector& cigarData); // calculate file offset for first alignment chunk overlapping 'left' - int64_t GetOffset(int refID, unsigned int left); + int64_t GetOffset(int refID, int left); // checks to see if alignment overlaps current region bool IsOverlap(BamAlignment& bAlignment); // retrieves header text from BAM file @@ -134,7 +134,7 @@ BamReader::~BamReader(void) { // file operations void BamReader::Close(void) { d->Close(); } -bool BamReader::Jump(int refID, unsigned int position) { return d->Jump(refID, position); } +bool BamReader::Jump(int refID, int position) { return d->Jump(refID, position); } void BamReader::Open(const string& filename, const string& indexFilename) { d->Open(filename, indexFilename); } bool BamReader::Rewind(void) { return d->Rewind(); } @@ -171,11 +171,11 @@ BamReader::BamReaderPrivate::~BamReaderPrivate(void) { } // calculate bins that overlap region ( left to reference end for now ) -int BamReader::BamReaderPrivate::BinsFromRegion(int refID, unsigned int left, uint16_t list[MAX_BIN]) { +int BamReader::BamReaderPrivate::BinsFromRegion(int refID, int left, uint16_t list[MAX_BIN]) { // get region boundaries - uint32_t begin = left; - uint32_t end = References.at(refID).RefLength - 1; + int32_t begin = left; + int32_t end = References.at(refID).RefLength - 1; // initialize list, bin '0' always a valid bin int i = 0; @@ -238,7 +238,7 @@ bool BamReader::BamReaderPrivate::BuildIndex(void) { // if lastCoordinate greater than BAM position - file not sorted properly else if ( lastCoordinate > bAlignment.Position ) { printf("BAM file not properly sorted:\n"); - printf("Alignment %s : %u > %u on reference (id = %d)", bAlignment.Name.c_str(), lastCoordinate, bAlignment.Position, bAlignment.RefID); + printf("Alignment %s : %d > %d on reference (id = %d)", bAlignment.Name.c_str(), lastCoordinate, bAlignment.Position, bAlignment.RefID); exit(1); } @@ -324,10 +324,10 @@ bool BamReader::BamReaderPrivate::BuildIndex(void) { } // calculates alignment end position based on starting position and provided CIGAR operations -unsigned int BamReader::BamReaderPrivate::CalculateAlignmentEnd(const unsigned int& position, const vector& cigarData) { +int BamReader::BamReaderPrivate::CalculateAlignmentEnd(const int& position, const vector& cigarData) { // initialize alignment end to starting position - unsigned int alignEnd = position; + int alignEnd = position; // iterate over cigar operations vector::const_iterator cigarIter = cigarData.begin(); @@ -361,9 +361,12 @@ bool BamReader::BamReaderPrivate::CreateIndex(void) { // clear out index ClearIndex(); + // build (& save) index from BAM file bool ok = true; ok &= BuildIndex(); ok &= WriteIndex(); + + // return success/fail return ok; } @@ -406,7 +409,7 @@ bool BamReader::BamReaderPrivate::GetNextAlignment(BamAlignment& bAlignment) { } // calculate closest indexed file offset for region specified -int64_t BamReader::BamReaderPrivate::GetOffset(int refID, unsigned int left) { +int64_t BamReader::BamReaderPrivate::GetOffset(int refID, int left) { // calculate which bins overlap this region uint16_t* bins = (uint16_t*)calloc(MAX_BIN, 2); @@ -508,14 +511,14 @@ bool BamReader::BamReaderPrivate::IsOverlap(BamAlignment& bAlignment) { if ( bAlignment.RefID != CurrentRefID ) { return false; } // read starts after left boundary - if ( bAlignment.Position >= (int32_t)CurrentLeft) { return true; } + if ( bAlignment.Position >= CurrentLeft) { return true; } // return whether alignment end overlaps left boundary return ( CalculateAlignmentEnd(bAlignment.Position, bAlignment.CigarData) >= CurrentLeft ); } // jumps to specified region(refID, leftBound) in BAM file, returns success/fail -bool BamReader::BamReaderPrivate::Jump(int refID, unsigned int position) { +bool BamReader::BamReaderPrivate::Jump(int refID, int position) { // if data exists for this reference and position is valid if ( References.at(refID).RefHasAlignments && (position <= References.at(refID).RefLength) ) { @@ -833,7 +836,7 @@ void BamReader::BamReaderPrivate::LoadReferenceData(void) { // get reference name and reference sequence length mBGZF.Read(refName, refNameLength); mBGZF.Read(buffer, 4); - const unsigned int refLength = BgzfData::UnpackUnsignedInt(buffer); + const int refLength = BgzfData::UnpackSignedInt(buffer); // store data for reference RefData aReference; diff --git a/BamReader.h b/BamReader.h index 2587b00..6332cde 100644 --- a/BamReader.h +++ b/BamReader.h @@ -39,7 +39,7 @@ class BamReader { // close BAM file void Close(void); // performs random-access jump to reference, position - bool Jump(int refID, unsigned int position = 0); + bool Jump(int refID, int position = 0); // opens BAM file (and optional BAM index file, if provided) void Open(const std::string& filename, const std::string& indexFilename = ""); // returns file pointer to beginning of alignments diff --git a/BamTrimMain.cpp b/BamTrimMain.cpp index 7349115..5b39d76 100644 --- a/BamTrimMain.cpp +++ b/BamTrimMain.cpp @@ -64,8 +64,8 @@ int main(int argc, char* argv[]) { } // convert boundary arguments to numeric values - unsigned int leftBound = (unsigned int) atoi( leftBound_str.c_str() ); - unsigned int rightBound = (unsigned int) atoi( rightBound_str.c_str() ); + int leftBound = (int) atoi( leftBound_str.c_str() ); + int rightBound = (int) atoi( rightBound_str.c_str() ); // attempt jump to range of interest if ( reader.Jump(refID, leftBound) ) {