| 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.app.coalgen; |
| 27 |
|
| 28 |
import dr.evolution.coalescent.*; |
| 29 |
import dr.evolution.util.Taxa; |
| 30 |
import dr.evolution.util.TaxonList; |
| 31 |
import dr.evolution.util.Units; |
| 32 |
import dr.inference.trace.LogFileTraces; |
| 33 |
|
| 34 |
import java.io.File; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class CoalGenData { |
| 42 |
public static final String version = "1.0"; |
| 43 |
|
| 44 |
public static String[] demographicModels = {"Constant Population", |
| 45 |
"Exponential Growth (Growth Rate)", |
| 46 |
"Exponential Growth (Doubling Time)", |
| 47 |
"Logistic Growth (Growth Rate)", |
| 48 |
"Logistic Growth (Doubling Time)", |
| 49 |
"Expansion (Growth Rate)", |
| 50 |
"Expansion (Doubling Time)", |
| 51 |
"Piecewise Constant (Skyline)", |
| 52 |
"Piecewise Linear (Skyline)", |
| 53 |
"Piecewise Linear (GMRF Skyride)"}; |
| 54 |
|
| 55 |
public static String[][] argumentGuesses = { |
| 56 |
{"populationsize", "population", "popsize", "n0", "size", "pop"}, |
| 57 |
{"ancestralsize", "ancestralproportion", "proportion", "ancestral", "n1"}, |
| 58 |
{"growthrate", "growth", "rate", "r"}, |
| 59 |
{"doublingtime", "doubling", "time", "t"}, |
| 60 |
{"logisticshape", "halflife", "t50", "time50", "shape"}, |
| 61 |
{"populationsize", "population", "popsize", "n0", "size", "pop"}, |
| 62 |
{"groupsize"}}; |
| 63 |
|
| 64 |
public static String[] argumentNames = new String[]{ |
| 65 |
"Population Size", "Ancestral Proportion", "Growth Rate", "Doubling Time", "Logistic Shape (Half-life)", |
| 66 |
"Population Sizes", "Group Sizes" |
| 67 |
}; |
| 68 |
|
| 69 |
public int[][] argumentIndices = {{0}, {0, 2}, {0, 3}, {0, 2, 4}, {0, 3, 4}, {0, 1, 2}, {0, 1, 3}, {5, 6}, {5, 6}, {5}}; |
| 70 |
|
| 71 |
public File logFile = null; |
| 72 |
public File treesFile = null; |
| 73 |
public LogFileTraces traces = null; |
| 74 |
public TaxonList taxonList = new Taxa(); |
| 75 |
public int demographicModel = 0; |
| 76 |
|
| 77 |
public int popSizeCount = 0; |
| 78 |
public int groupSizeCount = 0; |
| 79 |
|
| 80 |
public double[] argumentValues = new double[] {1000, 0.1, 0.5, 10.0, 50.0, 1000, 1}; |
| 81 |
public boolean[] argumentFromTraces = new boolean[argumentNames.length]; |
| 82 |
public int[] argumentTraces = new int[argumentNames.length]; |
| 83 |
public int replicateCount = 1000; |
| 84 |
|
| 85 |
public CoalGenData() { |
| 86 |
} |
| 87 |
|
| 88 |
public void setDemographicModel(int model) { |
| 89 |
demographicModel = model; |
| 90 |
argumentTraces = new int[argumentIndices[demographicModel].length]; |
| 91 |
for (int i = 0; i < argumentTraces.length; i++) { |
| 92 |
argumentTraces[i] = -1; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
private int current = 0; |
| 97 |
|
| 98 |
public boolean hasNext() { |
| 99 |
if (traces != null) { |
| 100 |
return (current < traces.getStateCount()); |
| 101 |
} |
| 102 |
|
| 103 |
return (current < replicateCount); |
| 104 |
} |
| 105 |
|
| 106 |
private DemographicFunction demo = null; |
| 107 |
|
| 108 |
public DemographicFunction nextDemographic() { |
| 109 |
int n = replicateCount; |
| 110 |
|
| 111 |
if (traces != null) { |
| 112 |
n = traces.getStateCount(); |
| 113 |
} |
| 114 |
|
| 115 |
if (current == n) { |
| 116 |
current = 0; |
| 117 |
demo = null; |
| 118 |
return null; |
| 119 |
} |
| 120 |
|
| 121 |
if (demo == null) { |
| 122 |
|
| 123 |
current = 0; |
| 124 |
|
| 125 |
if (traces != null) { |
| 126 |
traces.setBurnIn(0); |
| 127 |
} |
| 128 |
|
| 129 |
if (demographicModel == 0) { |
| 130 |
demo = new ConstantPopulation(Units.Type.YEARS); |
| 131 |
} else if (demographicModel == 1 || demographicModel == 2) { |
| 132 |
demo = new ExponentialGrowth(Units.Type.YEARS); |
| 133 |
} else if (demographicModel == 3 || demographicModel == 4) { |
| 134 |
demo = new LogisticGrowth(Units.Type.YEARS); |
| 135 |
} else if (demographicModel == 5 || demographicModel == 6) { |
| 136 |
demo = new Expansion(Units.Type.YEARS); |
| 137 |
} else if (demographicModel == 7) { |
| 138 |
demo = new PiecewiseConstantPopulation(Units.Type.YEARS); |
| 139 |
} else if (demographicModel == 8) { |
| 140 |
demo = new PiecewiseLinearPopulation(Units.Type.YEARS); |
| 141 |
} else if (demographicModel == 9) { |
| 142 |
demo = new PiecewiseLinearPopulation(Units.Type.YEARS); |
| 143 |
} |
| 144 |
|
| 145 |
guessArguments(); |
| 146 |
|
| 147 |
if (demographicModel >= 7) { |
| 148 |
popSizeCount = getTraceRange(argumentTraces[0]); |
| 149 |
if (demographicModel < 9) { |
| 150 |
groupSizeCount = getTraceRange(argumentTraces[1]); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
if (traces != null) { |
| 157 |
double value; |
| 158 |
if (demographicModel == 0) { |
| 159 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][0]], current); |
| 160 |
((ConstantPopulation) demo).setN0(value); |
| 161 |
} else if (demographicModel == 1) { |
| 162 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][0]], current); |
| 163 |
((ExponentialGrowth) demo).setN0(value); |
| 164 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][1]], current); |
| 165 |
((ExponentialGrowth) demo).setGrowthRate(value); |
| 166 |
} else if (demographicModel == 2) { |
| 167 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][0]], current); |
| 168 |
((ExponentialGrowth) demo).setN0(value); |
| 169 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][1]], current); |
| 170 |
((ExponentialGrowth) demo).setDoublingTime(value); |
| 171 |
} else if (demographicModel == 3) { |
| 172 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][0]], current); |
| 173 |
((LogisticGrowth) demo).setN0(value); |
| 174 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][1]], current); |
| 175 |
((LogisticGrowth) demo).setGrowthRate(value); |
| 176 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][2]], current); |
| 177 |
((LogisticGrowth) demo).setTime50(value); |
| 178 |
} else if (demographicModel == 4) { |
| 179 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][0]], current); |
| 180 |
((LogisticGrowth) demo).setN0(value); |
| 181 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][1]], current); |
| 182 |
((LogisticGrowth) demo).setDoublingTime(value); |
| 183 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][2]], current); |
| 184 |
((LogisticGrowth) demo).setTime50(value); |
| 185 |
} else if (demographicModel == 5) { |
| 186 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][0]], current); |
| 187 |
((Expansion) demo).setN0(value); |
| 188 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][1]], current); |
| 189 |
((Expansion) demo).setProportion(value); |
| 190 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][2]], current); |
| 191 |
((Expansion) demo).setGrowthRate(value); |
| 192 |
} else if (demographicModel == 6) { |
| 193 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][0]], current); |
| 194 |
((Expansion) demo).setN0(value); |
| 195 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][1]], current); |
| 196 |
((Expansion) demo).setProportion(value); |
| 197 |
value = traces.getStateValue(argumentTraces[argumentIndices[demographicModel][2]], current); |
| 198 |
((Expansion) demo).setDoublingTime(value); |
| 199 |
} else if (demographicModel >= 7) { |
| 200 |
} |
| 201 |
} else { |
| 202 |
double value; |
| 203 |
if (demographicModel == 0) { |
| 204 |
value = argumentValues[argumentIndices[demographicModel][0]]; |
| 205 |
((ConstantPopulation) demo).setN0(value); |
| 206 |
} else if (demographicModel == 1) { |
| 207 |
value = argumentValues[argumentIndices[demographicModel][0]]; |
| 208 |
((ExponentialGrowth) demo).setN0(value); |
| 209 |
value = argumentValues[argumentIndices[demographicModel][1]]; |
| 210 |
((ExponentialGrowth) demo).setGrowthRate(value); |
| 211 |
} else if (demographicModel == 2) { |
| 212 |
value = argumentValues[argumentIndices[demographicModel][0]]; |
| 213 |
((ExponentialGrowth) demo).setN0(value); |
| 214 |
value = argumentValues[argumentIndices[demographicModel][1]]; |
| 215 |
((ExponentialGrowth) demo).setDoublingTime(value); |
| 216 |
} else if (demographicModel == 3) { |
| 217 |
value = argumentValues[argumentIndices[demographicModel][0]]; |
| 218 |
((LogisticGrowth) demo).setN0(value); |
| 219 |
value = argumentValues[argumentIndices[demographicModel][1]]; |
| 220 |
((LogisticGrowth) demo).setGrowthRate(value); |
| 221 |
value = argumentValues[argumentIndices[demographicModel][2]]; |
| 222 |
((LogisticGrowth) demo).setTime50(value); |
| 223 |
} else if (demographicModel == 4) { |
| 224 |
value = argumentValues[argumentIndices[demographicModel][0]]; |
| 225 |
((LogisticGrowth) demo).setN0(value); |
| 226 |
value = argumentValues[argumentIndices[demographicModel][1]]; |
| 227 |
((LogisticGrowth) demo).setDoublingTime(value); |
| 228 |
value = argumentValues[argumentIndices[demographicModel][2]]; |
| 229 |
((LogisticGrowth) demo).setTime50(value); |
| 230 |
} else if (demographicModel == 5) { |
| 231 |
value = argumentValues[argumentIndices[demographicModel][0]]; |
| 232 |
((Expansion) demo).setN0(value); |
| 233 |
value = argumentValues[argumentIndices[demographicModel][1]]; |
| 234 |
((Expansion) demo).setProportion(value); |
| 235 |
value = argumentValues[argumentIndices[demographicModel][2]]; |
| 236 |
((Expansion) demo).setGrowthRate(value); |
| 237 |
} else if (demographicModel == 6) { |
| 238 |
value = argumentValues[argumentIndices[demographicModel][0]]; |
| 239 |
((Expansion) demo).setN0(value); |
| 240 |
value = argumentValues[argumentIndices[demographicModel][1]]; |
| 241 |
((Expansion) demo).setProportion(value); |
| 242 |
value = argumentValues[argumentIndices[demographicModel][2]]; |
| 243 |
((Expansion) demo).setDoublingTime(value); |
| 244 |
} else if (demographicModel >= 7) { |
| 245 |
throw new IllegalArgumentException("Fixed epoch model not supported"); |
| 246 |
} |
| 247 |
} |
| 248 |
current++; |
| 249 |
|
| 250 |
return demo; |
| 251 |
} |
| 252 |
|
| 253 |
public void setupSkyline() { |
| 254 |
guessArguments(); |
| 255 |
popSizeCount = getTraceRange(argumentTraces[0]); |
| 256 |
if (demographicModel < 9) { |
| 257 |
groupSizeCount = getTraceRange(argumentTraces[1]); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
public void getNextSkyline(double[] popSizes, double[] groupSizes) { |
| 262 |
int popSizeIndex = argumentTraces[0]; |
| 263 |
for (int i = 0; i < popSizeCount; i++) { |
| 264 |
popSizes[i] = traces.getStateValue(popSizeIndex + i, current); |
| 265 |
} |
| 266 |
|
| 267 |
if (groupSizes != null) { |
| 268 |
int groupSizeIndex = argumentTraces[1]; |
| 269 |
for (int i = 0; i < groupSizeCount; i++) { |
| 270 |
groupSizes[i] = traces.getStateValue(groupSizeIndex + i, current); |
| 271 |
} |
| 272 |
} |
| 273 |
current++; |
| 274 |
} |
| 275 |
|
| 276 |
public void reset() { |
| 277 |
current = 0; |
| 278 |
} |
| 279 |
|
| 280 |
private void guessArguments() { |
| 281 |
int argumentCount = argumentIndices[demographicModel].length; |
| 282 |
|
| 283 |
for (int i = 0; i < argumentCount; i++) { |
| 284 |
|
| 285 |
int index = -1; |
| 286 |
|
| 287 |
for (int j = 0; j < argumentGuesses[i].length; j++) { |
| 288 |
if (index != -1) break; |
| 289 |
|
| 290 |
index = findArgument(argumentGuesses[argumentIndices[demographicModel][i]][j]); |
| 291 |
} |
| 292 |
|
| 293 |
argumentTraces[i] = index; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
private int findArgument(String argument) { |
| 298 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 299 |
String item = traces.getTraceName(i).toLowerCase(); |
| 300 |
if (item.indexOf(argument) != -1) return i; |
| 301 |
} |
| 302 |
return -1; |
| 303 |
} |
| 304 |
|
| 305 |
private int getTraceRange(int first) { |
| 306 |
int i = 1; |
| 307 |
int k = first; |
| 308 |
|
| 309 |
String name = traces.getTraceName(first); |
| 310 |
String root = name.substring(0, name.length() - 1); |
| 311 |
while (k < traces.getTraceCount() && traces.getTraceName(k).equals(root + i)) { |
| 312 |
i++; |
| 313 |
k++; |
| 314 |
} |
| 315 |
|
| 316 |
return i - 1; |
| 317 |
} |
| 318 |
} |
| 319 |
|