Skip to content

Commit

Permalink
Added number of branches until evaluationTime
Browse files Browse the repository at this point in the history
  • Loading branch information
gkarthik committed Aug 17, 2023
1 parent 88241e6 commit 80e7469
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/dr/app/tools/PersistenceSummarizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private void processOneTree(Tree tree, double evaluationTime, double ancestralTi
double currentStateTime = 0;
boolean jumpOccured = false;
Object[] jumps = readCJH(node, tree, nodeStateAnnotation);
NodeRef basalNode = null; // This is the branch where the jump occurs i.e., node at the base of the unique introduction

//when no jumps are found, we need to go back it's ancestry to find a branch with a jump
if (jumps == null) {
Expand All @@ -122,12 +123,14 @@ private void processOneTree(Tree tree, double evaluationTime, double ancestralTi
// System.out.println("3\t"+currentStateTime);
jumpOccured = true;
totalEventDescendents = TreeUtils.getExternalNodes(tree, node);
basalNode = node;
}
node = tree.getParent(node);
} else {
currentStateTime += parentNodeHeight - nodeHeight;
// System.out.println("4\t"+currentStateTime);
totalEventDescendents = TreeUtils.getExternalNodes(tree, node);
basalNode = node;
jumpOccured = true;
}
}
Expand Down Expand Up @@ -156,6 +159,7 @@ private void processOneTree(Tree tree, double evaluationTime, double ancestralTi
// System.out.println("5\t"+currentStateTime);
ancestralState = (String) recentJump[1];
totalEventDescendents = TreeUtils.getExternalNodes(tree, node);
basalNode = node;
} else {
currentStateTime += parentNodeHeight - evaluationTime;
// System.out.println("6\t"+currentStateTime);
Expand All @@ -181,25 +185,29 @@ private void processOneTree(Tree tree, double evaluationTime, double ancestralTi
// System.out.println("8\t"+currentStateTime);
jumpOccured = true;
totalEventDescendents = TreeUtils.getExternalNodes(tree, node);
basalNode = node;
}
node = tree.getParent(node);
} else {
currentStateTime += parentNodeHeight - nodeHeight;
// System.out.println("9\t"+currentStateTime);
totalEventDescendents = TreeUtils.getExternalNodes(tree, node);
jumpOccured = true;
basalNode = node;
}
}
}
}

double independenceTime = getIndependenceTime(tree,nodes,originalNode,evaluationTime,currentState,nodeStateAnnotation);
nodes.add(originalNode);
int numberOfBranchesFromUniqueBeforeEvalTime = getNumberOfBranchesUntilEvalTime(tree, basalNode, currentState, nodeStateAnnotation, evaluationTime);

Row row = new Row(treeId, evaluationTime, ancestralTime, originalNode.getNumber(), node.getNumber(), currentState, ancestralState, currentStateTime, independenceTime,
nodeDescendants.size(),getSameStateDescendants(nodeDescendants,tree,currentState,nodeStateAnnotation, 0),
totalEventDescendents.size(),getSameStateDescendants(totalEventDescendents,tree,currentState,nodeStateAnnotation, 0),
nodesAfterEvalTime(totalEventDescendents, tree, evaluationTime),getSameStateDescendants(totalEventDescendents,tree,currentState,nodeStateAnnotation, evaluationTime));
nodesAfterEvalTime(totalEventDescendents, tree, evaluationTime),getSameStateDescendants(totalEventDescendents,tree,currentState,nodeStateAnnotation, evaluationTime),
numberOfBranchesFromUniqueBeforeEvalTime);
ps.println(row);
}
}
Expand Down Expand Up @@ -259,6 +267,24 @@ private double getIndependenceTime(Tree tree, Set nodes, NodeRef node, double ev
return independenceTime;
}

private int getNumberOfBranchesUntilEvalTime(Tree tree, NodeRef node, String state, String nodeStateAnnotation, double evalTime){
int childCount = tree.getChildCount(node);
int numberOfBranches = 1; // 1 branch that belongs to the node itself
for (int i = 0; i < childCount; i++) {
NodeRef childNode = tree.getChild(node, i);
if (tree.getNodeHeight(childNode) < evalTime && tree.getNodeHeight(node) < evalTime){ // branch starts after evalTime then continue
continue;
}
String nodeState = (String) tree.getNodeAttribute(childNode, nodeStateAnnotation);
if(!nodeState.equalsIgnoreCase(state)) {
numberOfBranches += 1; // Jump occurs along this branch but we consider the branch until the jump to count as 1
continue;
}
numberOfBranches += getNumberOfBranchesUntilEvalTime(tree, childNode, state, nodeStateAnnotation, evalTime);
}
return numberOfBranches;
}


private int getSameStateDescendants(Set leafs, Tree tree, String state, String nodeStateAnnotation, double evaluationTime){
int descendents = 0;
Expand Down Expand Up @@ -322,6 +348,7 @@ private class Row {
int totalnumberOfDescendantsFromUniqueEventAndSameState;
int numberOfDescendantsFromUniqueEventAfterEvalTime;
int numberOfDescendantsFromUniqueEventAndSameStateAfterEvalTime;
int numberOfBranchesFromUniqueBeforeEvalTime;

private static final String DELIMITER = ",";

Expand All @@ -330,7 +357,8 @@ private Row(String treeId, double evaluationTime, double ancestralTime,
String startLocation, String endLocation,
double time, double independenceTime, int numberOfDescendants, int numberOfDescendantsOfSameState,
int totalNumberOfDescendantsFromUniqueEvent, int totalnumberOfDescendantsFromUniqueEventAndSameState,
int numberOfDescendantsFromUniqueEventAfterEvalTime, int numberOfDescendantsFromUniqueEventAndSameStateAfterEvalTime
int numberOfDescendantsFromUniqueEventAfterEvalTime, int numberOfDescendantsFromUniqueEventAndSameStateAfterEvalTime,
int numberOfBranchesFromUniqueBeforeEvalTime
) {
this.treeId = treeId;
this.evaluationTime = evaluationTime;
Expand All @@ -347,6 +375,7 @@ private Row(String treeId, double evaluationTime, double ancestralTime,
this.totalnumberOfDescendantsFromUniqueEventAndSameState = totalnumberOfDescendantsFromUniqueEventAndSameState;
this.numberOfDescendantsFromUniqueEventAfterEvalTime = numberOfDescendantsFromUniqueEventAfterEvalTime;
this.numberOfDescendantsFromUniqueEventAndSameStateAfterEvalTime = numberOfDescendantsFromUniqueEventAndSameStateAfterEvalTime;
this.numberOfBranchesFromUniqueBeforeEvalTime = numberOfBranchesFromUniqueBeforeEvalTime;
}

public String toString() {
Expand All @@ -356,6 +385,7 @@ public String toString() {
+ time + DELIMITER + independenceTime + DELIMITER + numberOfDescendants + DELIMITER + numberOfDescendantsOfSameState
+ DELIMITER + totalNumberOfDescendantsFromUniqueEvent + DELIMITER + totalnumberOfDescendantsFromUniqueEventAndSameState
+ DELIMITER + numberOfDescendantsFromUniqueEventAfterEvalTime + DELIMITER + numberOfDescendantsFromUniqueEventAndSameStateAfterEvalTime
+ DELIMITER + numberOfBranchesFromUniqueBeforeEvalTime
;
}
}
Expand Down Expand Up @@ -393,7 +423,7 @@ private static Object[] readCJH(NodeRef node, Tree treeTime, String nodeStateAnn

protected PrintStream openOutputFile(String outputFileName) {
PrintStream ps = super.openOutputFile(outputFileName);
ps.println("treeId,evaluationTime,ancestralTime,evaluationNodeID,ancestralNodeID,stateAtEvaluationTime,ancestralState,persistenceTime,independenceTime,descendants,descendantsOfSameState,totalDescendantsFromUnique,totalDescendantsFromUniqueOfSameState,descendantsFromUniqueAfterEvalTime,descendantsFromUniqueOfSameStateAfterEvalTime");
ps.println("treeId,evaluationTime,ancestralTime,evaluationNodeID,ancestralNodeID,stateAtEvaluationTime,ancestralState,persistenceTime,independenceTime,descendants,descendantsOfSameState,totalDescendantsFromUnique,totalDescendantsFromUniqueOfSameState,descendantsFromUniqueAfterEvalTime,descendantsFromUniqueOfSameStateAfterEvalTime,numberOfBranchesFromUniqueBeforeEvalTime");
return ps;
}

Expand Down

0 comments on commit 80e7469

Please sign in to comment.