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 JustUnaligned){
196 m = MothurOut::getInstance();
200 if (name.length() != 0) {
201 name = name.substr(1);
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);
219 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
222 sequence = getSequenceString(fastaFile, numAmbig);
224 //setUnaligned removes any gap characters for us
225 setUnaligned(sequence);
227 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(); }
229 }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); }
232 catch(exception& e) {
233 m->errorOut(e, "Sequence", "Sequence");
238 //********************************************************************************************************************
239 string Sequence::getSequenceString(ifstream& fastaFile, int& numAmbig) {
242 string sequence = "";
246 letter= fastaFile.get();
248 fastaFile.putback(letter);
251 else if(isprint(letter)){
252 letter = toupper(letter);
253 if(letter == 'U'){letter = 'T';}
254 if(letter != '.' && letter != '-' && letter != 'A' && letter != 'T' && letter != 'G' && letter != 'C' && letter != 'N'){
264 catch(exception& e) {
265 m->errorOut(e, "Sequence", "getSequenceString");
269 //********************************************************************************************************************
270 //comment can contain '>' so we need to account for that
271 string Sequence::getCommentString(ifstream& fastaFile) {
274 string sequence = "";
277 letter=fastaFile.get();
278 if((letter == '\r') || (letter == '\n')){
279 m->gobble(fastaFile); //in case its a \r\n situation
286 catch(exception& e) {
287 m->errorOut(e, "Sequence", "getCommentString");
291 //********************************************************************************************************************
292 string Sequence::getSequenceString(istringstream& fastaFile, int& numAmbig) {
295 string sequence = "";
298 while(!fastaFile.eof()){
299 letter= fastaFile.get();
302 fastaFile.putback(letter);
305 else if(isprint(letter)){
306 letter = toupper(letter);
307 if(letter == 'U'){letter = 'T';}
308 if(letter != '.' && letter != '-' && letter != 'A' && letter != 'T' && letter != 'G' && letter != 'C' && letter != 'N'){
318 catch(exception& e) {
319 m->errorOut(e, "Sequence", "getSequenceString");
323 //********************************************************************************************************************
324 //comment can contain '>' so we need to account for that
325 string Sequence::getCommentString(istringstream& fastaFile) {
328 string sequence = "";
331 letter=fastaFile.get();
332 if((letter == '\r') || (letter == '\n')){
333 m->gobble(fastaFile); //in case its a \r\n situation
340 catch(exception& e) {
341 m->errorOut(e, "Sequence", "getCommentString");
345 //********************************************************************************************************************
347 void Sequence::initialize(){
359 longHomoPolymer = -1;
364 //********************************************************************************************************************
366 void Sequence::setName(string seqName) {
367 if(seqName[0] == '>') { name = seqName.substr(1); }
368 else { name = seqName; }
371 //********************************************************************************************************************
373 void Sequence::setUnaligned(string sequence){
375 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
377 for(int j=0;j<sequence.length();j++) {
378 if(isalpha(sequence[j])) { temp += sequence[j]; }
383 unaligned = sequence;
385 numBases = unaligned.length();
389 //********************************************************************************************************************
391 void Sequence::setAligned(string sequence){
393 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
395 alignmentLength = aligned.length();
396 setUnaligned(sequence);
398 if(aligned[0] == '-'){
399 for(int i=0;i<alignmentLength;i++){
400 if(aligned[i] == '-'){
407 for(int i=alignmentLength-1;i>=0;i--){
408 if(aligned[i] == '-'){
419 //********************************************************************************************************************
421 void Sequence::setPairwise(string sequence){
425 //********************************************************************************************************************
427 string Sequence::convert2ints() {
429 if(unaligned == "") { /* need to throw an error */ }
433 for(int i=0;i<unaligned.length();i++) {
434 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
435 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
436 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
437 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
438 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
439 else { processed += '4'; }
444 //********************************************************************************************************************
446 string Sequence::getName(){
450 //********************************************************************************************************************
452 string Sequence::getAligned(){
453 if(isAligned == 0) { return unaligned; }
454 else { return aligned; }
457 //********************************************************************************************************************
459 string Sequence::getInlineSeq(){
460 return name + '\t' + aligned;
464 //********************************************************************************************************************
466 string Sequence::getPairwise(){
470 //********************************************************************************************************************
472 string Sequence::getUnaligned(){
476 //********************************************************************************************************************
478 int Sequence::getNumBases(){
482 //********************************************************************************************************************
484 void Sequence::printSequence(ostream& out){
486 out << ">" << name << endl;
488 out << aligned << endl;
491 out << unaligned << endl;
495 //********************************************************************************************************************
497 int Sequence::getAlignLength(){
498 return alignmentLength;
501 //********************************************************************************************************************
503 int Sequence::getAmbigBases(){
504 if(ambigBases == -1){
506 for(int j=0;j<numBases;j++){
507 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
516 //********************************************************************************************************************
518 void Sequence::removeAmbigBases(){
520 for(int j=0;j<alignmentLength;j++){
521 if(aligned[j] != 'A' && aligned[j] != 'T' && aligned[j] != 'G' && aligned[j] != 'C'){
525 setUnaligned(aligned);
528 //********************************************************************************************************************
530 int Sequence::getLongHomoPolymer(){
531 if(longHomoPolymer == -1){
534 for(int j=1;j<numBases;j++){
535 if(unaligned[j] == unaligned[j-1]){
539 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
543 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
545 return longHomoPolymer;
548 //********************************************************************************************************************
550 int Sequence::getStartPos(){
552 for(int j = 0; j < alignmentLength; j++) {
553 if(aligned[j] != '.'){
559 if(isAligned == 0){ startPos = 1; }
564 //********************************************************************************************************************
566 void Sequence::padToPos(int start){
568 for(int j = startPos-1; j < start-1; j++) {
575 //********************************************************************************************************************
577 int Sequence::getEndPos(){
579 for(int j=alignmentLength-1;j>=0;j--){
580 if(aligned[j] != '.'){
586 if(isAligned == 0){ endPos = numBases; }
591 //********************************************************************************************************************
593 void Sequence::padFromPos(int end){
595 for(int j = end; j < endPos; j++) {
602 //********************************************************************************************************************
604 bool Sequence::getIsAligned(){
607 //********************************************************************************************************************
609 void Sequence::reverseComplement(){
612 for(int i=numBases-1;i>=0;i--){
613 if(unaligned[i] == 'A') { temp += 'T'; }
614 else if(unaligned[i] == 'T'){ temp += 'A'; }
615 else if(unaligned[i] == 'G'){ temp += 'C'; }
616 else if(unaligned[i] == 'C'){ temp += 'G'; }
617 else { temp += 'N'; }
624 //********************************************************************************************************************
626 void Sequence::trim(int length){
628 if(numBases > length){
629 unaligned = unaligned.substr(0,length);
635 ///**************************************************************************************************/