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