]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
added smart distance feature and optimized all commands using line by line processing
[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         
87                 //as long as you are not at the end of the file or done wih the lines you want
88                 while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
89                         
90                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
91                                 
92                                 rCurve = new Rarefact(lookup, rDisplays);
93                                 rCurve->getSharedCurve(freq, nIters);
94                                 delete rCurve;
95                         
96                                 cout << lookup[0]->getLabel() << '\t' << count << endl;
97                                 processedLabels.insert(lookup[0]->getLabel());
98                                 userLabels.erase(lookup[0]->getLabel());
99                         }
100                         
101                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
102                                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
103                                         rCurve = new Rarefact(lastLookup, rDisplays);
104                                         rCurve->getSharedCurve(freq, nIters);
105                                         delete rCurve;
106
107                                         processedLabels.insert(lastLookup[0]->getLabel());
108                                         userLabels.erase(lastLookup[0]->getLabel());
109                         }
110                                 
111                         //prevent memory leak
112                         if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
113                         lastLookup = lookup;
114                         
115                         //get next line to process
116                         lookup = input->getSharedRAbundVectors();
117                         count++;
118                 }
119                 
120                 //output error messages about any remaining user labels
121                 set<string>::iterator it;
122                 bool needToRun = false;
123                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
124                         cout << "Your file does not include the label "<< *it; 
125                         if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
126                                 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
127                                 needToRun = true;
128                         }else {
129                                 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
130                         }
131                 }
132                 
133                 //run last line if you need to
134                 if (needToRun == true)  {
135                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
136                         rCurve = new Rarefact(lastLookup, rDisplays);
137                         rCurve->getSharedCurve(freq, nIters);
138                         delete rCurve;
139                 }
140                 
141                 for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
142
143                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
144                 
145                 //reset groups parameter
146                 globaldata->Groups.clear();  globaldata->setGroups("");
147
148                 return 0;
149         }
150         catch(exception& e) {
151                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152                 exit(1);
153         }
154         catch(...) {
155                 cout << "An unknown error has occurred in the RareFactSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156                 exit(1);
157         }       
158 }
159
160
161 //**********************************************************************************************************************