]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
added multiple processors option for Windows users to align.seqs, dist.seqs, summary...
[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                 
427                 bool remove = false;
428                 for (int i = 0; i < lookup.size(); i++) {
429                         //if this sharedrabund is not from a group the user wants then delete it.
430                         if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) { 
431                                 remove = true;
432                                 delete lookup[i]; lookup[i] = NULL;
433                                 lookup.erase(lookup.begin()+i); 
434                                 i--; 
435                         }
436                 }
437                 
438                 delete util;
439                 
440                 if (remove) { eliminateZeroOTUS(lookup); }
441         
442                 return lookup;
443         }
444         catch(exception& e) {
445                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
446                 exit(1);
447         }
448 }
449 //**********************************************************************************************************************
450 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
451                 try {
452                         
453                         vector<SharedRAbundVector*> newLookup;
454                         for (int i = 0; i < thislookup.size(); i++) {
455                                 SharedRAbundVector* temp = new SharedRAbundVector();
456                                 temp->setLabel(thislookup[i]->getLabel());
457                                 temp->setGroup(thislookup[i]->getGroup());
458                                 newLookup.push_back(temp);
459                         }
460                         
461                         //for each bin
462                         vector<string> newBinLabels;
463                         for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
464                                 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
465                                 
466                                 //look at each sharedRabund and make sure they are not all zero
467                                 bool allZero = true;
468                                 for (int j = 0; j < thislookup.size(); j++) {
469                                         if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
470                                 }
471                                 
472                                 //if they are not all zero add this bin
473                                 if (!allZero) {
474                                         for (int j = 0; j < thislookup.size(); j++) {
475                                                 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
476                                         }
477                                         
478                                         //if there is a bin label use it otherwise make one
479                                         string binLabel = "Otu" + toString(i+1);
480                                         if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
481                                         
482                                         newBinLabels.push_back(binLabel);
483                                 }
484                         }
485                         
486                         for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
487                         
488                         thislookup = newLookup;
489                         m->currentBinLabels = newBinLabels;
490                         
491                         return 0;
492                         
493                 }
494                 catch(exception& e) {
495                         m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
496                         exit(1);
497                 }
498         }
499         
500 /***********************************************************************/
501 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
502         try {
503                 vector<SharedRAbundFloatVector*> newLookupFloat;        
504                 for (int i = 0; i < lookup.size(); i++) {
505                         SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
506                         temp->setLabel(thislookup[i]->getLabel());
507                         temp->setGroup(thislookup[i]->getGroup());
508                         newLookupFloat.push_back(temp);
509                 }
510                 
511                 for (int i = 0; i < thislookup.size(); i++) {
512                         
513                         for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
514                                 
515                                 if (m->control_pressed) { return newLookupFloat; }
516                                 
517                                 int abund = thislookup[i]->getAbundance(j);
518                                 
519                                 float relabund = abund / (float) thislookup[i]->getNumSeqs();
520                                 
521                                 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
522                         }
523                 }
524                 
525                 return newLookupFloat;
526         }
527         catch(exception& e) {
528                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
529                 exit(1);
530         }
531 }
532 /***********************************************************************/
533
534 RAbundVector SharedRAbundVector::getRAbundVector() {
535         try {
536                 RAbundVector rav;
537                 
538                 for (int i = 0; i < data.size(); i++) {
539                         if(data[i].abundance != 0) {
540                                 rav.push_back(data[i].abundance);
541                         }
542                 }
543                 
544                 rav.setLabel(label);
545                 return rav;
546         }
547         catch(exception& e) {
548                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
549                 exit(1);
550         }
551 }
552 /***********************************************************************/
553
554 RAbundVector SharedRAbundVector::getRAbundVector2() {
555         try {
556                 RAbundVector rav;
557                 for(int i = 0; i < numBins; i++)
558                         if(data[i].abundance != 0)
559                                 rav.push_back(data[i].abundance-1);
560                 return rav;
561         }
562         catch(exception& e) {
563                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
564                 exit(1);
565         }
566 }
567 /***********************************************************************/
568
569 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
570         try {
571                 SharedSAbundVector sav(maxRank+1);
572                 
573                 for(int i=0;i<data.size();i++){
574                         int abund = data[i].abundance;
575                         sav.set(abund, sav.getAbundance(abund) + 1, group);
576                 }
577                 
578                 sav.set(0, 0, group);
579                 sav.setLabel(label);
580                 sav.setGroup(group);
581                 
582                 return sav;
583         }
584         catch(exception& e) {
585                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
586                 exit(1);
587         }
588 }
589 /***********************************************************************/
590
591 SAbundVector SharedRAbundVector::getSAbundVector() {
592         try {
593                 SAbundVector sav(maxRank+1);
594                 
595                 for(int i=0;i<data.size();i++){
596                         int abund = data[i].abundance;
597                         sav.set(abund, sav.get(abund) + 1);
598                 }
599                 sav.set(0, 0);
600                 sav.setLabel(label);
601                 return sav;
602         }
603         catch(exception& e) {
604                 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");                
605                 exit(1);
606         }
607 }
608
609 /***********************************************************************/
610
611 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
612         try {
613                 SharedOrderVector ov;
614         
615                 for(int i=0;i<data.size();i++){
616                         for(int j=0;j<data[i].abundance;j++){
617                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
618                         }
619                 }
620                 random_shuffle(ov.begin(), ov.end());
621
622                 ov.setLabel(label);     
623                 ov.updateStats();
624                 
625                 return ov;
626         }
627         catch(exception& e) {
628                 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
629                 exit(1);
630         }
631 }
632 /***********************************************************************/
633
634 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
635         try {
636                 OrderVector ov;
637                 for(int i=0;i<numBins;i++){
638                         for(int j=0;j<data[i].abundance;j++){
639                                 ov.push_back(i);
640                         }
641                 }
642                 random_shuffle(ov.begin(), ov.end());
643                 
644                 ov.setLabel(label);     
645
646                 return ov;
647         }
648         catch(exception& e) {
649                 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
650                 exit(1);
651         }
652 }
653
654 /***********************************************************************/
655