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