| 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.seqgen; |
| 27 |
|
| 28 |
import dr.evolution.alignment.Alignment; |
| 29 |
import dr.evolution.alignment.SimpleAlignment; |
| 30 |
import dr.evolution.coalescent.*; |
| 31 |
import dr.evolution.datatype.Nucleotides; |
| 32 |
import dr.evolution.sequence.Sequence; |
| 33 |
import dr.evolution.tree.*; |
| 34 |
import dr.evolution.util.*; |
| 35 |
import dr.evomodel.branchratemodel.*; |
| 36 |
import dr.oldevomodel.sitemodel.GammaSiteModel; |
| 37 |
import dr.oldevomodel.sitemodel.SiteModel; |
| 38 |
import dr.oldevomodel.substmodel.FrequencyModel; |
| 39 |
import dr.oldevomodel.substmodel.HKY; |
| 40 |
import dr.inference.model.Parameter; |
| 41 |
import dr.math.MathUtils; |
| 42 |
import dr.xml.*; |
| 43 |
|
| 44 |
import java.util.*; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class RecomboGen { |
| 51 |
|
| 52 |
public RecomboGen(final double recombinationRate, final int length, |
| 53 |
final double ancestralPopulationSize, |
| 54 |
final SiteModel siteModel, |
| 55 |
final BranchRateModel branchRateModel, |
| 56 |
final Taxa taxa) { |
| 57 |
this.recombinationRate = recombinationRate; |
| 58 |
this.length = length; |
| 59 |
this.ancestralPopulationSize = ancestralPopulationSize; |
| 60 |
this.siteModel = siteModel; |
| 61 |
this.branchRateModel = branchRateModel; |
| 62 |
this.taxa = taxa; |
| 63 |
calculateHeights(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private void calculateHeights() { |
| 81 |
|
| 82 |
dr.evolution.util.Date mostRecent = null; |
| 83 |
boolean usingDates = false; |
| 84 |
|
| 85 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 86 |
if (TaxonList.Utils.hasAttribute(taxa, i, dr.evolution.util.Date.DATE)) { |
| 87 |
usingDates = true; |
| 88 |
dr.evolution.util.Date date = (dr.evolution.util.Date)taxa.getTaxonAttribute(i, dr.evolution.util.Date.DATE); |
| 89 |
if ((date != null) && (mostRecent == null || date.after(mostRecent))) { |
| 90 |
mostRecent = date; |
| 91 |
} |
| 92 |
} else { |
| 93 |
|
| 94 |
taxa.setTaxonAttribute(i, "height", 0.0); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if (usingDates && mostRecent != null ) { |
| 99 |
TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue()); |
| 100 |
|
| 101 |
for (int i =0; i < taxa.getTaxonCount(); i++) { |
| 102 |
dr.evolution.util.Date date = (dr.evolution.util.Date)taxa.getTaxonAttribute(i, dr.evolution.util.Date.DATE); |
| 103 |
|
| 104 |
if (date == null) { |
| 105 |
throw new IllegalArgumentException("Taxon, " + taxa.getTaxonId(i) + ", is missing its date"); |
| 106 |
} |
| 107 |
|
| 108 |
taxa.setTaxonAttribute(i, "height", timeScale.convertTime(date.getTimeValue(), date)); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public Alignment generate() { |
| 114 |
List<Node> tips = new ArrayList<Node>(); |
| 115 |
|
| 116 |
|
| 117 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 118 |
tips.add(new Node(taxa.getTaxon(i))); |
| 119 |
} |
| 120 |
Collections.sort(tips); |
| 121 |
|
| 122 |
List<Node> unusedTips = new ArrayList<Node>(tips); |
| 123 |
|
| 124 |
double time = 0; |
| 125 |
List<Node> nodes = new ArrayList<Node>(); |
| 126 |
|
| 127 |
|
| 128 |
List<Node> nodesToAdd = new ArrayList<Node>(); |
| 129 |
for (Node tip : unusedTips) { |
| 130 |
if (tip.getTime() == 0.0) { |
| 131 |
nodesToAdd.add(tip); |
| 132 |
System.out.println("Time: " + time + " adding " + tip.getTaxon()); |
| 133 |
} |
| 134 |
} |
| 135 |
nodes.addAll(nodesToAdd); |
| 136 |
unusedTips.removeAll(nodesToAdd); |
| 137 |
|
| 138 |
do { |
| 139 |
|
| 140 |
boolean tipsAdded; |
| 141 |
|
| 142 |
do { |
| 143 |
tipsAdded = false; |
| 144 |
|
| 145 |
final int lineageCount = nodes.size(); |
| 146 |
|
| 147 |
|
| 148 |
final double U = MathUtils.nextDouble(); |
| 149 |
final double interval = -Math.log(U)/ (lineageCount * recombinationRate); |
| 150 |
final double nextTime = time + interval; |
| 151 |
|
| 152 |
|
| 153 |
nodesToAdd.clear(); |
| 154 |
for (Node tip : unusedTips) { |
| 155 |
if (tip.getTime() <= nextTime) { |
| 156 |
nodesToAdd.add(tip); |
| 157 |
tipsAdded = true; |
| 158 |
|
| 159 |
System.out.println("Time: " + tip.getTime() + " adding " + tip.getTaxon()); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
time = tip.getTime(); |
| 164 |
} |
| 165 |
} |
| 166 |
nodes.addAll(nodesToAdd); |
| 167 |
unusedTips.removeAll(nodesToAdd); |
| 168 |
|
| 169 |
if (!tipsAdded) { |
| 170 |
time = nextTime; |
| 171 |
} |
| 172 |
|
| 173 |
} while(tipsAdded); |
| 174 |
|
| 175 |
int r = MathUtils.nextInt(nodes.size()); |
| 176 |
Node node = nodes.get(r); |
| 177 |
|
| 178 |
|
| 179 |
Node parent1 = new Node(node, time); |
| 180 |
Node parent2 = new Node(node, time); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
int breakPoint = MathUtils.nextInt(length - 2) + 1; |
| 185 |
|
| 186 |
|
| 187 |
node.setParent1(parent1); |
| 188 |
node.setParent2(parent2); |
| 189 |
node.setBreakPoint(breakPoint); |
| 190 |
|
| 191 |
System.out.println("Time: " + time + " recombining " + (node.getTaxon() != null ? node.getTaxon() : r) + " at breakpoint " + breakPoint); |
| 192 |
|
| 193 |
nodes.add(parent1); |
| 194 |
nodes.add(parent2); |
| 195 |
nodes.remove(node); |
| 196 |
|
| 197 |
} while (unusedTips.size() > 0); |
| 198 |
|
| 199 |
|
| 200 |
Taxa treeTaxa = new Taxa(); |
| 201 |
int i = 0; |
| 202 |
|
| 203 |
Map<Node, Taxon> nodeMap = new HashMap<Node, Taxon>(); |
| 204 |
|
| 205 |
for (Node node : nodes) { |
| 206 |
Taxon taxon = new Taxon("Taxon" + i); |
| 207 |
treeTaxa.addTaxon(taxon); |
| 208 |
nodeMap.put(node, taxon); |
| 209 |
i++; |
| 210 |
} |
| 211 |
|
| 212 |
CoalescentSimulator coalSim = new CoalescentSimulator(); |
| 213 |
ConstantPopulation demo = new ConstantPopulation(Units.Type.YEARS); |
| 214 |
demo.setN0(ancestralPopulationSize); |
| 215 |
|
| 216 |
Tree tree = coalSim.simulateTree(treeTaxa, demo); |
| 217 |
|
| 218 |
System.out.println("Tree MRCA " + tree.getNodeHeight(tree.getRoot()) + time); |
| 219 |
|
| 220 |
SequenceSimulator seqSim = new SequenceSimulator(tree, siteModel, branchRateModel, length); |
| 221 |
|
| 222 |
Alignment ancestralAlignment = seqSim.simulate(); |
| 223 |
|
| 224 |
SimpleAlignment alignment = new SimpleAlignment(); |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
for (Node tip : tips) { |
| 229 |
String seqString = generateRecombinant(tip, nodeMap, ancestralAlignment); |
| 230 |
Sequence sequence = new Sequence(tip.getTaxon(), seqString); |
| 231 |
|
| 232 |
|
| 233 |
alignment.addSequence(sequence); |
| 234 |
} |
| 235 |
|
| 236 |
return alignment; |
| 237 |
} |
| 238 |
|
| 239 |
private String generateRecombinant(final Node node, final Map<Node, Taxon> nodeMap, final Alignment alignment) { |
| 240 |
String seq = node.getSequence(); |
| 241 |
|
| 242 |
if (seq == null) { |
| 243 |
|
| 244 |
if (node.getParent1() == null && node.getParent2() == null) { |
| 245 |
|
| 246 |
Taxon taxon = nodeMap.get(node); |
| 247 |
int index = alignment.getTaxonIndex(taxon); |
| 248 |
Sequence sequence = alignment.getSequence(index); |
| 249 |
seq = sequence.getSequenceString(); |
| 250 |
} else { |
| 251 |
String part1 = generateRecombinant(node.getParent1(), nodeMap, alignment); |
| 252 |
String part2 = generateRecombinant(node.getParent2(), nodeMap, alignment); |
| 253 |
|
| 254 |
int breakPoint = node.getBreakPoint(); |
| 255 |
|
| 256 |
seq = part1.substring(0, breakPoint) + part2.substring(breakPoint); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
node.setSequence(seq); |
| 261 |
} |
| 262 |
|
| 263 |
return seq; |
| 264 |
} |
| 265 |
|
| 266 |
class Node implements Comparable<Node>{ |
| 267 |
Node(final Taxon taxon) { |
| 268 |
this.parent1 = null; |
| 269 |
this.parent2 = null; |
| 270 |
this.child = null; |
| 271 |
this.breakPoint = -1; |
| 272 |
this.taxon = taxon; |
| 273 |
this.time = (Double)taxon.getAttribute("height"); |
| 274 |
} |
| 275 |
|
| 276 |
Node(final Node child, final double time) { |
| 277 |
this.child = child; |
| 278 |
this.parent1 = null; |
| 279 |
this.parent2 = null; |
| 280 |
this.time = time; |
| 281 |
this.breakPoint = -1; |
| 282 |
this.taxon = null; |
| 283 |
} |
| 284 |
|
| 285 |
public Node getParent1() { |
| 286 |
return parent1; |
| 287 |
} |
| 288 |
|
| 289 |
public Node getParent2() { |
| 290 |
return parent2; |
| 291 |
} |
| 292 |
|
| 293 |
public void setParent1(final Node parent1) { |
| 294 |
this.parent1 = parent1; |
| 295 |
} |
| 296 |
|
| 297 |
public void setParent2(final Node parent2) { |
| 298 |
this.parent2 = parent2; |
| 299 |
} |
| 300 |
|
| 301 |
public Node getChild() { |
| 302 |
return child; |
| 303 |
} |
| 304 |
|
| 305 |
public double getTime() { |
| 306 |
return time; |
| 307 |
} |
| 308 |
|
| 309 |
public int getBreakPoint() { |
| 310 |
return breakPoint; |
| 311 |
} |
| 312 |
|
| 313 |
public void setBreakPoint(final int breakPoint) { |
| 314 |
this.breakPoint = breakPoint; |
| 315 |
} |
| 316 |
|
| 317 |
public Taxon getTaxon() { |
| 318 |
return taxon; |
| 319 |
} |
| 320 |
|
| 321 |
public String getSequence() { |
| 322 |
return sequence; |
| 323 |
} |
| 324 |
|
| 325 |
public void setSequence(final String sequence) { |
| 326 |
this.sequence = sequence; |
| 327 |
} |
| 328 |
|
| 329 |
public int compareTo(final Node node) { |
| 330 |
return Double.compare(time, node.time); |
| 331 |
} |
| 332 |
|
| 333 |
private Node parent1; |
| 334 |
private Node parent2; |
| 335 |
private final Node child; |
| 336 |
private final double time; |
| 337 |
private int breakPoint; |
| 338 |
private final Taxon taxon; |
| 339 |
private String sequence = null; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
public static final String RECOMBINATION_SIMULATOR = "recombinationSimulator"; |
| 344 |
public static final String SITE_MODEL = SiteModel.SITE_MODEL; |
| 345 |
public static final String TAXA = "taxa"; |
| 346 |
|
| 347 |
public static final String RECOMBINATION_RATE = "recombinationRate"; |
| 348 |
public static final String SUBSTITUTION_RATE = "substitutionRate"; |
| 349 |
public static final String ANCESTRAL_POPULATION_SIZE = "ancestralPopulationSize"; |
| 350 |
public static final String SEQUENCE_LENGTH = "sequenceLength"; |
| 351 |
|
| 352 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 353 |
|
| 354 |
public String getParserName() { |
| 355 |
return RECOMBINATION_SIMULATOR; |
| 356 |
} |
| 357 |
|
| 358 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 359 |
|
| 360 |
double recombinationRate = xo.getDoubleAttribute(RECOMBINATION_RATE); |
| 361 |
double ancestralPopulationSize = xo.getDoubleAttribute(ANCESTRAL_POPULATION_SIZE); |
| 362 |
int sequenceLength = xo.getIntegerAttribute(SEQUENCE_LENGTH); |
| 363 |
|
| 364 |
Taxa taxa = (Taxa) xo.getChild(Taxa.class); |
| 365 |
SiteModel siteModel = (SiteModel) xo.getChild(SiteModel.class); |
| 366 |
BranchRateModel rateModel = (BranchRateModel)xo.getChild(BranchRateModel.class); |
| 367 |
|
| 368 |
if (rateModel == null) { |
| 369 |
rateModel = new DefaultBranchRateModel(); |
| 370 |
} |
| 371 |
|
| 372 |
RecomboGen s = new RecomboGen( |
| 373 |
recombinationRate, sequenceLength, |
| 374 |
ancestralPopulationSize, siteModel, rateModel, taxa); |
| 375 |
return s.generate(); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public String getParserDescription() { |
| 383 |
return "A SequenceSimulator that generates random sequences for a given tree, siteratemodel and branch rate model"; |
| 384 |
} |
| 385 |
|
| 386 |
public Class getReturnType() { |
| 387 |
return Alignment.class; |
| 388 |
} |
| 389 |
|
| 390 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 391 |
return rules; |
| 392 |
} |
| 393 |
|
| 394 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 395 |
new ElementRule(Taxa.class), |
| 396 |
new ElementRule(SiteModel.class), |
| 397 |
new ElementRule(BranchRateModel.class, true), |
| 398 |
AttributeRule.newDoubleRule(RECOMBINATION_RATE), |
| 399 |
AttributeRule.newDoubleRule(ANCESTRAL_POPULATION_SIZE), |
| 400 |
AttributeRule.newIntegerRule(SEQUENCE_LENGTH) |
| 401 |
}; |
| 402 |
}; |
| 403 |
|
| 404 |
public static void main(String[] argv) { |
| 405 |
|
| 406 |
|
| 407 |
Parameter kappa = new Parameter.Default(1, 2); |
| 408 |
Parameter freqs = new Parameter.Default(new double[]{0.25, 0.25, 0.25, 0.25}); |
| 409 |
FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE, freqs); |
| 410 |
HKY hky = new HKY(kappa, freqModel); |
| 411 |
|
| 412 |
|
| 413 |
SiteModel siteModel = new GammaSiteModel(hky); |
| 414 |
|
| 415 |
Parameter rate = new Parameter.Default(1, 1.0E-4); |
| 416 |
BranchRateModel branchRateModel = new StrictClockBranchRates(rate); |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
} |
| 424 |
|
| 425 |
private final double recombinationRate; |
| 426 |
private final int length; |
| 427 |
private final double ancestralPopulationSize; |
| 428 |
private final SiteModel siteModel; |
| 429 |
private final BranchRateModel branchRateModel; |
| 430 |
private final Taxa taxa; |
| 431 |
|
| 432 |
|
| 433 |
} |