]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
fixed some bugs
[mothur.git] / sharedrabundvector.cpp
1 /*
2  *  sharedvector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/5/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedrabundvector.h" 
11 #include "sabundvector.hpp"
12 #include "ordervector.hpp"
13 #include "sharedutilities.h"
14
15
16 /***********************************************************************/
17
18 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {}
19
20 /***********************************************************************/
21
22 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
23                 individual newGuy;
24                 //initialize data
25                 for (int i=0; i< n; i++) {
26                         newGuy.bin = i;
27                         newGuy.abundance = 0;
28                         data.push_back(newGuy);
29                 }
30 }
31
32 /***********************************************************************
33
34 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
35         try {
36                 numBins = 0;
37                 maxRank = 0;
38                 numSeqs = 0;
39                 
40                 for(int i=0;i<data.size();i++){
41                         if(data[i].abundance != 0)              {       numBins = i+1;          }
42                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
43                         numSeqs += data[i].abundance;
44                 }
45         }
46         catch(exception& e) {
47                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
48                 exit(1);
49         }
50         catch(...) {
51                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52                 exit(1);
53         }
54 }
55
56
57 /***********************************************************************/
58 //reads a shared file
59 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
60         try {
61                 globaldata = GlobalData::getInstance();
62                 
63                 if (globaldata->gGroupmap == NULL) {  groupmap = new GroupMap(); }
64                 
65                 int num, inputData, pos, count;
66                 count = 0;  
67                 string holdLabel, nextLabel, groupN;
68                 individual newguy;
69                 
70                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
71                 lookup.clear();
72                 
73                 //read in first row since you know there is at least 1 group.
74                 f >> label >> groupN >> num;
75                 holdLabel = label;
76                 
77                 //add new vector to lookup
78                 SharedRAbundVector* temp = new SharedRAbundVector();
79                 lookup.push_back(temp);
80                 lookup[0]->setLabel(label);
81                 lookup[0]->setGroup(groupN);
82                 
83                 if (globaldata->gGroupmap == NULL) { 
84                         //save group in groupmap
85                         groupmap->namesOfGroups.push_back(groupN);
86                         groupmap->groupIndex[groupN] = 0;
87                 }
88                 
89                 //fill vector.  data = first sharedrabund in file
90                 for(int i=0;i<num;i++){
91                         f >> inputData;
92                         
93                         lookup[0]->push_back(inputData, i, groupN); //abundance, bin, group
94                         push_back(inputData, i, groupN);
95                         numSeqs += inputData;
96                         numBins++;
97                         if (inputData > maxRank) { maxRank = inputData; }
98                         
99                 }
100                 
101                 //save position in file in case next line is a new label.
102                 pos = f.tellg();
103                 
104                 if (f.eof() != true) { f >> nextLabel; }
105                 
106                 //read the rest of the groups info in
107                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
108                         f >> groupN >> num;
109                         count++;
110                         
111                         if (globaldata->gGroupmap == NULL) { 
112                                 //save group in groupmap
113                                 groupmap->namesOfGroups.push_back(groupN);
114                                 groupmap->groupIndex[groupN] = count;
115                         }
116                         
117                         //add new vector to lookup
118                         temp = new SharedRAbundVector();
119                         lookup.push_back(temp);
120                         lookup[count]->setLabel(label);
121                         lookup[count]->setGroup(groupN);
122
123                         //fill vector.  
124                         for(int i=0;i<num;i++){
125                                 f >> inputData;
126                                 lookup[count]->push_back(inputData, i, groupN); //abundance, bin, group
127                         }
128                         
129                         //save position in file in case next line is a new label.
130                         pos = f.tellg();
131         
132                         if (f.eof() != true) { f >> nextLabel; }
133                 }
134                 
135                 //put file pointer back since you are now at a new distance label
136                 f.seekg(pos, ios::beg);
137         
138                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
139                 
140         }
141         catch(exception& e) {
142                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
143                 exit(1);
144         }
145         catch(...) {
146                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }
149 }
150
151 /***********************************************************************/
152
153 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
154         try {
155                 int oldBinSize = data[binNumber].abundance;
156                 data[binNumber].abundance = newBinSize;
157                 data[binNumber].group = groupname;
158         
159                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
160         
161                 numSeqs += (newBinSize - oldBinSize);
162         }
163         catch(exception& e) {
164                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165                 exit(1);
166         }
167         catch(...) {
168                 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169                 exit(1);
170         }
171 }
172 /***********************************************************************/
173
174 void SharedRAbundVector::setData(vector <individual> newData){
175         data = newData;
176 }
177
178 /***********************************************************************/
179
180 int SharedRAbundVector::getAbundance(int index){
181         return data[index].abundance;
182         
183 }
184 /***********************************************************************/
185
186 int SharedRAbundVector::numNZ(){
187         int sum = 0;
188         for(int i = 1; i < numBins; i++)
189                 if(data[i].abundance > 0)
190                         sum++;
191         return sum;
192 }
193 /***********************************************************************/
194
195 void SharedRAbundVector::sortD(){
196         struct individual indObj;
197         sort(data.begin()+1, data.end(), indObj);
198 }
199 /***********************************************************************/
200
201 individual SharedRAbundVector::get(int index){
202         return data[index];
203         
204 }
205 /***********************************************************************/
206
207 vector <individual> SharedRAbundVector::getData(){
208         return data;
209 }
210 /***********************************************************************/
211
212 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
213         try {
214                 individual newGuy;
215                 newGuy.abundance = binSize;
216                 newGuy.group = groupName;
217                 newGuy.bin = otu;
218                 
219                 data.push_back(newGuy);
220                 numBins++;
221         
222                 if(binSize > maxRank){
223                         maxRank = binSize;
224                 }
225         
226                 numSeqs += binSize;
227         }
228         catch(exception& e) {
229                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
230                 exit(1);
231         }
232         catch(...) {
233                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
234                 exit(1);
235         }
236 }
237
238 /***********************************************************************/
239
240 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
241         try {
242                 individual newGuy;
243                 newGuy.abundance = binSize;
244                 newGuy.group = groupName;
245                 newGuy.bin = otu;
246                 
247                 data.insert(data.begin()+otu, newGuy);
248                 numBins++;
249         
250                 if(binSize > maxRank){
251                         maxRank = binSize;
252                 }
253         
254                 numSeqs += binSize;
255         }
256         catch(exception& e) {
257                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
258                 exit(1);
259         }
260         catch(...) {
261                 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
262                 exit(1);
263         }
264 }
265
266 /***********************************************************************/
267
268 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
269         try {
270                 individual newGuy;
271                 newGuy.abundance = binSize;
272                 newGuy.group = groupName;
273                 newGuy.bin = otu;
274                 
275                 data.insert(data.begin(), newGuy);
276                 numBins++;
277         
278                 if(binSize > maxRank){
279                         maxRank = binSize;
280                 }
281         
282                 numSeqs += binSize;
283         }
284         catch(exception& e) {
285                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286                 exit(1);
287         }
288         catch(...) {
289                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290                 exit(1);
291         }
292 }
293
294 /***********************************************************************/
295 void SharedRAbundVector::pop_back(){
296         numSeqs -= data[data.size()-1].abundance;
297         numBins--;
298         return data.pop_back();
299 }
300
301 /***********************************************************************/
302
303
304 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
305         return data.rbegin();                           
306 }
307
308 /***********************************************************************/
309
310 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
311         return data.rend();                                     
312 }
313
314 /***********************************************************************/
315 void SharedRAbundVector::resize(int size){
316         
317         data.resize(size);
318 }
319
320 /***********************************************************************/
321
322 int SharedRAbundVector::size(){
323         return data.size();
324 }
325
326 /***********************************************************************/
327 void SharedRAbundVector::print(ostream& output){
328         try {
329                 output << numBins << '\t';
330         
331                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
332                 output << endl;
333         }
334         catch(exception& e) {
335                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
336                 exit(1);
337         }
338         catch(...) {
339                 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
340                 exit(1);
341         }
342 }
343 /***********************************************************************/
344 string SharedRAbundVector::getGroup(){
345         return group;
346 }
347
348 /***********************************************************************/
349
350 void SharedRAbundVector::setGroup(string groupName){
351         group = groupName;
352 }
353 /***********************************************************************/
354 int SharedRAbundVector::getGroupIndex()  { return index; }
355 /***********************************************************************/
356 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
357 /***********************************************************************/
358 int SharedRAbundVector::getNumBins(){
359         return numBins;
360 }
361
362 /***********************************************************************/
363
364 int SharedRAbundVector::getNumSeqs(){
365         return numSeqs;
366 }
367
368 /***********************************************************************/
369
370 int SharedRAbundVector::getMaxRank(){
371         return maxRank;
372 }
373 /***********************************************************************/
374
375 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
376         return *this;                   
377 }
378 /***********************************************************************/
379 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
380         try {
381                 SharedUtil* util;
382                 util = new SharedUtil();
383                 
384                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
385
386                 for (int i = 0; i < lookup.size(); i++) {
387                         //if this sharedrabund is not from a group the user wants then delete it.
388                         if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) { 
389                                 delete lookup[i]; 
390                                 lookup.erase(lookup.begin()+i); 
391                                 i--; 
392                         }
393                 }
394
395                 return lookup;
396         }
397         catch(exception& e) {
398                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
399                 exit(1);
400         }
401         catch(...) {
402                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
403                 exit(1);
404         }
405 }
406 /***********************************************************************/
407
408 RAbundVector SharedRAbundVector::getRAbundVector() {
409         try {
410                 RAbundVector rav(data.size());
411                 
412                 for (int i = 0; i < data.size(); i++) {
413                         rav.set(i, data[i].abundance);
414                 }
415         
416                 return rav;
417         }
418         catch(exception& e) {
419                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
420                 exit(1);
421         }
422         catch(...) {
423                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
424                 exit(1);
425         }
426 }
427 /***********************************************************************/
428
429 RAbundVector SharedRAbundVector::getRAbundVector2() {
430         try {
431                 RAbundVector rav;
432                 for(int i = 0; i < numBins; i++)
433                         if(data[i].abundance != 0)
434                                 rav.push_back(data[i].abundance-1);
435                 return rav;
436         }
437         catch(exception& e) {
438                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
439                 exit(1);
440         }
441         catch(...) {
442                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
443                 exit(1);
444         }
445 }
446 /***********************************************************************/
447
448 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
449         try {
450                 SharedSAbundVector sav(maxRank+1);
451                 
452                 for(int i=0;i<data.size();i++){
453                         int abund = data[i].abundance;
454                         sav.set(abund, sav.getAbundance(abund) + 1, group);
455                 }
456                 
457                 sav.set(0, 0, group);
458                 sav.setLabel(label);
459                 sav.setGroup(group);
460                 
461                 return sav;
462         }
463         catch(exception& e) {
464                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
465                 exit(1);
466         }
467         catch(...) {
468                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
469                 exit(1);
470         }
471 }
472 /***********************************************************************/
473
474 SAbundVector SharedRAbundVector::getSAbundVector() {
475         try {
476                 SAbundVector sav(maxRank+1);
477                 
478                 for(int i=0;i<data.size();i++){
479                         int abund = data[i].abundance;
480                         sav.set(abund, sav.get(abund) + 1);
481                 }
482                 sav.set(0, 0);
483                 sav.setLabel(label);
484                 return sav;
485         }
486         catch(exception& e) {
487                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
488                 exit(1);
489         }
490         catch(...) {
491                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
492                 exit(1);
493         }
494 }
495
496 /***********************************************************************/
497
498 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
499         try {
500                 SharedOrderVector ov;
501         
502                 for(int i=0;i<data.size();i++){
503                         for(int j=0;j<data[i].abundance;j++){
504                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
505                         }
506                 }
507                 random_shuffle(ov.begin(), ov.end());
508
509                 ov.setLabel(label);     
510                 ov.updateStats();
511                 
512                 return ov;
513         }
514         catch(exception& e) {
515                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
516                 exit(1);
517         }
518         catch(...) {
519                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
520                 exit(1);
521         }
522 }
523 /***********************************************************************/
524
525 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
526         try {
527                 OrderVector ov;
528         
529                 for(int i=0;i<data.size();i++){
530                         for(int j=0;j<data[i].abundance;j++){
531                                 ov.push_back(i);
532                         }
533                 }
534                 random_shuffle(ov.begin(), ov.end());
535
536                 ov.setLabel(label);     
537                 return ov;
538         }
539         catch(exception& e) {
540                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
541                 exit(1);
542         }
543         catch(...) {
544                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
545                 exit(1);
546         }
547 }
548
549 /***********************************************************************/
550