]> git.donarmstrong.com Git - mothur.git/blob - sequencedb.cpp
e0bd10033459896f10c2d42fb9b3b4f6921add7f
[mothur.git] / sequencedb.cpp
1 /*
2  *  sequencedb.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 4/13/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sequencedb.h"
11 #include "sequence.hpp"
12 #include "mothur.h"
13 #include "calculator.h"
14
15
16 /***********************************************************************/
17
18 SequenceDB::SequenceDB() {}
19 /***********************************************************************/
20 //the clear function free's the memory
21 SequenceDB::~SequenceDB() { clear(); }
22
23 /***********************************************************************/
24
25 SequenceDB::SequenceDB(int newSize) {
26         data.resize(newSize, NULL);
27 }
28
29 /***********************************************************************/
30
31 SequenceDB::SequenceDB(ifstream& filehandle) {
32         try{
33                 string name, sequence, line;
34                 sequence = "";
35                 int c;
36                 string temp;
37                 
38                 
39                 //read through file
40                 while ((c = filehandle.get()) != EOF) {
41                         name = ""; sequence = ""; 
42                         //is this a name
43                         if (c == '>') { 
44                                 name = readName(filehandle); 
45                                 sequence = readSequence(filehandle); 
46                         }else {  cout << "Error fasta in your file. Please correct." << endl; }
47
48                         //input sequence info into sequencedb
49                         Sequence* newSequence = new Sequence(name, sequence);
50                         data.push_back(newSequence);
51                         
52                         //takes care of white space
53                         gobble(filehandle);
54                 }
55
56                 filehandle.close();
57                 
58         }
59         catch(exception& e) {
60                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function SequenceDB. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
61                 exit(1);
62         }
63         catch(...) {
64                 cout << "An unknown error has occurred in the SequenceDB class function SequenceDB. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }
67 }
68 /*******************************************************************************/
69 string SequenceDB::readName(ifstream& in) {
70         try{
71                 string name = "";
72                 int c;
73                 string temp;
74                 
75                 while ((c = in.get()) != EOF) {
76                         //if c is not a line return
77                         if (c != 10) {
78                                 name += c;
79                         }else { break;  }
80                 }
81                         
82                 return name;
83         }
84         catch(exception& e) {
85                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function readName. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
86                 exit(1);
87         }
88         catch(...) {
89                 cout << "An unknown error has occurred in the SequenceDB class function readName. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
90                 exit(1);
91         }
92 }
93
94 /*******************************************************************************/
95 string SequenceDB::readSequence(ifstream& in) {
96         try{
97                 string sequence = "";
98                 string line;
99                 int pos, c;
100                 
101                 while (!in.eof()) {
102                         //save position in file in case next line is a new name.
103                         pos = in.tellg();
104                         line = "";
105                         in >> line;                     
106                         //if you are at a new name
107                         if (line[0] == '>') {
108                                 //put file pointer back since you are now at a new name
109                                 in.seekg(pos, ios::beg);
110                                 c = in.get();  //because you put it back to a newline char
111                                 break;
112                         }else {  sequence += line;      }
113                 }
114                         
115                 return sequence;
116         }
117         catch(exception& e) {
118                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function readSequence. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
119                 exit(1);
120         }
121         catch(...) {
122                 cout << "An unknown error has occurred in the SequenceDB class function readSequence. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
123                 exit(1);
124         }
125 }
126         
127 /***********************************************************************/
128
129 int SequenceDB::getNumSeqs() {
130         return data.size();
131 }
132
133 /***********************************************************************/
134
135 void SequenceDB::set(int index, string newUnaligned) {
136         try {
137                 if (data[index] != NULL) {  delete data[index];  } //free memory
138         
139                 Sequence* newSeq = new Sequence(data[index]->getName(), newUnaligned);
140                 data[index] = newSeq;
141         }
142         catch(exception& e) {
143                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144                 exit(1);
145         }
146         catch(...) {
147                 cout << "An unknown error has occurred in the SequenceDB class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148                 exit(1);
149         }
150 }
151
152 /***********************************************************************/
153
154 void SequenceDB::set(int index, Sequence* newSeq) {
155         try {
156                 if (data[index] != NULL) {  delete data[index];  } //free memory
157                 data[index] = newSeq;
158         }
159         catch(exception& e) {
160                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161                 exit(1);
162         }
163         catch(...) {
164                 cout << "An unknown error has occurred in the SequenceDB class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165                 exit(1);
166         }
167 }
168
169 /***********************************************************************/
170
171 Sequence* SequenceDB::get(int index) {
172         return data[index];
173 }
174
175 /***********************************************************************/
176
177 void SequenceDB::resize(int newSize) {
178         try {
179                 int size = data.size();
180         
181                 for (int i = size; i > newSize; i--) {  delete data[i]; }
182                 data.resize(newSize);
183         }
184         catch(exception& e) {
185                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function resize. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186                 exit(1);
187         }
188         catch(...) {
189                 cout << "An unknown error has occurred in the SequenceDB class function resize. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190                 exit(1);
191         }
192 }
193
194 /***********************************************************************/
195
196 void SequenceDB::clear() {
197         try {
198                 for (int i = 0; i < data.size(); i++) { delete data[i];  }
199                 data.clear();
200         }
201         catch(exception& e) {
202                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function clear. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203                 exit(1);
204         }
205         catch(...) {
206                 cout << "An unknown error has occurred in the SequenceDB class function clear. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207                 exit(1);
208         }
209 }
210
211 /***********************************************************************/
212
213 int SequenceDB::size() {
214         return data.size();
215 }
216
217 /***********************************************************************/
218
219 void SequenceDB::print(ostream& out) {
220         try {
221                 for(int i = 0; i < data.size(); i++) {
222                         data[i]->printSequence(out);
223                 }
224         }
225         catch(exception& e) {
226                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227                 exit(1);
228         }
229         catch(...) {
230                 cout << "An unknown error has occurred in the SequenceDB class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
231                 exit(1);
232         }
233 }
234         
235 /***********************************************************************/
236
237 void SequenceDB::push_back(Sequence* newSequence) {
238         try {
239                 data.push_back(newSequence);
240         }
241         catch(exception& e) {
242                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function add. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
243                 exit(1);
244         }
245         catch(...) {
246                 cout << "An unknown error has occurred in the SequenceDB class function add. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
247                 exit(1);
248         }
249 }
250
251 /***********************************************************************/
252