5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 /***********************************************************************/
13 ReadTree::ReadTree() {
15 m = MothurOut::getInstance();
18 m->errorOut(e, "ReadTree", "ReadTree");
22 /***********************************************************************/
23 int ReadTree::AssembleTrees() {
25 //assemble users trees
26 for (int i = 0; i < Trees.size(); i++) {
27 if (m->control_pressed) { return 0; }
28 Trees[i]->assembleTree();
33 m->errorOut(e, "ReadTree", "AssembleTrees");
37 /***********************************************************************/
38 int ReadTree::readSpecialChar(istream& f, char c, string name) {
45 m->mothurOut("Error: Input file ends prematurely, expecting a " + name + "\n");
49 m->mothurOut("Error: Expected " + name + " in input file. Found " + toString(d) + ".\n");
52 if(d == ')' && f.peek() == '\n'){
58 m->errorOut(e, "ReadTree", "readSpecialChar");
62 /**************************************************************************************************/
64 int ReadTree::readNodeChar(istream& f) {
66 // while(isspace(d=f.get())) {;}
71 m->mothurOut("Error: Input file ends prematurely, expecting a left parenthesis\n");
77 m->errorOut(e, "ReadTree", "readNodeChar");
82 /**************************************************************************************************/
84 float ReadTree::readBranchLength(istream& f) {
89 m->mothurOut("Error: Missing branch length in input tree.\n");
96 m->errorOut(e, "ReadTree", "readBranchLength");
101 /***********************************************************************/
102 /***********************************************************************/
104 //Child Classes Below
106 /***********************************************************************/
107 /***********************************************************************/
108 //This class reads a file in Newick form and stores it in a tree.
110 int ReadNewickTree::read(CountTable* ct) {
116 //if you are not a nexus file
117 if ((c = filehandle.peek()) != '#') {
118 while((c = filehandle.peek()) != EOF) {
119 while ((c = filehandle.peek()) != EOF) {
127 if((c == '(') && (comment != 1)){ break; }
134 numNodes = T->getNumNodes();
135 numLeaves = T->getNumLeaves();
137 error = readTreeString(ct);
139 //save trees for later commands
141 m->gobble(filehandle);
143 //if you are a nexus file
144 }else if ((c = filehandle.peek()) == '#') {
145 //get right number of seqs from nexus file.
146 Tree* temp = new Tree(ct); delete temp;
148 nexusTranslation(ct); //reads file through the translation and updates treemap
149 while((c = filehandle.peek()) != EOF) {
151 while ((c = filehandle.peek()) != EOF) {
152 if(holder == "[" || holder == "[!"){
158 if((holder == "tree" || holder == "end;") && comment != 1){ holder = ""; comment = 0; break;}
159 filehandle >> holder;
162 //pass over the "tree rep.6878900 = "
163 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF) ) {;}
165 if (c == EOF ) { break; }
166 filehandle.putback(c); //put back first ( of tree.
170 numNodes = T->getNumNodes();
171 numLeaves = T->getNumLeaves();
174 error = readTreeString(ct);
176 //save trees for later commands
181 if (error != 0) { readOk = error; }
187 catch(exception& e) {
188 m->errorOut(e, "ReadNewickTree", "read");
192 /**************************************************************************************************/
193 //This function read the file through the translation of the sequences names and updates treemap.
194 string ReadNewickTree::nexusTranslation(CountTable* ct) {
198 int numSeqs = m->Treenames.size(); //must save this some when we clear old names we can still know how many sequences there were
202 while(holder != "translate" && holder != "Translate"){
203 if(holder == "[" || holder == "[!"){
209 filehandle >> holder;
210 if(holder == "tree" && comment != 1){return holder;}
214 for(int i=0;i<numSeqs;i++){
216 filehandle >> number;
218 name.erase(name.end()-1); //erase the comma
219 ct->renameSeq(name, toString(number));
224 catch(exception& e) {
225 m->errorOut(e, "ReadNewickTree", "nexusTranslation");
230 /**************************************************************************************************/
231 int ReadNewickTree::readTreeString(CountTable* ct) {
239 int ch = filehandle.peek();
242 n = numLeaves; //number of leaves / sequences, we want node 1 to start where the leaves left off
244 lc = readNewickInt(filehandle, n, T, ct);
245 if (lc == -1) { m->mothurOut("error with lc"); m->mothurOutEndLine(); return -1; } //reports an error in reading
247 if(filehandle.peek()==','){
248 readSpecialChar(filehandle,',',"comma");
250 // ';' means end of tree.
251 else if((ch=filehandle.peek())==';' || ch=='['){
256 rc = readNewickInt(filehandle, n, T, ct);
257 if (rc == -1) { m->mothurOut("error with rc"); m->mothurOutEndLine(); return -1; } //reports an error in reading
258 if(filehandle.peek() == ')'){
259 readSpecialChar(filehandle,')',"right parenthesis");
263 //note: treeclimber had the code below added - not sure why?
265 filehandle.putback(ch);
267 filehandle.get(name, MAX_LINE,'\n');
268 SKIPLINE(filehandle, ch);
270 n = T->getIndex(name);
273 m->mothurOut("Internal error: The only taxon is not taxon 0.\n");
275 readOk = -1; return -1;
280 while(((ch=filehandle.get())!=';') && (filehandle.eof() != true)){;}
283 T->tree[n].setChildren(lc,rc);
284 T->tree[n].setBranchLength(0);
285 T->tree[n].setParent(-1);
286 if(lc!=-1){ T->tree[lc].setParent(n); }
287 if(rc!=-1){ T->tree[rc].setParent(n); }
290 //T->printTree(); cout << endl;
294 catch(exception& e) {
295 m->errorOut(e, "ReadNewickTree", "readTreeString");
299 /**************************************************************************************************/
301 int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T, CountTable* ct) {
304 if (m->control_pressed) { return -1; }
306 int c = readNodeChar(f);
310 //to account for multifurcating trees generated by fasttree, we are forcing them to be bifurcating
312 vector<int> childrenNodes;
313 while(f.peek() != ')'){
314 int child = readNewickInt(f, n, T, ct);
315 if (child == -1) { return -1; } //reports an error in reading
316 //cout << "child = " << child << endl;
317 childrenNodes.push_back(child);
319 //after a child you either have , or ), check for both
320 if(f.peek()==')'){ break; }
321 else if (f.peek()==',') { readSpecialChar(f,',',"comma"); }
324 //cout << childrenNodes.size() << endl;
325 if (childrenNodes.size() < 2) { m->mothurOut("Error in tree, please correct."); m->mothurOutEndLine(); return -1; }
327 //then force into 2 node structure
328 for (int i = 1; i < childrenNodes.size(); i++) {
331 if (i == 1) { lc = childrenNodes[i-1]; rc = childrenNodes[i]; }
332 else { lc = n-1; rc = childrenNodes[i]; }
333 //cout << i << '\t' << lc << '\t' << rc << endl;
334 T->tree[n].setChildren(lc,rc);
335 T->tree[lc].setParent(n);
336 T->tree[rc].setParent(n);
338 //T->printTree(); cout << endl;
342 //to account for extra ++ in looping
346 readSpecialChar(f,')',"right parenthesis");
347 //to pass over labels in trees
349 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')&& (c!=')')){ c=filehandle.get(); }
350 filehandle.putback(c);
354 readSpecialChar(f,':',"colon");
356 if(n >= numNodes){ m->mothurOut("Error: Too many nodes in input tree\n"); readOk = -1; return -1; }
358 T->tree[n].setBranchLength(readBranchLength(f));
360 T->tree[n].setBranchLength(0.0);
369 while(d != ':' && d != ',' && d!=')' && d!='\n'){
373 //cout << name << endl;
375 if(d == ':') { blen = 1; }
380 vector<string> group = ct->getGroups(name);
381 //cout << name << endl;
382 //find index in tree of name
383 int n1 = T->getIndex(name);
385 //adds sequence names that are not in group file to the "xxx" group
386 if(group.size() == 0) {
387 m->mothurOut("Name: " + name + " is not in your groupfile, and will be disregarded. \n"); //readOk = -1; return n1;
389 vector<string> currentGroups = ct->getNamesOfGroups();
390 if (!m->inUsersGroups("xxx", currentGroups)) { ct->addGroup("xxx"); }
391 currentGroups = ct->getNamesOfGroups();
392 vector<int> thisCounts; thisCounts.resize(currentGroups.size(), 0);
393 for (int h = 0; h < currentGroups.size(); h++) {
394 if (currentGroups[h] == "xxx") { thisCounts[h] = 1; break; }
396 ct->push_back(name, thisCounts);
398 group.push_back("xxx");
400 T->tree[n1].setGroup(group);
401 T->tree[n1].setChildren(-1,-1);
405 T->tree[n1].setBranchLength(readBranchLength(f));
407 T->tree[n1].setBranchLength(0.0);
410 while((c=f.get())!=0 && (c != ':' && c != ',' && c!=')') ) {;}
417 catch(exception& e) {
418 m->errorOut(e, "ReadNewickTree", "readNewickInt");
422 /**************************************************************************************************/
423 /**************************************************************************************************/