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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
|
//
// biomhdf5.cpp
// Mothur
//
// Created by Sarah Westcott on 10/26/20.
// Copyright © 2020 Schloss Lab. All rights reserved.
//
#include "biomhdf5.hpp"
/**************************************************************************************************/
BiomHDF5::BiomHDF5(string fname, string l) : Biom("Biological Observation Matrix 2.1.0"){
try {
label = l;
numOTUs = 0; numSamples = 0;
read(fname);
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "BiomHDF5");
exit(1);
}
}
/**************************************************************************************************/
BiomHDF5::BiomHDF5() : Biom("Biological Observation Matrix 2.1.0"){
try {
numOTUs = 0; numSamples = 0;
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "BiomHDF5");
exit(1);
}
}
//**********************************************************************************************************************
#ifdef USE_HDF5
bool pathExists(hid_t id, const string& path) { return (H5Lexists( id, path.c_str(), H5P_DEFAULT ) > 0); }
#endif
/**************************************************************************************************/
void BiomHDF5::read(string fname){
try {
nnz = 0; maxLevel = 0;
otuNames.clear(); sampleNames.clear(); taxonomy.clear(); otuTaxonomies.clear();
#ifdef USE_HDF5
Picrust* picrust; vector<string> metadata;
H5::H5File file( fname.c_str(), H5F_ACC_RDONLY );
readAttributes(file); if (m->getControl_pressed()) { return; }
try {
//read otu names
if (pathExists(file.getId(), "observation/")) {
H5::Group group(file.openGroup("observation/"));
otuNames = readNames(file, group, "ids"); if (m->getControl_pressed()) { return; }
group.close();
}else {
m->mothurOut("[ERROR]: Missing /""observation/ids/"" needed for OTU names.\n");
m->setControl_pressed(true);
}
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: reading /""observation/ids/"" needed for OTU names.\n");
m->setControl_pressed(true);
}
try {
//read group names
if (pathExists(file.getId(), "sample/")) {
H5::Group group(file.openGroup("sample/"));
sampleNames = readNames(file, group, "ids"); if (m->getControl_pressed()) { return; }
group.close();
}else {
m->mothurOut("[ERROR]: Missing /""sample/ids/"" needed for group names.\n");
m->setControl_pressed(true);
}
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: reading /""sample/ids/"" needed for group names.\n");
m->setControl_pressed(true);
}
bool hasConsTaxonomy = false;
try {
//read otu taxonomies
if (pathExists(file.getId(), "observation/metadata/")) {
H5::Group group(file.openGroup("observation/metadata/"));
readTaxonomy(group, "taxonomy"); if (m->getControl_pressed()) { return; }
group.close();
if (otuTaxonomies.size() == otuNames.size()) { hasConsTaxonomy = true; }
}
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: reading /""observation/metadata/taxonomy"".\n");
hasConsTaxonomy = false;
m->setControl_pressed(true);
}
try {
//read otu abundances
if (pathExists(file.getId(), "observation/matrix/")) {
H5::Group group(file.openGroup("observation/matrix/"));
vector<string> datasets;
datasets.push_back("data"); datasets.push_back("indices"); datasets.push_back("indptr"); datasets.push_back("label");
label = readOTUAbundances(group, datasets); if (m->getControl_pressed()) { return; }
group.close();
}else {
m->mothurOut("[ERROR]: Missing /""observation/matrix/"" needed for OTU abundances.\n");
m->setControl_pressed(true);
}
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: reading /""observation/matrix/"" needed for OTU abundances.\n");
m->setControl_pressed(true);
}
bool error = false;
if (nnz != otudata.size()) { error = true; }
//create shared file
sort(sampleNames.begin(), sampleNames.end());
if (shared != nullptr) { delete shared; }
shared = new SharedRAbundVectors();
//create empty sharedrabundvectors so we can add otus below
for (int i = 0; i < sampleNames.size(); i++) {
SharedRAbundVector* temp = new SharedRAbundVector();
temp->setGroup(sampleNames[i]);
shared->push_back(temp);
}
shared->setLabels(label);
if (matrixElementType == "float") {
if (sharedFloat != nullptr) { delete sharedFloat; }
sharedFloat = new SharedRAbundFloatVectors();
//creates new sharedRAbunds
for (int i = 0; i < sampleNames.size(); i++) {
SharedRAbundFloatVector* temp = new SharedRAbundFloatVector(shared->getNumBins()); //sets all abunds to 0
temp->setLabel(label);
temp->setGroup(sampleNames[i]);
sharedFloat->push_back(temp);
}
sharedFloat->setLabels(label);
}
//for each otu
int count = 0;
for (int h = 0; h < indptr.size()-1; h++) {
int otuStart = indptr[h];
int otuEnd = indptr[h+1];
vector<int> otuAbunds; otuAbunds.resize(sampleNames.size(), 0); //initialze otus sample abundances to 0 - only non zero abunds are recorded
vector<float> otuFloatAbunds; otuFloatAbunds.resize(sampleNames.size(), 0); //initialze otus sample abundances to 0 - only non zero abunds are recorded
for (int i = otuStart; i < otuEnd; i++) {
otuAbunds[indices[i]] = (int)otudata[count];
otuFloatAbunds[indices[i]] = otudata[count];
count++;
}
shared->push_back(otuAbunds, otuNames[h]);
if (matrixElementType == "float") { sharedFloat->push_back(otuFloatAbunds, otuNames[h]); }
}
if (hasConsTaxonomy) {
if (shared->getNumBins() == otuTaxonomies.size()) {
for (int i = 0; i < otuTaxonomies.size(); i++) {
if (m->getControl_pressed()) { break; }
string thisOTUsTax = otuTaxonomies[i];
string newTax = util.addUnclassifieds(thisOTUsTax, maxLevel, false);
Taxonomy thisOTUsTaxonomy(otuNames[i], newTax, shared->getOTUTotal(i));
consTax.push_back(thisOTUsTaxonomy);
}
}
}
if (sampleNames.size() == taxonomy.size()) {
for (int i = 0; sampleNames.size(); i++) {
if (m->getControl_pressed()) { break; }
groupTaxonomies[sampleNames[i]] = taxonomy[i];
}
}
file.close();
#endif
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "read");
exit(1);
}
}
#ifdef USE_HDF5
//**********************************************************************************************************************
//Group = "observation/" or "sample/", datasetName = "ids"
vector<string> BiomHDF5::readNames( H5::H5File& file, H5::Group& group, string datasetname) {
try {
vector<string> items;
hsize_t numObjects = group.getNumObjs();
if (numObjects != 0) { //we have this group
H5::DataSet dataset = group.openDataSet(datasetname.c_str());
H5::DataType dataType(dataset.getDataType());
H5::DataSpace dataSpace = dataset.getSpace();
int rank = dataSpace.getSimpleExtentNdims(); //number of dimensions, should be 1
hsize_t dims[rank]; dataSpace.getSimpleExtentDims(dims); //size of each dimension
char **data = new char*[dims[0]];
H5::StrType str_type(H5::PredType::C_S1, H5T_VARIABLE);
dataset.read((void*)data, str_type);
if (m->getDebug()) { m->mothurOut("[DEBUG]: " + datasetname + " = "); }
for (int i = 0; i < dims[0]; i++) {
if (m->getDebug()) { m->mothurOut(toString(data[i]) + "\t"); }
items.push_back(data[i]);
delete[] data[i];
} if (m->getDebug()) { m->mothurOutEndLine(); }
delete[] data;
dataset.close();
}
return items;
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "readNames");
exit(1);
}
}
//**********************************************************************************************************************
//Group = "observation/metadata", datasetName = "taxonomy"
int BiomHDF5::readTaxonomy( H5::Group& group, string datasetName) {
try {
hsize_t numObjects = group.getNumObjs();
if (numObjects != 0) { //we have this group
H5::DataSet dataset = group.openDataSet(datasetName);
H5::DataType dataType(dataset.getDataType());
H5::DataSpace dataSpace = dataset.getSpace();
H5::StrType str_type(H5::PredType::C_S1, H5T_VARIABLE);
int rank = dataSpace.getSimpleExtentNdims(); //number of dimensions
hsize_t dims[rank]; dataSpace.getSimpleExtentDims(dims); //size of each dimension
if (dataset.getTypeClass() == H5T_STRING) {
int numberOfTaxonomies = dims[0];
if (rank == 2) { numberOfTaxonomies *= dims[1]; }
char **data = new char*[numberOfTaxonomies];
dataset.read((void*)data, dataType);
if (rank == 1) {
for (int i = 0; i < numberOfTaxonomies; i++) {
if (m->getDebug()) { m->mothurOut(toString(data[i]) + "\t"); }
taxonomy.push_back(data[i]);
delete[] data[i];
}
delete[] data;
}else if (rank == 2) {
string otuTaxonomy = ""; int count = 0;
for (int i = 0; i < numberOfTaxonomies; i++) {
otuTaxonomy += data[i]; otuTaxonomy += ";"; count++;
if (count == dims[1]) {
if (m->getDebug()) { m->mothurOut("[DEBUG]: " + toString(otuTaxonomy) + "\n"); }
otuTaxonomies.push_back(otuTaxonomy);
if (count > maxLevel) { maxLevel = count; }
count = 0; otuTaxonomy = "";
}
}
}
}
dataset.close();
}
return numObjects;
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "readTaxonomy");
exit(1);
}
}
//**********************************************************************************************************************
//Group = "observation/matrix"
string BiomHDF5::readOTUAbundances( H5::Group& group, vector<string> datasets) {
try {
string thisLabel = "";
hsize_t numObjects = group.getNumObjs();
if (numObjects == 0) { return thisLabel; }
for (int h = 0; h < datasets.size(); h++) {
H5std_string datasetName = datasets[h];
if (pathExists(group.getId(), datasetName)) {
H5::DataSet dataset = group.openDataSet(datasetName);
H5::DataSpace dataSpace = dataset.getSpace();
if (dataset.getTypeClass() == H5T_INTEGER) {
int rank = dataSpace.getSimpleExtentNdims(); //number of dimensions, should be 1
hsize_t dims[rank]; dataSpace.getSimpleExtentDims(dims); //size of each dimension
matrixElementType = "int";
if (rank == 1) {
int* data = new int[dims[0]];
H5::DataSpace data_mspace(rank, dims);
dataset.read(data, H5::PredType::NATIVE_INT, data_mspace, dataSpace);
if (m->getDebug()) { m->mothurOut("[DEBUG]: " + datasetName + " = "); }
for (int i = 0; i < dims[0]; i++) {
if (m->getDebug()) { m->mothurOut(toString(data[i]) + "\t"); }
if (datasetName == "data") { otudata.push_back(data[i]); }
else if (datasetName == "indices") { indices.push_back(data[i]); }
else if (datasetName == "indptr") { indptr.push_back(data[i]); }
} if (m->getDebug()) { m->mothurOutEndLine(); }
delete[] data;
}
}else if (dataset.getTypeClass() == H5T_FLOAT) {
int rank = dataSpace.getSimpleExtentNdims(); //number of dimensions, should be 1
hsize_t dims[rank]; dataSpace.getSimpleExtentDims(dims); //size of each dimension
matrixElementType = "float";
if (rank == 1) {
float* data = new float[dims[0]];
H5::DataSpace data_mspace(rank, dims);
dataset.read(data, H5::PredType::NATIVE_FLOAT, data_mspace, dataSpace);
if (m->getDebug()) { m->mothurOut("[DEBUG]: " + datasetName + " = "); }
for (int i = 0; i < dims[0]; i++) {
if (m->getDebug()) { m->mothurOut(toString(data[i]) + "\t"); }
if (datasetName == "data") { otudata.push_back(data[i]); }
else if (datasetName == "indices") { indices.push_back((int)data[i]); }
else if (datasetName == "indptr") { indptr.push_back((int)data[i]); }
} if (m->getDebug()) { m->mothurOutEndLine(); }
delete[] data;
}
}else if (dataset.getTypeClass() == H5T_STRING) {
H5::StrType strdatatype(H5::PredType::C_S1, H5T_VARIABLE);
H5std_string value;
dataset.read(value, strdatatype);
thisLabel = value;
}
dataset.close();
}
}
return thisLabel;
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "readOTUAbundances");
exit(1);
}
}
//**********************************************************************************************************************
//read attribute of group
string BiomHDF5::readStringAttributes(H5::Group& fileAttributes, string name) {
try {
H5::Attribute attribute(fileAttributes.openAttribute(name));
H5std_string attributeName; attribute.getName(attributeName);
H5::DataType attributeType(attribute.getDataType());
string attributeValue = "";
// Read the Attribute Data. Depends on the kind of data
if (attributeType.getClass() == H5T_STRING) {
H5std_string value; attribute.read(attributeType, value);
attributeValue = value;
if (m->getDebug()) { m->mothurOut("[DEBUG]: " + attributeName + " = " + value + "\n"); }
}
attribute.close();
return attributeValue;
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "readStringAttributes");
exit(1);
}
}
//**********************************************************************************************************************
//read attribute of group
void BiomHDF5::readIntAttributes(H5::Group& fileAttributes, string name) {
try {
H5::Attribute attribute(fileAttributes.openAttribute(name));
H5std_string attributeName; attribute.getName(attributeName);
H5::DataType attributeType(attribute.getDataType());
H5::DataSpace attDataSpace = attribute.getSpace();
int rank = attDataSpace.getSimpleExtentNdims(); //number of dimensions
hsize_t dims[rank]; attDataSpace.getSimpleExtentDims(dims); //size of each dimension
// Read the Attribute Data. Depends on the kind of data
if (attributeType.getClass() == H5T_INTEGER) {
if (attDataSpace.isSimple()) {
if (rank == 0) {
hsize_t data = 0;
attribute.read(attributeType, &data);
if (m->getDebug()) { m->mothurOut("[DEBUG]: " + attributeName + " = " + toString(data) + "\n"); }
if (attributeName == "nnz") { nnz = data; }
}else if (rank == 1) {
hsize_t data[dims[0]];
attribute.read(attributeType, data);
if (m->getDebug()) { m->mothurOut("[DEBUG]: " + attributeName + " = "); }
for (int i = 0; i < dims[0]; i++) {
if (m->getDebug()) { m->mothurOut(toString(data[i]) + "\t"); }
if (attributeName == "nnz") { nnz = data[i]; }
} if (m->getDebug()) { m->mothurOutEndLine(); }
if (attributeName == "shape") {
if (dims[0] == 2) { numOTUs = data[0]; numSamples = data[1]; }
}
}
}
}
attribute.close();
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "readIntAttributes");
exit(1);
}
}
//**********************************************************************************************************************
//Process attribute of group or dataset
void BiomHDF5::readAttributes(H5::H5File& file) {
try {
H5::Group fileAttributes(file.openGroup( "/" ));
//read table id
tableID = readStringAttributes(fileAttributes, "id");
//read table type
tableType = readStringAttributes(fileAttributes, "type");
//read format-url
formatURL = readStringAttributes(fileAttributes, "format-url");
//read shape
readIntAttributes(fileAttributes, "shape");
//read number non zero
readIntAttributes(fileAttributes, "nnz");
fileAttributes.close();
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "readAttributes");
exit(1);
}
}
//**********************************************************************************************************************
//print required dataset attributes
void BiomHDF5::printRequiredFileAttributes(H5::Group& fileAttributes, int numBins, int numSamples) {
try {
H5::DataSpace attr_dataspace = H5::DataSpace(H5S_SCALAR); // Create new dataspace for attribute
H5::StrType strdatatype(H5::PredType::C_S1, H5T_VARIABLE);
H5std_string idValue(tableID);
H5::Attribute idAttribute = fileAttributes.createAttribute("id", strdatatype, attr_dataspace);
idAttribute.write(strdatatype, idValue);
H5std_string typeValue(tableType);
H5::Attribute typeAttribute = fileAttributes.createAttribute("type", strdatatype, attr_dataspace);
typeAttribute.write(strdatatype, typeValue);
H5std_string formatUrl(formatURL);
H5::Attribute urlAttribute = fileAttributes.createAttribute("format-url", strdatatype, attr_dataspace);
urlAttribute.write(strdatatype, formatUrl);
H5std_string generatedByValue(mothurVersion);
H5::Attribute generatedByAttribute = fileAttributes.createAttribute("generated-by", strdatatype, attr_dataspace);
generatedByAttribute.write(strdatatype, generatedByValue);
time_t rawtime; struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
string dateString = asctime (timeinfo);
int pos = dateString.find('\n');
if (pos != string::npos) { dateString = dateString.substr(0, pos);}
H5std_string dataValue(dateString);
H5::Attribute dateAttribute = fileAttributes.createAttribute("creation-date", strdatatype, attr_dataspace);
dateAttribute.write(strdatatype, dataValue);
hsize_t dims[1]; dims[0] = 2;
H5::DataSpace dataspace( 1, dims );
hsize_t data[2]; data[0] = 2; data[1] = 1;
H5::Attribute formatVersionAttribute = fileAttributes.createAttribute("format-version", H5::PredType::NATIVE_INT, dataspace);
formatVersionAttribute.write(H5::PredType::NATIVE_INT, data);
data[0] = numBins; data[1] = numSamples;
H5::Attribute shapeAttribute = fileAttributes.createAttribute("shape", H5::PredType::NATIVE_INT, dataspace);
shapeAttribute.write(H5::PredType::NATIVE_INT, data);
hsize_t nnzValue = nnz;
H5::Attribute nnzAttribute = fileAttributes.createAttribute("nnz", H5::PredType::NATIVE_INT, attr_dataspace);
nnzAttribute.write(H5::PredType::NATIVE_INT, &nnzValue);
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "printRequiredAttributes");
exit(1);
}
}
//**********************************************************************************************************************
//print otuNames
//"observation/ids" -> otuLabels - "GG_OTU_1", "GG_OTU_2", "GG_OTU_3", "GG_OTU_4", "GG_OTU_5
//"sample/ids" -> group names - "Sample1", "Sample2", "Sample3", "Sample4", "Sample5", "Sample6"
void BiomHDF5::printNames(H5::Group& group, vector<string> names, string datasetname) {
try {
hsize_t dimsf[1]; dimsf[0] = names.size();
H5::DataSpace dataspace( 1, dimsf );
H5::StrType datatype(H5::PredType::C_S1, H5T_VARIABLE);
//fill data with names
char* data[dimsf[0]];
for (int i = 0; i < names.size(); i++) { data[i] = (char*) names[i].c_str(); }
const H5std_string DATASET_NAME( datasetname.c_str() );
H5::DataSet dataset = group.createDataSet( DATASET_NAME, datatype, dataspace );
dataset.write( data, datatype );
dataset.close();
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "printNames");
exit(1);
}
}
//**********************************************************************************************************************
//"observation/metadata/taxonomy" -> taxonomy info - otu classifications
void BiomHDF5::printOTUTaxonomy(H5::Group& group, string datasetname) {
try {
int maxLevel = consTax[0].getNumLevels();
hsize_t dimsf[2]; dimsf[0] = consTax.size(); dimsf[1] = maxLevel;
H5::DataSpace dataspace(2, dimsf); //2D array, (numOtus x vector<taxonomy>)
H5::StrType datatype(H5::PredType::C_S1, H5T_VARIABLE);
const H5std_string DATASET_NAME( datasetname.c_str() );
H5::DataSet dataset = group.createDataSet( DATASET_NAME, datatype, dataspace );
vector<char*> cPara;
for (int i = 0; i < consTax.size(); i++) {
if (m->getControl_pressed()) { break; }
vector<string> thisOtusTaxonomy = consTax[i].getSimpleTaxons();
for (int j = 0; j < maxLevel; j++) { cPara.push_back(util.mothurConvert(thisOtusTaxonomy[j])); }
}
char** data; data = new char*[cPara.size()];
for (int i = 0; i < cPara.size(); i++) {
data[i] = cPara[i];
}
dataset.write(data, datatype);
dataset.close();
//free memory
for(int i = 0; i < cPara.size(); i++) { delete cPara[i]; }
delete[] data;
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "printOTUTaxonomy");
exit(1);
}
}
//**************************************************************************************************************
//"observation/matrix/data" -> otu abundances for each non zero abundnace entry - 1, 5, 1, 2, 3, 1, 1, 4, 2, 2, 1, 1, 1, 1, 1
//"observation/matrix/indices" -> index of group - maps into samples/ids 2, 0, 1, 3, 4, 5, 2, 3, 5, 0, 1, 2, 5, 1, 2
//"observation/matrix/indptr" -> maps non zero abundance to OTU - 0, 1, 6, 9, 13, 15 - 0 start of OTU1s indexes, 1 start of OTU2s indexes, ... 15 start of OTU5s indexes
/*
label group numOtus GG_OTU_1 GG_OTU_2 GG_OTU_3 GG_OTU_4 GG_OTU_5
userLabel Sample1 0 5 0 2 0
userLabel Sample2 0 1 0 2 1
userLabel Sample3 1 0 1 1 1
userLabel Sample4 0 2 4 0 0
userLabel Sample5 0 3 0 0 0
userLabel Sample6 0 1 2 1 0
*/
//group = "observation/matrix/";
void BiomHDF5::printOTUAbundances(H5::Group& group, int numBins, int numSamples, string label, bool useRelabund=false) {
try {
int otusStartIndex = 0;
vector<int> indptr, indices, abunds;
vector<float> abundsFloat;
//fill indices, indptr and data vectors
for (int i = 0; i < numBins; i++) {
if (m->getControl_pressed()) { return; }
vector<int> thisOtusAbundances; vector<float> thisOtusFloatAbundances; float zero = 0.0;
if (useRelabund) { thisOtusFloatAbundances = sharedFloat->getOTU(i); }
else { thisOtusAbundances = shared->getOTU(i); }
indptr.push_back(otusStartIndex);
for (int j = 0; j < numSamples; j++) {
if (useRelabund) {
if (util.isEqual(thisOtusFloatAbundances[j], zero)) {} //skip zero values
else {
otusStartIndex++; //update number of non zero values for this OTU - use to create indptr values
indices.push_back(j); //index to sample providing this abund
abundsFloat.push_back(thisOtusFloatAbundances[j]); //save this samples OTU abundance
}
}else {
if (thisOtusAbundances[j] == 0) {} //skip zero values
else {
otusStartIndex++; //update number of non zero values for this OTU - use to create indptr values
indices.push_back(j); //index to sample providing this abund
abunds.push_back(thisOtusAbundances[j]); //save this samples OTU abundance
}
}
}
}
// dataset dimensions
hsize_t dimsf[1]; dimsf[0] = nnz;
H5::DataSpace dataspace( 1, dimsf ); //dataspace 1 x nnz
int data[nnz];
for (int i = 0; i < nnz; i++) { data[i] = indices[i]; } //fill data with indices
const H5std_string DATASET_NAME( "indices" );
H5::DataSet dataset = group.createDataSet( DATASET_NAME, H5::PredType::NATIVE_INT, dataspace );
dataset.write( data, H5::PredType::NATIVE_INT );
dataset.close();
//create data dataset - type depends on whether or not we are using the relabund values
if (useRelabund) { //print float
float dataFloat[nnz];
for (int i = 0; i < nnz; i++) { dataFloat[i] = abundsFloat[i]; } //fill data with abunds
const H5std_string DATASET_NAME( "data" );
H5::DataSet dataset = group.createDataSet( DATASET_NAME, H5::PredType::NATIVE_FLOAT, dataspace );
dataset.write( dataFloat, H5::PredType::NATIVE_FLOAT );
dataset.close();
}else { //print shared
for (int i = 0; i < nnz; i++) { data[i] = abunds[i]; } //fill data with abunds
const H5std_string DATASET_NAME( "data" );
H5::DataSet dataset = group.createDataSet( DATASET_NAME, H5::PredType::NATIVE_INT, dataspace );
dataset.write( data, H5::PredType::NATIVE_INT );
dataset.close();
}
//create indptr dataset
dimsf[0] = numBins+1;
H5::DataSpace dataspaceIndptr(1, dimsf); //dataspace 1 x numBins
int dataIndptr[numBins+1];
for (int i = 0; i < numBins; i++) { dataIndptr[i] = indptr[i]; } //fill data with indptr
dataIndptr[numBins] = nnz;
const H5std_string DATASET_NAME_INDPTR( "indptr" );
H5::DataSet datasetIndptr = group.createDataSet( DATASET_NAME_INDPTR, H5::PredType::NATIVE_INT, dataspaceIndptr );
datasetIndptr.write(dataIndptr, H5::PredType::NATIVE_INT);
datasetIndptr.close();
//create label dataset to store shared label
H5::DataSpace attr_dataspace = H5::DataSpace(H5S_SCALAR); // Create new dataspace for attribute
H5::StrType strdatatype(H5::PredType::C_S1, H5T_VARIABLE);
const H5std_string DATASET_NAME_LABEL( "label" );
H5::DataSet labelDataset = group.createDataSet( DATASET_NAME_LABEL, strdatatype, attr_dataspace );
labelDataset.write(label, strdatatype);
labelDataset.close();
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "printOTUAbundances");
exit(1);
}
}
#endif
//**********************************************************************************************************************
void BiomHDF5::printShared(string outputFileName, vector<string> sampleMetadata, Picrust* picrust) {
try {
//set required datasets - groupname -> datasetname
//"observation/ids" -> otuLabels - "GG_OTU_1", "GG_OTU_2", "GG_OTU_3", "GG_OTU_4", "GG_OTU_5
//"observation/matrix/data" -> otu abundances for each non zero abundnace entry - 1, 5, 1, 2, 3, 1, 1, 4, 2, 2, 1, 1, 1, 1, 1
//"observation/matrix/indices" -> index of group - maps into samples/ids 2, 0, 1, 3, 4, 5, 2, 3, 5, 0, 1, 2, 5, 1, 2
//"observation/matrix/indptr" -> maps non zero abundance to OTU - 0, 1, 6, 9, 13, 15 - 0 start of OTU1s indexes, 1 start of OTU2s indexes, ... 15 start of OTU5s indexes
/*
label group numOtus GG_OTU_1 GG_OTU_2 GG_OTU_3 GG_OTU_4 GG_OTU_5
userLabel Sample1 5 0 5 0 2 0
userLabel Sample2 5 0 1 0 2 1
userLabel Sample3 5 1 0 1 1 1
userLabel Sample4 5 0 2 4 0 0
userLabel Sample5 5 0 3 0 0 0
userLabel Sample6 5 0 1 2 1 0
*/
//"observation/metadata/taxonomy" -> taxonomy info - otu classifications
//"sample/ids" -> group names - "Sample1", "Sample2", "Sample3", "Sample4", "Sample5", "Sample6"
//"sample/metadata/" -> group metadata (optional)
//run this first because if picrust alters the shared vector we will need to use the updated info
//taxMetadata[0] = taxonomy for otu0
vector< vector<string> > taxMetadata = getMetaData(picrust);
//find number of non zero otus
nnz = 0;
for (int j = 0; j < shared->getNumBins(); j++) {
vector<int> thisOTU = shared->getOTU(j);
for (int i = 0; i < thisOTU.size(); i++) {
if (thisOTU[i] != 0) { nnz++; }
}
}
#ifdef USE_HDF5
H5::H5File file(outputFileName.c_str(), H5F_ACC_TRUNC );
try {
//print required file attributes
H5::Group fileAttributes(file.openGroup( "/" ));
printRequiredFileAttributes(fileAttributes, shared->getNumBins(), shared->size()); //id, type, format-url, format-version, generated-by, creation-date, shape, nnz
fileAttributes.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print H5 required file attributes.\n");
m->setControl_pressed(true);
}
try {
//print otuLabels called "observation/ids" in biom file
H5::Group observationGroup( file.createGroup( "observation" ));
printNames(observationGroup, shared->getOTUNames(), "ids");
observationGroup.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print otuLabels in 'observation/ids' group.\n");
m->setControl_pressed(true);
}
try {
//print group names called "sample/ids" in biom file
H5::Group sampleGroup( file.createGroup( "sample" ));
printNames(sampleGroup, shared->getNamesGroups(), "ids");
if (sampleMetadata.size() != 0) {
printNames(sampleGroup, sampleMetadata, "metadata");
}
sampleGroup.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print sample names or sample metadata.\n");
m->setControl_pressed(true);
}
try {
//print otuAbundances called "observation/matrix/" (data, indicies, indptr) in biom file
H5::Group matrixGroup( file.createGroup( "observation/matrix/" ));
printOTUAbundances(matrixGroup, shared->getNumBins(), shared->size(), shared->getLabel());
matrixGroup.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print otu abundances in 'observation/matrix/' group.\n");
m->setControl_pressed(true);
}
if (consTax.size() != 0) {
try {
//print otuTaxonomies called "observation/metadata/taxonomy" in the biom file
H5::Group taxonomyGroup( file.createGroup( "observation/metadata/" ));
printOTUTaxonomy(taxonomyGroup, "taxonomy");
taxonomyGroup.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print otu consensus taxonomies in 'observation/metadata/' group.\n");
m->setControl_pressed(true);
}
}
file.close();
#endif
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "printShared");
exit(1);
}
}
//**********************************************************************************************************************
void BiomHDF5::printFloat(string outputFileName, vector<string> sampleMetadata, Picrust* picrust) {
try {
//set required datasets - groupname -> datasetname
//"observation/ids" -> otuLabels - "GG_OTU_1", "GG_OTU_2", "GG_OTU_3", "GG_OTU_4", "GG_OTU_5
//"observation/matrix/data" -> otu abundances for each non zero abundnace entry - 1, 5, 1, 2, 3, 1, 1, 4, 2, 2, 1, 1, 1, 1, 1
//"observation/matrix/indices" -> index of group - maps into samples/ids 2, 0, 1, 3, 4, 5, 2, 3, 5, 0, 1, 2, 5, 1, 2
//"observation/matrix/indptr" -> maps non zero abundance to OTU - 0, 1, 6, 9, 13, 15 - 0 start of OTU1s indexes, 1 start of OTU2s indexes, ... 15 start of OTU5s indexes
/*
label group numOtus GG_OTU_1 GG_OTU_2 GG_OTU_3 GG_OTU_4 GG_OTU_5
userLabel Sample1 5 0 5 0 2 0
userLabel Sample2 5 0 1 0 2 1
userLabel Sample3 5 1 0 1 1 1
userLabel Sample4 5 0 2 4 0 0
userLabel Sample5 5 0 3 0 0 0
userLabel Sample6 5 0 1 2 1 0
*/
//"observation/metadata/taxonomy" -> taxonomy info - otu classifications
//"sample/ids" -> group names - "Sample1", "Sample2", "Sample3", "Sample4", "Sample5", "Sample6"
//"sample/metadata/" -> group metadata (optional)
//run this first because if picrust alters the shared vector we will need to use the updated info
//taxMetadata[0] = taxonomy for otu0
vector< vector<string> > taxMetadata = getMetaData(picrust);
//find number of non zero otus
nnz = 0; float zero = 0.0;
for (int j = 0; j < sharedFloat->getNumBins(); j++) {
vector<float> thisOTU = sharedFloat->getOTU(j);
for (int i = 0; i < thisOTU.size(); i++) {
if (util.isEqual(thisOTU[i], zero)) { nnz++; }
}
}
#ifdef USE_HDF5
H5::H5File file(outputFileName.c_str(), H5F_ACC_TRUNC );
try {
//print required file attributes
H5::Group fileAttributes(file.openGroup( "/" ));
printRequiredFileAttributes(fileAttributes, sharedFloat->getNumBins(), sharedFloat->size()); //id, type, format-url, format-version, generated-by, creation-date, shape, nnz
fileAttributes.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print H5 required file attributes.\n");
m->setControl_pressed(true);
}
try {
//print otuLabels called "observation/ids" in biom file
H5::Group observationGroup( file.createGroup( "observation" ));
printNames(observationGroup, sharedFloat->getOTUNames(), "ids");
observationGroup.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print otuLabels in 'observation/ids' group.\n");
m->setControl_pressed(true);
}
try {
//print group names called "sample/ids" in biom file
H5::Group sampleGroup( file.createGroup( "sample" ));
printNames(sampleGroup, sharedFloat->getNamesGroups(), "ids");
if (sampleMetadata.size() != 0) {
printNames(sampleGroup, sampleMetadata, "metadata");
}
sampleGroup.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print sample names or sample metadata.\n");
m->setControl_pressed(true);
}
try {
//print otuAbundances called "observation/matrix/" (data, indicies, indptr) in biom file
H5::Group matrixGroup( file.createGroup( "observation/matrix/" ));
printOTUAbundances(matrixGroup, sharedFloat->getNumBins(), sharedFloat->size(), sharedFloat->getLabel());
matrixGroup.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print otu abundances in 'observation/matrix/' group.\n");
m->setControl_pressed(true);
}
if (consTax.size() != 0) {
try {
//print otuTaxonomies called "observation/metadata/taxonomy" in the biom file
H5::Group taxonomyGroup( file.createGroup( "observation/metadata/" ));
printOTUTaxonomy(taxonomyGroup, "taxonomy");
taxonomyGroup.close();
}catch(H5::Exception& e){ //do nothing taxonomy info does not exist
m->mothurOut("[ERROR]: Unable to print otu consensus taxonomies in 'observation/metadata/' group.\n");
m->setControl_pressed(true);
}
}
file.close();
#endif
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "printFloat");
exit(1);
}
}
//**********************************************************************************************************************
void BiomHDF5::print(string outputFileName, vector<string> sampleMetadata, Picrust* picrust) {
try {
if (matrixElementType == "int") {
printShared(outputFileName, sampleMetadata, picrust);
}else {
printFloat(outputFileName, sampleMetadata, picrust);
}
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "print");
exit(1);
}
}
//**********************************************************************************************************************
vector< vector<string> > BiomHDF5::getMetaData(Picrust* picrust, bool useRelabund){
try {
vector< vector<string> > metadata;
if (consTax.size() == 0) {
if (!useRelabund) {
for (int i = 0; i < shared->getNumBins(); i++) { vector<string> temp; temp.push_back("null"); metadata.push_back(temp); } }
else {
for (int i = 0; i < sharedFloat->getNumBins(); i++) { vector<string> temp; temp.push_back("null"); metadata.push_back(temp); }
}
}
else {
if (!useRelabund) {
if (shared == nullptr) { m->setControl_pressed(true); return metadata; }
}else {
if (sharedFloat == nullptr) { m->setControl_pressed(true); return metadata; }
}
//should the labels be Otu001 or PhyloType001
vector<string> otuNames;
if (!useRelabund) { otuNames = shared->getOTUNames(); }
else { otuNames = sharedFloat->getOTUNames(); }
string firstBin = otuNames[0];
string binTag = "Otu";
if ((firstBin.find("Otu")) == string::npos) { binTag = "PhyloType"; }
map<string, string> labelTaxMap;
string snumBins = toString(otuNames.size());
for (int i = 0; i < consTax.size(); i++) {
if (m->getControl_pressed()) { return metadata; }
string thisOtuLabel = consTax[i].getName();
//if there is a bin label use it otherwise make one
if (util.isContainingOnlyDigits(thisOtuLabel)) {
string binLabel = binTag;
string sbinNumber = thisOtuLabel;
if (sbinNumber.length() < snumBins.length()) {
int diff = snumBins.length() - sbinNumber.length();
for (int h = 0; h < diff; h++) { binLabel += "0"; }
}
binLabel += sbinNumber;
binLabel = util.getSimpleLabel(binLabel);
labelTaxMap[binLabel] = consTax[i].getConsTaxString();
}else {
map<string, string>::iterator it = labelTaxMap.find(util.getSimpleLabel(thisOtuLabel));
if (it == labelTaxMap.end()) {
labelTaxMap[util.getSimpleLabel(thisOtuLabel)] = consTax[i].getConsTaxString();
}else {
m->mothurOut("[ERROR]: Cannot add OTULabel " + thisOtuLabel + " because it's simple label " + util.getSimpleLabel(consTax[i].getName()) + " has already been added and will result in downstream errors. Have you mixed mothur labels and non mothur labels? To make the files work well together and backwards compatible mothur treats 1, OTU01, OTU001, OTU0001 all the same. We do this by removing any non numeric characters and leading zeros. For eaxample: Otu000018 and OtuMY18 both map to 18.\n"); m->setControl_pressed(true);
}
}
}
//sanity check for file issues - do you have the same number of bins in the shared and constaxonomy file
if (!useRelabund) {
if (shared->getNumBins() != labelTaxMap.size()) {
m->mothurOut("[ERROR]: Your constaxonomy file contains " + toString(labelTaxMap.size()) + " otus and your shared file contain " + toString(shared->getNumBins()) + " otus, cannot continue.\n"); m->setControl_pressed(true); return metadata;
}
}else {
if (sharedFloat->getNumBins() != labelTaxMap.size()) {
m->mothurOut("[ERROR]: Your constaxonomy file contains " + toString(labelTaxMap.size()) + " otus and your shared file contain " + toString(sharedFloat->getNumBins()) + " otus, cannot continue.\n"); m->setControl_pressed(true); return metadata;
}
}
//merges OTUs classified to same gg otuid, sets otulabels to gg otuids, averages confidence scores of merged otus. overwritting of otulabels is fine because constaxonomy only allows for one label to be processed. If this assumption changes, could cause bug.
if (picrust != nullptr) {
if (!useRelabund) { picrust->setGGOTUIDs(labelTaxMap, shared); }
else { picrust->setGGOTUIDs(labelTaxMap, sharedFloat); }
}
//{"taxonomy":["k__Bacteria", "p__Proteobacteria", "c__Gammaproteobacteria", "o__Enterobacteriales", "f__Enterobacteriaceae", "g__Escherichia", "s__"]}
//traverse the binLabels forming the metadata strings and saving them
//make sure to sanity check
map<string, string>::iterator it;
vector<string> currentLabels; int numBins = 0;
if (!useRelabund) { currentLabels = shared->getOTUNames(); numBins = shared->getNumBins(); }
else { currentLabels = sharedFloat->getOTUNames(); numBins = sharedFloat->getNumBins();
}
for (int i = 0; i < numBins; i++) {
if (m->getControl_pressed()) { return metadata; }
it = labelTaxMap.find(util.getSimpleLabel(currentLabels[i]));
if (it == labelTaxMap.end()) { m->mothurOut("[ERROR]: can't find taxonomy information for " + currentLabels[i] + ".\n"); m->setControl_pressed(true); }
else {
vector<string> scores;
vector<string> taxonomies = util.parseTax(it->second, scores);
metadata.push_back(taxonomies);
}
}
}
return metadata;
}
catch(exception& e) {
m->errorOut(e, "BiomHDF5", "getMetadata");
exit(1);
}
}
/**************************************************************************************************/
|