]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundfloatvector.cpp
added multiple processors option for Windows users to align.seqs, dist.seqs, summary...
[mothur.git] / sharedrabundfloatvector.cpp
1 /*
2  *  sharedrabundfloatvector.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 8/18/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "sharedrabundfloatvector.h"
11 #include "sharedutilities.h"
12
13 /***********************************************************************/
14
15 SharedRAbundFloatVector::SharedRAbundFloatVector() : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {}
16 /***********************************************************************/
17
18 SharedRAbundFloatVector::~SharedRAbundFloatVector() {}
19
20 /***********************************************************************/
21 SharedRAbundFloatVector::SharedRAbundFloatVector(int n) : DataVector(), maxRank(0.0), numBins(n), numSeqs(0.0) {
22                 
23                 individualFloat newGuy;
24                 //initialize data
25                 for (int i=0; i< n; i++) {
26                         newGuy.bin = i;
27                         newGuy.abundance = 0.0;
28                         data.push_back(newGuy);
29                 }
30 }
31 /***********************************************************************/
32 //reads a shared file
33 SharedRAbundFloatVector::SharedRAbundFloatVector(ifstream& f) : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {
34         try {
35                 
36                 m->clearAllGroups();
37                 vector<string> allGroups;
38                 
39                 int num, count;
40                 float inputData;
41                 count = 0;  
42                 string holdLabel, nextLabel, groupN;
43                 individualFloat newguy;
44                 
45                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
46                 lookup.clear();
47                 
48                 //are we at the beginning of the file??
49                 if (m->saveNextLabel == "") {  
50                         f >> label; 
51                         
52                         //is this a shared file that has headers
53                         if (label == "label") { 
54                                 //gets "group"
55                                 f >> label; m->gobble(f);
56                                 
57                                 //gets "numOtus"
58                                 f >> label; m->gobble(f);
59                                 
60                                 //eat rest of line
61                                 label = m->getline(f); m->gobble(f);
62                                 
63                                 //parse labels to save
64                                 istringstream iStringStream(label);
65                                 m->binLabelsInFile.clear();
66                                 while(!iStringStream.eof()){
67                                         if (m->control_pressed) { break; }
68                                         string temp;
69                                         iStringStream >> temp;  m->gobble(iStringStream);
70                                         
71                                         m->binLabelsInFile.push_back(temp);
72                                 }
73                                 
74                                 f >> label;
75                         }
76                 }else { label = m->saveNextLabel; }
77                 
78                 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
79                 m->currentBinLabels = m-> binLabelsInFile;
80                 
81                 //read in first row since you know there is at least 1 group.
82                 f >> groupN >> num;
83
84                 holdLabel = label;
85                 
86                 //add new vector to lookup
87                 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
88                 lookup.push_back(temp);
89                 lookup[0]->setLabel(label);
90                 lookup[0]->setGroup(groupN);
91                 
92                 allGroups.push_back(groupN);
93                 
94                 //fill vector.  data = first sharedrabund in file
95                 for(int i=0;i<num;i++){
96                         f >> inputData;
97                         
98                         lookup[0]->push_back(inputData, groupN); //abundance, bin, group
99                         push_back(inputData, groupN);
100                         
101                         if (inputData > maxRank) { maxRank = inputData; }
102                 }
103                 
104                 m->gobble(f);
105                 
106                 if (f.eof() != true) { f >> nextLabel; }
107                 
108                 //read the rest of the groups info in
109                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
110                         f >> groupN >> num;
111                         count++;
112                         
113                         allGroups.push_back(groupN);
114                         
115                         //add new vector to lookup
116                         temp = new SharedRAbundFloatVector();
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, groupN); //abundance, bin, group
125                         }
126                         
127                         m->gobble(f);
128                                 
129                         if (f.eof() != true) { f >> nextLabel; }
130                 }
131                 
132                 m->saveNextLabel = nextLabel;
133                 m->setAllGroups(allGroups);
134         
135         }
136         catch(exception& e) {
137                 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
138                 exit(1);
139         }
140 }
141
142 /***********************************************************************/
143
144 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
145         try {
146                 float oldBinSize = data[binNumber].abundance;
147                 data[binNumber].abundance = newBinSize;
148                 data[binNumber].group = groupname;
149                 
150                 if(newBinSize > maxRank)        {       newBinSize = newBinSize;        }
151         
152                 numSeqs += (newBinSize - oldBinSize);
153         }
154         catch(exception& e) {
155                 m->errorOut(e, "SharedRAbundFloatVector", "set");
156                 exit(1);
157         }
158 }
159 /***********************************************************************/
160
161 void SharedRAbundFloatVector::clear(){
162         numBins = 0;
163         maxRank = 0;
164         numSeqs = 0;
165         data.clear();
166         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
167         lookup.clear();
168 }
169 /***********************************************************************/
170 float SharedRAbundFloatVector::getAbundance(int index){
171         return data[index].abundance;   
172 }
173 /***********************************************************************/
174 individualFloat SharedRAbundFloatVector::get(int index){
175         return data[index];     
176 }
177 /***********************************************************************/
178 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
179         try {
180                 individualFloat newGuy;
181                 newGuy.abundance = binSize;
182                 newGuy.group = groupName;
183                 newGuy.bin = data.size();
184                 
185                 data.push_back(newGuy);
186                 numBins++;
187         
188                 if(binSize > maxRank){  maxRank = binSize;      }
189         
190                 numSeqs += binSize;
191         }
192         catch(exception& e) {
193                 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
194                 exit(1);
195         }
196 }
197 /***********************************************************************/
198 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
199         try {
200                 individualFloat newGuy;
201                 newGuy.abundance = binSize;
202                 newGuy.group = groupName;
203                 newGuy.bin = otu;
204                 
205                 data.insert(data.begin()+otu, newGuy);
206                 numBins++;
207         
208                 if(binSize > maxRank){  maxRank = binSize;      }
209
210                 numSeqs += binSize;
211         }
212         catch(exception& e) {
213                 m->errorOut(e, "SharedRAbundFloatVector", "insert");
214                 exit(1);
215         }
216 }
217
218 /***********************************************************************/
219 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
220         try {
221                 individualFloat newGuy;
222                 newGuy.abundance = binSize;
223                 newGuy.group = groupName;
224                 newGuy.bin = otu;
225                 
226                 data.insert(data.begin(), newGuy);
227                 numBins++;
228         
229                 if(binSize > maxRank){  maxRank = binSize;      }
230         
231                 numSeqs += binSize;
232         }
233         catch(exception& e) {
234                 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
235                 exit(1);
236         }
237 }
238 /**********************************************************************/
239 void SharedRAbundFloatVector::pop_back(){
240         numSeqs -= data[data.size()-1].abundance;
241         numBins--;
242         data.pop_back();
243 }
244 /***********************************************************************/
245 void SharedRAbundFloatVector::resize(int size){
246         data.resize(size);
247 }
248 /**********************************************************************/
249 int SharedRAbundFloatVector::size(){
250         return data.size();
251 }
252 /***********************************************************************/
253 void SharedRAbundFloatVector::printHeaders(ostream& output){
254         try {
255                 output << "label\tGroup\tnumOtus\t";
256                 if (m->sharedHeaderMode == "tax") {
257                         for (int i = 0; i < numBins; i++) {  
258                                 
259                                 //if there is a bin label use it otherwise make one
260                                 string binLabel = "PhyloType" + toString(i+1);
261                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
262                                 
263                                 output << binLabel << '\t'; 
264                         }
265                         output << endl;
266                 }else {
267                         for (int i = 0; i < numBins; i++) {  
268                                 //if there is a bin label use it otherwise make one
269                                 string binLabel = "Otu" + toString(i+1);
270                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
271                                 
272                                 output << binLabel << '\t'; 
273                         }
274                         
275                         output << endl;
276                 }
277                 
278                 m->printedHeaders = true;
279         }
280         catch(exception& e) {
281                 m->errorOut(e, "SharedRAbundVector", "printHeaders");
282                 exit(1);
283         }
284 }
285 /***********************************************************************/
286 void SharedRAbundFloatVector::print(ostream& output){
287         try {
288                 output << numBins << '\t';
289         
290                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
291                 output << endl;
292         }
293         catch(exception& e) {
294                 m->errorOut(e, "SharedRAbundFloatVector", "print");
295                 exit(1);
296         }
297 }
298 /***********************************************************************/
299 string SharedRAbundFloatVector::getGroup(){
300         return group;
301 }
302 /***********************************************************************/
303 void SharedRAbundFloatVector::setGroup(string groupName){
304         group = groupName;
305 }
306 /***********************************************************************/
307 int SharedRAbundFloatVector::getGroupIndex()  { return index; }
308 /***********************************************************************/
309 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
310 /***********************************************************************/
311 int SharedRAbundFloatVector::getNumBins(){      return numBins; }
312 /***********************************************************************/
313 float SharedRAbundFloatVector::getNumSeqs(){    return numSeqs; }
314 /***********************************************************************/
315 float SharedRAbundFloatVector::getMaxRank(){    return maxRank; }
316 /***********************************************************************/
317 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
318         return *this;                   
319 }
320 /***********************************************************************
321 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
322         try {
323                 SharedRAbundVector rav(numBins);
324                 rav.setLabel(label);
325                 rav.setGroup(group);
326                 
327                 for (int i = 0; i < data.size(); i++) {
328                         
329                         rav.push_back(data[i].abundance);
330                 }
331                 
332         }
333         catch(exception& e) {
334                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
335                 exit(1);
336         }               
337 }
338 /***********************************************************************/
339 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
340         try {
341                 SharedUtil* util;
342                 util = new SharedUtil();
343                 
344                 vector<string> Groups = m->getGroups();
345                 vector<string> allGroups = m->getAllGroups();
346                 util->setGroups(Groups, allGroups);
347                 m->setGroups(Groups);
348                 
349                 bool remove = false;
350                 for (int i = 0; i < lookup.size(); i++) {
351                         //if this sharedrabund is not from a group the user wants then delete it.
352                         if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) { 
353                                 delete lookup[i]; lookup[i] = NULL;
354                                 lookup.erase(lookup.begin()+i); 
355                                 i--; 
356                                 remove = true;
357                         }
358                 }
359                 
360                 delete util;
361                 
362                 if (remove) { eliminateZeroOTUS(lookup); }
363         
364                 return lookup;
365         }
366         catch(exception& e) {
367                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
368                 exit(1);
369         }
370 }
371 /***********************************************************************/
372
373 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
374         try {
375                 RAbundVector rav(numBins);
376                 
377                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
378                 
379                 rav.setLabel(label);
380                 return rav;
381         }
382         catch(exception& e) {
383                 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
384                 exit(1);
385         }
386 }
387 /***********************************************************************
388
389 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
390         try {
391                 SharedSAbundVector sav(maxRank+1);
392                 
393                 for(int i=0;i<data.size();i++){
394                         int abund = data[i].abundance;
395                         sav.set(abund, sav.getAbundance(abund) + 1, group);
396                 }
397                 
398                 sav.set(0, 0, group);
399                 sav.setLabel(label);
400                 sav.setGroup(group);
401                 
402                 return sav;
403         }
404         catch(exception& e) {
405                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
406                 exit(1);
407         }
408 }
409 /***********************************************************************/
410
411 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
412         try {
413                 SAbundVector sav(ceil(maxRank)+1);
414                 
415                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
416                 
417                 sav.set(0, 0);
418                 sav.setLabel(label);
419                 return sav;
420         }
421         catch(exception& e) {
422                 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");           
423                 exit(1);
424         }
425 }
426
427 /***********************************************************************
428
429 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
430         try {
431                 SharedOrderVector ov;
432         
433                 for(int i=0;i<data.size();i++){
434                         int round = ceil(data[i].abundance);
435                         for(int j=0;j<round;j++){
436                                 ov.push_back(data[i].bin, round, data[i].group);
437                         }
438                 }
439                 random_shuffle(ov.begin(), ov.end());
440
441                 ov.setLabel(label);     
442                 ov.updateStats();
443                 
444                 return ov;
445         }
446         catch(exception& e) {
447                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
448                 exit(1);
449         }
450 }
451 /***********************************************************************/
452 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
453 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
454         try {
455                 OrderVector ov;
456         
457                 for(int i=0;i<data.size();i++){
458                         int round = ceil(data[i].abundance);
459                         for(int j=0;j<round;j++){
460                                 ov.push_back(i);
461                         }
462                 }
463                 random_shuffle(ov.begin(), ov.end());
464
465                 ov.setLabel(label);     
466                 return ov;
467         }
468         catch(exception& e) {
469                 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
470                 exit(1);
471         }
472 }
473 //**********************************************************************************************************************
474 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
475         try {
476                 
477                 vector<SharedRAbundFloatVector*> newLookup;
478                 for (int i = 0; i < thislookup.size(); i++) {
479                         SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
480                         temp->setLabel(thislookup[i]->getLabel());
481                         temp->setGroup(thislookup[i]->getGroup());
482                         newLookup.push_back(temp);
483                 }
484                 
485                 //for each bin
486                 vector<string> newBinLabels;
487                 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
488                         if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
489                         
490                         //look at each sharedRabund and make sure they are not all zero
491                         bool allZero = true;
492                         for (int j = 0; j < thislookup.size(); j++) {
493                                 if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
494                         }
495                         
496                         //if they are not all zero add this bin
497                         if (!allZero) {
498                                 for (int j = 0; j < thislookup.size(); j++) {
499                                         newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
500                                 }
501                                 //if there is a bin label use it otherwise make one
502                                 string binLabel = "Otu" + toString(i+1);
503                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
504                                 
505                                 newBinLabels.push_back(binLabel);
506                         }
507                 }
508                 
509                 for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
510                 
511                 thislookup = newLookup;
512                 m->currentBinLabels = newBinLabels;
513                 
514                 return 0;
515                 
516         }
517         catch(exception& e) {
518                 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
519                 exit(1);
520         }
521 }
522 /***********************************************************************/
523