]> git.donarmstrong.com Git - mothur.git/blob - globaldata.cpp
6849b880bd34fc20b74d720b86c299451be71802
[mothur.git] / globaldata.cpp
1 #include "globaldata.hpp"
2 #include "tree.h"
3 #include "sparsematrix.hpp"
4
5 /*******************************************************/
6
7 /******************************************************/
8 GlobalData* GlobalData::getInstance() {
9         if( _uniqueInstance == 0 ) {
10                 _uniqueInstance = new GlobalData();
11         }
12         return _uniqueInstance;
13 }
14 /*******************************************************/
15
16 /******************************************************/
17 //This function parses through the option string of the command to remove its parameters
18 void GlobalData::parseGlobalData(string commandString, string optionText){
19         try {
20                 commandName = commandString; //save command name to be used by other classes
21                 
22                 //set all non filename paramters to default
23                 reset();
24                 
25                 //clears out data from previous read
26                 if ((commandName == "read.dist") || (commandName == "read.otu") || (commandName == "read.tree")) { 
27                         clear();
28                         gGroupmap = NULL;
29                         gTree.clear();
30                         Treenames.clear();
31                         labels.clear(); lines.clear(); groups.clear();
32                         allLines = 1;
33                 }
34                 
35                 //saves help request
36                 if (commandName =="help") {
37                         helpRequest = optionText;
38                 }
39                 
40                 if (commandName == "libshuff") {
41                         iters = "10000";
42                         cutoff = "1.0";
43                 }
44                 
45                 string key, value;              
46                 //reads in parameters and values
47                 if((optionText != "") && (commandName != "help")){
48                         while((optionText.find_first_of(',') != -1)) {  //while there are parameters
49                                 splitAtComma(value, optionText);
50                                 splitAtEquals(key, value);
51                                 
52                                 if (key == "phylip" )   { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip";       }
53                                 if (key == "column" )   { columnfile = value; inputFileName = value; fileroot = value; format = "column";       }
54                                 if (key == "list" )             { listfile = value; inputFileName = value; fileroot = value; format = "list";           }
55                                 if (key == "rabund" )   { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund";       }
56                                 if (key == "sabund" )   { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund";       } 
57                                 if (key == "fasta" )    { fastafile = value; inputFileName = value; fileroot = value; format = "fasta";         }
58                                 if (key == "nexus" )    { nexusfile = value; inputFileName = value; fileroot = value; format = "nexus";         } 
59                                 if (key == "clustal" )  { clustalfile = value; inputFileName = value; fileroot = value; format = "clustal"; }
60                                 if (key == "tree" )             { treefile = value; inputFileName = value; fileroot = value; format = "tree";           }
61                                 if (key == "shared" )   { sharedfile = value; inputFileName = value; fileroot = value; format = "sharedfile";   }
62                                 if (key == "name" )             { namefile = value;             }
63                                 if (key == "order" )    { orderfile = value;    }
64                                 if (key == "group" )    { groupfile = value;    }
65                                 if (key == "cutoff" )           { cutoff = value;               }
66                                 if (key == "precision" )        { precision = value;    }
67                                 if (key == "iters" )            { iters = value;                }
68                                 if (key == "jumble" )           { jumble = value;               }
69                                 if (key == "freq" )                     { freq = value;                 }
70                                 if (key == "method" )           { method = value;               }
71                                 if (key == "fileroot" )         { fileroot = value;             }
72                                 if (key == "abund" )        { abund = value;        }
73                                 if (key == "random" )           { randomtree = value;   }
74                                 if (key == "calc")                      { calc = value;                 }
75                                 if (key == "step")                      { step = value;                 }
76                                 if (key == "form")                      { form = value;                 }
77                                 if (key == "sorted")            { sorted = value;               }
78                                 if (key == "vertical")          { vertical = value;             }
79                                 if (key == "trump")                 { trump = value;            }
80                                 if (key == "filter")            { filter = value;               }
81                                 if (key == "soft")                  { soft = value;                 }
82                                 if (key == "scale")                     { scale = value;                }
83                                 
84
85                                 
86
87                                 
88                                 if (key == "line") {//stores lines to be used in a set
89                                         lines.clear();
90                                         labels.clear();
91                                         line = value;
92                                         label = "";
93                                         splitAtDash(value, lines);
94                                         allLines = 0;
95                                 }
96                                 if (key == "label") {//stores labels to be used in a set
97                                         labels.clear();
98                                         lines.clear();
99                                         label = value;
100                                         line = "";
101                                         splitAtDash(value, labels);
102                                         allLines = 0;
103                                 }
104
105                                 if (key == "groups") {//stores groups to be used in a vector
106                                         Groups.clear();
107                                         groups = value;
108                                         splitAtDash(value, Groups);
109                                 }
110
111                         }
112                         
113                         //saves the last parameter
114                         value = optionText;
115                         splitAtEquals(key, value);
116                         if (key == "phylip" )   { phylipfile = value; inputFileName = value; fileroot = value; format = "phylip";       }
117                         if (key == "column" )   { columnfile = value; inputFileName = value; fileroot = value; format = "column";       }
118                         if (key == "list" )             { listfile = value; inputFileName = value; fileroot = value; format = "list";           }
119                         if (key == "rabund" )   { rabundfile = value; inputFileName = value; fileroot = value; format = "rabund";       }
120                         if (key == "sabund" )   { sabundfile = value; inputFileName = value; fileroot = value; format = "sabund";       }
121                         if (key == "fasta" )    { fastafile = value; inputFileName = value; fileroot = value; format = "fasta";         }
122                         if (key == "nexus" )    { nexusfile = value; inputFileName = value; fileroot = value; format = "nexus";         }
123                         if (key == "clustal" )  { clustalfile = value; inputFileName = value; fileroot = value; format = "clustal"; } 
124                         if (key == "tree" )             { treefile = value; inputFileName = value; fileroot = value; format = "tree";           } 
125                         if (key == "shared" )   { sharedfile = value; inputFileName = value; fileroot = value; format = "sharedfile";   } 
126                         if (key == "name" )             { namefile = value;             }
127                         if (key == "order" )    { orderfile = value;    }
128                         if (key == "group" )    { groupfile = value;    }
129                         if (key == "cutoff" )           { cutoff = value;               }
130                         if (key == "precision" )        { precision = value;    }
131                         if (key == "iters" )            { iters = value;                }
132                         if (key == "jumble" )           { jumble = value;               }
133                         if (key == "freq" )                     { freq = value;                 }
134                         if (key == "method" )           { method = value;               }
135                         if (key == "fileroot" )         { fileroot = value;             }
136                         if (key == "abund" )        { abund = value;        }
137                         if (key == "random" )           { randomtree = value;   }
138                         if (key == "calc")                      { calc = value;                 }
139                         if (key == "step")                      { step = value;                 }
140                         if (key == "form")                      { form = value;                 }
141                         if (key == "sorted")            { sorted = value;               }
142                         if (key == "vertical")          { vertical = value;             }
143                         if (key == "trump")                 { trump = value;            }
144                         if (key == "filter")            { filter = value;               }
145                         if (key == "soft")                  { soft = value;                 }
146                         if (key == "scale")                     { scale = value;                }
147
148                         
149                         
150
151
152                         if (key == "line") {//stores lines to be used in a vector
153                                 lines.clear();
154                                 labels.clear();
155                                 line = value;
156                                 label = "";
157                                 if (line != "all") {  splitAtDash(value, lines);  allLines = 0;  }
158                                 else { allLines = 1;  }
159                         }
160                         
161                         if (key == "label") {//stores lines to be used in a vector
162                                 labels.clear();
163                                 lines.clear();
164                                 label = value;
165                                 line = "";
166                                 if (label != "all") {  splitAtDash(value, labels);  allLines = 0;  }
167                                 else { allLines = 1;  }
168                         }
169                         
170                         if (key == "groups") {//stores groups to be used in a vector
171                                         Groups.clear();
172                                         groups = value;
173                                         splitAtDash(value, Groups);
174                         }
175                 }
176                 
177                 //set format for shared
178                 if ((listfile != "") && (groupfile != "")) { format = "shared"; }
179                 if ((phylipfile != "") && (groupfile != "")) { format = "matrix"; }
180                                 
181                 //input defaults for calculators
182                 if (commandName == "collect.single") {
183
184                         if ((calc == "default") || (calc == "")) { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
185                         Estimators.clear();
186                         splitAtDash(calc, Estimators); 
187                 }
188                 if (commandName == "rarefaction.single") {
189                         if ((calc == "default") || (calc == "")) { calc = "sobs"; }
190                         Estimators.clear();
191                         splitAtDash(calc, Estimators); 
192                 }
193                 if (commandName == "collect.shared") {
194
195                         if ((calc == "default") || (calc == "")) { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
196                         Estimators.clear();
197                         splitAtDash(calc, Estimators); 
198                 }
199                 if (commandName == "summary.single") {
200                         if ((calc == "default") || (calc == "")) { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
201                         Estimators.clear();
202                         splitAtDash(calc, Estimators); 
203                 }
204                 if (commandName == "summary.shared") {
205                         if ((calc == "default") || (calc == "")) { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
206                         Estimators.clear();
207                         splitAtDash(calc, Estimators); 
208                 }
209                 if (commandName == "rarefaction.shared") {
210                         if ((calc == "default") || (calc == "")) { calc = "sharedobserved"; }
211                         Estimators.clear();
212                         splitAtDash(calc, Estimators); 
213                 }
214                 if (commandName == "venn") {
215                         if ((calc == "default") || (calc == "")) { 
216                                 if (format == "list") { calc = "sobs"; }
217                                 else { calc = "sharedsobs"; }
218                         }
219                         Estimators.clear();
220                         splitAtDash(calc, Estimators); 
221                 }
222                 if ((commandName == "tree.shared") || (commandName == "bootstrap.shared")) {
223                         if (calc != "") { 
224                                 Estimators.clear();
225                                 splitAtDash(calc, Estimators);                  
226                         }else { cout << "You have not specified any calculators." << endl; }
227                 }
228
229
230                 //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
231                 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single")) {
232                         if (listfile != "") { format = "list"; }
233                         else if (sabundfile != "") { format = "sabund"; }
234                         else if (rabundfile != "") { format = "rabund"; }
235                 }
236         }
237         catch(exception& e) {
238                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }
241         catch(...) {
242                 cout << "An unknown error has occurred in the GlobalData class function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
243                 exit(1);
244         }
245 }
246 /*******************************************************/
247
248 /******************************************************/
249 // These functions give you the option parameters of the commands
250 string GlobalData::getPhylipFile()              {       return phylipfile;      }
251 string GlobalData::getColumnFile()              {       return columnfile;      }
252 string GlobalData::getListFile()                {       return listfile;        }
253 string GlobalData::getRabundFile()              {       return rabundfile;      }
254 string GlobalData::getSabundFile()              {       return sabundfile;      }
255 string GlobalData::getNameFile()                {       return namefile;        }
256 string GlobalData::getGroupFile()               {       return groupfile;       }
257 string GlobalData::getOrderFile()               {       return orderfile;       }
258 string GlobalData::getTreeFile()                {       return treefile;        }
259 string GlobalData::getSharedFile()              {       return sharedfile;      }
260 string GlobalData::getFastaFile()               {       return fastafile;       }
261 string GlobalData::getNexusFile()               {       return nexusfile;       }
262 string GlobalData::getClustalFile()     {   return clustalfile; }
263 string GlobalData::getCutOff()                  {       return cutoff;          }
264 string GlobalData::getFormat()                  {       return format;          }
265 string GlobalData::getPrecision()               {       return precision;       }
266 string GlobalData::getMethod()                  {       return method;          }
267 string GlobalData::getFileRoot()                {       return fileroot;        }
268 string GlobalData::getIters()                   {       return iters;           }
269 string GlobalData::getJumble()                  {       return jumble;          }
270 string GlobalData::getFreq()                    {       return freq;            }
271 string GlobalData::getAbund()           {   return abund;       }
272 string GlobalData::getRandomTree()              {       return randomtree;      }
273 string GlobalData::getGroups()                  {       return groups;          }
274 string GlobalData::getStep()                    {       return step;            }
275 string GlobalData::getForm()                    {       return form;            }
276 string GlobalData::getSorted()                  {       return sorted;          }
277 string GlobalData::getTrump()                   {   return trump;       }
278 string GlobalData::getSoft()                    {   return soft;                }
279 string GlobalData::getFilter()                  {   return filter;              }
280 string GlobalData::getScale()                   {       return scale;           }
281
282 void GlobalData::setListFile(string file)       {       listfile = file;        inputFileName = file;}
283 void GlobalData::setRabundFile(string file)     {       rabundfile = file;      inputFileName = file;}
284 void GlobalData::setSabundFile(string file)     {       sabundfile = file;      inputFileName = file;}
285 void GlobalData::setPhylipFile(string file)     {       phylipfile = file;    inputFileName = file;}
286 void GlobalData::setColumnFile(string file)     {       columnfile = file;    inputFileName = file;}
287 void GlobalData::setNameFile(string file)               {       namefile = file;                }
288 void GlobalData::setFormat(string Format)               {       format = Format;                }
289 void GlobalData::setRandomTree(string Random)   {       randomtree = Random;    }
290 void GlobalData::setGroups(string g)                    {       groups = g;                             }
291 void GlobalData::setCalc(string Calc)                   {       calc = Calc;                    }
292
293 /*******************************************************/
294
295 /******************************************************/
296 GlobalData::GlobalData() {
297         //option definitions should go here...
298         helpRequest = "";
299         clear();
300         gListVector == NULL;            
301         gSparseMatrix == NULL;  
302 }
303 /*******************************************************/
304
305 /******************************************************/
306 void GlobalData::clear() {
307         //option definitions should go here...
308         phylipfile              =       "";
309         columnfile              =       "";
310         listfile                =       "";
311         rabundfile              =       "";
312         sabundfile              =       "";
313         namefile                =       "";
314         groupfile               =       ""; 
315         orderfile               =       "";
316         fastafile               =   "";
317         nexusfile               =   "";
318         clustalfile             =   "";
319         treefile                =       "";
320         sharedfile              =       "";
321         cutoff                  =       "10.00";
322         format                  =       "";
323         precision               =       "100";
324         iters                   =       "1000"; 
325         line                    =   "";
326         label                   =       "";
327         groups                  =       "";
328         jumble                  =       "1";    //0 means don't jumble, 1 means jumble.
329         randomtree              =       "";  //"" means user will enter some user trees, "outputfile" means they just want the random tree distribution to be outputted to outputfile.
330         freq                    =       "100";
331         method                  =       "furthest";
332         fileroot                =       "";
333         abund           =   "10";
334         step                    =       "0.01";
335         form                    =       "integral";
336         sorted                  =       "T";  //F means don't sort, T means sort.
337         vertical        =   "";         
338         trump           =   "";         
339         filter          =   "";         
340         soft            =   ""; 
341         scale                   =       "log10";            
342
343 }
344
345 //*******************************************************/
346
347 /******************************************************/
348 void GlobalData::reset() {
349         cutoff                  =       "10.00";
350         precision               =       "100";
351         iters                   =       "1000"; 
352         groups                  =       "";
353         jumble                  =       "1";    //0 means don't jumble, 1 means jumble.
354         sorted                  =       "T";  //F means don't sort, T means sort.
355         randomtree              =       "";  //"" means user will enter some user trees, "outputfile" means they just want the random tree distribution to be outputted to outputfile.
356         freq                    =       "100";
357         method                  =       "furthest";
358         calc                    =       "";
359         abund                   =   "10";
360         step                    =       "0.01";
361         form                    =       "integral";
362 }
363 /*******************************************************/
364
365 /******************************************************/
366 GlobalData::~GlobalData() {
367         _uniqueInstance = 0;
368         if(gListVector != NULL)         {       delete gListVector;             }
369         if(gSparseMatrix != NULL)       {       delete gSparseMatrix;   }
370         if(gorder != NULL)                      {       delete gorder;          }
371 }
372 /*******************************************************/
373
374 /*******************************************************/
375 void GlobalData::parseTreeFile() {
376         //only takes names from the first tree and assumes that all trees use the same names.
377         try {
378                 string filename = treefile;
379                 ifstream filehandle;
380                 openInputFile(filename, filehandle);
381                 int c, comment;
382                 comment = 0;
383                 
384                 //if you are not a nexus file 
385                 if ((c = filehandle.peek()) != '#') {  
386                         while((c = filehandle.peek()) != ';') { 
387                                 while ((c = filehandle.peek()) != ';') {
388                                         // get past comments
389                                         if(c == '[') {
390                                                 comment = 1;
391                                         }
392                                         if(c == ']'){
393                                                 comment = 0;
394                                         }
395                                         if((c == '(') && (comment != 1)){ break; }
396                                         filehandle.get();
397                                 }
398
399                                 readTreeString(filehandle); 
400                         }
401                 //if you are a nexus file
402                 }else if ((c = filehandle.peek()) == '#') {
403                         string holder = "";
404                                         
405                         // get past comments
406                         while(holder != "translate" && holder != "Translate"){  
407                                 if(holder == "[" || holder == "[!"){
408                                         comment = 1;
409                                 }
410                                 if(holder == "]"){
411                                         comment = 0;
412                                 }
413                                 filehandle >> holder; 
414         
415                                 //if there is no translate then you must read tree string otherwise use translate to get names
416                                 if(holder == "tree" && comment != 1){   
417                                         //pass over the "tree rep.6878900 = "
418                                         while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF) ) {;}
419
420                                         if (c == EOF ) { break; }
421                                         filehandle.putback(c);  //put back first ( of tree.
422                                         readTreeString(filehandle);     
423                                         break;
424                                 }
425                         }
426                         
427                         //use nexus translation rather than parsing tree to save time
428                         if ((holder == "translate") || (holder == "Translate")) {
429
430                                 string number, name, h;
431                                 h = ""; // so it enters the loop the first time
432                                 while((h != ";") && (number != ";")) { 
433                                         filehandle >> number;
434                                         filehandle >> name;
435         
436                                         //c = , until done with translation then c = ;
437                                         h = name.substr(name.length()-1, name.length()); 
438                                         name.erase(name.end()-1);  //erase the comma
439                                         Treenames.push_back(number);
440                                 }
441                                 if (number == ";") { Treenames.pop_back(); }  //in case ';' from translation is on next line instead of next to last name
442                         }
443                 }
444                 
445         }
446         catch(exception& e) {
447                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
448                 exit(1);
449         }
450         catch(...) {
451                 cout << "An unknown error has occurred in the GlobalData class function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
452                 exit(1);
453         }               
454 }
455 /*******************************************************/
456
457 /*******************************************************/
458 void GlobalData::readTreeString(ifstream& filehandle)   {
459         try {
460                 int c;
461                 string name; //k
462                 
463                 while((c = filehandle.peek()) != ';') { 
464                                 //if you are a name
465                         if ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
466                                 name = "";
467                                 c = filehandle.get();
468         //              k = c;
469 //cout << k << endl;
470                                 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {                       
471                                         name += c;
472                                         c = filehandle.get();
473                 //      k = c;
474 //cout << " in name while " << k << endl;
475                                 }
476                                 
477 //cout << "name = " << name << endl;
478                                 Treenames.push_back(name);
479                                 filehandle.putback(c);
480 //k = c;
481 //cout << " after putback" <<  k << endl;
482                         } 
483                         
484                         if (c  == ':') { //read until you reach the end of the branch length
485                                 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
486                                         c = filehandle.get();
487                                 //      k = c;
488         //cout << " in branch while " << k << endl;
489                                 }
490                                 filehandle.putback(c);
491                         }
492                         c = filehandle.get();
493                         if (c == ';') { break; }
494                 //      k = c;
495 //cout << k << endl;
496
497                 }
498         }
499         catch(exception& e) {
500                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
501                 exit(1);
502         }
503         catch(...) {
504                 cout << "An unknown error has occurred in the GlobalData class function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
505                 exit(1);
506         }               
507 }       
508
509 /*******************************************************/
510
511 /*******************************************************/
512
513