]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
working on windows paralellization, added trimOligos class to be used by trim.flows...
[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 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {} 
18 /***********************************************************************/
19
20 SharedRAbundVector::~SharedRAbundVector() {
21         //for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
22
23 }
24
25 /***********************************************************************/
26
27 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
28                 individual newGuy;
29                 //initialize data
30                 for (int i=0; i< n; i++) {
31                         newGuy.bin = i;
32                         newGuy.abundance = 0;
33                         data.push_back(newGuy);
34                 }
35 }
36
37 /***********************************************************************
38
39 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
40         try {
41                 numBins = 0;
42                 maxRank = 0;
43                 numSeqs = 0;
44                 
45                 for(int i=0;i<data.size();i++){
46                         if(data[i].abundance != 0)              {       numBins = i+1;          }
47                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
48                         numSeqs += data[i].abundance;
49                 }
50         }
51         catch(exception& e) {
52                 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
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                 m->clearAllGroups();
63                 vector<string> allGroups;
64                 
65                 int num, inputData, 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]; lookup[i] = NULL; }  lookup.clear();
71                 
72                 //are we at the beginning of the file??
73                 if (m->saveNextLabel == "") {  
74                         f >> label; 
75                         
76                         //is this a shared file that has headers
77                         if (label == "label") { 
78                                 //gets "group"
79                                 f >> label; m->gobble(f);
80                                 
81                                 //gets "numOtus"
82                                 f >> label; m->gobble(f);
83                                 
84                                 //eat rest of line
85                                 label = m->getline(f); m->gobble(f);
86                                 
87                                 //parse labels to save
88                                 istringstream iStringStream(label);
89                                 m->binLabelsInFile.clear();
90                                 while(!iStringStream.eof()){
91                                         if (m->control_pressed) { break; }
92                                         string temp;
93                                         iStringStream >> temp;  m->gobble(iStringStream);
94                 
95                                         m->binLabelsInFile.push_back(temp);
96                                 }
97                                 
98                                 f >> label;
99                         }
100                 }else { label = m->saveNextLabel; }
101                 
102                 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
103                 m->currentBinLabels = m->binLabelsInFile;
104                 
105                 //read in first row since you know there is at least 1 group.
106                 f >> groupN >> num;
107
108                 holdLabel = label;
109                 
110                 //add new vector to lookup
111                 SharedRAbundVector* temp = new SharedRAbundVector();
112                 lookup.push_back(temp);
113                 lookup[0]->setLabel(label);
114                 lookup[0]->setGroup(groupN);
115                 
116                 allGroups.push_back(groupN);
117                 
118                 //fill vector.  data = first sharedrabund in file
119                 for(int i=0;i<num;i++){
120                         f >> inputData;
121                         
122                         lookup[0]->push_back(inputData, groupN); //abundance, bin, group
123                         push_back(inputData, groupN);
124                         
125                         if (inputData > maxRank) { maxRank = inputData; }
126                 }
127                 
128                 m->gobble(f);
129                 
130                 if (!(f.eof())) { f >> nextLabel; }
131         
132                 //read the rest of the groups info in
133                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
134                         f >> groupN >> num;
135                         count++;
136                         
137                         allGroups.push_back(groupN);
138                         
139                         //add new vector to lookup
140                         temp = new SharedRAbundVector();
141                         lookup.push_back(temp);
142                         lookup[count]->setLabel(label);
143                         lookup[count]->setGroup(groupN);
144
145                         //fill vector.  
146                         for(int i=0;i<num;i++){
147                                 f >> inputData;
148                                 
149                                 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
150                         }
151                         
152                         m->gobble(f);
153                                 
154                         if (f.eof() != true) { f >> nextLabel; }
155                 }
156                 m->saveNextLabel = nextLabel;
157                 m->setAllGroups(allGroups);
158                 
159         }
160         catch(exception& e) {
161                 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
162                 exit(1);
163         }
164 }
165
166 /***********************************************************************/
167
168 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
169         try {
170                 int oldBinSize = data[binNumber].abundance;
171                 data[binNumber].abundance = newBinSize;
172                 data[binNumber].group = groupname;
173         
174                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
175         
176                 numSeqs += (newBinSize - oldBinSize);
177         }
178         catch(exception& e) {
179                 m->errorOut(e, "SharedRAbundVector", "set");
180                 exit(1);
181         }
182 }
183 /***********************************************************************/
184
185 void SharedRAbundVector::setData(vector <individual> newData){
186         data = newData;
187 }
188
189 /***********************************************************************/
190
191 int SharedRAbundVector::getAbundance(int index){
192         return data[index].abundance;
193         
194 }
195 /***********************************************************************/
196
197 int SharedRAbundVector::numNZ(){
198         int sum = 0;
199         for(int i = 1; i < numBins; i++)
200                 if(data[i].abundance > 0)
201                         sum++;
202         return sum;
203 }
204 /***********************************************************************/
205
206 void SharedRAbundVector::sortD(){
207         struct individual indObj;
208         sort(data.begin()+1, data.end(), indObj);
209 }
210 /***********************************************************************/
211
212 individual SharedRAbundVector::get(int index){
213         return data[index];
214         
215 }
216 /***********************************************************************/
217
218 vector <individual> SharedRAbundVector::getData(){
219         return data;
220 }
221 /***********************************************************************/
222
223 void SharedRAbundVector::clear(){
224         numBins = 0;
225         maxRank = 0;
226         numSeqs = 0;
227         data.clear();
228         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
229         lookup.clear();
230 }
231 /***********************************************************************/
232
233 void SharedRAbundVector::push_back(int binSize, string groupName){
234         try {
235                 individual newGuy;
236                 newGuy.abundance = binSize;
237                 newGuy.group = groupName;
238                 newGuy.bin = data.size();
239                 
240                 data.push_back(newGuy);
241                 numBins++;
242         
243                 if(binSize > maxRank){
244                         maxRank = binSize;
245                 }
246         
247                 numSeqs += binSize;
248         }
249         catch(exception& e) {
250                 m->errorOut(e, "SharedRAbundVector", "push_back");
251                 exit(1);
252         }
253 }
254
255 /***********************************************************************/
256
257 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
258         try {
259                 individual newGuy;
260                 newGuy.abundance = binSize;
261                 newGuy.group = groupName;
262                 newGuy.bin = otu;
263                 
264                 data.insert(data.begin()+otu, newGuy);
265                 numBins++;
266         
267                 if(binSize > maxRank){
268                         maxRank = binSize;
269                 }
270         
271                 numSeqs += binSize;
272         }
273         catch(exception& e) {
274                 m->errorOut(e, "SharedRAbundVector", "insert");
275                 exit(1);
276         }
277 }
278
279 /***********************************************************************/
280
281 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
282         try {
283                 individual newGuy;
284                 newGuy.abundance = binSize;
285                 newGuy.group = groupName;
286                 newGuy.bin = otu;
287                 
288                 data.insert(data.begin(), newGuy);
289                 numBins++;
290         
291                 if(binSize > maxRank){
292                         maxRank = binSize;
293                 }
294         
295                 numSeqs += binSize;
296         }
297         catch(exception& e) {
298                 m->errorOut(e, "SharedRAbundVector", "push_front");
299                 exit(1);
300         }
301 }
302
303 /***********************************************************************/
304 void SharedRAbundVector::pop_back(){
305         numSeqs -= data[data.size()-1].abundance;
306         numBins--;
307         return data.pop_back();
308 }
309
310 /***********************************************************************/
311
312
313 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
314         return data.rbegin();                           
315 }
316
317 /***********************************************************************/
318
319 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
320         return data.rend();                                     
321 }
322
323 /***********************************************************************/
324 void SharedRAbundVector::resize(int size){
325         
326         data.resize(size);
327 }
328
329 /***********************************************************************/
330
331 int SharedRAbundVector::size(){
332         return data.size();
333 }
334
335
336 /***********************************************************************/
337 void SharedRAbundVector::printHeaders(ostream& output){
338         try {
339                 
340                 output << "label\tGroup\tnumOtus\t";
341                 if (m->sharedHeaderMode == "tax") {
342                         for (int i = 0; i < numBins; i++) {  
343                                 
344                                 //if there is a bin label use it otherwise make one
345                                 string binLabel = "PhyloType" + toString(i+1);
346                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
347                                 
348                                 output << binLabel << '\t'; 
349                         }
350                         output << endl;
351                 }else {
352                         for (int i = 0; i < numBins; i++) {  
353                                 //if there is a bin label use it otherwise make one
354                                 string mybinLabel = "Otu" + toString(i+1);
355                                 if (i < m->currentBinLabels.size()) {  mybinLabel = m->currentBinLabels[i]; }
356                                 
357                                 output << mybinLabel << '\t'; 
358                         }
359                         
360                         output << endl;
361                 }
362                 m->printedHeaders = true;
363         }
364         catch(exception& e) {
365                 m->errorOut(e, "SharedRAbundVector", "printHeaders");
366                 exit(1);
367         }
368 }
369 /***********************************************************************/
370 void SharedRAbundVector::print(ostream& output) {
371         try {
372                 output << numBins << '\t';
373         
374                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
375                 output << endl;
376         }
377         catch(exception& e) {
378                 m->errorOut(e, "SharedRAbundVector", "print");
379                 exit(1);
380         }
381 }
382 /***********************************************************************/
383 string SharedRAbundVector::getGroup(){
384         return group;
385 }
386
387 /***********************************************************************/
388
389 void SharedRAbundVector::setGroup(string groupName){
390         group = groupName;
391 }
392 /***********************************************************************/
393 int SharedRAbundVector::getGroupIndex()  { return index; }
394 /***********************************************************************/
395 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
396 /***********************************************************************/
397 int SharedRAbundVector::getNumBins(){
398                 return numBins;
399 }
400
401 /***********************************************************************/
402
403 int SharedRAbundVector::getNumSeqs(){
404         return numSeqs;
405 }
406
407 /***********************************************************************/
408
409 int SharedRAbundVector::getMaxRank(){
410         return maxRank;
411 }
412 /***********************************************************************/
413
414 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
415         return *this;                   
416 }
417 /***********************************************************************/
418 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
419         try {
420                 SharedUtil* util;
421                 util = new SharedUtil();
422                 
423                 vector<string> Groups = m->getGroups();
424                 vector<string> allGroups = m->getAllGroups();
425                 util->setGroups(Groups, allGroups);
426                 m->setGroups(Groups);
427                 
428                 bool remove = false;
429                 for (int i = 0; i < lookup.size(); i++) {
430                         //if this sharedrabund is not from a group the user wants then delete it.
431                         if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) { 
432                                 remove = true;
433                                 delete lookup[i]; lookup[i] = NULL;
434                                 lookup.erase(lookup.begin()+i); 
435                                 i--; 
436                         }
437                 }
438                 
439                 delete util;
440                 
441                 if (remove) { eliminateZeroOTUS(lookup); }
442         
443                 return lookup;
444         }
445         catch(exception& e) {
446                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
447                 exit(1);
448         }
449 }
450 //**********************************************************************************************************************
451 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
452                 try {
453                         
454                         vector<SharedRAbundVector*> newLookup;
455                         for (int i = 0; i < thislookup.size(); i++) {
456                                 SharedRAbundVector* temp = new SharedRAbundVector();
457                                 temp->setLabel(thislookup[i]->getLabel());
458                                 temp->setGroup(thislookup[i]->getGroup());
459                                 newLookup.push_back(temp);
460                         }
461                         
462                         //for each bin
463                         vector<string> newBinLabels;
464                         for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
465                                 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
466                                 
467                                 //look at each sharedRabund and make sure they are not all zero
468                                 bool allZero = true;
469                                 for (int j = 0; j < thislookup.size(); j++) {
470                                         if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
471                                 }
472                                 
473                                 //if they are not all zero add this bin
474                                 if (!allZero) {
475                                         for (int j = 0; j < thislookup.size(); j++) {
476                                                 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
477                                         }
478                                         
479                                         //if there is a bin label use it otherwise make one
480                                         string binLabel = "Otu" + toString(i+1);
481                                         if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
482                                         
483                                         newBinLabels.push_back(binLabel);
484                                 }
485                         }
486                         
487                         for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
488                         
489                         thislookup = newLookup;
490                         m->currentBinLabels = newBinLabels;
491                         
492                         return 0;
493                         
494                 }
495                 catch(exception& e) {
496                         m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
497                         exit(1);
498                 }
499         }
500         
501 /***********************************************************************/
502 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
503         try {
504                 vector<SharedRAbundFloatVector*> newLookupFloat;        
505                 for (int i = 0; i < lookup.size(); i++) {
506                         SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
507                         temp->setLabel(thislookup[i]->getLabel());
508                         temp->setGroup(thislookup[i]->getGroup());
509                         newLookupFloat.push_back(temp);
510                 }
511                 
512                 for (int i = 0; i < thislookup.size(); i++) {
513                         
514                         for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
515                                 
516                                 if (m->control_pressed) { return newLookupFloat; }
517                                 
518                                 int abund = thislookup[i]->getAbundance(j);
519                                 
520                                 float relabund = abund / (float) thislookup[i]->getNumSeqs();
521                                 
522                                 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
523                         }
524                 }
525                 
526                 return newLookupFloat;
527         }
528         catch(exception& e) {
529                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
530                 exit(1);
531         }
532 }
533 /***********************************************************************/
534
535 RAbundVector SharedRAbundVector::getRAbundVector() {
536         try {
537                 RAbundVector rav;
538                 
539                 for (int i = 0; i < data.size(); i++) {
540                         if(data[i].abundance != 0) {
541                                 rav.push_back(data[i].abundance);
542                         }
543                 }
544                 
545                 rav.setLabel(label);
546                 return rav;
547         }
548         catch(exception& e) {
549                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
550                 exit(1);
551         }
552 }
553 /***********************************************************************/
554
555 RAbundVector SharedRAbundVector::getRAbundVector2() {
556         try {
557                 RAbundVector rav;
558                 for(int i = 0; i < numBins; i++)
559                         if(data[i].abundance != 0)
560                                 rav.push_back(data[i].abundance-1);
561                 return rav;
562         }
563         catch(exception& e) {
564                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
565                 exit(1);
566         }
567 }
568 /***********************************************************************/
569
570 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
571         try {
572                 SharedSAbundVector sav(maxRank+1);
573                 
574                 for(int i=0;i<data.size();i++){
575                         int abund = data[i].abundance;
576                         sav.set(abund, sav.getAbundance(abund) + 1, group);
577                 }
578                 
579                 sav.set(0, 0, group);
580                 sav.setLabel(label);
581                 sav.setGroup(group);
582                 
583                 return sav;
584         }
585         catch(exception& e) {
586                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
587                 exit(1);
588         }
589 }
590 /***********************************************************************/
591
592 SAbundVector SharedRAbundVector::getSAbundVector() {
593         try {
594                 SAbundVector sav(maxRank+1);
595                 
596                 for(int i=0;i<data.size();i++){
597                         int abund = data[i].abundance;
598                         sav.set(abund, sav.get(abund) + 1);
599                 }
600                 sav.set(0, 0);
601                 sav.setLabel(label);
602                 return sav;
603         }
604         catch(exception& e) {
605                 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");                
606                 exit(1);
607         }
608 }
609
610 /***********************************************************************/
611
612 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
613         try {
614                 SharedOrderVector ov;
615         
616                 for(int i=0;i<data.size();i++){
617                         for(int j=0;j<data[i].abundance;j++){
618                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
619                         }
620                 }
621                 random_shuffle(ov.begin(), ov.end());
622
623                 ov.setLabel(label);     
624                 ov.updateStats();
625                 
626                 return ov;
627         }
628         catch(exception& e) {
629                 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
630                 exit(1);
631         }
632 }
633 /***********************************************************************/
634
635 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
636         try {
637                 OrderVector ov;
638                 for(int i=0;i<numBins;i++){
639                         for(int j=0;j<data[i].abundance;j++){
640                                 ov.push_back(i);
641                         }
642                 }
643                 random_shuffle(ov.begin(), ov.end());
644                 
645                 ov.setLabel(label);     
646
647                 return ov;
648         }
649         catch(exception& e) {
650                 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
651                 exit(1);
652         }
653 }
654
655 /***********************************************************************/
656