1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
/*
* rawTrainingDataMaker.cpp
* Mothur
*
* Created by westcott on 4/21/10.
* Copyright 2010 Schloss Lab. All rights reserved.
*
*/
#include "phylosummary.h"
/**************************************************************************************************/
PhyloSummary::PhyloSummary(string refTfile, string groupFile){
try {
m = MothurOut::getInstance();
maxLevel = 0;
ignore = false;
if (groupFile != "") {
groupmap = new GroupMap(groupFile);
groupmap->readMap();
}else{
groupmap = NULL;
}
//check for necessary files
string taxFileNameTest = refTfile.substr(0,refTfile.find_last_of(".")+1) + "tree.sum";
ifstream FileTest(taxFileNameTest.c_str());
if (!FileTest) {
m->mothurOut("Error: can't find " + taxFileNameTest + "."); m->mothurOutEndLine(); exit(1);
}else{
readTreeStruct(FileTest);
}
tree[0].rank = "0";
assignRank(0);
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "PhyloSummary");
exit(1);
}
}
/**************************************************************************************************/
PhyloSummary::PhyloSummary(string groupFile){
try {
m = MothurOut::getInstance();
maxLevel = 0;
ignore = true;
if (groupFile != "") {
groupmap = new GroupMap(groupFile);
groupmap->readMap();
}else{
groupmap = NULL;
}
tree.push_back(rawTaxNode("Root"));
tree[0].rank = "0";
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "PhyloSummary");
exit(1);
}
}
/**************************************************************************************************/
int PhyloSummary::summarize(string userTfile){
try {
ifstream in;
m->openInputFile(userTfile, in);
//read in users taxonomy file and add sequences to tree
string name, tax;
int numSeqs = 0;
while(!in.eof()){
in >> name >> tax; m->gobble(in);
addSeqToTree(name, tax);
numSeqs++;
if (m->control_pressed) { break; }
}
in.close();
return numSeqs;
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "summarize");
exit(1);
}
}
/**************************************************************************************************/
string PhyloSummary::getNextTaxon(string& heirarchy){
try {
string currentLevel = "";
if(heirarchy != ""){
int pos = heirarchy.find_first_of(';');
currentLevel=heirarchy.substr(0,pos);
if (pos != (heirarchy.length()-1)) { heirarchy=heirarchy.substr(pos+1); }
else { heirarchy = ""; }
}
return currentLevel;
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "getNextTaxon");
exit(1);
}
}
/**************************************************************************************************/
int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){
try {
numSeqs++;
map<string, int>::iterator childPointer;
int currentNode = 0;
string taxon;
int level = 0;
//are there confidence scores, if so remove them
if (seqTaxonomy.find_first_of('(') != -1) { m->removeConfidences(seqTaxonomy); }
while (seqTaxonomy != "") {
if (m->control_pressed) { return 0; }
//somehow the parent is getting one too many accnos
//use print to reassign the taxa id
taxon = getNextTaxon(seqTaxonomy);
childPointer = tree[currentNode].children.find(taxon);
if(childPointer != tree[currentNode].children.end()){ //if the node already exists, update count and move on
if (groupmap != NULL) {
//find out the sequences group
string group = groupmap->getGroup(seqName);
if (group == "not found") { m->mothurOut("[WARNING]: " + seqName + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine(); }
//do you have a count for this group?
map<string, int>::iterator itGroup = tree[childPointer->second].groupCount.find(group);
//if yes, increment it - there should not be a case where we can't find it since we load group in read
if (itGroup != tree[childPointer->second].groupCount.end()) {
tree[childPointer->second].groupCount[group]++;
}
}
tree[childPointer->second].total++;
currentNode = childPointer->second;
}else{
if (ignore) {
tree.push_back(rawTaxNode(taxon));
int index = tree.size() - 1;
tree[index].parent = currentNode;
tree[index].level = (level+1);
tree[index].total = 1;
tree[currentNode].children[taxon] = index;
//initialize groupcounts
if (groupmap != NULL) {
vector<string> mGroups = groupmap->getNamesOfGroups();
for (int j = 0; j < mGroups.size(); j++) {
tree[index].groupCount[mGroups[j]] = 0;
}
//find out the sequences group
string group = groupmap->getGroup(seqName);
if (group == "not found") { m->mothurOut("[WARNING]: " + seqName + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine(); }
//do you have a count for this group?
map<string, int>::iterator itGroup = tree[index].groupCount.find(group);
//if yes, increment it - there should not be a case where we can't find it since we load group in read
if (itGroup != tree[index].groupCount.end()) {
tree[index].groupCount[group]++;
}
}
currentNode = index;
}else{ //otherwise, error
m->mothurOut("Warning: cannot find taxon " + taxon + " in reference taxonomy tree at level " + toString(tree[currentNode].level) + " for " + seqName + ". This may cause totals of daughter levels not to add up in summary file."); m->mothurOutEndLine();
break;
}
}
level++;
if ((seqTaxonomy == "") && (level < maxLevel)) { //if you think you are done and you are not.
for (int k = level; k < maxLevel; k++) { seqTaxonomy += "unclassified;"; }
}
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "addSeqToTree");
exit(1);
}
}
/**************************************************************************************************/
int PhyloSummary::addSeqToTree(string seqTaxonomy, vector<string> names){
try {
numSeqs++;
map<string, int>::iterator childPointer;
int currentNode = 0;
string taxon;
int level = 0;
//are there confidence scores, if so remove them
if (seqTaxonomy.find_first_of('(') != -1) { m->removeConfidences(seqTaxonomy); }
while (seqTaxonomy != "") {
if (m->control_pressed) { return 0; }
//somehow the parent is getting one too many accnos
//use print to reassign the taxa id
taxon = getNextTaxon(seqTaxonomy);
childPointer = tree[currentNode].children.find(taxon);
if(childPointer != tree[currentNode].children.end()){ //if the node already exists, update count and move on
if (groupmap != NULL) {
map<string, bool> containsGroup;
vector<string> mGroups = groupmap->getNamesOfGroups();
for (int j = 0; j < mGroups.size(); j++) {
containsGroup[mGroups[j]] = false;
}
for (int k = 0; k < names.size(); k++) {
//find out the sequences group
string group = groupmap->getGroup(names[k]);
if (group == "not found") { m->mothurOut("[WARNING]: " + names[k] + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine(); }
else {
containsGroup[group] = true;
}
}
for (map<string, bool>::iterator itGroup = containsGroup.begin(); itGroup != containsGroup.end(); itGroup++) {
if (itGroup->second == true) {
tree[childPointer->second].groupCount[itGroup->first]++;
}
}
}
tree[childPointer->second].total++;
currentNode = childPointer->second;
}else{
if (ignore) {
tree.push_back(rawTaxNode(taxon));
int index = tree.size() - 1;
tree[index].parent = currentNode;
tree[index].level = (level+1);
tree[index].total = 1;
tree[currentNode].children[taxon] = index;
//initialize groupcounts
if (groupmap != NULL) {
map<string, bool> containsGroup;
vector<string> mGroups = groupmap->getNamesOfGroups();
for (int j = 0; j < mGroups.size(); j++) {
tree[index].groupCount[mGroups[j]] = 0;
containsGroup[mGroups[j]] = false;
}
for (int k = 0; k < names.size(); k++) {
//find out the sequences group
string group = groupmap->getGroup(names[k]);
if (group == "not found") { m->mothurOut("[WARNING]: " + names[k] + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine(); }
else {
containsGroup[group] = true;
}
}
for (map<string, bool>::iterator itGroup = containsGroup.begin(); itGroup != containsGroup.end(); itGroup++) {
if (itGroup->second == true) {
tree[index].groupCount[itGroup->first]++;
}
}
}
currentNode = index;
}else{ //otherwise, error
m->mothurOut("Warning: cannot find taxon " + taxon + " in reference taxonomy tree at level " + toString(tree[currentNode].level) + ". This may cause totals of daughter levels not to add up in summary file."); m->mothurOutEndLine();
break;
}
}
level++;
if ((seqTaxonomy == "") && (level < maxLevel)) { //if you think you are done and you are not.
for (int k = level; k < maxLevel; k++) { seqTaxonomy += "unclassified;"; }
}
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "addSeqToTree");
exit(1);
}
}
/**************************************************************************************************/
void PhyloSummary::assignRank(int index){
try {
map<string,int>::iterator it;
int counter = 1;
for(it=tree[index].children.begin();it!=tree[index].children.end();it++){
tree[it->second].rank = tree[index].rank + '.' + toString(counter);
counter++;
assignRank(it->second);
}
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "assignRank");
exit(1);
}
}
/**************************************************************************************************/
void PhyloSummary::print(ofstream& out){
try {
if (ignore) { assignRank(0); }
//print labels
out << "taxlevel\t rankID\t taxon\t daughterlevels\t total\t";
if (groupmap != NULL) {
//so the labels match the counts below, since the map sorts them automatically...
//sort(groupmap->namesOfGroups.begin(), groupmap->namesOfGroups.end());
vector<string> mGroups = groupmap->getNamesOfGroups();
for (int i = 0; i < mGroups.size(); i++) {
out << mGroups[i] << '\t';
}
}
out << endl;
int totalChildrenInTree = 0;
map<string, int>::iterator itGroup;
map<string,int>::iterator it;
for(it=tree[0].children.begin();it!=tree[0].children.end();it++){
if (tree[it->second].total != 0) {
totalChildrenInTree++;
tree[0].total += tree[it->second].total;
if (groupmap != NULL) {
vector<string> mGroups = groupmap->getNamesOfGroups();
for (int i = 0; i < mGroups.size(); i++) { tree[0].groupCount[mGroups[i]] += tree[it->second].groupCount[mGroups[i]]; }
}
}
}
//print root
out << tree[0].level << "\t" << tree[0].rank << "\t" << tree[0].name << "\t" << totalChildrenInTree << "\t" << tree[0].total << "\t";
if (groupmap != NULL) {
//for (itGroup = tree[0].groupCount.begin(); itGroup != tree[0].groupCount.end(); itGroup++) {
// out << itGroup->second << '\t';
//}
vector<string> mGroups = groupmap->getNamesOfGroups();
for (int i = 0; i < mGroups.size(); i++) { out << tree[0].groupCount[mGroups[i]] << '\t'; }
}
out << endl;
//print rest
print(0, out);
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "print");
exit(1);
}
}
/**************************************************************************************************/
void PhyloSummary::print(int i, ofstream& out){
try {
map<string,int>::iterator it;
for(it=tree[i].children.begin();it!=tree[i].children.end();it++){
if (tree[it->second].total != 0) {
int totalChildrenInTree = 0;
map<string,int>::iterator it2;
for(it2=tree[it->second].children.begin();it2!=tree[it->second].children.end();it2++){
if (tree[it2->second].total != 0) { totalChildrenInTree++; }
}
out << tree[it->second].level << "\t" << tree[it->second].rank << "\t" << tree[it->second].name << "\t" << totalChildrenInTree << "\t" << tree[it->second].total << "\t";
map<string, int>::iterator itGroup;
if (groupmap != NULL) {
//for (itGroup = tree[it->second].groupCount.begin(); itGroup != tree[it->second].groupCount.end(); itGroup++) {
// out << itGroup->second << '\t';
//}
vector<string> mGroups = groupmap->getNamesOfGroups();
for (int i = 0; i < mGroups.size(); i++) { out << tree[it->second].groupCount[mGroups[i]] << '\t'; }
}
out << endl;
}
print(it->second, out);
}
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "print");
exit(1);
}
}
/**************************************************************************************************/
void PhyloSummary::readTreeStruct(ifstream& in){
try {
//read version
string line = m->getline(in); m->gobble(in);
int num;
in >> num; m->gobble(in);
tree.resize(num);
in >> maxLevel; m->gobble(in);
//read the tree file
for (int i = 0; i < tree.size(); i++) {
in >> tree[i].level >> tree[i].name >> num; //num contains the number of children tree[i] has
//set children
string childName;
int childIndex;
for (int j = 0; j < num; j++) {
in >> childName >> childIndex;
tree[i].children[childName] = childIndex;
}
//initialize groupcounts
if (groupmap != NULL) {
for (int j = 0; j < (groupmap->getNamesOfGroups()).size(); j++) {
tree[i].groupCount[(groupmap->getNamesOfGroups())[j]] = 0;
}
}
tree[i].total = 0;
m->gobble(in);
//if (tree[i].level > maxLevel) { maxLevel = tree[i].level; }
}
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "readTreeStruct");
exit(1);
}
}
/**************************************************************************************************/
|