Dasobert updates, labels and prefix changes
authoramwaterhouse <Andrew Waterhouse>
Mon, 17 Jul 2006 10:01:00 +0000 (10:01 +0000)
committeramwaterhouse <Andrew Waterhouse>
Mon, 17 Jul 2006 10:01:00 +0000 (10:01 +0000)
13 files changed:
src/org/biojava/dasobert/das/DAS_FeatureRetrieve.java
src/org/biojava/dasobert/das/DAS_Feature_Handler.java
src/org/biojava/dasobert/das/FeatureThread.java
src/org/biojava/dasobert/das2/Das2Capability.java
src/org/biojava/dasobert/das2/Das2CapabilityImpl.java
src/org/biojava/dasobert/das2/Das2Source.java
src/org/biojava/dasobert/das2/DasSourceConverter.java
src/org/biojava/dasobert/das2/io/DAS2SourceHandler.java
src/org/biojava/dasobert/das2/io/DasSourceReaderImpl.java
src/org/biojava/dasobert/dasregistry/Das1Source.java
src/org/biojava/dasobert/dasregistry/DasSource.java
src/org/biojava/dasobert/eventmodel/FeatureEvent.java
src/org/biojava/dasobert/eventmodel/SequenceListener.java

index e6319f1..b65a847 100755 (executable)
@@ -39,6 +39,8 @@ import java.net.ConnectException;
 import java.lang.reflect.Method;
 
 
+
+
 /**
  * A class to perform a DAS features request
  *
@@ -52,6 +54,7 @@ public class DAS_FeatureRetrieve {
     int comeBackLater;
     URL url;
     /**
+     * @param url the URL the features should be downloaded from
      *
      */
     public DAS_FeatureRetrieve(URL url) {
@@ -138,8 +141,7 @@ public class DAS_FeatureRetrieve {
                 comeBackLater = cont_handle.getComBackLater();
             }
             catch ( Exception e){
-              System.out.println("ERROR PARSING RESULT FROM "+url+ "\n"+e+"\n");
-               // e.printStackTrace();
+                e.printStackTrace();
                 logger.log(Level.FINE,"error while parsing response from "+ url);
                 comeBackLater = -1;
                 features = new ArrayList();
@@ -151,6 +153,7 @@ public class DAS_FeatureRetrieve {
         }
     }
 
+
     /** open HttpURLConnection. Recommended way to open
      * HttpURLConnections, since this take care of setting timeouts
      * properly for java 1.4 and 1.5*/
@@ -196,6 +199,7 @@ public class DAS_FeatureRetrieve {
     return huc;
     }
 
+
     private InputStream open(URL url)
     throws java.io.IOException, java.net.ConnectException
     {
@@ -210,7 +214,8 @@ public class DAS_FeatureRetrieve {
 
     }
 
-    /** returns a List of Features */
+    /** returns a List of Features
+     * @return a List of Maps containing the features*/
     public List get_features() {
 
         return features;
index 88eb57d..105388e 100755 (executable)
@@ -48,7 +48,9 @@ public class DAS_Feature_Handler  extends DefaultHandler{
     String dasCommand ;
     
     int comeBackLater ;
-    
+
+    int maxFeatures ;
+
     public DAS_Feature_Handler() {
         super();
         
@@ -58,6 +60,21 @@ public class DAS_Feature_Handler  extends DefaultHandler{
         characterdata = "";
         dasCommand = "" ;
         comeBackLater = -1; 
+       maxFeatures = -1;
+    }
+
+    /** specifies a maximum number of features to be downloaded. if a
+       server returns more, they will be ignored.  default is to load
+       all features 
+    @param max the maximium number of features to be downloaded
+    */
+
+    public void setMaxFeatures(int max) {
+       maxFeatures = max;
+    }
+
+    public int getMaxFeatures() {
+       return maxFeatures;
     }
     
     public void setDASCommand(String cmd) { dasCommand = cmd ;}
@@ -72,6 +89,11 @@ public class DAS_Feature_Handler  extends DefaultHandler{
     }
     
     void start_feature(String uri, String name, String qName, Attributes atts) {
+
+       if (( maxFeatures > 0 ) && ( features.size() > maxFeatures ) ) {
+           characterdata = "";
+           return;
+       }
         feature = new HashMap() ;
         String id      = atts.getValue("id");
         feature.put("id",id);
@@ -83,6 +105,11 @@ public class DAS_Feature_Handler  extends DefaultHandler{
         //System.out.println("featurefield "+featurefield+ " data "+characterdata);
         // NOTE can have multiple lines ..
         
+       if (( maxFeatures > 0 ) && ( features.size() > maxFeatures ) ) {
+           return;
+       }
+
+
         String data = (String)feature.get(featurefield);
         if (data != null){
             characterdata = data + " " + characterdata;
@@ -122,16 +149,12 @@ public class DAS_Feature_Handler  extends DefaultHandler{
     }
     
     public void startDocument() {
-        //System.out.println("start document");
     }
     
-    public void endDocument () {
-        //System.out.println("adding feature " + feature);
-        //features.add(feature);
-        
+    public void endDocument () {        
     }
     public void endElement(String uri, String name, String qName) {
-        //System.out.println("end "+name);
+
         if ( qName.equals("METHOD") || 
                 qName.equals("TYPE") ||
                 qName.equals("START") ||
@@ -143,13 +166,20 @@ public class DAS_Feature_Handler  extends DefaultHandler{
             add_featuredata(uri,name,qName);
         }
         else if ( qName.equals("FEATURE")) {
-            //System.out.println("adding ffeature " + feature);
-            features.add(feature);
+
+           if ( maxFeatures > 0 ) {
+               if ( features.size() < maxFeatures ) {
+                    features.add(feature);
+               } 
+           } else {
+               // no restriction
+               features.add(feature);
+           }
         }
     }
     
     public void characters (char ch[], int start, int length){
-        //System.out.println("characters");
+
         for (int i = start; i < start + length; i++) {
             
             characterdata += ch[i];
index 7435cdb..8c8923e 100755 (executable)
@@ -32,16 +32,16 @@ import org.biojava.dasobert.eventmodel.FeatureEvent;
 import org.biojava.dasobert.dasregistry.Das1Source;
 
 /** a thread that connects to a DAS - Feature service and gets the features
- *
+ * 
  * @author Andreas Prlic
  */
 
 
 
 public class FeatureThread
-    implements Runnable
+    implements Runnable 
 {
-
+    
     /** number of times the client tries to reconnect to the server if a "come back later" is returned.
      * the server should provide a reasonable estimation how long it will take him to create results.
      * if this number of requests is still not successfull, give up.
@@ -56,7 +56,7 @@ public class FeatureThread
     String ac ;
     List featureListeners;
     Thread thread;
-
+    
     public FeatureThread (String accessionCode, Das1Source dasSource) {
        this.dasSource = dasSource;
        this.ac = accessionCode;
@@ -66,7 +66,7 @@ public class FeatureThread
     public void addFeatureListener(FeatureListener li) {
        featureListeners.add(li);
     }
-
+    
     public void clearFeatureListeners() {
        featureListeners.clear();
     }
@@ -76,7 +76,7 @@ public class FeatureThread
        notify();
     }
 
-
+    
 
 
     public void run() {
@@ -89,21 +89,22 @@ public class FeatureThread
                cmd = new URL(queryString);
            } catch (MalformedURLException e ) {
                logger.warning("got MalformedURL from das source " +dasSource);
-               e.printStackTrace();
+               e.printStackTrace();
+               
            }
 
            logger.info("requesting features from " + cmd);
            DAS_FeatureRetrieve ftmp = new DAS_FeatureRetrieve(cmd);
-
-
+        
+           
            int comeBackLater = ftmp.getComeBackLater();
            int securityCounter = 0;
            while ( (thread == me) && ( comeBackLater > 0 )) {
                securityCounter++;
                if ( securityCounter >= MAX_COME_BACK_ITERATIONS){
-                   comeBackLater = -1;
+                   comeBackLater = -1; 
                    break;
-
+                
                }
                notifyComeBackLater(comeBackLater);
                // server is still calculating - asks us to come back later
@@ -113,9 +114,9 @@ public class FeatureThread
                    comeBackLater = -1;
                    break;
                }
-
+                
                ftmp.reload();
-               comeBackLater = ftmp.getComeBackLater();
+               comeBackLater = ftmp.getComeBackLater(); 
            }
 
            if ( ! (thread == me ) ) {
@@ -123,15 +124,15 @@ public class FeatureThread
            }
 
            List features = ftmp.get_features();
-
+        
            // a fallback mechanism to prevent DAS sources from bringing down spice
            if ( features.size() > MAX_NR_FEATURES){
                logger.warning("DAS source returned more than " + MAX_NR_FEATURES + "features. " +
                               " throwing away excess features at " +cmd);
                features = features.subList(0,MAX_NR_FEATURES);
            }
-
-
+      
+              
            // notify FeatureListeners
            Map[] feats = (Map[])features.toArray(new Map[features.size()]);
            notifyFeatureListeners(feats);
@@ -158,9 +159,9 @@ public class FeatureThread
             fi.newFeatures(fevent);
         }
     }
-
+    
  /** the Annotation server requested to be queried again in a while
-     *
+     * 
      * @param comeBackLater
      */
     private void notifyComeBackLater(int comeBackLater){
@@ -171,7 +172,7 @@ public class FeatureThread
             FeatureListener fi = (FeatureListener)fiter.next();
             fi.comeBackLater(event);
         }
-
+        
     }
 
 
index 7bb1e89..5b5c17c 100755 (executable)
@@ -38,7 +38,7 @@ public interface Das2Capability {
     
     /** checks if this capability is actually of das1 style
      * 
-     * @return
+     * @return boolean true if the capability is in DAS1 style
      */
     public boolean isDas1Style();
     
index 48f0a8a..273defa 100755 (executable)
@@ -29,7 +29,7 @@ implements Das2Capability{
     String[] formats;
     String queryId;
     
-    public static String DAS1_CAPABILITY_PREFIX = "das1";
+    public static String DAS1_CAPABILITY_PREFIX = "das1:";
     
     public Das2CapabilityImpl() {
         super();
index 437acb5..21a008a 100755 (executable)
@@ -34,6 +34,7 @@ extends DasSource {
     *  if true - this source can be converted into a DAS1 source by using
     *  DasSourceConverter.toDas1(Das2Source);
     *
+    * @return true if the DasSource has DAS1 capabilties
     */
    public boolean hasDas1Capabilities();
 }
index 4fb294a..513ba3f 100755 (executable)
@@ -22,6 +22,7 @@
  */
 package org.biojava.dasobert.das2;
 
+//import org.biojava.bio.program.das.dasalignment.DASException;
 import org.biojava.dasobert.dasregistry.Das1Source;
 
 public class DasSourceConverter {
@@ -36,7 +37,9 @@ public class DasSourceConverter {
      * This only will work if is passes the Das2Source.isDas1Source() test
      * i.e. this is really a das1 server there
      *
-     * @return
+     * @param das2source a DAS2Source to be converted
+     * @return a Das1Source
+     * @throws DASException
      */
     public static Das1Source toDas1Source (Das2Source das2source) throws Exception{
         if ( ! das2source.hasDas1Capabilities())
@@ -52,16 +55,20 @@ public class DasSourceConverter {
         ds.setCoordinateSystem(das2source.getCoordinateSystem());
         ds.setNickname(das2source.getNickname());
         ds.setId(das2source.getId());
+        ds.setLabels(das2source.getLabels());
 
         // convert the capabilitites to das1 capabiltities and get the url
         Das2Capability[] caps = das2source.getDas2Capabilities();
         String[] das1capabilitites = new String[caps.length];
-        int DASPREFIXLENGTH = 4;
+        int DASPREFIXLENGTH = Das2CapabilityImpl.DAS1_CAPABILITY_PREFIX.length();
+
         for ( int i = 0 ; i< caps.length;i++){
             Das2Capability cap = caps[i];
 
             String c = cap.getCapability();
-            das1capabilitites[i] = c.substring(4,c.length());
+
+            das1capabilitites[i] = c.substring(DASPREFIXLENGTH,c.length());
+
             String query_uri = cap.getQueryUri();
 
             String url = query_uri.substring(0,(query_uri.length() - c.length() + DASPREFIXLENGTH));
index 810ec33..caf6798 100755 (executable)
@@ -46,14 +46,18 @@ public class DAS2SourceHandler extends DefaultHandler{
     Das2Source currentSource;
     List coordinates;
     List capabilities;
-    
+    List labels;
+
+    public static final String LABELPROPERTY = "label";
+
     public DAS2SourceHandler() {
         super();
         
-        sources = new ArrayList();
+        sources       = new ArrayList();
         currentSource = new Das2SourceImpl();
-        coordinates = new ArrayList();
-        capabilities = new ArrayList();
+        coordinates   = new ArrayList();
+        capabilities  = new ArrayList();
+       labels        = new ArrayList();
     }
     
     private void startSource (String uri, String name, String qName, Attributes atts){
@@ -121,10 +125,9 @@ public class DAS2SourceHandler extends DefaultHandler{
         } else if ( qName.equals("CAPABILITY")){
             Das2Capability cap = getCapability(uri,name,qName,atts);
             capabilities.add(cap);
-        }
-        
-        //TODO: add support for "labels"
-        
+        } else if (qName.equals("PROPERTY")) {
+           addProperty(uri,name,qName,atts);
+       }                
     }
     
     private Das2Capability getCapability(String uri, String name, String qName, Attributes atts){
@@ -139,6 +142,13 @@ public class DAS2SourceHandler extends DefaultHandler{
         
     }
     
+    private void addProperty(String uri, String name, String qName, Attributes atts){
+       String pname = atts.getValue("name");
+       String label = atts.getValue("value");
+       if ( pname.equals(LABELPROPERTY) )
+           labels.add(label);
+    }
+    
     public void startDocument(){
         sources = new ArrayList();
         coordinates = new ArrayList();
@@ -150,11 +160,15 @@ public class DAS2SourceHandler extends DefaultHandler{
             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()]));
+
+           currentSource.setLabels((String[])labels.toArray(new String[labels.size()]));
+           labels.clear();
+
             //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(){    
index 730d474..3f0a07a 100755 (executable)
@@ -31,12 +31,13 @@ import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 
+//import org.biojava.dasobert.das.AlignmentThread;
+import org.biojava.dasobert.das.DAS_FeatureRetrieve;
 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 {
 
index c962895..de007d6 100755 (executable)
@@ -29,6 +29,10 @@ import java.io.PrintWriter;
 import java.io.StringWriter;
 
 
+//import org.biojava.dasobert.das2.io.DasSourceWriter;
+//import org.biojava.dasobert.das2.io.DasSourceWriterImpl;
+//import org.biojava.utils.xml.PrettyXMLWriter;
+
 
 /** a simple Bean class to be returned via SOAP
  * @author Andreas Prlic
@@ -94,6 +98,26 @@ public class Das1Source implements DasSource {
         return h;
     }
 
+    /** the DAS2 string representation of this DAS source
+     *
+    public String toString() {
+
+        StringWriter writer = new StringWriter();
+
+        PrintWriter pw = new PrintWriter(writer);
+        PrettyXMLWriter xw = new PrettyXMLWriter(pw);
+
+        DasSourceWriter dswriter = new DasSourceWriterImpl();
+        try {
+            dswriter.writeDasSource(xw,this);
+        } catch (IOException e){
+            e.printStackTrace();
+        }
+
+        return writer.toString();
+
+    }
+    */
     public void setLocal(boolean flag){ local = flag;}
     public boolean isLocal(){return local;}
 
index c9aee5d..a2112b6 100755 (executable)
@@ -33,13 +33,13 @@ public interface DasSource {
     /** compare if two das sources are equal
      * 
      * @param ds
-     * @return
+     * @return returns true if two DAS sources are equivalent
      */
     public boolean equals(DasSource ds);
     
     /** classes that implement equals, should also implement hashKey
      * 
-     * @return
+     * @return the hash code of a das source
      */
     public int hashCode();
     
@@ -50,6 +50,7 @@ public interface DasSource {
      * identifier. The public DAS-Registry has Auto_Ids that look like
      * DASSOURCE:12345; public look like XYZ:12345, where the XYZ
      * prefix can be configured in the config file.
+     * @return String the ID of a Das Source
      */
     public  String getId();
 
index e941d89..4c9ac37 100755 (executable)
@@ -58,7 +58,7 @@ public class FeatureEvent {
      *      System.out.println(type);
      * }      
      * </pre>
-     * @return
+     * @return a Map containng the features
      */
     public Map[] getFeatures(){
         return features;
index 9232dc9..58cbdb3 100755 (executable)
@@ -34,10 +34,15 @@ extends ObjectListener{
     /* select a certain sequence position */
     public void selectedSeqPosition(int position);
     
-    /** select a certain range of a sequence */
+    /** select a certain range of a sequence 
+     * @param start the start
+     * @param end the end of the range
+     * */
     public void selectedSeqRange(int start, int end);
     
-    /** the current selecetion is locked and can not be changed */
+    /** the current selecetion is locked and can not be changed 
+     * @param flag true if selection should be locked
+     * */
     public void selectionLocked(boolean flag);
     
     public void newSequence(SequenceEvent e);