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