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();
42 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
43 cout << pid << " after mothur instance " << &name << endl;
45 cout << "after mothur initialize" << endl;
47 cout << "after name " << endl;
48 name = name.substr(1);
52 while ((name[0] == '#') && fastaString) {
53 while (fastaString) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
54 sequence = getCommentString(fastaString);
58 name = name.substr(1);
64 cout << "after mothur comment" << endl;
66 while (fastaString) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
67 cout << "after mothur name" << endl;
68 sequence = getSequenceString(fastaString);
69 cout << "after mothur sequence" << endl;
71 //setUnaligned removes any gap characters for us
72 setUnaligned(sequence);
75 m->errorOut(e, "Sequence", "Sequence");
80 //********************************************************************************************************************
81 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
82 Sequence::Sequence(ifstream& fastaFile){
84 m = MothurOut::getInstance();
87 name = name.substr(1);
91 while ((name[0] == '#') && fastaFile) {
92 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
93 sequence = getCommentString(fastaFile);
97 name = name.substr(1);
105 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
107 sequence = getSequenceString(fastaFile);
109 setAligned(sequence);
110 //setUnaligned removes any gap characters for us
111 setUnaligned(sequence);
113 catch(exception& e) {
114 m->errorOut(e, "Sequence", "Sequence");
118 //********************************************************************************************************************
119 string Sequence::getSequenceString(ifstream& fastaFile) {
122 string sequence = "";
125 letter= fastaFile.get();
127 fastaFile.putback(letter);
130 else if(isprint(letter)){
131 letter = toupper(letter);
132 if(letter == 'U'){letter = 'T';}
139 catch(exception& e) {
140 m->errorOut(e, "Sequence", "getSequenceString");
144 //********************************************************************************************************************
145 //comment can contain '>' so we need to account for that
146 string Sequence::getCommentString(ifstream& fastaFile) {
149 string sequence = "";
152 letter=fastaFile.get();
153 if((letter == '\r') || (letter == '\n')){
154 gobble(fastaFile); //in case its a \r\n situation
161 catch(exception& e) {
162 m->errorOut(e, "Sequence", "getCommentString");
166 //********************************************************************************************************************
167 string Sequence::getSequenceString(istringstream& fastaFile) {
170 string sequence = "";
173 letter= fastaFile.get();
175 fastaFile.putback(letter);
178 else if(isprint(letter)){
179 letter = toupper(letter);
180 if(letter == 'U'){letter = 'T';}
187 catch(exception& e) {
188 m->errorOut(e, "Sequence", "getSequenceString");
192 //********************************************************************************************************************
193 //comment can contain '>' so we need to account for that
194 string Sequence::getCommentString(istringstream& fastaFile) {
197 string sequence = "";
200 letter=fastaFile.get();
201 if((letter == '\r') || (letter == '\n')){
202 gobble(fastaFile); //in case its a \r\n situation
209 catch(exception& e) {
210 m->errorOut(e, "Sequence", "getCommentString");
214 //********************************************************************************************************************
216 void Sequence::initialize(){
228 longHomoPolymer = -1;
233 //********************************************************************************************************************
235 void Sequence::setName(string seqName) {
236 if(seqName[0] == '>') { name = seqName.substr(1); }
237 else { name = seqName; }
240 //********************************************************************************************************************
242 void Sequence::setUnaligned(string sequence){
244 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
246 for(int j=0;j<sequence.length();j++) {
247 if(isalpha(sequence[j])) { temp += sequence[j]; }
252 unaligned = sequence;
254 numBases = unaligned.length();
258 //********************************************************************************************************************
260 void Sequence::setAligned(string sequence){
262 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
264 alignmentLength = aligned.length();
265 setUnaligned(sequence);
267 if(aligned[0] == '-'){
268 for(int i=0;i<alignmentLength;i++){
269 if(aligned[i] == '-'){
276 for(int i=alignmentLength-1;i>=0;i--){
277 if(aligned[i] == '-'){
288 //********************************************************************************************************************
290 void Sequence::setPairwise(string sequence){
294 //********************************************************************************************************************
296 string Sequence::convert2ints() {
298 if(unaligned == "") { /* need to throw an error */ }
302 for(int i=0;i<unaligned.length();i++) {
303 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
304 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
305 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
306 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
307 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
308 else { processed += '4'; }
313 //********************************************************************************************************************
315 string Sequence::getName(){
319 //********************************************************************************************************************
321 string Sequence::getAligned(){
325 //********************************************************************************************************************
327 string Sequence::getPairwise(){
331 //********************************************************************************************************************
333 string Sequence::getUnaligned(){
337 //********************************************************************************************************************
339 int Sequence::getNumBases(){
343 //********************************************************************************************************************
345 void Sequence::printSequence(ostream& out){
347 out << ">" << name << endl;
349 out << aligned << endl;
352 out << unaligned << endl;
356 //********************************************************************************************************************
358 int Sequence::getAlignLength(){
359 return alignmentLength;
362 //********************************************************************************************************************
364 int Sequence::getAmbigBases(){
365 if(ambigBases == -1){
367 for(int j=0;j<numBases;j++){
368 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
377 //********************************************************************************************************************
379 int Sequence::getLongHomoPolymer(){
380 if(longHomoPolymer == -1){
383 for(int j=1;j<numBases;j++){
384 if(unaligned[j] == unaligned[j-1]){
388 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
392 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
394 return longHomoPolymer;
397 //********************************************************************************************************************
399 int Sequence::getStartPos(){
401 for(int j = 0; j < alignmentLength; j++) {
402 if(aligned[j] != '.'){
408 if(isAligned == 0){ startPos = 1; }
413 //********************************************************************************************************************
415 int Sequence::getEndPos(){
417 for(int j=alignmentLength-1;j>=0;j--){
418 if(aligned[j] != '.'){
424 if(isAligned == 0){ endPos = numBases; }
429 //********************************************************************************************************************
431 bool Sequence::getIsAligned(){
435 //********************************************************************************************************************
437 void Sequence::reverseComplement(){
440 for(int i=numBases-1;i>=0;i--){
441 if(unaligned[i] == 'A') { temp += 'T'; }
442 else if(unaligned[i] == 'T'){ temp += 'A'; }
443 else if(unaligned[i] == 'G'){ temp += 'C'; }
444 else if(unaligned[i] == 'C'){ temp += 'G'; }
445 else { temp += 'N'; }
452 //********************************************************************************************************************