Merge branch 'develop' into features/JAL-1793VCF; lambda Function for
[jalview.git] / src / jalview / ext / ensembl / EnsemblGene.java
index 50dfa90..afff4c2 100644 (file)
@@ -23,6 +23,8 @@ package jalview.ext.ensembl;
 import jalview.api.FeatureColourI;
 import jalview.api.FeatureSettingsModelI;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.GeneLociI;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
@@ -144,6 +146,9 @@ public class EnsemblGene extends EnsemblSeqProxy
       {
         continue;
       }
+      
+      parseChromosomeLocations(geneAlignment);
+      
       if (geneAlignment.getHeight() == 1)
       {
         getTranscripts(geneAlignment, geneId);
@@ -161,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, transcript, or
    * external (to Ensembl) identifiers, into a non-redundant list of gene
    * identifiers.
@@ -354,6 +398,8 @@ public class EnsemblGene extends EnsemblSeqProxy
     cdna.transferFeatures(gene.getFeatures().getPositionalFeatures(),
             transcript.getDatasetSequence(), mapping, parentId);
 
+    mapTranscriptToChromosome(transcript, gene, mapping);
+
     /*
      * fetch and save cross-references
      */
@@ -368,6 +414,42 @@ public class EnsemblGene extends EnsemblSeqProxy
   }
 
   /**
+   * If the gene has a mapping to chromosome coordinates, derive the transcript
+   * chromosome regions and save on the transcript sequence
+   * 
+   * @param transcript
+   * @param gene
+   * @param mapping
+   *          the mapping from gene to transcript positions
+   */
+  protected void mapTranscriptToChromosome(SequenceI transcript,
+          SequenceI gene, MapList mapping)
+  {
+    GeneLociI loci = gene.getGeneLoci();
+    if (loci == null)
+    {
+      return;
+    }
+
+    MapList geneMapping = loci.getMap();
+
+    List<int[]> exons = mapping.getFromRanges();
+    List<int[]> transcriptLoci = new ArrayList<>();
+
+    for (int[] exon : exons)
+    {
+      transcriptLoci.add(geneMapping.locateInTo(exon[0], exon[1]));
+    }
+
+    List<int[]> transcriptRange = Arrays.asList(new int[] {
+        transcript.getStart(), transcript.getEnd() });
+    MapList mapList = new MapList(transcriptRange, transcriptLoci, 1, 1);
+
+    transcript.setGeneLoci(loci.getSpeciesId(), loci.getAssemblyId(),
+            loci.getChromosomeId(), mapList);
+  }
+
+  /**
    * Returns the 'transcript_id' property of the sequence feature (or null)
    * 
    * @param feature