]> git.donarmstrong.com Git - mothur.git/blob - sequencedb.cpp
changed random forest output filename
[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() {  m = MothurOut::getInstance();  length = 0; samelength = true; }
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         length = 0; samelength = true;
28 }
29
30 /***********************************************************************/
31
32 SequenceDB::SequenceDB(ifstream& filehandle) {
33         try{
34                 length = 0; samelength = true;
35                                 
36                 //read through file
37                 while (!filehandle.eof()) {
38                         //input sequence info into sequencedb
39                         Sequence newSequence(filehandle);
40                         
41                         if (newSequence.getName() != "") {   
42                                 if (length == 0) { length = newSequence.getAligned().length(); }
43                                 if (length != newSequence.getAligned().length()) { samelength = false; }
44                                 data.push_back(newSequence);  
45                         }
46                         
47                         //takes care of white space
48                         m->gobble(filehandle);
49                 }
50
51                 filehandle.close();
52                 
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "SequenceDB", "SequenceDB");
56                 exit(1);
57         }
58 }
59 /*******************************************************************************/
60 string SequenceDB::readName(ifstream& in) {
61         try{
62                 string name = "";
63                 int c;
64                 string temp;
65                 
66                 while ((c = in.get()) != EOF) {
67                         //if c is not a line return
68                         if (c != 10) {
69                                 name += c;
70                         }else { break;  }
71                 }
72                         
73                 return name;
74         }
75         catch(exception& e) {
76                 m->errorOut(e, "SequenceDB", "readName");
77                 exit(1);
78         }
79 }
80
81 /*******************************************************************************/
82 string SequenceDB::readSequence(ifstream& in) {
83         try{
84                 string sequence = "";
85                 string line;
86                 int pos, c;
87                 
88                 while (!in.eof()) {
89                         //save position in file in case next line is a new name.
90                         pos = in.tellg();
91                         line = "";
92                         in >> line;                     
93                         //if you are at a new name
94                         if (line[0] == '>') {
95                                 //put file pointer back since you are now at a new name
96                                 in.seekg(pos, ios::beg);
97                                 c = in.get();  //because you put it back to a newline char
98                                 break;
99                         }else {  sequence += line;      }
100                 }
101                 
102                 if (length == 0) { length = sequence.length(); }
103                 if (length != sequence.length()) { samelength = false; }
104                 
105                 return sequence;
106         }
107         catch(exception& e) {
108                 m->errorOut(e, "SequenceDB", "readSequence");
109                 exit(1);
110         }
111 }
112         
113 /***********************************************************************/
114
115 int SequenceDB::getNumSeqs() {
116         return data.size();
117 }
118
119 /***********************************************************************/
120
121 void SequenceDB::set(int index, string newUnaligned) {
122         try {
123                 if (length == 0) { length = newUnaligned.length(); }
124                 if (length != newUnaligned.length()) { samelength = false; }
125                 
126                 data[index] = Sequence(data[index].getName(), newUnaligned);
127         }
128         catch(exception& e) {
129                 m->errorOut(e, "SequenceDB", "set");
130                 exit(1);
131         }
132 }
133
134 /***********************************************************************/
135
136 void SequenceDB::set(int index, Sequence newSeq) {
137         try {
138                 if (length == 0) { length = newSeq.getAligned().length(); }
139                 if (length != newSeq.getAligned().length()) { samelength = false; }
140
141                 data[index] = newSeq;
142         }
143         catch(exception& e) {
144                 m->errorOut(e, "SequenceDB", "set");
145                 exit(1);
146         }
147 }
148
149 /***********************************************************************/
150
151 Sequence SequenceDB::get(int index) {
152         return data[index];
153 }
154
155 /***********************************************************************/
156
157 void SequenceDB::resize(int newSize) {
158         try {
159                 data.resize(newSize);
160         }
161         catch(exception& e) {
162                 m->errorOut(e, "SequenceDB", "resize");
163                 exit(1);
164         }
165 }
166
167 /***********************************************************************/
168
169 void SequenceDB::clear() {
170         try {
171                 data.clear();
172         }
173         catch(exception& e) {
174                 m->errorOut(e, "SequenceDB", "clear");
175                 exit(1);
176         }
177 }
178
179 /***********************************************************************/
180
181 int SequenceDB::size() {
182         return data.size();
183 }
184
185 /***********************************************************************/
186
187 void SequenceDB::print(ostream& out) {
188         try {
189                 for(int i = 0; i < data.size(); i++) {
190                         data[i].printSequence(out);
191                 }
192         }
193         catch(exception& e) {
194                 m->errorOut(e, "SequenceDB", "print");
195                 exit(1);
196         }
197 }
198         
199 /***********************************************************************/
200
201 void SequenceDB::push_back(Sequence newSequence) {
202         try {
203                 if (length == 0) { length = newSequence.getAligned().length(); }
204                 if (length != newSequence.getAligned().length()) { samelength = false; }
205
206                 data.push_back(newSequence);
207         }
208         catch(exception& e) {
209                 m->errorOut(e, "SequenceDB", "push_back");
210                 exit(1);
211         }
212 }
213
214 /***********************************************************************/
215