JAL-1761 backbone type parameter for configuring which atoms are used for superpositi...
[jalview.git] / test / jalview / structures / models / AAStructureBindingModelTest.java
index c92daea..5187167 100644 (file)
  */
 package jalview.structures.models;
 
-import static org.testng.AssertJUnit.assertEquals;
-import static org.testng.AssertJUnit.assertFalse;
-import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+import java.awt.Color;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
 
-import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
-import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
@@ -35,22 +46,21 @@ import jalview.datamodel.PDBEntry;
 import jalview.datamodel.PDBEntry.Type;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
+import jalview.ext.rbvi.chimera.ChimeraCommands;
+import jalview.gui.AlignFrame;
 import jalview.gui.JvOptionPane;
+import jalview.gui.StructureViewer.ViewerType;
 import jalview.io.DataSourceType;
-import jalview.schemes.ColourSchemeI;
+import jalview.io.FileFormats;
+import jalview.io.FileLoader;
+import jalview.schemes.JalviewColourScheme;
 import jalview.structure.AtomSpec;
-import jalview.structure.StructureMappingcommandSet;
+import jalview.structure.AtomSpecModel;
+import jalview.structure.StructureCommandI;
+import jalview.structure.StructureCommandsI.AtomSpecType;
+import jalview.structure.StructureMapping;
 import jalview.structure.StructureSelectionManager;
-import jalview.structures.models.AAStructureBindingModel.SuperposeData;
-
-import java.awt.Color;
-import java.util.Arrays;
-import java.util.BitSet;
-import java.util.List;
-
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
+import junit.extensions.PA;
 
 /**
  * Unit tests for non-abstract methods of abstract base class
@@ -99,6 +109,95 @@ public class AAStructureBindingModelTest
           + "ATOM     33  CA  ALA A  10      26.790   4.320   3.172  1.00 11.98           C  \n"
           + "ATOM     39  CA AVAL A  12      24.424   3.853   6.106  0.50 13.83           C  \n";
 
+  /**
+   * Multichain PDB with identical sequences imported - Binding should correctly
+   * recover chain mappings for each derived sequence
+   */
+  private static final String PDB_4_MC = "HEADER    HYDROLASE                               09-SEP-09   3A6S              \n"
+          + "ATOM      2  CA  MET A   1      15.366 -11.648  24.854  1.00 32.05           C  \n"
+          + "ATOM     10  CA  LYS A   2      16.846  -9.215  22.340  1.00 25.68           C  \n"
+          + "ATOM     19  CA  LYS A   3      15.412  -6.335  20.343  1.00 19.42           C  \n"
+          + "ATOM     28  CA  LEU A   4      15.629  -5.719  16.616  1.00 15.49           C  \n"
+          + "ATOM     36  CA  GLN A   5      14.412  -2.295  15.567  1.00 12.19           C  \n"
+          + "ATOM   1030  CA  MET B   1      18.869  -7.572   3.432  1.00 31.52           C  \n"
+          + "ATOM   1038  CA  LYS B   2      19.182 -10.025   6.313  1.00 26.41           C  \n"
+          + "ATOM   1047  CA  LYS B   3      17.107 -12.963   7.534  1.00 19.71           C  \n"
+          + "ATOM   1056  CA  LEU B   4      16.142 -13.579  11.164  1.00 14.81           C  \n"
+          + "ATOM   1064  CA  GLN B   5      14.648 -17.005  11.785  1.00 13.38           C  \n";
+
+  // TODO: JAL-2227 - import mmCIF PISA assembly & identify master/copy chains
+
+  @Test(groups= {"Functional"})
+  public void testImportPDBPreservesChainMappings() throws IOException
+  {
+    AlignmentI importedAl = new jalview.io.FormatAdapter().readFile(
+            PDB_4_MC, DataSourceType.PASTE, FileFormats.getInstance()
+                    .forName(jalview.io.FileFormat.PDB.toString()));
+    // ideally, we would match on the actual data for the 'File' handle for
+    // pasted files,
+    // see JAL-623 - pasting is still not correctly handled...
+    PDBEntry importedPDB = new PDBEntry("3A6S", "", Type.PDB,
+            "Paste");
+    AAStructureBindingModel binder = new AAStructureBindingModel(
+            new StructureSelectionManager(), new PDBEntry[]
+            { importedPDB },
+            new SequenceI[][]
+            { importedAl.getSequencesArray() }, null)
+    {
+      
+      @Override
+      public void updateColours(Object source)
+      {
+      }
+      
+      @Override
+      public void releaseReferences(Object svl)
+      {
+      }
+      
+      @Override
+      public String[] getStructureFiles()
+      {
+        return null;
+      }
+      
+      @Override
+      public void highlightAtoms(List<AtomSpec> atoms)
+      {
+      }
+      
+      @Override
+      public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment)
+      {
+        return null;
+      }
+
+      @Override
+      protected List<String> executeCommand(StructureCommandI command,
+              boolean getReply)
+      {
+        return null;
+      }
+
+      @Override
+      protected String getModelIdForFile(String chainId)
+      {
+        return "";
+      }
+
+      @Override
+      protected ViewerType getViewerType()
+      {
+        return null;
+      }
+    };
+    String[][] chains = binder.getChains();
+    assertFalse(chains == null || chains[0] == null,
+            "No chains discovered by binding");
+    assertEquals(chains[0].length, 2);
+    assertEquals(chains[0][0], "A");
+    assertEquals(chains[0][1], "B");
+  }
   AAStructureBindingModel testee;
 
   AlignmentI al = null;
@@ -130,18 +229,39 @@ public class AAStructureBindingModelTest
     StructureSelectionManager ssm = new StructureSelectionManager();
 
     ssm.setMapping(new SequenceI[] { seq1a, seq1b }, null, PDB_1,
-            DataSourceType.PASTE);
+            DataSourceType.PASTE, null);
     ssm.setMapping(new SequenceI[] { seq2 }, null, PDB_2,
-            DataSourceType.PASTE);
+            DataSourceType.PASTE, null);
     ssm.setMapping(new SequenceI[] { seq3 }, null, PDB_3,
-            DataSourceType.PASTE);
+            DataSourceType.PASTE, null);
+
+    testee = newBindingModel(pdbFiles, seqs, ssm, null);
+  }
 
-    testee = new AAStructureBindingModel(ssm, pdbFiles, seqs, null)
+  /**
+   * A helper method to construct the test target object
+   * 
+   * @param pdbFiles
+   * @param seqs
+   * @param ssm
+   * @param alignPanel 
+   */
+  protected AAStructureBindingModel newBindingModel(PDBEntry[] pdbFiles,
+          SequenceI[][] seqs,
+          StructureSelectionManager ssm, AlignmentViewPanel avp)
+  {
+    AAStructureBindingModel model = new AAStructureBindingModel(ssm,
+            pdbFiles, seqs, null)
     {
       @Override
-      public String[] getPdbFile()
+      public String[] getStructureFiles()
       {
-        return new String[] { "INLINE1YCS", "INLINE3A6S", "INLINE1OOT" };
+        String[] files = new String[getPdbCount()];
+        for (int i = 0; i < this.getPdbCount(); i++)
+        {
+          files[i] = getPdbEntry(i).getFile();
+        }
+        return files;
       }
 
       @Override
@@ -160,65 +280,46 @@ public class AAStructureBindingModelTest
       }
 
       @Override
-      public List<String> getChainNames()
-      {
-        return null;
-      }
-
-      @Override
-      public void setJalviewColourScheme(ColourSchemeI cs)
-      {
-      }
-
-      @Override
-      public String superposeStructures(AlignmentI[] als, int[] alm,
-              ColumnSelection[] alc)
-      {
-        return null;
-      }
-
-      @Override
-      public void setBackgroundColour(Color col)
+      public SequenceRenderer getSequenceRenderer(
+              AlignmentViewPanel avp)
       {
+        return avp == null ? null
+                : new jalview.gui.SequenceRenderer(
+                        avp.getAlignViewport());
       }
 
       @Override
-      protected StructureMappingcommandSet[] getColourBySequenceCommands(
-              String[] files, SequenceRenderer sr, FeatureRenderer fr,
-              AlignViewportI viewport)
+      protected List<String> executeCommand(StructureCommandI command,
+              boolean getReply)
       {
         return null;
       }
 
+      /*
+       * for this test, let structure model ids be 0, 1, ...
+       * corresponding to first, second etc pdbfile
+       */
       @Override
-      public FeatureRenderer getFeatureRenderer(AlignmentViewPanel alignment)
+      protected String getModelIdForFile(String pdbfile)
       {
-        return null;
+        for (int i = 0; i < this.getPdbCount(); i++)
+        {
+          if (pdbfile.equals(this.getPdbEntry(i).getFile()))
+          {
+            return String.valueOf(i);
+          }
+        }
+        return "";
       }
 
       @Override
-      public SequenceRenderer getSequenceRenderer(
-              AlignmentViewPanel alignment)
+      protected ViewerType getViewerType()
       {
         return null;
       }
-
-      @Override
-      protected void colourBySequence(
-              StructureMappingcommandSet[] colourBySequenceCommands)
-      {
-      }
-
-      @Override
-      public void colourByChain()
-      {
-      }
-
-      @Override
-      public void colourByCharge()
-      {
-      }
     };
+    PA.setValue(model, "commandGenerator", new ChimeraCommands());
+    return model;
   }
 
   /**
@@ -231,10 +332,10 @@ public class AAStructureBindingModelTest
     /*
      * create a data bean to hold data per structure file
      */
-    SuperposeData[] structs = new SuperposeData[testee.getPdbFile().length];
+    AAStructureBindingModel.SuperposeData[] structs = new AAStructureBindingModel.SuperposeData[testee.getStructureFiles().length];
     for (int i = 0; i < structs.length; i++)
     {
-      structs[i] = testee.new SuperposeData(al.getWidth());
+      structs[i] = new AAStructureBindingModel.SuperposeData(al.getWidth(), "0");
     }
     /*
      * initialise BitSet of 'superposable columns' to true (would be false for
@@ -249,7 +350,7 @@ public class AAStructureBindingModelTest
     int refStructure = testee
             .findSuperposableResidues(al, matched, structs);
 
-    assertEquals(0, refStructure);
+    assertEquals(refStructure, 0);
 
     /*
      * only ungapped, structure-mapped columns are superposable
@@ -261,27 +362,28 @@ public class AAStructureBindingModelTest
     assertTrue(matched.get(4));
     assertTrue(matched.get(5)); // gap in second sequence
 
-    assertEquals("1YCS", structs[0].pdbId);
-    assertEquals("3A6S", structs[1].pdbId);
-    assertEquals("1OOT", structs[2].pdbId);
-    assertEquals("A", structs[0].chain); // ? struct has chains A _and_ B
-    assertEquals("B", structs[1].chain);
-    assertEquals("A", structs[2].chain);
+    assertEquals(structs[0].pdbId, "1YCS");
+    assertEquals(structs[1].pdbId, "3A6S");
+    assertEquals(structs[2].pdbId, "1OOT");
+    assertEquals(structs[0].chain, "A"); // ? struct has chains A _and_ B
+    assertEquals(structs[1].chain, "B");
+    assertEquals(structs[2].chain, "A");
     // the 0's for unsuperposable positions propagate down the columns:
-    assertEquals("[0, 97, 98, 99, 100, 102]",
-            Arrays.toString(structs[0].pdbResNo));
-    assertEquals("[0, 2, 0, 3, 4, 5]", Arrays.toString(structs[1].pdbResNo));
-    assertEquals("[0, 8, 0, 0, 10, 12]",
-            Arrays.toString(structs[2].pdbResNo));
+    assertEquals(Arrays.toString(structs[0].pdbResNo),
+            "[0, 97, 98, 99, 100, 102]");
+    assertEquals(Arrays.toString(structs[1].pdbResNo),
+            "[0, 2, 0, 3, 4, 5]");
+    assertEquals(Arrays.toString(structs[2].pdbResNo),
+            "[0, 8, 0, 0, 10, 12]");
   }
 
   @Test(groups = { "Functional" })
   public void testFindSuperposableResidues_hiddenColumn()
   {
-    SuperposeData[] structs = new SuperposeData[al.getHeight()];
+    AAStructureBindingModel.SuperposeData[] structs = new AAStructureBindingModel.SuperposeData[al.getHeight()];
     for (int i = 0; i < structs.length; i++)
     {
-      structs[i] = testee.new SuperposeData(al.getWidth());
+      structs[i] = new AAStructureBindingModel.SuperposeData(al.getWidth(), "0");
     }
     /*
      * initialise BitSet of 'superposable columns' to true (would be false for
@@ -299,7 +401,7 @@ public class AAStructureBindingModelTest
     int refStructure = testee
             .findSuperposableResidues(al, matched, structs);
 
-    assertEquals(0, refStructure);
+    assertEquals(refStructure, 0);
 
     // only ungapped, structure-mapped columns are not superposable
     assertFalse(matched.get(0));
@@ -309,4 +411,100 @@ public class AAStructureBindingModelTest
     assertFalse(matched.get(4)); // superposable, but hidden, column
     assertTrue(matched.get(5));
   }
-}
+
+  @Test(groups = { "Functional" })
+  public void testBuildColoursMap()
+  {
+    /*
+     * load these sequences, coloured by Strand propensity,
+     * with columns 2-4 hidden
+     */
+    String fasta = ">seq1\nMHRSQSSSGG\n>seq2\nMVRSNGGSSS";
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
+            DataSourceType.PASTE);
+    AlignmentI al = af.getViewport().getAlignment();
+    af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
+    ColumnSelection cs = new ColumnSelection();
+    cs.addElement(2);
+    cs.addElement(3);
+    cs.addElement(4);
+    af.getViewport().setColumnSelection(cs);
+    af.hideSelColumns_actionPerformed(null);
+    SequenceI seq1 = al.getSequenceAt(0);
+    SequenceI seq2 = al.getSequenceAt(1);
+    SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
+    PDBEntry[] pdbFiles = new PDBEntry[2];
+    pdbFiles[0] = new PDBEntry("PDB1", "A", Type.PDB, "seq1.pdb");
+    pdbFiles[1] = new PDBEntry("PDB2", "B", Type.PDB, "seq2.pdb");
+    StructureSelectionManager ssm = new StructureSelectionManager();
+  
+    /*
+     * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
+     */
+    HashMap<Integer, int[]> map = new HashMap<>();
+    for (int pos = 1; pos <= seq1.getLength(); pos++)
+    {
+      map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
+    }
+    StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
+            "A", map, null);
+    ssm.addStructureMapping(sm1);
+    StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
+            "B", map, null);
+    ssm.addStructureMapping(sm2);
+
+    AAStructureBindingModel binding = newBindingModel(pdbFiles, seqs, ssm,
+            af.alignPanel);
+
+    /*
+     * method under test builds a map of structures residues by colour
+     * verify the map holds what it should
+     */
+    Map<Object, AtomSpecModel> colours = binding.buildColoursMap(ssm, seqs,
+            af.alignPanel);
+    ChimeraCommands helper = new ChimeraCommands();
+    
+    /*
+     * M colour is #82827d (see strand.html help page)
+     * sequence residue 1 mapped to structure residue 21
+     */
+    Color mColor = new Color(0x82827d);
+    AtomSpecModel atomSpec = colours.get(mColor);
+    assertNotNull(atomSpec);
+    assertEquals(helper.getAtomSpec(atomSpec, AtomSpecType.RESIDUE_ONLY), "#0:21.A|#1:21.B");
+
+    /*
+     * H colour is #60609f, seq1.2 mapped to structure 0 residue 22
+     */
+    Color hColor = new Color(0x60609f);
+    atomSpec = colours.get(hColor);
+    assertNotNull(atomSpec);
+    assertEquals(helper.getAtomSpec(atomSpec, AtomSpecType.RESIDUE_ONLY), "#0:22.A");
+
+    /*
+     * V colour is #ffff00, seq2.2 mapped to structure 1 residue 22
+     */
+    Color vColor = new Color(0xffff00);
+    atomSpec = colours.get(vColor);
+    assertNotNull(atomSpec);
+    assertEquals(helper.getAtomSpec(atomSpec, AtomSpecType.RESIDUE_ONLY), "#1:22.B");
+
+    /*
+     * hidden columns are Gray (128, 128, 128)
+     * sequence positions 3-5 mapped to structure residues 23-25
+     */
+    Color gray = new Color(128, 128, 128);
+    atomSpec = colours.get(gray);
+    assertNotNull(atomSpec);
+    assertEquals(helper.getAtomSpec(atomSpec, AtomSpecType.RESIDUE_ONLY), "#0:23-25.A|#1:23-25.B");
+
+    /*
+     * S and G are both coloured #4949b6, structure residues 26-30
+     */
+    Color sgColour = new Color(0x4949b6);
+    atomSpec = colours.get(sgColour);
+    assertNotNull(atomSpec);
+    assertEquals(helper.getAtomSpec(atomSpec, AtomSpecType.RESIDUE_ONLY),
+            "#0:26-30.A|#1:26-30.B");
+  }
+}
\ No newline at end of file