]> git.donarmstrong.com Git - mothur.git/blob - globaldata.cpp
ae1b0e2ebbb463fefc32a2f8ad3fd1816b01109d
[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 == "distance") {        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 == "distance") {
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::setRabundFile(string file)     {       rabundfile = file;      inputFileName = file;}
295 void GlobalData::setSabundFile(string file)     {       sabundfile = file;      inputFileName = file;}
296 void GlobalData::setPhylipFile(string file)     {       phylipfile = file;    inputFileName = file;}
297 void GlobalData::setColumnFile(string file)     {       columnfile = file;    inputFileName = file;}
298 void GlobalData::setNameFile(string file)               {       namefile = file;                }
299 void GlobalData::setFormat(string Format)               {       format = Format;                }
300 void GlobalData::setRandomTree(string Random)   {       randomtree = Random;    }
301 void GlobalData::setGroups(string g)                    {       groups = g;                             }
302 void GlobalData::setCalc(string Calc)                   {       calc = Calc;                    }
303 void GlobalData::setEnds(string e)                              {   ends = e;                           }
304 void GlobalData::setProcessors(string p)                {       processors = p;                 }
305
306
307 /*******************************************************/
308
309 /******************************************************/
310 GlobalData::GlobalData() {
311         //option definitions should go here...
312         helpRequest = "";
313         clear();
314         gListVector == NULL;            
315         gSparseMatrix == NULL;  
316 }
317 /*******************************************************/
318
319 /******************************************************/
320 void GlobalData::clear() {
321         //option definitions should go here...
322         phylipfile              =       "";
323         columnfile              =       "";
324         listfile                =       "";
325         rabundfile              =       "";
326         sabundfile              =       "";
327         namefile                =       "";
328         groupfile               =       ""; 
329         orderfile               =       "";
330         fastafile               =   "";
331         nexusfile               =   "";
332         clustalfile             =   "";
333         treefile                =       "";
334         sharedfile              =       "";
335         cutoff                  =       "10.00";
336         format                  =       "";
337         precision               =       "100";
338         iters                   =       "1000"; 
339         line                    =   "";
340         label                   =       "";
341         groups                  =       "";
342         jumble                  =       "1";    //0 means don't jumble, 1 means jumble.
343         randomtree              =       "";  //"" means user will enter some user trees, "outputfile" means they just want the random tree distribution to be outputted to outputfile.
344         freq                    =       "100";
345         method                  =       "furthest";
346         fileroot                =       "";
347         abund           =   "10";
348         step                    =       "0.01";
349         form                    =       "integral";
350         sorted                  =       "T";  //F means don't sort, T means sort.
351         vertical        =   "";         
352         trump           =   "";         
353         filter          =   "";         
354         soft            =   ""; 
355         scale                   =       "log10";
356         ends                    =   "T";  //yes
357         processors              =       "1";
358
359 }
360
361 //*******************************************************/
362
363 /******************************************************/
364 void GlobalData::reset() {
365         cutoff                  =       "10.00";
366         precision               =       "100";
367         iters                   =       "1000"; 
368         groups                  =       "";
369         jumble                  =       "1";    //0 means don't jumble, 1 means jumble.
370         sorted                  =       "T";  //F means don't sort, T means sort.
371         randomtree              =       "";  //"" means user will enter some user trees, "outputfile" means they just want the random tree distribution to be outputted to outputfile.
372         freq                    =       "100";
373         method                  =       "furthest";
374         calc                    =       "";
375         abund                   =   "10";
376         step                    =       "0.01";
377         form                    =       "integral";
378         ends                    =   "T";
379         processors              =       "1";
380 }
381 /*******************************************************/
382
383 /******************************************************/
384 GlobalData::~GlobalData() {
385         _uniqueInstance = 0;
386         if(gListVector != NULL)         {       delete gListVector;             }
387         if(gSparseMatrix != NULL)       {       delete gSparseMatrix;   }
388         if(gorder != NULL)                      {       delete gorder;          }
389 }
390 /*******************************************************/
391
392 /*******************************************************/
393 void GlobalData::parseTreeFile() {
394         //only takes names from the first tree and assumes that all trees use the same names.
395         try {
396                 string filename = treefile;
397                 ifstream filehandle;
398                 openInputFile(filename, filehandle);
399                 int c, comment;
400                 comment = 0;
401                 
402                 //if you are not a nexus file 
403                 if ((c = filehandle.peek()) != '#') {  
404                         while((c = filehandle.peek()) != ';') { 
405                                 while ((c = filehandle.peek()) != ';') {
406                                         // get past comments
407                                         if(c == '[') {
408                                                 comment = 1;
409                                         }
410                                         if(c == ']'){
411                                                 comment = 0;
412                                         }
413                                         if((c == '(') && (comment != 1)){ break; }
414                                         filehandle.get();
415                                 }
416
417                                 readTreeString(filehandle); 
418                         }
419                 //if you are a nexus file
420                 }else if ((c = filehandle.peek()) == '#') {
421                         string holder = "";
422                                         
423                         // get past comments
424                         while(holder != "translate" && holder != "Translate"){  
425                                 if(holder == "[" || holder == "[!"){
426                                         comment = 1;
427                                 }
428                                 if(holder == "]"){
429                                         comment = 0;
430                                 }
431                                 filehandle >> holder; 
432         
433                                 //if there is no translate then you must read tree string otherwise use translate to get names
434                                 if(holder == "tree" && comment != 1){   
435                                         //pass over the "tree rep.6878900 = "
436                                         while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF) ) {;}
437
438                                         if (c == EOF ) { break; }
439                                         filehandle.putback(c);  //put back first ( of tree.
440                                         readTreeString(filehandle);     
441                                         break;
442                                 }
443                         }
444                         
445                         //use nexus translation rather than parsing tree to save time
446                         if ((holder == "translate") || (holder == "Translate")) {
447
448                                 string number, name, h;
449                                 h = ""; // so it enters the loop the first time
450                                 while((h != ";") && (number != ";")) { 
451                                         filehandle >> number;
452                                         filehandle >> name;
453         
454                                         //c = , until done with translation then c = ;
455                                         h = name.substr(name.length()-1, name.length()); 
456                                         name.erase(name.end()-1);  //erase the comma
457                                         Treenames.push_back(number);
458                                 }
459                                 if (number == ";") { Treenames.pop_back(); }  //in case ';' from translation is on next line instead of next to last name
460                         }
461                 }
462                 
463         }
464         catch(exception& e) {
465                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
466                 exit(1);
467         }
468         catch(...) {
469                 cout << "An unknown error has occurred in the GlobalData class function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
470                 exit(1);
471         }               
472 }
473 /*******************************************************/
474
475 /*******************************************************/
476 void GlobalData::readTreeString(ifstream& filehandle)   {
477         try {
478                 int c;
479                 string name; //k
480                 
481                 while((c = filehandle.peek()) != ';') { 
482                                 //if you are a name
483                         if ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
484                                 name = "";
485                                 c = filehandle.get();
486         //              k = c;
487 //cout << k << endl;
488                                 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {                       
489                                         name += c;
490                                         c = filehandle.get();
491                 //      k = c;
492 //cout << " in name while " << k << endl;
493                                 }
494                                 
495 //cout << "name = " << name << endl;
496                                 Treenames.push_back(name);
497                                 filehandle.putback(c);
498 //k = c;
499 //cout << " after putback" <<  k << endl;
500                         } 
501                         
502                         if (c  == ':') { //read until you reach the end of the branch length
503                                 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
504                                         c = filehandle.get();
505                                 //      k = c;
506         //cout << " in branch while " << k << endl;
507                                 }
508                                 filehandle.putback(c);
509                         }
510                         c = filehandle.get();
511                         if (c == ';') { break; }
512                 //      k = c;
513 //cout << k << endl;
514
515                 }
516         }
517         catch(exception& e) {
518                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
519                 exit(1);
520         }
521         catch(...) {
522                 cout << "An unknown error has occurred in the GlobalData class function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
523                 exit(1);
524         }               
525 }       
526
527 /*******************************************************/
528
529 /*******************************************************/
530
531