Merge branch 'develop' into features/JAL-2446NCList
[jalview.git] / src / jalview / controller / AlignViewController.java
index 9451c3b..84036ab 100644 (file)
@@ -33,6 +33,7 @@ import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.io.DataSourceType;
 import jalview.io.FeaturesFile;
 import jalview.util.MessageManager;
 
@@ -237,64 +238,63 @@ public class AlignViewController implements AlignViewControllerI
     int nseq = 0;
     for (SequenceI sq : seqs)
     {
-      boolean sequenceHasFeature = false;
       if (sq != null)
       {
-        SequenceFeature[] sfs = sq.getSequenceFeatures();
-        if (sfs != null)
+        int ist = sq.findPosition(sqcol.getStartRes());
+        int iend = sq.findPosition(sqcol.getEndRes()); // see JAL-2526
+        List<SequenceFeature> sfs = sq.getFeatures().findFeatures(ist,
+                iend, featureType);
+        if (!sfs.isEmpty())
         {
-          /*
-           * check whether the feature start/end (base 1) 
-           * overlaps the selection start/end
-           */
-          int ist = sq.findIndex(sq.getStart());
-          int iend = sq.findIndex(sq.getEnd());
-          if (iend < startPosition || ist > endPosition)
-          {
-            // sequence not in region
-            continue;
-          }
-          for (SequenceFeature sf : sfs)
+          nseq++;
+        }
+        for (SequenceFeature sf : sfs)
+        {
+          // future functionality - featureType == null means mark columns
+          // containing all displayed features
+          if (sf != null && (featureType.equals(sf.getType())))
           {
-            // future functionality - featureType == null means mark columns
-            // containing all displayed features
-            if (sf != null && (featureType.equals(sf.getType())))
-            {
-              // optimisation - could consider 'spos,apos' like cursor argument
-              // - findIndex wastes time by starting from first character and
-              // counting
+            int sfStartCol = sq.findIndex(sf.getBegin());
+            int sfEndCol = sq.findIndex(sf.getEnd()); // inefficient - JAL-2526
 
-              int i = sq.findIndex(sf.getBegin());
-              int j = sq.findIndex(sf.getEnd());
-              if (j < startPosition || i > endPosition)
-              {
-                // feature is outside selected region
-                continue;
-              }
-              sequenceHasFeature = true;
-              if (i < startPosition)
-              {
-                i = startPosition;
-              }
-              if (i < ist)
-              {
-                i = ist;
-              }
-              if (j > endPosition)
+            if (sf.isContactFeature())
+            {
+              /*
+               * 'contact' feature - check for 'start' or 'end'
+               * position within the selected region
+               */
+              if (sfStartCol >= startPosition && sfStartCol <= endPosition)
               {
-                j = endPosition;
+                bs.set(sfStartCol - 1);
               }
-              for (; i <= j; i++)
+              if (sfEndCol >= startPosition && sfEndCol <= endPosition)
               {
-                bs.set(i - 1); // convert to base 0
+                bs.set(sfEndCol - 1);
               }
+              continue;
             }
-          }
-        }
 
-        if (sequenceHasFeature)
-        {
-          nseq++;
+            /*
+             * contiguous feature - select feature positions (if any) 
+             * within the selected region
+             */
+            if (sfStartCol < startPosition)
+            {
+              sfStartCol = startPosition;
+            }
+            if (sfStartCol < ist)
+            {
+              sfStartCol = ist;
+            }
+            if (sfEndCol > endPosition)
+            {
+              sfEndCol = endPosition;
+            }
+            for (; sfStartCol <= sfEndCol; sfStartCol++)
+            {
+              bs.set(sfStartCol - 1); // convert to base 0
+            }
+          }
         }
       }
     }
@@ -349,7 +349,7 @@ public class AlignViewController implements AlignViewControllerI
   }
 
   @Override
-  public boolean parseFeaturesFile(String file, String protocol,
+  public boolean parseFeaturesFile(String file, DataSourceType protocol,
           boolean relaxedIdMatching)
   {
     boolean featuresFile = false;