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) {
35 cerr << "Error: Input file ends prematurely, expecting a " << name << "\n";
39 cerr << "Error: Expected " << name << " in input file. Found " << d << ".\n";
42 if(d == ')' && f.peek() == '\n'){
48 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function readSpecialChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52 cout << "An unknown error has occurred in the ReadTree class function readSpecialChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
56 /**************************************************************************************************/
58 int ReadTree::readNodeChar(istream& f) {
60 // while(isspace(d=f.get())) {;}
65 cerr << "Error: Input file ends prematurely, expecting a left parenthesis\n";
71 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function readNodeChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
75 cout << "An unknown error has occurred in the ReadTree class function readNodeChar. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80 /**************************************************************************************************/
82 float ReadTree::readBranchLength(istream& f) {
87 cerr << "Error: Missing branch length in input tree.\n";
94 cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function readBranchLength. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
98 cout << "An unknown error has occurred in the ReadTree class function readBranchLength. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103 /***********************************************************************/
104 /***********************************************************************/
106 //Child Classes Below
108 /***********************************************************************/
109 /***********************************************************************/
110 //This class reads a file in Newick form and stores it in a tree.
112 int ReadNewickTree::read() {
118 //if you are not a nexus file
119 if ((c = filehandle.peek()) != '#') {
120 while((c = filehandle.peek()) != EOF) {
121 while ((c = filehandle.peek()) != EOF) {
129 if((c == '(') && (comment != 1)){ break; }
135 numNodes = T->getNumNodes();
136 numLeaves = T->getNumLeaves();
138 error = readTreeString();
140 //save trees for later commands
141 globaldata->gTree.push_back(T);
144 //if you are a nexus file
145 }else if ((c = filehandle.peek()) == '#') {
146 nexusTranslation(); //reads file through the translation and updates treemap
147 while((c = filehandle.peek()) != EOF) {
149 while ((c = filehandle.peek()) != EOF) {
150 if(holder == "[" || holder == "[!"){
156 if((holder == "tree" || holder == "end;") && comment != 1){ holder = ""; comment = 0; break;}
157 filehandle >> holder;
160 //pass over the "tree rep.6878900 = "
161 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF) ) {;}
163 if (c == EOF ) { break; }
164 filehandle.putback(c); //put back first ( of tree.
168 numNodes = T->getNumNodes();
169 numLeaves = T->getNumLeaves();
172 error = readTreeString();
174 //save trees for later commands
175 globaldata->gTree.push_back(T);
179 if (error != 0) { readOk = error; }
182 catch(exception& e) {
183 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187 cout << "An unknown error has occurred in the ReadNewickTree class function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
191 /**************************************************************************************************/
192 //This function read the file through the translation of the sequences names and updates treemap.
193 void ReadNewickTree::nexusTranslation() {
197 int numSeqs = globaldata->gTreemap->getNumSeqs(); //must save this some when we clear old names we can still know how many sequences there were
201 while(holder != "translate" && holder != "Translate"){
202 if(holder == "[" || holder == "[!"){
208 filehandle >> holder;
209 if(holder == "tree" && comment != 1){return;}
213 globaldata->gTreemap->namesOfSeqs.clear();
214 for(int i=0;i<numSeqs;i++){
216 filehandle >> number;
218 name.erase(name.end()-1); //erase the comma
219 //insert new one with new name
220 globaldata->gTreemap->treemap[toString(number)].groupname = globaldata->gTreemap->treemap[name].groupname;
221 globaldata->gTreemap->treemap[toString(number)].vectorIndex = globaldata->gTreemap->treemap[name].vectorIndex;
222 //erase old one. so treemap[sarah].groupnumber is now treemap[1].groupnumber. if number is 1 and name is sarah.
223 globaldata->gTreemap->treemap.erase(name);
224 globaldata->gTreemap->namesOfSeqs.push_back(number);
227 catch(exception& e) {
228 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function nexus. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232 cout << "An unknown error has occurred in the ReadNewickTree class function nexus. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237 /**************************************************************************************************/
238 int ReadNewickTree::readTreeString() {
246 int ch = filehandle.peek();
249 n = numLeaves; //number of leaves / sequences, we want node 1 to start where the leaves left off
251 lc = readNewickInt(filehandle, n, T);
252 if (lc == -1) { cout << "error with lc" << endl; return -1; } //reports an error in reading
254 if(filehandle.peek()==','){
255 readSpecialChar(filehandle,',',"comma");
257 // ';' means end of tree.
258 else if((ch=filehandle.peek())==';' || ch=='['){
262 rc = readNewickInt(filehandle, n, T);
263 if (rc == -1) { cout << "error with rc" << endl; return -1; } //reports an error in reading
264 if(filehandle.peek() == ')'){
265 readSpecialChar(filehandle,')',"right parenthesis");
269 //note: treeclimber had the code below added - not sure why?
271 filehandle.putback(ch);
273 filehandle.get(name, MAX_LINE,'\n');
274 SKIPLINE(filehandle, ch);
276 n = T->getIndex(name);
279 cerr << "Internal error: The only taxon is not taxon 0.\n";
281 readOk = -1; return -1;
286 while((ch=filehandle.get())!=';'){;}
288 T->tree[n].setChildren(lc,rc);
289 T->tree[n].setBranchLength(0);
290 T->tree[n].setParent(-1);
291 if(lc!=-1){ T->tree[lc].setParent(n); }
292 if(rc!=-1){ T->tree[rc].setParent(n); }
297 catch(exception& e) {
298 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function readTreeString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
302 cout << "An unknown error has occurred in the ReadNewickTree class function readTreeString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
307 /**************************************************************************************************/
309 int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T) {
311 int c = readNodeChar(f);
314 int lc = readNewickInt(f, n, T);
315 if (lc == -1) { return -1; } //reports an error in reading
316 readSpecialChar(f,',',"comma");
318 int rc = readNewickInt(f, n, T);
319 if (rc == -1) { return -1; } //reports an error in reading
321 readSpecialChar(f,')',"right parenthesis");
325 readSpecialChar(f,':',"colon");
327 if(n >= numNodes){ cerr << "Error: Too many nodes in input tree\n"; readOk = -1; return -1; }
329 T->tree[n].setBranchLength(readBranchLength(f));
330 }else{T->tree[n].setBranchLength(0.0); }
332 T->tree[n].setChildren(lc,rc);
333 T->tree[lc].setParent(n);
334 T->tree[rc].setParent(n);
341 while(d != ':' && d != ',' && d!=')' && d!='\n'){
347 if(d == ':') { blen = 1; }
352 string group = globaldata->gTreemap->getGroup(name);
354 //find index in tree of name
355 int n1 = T->getIndex(name);
357 //adds sequence names that are not in group file to the "xxx" group
358 if(group == "not found") {
359 cout << "Name: " << name << " is not in your groupfile, and will be disregarded. \n"; //readOk = -1; return n1;
361 globaldata->gTreemap->namesOfSeqs.push_back(name);
362 globaldata->gTreemap->treemap[name].groupname = "xxx";
364 map<string, int>::iterator it;
365 it = globaldata->gTreemap->seqsPerGroup.find("xxx");
366 if (it == globaldata->gTreemap->seqsPerGroup.end()) { //its a new group
367 globaldata->gTreemap->namesOfGroups.push_back("xxx");
368 globaldata->gTreemap->seqsPerGroup["xxx"] = 1;
370 globaldata->gTreemap->seqsPerGroup["xxx"]++;
376 T->tree[n1].setGroup(group);
377 T->tree[n1].setChildren(-1,-1);
381 T->tree[n1].setBranchLength(readBranchLength(f));
383 T->tree[n1].setBranchLength(0.0);
386 while((c=f.get())!=0 && (c != ':' && c != ',' && c!=')') ) {;}
392 catch(exception& e) {
393 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
397 cout << "An unknown error has occurred in the ReadNewickTree class function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
401 /**************************************************************************************************/
402 /**************************************************************************************************/