| 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.tree.randomlocalmodel; |
| 27 |
|
| 28 |
import dr.stats.DiscreteStatistics; |
| 29 |
|
| 30 |
import java.io.BufferedReader; |
| 31 |
import java.io.FileReader; |
| 32 |
import java.io.IOException; |
| 33 |
import java.text.NumberFormat; |
| 34 |
import java.util.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class RLTVAnalyzer { |
| 40 |
|
| 41 |
static TreeMap<Integer, List<Double>> nodeRates = new TreeMap<Integer, List<Double>>(); |
| 42 |
static Map<String, Integer> comboCounts = new HashMap<String, Integer>(); |
| 43 |
static Map<String, Integer> pairCounts = new HashMap<String, Integer>(); |
| 44 |
|
| 45 |
static NumberFormat format = NumberFormat.getNumberInstance(Locale.ENGLISH); |
| 46 |
|
| 47 |
public static void main(String[] args) throws IOException { |
| 48 |
|
| 49 |
int total = 0; |
| 50 |
|
| 51 |
double priorChange = Double.parseDouble(args[1]); |
| 52 |
|
| 53 |
format.setMaximumFractionDigits(5); |
| 54 |
|
| 55 |
BufferedReader reader = new BufferedReader(new FileReader(args[0])); |
| 56 |
|
| 57 |
|
| 58 |
String line = reader.readLine(); |
| 59 |
while (line.toLowerCase().startsWith("state")) { |
| 60 |
line = reader.readLine(); |
| 61 |
} |
| 62 |
|
| 63 |
while (line != null) { |
| 64 |
StringTokenizer tokens = new StringTokenizer(line); |
| 65 |
|
| 66 |
|
| 67 |
tokens.nextToken(); |
| 68 |
|
| 69 |
List<Integer> changes = new ArrayList<Integer>(); |
| 70 |
while (tokens.hasMoreTokens()) { |
| 71 |
int nodeNumber = Integer.parseInt(tokens.nextToken()); |
| 72 |
double rate = Double.parseDouble(tokens.nextToken()); |
| 73 |
|
| 74 |
changes.add(nodeNumber); |
| 75 |
|
| 76 |
putRate(nodeNumber, rate); |
| 77 |
} |
| 78 |
putCombo(changes); |
| 79 |
total += 1; |
| 80 |
line = reader.readLine(); |
| 81 |
} |
| 82 |
|
| 83 |
List<RateCombo> rateCombos = new ArrayList<RateCombo>(); |
| 84 |
for (Map.Entry<String, Integer> stringIntegerEntry1 : comboCounts.entrySet()) { |
| 85 |
|
| 86 |
int count = stringIntegerEntry1.getValue(); |
| 87 |
if (count > 1) rateCombos.add(new RateCombo(stringIntegerEntry1.getKey(), count)); |
| 88 |
} |
| 89 |
|
| 90 |
Collections.sort(rateCombos, rateComboComparator); |
| 91 |
|
| 92 |
System.out.println("combo\tPr"); |
| 93 |
for (RateCombo rateCombo : rateCombos) { |
| 94 |
double posterior = (double) rateCombo.count / (double) total; |
| 95 |
|
| 96 |
String[] nodes = rateCombo.combo.split("-"); |
| 97 |
|
| 98 |
System.out.println(rateCombo.combo + "\t" + format.format(posterior)); |
| 99 |
} |
| 100 |
|
| 101 |
List<RateCombo> pairRateCombos = new ArrayList<RateCombo>(); |
| 102 |
for (Map.Entry<String, Integer> stringIntegerEntry : pairCounts.entrySet()) { |
| 103 |
int count = stringIntegerEntry.getValue(); |
| 104 |
if (count > 1) pairRateCombos.add(new RateCombo(stringIntegerEntry.getKey(), count)); |
| 105 |
} |
| 106 |
|
| 107 |
Collections.sort(pairRateCombos, rateComboComparator); |
| 108 |
|
| 109 |
System.out.println(); |
| 110 |
System.out.println("pair\tPr"); |
| 111 |
for (RateCombo rateCombo : pairRateCombos) { |
| 112 |
|
| 113 |
double posterior = (double) rateCombo.count / (double) total; |
| 114 |
|
| 115 |
System.out.println(rateCombo.combo + "\t" + format.format(posterior)); |
| 116 |
} |
| 117 |
|
| 118 |
List<RateCombo> singleRateCombos = new ArrayList<RateCombo>(); |
| 119 |
for (int nodeNumber : nodeRates.keySet()) { |
| 120 |
int count = nodeRates.get(nodeNumber).size(); |
| 121 |
|
| 122 |
if (count > 1) { |
| 123 |
double meanRate = getMeanRate(nodeNumber); |
| 124 |
|
| 125 |
double[] cpd = getRateCPD(nodeNumber); |
| 126 |
|
| 127 |
String details = format.format(meanRate) + |
| 128 |
"\t" + format.format(cpd[0]) + "\t" + format.format(cpd[1]); |
| 129 |
|
| 130 |
singleRateCombos.add( |
| 131 |
new RateCombo(nodeNumber + "\t" + details, count)); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
Collections.sort(singleRateCombos, rateComboComparator); |
| 136 |
|
| 137 |
System.out.println(); |
| 138 |
System.out.println("node change\trate\t95% lower\t95% upper\tPr\tBF"); |
| 139 |
for (RateCombo rateCombo : singleRateCombos) { |
| 140 |
|
| 141 |
double posterior = (double) rateCombo.count / (double) total; |
| 142 |
double bf = bayesFactor(posterior, priorChange); |
| 143 |
|
| 144 |
System.out.println(rateCombo.combo + "\t" + format.format(posterior) + "\t" + format.format(bf)); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private static double bayesFactor(double posterior, double prior) { |
| 154 |
|
| 155 |
double like1 = posterior / prior; |
| 156 |
double like2 = (1 - posterior) / (1 - prior); |
| 157 |
|
| 158 |
return like1 / like2; |
| 159 |
} |
| 160 |
|
| 161 |
private static void putCombo(List<Integer> changes) { |
| 162 |
|
| 163 |
if (changes.size() == 0) return; |
| 164 |
|
| 165 |
|
| 166 |
for (int i = 0; i < changes.size(); i++) { |
| 167 |
for (int j = i + 1; j < changes.size(); j++) { |
| 168 |
putPair(changes.get(i), changes.get(j)); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
StringBuilder builder = new StringBuilder(); |
| 173 |
builder.append(changes.get(0)); |
| 174 |
for (int i = 1; i < changes.size(); i++) { |
| 175 |
builder.append("-"); |
| 176 |
builder.append(changes.get(i)); |
| 177 |
} |
| 178 |
|
| 179 |
String combo = builder.toString(); |
| 180 |
|
| 181 |
Integer i = comboCounts.get(combo); |
| 182 |
if (i == null) { |
| 183 |
comboCounts.put(combo, 1); |
| 184 |
} else { |
| 185 |
comboCounts.put(combo, i + 1); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
private static void putPair(int i, int j) { |
| 190 |
|
| 191 |
String pair = "{" + i + "," + j + "}"; |
| 192 |
Integer count = pairCounts.get(pair); |
| 193 |
if (count == null) { |
| 194 |
pairCounts.put(pair, 1); |
| 195 |
} else { |
| 196 |
pairCounts.put(pair, count + 1); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
private static void putRate(int nodeNumber, double rate) { |
| 201 |
|
| 202 |
List<Double> rates = nodeRates.get(nodeNumber); |
| 203 |
|
| 204 |
if (rates == null) { |
| 205 |
rates = new ArrayList<Double>(); |
| 206 |
nodeRates.put(nodeNumber, rates); |
| 207 |
} |
| 208 |
|
| 209 |
rates.add(rate); |
| 210 |
} |
| 211 |
|
| 212 |
private static double getStdevRate(int nodeNumber) { |
| 213 |
List<Double> rates = nodeRates.get(nodeNumber); |
| 214 |
|
| 215 |
double[] r = new double[rates.size()]; |
| 216 |
for (int i = 0; i < r.length; i++) { |
| 217 |
r[i] = rates.get(i); |
| 218 |
} |
| 219 |
|
| 220 |
return DiscreteStatistics.stdev(r); |
| 221 |
} |
| 222 |
|
| 223 |
private static double[] getRateCPD(int nodeNumber) { |
| 224 |
List<Double> rates = nodeRates.get(nodeNumber); |
| 225 |
|
| 226 |
double[] r = new double[rates.size()]; |
| 227 |
for (int i = 0; i < r.length; i++) { |
| 228 |
r[i] = rates.get(i); |
| 229 |
} |
| 230 |
|
| 231 |
return new double[]{DiscreteStatistics.quantile(0.025, r), DiscreteStatistics.quantile(0.975, r)}; |
| 232 |
} |
| 233 |
|
| 234 |
private static double getMeanRate(int nodeNumber) { |
| 235 |
List<Double> rates = nodeRates.get(nodeNumber); |
| 236 |
double meanRate = 0.0; |
| 237 |
for (double rate : rates) { |
| 238 |
meanRate += rate; |
| 239 |
} |
| 240 |
meanRate /= rates.size(); |
| 241 |
return meanRate; |
| 242 |
} |
| 243 |
|
| 244 |
static class RateCombo { |
| 245 |
|
| 246 |
public RateCombo(String combo, int count) { |
| 247 |
this.combo = combo; |
| 248 |
this.count = count; |
| 249 |
} |
| 250 |
|
| 251 |
final String combo; |
| 252 |
final int count; |
| 253 |
|
| 254 |
public boolean equals(Object o) { |
| 255 |
assert o instanceof RateCombo; |
| 256 |
RateCombo c2 = (RateCombo) o; |
| 257 |
return (combo.equals(c2.combo) && count == c2.count); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
static Comparator<RateCombo> rateComboComparator = new Comparator<RateCombo>() { |
| 262 |
|
| 263 |
public int compare(RateCombo r1, RateCombo r2) { |
| 264 |
return r2.count - r1.count; |
| 265 |
} |
| 266 |
}; |
| 267 |
} |