| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodel.branchmodel; |
| 27 |
|
| 28 |
import java.util.LinkedHashMap; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
import dr.evomodel.substmodel.FrequencyModel; |
| 32 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 33 |
import dr.evolution.tree.NodeRef; |
| 34 |
import dr.evomodel.tree.TreeModel; |
| 35 |
import dr.inference.model.AbstractModel; |
| 36 |
import dr.inference.model.Model; |
| 37 |
import dr.inference.model.Variable; |
| 38 |
import dr.inference.model.Variable.ChangeType; |
| 39 |
import dr.math.MathUtils; |
| 40 |
|
| 41 |
@SuppressWarnings("serial") |
| 42 |
public class RandomBranchAssignmentModel extends AbstractModel implements BranchModel { |
| 43 |
|
| 44 |
public static final boolean DEBUG = true; |
| 45 |
|
| 46 |
public static final String RANDOM_BRANCH_ASSIGNMENT_MODEL = "randomBranchAssignmentModel"; |
| 47 |
private final TreeModel treeModel; |
| 48 |
private final List<SubstitutionModel> substitutionModels; |
| 49 |
|
| 50 |
|
| 51 |
private LinkedHashMap<NodeRef, Integer> branchAssignmentMap; |
| 52 |
|
| 53 |
public RandomBranchAssignmentModel(TreeModel treeModel, |
| 54 |
List<SubstitutionModel> substitutionModels) { |
| 55 |
|
| 56 |
super(RANDOM_BRANCH_ASSIGNMENT_MODEL); |
| 57 |
|
| 58 |
|
| 59 |
this.treeModel = treeModel; |
| 60 |
this.substitutionModels = substitutionModels; |
| 61 |
|
| 62 |
int nodeCount = treeModel.getNodeCount(); |
| 63 |
int nModels = substitutionModels.size(); |
| 64 |
|
| 65 |
|
| 66 |
branchAssignmentMap = new LinkedHashMap<NodeRef, Integer>(); |
| 67 |
for (int i = 0; i < nodeCount; i++) { |
| 68 |
|
| 69 |
NodeRef node = treeModel.getNode(i); |
| 70 |
int branchClass = Integer.MAX_VALUE; |
| 71 |
|
| 72 |
if(DEBUG) { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
if(node.toString().equalsIgnoreCase("node 0, height=0.0: SimSeq1") || |
| 78 |
node.toString().equalsIgnoreCase("node 1, height=0.0: SimSeq2") || |
| 79 |
node.toString().equalsIgnoreCase("node 4, height=22.0") |
| 80 |
) { |
| 81 |
branchClass = 0; |
| 82 |
} else { |
| 83 |
branchClass = 1; |
| 84 |
} |
| 85 |
|
| 86 |
} else { |
| 87 |
branchClass = MathUtils.nextInt(nModels); |
| 88 |
} |
| 89 |
|
| 90 |
branchAssignmentMap.put(node, branchClass); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public Mapping getBranchModelMapping(NodeRef branch) { |
| 98 |
|
| 99 |
final int branchClass = branchAssignmentMap.get(branch); |
| 100 |
|
| 101 |
return new Mapping() { |
| 102 |
public int[] getOrder() { |
| 103 |
return new int[] { branchClass }; |
| 104 |
} |
| 105 |
|
| 106 |
public double[] getWeights() { |
| 107 |
return new double[] { 1.0 }; |
| 108 |
} |
| 109 |
}; |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public List<SubstitutionModel> getSubstitutionModels() { |
| 114 |
return substitutionModels; |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public SubstitutionModel getRootSubstitutionModel() { |
| 119 |
int rootClass = branchAssignmentMap.get(treeModel.getRoot()); |
| 120 |
return substitutionModels.get(rootClass); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public FrequencyModel getRootFrequencyModel() { |
| 125 |
return getRootSubstitutionModel().getFrequencyModel(); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public boolean requiresMatrixConvolution() { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 135 |
fireModelChanged(); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
protected void handleVariableChangedEvent(Variable variable, int index, |
| 140 |
ChangeType type) { |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
protected void storeState() { |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
protected void restoreState() { |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
protected void acceptState() { |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
} |