| 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.tipstatesmodel; |
| 27 |
|
| 28 |
import dr.evolution.alignment.PatternList; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.util.TaxonList; |
| 31 |
import dr.inference.model.AbstractModel; |
| 32 |
import dr.inference.model.Model; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.model.Variable; |
| 35 |
|
| 36 |
import java.util.HashMap; |
| 37 |
import java.util.Map; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public abstract class TipStatesModel extends AbstractModel { |
| 45 |
|
| 46 |
|
| 47 |
public enum Type { |
| 48 |
PARTIALS, |
| 49 |
STATES |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public TipStatesModel(String name, TaxonList includeTaxa, TaxonList excludeTaxa) { |
| 56 |
super(name); |
| 57 |
|
| 58 |
this.includeTaxa = includeTaxa; |
| 59 |
this.excludeTaxa = excludeTaxa; |
| 60 |
} |
| 61 |
|
| 62 |
public final void setTree(Tree tree) { |
| 63 |
this.tree = tree; |
| 64 |
|
| 65 |
int extNodeCount = tree.getExternalNodeCount(); |
| 66 |
|
| 67 |
excluded = new boolean[extNodeCount]; |
| 68 |
if (includeTaxa != null) { |
| 69 |
for (int i = 0; i < extNodeCount; i++) { |
| 70 |
if (includeTaxa.getTaxonIndex(tree.getNodeTaxon(tree.getExternalNode(i))) == -1) { |
| 71 |
excluded[i] = true; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if (excludeTaxa != null) { |
| 77 |
for (int i = 0; i < extNodeCount; i++) { |
| 78 |
if (excludeTaxa.getTaxonIndex(tree.getNodeTaxon(tree.getExternalNode(i))) != -1) { |
| 79 |
excluded[i] = true; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
states = new int[extNodeCount][]; |
| 86 |
|
| 87 |
taxaChanged(); |
| 88 |
} |
| 89 |
|
| 90 |
protected abstract void taxaChanged(); |
| 91 |
|
| 92 |
public final void setStates(PatternList patternList, int sequenceIndex, int nodeIndex, String taxonId) { |
| 93 |
if (this.patternList == null) { |
| 94 |
this.patternList = patternList; |
| 95 |
patternCount = patternList.getPatternCount(); |
| 96 |
stateCount = patternList.getDataType().getStateCount(); |
| 97 |
} else if (patternList != this.patternList) { |
| 98 |
throw new RuntimeException("The TipStatesModel with id, " + getId() + ", has already been associated with a patternList."); |
| 99 |
} |
| 100 |
if (this.states[nodeIndex] == null) { |
| 101 |
this.states[nodeIndex] = new int[patternCount]; |
| 102 |
} |
| 103 |
|
| 104 |
for (int i = 0; i < patternCount; i++) { |
| 105 |
this.states[nodeIndex][i] = patternList.getPatternState(sequenceIndex, i); |
| 106 |
} |
| 107 |
|
| 108 |
taxonMap.put(nodeIndex, taxonId); |
| 109 |
} |
| 110 |
|
| 111 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 112 |
fireModelChanged(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 124 |
fireModelChanged(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
protected void storeState() { |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
protected void restoreState() { |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
protected void acceptState() { |
| 146 |
} |
| 147 |
|
| 148 |
public PatternList getPatternList() { |
| 149 |
return patternList; |
| 150 |
} |
| 151 |
|
| 152 |
public abstract Type getModelType(); |
| 153 |
|
| 154 |
public abstract void getTipPartials(int nodeIndex, double[] tipPartials); |
| 155 |
|
| 156 |
public abstract void getTipStates(int nodeIndex, int[] tipStates); |
| 157 |
|
| 158 |
protected int[][] states; |
| 159 |
protected boolean[] excluded; |
| 160 |
|
| 161 |
protected int patternCount = 0; |
| 162 |
protected int stateCount; |
| 163 |
|
| 164 |
protected TaxonList includeTaxa; |
| 165 |
protected TaxonList excludeTaxa; |
| 166 |
|
| 167 |
protected Tree tree; |
| 168 |
|
| 169 |
private PatternList patternList = null; |
| 170 |
|
| 171 |
protected Map<Integer, String> taxonMap = new HashMap<Integer, String>(); |
| 172 |
} |