2 * rawTrainingDataMaker.cpp
5 * Created by westcott on 4/21/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "phylosummary.h"
12 /**************************************************************************************************/
14 PhyloSummary::PhyloSummary(string refTfile, string groupFile){
16 m = MothurOut::getInstance();
19 if (groupFile != "") {
20 groupmap = new GroupMap(groupFile);
26 //check for necessary files
27 string taxFileNameTest = refTfile.substr(0,refTfile.find_last_of(".")+1) + "tree.sum";
28 ifstream FileTest(taxFileNameTest.c_str());
31 m->mothurOut("Error: can't find " + taxFileNameTest + "."); m->mothurOutEndLine(); exit(1);
33 readTreeStruct(FileTest);
41 m->errorOut(e, "PhyloSummary", "PhyloSummary");
45 /**************************************************************************************************/
47 void PhyloSummary::summarize(string userTfile){
51 openInputFile(userTfile, in);
53 //read in users taxonomy file and add sequences to tree
56 in >> name >> tax; gobble(in);
58 addSeqToTree(name, tax);
60 if (m->control_pressed) { break; }
65 m->errorOut(e, "PhyloSummary", "summarize");
70 /**************************************************************************************************/
72 string PhyloSummary::getNextTaxon(string& heirarchy){
74 string currentLevel = "";
76 int pos = heirarchy.find_first_of(';');
77 currentLevel=heirarchy.substr(0,pos);
78 if (pos != (heirarchy.length()-1)) { heirarchy=heirarchy.substr(pos+1); }
79 else { heirarchy = ""; }
84 m->errorOut(e, "PhyloSummary", "getNextTaxon");
89 /**************************************************************************************************/
91 int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){
95 map<string, int>::iterator childPointer;
102 while (seqTaxonomy != "") {
104 if (m->control_pressed) { return 0; }
106 //somehow the parent is getting one too many accnos
107 //use print to reassign the taxa id
108 taxon = getNextTaxon(seqTaxonomy);
110 childPointer = tree[currentNode].children.find(taxon);
112 if(childPointer != tree[currentNode].children.end()){ //if the node already exists, update count and move on
113 if (groupmap != NULL) {
114 //find out the sequences group
115 string group = groupmap->getGroup(seqName);
117 //do you have a count for this group?
118 map<string, int>::iterator itGroup = tree[currentNode].groupCount.find(group);
120 //if yes, increment it - there should not be a case where we can't find it since we load group in read
121 if (itGroup != tree[currentNode].groupCount.end()) {
122 tree[currentNode].groupCount[group]++;
126 tree[currentNode].total++;
128 currentNode = childPointer->second;
129 }else{ //otherwise, create it
130 m->mothurOut("Error: cannot find taxonomy in tree for " + seqName + "."); m->mothurOutEndLine();
136 if ((seqTaxonomy == "") && (level < maxLevel)) { //if you think you are done and you are not.
137 for (int k = level; k < maxLevel; k++) { seqTaxonomy += "unclassified;"; }
142 catch(exception& e) {
143 m->errorOut(e, "PhyloSummary", "addSeqToTree");
147 /**************************************************************************************************/
149 void PhyloSummary::assignRank(int index){
151 map<string,int>::iterator it;
154 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){
155 tree[it->second].rank = tree[index].rank + '.' + toString(counter);
158 assignRank(it->second);
161 catch(exception& e) {
162 m->errorOut(e, "PhyloSummary", "assignRank");
166 /**************************************************************************************************/
168 void PhyloSummary::print(ofstream& out){
171 out << "taxlevel\t rank ID\t label\t daughterlevels\t total\t";
172 if (groupmap != NULL) {
173 for (int i = 0; i < groupmap->namesOfGroups.size(); i++) {
174 out << groupmap->namesOfGroups[i] << '\t';
181 out << tree[0].level << "\t" << tree[0].rank << "\t" << tree[0].name << "\t" << tree[0].children.size() << "\t" << tree[0].total << "\t";
183 map<string, int>::iterator itGroup;
184 if (groupmap != NULL) {
185 for (itGroup = tree[0].groupCount.begin(); itGroup != tree[0].groupCount.end(); itGroup++) {
186 out << itGroup->second << '\t';
195 catch(exception& e) {
196 m->errorOut(e, "PhyloSummary", "print");
201 /**************************************************************************************************/
203 void PhyloSummary::print(int i, ofstream& out){
205 map<string,int>::iterator it;
206 for(it=tree[i].children.begin();it!=tree[i].children.end();it++){
208 if (tree[it->second].total != 0) {
209 out << tree[it->second].level << "\t" << tree[it->second].rank << "\t" << tree[it->second].name << "\t" << tree[it->second].children.size() << "\t" << tree[it->second].total << "\t";
211 map<string, int>::iterator itGroup;
212 if (groupmap != NULL) {
213 for (itGroup = tree[it->second].groupCount.begin(); itGroup != tree[it->second].groupCount.end(); itGroup++) {
214 out << itGroup->second << '\t';
220 print(it->second, out);
223 catch(exception& e) {
224 m->errorOut(e, "PhyloSummary", "print");
228 /**************************************************************************************************/
229 void PhyloSummary::readTreeStruct(ifstream& in){
233 in >> num; gobble(in);
238 for (int i = 0; i < tree.size(); i++) {
240 in >> tree[i].level >> tree[i].name >> num; //num contains the number of children tree[i] has
245 for (int j = 0; j < num; j++) {
246 in >> childName >> childIndex;
247 tree[i].children[childName] = childIndex;
250 //initialize groupcounts
251 if (groupmap != NULL) {
252 for (int j = 0; j < groupmap->namesOfGroups.size(); j++) {
253 tree[i].groupCount[groupmap->namesOfGroups[j]] = 0;
261 if (tree[i].level > maxLevel) { maxLevel = tree[i].level; }
265 catch(exception& e) {
266 m->errorOut(e, "PhyloSummary", "print");
271 /**************************************************************************************************/