2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 /* Author: Lauren Michelle Lui
22 * Methods are based on RALEE methods http://personalpages.manchester.ac.uk/staff/sam.griffiths-jones/software/ralee/
23 * Additional Author: Jan Engelhart (2011) - Structure consensus and bug fixing
24 * Additional Author: Anne Menard (2012) - Pseudoknot support and secondary structure consensus
27 package jalview.analysis;
29 import jalview.analysis.SecStrConsensus.SimpleBP;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.util.MessageManager;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Hashtable;
36 import java.util.List;
38 import java.util.Stack;
44 * Answers true if the character is a valid open pair rna secondary structure
45 * symbol. Currently accepts A-Z, ([{<
50 public static boolean isOpeningParenthesis(char c)
52 return ('A' <= c && c <= 'Z' || c == '(' || c == '[' || c == '{' || c == '<');
56 * Answers true if the string is a valid open pair rna secondary structure
57 * symbol. Currently accepts A-Z, ([{<
62 public static boolean isOpeningParenthesis(String s)
64 return s != null && s.length() == 1
65 && isOpeningParenthesis(s.charAt(0));
69 * Answers true if the character is a valid close pair rna secondary structure
70 * symbol. Currently accepts a-z, )]}>
75 public static boolean isClosingParenthesis(char c)
77 return ('a' <= c && c <= 'z' || c == ')' || c == ']' || c == '}' || c == '>');
81 * Answers true if the string is a valid close pair rna secondary structure
82 * symbol. Currently accepts a-z, )]}>
87 public static boolean isClosingParenthesis(String s)
89 return s != null && s.length() == 1
90 && isClosingParenthesis(s.charAt(0));
94 * Returns the matching open pair symbol for the given closing symbol.
95 * Currently returns A-Z for a-z, or ([{< for )]}>, or the input symbol if it
96 * is not a valid closing symbol.
101 public static char getMatchingOpeningParenthesis(char c)
103 if ('a' <= c && c <= 'z')
105 return (char) (c + 'A' - 'a');
123 * Based off of RALEE code ralee-get-base-pairs. Keeps track of open bracket
124 * positions in "stack" vector. When a close bracket is reached, pair this
125 * with the last matching element in the "stack" vector and store in "pairs"
126 * vector. Remove last element in the "stack" vector. Continue in this manner
127 * until the whole string is processed. Parse errors are thrown as exceptions
128 * wrapping the error location - position of the first unmatched closing
129 * bracket, or string length if there is an unmatched opening bracket.
132 * Secondary structure line of an RNA Stockholm file
134 * @throw {@link WUSSParseException}
136 protected static List<SimpleBP> getSimpleBPs(CharSequence line)
137 throws WUSSParseException
139 Hashtable<Character, Stack<Integer>> stacks = new Hashtable<Character, Stack<Integer>>();
140 List<SimpleBP> pairs = new ArrayList<SimpleBP>();
142 while (i < line.length())
144 char base = line.charAt(i);
146 if (isOpeningParenthesis(base))
148 if (!stacks.containsKey(base))
150 stacks.put(base, new Stack<Integer>());
152 stacks.get(base).push(i);
155 else if (isClosingParenthesis(base))
158 char opening = getMatchingOpeningParenthesis(base);
160 if (!stacks.containsKey(opening))
162 throw new WUSSParseException(MessageManager.formatMessage(
163 "exception.mismatched_unseen_closing_char",
164 new String[] { String.valueOf(base) }), i);
167 Stack<Integer> stack = stacks.get(opening);
170 // error whilst parsing i'th position. pass back
171 throw new WUSSParseException(MessageManager.formatMessage(
172 "exception.mismatched_closing_char",
173 new String[] { String.valueOf(base) }), i);
175 int temp = stack.pop();
177 pairs.add(new SimpleBP(temp, i));
181 for (char opening : stacks.keySet())
183 Stack<Integer> stack = stacks.get(opening);
187 * we have an unmatched opening bracket; report error as at
188 * i (length of input string)
190 throw new WUSSParseException(MessageManager.formatMessage(
191 "exception.mismatched_opening_char",
192 new String[] { String.valueOf(opening),
193 String.valueOf(stack.pop()) }), i);
204 * Function to get the end position corresponding to a given start position
207 * - start position of a base pair
208 * @return - end position of a base pair
211 * makes no sense at the moment :( public int findEnd(int indice){ //TODO:
212 * Probably extend this to find the start to a given end? //could be done by
213 * putting everything twice to the hash ArrayList<Integer> pair = new
214 * ArrayList<Integer>(); return pairHash.get(indice); }
218 * Answers true if the character is a recognised symbol for RNA secondary
219 * structure. Currently accepts a-z, A-Z, ()[]{}<>.
224 public static boolean isRnaSecondaryStructureSymbol(char c)
226 return isOpeningParenthesis(c) || isClosingParenthesis(c);
230 * Answers true if the string is a recognised symbol for RNA secondary
231 * structure. Currently accepts a-z, A-Z, ()[]{}<>.
236 public static boolean isRnaSecondaryStructureSymbol(String s)
238 return isOpeningParenthesis(s) || isClosingParenthesis(s);
242 * Translates a string to RNA secondary structure representation. Returns the
243 * string with any non-SS characters changed to spaces. Accepted characters
244 * are a-z, A-Z, and (){}[]<> brackets.
249 public static String getRNASecStrucState(String ssString)
251 if (ssString == null)
255 StringBuilder result = new StringBuilder(ssString.length());
256 for (int i = 0; i < ssString.length(); i++)
258 char c = ssString.charAt(i);
259 result.append(isRnaSecondaryStructureSymbol(c) ? c : " ");
261 return result.toString();
265 * Answers true if the base-pair is either a Watson-Crick (A:T/U, C:G) or a
266 * wobble (G:T/U) pair (either way round), else false
272 public static boolean isCanonicalOrWobblePair(char first, char second)
323 * Answers true if the base-pair is Watson-Crick - (A:T/U or C:G, either way
330 public static boolean isCanonicalPair(char first, char second)
379 * Returns the matching close pair symbol for the given opening symbol.
380 * Currently returns a-z for A-Z, or )]}> for ([{<, or the input symbol if it
381 * is not a valid opening symbol.
386 public static char getMatchingClosingParenthesis(char c)
388 if ('A' <= c && c <= 'Z')
390 return (char) (c + 'a' - 'A');
407 public static SequenceFeature[] getHelixMap(CharSequence rnaAnnotation)
408 throws WUSSParseException
410 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
412 int helix = 0; // Number of helices/current helix
413 int lastopen = 0; // Position of last open bracket reviewed
414 int lastclose = 9999999; // Position of last close bracket reviewed
416 Map<Integer, Integer> helices = new HashMap<Integer, Integer>();
417 // Keep track of helix number for each position
419 // Go through each base pair and assign positions a helix
420 List<SimpleBP> bps = getSimpleBPs(rnaAnnotation);
421 for (SimpleBP basePair : bps)
423 final int open = basePair.getBP5();
424 final int close = basePair.getBP3();
426 // System.out.println("open " + open + " close " + close);
427 // System.out.println("lastclose " + lastclose + " lastopen " + lastopen);
429 // we're moving from right to left based on closing pair
431 * catch things like <<..>>..<<..>> |
433 if (open > lastclose)
439 * catch things like <<..<<..>>..<<..>>>> |
441 int j = bps.size() - 1;
444 int popen = bps.get(j).getBP5();
446 // System.out.println("j " + j + " popen " + popen + " lastopen "
447 // +lastopen + " open " + open);
448 if ((popen < lastopen) && (popen > open))
450 if (helices.containsValue(popen)
451 && ((helices.get(popen)) == helix))
464 // Put positions and helix information into the hashtable
465 helices.put(open, helix);
466 helices.put(close, helix);
468 // Record helix as featuregroup
469 result.add(new SequenceFeature("RNA helix", "", open, close,
470 String.valueOf(helix)));
476 return result.toArray(new SequenceFeature[result.size()]);