]> git.donarmstrong.com Git - mothur.git/blob - sequence.cpp
This contains Pat's bug fixes and updates. It represents mothur v.1.3.0
[mothur.git] / sequence.cpp
1 /*
2  *  sequence.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 12/15/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 using namespace std;
11
12 #include "sequence.hpp"
13
14 /***********************************************************************/
15
16 Sequence::Sequence()  {}
17
18 /***********************************************************************/
19
20 Sequence::Sequence(string newName, string sequence) {
21         name = newName;
22         if(sequence.find_first_of('-') != string::npos) {
23                 setAligned(sequence);
24         }
25         setUnaligned(sequence);
26 }
27 //********************************************************************************************************************
28
29 Sequence::Sequence(ifstream& fastaFile){
30         
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...
33         setName(accession);
34
35         char letter;
36         string sequence;
37         
38         while(fastaFile){
39                 letter= fastaFile.get();
40                 if(letter == '>'){
41                         fastaFile.putback(letter);
42                         break;
43                 }
44                 else if(isprint(letter)){
45                         letter = toupper(letter);
46                         if(letter == 'U'){letter = 'T';}
47                         sequence += letter;
48                 }
49                 
50         }
51
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
54         }
55         setUnaligned(sequence);                                                         //      also set the unaligned sequence file
56 }
57
58 //********************************************************************************************************************
59
60 string Sequence::convert2ints() {
61         
62         if(unaligned == "")     {       /* need to throw an error */    }
63         
64         string processed;
65         
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';       }
73         }
74         return processed;
75 }
76
77 //********************************************************************************************************************
78
79 void Sequence::setName(string seqName) {
80         if(seqName[0] == '>')   {       name = seqName.substr(1);       }
81         else                                    {       name = seqName;                         }
82 }
83
84 //********************************************************************************************************************
85
86 void Sequence::setUnaligned(string sequence){
87         
88         if(sequence.find_first_of('-') != string::npos) {
89                 string temp = "";
90                 for(int j=0;j<sequence.length();j++) {
91                         if(isalpha(sequence[j]))        {       temp += sequence[j];    }
92                 }
93                 unaligned = temp;
94         }
95         else {
96                 unaligned = sequence;
97         }
98         
99 }
100
101 //********************************************************************************************************************
102
103 void Sequence::setAligned(string sequence){
104         
105         //if the alignment starts or ends with a gap, replace it with a period to indicate missing data
106         
107         if(sequence[0] == '-'){
108                 for(int i=0;i<sequence.length();i++){
109                         if(sequence[i] == '-'){
110                                 sequence[i] = '.';
111                         }
112                         else{
113                                 break;
114                         }
115                 }
116                 for(int i=sequence.length()-1;i>=0;i--){
117                         if(sequence[i] == '-'){
118                                 sequence[i] = '.';
119                         }
120                         else{
121                                 break;
122                         }
123                 }
124         }
125         aligned = sequence;
126 }
127
128 //********************************************************************************************************************
129
130 void Sequence::setPairwise(string sequence){
131         pairwise = sequence;
132 }
133
134 //********************************************************************************************************************
135
136 string Sequence::getName(){
137         return name;
138 }
139
140 //********************************************************************************************************************
141
142 string Sequence::getAligned(){
143         return aligned;
144 }
145
146 //********************************************************************************************************************
147
148 string Sequence::getPairwise(){
149         return pairwise;
150 }
151
152 //********************************************************************************************************************
153
154 string Sequence::getUnaligned(){
155         return unaligned;
156 }
157
158 //********************************************************************************************************************
159
160 int Sequence::getLength(){
161         if(unaligned.length() > aligned.length())
162                 return unaligned.length();
163         return aligned.length();
164 }
165
166 //********************************************************************************************************************
167
168 void Sequence::printSequence(ostream& out){
169         string toPrint = unaligned;
170         if(aligned.length() > unaligned.length())
171                 toPrint = aligned;
172         out << ">" << name << "\n" << toPrint << "\n";
173 }
174
175 //********************************************************************************************************************
176
177 int Sequence::getUnalignLength(){
178         return unaligned.length();
179 }
180
181 //********************************************************************************************************************
182
183 int Sequence::getAlignLength(){
184         return aligned.length();
185 }
186
187 //********************************************************************************************************************
188
189
190