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";
104 /***********************************************************************/
105 /***********************************************************************/
108 //Child Classes Below
110 /***********************************************************************/
111 /***********************************************************************/
112 //This class reads a file in Newick form and stores it in a tree.
114 void ReadNewickTree::read() {
119 //if you are not a nexus file
120 if ((c = filehandle.peek()) != '#') {
121 while((c = filehandle.peek()) != EOF) {
124 numNodes = T->getNumNodes();
125 numLeaves = T->getNumLeaves();
128 //save trees for later commands
129 globaldata->gTree.push_back(T);
132 //if you are a nexus file
133 }else if ((c = filehandle.peek()) == '#') {
134 nexusTranslation(); //reads file through the translation and updates treemap
135 while((c = filehandle.peek()) != EOF) {
137 while ((c = filehandle.peek()) != EOF) {
138 if(holder == "[" || holder == "[!"){
144 if((holder == "tree" || holder == "end;") && comment != 1){ holder = ""; comment = 0; break;}
145 filehandle >> holder;
148 //pass over the "tree rep.6878900 = "
149 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF) ) {;}
151 if (c == EOF ) { break; }
152 filehandle.putback(c); //put back first ( of tree.
156 numNodes = T->getNumNodes();
157 numLeaves = T->getNumLeaves();
161 //save trees for later commands
162 globaldata->gTree.push_back(T);
167 catch(exception& e) {
168 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
172 cout << "An unknown error has occurred in the ReadNewickTree class function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176 /**************************************************************************************************/
177 //This function read the file through the translation of the sequences names and updates treemap.
178 void ReadNewickTree::nexusTranslation() {
182 int numSeqs = globaldata->gTreemap->getNumSeqs(); //must save this some when we clear old names we can still know how many sequences there were
186 while(holder != "translate" && holder != "Translate"){
187 if(holder == "[" || holder == "[!"){
193 filehandle >> holder;
194 if(holder == "tree" && comment != 1){return;}
198 globaldata->gTreemap->namesOfSeqs.clear();
199 for(int i=0;i<numSeqs;i++){
201 filehandle >> number;
203 name.erase(name.end()-1); //erase the comma
204 //insert new one with new name
205 globaldata->gTreemap->treemap[toString(number)].groupname = globaldata->gTreemap->treemap[name].groupname;
206 globaldata->gTreemap->treemap[toString(number)].vectorIndex = globaldata->gTreemap->treemap[name].vectorIndex;
207 //erase old one. so treemap[sarah].groupnumber is now treemap[1].groupnumber. if number is 1 and name is sarah.
208 globaldata->gTreemap->treemap.erase(name);
209 globaldata->gTreemap->namesOfSeqs.push_back(number);
212 catch(exception& e) {
213 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function nexus. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
217 cout << "An unknown error has occurred in the ReadNewickTree class function nexus. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
222 /**************************************************************************************************/
223 void ReadNewickTree::readTreeString() {
231 int ch = filehandle.peek();
234 n = numLeaves; //number of leaves / sequences, we want node 1 to start where the leaves left off
236 lc = readNewickInt(filehandle, n, T);
238 if(filehandle.peek()==','){
239 readSpecialChar(filehandle,',',"comma");
241 // ';' means end of tree.
242 else if((ch=filehandle.peek())==';' || ch=='['){
246 rc = readNewickInt(filehandle, n, T);
247 if(filehandle.peek() == ')'){
248 readSpecialChar(filehandle,')',"right parenthesis");
252 //note: treeclimber had the code below added - not sure why?
254 filehandle.putback(ch);
256 filehandle.get(name, MAX_LINE,'\n');
257 SKIPLINE(filehandle, ch);
259 n = T->getIndex(name);
262 cerr << "Internal error: The only taxon is not taxon 0.\n";
268 while((ch=filehandle.get())!=';'){;}
270 T->tree[n].setChildren(lc,rc);
271 T->tree[n].setBranchLength(0);
272 T->tree[n].setParent(-1);
273 if(lc!=-1){ T->tree[lc].setParent(n); }
274 if(rc!=-1){ T->tree[rc].setParent(n); }
278 catch(exception& e) {
279 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function readTreeString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
283 cout << "An unknown error has occurred in the ReadNewickTree class function readTreeString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
288 /**************************************************************************************************/
290 int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T) {
292 int c = readNodeChar(f);
295 int lc = readNewickInt(f, n, T);
296 readSpecialChar(f,',',"comma");
298 int rc = readNewickInt(f, n, T);
300 readSpecialChar(f,')',"right parenthesis");
304 readSpecialChar(f,':',"colon");
305 if(n >= numNodes){ cerr << "Error: Too many nodes in input tree\n"; exit(1); }
306 T->tree[n].setBranchLength(readBranchLength(f));
307 }else{T->tree[n].setBranchLength(0.0); }
309 T->tree[n].setChildren(lc,rc);
310 T->tree[lc].setParent(n);
311 T->tree[rc].setParent(n);
318 while(d != ':' && d != ',' && d!=')' && d!='\n'){
324 if(d == ':') { blen = 1; }
329 string group = globaldata->gTreemap->getGroup(name);
331 //find index in tree of name
332 int n1 = T->getIndex(name);
334 //adds sequence names that are not in group file to the "xxx" group
336 cerr << "Name: " << name << " not found in your groupfile and it will be ignored. \n";
337 globaldata->gTreemap->namesOfSeqs.push_back(name);
338 globaldata->gTreemap->treemap[name].groupname = "xxx";
339 globaldata->gTreemap->treemap[name].vectorIndex = (globaldata->gTreemap->namesOfSeqs.size() - 1);
341 map<string, int>::iterator it;
342 it = globaldata->gTreemap->seqsPerGroup.find("xxx");
343 if (it == globaldata->gTreemap->seqsPerGroup.end()) { //its a new group
344 globaldata->gTreemap->namesOfGroups.push_back("xxx");
345 globaldata->gTreemap->seqsPerGroup["xxx"] = 1;
347 globaldata->gTreemap->seqsPerGroup["xxx"]++;
350 //find index in tree of name
351 n1 = T->getIndex(name);
355 T->tree[n1].setGroup(group);
357 T->tree[n1].setChildren(-1,-1);
361 T->tree[n1].setBranchLength(readBranchLength(f));
363 T->tree[n1].setBranchLength(0.0);
366 while((c=f.get())!=0 && (c != ':' && c != ',' && c!=')') ) {;}
372 catch(exception& e) {
373 cout << "Standard Error: " << e.what() << " has occurred in the ReadNewickTree class Function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377 cout << "An unknown error has occurred in the ReadNewickTree class function readNewickInt. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
381 /**************************************************************************************************/
382 /**************************************************************************************************/