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) {
24 if(sequence.find_first_of('-') != string::npos) {
27 setUnaligned(sequence);
30 //********************************************************************************************************************
32 Sequence::Sequence(ifstream& fastaFile){
36 name = name.substr(1);
38 while (!fastaFile.eof()) { char c = fastaFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
44 letter= fastaFile.get();
46 fastaFile.putback(letter);
49 else if(isprint(letter)){
50 letter = toupper(letter);
51 if(letter == 'U'){letter = 'T';}
56 if((sequence.find_first_of('-') != string::npos) || (sequence.find_first_of('.') != string::npos)) { // if there are any gaps in the sequence, assume that it is
57 setAligned(sequence); // an alignment file
59 setUnaligned(sequence); // also set the unaligned sequence file
62 //********************************************************************************************************************
64 void Sequence::initialize(){
81 //********************************************************************************************************************
83 void Sequence::setName(string seqName) {
84 if(seqName[0] == '>') { name = seqName.substr(1); }
85 else { name = seqName; }
88 //********************************************************************************************************************
90 void Sequence::setUnaligned(string sequence){
92 if(sequence.find_first_of('.') != string::npos || sequence.find_first_of('-') != string::npos) {
94 for(int j=0;j<sequence.length();j++) {
95 if(isalpha(sequence[j])) { temp += sequence[j]; }
100 unaligned = sequence;
102 numBases = unaligned.length();
106 //********************************************************************************************************************
108 void Sequence::setAligned(string sequence){
110 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
112 alignmentLength = aligned.length();
114 if(aligned[0] == '-'){
115 for(int i=0;i<alignmentLength;i++){
116 if(aligned[i] == '-'){
123 for(int i=alignmentLength-1;i>=0;i--){
124 if(aligned[i] == '-'){
135 //********************************************************************************************************************
137 void Sequence::setPairwise(string sequence){
141 //********************************************************************************************************************
143 string Sequence::convert2ints() {
145 if(unaligned == "") { /* need to throw an error */ }
149 for(int i=0;i<unaligned.length();i++) {
150 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
151 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
152 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
153 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
154 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
155 else { processed += '4'; }
160 //********************************************************************************************************************
162 string Sequence::getName(){
166 //********************************************************************************************************************
168 string Sequence::getAligned(){
172 //********************************************************************************************************************
174 string Sequence::getPairwise(){
178 //********************************************************************************************************************
180 string Sequence::getUnaligned(){
184 //********************************************************************************************************************
186 int Sequence::getNumBases(){
190 //********************************************************************************************************************
192 void Sequence::printSequence(ostream& out){
194 out << ">" << name << endl;
196 out << aligned << endl;
199 out << unaligned << endl;
203 //********************************************************************************************************************
205 int Sequence::getAlignLength(){
206 return alignmentLength;
209 //********************************************************************************************************************
211 int Sequence::getAmbigBases(){
212 if(ambigBases == -1){
214 for(int j=0;j<numBases;j++){
215 if(unaligned[j] != 'A' && unaligned[j] != 'T' && unaligned[j] != 'G' && unaligned[j] != 'C'){
224 //********************************************************************************************************************
226 int Sequence::getLongHomoPolymer(){
227 if(longHomoPolymer == -1){
230 for(int j=1;j<numBases;j++){
231 if(unaligned[j] == unaligned[j-1]){
235 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
239 if(homoPolymer > longHomoPolymer){ longHomoPolymer = homoPolymer; }
241 return longHomoPolymer;
244 //********************************************************************************************************************
246 int Sequence::getStartPos(){
248 for(int j = 0; j < alignmentLength; j++) {
249 if(aligned[j] != '.'){
255 if(isAligned == 0){ startPos = 1; }
260 //********************************************************************************************************************
262 int Sequence::getEndPos(){
264 for(int j=alignmentLength-1;j>=0;j--){
265 if(aligned[j] != '.'){
271 if(isAligned == 0){ endPos = numBases; }
276 //********************************************************************************************************************
278 bool Sequence::getIsAligned(){
282 //********************************************************************************************************************
284 void Sequence::reverseComplement(){
287 for(int i=numBases-1;i>=0;i--){
288 if(unaligned[i] == 'A') { temp += 'T'; }
289 else if(unaligned[i] == 'T'){ temp += 'A'; }
290 else if(unaligned[i] == 'G'){ temp += 'C'; }
291 else if(unaligned[i] == 'C'){ temp += 'G'; }
292 else { temp += 'N'; }
298 //********************************************************************************************************************