| 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.tools; |
| 27 |
|
| 28 |
import dr.app.beast.BeastVersion; |
| 29 |
import dr.app.util.Arguments; |
| 30 |
import dr.app.util.Utils; |
| 31 |
import dr.evolution.io.Importer; |
| 32 |
import dr.evolution.io.NexusImporter; |
| 33 |
import dr.evolution.io.TreeExporter; |
| 34 |
import dr.evolution.io.TreeImporter; |
| 35 |
import dr.evolution.tree.FlexibleTree; |
| 36 |
import dr.inference.trace.TraceException; |
| 37 |
import dr.util.Version; |
| 38 |
import java.util.*; |
| 39 |
import java.io.*; |
| 40 |
|
| 41 |
public class GetAncestralSequenceFromSplitTrait { |
| 42 |
|
| 43 |
private final static Version version = new BeastVersion(); |
| 44 |
|
| 45 |
|
| 46 |
public GetAncestralSequenceFromSplitTrait(String treeAnnotatorFileName, String outputFileName) throws IOException, TraceException { |
| 47 |
|
| 48 |
File outInputFile = new File(treeAnnotatorFileName); |
| 49 |
|
| 50 |
if (outInputFile.isFile()) { |
| 51 |
System.out.println("Analysing *.tree file: " + treeAnnotatorFileName); |
| 52 |
} else { |
| 53 |
System.err.println(treeAnnotatorFileName + " does not exist!"); |
| 54 |
System.exit(0); |
| 55 |
} |
| 56 |
|
| 57 |
if (outputFileName != null) { |
| 58 |
FileOutputStream outputStream = new FileOutputStream(outputFileName); |
| 59 |
System.setOut(new PrintStream(outputStream)); |
| 60 |
} |
| 61 |
|
| 62 |
analyze(outInputFile); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private void analyze(File treeAnnotatorInputFile) throws TraceException { |
| 73 |
|
| 74 |
try { |
| 75 |
|
| 76 |
FileReader fileReader = new FileReader(treeAnnotatorInputFile); |
| 77 |
TreeImporter importer = new NexusImporter(fileReader); |
| 78 |
FlexibleTree tree = (FlexibleTree) importer.importNextTree(); |
| 79 |
for(int i = 0; i<tree.getNodeCount(); i++) { |
| 80 |
Hashtable<Integer, State> states = new Hashtable<Integer, State>(); |
| 81 |
for (Iterator<String> j = tree.getNodeAttributeNames(tree.getNode(i)); j.hasNext();) { |
| 82 |
String name = j.next(); |
| 83 |
if(name.indexOf("states_")>=0) { |
| 84 |
Integer d = Integer.parseInt(name.replaceFirst("states_", "").replaceFirst("\\..+", "")); |
| 85 |
State s; |
| 86 |
if(states.containsKey(d)) { |
| 87 |
s = states.get(d); |
| 88 |
} |
| 89 |
else { |
| 90 |
s = new State(d); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
if(name.matches("states_"+d+".prob")) { |
| 114 |
Object o = tree.getNodeAttribute(tree.getNode(i), name); |
| 115 |
double probability = (Double) o; |
| 116 |
s.setProbability(probability); |
| 117 |
} |
| 118 |
else if(name.matches("states_"+d)) { |
| 119 |
Object o = tree.getNodeAttribute(tree.getNode(i), name); |
| 120 |
String value = (String) o; |
| 121 |
s.setState(value.replaceAll("\"", "")); |
| 122 |
} |
| 123 |
else if(name.matches("states_"+d+".set.prob")) { |
| 124 |
Object[] o = (Object[]) tree.getNodeAttribute(tree.getNode(i), name); |
| 125 |
double[] probabilities = new double[o.length]; |
| 126 |
for(int k=0; k<o.length; k++) { |
| 127 |
probabilities[k] = (Double) o[k]; |
| 128 |
} |
| 129 |
s.setProbabilities(probabilities); |
| 130 |
} |
| 131 |
else if(name.matches("states_"+d+".set")) { |
| 132 |
Object[] o = (Object[]) tree.getNodeAttribute(tree.getNode(i), name); |
| 133 |
String[] set = new String[o.length]; |
| 134 |
for(int k=0; k<o.length; k++) { |
| 135 |
set[k] = ((String) o[k]).replaceAll("\"", ""); |
| 136 |
} |
| 137 |
s.setSet(set); |
| 138 |
} |
| 139 |
|
| 140 |
states.put(d, s); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
State[] statesArray = states.values().toArray(new State[states.size()]); |
| 145 |
Arrays.sort(statesArray); |
| 146 |
|
| 147 |
StringBuffer sb = new StringBuffer(statesArray.length * statesArray[0].getState().length()); |
| 148 |
for(State s : statesArray) { |
| 149 |
sb.append(s.getState()); |
| 150 |
} |
| 151 |
|
| 152 |
tree.setNodeAttribute(tree.getNode(i), "seq", sb.toString()); |
| 153 |
} |
| 154 |
|
| 155 |
TreeExporter exporter = new NexusExporter(System.out); |
| 156 |
exporter.exportTree(tree); |
| 157 |
|
| 158 |
|
| 159 |
System.out.println("Begin trees;"); |
| 160 |
System.out.println("\ttree max_tree = " + tree.toString()); |
| 161 |
System.out.println("End;"); |
| 162 |
|
| 163 |
} catch (IOException e) { |
| 164 |
System.err.println("Error Parsing Input log: " + e.getMessage()); |
| 165 |
} |
| 166 |
catch (Importer.ImportException e) { |
| 167 |
System.err.println("Error Parsing Input Tree: " + e.getMessage()); |
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public static void printTitle() { |
| 175 |
System.out.println(); |
| 176 |
centreLine("GetAncestralSequenceFromSplitTrait " + version.getVersionString() + ", " + version.getDateString(), 60); |
| 177 |
centreLine("BAli-Phy MCMC Output analysis", 60); |
| 178 |
centreLine("by", 60); |
| 179 |
centreLine("Wai Lok Sibon Li and Marc A. Suchard", 60); |
| 180 |
System.out.println(); |
| 181 |
centreLine("David Geffen School of Medicine", 60); |
| 182 |
centreLine("University of California, Los Angeles", 60); |
| 183 |
centreLine("[email protected]", 60); |
| 184 |
centreLine("and", 60); |
| 185 |
centreLine("[email protected]",60); |
| 186 |
System.out.println(); |
| 187 |
System.out.println(); |
| 188 |
} |
| 189 |
|
| 190 |
public static void centreLine(String line, int pageWidth) { |
| 191 |
int n = pageWidth - line.length(); |
| 192 |
int n1 = n / 2; |
| 193 |
for (int i = 0; i < n1; i++) { |
| 194 |
System.out.print(" "); |
| 195 |
} |
| 196 |
System.out.println(line); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
public static void printUsage(Arguments arguments) { |
| 201 |
|
| 202 |
arguments.printUsage("GetAncestralSequenceFromSplitTrait", "[<*.tree file-name> [<output-file-name>]]"); |
| 203 |
System.out.println(); |
| 204 |
System.out.println(" Example: ..."); |
| 205 |
System.out.println(" Example: ..."); |
| 206 |
System.out.println(); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
public static void main(String[] args) throws IOException, TraceException { |
| 212 |
|
| 213 |
printTitle(); |
| 214 |
|
| 215 |
Arguments arguments = new Arguments( |
| 216 |
new Arguments.Option[]{ |
| 217 |
new Arguments.Option("help", "option to print this message") |
| 218 |
}); |
| 219 |
|
| 220 |
try { |
| 221 |
arguments.parseArguments(args); |
| 222 |
} catch (Arguments.ArgumentException ae) { |
| 223 |
System.out.println(ae); |
| 224 |
printUsage(arguments); |
| 225 |
System.exit(1); |
| 226 |
} |
| 227 |
|
| 228 |
if (arguments.hasOption("help")) { |
| 229 |
printUsage(arguments); |
| 230 |
System.exit(0); |
| 231 |
} |
| 232 |
|
| 233 |
String outInputFileName = null; |
| 234 |
String outputFileName = null; |
| 235 |
String[] args2 = arguments.getLeftoverArguments(); |
| 236 |
|
| 237 |
if (args2.length > 2) { |
| 238 |
System.err.println("Unknown option: " + args2[2]); |
| 239 |
System.err.println(); |
| 240 |
printUsage(arguments); |
| 241 |
System.exit(1); |
| 242 |
} |
| 243 |
|
| 244 |
if (args2.length > 0) { |
| 245 |
outInputFileName = args2[0]; |
| 246 |
} |
| 247 |
if (args2.length > 1) { |
| 248 |
outputFileName = args2[1]; |
| 249 |
} |
| 250 |
|
| 251 |
if (outInputFileName == null) { |
| 252 |
|
| 253 |
outInputFileName = Utils.getLoadFileName("GetAncestralSequenceFromSplitTrait " + version.getVersionString() + " - Select *.tree file to analyse"); |
| 254 |
} |
| 255 |
new GetAncestralSequenceFromSplitTrait(outInputFileName, outputFileName); |
| 256 |
|
| 257 |
System.exit(0); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
private class State implements Comparable<State> { |
| 262 |
private State(int stateIndex) { |
| 263 |
this.stateIndex = stateIndex; |
| 264 |
} |
| 265 |
|
| 266 |
private int stateIndex; |
| 267 |
private String[] set; |
| 268 |
private double[] probabilities; |
| 269 |
private String state; |
| 270 |
private double probability; |
| 271 |
|
| 272 |
public String getState() { |
| 273 |
return state; |
| 274 |
} |
| 275 |
|
| 276 |
public void setState(String state) { |
| 277 |
this.state = state; |
| 278 |
} |
| 279 |
|
| 280 |
public double getProbability() { |
| 281 |
return probability; |
| 282 |
} |
| 283 |
|
| 284 |
public void setProbability(double probability) { |
| 285 |
this.probability = probability; |
| 286 |
} |
| 287 |
|
| 288 |
public int getStateIndex() { |
| 289 |
return stateIndex; |
| 290 |
} |
| 291 |
|
| 292 |
public void setStateIndex(int stateIndex) { |
| 293 |
this.stateIndex = stateIndex; |
| 294 |
} |
| 295 |
|
| 296 |
public String[] getSet() { |
| 297 |
return set; |
| 298 |
} |
| 299 |
|
| 300 |
public void setSet(String[] set) { |
| 301 |
this.set = set; |
| 302 |
} |
| 303 |
|
| 304 |
public double[] getProbabilities() { |
| 305 |
return probabilities; |
| 306 |
} |
| 307 |
|
| 308 |
public void setProbabilities(double[] probabilities) { |
| 309 |
this.probabilities = probabilities; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
public int compareTo(State o) { |
| 314 |
return this.getStateIndex()-o.getStateIndex(); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
} |
| 335 |
} |
| 336 |
|