Merge branch 'Jalview-JS/jim/JAL-3253-JAL-3418' into Jalview-JS/JAL-3253-applet
[jalview.git] / srcjar / fr / orsay / lri / varna / models / treealign / TreeGraphviz.java
1 package fr.orsay.lri.varna.models.treealign;
2 import java.io.BufferedReader;
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8
9
10
11 /**
12  * This class translates a Tree to a graphviz file.
13  * @author Raphael Champeimont
14  *
15  * @param <? extends GraphvizDrawableNodeValue> the type of values in the tree
16  */
17 public class TreeGraphviz {
18
19         /**
20          * Generates a PostScript file using graphviz.
21          * The dot command must be available.
22          */
23         public static void treeToGraphvizPostscript(Tree<? extends GraphvizDrawableNodeValue> tree, String filename, String title) throws IOException {
24                 // generate graphviz source
25                 String graphvizSource = treeToGraphviz(tree, title);
26                 
27                 // open output file
28                 BufferedWriter fbw;
29                 fbw = new BufferedWriter(new FileWriter(filename));
30         
31         // execute graphviz
32                 Process proc = Runtime.getRuntime().exec("dot -Tps");
33         BufferedWriter bw  = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
34         BufferedReader br  = new BufferedReader(new InputStreamReader(proc.getInputStream()));
35         BufferedReader bre = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
36         bw.write(graphvizSource);
37         bw.close();
38         {
39                 String line = null;
40             while ((line = br.readLine()) != null) {
41                 fbw.write(line + "\n");
42             }
43         }
44         {
45                 String line = null;
46             while ((line = bre.readLine()) != null) {
47                 System.err.println(line);
48             }
49         }
50         
51         // wait for graphviz to end
52         try {
53                         proc.waitFor();
54                 } catch (InterruptedException e) {
55                         e.printStackTrace();
56                 }
57
58         // close file
59                 fbw.close();
60         }
61         
62         /**
63          * Like treeToGraphvizPostscript(Tree,String,String) but with the title
64          * equal to the filename.
65          */
66         public static void treeToGraphvizPostscript(Tree<? extends GraphvizDrawableNodeValue> tree, String filename) throws IOException {
67                 treeToGraphvizPostscript(tree, filename, filename);
68         }
69         
70         /**
71          * Creates a graphviz source file from a Tree.
72          * @param title the title of the graph
73          */
74         public static void treeToGraphvizFile(Tree<? extends GraphvizDrawableNodeValue> tree, String filename, String title) throws IOException {
75                 BufferedWriter bw;
76                 bw = new BufferedWriter(new FileWriter(filename));
77         bw.write(treeToGraphviz(tree, filename));
78         bw.close();
79         }
80         
81         /**
82          * Like treeToGraphvizFile(Tree,String,String) but with the title
83          * equal to the filename.
84          */
85         public static void treeToGraphvizFile(Tree<? extends GraphvizDrawableNodeValue> tree, String filename) throws IOException {
86                 treeToGraphvizFile(tree, filename, filename);
87         }
88         
89         /**
90          * Creates a graphviz source from a Tree.
91          * @param title the title of the graph
92          */
93         public static String treeToGraphviz(Tree<? extends GraphvizDrawableNodeValue> tree, String title) {
94                 return "digraph \"" + title + "\" {\n" + subtreeToGraphviz(tree) + "}\n";
95         }
96         
97         private static String subtreeToGraphviz(Tree<? extends GraphvizDrawableNodeValue> tree) {
98                 String s = "";
99                 String myId = tree.toGraphvizNodeId();
100                 
101                 s +=
102                         "\""
103                         + myId
104                         + "\" [label=\""
105                         + ((tree.getValue() != null) ? tree.getValue().toGraphvizNodeName() : "null")
106                         + "\"]\n";
107                 for (Tree<? extends GraphvizDrawableNodeValue> child: tree.getChildren()) {
108                         s += "\"" + myId + "\" -> \"" + child.toGraphvizNodeId() + "\"\n";
109                         s += subtreeToGraphviz(child);
110                 }
111                 
112                 return s;
113         }
114 }