void writeAsNexus(PublicNexusReader & nexusReader, ostream & os) {
  BlockReaderList blocks = nexusReader.GetUsedBlocksInOrder();
  os << "#NEXUS\n";
  for (BlockReaderList::const_iterator bIt = blocks.begin(); bIt != blocks.end(); ++bIt) {
    NxsBlock * b = *bIt;
    if (b)
      b->WriteAsNexus(os);
  }
}
Example #2
0
////////////////////////////////////////////////////////////////////////////////
// Takes NxsReader that has successfully read a file, and processes the
//	information stored in the reader.
//
// The caller is responsibel for calling DeleteBlocksFromFactories() to clean
//	up (if the reader uses the factory API).
////////////////////////////////////////////////////////////////////////////////
void processContent(PublicNexusReader & nexusReader, ostream *out)
{
	if (!out)
		return;
	BlockReaderList blocks = nexusReader.GetUsedBlocksInOrder();

	*out << "#NEXUS\n";
	for (BlockReaderList::const_iterator bIt = blocks.begin(); bIt != blocks.end(); ++bIt)
		{
		NxsBlock * b = *bIt;
		if (b->GetID() == "TAXA")
			b->WriteAsNexus(*out);
		}
	TreesToSplits::TaxaBlockToSplitsMap::const_iterator tbtsm = gTreesToSplitsB->taxaBlockToSplitsMap.begin();
	for (; tbtsm != gTreesToSplitsB->taxaBlockToSplitsMap.end(); ++tbtsm) {
		*out << "Begin Trees;\n";
		NxsTaxaBlockAPI * tb = tbtsm->first;
		if (tb && gTreesToSplitsB->taxaBlockToSplitsMap.size() > 1)
			*out << "Link Taxa = " << NxsString::GetEscaped(tb->GetTitle()) << ";\n";
		writeTranslateCommand(tb, *out);
		const TreesToSplits::NTreesSplitsMap & ntsm = tbtsm->second;
		const unsigned nt = ntsm.first;
		const TreesToSplits::SplitsMap & sm = ntsm.second;
		unsigned n = 1;
		writeStarTreeCommand(tb, *out);


		for (TreesToSplits::SplitsMap::const_iterator smIt = sm.begin(); smIt != sm.end(); ++smIt, ++n) {
			const SplitInfo & si = smIt->second;
			*out << "Tree split_" << n << " = " << " [&";
			*out << (gTreesToSplitsB->treatAsRooted ? 'R' : 'U');
			*out << "] [";
			if (gTreesToSplitsB->trackFreq || gTreesToSplitsB->trackEdgeLenSummary || gTreesToSplitsB->trackEdgeLen)
				*out << "&W " << ((double)si.nTimes)/((double)nt) << " ] [";
			if (gTreesToSplitsB->trackHeightSummary) {
				*out << "meanH =" << ((double)si.heightSum)/((double)si.nTimes);
			}
			*out << "] ";

			if (gTreesToSplitsB->trackEdgeLenSummary)
				smIt->first.writeNewick(*out, si.edgeLenSum/((double)si.nTimes), true);
			else
				smIt->first.writeNewick(*out, true);
			*out << ";\n";

		}
		*out << "End;\n";
	}

}
Example #3
0
////////////////////////////////////////////////////////////////////////////////
// Takes NxsReader that has successfully read a file, and processes the
//	information stored in the reader.
//
// The caller is responsibel for calling DeleteBlocksFromFactories() to clean
//	up (if the reader uses the factory API).
////////////////////////////////////////////////////////////////////////////////
void processContent(PublicNexusReader & nexusReader, ostream *out)
	{
	if (!out)
		return;
	BlockReaderList blocks = nexusReader.GetUsedBlocksInOrder();

	*out << "#NEXUS\n";
	for (BlockReaderList::const_iterator bIt = blocks.begin(); bIt != blocks.end(); ++bIt)
		{
		NxsBlock * b = *bIt;
		if (b->GetID() == "TAXA")
			b->WriteAsNexus(*out);
		else if (b->GetID() == "CHARACTERS" || b->GetID() == "DATA" )
			{
			NxsCharactersBlock * charactersBPtr = (NxsCharactersBlock *) b;
			writeCharactersAsGapped(*out, charactersBPtr);
			}
		}
	}
Example #4
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 #5
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";
            }
        }
    }
}
Example #6
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);
            }
        }
    }
}
Example #7
0
bool substituteSafeTaxaLabels(PublicNexusReader &nexusReader, std::string filepath, bool evenTrivial)
{
    std::ostream * outStrPtr = 0L;
    std::ofstream outStream;
    const unsigned nTaxaBlocks = nexusReader.GetNumTaxaBlocks();
    bool someChanged = false;
    std::set<std::string> safeNameSet;
    try
    {
        for (unsigned t = 0; t < nTaxaBlocks; ++t)
        {
            NxsTaxaBlock * tb = nexusReader.GetTaxaBlock(t);
            std::vector<std::string> safeLabelsVec;

            bool changed = constructSafeLabels(*tb, &safeLabelsVec, safeNameSet);
            someChanged = someChanged | changed;
            if (changed || evenTrivial)
            {
                if (outStrPtr == 0L)
                {
                    outStream.open(filepath.c_str());
                    if (!outStream.good())
                    {
                        NxsString eMessage;
                        eMessage << "Could not open the file \"" << filepath << "\"";
                        throw NxsException(eMessage);
                    }
                    outStrPtr = &outStream;
                    outStream << "#NEXUS\n";
                }
                tb->WriteAsNexus(*outStrPtr);
                std::vector<NxsString> oldNameVec;
                const unsigned numTaxa = tb->GetNTaxTotal();
                for (unsigned i = 0; i < numTaxa; ++i)
                {
                    NxsString oldName = tb->GetTaxonLabel(i);
                    oldNameVec.push_back(oldName);
                    NxsString newName;
                    newName << safeLabelsVec[i];
                    tb->ChangeTaxonLabel(i, newName);
                }
                NxsString title;
                title << tb->GetTitle();
                NxsString nt;
                nt << "Renamed " << title;
                tb->SetTitle(nt, false);
                tb->WriteAsNexus(*outStrPtr);

                // We can't just create a TaxaAssociationBlock instance here,
                //  and call WriteAsNexus because it first and second taxa block pointers
                //  would refer to the same instance...
                NxsString taTitle;
                taTitle << "Renaming " << title;
                *outStrPtr << "BEGIN TaxaAssociation;\n  Title " << NxsString::GetEscaped(taTitle) << " ;\n";
                *outStrPtr << "  Taxa " << NxsString::GetEscaped(title) << " , " << NxsString::GetEscaped(nt) << " ;\n";
                *outStrPtr << "  Associates ";
                for (unsigned i = 0; i < numTaxa; ++i)
                {
                    if (i > 0)
                        *outStrPtr << " ,\n    ";
                    *outStrPtr <<  NxsString::GetEscaped(oldNameVec[i]) << " / " << NxsString::GetEscaped(safeLabelsVec[i]);
                }
                *outStrPtr << " ;\nEND;\n" ;
            }
        }
    }
    catch (...)
    {
        if (outStrPtr != 0L)
            outStream.close();
        throw;
    }

    if (outStrPtr != 0L)
        outStream.close();
    return someChanged;
}
////////////////////////////////////////////////////////////////////////////////
// Takes NxsReader that has successfully read a file, and processes the
//	information stored in the reader.
//
// The caller is responsibel for calling DeleteBlocksFromFactories() to clean
//	up (if the reader uses the factory API).
////////////////////////////////////////////////////////////////////////////////
void processContent(PublicNexusReader & nexusReader, ostream *os, ProcessActionsEnum currentAction) {
  BlockReaderList blocks = nexusReader.GetUsedBlocksInOrder();
  writeAsNexus(nexusReader, *os);
}