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
|
//
// picrust.cpp
// Mothur
//
// Created by Sarah Westcott on 11/16/20.
// Copyright © 2020 Schloss Lab. All rights reserved.
//
#include "picrust.hpp"
/**************************************************************************************************/
Picrust::Picrust(string ref, string otumapfile){
try {
m = MothurOut::getInstance();
phyloTree = nullptr;
read(ref, otumapfile);
}
catch(exception& e) {
m->errorOut(e, "Picrust", "Picrust");
exit(1);
}
}
/**************************************************************************************************/
Picrust::Picrust(){
try {
m = MothurOut::getInstance();
phyloTree = nullptr;
}
catch(exception& e) {
m->errorOut(e, "Picrust", "Picrust");
exit(1);
}
}
/**************************************************************************************************/
Picrust::~Picrust(){
try {
if (phyloTree != nullptr) { delete phyloTree; }
}
catch(exception& e) {
m->errorOut(e, "Picrust", "Picrust");
exit(1);
}
}
/**************************************************************************************************/
void Picrust::read(string ref, string otumapfile){
try {
//read reftaxonomy
phyloTree = new PhyloTree(ref);
//read otu map file
readGGOtuMap(otumapfile); //maps reference ID -> OTU ID
}
catch(exception& e) {
m->errorOut(e, "Picrust", "read");
exit(1);
}
}
//**********************************************************************************************************************
void Picrust::setGGOTUIDs(map<string, string>& labelTaxMap, SharedRAbundFloatVectors*& lookup){
try {
map<string, vector<string> > ggOTUIDs;
//loop through otu taxonomies
for (map<string, string>::iterator it = labelTaxMap.begin(); it != labelTaxMap.end(); it++) { //maps label -> consensus taxonomy
if (m->getControl_pressed()) { break; }
string OTUTaxonomy = it->second;
//remove confidences
util.removeConfidences(OTUTaxonomy);
//remove unclassifieds to match template
int thisPos = OTUTaxonomy.find("unclassified;"); //"Porphyromonadaceae"_unclassified;
if (thisPos != string::npos) {
OTUTaxonomy = OTUTaxonomy.substr(0, thisPos);
thisPos = OTUTaxonomy.find_last_of(";"); //remove rest of parent taxon
if (thisPos != string::npos) {
OTUTaxonomy = OTUTaxonomy.substr(0, thisPos+1);
}
}
//get list of reference ids that map to this taxonomy
vector<string> referenceIds = phyloTree->getSeqs(OTUTaxonomy);
if (m->getControl_pressed()) { break; }
//look for each one in otu map to find match
string otuID = "not found";
string referenceString = "";
for (int i = 0; i < referenceIds.size(); i++) {
referenceString += referenceIds[i] + " ";
map<string, string>::iterator itMap = otuMap.find(referenceIds[i]);
if (itMap != otuMap.end()) { //found it
otuID = itMap->second;
i += referenceIds.size(); //stop looking
}
}
//if found, add otu to ggOTUID list
if (otuID != "not found") {
map<string, vector<string> >::iterator itGG = ggOTUIDs.find(otuID);
if (itGG == ggOTUIDs.end()) {
vector<string> temp; temp.push_back(it->first); //save mothur OTU label
ggOTUIDs[otuID] = temp;
}else { ggOTUIDs[otuID].push_back(it->first); } //add mothur OTU label to list
}else { m->mothurOut("[ERROR]: could not find OTUId for " + it->second + ". Its reference sequences are " + referenceString + ".\n"); m->setControl_pressed(true); }
}
vector<SharedRAbundFloatVector*> newLookup;
vector<string> namesOfGroups = lookup->getNamesGroups();
for (int i = 0; i < namesOfGroups.size(); i++) {
SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
temp->setLabel(lookup->getLabel());
temp->setGroup(namesOfGroups[i]);
newLookup.push_back(temp);
}
map<string, int> labelIndex;
vector<string> currentLabels = lookup->getOTUNames();
for (int i = 0; i < currentLabels.size(); i++) { labelIndex[util.getSimpleLabel(currentLabels[i])] = i; }
vector<string> newBinLabels;
map<string, string> newLabelTaxMap;
//loop through ggOTUID list combining mothur otus and adjusting labels
//ggOTUIDs = 16097 -> <OTU01, OTU10, OTU22>
for (map<string, vector<string> >::iterator itMap = ggOTUIDs.begin(); itMap != ggOTUIDs.end(); itMap++) {
if (m->getControl_pressed()) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return; }
//set new gg otu id to taxonomy. OTU01 -> k__Bacteria becomes 16097 -> k__Bacteria
//find taxonomy of this otu
map<string, string>::iterator it = labelTaxMap.find(util.getSimpleLabel(itMap->second[0]));
vector<string> scores;
vector<string> taxonomies = util.parseTax(it->second, scores);
//merge/set OTU abundances
vector<float> abunds; abunds.resize(lookup->size(), 0.0);
string mergeString = "";
vector<float> boots; boots.resize(scores.size(), 0.0);
bool scoresnullptr = false;
for (int j = 0; j < itMap->second.size(); j++) { //<OTU01, OTU10, OTU22>
if (scores[0] != "null") {
//merge bootstrap scores
vector<string> scores;
vector<string> taxonomies = util.parseTax(it->second, scores);
for (int i = 0; i < boots.size(); i++) {
if (scores[i] == "null") { scoresnullptr = true; break; }
else {
float tempScore; util.mothurConvert(scores[i], tempScore);
boots[i] += tempScore;
}
}
}else { scoresnullptr = true; }
//merge abunds
mergeString += (itMap->second)[j] + " ";
for (int i = 0; i < lookup->size(); i++) { abunds[i] += lookup->get(labelIndex[util.getSimpleLabel((itMap->second)[j])], namesOfGroups[i]); }
}
if (m->getDebug()) { m->mothurOut("[DEBUG]: merging " + mergeString + " for ggOTUid = " + itMap->first + ".\n"); }
//average scores
//add merged otu to new lookup
string newTaxString = "";
if (!scoresnullptr) {
for (int j = 0; j < boots.size(); j++) { boots[j] /= (float) itMap->second.size(); }
//assemble new taxomoy
for (int j = 0; j < boots.size(); j++) {
newTaxString += taxonomies[j] + "(" + toString(boots[j]) + ");";
}
}else {
//assemble new taxomoy
for (int j = 0; j < taxonomies.size(); j++) {
newTaxString += taxonomies[j] + ";";
}
}
//set new gg otu id to taxonomy. OTU01 -> k__Bacteria becomes 16097 -> k__Bacteria
//find taxonomy of this otu
newLabelTaxMap[itMap->first] = newTaxString;
//add merged otu to new lookup
for (int j = 0; j < abunds.size(); j++) { newLookup[j]->push_back(abunds[j]); }
//saved otu label
newBinLabels.push_back(itMap->first);
}
lookup->clear();
for (int i = 0; i < newLookup.size(); i++) { lookup->push_back(newLookup[i]); }
lookup->eliminateZeroOTUS();
lookup->setOTUNames(newBinLabels);
labelTaxMap = newLabelTaxMap;
return;
}
catch(exception& e) {
m->errorOut(e, "Picrust", "setGGOTUIDs");
exit(1);
}
}
//**********************************************************************************************************************
void Picrust::setGGOTUIDs(map<string, string>& labelTaxMap, SharedRAbundVectors*& lookup){
try {
map<string, vector<string> > ggOTUIDs;
//loop through otu taxonomies
for (map<string, string>::iterator it = labelTaxMap.begin(); it != labelTaxMap.end(); it++) { //maps label -> consensus taxonomy
if (m->getControl_pressed()) { break; }
string OTUTaxonomy = it->second;
//remove confidences
util.removeConfidences(OTUTaxonomy);
//remove unclassifieds to match template
int thisPos = OTUTaxonomy.find("unclassified;"); //"Porphyromonadaceae"_unclassified;
if (thisPos != string::npos) {
OTUTaxonomy = OTUTaxonomy.substr(0, thisPos);
thisPos = OTUTaxonomy.find_last_of(";"); //remove rest of parent taxon
if (thisPos != string::npos) {
OTUTaxonomy = OTUTaxonomy.substr(0, thisPos+1);
}
}
//get list of reference ids that map to this taxonomy
vector<string> referenceIds = phyloTree->getSeqs(OTUTaxonomy);
if (m->getControl_pressed()) { break; }
//look for each one in otu map to find match
string otuID = "not found";
string referenceString = "";
for (int i = 0; i < referenceIds.size(); i++) {
referenceString += referenceIds[i] + " ";
map<string, string>::iterator itMap = otuMap.find(referenceIds[i]);
if (itMap != otuMap.end()) { //found it
otuID = itMap->second;
i += referenceIds.size(); //stop looking
}
}
//if found, add otu to ggOTUID list
if (otuID != "not found") {
map<string, vector<string> >::iterator itGG = ggOTUIDs.find(otuID);
if (itGG == ggOTUIDs.end()) {
vector<string> temp; temp.push_back(it->first); //save mothur OTU label
ggOTUIDs[otuID] = temp;
}else { ggOTUIDs[otuID].push_back(it->first); } //add mothur OTU label to list
}else { m->mothurOut("[ERROR]: could not find OTUId for " + it->second + ". Its reference sequences are " + referenceString + ".\n"); m->setControl_pressed(true); }
}
vector<SharedRAbundVector*> newLookup;
vector<string> namesOfGroups = lookup->getNamesGroups();
for (int i = 0; i < namesOfGroups.size(); i++) {
SharedRAbundVector* temp = new SharedRAbundVector();
temp->setLabel(lookup->getLabel());
temp->setGroup(namesOfGroups[i]);
newLookup.push_back(temp);
}
map<string, int> labelIndex;
vector<string> currentLabels = lookup->getOTUNames();
for (int i = 0; i < currentLabels.size(); i++) { labelIndex[util.getSimpleLabel(currentLabels[i])] = i; }
vector<string> newBinLabels;
map<string, string> newLabelTaxMap;
//loop through ggOTUID list combining mothur otus and adjusting labels
//ggOTUIDs = 16097 -> <OTU01, OTU10, OTU22>
for (map<string, vector<string> >::iterator itMap = ggOTUIDs.begin(); itMap != ggOTUIDs.end(); itMap++) {
if (m->getControl_pressed()) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return; }
//set new gg otu id to taxonomy. OTU01 -> k__Bacteria becomes 16097 -> k__Bacteria
//find taxonomy of this otu
map<string, string>::iterator it = labelTaxMap.find(util.getSimpleLabel(itMap->second[0]));
vector<string> scores;
vector<string> taxonomies = util.parseTax(it->second, scores);
//merge/set OTU abundances
vector<float> abunds; abunds.resize(lookup->size(), 0.0);
string mergeString = "";
vector<float> boots; boots.resize(scores.size(), 0.0);
bool scoresnullptr = false;
for (int j = 0; j < itMap->second.size(); j++) { //<OTU01, OTU10, OTU22>
if (scores[0] != "null") {
//merge bootstrap scores
vector<string> scores;
vector<string> taxonomies = util.parseTax(it->second, scores);
for (int i = 0; i < boots.size(); i++) {
if (scores[i] == "null") { scoresnullptr = true; break; }
else {
float tempScore; util.mothurConvert(scores[i], tempScore);
boots[i] += tempScore;
}
}
}else { scoresnullptr = true; }
//merge abunds
mergeString += (itMap->second)[j] + " ";
for (int i = 0; i < lookup->size(); i++) { abunds[i] += lookup->get(labelIndex[util.getSimpleLabel((itMap->second)[j])], namesOfGroups[i]); }
}
if (m->getDebug()) { m->mothurOut("[DEBUG]: merging " + mergeString + " for ggOTUid = " + itMap->first + ".\n"); }
//average scores
//add merged otu to new lookup
string newTaxString = "";
if (!scoresnullptr) {
for (int j = 0; j < boots.size(); j++) { boots[j] /= (float) itMap->second.size(); }
//assemble new taxomoy
for (int j = 0; j < boots.size(); j++) {
newTaxString += taxonomies[j] + "(" + toString(boots[j]) + ");";
}
}else {
//assemble new taxomoy
for (int j = 0; j < taxonomies.size(); j++) {
newTaxString += taxonomies[j] + ";";
}
}
//set new gg otu id to taxonomy. OTU01 -> k__Bacteria becomes 16097 -> k__Bacteria
//find taxonomy of this otu
newLabelTaxMap[itMap->first] = newTaxString;
//add merged otu to new lookup
for (int j = 0; j < abunds.size(); j++) { newLookup[j]->push_back(abunds[j]); }
//saved otu label
newBinLabels.push_back(itMap->first);
}
lookup->clear();
for (int i = 0; i < newLookup.size(); i++) { lookup->push_back(newLookup[i]); }
lookup->eliminateZeroOTUS();
lookup->setOTUNames(newBinLabels);
labelTaxMap = newLabelTaxMap;
return;
}
catch(exception& e) {
m->errorOut(e, "Picrust", "setGGOTUIDs");
exit(1);
}
}
//**********************************************************************************************************************
void Picrust::readGGOtuMap(string otumapfile){
try {
ifstream in; util.openInputFile(otumapfile, in);
//map referenceIDs -> otuIDs
//lines look like:
//16097 671376 616121 533566 683683 4332909 4434717 772666 611808 695209
while(!in.eof()) {
if (m->getControl_pressed()) { break; }
string line = util.getline(in); gobble(in);
vector<string> pieces = util.splitWhiteSpace(line);
if (pieces.size() != 0) {
string otuID = pieces[1];
for (int i = 1; i < pieces.size(); i++) { otuMap[pieces[i]] = otuID; }
}
}
in.close();
}
catch(exception& e) {
m->errorOut(e, "Picrust", "readGGOtuMap");
exit(1);
}
}
/**************************************************************************************************/
|