Debian Codegraph @

src/dr/evoxml/util/GraphMLUtils.java

raw symbols SCIP
1 /*
2 * GraphMLUtils.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.evoxml.util;
27
28 import dr.evomodel.arg.ARGModel;
29 import org.jdom.Attribute;
30 import org.jdom.DataConversionException;
31 import org.jdom.Element;
32
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36
37 /**
38 * @author Marc Suchard
39 */
40 public class GraphMLUtils {
41
42 public static final String GRAPH_NAME = "digraph";
43 public static final String SPACE = " ";
44 public static final String NEW_LINE = "\n";
45 public static final String START_SECTION = "{";
46 public static final String END_SECTION = "}";
47 public static final String START_ATTRIBUTE = "[";
48 public static final String END_ATTRIBUTE = "]";
49 public static final String NEXT_ATTRIBUTE = ",";
50 public static final String TAB = "\t";
51 public static final String END_LINE = ";\n";
52
53 public static final boolean printLengths = false;
54
55 private static void space(StringBuilder sb) {
56 sb.append(SPACE);
57 }
58
59
60 private static void endLine(StringBuilder sb) {
61 sb.append(END_LINE);
62 }
63
64 private static void newLine(StringBuilder sb) {
65 sb.append(NEW_LINE);
66 }
67
68 private static void tab(StringBuilder sb) {
69 sb.append(TAB);
70 }
71
72 private static void startSection(StringBuilder sb) {
73 sb.append(START_SECTION);
74 }
75
76 private static void endSection(StringBuilder sb) {
77 sb.append(END_SECTION);
78 }
79
80 private static void startAttribute(StringBuilder sb) {
81 sb.append(START_ATTRIBUTE);
82 }
83
84 private static void endAttribute(StringBuilder sb) {
85 sb.append(END_ATTRIBUTE);
86 }
87
88 private static void nextAttribute(StringBuilder sb) {
89 sb.append(NEXT_ATTRIBUTE);
90 }
91
92 public static String dotFormat(Element graphElement) {
93 StringBuilder sb = new StringBuilder();
94 String graphName = graphElement.getAttributeValue(ARGModel.ID_ATTRIBUTE);
95 sb.append(GRAPH_NAME);
96 space(sb);
97 if (graphName != null) {
98 sb.append(graphName);
99 space(sb);
100 }
101 startSection(sb);
102 newLine(sb);
103 tab(sb);
104 sb.append(ARGModel.GRAPH_SIZE);
105 endLine(sb);
106 // newLine(sb);
107 tab(sb);
108 sb.append(ARGModel.DOT_EDGE_DEF);
109 endLine(sb);
110 // newLine(sb);
111 tab(sb);
112 sb.append(ARGModel.DOT_NODE_DEF);
113 endLine(sb);
114 // Fill in graph details
115 List<Element> nodeList = graphElement.getChildren(ARGModel.NODE_ELEMENT);
116 List<String> tipNames = new ArrayList<String>();
117 for (Element nodeElement : nodeList) {
118 String nodeName = nodeElement.getAttributeValue(ARGModel.ID_ATTRIBUTE);
119 tab(sb);
120 sb.append(nodeName);
121 List<Attribute> attributes = nodeElement.getAttributes();
122 int cnt = 1;
123 boolean started = false;
124 boolean isTip = false;
125 int length = attributes.size();
126 for (Attribute attribute : attributes) {
127 String name = attribute.getName();
128 if (name.compareTo(ARGModel.ID_ATTRIBUTE) != 0) {
129 if (name.compareTo(ARGModel.IS_TIP) == 0) {
130 isTip = true;
131 try {
132 if (attribute.getBooleanValue()) {
133 tipNames.add(nodeName);
134 }
135 } catch (DataConversionException e) {
136 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
137 }
138 cnt++;
139 } else {
140 if (!ignore(name)) {
141 if (!started) {
142 space(sb);
143 startAttribute(sb);
144 started = true;
145 } else
146 nextAttribute(sb);
147 sb.append(translate(name) + "=" + attribute.getValue());
148 cnt++;
149 // if (cnt < length)
150 // nextAttribute(sb);
151 // else {
152
153 // }
154 }
155 }
156 }
157 }
158 if (!isTip) {
159 if (!started) {
160 space(sb);
161 startAttribute(sb);
162 started = true;
163 } else {
164 nextAttribute(sb);
165 }
166 sb.append("label=\"\",shape=circle,height=0.02,width=0.2,fontsize=1");
167
168
169 }
170 if (started) {
171 endAttribute(sb);
172
173
174 }
175 // if (!isTip) {
176 //
177 // }
178 endLine(sb);
179 }
180 List<Element> edgeList = graphElement.getChildren(ARGModel.EDGE_ELEMENT);
181 for (Element edgeElement : edgeList) {
182 String fromName = edgeElement.getAttributeValue(ARGModel.EDGE_FROM);
183 String toName = edgeElement.getAttributeValue(ARGModel.EDGE_TO);
184 tab(sb);
185 sb.append(fromName + " -> " + toName);
186 String edgeLength = edgeElement.getAttributeValue(ARGModel.EDGE_LENGTH);
187 if (edgeLength != null && printLengths) {
188 space(sb);
189 startAttribute(sb);
190 sb.append(ARGModel.EDGE_LENGTH + "=" + edgeLength + ",weight=1000.0");
191 endAttribute(sb);
192 }
193 String partitions = edgeElement.getAttributeValue(ARGModel.EDGE_PARTITIONS);
194 if (partitions != null) {
195 space(sb);
196 startAttribute(sb);
197 sb.append("label=\"" + partitions + "\"");
198 endAttribute(sb);
199 }
200 endLine(sb);
201 }
202 //newLine(sb);
203 if (tipNames.size() > 0) {
204 tab(sb);
205 startSection(sb);
206 sb.append("rank=same;");
207 for (String name : tipNames) {
208 space(sb);
209 sb.append(name);
210 }
211 endSection(sb);
212 newLine(sb);
213 }
214
215 endSection(sb);
216 return sb.toString();
217 }
218
219 private static class TranslationMap extends HashMap<String, String> {
220
221 TranslationMap() {
222 put("taxonName", "label");
223 }
224
225 }
226
227 private static class IgnoreList extends ArrayList<String> {
228 IgnoreList() {
229 add("nodeHeight");
230 add("isRoot");
231 // add()
232 }
233 }
234
235 private static TranslationMap translation = new TranslationMap();
236
237 private static IgnoreList ignoreList = new IgnoreList();
238
239 private static String translate(String text) {
240 String newText = null;
241 if ((newText = translation.get(text)) == null)
242 return text;
243 return newText;
244 // if ()
245 // System.err.println("t size = "+translation.size());
246 // return text;
247 }
248
249 private static boolean ignore(String text) {
250 return ignoreList.contains(text);
251 }
252
253 }