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