| 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.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 |
|
| 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 |
|
| 76 |
|
| 77 |
Sequence intArray2Sequence(int [] seq, NodeRef node) { |
| 78 |
String sSeq = ""+seq[0]; |
| 79 |
return new Sequence(m_tree.getNodeTaxon(node), sSeq); |
| 80 |
} |
| 81 |
|
| 82 |
public double[][] simulateLocations() { |
| 83 |
NodeRef root = m_tree.getRoot(); |
| 84 |
|
| 85 |
|
| 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 |
|
| 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 |
|
| 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 |
} |