5 * Created by Pat Schloss on 12/15/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
10 #include "sequence.hpp"
12 /***********************************************************************/
15 m = MothurOut::getInstance();
19 /***********************************************************************/
21 Sequence::Sequence(string newName, string sequence) {
23 m = MothurOut::getInstance();
27 //setUnaligned removes any gap characters for us
28 setUnaligned(sequence);
32 m->errorOut(e, "Sequence", "Sequence");
36 //********************************************************************************************************************
37 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
38 Sequence::Sequence(istringstream& fastaString){
40 m = MothurOut::getInstance();
44 name = name.substr(1);
48 while ((name[0] == '#') && fastaString) {
49 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
50 sequence = getCommentString(fastaString);
54 name = name.substr(1);
61 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 = getSequenceString(fastaString);
65 //setUnaligned removes any gap characters for us
66 setUnaligned(sequence);
69 m->errorOut(e, "Sequence", "Sequence");
74 //********************************************************************************************************************
75 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
76 Sequence::Sequence(ifstream& fastaFile){
78 m = MothurOut::getInstance();
81 name = name.substr(1);
85 while ((name[0] == '#') && fastaFile) {
86 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
87 sequence = getCommentString(fastaFile);
91 name = name.substr(1);
99 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
101 sequence = getSequenceString(fastaFile);
103 setAligned(sequence);
104 //setUnaligned removes any gap characters for us
105 setUnaligned(sequence);
107 catch(exception& e) {
108 m->errorOut(e, "Sequence", "Sequence");
112 //********************************************************************************************************************
113 string Sequence::getSequenceString(ifstream& fastaFile) {
116 string sequence = "";
119 letter= fastaFile.get();
121 fastaFile.putback(letter);
124 else if(isprint(letter)){
125 letter = toupper(letter);
126 if(letter == 'U'){letter = 'T';}
133 catch(exception& e) {
134 m->errorOut(e, "Sequence", "getSequenceString");
138 //********************************************************************************************************************
139 //comment can contain '>' so we need to account for that
140 string Sequence::getCommentString(ifstream& fastaFile) {
143 string sequence = "";
146 letter=fastaFile.get();
147 if((letter == '\r') || (letter == '\n')){
148 gobble(fastaFile); //in case its a \r\n situation
155 catch(exception& e) {
156 m->errorOut(e, "Sequence", "getCommentString");
160 //********************************************************************************************************************
161 string Sequence::getSequenceString(istringstream& fastaFile) {
164 string sequence = "";
166 while(!fastaFile.eof()){
167 letter= fastaFile.get();
170 fastaFile.putback(letter);
173 else if(isprint(letter)){
174 letter = toupper(letter);
175 if(letter == 'U'){letter = 'T';}
182 catch(exception& e) {
183 m->errorOut(e, "Sequence", "getSequenceString");
187 //********************************************************************************************************************
188 //comment can contain '>' so we need to account for that
189 string Sequence::getCommentString(istringstream& fastaFile) {
192 string sequence = "";
195 letter=fastaFile.get();
196 if((letter == '\r') || (letter == '\n')){
197 gobble(fastaFile); //in case its a \r\n situation
204 catch(exception& e) {
205 m->errorOut(e, "Sequence", "getCommentString");
209 //********************************************************************************************************************
211 void Sequence::initialize(){
223 longHomoPolymer = -1;
228 //********************************************************************************************************************
230 void Sequence::setName(string seqName) {
231 if(seqName[0] == '>') { name = seqName.substr(1); }
232 else { name = seqName; }
235 //********************************************************************************************************************
237 void Sequence::setUnaligned(string sequence){
239 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
241 for(int j=0;j<sequence.length();j++) {
242 if(isalpha(sequence[j])) { temp += sequence[j]; }
247 unaligned = sequence;
249 numBases = unaligned.length();
253 //********************************************************************************************************************
255 void Sequence::setAligned(string sequence){
257 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
259 alignmentLength = aligned.length();
260 setUnaligned(sequence);
262 if(aligned[0] == '-'){
263 for(int i=0;i<alignmentLength;i++){
264 if(aligned[i] == '-'){
271 for(int i=alignmentLength-1;i>=0;i--){
272 if(aligned[i] == '-'){
283 //********************************************************************************************************************
285 void Sequence::setPairwise(string sequence){
289 //********************************************************************************************************************
291 string Sequence::convert2ints() {
293 if(unaligned == "") { /* need to throw an error */ }
297 for(int i=0;i<unaligned.length();i++) {
298 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
299 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
300 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
301 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
302 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
303 else { processed += '4'; }
308 //********************************************************************************************************************
310 string Sequence::getName(){
314 //********************************************************************************************************************
316 string Sequence::getAligned(){
320 //********************************************************************************************************************
322 string Sequence::getPairwise(){
326 //********************************************************************************************************************
328 string Sequence::getUnaligned(){
332 //********************************************************************************************************************
334 int Sequence::getNumBases(){
338 //********************************************************************************************************************
340 void Sequence::printSequence(ostream& out){
342 out << ">" << name << endl;
344 out << aligned << endl;
347 out << unaligned << endl;
351 //********************************************************************************************************************
353 int Sequence::getAlignLength(){
354 return alignmentLength;
357 //********************************************************************************************************************
359 int Sequence::getAmbigBases(){
360 if(ambigBases == -1){
362 for(int j=0;j<numBases;j++){
363 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
372 //********************************************************************************************************************
374 int Sequence::getLongHomoPolymer(){
375 if(longHomoPolymer == -1){
378 for(int j=1;j<numBases;j++){
379 if(unaligned[j] == unaligned[j-1]){
383 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
387 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
389 return longHomoPolymer;
392 //********************************************************************************************************************
394 int Sequence::getStartPos(){
396 for(int j = 0; j < alignmentLength; j++) {
397 if(aligned[j] != '.'){
403 if(isAligned == 0){ startPos = 1; }
408 //********************************************************************************************************************
410 int Sequence::getEndPos(){
412 for(int j=alignmentLength-1;j>=0;j--){
413 if(aligned[j] != '.'){
419 if(isAligned == 0){ endPos = numBases; }
424 //********************************************************************************************************************
426 bool Sequence::getIsAligned(){
430 //********************************************************************************************************************
432 void Sequence::reverseComplement(){
435 for(int i=numBases-1;i>=0;i--){
436 if(unaligned[i] == 'A') { temp += 'T'; }
437 else if(unaligned[i] == 'T'){ temp += 'A'; }
438 else if(unaligned[i] == 'G'){ temp += 'C'; }
439 else if(unaligned[i] == 'C'){ temp += 'G'; }
440 else { temp += 'N'; }
447 //********************************************************************************************************************
448 int Sequence::MPISend(int receiver) {
455 catch(exception& e) {
456 m->errorOut(e, "Sequence", "MPISend");
460 /**************************************************************************************************/
461 int Sequence::MPIRecv(int sender) {
467 catch(exception& e) {
468 m->errorOut(e, "Sequence", "MPIRecv");
473 /**************************************************************************************************/