| 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 dr.evomodel.substmodel.FrequencyModel; |
| 29 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 30 |
import dr.evolution.tree.NodeRef; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.inference.model.AbstractModel; |
| 33 |
import dr.inference.model.Model; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.inference.model.Variable; |
| 36 |
import dr.util.Author; |
| 37 |
import dr.util.Citable; |
| 38 |
import dr.util.Citation; |
| 39 |
|
| 40 |
import java.util.*; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@SuppressWarnings("serial") |
| 49 |
public class EpochBranchModel extends AbstractModel implements BranchModel, Citable { |
| 50 |
|
| 51 |
public static final String EPOCH_BRANCH_MODEL = "EpochBranchModel"; |
| 52 |
|
| 53 |
|
| 54 |
public EpochBranchModel(TreeModel tree, |
| 55 |
List<SubstitutionModel> substitutionModels, |
| 56 |
Parameter epochTimes) { |
| 57 |
|
| 58 |
super(EPOCH_BRANCH_MODEL); |
| 59 |
|
| 60 |
this.substitutionModels = substitutionModels; |
| 61 |
if (substitutionModels == null || substitutionModels.size() == 0) { |
| 62 |
throw new IllegalArgumentException("EpochBranchModel must be provided with at least one substitution model"); |
| 63 |
} |
| 64 |
|
| 65 |
this.epochTimes = epochTimes; |
| 66 |
this.tree = tree; |
| 67 |
|
| 68 |
for (SubstitutionModel model : substitutionModels) { |
| 69 |
addModel(model); |
| 70 |
} |
| 71 |
|
| 72 |
addModel(tree); |
| 73 |
addVariable(epochTimes); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public Mapping getBranchModelMapping(NodeRef node) { |
| 78 |
|
| 79 |
int nModels = substitutionModels.size(); |
| 80 |
int epochCount = nModels - 1; |
| 81 |
|
| 82 |
double[] transitionTimes = epochTimes.getParameterValues(); |
| 83 |
|
| 84 |
double parentHeight = tree.getNodeHeight(tree.getParent(node)); |
| 85 |
double nodeHeight = tree.getNodeHeight(node); |
| 86 |
|
| 87 |
List<Double> weightList = new ArrayList<Double>(); |
| 88 |
List<Integer> orderList = new ArrayList<Integer>(); |
| 89 |
|
| 90 |
|
| 91 |
int epoch = 0; |
| 92 |
while (epoch < epochCount && nodeHeight >= transitionTimes[epoch]) { |
| 93 |
epoch ++; |
| 94 |
} |
| 95 |
|
| 96 |
double currentHeight = nodeHeight; |
| 97 |
|
| 98 |
|
| 99 |
while (epoch < epochCount && parentHeight >= transitionTimes[epoch]) { |
| 100 |
weightList.add( transitionTimes[epoch] - currentHeight ); |
| 101 |
orderList.add(epoch); |
| 102 |
|
| 103 |
currentHeight = transitionTimes[epoch]; |
| 104 |
|
| 105 |
epoch ++; |
| 106 |
} |
| 107 |
|
| 108 |
weightList.add( parentHeight - currentHeight ); |
| 109 |
orderList.add(epoch); |
| 110 |
|
| 111 |
|
| 112 |
if (orderList.size() == 0) { |
| 113 |
throw new RuntimeException("EpochBranchModel failed to give a valid mapping"); |
| 114 |
} |
| 115 |
|
| 116 |
final int[] order = new int[orderList.size()]; |
| 117 |
final double[] weights = new double[weightList.size()]; |
| 118 |
for (int i = 0; i < orderList.size(); i++) { |
| 119 |
order[i] = orderList.get(i); |
| 120 |
weights[i] = weightList.get(i); |
| 121 |
} |
| 122 |
|
| 123 |
return new Mapping() { |
| 124 |
@Override |
| 125 |
public int[] getOrder() { |
| 126 |
return order; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public double[] getWeights() { |
| 131 |
return weights; |
| 132 |
} |
| 133 |
}; |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public boolean requiresMatrixConvolution() { |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public List<SubstitutionModel> getSubstitutionModels() { |
| 143 |
return substitutionModels; |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public SubstitutionModel getRootSubstitutionModel() { |
| 148 |
return substitutionModels.get(substitutionModels.size() - 1); |
| 149 |
} |
| 150 |
|
| 151 |
public FrequencyModel getRootFrequencyModel() { |
| 152 |
return getRootSubstitutionModel().getFrequencyModel(); |
| 153 |
} |
| 154 |
|
| 155 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 156 |
fireModelChanged(); |
| 157 |
} |
| 158 |
|
| 159 |
@SuppressWarnings("rawtypes") |
| 160 |
protected void handleVariableChangedEvent(Variable variable, int index, |
| 161 |
Parameter.ChangeType type) { |
| 162 |
} |
| 163 |
|
| 164 |
protected void storeState() { |
| 165 |
} |
| 166 |
|
| 167 |
protected void restoreState() { |
| 168 |
} |
| 169 |
|
| 170 |
protected void acceptState() { |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public Citation.Category getCategory() { |
| 175 |
return Citation.Category.SUBSTITUTION_MODELS; |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public String getDescription() { |
| 180 |
return "Epoch Branch model"; |
| 181 |
} |
| 182 |
|
| 183 |
public List<Citation> getCitations() { |
| 184 |
return Arrays.asList( |
| 185 |
new Citation(new Author[]{new Author("F", "Bielejec"), |
| 186 |
new Author("P", "Lemey"), new Author("G", "Baele"), new Author("A", "Rambaut"), |
| 187 |
new Author("MA", "Suchard")}, Citation.Status.IN_PREPARATION)); |
| 188 |
} |
| 189 |
|
| 190 |
private final TreeModel tree; |
| 191 |
private final List<SubstitutionModel> substitutionModels; |
| 192 |
private final Parameter epochTimes; |
| 193 |
} |