JAL-1622 code tidy (+Junit) of collateForPdb()
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 4 Jun 2015 14:39:29 +0000 (15:39 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 4 Jun 2015 14:39:29 +0000 (15:39 +0100)
src/jalview/gui/AlignViewport.java
test/jalview/gui/AlignViewportTest.java [new file with mode: 0644]

index e32e910..8ecf19d 100644 (file)
  */
 package jalview.gui;
 
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.Rectangle;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Set;
+import java.util.Vector;
+
+import javax.swing.JInternalFrame;
+import javax.swing.JOptionPane;
+
 import jalview.analysis.AlignmentUtils;
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.analysis.NJTree;
@@ -65,18 +78,6 @@ import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.ws.params.AutoCalcSetting;
 
-import java.awt.Container;
-import java.awt.Dimension;
-import java.awt.Font;
-import java.awt.Rectangle;
-import java.util.ArrayList;
-import java.util.Hashtable;
-import java.util.Set;
-import java.util.Vector;
-
-import javax.swing.JInternalFrame;
-import javax.swing.JOptionPane;
-
 /**
  * DOCUMENT ME!
  * 
@@ -659,35 +660,32 @@ public class AlignViewport extends AlignmentViewport implements
   /**
    * 
    * @param pdbEntries
-   * @return a series of SequenceI arrays, one for each PDBEntry, listing which
-   *         sequence in the alignment holds a reference to it
+   * @return an array of SequenceI arrays, one for each PDBEntry, listing which
+   *         sequences in the alignment hold a reference to it
    */
   public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
   {
-    ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
+    List<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
     for (PDBEntry pdb : pdbEntries)
     {
-      ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
-      for (int i = 0; i < alignment.getHeight(); i++)
+      List<SequenceI> seqs = new ArrayList<SequenceI>();
+      for (SequenceI sq : alignment.getSequences())
       {
-        Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
-                .getPDBId();
+        Vector<PDBEntry> pdbs = sq
+                .getDatasetSequence().getPDBId();
         if (pdbs == null)
         {
           continue;
         }
-        SequenceI sq;
-        for (int p = 0; p < pdbs.size(); p++)
+        for (PDBEntry p1 : pdbs)
         {
-          PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
           if (p1.getId().equals(pdb.getId()))
           {
-            if (!seqs.contains(sq = alignment.getSequenceAt(i)))
+            if (!seqs.contains(sq))
             {
               seqs.add(sq);
+              continue;
             }
-
-            continue;
           }
         }
       }
diff --git a/test/jalview/gui/AlignViewportTest.java b/test/jalview/gui/AlignViewportTest.java
new file mode 100644 (file)
index 0000000..ac7d84d
--- /dev/null
@@ -0,0 +1,87 @@
+package jalview.gui;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.PDBEntry.Type;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
+
+public class AlignViewportTest
+{
+
+  AlignmentI al;
+  AlignViewport testee;
+
+  @Before
+  public void setUp()
+  {
+    SequenceI seq1 = new Sequence("Seq1", "ABC");
+    SequenceI seq2 = new Sequence("Seq2", "ABC");
+    SequenceI seq3 = new Sequence("Seq3", "ABC");
+    SequenceI[] seqs = new SequenceI[]
+    { seq1, seq2, seq3 };
+    al = new Alignment(seqs);
+    al.setDataset(null);
+    testee = new AlignViewport(al);
+  }
+
+  @Test
+  public void testCollateForPdb()
+  {
+    /*
+     * Set up sequence pdb ids
+     */
+    PDBEntry pdb1 = new PDBEntry("1ABC", "A", Type.PDB, "1ABC.pdb");
+    PDBEntry pdb2 = new PDBEntry("2ABC", "A", Type.PDB, "2ABC.pdb");
+    PDBEntry pdb3 = new PDBEntry("3ABC", "A", Type.PDB, "3ABC.pdb");
+
+    /*
+     * seq1 and seq3 refer to 1ABC, seq2 to 2ABC, none to 3ABC
+     */
+    al.getSequenceAt(0).getDatasetSequence()
+            .addPDBId(
+            new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
+    al.getSequenceAt(2).getDatasetSequence()
+            .addPDBId(
+            new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
+    al.getSequenceAt(1).getDatasetSequence()
+            .addPDBId(
+            new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb"));
+    /*
+     * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
+     * the results
+     */
+    al.getSequenceAt(1).getDatasetSequence()
+            .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
+    /*
+     * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
+     */
+    al.getSequenceAt(2).getDatasetSequence()
+            .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
+
+    /*
+     * run method under test
+     */
+    SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[]
+    { pdb1, pdb2, pdb3 });
+
+    // seq1 and seq3 refer to PDBEntry[0]
+    assertEquals(2, seqs[0].length);
+    assertSame(al.getSequenceAt(0), seqs[0][0]);
+    assertSame(al.getSequenceAt(2), seqs[0][1]);
+
+    // seq2 refers to PDBEntry[1]
+    assertEquals(1, seqs[1].length);
+    assertSame(al.getSequenceAt(1), seqs[1][0]);
+
+    // no sequence refers to PDBEntry[2]
+    assertEquals(0, seqs[2].length);
+  }
+}