JAL-1796 GeneLociI as distinguished DBRefEntry
[jalview.git] / src / jalview / ext / ensembl / EnsemblGene.java
index 5e970b1..bc914e5 100644 (file)
@@ -23,7 +23,8 @@ package jalview.ext.ensembl;
 import jalview.api.FeatureColourI;
 import jalview.api.FeatureSettingsModelI;
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.GeneLoci;
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.GeneLociI;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
@@ -145,6 +146,9 @@ public class EnsemblGene extends EnsemblSeqProxy
       {
         continue;
       }
+      
+      parseChromosomeLocations(geneAlignment);
+      
       if (geneAlignment.getHeight() == 1)
       {
         getTranscripts(geneAlignment, geneId);
@@ -162,6 +166,45 @@ public class EnsemblGene extends EnsemblSeqProxy
   }
 
   /**
+   * Parses and saves fields of an Ensembl-style description e.g.
+   * chromosome:GRCh38:17:45051610:45109016:1
+   * 
+   * @param alignment
+   */
+  private void parseChromosomeLocations(AlignmentI alignment)
+  {
+    for (SequenceI seq : alignment.getSequences())
+    {
+      String description = seq.getDescription();
+      if (description == null)
+      {
+        continue;
+      }
+      String[] tokens = description.split(":");
+      if (tokens.length == 6 && tokens[0].startsWith(DBRefEntry.CHROMOSOME))
+      {
+        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);
+        }
+      }
+    }
+  }
+
+  /**
    * Converts a query, which may contain one or more gene or transcript
    * identifiers, into a non-redundant list of gene identifiers.
    * 
@@ -429,22 +472,13 @@ public class EnsemblGene extends EnsemblSeqProxy
   protected void mapTranscriptToChromosome(SequenceI transcript,
           SequenceI gene, MapList mapping)
   {
-    GeneLoci loci = gene.getGeneLoci();
+    GeneLociI loci = gene.getGeneLoci();
     if (loci == null)
     {
       return;
     }
 
-    /*
-     * patch to ensure gene to chromosome mapping is complete
-     * (in case created before gene length was known)
-     */
-    MapList geneMapping = loci.mapping;
-    if (geneMapping.getFromRanges().get(0)[1] == 0)
-    {
-      geneMapping.getFromRanges().get(0)[0] = gene.getStart();
-      geneMapping.getFromRanges().get(0)[1] = gene.getEnd();
-    }
+    MapList geneMapping = loci.getMap();
 
     List<int[]> exons = mapping.getFromRanges();
     List<int[]> transcriptLoci = new ArrayList<>();
@@ -457,10 +491,9 @@ public class EnsemblGene extends EnsemblSeqProxy
     List<int[]> transcriptRange = Arrays.asList(new int[] {
         transcript.getStart(), transcript.getEnd() });
     MapList mapList = new MapList(transcriptRange, transcriptLoci, 1, 1);
-    GeneLoci gl = new GeneLoci(loci.species, loci.assembly,
-            loci.chromosome, mapList);
 
-    transcript.setGeneLoci(gl);
+    transcript.setGeneLoci(loci.getSpeciesId(), loci.getAssemblyId(),
+            loci.getChromosomeId(), mapList);
   }
 
   /**