00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef NCL_NXSTREESBLOCK_H
00021 #define NCL_NXSTREESBLOCK_H
00022 #include <climits>
00023 #include <cfloat>
00024 #include "ncl/nxsdefs.h"
00025 #include "ncl/nxstaxablock.h"
00026
00027 class NxsTreesBlockAPI
00028 : public NxsBlock, public NxsLabelToIndicesMapper
00029 {
00030 public:
00031 virtual unsigned GetNumDefaultTree() = 0;
00032 virtual unsigned GetNumTrees() = 0;
00033 virtual NxsString GetTreeName(unsigned i) = 0;
00034 virtual NxsString GetTreeDescription(unsigned i) = 0;
00035 virtual NxsString GetTranslatedTreeDescription(unsigned i) = 0;
00036 virtual bool IsDefaultTree(unsigned i) = 0;
00037 virtual bool IsRootedTree(unsigned i) = 0;
00038 };
00046 std::string parseNHXComment(const std::string comment,
00047 std::map<std::string, std::string> *infoMap);
00048 class NxsFullTreeDescription;
00049 class NxsSimpleNode;
00052 class NxsSimpleEdge
00053 {
00054 public:
00055 bool EdgeLenIsDefaultValue() const
00056 {
00057 return defaultEdgeLen;
00058 }
00059
00060 bool IsIntEdgeLen() const
00061 {
00062 return hasIntEdgeLens;
00063 }
00064
00065 double GetDblEdgeLen() const
00066 {
00067 return hasIntEdgeLens ? (double) iEdgeLen : dEdgeLen ;
00068 }
00069
00070 int GetIntEdgeLen() const
00071 {
00072 return hasIntEdgeLens ? iEdgeLen : (int) dEdgeLen ;
00073 }
00074
00075 std::vector<NxsComment> GetUnprocessedComments() const
00076 {
00077 return unprocessedComments;
00078 }
00079
00084 bool GetInfo(const std::string &key, std::string *value) const
00085 {
00086 std::map<std::string, std::string>::const_iterator kvit = parsedInfo.find(key);
00087 if (kvit == parsedInfo.end())
00088 return false;
00089 if (value != NULL)
00090 *value = kvit->second;
00091 return true;
00092 }
00099 const std::map<std::string, std::string> & GetInfo() const
00100 {
00101 return parsedInfo;
00102 }
00103 const NxsSimpleNode * GetParent() const
00104 {
00105 return parent;
00106 }
00107 const NxsSimpleNode * GetChild() const
00108 {
00109 return child;
00110 }
00111
00112 void SetDblEdgeLen(double e, const char *asString)
00113 {
00114 defaultEdgeLen = false;
00115 hasIntEdgeLens = false;
00116 dEdgeLen = e;
00117 if (asString)
00118 lenAsString.assign(asString);
00119
00120 }
00121
00122 void SetIntEdgeLen(int e, const char *asString)
00123 {
00124 defaultEdgeLen = false;
00125 hasIntEdgeLens = true;
00126 iEdgeLen = e;
00127 if (asString)
00128 lenAsString.assign(asString);
00129 }
00130 mutable void * scratch;
00131 private:
00132 void WriteAsNewick(std::ostream &out, bool nhx) const;
00133 void DealWithNexusComments(const std::vector<NxsComment> & ecs, bool NHXComments);
00134
00135 NxsSimpleEdge(NxsSimpleNode *par, NxsSimpleNode * des, double edgeLen)
00136 :scratch(0L),
00137 parent(par),
00138 child(des),
00139 defaultEdgeLen(true),
00140 hasIntEdgeLens(false),
00141 dEdgeLen(edgeLen)
00142 {
00143 }
00144
00145 NxsSimpleEdge(int edgeLen, NxsSimpleNode *par, NxsSimpleNode * des)
00146 :scratch(0L),
00147 parent(par),
00148 child(des),
00149 defaultEdgeLen(true),
00150 hasIntEdgeLens(true),
00151 iEdgeLen(edgeLen)
00152 {
00153 }
00154
00155 NxsSimpleNode * GetMutableParent() const
00156 {
00157 return parent;
00158 }
00159
00160 NxsSimpleNode * parent;
00161 NxsSimpleNode * child;
00162 bool defaultEdgeLen;
00163 bool hasIntEdgeLens;
00164 int iEdgeLen;
00165 double dEdgeLen;
00166 std::string lenAsString;
00167 std::vector<NxsComment> unprocessedComments;
00168 std::map<std::string, std::string> parsedInfo;
00169 friend class NxsSimpleTree;
00170 friend class NxsSimpleNode;
00171 };
00172
00175 class NxsSimpleNode
00176 {
00177 public:
00178 NxsSimpleEdge GetEdgeToParent() const
00179 {
00180 return edgeToPar;
00181 }
00182
00183 NxsSimpleNode *GetFirstChild() const
00184 {
00185 return lChild;
00186 }
00187 NxsSimpleNode * GetNextSib() const
00188 {
00189 return rSib;
00190 }
00191 NxsSimpleNode * GetLastChild() const
00192 {
00193 NxsSimpleNode * currNode = GetFirstChild();
00194 if (!currNode)
00195 return NULL;
00196 NxsSimpleNode * nextNd = currNode->GetNextSib();
00197 while (nextNd)
00198 {
00199 currNode = nextNd;
00200 nextNd = currNode->GetNextSib();
00201 }
00202 return currNode;
00203 }
00204
00205 std::vector<NxsSimpleNode *> GetChildren() const
00206 {
00207 std::vector<NxsSimpleNode *> children;
00208 NxsSimpleNode * currNode = GetFirstChild();
00209 while(currNode)
00210 {
00211 children.push_back(currNode);
00212 currNode = currNode->GetNextSib();
00213 }
00214 return children;
00215 }
00216
00217 unsigned GetTaxonIndex() const
00218 {
00219 return taxIndex;
00220 }
00221
00222
00223 std::string GetName() const
00224 {
00225 return name;
00226 }
00227 void SetName(const std::string &n)
00228 {
00229 name = n;
00230 }
00231 mutable void * scratch;
00232 private:
00233 void WriteAsNewick(std::ostream &out, bool nhx, bool useLeafNames, bool escapeNames, const NxsTaxaBlockAPI *taxa=0L) const;
00234
00235
00236 NxsSimpleNode(NxsSimpleNode *par, double edgeLen)
00237 :scratch(0L),
00238 lChild(0L),
00239 rSib(0L),
00240 edgeToPar(par, 0L, edgeLen),
00241 taxIndex(UINT_MAX)
00242 {
00243 edgeToPar.child = this;
00244 }
00245
00246 NxsSimpleNode(int edgeLen, NxsSimpleNode *par)
00247 :scratch(0L),
00248 lChild(0L),
00249 rSib(0L),
00250 edgeToPar(edgeLen, par, 0L),
00251 taxIndex(UINT_MAX)
00252 {
00253 edgeToPar.child = this;
00254 }
00255
00256 NxsSimpleNode * GetParent() const
00257 {
00258 return edgeToPar.GetMutableParent();
00259 }
00260
00261 void AddSib(NxsSimpleNode *n)
00262 {
00263 if (rSib)
00264 rSib->AddSib(n);
00265 else
00266 rSib = n;
00267 }
00268 void AddChild(NxsSimpleNode *n)
00269 {
00270 if (lChild)
00271 lChild->AddSib(n);
00272 else
00273 lChild = n;
00274 }
00275 void AddSelfAndDesToPreorder(std::vector<const NxsSimpleNode *> &p) const;
00276 NxsSimpleNode * FindTaxonIndex(unsigned leafIndex);
00277 NxsSimpleNode * lChild;
00278 NxsSimpleNode * rSib;
00279 NxsSimpleEdge edgeToPar;
00280 std::string name;
00281 unsigned taxIndex;
00282 friend class NxsSimpleTree;
00283 };
00288 class NxsSimpleTree
00289 {
00290 public:
00291 NxsSimpleTree(const NxsFullTreeDescription &ftd, const int defaultIntEdgeLen, const double defaultDblEdgeLen)
00292 :defIntEdgeLen(defaultIntEdgeLen),
00293 defDblEdgeLen(defaultDblEdgeLen),
00294 realEdgeLens(false)
00295 {
00296 Initialize(ftd);
00297 }
00298 NxsSimpleTree(const int defaultIntEdgeLen, const double defaultDblEdgeLen)
00299 :defIntEdgeLen(defaultIntEdgeLen),
00300 defDblEdgeLen(defaultDblEdgeLen),
00301 realEdgeLens(false)
00302 {}
00303 ~NxsSimpleTree()
00304 {
00305 Clear();
00306 }
00307 void Initialize(const NxsFullTreeDescription &);
00308
00309
00310 std::vector<const NxsSimpleNode *> GetPreorderTraversal() const;
00311 std::vector<NxsSimpleNode *> & GetLeavesRef()
00312 {
00313 return leaves;
00314 }
00315 std::vector<std::vector<int> > GetIntPathDistances(bool toMRCA=false) const;
00316 std::vector<std::vector<double> > GetDblPathDistances(bool toMRCA=false) const;
00317
00321 void WriteAsNewick(std::ostream &out, bool nhx, bool useLeafNames, bool escapeNames, const NxsTaxaBlockAPI * taxa) const
00322 {
00323 if (root)
00324 root->WriteAsNewick(out, nhx, useLeafNames, escapeNames, taxa);
00325 }
00326 void RerootAt(unsigned leafIndex);
00327
00328 const NxsSimpleNode * GetRootConst() const
00329 {
00330 return root;
00331 }
00332 protected:
00333 std::vector<NxsSimpleNode *> allNodes;
00334 std::vector<NxsSimpleNode *> leaves;
00335 NxsSimpleNode * root;
00336 int defIntEdgeLen;
00337 double defDblEdgeLen;
00338 bool realEdgeLens;
00339 private:
00340 NxsSimpleNode * AllocNewNode(NxsSimpleNode *p)
00341 {
00342 NxsSimpleNode * nd;
00343 if (realEdgeLens)
00344 nd = new NxsSimpleNode(p, defDblEdgeLen);
00345 else
00346 nd = new NxsSimpleNode(defIntEdgeLen, p);
00347 allNodes.push_back(nd);
00348 return nd;
00349 }
00350
00351 void Clear()
00352 {
00353 root = NULL;
00354 for (std::vector<NxsSimpleNode *>::iterator nIt = allNodes.begin(); nIt != allNodes.end(); ++nIt)
00355 delete *nIt;
00356 allNodes.clear();
00357 leaves.clear();
00358 }
00359 void FlipRootsChildToRoot(NxsSimpleNode *subRoot);
00360 NxsSimpleTree(const NxsSimpleTree &);
00361 NxsSimpleTree & operator=(const NxsSimpleTree &);
00362 };
00363
00383 class NxsFullTreeDescription
00384 {
00385 public:
00386 enum TreeDescFlags
00387 { NXS_IS_ROOTED_BIT = 0x0001,
00388 NXS_HAS_SOME_EDGE_LENGTHS_BIT = 0x0002,
00389 NXS_MISSING_SOME_EDGE_LENGTHS_BIT = 0x0004,
00390 NXS_EDGE_LENGTH_UNION = 0x0006,
00391 NXS_INT_EDGE_LENGTHS_BIT = 0x0008,
00392 NXS_HAS_ALL_TAXA_BIT = 0x0010,
00393 NXS_HAS_NHX_BIT = 0x0020,
00394 NXS_HAS_DEG_TWO_NODES_BIT = 0x0040,
00395 NXS_HAS_POLYTOMY_BIT = 0x0080,
00396 NXS_HAS_INTERNAL_NAMES_BIT = 0x0100,
00397 NXS_HAS_NEW_INTERNAL_NAMES_BIT = 0x0200,
00398 NXS_KNOWN_INTERNAL_NAMES_BIT = 0x0400,
00399 NXS_SOME_ZERO_EDGE_LEN_BIT = 0x0800,
00400 NXS_SOME_NEGATIVE_EDGE_LEN_BIT = 0x1000,
00401 NXS_TREE_PROCESSED = 0x2000
00402 };
00406 NxsFullTreeDescription(const std::string & newickStr,
00407 const std::string &treeName,
00408 int infoFlags)
00409 :newick(newickStr),
00410 name(treeName),
00411 flags(infoFlags),
00412 minIntEdgeLen(INT_MAX),
00413 minDblEdgeLen(DBL_MAX)
00414 {}
00418 std::vector<std::string> GetTreeTokens() const;
00419
00429 const std::string & GetNewick() const
00430 {
00431 return newick;
00432 }
00434 const std::string & GetName() const
00435 {
00436 return name;
00437 }
00439 bool IsProcessed() const
00440 {
00441 return (flags&NXS_TREE_PROCESSED) != 0;
00442 }
00444 void AssertProcessed() const
00445 {
00446 if (!IsProcessed())
00447 throw NxsNCLAPIException("Tree description queries are only supported on processed tree descriptions.");
00448 }
00450 bool IsRooted() const
00451 {
00452 AssertProcessed();
00453 return (flags&NXS_IS_ROOTED_BIT) != 0;
00454 }
00458 bool AllEdgesHaveLengths() const
00459 {
00460 AssertProcessed();
00461 return (flags&NXS_EDGE_LENGTH_UNION) == NXS_HAS_SOME_EDGE_LENGTHS_BIT;
00462 }
00466 bool SomeEdgesHaveLengths() const
00467 {
00468 AssertProcessed();
00469 return (flags&NXS_HAS_SOME_EDGE_LENGTHS_BIT) != 0;
00470 }
00474 bool EdgeLengthsAreAllIntegers() const
00475 {
00476 AssertProcessed();
00477 return (flags&NXS_INT_EDGE_LENGTHS_BIT) != 0;
00478 }
00482 bool AllTaxaAreIncluded() const
00483 {
00484 AssertProcessed();
00485 return (flags&NXS_HAS_ALL_TAXA_BIT) != 0;
00486 }
00490 bool HasNHXComments() const
00491 {
00492 AssertProcessed();
00493 return (flags&NXS_HAS_NHX_BIT) != 0;
00494 }
00498 bool HasPolytomies() const
00499 {
00500 AssertProcessed();
00501 return (flags&NXS_HAS_POLYTOMY_BIT) != 0;
00502 }
00506 bool HasDegreeTwoNodes() const
00507 {
00508 AssertProcessed();
00509 return (flags&NXS_HAS_DEG_TWO_NODES_BIT) != 0;
00510 }
00515 int smallestIntEdgeLength() const
00516 {
00517 return minIntEdgeLen;
00518 }
00523 double smallestRealEdgeLength() const
00524 {
00525 return minDblEdgeLen;
00526 }
00527 private:
00528 std::string newick;
00529 std::string name;
00530 int flags;
00531 int minIntEdgeLen;
00532 double minDblEdgeLen;
00533
00534 friend class NxsTreesBlock;
00535 };
00536 class NxsTreesBlock;
00537 typedef bool (* ProcessedTreeValidationFunction)(NxsFullTreeDescription &, void *, NxsTreesBlock *);
00553 class NxsTreesBlock
00554 : public NxsTreesBlockAPI, public NxsTaxaBlockSurrogate
00555 {
00556 public:
00557 NxsTreesBlock(NxsTaxaBlockAPI *tb);
00558 virtual ~NxsTreesBlock();
00559
00560 void ReplaceTaxaBlockPtr(NxsTaxaBlockAPI *tb);
00561 unsigned GetIndexSet(const std::string &label, NxsUnsignedSet * toFill) const
00562 {
00563 return NxsLabelToIndicesMapper::GetIndicesFromSets(label, toFill, treeSets);
00564 }
00565
00569 unsigned GetNumDefaultTree();
00571 unsigned GetNumTrees();
00573 unsigned GetNumTrees() const;
00586 const NxsFullTreeDescription & GetFullTreeDescription(unsigned i) const;
00588 unsigned TreeLabelToNumber(const std::string & name) const;
00592 NxsString GetTreeName(unsigned i);
00596 NxsString GetTreeDescription(unsigned i);
00601 NxsString GetTranslatedTreeDescription(unsigned i);
00605 bool IsDefaultTree(unsigned i);
00611 bool IsRootedTree(unsigned i);
00612 virtual void Report(std::ostream &out) NCL_COULD_BE_CONST ;
00613 virtual void BriefReport(NxsString &s) NCL_COULD_BE_CONST ;
00614 virtual void Reset();
00615 void SetNexus(NxsReader *nxsptr)
00616 {
00617 NxsBlock::SetNexus(nxsptr);
00618 NxsTaxaBlockSurrogate::SetNexusReader(nxsptr);
00619 }
00621 virtual const std::string & GetBlockName() const
00622 {
00623 return id;
00624 }
00625
00626 void WriteAsNexus(std::ostream &out) const;
00627
00628 virtual VecBlockPtr GetImpliedBlocks()
00629 {
00630 return GetCreatedTaxaBlocks();
00631 }
00632
00633
00634 virtual void HandleLinkCommand(NxsToken & token)
00635 {
00636 HandleLinkTaxaCommand(token);
00637 }
00638 virtual void WriteLinkCommand(std::ostream &out) const
00639 {
00640 WriteLinkTaxaCommand(out);
00641 }
00642
00643 unsigned GetMaxIndex() const;
00644 unsigned GetIndicesForLabel(const std::string &label, NxsUnsignedSet *inds) const;
00645 bool AddNewIndexSet(const std::string &label, const NxsUnsignedSet & inds);
00646 bool AddNewPartition(const std::string &label, const NxsPartition & inds);
00647
00648 bool GetAllowImplicitNames() const
00649 {
00650 return allowImplicitNames;
00651 }
00659 bool GetProcessAllTreesDuringParse() const
00660 {
00661 return processAllTreesDuringParse;
00662 }
00663 void SetAllowImplicitNames(bool s)
00664 {
00665 allowImplicitNames = s;
00666 }
00674 void SetProcessAllTreesDuringParse(bool s)
00675 {
00676 processAllTreesDuringParse = s;
00677 }
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691 void ProcessTree(NxsFullTreeDescription &treeDesc) const;
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703 void ProcessAllTrees() const
00704 {
00705 std::vector<NxsFullTreeDescription>::iterator trIt = trees.begin();
00706 for (; trIt != trees.end(); ++trIt)
00707 ProcessTree(*trIt);
00708 }
00709
00710
00711
00712
00713
00714 NxsTreesBlock & operator=(const NxsTreesBlock &other)
00715 {
00716 Reset();
00717 CopyBaseBlockContents(static_cast<const NxsBlock &>(other));
00718 CopyTaxaBlockSurrogateContents(other);
00719 CopyTreesBlockContents(other);
00720 return *this;
00721 }
00722
00723
00724
00725
00726 virtual void CopyTreesBlockContents(const NxsTreesBlock &other)
00727 {
00728 allowImplicitNames = other.allowImplicitNames;
00729 processAllTreesDuringParse = other.processAllTreesDuringParse;
00730 writeFromNodeEdgeDataStructure = other.writeFromNodeEdgeDataStructure;
00731 constructingTaxaBlock = other.constructingTaxaBlock;
00732 newtaxa = other.newtaxa;
00733 trees = other.trees;
00734 capNameToInd = other.capNameToInd;
00735 defaultTreeInd = other.defaultTreeInd;
00736 writeTranslateTable = other.writeTranslateTable;
00737 treeSets = other.treeSets;
00738 treePartitions = other.treePartitions;
00739 processedTreeValidationFunction = other.processedTreeValidationFunction;
00740 ptvArg = other.ptvArg;
00741 }
00742
00743 virtual NxsTreesBlock * Clone() const
00744 {
00745 NxsTreesBlock * a = new NxsTreesBlock(taxa);
00746 *a = *this;
00747 return a;
00748 }
00749 static void ProcessTokenVecIntoTree(const ProcessedNxsCommand & token, NxsFullTreeDescription & ftd, NxsLabelToIndicesMapper *, std::map<std::string, unsigned> &capNameToInd, bool allowNewTaxa, NxsReader * nexusReader, const bool respectCase=false);
00750 static void ProcessTokenStreamIntoTree(NxsToken & token, NxsFullTreeDescription & ftd, NxsLabelToIndicesMapper *, std::map<std::string, unsigned> &capNameToInd, bool allowNewTaxa, NxsReader * nexusReader, const bool respectCase=false);
00751
00752 void SetWriteFromNodeEdgeDataStructure(bool v)
00753 {
00754 writeFromNodeEdgeDataStructure = v;
00755 }
00756
00757
00758
00759 std::vector<NxsFullTreeDescription> & GetProcessedTrees()
00760 {
00761 ProcessAllTrees();
00762 return trees;
00763 }
00764
00791 void setValidationCallbacks(
00792 ProcessedTreeValidationFunction func,
00793 void * blob)
00794 {
00795 this->processedTreeValidationFunction = func;
00796 this->ptvArg = blob;
00797 }
00798 bool SwapEquivalentTaxaBlock(NxsTaxaBlockAPI * tb)
00799 {
00800 return SurrogateSwapEquivalentTaxaBlock(tb);
00801 }
00802 void ReadPhylipTreeFile(NxsToken & token);
00803 void setWriteTranslateTable(bool wtt)
00804 {
00805 this->writeTranslateTable = wtt;
00806 }
00807 protected :
00808 void ReadTreeFromOpenParensToken(NxsFullTreeDescription &td, NxsToken & token);
00809
00810 void WriteTranslateCommand(std::ostream & out) const;
00811 void WriteTreesCommand(std::ostream & out) const;
00812 void ConstructDefaultTranslateTable(NxsToken &token, const char * cmd);
00813
00814 bool allowImplicitNames;
00815 bool processAllTreesDuringParse;
00816 bool constructingTaxaBlock;
00817 bool writeFromNodeEdgeDataStructure;
00819 mutable std::vector<NxsFullTreeDescription> trees;
00820 mutable std::map<std::string, unsigned> capNameToInd;
00821 unsigned defaultTreeInd;
00822 NxsUnsignedSetMap treeSets;
00823 NxsPartitionsByName treePartitions;
00824
00825 bool writeTranslateTable ;
00826
00827 ProcessedTreeValidationFunction processedTreeValidationFunction;
00828 void * ptvArg;
00829
00830 virtual void Read(NxsToken &token);
00831 void HandleTranslateCommand(NxsToken &token);
00832 void HandleTreeCommand(NxsToken &token, bool rooted);
00833
00834 friend class PublicNexusReader;
00835 };
00836
00837 typedef NxsTreesBlock TreesBlock;
00838 class NxsTreesBlockFactory
00839 :public NxsBlockFactory
00840 {
00841 public:
00842 virtual NxsTreesBlock * GetBlockReaderForID(const std::string & id, NxsReader *reader, NxsToken *token);
00843 };
00844
00845 #endif