]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
added get.rabund and get.sabund command and fixed bug introduced by line by line...
[mothur.git] / rarefactsharedcommand.cpp
1 /*
2  *  rarefactsharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/6/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "rarefactsharedcommand.h"
11 #include "sharedsobs.h"
12 #include "sharednseqs.h"
13
14 //**********************************************************************************************************************
15
16 RareFactSharedCommand::RareFactSharedCommand(){
17         try {
18                 globaldata = GlobalData::getInstance();
19                 string fileNameRoot;
20                 fileNameRoot = getRootName(globaldata->inputFileName);
21                 format = globaldata->getFormat();
22                 convert(globaldata->getFreq(), freq);
23                 convert(globaldata->getIters(), nIters);
24                 validCalculator = new ValidCalculators();
25                                 
26                 int i;
27                 for (i=0; i<globaldata->Estimators.size(); i++) {
28                         if (validCalculator->isValidCalculator("sharedrarefaction", globaldata->Estimators[i]) == true) { 
29                                 if (globaldata->Estimators[i] == "sharedobserved") { 
30                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
31                                 }else if (globaldata->Estimators[i] == "sharednseqs") { 
32                                         rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
33                                 }
34
35                         }
36                 }
37                 
38                 //reset calc for next command
39                 globaldata->setCalc("");
40
41         }
42         catch(exception& e) {
43                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
44                 exit(1);
45         }
46         catch(...) {
47                 cout << "An unknown error has occurred in the RareFactSharedCommand class function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
48                 exit(1);
49         }       
50                         
51 }
52
53 //**********************************************************************************************************************
54
55 RareFactSharedCommand::~RareFactSharedCommand(){
56         delete input;
57         delete rCurve;
58         delete read;
59 }
60
61 //**********************************************************************************************************************
62
63 int RareFactSharedCommand::execute(){
64         try {
65                 int count = 1;
66                 
67                 //if the users entered no valid calculators don't execute command
68                 if (rDisplays.size() == 0) { return 0; }
69
70                 read = new ReadOTUFile(globaldata->inputFileName);      
71                 read->read(&*globaldata); 
72                         
73                 input = globaldata->ginput;
74                 lookup = input->getSharedRAbundVectors();
75                 vector<SharedRAbundVector*> lastLookup = lookup;
76                 
77                 if (lookup.size() < 2) { 
78                         cout << "I cannot run the command without at least 2 valid groups."; 
79                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
80                         return 0;
81                 }
82                 
83                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
84                 set<string> processedLabels;
85                 set<string> userLabels = globaldata->labels;
86                 set<int> userLines = globaldata->lines;
87         
88                 //as long as you are not at the end of the file or done wih the lines you want
89                 while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
90                         
91                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
92                                 
93                                 rCurve = new Rarefact(lookup, rDisplays);
94                                 rCurve->getSharedCurve(freq, nIters);
95                                 delete rCurve;
96                         
97                                 cout << lookup[0]->getLabel() << '\t' << count << endl;
98                                 processedLabels.insert(lookup[0]->getLabel());
99                                 userLabels.erase(lookup[0]->getLabel());
100                                 userLines.erase(count);
101                         }
102                         
103                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
104                                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
105                                         rCurve = new Rarefact(lastLookup, rDisplays);
106                                         rCurve->getSharedCurve(freq, nIters);
107                                         delete rCurve;
108
109                                         processedLabels.insert(lastLookup[0]->getLabel());
110                                         userLabels.erase(lastLookup[0]->getLabel());
111                         }
112                                 
113                         //prevent memory leak
114                         if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
115                         lastLookup = lookup;
116                         
117                         //get next line to process
118                         lookup = input->getSharedRAbundVectors();
119                         count++;
120                 }
121                 
122                 //output error messages about any remaining user labels
123                 set<string>::iterator it;
124                 bool needToRun = false;
125                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
126                         cout << "Your file does not include the label "<< *it; 
127                         if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
128                                 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
129                                 needToRun = true;
130                         }else {
131                                 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
132                         }
133                 }
134                 
135                 //run last line if you need to
136                 if (needToRun == true)  {
137                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
138                         rCurve = new Rarefact(lastLookup, rDisplays);
139                         rCurve->getSharedCurve(freq, nIters);
140                         delete rCurve;
141                 }
142                 
143                 for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
144
145                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
146                 
147                 //reset groups parameter
148                 globaldata->Groups.clear();  globaldata->setGroups("");
149
150                 return 0;
151         }
152         catch(exception& e) {
153                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
154                 exit(1);
155         }
156         catch(...) {
157                 cout << "An unknown error has occurred in the RareFactSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
158                 exit(1);
159         }       
160 }
161
162
163 //**********************************************************************************************************************