]> git.donarmstrong.com Git - mothur.git/blob - globaldata.cpp
e58d19181d156e00dff0190388552858a32dbbde
[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 == "phylip" )   { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip";       }
101                                 if (key == "column" )   { columnfile = value; inputFileName = value; fileroot = value; format = "column";       }
102                                 if (key == "list" )             { listfile = value; inputFileName = value; fileroot = value; format = "list";           }
103                                 if (key == "rabund" )   { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund";       }
104                                 if (key == "sabund" )   { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund";       } 
105                                 if (key == "fasta" )    { fastafile = value; inputFileName = value; fileroot = value; format = "fasta";         } 
106                                 if (key == "tree" )             { treefile = value; inputFileName = value; fileroot = value; format = "tree";           }
107                                 if (key == "name" )             { namefile = value;             }
108                                 if (key == "order" )    { orderfile = value;    }
109                                 if (key == "group" )    { 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                                 if (key == "randomtree" )       { randomtree = value;   }
118                                 if (key == "groups" )           { groups = value;       }
119                                 
120                                 if (key == "single") {//stores estimators in a vector
121                                         singleEstimators.clear(); //clears out old values
122                                         if (value == "default") { value = "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson-rarefraction"; }
123                                         splitAtDash(value, singleEstimators);
124                                 }
125                                 if (key == "rarefaction") {//stores estimators in a vector
126                                         rareEstimators.clear(); //clears out old values
127                                         if (value == "default") { value = "rarefraction"; }
128                                         splitAtDash(value, rareEstimators);
129                                 }
130                                 if (key == "shared") {//stores estimators in a vector
131                                         sharedEstimators.clear(); //clears out old values
132                                         if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
133                                         splitAtDash(value, sharedEstimators);
134                                 }
135                                 if (key == "summary") { //stores summaries to be used in a vector
136                                         summaryEstimators.clear();
137                                         if (value == "default") { value = "summary-chao-ace-jack-bootstrap-shannon-npshannon-simpson"; }
138                                         splitAtDash(value, summaryEstimators);
139                                 }
140                                 if (key == "sharedsummary") { //stores sharedSummaries to be used in a vector
141                                         sharedSummaryEstimators.clear();
142                                         if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
143                                         splitAtDash(value, sharedSummaryEstimators);
144                                 }
145                                 if (key == "sharedrarefaction") { //stores sharedrarefaction to be used in a vector
146                                         sharedRareEstimators.clear();
147                                         if (value == "default") { value = "sharedobserved"; }
148                                         splitAtDash(value, sharedRareEstimators);
149                                 }
150                                 if (key == "line") {//stores lines to be used in a set
151                                         lines.clear();
152                                         line = value;
153                                         label = "";
154                                         splitAtDash(value, lines);
155                                         allLines = 0;
156                                 }
157                                 if (key == "label") {//stores labels to be used in a set
158                                         labels.clear();
159                                         label = value;
160                                         line = "";
161                                         splitAtDash(value, labels);
162                                         allLines = 0;
163                                 }
164                                 if (key == "groups") {//stores groups to be used in a vector
165                                         Groups.clear();
166                                         groups = value;
167                                         splitAtDash(value, Groups);
168                                 }
169
170                         }
171                         
172                         //saves the last parameter
173                         value = optionText;
174                         splitAtEquals(key, value);
175                         if (key == "phylip" )   { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip";       }
176                         if (key == "column" )   { columnfile = value; inputFileName = value; fileroot = value; format = "column";       }
177                         if (key == "list" )             { listfile = value; inputFileName = value; fileroot = value; format = "list";           }
178                         if (key == "rabund" )   { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund";       }
179                         if (key == "sabund" )   { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund";       }
180                         if (key == "fasta" )    { fastafile = value; inputFileName = value; fileroot = value; format = "fasta";         }
181                         if (key == "tree" )             { treefile = value; inputFileName = value; fileroot = value; format = "tree";           }  
182                         if (key == "name" )             { namefile = value;             }
183                         if (key == "order" )    { orderfile = value;    }
184                         if (key == "group" )    { groupfile = value;    }
185                         if (key == "cutoff" )           { cutoff = value;               }
186                         if (key == "precision" )        { precision = value;    }
187                         if (key == "iters" )            { iters = value;                }
188                         if (key == "jumble" )           { jumble = value;               }
189                         if (key == "freq" )                     { freq = value;                 }
190                         if (key == "method" )           { method = value;               }
191                         if (key == "fileroot" )         { fileroot = value;             }
192                         if (key == "randomtree" )       { randomtree = value;   }
193                         if (key == "groups" )           { groups = value;       }
194
195                         
196                         if (key == "single") {//stores estimators in a vector
197                                 singleEstimators.clear(); //clears out old values
198                                 if (value == "default") { value = "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson-rarefraction"; }
199                                 splitAtDash(value, singleEstimators);
200                         }
201                         if (key == "rarefaction") {//stores estimators in a vector
202                                 rareEstimators.clear(); //clears out old values
203                                 if (value == "default") { value = "rarefraction"; }
204                                 splitAtDash(value, rareEstimators);
205                         }
206                         if (key == "shared") {//stores estimators in a vector
207                                 sharedEstimators.clear(); //clears out old values
208                                 if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
209                                 splitAtDash(value, sharedEstimators);
210                         }
211                         if (key == "summary") { //stores summaries to be used in a vector
212                                 summaryEstimators.clear();
213                                 if (value == "default") { value = "summary-chao-ace-jack-bootstrap-shannon-npshannon-simpson"; }
214                                 splitAtDash(value, summaryEstimators);
215                         }
216                         if (key == "sharedsummary") { //stores sharedSummaries to be used in a vector
217                                 sharedSummaryEstimators.clear();
218                                 if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
219                                 splitAtDash(value, sharedSummaryEstimators);
220                         }
221                         if (key == "sharedrarefaction") { //stores sharedrarefaction to be used in a vector
222                                 sharedRareEstimators.clear();
223                                 if (value == "default") { value = "sharedobserved"; }
224                                 splitAtDash(value, sharedRareEstimators);
225                         }
226                         
227                         if (key == "line") {//stores lines to be used in a vector
228                                 lines.clear();
229                                 line = value;
230                                 label = "";
231                                 splitAtDash(value, lines);
232                                 allLines = 0;
233                         }
234                         if (key == "label") {//stores lines to be used in a vector
235                                 labels.clear();
236                                 label = value;
237                                 line = "";
238                                 splitAtDash(value, labels);
239                                 allLines = 0;
240                         }
241                         if (key == "groups") {//stores groups to be used in a vector
242                                         Groups.clear();
243                                         groups = value;
244                                         splitAtDash(value, Groups);
245                         }
246
247                 }
248                 
249                 //set format for shared
250                 if ((listfile != "") && (groupfile != "")) { format = "shared"; }
251                                 
252                 //input defaults
253                 if (commandName == "collect.single") {
254                         if (singleEstimators.size() == 0) { splitAtDash(single, singleEstimators); }
255                 }
256                 if (commandName == "rarefaction.single") {
257                         if (rareEstimators.size() == 0) { splitAtDash(rarefaction, rareEstimators);  }  
258                 }
259                 if (commandName == "collect.shared") {
260                         if (sharedEstimators.size() == 0) { splitAtDash(shared, sharedEstimators); }    
261                 }
262                 if (commandName == "summary.single") {
263                         if (summaryEstimators.size() == 0) { splitAtDash(summary, summaryEstimators); }
264                 }
265                 if (commandName == "summary.shared") {
266                         if (sharedSummaryEstimators.size() == 0) { splitAtDash(sharedsummary, sharedSummaryEstimators); }
267                 }
268                 if (commandName == "rarefaction.shared") {
269                         if (sharedRareEstimators.size() == 0) { splitAtDash(sharedrarefaction, sharedRareEstimators); }
270                 }
271
272
273                 //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
274                 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single")) {
275                         if (listfile != "") { format = "list"; }
276                         else if (sabundfile != "") { format = "sabund"; }
277                         else if (rabundfile != "") { format = "rabund"; }
278                 }
279                                 
280         }
281         catch(exception& e) {
282                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
283                 exit(1);
284         }
285         catch(...) {
286                 cout << "An unknown error has occurred in the GlobalData class function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287                 exit(1);
288         }
289
290 }
291 /*******************************************************/
292
293 /******************************************************/
294 // These functions give you the option parameters of the commands
295 string GlobalData::getPhylipFile()              {       return phylipfile;      }
296 string GlobalData::getColumnFile()              {       return columnfile;      }
297 string GlobalData::getListFile()                {       return listfile;        }
298 string GlobalData::getRabundFile()              {       return rabundfile;      }
299 string GlobalData::getSabundFile()              {       return sabundfile;      }
300 string GlobalData::getNameFile()                {       return namefile;        }
301 string GlobalData::getGroupFile()               {       return groupfile;       }
302 string GlobalData::getOrderFile()               {       return orderfile;       }
303 string GlobalData::getTreeFile()                {       return treefile;        }
304 string GlobalData::getFastaFile()               {       return fastafile;       }
305 string GlobalData::getCutOff()                  {       return cutoff;          }
306 string GlobalData::getFormat()                  {       return format;          }
307 string GlobalData::getPrecision()               {       return precision;       }
308 string GlobalData::getMethod()                  {       return method;          }
309 string GlobalData::getFileRoot()                {       return fileroot;        }
310 string GlobalData::getIters()                   {       return iters;           }
311 string GlobalData::getJumble()                  {       return jumble;          }
312 string GlobalData::getFreq()                    {       return freq;            }
313 string GlobalData::getRandomTree()              {       return randomtree;      }
314 void GlobalData::setListFile(string file)       {       listfile = file;        inputFileName = file;}
315 void GlobalData::setRabundFile(string file)     {       rabundfile = file;      inputFileName = file;}
316 void GlobalData::setSabundFile(string file)     {       sabundfile = file;      inputFileName = file;}
317 void GlobalData::setPhylipFile(string file)     {       phylipfile = file;    inputFileName = file;}
318 void GlobalData::setColumnFile(string file)     {       columnfile = file;    inputFileName = file;}
319 //void GlobalData::setGroupFile(string file)    {       groupfile = file;               }
320 void GlobalData::setNameFile(string file)               {       namefile = file;                }
321 void GlobalData::setFormat(string Format)               {       format = Format;                }
322 void GlobalData::setRandomTree(string Random)   {       randomtree = Random;    }
323
324
325 /*******************************************************/
326
327 /******************************************************/
328
329 GlobalData::GlobalData() {
330         //option definitions should go here...
331         helpRequest = "";
332         clear();
333 }
334 /*******************************************************/
335
336 /******************************************************/
337
338 void GlobalData::clear() {
339         //option definitions should go here...
340         phylipfile              =       "";
341         columnfile              =       "";
342         listfile                =       "";
343         rabundfile              =       "";
344         sabundfile              =       "";
345         namefile                =       "";
346         groupfile               =       ""; 
347         orderfile               =       "";
348         fastafile               =   "";
349         treefile                =       "";
350         cutoff                  =       "10.00";
351         format                  =       "";
352         precision               =       "100";
353         iters                   =       "1000"; 
354         line                    =   "";
355         label                   =       "";
356         groups                  =       "";
357         jumble                  =       "1";    //0 means don't jumble, 1 means jumble.
358         randomtree              =       "0";  //0 means user will enter some user trees, 1 means they just want the random tree distribution.
359         freq                    =       "100";
360         method                  =       "furthest";
361         fileroot                =       "";
362         single                  =       "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson-rarefraction";
363         rarefaction             =       "sobs";
364         shared                  =       "sharedSobs-sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN";
365         sharedsummary   =   "sharedSobs-sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN";
366         summary                 =       "summary-chao-ace-jack-bootstrap-shannon-npshannon-simpson";
367         sharedrarefaction = "sharedobserved";
368 }
369 /*******************************************************/
370
371 /******************************************************/
372
373 GlobalData::~GlobalData() {
374         _uniqueInstance = 0;
375         if(gListVector != NULL)         {       delete gListVector;             }
376         if(gSparseMatrix != NULL)       {       delete gSparseMatrix;   }
377         if(gorder != NULL)                      {       delete gorder;          }
378 }
379 /*******************************************************/
380
381 /******************************************************/
382 //This function parses the estimator options and puts them in a vector
383 void GlobalData::splitAtDash(string& estim, vector<string>& container) {
384         try {
385                 string individual;
386                 
387                 while (estim.find_first_of('-') != -1) {
388                         individual = estim.substr(0,estim.find_first_of('-'));
389                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
390                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
391                                 container.push_back(individual);
392                         }
393                 }
394                 //get last one
395                 container.push_back(estim);
396         }
397         catch(exception& e) {
398                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
399                 exit(1);
400         }
401         catch(...) {
402                 cout << "An unknown error has occurred in the GlobalData class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
403                 exit(1);
404         }
405
406 }
407 /*******************************************************/
408
409 /******************************************************/
410 //This function parses the label options and puts them in a set
411 void GlobalData::splitAtDash(string& estim, set<string>& container) {
412         try {
413                 string individual;
414                 
415                 while (estim.find_first_of('-') != -1) {
416                         individual = estim.substr(0,estim.find_first_of('-'));
417                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
418                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
419                                 container.insert(individual);
420                         }
421                 }
422                 //get last one
423                 container.insert(estim);
424         }
425         catch(exception& e) {
426                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
427                 exit(1);
428         }
429         catch(...) {
430                 cout << "An unknown error has occurred in the GlobalData class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
431                 exit(1);
432         }
433
434 }
435 /*******************************************************/
436
437 /******************************************************/
438 //This function parses the line options and puts them in a set
439 void GlobalData::splitAtDash(string& estim, set<int>& container) {
440         try {
441                 string individual;
442                 int lineNum;
443                 
444                 while (estim.find_first_of('-') != -1) {
445                         individual = estim.substr(0,estim.find_first_of('-'));
446                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
447                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
448                                 convert(individual, lineNum); //convert the string to int
449                                 container.insert(lineNum);
450                         }
451                 }
452                 //get last one
453                 convert(estim, lineNum); //convert the string to int
454                 container.insert(lineNum);
455         }
456         catch(exception& e) {
457                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
458                 exit(1);
459         }
460         catch(...) {
461                 cout << "An unknown error has occurred in the GlobalData class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
462                 exit(1);
463         }
464
465 }
466 /*******************************************************/
467
468 /******************************************************/
469
470 //This function splits up the various option parameters
471 void GlobalData::splitAtComma(string& prefix, string& suffix){
472         try {
473                 prefix = suffix.substr(0,suffix.find_first_of(','));
474                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
475                         suffix = suffix.substr(suffix.find_first_of(',')+2, suffix.length());
476                 }
477         }
478         catch(exception& e) {
479                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
480                 exit(1);
481         }
482         catch(...) {
483                 cout << "An unknown error has occurred in the GlobalData class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
484                 exit(1);
485         }
486
487 }
488 /*******************************************************/
489
490 /******************************************************/
491 //This function separates the key value from the option value i.e. dist=96_...
492 void GlobalData::splitAtEquals(string& key, string& value){             
493         try {
494                 if(value.find_first_of('=') != -1){
495                         key = value.substr(0,value.find_first_of('='));
496                         if ((value.find_first_of('=')+1) <= value.length()) {
497                                 value = value.substr(value.find_first_of('=')+1, value.length());
498                         }
499                 }else{
500                         key = value;
501                         value = 1;
502                 }
503         }
504         catch(exception& e) {
505                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
506                 exit(1);
507         }
508         catch(...) {
509                 cout << "An unknown error has occurred in the GlobalData class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
510                 exit(1);
511         }
512
513 }
514 /*******************************************************/
515
516 /******************************************************/