5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 /***********************************************************************/
14 // The following functions are used by all reading formats.
15 /***********************************************************************/
16 ReadTree::ReadTree() {
18 globaldata = GlobalData::getInstance();
20 numNodes = T->getNumNodes();
21 numLeaves = T->getNumLeaves();
24 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function ReadTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
28 cout << "An unknown error has occurred in the ReadTree class function ReadTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
32 /***********************************************************************/
33 int ReadTree::readSpecialChar(istream& f, char c, string name) {
37 while(isspace(d=f.get())) {;}
39 cerr << "Error: Input file ends prematurely, expecting a " << name << "\n";
43 cerr << "Error: Expected " << name << " in input file. Found " << d << ".\n";
46 if(d == ')' && f.peek() == '\n'){
47 while(isspace(d=f.get())) {;}
54 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function readSpecialChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58 cout << "An unknown error has occurred in the ReadTree class function readSpecialChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
62 /**************************************************************************************************/
64 int ReadTree::readNodeChar(istream& f) {
67 while(isspace(d=f.get())) {;}
69 cerr << "Error: Input file ends prematurely, expecting a left parenthesis\n";
75 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function readNodeChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79 cout << "An unknown error has occurred in the ReadTree class function readNodeChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84 /**************************************************************************************************/
86 float ReadTree::readBranchLength(istream& f) {
91 cerr << "Error: Missing branch length in input tree.\n";
98 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function readBranchLength. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102 cout << "An unknown error has occurred in the ReadTree class function readBranchLength. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108 /***********************************************************************/
109 /***********************************************************************/
112 //Child Classes Below
114 /***********************************************************************/
115 /***********************************************************************/
116 //This class reads a file in Newick form and stores it in a tree.
118 void ReadNewickTree::read() {
125 int ch = filehandle.peek();
128 n = numLeaves; //number of leaves / sequences, we want node 1 to start where the leaves left off
129 lc = readNewickInt(filehandle, n, T);
131 if(filehandle.peek()==','){
132 readSpecialChar(filehandle,',',"comma");
134 // ';' means end of tree.
135 else if((ch=filehandle.peek())==';' || ch=='['){
139 rc = readNewickInt(filehandle, n, T);
140 if(filehandle.peek() == ')'){
141 readSpecialChar(filehandle,')',"right parenthesis");
145 //note: treeclimber had the code below added - not sure why?
147 filehandle.putback(ch);
149 filehandle.get(name, MAX_LINE,'\n');
150 SKIPLINE(filehandle, ch);
153 n = T->getIndex(name);
155 cerr << "Internal error: The only taxon is not taxon 0.\n";
161 while((ch=filehandle.get())!=';'){;}
163 T->tree[n].setChildren(lc,rc);
164 T->tree[n].setBranchLength(0);
165 T->tree[n].setParent(-1);
166 if(lc!=-1){ T->tree[lc].setParent(n); }
167 if(rc!=-1){ T->tree[rc].setParent(n); }
170 //save tree for later commands
171 globaldata->gTree = T;
173 catch(exception& e) {
174 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178 cout << "An unknown error has occurred in the ReadNewickTree class function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182 /**************************************************************************************************/
184 int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T) {
186 int c = readNodeChar(f);
189 int lc = readNewickInt(f, n, T);
190 readSpecialChar(f,',',"comma");
192 int rc = readNewickInt(f, n, T);
194 readSpecialChar(f,')',"right parenthesis");
198 readSpecialChar(f,':',"colon");
199 if(n >= numNodes){ cerr << "Error: Too many nodes in input tree\n"; exit(1); }
200 T->tree[n].setBranchLength(readBranchLength(f));
201 }else{T->tree[n].setBranchLength(0.0); }
203 T->tree[n].setChildren(lc,rc);
204 T->tree[lc].setParent(n);
205 T->tree[rc].setParent(n);
212 while(d != ':' && d != ',' && d!=')' && d!='\n'){
218 if(d == ':') { blen = 1; }
223 string group = globaldata->gTreemap->getGroup(name);
225 //find index in tree of name
226 int n1 = T->getIndex(name);
228 if(n1 == -1){cerr << "Name: " << name << " not found\n"; exit(1);}
230 else T->tree[n1].setGroup(group);
232 T->tree[n1].setChildren(-1,-1);
236 T->tree[n1].setBranchLength(readBranchLength(f));
238 T->tree[n1].setBranchLength(0.0);
241 while((c=f.get())!=0 && (c != ':' && c != ',' && c!=')') ) {;}
247 catch(exception& e) {
248 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
252 cout << "An unknown error has occurred in the ReadNewickTree class function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
256 /**************************************************************************************************/
257 /**************************************************************************************************/