Merge branch 'develop' into features/JAL-2360colourSchemeApplicability
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 6 Jan 2017 09:03:27 +0000 (09:03 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 6 Jan 2017 09:03:27 +0000 (09:03 +0000)
src/jalview/appletgui/SeqPanel.java
src/jalview/datamodel/Alignment.java
src/jalview/datamodel/AlignmentI.java
src/jalview/gui/SeqPanel.java
test/jalview/datamodel/AlignmentTest.java

index 8d6e683..4278744 100644 (file)
@@ -1425,19 +1425,12 @@ public class SeqPanel extends Panel implements MouseMotionListener,
 
     if (stretchGroup == null)
     {
-      stretchGroup = av.getAlignment().findGroup(sequence);
-      if (stretchGroup != null && res > stretchGroup.getStartRes()
-              && res < stretchGroup.getEndRes())
-      {
-        av.setSelectionGroup(stretchGroup);
-      }
-      else
-      {
-        stretchGroup = null;
-      }
+      stretchGroup = av.getAlignment().findGroup(sequence, res);
+      av.setSelectionGroup(stretchGroup);
     }
 
-    else if (!stretchGroup.getSequences(null).contains(sequence)
+    if (stretchGroup == null
+            || !stretchGroup.getSequences(null).contains(sequence)
             || stretchGroup.getStartRes() > res
             || stretchGroup.getEndRes() < res)
     {
index 49f6268..a6f2bf4 100755 (executable)
@@ -357,17 +357,18 @@ public class Alignment implements AlignmentI
    * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
    */
   @Override
-  public SequenceGroup findGroup(SequenceI s)
+  public SequenceGroup findGroup(SequenceI seq, int position)
   {
     synchronized (groups)
     {
-      for (int i = 0; i < this.groups.size(); i++)
+      for (SequenceGroup sg : groups)
       {
-        SequenceGroup sg = groups.get(i);
-
-        if (sg.getSequences(null).contains(s))
+        if (sg.getSequences(null).contains(seq))
         {
-          return sg;
+          if (position >= sg.getStartRes() && position <= sg.getEndRes())
+          {
+            return sg;
+          }
         }
       }
     }
index 1b5cc0b..a0b3ff1 100755 (executable)
@@ -156,15 +156,16 @@ public interface AlignmentI extends AnnotatedCollectionI
   int findIndex(SequenceI s);
 
   /**
-   * Finds group that given sequence is part of.
+   * Returns the first group (in the order in which groups were added) that
+   * includes the given sequence and aligned position (base 0), or null if none
+   * found
    * 
-   * @param s
-   *          Sequence in alignment.
+   * @param seq
+   * @param position
    * 
-   * @return First group found for sequence. WARNING : Sequences may be members
-   *         of several groups. This method is incomplete.
+   * @return
    */
-  SequenceGroup findGroup(SequenceI s);
+  SequenceGroup findGroup(SequenceI seq, int position);
 
   /**
    * Finds all groups that a given sequence is part of.
index 8726c4a..5b87445 100644 (file)
@@ -60,7 +60,6 @@ import java.awt.event.MouseWheelListener;
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.SwingUtilities;
 import javax.swing.ToolTipManager;
@@ -1589,19 +1588,11 @@ public class SeqPanel extends JPanel implements MouseListener,
 
     if (stretchGroup == null)
     {
-      stretchGroup = av.getAlignment().findGroup(sequence);
-
-      if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
-              && (res < stretchGroup.getEndRes()))
-      {
-        av.setSelectionGroup(stretchGroup);
-      }
-      else
-      {
-        stretchGroup = null;
-      }
+      stretchGroup = av.getAlignment().findGroup(sequence, res);
+      av.setSelectionGroup(stretchGroup);
     }
-    else if (!stretchGroup.getSequences(null).contains(sequence)
+    if (stretchGroup == null
+            || !stretchGroup.getSequences(null).contains(sequence)
             || (stretchGroup.getStartRes() > res)
             || (stretchGroup.getEndRes() < res))
     {
index 0d6c8ae..7af77f5 100644 (file)
@@ -1137,4 +1137,63 @@ public class AlignmentTest
     assertSame(pep.getDatasetSequence(), cds.getDBRefs()[0].map.to);
   }
 
+  @Test(groups = { "Functional" })
+  public void testFindGroup()
+  {
+    SequenceI seq1 = new Sequence("seq1", "ABCDEF---GHI");
+    SequenceI seq2 = new Sequence("seq2", "---JKLMNO---");
+    AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2 });
+
+    assertNull(a.findGroup(null, 0));
+    assertNull(a.findGroup(seq1, 1));
+    assertNull(a.findGroup(seq1, -1));
+
+    /*
+     * add a group consisting of just "DEF"
+     */
+    SequenceGroup sg1 = new SequenceGroup();
+    sg1.addSequence(seq1, false);
+    sg1.setStartRes(3);
+    sg1.setEndRes(5);
+    a.addGroup(sg1);
+
+    assertNull(a.findGroup(seq1, 2)); // position not in group
+    assertNull(a.findGroup(seq1, 6)); // position not in group
+    assertNull(a.findGroup(seq2, 5)); // sequence not in group
+    assertSame(a.findGroup(seq1, 3), sg1); // yes
+    assertSame(a.findGroup(seq1, 4), sg1);
+    assertSame(a.findGroup(seq1, 5), sg1);
+
+    /*
+     * add a group consisting of 
+     * EF--
+     * KLMN
+     */
+    SequenceGroup sg2 = new SequenceGroup();
+    sg2.addSequence(seq1, false);
+    sg2.addSequence(seq2, false);
+    sg2.setStartRes(4);
+    sg2.setEndRes(7);
+    a.addGroup(sg2);
+
+    assertNull(a.findGroup(seq1, 2)); // unchanged
+    assertSame(a.findGroup(seq1, 3), sg1); // unchanged
+    /*
+     * if a residue is in more than one group, method returns
+     * the first found (in order groups were added)
+     */
+    assertSame(a.findGroup(seq1, 4), sg1);
+    assertSame(a.findGroup(seq1, 5), sg1);
+
+    /*
+     * seq2 only belongs to the second group
+     */
+    assertSame(a.findGroup(seq2, 4), sg2);
+    assertSame(a.findGroup(seq2, 5), sg2);
+    assertSame(a.findGroup(seq2, 6), sg2);
+    assertSame(a.findGroup(seq2, 7), sg2);
+    assertNull(a.findGroup(seq2, 3));
+    assertNull(a.findGroup(seq2, 8));
+  }
+
 }