void writeCoreAssumptions(ostream &out, NxsCharactersBlock * cb, const char * newTitle) { std::vector<int> intWts; std::vector<double> dblWts; NxsUnsignedSet activeExSet = cb->GetExcludedIndexSet(); const NxsTransformationManager &tm = cb->GetNxsTransformationManagerRef(); intWts = tm.GetDefaultIntWeights(); if (intWts.empty()) dblWts = tm.GetDefaultDoubleWeights(); if (!(activeExSet.empty() && intWts.empty() && dblWts.empty())) { out << "BEGIN ASSUMPTIONS; \n LINK CHARACTERS = "; out << NxsString::GetEscaped(newTitle) << " ;\n"; if (!activeExSet.empty()) { NxsString exsetName; exsetName << newTitle; exsetName.append("ExSet"); NxsUnsignedSetMap m; m[exsetName] = activeExSet; NxsWriteSetCommand("EXSET", m, out, exsetName.c_str());; } if (!(intWts.empty() && dblWts.empty())) { NxsTransformationManager &cbntm = cb->GetNxsTransformationManagerRef(); const std::string &wtSetName = cbntm.GetDefaultWeightSetName(); NxsTransformationManager ntm; if (!intWts.empty()) { NxsTransformationManager::ListOfIntWeights iw; vecToListOfIntWeights(intWts, iw); ntm.AddIntWeightSet(wtSetName, iw, true); } else { NxsTransformationManager::ListOfDblWeights dw; vecToListOfDblWeights(dblWts, dw); ntm.AddRealWeightSet(wtSetName, dw, true); } ntm.WriteWtSet(out); } out << "END;\n"; } }
void writeCharactersAsGapped(ostream &out, NxsCharactersBlock * cb) { if (!cb ) return; NxsTaxaBlockAPI *tb = cb->GetTaxaBlockPtr(NULL); if (tb == NULL) return; NxsCharactersBlock::DataTypesEnum dt = cb->GetDataType(); const char g = cb->GetGapSymbol(); const std::string baseTitle = cb->GetTitle(); if (tb == NULL || dt == NxsCharactersBlock::standard || dt == NxsCharactersBlock::continuous || dt == NxsCharactersBlock::mixed || (g == '\0' && !gGapCodeAllPresentSites)) { cb->WriteAsNexus(out); writeCoreAssumptions(out, cb, baseTitle.c_str()); return; } bool isAutogen = cb->IsAutoGeneratedTitle(); std::string newTitle = baseTitle; newTitle.append("GapsAsMissing"); cb->SetTitle(newTitle, isAutogen); std::set<unsigned> gappedColumns; std::set<unsigned> residueColumns; out << "BEGIN CHARACTERS;\n"; cb->WriteBasicBlockCommands(out); const unsigned ntaxTotal = tb->GetNTax(); out << " DIMENSIONS"; if (tb) { const unsigned wod = cb->GetNTaxWithData(); if (wod > 0 && wod != ntaxTotal) out << " NTax=" << wod; } const unsigned nc = cb->GetNCharTotal(); out << " NChar=" << nc << ";\n"; cb->WriteEliminateCommand(out); cb->SetGapSymbol('\0'); cb->WriteFormatCommand(out); cb->SetGapSymbol(g); cb->WriteCharStateLabelsCommand(out); std::vector<int> intWts; std::vector<double> dblWts; NxsUnsignedSet activeExSet = cb->GetExcludedIndexSet(); const NxsTransformationManager &tm = cb->GetNxsTransformationManagerRef(); intWts = tm.GetDefaultIntWeights(); if (intWts.empty()) dblWts = tm.GetDefaultDoubleWeights(); unsigned width = tb->GetMaxTaxonLabelLength(); out << "Matrix\n"; unsigned begCharInd = 0; unsigned endCharInd = nc; for (unsigned i = 0; i < ntaxTotal; i++) { if (cb->TaxonIndHasData(i)) { const std::string currTaxonLabel = NxsString::GetEscaped(tb->GetTaxonLabel(i)); out << currTaxonLabel; unsigned currTaxonLabelLen = (unsigned)currTaxonLabel.size(); unsigned diff = width - currTaxonLabelLen; for (unsigned k = 0; k < diff + 5; k++) out << ' '; const NxsDiscreteStateRow & row = cb->GetDiscreteMatrixRow(i); if (!row.empty()) { const NxsDiscreteDatatypeMapper * dm = cb->GetDatatypeMapperForChar(0); if (dm == NULL) throw NxsNCLAPIException("No DatatypeMapper in WriteStatesForTaxonAsNexus"); if (cb->IsMixedType()) throw NxsNCLAPIException("Mixed datatypes are not supported by the NEXUSgapcode program."); if (cb->IsTokens()) { for (unsigned charInd = begCharInd; charInd < endCharInd; ++charInd) { int sc = row[charInd]; out << ' '; if (sc == NXS_GAP_STATE_CODE) out << dm->GetMissingSymbol(); else { NxsString sl = cb->GetStateLabel(charInd, sc); /*v2.1to2.2 2 */ if (sl == " ") { NxsString errormsg = "Writing character state "; errormsg << 1 + sc << " for character " << 1+charInd << ", but no appropriate chararcter label or symbol was found."; throw NxsNCLAPIException(errormsg); } else out << NxsString::GetEscaped(sl); } } } else { std::vector<NxsDiscreteStateCell>::const_iterator endIt = row.begin() + begCharInd; std::vector<NxsDiscreteStateCell>::const_iterator begIt = endIt; if (endCharInd == row.size()) endIt = row.end(); else endIt += endCharInd - begCharInd; unsigned j = begCharInd; for (; begIt != endIt; ++begIt, ++j) { NxsDiscreteStateCell c; if (*begIt == NXS_GAP_STATE_CODE) { c = NXS_MISSING_CODE; gappedColumns.insert(j); } else { c = *begIt; if (gGapCodeAllPresentSites) gappedColumns.insert(j); residueColumns.insert(j); } dm->WriteStateCodeAsNexusString(out, c, true); } } } out << '\n'; } } out << ";\n"; cb->WriteSkippedCommands(out); out << "END;\n"; writeCoreAssumptions(out, cb, newTitle.c_str()); const unsigned nGappedCols = gappedColumns.size(); if (nGappedCols > 0) { newTitle = baseTitle; newTitle.append("GapsAsBinary"); cb->SetTitle(newTitle, isAutogen); out << "BEGIN CHARACTERS;\n"; cb->WriteBasicBlockCommands(out); out << " DIMENSIONS"; if (tb) { const unsigned wod = cb->GetNTaxWithData(); if (wod > 0 && wod != ntaxTotal) out << " NTax=" << wod; } out << " NChar=" << nGappedCols << ";\n"; out << " CharStateLabels " ; unsigned currChNumber = 1; std::set<unsigned>::iterator gcIt = gappedColumns.begin(); out << currChNumber++ << " col_" << (1 + *gcIt); for (++gcIt ; gcIt != gappedColumns.end(); ++gcIt) out << ",\n " << currChNumber++ << " col_" << (1 + *gcIt); out << " ;\n" ; out << "Format Datatype = Standard Symbols=\"01\" missing = '?' ;\n"; out << "Matrix\n"; for (unsigned i = 0; i < ntaxTotal; i++) { if (cb->TaxonIndHasData(i)) { const std::string currTaxonLabel = NxsString::GetEscaped(tb->GetTaxonLabel(i)); out << currTaxonLabel; unsigned currTaxonLabelLen = (unsigned)currTaxonLabel.size(); unsigned diff = width - currTaxonLabelLen; for (unsigned k = 0; k < diff + 5; k++) out << ' '; const NxsDiscreteStateRow & row = cb->GetDiscreteMatrixRow(i); for (std::set<unsigned>::iterator cIt = gappedColumns.begin(); cIt != gappedColumns.end(); ++cIt) { NxsDiscreteStateCell sc = row[*cIt]; if (sc == NXS_GAP_STATE_CODE) { if (gGapCodeAllAbsentSites || (residueColumns.find(*cIt) != residueColumns.end())) out << '0'; else out << '?'; } else if (sc == NXS_MISSING_CODE) { out << '?'; } else { out << '1'; } } out << '\n'; } } out << ";\n"; cb->WriteSkippedCommands(out); out << "END;\n"; } std::vector<int> gapintWts; std::vector<double> gapdblWts; std::set<unsigned> gapactiveExSet; if (!activeExSet.empty()) { unsigned gapind = 0; for (std::set<unsigned>::iterator cIt = gappedColumns.begin(); cIt != gappedColumns.end(); ++cIt, ++gapind) { if (activeExSet.find(*cIt) != activeExSet.end()) gapactiveExSet.insert(gapind); } } if (!intWts.empty()) { for (std::set<unsigned>::iterator cIt = gappedColumns.begin(); cIt != gappedColumns.end(); ++cIt) gapintWts.push_back(intWts[*cIt]); } if (!dblWts.empty()) { for (std::set<unsigned>::iterator cIt = gappedColumns.begin(); cIt != gappedColumns.end(); ++cIt) gapdblWts.push_back(dblWts[*cIt]); } if (!(gapactiveExSet.empty() && gapintWts.empty() && gapdblWts.empty())) { out << "BEGIN ASSUMPTIONS; \n LINK CHARACTERS = "; out << NxsString::GetEscaped(newTitle) << " ;\n"; if (!gapactiveExSet.empty()) { NxsString exsetName; exsetName << newTitle; exsetName.append("GapExSet"); NxsUnsignedSetMap m; m[exsetName] = gapactiveExSet; NxsWriteSetCommand("EXSET", m, out, exsetName.c_str());; } if (!(gapintWts.empty() && gapdblWts.empty())) { NxsTransformationManager &cbntm = cb->GetNxsTransformationManagerRef(); std::string wtSetName = cbntm.GetDefaultWeightSetName(); wtSetName.append("GapWtSet"); NxsTransformationManager ntm; if (!gapintWts.empty()) { NxsTransformationManager::ListOfIntWeights iw; vecToListOfIntWeights(gapintWts, iw); ntm.AddIntWeightSet(wtSetName, iw, true); } else { NxsTransformationManager::ListOfDblWeights dw; vecToListOfDblWeights(gapdblWts, dw); ntm.AddRealWeightSet(wtSetName, dw, true); } ntm.WriteWtSet(out); } out << "END;\n"; } cb->SetTitle(baseTitle, isAutogen); }