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();
26 //setUnaligned removes any gap characters for us
27 setUnaligned(sequence);
31 m->errorOut(e, "Sequence", "Sequence");
35 /***********************************************************************/
36 Sequence::Sequence(string newName, string sequence, string justUnAligned) {
38 m = MothurOut::getInstance();
44 //setUnaligned removes any gap characters for us
45 setUnaligned(sequence);
48 m->errorOut(e, "Sequence", "Sequence");
53 //********************************************************************************************************************
54 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
55 Sequence::Sequence(istringstream& fastaString){
57 m = MothurOut::getInstance();
60 name = getSequenceName(fastaString);
62 if (!m->control_pressed) {
66 while ((name[0] == '#') && fastaString) {
67 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
68 sequence = getCommentString(fastaString);
72 name = name.substr(1);
79 //while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
80 comment = getCommentString(fastaString);
83 sequence = getSequenceString(fastaString, numAmbig);
86 //setUnaligned removes any gap characters for us
87 setUnaligned(sequence);
89 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(); }
94 m->errorOut(e, "Sequence", "Sequence");
98 //********************************************************************************************************************
99 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
100 Sequence::Sequence(istringstream& fastaString, string JustUnaligned){
102 m = MothurOut::getInstance();
105 name = getSequenceName(fastaString);
107 if (!m->control_pressed) {
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
125 comment = getCommentString(fastaString);
128 sequence = getSequenceString(fastaString, numAmbig);
130 //setUnaligned removes any gap characters for us
131 setUnaligned(sequence);
133 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(); }
138 catch(exception& e) {
139 m->errorOut(e, "Sequence", "Sequence");
145 //********************************************************************************************************************
146 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
147 Sequence::Sequence(ifstream& fastaFile){
149 m = MothurOut::getInstance();
151 name = getSequenceName(fastaFile);
153 if (!m->control_pressed) {
158 while ((name[0] == '#') && fastaFile) {
159 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
160 sequence = getCommentString(fastaFile);
164 name = name.substr(1);
171 //while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
172 comment = getCommentString(fastaFile);
175 sequence = getSequenceString(fastaFile, numAmbig);
177 setAligned(sequence);
178 //setUnaligned removes any gap characters for us
179 setUnaligned(sequence);
181 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(); }
186 catch(exception& e) {
187 m->errorOut(e, "Sequence", "Sequence");
191 //********************************************************************************************************************
192 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
193 Sequence::Sequence(ifstream& fastaFile, string& extraInfo, bool getInfo){
195 m = MothurOut::getInstance();
199 name = getSequenceName(fastaFile);
201 if (!m->control_pressed) {
205 while ((name[0] == '#') && fastaFile) {
206 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
207 sequence = getCommentString(fastaFile);
211 name = name.substr(1);
218 //read info after sequence name
219 while (!fastaFile.eof()) {
220 char c = fastaFile.get();
221 if (c == 10 || c == 13 || c == -1){ break; }
228 sequence = getSequenceString(fastaFile, numAmbig);
230 setAligned(sequence);
231 //setUnaligned removes any gap characters for us
232 setUnaligned(sequence);
234 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(); }
238 catch(exception& e) {
239 m->errorOut(e, "Sequence", "Sequence");
243 //********************************************************************************************************************
244 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
245 Sequence::Sequence(ifstream& fastaFile, string JustUnaligned){
247 m = MothurOut::getInstance();
249 name = getSequenceName(fastaFile);
251 if (!m->control_pressed) {
255 while ((name[0] == '#') && fastaFile) {
256 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
257 sequence = getCommentString(fastaFile);
261 name = name.substr(1);
268 //while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
269 comment = getCommentString(fastaFile);
272 sequence = getSequenceString(fastaFile, numAmbig);
274 //setUnaligned removes any gap characters for us
275 setUnaligned(sequence);
277 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 catch(exception& e) {
283 m->errorOut(e, "Sequence", "Sequence");
287 //********************************************************************************************************************
288 string Sequence::getSequenceName(ifstream& fastaFile) {
294 if (name.length() != 0) {
296 name = name.substr(1);
300 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); m->control_pressed = true; }
304 catch(exception& e) {
305 m->errorOut(e, "Sequence", "getSequenceName");
309 //********************************************************************************************************************
310 string Sequence::getSequenceName(istringstream& fastaFile) {
316 if (name.length() != 0) {
318 name = name.substr(1);
322 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); m->control_pressed = true; }
326 catch(exception& e) {
327 m->errorOut(e, "Sequence", "getSequenceName");
331 //********************************************************************************************************************
332 string Sequence::getSequenceString(ifstream& fastaFile, int& numAmbig) {
335 string sequence = "";
339 letter= fastaFile.get();
341 fastaFile.putback(letter);
343 }else if (letter == ' ') {;}
344 else if(isprint(letter)){
345 letter = toupper(letter);
346 if(letter == 'U'){letter = 'T';}
347 if(letter != '.' && letter != '-' && letter != 'A' && letter != 'T' && letter != 'G' && letter != 'C' && letter != 'N'){
357 catch(exception& e) {
358 m->errorOut(e, "Sequence", "getSequenceString");
362 //********************************************************************************************************************
363 //comment can contain '>' so we need to account for that
364 string Sequence::getCommentString(ifstream& fastaFile) {
370 letter=fastaFile.get();
371 if((letter == '\r') || (letter == '\n') || letter == -1){
372 m->gobble(fastaFile); //in case its a \r\n situation
381 catch(exception& e) {
382 m->errorOut(e, "Sequence", "getCommentString");
386 //********************************************************************************************************************
387 string Sequence::getSequenceString(istringstream& fastaFile, int& numAmbig) {
390 string sequence = "";
393 while(!fastaFile.eof()){
394 letter= fastaFile.get();
397 fastaFile.putback(letter);
399 }else if (letter == ' ') {;}
400 else if(isprint(letter)){
401 letter = toupper(letter);
402 if(letter == 'U'){letter = 'T';}
403 if(letter != '.' && letter != '-' && letter != 'A' && letter != 'T' && letter != 'G' && letter != 'C' && letter != 'N'){
413 catch(exception& e) {
414 m->errorOut(e, "Sequence", "getSequenceString");
418 //********************************************************************************************************************
419 //comment can contain '>' so we need to account for that
420 string Sequence::getCommentString(istringstream& fastaFile) {
426 letter=fastaFile.get();
427 if((letter == '\r') || (letter == '\n') || letter == -1){
428 m->gobble(fastaFile); //in case its a \r\n situation
437 catch(exception& e) {
438 m->errorOut(e, "Sequence", "getCommentString");
442 //********************************************************************************************************************
444 void Sequence::initialize(){
457 longHomoPolymer = -1;
462 //********************************************************************************************************************
464 void Sequence::setName(string seqName) {
465 if(seqName[0] == '>') { name = seqName.substr(1); }
466 else { name = seqName; }
469 //********************************************************************************************************************
471 void Sequence::setUnaligned(string sequence){
473 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
475 for(int j=0;j<sequence.length();j++) {
476 if(isalpha(sequence[j])) { temp += sequence[j]; }
481 unaligned = sequence;
483 numBases = unaligned.length();
487 //********************************************************************************************************************
489 void Sequence::setAligned(string sequence){
491 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
493 alignmentLength = aligned.length();
494 setUnaligned(sequence);
496 if(aligned[0] == '-'){
497 for(int i=0;i<alignmentLength;i++){
498 if(aligned[i] == '-'){
505 for(int i=alignmentLength-1;i>=0;i--){
506 if(aligned[i] == '-'){
517 //********************************************************************************************************************
519 void Sequence::setPairwise(string sequence){
523 //********************************************************************************************************************
525 string Sequence::convert2ints() {
527 if(unaligned == "") { /* need to throw an error */ }
531 for(int i=0;i<unaligned.length();i++) {
532 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
533 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
534 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
535 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
536 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
537 else { processed += '4'; }
542 //********************************************************************************************************************
544 string Sequence::getName(){
548 //********************************************************************************************************************
550 string Sequence::getAligned(){
551 if(isAligned == 0) { return unaligned; }
552 else { return aligned; }
555 //********************************************************************************************************************
557 string Sequence::getInlineSeq(){
558 return name + '\t' + aligned;
562 //********************************************************************************************************************
564 string Sequence::getPairwise(){
568 //********************************************************************************************************************
570 string Sequence::getUnaligned(){
574 //********************************************************************************************************************
576 int Sequence::getNumBases(){
579 //********************************************************************************************************************
581 int Sequence::getNumNs(){
583 for (int i = 0; i < unaligned.length(); i++) {
584 if(toupper(unaligned[i]) == 'N') { numNs++; }
589 //********************************************************************************************************************
591 void Sequence::printSequence(ostream& out){
593 out << ">" << name << comment << endl;
595 out << aligned << endl;
598 out << unaligned << endl;
602 //********************************************************************************************************************
604 int Sequence::getAlignLength(){
605 return alignmentLength;
608 //********************************************************************************************************************
610 int Sequence::getAmbigBases(){
611 if(ambigBases == -1){
613 for(int j=0;j<numBases;j++){
614 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
623 //********************************************************************************************************************
625 void Sequence::removeAmbigBases(){
627 for(int j=0;j<alignmentLength;j++){
628 if(aligned[j] != 'A' && aligned[j] != 'T' && aligned[j] != 'G' && aligned[j] != 'C'){
632 setUnaligned(aligned);
635 //********************************************************************************************************************
637 int Sequence::getLongHomoPolymer(){
638 if(longHomoPolymer == -1){
641 for(int j=1;j<numBases;j++){
642 if(unaligned[j] == unaligned[j-1]){
646 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
650 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
652 return longHomoPolymer;
655 //********************************************************************************************************************
657 int Sequence::getStartPos(){
659 for(int j = 0; j < alignmentLength; j++) {
660 if((aligned[j] != '.')&&(aligned[j] != '-')){
666 if(isAligned == 0){ startPos = 1; }
671 //********************************************************************************************************************
673 void Sequence::padToPos(int start){
675 for(int j = startPos-1; j < start-1; j++) {
681 //********************************************************************************************************************
683 int Sequence::filterToPos(int start){
685 if (start > aligned.length()) { start = aligned.length(); m->mothurOut("[ERROR]: start to large.\n"); }
687 for(int j = 0; j < start; j++) {
691 //things like ......----------AT become ................AT
692 for(int j = start; j < aligned.length(); j++) {
693 if (isalpha(aligned[j])) { break; }
694 else { aligned[j] = '.'; }
696 setUnaligned(aligned);
701 //********************************************************************************************************************
703 int Sequence::filterFromPos(int end){
705 if (end > aligned.length()) { end = aligned.length(); m->mothurOut("[ERROR]: end to large.\n"); }
707 for(int j = end; j < aligned.length(); j++) {
711 for(int j = aligned.length()-1; j < 0; j--) {
712 if (isalpha(aligned[j])) { break; }
713 else { aligned[j] = '.'; }
716 setUnaligned(aligned);
720 //********************************************************************************************************************
722 int Sequence::getEndPos(){
724 for(int j=alignmentLength-1;j>=0;j--){
725 if((aligned[j] != '.')&&(aligned[j] != '-')){
731 if(isAligned == 0){ endPos = numBases; }
736 //********************************************************************************************************************
738 void Sequence::padFromPos(int end){
739 //cout << end << '\t' << endPos << endl;
740 for(int j = end; j < endPos; j++) {
747 //********************************************************************************************************************
749 bool Sequence::getIsAligned(){
752 //********************************************************************************************************************
754 void Sequence::reverseComplement(){
757 for(int i=numBases-1;i>=0;i--){
758 if(unaligned[i] == 'A') { temp += 'T'; }
759 else if(unaligned[i] == 'T'){ temp += 'A'; }
760 else if(unaligned[i] == 'G'){ temp += 'C'; }
761 else if(unaligned[i] == 'C'){ temp += 'G'; }
762 else { temp += 'N'; }
769 //********************************************************************************************************************
771 void Sequence::trim(int length){
773 if(numBases > length){
774 unaligned = unaligned.substr(0,length);
782 ///**************************************************************************************************/