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