From 4ed0cd5f8477ba52a9f5b19b021f8f6183f9dd61 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 29 Aug 2016 10:10:20 +0100 Subject: [PATCH] JAL-1841 robustness fix --- src/jalview/analysis/Rna.java | 38 ++++++++++++++++ src/jalview/renderer/AnnotationRenderer.java | 2 +- test/jalview/analysis/RnaTest.java | 63 +++++++++++++++++++++++--- 3 files changed, 96 insertions(+), 7 deletions(-) diff --git a/src/jalview/analysis/Rna.java b/src/jalview/analysis/Rna.java index f497f0e..e3d999a 100644 --- a/src/jalview/analysis/Rna.java +++ b/src/jalview/analysis/Rna.java @@ -52,6 +52,19 @@ public class Rna } /** + * Answers true if the string is a valid open pair rna secondary structure + * symbol. Currently accepts A-Z, ([{< + * + * @param s + * @return + */ + public static boolean isOpeningParenthesis(String s) + { + return s != null && s.length() == 1 + && isOpeningParenthesis(s.charAt(0)); + } + + /** * Answers true if the character is a valid close pair rna secondary structure * symbol. Currently accepts a-z, )]}> * @@ -64,6 +77,19 @@ public class Rna } /** + * Answers true if the string is a valid close pair rna secondary structure + * symbol. Currently accepts a-z, )]}> + * + * @param s + * @return + */ + public static boolean isClosingParenthesis(String s) + { + return s != null && s.length() == 1 + && isClosingParenthesis(s.charAt(0)); + } + + /** * Returns the matching open pair symbol for the given closing symbol. * Currently returns A-Z for a-z, or ([{< for )]}>, or the input symbol if it * is not a valid closing symbol. @@ -298,6 +324,18 @@ public class Rna } /** + * Answers true if the string is a recognised symbol for RNA secondary + * structure. Currently accepts a-z, A-Z, ()[]{}<>. + * + * @param s + * @return + */ + public static boolean isRnaSecondaryStructureSymbol(String s) + { + return isOpeningParenthesis(s) || isClosingParenthesis(s); + } + + /** * Translates a string to RNA secondary structure representation. Returns the * string with any non-SS characters changed to spaces. Accepted characters * are a-z, A-Z, and (){}[]<> brackets. diff --git a/src/jalview/renderer/AnnotationRenderer.java b/src/jalview/renderer/AnnotationRenderer.java index 94015de..82f6ffb 100644 --- a/src/jalview/renderer/AnnotationRenderer.java +++ b/src/jalview/renderer/AnnotationRenderer.java @@ -220,7 +220,7 @@ public class AnnotationRenderer || !dc.equals(row_annotations[column].displayCharacter); // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream); // If a closing base pair half of the stem, display a backward arrow - if (column > 0 && Rna.isClosingParenthesis(dc.charAt(0))) + if (column > 0 && Rna.isClosingParenthesis(dc)) { if (diffupstream) diff --git a/test/jalview/analysis/RnaTest.java b/test/jalview/analysis/RnaTest.java index 95c37ac..f33525f 100644 --- a/test/jalview/analysis/RnaTest.java +++ b/test/jalview/analysis/RnaTest.java @@ -128,24 +128,34 @@ public class RnaTest Rna.getRNASecStrucState("a.[K-]z}?{Q b(w)p> are closing bracket symbols */ for (int i = 0; i <= 255; i++) { - boolean isClosing = Rna.isClosingParenthesis((char) i); + boolean isClosingChar = Rna.isClosingParenthesis((char) i); + boolean isClosingString = Rna.isClosingParenthesis(String + .valueOf((char) i)); if ((i >= 'a' && i <= 'z') || i == ')' || i == '}' || i == ']' || i == '>') { - assertTrue(String.format("close base pair %c", i), isClosing); + assertTrue(String.format("close base pair %c", i), isClosingChar); + assertTrue(String.format("close base pair %c", i), isClosingString); } else { - assertFalse(String.format("close base pair %c", i), isClosing); + assertFalse(String.format("close base pair %c", i), isClosingChar); + assertFalse(String.format("close base pair %c", i), isClosingString); } + assertFalse(Rna.isClosingParenthesis(String.valueOf((char) i) + " ")); } } @@ -178,6 +188,9 @@ public class RnaTest } } + /** + * Tests for isOpeningParenthesis with char or String argument + */ @Test(groups = { "Functional" }) public void testIsOpeningParenthesis() { @@ -186,16 +199,21 @@ public class RnaTest */ for (int i = 0; i <= 255; i++) { - boolean isOpening = Rna.isOpeningParenthesis((char) i); + boolean isOpeningChar = Rna.isOpeningParenthesis((char) i); + boolean isOpeningString = Rna.isOpeningParenthesis(String + .valueOf((char) i)); if ((i >= 'A' && i <= 'Z') || i == '(' || i == '{' || i == '[' || i == '<') { - assertTrue(String.format("Open base pair %c", i), isOpening); + assertTrue(String.format("Open base pair %c", i), isOpeningChar); + assertTrue(String.format("Open base pair %c", i), isOpeningString); } else { - assertFalse(String.format("Open base pair %c", i), isOpening); + assertFalse(String.format("Open base pair %c", i), isOpeningChar); + assertFalse(String.format("Open base pair %c", i), isOpeningString); } + assertFalse(Rna.isOpeningParenthesis(String.valueOf((char) i) + " ")); } } @@ -225,4 +243,37 @@ public class RnaTest } } } + + /** + * Tests for isRnaSecondaryStructureSymbol with char or String argument + */ + @Test(groups = { "Functional" }) + public void testIsRnaSecondaryStructureSymbol() + { + assertFalse(Rna.isRnaSecondaryStructureSymbol(null)); + + /* + * only A-Z, a-z, ()[]{}<> are valid symbols + */ + for (int i = 0; i <= 255; i++) + { + boolean isValidChar = Rna.isRnaSecondaryStructureSymbol((char) i); + boolean isValidString = Rna.isRnaSecondaryStructureSymbol(String + .valueOf((char) i)); + if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || i == '(' + || i == ')' || i == '{' || i == '}' || i == '[' || i == ']' + || i == '<' || i == '>') + { + assertTrue(String.format("close base pair %c", i), isValidChar); + assertTrue(String.format("close base pair %c", i), isValidString); + } + else + { + assertFalse(String.format("close base pair %c", i), isValidChar); + assertFalse(String.format("close base pair %c", i), isValidString); + } + assertFalse(Rna.isRnaSecondaryStructureSymbol(String + .valueOf((char) i) + " ")); + } + } } -- 1.7.10.2