JAL-2110 SequenceI.isProtein method
authorJim Procter <jprocter@issues.jalview.org>
Thu, 23 Jun 2016 13:01:36 +0000 (14:01 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 23 Jun 2016 13:01:36 +0000 (14:01 +0100)
src/jalview/datamodel/Sequence.java
src/jalview/datamodel/SequenceI.java
test/jalview/datamodel/SequenceTest.java

index 151d8c4..31ffdfd 100755 (executable)
@@ -1086,6 +1086,25 @@ public class Sequence extends ASequence implements SequenceI
     return new Sequence(this);
   }
 
+  private boolean _isNa;
+
+  private long _seqhash = 0;
+
+  @Override
+  public boolean isProtein()
+  {
+    if (datasetSequence != null)
+    {
+      return datasetSequence.isProtein();
+    }
+    if (_seqhash != sequence.hashCode())
+    {
+      _seqhash = sequence.hashCode();
+      _isNa=jalview.util.Comparison.isNucleotide(new SequenceI[] { this });
+    }
+    return !_isNa;
+  };
+
   /*
    * (non-Javadoc)
    * 
index 69eb1d4..355e271 100755 (executable)
@@ -219,6 +219,12 @@ public interface SequenceI extends ASequenceI
   public int[] findPositionMap();
 
   /**
+   * 
+   * @return true if sequence is composed of amino acid characters
+   */
+  public boolean isProtein();
+
+  /**
    * Delete a range of aligned sequence columns, creating a new dataset sequence
    * if necessary and adjusting start and end positions accordingly.
    * 
index 17dfcdc..5c5c5c3 100644 (file)
@@ -65,6 +65,20 @@ public class SequenceTest
     assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
   }
 
+  @Test(groups = ("Functional"))
+  public void testIsProtein()
+  {
+    // test Protein
+    assertTrue(new Sequence("prot","ASDFASDFASDF").isProtein());
+    // test DNA
+    assertFalse(new Sequence("prot","ACGTACGTACGT").isProtein());
+    // test RNA
+    SequenceI sq = new Sequence("prot","ACGUACGUACGU");
+    assertFalse(sq.isProtein());
+    // change sequence, should trigger an update of cached result
+    sq.setSequence("ASDFASDFADSF");
+    assertTrue(sq.isProtein());
+  }
   @Test(groups = { "Functional" })
   public void testGetAnnotation()
   {
@@ -388,6 +402,20 @@ public class SequenceTest
   }
 
   /**
+   * test createDatasetSequence behaves to doc
+   */
+  @Test(groups = { "Functional" })
+  public void testCreateDatasetSequence()
+  {
+    SequenceI sq = new Sequence("my","ASDASD");
+    assertNull(sq.getDatasetSequence());
+    SequenceI rds = sq.createDatasetSequence();
+    assertNotNull(rds);
+    assertNull(rds.getDatasetSequence());
+    assertEquals(sq.getDatasetSequence(), rds);
+  }
+
+  /**
    * Test for deriveSequence applied to a sequence with a dataset
    */
   @Test(groups = { "Functional" })