| 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.beauti.components.tipdatesampling; |
| 27 |
|
| 28 |
import dr.app.beauti.options.AbstractPartitionData; |
| 29 |
import dr.app.beauti.options.PartitionTreeModel; |
| 30 |
import dr.app.beauti.util.XMLWriter; |
| 31 |
import dr.app.beauti.types.TipDateSamplingType; |
| 32 |
import dr.app.beauti.generator.BaseComponentGenerator; |
| 33 |
import dr.app.beauti.options.BeautiOptions; |
| 34 |
import dr.evolution.util.Date; |
| 35 |
import dr.evolution.util.Taxa; |
| 36 |
import dr.evolution.util.Taxon; |
| 37 |
import dr.evolution.util.TaxonList; |
| 38 |
import dr.evoxml.TaxonParser; |
| 39 |
import dr.inference.model.ParameterParser; |
| 40 |
import dr.util.Attribute; |
| 41 |
import dr.xml.XMLParser; |
| 42 |
|
| 43 |
import java.util.HashSet; |
| 44 |
import java.util.Set; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class TipDateSamplingComponentGenerator extends BaseComponentGenerator { |
| 51 |
|
| 52 |
public TipDateSamplingComponentGenerator(final BeautiOptions options) { |
| 53 |
super(options); |
| 54 |
} |
| 55 |
|
| 56 |
public boolean usesInsertionPoint(final InsertionPoint point) { |
| 57 |
TipDateSamplingComponentOptions comp = (TipDateSamplingComponentOptions)options.getComponentOptions(TipDateSamplingComponentOptions.class); |
| 58 |
|
| 59 |
if (comp.tipDateSamplingType == TipDateSamplingType.NO_SAMPLING) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
switch (point) { |
| 64 |
case IN_TREE_MODEL: |
| 65 |
case IN_FILE_LOG_PARAMETERS: |
| 66 |
return true; |
| 67 |
case AFTER_TREE_MODEL: |
| 68 |
return options.getPartitionTreeModels().size() > 1 || comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT; |
| 69 |
case IN_MCMC_PRIOR: |
| 70 |
return comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT; |
| 71 |
default: |
| 72 |
return false; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
protected void generate(final InsertionPoint point, final Object item, final String prefix, final XMLWriter writer) { |
| 77 |
TipDateSamplingComponentOptions comp = (TipDateSamplingComponentOptions)options.getComponentOptions(TipDateSamplingComponentOptions.class); |
| 78 |
|
| 79 |
TaxonList taxa = comp.getTaxonSet(); |
| 80 |
|
| 81 |
switch (point) { |
| 82 |
case IN_TREE_MODEL: { |
| 83 |
writeLeafHeightParameters(writer, (PartitionTreeModel)item, taxa); |
| 84 |
} break; |
| 85 |
case AFTER_TREE_MODEL: |
| 86 |
if (options.getPartitionTreeModels().size() > 1) { |
| 87 |
|
| 88 |
|
| 89 |
writeJointParameters(writer, taxa); |
| 90 |
} |
| 91 |
|
| 92 |
if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) { |
| 93 |
writer.writeOpenTag("compoundParameter", |
| 94 |
new Attribute[]{ |
| 95 |
new Attribute.Default<String>(XMLParser.ID, "treeModel.tipDates"), |
| 96 |
} |
| 97 |
); |
| 98 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 99 |
Taxon taxon = taxa.getTaxon(i); |
| 100 |
writer.writeIDref(ParameterParser.PARAMETER, "age(" + taxon.getId() + ")"); |
| 101 |
} |
| 102 |
|
| 103 |
writer.writeCloseTag("compoundParameter"); |
| 104 |
} |
| 105 |
break; |
| 106 |
case IN_MCMC_PRIOR: |
| 107 |
if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_INDIVIDUALLY || |
| 108 |
comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) { |
| 109 |
|
| 110 |
} else if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) { |
| 111 |
|
| 112 |
} |
| 113 |
break; |
| 114 |
case IN_FILE_LOG_PARAMETERS: |
| 115 |
if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_INDIVIDUALLY || |
| 116 |
comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) { |
| 117 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 118 |
Taxon taxon = taxa.getTaxon(i); |
| 119 |
writer.writeIDref(ParameterParser.PARAMETER, "age(" + taxon.getId() + ")"); |
| 120 |
} |
| 121 |
} else if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) { |
| 122 |
writer.writeIDref(ParameterParser.PARAMETER, "treeModel.tipDates"); |
| 123 |
} |
| 124 |
break; |
| 125 |
default: |
| 126 |
throw new IllegalArgumentException("This insertion point is not implemented for " + this.getClass().getName()); |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
private void writeJointParameters(XMLWriter writer, TaxonList taxa) { |
| 132 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 133 |
Taxon taxon = taxa.getTaxon(i); |
| 134 |
|
| 135 |
Set<PartitionTreeModel> treeModels = new HashSet<PartitionTreeModel>(); |
| 136 |
for (PartitionTreeModel treeModel : options.getPartitionTreeModels()) { |
| 137 |
for (AbstractPartitionData data : options.getDataPartitions(treeModel)) { |
| 138 |
if (data.getTaxonList().asList().contains(taxon)) { |
| 139 |
treeModels.add(treeModel); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
if (treeModels.size() > 0) { |
| 146 |
writer.writeOpenTag("jointParameter", |
| 147 |
new Attribute[]{ |
| 148 |
new Attribute.Default<String>(XMLParser.ID, "age(" + taxon.getId() + ")") |
| 149 |
} |
| 150 |
); |
| 151 |
|
| 152 |
for (PartitionTreeModel treeModel : treeModels) { |
| 153 |
writer.writeTag(ParameterParser.PARAMETER, new Attribute.Default<String>(XMLParser.IDREF, treeModel.getPrefix() + "age(" + taxon.getId() + ")"), true); |
| 154 |
} |
| 155 |
|
| 156 |
writer.writeCloseTag("jointParameter"); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
private void writeLeafHeightParameters(XMLWriter writer, PartitionTreeModel item, TaxonList taxa) { |
| 163 |
|
| 164 |
PartitionTreeModel treeModel = (PartitionTreeModel)item; |
| 165 |
|
| 166 |
Set<Taxon> taxonSet = new HashSet<Taxon>(); |
| 167 |
for (AbstractPartitionData data : options.getDataPartitions(treeModel)) { |
| 168 |
if (data.getTaxonList() != null) { |
| 169 |
for (Taxon taxon : data.getTaxonList()) { |
| 170 |
taxonSet.add(taxon); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 176 |
Taxon taxon = taxa.getTaxon(i); |
| 177 |
|
| 178 |
if (taxonSet.contains(taxon)) { |
| 179 |
|
| 180 |
|
| 181 |
writer.writeOpenTag("leafHeight", |
| 182 |
new Attribute[]{ |
| 183 |
new Attribute.Default<String>(TaxonParser.TAXON, taxon.getId()), |
| 184 |
} |
| 185 |
); |
| 186 |
writer.writeTag(ParameterParser.PARAMETER, new Attribute.Default<String>(XMLParser.ID, treeModel.getPrefix() + "age(" + taxon.getId() + ")"), true); |
| 187 |
writer.writeCloseTag("leafHeight"); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
protected String getCommentLabel() { |
| 193 |
return "Tip date sampling"; |
| 194 |
} |
| 195 |
|
| 196 |
} |