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