Debian Codegraph @

src/dr/app/bss/test/BeagleSeqSimTest.java

raw symbols SCIP
1 /*
2 * BeagleSeqSimTest.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.bss.test;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.text.NumberFormat;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.Locale;
34
35 import dr.evolution.tree.TreeUtils;
36 import dr.evomodel.branchmodel.HomogeneousBranchModel;
37 import dr.evomodel.branchmodel.RandomBranchModel;
38 import dr.evomodel.siteratemodel.GammaSiteRateModel;
39 import dr.evomodel.substmodel.EmpiricalRateMatrix;
40 import dr.evomodel.substmodel.FrequencyModel;
41 import dr.evomodel.substmodel.aminoacid.Blosum62;
42 import dr.evomodel.substmodel.aminoacid.EmpiricalAminoAcidModel;
43 import dr.evomodel.substmodel.codon.GY94CodonModel;
44 import dr.evomodel.substmodel.nucleotide.HKY;
45 import dr.evomodel.substmodel.codon.MG94CodonModel;
46 import dr.evomodel.treelikelihood.BeagleTreeLikelihood;
47 import dr.evomodel.treelikelihood.PartialsRescalingScheme;
48 import dr.app.beagle.tools.BeagleSequenceSimulator;
49 import dr.app.beagle.tools.Partition;
50 import dr.app.bss.Utils;
51 import dr.evolution.alignment.Alignment;
52 import dr.evolution.alignment.ConvertAlignment;
53 import dr.evolution.alignment.SimpleAlignment;
54 import dr.evolution.coalescent.CoalescentSimulator;
55 import dr.evolution.coalescent.ExponentialGrowth;
56 import dr.evolution.datatype.AminoAcids;
57 import dr.evolution.datatype.Codons;
58 import dr.evolution.datatype.DataType;
59 import dr.evolution.datatype.GeneticCode;
60 import dr.evolution.datatype.Nucleotides;
61 import dr.evolution.io.Importer.ImportException;
62 import dr.evolution.io.NewickImporter;
63 import dr.evolution.sequence.Sequence;
64 import dr.evolution.tree.NodeRef;
65 import dr.evolution.tree.Tree;
66 import dr.evolution.tree.TreeTraitProvider;
67 import dr.evolution.util.Taxa;
68 import dr.evolution.util.Taxon;
69 import dr.evolution.util.Units;
70 import dr.evomodel.branchratemodel.BranchRateModel;
71 import dr.evomodel.branchratemodel.DefaultBranchRateModel;
72 import dr.evomodel.tree.TreeModel;
73 import dr.inference.model.Parameter;
74 import dr.math.MathUtils;
75
76 public class BeagleSeqSimTest {
77
78 public static final boolean simulateInPar = true;
79
80 public static void main(String[] args) {
81
82 // start timing
83 long tic = System.currentTimeMillis();
84 int N = 1;
85 for (int i = 0; i < N; i++) {
86 // simulateTopology();
87 // simulateOnePartition();
88 // simulateTwoPartitions();
89 // simulateThreePartitions(i, N);
90 // simulateAminoAcid();
91 // simulateCodon();
92 // ancestralSequenceTree();
93 simulateRandomBranchAssignment();
94
95 }
96 long toc = System.currentTimeMillis();
97
98 long time = toc - tic;
99 System.out.println("Time: " + time + "milliseconds");
100
101 } // END: main
102
103 static void simulateRandomBranchAssignment() {
104
105 try {
106
107 System.out.println("Test case I dunno which: simulate random branch assignments");
108
109 MathUtils.setSeed(666);
110
111 int sequenceLength = 10;
112 ArrayList<Partition> partitionsList = new ArrayList<Partition>();
113
114 File treeFile = new File("/home/filip/Dropbox/BeagleSequenceSimulator/SimTree/SimTree.figtree");
115 Tree tree = Utils.importTreeFromFile(treeFile);
116 TreeModel treeModel = new TreeModel(tree);
117
118 // create Frequency Model
119 Parameter freqs = new Parameter.Default(Utils.UNIFORM_CODON_FREQUENCIES);
120 FrequencyModel freqModel = new FrequencyModel(Codons.UNIVERSAL,
121 freqs);
122
123 // create base subst model
124 Parameter omegaParameter = new Parameter.Default("omega", 1, 1.0);
125 Parameter kappaParameter = new Parameter.Default("kappa", 1, 1.0);
126 GY94CodonModel baseSubModel = new GY94CodonModel(Codons.UNIVERSAL, omegaParameter, kappaParameter, freqModel);
127
128 RandomBranchModel substitutionModel = new RandomBranchModel(treeModel, baseSubModel, 0.25, false, -1);
129
130 // create site model
131 GammaSiteRateModel siteRateModel = new GammaSiteRateModel(
132 "siteModel");
133
134 // create branch rate model
135 BranchRateModel branchRateModel = new DefaultBranchRateModel();
136
137 // create partition
138 Partition partition1 = new Partition(treeModel, //
139 substitutionModel,//
140 siteRateModel, //
141 branchRateModel, //
142 freqModel, //
143 0, // from
144 sequenceLength - 1, // to
145 1 // every
146 );
147
148 // Sequence ancestralSequence = new Sequence();
149 // ancestralSequence.appendSequenceString("TCAAGTGAGG");
150 // partition1.setRootSequence(ancestralSequence);
151
152 partitionsList.add(partition1);
153
154 // feed to sequence simulator and generate data
155 BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(
156 partitionsList);
157
158 SimpleAlignment alignment = simulator.simulate(simulateInPar, true);
159
160 // alignment.setOutputType(SimpleAlignment.OutputType.NEXUS);
161 alignment.setOutputType(SimpleAlignment.OutputType.XML);
162
163 System.out.println(alignment.toString());
164
165
166 } catch (Exception e) {
167
168 e.printStackTrace();
169
170 }// END: try-catch
171
172 }//END: simulateRandomBranchAssignment
173
174 static void ancestralSequenceTree() {
175
176 try {
177
178 LinkedHashMap<NodeRef, int[]> sequenceMap = new LinkedHashMap<NodeRef, int[]>();
179 DataType dataType = Nucleotides.INSTANCE;
180
181 // create tree
182 NewickImporter importer = new NewickImporter(
183 "(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
184 Tree tree = importer.importTree(null);
185 TreeModel treeModel = new TreeModel(tree);
186
187 for (NodeRef node : treeModel.getNodes()) {
188
189 if (treeModel.isExternal(node)) {
190
191 int[] seq = new int[] { 1, 1, 1 };
192 sequenceMap.put(node, seq);
193
194 } else {
195
196 int[] seq = new int[] { 2, 2, 2 };
197 sequenceMap.put(node, seq);
198
199 }
200
201 }// END: nodes loop
202
203 AncestralSequenceTrait ancestralSequence = new AncestralSequenceTrait(sequenceMap, dataType);
204 TreeTraitProvider[] treeTraitProviders = new TreeTraitProvider[] { ancestralSequence };
205
206 StringBuffer buffer = new StringBuffer();
207 NumberFormat format = NumberFormat.getNumberInstance(Locale.ENGLISH);
208 boolean useTipLabels = true;
209
210 TreeUtils.newick(treeModel, //
211 treeModel.getRoot(), //
212 useTipLabels, //
213 TreeUtils.BranchLengthType.LENGTHS_AS_TIME, //
214 format, //
215 null, //
216 treeTraitProviders, //
217 null, buffer);
218
219 System.out.println(buffer);
220
221 } catch (IOException e) {
222
223 e.printStackTrace();
224
225 } catch (ImportException e) {
226
227 e.printStackTrace();
228
229 }// END: try-catch
230
231 }// END: annotateTree
232
233 static void simulateTopology() {
234
235 try {
236
237 System.out.println("Test case 1: simulateTopology");
238
239 MathUtils.setSeed(666);
240
241 int sequenceLength = 10;
242 ArrayList<Partition> partitionsList = new ArrayList<Partition>();
243
244 // create tree
245 NewickImporter importer = new NewickImporter(
246 "(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
247 Tree tree = importer.importTree(null);
248
249 // set demographic function
250 ExponentialGrowth exponentialGrowth = new ExponentialGrowth(
251 Units.Type.YEARS);
252 exponentialGrowth.setN0(10);
253 exponentialGrowth.setGrowthRate(0.5);
254
255 Taxa taxa = new Taxa();
256 for (Taxon taxon : tree.asList()) {
257
258 double absoluteHeight = Utils.getAbsoluteTaxonHeight(taxon,
259 tree);
260
261 taxon.setAttribute(Utils.ABSOLUTE_HEIGHT, absoluteHeight);
262
263 // taxon.setAttribute("date", new Date(absoluteHeight,
264 // Units.Type.YEARS, true));
265
266 taxa.addTaxon(taxon);
267
268 }// END: taxon loop
269
270 CoalescentSimulator topologySimulator = new CoalescentSimulator();
271
272 TreeModel treeModel = new TreeModel(topologySimulator.simulateTree(
273 taxa, exponentialGrowth));
274
275 System.out.println(treeModel.toString());
276
277 Parameter freqs = new Parameter.Default(new double[] { 0.25, 0.25,
278 0.25, 0.25 });
279 FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE,
280 freqs);
281
282 // create substitution model
283 Parameter kappa = new Parameter.Default(1, 10);
284 HKY hky = new HKY(kappa, freqModel);
285 HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(
286 hky);
287
288 // create site model
289 GammaSiteRateModel siteRateModel = new GammaSiteRateModel(
290 "siteModel");
291
292 // create branch rate model
293 BranchRateModel branchRateModel = new DefaultBranchRateModel();
294
295 // create partition
296 Partition partition1 = new Partition(treeModel, //
297 substitutionModel,//
298 siteRateModel, //
299 branchRateModel, //
300 freqModel, //
301 0, // from
302 sequenceLength - 1, // to
303 1 // every
304 );
305
306 partitionsList.add(partition1);
307
308 // feed to sequence simulator and generate data
309 BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(
310 partitionsList);
311
312 System.out.println(simulator.simulate(simulateInPar, false).toString());
313
314 } catch (Exception e) {
315 e.printStackTrace();
316 System.exit(-1);
317 } // END: try-catch block
318
319 }// END: simulate topology
320
321 static void simulateOnePartition() {
322
323 try {
324
325 MathUtils.setSeed(666);
326
327 System.out.println("Test case 2: simulateOnePartition");
328
329 int sequenceLength = 10;
330 ArrayList<Partition> partitionsList = new ArrayList<Partition>();
331
332 // create tree
333 NewickImporter importer = new NewickImporter(
334 "(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
335 Tree tree = importer.importTree(null);
336 TreeModel treeModel = new TreeModel(tree);
337
338 // create Frequency Model
339 Parameter freqs = new Parameter.Default(new double[] { 0.25, 0.25,
340 0.25, 0.25 });
341 FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE,
342 freqs);
343
344 // create substitution model
345 Parameter kappa = new Parameter.Default(1, 10);
346 HKY hky = new HKY(kappa, freqModel);
347 HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(
348 hky);
349
350 // create site model
351 GammaSiteRateModel siteRateModel = new GammaSiteRateModel(
352 "siteModel");
353
354 // create branch rate model
355 BranchRateModel branchRateModel = new DefaultBranchRateModel();
356
357 // create partition
358 Partition partition1 = new Partition(treeModel, //
359 substitutionModel,//
360 siteRateModel, //
361 branchRateModel, //
362 freqModel, //
363 0, // from
364 sequenceLength - 1, // to
365 1 // every
366 );
367
368 Sequence ancestralSequence = new Sequence();
369 ancestralSequence.appendSequenceString("TCAAGTGAGG");
370 partition1.setRootSequence(ancestralSequence);
371
372 partitionsList.add(partition1);
373
374 // feed to sequence simulator and generate data
375 BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(
376 partitionsList);
377
378 SimpleAlignment alignment = simulator.simulate(simulateInPar, false);
379
380 // alignment.setOutputType(SimpleAlignment.OutputType.NEXUS);
381 alignment.setOutputType(SimpleAlignment.OutputType.XML);
382
383 System.out.println(alignment.toString());
384
385 } catch (Exception e) {
386 e.printStackTrace();
387 System.exit(-1);
388 } // END: try-catch block
389
390 }// END: simulateOnePartition
391
392 static void simulateTwoPartitions() {
393
394 try {
395
396 System.out.println("Test case 3: simulateTwoPartitions");
397
398 MathUtils.setSeed(666);
399
400 int sequenceLength = 11;
401 ArrayList<Partition> partitionsList = new ArrayList<Partition>();
402
403 // create tree
404 NewickImporter importer = new NewickImporter(
405 "(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
406 Tree tree = importer.importTree(null);
407 TreeModel treeModel = new TreeModel(tree);
408
409 // create Frequency Model
410 Parameter freqs = new Parameter.Default(new double[] { 0.25, 0.25,
411 0.25, 0.25 });
412 FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE,
413 freqs);
414
415 // create substitution model
416 Parameter kappa = new Parameter.Default(1, 10);
417 HKY hky = new HKY(kappa, freqModel);
418 HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(
419 hky);
420
421 // create site model
422 GammaSiteRateModel siteRateModel = new GammaSiteRateModel(
423 "siteModel");
424
425 // create branch rate model
426 BranchRateModel branchRateModel = new DefaultBranchRateModel();
427
428 // create partition
429 Partition partition1 = new Partition(treeModel, //
430 substitutionModel, //
431 siteRateModel, //
432 branchRateModel, //
433 freqModel, //
434 0, // from
435 3, // to
436 1 // every
437 );
438
439 // create partition
440 Partition Partition = new Partition(treeModel, //
441 substitutionModel,//
442 siteRateModel, //
443 branchRateModel, //
444 freqModel, //
445 4, // from
446 sequenceLength - 1, // to
447 1 // every
448 );
449
450 Sequence ancestralSequence = new Sequence();
451 ancestralSequence.appendSequenceString("TCAAGTG");
452 Partition.setRootSequence(ancestralSequence);
453
454 partitionsList.add(partition1);
455 partitionsList.add(Partition);
456
457 // feed to sequence simulator and generate data
458 BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(
459 partitionsList);
460
461 System.out.println(simulator.simulate(simulateInPar, false).toString());
462
463 } catch (Exception e) {
464 e.printStackTrace();
465 System.exit(-1);
466 } // END: try-catch block
467
468 }// END: simulateTwoPartitions
469
470 static void simulateThreePartitions(int i, int N) {
471
472 try {
473
474 MathUtils.setSeed(666);
475
476 System.out.println("Test case 3: simulateThreePartitions");
477
478 int sequenceLength = 100000;
479 ArrayList<Partition> partitionsList = new ArrayList<Partition>();
480
481 // create tree
482 NewickImporter importer = new NewickImporter(
483 "(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
484 Tree tree = importer.importTree(null);
485 TreeModel treeModel = new TreeModel(tree);
486
487 // create Frequency Model
488 Parameter freqs = new Parameter.Default(new double[] { 0.25, 0.25,
489 0.25, 0.25 });
490 FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE,
491 freqs);
492
493 // create substitution model
494 Parameter kappa = new Parameter.Default(1, 10);
495 HKY hky = new HKY(kappa, freqModel);
496 HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(
497 hky);
498
499 // create site model
500 GammaSiteRateModel siteRateModel = new GammaSiteRateModel(
501 "siteModel");
502
503 // create branch rate model
504 BranchRateModel branchRateModel = new DefaultBranchRateModel();
505
506 // create partition
507 Partition partition1 = new Partition(treeModel, //
508 substitutionModel, //
509 siteRateModel, //
510 branchRateModel, //
511 freqModel, //
512 0, // from
513 sequenceLength - 1, // to
514 3 // every
515 );
516
517 // create partition
518 Partition Partition = new Partition(treeModel, //
519 substitutionModel,//
520 siteRateModel, //
521 branchRateModel, //
522 freqModel, //
523 1, // from
524 sequenceLength - 1, // to
525 3 // every
526 );
527
528 // create partition
529 Partition partition3 = new Partition(treeModel, //
530 substitutionModel,//
531 siteRateModel, //
532 branchRateModel, //
533 freqModel, //
534 2, // from
535 sequenceLength - 1, // to
536 3 // every
537 );
538
539 partitionsList.add(partition1);
540 partitionsList.add(Partition);
541 partitionsList.add(partition3);
542
543 // feed to sequence simulator and generate data
544 BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(
545 partitionsList);
546
547 if (i == (N - 1)) {
548 System.out
549 .println(simulator.simulate(simulateInPar, false).toString());
550 } else {
551 simulator.simulate(simulateInPar, false);
552 }
553
554 } catch (Exception e) {
555 e.printStackTrace();
556 System.exit(-1);
557 } // END: try-catch block
558
559 }// END: simulateThreePartitions
560
561 static void simulateAminoAcid() {
562
563 try {
564
565 System.out.println("Test case 4: simulateAminoAcid");
566
567 MathUtils.setSeed(666);
568
569 int sequenceLength = 10;
570 ArrayList<Partition> partitionsList = new ArrayList<Partition>();
571
572 // create tree
573 NewickImporter importer = new NewickImporter(
574 "(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
575 Tree tree = importer.importTree(null);
576 TreeModel treeModel = new TreeModel(tree);
577
578 // create site model
579 GammaSiteRateModel siteRateModel = new GammaSiteRateModel(
580 "siteModel");
581
582 // create branch rate model
583 BranchRateModel branchRateModel = new DefaultBranchRateModel();
584
585 // create Frequency Model
586 Parameter freqs = new Parameter.Default(new double[] { 0.05, 0.05,
587 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
588 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05 });
589 FrequencyModel freqModel = new FrequencyModel(AminoAcids.INSTANCE,
590 freqs);
591
592 // create substitution model
593 EmpiricalRateMatrix rateMatrix = Blosum62.INSTANCE;
594
595 EmpiricalAminoAcidModel empiricalAminoAcidModel = new EmpiricalAminoAcidModel(
596 rateMatrix, freqModel);
597
598 HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(
599 empiricalAminoAcidModel);
600
601 // create partition
602 Partition partition1 = new Partition(treeModel, //
603 substitutionModel,//
604 siteRateModel, //
605 branchRateModel, //
606 freqModel, //
607 0, // from
608 sequenceLength - 1, // to
609 1 // every
610 );
611
612 partitionsList.add(partition1);
613
614 // feed to sequence simulator and generate data
615 BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(
616 partitionsList);
617
618 System.out.println(simulator.simulate(simulateInPar, false).toString());
619
620 } catch (Exception e) {
621
622 e.printStackTrace();
623 System.exit(-1);
624
625 } // END: try-catch
626
627 }// END: simulateAminoAcid
628
629 static void simulateCodon() {
630
631 try {
632
633 boolean calculateLikelihood = true;
634
635 System.out.println("Test case 6: simulate codons");
636
637 MathUtils.setSeed(666);
638
639 int sequenceLength = 10;
640 ArrayList<Partition> partitionsList = new ArrayList<Partition>();
641
642 // create tree
643 NewickImporter importer = new NewickImporter(
644 "(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
645 Tree tree = importer.importTree(null);
646 TreeModel treeModel = new TreeModel(tree);
647
648 // create site model
649 GammaSiteRateModel siteRateModel = new GammaSiteRateModel(
650 "siteModel");
651
652 // create branch rate model
653 BranchRateModel branchRateModel = new DefaultBranchRateModel();
654
655 // create Frequency Model
656 Parameter freqs = new Parameter.Default(Utils.UNIFORM_CODON_FREQUENCIES);
657 FrequencyModel freqModel = new FrequencyModel(Codons.UNIVERSAL,
658 freqs);
659
660 // create substitution model
661 Parameter alpha = new Parameter.Default(1, 10);
662 Parameter beta = new Parameter.Default(1, 5);
663 // Parameter kappa = new Parameter.Default(1, 1);
664 MG94CodonModel mg94 = new MG94CodonModel(Codons.UNIVERSAL, alpha,
665 beta, freqModel);
666
667 HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(
668 mg94);
669
670 // create partition
671 Partition partition1 = new Partition(treeModel, //
672 substitutionModel,//
673 siteRateModel, //
674 branchRateModel, //
675 freqModel, //
676 0, // from
677 sequenceLength - 1, // to
678 1 // every
679 );
680
681 partitionsList.add(partition1);
682
683 // feed to sequence simulator and generate data
684 BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(
685 partitionsList);
686
687 Alignment alignment = simulator.simulate(simulateInPar, false);
688
689 System.out.println(alignment.toString());
690
691 if (calculateLikelihood) {
692
693 // NewBeagleSequenceLikelihood nbtl = new
694 // NewBeagleSequenceLikelihood(alignment, treeModel,
695 // substitutionModel, (SiteModel) siteRateModel,
696 // branchRateModel, null, false,
697 // PartialsRescalingScheme.DEFAULT);
698
699 ConvertAlignment convert = new ConvertAlignment(
700 Nucleotides.INSTANCE, GeneticCode.UNIVERSAL, alignment);
701
702 BeagleTreeLikelihood nbtl = new BeagleTreeLikelihood(convert, //
703 treeModel, //
704 substitutionModel, //
705 siteRateModel, //
706 branchRateModel, //
707 null, //
708 false, //
709 PartialsRescalingScheme.DEFAULT, true);
710
711 System.out.println("likelihood = " + nbtl.getLogLikelihood());
712
713 }
714
715 } catch (Exception e) {
716
717 e.printStackTrace();
718 System.exit(-1);
719
720 } // END: try-catch
721
722 }// END: simulateCodon
723
724 }// END: class