Debian Codegraph @

src/dr/app/seqgen/GeoDiffusionSimulator.java

raw symbols SCIP
1 /*
2 * GeoDiffusionSimulator.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.seqgen;
27
28 import dr.evolution.util.Taxa;
29 import dr.evolution.datatype.Microsatellite;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.tree.NodeRef;
32 import dr.evolution.sequence.Sequence;
33 import dr.oldevomodel.sitemodel.SiteModel;
34 import dr.evomodel.branchratemodel.BranchRateModel;
35 import dr.math.MathUtils;
36 import dr.inference.model.Parameter;
37
38 import java.util.ArrayList;
39
40 /**
41 * @author Chieh-Hsi Wu
42 */
43 public class GeoDiffusionSimulator extends SequenceSimulator{
44 public static final int LATITUDE_INDEX = 0;
45 public static final int LONGITUDE_INDEX = 1;
46 private Taxa taxa;
47 private Microsatellite dataType;
48 private double maxLat;
49 private double minLat;
50 private double maxLong;
51 private double minLong;
52
53 public GeoDiffusionSimulator(
54 Microsatellite dataType,
55 Taxa taxa,
56 Tree tree,
57 SiteModel siteModel,
58 BranchRateModel branchRateModel,
59 double maxLat,
60 double minLat,
61 double maxLong,
62 double minLong) {
63
64 super(tree, siteModel, branchRateModel, 1);
65 this.dataType = dataType;
66 this.taxa = taxa;
67 this.maxLat = maxLat;
68 this.minLat = minLat;
69 this.maxLong = maxLong;
70 this.minLong = minLong;
71
72 }
73
74 /**
75 * Convert integer representation of microsatellite length to string.
76 */
77 Sequence intArray2Sequence(int [] seq, NodeRef node) {
78 String sSeq = ""+seq[0];
79 return new Sequence(m_tree.getNodeTaxon(node), sSeq);
80 } // intArray2Sequence
81
82 public double[][] simulateLocations() {
83 NodeRef root = m_tree.getRoot();
84
85 //assume uniform
86 double[][] latLongs = new double[m_tree.getNodeCount()][2];
87 double rootLat = MathUtils.nextDouble()*(maxLat-minLat)+minLat;
88 double rootLong = MathUtils.nextDouble()*(maxLong-minLong)+minLong;
89 int rootNum = root.getNumber();
90 latLongs[rootNum] [LATITUDE_INDEX] = rootLat;
91 latLongs[rootNum] [LONGITUDE_INDEX] = rootLong;
92 traverse(root, latLongs[rootNum], latLongs);
93
94
95
96 return latLongs;
97 }
98
99 void traverse(NodeRef node, double [] parentSequence, double[][] latLongs) {
100 for (int iChild = 0; iChild < m_tree.getChildCount(node); iChild++) {
101 NodeRef child = m_tree.getChild(node, iChild);
102
103 //find the branch length
104 final double branchRate = m_branchRateModel.getBranchRate(m_tree, child);
105 final double branchLength = branchRate * (m_tree.getNodeHeight(node) - m_tree.getNodeHeight(child));
106 if (branchLength < 0.0) {
107 throw new RuntimeException("Negative branch length: " + branchLength);
108 }
109
110 double childLat = MathUtils.nextGaussian()*Math.sqrt(branchLength)+parentSequence[LATITUDE_INDEX];
111 double childLong = MathUtils.nextGaussian()*Math.sqrt(branchLength)+parentSequence[LONGITUDE_INDEX];
112 int childNum = child.getNumber();
113
114 latLongs[childNum][LATITUDE_INDEX] = childLat;
115 latLongs[childNum][LONGITUDE_INDEX] = childLong;
116
117 traverse(m_tree.getChild(node, iChild), latLongs[childNum], latLongs);
118 }
119 }
120
121 /**
122 * Convert an alignment to a pattern
123 */
124 public ArrayList simulateGeoAttr(){
125 double[][] locations = simulateLocations();
126
127 ArrayList<Parameter> locationList = new ArrayList<Parameter>();
128 for(int i = 0; i < m_tree.getExternalNodeCount(); i++){
129 NodeRef node = m_tree.getNode(i);
130 String taxaName = m_tree.getTaxon(node.getNumber()).getId();
131 Parameter location = new Parameter.Default(locations[i]);
132 System.out.println("taxon: "+taxaName+", lat: "+locations[i][0]+", long: "+locations[i][1]);
133 locationList.add(location);
134 }
135
136 return locationList;
137 }
138
139 }