1 #include "globaldata.hpp"
3 #include "sparsematrix.hpp"
5 /*******************************************************/
7 /******************************************************/
8 GlobalData* GlobalData::getInstance() {
9 if( _uniqueInstance == 0 ) {
10 _uniqueInstance = new GlobalData();
12 return _uniqueInstance;
14 /*******************************************************/
16 /******************************************************/
17 //This function parses through the option string of the command to remove its parameters
18 void GlobalData::parseGlobalData(string commandString, string optionText){
20 commandName = commandString; //save command name to be used by other classes
22 //set all non filename paramters to default
25 //clears out data from previous read
26 if ((commandName == "read.dist") || (commandName == "read.otu") || (commandName == "read.tree")) {
31 labels.clear(); lines.clear(); groups.clear();
36 if (commandName =="help") {
37 helpRequest = optionText;
40 if (commandName == "libshuff") {
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);
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; }
80 if (key == "line") {//stores lines to be used in a set
85 splitAtDash(value, lines);
88 if (key == "label") {//stores labels to be used in a set
93 splitAtDash(value, labels);
97 if (key == "groups") {//stores groups to be used in a vector
100 splitAtDash(value, Groups);
105 //saves the last parameter
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; }
135 if (key == "line") {//stores lines to be used in a vector
140 if (line != "all") { splitAtDash(value, lines); allLines = 0; }
141 else { allLines = 1; }
144 if (key == "label") {//stores lines to be used in a vector
149 if (label != "all") { splitAtDash(value, labels); allLines = 0; }
150 else { allLines = 1; }
153 if (key == "groups") {//stores groups to be used in a vector
156 splitAtDash(value, Groups);
160 //set format for shared
161 if ((listfile != "") && (groupfile != "")) { format = "shared"; }
162 if ((phylipfile != "") && (groupfile != "")) { format = "matrix"; }
164 //input defaults for calculators
165 if (commandName == "collect.single") {
166 if ((calc == "default") || (calc == "")) { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
168 splitAtDash(calc, Estimators);
170 if (commandName == "rarefaction.single") {
171 if ((calc == "default") || (calc == "")) { calc = "sobs"; }
173 splitAtDash(calc, Estimators);
175 if (commandName == "collect.shared") {
176 if ((calc == "default") || (calc == "")) { calc = "sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
178 splitAtDash(calc, Estimators);
180 if (commandName == "summary.single") {
181 if ((calc == "default") || (calc == "")) { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
183 splitAtDash(calc, Estimators);
185 if (commandName == "summary.shared") {
186 if ((calc == "default") || (calc == "")) { calc = "sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
188 splitAtDash(calc, Estimators);
190 if (commandName == "rarefaction.shared") {
191 if ((calc == "default") || (calc == "")) { calc = "sharedobserved"; }
193 splitAtDash(calc, Estimators);
195 if (commandName == "venn") {
196 if ((calc == "default") || (calc == "")) {
197 if (format == "list") { calc = "sobs"; }
198 else { calc = "sharedsobs"; }
201 splitAtDash(calc, Estimators);
203 if ((commandName == "tree.shared") || (commandName == "bootstrap.shared")) {
206 splitAtDash(calc, Estimators);
207 }else { cout << "You have not specified any calculators." << endl; }
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"; }
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";
223 cout << "An unknown error has occurred in the GlobalData class function parseGlobalData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227 /*******************************************************/
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; }
268 /*******************************************************/
270 /******************************************************/
271 GlobalData::GlobalData() {
272 //option definitions should go here...
276 /*******************************************************/
278 /******************************************************/
279 void GlobalData::clear() {
280 //option definitions should go here...
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.
307 sorted = "T"; //F means don't sort, T means sort.
311 //*******************************************************/
313 /******************************************************/
314 void GlobalData::reset() {
319 jumble = "1"; //0 means don't jumble, 1 means jumble.
320 sorted = "T"; //F means don't sort, T 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.
329 /*******************************************************/
331 /******************************************************/
332 GlobalData::~GlobalData() {
334 if(gListVector != NULL) { delete gListVector; }
335 if(gSparseMatrix != NULL) { delete gSparseMatrix; }
336 if(gorder != NULL) { delete gorder; }
338 /*******************************************************/
340 /*******************************************************/
341 void GlobalData::parseTreeFile() {
342 //only takes names from the first tree and assumes that all trees use the same names.
344 string filename = treefile;
346 openInputFile(filename, filehandle);
350 //if you are not a nexus file
351 if ((c = filehandle.peek()) != '#') {
352 while((c = filehandle.peek()) != ';') {
353 while ((c = filehandle.peek()) != ';') {
361 if((c == '(') && (comment != 1)){ break; }
365 readTreeString(filehandle);
367 //if you are a nexus file
368 }else if ((c = filehandle.peek()) == '#') {
372 while(holder != "translate" && holder != "Translate"){
373 if(holder == "[" || holder == "[!"){
379 filehandle >> holder;
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) ) {;}
386 if (c == EOF ) { break; }
387 filehandle.putback(c); //put back first ( of tree.
388 readTreeString(filehandle);
393 //use nexus translation rather than parsing tree to save time
394 if ((holder == "translate") || (holder == "Translate")) {
396 string number, name, h;
397 h = ""; // so it enters the loop the first time
398 while((h != ";") && (number != ";")) {
399 filehandle >> number;
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);
407 if (number == ";") { Treenames.pop_back(); } //in case ';' from translation is on next line instead of next to last name
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";
417 cout << "An unknown error has occurred in the GlobalData class function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
421 /*******************************************************/
423 /*******************************************************/
424 void GlobalData::readTreeString(ifstream& filehandle) {
429 while((c = filehandle.peek()) != ';') {
431 if ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
433 c = filehandle.get();
436 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {
438 c = filehandle.get();
440 //cout << " in name while " << k << endl;
443 //cout << "name = " << name << endl;
444 Treenames.push_back(name);
445 filehandle.putback(c);
447 //cout << " after putback" << k << endl;
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();
454 //cout << " in branch while " << k << endl;
456 filehandle.putback(c);
458 c = filehandle.get();
459 if (c == ';') { break; }
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";
470 cout << "An unknown error has occurred in the GlobalData class function parseTreeFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
475 /*******************************************************/
477 /*******************************************************/