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) {
26 //setUnaligned removes any gap characters for us
27 setUnaligned(sequence);
31 //********************************************************************************************************************
32 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
33 Sequence::Sequence(ifstream& fastaFile){
37 name = name.substr(1);
41 while ((name[0] == '#') && fastaFile) {
42 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
43 sequence = getCommentString(fastaFile);
47 name = name.substr(1);
55 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
57 sequence = getSequenceString(fastaFile);
60 //setUnaligned removes any gap characters for us
61 setUnaligned(sequence);
63 //********************************************************************************************************************
64 string Sequence::getSequenceString(ifstream& fastaFile) {
70 letter= fastaFile.get();
72 fastaFile.putback(letter);
75 else if(isprint(letter)){
76 letter = toupper(letter);
77 if(letter == 'U'){letter = 'T';}
85 m->errorOut(e, "Sequence", "getSequenceString");
89 //********************************************************************************************************************
90 //comment can contain '>' so we need to account for that
91 string Sequence::getCommentString(ifstream& fastaFile) {
97 letter=fastaFile.get();
98 if((letter == '\r') || (letter == '\n')){
99 gobble(fastaFile); //in case its a \r\n situation
106 catch(exception& e) {
107 m->errorOut(e, "Sequence", "getCommentString");
112 //********************************************************************************************************************
114 void Sequence::initialize(){
126 longHomoPolymer = -1;
131 //********************************************************************************************************************
133 void Sequence::setName(string seqName) {
134 if(seqName[0] == '>') { name = seqName.substr(1); }
135 else { name = seqName; }
138 //********************************************************************************************************************
140 void Sequence::setUnaligned(string sequence){
142 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
144 for(int j=0;j<sequence.length();j++) {
145 if(isalpha(sequence[j])) { temp += sequence[j]; }
150 unaligned = sequence;
152 numBases = unaligned.length();
156 //********************************************************************************************************************
158 void Sequence::setAligned(string sequence){
160 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
162 alignmentLength = aligned.length();
163 setUnaligned(sequence);
165 if(aligned[0] == '-'){
166 for(int i=0;i<alignmentLength;i++){
167 if(aligned[i] == '-'){
174 for(int i=alignmentLength-1;i>=0;i--){
175 if(aligned[i] == '-'){
186 //********************************************************************************************************************
188 void Sequence::setPairwise(string sequence){
192 //********************************************************************************************************************
194 string Sequence::convert2ints() {
196 if(unaligned == "") { /* need to throw an error */ }
200 for(int i=0;i<unaligned.length();i++) {
201 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
202 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
203 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
204 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
205 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
206 else { processed += '4'; }
211 //********************************************************************************************************************
213 string Sequence::getName(){
217 //********************************************************************************************************************
219 string Sequence::getAligned(){
223 //********************************************************************************************************************
225 string Sequence::getPairwise(){
229 //********************************************************************************************************************
231 string Sequence::getUnaligned(){
235 //********************************************************************************************************************
237 int Sequence::getNumBases(){
241 //********************************************************************************************************************
243 void Sequence::printSequence(ostream& out){
245 out << ">" << name << endl;
247 out << aligned << endl;
250 out << unaligned << endl;
254 //********************************************************************************************************************
256 int Sequence::getAlignLength(){
257 return alignmentLength;
260 //********************************************************************************************************************
262 int Sequence::getAmbigBases(){
263 if(ambigBases == -1){
265 for(int j=0;j<numBases;j++){
266 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
275 //********************************************************************************************************************
277 int Sequence::getLongHomoPolymer(){
278 if(longHomoPolymer == -1){
281 for(int j=1;j<numBases;j++){
282 if(unaligned[j] == unaligned[j-1]){
286 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
290 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
292 return longHomoPolymer;
295 //********************************************************************************************************************
297 int Sequence::getStartPos(){
299 for(int j = 0; j < alignmentLength; j++) {
300 if(aligned[j] != '.'){
306 if(isAligned == 0){ startPos = 1; }
311 //********************************************************************************************************************
313 int Sequence::getEndPos(){
315 for(int j=alignmentLength-1;j>=0;j--){
316 if(aligned[j] != '.'){
322 if(isAligned == 0){ endPos = numBases; }
327 //********************************************************************************************************************
329 bool Sequence::getIsAligned(){
333 //********************************************************************************************************************
335 void Sequence::reverseComplement(){
338 for(int i=numBases-1;i>=0;i--){
339 if(unaligned[i] == 'A') { temp += 'T'; }
340 else if(unaligned[i] == 'T'){ temp += 'A'; }
341 else if(unaligned[i] == 'G'){ temp += 'C'; }
342 else if(unaligned[i] == 'C'){ temp += 'G'; }
343 else { temp += 'N'; }
350 //********************************************************************************************************************