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