| 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.evolution.coalescent; |
| 27 |
|
| 28 |
import dr.evolution.util.Units; |
| 29 |
|
| 30 |
import java.util.Arrays; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class Intervals implements IntervalList { |
| 40 |
|
| 41 |
public Intervals(int maxEventCount) { |
| 42 |
events = new Event[maxEventCount]; |
| 43 |
for (int i = 0; i < maxEventCount; i++) { |
| 44 |
events[i] = new Event(); |
| 45 |
} |
| 46 |
eventCount = 0; |
| 47 |
sampleCount = 0; |
| 48 |
|
| 49 |
intervals = new double[maxEventCount - 1]; |
| 50 |
intervalTypes = new IntervalType[maxEventCount - 1]; |
| 51 |
lineageCounts = new int[maxEventCount - 1]; |
| 52 |
|
| 53 |
intervalsKnown = false; |
| 54 |
} |
| 55 |
|
| 56 |
public void copyIntervals(Intervals source) { |
| 57 |
intervalsKnown = source.intervalsKnown; |
| 58 |
eventCount = source.eventCount; |
| 59 |
sampleCount = source.sampleCount; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if (intervalsKnown) { |
| 69 |
System.arraycopy(source.intervals, 0, intervals, 0, intervals.length); |
| 70 |
System.arraycopy(source.intervalTypes, 0, intervalTypes, 0, intervals.length); |
| 71 |
System.arraycopy(source.lineageCounts, 0, lineageCounts, 0, intervals.length); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public void resetEvents() { |
| 76 |
intervalsKnown = false; |
| 77 |
eventCount = 0; |
| 78 |
sampleCount = 0; |
| 79 |
} |
| 80 |
|
| 81 |
public void addSampleEvent(double time) { |
| 82 |
events[eventCount].time = time; |
| 83 |
events[eventCount].type = IntervalType.SAMPLE; |
| 84 |
eventCount++; |
| 85 |
sampleCount++; |
| 86 |
intervalsKnown = false; |
| 87 |
} |
| 88 |
|
| 89 |
public void addCoalescentEvent(double time) { |
| 90 |
events[eventCount].time = time; |
| 91 |
events[eventCount].type = IntervalType.COALESCENT; |
| 92 |
eventCount++; |
| 93 |
intervalsKnown = false; |
| 94 |
} |
| 95 |
|
| 96 |
public void addMigrationEvent(double time, int destination) { |
| 97 |
events[eventCount].time = time; |
| 98 |
events[eventCount].type = IntervalType.MIGRATION; |
| 99 |
events[eventCount].info = destination; |
| 100 |
eventCount++; |
| 101 |
intervalsKnown = false; |
| 102 |
} |
| 103 |
|
| 104 |
public void addNothingEvent(double time) { |
| 105 |
events[eventCount].time = time; |
| 106 |
events[eventCount].type = IntervalType.NOTHING; |
| 107 |
eventCount++; |
| 108 |
intervalsKnown = false; |
| 109 |
} |
| 110 |
|
| 111 |
public int getSampleCount() { |
| 112 |
return sampleCount; |
| 113 |
} |
| 114 |
|
| 115 |
public int getIntervalCount() { |
| 116 |
if (!intervalsKnown) calculateIntervals(); |
| 117 |
return intervalCount; |
| 118 |
} |
| 119 |
|
| 120 |
public double getInterval(int i) { |
| 121 |
if (!intervalsKnown) calculateIntervals(); |
| 122 |
return intervals[i]; |
| 123 |
} |
| 124 |
|
| 125 |
public int getLineageCount(int i) { |
| 126 |
if (!intervalsKnown) calculateIntervals(); |
| 127 |
return lineageCounts[i]; |
| 128 |
} |
| 129 |
|
| 130 |
public int getCoalescentEvents(int i) { |
| 131 |
if (!intervalsKnown) calculateIntervals(); |
| 132 |
if (i < intervalCount - 1) { |
| 133 |
return lineageCounts[i] - lineageCounts[i + 1]; |
| 134 |
} else { |
| 135 |
return lineageCounts[i] - 1; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public IntervalType getIntervalType(int i) { |
| 140 |
if (!intervalsKnown) calculateIntervals(); |
| 141 |
return intervalTypes[i]; |
| 142 |
} |
| 143 |
|
| 144 |
public double getTotalDuration() { |
| 145 |
|
| 146 |
if (!intervalsKnown) calculateIntervals(); |
| 147 |
return events[eventCount - 1].time; |
| 148 |
} |
| 149 |
|
| 150 |
public boolean isBinaryCoalescent() { |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
public boolean isCoalescentOnly() { |
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
private void calculateIntervals() { |
| 159 |
|
| 160 |
if (eventCount < 2) { |
| 161 |
throw new IllegalArgumentException("Too few events to construct intervals"); |
| 162 |
} |
| 163 |
|
| 164 |
Arrays.sort(events, 0, eventCount); |
| 165 |
|
| 166 |
if (events[0].type != IntervalType.SAMPLE) { |
| 167 |
throw new IllegalArgumentException("First event is not a sample event"); |
| 168 |
} |
| 169 |
|
| 170 |
intervalCount = eventCount - 1; |
| 171 |
|
| 172 |
double lastTime = events[0].time; |
| 173 |
|
| 174 |
int lineages = 1; |
| 175 |
for (int i = 1; i < eventCount; i++) { |
| 176 |
|
| 177 |
intervals[i - 1] = events[i].time - lastTime; |
| 178 |
intervalTypes[i - 1] = events[i].type; |
| 179 |
lineageCounts[i - 1] = lineages; |
| 180 |
if (events[i].type == IntervalType.SAMPLE) { |
| 181 |
lineages++; |
| 182 |
} else if (events[i].type == IntervalType.COALESCENT) { |
| 183 |
lineages--; |
| 184 |
} |
| 185 |
lastTime = events[i].time; |
| 186 |
} |
| 187 |
intervalsKnown = true; |
| 188 |
} |
| 189 |
|
| 190 |
private Units.Type units = Units.Type.GENERATIONS; |
| 191 |
|
| 192 |
public final Units.Type getUnits() { |
| 193 |
return units; |
| 194 |
} |
| 195 |
|
| 196 |
public final void setUnits(Type units) { |
| 197 |
this.units = units; |
| 198 |
} |
| 199 |
|
| 200 |
private class Event implements Comparable { |
| 201 |
|
| 202 |
public int compareTo(Object o) { |
| 203 |
double t = ((Event) o).time; |
| 204 |
if (t < time) { |
| 205 |
return 1; |
| 206 |
} else if (t > time) { |
| 207 |
return -1; |
| 208 |
} else { |
| 209 |
|
| 210 |
return type.compareTo(((Event) o).type); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
IntervalType type; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
double time; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
int info; |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
private Event[] events; |
| 232 |
private int eventCount; |
| 233 |
private int sampleCount; |
| 234 |
|
| 235 |
private boolean intervalsKnown = false; |
| 236 |
private double[] intervals; |
| 237 |
private int[] lineageCounts; |
| 238 |
private IntervalType[] intervalTypes; |
| 239 |
|
| 240 |
private int intervalCount = 0; |
| 241 |
} |