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();
57 name = name.substr(1);
61 while ((name[0] == '#') && fastaString) {
62 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
63 sequence = getCommentString(fastaString);
67 name = name.substr(1);
74 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
76 sequence = getSequenceString(fastaString);
78 //setUnaligned removes any gap characters for us
79 setUnaligned(sequence);
82 m->errorOut(e, "Sequence", "Sequence");
86 //********************************************************************************************************************
87 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
88 Sequence::Sequence(istringstream& fastaString, string JustUnaligned){
90 m = MothurOut::getInstance();
94 name = name.substr(1);
98 while ((name[0] == '#') && fastaString) {
99 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
100 sequence = getCommentString(fastaString);
104 name = name.substr(1);
111 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 = getSequenceString(fastaString);
115 //setUnaligned removes any gap characters for us
116 setUnaligned(sequence);
118 catch(exception& e) {
119 m->errorOut(e, "Sequence", "Sequence");
125 //********************************************************************************************************************
126 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
127 Sequence::Sequence(ifstream& fastaFile){
129 m = MothurOut::getInstance();
132 name = name.substr(1);
136 while ((name[0] == '#') && fastaFile) {
137 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
138 sequence = getCommentString(fastaFile);
142 name = name.substr(1);
150 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
152 sequence = getSequenceString(fastaFile);
154 setAligned(sequence);
155 //setUnaligned removes any gap characters for us
156 setUnaligned(sequence);
158 catch(exception& e) {
159 m->errorOut(e, "Sequence", "Sequence");
163 //********************************************************************************************************************
164 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
165 Sequence::Sequence(ifstream& fastaFile, string JustUnaligned){
167 m = MothurOut::getInstance();
170 name = name.substr(1);
174 while ((name[0] == '#') && fastaFile) {
175 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 = getCommentString(fastaFile);
180 name = name.substr(1);
188 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
190 sequence = getSequenceString(fastaFile);
192 //setUnaligned removes any gap characters for us
193 setUnaligned(sequence);
195 catch(exception& e) {
196 m->errorOut(e, "Sequence", "Sequence");
201 //********************************************************************************************************************
202 string Sequence::getSequenceString(ifstream& fastaFile) {
205 string sequence = "";
208 letter= fastaFile.get();
210 fastaFile.putback(letter);
213 else if(isprint(letter)){
214 letter = toupper(letter);
215 if(letter == 'U'){letter = 'T';}
222 catch(exception& e) {
223 m->errorOut(e, "Sequence", "getSequenceString");
227 //********************************************************************************************************************
228 //comment can contain '>' so we need to account for that
229 string Sequence::getCommentString(ifstream& fastaFile) {
232 string sequence = "";
235 letter=fastaFile.get();
236 if((letter == '\r') || (letter == '\n')){
237 gobble(fastaFile); //in case its a \r\n situation
244 catch(exception& e) {
245 m->errorOut(e, "Sequence", "getCommentString");
249 //********************************************************************************************************************
250 string Sequence::getSequenceString(istringstream& fastaFile) {
253 string sequence = "";
255 while(!fastaFile.eof()){
256 letter= fastaFile.get();
259 fastaFile.putback(letter);
262 else if(isprint(letter)){
263 letter = toupper(letter);
264 if(letter == 'U'){letter = 'T';}
271 catch(exception& e) {
272 m->errorOut(e, "Sequence", "getSequenceString");
276 //********************************************************************************************************************
277 //comment can contain '>' so we need to account for that
278 string Sequence::getCommentString(istringstream& fastaFile) {
281 string sequence = "";
284 letter=fastaFile.get();
285 if((letter == '\r') || (letter == '\n')){
286 gobble(fastaFile); //in case its a \r\n situation
293 catch(exception& e) {
294 m->errorOut(e, "Sequence", "getCommentString");
298 //********************************************************************************************************************
300 void Sequence::initialize(){
312 longHomoPolymer = -1;
317 //********************************************************************************************************************
319 void Sequence::setName(string seqName) {
320 if(seqName[0] == '>') { name = seqName.substr(1); }
321 else { name = seqName; }
324 //********************************************************************************************************************
326 void Sequence::setUnaligned(string sequence){
328 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
330 for(int j=0;j<sequence.length();j++) {
331 if(isalpha(sequence[j])) { temp += sequence[j]; }
336 unaligned = sequence;
338 numBases = unaligned.length();
342 //********************************************************************************************************************
344 void Sequence::setAligned(string sequence){
346 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
348 alignmentLength = aligned.length();
349 setUnaligned(sequence);
351 if(aligned[0] == '-'){
352 for(int i=0;i<alignmentLength;i++){
353 if(aligned[i] == '-'){
360 for(int i=alignmentLength-1;i>=0;i--){
361 if(aligned[i] == '-'){
372 //********************************************************************************************************************
374 void Sequence::setPairwise(string sequence){
378 //********************************************************************************************************************
380 string Sequence::convert2ints() {
382 if(unaligned == "") { /* need to throw an error */ }
386 for(int i=0;i<unaligned.length();i++) {
387 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
388 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
389 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
390 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
391 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
392 else { processed += '4'; }
397 //********************************************************************************************************************
399 string Sequence::getName(){
403 //********************************************************************************************************************
405 string Sequence::getAligned(){
406 if(isAligned == 0) { return unaligned; }
407 else { return aligned; }
410 //********************************************************************************************************************
412 string Sequence::getPairwise(){
416 //********************************************************************************************************************
418 string Sequence::getUnaligned(){
422 //********************************************************************************************************************
424 int Sequence::getNumBases(){
428 //********************************************************************************************************************
430 void Sequence::printSequence(ostream& out){
432 out << ">" << name << endl;
434 out << aligned << endl;
437 out << unaligned << endl;
441 //********************************************************************************************************************
443 int Sequence::getAlignLength(){
444 return alignmentLength;
447 //********************************************************************************************************************
449 int Sequence::getAmbigBases(){
450 if(ambigBases == -1){
452 for(int j=0;j<numBases;j++){
453 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
462 //********************************************************************************************************************
464 int Sequence::getLongHomoPolymer(){
465 if(longHomoPolymer == -1){
468 for(int j=1;j<numBases;j++){
469 if(unaligned[j] == unaligned[j-1]){
473 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
477 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
479 return longHomoPolymer;
482 //********************************************************************************************************************
484 int Sequence::getStartPos(){
486 for(int j = 0; j < alignmentLength; j++) {
487 if(aligned[j] != '.'){
493 if(isAligned == 0){ startPos = 1; }
498 //********************************************************************************************************************
500 int Sequence::getEndPos(){
502 for(int j=alignmentLength-1;j>=0;j--){
503 if(aligned[j] != '.'){
509 if(isAligned == 0){ endPos = numBases; }
514 //********************************************************************************************************************
516 bool Sequence::getIsAligned(){
520 //********************************************************************************************************************
522 void Sequence::reverseComplement(){
525 for(int i=numBases-1;i>=0;i--){
526 if(unaligned[i] == 'A') { temp += 'T'; }
527 else if(unaligned[i] == 'T'){ temp += 'A'; }
528 else if(unaligned[i] == 'G'){ temp += 'C'; }
529 else if(unaligned[i] == 'C'){ temp += 'G'; }
530 else { temp += 'N'; }
536 /**************************************************************************************************/