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 << pid << " after name " << name << endl;
48 name = name.substr(1);
51 cout << pid << " name = " << name << endl;
53 while ((name[0] == '#') && fastaString) {
54 while (!fastaString.eof()) { char c = fastaString.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
55 sequence = getCommentString(fastaString);
59 name = name.substr(1);
64 cout << pid << "in while comment" << endl;
66 cout << pid << "after mothur comment" << endl;
67 while (!fastaString.eof()) { char c = fastaString.get(); cout << pid << " char = " << int(c) << endl; if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
68 cout << pid << " after mothur name" << endl;
69 sequence = getSequenceString(fastaString);
70 cout << pid << " after mothur sequence" << endl;
72 //setUnaligned removes any gap characters for us
73 setUnaligned(sequence);
76 m->errorOut(e, "Sequence", "Sequence");
81 //********************************************************************************************************************
82 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
83 Sequence::Sequence(ifstream& fastaFile){
85 m = MothurOut::getInstance();
88 name = name.substr(1);
92 while ((name[0] == '#') && fastaFile) {
93 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
94 sequence = getCommentString(fastaFile);
98 name = name.substr(1);
106 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
108 sequence = getSequenceString(fastaFile);
110 setAligned(sequence);
111 //setUnaligned removes any gap characters for us
112 setUnaligned(sequence);
114 catch(exception& e) {
115 m->errorOut(e, "Sequence", "Sequence");
119 //********************************************************************************************************************
120 string Sequence::getSequenceString(ifstream& fastaFile) {
123 string sequence = "";
126 letter= fastaFile.get();
128 fastaFile.putback(letter);
131 else if(isprint(letter)){
132 letter = toupper(letter);
133 if(letter == 'U'){letter = 'T';}
140 catch(exception& e) {
141 m->errorOut(e, "Sequence", "getSequenceString");
145 //********************************************************************************************************************
146 //comment can contain '>' so we need to account for that
147 string Sequence::getCommentString(ifstream& fastaFile) {
150 string sequence = "";
153 letter=fastaFile.get();
154 if((letter == '\r') || (letter == '\n')){
155 gobble(fastaFile); //in case its a \r\n situation
162 catch(exception& e) {
163 m->errorOut(e, "Sequence", "getCommentString");
167 //********************************************************************************************************************
168 string Sequence::getSequenceString(istringstream& fastaFile) {
171 string sequence = "";
173 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
174 while(!fastaFile.eof()){
175 letter= fastaFile.get();
176 cout << pid << '\t' << letter << endl;
178 fastaFile.putback(letter);
181 else if(isprint(letter)){
182 letter = toupper(letter);
183 if(letter == 'U'){letter = 'T';}
190 catch(exception& e) {
191 m->errorOut(e, "Sequence", "getSequenceString");
195 //********************************************************************************************************************
196 //comment can contain '>' so we need to account for that
197 string Sequence::getCommentString(istringstream& fastaFile) {
200 string sequence = "";
203 letter=fastaFile.get();
204 if((letter == '\r') || (letter == '\n')){
205 gobble(fastaFile); //in case its a \r\n situation
212 catch(exception& e) {
213 m->errorOut(e, "Sequence", "getCommentString");
217 //********************************************************************************************************************
219 void Sequence::initialize(){
231 longHomoPolymer = -1;
236 //********************************************************************************************************************
238 void Sequence::setName(string seqName) {
239 if(seqName[0] == '>') { name = seqName.substr(1); }
240 else { name = seqName; }
243 //********************************************************************************************************************
245 void Sequence::setUnaligned(string sequence){
247 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
249 for(int j=0;j<sequence.length();j++) {
250 if(isalpha(sequence[j])) { temp += sequence[j]; }
255 unaligned = sequence;
257 numBases = unaligned.length();
261 //********************************************************************************************************************
263 void Sequence::setAligned(string sequence){
265 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
267 alignmentLength = aligned.length();
268 setUnaligned(sequence);
270 if(aligned[0] == '-'){
271 for(int i=0;i<alignmentLength;i++){
272 if(aligned[i] == '-'){
279 for(int i=alignmentLength-1;i>=0;i--){
280 if(aligned[i] == '-'){
291 //********************************************************************************************************************
293 void Sequence::setPairwise(string sequence){
297 //********************************************************************************************************************
299 string Sequence::convert2ints() {
301 if(unaligned == "") { /* need to throw an error */ }
305 for(int i=0;i<unaligned.length();i++) {
306 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
307 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
308 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
309 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
310 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
311 else { processed += '4'; }
316 //********************************************************************************************************************
318 string Sequence::getName(){
322 //********************************************************************************************************************
324 string Sequence::getAligned(){
328 //********************************************************************************************************************
330 string Sequence::getPairwise(){
334 //********************************************************************************************************************
336 string Sequence::getUnaligned(){
340 //********************************************************************************************************************
342 int Sequence::getNumBases(){
346 //********************************************************************************************************************
348 void Sequence::printSequence(ostream& out){
350 out << ">" << name << endl;
352 out << aligned << endl;
355 out << unaligned << endl;
359 //********************************************************************************************************************
361 int Sequence::getAlignLength(){
362 return alignmentLength;
365 //********************************************************************************************************************
367 int Sequence::getAmbigBases(){
368 if(ambigBases == -1){
370 for(int j=0;j<numBases;j++){
371 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
380 //********************************************************************************************************************
382 int Sequence::getLongHomoPolymer(){
383 if(longHomoPolymer == -1){
386 for(int j=1;j<numBases;j++){
387 if(unaligned[j] == unaligned[j-1]){
391 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
395 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
397 return longHomoPolymer;
400 //********************************************************************************************************************
402 int Sequence::getStartPos(){
404 for(int j = 0; j < alignmentLength; j++) {
405 if(aligned[j] != '.'){
411 if(isAligned == 0){ startPos = 1; }
416 //********************************************************************************************************************
418 int Sequence::getEndPos(){
420 for(int j=alignmentLength-1;j>=0;j--){
421 if(aligned[j] != '.'){
427 if(isAligned == 0){ endPos = numBases; }
432 //********************************************************************************************************************
434 bool Sequence::getIsAligned(){
438 //********************************************************************************************************************
440 void Sequence::reverseComplement(){
443 for(int i=numBases-1;i>=0;i--){
444 if(unaligned[i] == 'A') { temp += 'T'; }
445 else if(unaligned[i] == 'T'){ temp += 'A'; }
446 else if(unaligned[i] == 'G'){ temp += 'C'; }
447 else if(unaligned[i] == 'C'){ temp += 'G'; }
448 else { temp += 'N'; }
455 //********************************************************************************************************************