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 exportCharacters(
    PublicNexusReader & nexusReader,
    MultiFormatReader::DataFormatType f,
    long interleaveLen,
    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 nCharBlocks = nexusReader.GetNumCharactersBlocks(tb);
        if (nCharBlocks == 0)
            continue;

        NxsString tbSpecificPrefix;
        if (t > 0)
            tbSpecificPrefix << (1 + t);
        tbSpecificPrefix << prefix;

        typedef std::pair<const NxsCharactersBlock *, std::string> PairCBAndString;
        typedef std::vector< PairCBAndString > VecPairCBAndString;
        VecPairCBAndString cbToWrite;
        for (unsigned i = 0; i < nCharBlocks; ++i)
        {
            NxsString fn = tbSpecificPrefix;
            if (i > 0)
                fn << (1 + i);
            fn << getFileExtension(f);

            const NxsCharactersBlock * cb = nexusReader.GetCharactersBlock(tb, i);

            NxsCharactersBlock::DataTypesEnum dt = cb->GetDataType();
            bool writeBlock = false;
            if ((dt == NxsCharactersBlock::standard)
                    && (f == MultiFormatReader::PHYLIP_DISC_FORMAT
                        || f == MultiFormatReader::INTERLEAVED_PHYLIP_DISC_FORMAT
                        || f == MultiFormatReader::RELAXED_PHYLIP_DISC_FORMAT
                        || f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DISC_FORMAT))
                writeBlock = true;
            else if ((dt == NxsCharactersBlock::dna || dt == NxsCharactersBlock::nucleotide)
                     && (f == MultiFormatReader::PHYLIP_DNA_FORMAT
                         || f == MultiFormatReader::INTERLEAVED_PHYLIP_DNA_FORMAT
                         || f == MultiFormatReader::FASTA_DNA_FORMAT
                         || f == MultiFormatReader::RELAXED_PHYLIP_DNA_FORMAT
                         || f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DNA_FORMAT))
                writeBlock = true;
            else if ((dt == NxsCharactersBlock::rna)
                     && (f == MultiFormatReader::PHYLIP_RNA_FORMAT
                         || f == MultiFormatReader::INTERLEAVED_PHYLIP_RNA_FORMAT
                         || f == MultiFormatReader::FASTA_RNA_FORMAT
                         || f == MultiFormatReader::RELAXED_PHYLIP_RNA_FORMAT
                         || f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_RNA_FORMAT))
                writeBlock = true;
            else if ((dt == NxsCharactersBlock::protein)
                     && (f == MultiFormatReader::PHYLIP_AA_FORMAT
                         || f == MultiFormatReader::INTERLEAVED_PHYLIP_AA_FORMAT
                         || f == MultiFormatReader::FASTA_AA_FORMAT
                         || f == MultiFormatReader::RELAXED_PHYLIP_AA_FORMAT
                         || f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_AA_FORMAT))
                writeBlock = true;

            if (writeBlock)
                cbToWrite.push_back(std::pair<const NxsCharactersBlock *, std::string>(cb, fn));
        }
        if (!cbToWrite.empty())
        {
            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 (VecPairCBAndString::const_iterator vIt = cbToWrite.begin(); vIt != cbToWrite.end(); ++vIt)
            {
                const NxsCharactersBlock * cbP = vIt->first;
                std::string fn = vIt->second;
                ofstream outf;
                if (fp == 0L)
                {
                    openOrThrow(outf, fn);
                    fpToUse = &outf;
                }

                writeCharactersBlockToStream(*cbP, *fpToUse, namesToPrint, f, interleaveLen);
            }
        }
    }
}