JAL-2738 getGeneLoci promoted to SequenceI
[jalview.git] / src / jalview / datamodel / Sequence.java
index 96a2fa1..5d8a3ef 100755 (executable)
@@ -106,6 +106,8 @@ public class Sequence extends ASequence implements SequenceI
    */
   private int changeCount;
 
+  private GeneLoci geneLoci;
+
   /**
    * Creates a new Sequence object.
    * 
@@ -155,8 +157,8 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (name == null)
     {
-      System.err
-              .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
+      System.err.println(
+              "POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
       name = "";
     }
     // Does sequence have the /start-end signature?
@@ -645,21 +647,70 @@ public class Sequence extends ASequence implements SequenceI
   }
 
   /**
-   * DOCUMENT ME!
+   * Sets the sequence description, and also parses out any special formats of
+   * interest
    * 
    * @param desc
-   *          DOCUMENT ME!
    */
   @Override
   public void setDescription(String desc)
   {
     this.description = desc;
+    parseDescription();
   }
 
   /**
-   * DOCUMENT ME!
+   * Parses and saves fields of an Ensembl-style description e.g.
+   * chromosome:GRCh38:17:45051610:45109016:1
+   */
+  protected void parseDescription()
+  {
+    if (description == null)
+    {
+      return;
+    }
+    String[] tokens = description.split(":");
+    if (tokens.length == 6 && "chromosome".equals(tokens[0])) {
+      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[] { start, end };
+        int[] to = new int[] { forwardStrand ? chStart : chEnd,
+            forwardStrand ? chEnd : chStart };
+        MapList map = new MapList(from, to, 1, 1);
+        GeneLoci gl = new GeneLoci(species, ref, chrom, map);
+        setGeneLoci(gl);
+      } catch (NumberFormatException e)
+      {
+        System.err.println("Bad integers in description " + description);
+      }
+    }
+  }
+
+  public void setGeneLoci(GeneLoci gl)
+  {
+    geneLoci = gl;
+  }
+
+  /**
+   * Returns the gene loci mapping for the sequence (may be null)
    * 
-   * @return DOCUMENT ME!
+   * @return
+   */
+  @Override
+  public GeneLoci getGeneLoci()
+  {
+    return geneLoci;
+  }
+
+  /**
+   * Answers the description
+   * 
+   * @return
    */
   @Override
   public String getDescription()
@@ -1364,8 +1415,9 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public AlignmentAnnotation[] getAnnotation()
   {
-    return annotation == null ? null : annotation
-            .toArray(new AlignmentAnnotation[annotation.size()]);
+    return annotation == null ? null
+            : annotation
+                    .toArray(new AlignmentAnnotation[annotation.size()]);
   }
 
   @Override
@@ -1477,8 +1529,9 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (datasetSequence == null)
     {
-      Sequence dsseq = new Sequence(getName(), AlignSeq.extractGaps(
-              jalview.util.Comparison.GapChars, getSequenceAsString()),
+      Sequence dsseq = new Sequence(getName(),
+              AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
+                      getSequenceAsString()),
               getStart(), getEnd());
 
       datasetSequence = dsseq;
@@ -1620,7 +1673,7 @@ public class Sequence extends ASequence implements SequenceI
       List<SequenceFeature> sfs = entry.getSequenceFeatures();
       for (SequenceFeature feature : sfs)
       {
-        SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature)
+       SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature)
                 : new SequenceFeature[] { new SequenceFeature(feature) };
         if (sf != null)
         {
@@ -1777,8 +1830,8 @@ public class Sequence extends ASequence implements SequenceI
           }
         }
         // whilst it looks like it is a primary ref, we also sanity check type
-        if (DBRefUtils.getCanonicalName(DBRefSource.PDB).equals(
-                DBRefUtils.getCanonicalName(ref.getSource())))
+        if (DBRefUtils.getCanonicalName(DBRefSource.PDB)
+                .equals(DBRefUtils.getCanonicalName(ref.getSource())))
         {
           // PDB dbrefs imply there should be a PDBEntry associated
           // TODO: tighten PDB dbrefs
@@ -1821,12 +1874,11 @@ public class Sequence extends ASequence implements SequenceI
             endPos, types);
 
     /*
-     * if the start or end column is gapped, startPos or endPos may be to the 
-     * left or right, and we may have included adjacent or enclosing features;
-     * remove any that are not enclosing features
+     * if end column is gapped, endPos may be to the right, 
+     * and we may have included adjacent or enclosing features;
+     * remove any that are not enclosing, non-contact features
      */
-    if (endPos > this.end || Comparison.isGap(sequence[fromColumn - 1])
-            || Comparison.isGap(sequence[toColumn - 1]))
+    if (endPos > this.end || Comparison.isGap(sequence[toColumn - 1]))
     {
       ListIterator<SequenceFeature> it = result.listIterator();
       while (it.hasNext())
@@ -1847,6 +1899,13 @@ public class Sequence extends ASequence implements SequenceI
           {
             it.remove();
           }
+          else if (featureEndColumn > toColumn && sf.isContactFeature())
+          {
+            /*
+             * remove an enclosing feature if it is a contact feature
+             */
+            it.remove();
+          }
         }
       }
     }