X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=sequence.cpp;h=6a15eb5f7d23f93272811a975beedc649a0d9ce9;hb=64eee3a595ae53817f52807d7393b22e74e31f56;hp=b59363e13cc92f969145434d1ba8c0fa64573778;hpb=c196b6b4768ccb84955d773ff0f22e4994d1ba7b;p=mothur.git diff --git a/sequence.cpp b/sequence.cpp index b59363e..6a15eb5 100644 --- a/sequence.cpp +++ b/sequence.cpp @@ -7,45 +7,80 @@ * */ -using namespace std; - #include "sequence.hpp" /***********************************************************************/ -Sequence::Sequence() {} +Sequence::Sequence(){ + initialize(); +} /***********************************************************************/ Sequence::Sequence(string newName, string sequence) { + + initialize(); name = newName; if(sequence.find_first_of('-') != string::npos) { setAligned(sequence); } setUnaligned(sequence); + } - //******************************************************************************************************************** -string Sequence::convert2ints() { +Sequence::Sequence(ifstream& fastaFile){ + + initialize(); + fastaFile >> name; + name = name.substr(1); + char c; - if(unaligned == "") { /* need to throw an error */ } + while ((c = fastaFile.get()) != EOF) { if (c == 10){ break; } } // get rest of line if there's any crap there - string processed; + char letter; + string sequence; - for(int i=0;i'){ + fastaFile.putback(letter); + break; + } + else if(isprint(letter)){ + letter = toupper(letter); + if(letter == 'U'){letter = 'T';} + sequence += letter; + } } - return processed; + + if(sequence.find_first_of('-') != string::npos){ // if there are any gaps in the sequence, assume that it is + setAligned(sequence); // an alignment file + } + setUnaligned(sequence); // also set the unaligned sequence file } //******************************************************************************************************************** +void Sequence::initialize(){ + + name = ""; + unaligned = ""; + aligned = ""; + pairwise = ""; + + numBases = 0; + alignmentLength = 0; + isAligned = 0; + startPos = -1; + endPos = -1; + longHomoPolymer = -1; + ambigBases = -1; + +} + +//******************************************************************************************************************** + void Sequence::setName(string seqName) { if(seqName[0] == '>') { name = seqName.substr(1); } else { name = seqName; } @@ -65,13 +100,37 @@ void Sequence::setUnaligned(string sequence){ else { unaligned = sequence; } + numBases = unaligned.length(); } //******************************************************************************************************************** void Sequence::setAligned(string sequence){ + + //if the alignment starts or ends with a gap, replace it with a period to indicate missing data aligned = sequence; + alignmentLength = aligned.length(); + + if(aligned[0] == '-'){ + for(int i=0;i=0;i--){ + if(aligned[i] == '-'){ + aligned[i] = '.'; + } + else{ + break; + } + } + } + isAligned = 1; } //******************************************************************************************************************** @@ -82,6 +141,25 @@ void Sequence::setPairwise(string sequence){ //******************************************************************************************************************** +string Sequence::convert2ints() { + + if(unaligned == "") { /* need to throw an error */ } + + string processed; + + for(int i=0;i aligned.length()) - return unaligned.length(); - return aligned.length(); +int Sequence::getNumBases(){ + return numBases; } //******************************************************************************************************************** void Sequence::printSequence(ostream& out){ - string toPrint = unaligned; - if(aligned.length() > unaligned.length()) - toPrint = aligned; - out << ">" << name << "\n" << toPrint << "\n"; + + out << ">" << name << endl; + if(isAligned){ + out << aligned << endl; + } + else{ + out << unaligned << endl; + } +} + +//******************************************************************************************************************** + +int Sequence::getAlignLength(){ + return alignmentLength; +} + +//******************************************************************************************************************** + +int Sequence::getAmbigBases(){ + if(ambigBases == -1){ + ambigBases = 0; + for(int j=0;j longHomoPolymer){ longHomoPolymer = homoPolymer; } + homoPolymer = 1; + } + } + if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; } + } + return longHomoPolymer; +} + +//******************************************************************************************************************** + +int Sequence::getStartPos(){ + if(endPos == -1){ + for(int j = 0; j < alignmentLength; j++) { + if(aligned[j] != '.'){ + startPos = j + 1; + break; + } + } + } + if(isAligned == 0){ startPos = 1; } + + return startPos; +} + +//******************************************************************************************************************** + +int Sequence::getEndPos(){ + if(endPos == -1){ + for(int j=alignmentLength-1;j>=0;j--){ + if(aligned[j] != '.'){ + endPos = j + 1; + break; + } + } + } + if(isAligned == 0){ endPos = numBases; } + + return endPos; +} + +//******************************************************************************************************************** + +bool Sequence::getIsAligned(){ + return isAligned; +} + +//******************************************************************************************************************** + +void Sequence::reverseComplement(){ + + string temp; + for(int i=numBases-1;i>=0;i--){ + if(unaligned[i] == 'A') { temp += 'T'; } + else if(unaligned[i] == 'T'){ temp += 'A'; } + else if(unaligned[i] == 'G'){ temp += 'C'; } + else if(unaligned[i] == 'C'){ temp += 'G'; } + else { temp += 'N'; } + } + unaligned = temp; + } //********************************************************************************************************************