5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 /***********************************************************************/
13 ReadTree::ReadTree() {
15 globaldata = GlobalData::getInstance();
16 m = MothurOut::getInstance();
17 globaldata->gTree.clear();
20 m->errorOut(e, "ReadTree", "ReadTree");
24 /***********************************************************************/
25 int ReadTree::readSpecialChar(istream& f, char c, string name) {
32 m->mothurOut("Error: Input file ends prematurely, expecting a " + name + "\n");
36 m->mothurOut("Error: Expected " + name + " in input file. Found " + toString(d) + ".\n");
39 if(d == ')' && f.peek() == '\n'){
45 m->errorOut(e, "ReadTree", "readSpecialChar");
49 /**************************************************************************************************/
51 int ReadTree::readNodeChar(istream& f) {
53 // while(isspace(d=f.get())) {;}
58 m->mothurOut("Error: Input file ends prematurely, expecting a left parenthesis\n");
64 m->errorOut(e, "ReadTree", "readNodeChar");
69 /**************************************************************************************************/
71 float ReadTree::readBranchLength(istream& f) {
76 m->mothurOut("Error: Missing branch length in input tree.\n");
83 m->errorOut(e, "ReadTree", "readBranchLength");
88 /***********************************************************************/
89 /***********************************************************************/
93 /***********************************************************************/
94 /***********************************************************************/
95 //This class reads a file in Newick form and stores it in a tree.
97 int ReadNewickTree::read() {
103 //if you are not a nexus file
104 if ((c = filehandle.peek()) != '#') {
105 while((c = filehandle.peek()) != EOF) {
106 while ((c = filehandle.peek()) != EOF) {
114 if((c == '(') && (comment != 1)){ break; }
121 numNodes = T->getNumNodes();
122 numLeaves = T->getNumLeaves();
124 error = readTreeString();
126 //save trees for later commands
127 globaldata->gTree.push_back(T);
128 m->gobble(filehandle);
130 //if you are a nexus file
131 }else if ((c = filehandle.peek()) == '#') {
132 holder = nexusTranslation(); //reads file through the translation and updates treemap
133 while((c = filehandle.peek()) != EOF) {
135 while ((c = filehandle.peek()) != EOF) {
136 if(holder == "[" || holder == "[!"){
142 if((holder == "tree" || holder == "end;") && comment != 1){ holder = ""; comment = 0; break;}
143 filehandle >> holder;
146 //pass over the "tree rep.6878900 = "
147 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF) ) {;}
149 if (c == EOF ) { break; }
150 filehandle.putback(c); //put back first ( of tree.
154 numNodes = T->getNumNodes();
155 numLeaves = T->getNumLeaves();
158 error = readTreeString();
160 //save trees for later commands
161 globaldata->gTree.push_back(T);
165 if (error != 0) { readOk = error; }
171 catch(exception& e) {
172 m->errorOut(e, "ReadNewickTree", "read");
176 /**************************************************************************************************/
177 //This function read the file through the translation of the sequences names and updates treemap.
178 string ReadNewickTree::nexusTranslation() {
182 //int numSeqs = globaldata->gTreemap->getNumSeqs(); //must save this some when we clear old names we can still know how many sequences there were
186 while(holder != "translate" && holder != "Translate"){
187 if(holder == "[" || holder == "[!"){
193 filehandle >> holder;
194 if(holder == "tree" && comment != 1){return holder;}
198 globaldata->gTreemap->namesOfSeqs.clear();
202 while ((c = filehandle.peek()) != EOF) {
204 filehandle >> number;
206 if ((number == "tree") || (number == ";") ) { name = number; break; }
211 if (name.length() != 0) { lastChar = name[name.length()-1]; }
213 if ((name == "tree") || (name == ";") ) { break; }
215 if (lastChar == ',') { name.erase(name.end()-1); } //erase the comma
217 //insert new one with new name
218 string group = globaldata->gTreemap->getGroup(name);
219 globaldata->gTreemap->treemap[toString(number)].groupname = group;
220 globaldata->gTreemap->treemap[toString(number)].vectorIndex = globaldata->gTreemap->getIndex(name);
221 //erase old one. so treemap[sarah].groupnumber is now treemap[1].groupnumber. if number is 1 and name is sarah.
222 globaldata->gTreemap->treemap.erase(name);
223 globaldata->gTreemap->namesOfSeqs.push_back(number);
228 catch(exception& e) {
229 m->errorOut(e, "ReadNewickTree", "nexusTranslation");
234 /**************************************************************************************************/
235 int ReadNewickTree::readTreeString() {
243 int ch = filehandle.peek();
246 n = numLeaves; //number of leaves / sequences, we want node 1 to start where the leaves left off
248 lc = readNewickInt(filehandle, n, T);
249 if (lc == -1) { m->mothurOut("error with lc"); m->mothurOutEndLine(); return -1; } //reports an error in reading
251 if(filehandle.peek()==','){
252 readSpecialChar(filehandle,',',"comma");
254 // ';' means end of tree.
255 else if((ch=filehandle.peek())==';' || ch=='['){
260 rc = readNewickInt(filehandle, n, T);
261 if (rc == -1) { m->mothurOut("error with rc"); m->mothurOutEndLine(); return -1; } //reports an error in reading
262 if(filehandle.peek() == ')'){
263 readSpecialChar(filehandle,')',"right parenthesis");
267 //note: treeclimber had the code below added - not sure why?
269 filehandle.putback(ch);
271 filehandle.get(name, MAX_LINE,'\n');
272 SKIPLINE(filehandle, ch);
274 n = T->getIndex(name);
277 m->mothurOut("Internal error: The only taxon is not taxon 0.\n");
279 readOk = -1; return -1;
284 while(((ch=filehandle.get())!=';') && (filehandle.eof() != true)){;}
287 T->tree[n].setChildren(lc,rc);
288 T->tree[n].setBranchLength(0);
289 T->tree[n].setParent(-1);
290 if(lc!=-1){ T->tree[lc].setParent(n); }
291 if(rc!=-1){ T->tree[rc].setParent(n); }
294 //T->printTree(); cout << endl;
298 catch(exception& e) {
299 m->errorOut(e, "ReadNewickTree", "readTreeString");
303 /**************************************************************************************************/
305 int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T) {
308 if (m->control_pressed) { return -1; }
310 int c = readNodeChar(f);
314 //to account for multifurcating trees generated by fasttree, we are forcing them to be bifurcating
316 vector<int> childrenNodes;
317 while(f.peek() != ')'){
318 int child = readNewickInt(f, n, T);
319 if (child == -1) { return -1; } //reports an error in reading
320 //cout << "child = " << child << endl;
321 childrenNodes.push_back(child);
323 //after a child you either have , or ), check for both
324 if(f.peek()==')'){ break; }
325 else if (f.peek()==',') { readSpecialChar(f,',',"comma"); }
328 //cout << childrenNodes.size() << endl;
329 if (childrenNodes.size() < 2) { m->mothurOut("Error in tree, please correct."); m->mothurOutEndLine(); return -1; }
331 //then force into 2 node structure
332 for (int i = 1; i < childrenNodes.size(); i++) {
335 if (i == 1) { lc = childrenNodes[i-1]; rc = childrenNodes[i]; }
336 else { lc = n-1; rc = childrenNodes[i]; }
337 //cout << i << '\t' << lc << '\t' << rc << endl;
338 T->tree[n].setChildren(lc,rc);
339 T->tree[lc].setParent(n);
340 T->tree[rc].setParent(n);
342 //T->printTree(); cout << endl;
346 //to account for extra ++ in looping
350 readSpecialChar(f,')',"right parenthesis");
351 //to pass over labels in trees
353 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')&& (c!=')')){ c=filehandle.get(); }
354 filehandle.putback(c);
358 readSpecialChar(f,':',"colon");
360 if(n >= numNodes){ m->mothurOut("Error: Too many nodes in input tree\n"); readOk = -1; return -1; }
362 T->tree[n].setBranchLength(readBranchLength(f));
364 T->tree[n].setBranchLength(0.0);
367 //T->tree[n].setChildren(lc,rc);
368 //T->tree[lc].setParent(n);
369 //T->tree[rc].setParent(n);
370 //T->printTree(); cout << endl;
378 while(d != ':' && d != ',' && d!=')' && d!='\n'){
382 //cout << name << endl;
384 if(d == ':') { blen = 1; }
389 string group = globaldata->gTreemap->getGroup(name);
391 //find index in tree of name
392 int n1 = T->getIndex(name);
394 //adds sequence names that are not in group file to the "xxx" group
395 if(group == "not found") {
396 m->mothurOut("Name: " + name + " is not in your groupfile, and will be disregarded. \n"); //readOk = -1; return n1;
398 globaldata->gTreemap->namesOfSeqs.push_back(name);
399 globaldata->gTreemap->treemap[name].groupname = "xxx";
401 map<string, int>::iterator it;
402 it = globaldata->gTreemap->seqsPerGroup.find("xxx");
403 if (it == globaldata->gTreemap->seqsPerGroup.end()) { //its a new group
404 globaldata->gTreemap->namesOfGroups.push_back("xxx");
405 globaldata->gTreemap->seqsPerGroup["xxx"] = 1;
407 globaldata->gTreemap->seqsPerGroup["xxx"]++;
413 vector<string> tempGroup; tempGroup.push_back(group);
415 T->tree[n1].setGroup(tempGroup);
416 T->tree[n1].setChildren(-1,-1);
420 T->tree[n1].setBranchLength(readBranchLength(f));
422 T->tree[n1].setBranchLength(0.0);
425 while((c=f.get())!=0 && (c != ':' && c != ',' && c!=')') ) {;}
432 catch(exception& e) {
433 m->errorOut(e, "ReadNewickTree", "readNewickInt");
437 /**************************************************************************************************/
438 /**************************************************************************************************/