| 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.branchmodel.lineagespecific; |
| 27 |
|
| 28 |
import java.util.*; |
| 29 |
|
| 30 |
import dr.evomodel.branchmodel.BranchModel; |
| 31 |
import dr.evomodel.branchmodel.HomogeneousBranchModel; |
| 32 |
import dr.evomodel.siteratemodel.GammaSiteRateModel; |
| 33 |
import dr.evomodel.substmodel.FrequencyModel; |
| 34 |
import dr.evomodel.substmodel.codon.MG94CodonModel; |
| 35 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 36 |
import dr.evomodel.treelikelihood.BeagleTreeLikelihood; |
| 37 |
import dr.evomodel.treelikelihood.PartialsRescalingScheme; |
| 38 |
import dr.app.beagle.tools.BeagleSequenceSimulator; |
| 39 |
import dr.app.beagle.tools.Partition; |
| 40 |
import dr.evolution.alignment.Alignment; |
| 41 |
import dr.evolution.alignment.ConvertAlignment; |
| 42 |
import dr.evolution.datatype.Codons; |
| 43 |
import dr.evolution.datatype.GeneticCode; |
| 44 |
import dr.evolution.datatype.Nucleotides; |
| 45 |
import dr.evolution.io.NewickImporter; |
| 46 |
import dr.evolution.tree.NodeRef; |
| 47 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 48 |
import dr.evomodel.branchratemodel.CountableBranchCategoryProvider; |
| 49 |
import dr.evomodel.branchratemodel.DefaultBranchRateModel; |
| 50 |
import dr.evomodel.tree.TreeModel; |
| 51 |
import dr.inference.model.AbstractModel; |
| 52 |
import dr.inference.model.Model; |
| 53 |
import dr.inference.model.Parameter; |
| 54 |
import dr.inference.model.Variable; |
| 55 |
import dr.inference.model.Variable.ChangeType; |
| 56 |
import dr.math.MathUtils; |
| 57 |
import dr.util.Author; |
| 58 |
import dr.util.Citable; |
| 59 |
import dr.util.Citation; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
@SuppressWarnings("serial") |
| 68 |
public class LineageSpecificBranchModel extends AbstractModel implements BranchModel, Citable { |
| 69 |
|
| 70 |
public static final String LINEAGE_SPECIFIC_BRANCH_MODEL = "lineageSpecificBranchModel"; |
| 71 |
|
| 72 |
private static final boolean DEBUG = false; |
| 73 |
|
| 74 |
private boolean setupMapping = true; |
| 75 |
|
| 76 |
private Map<NodeRef, Mapping> nodeMap; |
| 77 |
private List<SubstitutionModel> substitutionModels; |
| 78 |
private TreeModel treeModel; |
| 79 |
private FrequencyModel rootFrequencyModel; |
| 80 |
|
| 81 |
|
| 82 |
private CountableBranchCategoryProvider categoriesProvider; |
| 83 |
private Parameter categoriesParameter; |
| 84 |
|
| 85 |
public LineageSpecificBranchModel(TreeModel treeModel, |
| 86 |
FrequencyModel rootFrequencyModel, |
| 87 |
final List<SubstitutionModel> substitutionModels, |
| 88 |
|
| 89 |
Parameter categoriesParameter |
| 90 |
) { |
| 91 |
|
| 92 |
super(""); |
| 93 |
|
| 94 |
this.treeModel = treeModel; |
| 95 |
this.substitutionModels = substitutionModels; |
| 96 |
|
| 97 |
this.categoriesParameter = categoriesParameter; |
| 98 |
this.rootFrequencyModel = rootFrequencyModel; |
| 99 |
|
| 100 |
this.categoriesProvider = new CountableBranchCategoryProvider.IndependentBranchCategoryModel(treeModel, categoriesParameter); |
| 101 |
|
| 102 |
this.nodeMap = new HashMap<NodeRef, Mapping>(); |
| 103 |
|
| 104 |
for (SubstitutionModel model : this.substitutionModels) { |
| 105 |
addModel(model); |
| 106 |
} |
| 107 |
|
| 108 |
addModel(this.treeModel); |
| 109 |
addModel(this.rootFrequencyModel); |
| 110 |
addModel((Model) this.categoriesProvider); |
| 111 |
addVariable(this.categoriesParameter); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public Mapping getBranchModelMapping(NodeRef branch) { |
| 117 |
|
| 118 |
if (setupMapping) { |
| 119 |
setupNodeMap(branch); |
| 120 |
} |
| 121 |
|
| 122 |
return nodeMap.get(branch); |
| 123 |
} |
| 124 |
|
| 125 |
private void setupNodeMap(NodeRef branch) { |
| 126 |
|
| 127 |
if (branch != treeModel.getRoot()) { |
| 128 |
|
| 129 |
int branchCategory = categoriesProvider.getBranchCategory( |
| 130 |
treeModel, branch); |
| 131 |
final int uCategory = (int) categoriesParameter.getParameterValue(branchCategory); |
| 132 |
|
| 133 |
if (DEBUG) { |
| 134 |
System.out.println("branch length: " + treeModel.getBranchLength(branch) + ", " + "category:" + uCategory); |
| 135 |
} |
| 136 |
|
| 137 |
nodeMap.put(branch, new Mapping() { |
| 138 |
|
| 139 |
@Override |
| 140 |
public int[] getOrder() { |
| 141 |
return new int[] { uCategory }; |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public double[] getWeights() { |
| 146 |
return new double[] { 1.0 }; |
| 147 |
} |
| 148 |
|
| 149 |
}); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public List<SubstitutionModel> getSubstitutionModels() { |
| 157 |
return substitutionModels; |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public SubstitutionModel getRootSubstitutionModel() { |
| 162 |
throw new RuntimeException("This method should never be called!"); |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public FrequencyModel getRootFrequencyModel() { |
| 167 |
return rootFrequencyModel; |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public boolean requiresMatrixConvolution() { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 177 |
|
| 178 |
fireModelChanged(); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
protected void handleVariableChangedEvent(Variable variable, int index, |
| 184 |
ChangeType type) { |
| 185 |
fireModelChanged(); |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
protected void storeState() { |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
protected void restoreState() { |
| 195 |
setupMapping = true; |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
protected void acceptState() { |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
public static void main(String[] args) { |
| 204 |
|
| 205 |
try { |
| 206 |
|
| 207 |
|
| 208 |
MathUtils.setSeed(666); |
| 209 |
|
| 210 |
|
| 211 |
NewickImporter importer = new NewickImporter( |
| 212 |
"(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);"); |
| 213 |
TreeModel tree = new TreeModel(importer.importTree(null)); |
| 214 |
|
| 215 |
|
| 216 |
GammaSiteRateModel siteRateModel = new GammaSiteRateModel( |
| 217 |
"siteModel"); |
| 218 |
|
| 219 |
|
| 220 |
BranchRateModel branchRateModel = new DefaultBranchRateModel(); |
| 221 |
|
| 222 |
int sequenceLength = 10; |
| 223 |
ArrayList<Partition> partitionsList = new ArrayList<Partition>(); |
| 224 |
|
| 225 |
|
| 226 |
Parameter freqs = new Parameter.Default(new double[]{ |
| 227 |
0.0163936, |
| 228 |
0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, |
| 229 |
0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, |
| 230 |
0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, |
| 231 |
0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, |
| 232 |
0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344 |
| 233 |
}); |
| 234 |
FrequencyModel freqModel = new FrequencyModel(Codons.UNIVERSAL, |
| 235 |
freqs); |
| 236 |
|
| 237 |
|
| 238 |
Parameter alpha = new Parameter.Default(1, 10); |
| 239 |
Parameter beta = new Parameter.Default(1, 5); |
| 240 |
MG94CodonModel mg94 = new MG94CodonModel(Codons.UNIVERSAL, alpha, beta, freqModel); |
| 241 |
|
| 242 |
HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(mg94); |
| 243 |
|
| 244 |
|
| 245 |
Partition partition1 = new Partition(tree, |
| 246 |
substitutionModel, |
| 247 |
siteRateModel, |
| 248 |
branchRateModel, |
| 249 |
freqModel, |
| 250 |
0, |
| 251 |
sequenceLength - 1, |
| 252 |
1 |
| 253 |
); |
| 254 |
|
| 255 |
partitionsList.add(partition1); |
| 256 |
|
| 257 |
|
| 258 |
BeagleSequenceSimulator simulator = new BeagleSequenceSimulator( |
| 259 |
partitionsList); |
| 260 |
|
| 261 |
Alignment alignment = simulator.simulate(false, false); |
| 262 |
|
| 263 |
ConvertAlignment convert = new ConvertAlignment(Nucleotides.INSTANCE, |
| 264 |
GeneticCode.UNIVERSAL, alignment); |
| 265 |
|
| 266 |
|
| 267 |
List<SubstitutionModel> substModels = new ArrayList<SubstitutionModel>(); |
| 268 |
for (int i = 0; i < 2; i++) { |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
substModels.add(mg94); |
| 274 |
} |
| 275 |
|
| 276 |
Parameter uCategories = new Parameter.Default(2, 0); |
| 277 |
|
| 278 |
|
| 279 |
LineageSpecificBranchModel branchSpecific = new LineageSpecificBranchModel(tree, freqModel, substModels, |
| 280 |
uCategories); |
| 281 |
|
| 282 |
BeagleTreeLikelihood like = new BeagleTreeLikelihood(convert, |
| 283 |
tree, |
| 284 |
branchSpecific, |
| 285 |
siteRateModel, |
| 286 |
branchRateModel, |
| 287 |
null, |
| 288 |
false, |
| 289 |
PartialsRescalingScheme.DEFAULT, true); |
| 290 |
|
| 291 |
BeagleTreeLikelihood gold = new BeagleTreeLikelihood(convert, |
| 292 |
tree, |
| 293 |
substitutionModel, |
| 294 |
siteRateModel, |
| 295 |
branchRateModel, |
| 296 |
null, |
| 297 |
false, |
| 298 |
PartialsRescalingScheme.DEFAULT, true); |
| 299 |
|
| 300 |
System.out.println("likelihood (gold) = " + gold.getLogLikelihood()); |
| 301 |
System.out.println("likelihood = " + like.getLogLikelihood()); |
| 302 |
|
| 303 |
} catch (Exception e) { |
| 304 |
e.printStackTrace(); |
| 305 |
} |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public Citation.Category getCategory() { |
| 311 |
return Citation.Category.MOLECULAR_CLOCK; |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public String getDescription() { |
| 316 |
return "Lineage Specific Branch model"; |
| 317 |
} |
| 318 |
|
| 319 |
public List<Citation> getCitations() { |
| 320 |
return Collections.singletonList( |
| 321 |
new Citation(new Author[] { new Author("F", "Bielejec"), |
| 322 |
new Author("P", "Lemey"), new Author("G", "Baele"), new Author("A", "Rambaut"), |
| 323 |
new Author("MA", "Suchard") }, Citation.Status.IN_PREPARATION)); |
| 324 |
} |
| 325 |
|
| 326 |
} |