Merge branch 'patch/JAL-3874_newJmolAndGradleDedup' into develop
[jalview.git] / test / jalview / datamodel / SequenceTest.java
index 4ac8c5b..2b44261 100644 (file)
@@ -20,6 +20,8 @@
  */
 package jalview.datamodel;
 
+import java.util.Locale;
+
 import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertFalse;
 import static org.testng.AssertJUnit.assertNotNull;
@@ -34,6 +36,7 @@ import jalview.commands.EditCommand.Action;
 import jalview.datamodel.PDBEntry.Type;
 import jalview.gui.JvOptionPane;
 import jalview.util.MapList;
+import jalview.ws.params.InvalidArgumentException;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -109,6 +112,23 @@ public class SequenceTest
     assertTrue(sq.isProtein());
   }
 
+  @Test(groups = ("Functional"))
+  public void testIsProteinWithXorNAmbiguityCodes()
+  {
+    // test Protein with N - poly asparagine 
+    assertTrue(new Sequence("prot", "ASDFASDFASDFNNNNNNNNN").isProtein());
+    assertTrue(new Sequence("prot", "NNNNNNNNNNNNNNNNNNNNN").isProtein());
+    // test Protein with X
+    assertTrue(new Sequence("prot", "ASDFASDFASDFXXXXXXXXX").isProtein());
+    // test DNA with X
+    assertFalse(new Sequence("prot", "ACGTACGTACGTXXXXXXXX").isProtein());
+    // test DNA with N
+    assertFalse(new Sequence("prot", "ACGTACGTACGTNNNNNNNN").isProtein());
+    // test RNA with X
+    assertFalse(new Sequence("prot", "ACGUACGUACGUXXXXXXXXX").isProtein());
+    assertFalse(new Sequence("prot", "ACGUACGUACGUNNNNNNNNN").isProtein());
+  }
+
   @Test(groups = { "Functional" })
   public void testGetAnnotation()
   {
@@ -174,6 +194,35 @@ public class SequenceTest
     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
   }
 
+
+  @Test(groups = { "Functional" })
+  public void testGetAlignmentAnnotations_forCalcIdLabelAndDescription()
+  {
+    addAnnotation("label1", "desc1", "calcId1", 1f);
+    AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
+            1f);
+    addAnnotation("label2", "desc3", "calcId3", 1f);
+    AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
+            1f);
+    addAnnotation("label5", "desc3", null, 1f);
+    addAnnotation(null, "desc3", "calcId3", 1f);
+
+    List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
+            "label2", "desc3");
+    assertEquals(1, anns.size());
+    assertSame(ann4, anns.get(0));
+    /**
+     * null matching should fail
+     */
+    assertTrue(seq.getAlignmentAnnotations("calcId3", "label2",null).isEmpty());
+    
+    assertTrue(seq.getAlignmentAnnotations("calcId2", "label3",null).isEmpty());
+    assertTrue(seq.getAlignmentAnnotations("calcId3", "label5",null).isEmpty());
+    assertTrue(seq.getAlignmentAnnotations("calcId2", null,null).isEmpty());
+    assertTrue(seq.getAlignmentAnnotations(null, "label3",null).isEmpty());
+    assertTrue(seq.getAlignmentAnnotations(null, null,null).isEmpty());
+  }
+
   /**
    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
    * setting the sequenceRef on the annotation. Adding the same annotation twice
@@ -615,9 +664,9 @@ public class SequenceTest
     assertNotNull(newDs);
     assertNotSame(ds, newDs);
     assertNotNull(sq.getDBRefs());
-    assertEquals(1, sq.getDBRefs().length);
-    assertNotSame(dbr1, sq.getDBRefs()[0]);
-    assertEquals(dbr1, sq.getDBRefs()[0]);
+    assertEquals(1, sq.getDBRefs().size());
+    assertNotSame(dbr1, sq.getDBRefs().get(0));
+    assertEquals(dbr1, sq.getDBRefs().get(0));
 
     /*
      * internal delete with sequence features
@@ -678,8 +727,8 @@ public class SequenceTest
     assertEquals(4, sq.getEnd());
     assertSame(ds, PA.getValue(sq, "datasetSequence"));
     assertNotNull(sq.getDBRefs());
-    assertEquals(1, sq.getDBRefs().length);
-    assertSame(dbr1, sq.getDBRefs()[0]);
+    assertEquals(1, sq.getDBRefs().size());
+    assertSame(dbr1, sq.getDBRefs().get(0));
   }
 
   @Test(groups = { "Functional" })
@@ -765,7 +814,7 @@ public class SequenceTest
     } catch (IllegalArgumentException e)
     {
       // TODO Jalview error/exception class for raising implementation errors
-      assertTrue(e.getMessage().toLowerCase()
+      assertTrue(e.getMessage().toLowerCase(Locale.ROOT)
               .contains("implementation error"));
     }
     assertTrue(sq.getSequenceFeatures().isEmpty());
@@ -913,12 +962,12 @@ public class SequenceTest
             new AlignmentAnnotation("Test annot", "Test annot description",
                     annots));
     Assert.assertEquals(sq.getDescription(), "Test sequence description..");
-    Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
+    Assert.assertEquals(sq.getDBRefs().size(), 5); // DBRefs are on dataset
                                                    // sequence
     Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
     Assert.assertNotNull(sq.getAnnotation());
     Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
-    Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
+    Assert.assertEquals(sq.getDatasetSequence().getDBRefs().size(), 5); // same
                                                                         // as
                                                                         // sq.getDBRefs()
     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
@@ -929,11 +978,11 @@ public class SequenceTest
 
     Assert.assertEquals(derived.getDescription(),
             "Test sequence description..");
-    Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
+    Assert.assertEquals(derived.getDBRefs().size(), 5); // come from dataset
     Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
     Assert.assertNotNull(derived.getAnnotation());
     Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
-    Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
+    Assert.assertEquals(derived.getDatasetSequence().getDBRefs().size(), 5);
     Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
             .size(), 4);
     Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
@@ -1012,10 +1061,10 @@ public class SequenceTest
     // but that doesn't distinguish it from an aligned sequence
     // which has not yet generated a dataset sequence
     // NB getDBRef looks inside dataset sequence if not null
-    DBRefEntry[] dbrefs = copy.getDBRefs();
-    assertEquals(1, dbrefs.length);
-    assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
-    assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
+    List<DBRefEntry> dbrefs = copy.getDBRefs();
+    assertEquals(1, dbrefs.size());
+    assertFalse(dbrefs.get(0) == seq1.getDBRefs().get(0));
+    assertTrue(dbrefs.get(0).equals(seq1.getDBRefs().get(0)));
   }
 
   @Test(groups = { "Functional" })
@@ -1044,9 +1093,9 @@ public class SequenceTest
 
     // getDBRef looks inside dataset sequence and this is shared,
     // so holds the same dbref objects
-    DBRefEntry[] dbrefs = copy.getDBRefs();
-    assertEquals(1, dbrefs.length);
-    assertSame(dbrefs[0], seq1.getDBRefs()[0]);
+    List<DBRefEntry> dbrefs = copy.getDBRefs();
+    assertEquals(1, dbrefs.size());
+    assertSame(dbrefs.get(0), seq1.getDBRefs().get(0));
   }
 
   /**
@@ -1145,39 +1194,39 @@ public class SequenceTest
     assertNull(sq.getDBRefs());
     DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
     sq.addDBRef(dbref);
-    assertEquals(1, sq.getDBRefs().length);
-    assertSame(dbref, sq.getDBRefs()[0]);
+    assertEquals(1, sq.getDBRefs().size());
+    assertSame(dbref, sq.getDBRefs().get(0));
 
     /*
      * change of version - new entry
      */
     DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
     sq.addDBRef(dbref2);
-    assertEquals(2, sq.getDBRefs().length);
-    assertSame(dbref, sq.getDBRefs()[0]);
-    assertSame(dbref2, sq.getDBRefs()[1]);
+    assertEquals(2, sq.getDBRefs().size());
+    assertSame(dbref, sq.getDBRefs().get(0));
+    assertSame(dbref2, sq.getDBRefs().get(1));
 
     /*
      * matches existing entry - not added
      */
     sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
-    assertEquals(2, sq.getDBRefs().length);
+    assertEquals(2, sq.getDBRefs().size());
 
     /*
      * different source = new entry
      */
     DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
     sq.addDBRef(dbref3);
-    assertEquals(3, sq.getDBRefs().length);
-    assertSame(dbref3, sq.getDBRefs()[2]);
+    assertEquals(3, sq.getDBRefs().size());
+    assertSame(dbref3, sq.getDBRefs().get(2));
 
     /*
      * different ref = new entry
      */
     DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
     sq.addDBRef(dbref4);
-    assertEquals(4, sq.getDBRefs().length);
-    assertSame(dbref4, sq.getDBRefs()[3]);
+    assertEquals(4, sq.getDBRefs().size());
+    assertSame(dbref4, sq.getDBRefs().get(3));
 
     /*
      * matching ref with a mapping - map updated
@@ -1187,8 +1236,8 @@ public class SequenceTest
         1, 1 }, 3, 1));
     dbref5.setMap(map);
     sq.addDBRef(dbref5);
-    assertEquals(4, sq.getDBRefs().length);
-    assertSame(dbref4, sq.getDBRefs()[3]);
+    assertEquals(4, sq.getDBRefs().size());
+    assertSame(dbref4, sq.getDBRefs().get(3));
     assertSame(map, dbref4.getMap());
 
     /*
@@ -1198,8 +1247,8 @@ public class SequenceTest
     DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
             dbref2.getAccessionId());
     sq.addDBRef(dbref6);
-    assertEquals(4, sq.getDBRefs().length);
-    assertSame(dbref2, sq.getDBRefs()[1]);
+    assertEquals(4, sq.getDBRefs().size());
+    assertSame(dbref2, sq.getDBRefs().get(1));
     assertEquals("3", dbref2.getVersion());
 
     /*
@@ -1209,8 +1258,8 @@ public class SequenceTest
     DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
             dbref3.getAccessionId());
     sq.addDBRef(dbref7);
-    assertEquals(4, sq.getDBRefs().length);
-    assertSame(dbref3, sq.getDBRefs()[2]);
+    assertEquals(4, sq.getDBRefs().size());
+    assertSame(dbref3, sq.getDBRefs().get(2));
     assertEquals("3", dbref2.getVersion());
   }
 
@@ -1224,7 +1273,7 @@ public class SequenceTest
     assertTrue(primaryDBRefs.isEmpty());
 
     // empty dbrefs
-    sq.setDBRefs(new DBRefEntry[] {});
+       sq.setDBRefs(null);
     primaryDBRefs = sq.getPrimaryDBRefs();
     assertTrue(primaryDBRefs.isEmpty());
 
@@ -1413,6 +1462,21 @@ public class SequenceTest
     seq.addPDBId(pdbe5);
     assertEquals(4, seq.getAllPDBEntries().size());
     assertSame(pdbe5, seq.getAllPDBEntries().get(3));
+    
+    // add with a fake pdbid 
+    // (models don't have an embedded ID)
+    String realId = "RealIDQ";
+    PDBEntry pdbe6 = new PDBEntry(realId,null,Type.PDB,"real/localpath");
+    PDBEntry pdbe7 = new PDBEntry("RealID/real/localpath","C",Type.MMCIF,"real/localpath");
+    pdbe7.setFakedPDBId(true);
+    seq.addPDBId(pdbe6);
+    assertEquals(5,seq.getAllPDBEntries().size());
+    seq.addPDBId(pdbe7);
+    assertEquals(5,seq.getAllPDBEntries().size());
+    assertFalse(pdbe6.fakedPDBId());
+    assertSame(pdbe6,seq.getAllPDBEntries().get(4));
+    assertEquals("C",pdbe6.getChainCode());
+    assertEquals(realId, pdbe6.getId());
   }
 
   @Test(
@@ -2174,4 +2238,25 @@ public class SequenceTest
     assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
 
   }
+  @Test(groups= {"Functional"})
+  public void testTransferAnnotation() {
+    Sequence origSeq = new Sequence("MYSEQ","THISISASEQ");
+    Sequence toSeq = new Sequence("MYSEQ","THISISASEQ");
+    origSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, true));
+    toSeq.transferAnnotation(origSeq, null);
+    assertTrue(toSeq.getDBRefs().size()==1);
+    
+    assertTrue(toSeq.getDBRefs().get(0).isCanonical());
+    
+    // check for promotion of non-canonical 
+    // to canonical (e.g. fetch-db-refs on a jalview project pre 2.11.2)
+    toSeq.setDBRefs(null);
+    toSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, false));
+    toSeq.transferAnnotation(origSeq, null);
+    assertTrue(toSeq.getDBRefs().size()==1);
+    
+    assertTrue("Promotion of non-canonical DBRefEntry failed",toSeq.getDBRefs().get(0).isCanonical());
+    
+    
+  }
 }