JAL-2541 revised cut with features (work in progress)
[jalview.git] / src / jalview / datamodel / Sequence.java
index 96a2fa1..b758d51 100755 (executable)
@@ -44,10 +44,7 @@ import fr.orsay.lri.varna.models.rna.RNA;
 
 /**
  * 
- * Implements the SequenceI interface for a char[] based sequence object.
- * 
- * @author $author$
- * @version $Revision$
+ * Implements the SequenceI interface for a char[] based sequence object
  */
 public class Sequence extends ASequence implements SequenceI
 {
@@ -155,8 +152,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?
@@ -1364,8 +1361,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 +1475,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 +1619,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 +1776,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 +1820,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 +1845,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();
+          }
         }
       }
     }
@@ -1893,4 +1898,75 @@ public class Sequence extends ASequence implements SequenceI
 
     return count;
   }
+
+  @Override
+  public List<SequenceFeature[]> adjustFeatures(int fromColumn, int toColumn)
+  {
+    List<SequenceFeature[]> amended = new ArrayList<>();
+
+    if (toColumn < fromColumn)
+    {
+      return amended;
+    }
+
+    synchronized (sequenceFeatureStore)
+    {
+      /*
+       * get features that overlap or span the cut region
+       */
+      List<SequenceFeature> overlaps = findFeatures(fromColumn, toColumn);
+      int cutWidth = toColumn - fromColumn + 1;
+
+      /*
+       * get features that strictly follow the cut region,
+       *  and shift them left by the width of the cut
+       */
+      List<SequenceFeature> follow = findFeatures(toColumn + 1,
+              Integer.MAX_VALUE);
+      follow.removeAll(overlaps);
+      for (SequenceFeature sf : follow)
+      {
+        SequenceFeature copy = new SequenceFeature(sf, sf.getBegin()
+                - cutWidth, sf.getEnd() - cutWidth, sf.getFeatureGroup(),
+                sf.getScore());
+        deleteFeature(sf);
+        addSequenceFeature(copy);
+      }
+
+      /*
+       * adjust start-end of overlapping features, and delete if enclosed by
+       * the cut, or a partially overlapping contact feature
+       */
+      for (SequenceFeature sf : overlaps)
+      {
+        // TODO recode to compute newBegin, newEnd, isDelete
+        // then perform the action
+        int sfBegin = sf.getBegin();
+        int sfEnd = sf.getEnd();
+        int startCol = findIndex(sfBegin);
+        int endCol = findIndex(sfEnd);
+        if (startCol >= fromColumn && endCol <= toColumn)
+        {
+          // within cut region - delete feature
+          deleteFeature(sf);
+          amended.add(new SequenceFeature[] { sf, null });
+          continue;
+        }
+        if (startCol < fromColumn && endCol > toColumn)
+        {
+          // feature spans cut region - shift end left
+          SequenceFeature copy = new SequenceFeature(sf, sf.getBegin(),
+                  sf.getEnd() - cutWidth, sf.getFeatureGroup(),
+                  sf.getScore());
+          deleteFeature(sf);
+          addSequenceFeature(copy);
+          amended.add(new SequenceFeature[] { sf, copy });
+          continue;
+        }
+        // todo partial overlap - delete if contact feature
+      }
+    }
+
+    return amended;
+  }
 }