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