5 * Created by Pat Schloss on 12/15/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
12 #include "sequence.hpp"
14 /***********************************************************************/
16 Sequence::Sequence() {}
18 /***********************************************************************/
20 Sequence::Sequence(string newName, string sequence) {
22 if(sequence.find_first_of('-') != string::npos) {
25 setUnaligned(sequence);
27 //********************************************************************************************************************
29 Sequence::Sequence(ifstream& fastaFile){
31 string accession; // provided a file handle to a fasta-formatted sequence file, read in the next
32 fastaFile >> accession; // accession number and sequence we find...
39 letter= fastaFile.get();
41 fastaFile.putback(letter);
44 else if(isprint(letter)){
45 letter = toupper(letter);
46 if(letter == 'U'){letter = 'T';}
52 if(sequence.find_first_of('-') != string::npos){ // if there are any gaps in the sequence, assume that it is
53 setAligned(sequence); // an alignment file
55 setUnaligned(sequence); // also set the unaligned sequence file
58 //********************************************************************************************************************
60 string Sequence::convert2ints() {
62 if(unaligned == "") { /* need to throw an error */ }
66 for(int i=0;i<unaligned.length();i++) {
67 if(toupper(unaligned[i]) == 'A') { processed += '0'; }
68 else if(toupper(unaligned[i]) == 'C') { processed += '1'; }
69 else if(toupper(unaligned[i]) == 'G') { processed += '2'; }
70 else if(toupper(unaligned[i]) == 'T') { processed += '3'; }
71 else if(toupper(unaligned[i]) == 'U') { processed += '3'; }
72 else { processed += '4'; }
77 //********************************************************************************************************************
79 void Sequence::setName(string seqName) {
80 if(seqName[0] == '>') { name = seqName.substr(1); }
81 else { name = seqName; }
84 //********************************************************************************************************************
86 void Sequence::setUnaligned(string sequence){
88 if(sequence.find_first_of('-') != string::npos) {
90 for(int j=0;j<sequence.length();j++) {
91 if(isalpha(sequence[j])) { temp += sequence[j]; }
101 //********************************************************************************************************************
103 void Sequence::setAligned(string sequence){
105 //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
107 if(sequence[0] == '-'){
108 for(int i=0;i<sequence.length();i++){
109 if(sequence[i] == '-'){
116 for(int i=sequence.length()-1;i>=0;i--){
117 if(sequence[i] == '-'){
128 //********************************************************************************************************************
130 void Sequence::setPairwise(string sequence){
134 //********************************************************************************************************************
136 string Sequence::getName(){
140 //********************************************************************************************************************
142 string Sequence::getAligned(){
146 //********************************************************************************************************************
148 string Sequence::getPairwise(){
152 //********************************************************************************************************************
154 string Sequence::getUnaligned(){
158 //********************************************************************************************************************
160 int Sequence::getLength(){
161 if(unaligned.length() > aligned.length())
162 return unaligned.length();
163 return aligned.length();
166 //********************************************************************************************************************
168 void Sequence::printSequence(ostream& out){
169 string toPrint = unaligned;
170 if(aligned.length() > unaligned.length())
172 out << ">" << name << "\n" << toPrint << "\n";
175 //********************************************************************************************************************
177 int Sequence::getUnalignLength(){
178 return unaligned.length();
181 //********************************************************************************************************************
183 int Sequence::getAlignLength(){
184 return aligned.length();
187 //********************************************************************************************************************