Debian Codegraph @

src/dr/app/tools/BranchJumpPlotter.java

raw symbols SCIP
1 /*
2 * BranchJumpPlotter.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.tools;
27
28 import dr.evolution.io.Importer;
29 import dr.evolution.io.NexusImporter;
30 import dr.evolution.tree.FlexibleNode;
31 import dr.evolution.tree.FlexibleTree;
32 import dr.evolution.tree.Tree;
33
34 import java.io.FileNotFoundException;
35 import java.io.FileReader;
36 import java.io.IOException;
37 import java.io.PrintStream;
38 import java.util.ArrayList;
39 import java.util.HashSet;
40
41 /**
42 * Takes a tree with a complete jump history annotated to its branches and makes new internal nodes of degree 1
43 * corresponding to the jump points.
44 *
45 * @author Matthew Hall
46 */
47 public class BranchJumpPlotter {
48
49 private NexusImporter treesIn;
50 private NexusExporter treesOut;
51 private String traitName;
52
53 public BranchJumpPlotter(NexusImporter treesIn, NexusExporter treesOut, String traitName){
54 this.treesIn = treesIn;
55 this.treesOut = treesOut;
56 this.traitName = traitName;
57 }
58
59 private FlexibleTree rewireTree(Tree tree, boolean verbose){
60 int totalJumps = 0;
61 FlexibleTree outTree = new FlexibleTree(tree, true);
62 for(int nodeNo = 0; nodeNo < outTree.getNodeCount(); nodeNo++){
63
64 FlexibleNode node = (FlexibleNode)outTree.getNode(nodeNo);
65 String finalHost = (String)node.getAttribute(traitName);
66 node.setAttribute(traitName,finalHost.replaceAll("\"",""));
67 Object[] jumps = readCJH(node);
68 if(verbose){
69 System.out.print("Node "+nodeNo+": ");
70 }
71
72 if(jumps != null){
73
74 FlexibleNode needsNewParent = node;
75 Double height = tree.getNodeHeight(node);
76
77 for (int i = jumps.length-1; i>=0; i--) {
78 totalJumps++;
79 Object[] jump = (Object[])jumps[i];
80 if(i<jumps.length-1 && (Double)jump[1] <= height){
81 throw new RuntimeException("Jumps do not appear to be in descending order of height");
82 }
83 height = (Double)jump[1];
84
85 if(!needsNewParent.getAttribute(traitName).equals(jump[3])){
86 throw new RuntimeException("Destination traits do not match");
87 }
88 FlexibleNode parent = (FlexibleNode)outTree.getParent(needsNewParent);
89 outTree.beginTreeEdit();
90 outTree.removeChild(parent, needsNewParent);
91
92
93
94 needsNewParent.setLength(height-needsNewParent.getHeight());
95 FlexibleNode jumpNode = new FlexibleNode();
96 jumpNode.setHeight(height);
97 jumpNode.setLength(parent.getHeight() - height);
98 jumpNode.setAttribute(traitName,jump[2]);
99 outTree.addChild(parent, jumpNode);
100 outTree.addChild(jumpNode, needsNewParent);
101 outTree.endTreeEdit();
102 needsNewParent = jumpNode;
103 }
104 }
105 if(verbose){
106 if(jumps==null){
107 System.out.println(0+" ("+totalJumps+")");
108 } else {
109 System.out.println(jumps.length+" ("+totalJumps+")");
110 }
111 }
112 }
113
114 outTree = new FlexibleTree((FlexibleNode)outTree.getRoot());
115 if(verbose){
116 System.out.println("Total jumps: "+totalJumps);
117 int[] childCounts = new int[3];
118 for(int i=0; i<outTree.getNodeCount(); i++){
119 childCounts[outTree.getChildCount(outTree.getNode(i))]++;
120 }
121 for(int i=0; i<3; i++){
122 System.out.println(childCounts[i]+" nodes have "+i+" children");
123 }
124 }
125 return outTree;
126 }
127
128 private Object[] readCJH(FlexibleNode node){
129 if(node.getAttribute("history_all")!=null){
130 HashSet<String[]> out = new HashSet<String[]>();
131 Object[] cjh = (Object[])node.getAttribute("history_all");
132 return cjh;
133 } else {
134 return null;
135 }
136 }
137
138 private void translateTreeFile(){
139 try{
140 ArrayList<Tree> trees = new ArrayList<Tree>();
141 int count = 1;
142 while(treesIn.hasTree()){
143 System.out.println("Doing tree "+count);
144
145 trees.add(rewireTree(treesIn.importNextTree(),true));
146 count++;
147 System.out.println();
148 }
149 Tree[] treeArray = trees.toArray(new Tree[trees.size()]);
150 treesOut.exportTrees(treeArray);
151 } catch(IOException e){
152 System.out.println("Problem reading file ("+e.toString()+")");
153 } catch(Importer.ImportException e){
154 System.out.println("Problem importing trees ("+e.toString()+")");
155 }
156 }
157
158 public static void main(String[] args){
159 try{
160 String traitName = args[0];
161 String inputFile = args[1];
162 String outputFile = args[2];
163 NexusImporter importer = new NexusImporter(new FileReader(inputFile));
164 NexusExporter exporter = new NexusExporter(new PrintStream(outputFile));
165 BranchJumpPlotter plotter = new BranchJumpPlotter(importer,exporter,traitName);
166 plotter.translateTreeFile();
167 } catch(FileNotFoundException e){
168 System.out.println("File not found");
169 }
170
171 }
172
173
174
175 }