]> git.donarmstrong.com Git - mothur.git/blob - sharedordervector.cpp
finishing the container classes, combining read.otu and read.list commands. some...
[mothur.git] / sharedordervector.cpp
1 /*
2  *  sharedSharedOrderVector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/9/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 using namespace std;
11
12
13 #include "sharedordervector.h"
14 #include "utilities.hpp"
15 #include <exception>
16
17 /***********************************************************************/
18
19 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0)  {}
20
21 /***********************************************************************/
22
23 SharedOrderVector::SharedOrderVector(string id, vector<individual>  ov) : 
24                                                                                         DataVector(id), data(ov)
25 {
26                 updateStats();
27 }
28
29 /***********************************************************************
30
31 //does not work since we don't have a shared order file format yet.
32
33 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() {
34         try {
35                 int hold;
36         
37                 f >> label;
38                 f >> hold;
39         
40                 data.assign(hold, -1);
41         
42                 int inputData;
43         
44                 for(int i=0;i<hold;i++){
45                         f >> inputData;
46                         set(i, inputData, inputData, group);
47                 }
48         
49                 updateStats();
50         }
51         catch(exception& e) {
52                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53                 exit(1);
54         }
55         catch(...) {
56                 cout << "An unknown error has occurred in the SharedOrderVector class function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57                 exit(1);
58         }
59 }
60
61 /***********************************************************************/
62
63
64 int SharedOrderVector::getNumBins(){
65         if(needToUpdate == 1){  updateStats();  }
66         return numBins;
67 }
68
69 /***********************************************************************/
70
71 int SharedOrderVector::getNumSeqs(){
72         if(needToUpdate == 1){  updateStats();  }
73         return numSeqs;
74 }
75
76 /***********************************************************************/
77
78 int SharedOrderVector::getMaxRank(){
79         if(needToUpdate == 1){  updateStats();  }
80         return maxRank;
81 }
82
83
84 /***********************************************************************/
85
86
87
88 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
89         
90         data[index].group = groupName;
91         data[index].bin = binNumber;
92         data[index].abundance = abund;
93         needToUpdate = 1;
94         
95 }
96
97 /***********************************************************************/
98
99 individual SharedOrderVector::get(int index){
100         return data[index];                     
101 }
102
103
104 /***********************************************************************/
105
106 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
107         individual newGuy;
108         newGuy.group = groupName;
109         newGuy.abundance = abund;
110         newGuy.bin = binNumber;
111         data.push_back(newGuy);
112         needToUpdate = 1;
113         
114 }
115
116 /***********************************************************************/
117
118 void SharedOrderVector::print(ostream& output){
119         try {
120                 output << label << '\t' << numSeqs << '\t';
121         
122                 for(int i=0;i<data.size();i++){
123                         output << data[i].bin << '\t';
124                 }
125                 output << endl;
126         }
127         catch(exception& e) {
128                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
129                 exit(1);
130         }
131         catch(...) {
132                 cout << "An unknown error has occurred in the SharedOrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
133                 exit(1);
134         }
135 }
136
137
138 /***********************************************************************/
139
140 void SharedOrderVector::resize(int){
141         cout << "resize() did nothing in class SharedOrderVector";
142 }
143
144 /***********************************************************************/
145
146
147 vector<individual>::iterator SharedOrderVector::begin(){
148         return data.begin();    
149 }
150
151 /***********************************************************************/
152
153 vector<individual>::iterator SharedOrderVector::end(){
154         return data.end();              
155 }
156
157 /***********************************************************************/
158
159 int SharedOrderVector::size(){
160         return data.size();                                     
161 }
162
163 /***********************************************************************/
164
165 RAbundVector SharedOrderVector::getRAbundVector(){
166         try {
167                 RAbundVector rav(data.size());
168         
169                 for(int i=0;i<numSeqs;i++){
170                         rav.set(data[i].bin, rav.get(data[i].bin) + 1);
171                 }       
172                 sort(rav.rbegin(), rav.rend());
173                 for(int i=numSeqs-1;i>=0;i--){
174                         if(rav.get(i) == 0){    rav.pop_back(); }
175                         else{
176                                 break;
177                         }
178                 }
179                 rav.setLabel(label);
180
181                 return rav;
182         }
183         catch(exception& e) {
184                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185                 exit(1);
186         }
187         catch(...) {
188                 cout << "An unknown error has occurred in the SharedOrderVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189                 exit(1);
190         }       
191 }
192 /***********************************************************************/
193
194 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
195         try {
196                 OrderVector ov;
197         
198                 for (int i = 0; i < data.size(); i++) {
199                         ov.push_back(data[i].bin);
200                 }
201                 
202                 random_shuffle(ov.begin(), ov.end());
203
204                 ov.setLabel(label);     
205                 return ov;
206         }
207         catch(exception& e) {
208                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
209                 exit(1);
210         }
211         catch(...) {
212                 cout << "An unknown error has occurred in the SharedOrderVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213                 exit(1);
214         }
215 }
216
217
218 /***********************************************************************/
219
220 SAbundVector SharedOrderVector::getSAbundVector(){
221         
222         RAbundVector rav(this->getRAbundVector());
223         return rav.getSAbundVector();
224
225 }
226 /***********************************************************************/
227 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
228         try {
229                 SharedRAbundVector sharedRav(data.size());
230                 
231                 sharedRav.setLabel(label);
232                 sharedRav.setGroup(group);
233                 
234                 for (int i = 0; i < data.size(); i++) {
235                         if (data[i].group == group) {
236                                 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
237                         }
238                 }
239                 return sharedRav;
240         }
241         catch(exception& e) {
242                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. 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 SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
247                 exit(1);
248         }
249         
250 }
251 /***********************************************************************/
252 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
253         try {
254                 
255                 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
256                 return sharedRav.getSharedSAbundVector();
257                                 
258         }
259         catch(exception& e) {
260                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261                 exit(1);
262         }
263         catch(...) {
264                 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265                 exit(1);
266         }
267         
268 }
269
270 /***********************************************************************/
271
272 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
273         return *this;                   
274 }
275
276 /***********************************************************************/
277
278 void SharedOrderVector::updateStats(){
279         try {
280                 needToUpdate = 0;
281                 numSeqs = 0;
282                 numBins = 0;
283                 maxRank = 0;
284         
285                 for(int i=0;i<data.size();i++){
286                         if(data[i].bin != -1){
287                                 numSeqs++;
288                         }
289                 }
290         
291                 vector<individual> hold(numSeqs);
292         
293                 for(int i=0;i<numSeqs;i++){
294                         if(data[i].bin != -1){
295                                 hold[data[i].bin].bin = hold[data[i].bin].bin+1;
296                         }
297                 }       
298
299                 for(int i=0;i<numSeqs;i++){
300                         if(data[i].bin > numBins) { numBins = data[i].bin;      } 
301                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
302                 }
303                 numBins++; //if you have 10 bins largest .bin is 9 since we start at 0.
304         }
305         catch(exception& e) {
306                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
307                 exit(1);
308         }
309         catch(...) {
310                 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311                 exit(1);
312         }       
313 }
314
315 /***********************************************************************/
316
317