/***********************************************************************/
ReadTree::ReadTree() {
try {
- globaldata = GlobalData::getInstance();
m = MothurOut::getInstance();
- globaldata->gTree.clear();
}
catch(exception& e) {
m->errorOut(e, "ReadTree", "ReadTree");
}
}
/***********************************************************************/
+int ReadTree::AssembleTrees() {
+ try {
+ //assemble users trees
+ for (int i = 0; i < Trees.size(); i++) {
+ if (m->control_pressed) { return 0; }
+ Trees[i]->assembleTree();
+ }
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "ReadTree", "AssembleTrees");
+ exit(1);
+ }
+}
+/***********************************************************************/
int ReadTree::readSpecialChar(istream& f, char c, string name) {
try {
- gobble(f);
+ m->gobble(f);
char d = f.get();
if(d == EOF){
exit(1);
}
if(d == ')' && f.peek() == '\n'){
- gobble(f);
+ m->gobble(f);
}
return d;
}
int ReadTree::readNodeChar(istream& f) {
try {
// while(isspace(d=f.get())) {;}
- gobble(f);
+ m->gobble(f);
char d = f.get();
if(d == EOF){
m->mothurOut("Error: Missing branch length in input tree.\n");
exit(1);
}
- gobble(f);
+ m->gobble(f);
return b;
}
catch(exception& e) {
/***********************************************************************/
//This class reads a file in Newick form and stores it in a tree.
-int ReadNewickTree::read() {
+int ReadNewickTree::read(TreeMap* tmap) {
try {
holder = "";
int c, error;
}
//make new tree
- T = new Tree();
+ T = new Tree(tmap);
numNodes = T->getNumNodes();
numLeaves = T->getNumLeaves();
- error = readTreeString();
+ error = readTreeString(tmap);
//save trees for later commands
- globaldata->gTree.push_back(T);
- gobble(filehandle);
+ Trees.push_back(T);
+ m->gobble(filehandle);
}
//if you are a nexus file
}else if ((c = filehandle.peek()) == '#') {
- nexusTranslation(); //reads file through the translation and updates treemap
+ //get right number of seqs from nexus file.
+ Tree* temp = new Tree(tmap); delete temp;
+
+ nexusTranslation(tmap); //reads file through the translation and updates treemap
while((c = filehandle.peek()) != EOF) {
// get past comments
while ((c = filehandle.peek()) != EOF) {
filehandle.putback(c); //put back first ( of tree.
//make new tree
- T = new Tree();
+ T = new Tree(tmap);
numNodes = T->getNumNodes();
numLeaves = T->getNumLeaves();
//read tree info
- error = readTreeString();
+ error = readTreeString(tmap);
//save trees for later commands
- globaldata->gTree.push_back(T);
+ Trees.push_back(T);
}
}
}
/**************************************************************************************************/
//This function read the file through the translation of the sequences names and updates treemap.
-void ReadNewickTree::nexusTranslation() {
+string ReadNewickTree::nexusTranslation(TreeMap* tmap) {
try {
holder = "";
- int numSeqs = globaldata->gTreemap->getNumSeqs(); //must save this some when we clear old names we can still know how many sequences there were
+ int numSeqs = m->Treenames.size(); //must save this some when we clear old names we can still know how many sequences there were
int comment = 0;
// get past comments
comment = 0;
}
filehandle >> holder;
- if(holder == "tree" && comment != 1){return;}
+ if(holder == "tree" && comment != 1){return holder;}
}
//update treemap
- globaldata->gTreemap->namesOfSeqs.clear();
+ tmap->namesOfSeqs.clear();
+
+ /*char c;
+ string number, name;
+ while ((c = filehandle.peek()) != EOF) {
+
+ filehandle >> number;
+
+ if ((number == "tree") || (number == ";") ) { name = number; break; }
+
+ filehandle >> name;
+
+ char lastChar;
+ if (name.length() != 0) { lastChar = name[name.length()-1]; }
+
+ if ((name == "tree") || (name == ";") ) { break; }
+
+ if (lastChar == ',') { name.erase(name.end()-1); } //erase the comma
+ */
+
+ string number, name;
for(int i=0;i<numSeqs;i++){
- string number, name;
+
filehandle >> number;
filehandle >> name;
name.erase(name.end()-1); //erase the comma
+
//insert new one with new name
- globaldata->gTreemap->treemap[toString(number)].groupname = globaldata->gTreemap->treemap[name].groupname;
- globaldata->gTreemap->treemap[toString(number)].vectorIndex = globaldata->gTreemap->treemap[name].vectorIndex;
+ string group = tmap->getGroup(name);
+ tmap->treemap[toString(number)].groupname = group;
+ tmap->treemap[toString(number)].vectorIndex = tmap->getIndex(name);
//erase old one. so treemap[sarah].groupnumber is now treemap[1].groupnumber. if number is 1 and name is sarah.
- globaldata->gTreemap->treemap.erase(name);
- globaldata->gTreemap->namesOfSeqs.push_back(number);
+ tmap->treemap.erase(name);
+ tmap->namesOfSeqs.push_back(number);
}
+
+ return name;
}
catch(exception& e) {
m->errorOut(e, "ReadNewickTree", "nexusTranslation");
}
/**************************************************************************************************/
-int ReadNewickTree::readTreeString() {
+int ReadNewickTree::readTreeString(TreeMap* tmap) {
try {
int n = 0;
if(ch == '('){
n = numLeaves; //number of leaves / sequences, we want node 1 to start where the leaves left off
- lc = readNewickInt(filehandle, n, T);
+ lc = readNewickInt(filehandle, n, T, tmap);
if (lc == -1) { m->mothurOut("error with lc"); m->mothurOutEndLine(); return -1; } //reports an error in reading
if(filehandle.peek()==','){
}
if(rooted != 1){
- rc = readNewickInt(filehandle, n, T);
+ rc = readNewickInt(filehandle, n, T, tmap);
if (rc == -1) { m->mothurOut("error with rc"); m->mothurOutEndLine(); return -1; } //reports an error in reading
if(filehandle.peek() == ')'){
readSpecialChar(filehandle,')',"right parenthesis");
}
/**************************************************************************************************/
-int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T) {
+int ReadNewickTree::readNewickInt(istream& f, int& n, Tree* T, TreeMap* tmap) {
try {
if (m->control_pressed) { return -1; }
//read all children
vector<int> childrenNodes;
while(f.peek() != ')'){
- int child = readNewickInt(f, n, T);
+ int child = readNewickInt(f, n, T, tmap);
if (child == -1) { return -1; } //reports an error in reading
//cout << "child = " << child << endl;
childrenNodes.push_back(child);
if(f.peek() == ':'){
readSpecialChar(f,':',"colon");
- if(n >= numNodes){ m->mothurOut("Error: Too many nodes in input tree\n"); readOk = -1; return -1; }
+ if(n >= numNodes){ m->mothurOut("Error: Too many nodes in input tree\n"); readOk = -1; return -1; }
T->tree[n].setBranchLength(readBranchLength(f));
}else{
f.putback(d);
//set group info
- string group = globaldata->gTreemap->getGroup(name);
+ string group = tmap->getGroup(name);
//find index in tree of name
int n1 = T->getIndex(name);
if(group == "not found") {
m->mothurOut("Name: " + name + " is not in your groupfile, and will be disregarded. \n"); //readOk = -1; return n1;
- globaldata->gTreemap->namesOfSeqs.push_back(name);
- globaldata->gTreemap->treemap[name].groupname = "xxx";
+ tmap->namesOfSeqs.push_back(name);
+ tmap->treemap[name].groupname = "xxx";
map<string, int>::iterator it;
- it = globaldata->gTreemap->seqsPerGroup.find("xxx");
- if (it == globaldata->gTreemap->seqsPerGroup.end()) { //its a new group
- globaldata->gTreemap->namesOfGroups.push_back("xxx");
- globaldata->gTreemap->seqsPerGroup["xxx"] = 1;
+ it = tmap->seqsPerGroup.find("xxx");
+ if (it == tmap->seqsPerGroup.end()) { //its a new group
+ tmap->namesOfGroups.push_back("xxx");
+ tmap->seqsPerGroup["xxx"] = 1;
}else {
- globaldata->gTreemap->seqsPerGroup["xxx"]++;
+ tmap->seqsPerGroup["xxx"]++;
}
group = "xxx";