]> git.donarmstrong.com Git - mothur.git/blob - globaldata.cpp
fixed memory issue with cluster command
[mothur.git] / globaldata.cpp
1 #include "globaldata.hpp"
2 #include "sparsematrix.hpp"
3 #include "tree.h"
4 #include "rabundvector.hpp"
5 #include "sabundvector.hpp"
6 #include "listvector.hpp"
7
8 /*******************************************************/
9
10 /******************************************************/
11 GlobalData* GlobalData::getInstance() {
12         if( _uniqueInstance == 0 ) {
13                 _uniqueInstance = new GlobalData();
14         }
15         return _uniqueInstance;
16 }
17 /*******************************************************/
18
19 /******************************************************/
20 //This function parses through the option string of the command to remove its parameters
21 void GlobalData::parseGlobalData(string commandString, string optionText){
22         try {
23                 allLines = 1;
24                 commandName = commandString; //save command name to be used by other classes
25                 
26                 //set all non filename paramters to default
27                 reset();
28                 
29                 //clears out data from previous read
30                 if ((commandName == "read.dist") || (commandName == "read.otu") || (commandName == "read.tree") || (commandName == "read.shared")) { 
31                         clear();
32                         gGroupmap = NULL;
33                         gTree.clear();
34                         labels.clear(); lines.clear(); groups.clear();
35                 }
36                 
37                 //saves help request
38                 if (commandName =="help") {
39                         helpRequest = optionText;
40                 }
41                 
42                 string key, value;              
43                 //reads in parameters and values
44                 if((optionText != "") && (commandName != "help")){
45                         while((optionText.find_first_of(',') != -1)) {  //while there are parameters
46                                 splitAtComma(value, optionText);
47                                 splitAtEquals(key, value);
48                                 
49                                 if (key == "phylip" )   { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip";       }
50                                 if (key == "column" )   { columnfile = value; inputFileName = value; fileroot = value; format = "column";       }
51                                 if (key == "list" )             { listfile = value; inputFileName = value; fileroot = value; format = "list";           }
52                                 if (key == "rabund" )   { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund";       }
53                                 if (key == "sabund" )   { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund";       } 
54                                 if (key == "fasta" )    { fastafile = value; inputFileName = value; fileroot = value; format = "fasta";         } 
55                                 if (key == "tree" )             { treefile = value; inputFileName = value; fileroot = value; format = "tree";           }
56                                 if (key == "shared" )   { sharedfile = value; inputFileName = value; fileroot = value; format = "sharedfile";   }
57                                 if (key == "name" )             { namefile = value;             }
58                                 if (key == "order" )    { orderfile = value;    }
59                                 if (key == "group" )    { groupfile = value;    }
60                                 if (key == "cutoff" )           { cutoff = value;               }
61                                 if (key == "precision" )        { precision = value;    }
62                                 if (key == "iters" )            { iters = value;                }
63                                 if (key == "jumble" )           { jumble = value;               }
64                                 if (key == "freq" )                     { freq = value;                 }
65                                 if (key == "method" )           { method = value;               }
66                                 if (key == "fileroot" )         { fileroot = value;             }
67                                 if (key == "abund" )        { abund = value;        }
68                                 if (key == "random" )           { randomtree = value;   }
69                                 if (key == "calc")                      { calc = value;         }
70
71                                 
72                                 if (key == "line") {//stores lines to be used in a set
73                                         lines.clear();
74                                         line = value;
75                                         label = "";
76                                         splitAtDash(value, lines);
77                                         allLines = 0;
78                                 }
79                                 if (key == "label") {//stores labels to be used in a set
80                                         labels.clear();
81                                         label = value;
82                                         line = "";
83                                         splitAtDash(value, labels);
84                                         allLines = 0;
85                                 }
86
87                                 if (key == "groups") {//stores groups to be used in a vector
88                                         Groups.clear();
89                                         groups = value;
90                                         splitAtDash(value, Groups);
91                                 }
92
93                         }
94                         
95                         //saves the last parameter
96                         value = optionText;
97                         splitAtEquals(key, value);
98                         if (key == "phylip" )   { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip";       }
99                         if (key == "column" )   { columnfile = value; inputFileName = value; fileroot = value; format = "column";       }
100                         if (key == "list" )             { listfile = value; inputFileName = value; fileroot = value; format = "list";           }
101                         if (key == "rabund" )   { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund";       }
102                         if (key == "sabund" )   { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund";       }
103                         if (key == "fasta" )    { fastafile = value; inputFileName = value; fileroot = value; format = "fasta";         }
104                         if (key == "tree" )             { treefile = value; inputFileName = value; fileroot = value; format = "tree";           } 
105                         if (key == "shared" )   { sharedfile = value; inputFileName = value; fileroot = value; format = "sharedfile";   } 
106                         if (key == "name" )             { namefile = value;             }
107                         if (key == "order" )    { orderfile = value;    }
108                         if (key == "group" )    { groupfile = value;    }
109                         if (key == "cutoff" )           { cutoff = value;               }
110                         if (key == "precision" )        { precision = value;    }
111                         if (key == "iters" )            { iters = value;                }
112                         if (key == "jumble" )           { jumble = value;               }
113                         if (key == "freq" )                     { freq = value;                 }
114                         if (key == "method" )           { method = value;               }
115                         if (key == "fileroot" )         { fileroot = value;             }
116                         if (key == "abund" )        { abund = value;        }
117                         if (key == "random" )           { randomtree = value;   }
118                         if (key == "calc")                      { calc = value;         }
119
120
121                         if (key == "line") {//stores lines to be used in a vector
122                                 lines.clear();
123                                 line = value;
124                                 label = "";
125                                 splitAtDash(value, lines);
126                                 allLines = 0;
127                         }
128                         if (key == "label") {//stores lines to be used in a vector
129                                 labels.clear();
130                                 label = value;
131                                 line = "";
132                                 splitAtDash(value, labels);
133                                 allLines = 0;
134                         }
135                         if (key == "groups") {//stores groups to be used in a vector
136                                         Groups.clear();
137                                         groups = value;
138                                         splitAtDash(value, Groups);
139                         }
140                 }
141                 
142                 //set format for shared
143                 if ((listfile != "") && (groupfile != "")) { format = "shared"; }
144                 if ((phylipfile != "") && (groupfile != "")) { format = "matrix"; }
145                                 
146                 //input defaults for calculators
147                 if (commandName == "collect.single") {
148                         if ((calc == "default") || (calc == "")) { calc = "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson"; }
149                         Estimators.clear();
150                         splitAtDash(calc, Estimators); 
151                 }
152                 if (commandName == "rarefaction.single") {
153                         if ((calc == "default") || (calc == "")) { calc = "sobs"; }
154                         Estimators.clear();
155                         splitAtDash(calc, Estimators); 
156                 }
157                 if (commandName == "collect.shared") {
158                         if ((calc == "default") || (calc == "")) { calc = "sharedsobs-sharedchao-sharedace-sharedjabund-sharedsorensonabund-sharedjclass-sharedsorclass-sharedjest-sharedsorest-sharedthetayc-sharedthetan"; }
159                         Estimators.clear();
160                         splitAtDash(calc, Estimators); 
161                 }
162                 if (commandName == "summary.single") {
163                         if ((calc == "default") || (calc == "")) { calc = "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson"; }
164                         Estimators.clear();
165                         splitAtDash(calc, Estimators); 
166                 }
167                 if (commandName == "summary.shared") {
168                         if ((calc == "default") || (calc == "")) { calc = "sharedsobs-sharedchao-sharedace-sharedjabund-sharedsorensonabund-sharedjclass-sharedsorclass-sharedjest-sharedsorest-sharedthetayc-sharedthetan"; }
169                         Estimators.clear();
170                         splitAtDash(calc, Estimators); 
171                 }
172                 if (commandName == "rarefaction.shared") {
173                         if ((calc == "default") || (calc == "")) { calc = "sharedobserved"; }
174                         Estimators.clear();
175                         splitAtDash(calc, Estimators); 
176                 }
177
178                 //if you have done a read.otu with a groupfile but don't want to use it anymore because you want to do single commands
179                 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single")) {
180                         if (listfile != "") { format = "list"; }
181                         else if (sabundfile != "") { format = "sabund"; }
182                         else if (rabundfile != "") { format = "rabund"; }
183                 }
184         }
185         catch(exception& e) {
186                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187                 exit(1);
188         }
189         catch(...) {
190                 cout << "An unknown error has occurred in the GlobalData class function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
191                 exit(1);
192         }
193 }
194 /*******************************************************/
195
196 /******************************************************/
197 // These functions give you the option parameters of the commands
198 string GlobalData::getPhylipFile()              {       return phylipfile;      }
199 string GlobalData::getColumnFile()              {       return columnfile;      }
200 string GlobalData::getListFile()                {       return listfile;        }
201 string GlobalData::getRabundFile()              {       return rabundfile;      }
202 string GlobalData::getSabundFile()              {       return sabundfile;      }
203 string GlobalData::getNameFile()                {       return namefile;        }
204 string GlobalData::getGroupFile()               {       return groupfile;       }
205 string GlobalData::getOrderFile()               {       return orderfile;       }
206 string GlobalData::getTreeFile()                {       return treefile;        }
207 string GlobalData::getSharedFile()              {       return sharedfile;      }
208 string GlobalData::getFastaFile()               {       return fastafile;       }
209 string GlobalData::getCutOff()                  {       return cutoff;          }
210 string GlobalData::getFormat()                  {       return format;          }
211 string GlobalData::getPrecision()               {       return precision;       }
212 string GlobalData::getMethod()                  {       return method;          }
213 string GlobalData::getFileRoot()                {       return fileroot;        }
214 string GlobalData::getIters()                   {       return iters;           }
215 string GlobalData::getJumble()                  {       return jumble;          }
216 string GlobalData::getFreq()                    {       return freq;            }
217 string GlobalData::getAbund()           {   return abund;       }
218 string GlobalData::getRandomTree()              {       return randomtree;      }
219 string GlobalData::getGroups()                  {       return groups;          }
220 void GlobalData::setListFile(string file)       {       listfile = file;        inputFileName = file;}
221 void GlobalData::setRabundFile(string file)     {       rabundfile = file;      inputFileName = file;}
222 void GlobalData::setSabundFile(string file)     {       sabundfile = file;      inputFileName = file;}
223 void GlobalData::setPhylipFile(string file)     {       phylipfile = file;    inputFileName = file;}
224 void GlobalData::setColumnFile(string file)     {       columnfile = file;    inputFileName = file;}
225 void GlobalData::setNameFile(string file)               {       namefile = file;                }
226 void GlobalData::setFormat(string Format)               {       format = Format;                }
227 void GlobalData::setRandomTree(string Random)   {       randomtree = Random;    }
228 void GlobalData::setGroups(string g)                    {       groups = g;                             }
229 void GlobalData::setCalc(string Calc)                   {       calc = Calc;                    }
230
231 /*******************************************************/
232
233 /******************************************************/
234 GlobalData::GlobalData() {
235         //option definitions should go here...
236         helpRequest = "";
237         clear();
238 }
239 /*******************************************************/
240
241 /******************************************************/
242 void GlobalData::clear() {
243         //option definitions should go here...
244         phylipfile              =       "";
245         columnfile              =       "";
246         listfile                =       "";
247         rabundfile              =       "";
248         sabundfile              =       "";
249         namefile                =       "";
250         groupfile               =       ""; 
251         orderfile               =       "";
252         fastafile               =   "";
253         treefile                =       "";
254         sharedfile              =       "";
255         cutoff                  =       "10.00";
256         format                  =       "";
257         precision               =       "100";
258         iters                   =       "1000"; 
259         line                    =   "";
260         label                   =       "";
261         groups                  =       "";
262         jumble                  =       "1";    //0 means don't jumble, 1 means jumble.
263         randomtree              =       "";  //"" means user will enter some user trees, "outputfile" means they just want the random tree distribution to be outputted to outputfile.
264         freq                    =       "100";
265         method                  =       "furthest";
266         fileroot                =       "";
267         abund           =   "10";
268 }
269
270 //*******************************************************/
271
272 /******************************************************/
273 void GlobalData::reset() {
274         cutoff                  =       "10.00";
275         precision               =       "100";
276         iters                   =       "1000"; 
277         line                    =   "";
278         label                   =       "";
279         groups                  =       "";
280         jumble                  =       "1";    //0 means don't jumble, 1 means jumble.
281         randomtree              =       "";  //"" means user will enter some user trees, "outputfile" means they just want the random tree distribution to be outputted to outputfile.
282         freq                    =       "100";
283         method                  =       "furthest";
284         calc                    =       "";
285         abund = "10";
286 }
287 /*******************************************************/
288
289 /******************************************************/
290 GlobalData::~GlobalData() {
291         _uniqueInstance = 0;
292         if(gListVector != NULL)         {       delete gListVector;             }
293         if(gSparseMatrix != NULL)       {       delete gSparseMatrix;   }
294         if(gorder != NULL)                      {       delete gorder;          }
295 }
296 /*******************************************************/