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