Debian Codegraph @

uk/ac/babraham/FastQC/Report/HTMLReportArchive.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC 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 * FastQC 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 FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package uk.ac.babraham.FastQC.Report;
21
22 import java.awt.image.BufferedImage;
23 import java.io.BufferedInputStream;
24 import java.io.BufferedOutputStream;
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.PrintWriter;
31 import java.io.StringReader;
32 import java.io.StringWriter;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35 import java.util.Enumeration;
36 import java.util.zip.ZipEntry;
37 import java.util.zip.ZipFile;
38 import java.util.zip.ZipOutputStream;
39
40 import javax.imageio.ImageIO;
41 import javax.xml.parsers.DocumentBuilder;
42 import javax.xml.parsers.DocumentBuilderFactory;
43 import javax.xml.stream.XMLOutputFactory;
44 import javax.xml.stream.XMLStreamException;
45 import javax.xml.stream.XMLStreamWriter;
46 import javax.xml.transform.Templates;
47 import javax.xml.transform.TransformerFactory;
48 import javax.xml.transform.dom.DOMSource;
49 import javax.xml.transform.stream.StreamResult;
50
51 import org.w3c.dom.Document;
52 import org.xml.sax.InputSource;
53
54 import uk.ac.babraham.FastQC.FastQCConfig;
55 import uk.ac.babraham.FastQC.FastQCApplication;
56 import uk.ac.babraham.FastQC.Modules.QCModule;
57 import uk.ac.babraham.FastQC.Sequence.SequenceFile;
58 import uk.ac.babraham.FastQC.Utilities.ImageToBase64;
59
60 public class HTMLReportArchive {
61 private XMLStreamWriter xhtml=null;
62 private StringBuffer data = new StringBuffer();
63 private QCModule [] modules;
64 private ZipOutputStream zip;
65 private SequenceFile sequenceFile;
66 private byte [] buffer = new byte[1024];
67 private File htmlFile;
68 private File zipFile;
69
70 public HTMLReportArchive (SequenceFile sequenceFile, QCModule [] modules, File htmlFile) throws IOException, XMLStreamException {
71 this.sequenceFile = sequenceFile;
72 this.modules = modules;
73 this.htmlFile = htmlFile;
74 this.zipFile = new File(htmlFile.getAbsoluteFile().toString().replaceAll("\\.html$", "")+".zip");
75 StringWriter htmlStr = new StringWriter();
76 XMLOutputFactory xmlfactory = XMLOutputFactory.newInstance();
77 this.xhtml= xmlfactory.createXMLStreamWriter(htmlStr);
78
79
80 zip = new ZipOutputStream(new FileOutputStream(zipFile));
81 zip.putNextEntry(new ZipEntry(folderName()+"/"));
82 zip.putNextEntry(new ZipEntry(folderName()+"/Icons/"));
83 zip.putNextEntry(new ZipEntry(folderName()+"/Images/"));
84 startDocument();
85 for (int m=0;m<modules.length;m++) {
86
87 if (modules[m].ignoreInReport()) continue;
88
89 xhtml.writeStartElement("div");
90 xhtml.writeAttribute("class", "module");
91 xhtml.writeStartElement("h2");
92 xhtml.writeAttribute("id", "M"+m);
93
94
95 // Add an icon before the module name
96 if (modules[m].raisesError())
97 {
98 xhtml.writeEmptyElement("img");
99 xhtml.writeAttribute("src",base64ForIcon("Icons/error.png"));
100 xhtml.writeAttribute("alt","[FAIL]");
101 }
102
103 else if (modules[m].raisesWarning())
104 {
105 xhtml.writeEmptyElement("img");
106 xhtml.writeAttribute("src",base64ForIcon("Icons/warning.png"));
107 xhtml.writeAttribute("alt","[WARN]");
108 }
109 else {
110 xhtml.writeEmptyElement("img");
111 xhtml.writeAttribute("src",base64ForIcon("Icons/tick.png"));
112 xhtml.writeAttribute("alt","[OK]");
113 }
114
115
116 xhtml.writeCharacters(modules[m].name());
117 data.append(">>");
118 data.append(modules[m].name());
119 data.append("\t");
120 if (modules[m].raisesError()) {
121 data.append("fail");
122 }
123 else if (modules[m].raisesWarning()) {
124 data.append("warn");
125 }
126 else {
127 data.append("pass");
128 }
129 data.append("\n");
130 xhtml.writeEndElement();
131 modules[m].makeReport(this);
132 data.append(">>END_MODULE\n");
133
134 xhtml.writeEndElement();
135 }
136 closeDocument();
137
138 zip.putNextEntry(new ZipEntry(folderName()+"/fastqc_report.html"));
139 xhtml.flush();
140 xhtml.close();
141 zip.write(htmlStr.toString().getBytes());
142 zip.closeEntry();
143 zip.putNextEntry(new ZipEntry(folderName()+"/fastqc_data.txt"));
144 zip.write(data.toString().getBytes());
145 zip.closeEntry();
146
147 //XSL-FO
148 try {
149 DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
150 domFactory.setNamespaceAware(false);
151 DocumentBuilder builder=domFactory.newDocumentBuilder();
152 Document src=builder.parse(new InputSource( new StringReader(htmlStr.toString())));
153 InputStream rsrc=getClass().getResourceAsStream("/Templates/fastqc2fo.xsl");
154 if(rsrc!=null)
155 {
156 domFactory.setNamespaceAware(true);
157 builder=domFactory.newDocumentBuilder();
158 Document html2fo=builder.parse(rsrc);
159 rsrc.close();
160
161 TransformerFactory tf=TransformerFactory.newInstance();
162 Templates templates=tf.newTemplates(new DOMSource(html2fo));
163 zip.putNextEntry(new ZipEntry(folderName()+"/fastqc.fo"));
164 templates.newTransformer().transform(new DOMSource(src), new StreamResult(zip));
165 zip.closeEntry();
166 }
167 }
168 catch (Exception e) {
169 e.printStackTrace();
170 }
171
172
173 zip.close();
174
175 // Save the HTML file at the same level as the zip file
176
177 PrintWriter pr = new PrintWriter(new FileWriter(htmlFile));
178
179 pr.print(htmlStr.toString());
180
181 pr.close();
182
183
184 if (FastQCConfig.getInstance().do_unzip) {
185 unzipZipFile(zipFile);
186 if (FastQCConfig.getInstance().delete_after_unzip) {
187 zipFile.delete();
188 }
189 }
190 }
191
192 private void unzipZipFile (File file) throws IOException {
193 ZipFile zipFile = new ZipFile(file);
194 Enumeration<? extends ZipEntry> entries = zipFile.entries();
195 int size;
196 byte [] buffer = new byte[1024];
197
198 while (entries.hasMoreElements()) {
199 ZipEntry entry = entries.nextElement();
200
201 // System.out.println("Going to extract '"+entry.getName()+"'");
202
203 if (entry.isDirectory()) {
204 File dir = new File(file.getParent()+"/"+entry.getName());
205 if (dir.exists() && dir.isDirectory()) continue; // Don't need to do anything
206 if (dir.exists() && ! dir.isDirectory()) throw new IOException ("File exists with dir name "+dir.getName());
207 if (!dir.mkdir()) throw new IOException("Failed to make dir for "+dir.getName());
208 continue;
209 }
210
211 BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
212 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getParent()+"/"+entry.getName()),buffer.length);
213 while ((size = bis.read(buffer,0,buffer.length)) != -1) {
214 bos.write(buffer,0,size);
215 }
216 bos.flush();
217 bos.close();
218 bis.close();
219 }
220
221 zipFile.close();
222 }
223
224 public XMLStreamWriter xhtmlStream ()
225 {
226 return this.xhtml;
227 }
228
229 public StringBuffer dataDocument() {
230 return data;
231 }
232
233 public String folderName () {
234 return htmlFile.getName().replaceAll("\\.html$", "");
235 }
236
237 public ZipOutputStream zipFile () {
238 return zip;
239 }
240
241 private void startDocument () throws IOException,XMLStreamException
242 {
243
244 // Just put the fastQC version at the start of the text report
245 data.append("##FastQC\t");
246 data.append(FastQCApplication.VERSION);
247 data.append("\n");
248
249 // Add in the icon files for pass/fail/warn
250 for(String icnName:new String[]{
251 "fastqc_icon.png",
252 "warning.png",
253 "error.png",
254 "tick.png"})
255 {
256 InputStream in =getClass().getResourceAsStream("/Templates/Icons/"+icnName);
257 if(in==null) continue;
258 zip.putNextEntry(new ZipEntry(folderName()+"/Icons/"+icnName));
259 int len;
260 while ((len = in.read(buffer)) > 0) {
261 zip.write(buffer, 0, len);
262 }
263 in.close();
264 zip.closeEntry();
265 }
266
267
268
269 SimpleDateFormat df = new SimpleDateFormat("EEE d MMM yyyy");
270
271 xhtml.writeDTD("<!DOCTYPE html>");
272 xhtml.writeStartElement("html");
273 xhtml.writeStartElement("head");
274
275 xhtml.writeStartElement("title");
276 xhtml.writeCharacters(sequenceFile.name());
277 xhtml.writeCharacters(" FastQC Report");
278 xhtml.writeEndElement();//title
279
280 InputStream rsrc=getClass().getResourceAsStream("/Templates/header_template.html");
281 if(rsrc!=null)
282 {
283 xhtml.writeStartElement("style");
284 xhtml.writeAttribute("type", "text/css");
285
286 byte array[]=new byte[128];
287 int nRead;
288 while((nRead=rsrc.read(array))!=-1) { xhtml.writeCharacters(new String(array,0,nRead));}
289 rsrc.close();
290 xhtml.writeEndElement();//style
291 }
292
293
294
295
296 xhtml.writeEndElement();//head
297
298 xhtml.writeStartElement("body");
299
300 xhtml.writeStartElement("div");
301 xhtml.writeAttribute("class", "header");
302
303 xhtml.writeStartElement("div");
304 xhtml.writeAttribute("id", "header_title");
305
306 xhtml.writeEmptyElement("img");
307 xhtml.writeAttribute("src", base64ForIcon("Icons/fastqc_icon.png"));
308 xhtml.writeAttribute("alt", "FastQC");
309 xhtml.writeCharacters("FastQC Report");
310 xhtml.writeEndElement();//div
311
312 xhtml.writeStartElement("div");
313 xhtml.writeAttribute("id", "header_filename");
314 xhtml.writeCharacters(df.format(new Date()));
315 xhtml.writeEmptyElement("br");
316 xhtml.writeCharacters(sequenceFile.name());
317 xhtml.writeEndElement();//div
318 xhtml.writeEndElement();//div
319
320
321 xhtml.writeStartElement("div");
322 xhtml.writeAttribute("class", "summary");
323
324 xhtml.writeStartElement("h2");
325 xhtml.writeCharacters("Summary");
326 xhtml.writeEndElement();//h2
327
328
329 xhtml.writeStartElement("ul");
330
331 StringBuffer summaryText = new StringBuffer();
332
333 for (int m=0;m<modules.length;m++) {
334
335 if (modules[m].ignoreInReport()) {
336 continue;
337 }
338 xhtml.writeStartElement("li");
339 xhtml.writeEmptyElement("img");
340 if (modules[m].raisesError()) {
341 xhtml.writeAttribute("src", base64ForIcon("Icons/error.png"));
342 xhtml.writeAttribute("alt", "[FAIL]");
343 summaryText.append("FAIL");
344 }
345 else if (modules[m].raisesWarning()) {
346 xhtml.writeAttribute("src", base64ForIcon("Icons/warning.png"));
347 xhtml.writeAttribute("alt", "[WARNING]");
348 summaryText.append("WARN");
349 }
350 else {
351 xhtml.writeAttribute("src", base64ForIcon("Icons/tick.png"));
352 xhtml.writeAttribute("alt", "[PASS]");
353 summaryText.append("PASS");
354 }
355 summaryText.append("\t");
356 summaryText.append(modules[m].name());
357 summaryText.append("\t");
358 summaryText.append(sequenceFile.name());
359 summaryText.append(FastQCConfig.getInstance().lineSeparator);
360
361 xhtml.writeStartElement("a");
362 xhtml.writeAttribute("href", "#M"+m);
363 xhtml.writeCharacters(modules[m].name());
364 xhtml.writeEndElement();//a
365 xhtml.writeEndElement();//li
366
367
368 }
369 xhtml.writeEndElement();//ul
370 xhtml.writeEndElement();//div
371
372 xhtml.writeStartElement("div");
373 xhtml.writeAttribute("class", "main");
374
375
376 zip.putNextEntry(new ZipEntry(folderName()+"/summary.txt"));
377 zip.write(summaryText.toString().getBytes());
378
379 }
380
381 private String base64ForIcon (String path) {
382 try {
383 BufferedImage b = ImageIO.read(ClassLoader.getSystemResource("Templates/"+path));
384 return (ImageToBase64.imageToBase64(b));
385 }
386 catch (IOException ioe) {
387 ioe.printStackTrace();
388 return "Failed";
389 }
390 }
391
392 private void closeDocument () throws XMLStreamException
393 {
394 xhtml.writeEndElement();//div
395 xhtml.writeStartElement("div");
396 xhtml.writeAttribute("class", "footer");
397 xhtml.writeCharacters("Produced by ");
398 xhtml.writeStartElement("a");
399 xhtml.writeAttribute("href", "http://www.bioinformatics.babraham.ac.uk/projects/fastqc/");
400 xhtml.writeCharacters("FastQC");
401 xhtml.writeEndElement();//a
402 xhtml.writeCharacters(" (version "+FastQCApplication.VERSION+")");
403 xhtml.writeEndElement();//div
404
405 xhtml.writeEndElement();//body
406 xhtml.writeEndElement();//html
407 }
408
409
410
411
412 }