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; }
184 catch(exception& e) {
185 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189 cout << "An unknown error has occurred in the ReadNewickTree class function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 /**************************************************************************************************/
194 //This function read the file through the translation of the sequences names and updates treemap.
195 void ReadNewickTree::nexusTranslation() {
199 int numSeqs = globaldata->gTreemap->getNumSeqs(); //must save this some when we clear old names we can still know how many sequences there were
203 while(holder != "translate" && holder != "Translate"){
204 if(holder == "[" || holder == "[!"){
210 filehandle >> holder;
211 if(holder == "tree" && comment != 1){return;}
215 globaldata->gTreemap->namesOfSeqs.clear();
216 for(int i=0;i<numSeqs;i++){
218 filehandle >> number;
220 name.erase(name.end()-1); //erase the comma
221 //insert new one with new name
222 globaldata->gTreemap->treemap[toString(number)].groupname = globaldata->gTreemap->treemap[name].groupname;
223 globaldata->gTreemap->treemap[toString(number)].vectorIndex = globaldata->gTreemap->treemap[name].vectorIndex;
224 //erase old one. so treemap[sarah].groupnumber is now treemap[1].groupnumber. if number is 1 and name is sarah.
225 globaldata->gTreemap->treemap.erase(name);
226 globaldata->gTreemap->namesOfSeqs.push_back(number);
229 catch(exception& e) {
230 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function nexus. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
234 cout << "An unknown error has occurred in the ReadNewickTree class function nexus. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239 /**************************************************************************************************/
240 int ReadNewickTree::readTreeString() {
248 int ch = filehandle.peek();
251 n = numLeaves; //number of leaves / sequences, we want node 1 to start where the leaves left off
253 lc = readNewickInt(filehandle, n, T);
254 if (lc == -1) { cout << "error with lc" << endl; return -1; } //reports an error in reading
256 if(filehandle.peek()==','){
257 readSpecialChar(filehandle,',',"comma");
259 // ';' means end of tree.
260 else if((ch=filehandle.peek())==';' || ch=='['){
264 rc = readNewickInt(filehandle, n, T);
265 if (rc == -1) { cout << "error with rc" << endl; return -1; } //reports an error in reading
266 if(filehandle.peek() == ')'){
267 readSpecialChar(filehandle,')',"right parenthesis");
271 //note: treeclimber had the code below added - not sure why?
273 filehandle.putback(ch);
275 filehandle.get(name, MAX_LINE,'\n');
276 SKIPLINE(filehandle, ch);
278 n = T->getIndex(name);
281 cerr << "Internal error: The only taxon is not taxon 0.\n";
283 readOk = -1; return -1;
288 while((ch=filehandle.get())!=';'){;}
290 T->tree[n].setChildren(lc,rc);
291 T->tree[n].setBranchLength(0);
292 T->tree[n].setParent(-1);
293 if(lc!=-1){ T->tree[lc].setParent(n); }
294 if(rc!=-1){ T->tree[rc].setParent(n); }
299 catch(exception& e) {
300 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function readTreeString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
304 cout << "An unknown error has occurred in the ReadNewickTree class function readTreeString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
309 /**************************************************************************************************/
311 int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T) {
313 int c = readNodeChar(f);
316 int lc = readNewickInt(f, n, T);
317 if (lc == -1) { return -1; } //reports an error in reading
318 readSpecialChar(f,',',"comma");
320 int rc = readNewickInt(f, n, T);
321 if (rc == -1) { return -1; } //reports an error in reading
323 readSpecialChar(f,')',"right parenthesis");
327 readSpecialChar(f,':',"colon");
329 if(n >= numNodes){ cerr << "Error: Too many nodes in input tree\n"; readOk = -1; return -1; }
331 T->tree[n].setBranchLength(readBranchLength(f));
332 }else{T->tree[n].setBranchLength(0.0); }
334 T->tree[n].setChildren(lc,rc);
335 T->tree[lc].setParent(n);
336 T->tree[rc].setParent(n);
343 while(d != ':' && d != ',' && d!=')' && d!='\n'){
349 if(d == ':') { blen = 1; }
354 string group = globaldata->gTreemap->getGroup(name);
356 //find index in tree of name
357 int n1 = T->getIndex(name);
359 //adds sequence names that are not in group file to the "xxx" group
360 if(group == "not found") {
361 cout << "Name: " << name << " is not in your groupfile, and will be disregarded. \n"; //readOk = -1; return n1;
363 globaldata->gTreemap->namesOfSeqs.push_back(name);
364 globaldata->gTreemap->treemap[name].groupname = "xxx";
366 map<string, int>::iterator it;
367 it = globaldata->gTreemap->seqsPerGroup.find("xxx");
368 if (it == globaldata->gTreemap->seqsPerGroup.end()) { //its a new group
369 globaldata->gTreemap->namesOfGroups.push_back("xxx");
370 globaldata->gTreemap->seqsPerGroup["xxx"] = 1;
372 globaldata->gTreemap->seqsPerGroup["xxx"]++;
378 T->tree[n1].setGroup(group);
379 T->tree[n1].setChildren(-1,-1);
383 T->tree[n1].setBranchLength(readBranchLength(f));
385 T->tree[n1].setBranchLength(0.0);
388 while((c=f.get())!=0 && (c != ':' && c != ',' && c!=')') ) {;}
394 catch(exception& e) {
395 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
399 cout << "An unknown error has occurred in the ReadNewickTree class function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
403 /**************************************************************************************************/
404 /**************************************************************************************************/