JAL-1705 use GET for single queries; set gene name as description on
[jalview.git] / src / jalview / ext / ensembl / EnsemblSeqProxy.java
index d7811b9..a6d838b 100644 (file)
@@ -42,6 +42,8 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
    */
   protected static final String NMD_VARIANT = "NMD_transcript_variant";
 
+  protected static final String NAME = "Name";
+
   public enum EnsemblSeqType
   {
     /**
@@ -111,12 +113,10 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
 
   /**
    * Makes the sequence queries to Ensembl's REST service and returns an
-   * alignment consisting of the returned sequences. This overloaded method
-   * allows the genomic sequence (with features) to be passed in if it has
-   * already been retrieved, to avoid repeat calls to fetch it.
+   * alignment consisting of the returned sequences.
    */
-  public AlignmentI getSequenceRecords(String query,
-          SequenceI genomicSequence) throws Exception
+  @Override
+  public AlignmentI getSequenceRecords(String query) throws Exception
   {
     long now = System.currentTimeMillis();
     // TODO use a String... query vararg instead?
@@ -159,11 +159,12 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
     }
 
     /*
-     * fetch and transfer genomic sequence features
+     * fetch and transfer genomic sequence features,
+     * fetch protein product and add as cross-reference
      */
     for (String accId : allIds)
     {
-      addFeaturesAndProduct(accId, alignment, genomicSequence);
+      addFeaturesAndProduct(accId, alignment);
     }
 
     inProgress = false;
@@ -181,24 +182,26 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
    * @param accId
    * @param alignment
    */
-  protected void addFeaturesAndProduct(String accId, AlignmentI alignment,
-          SequenceI genomicSequence)
+  protected void addFeaturesAndProduct(String accId, AlignmentI alignment)
   {
+    if (alignment == null)
+    {
+      return;
+    }
+
     try
     {
       /*
        * get 'dummy' genomic sequence with exon, cds and variation features
        */
-      if (genomicSequence == null)
+      SequenceI genomicSequence = null;
+      EnsemblOverlap gffFetcher = new EnsemblOverlap();
+      EnsemblFeatureType[] features = getFeaturesToFetch();
+      AlignmentI geneFeatures = gffFetcher.getSequenceRecords(accId,
+              features);
+      if (geneFeatures.getHeight() > 0)
       {
-        EnsemblOverlap gffFetcher = new EnsemblOverlap();
-        EnsemblFeatureType[] features = getFeaturesToFetch();
-        AlignmentI geneFeatures = gffFetcher.getSequenceRecords(accId,
-                features);
-        if (geneFeatures.getHeight() > 0)
-        {
-          genomicSequence = geneFeatures.getSequenceAt(0);
-        }
+        genomicSequence = geneFeatures.getSequenceAt(0);
       }
       if (genomicSequence != null)
       {
@@ -386,6 +389,15 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
               "Only retrieved %d sequences for %d query strings", fr
                       .getSeqs().size(), ids.size()));
     }
+
+    if (fr.getSeqs().size() == 1 && fr.getSeqs().get(0).getLength() == 0)
+    {
+      /*
+       * POST request has returned an empty FASTA file e.g. for invalid id
+       */
+      throw new IOException("No data returned for " + ids);
+    }
+
     if (fr.getSeqs().size() > 0)
     {
       AlignmentI seqal = new Alignment(
@@ -424,10 +436,16 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
   @Override
   protected URL getUrl(List<String> ids) throws MalformedURLException
   {
-    // ids are not used - they go in the POST body instead
+    /*
+     * a single id is included in the URL path
+     * multiple ids go in the POST body instead
+     */
     StringBuffer urlstring = new StringBuffer(128);
     urlstring.append(SEQUENCE_ID_URL);
-
+    if (ids.size() == 1)
+    {
+      urlstring.append("/").append(ids.get(0));
+    }
     // @see https://github.com/Ensembl/ensembl-rest/wiki/Output-formats
     urlstring.append("?type=").append(getSourceEnsemblType().getType());
     urlstring.append(("&Accept=text/x-fasta"));
@@ -454,9 +472,9 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
   }
 
   @Override
-  protected String getRequestMimeType()
+  protected String getRequestMimeType(boolean multipleIds)
   {
-    return "application/json";
+    return multipleIds ? "application/json" : "text/x-fasta";
   }
 
   @Override
@@ -751,12 +769,6 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
             + " sequence with variant features";
   }
 
-  @Override
-  public AlignmentI getSequenceRecords(String identifier) throws Exception
-  {
-    return getSequenceRecords(identifier, null);
-  }
-
   /**
    * Returns a (possibly empty) list of features on the sequence which have the
    * specified sequence ontology type (or a sub-type of it), and the given