]> git.donarmstrong.com Git - mothur.git/blob - globaldata.cpp
changed read.shared to read.list and incorporated the shared and parselist commands...
[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 == "phylipfile" )       { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip";}
98                                 if (key == "columnfile" )       { columnfile = value; inputFileName = value; fileroot = value; format = "column";}
99                                 if (key == "listfile" )         { listfile = value; inputFileName = value; fileroot = value; format = "list"; }
100                                 if (key == "rabundfile" )       { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund"; }
101                                 if (key == "sabundfile" )       { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund"; } 
102                                 if (key == "namefile" )         { namefile = value; }
103                                 if (key == "orderfile" )        { orderfile = value; }
104                                 if (key == "groupfile" )        { groupfile = value; }
105                                 if (key == "cutoff" )           { cutoff = value; }
106                                 if (key == "precision" )        { precision = value; }
107                                 if (key == "iters" )            { iters = value; }
108                                 if (key == "jumble" )           { jumble = value; }
109                                 if (key == "freq" )                     { freq = value; }
110                                 if (key == "method" )           { method = value; }
111                                 if (key == "fileroot" )         { fileroot = value; }
112                                 
113                                 if (key == "single") {//stores estimators in a vector
114                                         singleEstimators.clear(); //clears out old values
115                                         if (value == "default") { value = "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson-rarefraction"; }
116                                         splitAtDash(value, singleEstimators);
117                                 }
118                                 if (key == "rarefaction") {//stores estimators in a vector
119                                         rareEstimators.clear(); //clears out old values
120                                         if (value == "default") { value = "rarefraction"; }
121                                         splitAtDash(value, rareEstimators);
122                                 }
123                                 if (key == "shared") {//stores estimators in a vector
124                                         sharedEstimators.clear(); //clears out old values
125                                         if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
126                                         splitAtDash(value, sharedEstimators);
127                                 }
128                                 if (key == "summary") { //stores summaries to be used in a vector
129                                         summaryEstimators.clear();
130                                         if (value == "default") { value = "summary-chao-ace-jack-bootstrap-shannon-npshannon-simpson"; }
131                                         splitAtDash(value, summaryEstimators);
132                                 }
133                                 if (key == "sharedsummary") { //stores sharedSummaries to be used in a vector
134                                         sharedSummaryEstimators.clear();
135                                         if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
136                                         splitAtDash(value, sharedSummaryEstimators);
137                                 }
138                                 if (key == "sharedrarefaction") { //stores sharedrarefaction to be used in a vector
139                                         sharedRareEstimators.clear();
140                                         if (value == "default") { value = "sharedobserved"; }
141                                         splitAtDash(value, sharedRareEstimators);
142                                 }
143                                 if (key == "line") {//stores lines to be used in a vector
144                                         lines.clear();
145                                         line = value;
146                                         splitAtDash(value, lines);
147                                         allLines = 0;
148                                 }
149                                 if (key == "label") {//stores lines to be used in a vector
150                                         labels.clear();
151                                         label = value;
152                                         splitAtDash(value, labels);
153                                         allLines = 0;
154                                 }
155                         }
156                         
157                         //saves the last parameter
158                         value = optionText;
159                         splitAtEquals(key, value);
160                         if (key == "phylipfile" )       { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip"; }
161                         if (key == "columnfile" )       { columnfile = value; inputFileName = value; fileroot = value; format = "column"; }
162                         if (key == "listfile" )         { listfile = value; inputFileName = value; fileroot = value; format = "list"; }
163                         if (key == "rabundfile" )       { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund"; }
164                         if (key == "sabundfile" )       { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund"; }
165                         if (key == "namefile" )         { namefile = value; }
166                         if (key == "orderfile" )        { orderfile = value; }
167                         if (key == "groupfile" )        { groupfile = value; }
168                         if (key == "cutoff" )           { cutoff = value; }
169                         if (key == "precision" )        { precision = value; }
170                         if (key == "iters" )            { iters = value; }
171                         if (key == "jumble" )           { jumble = value; }
172                         if (key == "freq" )                     { freq = value; }
173                         if (key == "method" )           { method = value; }
174                         if (key == "fileroot" )         { fileroot = value; }
175
176                         if (key == "single") {//stores estimators in a vector
177                                 singleEstimators.clear(); //clears out old values
178                                 if (value == "default") { value = "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson-rarefraction"; }
179                                 splitAtDash(value, singleEstimators);
180                         }
181                         if (key == "rarefaction") {//stores estimators in a vector
182                                 rareEstimators.clear(); //clears out old values
183                                 if (value == "default") { value = "rarefraction"; }
184                                 splitAtDash(value, rareEstimators);
185                         }
186                         if (key == "shared") {//stores estimators in a vector
187                                 sharedEstimators.clear(); //clears out old values
188                                 if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
189                                 splitAtDash(value, sharedEstimators);
190                         }
191                         if (key == "summary") { //stores summaries to be used in a vector
192                                 summaryEstimators.clear();
193                                 if (value == "default") { value = "summary-chao-ace-jack-bootstrap-shannon-npshannon-simpson"; }
194                                 splitAtDash(value, summaryEstimators);
195                         }
196                         if (key == "sharedsummary") { //stores sharedSummaries to be used in a vector
197                                 sharedSummaryEstimators.clear();
198                                 if (value == "default") { value = "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN"; }
199                                 splitAtDash(value, sharedSummaryEstimators);
200                         }
201                         if (key == "sharedrarefaction") { //stores sharedrarefaction to be used in a vector
202                                 sharedRareEstimators.clear();
203                                 if (value == "default") { value = "sharedobserved"; }
204                                 splitAtDash(value, sharedRareEstimators);
205                         }
206                         
207                         if (key == "line") {//stores lines to be used in a vector
208                                 lines.clear();
209                                 line = value;
210                                 splitAtDash(value, lines);
211                                 allLines = 0;
212                         }
213                         if (key == "label") {//stores lines to be used in a vector
214                                 labels.clear();
215                                 label = value;
216                                 splitAtDash(value, labels);
217                                 allLines = 0;
218                         }
219                 }
220                 
221                                 
222                 //input defaults
223                 if (commandString == "collect.single") {
224                         if (singleEstimators.size() == 0) { splitAtDash(single, singleEstimators); }    
225                 }
226                 if (commandString == "rarefaction.single") {
227                         if (rareEstimators.size() == 0) { splitAtDash(rarefaction, rareEstimators);  }  
228                 }
229                 if (commandString == "collect.shared") {
230                         if (sharedEstimators.size() == 0) { splitAtDash(shared, sharedEstimators); }    
231                 }
232                 if (commandString == "summary.single") {
233                         if (summaryEstimators.size() == 0) { splitAtDash(summary, summaryEstimators); }
234                 }
235                 if (commandString == "summary.shared") {
236                         if (sharedSummaryEstimators.size() == 0) { splitAtDash(sharedsummary, sharedSummaryEstimators); }
237                 }
238                 if (commandString == "rarefaction.shared") {
239                         if (sharedRareEstimators.size() == 0) { splitAtDash(sharedrarefaction, sharedRareEstimators); }
240                 }
241
242                                 
243         }
244         catch(exception& e) {
245                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
246                 exit(1);
247         }
248         catch(...) {
249                 cout << "An unknown error has occurred in the GlobalData class function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
250                 exit(1);
251         }
252
253 }
254 /*******************************************************/
255
256 /******************************************************/
257 void GlobalData::setReadFormat(string command){
258         try {
259                 if (command == "read.dist") { 
260                         clear();
261                 }else if (command == "read.otu") { 
262                         clear();
263                 }else if (command == "read.shared") { 
264                         clear();
265                         format = "shared";
266                 }else if (command == "parselist") { format = "list"; }
267         }
268         catch(exception& e) {
269                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function setReadFormat. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270                 exit(1);
271         }
272         catch(...) {
273                 cout << "An unknown error has occurred in the GlobalData class function setReadFormat. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274                 exit(1);
275         }
276 }
277
278 /*******************************************************/
279
280 /******************************************************/
281 // These functions give you the option parameters of the commands
282 string GlobalData::getPhylipFile()              {       return phylipfile;      }
283 string GlobalData::getColumnFile()              {       return columnfile;      }
284 string GlobalData::getListFile()                {       return listfile;        }
285 string GlobalData::getRabundFile()              {       return rabundfile;      }
286 string GlobalData::getSabundFile()              {       return sabundfile;      }
287 string GlobalData::getNameFile()                {       return namefile;        }
288 string GlobalData::getGroupFile()               {       return groupfile;       }
289 string GlobalData::getOrderFile()               {       return orderfile;       }
290 string GlobalData::getCutOff()                  {       return cutoff;          }
291 string GlobalData::getFormat()                  {       return format;          }
292 string GlobalData::getPrecision()               {       return precision;       }
293 string GlobalData::getMethod()                  {       return method;          }
294 string GlobalData::getFileRoot()                {       return fileroot;        }
295 string GlobalData::getIters()                   {       return iters;           }
296 string GlobalData::getJumble()                  {       return jumble;          }
297 string GlobalData::getFreq()                    {       return freq;            }
298 void GlobalData::setListFile(string file)       {       listfile = file;        inputFileName = file;}
299 void GlobalData::setRabundFile(string file)     {       rabundfile = file;      inputFileName = file;}
300 void GlobalData::setSabundFile(string file)     {       sabundfile = file;      inputFileName = file;}
301 void GlobalData::setPhylipFile(string file)     {       phylipfile = file;    inputFileName = file;}
302 void GlobalData::setColumnFile(string file)     {       columnfile = file;    inputFileName = file;}
303 //void GlobalData::setGroupFile(string file)    {       groupfile = file;       }
304 void GlobalData::setNameFile(string file)       {       namefile = file;        }
305 void GlobalData::setFormat(string Format)       {       format = Format;        }
306
307
308 /*******************************************************/
309
310 /******************************************************/
311
312 GlobalData::GlobalData() {
313         //option definitions should go here...
314         helpRequest = "";
315         clear();
316 }
317 /*******************************************************/
318
319 /******************************************************/
320
321 void GlobalData::clear() {
322         //option definitions should go here...
323         phylipfile              =       "";
324         columnfile              =       "";
325         listfile                =       "";
326         rabundfile              =       "";
327         sabundfile              =       "";
328         namefile                =       "";
329         groupfile               =       ""; 
330         orderfile               =       "";
331         cutoff                  =       "10.00";
332         format                  =       "";
333         precision               =       "100";
334         iters                   =       "1000"; 
335         line                    =   "";
336         label                   =       "";
337         jumble                  =       "0";
338         freq                    =       "100";
339         method                  =       "furthest";
340         fileroot                =       "";
341         single                  =       "sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson-rarefraction";
342         rarefaction             =       "rarefaction";
343         shared                  =       "sharedChao-sharedAce-sharedJabund-sharedSorensonAbund-sharedJclass-sharedSorClass-sharedJest-sharedSorEst-SharedThetaYC-SharedThetaN";
344         sharedsummary   =   "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 /******************************************************/