5 * Created by Pat Schloss on 12/15/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
10 #include "sequence.hpp"
12 /***********************************************************************/
14 m = MothurOut::getInstance();
17 /***********************************************************************/
18 Sequence::Sequence(string newName, string sequence) {
20 m = MothurOut::getInstance();
24 //setUnaligned removes any gap characters for us
25 setUnaligned(sequence);
29 m->errorOut(e, "Sequence", "Sequence");
33 /***********************************************************************/
34 Sequence::Sequence(string newName, string sequence, string justUnAligned) {
36 m = MothurOut::getInstance();
40 //setUnaligned removes any gap characters for us
41 setUnaligned(sequence);
44 m->errorOut(e, "Sequence", "Sequence");
49 //********************************************************************************************************************
50 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
51 Sequence::Sequence(istringstream& fastaString){
53 m = MothurOut::getInstance();
58 if (name.length() != 0) {
60 name = name.substr(1);
64 while ((name[0] == '#') && fastaString) {
65 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
66 sequence = getCommentString(fastaString);
70 name = name.substr(1);
77 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
80 sequence = getSequenceString(fastaString, numAmbig);
83 //setUnaligned removes any gap characters for us
84 setUnaligned(sequence);
86 if ((numAmbig / (float) numBases) > 0.25) { m->mothurOut("[WARNING]: We found more than 25% of the bases in sequence " + name + " to be ambiguous. Mothur is not setup to process protein sequences."); m->mothurOutEndLine(); }
88 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaString.tellg()) + ". Blank name."); m->mothurOutEndLine(); }
92 m->errorOut(e, "Sequence", "Sequence");
96 //********************************************************************************************************************
97 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
98 Sequence::Sequence(istringstream& fastaString, string JustUnaligned){
100 m = MothurOut::getInstance();
105 if (name.length() != 0) {
107 name = name.substr(1);
111 while ((name[0] == '#') && fastaString) {
112 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
113 sequence = getCommentString(fastaString);
117 name = name.substr(1);
124 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
127 sequence = getSequenceString(fastaString, numAmbig);
129 //setUnaligned removes any gap characters for us
130 setUnaligned(sequence);
132 if ((numAmbig / (float) numBases) > 0.25) { m->mothurOut("[WARNING]: We found more than 25% of the bases in sequence " + name + " to be ambiguous. Mothur is not setup to process protein sequences."); m->mothurOutEndLine(); }
134 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaString.tellg()) + ". Blank name."); m->mothurOutEndLine(); }
137 catch(exception& e) {
138 m->errorOut(e, "Sequence", "Sequence");
144 //********************************************************************************************************************
145 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
146 Sequence::Sequence(ifstream& fastaFile){
148 m = MothurOut::getInstance();
152 if (name.length() != 0) {
154 name = name.substr(1);
159 while ((name[0] == '#') && fastaFile) {
160 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
161 sequence = getCommentString(fastaFile);
165 name = name.substr(1);
173 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
176 sequence = getSequenceString(fastaFile, numAmbig);
178 setAligned(sequence);
179 //setUnaligned removes any gap characters for us
180 setUnaligned(sequence);
182 if ((numAmbig / (float) numBases) > 0.25) { m->mothurOut("[WARNING]: We found more than 25% of the bases in sequence " + name + " to be ambiguous. Mothur is not setup to process protein sequences."); m->mothurOutEndLine(); }
184 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); }
187 catch(exception& e) {
188 m->errorOut(e, "Sequence", "Sequence");
192 //********************************************************************************************************************
193 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
194 Sequence::Sequence(ifstream& fastaFile, string& extraInfo, bool getInfo){
196 m = MothurOut::getInstance();
201 if (name.length() != 0) {
203 name = name.substr(1);
208 while ((name[0] == '#') && fastaFile) {
209 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
210 sequence = getCommentString(fastaFile);
214 name = name.substr(1);
221 //read info after sequence name
222 while (!fastaFile.eof()) {
223 char c = fastaFile.get();
224 if (c == 10 || c == 13){ break; }
229 sequence = getSequenceString(fastaFile, numAmbig);
231 setAligned(sequence);
232 //setUnaligned removes any gap characters for us
233 setUnaligned(sequence);
235 if ((numAmbig / (float) numBases) > 0.25) { m->mothurOut("[WARNING]: We found more than 25% of the bases in sequence " + name + " to be ambiguous. Mothur is not setup to process protein sequences."); m->mothurOutEndLine(); }
237 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); }
240 catch(exception& e) {
241 m->errorOut(e, "Sequence", "Sequence");
245 //********************************************************************************************************************
246 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
247 Sequence::Sequence(ifstream& fastaFile, string JustUnaligned){
249 m = MothurOut::getInstance();
253 if (name.length() != 0) {
254 name = name.substr(1);
258 while ((name[0] == '#') && fastaFile) {
259 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
260 sequence = getCommentString(fastaFile);
264 name = name.substr(1);
272 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
275 sequence = getSequenceString(fastaFile, numAmbig);
277 //setUnaligned removes any gap characters for us
278 setUnaligned(sequence);
280 if ((numAmbig / (float) numBases) > 0.25) { m->mothurOut("[WARNING]: We found more than 25% of the bases in sequence " + name + " to be ambiguous. Mothur is not setup to process protein sequences."); m->mothurOutEndLine(); }
282 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); }
285 catch(exception& e) {
286 m->errorOut(e, "Sequence", "Sequence");
291 //********************************************************************************************************************
292 string Sequence::getSequenceString(ifstream& fastaFile, int& numAmbig) {
295 string sequence = "";
299 letter= fastaFile.get();
301 fastaFile.putback(letter);
304 else if(isprint(letter)){
305 letter = toupper(letter);
306 if(letter == 'U'){letter = 'T';}
307 if(letter != '.' && letter != '-' && letter != 'A' && letter != 'T' && letter != 'G' && letter != 'C' && letter != 'N'){
317 catch(exception& e) {
318 m->errorOut(e, "Sequence", "getSequenceString");
322 //********************************************************************************************************************
323 //comment can contain '>' so we need to account for that
324 string Sequence::getCommentString(ifstream& fastaFile) {
327 string sequence = "";
330 letter=fastaFile.get();
331 if((letter == '\r') || (letter == '\n')){
332 m->gobble(fastaFile); //in case its a \r\n situation
339 catch(exception& e) {
340 m->errorOut(e, "Sequence", "getCommentString");
344 //********************************************************************************************************************
345 string Sequence::getSequenceString(istringstream& fastaFile, int& numAmbig) {
348 string sequence = "";
351 while(!fastaFile.eof()){
352 letter= fastaFile.get();
355 fastaFile.putback(letter);
358 else if(isprint(letter)){
359 letter = toupper(letter);
360 if(letter == 'U'){letter = 'T';}
361 if(letter != '.' && letter != '-' && letter != 'A' && letter != 'T' && letter != 'G' && letter != 'C' && letter != 'N'){
371 catch(exception& e) {
372 m->errorOut(e, "Sequence", "getSequenceString");
376 //********************************************************************************************************************
377 //comment can contain '>' so we need to account for that
378 string Sequence::getCommentString(istringstream& fastaFile) {
381 string sequence = "";
384 letter=fastaFile.get();
385 if((letter == '\r') || (letter == '\n')){
386 m->gobble(fastaFile); //in case its a \r\n situation
393 catch(exception& e) {
394 m->errorOut(e, "Sequence", "getCommentString");
398 //********************************************************************************************************************
400 void Sequence::initialize(){
412 longHomoPolymer = -1;
417 //********************************************************************************************************************
419 void Sequence::setName(string seqName) {
420 if(seqName[0] == '>') { name = seqName.substr(1); }
421 else { name = seqName; }
424 //********************************************************************************************************************
426 void Sequence::setUnaligned(string sequence){
428 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
430 for(int j=0;j<sequence.length();j++) {
431 if(isalpha(sequence[j])) { temp += sequence[j]; }
436 unaligned = sequence;
438 numBases = unaligned.length();
442 //********************************************************************************************************************
444 void Sequence::setAligned(string sequence){
446 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
448 alignmentLength = aligned.length();
449 setUnaligned(sequence);
451 if(aligned[0] == '-'){
452 for(int i=0;i<alignmentLength;i++){
453 if(aligned[i] == '-'){
460 for(int i=alignmentLength-1;i>=0;i--){
461 if(aligned[i] == '-'){
472 //********************************************************************************************************************
474 void Sequence::setPairwise(string sequence){
478 //********************************************************************************************************************
480 string Sequence::convert2ints() {
482 if(unaligned == "") { /* need to throw an error */ }
486 for(int i=0;i<unaligned.length();i++) {
487 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
488 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
489 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
490 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
491 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
492 else { processed += '4'; }
497 //********************************************************************************************************************
499 string Sequence::getName(){
503 //********************************************************************************************************************
505 string Sequence::getAligned(){
506 if(isAligned == 0) { return unaligned; }
507 else { return aligned; }
510 //********************************************************************************************************************
512 string Sequence::getInlineSeq(){
513 return name + '\t' + aligned;
517 //********************************************************************************************************************
519 string Sequence::getPairwise(){
523 //********************************************************************************************************************
525 string Sequence::getUnaligned(){
529 //********************************************************************************************************************
531 int Sequence::getNumBases(){
535 //********************************************************************************************************************
537 void Sequence::printSequence(ostream& out){
539 out << ">" << name << endl;
541 out << aligned << endl;
544 out << unaligned << endl;
548 //********************************************************************************************************************
550 int Sequence::getAlignLength(){
551 return alignmentLength;
554 //********************************************************************************************************************
556 int Sequence::getAmbigBases(){
557 if(ambigBases == -1){
559 for(int j=0;j<numBases;j++){
560 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
569 //********************************************************************************************************************
571 void Sequence::removeAmbigBases(){
573 for(int j=0;j<alignmentLength;j++){
574 if(aligned[j] != 'A' && aligned[j] != 'T' && aligned[j] != 'G' && aligned[j] != 'C'){
578 setUnaligned(aligned);
581 //********************************************************************************************************************
583 int Sequence::getLongHomoPolymer(){
584 if(longHomoPolymer == -1){
587 for(int j=1;j<numBases;j++){
588 if(unaligned[j] == unaligned[j-1]){
592 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
596 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
598 return longHomoPolymer;
601 //********************************************************************************************************************
603 int Sequence::getStartPos(){
605 for(int j = 0; j < alignmentLength; j++) {
606 if(aligned[j] != '.'){
612 if(isAligned == 0){ startPos = 1; }
617 //********************************************************************************************************************
619 void Sequence::padToPos(int start){
621 for(int j = startPos-1; j < start-1; j++) {
627 //********************************************************************************************************************
629 int Sequence::filterToPos(int start){
631 if (start > aligned.length()) { start = aligned.length(); m->mothurOut("[ERROR]: start to large.\n"); }
633 for(int j = 0; j < start-1; j++) {
637 //things like ......----------AT become ................AT
638 for(int j = start-1; j < aligned.length(); j++) {
639 if (isalpha(aligned[j])) { break; }
640 else { aligned[j] = '.'; }
642 setUnaligned(aligned);
647 //********************************************************************************************************************
649 int Sequence::filterFromPos(int end){
651 if (end > aligned.length()) { end = aligned.length(); m->mothurOut("[ERROR]: end to large.\n"); }
653 for(int j = end; j < aligned.length(); j++) {
657 for(int j = aligned.length()-1; j < 0; j--) {
658 if (isalpha(aligned[j])) { break; }
659 else { aligned[j] = '.'; }
662 setUnaligned(aligned);
666 //********************************************************************************************************************
668 int Sequence::getEndPos(){
670 for(int j=alignmentLength-1;j>=0;j--){
671 if(aligned[j] != '.'){
677 if(isAligned == 0){ endPos = numBases; }
682 //********************************************************************************************************************
684 void Sequence::padFromPos(int end){
685 cout << end << '\t' << endPos << endl;
686 for(int j = end; j < endPos; j++) {
693 //********************************************************************************************************************
695 bool Sequence::getIsAligned(){
698 //********************************************************************************************************************
700 void Sequence::reverseComplement(){
703 for(int i=numBases-1;i>=0;i--){
704 if(unaligned[i] == 'A') { temp += 'T'; }
705 else if(unaligned[i] == 'T'){ temp += 'A'; }
706 else if(unaligned[i] == 'G'){ temp += 'C'; }
707 else if(unaligned[i] == 'C'){ temp += 'G'; }
708 else { temp += 'N'; }
715 //********************************************************************************************************************
717 void Sequence::trim(int length){
719 if(numBases > length){
720 unaligned = unaligned.substr(0,length);
726 ///**************************************************************************************************/