| 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.structure; |
| 27 |
|
| 28 |
import dr.evolution.coalescent.IntervalType; |
| 29 |
import dr.evolution.colouring.BranchColouring; |
| 30 |
import dr.evolution.colouring.TreeColouring; |
| 31 |
import dr.evolution.tree.ColourChange; |
| 32 |
import dr.evolution.tree.NodeRef; |
| 33 |
import dr.evolution.tree.Tree; |
| 34 |
|
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.Collections; |
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class ColouredTreeIntervals implements StructuredIntervalList { |
| 44 |
|
| 45 |
int intervalCount; |
| 46 |
int sampleCount; |
| 47 |
List<Event> eventList = new ArrayList<Event>(); |
| 48 |
int colourStateCount; |
| 49 |
int[][] lineageCount; |
| 50 |
Type units; |
| 51 |
|
| 52 |
public ColouredTreeIntervals(Tree tree, TreeColouring colouring) { |
| 53 |
|
| 54 |
colourStateCount = colouring.getColourCount(); |
| 55 |
units = tree.getUnits(); |
| 56 |
extractCoalescentEvents(tree, colouring, eventList); |
| 57 |
sampleCount = extractSampleEvents(tree, colouring, eventList); |
| 58 |
extractMigrationEvents(tree, colouring, eventList); |
| 59 |
Collections.sort(eventList); |
| 60 |
|
| 61 |
lineageCount = new int[eventList.size() + 1][colourStateCount]; |
| 62 |
int externalNodeCount = tree.getExternalNodeCount(); |
| 63 |
for (int i = 0; i < externalNodeCount; i++) { |
| 64 |
NodeRef node = tree.getExternalNode(i); |
| 65 |
double time = tree.getNodeHeight(node); |
| 66 |
int colour = colouring.getNodeColour(node); |
| 67 |
if (time == 0.0) { |
| 68 |
lineageCount[0][colour] += 1; |
| 69 |
} |
| 70 |
} |
| 71 |
for (int i = 0; i < eventList.size(); i++) { |
| 72 |
Event event = eventList.get(i); |
| 73 |
|
| 74 |
|
| 75 |
for (int j = 0; j < colourStateCount; j++) { |
| 76 |
lineageCount[i + 1][j] = lineageCount[i][j] + event.lineageChanges[j]; |
| 77 |
if (lineageCount[i + 1][j] < 0) { |
| 78 |
throw new RuntimeException("lineageCount[" + (i + 1) + "][" + j + "] = " + lineageCount[i + 1][j] + ". This is wrong!"); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
intervalCount = eventList.size(); |
| 83 |
} |
| 84 |
|
| 85 |
public int getPopulationCount() { |
| 86 |
return lineageCount[0].length; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public int getIntervalCount() { |
| 93 |
return intervalCount; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public int getSampleCount() { |
| 100 |
return sampleCount; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public double getInterval(int i) { |
| 107 |
if (i == 0) return getEvent(i).time; |
| 108 |
return getEvent(i).time - getEvent(i - 1).time; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public int getLineageCount(int interval, int population) { |
| 116 |
return lineageCount[interval][population]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public int getLineageCount(int interval) { |
| 124 |
int totalLineages = 0; |
| 125 |
for (int i = 0; i < colourStateCount; i++) { |
| 126 |
totalLineages += lineageCount[interval][i]; |
| 127 |
} |
| 128 |
return totalLineages; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public int getCoalescentEvents(int i) { |
| 135 |
if (getEvent(i).getType() == IntervalType.COALESCENT) return 1; |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public IntervalType getIntervalType(int i) { |
| 143 |
return getEvent(i).getType(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public double getTotalDuration() { |
| 150 |
return getEvent(getIntervalCount()).time; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public boolean isBinaryCoalescent() { |
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public boolean isCoalescentOnly() { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public Type getUnits() { |
| 175 |
return units; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public void setUnits(Type units) { |
| 182 |
this.units = units; |
| 183 |
} |
| 184 |
|
| 185 |
private void extractCoalescentEvents(Tree tree, TreeColouring colouring, List<Event> eventList) { |
| 186 |
int internalNodeCount = tree.getInternalNodeCount(); |
| 187 |
for (int i = 0; i < internalNodeCount; i++) { |
| 188 |
NodeRef node = tree.getInternalNode(i); |
| 189 |
double time = tree.getNodeHeight(node); |
| 190 |
int colour = colouring.getNodeColour(node); |
| 191 |
eventList.add(Event.createCoalescentEvent(time, colour, colouring.getColourCount())); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private int extractSampleEvents(Tree tree, TreeColouring colouring, List<Event> eventList) { |
| 196 |
int externalNodeCount = tree.getExternalNodeCount(); |
| 197 |
int sampleEventCount = 0; |
| 198 |
for (int i = 0; i < externalNodeCount; i++) { |
| 199 |
NodeRef node = tree.getExternalNode(i); |
| 200 |
double time = tree.getNodeHeight(node); |
| 201 |
int colour = colouring.getNodeColour(node); |
| 202 |
if (time != 0.0) { |
| 203 |
eventList.add(Event.createAddSampleEvent(time, colour, colouring.getColourCount())); |
| 204 |
sampleEventCount += 1; |
| 205 |
} |
| 206 |
} |
| 207 |
return sampleEventCount; |
| 208 |
} |
| 209 |
|
| 210 |
private void extractMigrationEvents(Tree tree, TreeColouring colouring, List<Event> eventList) { |
| 211 |
int nodeCount = tree.getNodeCount(); |
| 212 |
for (int i = 0; i < nodeCount; i++) { |
| 213 |
NodeRef node = tree.getNode(i); |
| 214 |
if (!tree.isRoot(node)) { |
| 215 |
|
| 216 |
BranchColouring branchColouring = colouring.getBranchColouring(node); |
| 217 |
List<ColourChange> changes = branchColouring.getColourChanges(); |
| 218 |
int belowColour = colouring.getNodeColour(node); |
| 219 |
for (ColourChange change : changes) { |
| 220 |
|
| 221 |
double time = change.getTime(); |
| 222 |
int aboveColour = change.getColourAbove(); |
| 223 |
eventList.add(Event.createMigrationEvent(time, belowColour, aboveColour, colouring.getColourCount())); |
| 224 |
belowColour = aboveColour; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
public final Event getEvent(int i) { |
| 231 |
return eventList.get(i); |
| 232 |
} |
| 233 |
|
| 234 |
} |