Merge branch 'Release_2_8_3_Branch' of https://source.jalview.org/git/jalview into...
[jalview.git] / test / jalview / util / DBRefUtilsTest.java
diff --git a/test/jalview/util/DBRefUtilsTest.java b/test/jalview/util/DBRefUtilsTest.java
new file mode 100644 (file)
index 0000000..e606665
--- /dev/null
@@ -0,0 +1,222 @@
+package jalview.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.DBRefSource;
+import jalview.datamodel.Mapping;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
+
+public class DBRefUtilsTest
+{
+
+  /**
+   * Test the method that selects DBRefEntry items whose source is in a supplied
+   * list
+   */
+  @Test
+  public void testSelectRefs()
+  {
+    assertNull(DBRefUtils.selectRefs(null, null));
+    assertNull(DBRefUtils.selectRefs(null, DBRefSource.CODINGDBS));
+
+    DBRefEntry ref1 = new DBRefEntry("EMBL", "1.2", "A12345");
+    DBRefEntry ref2 = new DBRefEntry("UNIPROT", "1.2", "A12346");
+    // Source is converted to upper-case by this constructor!
+    DBRefEntry ref3 = new DBRefEntry("Uniprot", "1.2", "A12347");
+    DBRefEntry[] dbrefs = new DBRefEntry[]
+    { ref1, ref2, ref3 };
+    String[] sources = new String[]
+    { "EMBL", "UNIPROT" };
+
+    DBRefEntry[] selected = DBRefUtils.selectRefs(dbrefs, sources);
+    assertEquals(3, selected.length);
+    assertSame(ref1, selected[0]);
+    assertSame(ref2, selected[1]);
+    assertSame(ref3, selected[2]);
+
+    sources = new String[]
+    { "EMBL" };
+    selected = DBRefUtils.selectRefs(dbrefs, sources);
+    assertEquals(1, selected.length);
+    assertSame(ref1, selected[0]);
+
+    sources = new String[]
+    { "UNIPROT" };
+    selected = DBRefUtils.selectRefs(dbrefs, sources);
+    assertEquals(2, selected.length);
+    assertSame(ref2, selected[0]);
+    assertSame(ref3, selected[1]);
+
+    sources = new String[]
+    { "Uniprot", "EMBLCDS" };
+    selected = DBRefUtils.selectRefs(dbrefs, sources);
+    assertNull(selected);
+  }
+
+  /**
+   * Test the method that converts (currently three) database names to a
+   * canonical name (not case-sensitive)
+   */
+  @Test
+  public void testGetCanonicalName()
+  {
+    assertNull(DBRefUtils.getCanonicalName(null));
+    assertEquals("", DBRefUtils.getCanonicalName(""));
+    assertEquals("PDB", DBRefUtils.getCanonicalName("pdb"));
+    assertEquals("PDB", DBRefUtils.getCanonicalName("Pdb"));
+    assertEquals("UNIPROT",
+            DBRefUtils.getCanonicalName("uniprotkb/swiss-prot"));
+    assertEquals("UNIPROT", DBRefUtils.getCanonicalName("uniprotkb/trembl"));
+    assertEquals("UNIPROT",
+            DBRefUtils.getCanonicalName("UNIPROTKB/SWISS-PROT"));
+    assertEquals("UNIPROT", DBRefUtils.getCanonicalName("UNIPROTKB/TREMBL"));
+    assertEquals("UNIPROTKB/SWISS-CHEESE",
+            DBRefUtils.getCanonicalName("UNIPROTKB/SWISS-CHEESE"));
+  }
+
+  @Test
+  public void testIsDasCoordinateSystem()
+  {
+    assertFalse(DBRefUtils.isDasCoordinateSystem(null, null));
+    assertFalse(DBRefUtils.isDasCoordinateSystem("pdbresnum", null));
+    assertFalse(DBRefUtils.isDasCoordinateSystem(null, new DBRefEntry(
+            "PDB", "v1", "a1")));
+
+    assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
+            new DBRefEntry("PDB", "v1", "a1")));
+    assertTrue(DBRefUtils.isDasCoordinateSystem("PDBRESNUM",
+            new DBRefEntry("PDB", "v1", "a1")));
+    // "pdb" is converted to upper-case in DBRefEntry constructor
+    assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
+            new DBRefEntry("pdb", "v1", "a1")));
+    assertFalse(DBRefUtils.isDasCoordinateSystem("pdb", new DBRefEntry(
+            "pdb", "v1", "a1")));
+
+    assertTrue(DBRefUtils.isDasCoordinateSystem("UNIPROT", new DBRefEntry(
+            "Uniprot", "v1", "a1")));
+    assertTrue(DBRefUtils.isDasCoordinateSystem("Uniprot", new DBRefEntry(
+            "UNIPROT", "v1", "a1")));
+    assertFalse(DBRefUtils.isDasCoordinateSystem("UNIPROTKB",
+            new DBRefEntry(
+            "pdb", "v1", "a1")));
+
+    assertTrue(DBRefUtils.isDasCoordinateSystem("EMBL", new DBRefEntry(
+            "EMBL", "v1", "a1")));
+    assertTrue(DBRefUtils.isDasCoordinateSystem("embl", new DBRefEntry(
+            "embl", "v1", "a1")));
+  }
+
+  /**
+   * Test 'parsing' a DBRef - non PDB case
+   */
+  @Test
+  public void testParseToDbRef()
+  {
+    SequenceI seq = new Sequence("Seq1", "ABCD");
+    DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "EMBL", "1.2", "a7890");
+    DBRefEntry[] refs = seq.getDBRef();
+    assertEquals(1, refs.length);
+    assertSame(ref, refs[0]);
+    assertEquals("EMBL", ref.getSource());
+    assertEquals("1.2", ref.getVersion());
+    assertEquals("a7890", ref.getAccessionId());
+    assertNull(seq.getPDBId());
+  }
+
+  /**
+   * Test 'parsing' a DBRef - Stockholm PDB format
+   */
+  @Test
+  public void testParseToDbRef_PDB()
+  {
+    SequenceI seq = new Sequence("Seq1", "ABCD");
+    DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "pdb", "1.2",
+            "1WRI A; 7-80;");
+    DBRefEntry[] refs = seq.getDBRef();
+    assertEquals(1, refs.length);
+    assertSame(ref, refs[0]);
+    assertEquals("PDB", ref.getSource());
+    assertEquals("1.2", ref.getVersion());
+    // DBRef id is pdbId + chain code
+    assertEquals("1WRIA", ref.getAccessionId());
+    assertEquals(1, seq.getPDBId().size());
+    PDBEntry pdbRef = seq.getPDBId().get(0);
+    assertEquals("1WRI", pdbRef.getId());
+    assertNull(pdbRef.getFile());
+    assertEquals("A", pdbRef.getProperty().get("CHAIN"));
+    assertNull(pdbRef.getType());
+  }
+
+  /**
+   * Test the method that searches for matches references - case when we are
+   * matching a reference with no mappings
+   */
+  @Test
+  public void testSearchRefs_noMapping()
+  {
+    DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
+
+    DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
+    // constructor changes embl to EMBL
+    DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
+    // constructor does not upper-case accession id
+    DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
+    DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
+    // ref5 matches although it has a mapping - ignored
+    DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
+    ref5.setMap(new Mapping(new MapList(new int[]
+    { 1, 1 }, new int[]
+    { 1, 1 }, 1, 1)));
+
+    DBRefEntry[] matches = DBRefUtils.searchRefs(new DBRefEntry[]
+    { ref1, ref2, ref3, ref4, ref5 }, target);
+    assertEquals(3, matches.length);
+    assertSame(ref1, matches[0]);
+    assertSame(ref2, matches[1]);
+    assertSame(ref5, matches[2]);
+  }
+
+  /**
+   * Test the method that searches for matches references - case when we are
+   * matching a reference with a mapping
+   */
+  @Test
+  public void testSearchRefs_withMapping()
+  {
+    DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
+    final Mapping map1 = new Mapping(new MapList(new int[]
+    { 1, 1 }, new int[]
+    { 1, 1 }, 1, 1));
+    target.setMap(map1);
+
+    // these all match target iff mappings match
+    DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // no map: matches
+    DBRefEntry ref2 = new DBRefEntry("EMBL", "1", "A1234"); // =map: matches
+    final Mapping map2 = new Mapping(new MapList(new int[]
+    { 1, 1 }, new int[]
+    { 1, 1 }, 1, 1));
+    ref2.setMap(map2);
+
+    // different map: no match
+    DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1234");
+    final Mapping map3 = new Mapping(new MapList(new int[]
+    { 1, 1 }, new int[]
+    { 1, 1 }, 2, 2));
+    ref3.setMap(map3);
+
+    DBRefEntry[] matches = DBRefUtils.searchRefs(new DBRefEntry[]
+    { ref1, ref2, ref3 }, target);
+    assertEquals(2, matches.length);
+    assertSame(ref1, matches[0]);
+    assertSame(ref2, matches[1]);
+  }
+}