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 globaldata->gTree.clear();
19 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function ReadTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
23 cout << "An unknown error has occurred in the ReadTree class function ReadTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
27 /***********************************************************************/
28 int ReadTree::readSpecialChar(istream& f, char c, string name) {
32 while(isspace(d=f.get())) {;}
34 cerr << "Error: Input file ends prematurely, expecting a " << name << "\n";
38 cerr << "Error: Expected " << name << " in input file. Found " << d << ".\n";
41 if(d == ')' && f.peek() == '\n'){
42 while(isspace(d=f.get())) {;}
49 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function readSpecialChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53 cout << "An unknown error has occurred in the ReadTree class function readSpecialChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57 /**************************************************************************************************/
59 int ReadTree::readNodeChar(istream& f) {
62 while(isspace(d=f.get())) {;}
64 cerr << "Error: Input file ends prematurely, expecting a left parenthesis\n";
70 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function readNodeChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
74 cout << "An unknown error has occurred in the ReadTree class function readNodeChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79 /**************************************************************************************************/
81 float ReadTree::readBranchLength(istream& f) {
86 cerr << "Error: Missing branch length in input tree.\n";
93 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function readBranchLength. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
97 cout << "An unknown error has occurred in the ReadTree class function readBranchLength. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103 /***********************************************************************/
104 /***********************************************************************/
107 //Child Classes Below
109 /***********************************************************************/
110 /***********************************************************************/
111 //This class reads a file in Newick form and stores it in a tree.
113 void ReadNewickTree::read() {
118 //if you are not a nexus file
119 if ((c = filehandle.peek()) != '#') {
120 while((c = filehandle.peek()) != EOF) {
123 numNodes = T->getNumNodes();
124 numLeaves = T->getNumLeaves();
127 //save trees for later commands
128 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();
159 //save trees for later commands
160 globaldata->gTree.push_back(T);
165 catch(exception& e) {
166 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170 cout << "An unknown error has occurred in the ReadNewickTree class function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174 /**************************************************************************************************/
175 //This function read the file through the translation of the sequences names and updates treemap.
176 void ReadNewickTree::nexusTranslation() {
180 int numSeqs = globaldata->gTreemap->getNumSeqs(); //must save this some when we clear old names we can still know how many sequences there were
184 while(holder != "translate" && holder != "Translate"){
185 if(holder == "[" || holder == "[!"){
191 filehandle >> holder;
192 if(holder == "tree" && comment != 1){return;}
196 globaldata->gTreemap->namesOfSeqs.clear();
197 for(int i=0;i<numSeqs;i++){
199 filehandle >> number;
201 name.erase(name.end()-1); //erase the comma
202 //insert new one with new name
203 globaldata->gTreemap->treemap[toString(number)].groupname = globaldata->gTreemap->treemap[name].groupname;
204 globaldata->gTreemap->treemap[toString(number)].vectorIndex = globaldata->gTreemap->treemap[name].vectorIndex;
205 //erase old one. so treemap[sarah].groupnumber is now treemap[1].groupnumber. if number is 1 and name is sarah.
206 globaldata->gTreemap->treemap.erase(name);
207 globaldata->gTreemap->namesOfSeqs.push_back(number);
210 catch(exception& e) {
211 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function nexus. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
215 cout << "An unknown error has occurred in the ReadNewickTree class function nexus. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
220 /**************************************************************************************************/
221 void ReadNewickTree::readTreeString() {
229 int ch = filehandle.peek();
232 n = numLeaves; //number of leaves / sequences, we want node 1 to start where the leaves left off
233 lc = readNewickInt(filehandle, n, T);
235 if(filehandle.peek()==','){
236 readSpecialChar(filehandle,',',"comma");
238 // ';' means end of tree.
239 else if((ch=filehandle.peek())==';' || ch=='['){
243 rc = readNewickInt(filehandle, n, T);
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);
257 n = T->getIndex(name);
259 cerr << "Internal error: The only taxon is not taxon 0.\n";
265 while((ch=filehandle.get())!=';'){;}
267 T->tree[n].setChildren(lc,rc);
268 T->tree[n].setBranchLength(0);
269 T->tree[n].setParent(-1);
270 if(lc!=-1){ T->tree[lc].setParent(n); }
271 if(rc!=-1){ T->tree[rc].setParent(n); }
275 catch(exception& e) {
276 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function readTreeString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280 cout << "An unknown error has occurred in the ReadNewickTree class function readTreeString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
285 /**************************************************************************************************/
287 int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T) {
289 int c = readNodeChar(f);
292 int lc = readNewickInt(f, n, T);
293 readSpecialChar(f,',',"comma");
295 int rc = readNewickInt(f, n, T);
297 readSpecialChar(f,')',"right parenthesis");
301 readSpecialChar(f,':',"colon");
302 if(n >= numNodes){ cerr << "Error: Too many nodes in input tree\n"; exit(1); }
303 T->tree[n].setBranchLength(readBranchLength(f));
304 }else{T->tree[n].setBranchLength(0.0); }
306 T->tree[n].setChildren(lc,rc);
307 T->tree[lc].setParent(n);
308 T->tree[rc].setParent(n);
315 while(d != ':' && d != ',' && d!=')' && d!='\n'){
321 if(d == ':') { blen = 1; }
326 string group = globaldata->gTreemap->getGroup(name);
328 //find index in tree of name
329 int n1 = T->getIndex(name);
331 if(n1 == -1){cerr << "Name: " << name << " not found\n"; exit(1);}
333 else T->tree[n1].setGroup(group);
335 T->tree[n1].setChildren(-1,-1);
339 T->tree[n1].setBranchLength(readBranchLength(f));
341 T->tree[n1].setBranchLength(0.0);
344 while((c=f.get())!=0 && (c != ':' && c != ',' && c!=')') ) {;}
350 catch(exception& e) {
351 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
355 cout << "An unknown error has occurred in the ReadNewickTree class function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
359 /**************************************************************************************************/
360 /**************************************************************************************************/