]> git.donarmstrong.com Git - mothur.git/blob - globaldata.cpp
fixed summary.shared bug and set jumble default to 1.
[mothur.git] / globaldata.cpp
1 #include <iostream>
2 #include <iomanip>
3 #include <string>
4 #include <map>
5 #include <sstream>
6 #include <stdexcept>
7
8 using namespace std;
9
10 #include "globaldata.hpp"
11 #include "sparsematrix.hpp"
12 #include "tree.h"
13 #include "rabundvector.hpp"
14 #include "sabundvector.hpp"
15 #include "listvector.hpp"
16 #include <exception>
17 #include <iostream>
18
19 /*******************************************************/
20
21 /******************************************************/
22 GlobalData* GlobalData::getInstance() {
23         if( _uniqueInstance == 0 ) {
24                 _uniqueInstance = new GlobalData();
25         }
26         return _uniqueInstance;
27 }
28 /*******************************************************/
29
30 /******************************************************/
31
32 ListVector* GlobalData::getListVector()         {       return gListVector;             }
33 /*******************************************************/
34
35 /******************************************************/
36 void GlobalData::setListVector(ListVector* lv){
37         try {
38                 if(gListVector != NULL){        delete gListVector;     }
39                 gListVector = new ListVector(*lv);
40         }
41         catch(exception& e) {
42                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function setListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
43                 exit(1);
44         }
45         catch(...) {
46                 cout << "An unknown error has occurred in the GlobalData class function setListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47                 exit(1);
48         }
49 }
50
51 /*******************************************************/
52
53 /******************************************************/
54
55 SparseMatrix* GlobalData::getSparseMatrix()     {       return gSparseMatrix;   }
56 /*******************************************************/
57
58 /******************************************************/
59 void GlobalData::setSparseMatrix(SparseMatrix* sm){
60         try{
61                 if(gSparseMatrix != NULL){      delete gSparseMatrix;   }
62                 gSparseMatrix = new SparseMatrix(*sm);
63         }
64         catch(exception& e) {
65                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function setSparseMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
66                 exit(1);
67         }
68         catch(...) {
69                 cout << "An unknown error has occurred in the GlobalData class function setSparseMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
70                 exit(1);
71         }
72
73 }
74 /*******************************************************/
75
76 /******************************************************/
77 //This function parses through the option string of the command to remove its parameters
78 void GlobalData::parseGlobalData(string commandString, string optionText){
79         try {
80                 allLines = 1;
81                 commandName = commandString; //save command name to be used by other classes
82                 
83                 //clears out data from previous read
84                 if ((commandName == "read.dist") || (commandName == "read.otu") || (commandName == "read.tree")) { 
85                         clear();
86                 }
87                 
88                 //saves help request
89                 if (commandName =="help") {
90                         helpRequest = optionText;
91                 }
92                 
93                 string key, value;              
94                 //reads in parameters and values
95                 if((optionText != "") && (commandName != "help")){
96                         while((optionText.find_first_of(',') != -1)) {  //while there are parameters
97                                 splitAtComma(value, optionText);
98                                 splitAtEquals(key, value);
99                                 
100                                 if (key == "phylipfile" )       { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip";       }
101                                 if (key == "columnfile" )       { columnfile = value; inputFileName = value; fileroot = value; format = "column";       }
102                                 if (key == "listfile" )         { listfile = value; inputFileName = value; fileroot = value; format = "list";           }
103                                 if (key == "rabundfile" )       { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund";       }
104                                 if (key == "sabundfile" )       { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund";       } 
105                                 if (key == "fastafile" )        { fastafile = value; inputFileName = value; fileroot = value; format = "fasta";         } 
106                                 if (key == "treefile" )         { treefile = value; inputFileName = value; fileroot = value; format = "tree";           }
107                                 if (key == "namefile" )         { namefile = value;             }
108                                 if (key == "orderfile" )        { orderfile = value;    }
109                                 if (key == "groupfile" )        { groupfile = value;    }
110                                 if (key == "cutoff" )           { cutoff = value;               }
111                                 if (key == "precision" )        { precision = value;    }
112                                 if (key == "iters" )            { iters = value;                }
113                                 if (key == "jumble" )           { jumble = value;               }
114                                 if (key == "freq" )                     { freq = value;                 }
115                                 if (key == "method" )           { method = value;               }
116                                 if (key == "fileroot" )         { fileroot = value;             }
117                                 
118                                 if (key == "single") {//stores estimators in a vector
119                                         singleEstimators.clear(); //clears out old values
120                                         if (value == "default") { value = "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson-rarefraction"; }
121                                         splitAtDash(value, singleEstimators);
122                                 }
123                                 if (key == "rarefaction") {//stores estimators in a vector
124                                         rareEstimators.clear(); //clears out old values
125                                         if (value == "default") { value = "rarefraction"; }
126                                         splitAtDash(value, rareEstimators);
127                                 }
128                                 if (key == "shared") {//stores estimators in a vector
129                                         sharedEstimators.clear(); //clears out old values
130                                         if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
131                                         splitAtDash(value, sharedEstimators);
132                                 }
133                                 if (key == "summary") { //stores summaries to be used in a vector
134                                         summaryEstimators.clear();
135                                         if (value == "default") { value = "summary-chao-ace-jack-bootstrap-shannon-npshannon-simpson"; }
136                                         splitAtDash(value, summaryEstimators);
137                                 }
138                                 if (key == "sharedsummary") { //stores sharedSummaries to be used in a vector
139                                         sharedSummaryEstimators.clear();
140                                         if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
141                                         splitAtDash(value, sharedSummaryEstimators);
142                                 }
143                                 if (key == "sharedrarefaction") { //stores sharedrarefaction to be used in a vector
144                                         sharedRareEstimators.clear();
145                                         if (value == "default") { value = "sharedobserved"; }
146                                         splitAtDash(value, sharedRareEstimators);
147                                 }
148                                 if (key == "line") {//stores lines to be used in a vector
149                                         lines.clear();
150                                         line = value;
151                                         splitAtDash(value, lines);
152                                         allLines = 0;
153                                 }
154                                 if (key == "label") {//stores lines to be used in a vector
155                                         labels.clear();
156                                         label = value;
157                                         splitAtDash(value, labels);
158                                         allLines = 0;
159                                 }
160                         }
161                         
162                         //saves the last parameter
163                         value = optionText;
164                         splitAtEquals(key, value);
165                         if (key == "phylipfile" )       { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip";       }
166                         if (key == "columnfile" )       { columnfile = value; inputFileName = value; fileroot = value; format = "column";       }
167                         if (key == "listfile" )         { listfile = value; inputFileName = value; fileroot = value; format = "list";           }
168                         if (key == "rabundfile" )       { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund";       }
169                         if (key == "sabundfile" )       { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund";       }
170                         if (key == "fastafile" )        { fastafile = value; inputFileName = value; fileroot = value; format = "fasta";         }
171                         if (key == "treefile" )         { treefile = value; inputFileName = value; fileroot = value; format = "tree";           }  
172                         if (key == "namefile" )         { namefile = value;             }
173                         if (key == "orderfile" )        { orderfile = value;    }
174                         if (key == "groupfile" )        { groupfile = value;    }
175                         if (key == "cutoff" )           { cutoff = value;               }
176                         if (key == "precision" )        { precision = value;    }
177                         if (key == "iters" )            { iters = value;                }
178                         if (key == "jumble" )           { jumble = value;               }
179                         if (key == "freq" )                     { freq = value;                 }
180                         if (key == "method" )           { method = value;               }
181                         if (key == "fileroot" )         { fileroot = value;             }
182
183                         if (key == "single") {//stores estimators in a vector
184                                 singleEstimators.clear(); //clears out old values
185                                 if (value == "default") { value = "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson-rarefraction"; }
186                                 splitAtDash(value, singleEstimators);
187                         }
188                         if (key == "rarefaction") {//stores estimators in a vector
189                                 rareEstimators.clear(); //clears out old values
190                                 if (value == "default") { value = "rarefraction"; }
191                                 splitAtDash(value, rareEstimators);
192                         }
193                         if (key == "shared") {//stores estimators in a vector
194                                 sharedEstimators.clear(); //clears out old values
195                                 if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
196                                 splitAtDash(value, sharedEstimators);
197                         }
198                         if (key == "summary") { //stores summaries to be used in a vector
199                                 summaryEstimators.clear();
200                                 if (value == "default") { value = "summary-chao-ace-jack-bootstrap-shannon-npshannon-simpson"; }
201                                 splitAtDash(value, summaryEstimators);
202                         }
203                         if (key == "sharedsummary") { //stores sharedSummaries to be used in a vector
204                                 sharedSummaryEstimators.clear();
205                                 if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
206                                 splitAtDash(value, sharedSummaryEstimators);
207                         }
208                         if (key == "sharedrarefaction") { //stores sharedrarefaction to be used in a vector
209                                 sharedRareEstimators.clear();
210                                 if (value == "default") { value = "sharedobserved"; }
211                                 splitAtDash(value, sharedRareEstimators);
212                         }
213                         
214                         if (key == "line") {//stores lines to be used in a vector
215                                 lines.clear();
216                                 line = value;
217                                 splitAtDash(value, lines);
218                                 allLines = 0;
219                         }
220                         if (key == "label") {//stores lines to be used in a vector
221                                 labels.clear();
222                                 label = value;
223                                 splitAtDash(value, labels);
224                                 allLines = 0;
225                         }
226                 }
227                 
228                 //set format for shared
229                 if ((listfile != "") && (groupfile != "")) { format = "shared"; }
230                                 
231                 //input defaults
232                 if (commandName == "collect.single") {
233                         if (singleEstimators.size() == 0) { splitAtDash(single, singleEstimators); }
234                 }
235                 if (commandName == "rarefaction.single") {
236                         if (rareEstimators.size() == 0) { splitAtDash(rarefaction, rareEstimators);  }  
237                 }
238                 if (commandName == "collect.shared") {
239                         if (sharedEstimators.size() == 0) { splitAtDash(shared, sharedEstimators); }    
240                 }
241                 if (commandName == "summary.single") {
242                         if (summaryEstimators.size() == 0) { splitAtDash(summary, summaryEstimators); }
243                 }
244                 if (commandName == "summary.shared") {
245                         if (sharedSummaryEstimators.size() == 0) { splitAtDash(sharedsummary, sharedSummaryEstimators); }
246                 }
247                 if (commandName == "rarefaction.shared") {
248                         if (sharedRareEstimators.size() == 0) { splitAtDash(sharedrarefaction, sharedRareEstimators); }
249                 }
250
251
252                 //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
253                 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single")) {
254                         if (listfile != "") { format = "list"; }
255                         else if (sabundfile != "") { format = "sabund"; }
256                         else if (rabundfile != "") { format = "rabund"; }
257                 }
258                                 
259         }
260         catch(exception& e) {
261                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
262                 exit(1);
263         }
264         catch(...) {
265                 cout << "An unknown error has occurred in the GlobalData class function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
266                 exit(1);
267         }
268
269 }
270 /*******************************************************/
271
272 /******************************************************/
273 // These functions give you the option parameters of the commands
274 string GlobalData::getPhylipFile()              {       return phylipfile;      }
275 string GlobalData::getColumnFile()              {       return columnfile;      }
276 string GlobalData::getListFile()                {       return listfile;        }
277 string GlobalData::getRabundFile()              {       return rabundfile;      }
278 string GlobalData::getSabundFile()              {       return sabundfile;      }
279 string GlobalData::getNameFile()                {       return namefile;        }
280 string GlobalData::getGroupFile()               {       return groupfile;       }
281 string GlobalData::getOrderFile()               {       return orderfile;       }
282 string GlobalData::getTreeFile()                {       return treefile;        }
283 string GlobalData::getFastaFile()               {       return fastafile;       }
284 string GlobalData::getCutOff()                  {       return cutoff;          }
285 string GlobalData::getFormat()                  {       return format;          }
286 string GlobalData::getPrecision()               {       return precision;       }
287 string GlobalData::getMethod()                  {       return method;          }
288 string GlobalData::getFileRoot()                {       return fileroot;        }
289 string GlobalData::getIters()                   {       return iters;           }
290 string GlobalData::getJumble()                  {       return jumble;          }
291 string GlobalData::getFreq()                    {       return freq;            }
292 void GlobalData::setListFile(string file)       {       listfile = file;        inputFileName = file;}
293 void GlobalData::setRabundFile(string file)     {       rabundfile = file;      inputFileName = file;}
294 void GlobalData::setSabundFile(string file)     {       sabundfile = file;      inputFileName = file;}
295 void GlobalData::setPhylipFile(string file)     {       phylipfile = file;    inputFileName = file;}
296 void GlobalData::setColumnFile(string file)     {       columnfile = file;    inputFileName = file;}
297 //void GlobalData::setGroupFile(string file)    {       groupfile = file;       }
298 void GlobalData::setNameFile(string file)       {       namefile = file;        }
299 void GlobalData::setFormat(string Format)       {       format = Format;        }
300
301
302 /*******************************************************/
303
304 /******************************************************/
305
306 GlobalData::GlobalData() {
307         //option definitions should go here...
308         helpRequest = "";
309         clear();
310 }
311 /*******************************************************/
312
313 /******************************************************/
314
315 void GlobalData::clear() {
316         //option definitions should go here...
317         phylipfile              =       "";
318         columnfile              =       "";
319         listfile                =       "";
320         rabundfile              =       "";
321         sabundfile              =       "";
322         namefile                =       "";
323         groupfile               =       ""; 
324         orderfile               =       "";
325         fastafile               =   "";
326         treefile                =       "";
327         cutoff                  =       "10.00";
328         format                  =       "";
329         precision               =       "100";
330         iters                   =       "1000"; 
331         line                    =   "";
332         label                   =       "";
333         jumble                  =       "1";
334         freq                    =       "100";
335         method                  =       "furthest";
336         fileroot                =       "";
337         single                  =       "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson-rarefraction";
338         rarefaction             =       "rarefaction";
339         shared                  =       "sharedSobs-sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN";
340         sharedsummary   =   "sharedSobs-sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN";
341         summary                 =       "summary-chao-ace-jack-bootstrap-shannon-npshannon-simpson";
342         sharedrarefaction = "sharedobserved";
343 }
344 /*******************************************************/
345
346 /******************************************************/
347
348 GlobalData::~GlobalData() {
349         _uniqueInstance = 0;
350         if(gListVector != NULL)         {       delete gListVector;             }
351         if(gSparseMatrix != NULL)       {       delete gSparseMatrix;   }
352         if(gorder != NULL)                      {       delete gorder;          }
353 }
354 /*******************************************************/
355
356 /******************************************************/
357 //This function parses the estimator options and puts them in a vector
358 void GlobalData::splitAtDash(string& estim, vector<string>& container) {
359         try {
360                 string individual;
361                 
362                 while (estim.find_first_of('-') != -1) {
363                         individual = estim.substr(0,estim.find_first_of('-'));
364                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
365                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
366                                 container.push_back(individual);
367                         }
368                 }
369                 //get last one
370                 container.push_back(estim);
371         }
372         catch(exception& e) {
373                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374                 exit(1);
375         }
376         catch(...) {
377                 cout << "An unknown error has occurred in the GlobalData class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378                 exit(1);
379         }
380
381 }
382 /*******************************************************/
383
384 /******************************************************/
385 //This function parses the label options and puts them in a set
386 void GlobalData::splitAtDash(string& estim, set<string>& container) {
387         try {
388                 string individual;
389                 
390                 while (estim.find_first_of('-') != -1) {
391                         individual = estim.substr(0,estim.find_first_of('-'));
392                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
393                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
394                                 container.insert(individual);
395                         }
396                 }
397                 //get last one
398                 container.insert(estim);
399         }
400         catch(exception& e) {
401                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
402                 exit(1);
403         }
404         catch(...) {
405                 cout << "An unknown error has occurred in the GlobalData class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
406                 exit(1);
407         }
408
409 }
410 /*******************************************************/
411
412 /******************************************************/
413 //This function parses the line options and puts them in a set
414 void GlobalData::splitAtDash(string& estim, set<int>& container) {
415         try {
416                 string individual;
417                 int lineNum;
418                 
419                 while (estim.find_first_of('-') != -1) {
420                         individual = estim.substr(0,estim.find_first_of('-'));
421                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
422                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
423                                 convert(individual, lineNum); //convert the string to int
424                                 container.insert(lineNum);
425                         }
426                 }
427                 //get last one
428                 convert(estim, lineNum); //convert the string to int
429                 container.insert(lineNum);
430         }
431         catch(exception& e) {
432                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
433                 exit(1);
434         }
435         catch(...) {
436                 cout << "An unknown error has occurred in the GlobalData class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
437                 exit(1);
438         }
439
440 }
441 /*******************************************************/
442
443 /******************************************************/
444
445 //This function splits up the various option parameters
446 void GlobalData::splitAtComma(string& prefix, string& suffix){
447         try {
448                 prefix = suffix.substr(0,suffix.find_first_of(','));
449                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
450                         suffix = suffix.substr(suffix.find_first_of(',')+2, suffix.length());
451                 }
452         }
453         catch(exception& e) {
454                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
455                 exit(1);
456         }
457         catch(...) {
458                 cout << "An unknown error has occurred in the GlobalData class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
459                 exit(1);
460         }
461
462 }
463 /*******************************************************/
464
465 /******************************************************/
466 //This function separates the key value from the option value i.e. distfile = "96_..."
467 void GlobalData::splitAtEquals(string& key, string& value){             
468         try {
469                 if(value.find_first_of('=') != -1){
470                         key = value.substr(0,value.find_first_of('='));
471                         if ((value.find_first_of('=')+1) <= value.length()) {
472                                 value = value.substr(value.find_first_of('=')+1, value.length());
473                         }
474                 }else{
475                         key = value;
476                         value = 1;
477                 }
478         }
479         catch(exception& e) {
480                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
481                 exit(1);
482         }
483         catch(...) {
484                 cout << "An unknown error has occurred in the GlobalData class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
485                 exit(1);
486         }
487
488 }
489 /*******************************************************/
490
491 /******************************************************/