| 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.transmission; |
| 27 |
|
| 28 |
import dr.evolution.colouring.DefaultBranchColouring; |
| 29 |
import dr.evolution.colouring.DefaultTreeColouring; |
| 30 |
import dr.evolution.colouring.TreeColouring; |
| 31 |
import dr.evolution.colouring.TreeColouringProvider; |
| 32 |
import dr.evolution.tree.NodeRef; |
| 33 |
import dr.evolution.tree.Tree; |
| 34 |
import dr.evolution.util.Taxon; |
| 35 |
import dr.evolution.util.Units; |
| 36 |
import dr.evoxml.util.XMLUnits; |
| 37 |
import dr.inference.model.AbstractModel; |
| 38 |
import dr.inference.model.Model; |
| 39 |
import dr.inference.model.Parameter; |
| 40 |
import dr.inference.model.Variable; |
| 41 |
import dr.xml.*; |
| 42 |
|
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.HashMap; |
| 45 |
import java.util.List; |
| 46 |
import java.util.Map; |
| 47 |
import java.util.logging.Logger; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public class TransmissionHistoryModel extends AbstractModel implements TreeColouringProvider, Units { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static String TRANSMISSION_HISTORY_MODEL = "transmissionHistory"; |
| 65 |
public static String TRANSMISSION = "transmission"; |
| 66 |
public static String DONOR = "donor"; |
| 67 |
public static String RECIPIENT = "recipient"; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public TransmissionHistoryModel(Type units) { |
| 74 |
|
| 75 |
this(TRANSMISSION_HISTORY_MODEL, units); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public TransmissionHistoryModel(String name, Type units) { |
| 82 |
|
| 83 |
super(name); |
| 84 |
|
| 85 |
setUnits(units); |
| 86 |
} |
| 87 |
|
| 88 |
private void addTransmission(Taxon donor, Taxon recipient, Parameter parameter) { |
| 89 |
if (donor.equals(recipient)) { |
| 90 |
throw new RuntimeException("Donor and recipient are the same, " + donor); |
| 91 |
} |
| 92 |
|
| 93 |
if (parameter != null) { |
| 94 |
addVariable(parameter); |
| 95 |
} |
| 96 |
|
| 97 |
TransmissionEvent transmissionEvent = new TransmissionEvent(donor, recipient, parameter); |
| 98 |
transmissionEvents.add(transmissionEvent); |
| 99 |
transmissionEventMap.put(recipient, transmissionEvent); |
| 100 |
if (!hosts.contains(donor)) { |
| 101 |
hosts.add(donor); |
| 102 |
} |
| 103 |
if (!hosts.contains(recipient)) { |
| 104 |
hosts.add(recipient); |
| 105 |
} |
| 106 |
|
| 107 |
Logger.getLogger("dr.evomodel").info("Transmission from " |
| 108 |
+ donor + " to " + recipient + (parameter != null ? "at " + parameter.getParameterValue(0) : "")); |
| 109 |
} |
| 110 |
|
| 111 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 119 |
} |
| 120 |
|
| 121 |
public int getTransmissionEventCount() { |
| 122 |
return transmissionEvents.size(); |
| 123 |
} |
| 124 |
|
| 125 |
public TransmissionEvent getTransmissionEvent(int index) { |
| 126 |
return transmissionEvents.get(index); |
| 127 |
} |
| 128 |
|
| 129 |
public TransmissionEvent getTransmissionEventToHost(Taxon recipient) { |
| 130 |
return transmissionEventMap.get(recipient); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
protected void storeState() { |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected void restoreState() { |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
protected void acceptState() { |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public final void setUnits(Type u) { |
| 166 |
units = u; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public final Type getUnits() { |
| 174 |
return units; |
| 175 |
} |
| 176 |
|
| 177 |
private Type units; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 183 |
|
| 184 |
public String getParserName() { |
| 185 |
return TRANSMISSION_HISTORY_MODEL; |
| 186 |
} |
| 187 |
|
| 188 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 189 |
|
| 190 |
Type units = XMLUnits.Utils.getUnitsAttr(xo); |
| 191 |
|
| 192 |
TransmissionHistoryModel history = new TransmissionHistoryModel(units); |
| 193 |
|
| 194 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 195 |
XMLObject xoc = (XMLObject) xo.getChild(i); |
| 196 |
if (xoc.getName().equals(TRANSMISSION)) { |
| 197 |
Taxon donor = (Taxon) xoc.getElementFirstChild(DONOR); |
| 198 |
Taxon recipient = (Taxon) xoc.getElementFirstChild(RECIPIENT); |
| 199 |
if (donor.equals(recipient)) { |
| 200 |
throw new XMLParseException("Donor and recipient in TransmissionHistoryModel are the same: " + donor); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
Parameter parameter = (Parameter) xoc.getChild(Parameter.class); |
| 205 |
history.addTransmission(donor, recipient, parameter); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return history; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public String getParserDescription() { |
| 217 |
return "Defines a transmission history"; |
| 218 |
} |
| 219 |
|
| 220 |
public Class getReturnType() { |
| 221 |
return TransmissionHistoryModel.class; |
| 222 |
} |
| 223 |
|
| 224 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 225 |
return rules; |
| 226 |
} |
| 227 |
|
| 228 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 229 |
XMLUnits.UNITS_RULE, |
| 230 |
new ElementRule(TRANSMISSION, |
| 231 |
new XMLSyntaxRule[]{ |
| 232 |
|
| 233 |
new ElementRule(Parameter.class), |
| 234 |
new ElementRule(DONOR, |
| 235 |
new XMLSyntaxRule[]{new ElementRule(Taxon.class)}), |
| 236 |
new ElementRule(RECIPIENT, |
| 237 |
new XMLSyntaxRule[]{new ElementRule(Taxon.class)}) |
| 238 |
}, 1, Integer.MAX_VALUE |
| 239 |
) |
| 240 |
}; |
| 241 |
|
| 242 |
}; |
| 243 |
|
| 244 |
public Taxon getHost(int index) { |
| 245 |
return hosts.get(index); |
| 246 |
} |
| 247 |
|
| 248 |
public int getHostIndex(Taxon host) { |
| 249 |
return hosts.indexOf(host); |
| 250 |
} |
| 251 |
|
| 252 |
public int getHostCount() { |
| 253 |
return hosts.size(); |
| 254 |
} |
| 255 |
|
| 256 |
public TreeColouring getTreeColouring(Tree tree) { |
| 257 |
DefaultTreeColouring treeColouring = new DefaultTreeColouring(getHostCount(), tree); |
| 258 |
|
| 259 |
createTreeColouring(tree, tree.getRoot(), treeColouring); |
| 260 |
|
| 261 |
return treeColouring; |
| 262 |
} |
| 263 |
|
| 264 |
private Taxon createTreeColouring(Tree tree, NodeRef node, DefaultTreeColouring treeColouring) { |
| 265 |
Taxon parentHost = null; |
| 266 |
Taxon childHost; |
| 267 |
|
| 268 |
if (tree.isExternal(node)) { |
| 269 |
childHost = (Taxon) tree.getNodeTaxon(node).getAttribute("host"); |
| 270 |
if (childHost == null) { |
| 271 |
throw new RuntimeException("One or more of the viruses tree's taxa are missing the 'host' attribute"); |
| 272 |
} |
| 273 |
|
| 274 |
} else { |
| 275 |
Taxon h1 = createTreeColouring(tree, tree.getChild(node, 0), treeColouring); |
| 276 |
Taxon h2 = createTreeColouring(tree, tree.getChild(node, 1), treeColouring); |
| 277 |
if (h1 != h2) { |
| 278 |
throw new RuntimeException("Two children have different hosts at coalescent event"); |
| 279 |
} |
| 280 |
|
| 281 |
childHost = h1; |
| 282 |
} |
| 283 |
|
| 284 |
NodeRef parent = tree.getParent(node); |
| 285 |
if (parent != null) { |
| 286 |
double height0 = tree.getNodeHeight(node); |
| 287 |
double height1 = tree.getNodeHeight(parent); |
| 288 |
|
| 289 |
TransmissionEvent event = getTransmissionEventToHost(childHost); |
| 290 |
DefaultBranchColouring branchColouring; |
| 291 |
|
| 292 |
if (event != null && event.getTransmissionTime() < height1) { |
| 293 |
if (event.getTransmissionTime() < height0) { |
| 294 |
throw new RuntimeException("Transmission event is before the node"); |
| 295 |
} |
| 296 |
|
| 297 |
List<Taxon> hosts = new ArrayList<Taxon>(); |
| 298 |
List<Double> times = new ArrayList<Double>(); |
| 299 |
|
| 300 |
parentHost = childHost; |
| 301 |
|
| 302 |
while (event != null && event.getTransmissionTime() < height1) { |
| 303 |
hosts.add(parentHost); |
| 304 |
times.add(event.getTransmissionTime()); |
| 305 |
|
| 306 |
parentHost = event.donor; |
| 307 |
|
| 308 |
event = getTransmissionEventToHost(parentHost); |
| 309 |
} |
| 310 |
|
| 311 |
int host1 = getHostIndex(childHost); |
| 312 |
int host2 = getHostIndex(parentHost); |
| 313 |
branchColouring = new DefaultBranchColouring(host2, host1); |
| 314 |
for (int i = hosts.size() - 1; i >= 0; i--) { |
| 315 |
int host = getHostIndex(hosts.get(i)); |
| 316 |
double time = times.get(i); |
| 317 |
branchColouring.addEvent(host, time); |
| 318 |
} |
| 319 |
|
| 320 |
} else { |
| 321 |
int host = getHostIndex(childHost); |
| 322 |
branchColouring = new DefaultBranchColouring(host, host); |
| 323 |
parentHost = childHost; |
| 324 |
} |
| 325 |
|
| 326 |
treeColouring.setBranchColouring(node, branchColouring); |
| 327 |
} |
| 328 |
|
| 329 |
return parentHost; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
class TransmissionEvent { |
| 336 |
Taxon donor; |
| 337 |
Taxon recipient; |
| 338 |
Parameter timeParameter = null; |
| 339 |
|
| 340 |
public TransmissionEvent(Taxon donor, Taxon recipient, Parameter timeParameter) { |
| 341 |
this.donor = donor; |
| 342 |
this.recipient = recipient; |
| 343 |
this.timeParameter = timeParameter; |
| 344 |
} |
| 345 |
|
| 346 |
public double getTransmissionTime() { |
| 347 |
return timeParameter.getParameterValue(0); |
| 348 |
} |
| 349 |
|
| 350 |
public Taxon getDonor() { |
| 351 |
return donor; |
| 352 |
} |
| 353 |
|
| 354 |
public Taxon getRecipient() { |
| 355 |
return recipient; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
private List<TransmissionEvent> transmissionEvents = new ArrayList<TransmissionEvent>(); |
| 360 |
private Map<Taxon, TransmissionEvent> transmissionEventMap = new HashMap<Taxon, TransmissionEvent>(); |
| 361 |
private List<Taxon> hosts = new ArrayList<Taxon>(); |
| 362 |
} |