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