| 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.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.util.Taxon; |
| 31 |
import dr.evomodel.tree.TreeStatistic; |
| 32 |
import dr.inference.model.BooleanStatistic; |
| 33 |
import dr.inference.model.Statistic; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.util.HashSet; |
| 37 |
import java.util.Set; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class TransmissionStatistic extends TreeStatistic implements BooleanStatistic { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static final String TRANSMISSION_STATISTIC = "transmissionStatistic"; |
| 55 |
|
| 56 |
public TransmissionStatistic(String name, TransmissionHistoryModel transmissionHistoryModel, Tree virusTree) { |
| 57 |
|
| 58 |
super(name); |
| 59 |
|
| 60 |
this.transmissionHistoryModel = transmissionHistoryModel; |
| 61 |
this.virusTree = virusTree; |
| 62 |
|
| 63 |
setupHosts(); |
| 64 |
} |
| 65 |
|
| 66 |
public TransmissionStatistic(String name, Tree hostTree, Tree virusTree) { |
| 67 |
|
| 68 |
super(name); |
| 69 |
|
| 70 |
this.hostTree = hostTree; |
| 71 |
this.virusTree = virusTree; |
| 72 |
|
| 73 |
setupHosts(); |
| 74 |
} |
| 75 |
|
| 76 |
private void setupHosts() { |
| 77 |
|
| 78 |
if (transmissionHistoryModel != null) { |
| 79 |
hostCount = transmissionHistoryModel.getHostCount(); |
| 80 |
|
| 81 |
} else { |
| 82 |
hostCount = hostTree.getTaxonCount(); |
| 83 |
} |
| 84 |
|
| 85 |
donorHost = new int[hostCount]; |
| 86 |
donorHost[0] = -1; |
| 87 |
transmissionTime = new double[hostCount]; |
| 88 |
transmissionTime[0] = java.lang.Double.POSITIVE_INFINITY; |
| 89 |
|
| 90 |
if (transmissionHistoryModel != null) { |
| 91 |
for (int i = 0; i < transmissionHistoryModel.getTransmissionEventCount(); i++) |
| 92 |
{ |
| 93 |
TransmissionHistoryModel.TransmissionEvent event = transmissionHistoryModel.getTransmissionEvent(i); |
| 94 |
|
| 95 |
int host1 = transmissionHistoryModel.getHostIndex(event.getDonor()); |
| 96 |
int host2 = transmissionHistoryModel.getHostIndex(event.getRecipient()); |
| 97 |
|
| 98 |
donorHost[host2] = host1; |
| 99 |
transmissionTime[host2] = event.getTransmissionTime(); |
| 100 |
} |
| 101 |
} else { |
| 102 |
setupHostsTree(hostTree.getRoot()); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private int setupHostsTree(NodeRef node) { |
| 107 |
|
| 108 |
int host; |
| 109 |
|
| 110 |
if (hostTree.isExternal(node)) { |
| 111 |
host = node.getNumber(); |
| 112 |
} else { |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
int host1 = setupHostsTree(hostTree.getChild(node, 0)); |
| 118 |
int host2 = setupHostsTree(hostTree.getChild(node, 1)); |
| 119 |
|
| 120 |
donorHost[host2] = host1; |
| 121 |
transmissionTime[host2] = hostTree.getNodeHeight(node); |
| 122 |
|
| 123 |
host = host1; |
| 124 |
} |
| 125 |
|
| 126 |
return host; |
| 127 |
} |
| 128 |
|
| 129 |
public void setTree(Tree tree) { |
| 130 |
this.virusTree = tree; |
| 131 |
} |
| 132 |
|
| 133 |
public Tree getTree() { |
| 134 |
return virusTree; |
| 135 |
} |
| 136 |
|
| 137 |
public String getDimensionName(int dim) { |
| 138 |
String recipient = transmissionHistoryModel.getHost(dim).getId(); |
| 139 |
|
| 140 |
String donor = (donorHost[dim] == -1 ? "" : transmissionHistoryModel.getHost(donorHost[dim]).getId() + "->"); |
| 141 |
return "transmission(" + donor + recipient + ")"; |
| 142 |
} |
| 143 |
|
| 144 |
public int getDimension() { |
| 145 |
return hostCount; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public double getStatisticValue(int dim) { |
| 152 |
return getBoolean(dim) ? 1.0 : 0.0; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public boolean getBoolean(int dim) { |
| 159 |
Set<Integer> incompatibleSet = new HashSet<Integer>(); |
| 160 |
setupHosts(); |
| 161 |
isCompatible(virusTree.getRoot(), incompatibleSet); |
| 162 |
|
| 163 |
return !incompatibleSet.contains(dim); |
| 164 |
} |
| 165 |
|
| 166 |
private int isCompatible(NodeRef node, Set<Integer> incompatibleSet) { |
| 167 |
|
| 168 |
double height = virusTree.getNodeHeight(node); |
| 169 |
int host; |
| 170 |
|
| 171 |
if (virusTree.isExternal(node)) { |
| 172 |
Taxon hostTaxon = (Taxon) virusTree.getTaxonAttribute(node.getNumber(), "host"); |
| 173 |
if (transmissionHistoryModel != null) { |
| 174 |
host = transmissionHistoryModel.getHostIndex(hostTaxon); |
| 175 |
} else { |
| 176 |
host = hostTree.getTaxonIndex(hostTaxon); |
| 177 |
} |
| 178 |
|
| 179 |
if (host != -1 && height > transmissionTime[host]) { |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
throw new RuntimeException("Sequence " + virusTree.getNodeTaxon(node) + ", was sampled ("+height+") before host, " + hostTaxon + ", was infected ("+transmissionTime[host]+")"); |
| 184 |
} |
| 185 |
|
| 186 |
} else { |
| 187 |
|
| 188 |
|
| 189 |
int host1 = isCompatible(virusTree.getChild(node, 0), incompatibleSet); |
| 190 |
|
| 191 |
int host2 = isCompatible(virusTree.getChild(node, 1), incompatibleSet); |
| 192 |
|
| 193 |
if (host1 == host2) { |
| 194 |
|
| 195 |
host = host1; |
| 196 |
while (height > transmissionTime[host]) { |
| 197 |
host = donorHost[host]; |
| 198 |
} |
| 199 |
|
| 200 |
} else { |
| 201 |
while (height > transmissionTime[host1]) { |
| 202 |
host1 = donorHost[host1]; |
| 203 |
} |
| 204 |
|
| 205 |
while (height > transmissionTime[host2]) { |
| 206 |
host2 = donorHost[host2]; |
| 207 |
} |
| 208 |
|
| 209 |
if (host1 != host2) { |
| 210 |
if (transmissionTime[host1] < transmissionTime[host2]) { |
| 211 |
incompatibleSet.add(host1); |
| 212 |
host = host2; |
| 213 |
} else { |
| 214 |
incompatibleSet.add(host2); |
| 215 |
host = host1; |
| 216 |
} |
| 217 |
} else { |
| 218 |
host = host1; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return host; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 231 |
|
| 232 |
public String getParserName() { |
| 233 |
return TRANSMISSION_STATISTIC; |
| 234 |
} |
| 235 |
|
| 236 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 237 |
|
| 238 |
String name = xo.getStringAttribute("name"); |
| 239 |
Tree virusTree = (Tree) xo.getElementFirstChild("parasiteTree"); |
| 240 |
|
| 241 |
if (xo.getChild(TransmissionHistoryModel.class) != null) { |
| 242 |
TransmissionHistoryModel history = (TransmissionHistoryModel) xo.getChild(TransmissionHistoryModel.class); |
| 243 |
return new TransmissionStatistic(name, history, virusTree); |
| 244 |
} else { |
| 245 |
Tree hostTree = (Tree) xo.getElementFirstChild("hostTree"); |
| 246 |
return new TransmissionStatistic(name, hostTree, virusTree); |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
public String getParserDescription() { |
| 252 |
return "A statistic that returns true if the given parasite tree is compatible with the host tree."; |
| 253 |
} |
| 254 |
|
| 255 |
public Class getReturnType() { |
| 256 |
return Statistic.class; |
| 257 |
} |
| 258 |
|
| 259 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 260 |
return rules; |
| 261 |
} |
| 262 |
|
| 263 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 264 |
new StringAttributeRule("name", "A name for this statistic for the purpose of logging"), |
| 265 |
new XORRule( |
| 266 |
new ElementRule("hostTree", |
| 267 |
new XMLSyntaxRule[]{new ElementRule(Tree.class)}), |
| 268 |
new ElementRule(TransmissionHistoryModel.class, |
| 269 |
"This describes the transmission history of the patients.") |
| 270 |
), |
| 271 |
new ElementRule("parasiteTree", |
| 272 |
new XMLSyntaxRule[]{new ElementRule(Tree.class)}) |
| 273 |
}; |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
private Tree hostTree = null; |
| 280 |
|
| 281 |
private TransmissionHistoryModel transmissionHistoryModel = null; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private Tree virusTree = null; |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
private int hostCount; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private int[] donorHost; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
private double[] transmissionTime; |
| 302 |
} |