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