Debian Codegraph @

edu/mit/compbio/ChromHMM/Util.java

raw symbols SCIP
1
2 /**
3 * ChromHMM - automating chromatin state discovery and characterization
4 * Copyright (C) 2008-2012 Massachusetts Institute of Technology
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20 package edu.mit.compbio.ChromHMM;
21
22 import java.io.*;
23 import java.util.*;
24 import java.util.zip.*;
25
26 import java.awt.*;
27 import java.awt.geom.*;
28 import java.awt.image.*;
29
30 import org.apache.batik.svggen.SVGGeneratorContext;
31 import org.tc33.jheatchart.HeatChart;
32 import org.apache.batik.svggen.SVGGraphics2D;
33 import org.apache.batik.dom.GenericDOMImplementation;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.DOMImplementation;
36
37
38
39 /**
40 * Class contains general utility functions
41 * The ChromHMM code was written by Jason Ernst
42 */
43 public class Util
44 {
45 /**
46 * Returns a buffered reader. If szFile ends in a ".gz" tries to open it as a gzip file
47 * otherwise tries to open it as a normal file.
48 */
49 static BufferedReader getBufferedReader(String szFile) throws IOException
50 {
51 BufferedReader br;
52 if (szFile.endsWith(".gz"))
53 {
54 try
55 {
56 br =new BufferedReader(new InputStreamReader(
57 new GZIPInputStream(new FileInputStream(szFile))));
58 }
59 catch(IOException ioex)
60 {
61 System.out.println("IOException thrown for file "+szFile);
62 throw ioex;
63 }
64 }
65 else
66 {
67 br = new BufferedReader(new FileReader(szFile));
68 }
69 return br;
70 }
71
72 /**
73 * Computes the euclidean distance between the values in xvalues and the values in yvalues
74 */
75 static double euclid(double[] xvalues, double[] yvalues)
76 {
77 double ddist = 0;
78 for (int ni =0; ni < xvalues.length; ni++)
79 {
80 ddist += (xvalues[ni]-yvalues[ni])* (xvalues[ni]-yvalues[ni]);
81 }
82
83 return (Math.sqrt(ddist));
84 }
85
86 /**
87 * Returns the correlation coeffiection between xvalues and yvalues
88 * or 0 if either vector has variance 0
89 */
90 static double correlation(double[] xvalues, double[] yvalues)
91 {
92 double dsumx = 0,
93 dsumy = 0,
94 dsumxsq = 0,
95 dsumysq = 0,
96 dsumxy = 0,
97 dvarx,
98 dvary,
99 dcoeff;
100
101 int numvalues = 0;
102
103 for (int nindex = 0; nindex < xvalues.length; nindex++)
104 {
105 dsumx += xvalues[nindex];
106 dsumy += yvalues[nindex];
107 dsumxsq += xvalues[nindex]*xvalues[nindex];
108 dsumysq += yvalues[nindex]*yvalues[nindex];
109 dsumxy += xvalues[nindex]*yvalues[nindex];
110 }
111 numvalues = xvalues.length;
112
113 if (numvalues==0)
114 {
115 dcoeff = 0;
116 }
117 else
118 {
119 dvarx = dsumxsq - dsumx*dsumx/numvalues;
120 dvary = dsumysq - dsumy*dsumy/numvalues;
121 double dvarxdvary = dvarx*dvary;
122 if (dvarxdvary <= 0)
123 {
124 dcoeff = 0;
125 }
126 else
127 {
128 dcoeff = (dsumxy - dsumx*dsumy/numvalues)/Math.sqrt(dvarxdvary);
129 }
130 }
131 return dcoeff;
132 }
133
134
135 static void printImageToSVG(HeatChart map, String szoutfile) throws IOException
136 {
137
138 File svgFile = new File(szoutfile);
139 //Image imap =
140 map.getChartImage();
141 // Get a DOMImplementation.
142 DOMImplementation domImpl =
143 GenericDOMImplementation.getDOMImplementation();
144 // Create an instance of org.w3c.dom.Document.
145 String svgNS = "http://www.w3.org/2000/svg";
146 Document document = domImpl.createDocument(svgNS, "svg", null);
147 // Create an instance of the SVG Generator.
148
149 SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
150 ctx.setEmbeddedFontsOn(true);
151
152 SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx,true);
153 //svgGenerator = canvas.getGraphics();
154
155 svgGenerator.setColor(Color.white);
156
157 map.getChartGraphics(svgGenerator);
158
159 //svgGenerator.drawImage(imap, new AffineTransform(), null);
160 //svgGenerator.drawImage(imap, new AffineTransform(1f,0f,0f,1f,0,0), null);
161 //bimap1.paintComponent(svgGenerator);
162 // Ask the test to render into the SVG Graphics2D implementation.
163 //TestSVGGen test = new TestSVGGen();
164
165 // Finally, stream out SVG to the standard output using
166 // UTF-8 encoding.
167 boolean useCSS = true; // we want to use CSS style attributes
168 Writer out = new OutputStreamWriter(new FileOutputStream(svgFile) , "UTF-8");
169
170 svgGenerator.stream(out, useCSS);
171
172 }
173 }