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