]> git.donarmstrong.com Git - mothur.git/blob - sharedordervector.cpp
fixed some bugs
[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 #include "sharedordervector.h"
11 #include "sharedutilities.h"
12
13 /***********************************************************************/
14
15 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0)  {}
16
17 /***********************************************************************/
18
19 SharedOrderVector::SharedOrderVector(string id, vector<individual>  ov) : 
20                                                                                         DataVector(id), data(ov)
21 {
22                 updateStats();
23 }
24
25 /***********************************************************************/
26 //This function is used to read a .shared file for the collect.shared, rarefaction.shared and summary.shared commands
27 //if you don't use a list and groupfile.  
28
29 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() {  //reads in a shared file
30         try {
31                 globaldata = GlobalData::getInstance();
32                 maxRank = 0; numBins = 0; numSeqs = 0;
33                 
34                 if (globaldata->gGroupmap == NULL) {  groupmap = new GroupMap(); }
35                 
36                 int num, inputData, pos, count;
37                 count = 0;  numSeqs = 0;
38                 string holdLabel, nextLabel, groupN;
39                 individual newguy;
40                 
41                 //read in first row since you know there is at least 1 group.
42                 f >> label >> groupN >> num;
43                 holdLabel = label;
44                 
45                 if (globaldata->gGroupmap == NULL) { 
46                         //save group in groupmap
47                         groupmap->namesOfGroups.push_back(groupN);
48                         groupmap->groupIndex[groupN] = 0;
49                 }
50                 
51                 for(int i=0;i<num;i++){
52                         f >> inputData;
53                         
54                         for (int j = 0; j < inputData; j++) {
55                                 push_back(i, i, groupN);
56                                 numSeqs++;
57                         }
58                 }
59                 
60                 //save position in file in case next line is a new label.
61                 pos = f.tellg();
62                 
63                 if (f.eof() != true) { f >> nextLabel; }
64                 
65                 //read the rest of the groups info in
66                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
67                         f >> groupN >> num;
68                         count++;
69                         
70                         if (globaldata->gGroupmap == NULL) { 
71                                 //save group in groupmap
72                                 groupmap->namesOfGroups.push_back(groupN);
73                                 groupmap->groupIndex[groupN] = count;
74                         }
75                         
76                         for(int i=0;i<num;i++){
77                                 f >> inputData;
78                                 
79                                 for (int j = 0; j < inputData; j++) {
80                                         push_back(i, i, groupN);
81                                         numSeqs++;
82                                 }
83                         }
84                         
85                         //save position in file in case next line is a new label.
86                         pos = f.tellg();
87         
88                         if (f.eof() != true) { f >> nextLabel; }
89
90                 }
91                 
92                 //put file pointer back since you are now at a new distance label
93                 f.seekg(pos, ios::beg);
94         
95                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
96                 
97                 updateStats();
98                 
99         }
100         catch(exception& e) {
101                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102                 exit(1);
103         }
104         catch(...) {
105                 cout << "An unknown error has occurred in the SharedOrderVector class function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
106                 exit(1);
107         }
108 }
109 /***********************************************************************/
110
111 int SharedOrderVector::getNumBins(){
112         return numBins;
113 }
114
115 /***********************************************************************/
116
117 int SharedOrderVector::getNumSeqs(){
118         return numSeqs;
119 }
120
121 /***********************************************************************/
122
123 int SharedOrderVector::getMaxRank(){
124         return maxRank;
125 }
126
127
128 /***********************************************************************/
129
130
131
132 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
133         
134         data[index].group = groupName;
135         data[index].bin = binNumber;
136         data[index].abundance = abund;
137         //if (abund > maxRank) { maxRank = abund; }
138         updateStats();
139 }
140
141 /***********************************************************************/
142
143 individual SharedOrderVector::get(int index){
144         return data[index];                     
145 }
146
147
148 /***********************************************************************/
149 //commented updateStats out to improve speed, but whoever calls this must remember to update when they are done with all the pushbacks they are doing 
150 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
151         individual newGuy;
152         newGuy.group = groupName;
153         newGuy.abundance = abund;
154         newGuy.bin = binNumber;
155         data.push_back(newGuy);
156         //numSeqs++;
157         //numBins++;
158         //if (abund > maxRank) { maxRank = abund; }
159         
160         //updateStats();
161 }
162
163 /***********************************************************************/
164
165 void SharedOrderVector::print(ostream& output){
166         try {
167                 output << label << '\t' << numSeqs << '\t';
168         
169                 for(int i=0;i<data.size();i++){
170                         output << data[i].bin << '\t';
171                 }
172                 output << endl;
173         }
174         catch(exception& e) {
175                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176                 exit(1);
177         }
178         catch(...) {
179                 cout << "An unknown error has occurred in the SharedOrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180                 exit(1);
181         }
182 }
183
184
185 /***********************************************************************/
186
187 void SharedOrderVector::resize(int){
188         cout << "resize() did nothing in class SharedOrderVector";
189 }
190
191 /***********************************************************************/
192
193
194 vector<individual>::iterator SharedOrderVector::begin(){
195         return data.begin();    
196 }
197
198 /***********************************************************************/
199
200 vector<individual>::iterator SharedOrderVector::end(){
201         return data.end();              
202 }
203
204 /***********************************************************************/
205
206 int SharedOrderVector::size(){
207         return data.size();                                     
208 }
209
210 /***********************************************************************/
211
212 RAbundVector SharedOrderVector::getRAbundVector(){
213         try {
214                 RAbundVector rav(data.size());
215         
216                 for(int i=0;i<numSeqs;i++){
217                         rav.set(data[i].bin, rav.get(data[i].bin) + 1);
218                 }       
219                 sort(rav.rbegin(), rav.rend());
220                 for(int i=numSeqs-1;i>=0;i--){
221                         if(rav.get(i) == 0){    rav.pop_back(); }
222                         else{
223                                 break;
224                         }
225                 }
226                 rav.setLabel(label);
227
228                 return rav;
229         }
230         catch(exception& e) {
231                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232                 exit(1);
233         }
234         catch(...) {
235                 cout << "An unknown error has occurred in the SharedOrderVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
236                 exit(1);
237         }       
238 }
239 /***********************************************************************/
240
241 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
242         try {
243                 OrderVector ov;
244         
245                 for (int i = 0; i < data.size(); i++) {
246                         ov.push_back(data[i].bin);
247                 }
248                 
249                 random_shuffle(ov.begin(), ov.end());
250
251                 ov.setLabel(label);     
252                 return ov;
253         }
254         catch(exception& e) {
255                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
256                 exit(1);
257         }
258         catch(...) {
259                 cout << "An unknown error has occurred in the SharedOrderVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
260                 exit(1);
261         }
262 }
263
264
265 /***********************************************************************/
266
267 SAbundVector SharedOrderVector::getSAbundVector(){
268         
269         RAbundVector rav(this->getRAbundVector());
270         return rav.getSAbundVector();
271
272 }
273 /***********************************************************************/
274 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
275         try {
276                 SharedRAbundVector sharedRav(data.size());
277                 
278                 sharedRav.setLabel(label);
279                 sharedRav.setGroup(group);
280                 
281                 for (int i = 0; i < data.size(); i++) {
282                         if (data[i].group == group) {
283                                 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
284                         }
285                 }
286                 return sharedRav;
287         }
288         catch(exception& e) {
289                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290                 exit(1);
291         }
292         catch(...) {
293                 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294                 exit(1);
295         }
296         
297 }
298 /***********************************************************************/
299 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
300         try {
301                 SharedUtil* util;
302                 util = new SharedUtil();
303                 vector<SharedRAbundVector*> lookup;
304                 
305                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
306                 util->getSharedVectors(globaldata->Groups, lookup, this);
307                 
308                 return lookup;
309         }
310         catch(exception& e) {
311                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
312                 exit(1);
313         }
314         catch(...) {
315                 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
316                 exit(1);
317         }
318         
319 }
320 /***********************************************************************/
321 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
322         try {
323                 
324                 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
325                 return sharedRav.getSharedSAbundVector();
326                                 
327         }
328         catch(exception& e) {
329                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
330                 exit(1);
331         }
332         catch(...) {
333                 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
334                 exit(1);
335         }
336         
337 }
338
339 /***********************************************************************/
340
341 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
342         random_shuffle(data.begin(), data.end());
343         return *this;                   
344 }
345
346 /***********************************************************************/
347
348 void SharedOrderVector::updateStats(){
349         try {
350                 needToUpdate = 0;
351                 numSeqs = 0;
352                 numBins = 0;
353                 maxRank = 0;
354         
355                 numSeqs = data.size();
356                                 
357                 vector<int> hold(numSeqs, 0);
358                 for(int i=0;i<numSeqs;i++){
359                         hold[data[i].bin] = hold[data[i].bin]+1;
360                 }       
361                 
362                 for(int i=0;i<numSeqs;i++){
363                         if(hold[i] > 0)                         {       numBins++;                              }
364                         if(hold[i] > maxRank)           {       maxRank = hold[i];              }
365                 }
366                 
367         }
368         catch(exception& e) {
369                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
370                 exit(1);
371         }
372         catch(...) {
373                 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374                 exit(1);
375         }       
376 }
377
378 /***********************************************************************/
379
380