From: gmungoc Date: Tue, 16 May 2017 14:00:41 +0000 (+0100) Subject: JAL-2505 set helix number as feature group in SequenceFeature X-Git-Tag: Release_2_10_3b1~271 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=beceff8fb262b9b1c5d119855b75e5329c252a71;p=jalview.git JAL-2505 set helix number as feature group in SequenceFeature constructor --- diff --git a/src/jalview/analysis/Rna.java b/src/jalview/analysis/Rna.java index 89c5c30..6bb123a 100644 --- a/src/jalview/analysis/Rna.java +++ b/src/jalview/analysis/Rna.java @@ -31,10 +31,11 @@ import jalview.datamodel.SequenceFeature; import jalview.util.MessageManager; import java.util.ArrayList; +import java.util.HashMap; import java.util.Hashtable; import java.util.List; +import java.util.Map; import java.util.Stack; -import java.util.Vector; public class Rna { @@ -132,11 +133,11 @@ public class Rna * @return * @throw {@link WUSSParseException} */ - public static Vector getSimpleBPs(CharSequence line) + protected static List getSimpleBPs(CharSequence line) throws WUSSParseException { Hashtable> stacks = new Hashtable>(); - Vector pairs = new Vector(); + List pairs = new ArrayList(); int i = 0; while (i < line.length()) { @@ -195,25 +196,9 @@ public class Rna return pairs; } - public static SequenceFeature[] getBasePairs(List bps) - throws WUSSParseException - { - SequenceFeature[] outPairs = new SequenceFeature[bps.size()]; - for (int p = 0; p < bps.size(); p++) - { - SimpleBP bp = bps.get(p); - outPairs[p] = new SequenceFeature("RNA helix", "", "", bp.getBP5(), - bp.getBP3(), ""); - } - return outPairs; - } + - public static List getModeleBP(CharSequence line) - throws WUSSParseException - { - Vector bps = getSimpleBPs(line); - return new ArrayList(bps); - } + /** * Function to get the end position corresponding to a given start position @@ -237,23 +222,22 @@ public class Rna * @param pairs * Array of SequenceFeature (output from Rna.GetBasePairs) */ - public static void HelixMap(SequenceFeature[] pairs) + protected static void computeHelixMap(SequenceFeature[] pairs) { int helix = 0; // Number of helices/current helix int lastopen = 0; // Position of last open bracket reviewed int lastclose = 9999999; // Position of last close bracket reviewed - int i = pairs.length; // Number of pairs int open; // Position of an open bracket under review int close; // Position of a close bracket under review int j; // Counter - Hashtable helices = new Hashtable(); + Map helices = new HashMap(); // Keep track of helix number for each position // Go through each base pair and assign positions a helix - for (i = 0; i < pairs.length; i++) + for (int i = 0; i < pairs.length; i++) { open = pairs[i].getBegin(); @@ -307,7 +291,6 @@ public class Rna lastopen = open; lastclose = close; - } } @@ -500,4 +483,76 @@ public class Rna return c; } } + + public static SequenceFeature[] getHelixMap(CharSequence rnaAnnotation) + throws WUSSParseException + { + List result = new ArrayList(); + + int helix = 0; // Number of helices/current helix + int lastopen = 0; // Position of last open bracket reviewed + int lastclose = 9999999; // Position of last close bracket reviewed + + Map helices = new HashMap(); + // Keep track of helix number for each position + + // Go through each base pair and assign positions a helix + List bps = getSimpleBPs(rnaAnnotation); + for (SimpleBP basePair : bps) + { + final int open = basePair.getBP5(); + final int close = basePair.getBP3(); + + // System.out.println("open " + open + " close " + close); + // System.out.println("lastclose " + lastclose + " lastopen " + lastopen); + + // we're moving from right to left based on closing pair + /* + * catch things like <<..>>..<<..>> | + */ + if (open > lastclose) + { + helix++; + } + + /* + * catch things like <<..<<..>>..<<..>>>> | + */ + int j = bps.size() - 1; + while (j >= 0) + { + int popen = bps.get(j).getBP5(); + + // System.out.println("j " + j + " popen " + popen + " lastopen " + // +lastopen + " open " + open); + if ((popen < lastopen) && (popen > open)) + { + if (helices.containsValue(popen) + && ((helices.get(popen)) == helix)) + { + continue; + } + else + { + helix++; + break; + } + } + j -= 1; + } + + // Put positions and helix information into the hashtable + helices.put(open, helix); + helices.put(close, helix); + + // Record helix as featuregroup + result.add(new SequenceFeature("RNA helix", "", "", open, close, + String.valueOf(helix))); + + lastopen = open; + lastclose = close; + } + + return result.toArray(new SequenceFeature[result.size()]); + } } diff --git a/src/jalview/datamodel/AlignmentAnnotation.java b/src/jalview/datamodel/AlignmentAnnotation.java index 1594f2b..56bfd74 100755 --- a/src/jalview/datamodel/AlignmentAnnotation.java +++ b/src/jalview/datamodel/AlignmentAnnotation.java @@ -96,14 +96,13 @@ public class AlignmentAnnotation * Updates the _rnasecstr field Determines the positions that base pair and * the positions of helices based on secondary structure from a Stockholm file * - * @param RNAannot + * @param rnaAnnotation */ - private void _updateRnaSecStr(CharSequence RNAannot) + private void _updateRnaSecStr(CharSequence rnaAnnotation) { try { - bps = Rna.getModeleBP(RNAannot); - _rnasecstr = Rna.getBasePairs(bps); + _rnasecstr = Rna.getHelixMap(rnaAnnotation); invalidrnastruc = -1; } catch (WUSSParseException px) { @@ -114,8 +113,6 @@ public class AlignmentAnnotation { return; } - Rna.HelixMap(_rnasecstr); - // setRNAStruc(RNAannot); if (_rnasecstr != null && _rnasecstr.length > 0) { @@ -273,12 +270,6 @@ public class AlignmentAnnotation } } - // JBPNote: what does this do ? - public void ConcenStru(CharSequence RNAannot) throws WUSSParseException - { - bps = Rna.getModeleBP(RNAannot); - } - /** * Creates a new AlignmentAnnotation object. * diff --git a/test/jalview/analysis/RnaTest.java b/test/jalview/analysis/RnaTest.java index 814d2d4..1faf3f2 100644 --- a/test/jalview/analysis/RnaTest.java +++ b/test/jalview/analysis/RnaTest.java @@ -27,9 +27,10 @@ import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.fail; import jalview.analysis.SecStrConsensus.SimpleBP; +import jalview.datamodel.SequenceFeature; import jalview.gui.JvOptionPane; -import java.util.Vector; +import java.util.List; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -48,7 +49,7 @@ public class RnaTest public void testGetSimpleBPs() throws WUSSParseException { String rna = "([{})]"; // JAL-1081 example - Vector bps = Rna.getSimpleBPs(rna); + List bps = Rna.getSimpleBPs(rna); assertEquals(3, bps.size()); /* @@ -313,4 +314,54 @@ public class RnaTest .valueOf((char) i) + " ")); } } + + @Test(groups = "Functional") + public void testGetHelixMap_oneHelix() throws WUSSParseException + { + String rna = ".(..[{.<..>}..].)"; + SequenceFeature[] sfs = Rna.getHelixMap(rna); + assertEquals(4, sfs.length); + + /* + * pairs are added in the order in which the closing bracket is found + * (see testGetSimpleBPs) + */ + assertEquals(7, sfs[0].getBegin()); + assertEquals(10, sfs[0].getEnd()); + assertEquals("0", sfs[0].getFeatureGroup()); + assertEquals(5, sfs[1].getBegin()); + assertEquals(11, sfs[1].getEnd()); + assertEquals("0", sfs[1].getFeatureGroup()); + assertEquals(4, sfs[2].getBegin()); + assertEquals(14, sfs[2].getEnd()); + assertEquals("0", sfs[2].getFeatureGroup()); + assertEquals(1, sfs[3].getBegin()); + assertEquals(16, sfs[3].getEnd()); + assertEquals("0", sfs[3].getFeatureGroup()); + } + + @Test(groups = "Functional") + public void testGetHelixMap_twoHelices() throws WUSSParseException + { + String rna = ".([.)]..{.<}.>"; + SequenceFeature[] sfs = Rna.getHelixMap(rna); + assertEquals(4, sfs.length); + + /* + * pairs are added in the order in which the closing bracket is found + * (see testGetSimpleBPs) + */ + assertEquals(1, sfs[0].getBegin()); + assertEquals(4, sfs[0].getEnd()); + assertEquals("0", sfs[0].getFeatureGroup()); + assertEquals(2, sfs[1].getBegin()); + assertEquals(5, sfs[1].getEnd()); + assertEquals("0", sfs[1].getFeatureGroup()); + assertEquals(8, sfs[2].getBegin()); + assertEquals(11, sfs[2].getEnd()); + assertEquals("1", sfs[2].getFeatureGroup()); + assertEquals(10, sfs[3].getBegin()); + assertEquals(13, sfs[3].getEnd()); + assertEquals("1", sfs[3].getFeatureGroup()); + } }