-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/LinguaPhylo/linguaPhylo
- Loading branch information
Showing
4 changed files
with
117 additions
and
25 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
lphy-base/src/main/java/lphy/base/evolution/tree/LabelClade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
lphy-base/src/test/java/lphy/base/evolution/tree/LabelCladeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |