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