]> git.donarmstrong.com Git - mothur.git/blob - sequencedb.cpp
merged pat's trim seqs edits with sarah's major overhaul of global data; also added...
[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, Sequence());
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(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                 data[index] = Sequence(data[index].getName(), newUnaligned);
138         }
139         catch(exception& e) {
140                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141                 exit(1);
142         }
143         catch(...) {
144                 cout << "An unknown error has occurred in the SequenceDB class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145                 exit(1);
146         }
147 }
148
149 /***********************************************************************/
150
151 void SequenceDB::set(int index, Sequence newSeq) {
152         try {
153                 data[index] = newSeq;
154         }
155         catch(exception& e) {
156                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
157                 exit(1);
158         }
159         catch(...) {
160                 cout << "An unknown error has occurred in the SequenceDB class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161                 exit(1);
162         }
163 }
164
165 /***********************************************************************/
166
167 Sequence SequenceDB::get(int index) {
168         return data[index];
169 }
170
171 /***********************************************************************/
172
173 void SequenceDB::resize(int newSize) {
174         try {
175                 data.resize(newSize);
176         }
177         catch(exception& e) {
178                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function resize. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179                 exit(1);
180         }
181         catch(...) {
182                 cout << "An unknown error has occurred in the SequenceDB class function resize. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }
185 }
186
187 /***********************************************************************/
188
189 void SequenceDB::clear() {
190         try {
191                 data.clear();
192         }
193         catch(exception& e) {
194                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function clear. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
195                 exit(1);
196         }
197         catch(...) {
198                 cout << "An unknown error has occurred in the SequenceDB class function clear. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199                 exit(1);
200         }
201 }
202
203 /***********************************************************************/
204
205 int SequenceDB::size() {
206         return data.size();
207 }
208
209 /***********************************************************************/
210
211 void SequenceDB::print(ostream& out) {
212         try {
213                 for(int i = 0; i < data.size(); i++) {
214                         data[i].printSequence(out);
215                 }
216         }
217         catch(exception& e) {
218                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
219                 exit(1);
220         }
221         catch(...) {
222                 cout << "An unknown error has occurred in the SequenceDB class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
223                 exit(1);
224         }
225 }
226         
227 /***********************************************************************/
228
229 void SequenceDB::push_back(Sequence newSequence) {
230         try {
231                 data.push_back(newSequence);
232         }
233         catch(exception& e) {
234                 cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function add. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235                 exit(1);
236         }
237         catch(...) {
238                 cout << "An unknown error has occurred in the SequenceDB class function add. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }
241 }
242
243 /***********************************************************************/
244