]> git.donarmstrong.com Git - mothur.git/blob - collectsharedcommand.cpp
fixed memory leak in collect.shared()
[mothur.git] / collectsharedcommand.cpp
1 /*
2  *  collectsharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "collectsharedcommand.h"
11 #include "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharedjabund.h"
15 #include "sharedsorabund.h"
16 #include "sharedjclass.h"
17 #include "sharedsorclass.h"
18 #include "sharedjest.h"
19 #include "sharedsorest.h"
20 #include "sharedthetayc.h"
21 #include "sharedthetan.h"
22 #include "sharedkstest.h"
23 #include "whittaker.h"
24 #include "sharednseqs.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "sharedwhittaker.h"
34
35
36
37 //**********************************************************************************************************************
38
39 CollectSharedCommand::CollectSharedCommand(){
40         try {
41                 globaldata = GlobalData::getInstance();
42                 string fileNameRoot;
43                 fileNameRoot = getRootName(globaldata->inputFileName);
44                 format = globaldata->getFormat();
45                 validCalculator = new ValidCalculators();
46                 util = new SharedUtil();
47                 
48                 int i;
49                 for (i=0; i<globaldata->Estimators.size(); i++) {
50                         if (validCalculator->isValidCalculator("shared", globaldata->Estimators[i]) == true) { 
51                                 if (globaldata->Estimators[i] == "sharedchao") { 
52                                         cDisplays.push_back(new CollectDisplay(new SharedChao1(), new SharedOneColumnFile(fileNameRoot+"shared.chao")));
53                                 }else if (globaldata->Estimators[i] == "sharedsobs") { 
54                                         cDisplays.push_back(new CollectDisplay(new SharedSobsCS(), new SharedOneColumnFile(fileNameRoot+"shared.sobs")));
55                                 }else if (globaldata->Estimators[i] == "sharedace") { 
56                                         cDisplays.push_back(new CollectDisplay(new SharedAce(), new SharedOneColumnFile(fileNameRoot+"shared.ace")));
57                                 }else if (globaldata->Estimators[i] == "jabund") {      
58                                         cDisplays.push_back(new CollectDisplay(new JAbund(), new SharedOneColumnFile(fileNameRoot+"jabund")));
59                                 }else if (globaldata->Estimators[i] == "sorabund") { 
60                                         cDisplays.push_back(new CollectDisplay(new SorAbund(), new SharedOneColumnFile(fileNameRoot+"sorabund")));
61                                 }else if (globaldata->Estimators[i] == "jclass") { 
62                                         cDisplays.push_back(new CollectDisplay(new Jclass(), new SharedOneColumnFile(fileNameRoot+"jclass")));
63                                 }else if (globaldata->Estimators[i] == "sorclass") { 
64                                         cDisplays.push_back(new CollectDisplay(new SorClass(), new SharedOneColumnFile(fileNameRoot+"sorclass")));
65                                 }else if (globaldata->Estimators[i] == "jest") { 
66                                         cDisplays.push_back(new CollectDisplay(new Jest(), new SharedOneColumnFile(fileNameRoot+"jest")));
67                                 }else if (globaldata->Estimators[i] == "sorest") { 
68                                         cDisplays.push_back(new CollectDisplay(new SorEst(), new SharedOneColumnFile(fileNameRoot+"sorest")));
69                                 }else if (globaldata->Estimators[i] == "thetayc") { 
70                                         cDisplays.push_back(new CollectDisplay(new ThetaYC(), new SharedOneColumnFile(fileNameRoot+"thetayc")));
71                                 }else if (globaldata->Estimators[i] == "thetan") { 
72                                         cDisplays.push_back(new CollectDisplay(new ThetaN(), new SharedOneColumnFile(fileNameRoot+"thetan")));
73                                 }else if (globaldata->Estimators[i] == "kstest") { 
74                                         cDisplays.push_back(new CollectDisplay(new KSTest(), new SharedOneColumnFile(fileNameRoot+"kstest")));
75                                 }else if (globaldata->Estimators[i] == "whittaker") { 
76                                         cDisplays.push_back(new CollectDisplay(new Whittaker(), new SharedOneColumnFile(fileNameRoot+"whittaker")));
77                                 }else if (globaldata->Estimators[i] == "sharednseqs") { 
78                                         cDisplays.push_back(new CollectDisplay(new SharedNSeqs(), new SharedOneColumnFile(fileNameRoot+"shared.nseqs")));
79
80                                 }else if (globaldata->Estimators[i] == "ochiai") { 
81                                         cDisplays.push_back(new CollectDisplay(new Ochiai(), new SharedOneColumnFile(fileNameRoot+"ochiai")));
82                                 }else if (globaldata->Estimators[i] == "anderberg") { 
83                                         cDisplays.push_back(new CollectDisplay(new Anderberg(), new SharedOneColumnFile(fileNameRoot+"anderberg")));
84                                 }else if (globaldata->Estimators[i] == "skulczynski") { 
85                                         cDisplays.push_back(new CollectDisplay(new Kulczynski(), new SharedOneColumnFile(fileNameRoot+"kulczynski")));
86                                 }else if (globaldata->Estimators[i] == "kulczynskicody") { 
87                                         cDisplays.push_back(new CollectDisplay(new KulczynskiCody(), new SharedOneColumnFile(fileNameRoot+"kulczynskicody")));
88                                 }else if (globaldata->Estimators[i] == "lennon") { 
89                                         cDisplays.push_back(new CollectDisplay(new Lennon(), new SharedOneColumnFile(fileNameRoot+"lennon")));
90                                 }else if (globaldata->Estimators[i] == "morisitahorn") { 
91                                         cDisplays.push_back(new CollectDisplay(new MorHorn(), new SharedOneColumnFile(fileNameRoot+"morisitahorn")));
92                                 }else if (globaldata->Estimators[i] == "braycurtis") { 
93                                         cDisplays.push_back(new CollectDisplay(new BrayCurtis(), new SharedOneColumnFile(fileNameRoot+"braycurtis")));
94                                 }
95                         }
96                 }
97                 
98                 //reset calc for next command
99                 globaldata->setCalc("");
100
101         }
102         catch(exception& e) {
103                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function CollectSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104                 exit(1);
105         }
106         catch(...) {
107                 cout << "An unknown error has occurred in the CollectSharedCommand class function CollectSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108                 exit(1);
109         }       
110                         
111 }
112
113 //**********************************************************************************************************************
114
115 CollectSharedCommand::~CollectSharedCommand(){
116         delete order;
117         delete input;
118         delete cCurve;
119         delete read;
120         delete util;
121 }
122
123 //**********************************************************************************************************************
124
125 int CollectSharedCommand::execute(){
126         try {
127                 int count = 1;
128                 
129                 //if the users entered no valid calculators don't execute command
130                 if (cDisplays.size() == 0) { return 0; }
131                 
132                 read = new ReadOTUFile(globaldata->inputFileName);      
133                 read->read(&*globaldata); 
134                         
135                 input = globaldata->ginput;
136                 order = input->getSharedOrderVector();
137                 
138                 set<string> orderList;
139                 
140                 //set users groups
141                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "collect");
142                 util->updateGroupIndex(globaldata->Groups, globaldata->gGroupmap->groupIndex);
143
144                 while(order != NULL){
145
146                         orderList.insert(order->getLabel());
147                         
148                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
149                                 
150                                 //create collectors curve
151                                 cCurve = new Collect(order, cDisplays);
152                                 convert(globaldata->getFreq(), freq);
153                                 cCurve->getSharedCurve(freq);
154                         
155                                 delete cCurve;
156                         
157                                 cout << order->getLabel() << '\t' << count << endl;
158                         }
159                         
160                         //get next line to process
161                         delete order;
162                         order = input->getSharedOrderVector();
163                         count++;
164                 }
165                 set<string>::iterator i;
166                 for(i = globaldata->labels.begin(); i != globaldata->labels.end(); ++i)
167                         if(orderList.count(*i) == 0)
168                                 cout << "'" << *i << "'" << " is not a valid label.\n";
169                                 
170                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }       
171                 
172                 //reset groups parameter
173                 globaldata->Groups.clear();  globaldata->setGroups("");
174                 
175                 return 0;
176         }
177         catch(exception& e) {
178                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179                 exit(1);
180         }
181         catch(...) {
182                 cout << "An unknown error has occurred in the CollectSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }       
185 }
186
187 /***********************************************************/
188