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