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