JAL-2743 look up species and gene loci for Ensembl gene
[jalview.git] / src / jalview / ext / ensembl / EnsemblGene.java
index afff4c2..11322c8 100644 (file)
@@ -147,10 +147,9 @@ public class EnsemblGene extends EnsemblSeqProxy
         continue;
       }
       
-      parseChromosomeLocations(geneAlignment);
-      
       if (geneAlignment.getHeight() == 1)
       {
+        findGeneLoci(geneAlignment.getSequenceAt(0), geneId);
         getTranscripts(geneAlignment, geneId);
       }
       if (al == null)
@@ -166,42 +165,64 @@ public class EnsemblGene extends EnsemblSeqProxy
   }
 
   /**
+   * Calls the /lookup/id REST service, parses the response for gene
+   * coordinates, and if successful, adds these to the sequence
+   * 
+   * @param seq
+   * @param geneId
+   */
+  void findGeneLoci(SequenceI seq, String geneId)
+  {
+    /*
+     * if sequence description is in gene loci format, parse this
+     * - but try remote lookup anyway as 'better' if available
+     */
+    parseChromosomeLocations(seq);
+
+    GeneLociI geneLoci = new EnsemblLookup(getDomain()).getGeneLoci(geneId);
+    if (geneLoci != null)
+    {
+      seq.setGeneLoci(geneLoci.getSpeciesId(), geneLoci.getAssemblyId(),
+              geneLoci.getChromosomeId(), geneLoci.getMap());
+    }
+  }
+
+  /**
    * Parses and saves fields of an Ensembl-style description e.g.
    * chromosome:GRCh38:17:45051610:45109016:1
    * 
-   * @param alignment
+   * @param seq
    */
-  private void parseChromosomeLocations(AlignmentI alignment)
+  boolean parseChromosomeLocations(SequenceI seq)
   {
-    for (SequenceI seq : alignment.getSequences())
+    String description = seq.getDescription();
+    if (description == null)
+    {
+      return false;
+    }
+    String[] tokens = description.split(":");
+    if (tokens.length == 6 && tokens[0].startsWith(DBRefEntry.CHROMOSOME))
     {
-      String description = seq.getDescription();
-      if (description == null)
+      String ref = tokens[1];
+      String chrom = tokens[2];
+      try
       {
-        continue;
-      }
-      String[] tokens = description.split(":");
-      if (tokens.length == 6 && tokens[0].startsWith(DBRefEntry.CHROMOSOME))
+        int chStart = Integer.parseInt(tokens[3]);
+        int chEnd = Integer.parseInt(tokens[4]);
+        boolean forwardStrand = "1".equals(tokens[5]);
+        String species = ""; // not known here
+        int[] from = new int[] { seq.getStart(), seq.getEnd() };
+        int[] to = new int[] { forwardStrand ? chStart : chEnd,
+            forwardStrand ? chEnd : chStart };
+        MapList map = new MapList(from, to, 1, 1);
+        seq.setGeneLoci(species, ref, chrom, map);
+        return true;
+      } catch (NumberFormatException e)
       {
-        String ref = tokens[1];
-        String chrom = tokens[2];
-        try
-        {
-          int chStart = Integer.parseInt(tokens[3]);
-          int chEnd = Integer.parseInt(tokens[4]);
-          boolean forwardStrand = "1".equals(tokens[5]);
-          String species = ""; // dunno yet!
-          int[] from = new int[] { seq.getStart(), seq.getEnd() };
-          int[] to = new int[] { forwardStrand ? chStart : chEnd,
-              forwardStrand ? chEnd : chStart };
-          MapList map = new MapList(from, to, 1, 1);
-          seq.setGeneLoci(species, ref, chrom, map);
-        } catch (NumberFormatException e)
-        {
-          System.err.println("Bad integers in description " + description);
-        }
+        System.err.println("Bad integers in description " + description);
       }
     }
+    return false;
   }
 
   /**