5 * Created by Pat Schloss on 12/15/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
10 #include "sequence.hpp"
12 /***********************************************************************/
18 /***********************************************************************/
20 Sequence::Sequence(string newName, string sequence) {
25 //setUnaligned removes any gap characters for us
26 setUnaligned(sequence);
30 //********************************************************************************************************************
31 //this function will jump over commented out sequences, but if the last sequence in a file is commented out it makes a blank seq
32 Sequence::Sequence(ifstream& fastaFile){
36 name = name.substr(1);
40 while ((name[0] == '#') && fastaFile) {
41 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
42 sequence = getCommentString(fastaFile);
46 name = name.substr(1);
54 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
56 sequence = getSequenceString(fastaFile);
59 //setUnaligned removes any gap characters for us
60 setUnaligned(sequence);
62 //********************************************************************************************************************
63 string Sequence::getSequenceString(ifstream& fastaFile) {
69 letter= fastaFile.get();
71 fastaFile.putback(letter);
74 else if(isprint(letter)){
75 letter = toupper(letter);
76 if(letter == 'U'){letter = 'T';}
84 errorOut(e, "Sequence", "getSequenceString");
88 //********************************************************************************************************************
89 //comment can contain '>' so we need to account for that
90 string Sequence::getCommentString(ifstream& fastaFile) {
96 letter=fastaFile.get();
97 if((letter == '\r') || (letter == '\n')){
98 gobble(fastaFile); //in case its a \r\n situation
105 catch(exception& e) {
106 errorOut(e, "Sequence", "getCommentString");
111 //********************************************************************************************************************
113 void Sequence::initialize(){
125 longHomoPolymer = -1;
130 //********************************************************************************************************************
132 void Sequence::setName(string seqName) {
133 if(seqName[0] == '>') { name = seqName.substr(1); }
134 else { name = seqName; }
137 //********************************************************************************************************************
139 void Sequence::setUnaligned(string sequence){
141 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
143 for(int j=0;j<sequence.length();j++) {
144 if(isalpha(sequence[j])) { temp += sequence[j]; }
149 unaligned = sequence;
151 numBases = unaligned.length();
155 //********************************************************************************************************************
157 void Sequence::setAligned(string sequence){
159 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
161 alignmentLength = aligned.length();
162 setUnaligned(sequence);
164 if(aligned[0] == '-'){
165 for(int i=0;i<alignmentLength;i++){
166 if(aligned[i] == '-'){
173 for(int i=alignmentLength-1;i>=0;i--){
174 if(aligned[i] == '-'){
185 //********************************************************************************************************************
187 void Sequence::setPairwise(string sequence){
191 //********************************************************************************************************************
193 string Sequence::convert2ints() {
195 if(unaligned == "") { /* need to throw an error */ }
199 for(int i=0;i<unaligned.length();i++) {
200 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
201 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
202 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
203 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
204 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
205 else { processed += '4'; }
210 //********************************************************************************************************************
212 string Sequence::getName(){
216 //********************************************************************************************************************
218 string Sequence::getAligned(){
222 //********************************************************************************************************************
224 string Sequence::getPairwise(){
228 //********************************************************************************************************************
230 string Sequence::getUnaligned(){
234 //********************************************************************************************************************
236 int Sequence::getNumBases(){
240 //********************************************************************************************************************
242 void Sequence::printSequence(ostream& out){
244 out << ">" << name << endl;
246 out << aligned << endl;
249 out << unaligned << endl;
253 //********************************************************************************************************************
255 int Sequence::getAlignLength(){
256 return alignmentLength;
259 //********************************************************************************************************************
261 int Sequence::getAmbigBases(){
262 if(ambigBases == -1){
264 for(int j=0;j<numBases;j++){
265 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
274 //********************************************************************************************************************
276 int Sequence::getLongHomoPolymer(){
277 if(longHomoPolymer == -1){
280 for(int j=1;j<numBases;j++){
281 if(unaligned[j] == unaligned[j-1]){
285 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
289 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
291 return longHomoPolymer;
294 //********************************************************************************************************************
296 int Sequence::getStartPos(){
298 for(int j = 0; j < alignmentLength; j++) {
299 if(aligned[j] != '.'){
305 if(isAligned == 0){ startPos = 1; }
310 //********************************************************************************************************************
312 int Sequence::getEndPos(){
314 for(int j=alignmentLength-1;j>=0;j--){
315 if(aligned[j] != '.'){
321 if(isAligned == 0){ endPos = numBases; }
326 //********************************************************************************************************************
328 bool Sequence::getIsAligned(){
332 //********************************************************************************************************************
334 void Sequence::reverseComplement(){
337 for(int i=numBases-1;i>=0;i--){
338 if(unaligned[i] == 'A') { temp += 'T'; }
339 else if(unaligned[i] == 'T'){ temp += 'A'; }
340 else if(unaligned[i] == 'G'){ temp += 'C'; }
341 else if(unaligned[i] == 'C'){ temp += 'G'; }
342 else { temp += 'N'; }
349 //********************************************************************************************************************