Beispiel #1
0
BlockReaderList NxsReader::FindAllBlocksByTitleNoPrioritization(const BlockReaderList & chosenBlockList, const char *title)
{
    BlockReaderList found;
    if (chosenBlockList.empty() || title == NULL)
    {
        found = chosenBlockList;
    }
    else
    {
        bool emptyTitle = strlen(title) == 0;
        for (BlockReaderList::const_iterator cblIt = chosenBlockList.begin(); cblIt != chosenBlockList.end(); ++cblIt)
        {
            NxsBlock * b = *cblIt;
            std::vector<std::string> v = this->GetAllTitlesForBlock(b);
            for (std::vector<std::string>::const_iterator vIt = v.begin(); vIt != v.end(); ++vIt)
            {
                const std::string & n = *vIt;
                if ((emptyTitle && n.empty()) || (NxsString::case_insensitive_equals(title, n.c_str())))
                {
                    found.push_back(b);
                    break;
                }
            }
        }
    }
    return found;

}
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);
  }
}
Beispiel #3
0
void NxsReader::DemoteBlocks(int priorityLevel)
{
    BlockReaderList brl = GetUsedBlocksInOrder();
    BlockReaderList::iterator brlIt = brl.begin();
    for (; brlIt != brl.end(); ++brlIt)
    {
        NxsBlock * b = *brlIt;
        AssignBlockPriority(b, priorityLevel);
    }
}
Beispiel #4
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";
	}

}
Beispiel #5
0
BlockReaderList NxsReader::FindAllBlocksByTitle(const BlockReaderList & chosenBlockList, const char *title)
{
    BlockReaderList found = FindAllBlocksByTitleNoPrioritization(chosenBlockList, title);
    if (found.empty())
    {
        return found;
    }
    map<int, BlockReaderList> byPriority;
    for (BlockReaderList::const_iterator fIt = found.begin(); fIt != found.end(); ++fIt)
    {
        NxsBlock * b = *fIt;
        int priority = GetBlockPriority(b);
        byPriority[priority].push_back(b);
    }
    NCL_ASSERT(!byPriority.empty());
    return byPriority.rbegin()->second;
}
Beispiel #6
0
/*! \returns a NxsBlock from `chosenBlockList` with a Title that matches `title`.
    In the event of ties, the most recently read block is returned.
    If `title` is NULL, then any block is considered a match.
    On output *nMatches will be the number of matches (if `nMatches` is not NULL).
    NULL will be returned if there are no matches.
 */
NxsBlock *NxsReader::FindBlockByTitle(const BlockReaderList & chosenBlockList, const char *title, unsigned *nMatches)
{
    BlockReaderList found = FindAllBlocksByTitle(chosenBlockList, title);

    if (found.empty())
    {
        if (nMatches)
        {
            *nMatches = 0;
        }
        return NULL;
    }
    if (nMatches)
    {
        *nMatches = (unsigned)found.size();
    }
    return (NxsBlock *) found.back();
}
Beispiel #7
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);
			}
		}
	}
Beispiel #8
0
void NxsReader::DeleteBlocksFromFactories()
	{
	BlockReaderList saved;
	std::set<NxsBlock *> todel;
	for (BlockReaderList::iterator bIt = blocksInOrder.begin(); bIt != blocksInOrder.end(); ++bIt)
		{
		NxsBlock * b  = *bIt;
		if (BlockIsASingeltonReader(b))
			saved.push_back(b);
		else
			todel.insert(b);
		}
	for (std::set<NxsBlock *>::iterator d = todel.begin(); d != todel.end(); ++d) 
		{
		
		delete *d;
		}
	blocksInOrder = saved;
	lastExecuteBlocksInOrder = saved;
	}
Beispiel #9
0
/*! Returns the set of blocks that have been created from factories, and
    removes reference to from the NxsReader's collections.
 */
std::set<NxsBlock *> NxsReader::RemoveBlocksFromFactoriesFromUsedBlockLists()
{
    std::set<NxsBlock *> todel;
    BlockReaderList saved;
    for (BlockReaderList::iterator bIt = blocksInOrder.begin(); bIt != blocksInOrder.end(); ++bIt)
    {
        NxsBlock * b  = *bIt;
        if (BlockIsASingeltonReader(b))
        {
            saved.push_back(b);
        }
        else
        {
            todel.insert(b);
        }
    }
    for (std::set<NxsBlock *>::iterator d = todel.begin(); d != todel.end(); ++d)
    {
        RemoveBlockFromUsedBlockList(*d);
    }
    return todel;
}
Beispiel #10
0
void BullShell::ExecGetTrees(const GetTreesOpts & gto) {
	errormsg.clear();
	BlockReaderList blocks = getBlocksFromFile(gto.filename, true);
	if (blocks.empty()) {
		errormsg << "No Trees Block found in " << gto.filename;
		throw XBull(errormsg);
	}
	
	unsigned ntreesInFile = 0;
	const unsigned ntreesBlocks = blocks.size();
	vector<NxsTreesBlockAPI *> trBlocks;
	for (BlockReaderList::iterator b = blocks.begin(); b != blocks.end(); ++b) {
		NxsTreesBlockAPI *tb = (NxsTreesBlockAPI *)(*b);
		trBlocks.push_back(tb);
		assert(tb);
		ntreesInFile += tb->GetNumTrees();
	}

	if (ntreesInFile < 1) {
		errormsg << "No Trees found in " << gto.filename;
		throw XBull(errormsg);
	}
	
	const unsigned toTree = (unsigned) (gto.toTree < 0 ? 0 : gto.toTree); 
	const unsigned fromTree = (unsigned) (gto.fromTree < 0 ? ntreesInFile - 1 : gto.fromTree);
	if (fromTree > toTree) {
		errormsg << "Cant get Trees from " << fromTree << " to " << toTree;
		throw XBull( errormsg);
	}

	// Store the specified trees
	// these trees are deleted in the for loop if there is an error, otherwise
	//	they are put under the control of the kernel
	vector<Tree *>	treesToBeAdded; 
	vector<NxsTreesBlockAPI *>::const_iterator tbIt = trBlocks.begin();
	NxsTreesBlockAPI * currTB = *tbIt;
	unsigned nTreesInThisBlock = currTB->GetNumTrees();
	int nleftInThisBlock = (int) nTreesInThisBlock;
	for (unsigned i = 0; i < toTree; i++, --nleftInThisBlock) {
		while (nleftInThisBlock < 1) {
			++tbIt;
			assert(tbIt != trBlocks.end());
			currTB = *tbIt;
			nTreesInThisBlock = currTB->GetNumTrees();
			nleftInThisBlock = (int) nTreesInThisBlock;
		}
		if (i >= fromTree) {
			const std::string newick = currTB->GetTranslatedTreeDescription((unsigned)(nTreesInThisBlock - nleftInThisBlock));
			Tree * temptree = new Tree(newick, gto.storeBrLensFromFile);
			if (!temptree->IsGood()) {
				for (unsigned j = 0; j < i - fromTree; j++)
					delete treesToBeAdded[j];
				errormsg << "Problem Reading Tree Description of " << currTB->GetTreeName(i);
				delete temptree;
				throw XBull(errormsg);
			}
			std::string s = currTB->GetTreeName(i);
			ToUpper(s);
			temptree->SetName(s);
			treesToBeAdded.push_back(temptree);
		}
	}

	ioObject.message.clear();
	ioObject.message << (int)(toTree - fromTree) << " trees read from " << ntreesBlocks << " TREES block(s) in " <<  gto.filename;
	ioObject.printMessage(BullIO::STATUS_MSG_LEVEL);

	assert(gto.mode <= 7 && gto.mode >= 0);
	kernel.updateTrees(treesToBeAdded, UpdateMode(gto.mode));
	for (BlockReaderList::iterator b = blocks.begin(); b != blocks.end(); ++b) {
		if (*b != this)
			delete *b;
	}
}		
Beispiel #11
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;
}