Example #1
0
int processContent(PublicNexusReader & nexusReader,
                   const char *gFilename,
                   std::ostream *os,
                   INIReader & iniReader) {
    BlockReaderList blocks = nexusReader.GetUsedBlocksInOrder();
    if (blocks.size() == 0) {
        cerr << "Error:\n No understandable content was found.\n";
        exit(1);
    }
    const unsigned numTaxaBlocks = nexusReader.GetNumTaxaBlocks();
    if (numTaxaBlocks != 1) {
        std::cerr << "Expecting a file with exactly 1 TAXA block, but found " << numTaxaBlocks << " in the file " << gFilename << ".\n";
        return 2;
    }
    NxsTaxaBlock * taxaBlock = nexusReader.GetTaxaBlock(0);
    const unsigned nCharBlocks = nexusReader.GetNumCharactersBlocks(taxaBlock);
    if (nCharBlocks != 1) {
        std::cerr << "Expecting a file with exactly 1 CHARACTERS/DATA block, but found " << nCharBlocks << " in the file " << gFilename << ".\n";
        return 3;
    }
    const unsigned nTreesBlocks = nexusReader.GetNumTreesBlocks(taxaBlock);
    if (nTreesBlocks != 1) {
        std::cerr << "Expecting a file with exactly 1 TREES block, but found " << nTreesBlocks << " in the file " << gFilename << ".\n";
        return 3;
    }
    const  NxsCharactersBlock * charBlock = nexusReader.GetCharactersBlock(taxaBlock, 0);
    std::vector<const NxsDiscreteDatatypeMapper *> mappers = charBlock->GetAllDatatypeMappers();
    if (mappers.size() != 1) {
        std::cerr << "Expecting an unmixed characters block, but found a matrix with datatype = mixed or a datatype with augmented symbols\n";
        return 4;
    }
    const NxsDiscreteDatatypeMapper * dm = mappers[0];
    ScopedTwoDMatrix<NxsCDiscreteStateSet> compressedMatrix;
    std::vector<unsigned> patternCounts;
    std::vector<double> patternWeights;
    bool hasWeights = true;
    std::vector<NxsCharacterPattern> compressedTransposedMatrix;
    std::vector<std::set<unsigned> > compressedIndexToOriginal;
    std::vector<int> originalIndexToCompressed;
    if (true) {
        if (true) {
            NxsCXXDiscreteMatrix cxxMat(*charBlock, false, 0L, false);
            hasWeights = cxxMat.hasWeights();
            NxsCompressDiscreteMatrix(cxxMat, compressedTransposedMatrix, &originalIndexToCompressed, &compressedIndexToOriginal);
        }
        std::vector<double> * wtsPtr = (hasWeights ? &patternWeights : 0L);
        NxsTransposeCompressedMatrix(compressedTransposedMatrix, compressedMatrix, &patternCounts, wtsPtr);
        patternWeights.clear();
        for (auto i : patternCounts) {
            patternWeights.push_back(double(i));
        }
    }
    _DEBUG_VEC(patternWeights);
    NxsCDiscreteStateSet ** matrixAlias = compressedMatrix.GetAlias();
    const unsigned ntaxTotal =  charBlock->GetNTaxTotal();
    const  NxsTreesBlock * treesBlock = nexusReader.GetTreesBlock(taxaBlock, 0);
    //mt::ModelDescription md(mt::ModelDescription::VAR_ONLY_NO_MISSING_ASC_BIAS); //@TODO should be run-time setting
    mt::ModelDescription md(mt::ModelDescription::NO_ASC_BIAS); //@TODO should be run-time setting
    mt::NCL2MT ncl2mt;
    for (unsigned nti = 0; nti < treesBlock->GetNumTrees(); ++nti) {
        const NxsSimpleTree nst(treesBlock->GetFullTreeDescription(nti), 1, 0.1, true);
        ncl2mt.processTree(os,
                           ntaxTotal,
                           charBlock,
                           dm,
                           (const NxsCDiscreteStateSet **) matrixAlias,
                           patternWeights,
                           originalIndexToCompressed,
                           nst,
                           md,
                           iniReader);
    }
    return 0;
}
Example #2
0
void exportTrees(
    PublicNexusReader & nexusReader,
    MultiFormatReader::DataFormatType f,
    std::string prefix,
    std::ostream * fp)
{
    std::ostream * fpToUse = fp;
    const unsigned nTaxaBlocks = nexusReader.GetNumTaxaBlocks();
    for (unsigned t = 0; t < nTaxaBlocks; ++t)
    {
        const NxsTaxaBlock * tb = nexusReader.GetTaxaBlock(t);
        const unsigned nTreesBlocks = nexusReader.GetNumTreesBlocks(tb);
        if (nTreesBlocks == 0)
            continue;

        NxsString tbSpecificPrefix;
        if (t > 0)
            tbSpecificPrefix << (1 + t);
        tbSpecificPrefix << prefix;
        std::vector<std::string> namesToPrint(tb->GetAllLabels());
        std::vector<NxsNameToNameTrans> nameTrans = nameTranslationDict(namesToPrint, f);
        if (!nameTrans.empty())
        {
            namesToPrint.clear();
            for (std::vector<NxsNameToNameTrans>::const_iterator nIt = nameTrans.begin(); nIt != nameTrans.end(); ++nIt)
                namesToPrint.push_back(nIt->second);
            if (nexusReader.conversionOutputRecord.writeNameTranslationFile)
                nexusReader.conversionOutputRecord.writeNameTranslation(nameTrans, tb);
        }

        for (unsigned i = 0; i < nTreesBlocks; ++i)
        {
            NxsString fn = tbSpecificPrefix;
            if (i > 0)
                fn << (1 + i);
            fn << getFileExtension(f);

            const NxsTreesBlock * trb = nexusReader.GetTreesBlock(tb, i);

            ofstream outf;
            if (fp == 0L)
            {
                openOrThrow(outf, fn);
                fpToUse = &outf;
            }

            trb->ProcessAllTrees();
            for (unsigned j = 0; j < trb->GetNumTrees(); ++j)
            {
                const NxsFullTreeDescription & ftd = trb->GetFullTreeDescription(j);
                NxsSimpleTree tree(ftd, -1, -1.0);
                std::vector<NxsSimpleNode *> & leaves = tree.GetLeavesRef();
                for (std::vector<NxsSimpleNode *>::const_iterator leafIt = leaves.begin(); leafIt != leaves.end(); ++leafIt)
                {
                    NxsSimpleNode * leaf = *leafIt;
                    if (leaf)
                    {
                        const std::string name = namesToPrint[leaf->GetTaxonIndex()];
                        if (!gQuietMode)
                            std::cerr << "Setting name=" << name << '\n';
                        leaf->SetName(name);
                    }
                }
                tree.WriteAsNewick(*fpToUse, true, true, false, tb, true);
                *fpToUse << ";\n";
            }
        }
    }
}