Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Jun 11, 2024
2 parents d767faf + b141fdb commit 7151b4a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 25 deletions.
49 changes: 49 additions & 0 deletions lphy-base/src/main/java/lphy/base/evolution/tree/LabelClade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lphy.base.evolution.tree;

import lphy.core.model.DeterministicFunction;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;

public class LabelClade extends DeterministicFunction<TimeTree> {
public static final String treeParamName = "tree";
public static final String taxaParamName = "taxa";
public static final String labelParamName = "label";
public LabelClade(@ParameterInfo(name = treeParamName, description = "the tree to label")Value<TimeTree> tree,
@ParameterInfo(name = taxaParamName, description = "the root of the taxa names would be labelled") Value<String[]> taxa,
@ParameterInfo(name = labelParamName, description = "the label") Value<String> label){
if (tree == null) throw new IllegalArgumentException("The tree cannot be null!");
if (taxa == null) throw new IllegalArgumentException("The taxa name cannot be null!");
if (label == null) throw new IllegalArgumentException("Please label the mrca of the taxa!");
setParam(treeParamName, tree);
setParam(taxaParamName, taxa);
setParam(labelParamName, label);
}
@GeneratorInfo(name = "labelClade", description = "Find the most recent common ancestor of given taxa names in the tree and give it a label.")
@Override
public Value<TimeTree> apply() {
// make a deep copy of the tree
TimeTree tree = getTree().value();
TimeTree newTree = new TimeTree(tree);

// find mrca node
Value<TimeTree> treeValue = new Value<>("newTree", newTree);
MRCA mrcaInstance = new MRCA(treeValue, getTaxa());
TimeTreeNode mrca = mrcaInstance.apply().value();

// set label metadata
String label = getLabel().value();
mrca.setMetaData("label", label);

return new Value<>(null, newTree,this);
}
public Value<TimeTree> getTree(){
return getParams().get(treeParamName);
}
public Value<String[]> getTaxa(){
return getParams().get(taxaParamName);
}
public Value<String> getLabel(){
return getParams().get(labelParamName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
import lphy.core.model.annotation.ParameterInfo;

public class SubstituteClade extends DeterministicFunction<TimeTree> {
// Value<TimeTree> baseTree;
// Value<TimeTree> cladeTree;
// Value<Double> time;
// Value<TimeTreeNode> node;
// Value<String> nodeLabel;
public static final String baseTreeName = "baseTree";
public static final String cladeTreeName = "cladeTree";
public static final String nodeName = "node";
Expand All @@ -34,11 +29,6 @@ public SubstituteClade(@ParameterInfo(name = baseTreeName, description = "the tr
if (time != null)
setParam(mutationHappenTimeName, time);
setParam(nodeLabelName,nodeLabel);
// this.baseTree = baseTree;
// this.cladeTree = cladeTree;
// this.node = node;
// this.nodeLabel = nodeLabel;
// this.time = time;
}

@GeneratorInfo(name = "substituteClade", examples = {"substituteClade.lphy"},
Expand Down Expand Up @@ -93,16 +83,6 @@ public Value<TimeTree> apply() {
return new Value<>(null, newTree, this);
}

// @Override
// public Map<String, Value> getParams() {
// SortedMap<String, Value> map = new TreeMap<>();
// if (baseTree != null) map.put(baseTreeName, baseTree);
// if (cladeTree != null) map.put(cladeTreeName, cladeTree);
// if (node != null) map.put(nodeName, node);
// if (time != null) map.put(mutationHappenTimeName, time);
// if (nodeLabelName != null) map.put(nodeLabelName, nodeLabel);
// return map;
// }
public Value<TimeTree> getBaseTree() {
return getParams().get(baseTreeName);
}
Expand Down
7 changes: 2 additions & 5 deletions lphy-base/src/main/java/lphy/base/spi/LPhyBaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import lphy.base.evolution.likelihood.PhyloCTMC;
import lphy.base.evolution.likelihood.PhyloCTMCSiteModel;
import lphy.base.evolution.substitutionmodel.*;
import lphy.base.evolution.tree.MRCA;
import lphy.base.evolution.tree.SampleBranch;
import lphy.base.evolution.tree.SubsampledTree;
import lphy.base.evolution.tree.SubstituteClade;
import lphy.base.evolution.tree.*;
import lphy.base.function.*;
import lphy.base.function.alignment.*;
import lphy.base.function.datatype.AminoAcidsFunction;
Expand Down Expand Up @@ -97,7 +94,7 @@ public List<Class<? extends BasicFunction>> declareFunctions() {
VariableSites.class, InvariableSites.class, CopySites.class,
// Tree
LocalBranchRates.class, ExtantTree.class, PruneTree.class, LocalClock.class,
SubstituteClade.class, MRCA.class,//NodeCount.class, TreeLength.class,
SubstituteClade.class, MRCA.class, LabelClade.class,//NodeCount.class, TreeLength.class,
// Matrix
BinaryRateMatrix.class, MigrationMatrix.class, MigrationCount.class,
// IO
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package lphy.base.evolution.tree;

import lphy.base.function.tree.Newick;
import lphy.core.model.Value;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class LabelCladeTest {
String newickTree;

@BeforeEach
void setUp() {
newickTree = "(((((1:2.0, (2:1.0, 3:1.0):1.0):2.0, (5:2.0, 6:2.0):2.0):2.0):0.0,4:6.0):6.0, 7:12.0)";
}

@Test
void applyTest1() {
TimeTree tree = Newick.parseNewick(newickTree);
String[] taxa = {"4", "7"};
String label = "label";

Value<TimeTree> treeValue = new Value<>("tree", tree);
Value<String[]> taxaValue = new Value<>("taxa", taxa);
Value<String> labelValue = new Value<>("label", label);

LabelClade labelCladeInstance = new LabelClade(treeValue, taxaValue, labelValue);
MRCA mrcaInstance = new MRCA(treeValue,taxaValue);
TimeTree observe = labelCladeInstance.apply().value();
TimeTreeNode mrcaNode = mrcaInstance.apply().value();

// only index should be same for mrca
assertEquals(mrcaNode.getIndex(), observe.getRoot().getIndex());
// check label
assertEquals(label, observe.getRoot().getMetaData("label"));
assertEquals(observe.getRoot(), observe.getLabeledNode(label));
}

@Test
void applyTest2() {
TimeTree tree = Newick.parseNewick(newickTree);
String[] taxa = {"3", "5"};
String label = "label";

Value<TimeTree> treeValue = new Value<>("tree", tree);
Value<String[]> taxaValue = new Value<>("taxa", taxa);
Value<String> labelValue = new Value<>("label", label);

LabelClade labelCladeInstance = new LabelClade(treeValue, taxaValue, labelValue);
MRCA mrcaInstance = new MRCA(treeValue,taxaValue);
TimeTree observe = labelCladeInstance.apply().value();
TimeTreeNode mrcaNode = mrcaInstance.apply().value();

boolean found = false;
for (TimeTreeNode node: observe.getInternalNodes()){
if (node.getAllLeafNodes().size() == 5 && node.age == 4){
assertEquals(node.getIndex(), mrcaNode.getIndex());
assertEquals(label, node.getMetaData("label"));
assertEquals(node, observe.getLabeledNode(label));
found = true;
}
}
assert(found);
}
}

0 comments on commit 7151b4a

Please sign in to comment.