当前位置: 首页>>代码示例>>C++>>正文


C++ SequenceTree::recalcNodeIdsPostfixOrderAndAddInOrder方法代码示例

本文整理汇总了C++中SequenceTree::recalcNodeIdsPostfixOrderAndAddInOrder方法的典型用法代码示例。如果您正苦于以下问题:C++ SequenceTree::recalcNodeIdsPostfixOrderAndAddInOrder方法的具体用法?C++ SequenceTree::recalcNodeIdsPostfixOrderAndAddInOrder怎么用?C++ SequenceTree::recalcNodeIdsPostfixOrderAndAddInOrder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SequenceTree的用法示例。


在下文中一共展示了SequenceTree::recalcNodeIdsPostfixOrderAndAddInOrder方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: dm

double
computeLeastSquaresEdgeLengths(const StrDblMatrix &orig_dm,  SequenceTree &tree){
  

  StrDblMatrix dm(orig_dm);
  const int numOriginalLeafs = dm.getSize();
  SequenceTree::NodeVector nodes;
  tree.recalcNodeIdsPostfixOrderAndAddInOrder(nodes);
  size_t nodeIdToRowIndex[nodes.size()];
  size_t rowIndexToNodeId[nodes.size()];
  str2int_hashmap name2Id((int)(nodes.size()*1.7));

  for(size_t i=0 ; i<nodes.size() ; i++)
    if(nodes[i]->isLeaf()){
      //PRINT(NAME(nodes[i]));PRINT(ID(nodes[i]));
      name2Id[NAME(nodes[i])] = ID(nodes[i]);
    }

  for(size_t row=0 ; row<dm.getSize() ; row++){
    str2int_hashmap::iterator f = name2Id.find(dm.getIdentifier(row));
    if(f==name2Id.end())
      USER_ERROR("name doesn't exist in tree: " << dm.getIdentifier(row));
    
    nodeIdToRowIndex[(*f).second] = row;
    rowIndexToNodeId[row] = (*f).second;
  }
  
  //the number of leafs below each node
  int numNodesBelow[nodes.size()];
  for(size_t i=0;i<nodes.size();i++)
    numNodesBelow[i]=1;

  //--------------------------------
  //BOTTOM UP TRAVERSAL IN TREE
  for(size_t i=0;i<nodes.size()-1;i++){
    if(nodes[i]->isLeaf())
      continue;

    //get the children and do the UNJ calculation to get the edge lengths
    SequenceTree::Node *parent = nodes[i];
    SequenceTree::Node *child1 = parent->getRightMostChild();
    SequenceTree::Node *child2 = child1->getLeftSibling();
    if(child2->getLeftSibling()!=NULL ){
      USER_ERROR("Have to be unrooted binary tree. Parent has " << parent->getNumChildren() << " children");
    }
    numNodesBelow[ID(parent)] = numNodesBelow[ID(child1)] + numNodesBelow[ID(child2)];
    //SEPARATOR();PRINT(NAME(child1));PRINT(NAME(child2));

    
    double sum = 0;
    for(size_t row=0;row<dm.getSize();row++){
      if(row==nodeIdToRowIndex[ID(child1)] || 
	 row==nodeIdToRowIndex[ID(child2)] )
	continue;
      sum += numNodesBelow[rowIndexToNodeId[row]]*(dm.getDistance(nodeIdToRowIndex[ID(child1)],row)-
						   dm.getDistance(nodeIdToRowIndex[ID(child2)],row));				
    }

    if(!isfinite(sum)){
      USER_ERROR("Distance Matrix contains a non finite number: " << sum);
    }

    EDGE(child1) = 0.5*dm.getDistance(nodeIdToRowIndex[ID(child1)],
				      nodeIdToRowIndex[ID(child2)])
      + 1.0/(2*(numOriginalLeafs-numNodesBelow[ID(parent)]))*sum;
    EDGE(child2) = 0.5*dm.getDistance(nodeIdToRowIndex[ID(child1)],
				      nodeIdToRowIndex[ID(child2)])
      - 1.0/(2*(numOriginalLeafs-numNodesBelow[ID(parent)]))*sum;
    // PRINT(dm.getDistance(nodeIdToRowIndex[ID(child1)],nodeIdToRowIndex[ID(child2)]));
    //     PRINT((numOriginalLeafs-numNodesBelow[ID(parent)]));
    //     PRINT(sum);PRINT(EDGE(child1));PRINT( EDGE(child2));
    //PRINT(1/(2*(numOriginalLeafs-numNodesBelow[ID(parent)]))*sum);
    
    //swap child1 to last row 
    int idOnLastRow = rowIndexToNodeId[dm.getSize()-1];
    if(idOnLastRow!=ID(child1)){
      int rowChild1 = nodeIdToRowIndex[ID(child1)];
      //PRINT(nodeIdToRowIndex[ID(child1)]);PRINT(dm.getSize());
      dm.swapRowToLast(nodeIdToRowIndex[ID(child1)]);
      nodeIdToRowIndex[idOnLastRow] = rowChild1;
      rowIndexToNodeId[rowChild1] = idOnLastRow;
      rowIndexToNodeId[dm.getSize()-1] = ID(child1);
      nodeIdToRowIndex[ID(child1)] = dm.getSize()-1;
    }
    //update distances to parent
    double w1 = (1.0*numNodesBelow[ID(child1)])/numNodesBelow[ID(parent)];
    double w2 = (1.0*numNodesBelow[ID(child2)])/numNodesBelow[ID(parent)];
    double distChild1Child2 = w1*EDGE(child1)+w2*EDGE(child2);
  
    //put parent on the row of child 2
    nodeIdToRowIndex[ID(parent)] = nodeIdToRowIndex[ID(child2)];
    rowIndexToNodeId[nodeIdToRowIndex[ID(parent)]] = ID(parent);
    int parentRow = nodeIdToRowIndex[ID(parent)]; 
    int child1Row = nodeIdToRowIndex[ID(child1)];
    int child2Row = nodeIdToRowIndex[ID(child2)];

    for(size_t row=0 ; row<dm.getSize()-1 ; row++){
      dm.setDistance(parentRow,row,
		     w1*dm.getDistance(child1Row,row)+
		     w2*dm.getDistance(child2Row,row)-
//.........这里部分代码省略.........
开发者ID:arvestad,项目名称:FastPhylo,代码行数:101,代码来源:LeastSquaresFit.cpp


注:本文中的SequenceTree::recalcNodeIdsPostfixOrderAndAddInOrder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。