From 63c08febd1f76249b308628611857b9698baaf8d Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Thu, 23 Jun 2016 14:01:36 +0100 Subject: [PATCH] JAL-2110 SequenceI.isProtein method --- src/jalview/datamodel/Sequence.java | 19 +++++++++++++++++++ src/jalview/datamodel/SequenceI.java | 6 ++++++ test/jalview/datamodel/SequenceTest.java | 28 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index 151d8c4..31ffdfd 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -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) * diff --git a/src/jalview/datamodel/SequenceI.java b/src/jalview/datamodel/SequenceI.java index 69eb1d4..355e271 100755 --- a/src/jalview/datamodel/SequenceI.java +++ b/src/jalview/datamodel/SequenceI.java @@ -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. * diff --git a/test/jalview/datamodel/SequenceTest.java b/test/jalview/datamodel/SequenceTest.java index 17dfcdc..5c5c5c3 100644 --- a/test/jalview/datamodel/SequenceTest.java +++ b/test/jalview/datamodel/SequenceTest.java @@ -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" }) -- 1.7.10.2