From: gmungoc Date: Wed, 8 Jul 2015 12:19:14 +0000 (+0100) Subject: Merge branch 'develop' of https://source.jalview.org/git/jalview.git into develop X-Git-Tag: Jalview_2_9~12^2~3 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=f0abe70b5a54b6a9730aa5f4e2bf2bd697508757;hp=1ba05544b34253204ef6410017a179c31fe5e23d Merge branch 'develop' of https://source.jalview.org/git/jalview.git into develop --- diff --git a/src/jalview/gui/AppVarna.java b/src/jalview/gui/AppVarna.java index dcb2a7d..99bcff4 100644 --- a/src/jalview/gui/AppVarna.java +++ b/src/jalview/gui/AppVarna.java @@ -43,8 +43,6 @@ import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.swing.JInternalFrame; import javax.swing.JSplitPane; @@ -66,8 +64,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener, SecondaryStructureListener, InterfaceVARNASelectionListener, VamsasSource { - private static final Pattern PAIRS_PATTERN = Pattern - .compile("[^([{<>}])]"); + private static final byte[] PAIRS = new byte[] + { '(', ')', '[', ']', '{', '}', '<', '>' }; private AppVarnaBinding vab; @@ -260,13 +258,6 @@ public class AppVarna extends JInternalFrame implements SelectionListener, showPanel(true); } - public String replaceOddGaps(String oldStr) - { - Matcher matcher = PAIRS_PATTERN.matcher(oldStr); - String newStr = matcher.replaceAll("."); - return newStr; - } - /** * Constructs a new RNA model from the given one, without gaps. Also * calculates and saves a 'shift list' @@ -282,7 +273,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener, RNA rnaTrim = new RNA(name); try { - rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(rna.getStructDBN())); + String structDBN = rna.getStructDBN(true); + rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(structDBN)); } catch (ExceptionUnmatchedClosingParentheses e2) { e2.printStackTrace(); @@ -293,7 +285,7 @@ public class AppVarna extends JInternalFrame implements SelectionListener, String seq = rnaTrim.getSeq(); StringBuilder struc = new StringBuilder(256); - struc.append(rnaTrim.getStructDBN()); + struc.append(rnaTrim.getStructDBN(true)); int ofstart = -1; int sleng = seq.length(); @@ -306,7 +298,7 @@ public class AppVarna extends JInternalFrame implements SelectionListener, ofstart = i; } /* - * mark base or base pair in the structure with * + * mark base or base & pair in the structure with * */ if (!rnaTrim.findPair(i).isEmpty()) { @@ -723,4 +715,42 @@ public class AppVarna extends JInternalFrame implements SelectionListener, return null; } } + + + /** + * Replace everything except RNA secondary structure characters with a period + * + * @param s + * @return + */ + public static String replaceOddGaps(String s) + { + if (s == null) + { + return null; + } + + // this is measured to be 10 times faster than a regex replace + boolean changed = false; + byte[] bytes = s.getBytes(); + for (int i = 0; i < bytes.length; i++) + { + boolean ok = false; + // todo check for ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) if + // wanted also + for (int j = 0; !ok && (j < PAIRS.length); j++) + { + if (bytes[i] == PAIRS[j]) + { + ok = true; + } + } + if (!ok) + { + bytes[i] = '.'; + changed = true; + } + } + return changed ? new String(bytes) : s; + } } diff --git a/test/jalview/gui/AppVarnaTest.java b/test/jalview/gui/AppVarnaTest.java new file mode 100644 index 0000000..94a3300 --- /dev/null +++ b/test/jalview/gui/AppVarnaTest.java @@ -0,0 +1,24 @@ +package jalview.gui; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNull; +import static org.testng.AssertJUnit.assertSame; + +import org.testng.annotations.Test; + +public class AppVarnaTest +{ + @Test + public void testReplaceOddGaps() + { + String struct = "{(<]}>)"; + String replaced = AppVarna.replaceOddGaps(struct); + assertEquals(struct, replaced); + assertSame(struct, replaced); + assertEquals("..{.([.<.].}.>)", + AppVarna.replaceOddGaps("..{ ([*<-]?} >)")); + assertEquals("....", AppVarna.replaceOddGaps("cgta")); + assertEquals("", AppVarna.replaceOddGaps("")); + assertNull(AppVarna.replaceOddGaps(null)); + } +}