comparison NGSrich_0.5.5/src/middlewares/XMLSummaryFileBuilder.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:89ad0a9cca52
1 package middlewares;
2
3 import java.util.*;
4 import java.io.*;
5 import org.jdom.*;
6 import org.jdom.input.*;
7 import org.jdom.output.*;
8
9 /**
10 * Used to construct the summary file based on the computation of an
11 * EnrichmentStatsComputer object. It uses a file pattern to generate this file
12 * dynamically.
13 *
14 * @author Ali Abdallah
15 * @version 20.07.2011
16 * @since Java 1.6
17 */
18
19 public class XMLSummaryFileBuilder {
20
21 /**
22 * The pattern file containing the structure of the summary without real
23 * data.
24 */
25 static String xmlPatternFile;
26
27 /**
28 * The summary file containing the computed data.
29 */
30 static String xmlOutputFile;
31
32 /**
33 * An object with an abstract representation on an xml-file.
34 */
35 static Document doc;
36
37 /**
38 * The leaf-tags containing data as text.
39 */
40 List<Element> leafs;
41
42 /**
43 * The name of the current sample.
44 */
45 static String currSample;
46
47 /**
48 * Creates and initializes a XMLSummaryFileBuilder object.
49 *
50 * @param xmlPatternFile the pattern file.
51 * @param xmlOutputFile the output file.
52 * @param currSample the current read alignment sample name without
53 * the extension.
54 */
55 public XMLSummaryFileBuilder(String xmlPatternFile, String xmlOutputFile,
56 String currSample) {
57
58 leafs = new ArrayList<Element>();
59 XMLSummaryFileBuilder.xmlPatternFile = xmlPatternFile;
60 XMLSummaryFileBuilder.xmlOutputFile = xmlOutputFile;
61 XMLSummaryFileBuilder.currSample = currSample;
62 SAXBuilder builder = new SAXBuilder();
63 try {
64 doc = builder.build(xmlPatternFile);
65 } catch (JDOMException e) {
66 e.printStackTrace();
67 } catch (IOException e) {
68 e.printStackTrace();
69 }
70 }
71
72 /**
73 * Same as the constructor above but the pattern file is internally
74 * specified.
75 *
76 * @param xmlOutputFile the output file.
77 * @param currSample the current read alignment sample name without
78 * the extension.
79 */
80 public XMLSummaryFileBuilder(String xmlOutputFile,
81 String currSample) {
82 this(Misc.binDir()+"/xmlFilePattern.xml", xmlOutputFile, currSample);
83 }
84
85 /**
86 * Computes all the leafs of the subtree with root e.
87 *
88 * @param e an Element.
89 * @return all the leafs of the subtree with root e.
90 */
91 public Element[] getLeafs(Element e){
92 @SuppressWarnings("unchecked")
93 List<Element> children = e.getChildren();
94 for(int i = 0; i < children.size(); i++){
95 Element child = children.get(i);
96 if(child.getChildren().size() == 0)
97 leafs.add(child);
98 else
99 getLeafs(child);
100 }
101
102 Object[] oleafs = leafs.toArray();
103 Element[] eleafs = new Element[oleafs.length];
104 for(int i = 0; i < eleafs.length; i++)
105 eleafs[i] = (Element)oleafs[i];
106
107 return eleafs;
108 }
109
110 /**
111 * Same as the above method but the element is internally specified as the
112 * root of the document.
113 *
114 * @return an array with all leaf elements of the document.
115 */
116 public Element[] getLeafs(){
117 return getLeafs(doc.getRootElement());
118 }
119
120 /**
121 * Write the summary file created.
122 * @param name the name of the file.
123 */
124 public void writeXMLFile(String name) {
125 XMLOutputter outp = new XMLOutputter();
126 outp.setFormat(Format.getPrettyFormat());
127 try {
128 outp.output(doc, new FileOutputStream(name));
129 } catch (FileNotFoundException e) {
130 e.printStackTrace();
131 } catch (IOException e) {
132 e.printStackTrace();
133 }
134 }
135
136 }