new files
authoramwaterhouse <Andrew Waterhouse>
Thu, 15 Jun 2006 14:06:37 +0000 (14:06 +0000)
committeramwaterhouse <Andrew Waterhouse>
Thu, 15 Jun 2006 14:06:37 +0000 (14:06 +0000)
src/org/biojava/dasobert/das2/io/DAS2SourceHandler.java [new file with mode: 0755]
src/org/biojava/dasobert/das2/io/DasSourceReader.java [new file with mode: 0755]
src/org/biojava/dasobert/das2/io/DasSourceReaderImpl.java [new file with mode: 0755]

diff --git a/src/org/biojava/dasobert/das2/io/DAS2SourceHandler.java b/src/org/biojava/dasobert/das2/io/DAS2SourceHandler.java
new file mode 100755 (executable)
index 0000000..810ec33
--- /dev/null
@@ -0,0 +1,167 @@
+/*
+ *                  BioJava development code
+ *
+ * This code may be freely distributed and modified under the
+ * terms of the GNU Lesser General Public Licence.  This should
+ * be distributed with the code.  If you do not have a copy,
+ * see:
+ *
+ *      http://www.gnu.org/copyleft/lesser.html
+ *
+ * Copyright for this code is held jointly by the individual
+ * authors.  These should be listed in @author doc comments.
+ *
+ * For more information on the BioJava project and its aims,
+ * or to join the biojava-l mailing list, visit the home page
+ * at:
+ *
+ *      http://www.biojava.org/
+ * 
+ * Created on Mar 15, 2006
+ *
+ */
+package org.biojava.dasobert.das2.io;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.biojava.dasobert.das2.Das2Capability;
+import org.biojava.dasobert.das2.Das2CapabilityImpl;
+import org.biojava.dasobert.das2.Das2Source;
+import org.biojava.dasobert.das2.Das2SourceImpl;
+import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
+import org.biojava.dasobert.dasregistry.DasSource;
+import org.xml.sax.Attributes;
+import org.xml.sax.helpers.DefaultHandler;
+
+/** a parser for the DAS2 sources response
+ * 
+ * @author Andreas Prlic
+ * @since 6:53:45 PM
+ * @version %I% %G%
+ */
+public class DAS2SourceHandler extends DefaultHandler{
+
+    List sources;
+    Das2Source currentSource;
+    List coordinates;
+    List capabilities;
+    
+    public DAS2SourceHandler() {
+        super();
+        
+        sources = new ArrayList();
+        currentSource = new Das2SourceImpl();
+        coordinates = new ArrayList();
+        capabilities = new ArrayList();
+    }
+    
+    private void startSource (String uri, String name, String qName, Attributes atts){
+        
+        String id = atts.getValue("uri");
+        String title = atts.getValue("title");
+        String doc_ref = atts.getValue("doc_href");
+        String description = atts.getValue("description");
+
+           
+        currentSource.setId(id);
+        currentSource.setNickname(title);
+        currentSource.setHelperurl(doc_ref);
+        currentSource.setDescription(description);
+        
+    }
+    
+    private DasCoordinateSystem getCoordinateSystem(String uri, String name, String qname, Attributes atts){
+        // e.g. uri="http://das.sanger.ac.uk/dasregistry/coordsys/CS_LOCAL6" 
+        // source="Protein Sequence" authority="UniProt" test_range="P06213" />
+        DasCoordinateSystem dcs = new DasCoordinateSystem();
+        String id = atts.getValue("uri");
+        dcs.setUniqueId(id);
+        
+        String source = atts.getValue("source");
+        dcs.setCategory(source);
+        
+        String authority = atts.getValue("authority");
+        dcs.setName(authority);
+        
+        String test_range = atts.getValue("test_range");
+        dcs.setTestCode(test_range);
+        
+        try {
+            String taxidstr = atts.getValue("taxid");
+            int taxid = Integer.parseInt(taxidstr);
+            dcs.setNCBITaxId(taxid);
+        } catch (Exception e){}
+        
+        String version = atts.getValue("version");
+        if ( version != null)
+            dcs.setVersion(version);
+        
+        return dcs;
+    }
+    
+    public void startElement (String uri, String name, String qName, Attributes atts){
+        //System.out.println("new element "+qName);
+        
+        if (qName.equals("SOURCE")) {
+            //System.out.println("new Source " + atts.getValue(uri));
+            currentSource = new Das2SourceImpl();
+            coordinates = new ArrayList();
+            capabilities = new ArrayList();
+            
+            startSource(uri,name, qName, atts);
+            
+        } else if ( qName.equals("MAINTAINER")){
+            String email = atts.getValue("email");
+            currentSource.setAdminemail(email);
+        } else if ( qName.equals("COORDINATES")){
+            DasCoordinateSystem dcs = getCoordinateSystem(uri,name,qName,atts);
+            coordinates.add(dcs);
+            
+        } else if ( qName.equals("CAPABILITY")){
+            Das2Capability cap = getCapability(uri,name,qName,atts);
+            capabilities.add(cap);
+        }
+        
+        //TODO: add support for "labels"
+        
+    }
+    
+    private Das2Capability getCapability(String uri, String name, String qName, Attributes atts){
+        // e.g <CAPABILITY type="features" query_id="http://das.biopackages.net/das/genome/yeast/S228C/feature" />
+        Das2Capability cap = new Das2CapabilityImpl();
+        
+        String type = atts.getValue("type");
+        cap.setCapability(type);
+        String query_uri = atts.getValue("query_uri");
+        cap.setQueryUri(query_uri);
+        return cap;
+        
+    }
+    
+    public void startDocument(){
+        sources = new ArrayList();
+        coordinates = new ArrayList();
+        capabilities = new ArrayList();
+    }
+    
+    public void endElement(String uri, String name, String qName) {
+        if ( qName.equals("SOURCE")) {
+            currentSource.setDas2Capabilities((Das2Capability[])capabilities.toArray(new Das2Capability[capabilities.size()]));
+            //System.out.println("got coordinates " + coordinates.size());
+            currentSource.setCoordinateSystem((DasCoordinateSystem[])coordinates.toArray(new DasCoordinateSystem[coordinates.size()]));
+            //System.out.println("Das2SourceHandler endElement name " + name + " uri " + uri + " qName " + qName);
+            //System.out.println("Das2SourceHandler adding to source: " + currentSource.getId());    
+            sources.add(currentSource);   
+            currentSource = new Das2SourceImpl();
+        }
+    }
+    
+    public DasSource[] getSources(){    
+        //System.out.println("Das2SourceHandler: source size: " + sources.size());
+        return (DasSource[])sources.toArray(new DasSource[sources.size()]);
+    }
+    
+    
+
+}
diff --git a/src/org/biojava/dasobert/das2/io/DasSourceReader.java b/src/org/biojava/dasobert/das2/io/DasSourceReader.java
new file mode 100755 (executable)
index 0000000..0b05df8
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ *                  BioJava development code
+ *
+ * This code may be freely distributed and modified under the
+ * terms of the GNU Lesser General Public Licence.  This should
+ * be distributed with the code.  If you do not have a copy,
+ * see:
+ *
+ *      http://www.gnu.org/copyleft/lesser.html
+ *
+ * Copyright for this code is held jointly by the individual
+ * authors.  These should be listed in @author doc comments.
+ *
+ * For more information on the BioJava project and its aims,
+ * or to join the biojava-l mailing list, visit the home page
+ * at:
+ *
+ *      http://www.biojava.org/
+ * 
+ * Created on Feb 24, 2006
+ *
+ */
+package org.biojava.dasobert.das2.io;
+
+import java.io.InputStream;
+
+import org.biojava.dasobert.dasregistry.DasSource;
+
+public interface DasSourceReader {
+
+    public DasSource[] readDasSource(InputStream stream);
+}
diff --git a/src/org/biojava/dasobert/das2/io/DasSourceReaderImpl.java b/src/org/biojava/dasobert/das2/io/DasSourceReaderImpl.java
new file mode 100755 (executable)
index 0000000..730d474
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ *                  BioJava development code
+ *
+ * This code may be freely distributed and modified under the
+ * terms of the GNU Lesser General Public Licence.  This should
+ * be distributed with the code.  If you do not have a copy,
+ * see:
+ *
+ *      http://www.gnu.org/copyleft/lesser.html
+ *
+ * Copyright for this code is held jointly by the individual
+ * authors.  These should be listed in @author doc comments.
+ *
+ * For more information on the BioJava project and its aims,
+ * or to join the biojava-l mailing list, visit the home page
+ * at:
+ *
+ *      http://www.biojava.org/
+ *
+ * Created on Feb 24, 2006
+ *
+ */
+package org.biojava.dasobert.das2.io;
+
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.biojava.dasobert.dasregistry.DasSource;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.XMLReader;
+import org.biojava.dasobert.das.DAS_FeatureRetrieve;
+
+public class DasSourceReaderImpl implements DasSourceReader {
+
+    Exception loggedException;
+
+    public DasSourceReaderImpl() {
+        super();
+        loggedException = null;
+
+        // open the stream to a server and then parse the result ...
+    }
+
+    private InputStream open(URL url)
+    throws java.io.IOException, java.net.ConnectException
+    {
+        InputStream inStream = null;
+
+
+        HttpURLConnection huc = DAS_FeatureRetrieve.openHttpURLConnection(url);
+
+        inStream = huc.getInputStream();
+
+        return inStream;
+
+    }
+
+
+    public DasSource[] readDasSource(URL url){
+        DasSource[] sources = new DasSource[0];
+
+        try {
+            InputStream stream = open(url);
+
+            sources = readDasSource(stream);
+        } catch (Exception e){
+            e.printStackTrace();
+            loggedException = e;
+        }
+        return sources;
+    }
+
+    /** read a DAS2 sources response and return a list of DAS sources.
+     *
+     */
+    public DasSource[] readDasSource(InputStream stream)  {
+
+        DasSource[] sources = new DasSource[0];
+
+        try {
+        SAXParserFactory spfactory =
+            SAXParserFactory.newInstance();
+
+        spfactory.setValidating(false);
+
+        SAXParser saxParser = null ;
+
+        try{
+            saxParser =
+                spfactory.newSAXParser();
+        } catch (ParserConfigurationException e) {
+            e.printStackTrace();
+            loggedException = e;
+        }
+
+        String vali = System.getProperty("XMLVALIDATION");
+
+        boolean validation = false ;
+        if ( vali != null )
+            if ( vali.equals("true") )
+                validation = true ;
+
+
+        XMLReader xmlreader = saxParser.getXMLReader();
+
+        //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
+        try {
+            xmlreader.setFeature("http://xml.org/sax/features/validation", validation);
+        } catch (SAXException e) {
+            //logger.log(Level.FINE,"Cannot set validation " + validation);
+        }
+
+        try {
+            xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",validation);
+        } catch (SAXNotRecognizedException e){
+            e.printStackTrace();
+            //logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
+
+        }
+
+        DAS2SourceHandler cont_handle = new DAS2SourceHandler() ;
+
+        xmlreader.setContentHandler(cont_handle);
+        xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
+        InputSource insource = new InputSource() ;
+        insource.setByteStream(stream);
+
+
+        xmlreader.parse(insource);
+        sources = cont_handle.getSources();
+
+
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            loggedException = e;
+        }
+        return sources;
+    }
+
+    public Exception getLoggedException(){
+        return loggedException;
+    }
+
+    public static void main (String[] args){
+        String url = "http://www.spice-3d.org/dasregistry/das2/sources/";
+        DasSourceReaderImpl reader = new DasSourceReaderImpl();
+        try {
+            URL u = new URL(url);
+            DasSource[] sources = reader.readDasSource(u);
+            for (int i=0; i< sources.length;i++){
+                DasSource ds = sources[i];
+                System.out.println(ds.toString());
+            }
+
+        } catch (Exception e){
+            e.printStackTrace();
+        }
+
+    }
+
+
+
+}