]> git.donarmstrong.com Git - mothur.git/blob - globaldata.cpp
removed "shared" from some of the calculator names and classes
[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 == "scaler")            { scaler = 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 == "scaler")            { scaler = 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-sorensonabund-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-sorensonabund-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.groups") {
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::getScaler()                  {       return scaler;          }
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 }
276 /*******************************************************/
277
278 /******************************************************/
279 void GlobalData::clear() {
280         //option definitions should go here...
281         phylipfile              =       "";
282         columnfile              =       "";
283         listfile                =       "";
284         rabundfile              =       "";
285         sabundfile              =       "";
286         namefile                =       "";
287         groupfile               =       ""; 
288         orderfile               =       "";
289         fastafile               =   "";
290         treefile                =       "";
291         sharedfile              =       "";
292         cutoff                  =       "10.00";
293         format                  =       "";
294         precision               =       "100";
295         iters                   =       "1000"; 
296         line                    =   "";
297         label                   =       "";
298         groups                  =       "";
299         jumble                  =       "1";    //0 means don't jumble, 1 means jumble.
300         randomtree              =       "";  //"" means user will enter some user trees, "outputfile" means they just want the random tree distribution to be outputted to outputfile.
301         freq                    =       "100";
302         method                  =       "furthest";
303         fileroot                =       "";
304         abund           =   "10";
305         step                    =       "0.01";
306         form                    =       "integral";
307         sorted                  =       "1";  //0 means don't sort, 1 means sort.
308         scaler                  =       "log10";
309 }
310
311 //*******************************************************/
312
313 /******************************************************/
314 void GlobalData::reset() {
315         cutoff                  =       "10.00";
316         precision               =       "100";
317         iters                   =       "1000"; 
318         groups                  =       "";
319         jumble                  =       "1";    //0 means don't jumble, 1 means jumble.
320         sorted                  =       "1";  //0 means don't sort, 1 means sort.
321         randomtree              =       "";  //"" means user will enter some user trees, "outputfile" means they just want the random tree distribution to be outputted to outputfile.
322         freq                    =       "100";
323         method                  =       "furthest";
324         calc                    =       "";
325         abund                   =   "10";
326         step                    =       "0.01";
327         form                    =       "integral";
328 }
329 /*******************************************************/
330
331 /******************************************************/
332 GlobalData::~GlobalData() {
333         _uniqueInstance = 0;
334         if(gListVector != NULL)         {       delete gListVector;             }
335         if(gSparseMatrix != NULL)       {       delete gSparseMatrix;   }
336         if(gorder != NULL)                      {       delete gorder;          }
337 }
338 /*******************************************************/
339
340 /*******************************************************/
341 void GlobalData::parseTreeFile() {
342         //only takes names from the first tree and assumes that all trees use the same names.
343         try {
344                 string filename = treefile;
345                 ifstream filehandle;
346                 openInputFile(filename, filehandle);
347                 int c, comment;
348                 comment = 0;
349                 
350                 //if you are not a nexus file 
351                 if ((c = filehandle.peek()) != '#') {  
352                         while((c = filehandle.peek()) != ';') { 
353                                 while ((c = filehandle.peek()) != ';') {
354                                         // get past comments
355                                         if(c == '[') {
356                                                 comment = 1;
357                                         }
358                                         if(c == ']'){
359                                                 comment = 0;
360                                         }
361                                         if((c == '(') && (comment != 1)){ break; }
362                                         filehandle.get();
363                                 }
364
365                                 readTreeString(filehandle); 
366                         }
367                 //if you are a nexus file
368                 }else if ((c = filehandle.peek()) == '#') {
369                         string holder = "";
370                                         
371                         // get past comments
372                         while(holder != "translate" && holder != "Translate"){  
373                                 if(holder == "[" || holder == "[!"){
374                                         comment = 1;
375                                 }
376                                 if(holder == "]"){
377                                         comment = 0;
378                                 }
379                                 filehandle >> holder; 
380         
381                                 //if there is no translate then you must read tree string otherwise use translate to get names
382                                 if(holder == "tree" && comment != 1){   
383                                         //pass over the "tree rep.6878900 = "
384                                         while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF) ) {;}
385
386                                         if (c == EOF ) { break; }
387                                         filehandle.putback(c);  //put back first ( of tree.
388                                         readTreeString(filehandle);     
389                                         break;
390                                 }
391                         }
392                         
393                         //use nexus translation rather than parsing tree to save time
394                         if ((holder == "translate") || (holder == "Translate")) {
395 cout << "there is a translate " << endl;
396                                 string number, name, h;
397                                 h = ""; // so it enters the loop the first time
398                                 while((h != ";") && (number != ";")) { 
399                                         filehandle >> number;
400                                         filehandle >> name;
401         
402                                         //c = , until done with translation then c = ;
403                                         h = name.substr(name.length()-1, name.length()); 
404                                         name.erase(name.end()-1);  //erase the comma
405                                         Treenames.push_back(number);
406                                 }
407                                 if (number == ";") { Treenames.pop_back(); }  //in case ';' from translation is on next line instead of next to last name
408                         }
409                 }
410                 
411         }
412         catch(exception& e) {
413                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
414                 exit(1);
415         }
416         catch(...) {
417                 cout << "An unknown error has occurred in the GlobalData class function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
418                 exit(1);
419         }               
420 }
421 /*******************************************************/
422
423 /*******************************************************/
424 void GlobalData::readTreeString(ifstream& filehandle)   {
425         try {
426                 int c;
427                 string name; //k
428                 
429                 while((c = filehandle.peek()) != ';') { 
430                                 //if you are a name
431                         if ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
432                                 name = "";
433                                 c = filehandle.get();
434         //              k = c;
435 //cout << k << endl;
436                                 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {                       
437                                         name += c;
438                                         c = filehandle.get();
439                 //      k = c;
440 //cout << " in name while " << k << endl;
441                                 }
442                                 
443 //cout << "name = " << name << endl;
444                                 Treenames.push_back(name);
445                                 filehandle.putback(c);
446 //k = c;
447 //cout << " after putback" <<  k << endl;
448                         } 
449                         
450                         if (c  == ':') { //read until you reach the end of the branch length
451                                 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
452                                         c = filehandle.get();
453                                 //      k = c;
454         //cout << " in branch while " << k << endl;
455                                 }
456                                 filehandle.putback(c);
457                         }
458                         c = filehandle.get();
459                         if (c == ';') { break; }
460                 //      k = c;
461 //cout << k << endl;
462
463                 }
464         }
465         catch(exception& e) {
466                 cout << "Standard Error: " << e.what() << " has occurred in the GlobalData class Function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
467                 exit(1);
468         }
469         catch(...) {
470                 cout << "An unknown error has occurred in the GlobalData class function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
471                 exit(1);
472         }               
473 }       
474
475 /*******************************************************/
476
477 /*******************************************************/
478
479