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);
130 //if you are a nexus file
131 }else if ((c = filehandle.peek()) == '#') {
132 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; }
170 catch(exception& e) {
171 m->errorOut(e, "ReadNewickTree", "read");
175 /**************************************************************************************************/
176 //This function read the file through the translation of the sequences names and updates treemap.
177 void ReadNewickTree::nexusTranslation() {
181 int numSeqs = globaldata->gTreemap->getNumSeqs(); //must save this some when we clear old names we can still know how many sequences there were
185 while(holder != "translate" && holder != "Translate"){
186 if(holder == "[" || holder == "[!"){
192 filehandle >> holder;
193 if(holder == "tree" && comment != 1){return;}
197 globaldata->gTreemap->namesOfSeqs.clear();
198 for(int i=0;i<numSeqs;i++){
200 filehandle >> number;
202 name.erase(name.end()-1); //erase the comma
203 //insert new one with new name
204 globaldata->gTreemap->treemap[toString(number)].groupname = globaldata->gTreemap->treemap[name].groupname;
205 globaldata->gTreemap->treemap[toString(number)].vectorIndex = globaldata->gTreemap->treemap[name].vectorIndex;
206 //erase old one. so treemap[sarah].groupnumber is now treemap[1].groupnumber. if number is 1 and name is sarah.
207 globaldata->gTreemap->treemap.erase(name);
208 globaldata->gTreemap->namesOfSeqs.push_back(number);
211 catch(exception& e) {
212 m->errorOut(e, "ReadNewickTree", "nexusTranslation");
217 /**************************************************************************************************/
218 int ReadNewickTree::readTreeString() {
226 int ch = filehandle.peek();
229 n = numLeaves; //number of leaves / sequences, we want node 1 to start where the leaves left off
231 lc = readNewickInt(filehandle, n, T);
232 if (lc == -1) { m->mothurOut("error with lc"); m->mothurOutEndLine(); return -1; } //reports an error in reading
234 if(filehandle.peek()==','){
235 readSpecialChar(filehandle,',',"comma");
237 // ';' means end of tree.
238 else if((ch=filehandle.peek())==';' || ch=='['){
242 rc = readNewickInt(filehandle, n, T);
243 if (rc == -1) { m->mothurOut("error with rc"); m->mothurOutEndLine(); return -1; } //reports an error in reading
244 if(filehandle.peek() == ')'){
245 readSpecialChar(filehandle,')',"right parenthesis");
249 //note: treeclimber had the code below added - not sure why?
251 filehandle.putback(ch);
253 filehandle.get(name, MAX_LINE,'\n');
254 SKIPLINE(filehandle, ch);
256 n = T->getIndex(name);
259 m->mothurOut("Internal error: The only taxon is not taxon 0.\n");
261 readOk = -1; return -1;
266 while(((ch=filehandle.get())!=';') && (filehandle.eof() != true)){;}
268 T->tree[n].setChildren(lc,rc);
269 T->tree[n].setBranchLength(0);
270 T->tree[n].setParent(-1);
271 if(lc!=-1){ T->tree[lc].setParent(n); }
272 if(rc!=-1){ T->tree[rc].setParent(n); }
277 catch(exception& e) {
278 m->errorOut(e, "ReadNewickTree", "readTreeString");
282 /**************************************************************************************************/
284 int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T) {
287 if (m->control_pressed) { return -1; }
289 int c = readNodeChar(f);
292 int lc = readNewickInt(f, n, T);
293 if (lc == -1) { return -1; } //reports an error in reading
295 readSpecialChar(f,',',"comma");
297 int rc = readNewickInt(f, n, T);
298 if (rc == -1) { return -1; } //reports an error in reading
301 readSpecialChar(f,')',"right parenthesis");
302 //to pass over labels in trees
304 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
305 filehandle.putback(c);
310 readSpecialChar(f,':',"colon");
312 if(n >= numNodes){ m->mothurOut("Error: Too many nodes in input tree\n"); readOk = -1; return -1; }
314 T->tree[n].setBranchLength(readBranchLength(f));
316 T->tree[n].setBranchLength(0.0);
319 //to account for multifurcating trees generated by fasttree, we are forcing them to be bifurcating
320 while(f.peek() == ','){
322 //force this node to be left child and read new rc
323 T->tree[n].setChildren(lc,rc);
324 T->tree[lc].setParent(n);
325 T->tree[rc].setParent(n);
327 T->printTree(); cout << endl;
331 readSpecialChar(f,',',"comma");
333 int rc = readNewickInt(f, n, T);
334 cout << n << '\t' << lc << '\t' << rc << endl;
335 if (rc == -1) { return -1; } //reports an error in reading
340 cout << "in here " << k << endl;
341 readSpecialChar(f,')',"right parenthesis");
342 //to pass over labels in trees
344 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
345 filehandle.putback(c);
348 readSpecialChar(f,':',"colon");
350 if(n >= numNodes){ m->mothurOut("Error: Too many nodes in input tree\n"); readOk = -1; return -1; }
352 T->tree[n].setBranchLength(readBranchLength(f));
354 T->tree[n].setBranchLength(0.0);
360 cout << n << '\t' << lc << '\t' << rc << endl;
361 T->tree[n].setChildren(lc,rc);
362 T->tree[lc].setParent(n);
363 T->tree[rc].setParent(n);
364 T->printTree(); cout << endl;
371 while(d != ':' && d != ',' && d!=')' && d!='\n'){
375 cout << name << endl;
377 if(d == ':') { blen = 1; }
382 string group = globaldata->gTreemap->getGroup(name);
384 //find index in tree of name
385 int n1 = T->getIndex(name);
387 //adds sequence names that are not in group file to the "xxx" group
388 if(group == "not found") {
389 m->mothurOut("Name: " + name + " is not in your groupfile, and will be disregarded. \n"); //readOk = -1; return n1;
391 globaldata->gTreemap->namesOfSeqs.push_back(name);
392 globaldata->gTreemap->treemap[name].groupname = "xxx";
394 map<string, int>::iterator it;
395 it = globaldata->gTreemap->seqsPerGroup.find("xxx");
396 if (it == globaldata->gTreemap->seqsPerGroup.end()) { //its a new group
397 globaldata->gTreemap->namesOfGroups.push_back("xxx");
398 globaldata->gTreemap->seqsPerGroup["xxx"] = 1;
400 globaldata->gTreemap->seqsPerGroup["xxx"]++;
406 vector<string> tempGroup; tempGroup.push_back(group);
408 T->tree[n1].setGroup(tempGroup);
409 T->tree[n1].setChildren(-1,-1);
413 T->tree[n1].setBranchLength(readBranchLength(f));
415 T->tree[n1].setBranchLength(0.0);
418 while((c=f.get())!=0 && (c != ':' && c != ',' && c!=')') ) {;}
424 catch(exception& e) {
425 m->errorOut(e, "ReadNewickTree", "readNewickInt");
429 /**************************************************************************************************/
430 /**************************************************************************************************/