diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NGSrich_0.5.5/src/middlewares/XMLSummaryFileBuilder.java	Mon Nov 21 08:12:19 2011 -0500
@@ -0,0 +1,136 @@
+package middlewares;
+
+import java.util.*;
+import java.io.*;
+import org.jdom.*;
+import org.jdom.input.*;
+import org.jdom.output.*;
+
+/**
+ * Used to construct the summary file based on the computation of an 
+ * EnrichmentStatsComputer object. It uses a file pattern to generate this file
+ * dynamically.
+ * 
+ * @author Ali Abdallah
+ * @version 20.07.2011
+ * @since Java 1.6
+ */
+
+public class XMLSummaryFileBuilder {
+	
+	/**
+	 * The pattern file containing the structure of the summary without real
+	 * data.
+	 */
+	static String xmlPatternFile;
+	
+	/**
+	 * The summary file containing the computed data.
+	 */
+	static String xmlOutputFile;
+	
+	/**
+	 * An object with an abstract representation on an xml-file.
+	 */
+	static Document doc;
+	
+	/**
+	 * The leaf-tags containing data as text.
+	 */
+	List<Element> leafs;
+	
+	/**
+	 * The name of the current sample.  
+	 */
+	static String currSample;
+	
+	/**
+	 * Creates and initializes a XMLSummaryFileBuilder object.
+	 * 
+	 * @param xmlPatternFile the pattern file.
+	 * @param xmlOutputFile the output file.
+	 * @param currSample the current read alignment sample name without 
+	 * the extension.
+	 */
+	public XMLSummaryFileBuilder(String xmlPatternFile, String xmlOutputFile,
+			String currSample) {
+
+		leafs = new ArrayList<Element>();
+		XMLSummaryFileBuilder.xmlPatternFile = xmlPatternFile;
+		XMLSummaryFileBuilder.xmlOutputFile = xmlOutputFile;
+		XMLSummaryFileBuilder.currSample = currSample;
+		SAXBuilder builder = new SAXBuilder();
+		try {
+			doc = builder.build(xmlPatternFile);
+		} catch (JDOMException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * Same as the constructor above but the pattern file is internally 
+	 * specified.
+	 * 
+	 * @param xmlOutputFile the output file.
+	 * @param currSample the current read alignment sample name without 
+	 * the extension.
+	 */
+	public XMLSummaryFileBuilder(String xmlOutputFile,
+			String currSample) {
+		this(Misc.binDir()+"/xmlFilePattern.xml", xmlOutputFile, currSample);
+	}
+
+	/**
+	 * Computes all the leafs of the subtree with root e.
+	 * 
+	 * @param e an Element.
+	 * @return all the leafs of the subtree with root e.
+	 */
+	public Element[] getLeafs(Element e){
+		@SuppressWarnings("unchecked")
+		List<Element> children = e.getChildren();
+		for(int i = 0; i < children.size(); i++){
+			Element child = children.get(i);
+			if(child.getChildren().size() == 0)
+				leafs.add(child);
+			else
+				getLeafs(child);
+		}
+		
+		Object[] oleafs = leafs.toArray();
+		Element[] eleafs = new Element[oleafs.length];
+		for(int i = 0; i < eleafs.length; i++)
+			eleafs[i] = (Element)oleafs[i];
+		
+		return eleafs;
+	}
+	
+	/**
+	 * Same as the above method but the element is internally specified as the
+	 * root of the document.
+	 * 
+	 * @return an array with all leaf elements of the document. 
+	 */
+	public Element[] getLeafs(){
+		return getLeafs(doc.getRootElement());
+	}
+
+	/**
+	 * Write the summary file created.
+	 * @param name the name of the file.
+	 */
+	public void writeXMLFile(String name) {
+		XMLOutputter outp = new XMLOutputter();
+		outp.setFormat(Format.getPrettyFormat());
+		try {
+			outp.output(doc, new FileOutputStream(name));
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+}