Example #1
0
void exportUnalignedBlocks(
    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 nUnalignedBlocks = nexusReader.GetNumUnalignedBlocks(tb);
        if (nUnalignedBlocks == 0)
            continue;

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

        typedef std::pair<const NxsUnalignedBlock *, std::string> PairUBAndString;
        typedef std::vector< PairUBAndString > VecPairUBAndString;
        VecPairUBAndString ubToWrite;
        for (unsigned i = 0; i < nUnalignedBlocks; ++i)
        {
            NxsString fn = tbSpecificPrefix;
            if (i > 0)
                fn << (1 + i);
            fn << getFileExtension(f);

            const NxsUnalignedBlock * ub = nexusReader.GetUnalignedBlock(tb, i);

            NxsCharactersBlock::DataTypesEnum dt = ub->GetDataType();
            bool writeBlock = false;
            if ((dt == NxsCharactersBlock::dna || dt == NxsCharactersBlock::nucleotide) && (f == MultiFormatReader::FASTA_DNA_FORMAT))
                writeBlock = true;
            else if ((dt == NxsCharactersBlock::rna) && (f == MultiFormatReader::FASTA_RNA_FORMAT))
                writeBlock = true;
            else if ((dt == NxsCharactersBlock::protein) && (f == MultiFormatReader::FASTA_AA_FORMAT))
                writeBlock = true;

            if (writeBlock)
                ubToWrite.push_back(std::pair<const NxsUnalignedBlock *, std::string>(ub, fn));
        }
        if (!ubToWrite.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 (VecPairUBAndString::const_iterator vIt = ubToWrite.begin(); vIt != ubToWrite.end(); ++vIt)
            {
                const NxsUnalignedBlock * ubP = vIt->first;
                std::string fn = vIt->second;
                ofstream outf;
                if (fp == 0L)
                {
                    openOrThrow(outf, fn);
                    fpToUse = &outf;
                }

                writeUnalignedBlockToStream(*ubP, *fpToUse, namesToPrint, f, interleaveLen);
            }
        }
    }
}