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