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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
//
// designmap.cpp
// Mothur
//
// Created by SarahsWork on 6/17/13.
// Copyright (c) 2013 Schloss Lab. All rights reserved.
//
#include "designmap.h"
/************************************************************/
DesignMap::DesignMap(string file) {
try {
m = MothurOut::getInstance();
defaultClass = "not found";
read(file);
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "DesignMap");
exit(1);
}
}
/************************************************************/
int DesignMap::read(string file) {
try {
namesOfCategories.clear();
indexCategoryMap.clear();
indexGroupNameMap.clear();
designMap.clear();
ifstream in; util.openInputFile(file, in);
string temp = "";
string headers = util.getline(in); gobble(in);
vector<string> tempColumnHeaders = util.splitWhiteSpace(headers);
if (tempColumnHeaders.size() != 0) { temp = tempColumnHeaders[0]; }
else { m->setControl_pressed(true); return 0; }
vector<string> columnHeaders;
if ((temp == "group") || (temp == "Group")) {
columnHeaders.push_back("group");
for (int i = 1; i < tempColumnHeaders.size(); i++) { columnHeaders.push_back(tempColumnHeaders[i]); }
}else {
m->mothurOut("\n[ERROR]: Expected 'group' and found '" + temp + "'. Mothur expects the design file to have column headers. The first column header should be 'group'.\n\nSomething like: group\ttreatment\tmetadata.\n\nWithout the headers, mothur is unable to determine if the first row is group information or a header of the wrong name. This can result in a dummy group or missing group which will results in errors. Quitting, please correct.\n\n");
m->setControl_pressed(true); in.close(); return 0;
}
map<int, string> originalGroupIndexes;
for (int i = 1; i < columnHeaders.size(); i++) { namesOfCategories.push_back(columnHeaders[i]); originalGroupIndexes[i-1] = columnHeaders[i]; }
if (columnHeaders.size() > 1) { defaultClass = columnHeaders[1]; }
else { m->mothurOut("[ERROR]: Your design file contains only one column. Please correct.\n"); m->setControl_pressed(true); }
//sort groups to keep consistent with how we store the groups in groupmap
sort(namesOfCategories.begin(), namesOfCategories.end());
for (int i = 0; i < namesOfCategories.size(); i++) { indexCategoryMap[namesOfCategories[i]] = i; }
int numCategories = namesOfCategories.size();
bool error = false;
string group;
totalCategories.resize(numCategories);
int count = 0;
while (!in.eof()) {
if (m->getControl_pressed()) { break; }
in >> group; gobble(in);
util.checkGroupName(group); if (m->getDebug()) { m->mothurOut("[DEBUG]: group = " + group + "\n"); }
//if group info, then read it
vector<string> categoryValues; categoryValues.resize(numCategories, "not found");
for (int i = 0; i < numCategories; i++) {
int thisIndex = indexCategoryMap[originalGroupIndexes[i]]; //find index of this category because we sort the values.
string temp = "not found";
in >> temp; categoryValues[thisIndex] = temp; gobble(in);
util.checkGroupName(temp);
if (m->getDebug()) { m->mothurOut("[DEBUG]: value = " + temp + "\n"); }
//do we have this value for this category already
map<string, int>::iterator it = totalCategories[thisIndex].find(temp);
if (it == totalCategories[thisIndex].end()) { totalCategories[thisIndex][temp] = 1; }
else { totalCategories[thisIndex][temp]++; }
}
map<string, int>::iterator it = indexGroupNameMap.find(group);
if (it == indexGroupNameMap.end()) {
groups.push_back(group);
indexGroupNameMap[group] = count;
designMap.push_back(categoryValues);
count++;
}else {
error = true;
m->mothurOut("[ERROR]: Your design file contains more than 1 group named " + group + ", group names must be unique. Please correct.\n");
}
}
in.close();
if (error) { m->setControl_pressed(true); }
return 0;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "read");
exit(1);
}
}
/************************************************************/
////groupName, returns default categories value.
string DesignMap::get(string groupName) {
try {
string value = "not found";
map<string, int>::iterator it2 = indexGroupNameMap.find(groupName);
if (it2 == indexGroupNameMap.end()) {
m->mothurOut("[ERROR]: group " + groupName + " is not in your design file. Please correct.\n"); m->setControl_pressed(true);
}else {
return designMap[it2->second][indexCategoryMap[defaultClass]];
}
return value;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "get");
exit(1);
}
}
/************************************************************/
////groupName, returns default categories value.
vector<string> DesignMap::getCategory() {
try {
//oldStyle design file group -> treatment. returns treatments
set<string> uniqueNames;
for (int i = 0; i < groups.size(); i++) { uniqueNames.insert(get(groups[i])); }
vector<string> values;
for (set<string>::iterator it = uniqueNames.begin(); it != uniqueNames.end(); it++) { values.push_back(*it); }
return values;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "getCategory");
exit(1);
}
}
/************************************************************/
////categoryName, returns category values.
vector<string> DesignMap::getCategory(string catName) {
try {
vector<string> values;
map<string, int>::iterator it2 = indexCategoryMap.find(catName);
if (it2 == indexCategoryMap.end()) {
m->mothurOut("[ERROR]: category " + catName + " is not in your design file. Please correct.\n"); m->setControl_pressed(true);
}else {
for (map<string, int>::iterator it = totalCategories[it2->second].begin(); it != totalCategories[it2->second].end(); it++) {
values.push_back(it->first);
}
}
return values;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "getCategory");
exit(1);
}
}
/************************************************************/
////groupName, category returns value. example F000132, sex -> male
string DesignMap::get(string groupName, string categoryName) {
try {
string value = "not found";
map<string, int>::iterator it = indexCategoryMap.find(categoryName);
if (it == indexCategoryMap.end()) {
m->mothurOut("[ERROR]: category " + categoryName + " is not in your design file. Please correct.\n"); m->setControl_pressed(true);
}else {
map<string, int>::iterator it2 = indexGroupNameMap.find(groupName);
if (it2 == indexGroupNameMap.end()) {
m->mothurOut("[ERROR]: group " + groupName + " is not in your design file. Please correct.\n"); m->setControl_pressed(true);
}else {
return designMap[it2->second][it->second];
}
}
return value;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "get");
exit(1);
}
}
/************************************************************/
//add group, assumes order is correct
int DesignMap::push_back(string group, vector<string> values) {
try {
util.checkGroupName(group);
map<string, int>::iterator it = indexGroupNameMap.find(group);
if (it == indexGroupNameMap.end()) {
if (values.size() != getNumCategories()) { m->mothurOut("[ERROR]: Your design file has a " + toString(getNumCategories()) + " categories and " + group + " has " + toString(values.size()) + ", please correct.\n"); m->setControl_pressed(true); return 0; }
for (int i = 0; i < values.size(); i++) {
//do we have this value for this category already
map<string, int>::iterator it = totalCategories[i].find(values[i]);
if (it == totalCategories[i].end()) { totalCategories[i][values[i]] = 1; }
else { totalCategories[i][values[i]]++; }
}
int count = indexGroupNameMap.size();
indexGroupNameMap[group] = count;
designMap.push_back(values);
}else {
m->mothurOut("[ERROR]: Your design file contains more than 1 group named " + group + ", group names must be unique. Please correct.\n"); m->setControl_pressed(true);
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "push_back");
exit(1);
}
}
/************************************************************/
//set values for group, does not need to set all values. assumes group is in table already
int DesignMap::setValues(string group, map<string, string> values) {
try {
map<string, int>::iterator it = indexGroupNameMap.find(group);
if (it != indexGroupNameMap.end()) {
for (map<string, string>::iterator it2 = values.begin(); it2 != values.end(); it2++) {
map<string, int>::iterator it3 = indexCategoryMap.find(it2->first); //do we have this category
if (it3 == indexCategoryMap.end()) {
m->mothurOut("[ERROR]: Your design file does not contain a category called " + it2->first + ". Please correct.\n"); m->setControl_pressed(true);
}else {
string oldCategory = designMap[it->second][it3->second];
//adjust totals for old category
int oldCount = totalCategories[it3->second][oldCategory];
if (oldCount == 1) { totalCategories[it3->second].erase(oldCategory); }
else { totalCategories[it3->second][oldCategory]--; }
designMap[it->second][it3->second] = it2->second; //reset value
//adjust totals for new category
map<string, int>::iterator it4 = totalCategories[it3->second].find(it2->second);
if (it4 == totalCategories[it3->second].end()) { totalCategories[it3->second][it2->second] = 1; }
else { totalCategories[it3->second][it2->second]++; }
}
}
}else {
m->mothurOut("[ERROR]: Your design file does not contain a group named " + group + ". Please correct.\n"); m->setControl_pressed(true);
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "setValues");
exit(1);
}
}
/************************************************************/
//set defaultclass
void DesignMap::setDefaultClass(string dClass) {
try {
if (util.inUsersGroups(dClass, namesOfCategories)) {
defaultClass = dClass;
}else{
m->mothurOut("[WARNING]: Your design file does not contain a category named " + dClass + ". Using default class " + defaultClass + " .\n\n");
}
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "setDefaultClass");
exit(1);
}
}
/************************************************************/
//get number of groups belonging to a category or set of categories, with value or a set of values. Must have all categories and values. Example:
// map<treatment - > early, late>, <sex -> male> would return 1. Only one group is male and from early or late.
int DesignMap::getNumUnique(map<string, vector<string> > selected) {
try {
int num = 0;
map<int, vector<string> > indexes;
for (map<string, vector<string> >::iterator it = selected.begin(); it != selected.end(); it++) {
map<string, int>::iterator it2 = indexCategoryMap.find(it->first);
if (it2 == indexCategoryMap.end()) {
m->mothurOut("[ERROR]: Your design file does not contain a category named " + it->first + ". Please correct.\n"); m->setControl_pressed(true); return 0;
}else {
indexes[it2->second] = it->second;
}
}
for (int j = 0; j < designMap.size(); j++) {
bool hasAll = true; //innocent til proven guilty
for (map<int, vector<string> >::iterator it = indexes.begin(); it != indexes.end(); it++) {
//column number is it->first
if (!util.inUsersGroups(designMap[j][it->first], it->second)) { hasAll = false; }
}
if (hasAll) { num++; }
}
return num;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "getNumUnique");
exit(1);
}
}
/************************************************************/
//get number of groups belonging to a category or set of categories, with value or a set of values. Must have at least one categories and values. Example:
// map<treatment - > early, late>, <sex -> male> would return 3. All three group have are either male or from early or late.
int DesignMap::getNumShared(map<string, vector<string> > selected) {
try {
int num = 0;
map<int, vector<string> > indexes;
for (map<string, vector<string> >::iterator it = selected.begin(); it != selected.end(); it++) {
map<string, int>::iterator it2 = indexCategoryMap.find(it->first);
if (it2 == indexCategoryMap.end()) {
m->mothurOut("[ERROR]: Your design file does not contain a category named " + it->first + ". Please correct.\n"); m->setControl_pressed(true); return 0;
}else {
indexes[it2->second] = it->second;
}
}
for (int j = 0; j < designMap.size(); j++) {
bool hasAny = false; //innocent til proven guilty
for (map<int, vector<string> >::iterator it = indexes.begin(); it != indexes.end(); it++) {
//column number is it->first
if (util.inUsersGroups(designMap[j][it->first], it->second)) { hasAny = true; }
}
if (hasAny) { num++; }
}
return num;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "getNumShared");
exit(1);
}
}
/************************************************************/
//get names of groups belonging to a category or set of categories, with value or a set of values. Must have all categories and values. Example:
// map<treatment - > early, late>, <sex -> male> would return F000132. F000132 is the only group which is male and from early or late.
vector<string> DesignMap::getNamesUnique(map<string, vector<string> > selected) {
try {
vector<string> names;
map<int, vector<string> > indexes;
for (map<string, vector<string> >::iterator it = selected.begin(); it != selected.end(); it++) {
map<string, int>::iterator it2 = indexCategoryMap.find(it->first);
if (it2 == indexCategoryMap.end()) {
m->mothurOut("[ERROR]: Your design file does not contain a category named " + it->first + ". Please correct.\n"); m->setControl_pressed(true); return names;
}else {
indexes[it2->second] = it->second;
}
}
//map int to name
map<int, string> reverse;
for (map<string, int>::iterator it = indexGroupNameMap.begin(); it != indexGroupNameMap.end(); it++) {
reverse[it->second] = it->first;
}
for (int j = 0; j < designMap.size(); j++) {
bool hasAll = true; //innocent til proven guilty
for (map<int, vector<string> >::iterator it = indexes.begin(); it != indexes.end(); it++) {
//column number is it->first
if (!util.inUsersGroups(designMap[j][it->first], it->second)) { hasAll = false; }
}
if (hasAll) {
map<int, string>::iterator it = reverse.find(j);
if (it == reverse.end()) {
m->mothurOut("[ERROR]: should never get here, oops. Please correct.\n"); m->setControl_pressed(true); return names;
}else { names.push_back(it->second); }
}
}
return names;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "getNamesUnique");
exit(1);
}
}
/************************************************************/
//get names of groups belonging to a category or set of categories, with value or a set of values. Must have all categories and values. Example:
// map<treatment - > early, late>, <sex -> male> would return F000132. F000132 is the only group which is male and from early or late.
vector<string> DesignMap::getNamesShared(map<string, vector<string> > selected) {
try {
vector<string> names;
map<int, vector<string> > indexes;
for (map<string, vector<string> >::iterator it = selected.begin(); it != selected.end(); it++) {
map<string, int>::iterator it2 = indexCategoryMap.find(it->first);
if (it2 == indexCategoryMap.end()) {
m->mothurOut("[ERROR]: Your design file does not contain a category named " + it->first + ". Please correct.\n"); m->setControl_pressed(true); return names;
}else {
indexes[it2->second] = it->second;
}
}
//map int to name
map<int, string> reverse;
for (map<string, int>::iterator it = indexGroupNameMap.begin(); it != indexGroupNameMap.end(); it++) {
reverse[it->second] = it->first;
}
for (int j = 0; j < designMap.size(); j++) {
bool hasAny = false; //innocent til proven guilty
for (map<int, vector<string> >::iterator it = indexes.begin(); it != indexes.end(); it++) {
//column number is it->first
if (util.inUsersGroups(designMap[j][it->first], it->second)) { hasAny = true; }
}
if (hasAny) {
map<int, string>::iterator it = reverse.find(j);
if (it == reverse.end()) {
m->mothurOut("[ERROR]: should never get here, oops. Please correct.\n"); m->setControl_pressed(true); return names;
}else { names.push_back(it->second); }
}
}
return names;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "getNamesShared");
exit(1);
}
}
/************************************************************/
//get names of groups belonging to a category or set of categories, with value or a set of values. Must have at least one categories and values. Example:
// map<treatment - > early, late>, <sex -> male> would return F000132, F000142, F000138. All three group have are either male or from early or late.
vector<string> DesignMap::getNamesGroups(string category, string value) {
try {
vector<string> names;
map<string, int>::iterator it = indexCategoryMap.find(category);
if (it == indexCategoryMap.end()) {
m->mothurOut("[ERROR]: category " + category + " is not in your design file. Please correct.\n"); m->setControl_pressed(true);
}else {
int column = it->second;
//map int to name
map<int, string> reverse;
for (map<string, int>::iterator it2 = indexGroupNameMap.begin(); it2 != indexGroupNameMap.end(); it2++) {
reverse[it2->second] = it2->first;
}
for (int i = 0; i < designMap.size(); i++) {
if (designMap[i][column] == value) {
map<int, string>::iterator it2 = reverse.find(i);
if (it2 == reverse.end()) {
m->mothurOut("[ERROR]: should never get here, oops. Please correct.\n"); m->setControl_pressed(true); return names;
}else { names.push_back(it2->second); }
}
}
}
return names;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "getNamesGroups");
exit(1);
}
}
/************************************************************/
//assume default category and get names groups that match any values in vector passed in. <early, late> = F000142, F000132.
vector<string> DesignMap::getNamesGroups(vector<string> sets) {
try {
vector<string> names;
if (sets.size() == 0) { return names; }
map<string, vector<string> > temp;
temp[defaultClass] = sets;
names = getNamesShared(temp);
return names;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "getNamesGroups");
exit(1);
}
}
/************************************************************/
int DesignMap::print(ofstream& out) {
try {
out << "group";
for (int i = 0; i < namesOfCategories.size(); i++) { out << '\t' << namesOfCategories[i]; }
out << endl;
map<int, string> reverse; //use this to preserve order
for (map<string, int>::iterator it = indexGroupNameMap.begin(); it !=indexGroupNameMap.end(); it++) { reverse[it->second] = it->first; }
for (int i = 0; i < designMap.size(); i++) {
map<int, string>::iterator itR = reverse.find(i);
if (itR != reverse.end()) { //will equal end if seqs were removed because remove just removes from indexNameMap
out << itR->second;
for (int j = 0; j < namesOfCategories.size(); j++) {
out << '\t' << designMap[i][j];
}
out << endl;
}
}
out.close();
return 0;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "print");
exit(1);
}
}
/************************************************************/
//print specific categories
int DesignMap::printCategories(ofstream& out, vector<string> cats) {
try {
out << "group";
for (int i = 0; i < namesOfCategories.size(); i++) { if (util.inUsersGroups(namesOfCategories[i], cats)) { out << '\t' << namesOfCategories[i]; } }
out << endl;
map<int, string> reverse; //use this to preserve order
for (map<string, int>::iterator it = indexGroupNameMap.begin(); it !=indexGroupNameMap.end(); it++) { reverse[it->second] = it->first; }
for (int i = 0; i < designMap.size(); i++) {
map<int, string>::iterator itR = reverse.find(i);
if (itR != reverse.end()) { //will equal end if seqs were removed because remove just removes from indexNameMap
out << itR->second;
for (int j = 0; j < namesOfCategories.size(); j++) {
if (util.inUsersGroups(namesOfCategories[i], cats)) {
out << '\t' << designMap[i][j];
}
}
out << endl;
}
}
out.close();
return 0;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "printCategories");
exit(1);
}
}
/************************************************************/
//print specific groups
int DesignMap::printGroups(ofstream& out, vector<string> groups) {
try {
int numSelected = 0;
out << "group";
for (int i = 0; i < namesOfCategories.size(); i++) { out << '\t' << namesOfCategories[i]; }
out << endl;
map<int, string> reverse; //use this to preserve order
for (map<string, int>::iterator it = indexGroupNameMap.begin(); it !=indexGroupNameMap.end(); it++) { reverse[it->second] = it->first; }
for (int i = 0; i < designMap.size(); i++) {
map<int, string>::iterator itR = reverse.find(i);
if (itR != reverse.end()) { //will equal end if groups were removed because remove just removes from indexNameMap
if (util.inUsersGroups(itR->second, groups)) {
out << itR->second;
for (int j = 0; j < namesOfCategories.size(); j++) {
out << '\t' << designMap[i][j];
}
out << endl;
numSelected++;
}
}
}
out.close();
return numSelected;
}
catch(exception& e) {
m->errorOut(e, "DesignMap", "printGroups");
exit(1);
}
}
/************************************************************/
|