void NxsReader::BlockReadHook(const NxsString &currBlockName, NxsBlock *currentBlock, NxsToken * token) { VecBlockPtr implied = currentBlock->GetImpliedBlocks(); for (VecBlockPtr::iterator impIt = implied.begin(); impIt != implied.end(); ++impIt) { NxsBlock * nb = *impIt; NCL_ASSERT(nb); NxsString impID = nb->GetID(); bool storeBlock = true; if (destroyRepeatedTaxaBlocks && impID.EqualsCaseInsensitive("TAXA")) { NxsTaxaBlockAPI * oldTB = this->GetOriginalTaxaBlock((NxsTaxaBlockAPI *) nb); if (oldTB) { storeBlock = ! currentBlock->SwapEquivalentTaxaBlock(oldTB); const std::string altTitle = nb->GetTitle(); this->RegisterAltTitle(oldTB, altTitle); if (!storeBlock) { delete nb; } } } if (storeBlock) { std::cerr << "storing implied block: " << impID << std::endl; this->AddBlockToUsedBlockList(impID, nb, token); } } std::cerr << "storing read block: " << currentBlock->GetID() << std::endl; this->AddBlockToUsedBlockList(currBlockName, currentBlock, token); }
/*! Reassign should be called if a block (`oldb') is about to be deleted (perhaps to make way for new data). Create the new block (`newb') before deleting `oldb', then call Reassign to replace `oldb' in `blockList' with `newb'. Assumes `oldb' exists and is in `blockList'. This function is necessary in v2.0, but replacement of blocks is more easily done with block factories in NCL v2.1 and higher. */ void NxsReader::Reassign( NxsBlock *oldb, /* a pointer to the block object soon to be deleted */ NxsBlock *newb) /* a pointer to oldb's replacement */ { NxsBlock *prev = NULL; NxsBlock *curr = blockList; newb->SetNexus(this); for (;; ) { if (curr == NULL || curr == oldb) { break; } prev = curr; curr = curr->next; } NCL_ASSERT(curr != NULL); newb->next = curr->next; if (prev == NULL) { blockList = newb; } else { prev->next = newb; } curr->next = NULL; curr->SetNexus(NULL); }
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); } }
void NormalizingReader::PostBlockReadingHook(NxsBlock & block) { //cerr << "PostBlockReadingHook for " << GetBlockIDTitleString(block) << endl; NxsBlock * toStore; if (this->storingBlocks) { try { toStore = block.CloneBlock(blockMapper); } catch (...) { cerr << "CloneBlock of " << GetBlockIDTitleString(block) << " failed with an exception. Only Clonable blocks can be normalized." << endl; throw; } blocksToDelete.push_back(toStore); blockMapper[block.GetInstanceIdentifier()] = toStore; } else toStore = █ /* VecBlockPtr impliedBlocks = toStore->GetImpliedBlocks(); for (VecBlockPtr::const_iterator ibIt = impliedBlocks.begin(); ibIt != impliedBlocks.end(); ++ibIt) { const BlockUniqueID currBlockID = (*ibIt)->GetInstanceIdentifier(); if ((!this->supportingSharedImpliedBlocks) || writtenBlocks.find(currBlockID) == writtenBlocks.end()) { if (this->storingBlocks) blocksToDelete.push_back(*ibIt); try { if (outf != 0L) processNexusBlock(*outf, *ibIt); } catch (...) { cerr << block.GetInstanceName() << "raised an exception when writing as NEXUS." << endl; throw; } if (this->supportingSharedImpliedBlocks) writtenBlocks.insert(currBlockID); } } */ const BlockUniqueID toStoreBlockID = toStore->GetInstanceIdentifier(); try { if (outf != 0L) processNexusBlock(*outf, toStore); } catch (...) { cerr << GetBlockIDTitleString(block) << " raised an exception when writing as NEXUS." << endl; throw; } writtenBlocks.insert(toStoreBlockID); }
std::string GetBlockIDTitleString(NxsBlock &b) { const std::string &t = b.GetInstanceName(); std::string r = b.GetID(); r.append(" block"); if (t.length() > 0) { r.append(" ("); r.append(t); r.append(")"); } return r; }
//////////////////////////////////////////////////////////////////////////////// // 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"; } }
/*---------------------------------------------------------------------------------------------------------------------- | Nothing to be done. */ NxsReader::~NxsReader() { NxsBlock *curr; for (curr = blockList; curr;) { if (curr->GetNexus() == this) curr->SetNexus(NULL); curr = curr->next; } for (BlockReaderList::iterator b = blocksInOrder.begin(); b != blocksInOrder.end(); ++b) { if ((*b)->GetNexus() == this) (*b)->SetNexus(NULL); } }
//////////////////////////////////////////////////////////////////////////////// // 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); } } }
/*---------------------------------------------------------------------------------------------------------------------- | Uses the registered Factories to spawn a reader for blocks with the name "currBlockName." | if sourceOfBlock is not NULL, then *sourceOfBlock will alias to the NxsBlockFactory used. | Returns NULL (and does not modify *sourceOfBlock), if no Factory is found that returns a block */ NxsBlock *NxsReader::CreateBlockFromFactories(const std::string & currBlockName, NxsToken &token, NxsBlockFactory **sourceOfBlock) { for (BlockFactoryList::iterator fIt = factories.begin(); currBlock == NULL && fIt != factories.end(); ++fIt) { NxsBlock *b = (*fIt)->GetBlockReaderForID(currBlockName, this, &token); if (b) { if (b->CanReadBlockType(token)) { if (sourceOfBlock) *sourceOfBlock = *fIt; b->SetNexus(this); return b; } (*fIt)->BlockError(b); } } return NULL; }