| 1 |
package test.dr.evomodel.treelikelihood; |
| 2 |
|
| 3 |
import dr.evolution.alignment.SitePatterns; |
| 4 |
import dr.evolution.datatype.Nucleotides; |
| 5 |
import dr.evolution.tree.SimpleNode; |
| 6 |
import dr.evolution.tree.SimpleTree; |
| 7 |
import dr.evolution.tree.Tree; |
| 8 |
import dr.evolution.util.Units; |
| 9 |
import dr.evomodel.tree.TreeModel; |
| 10 |
import dr.math.matrixAlgebra.Vector; |
| 11 |
|
| 12 |
import java.util.ArrayList; |
| 13 |
import java.util.List; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class NormalizedSequenceLikelihoodTest extends SequenceLikelihoodTest { |
| 19 |
|
| 20 |
public NormalizedSequenceLikelihoodTest(String name) { |
| 21 |
super(name); |
| 22 |
} |
| 23 |
|
| 24 |
public void setUp() throws Exception { |
| 25 |
|
| 26 |
format.setMaximumFractionDigits(5); |
| 27 |
|
| 28 |
int numTaxa = TWO_TAXON_SEQUENCE[0].length; |
| 29 |
|
| 30 |
createAlignmentWithAllUniquePatterns(TWO_TAXON_SEQUENCE, Nucleotides.INSTANCE); |
| 31 |
|
| 32 |
treeModel = createSillyTreeModel(numTaxa); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
protected TreeModel createSillyTreeModel(int numTaxa) { |
| 37 |
|
| 38 |
SimpleNode[] nodes = new SimpleNode[2 * numTaxa - 1]; |
| 39 |
for (int n = 0; n < 2 * numTaxa - 1; n++) { |
| 40 |
nodes[n] = new SimpleNode(); |
| 41 |
} |
| 42 |
|
| 43 |
nodes[0].setTaxon(taxa[0]); |
| 44 |
nodes[1].setTaxon(taxa[1]); |
| 45 |
|
| 46 |
nodes[2].setHeight(1); |
| 47 |
nodes[2].addChild(nodes[0]); |
| 48 |
nodes[2].addChild(nodes[1]); |
| 49 |
|
| 50 |
SimpleNode root = nodes[2]; |
| 51 |
|
| 52 |
if (numTaxa == 3) { |
| 53 |
nodes[3].setTaxon(taxa[2]); |
| 54 |
nodes[4].setHeight(2); |
| 55 |
nodes[4].addChild(nodes[2]); |
| 56 |
nodes[4].addChild(nodes[3]); |
| 57 |
root = nodes[4]; |
| 58 |
} |
| 59 |
|
| 60 |
Tree tree = new SimpleTree(root); |
| 61 |
tree.setUnits(Units.Type.YEARS); |
| 62 |
|
| 63 |
return new TreeModel(tree); |
| 64 |
} |
| 65 |
|
| 66 |
public void testAllPossibleAlignments() { |
| 67 |
SitePatterns patterns = new SitePatterns(alignment, null, 0, -1, 1, true); |
| 68 |
tryAllPossibleAlignments(3, patterns); |
| 69 |
} |
| 70 |
|
| 71 |
private void tryAllPossibleAlignments(int length, SitePatterns patterns) { |
| 72 |
System.out.println("Trying all possible " + length + " site alignments"); |
| 73 |
double[] patternLogLikeihoods = computeSitePatternLikelihoods(patterns); |
| 74 |
|
| 75 |
System.out.println("Site logLikelihoods: " + new Vector(patternLogLikeihoods)); |
| 76 |
|
| 77 |
double total = 0.0; |
| 78 |
|
| 79 |
List<Double> allAlignmentLogProbabilities = new ArrayList<Double>(); |
| 80 |
recursivelyComputeAlignmentLikelihood(allAlignmentLogProbabilities, patternLogLikeihoods, length, 0, 0); |
| 81 |
|
| 82 |
System.out.println("Total possible alignments: " + allAlignmentLogProbabilities.size()); |
| 83 |
|
| 84 |
for (Double x : allAlignmentLogProbabilities) { |
| 85 |
total += Math.exp(x); |
| 86 |
} |
| 87 |
System.out.println("Total probability = " + total); |
| 88 |
assertEquals("uncorrected", 1.0, total, tolerance); |
| 89 |
} |
| 90 |
|
| 91 |
private void recursivelyComputeAlignmentLikelihood(List<Double> finalLogProbabilities, double[] patternLogLikelihoods, |
| 92 |
int alignmentLength, int level, double logProbability) { |
| 93 |
|
| 94 |
if (level < alignmentLength) { |
| 95 |
for (int i = 0; i < patternLogLikelihoods.length; i++) { |
| 96 |
double thisLogProb = logProbability + patternLogLikelihoods[i]; |
| 97 |
recursivelyComputeAlignmentLikelihood(finalLogProbabilities, patternLogLikelihoods, alignmentLength, |
| 98 |
level + 1, thisLogProb); |
| 99 |
} |
| 100 |
} else { |
| 101 |
finalLogProbabilities.add(logProbability); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
protected static final String[][] TWO_TAXON_SEQUENCE = {{"human", "chimp", "gorrila"}, |
| 106 |
{"","", ""}}; |
| 107 |
|
| 108 |
} |