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
82 sequence = getSequenceString(fastaString, numAmbig);
85 //setUnaligned removes any gap characters for us
86 setUnaligned(sequence);
88 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(); }
93 m->errorOut(e, "Sequence", "Sequence");
97 //********************************************************************************************************************
98 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
99 Sequence::Sequence(istringstream& fastaString, string JustUnaligned){
101 m = MothurOut::getInstance();
104 name = getSequenceName(fastaString);
106 if (!m->control_pressed) {
110 while ((name[0] == '#') && fastaString) {
111 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
112 sequence = getCommentString(fastaString);
116 name = name.substr(1);
123 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
126 sequence = getSequenceString(fastaString, numAmbig);
128 //setUnaligned removes any gap characters for us
129 setUnaligned(sequence);
131 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(); }
136 catch(exception& e) {
137 m->errorOut(e, "Sequence", "Sequence");
143 //********************************************************************************************************************
144 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
145 Sequence::Sequence(ifstream& fastaFile){
147 m = MothurOut::getInstance();
149 name = getSequenceName(fastaFile);
151 if (!m->control_pressed) {
156 while ((name[0] == '#') && fastaFile) {
157 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
158 sequence = getCommentString(fastaFile);
162 name = name.substr(1);
170 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
173 sequence = getSequenceString(fastaFile, numAmbig);
175 setAligned(sequence);
176 //setUnaligned removes any gap characters for us
177 setUnaligned(sequence);
179 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 catch(exception& e) {
185 m->errorOut(e, "Sequence", "Sequence");
189 //********************************************************************************************************************
190 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
191 Sequence::Sequence(ifstream& fastaFile, string& extraInfo, bool getInfo){
193 m = MothurOut::getInstance();
197 name = getSequenceName(fastaFile);
199 if (!m->control_pressed) {
203 while ((name[0] == '#') && fastaFile) {
204 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
205 sequence = getCommentString(fastaFile);
209 name = name.substr(1);
216 //read info after sequence name
217 while (!fastaFile.eof()) {
218 char c = fastaFile.get();
219 if (c == 10 || c == 13 || c == -1){ break; }
224 sequence = getSequenceString(fastaFile, numAmbig);
226 setAligned(sequence);
227 //setUnaligned removes any gap characters for us
228 setUnaligned(sequence);
230 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(); }
234 catch(exception& e) {
235 m->errorOut(e, "Sequence", "Sequence");
239 //********************************************************************************************************************
240 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
241 Sequence::Sequence(ifstream& fastaFile, string JustUnaligned){
243 m = MothurOut::getInstance();
245 name = getSequenceName(fastaFile);
247 if (!m->control_pressed) {
251 while ((name[0] == '#') && fastaFile) {
252 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
253 sequence = getCommentString(fastaFile);
257 name = name.substr(1);
265 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
268 sequence = getSequenceString(fastaFile, numAmbig);
270 //setUnaligned removes any gap characters for us
271 setUnaligned(sequence);
273 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(); }
278 catch(exception& e) {
279 m->errorOut(e, "Sequence", "Sequence");
283 //********************************************************************************************************************
284 string Sequence::getSequenceName(ifstream& fastaFile) {
290 if (name.length() != 0) {
292 name = name.substr(1);
296 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); m->control_pressed = true; }
300 catch(exception& e) {
301 m->errorOut(e, "Sequence", "getSequenceName");
305 //********************************************************************************************************************
306 string Sequence::getSequenceName(istringstream& fastaFile) {
312 if (name.length() != 0) {
314 name = name.substr(1);
318 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); m->control_pressed = true; }
322 catch(exception& e) {
323 m->errorOut(e, "Sequence", "getSequenceName");
327 //********************************************************************************************************************
328 string Sequence::getSequenceString(ifstream& fastaFile, int& numAmbig) {
331 string sequence = "";
335 letter= fastaFile.get();
337 fastaFile.putback(letter);
339 }else if (letter == ' ') {;}
340 else if(isprint(letter)){
341 letter = toupper(letter);
342 if(letter == 'U'){letter = 'T';}
343 if(letter != '.' && letter != '-' && letter != 'A' && letter != 'T' && letter != 'G' && letter != 'C' && letter != 'N'){
353 catch(exception& e) {
354 m->errorOut(e, "Sequence", "getSequenceString");
358 //********************************************************************************************************************
359 //comment can contain '>' so we need to account for that
360 string Sequence::getCommentString(ifstream& fastaFile) {
363 string sequence = "";
366 letter=fastaFile.get();
367 if((letter == '\r') || (letter == '\n')){
368 m->gobble(fastaFile); //in case its a \r\n situation
375 catch(exception& e) {
376 m->errorOut(e, "Sequence", "getCommentString");
380 //********************************************************************************************************************
381 string Sequence::getSequenceString(istringstream& fastaFile, int& numAmbig) {
384 string sequence = "";
387 while(!fastaFile.eof()){
388 letter= fastaFile.get();
391 fastaFile.putback(letter);
393 }else if (letter == ' ') {;}
394 else if(isprint(letter)){
395 letter = toupper(letter);
396 if(letter == 'U'){letter = 'T';}
397 if(letter != '.' && letter != '-' && letter != 'A' && letter != 'T' && letter != 'G' && letter != 'C' && letter != 'N'){
407 catch(exception& e) {
408 m->errorOut(e, "Sequence", "getSequenceString");
412 //********************************************************************************************************************
413 //comment can contain '>' so we need to account for that
414 string Sequence::getCommentString(istringstream& fastaFile) {
417 string sequence = "";
420 letter=fastaFile.get();
421 if((letter == '\r') || (letter == '\n')){
422 m->gobble(fastaFile); //in case its a \r\n situation
429 catch(exception& e) {
430 m->errorOut(e, "Sequence", "getCommentString");
434 //********************************************************************************************************************
436 void Sequence::initialize(){
448 longHomoPolymer = -1;
453 //********************************************************************************************************************
455 void Sequence::setName(string seqName) {
456 if(seqName[0] == '>') { name = seqName.substr(1); }
457 else { name = seqName; }
460 //********************************************************************************************************************
462 void Sequence::setUnaligned(string sequence){
464 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
466 for(int j=0;j<sequence.length();j++) {
467 if(isalpha(sequence[j])) { temp += sequence[j]; }
472 unaligned = sequence;
474 numBases = unaligned.length();
478 //********************************************************************************************************************
480 void Sequence::setAligned(string sequence){
482 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
484 alignmentLength = aligned.length();
485 setUnaligned(sequence);
487 if(aligned[0] == '-'){
488 for(int i=0;i<alignmentLength;i++){
489 if(aligned[i] == '-'){
496 for(int i=alignmentLength-1;i>=0;i--){
497 if(aligned[i] == '-'){
508 //********************************************************************************************************************
510 void Sequence::setPairwise(string sequence){
514 //********************************************************************************************************************
516 string Sequence::convert2ints() {
518 if(unaligned == "") { /* need to throw an error */ }
522 for(int i=0;i<unaligned.length();i++) {
523 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
524 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
525 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
526 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
527 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
528 else { processed += '4'; }
533 //********************************************************************************************************************
535 string Sequence::getName(){
539 //********************************************************************************************************************
541 string Sequence::getAligned(){
542 if(isAligned == 0) { return unaligned; }
543 else { return aligned; }
546 //********************************************************************************************************************
548 string Sequence::getInlineSeq(){
549 return name + '\t' + aligned;
553 //********************************************************************************************************************
555 string Sequence::getPairwise(){
559 //********************************************************************************************************************
561 string Sequence::getUnaligned(){
565 //********************************************************************************************************************
567 int Sequence::getNumBases(){
570 //********************************************************************************************************************
572 int Sequence::getNumNs(){
574 for (int i = 0; i < unaligned.length(); i++) {
575 if(toupper(unaligned[i]) == 'N') { numNs++; }
580 //********************************************************************************************************************
582 void Sequence::printSequence(ostream& out){
584 out << ">" << name << endl;
586 out << aligned << endl;
589 out << unaligned << endl;
593 //********************************************************************************************************************
595 int Sequence::getAlignLength(){
596 return alignmentLength;
599 //********************************************************************************************************************
601 int Sequence::getAmbigBases(){
602 if(ambigBases == -1){
604 for(int j=0;j<numBases;j++){
605 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
614 //********************************************************************************************************************
616 void Sequence::removeAmbigBases(){
618 for(int j=0;j<alignmentLength;j++){
619 if(aligned[j] != 'A' && aligned[j] != 'T' && aligned[j] != 'G' && aligned[j] != 'C'){
623 setUnaligned(aligned);
626 //********************************************************************************************************************
628 int Sequence::getLongHomoPolymer(){
629 if(longHomoPolymer == -1){
632 for(int j=1;j<numBases;j++){
633 if(unaligned[j] == unaligned[j-1]){
637 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
641 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
643 return longHomoPolymer;
646 //********************************************************************************************************************
648 int Sequence::getStartPos(){
650 for(int j = 0; j < alignmentLength; j++) {
651 if((aligned[j] != '.')&&(aligned[j] != '-')){
657 if(isAligned == 0){ startPos = 1; }
662 //********************************************************************************************************************
664 void Sequence::padToPos(int start){
666 for(int j = startPos-1; j < start-1; j++) {
672 //********************************************************************************************************************
674 int Sequence::filterToPos(int start){
676 if (start > aligned.length()) { start = aligned.length(); m->mothurOut("[ERROR]: start to large.\n"); }
678 for(int j = 0; j < start; j++) {
682 //things like ......----------AT become ................AT
683 for(int j = start; j < aligned.length(); j++) {
684 if (isalpha(aligned[j])) { break; }
685 else { aligned[j] = '.'; }
687 setUnaligned(aligned);
692 //********************************************************************************************************************
694 int Sequence::filterFromPos(int end){
696 if (end > aligned.length()) { end = aligned.length(); m->mothurOut("[ERROR]: end to large.\n"); }
698 for(int j = end; j < aligned.length(); j++) {
702 for(int j = aligned.length()-1; j < 0; j--) {
703 if (isalpha(aligned[j])) { break; }
704 else { aligned[j] = '.'; }
707 setUnaligned(aligned);
711 //********************************************************************************************************************
713 int Sequence::getEndPos(){
715 for(int j=alignmentLength-1;j>=0;j--){
716 if((aligned[j] != '.')&&(aligned[j] != '-')){
722 if(isAligned == 0){ endPos = numBases; }
727 //********************************************************************************************************************
729 void Sequence::padFromPos(int end){
730 //cout << end << '\t' << endPos << endl;
731 for(int j = end; j < endPos; j++) {
738 //********************************************************************************************************************
740 bool Sequence::getIsAligned(){
743 //********************************************************************************************************************
745 void Sequence::reverseComplement(){
748 for(int i=numBases-1;i>=0;i--){
749 if(unaligned[i] == 'A') { temp += 'T'; }
750 else if(unaligned[i] == 'T'){ temp += 'A'; }
751 else if(unaligned[i] == 'G'){ temp += 'C'; }
752 else if(unaligned[i] == 'C'){ temp += 'G'; }
753 else { temp += 'N'; }
760 //********************************************************************************************************************
762 void Sequence::trim(int length){
764 if(numBases > length){
765 unaligned = unaligned.substr(0,length);
773 ///**************************************************************************************************/