Team:Freiburg software/Code/BioBrickManager.java

From 2009.igem.org


/*
    Copyright: synbiowave
     
    License: GPL
     
    Authors: Jörg Wassolesk
     
    Version: 0.1 
    
    DESCRIPTION:
    	This class is used as main class for managing BioBrick DAS connections.
*/

package org.synbiowave.biobrick;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.zip.GZIPInputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.XMLReader;

// TODO: Auto-generated Javadoc
/**
 * The Class BioBrickManager.
 */
public class BioBrickManager{

	/**
	 * Instantiates a new bio brick manager.
	 */
	public BioBrickManager(){}
	
	/**
	 * Read bio brick features.
	 * 
	 * @param id the id
	 * 
	 * @return the list< map< string, string>>
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws SAXException the SAX exception
	 */
	public static List<Map<String,String>> readBioBrickFeatures(String id) throws IOException,SAXException{
		String url="http://partsregistry.org/das/parts/features/?segment="+id;
		InputStream stream=getBioBrickStream(url);
		XMLReader xmlreader=getBioBrickReader();
		BioBrick_DAS_Feature_Handler cont_handle=new BioBrick_DAS_Feature_Handler();
		xmlreader.setContentHandler(cont_handle);
		xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
		InputSource insource=new InputSource();
		insource.setByteStream(stream);
		xmlreader.parse(insource);
		return cont_handle.get_features();}
	
	/**
	 * Read bio brick sequence.
	 * 
	 * @param id the id
	 * 
	 * @return the string
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws SAXException the SAX exception
	 */
	public static String readBioBrickSequence(String id) throws IOException,SAXException{
		String url="http://partsregistry.org/das/parts/dna/?segment="+id;
		InputStream stream=getBioBrickStream(url);
		XMLReader xmlreader=getBioBrickReader();
		BioBrick_DAS_DNA_Handler cont_handle=new BioBrick_DAS_DNA_Handler();
		xmlreader.setContentHandler(cont_handle);
		xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
		InputSource insource=new InputSource();
		insource.setByteStream(stream);
		xmlreader.parse(insource);
		return cont_handle.get_sequence();}

	/**
	 * Read bio brick i ds.
	 * 
	 * @return the tree set< string>
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws SAXException the SAX exception
	 */
	public static TreeSet<String> readBioBrickIDs() throws IOException,SAXException{
		String url="http://partsregistry.org/das/parts/entry_points/";
		InputStream stream=getBioBrickStream(url);
		XMLReader xmlreader=getBioBrickReader();
		BioBrick_DAS_Entry_Points_Handler cont_handle=new BioBrick_DAS_Entry_Points_Handler();
		xmlreader.setContentHandler(cont_handle);
		xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
		InputSource insource=new InputSource();
		insource.setByteStream(stream);
		xmlreader.parse(insource);    
		return cont_handle.getIDs();}
	
	/**
	 * Gets the bio brick stream.
	 * 
	 * @param urlPath the url path
	 * 
	 * @return the bio brick stream
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static InputStream getBioBrickStream(String urlPath) throws IOException{
		URL url=new URL(urlPath);
		HttpURLConnection huc=(HttpURLConnection)url.openConnection();
		huc.setRequestProperty("Accept-Encoding","gzip");
		String contentEncoding=huc.getContentEncoding();
		InputStream stream=huc.getInputStream();
		if(contentEncoding!=null){
			if(contentEncoding.indexOf("gzip")!=-1){stream=new GZIPInputStream(stream);}}
		return stream;}

	/**
	 * Gets the bio brick reader.
	 * 
	 * @return the bio brick reader
	 * 
	 * @throws SAXException the SAX exception
	 */
	private static XMLReader getBioBrickReader() throws SAXException{
		SAXParserFactory spfactory=SAXParserFactory.newInstance();
		spfactory.setValidating(false);
		SAXParser saxParser = null ;
		try{saxParser=spfactory.newSAXParser();}
		catch(ParserConfigurationException e){System.out.println(e.toString());}
		String vali=System.getProperty("XMLVALIDATION");
		boolean validation=false ;
		if(vali!=null){if(vali.equals("true")){validation=true;}}
		XMLReader xmlreader=saxParser.getXMLReader();
		try{xmlreader.setFeature("http://xml.org/sax/features/validation",validation);}
		catch(SAXException e){System.out.println(e.toString());}
		try{xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",validation);}
		catch(SAXNotRecognizedException e){System.out.println(e.toString());}
		return xmlreader;}
}