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 == '{'
57 * Answers true if the string is a valid open pair rna secondary structure
58 * symbol. Currently accepts A-Z, ([{<
63 public static boolean isOpeningParenthesis(String s)
65 return s != null && s.length() == 1
66 && isOpeningParenthesis(s.charAt(0));
70 * Answers true if the character is a valid close pair rna secondary structure
71 * symbol. Currently accepts a-z, )]}>
76 public static boolean isClosingParenthesis(char c)
78 return ('a' <= c && c <= 'z' || c == ')' || c == ']' || c == '}'
83 * Answers true if the string is a valid close pair rna secondary structure
84 * symbol. Currently accepts a-z, )]}>
89 public static boolean isClosingParenthesis(String s)
91 return s != null && s.length() == 1
92 && isClosingParenthesis(s.charAt(0));
96 * Returns the matching open pair symbol for the given closing symbol.
97 * Currently returns A-Z for a-z, or ([{< for )]}>, or the input symbol if it
98 * is not a valid closing symbol.
103 public static char getMatchingOpeningParenthesis(char c)
105 if ('a' <= c && c <= 'z')
107 return (char) (c + 'A' - 'a');
125 * Based off of RALEE code ralee-get-base-pairs. Keeps track of open bracket
126 * positions in "stack" vector. When a close bracket is reached, pair this
127 * with the last matching element in the "stack" vector and store in "pairs"
128 * vector. Remove last element in the "stack" vector. Continue in this manner
129 * until the whole string is processed. Parse errors are thrown as exceptions
130 * wrapping the error location - position of the first unmatched closing
131 * bracket, or string length if there is an unmatched opening bracket.
134 * Secondary structure line of an RNA Stockholm file
136 * @throw {@link WUSSParseException}
138 protected static List<SimpleBP> getSimpleBPs(CharSequence line)
139 throws WUSSParseException
141 Hashtable<Character, Stack<Integer>> stacks = new Hashtable<Character, Stack<Integer>>();
142 List<SimpleBP> pairs = new ArrayList<SimpleBP>();
144 while (i < line.length())
146 char base = line.charAt(i);
148 if (isOpeningParenthesis(base))
150 if (!stacks.containsKey(base))
152 stacks.put(base, new Stack<Integer>());
154 stacks.get(base).push(i);
157 else if (isClosingParenthesis(base))
160 char opening = getMatchingOpeningParenthesis(base);
162 if (!stacks.containsKey(opening))
164 throw new WUSSParseException(MessageManager.formatMessage(
165 "exception.mismatched_unseen_closing_char", new String[]
166 { String.valueOf(base) }), i);
169 Stack<Integer> stack = stacks.get(opening);
172 // error whilst parsing i'th position. pass back
173 throw new WUSSParseException(MessageManager.formatMessage(
174 "exception.mismatched_closing_char", new String[]
175 { String.valueOf(base) }), i);
177 int temp = stack.pop();
179 pairs.add(new SimpleBP(temp, i));
183 for (char opening : stacks.keySet())
185 Stack<Integer> stack = stacks.get(opening);
189 * we have an unmatched opening bracket; report error as at
190 * i (length of input string)
192 throw new WUSSParseException(MessageManager.formatMessage(
193 "exception.mismatched_opening_char", new String[]
194 { String.valueOf(opening), String.valueOf(stack.pop()) }),
206 * Function to get the end position corresponding to a given start position
209 * - start position of a base pair
210 * @return - end position of a base pair
213 * makes no sense at the moment :( public int findEnd(int indice){ //TODO:
214 * Probably extend this to find the start to a given end? //could be done by
215 * putting everything twice to the hash ArrayList<Integer> pair = new
216 * ArrayList<Integer>(); return pairHash.get(indice); }
220 * Answers true if the character is a recognised symbol for RNA secondary
221 * structure. Currently accepts a-z, A-Z, ()[]{}<>.
226 public static boolean isRnaSecondaryStructureSymbol(char c)
228 return isOpeningParenthesis(c) || isClosingParenthesis(c);
232 * Answers true if the string is a recognised symbol for RNA secondary
233 * structure. Currently accepts a-z, A-Z, ()[]{}<>.
238 public static boolean isRnaSecondaryStructureSymbol(String s)
240 return isOpeningParenthesis(s) || isClosingParenthesis(s);
244 * Translates a string to RNA secondary structure representation. Returns the
245 * string with any non-SS characters changed to spaces. Accepted characters
246 * are a-z, A-Z, and (){}[]<> brackets.
251 public static String getRNASecStrucState(String ssString)
253 if (ssString == null)
257 StringBuilder result = new StringBuilder(ssString.length());
258 for (int i = 0; i < ssString.length(); i++)
260 char c = ssString.charAt(i);
261 result.append(isRnaSecondaryStructureSymbol(c) ? c : " ");
263 return result.toString();
267 * Answers true if the base-pair is either a Watson-Crick (A:T/U, C:G) or a
268 * wobble (G:T/U) pair (either way round), else false
274 public static boolean isCanonicalOrWobblePair(char first, char second)
325 * Answers true if the base-pair is Watson-Crick - (A:T/U or C:G, either way
332 public static boolean isCanonicalPair(char first, char second)
381 * Returns the matching close pair symbol for the given opening symbol.
382 * Currently returns a-z for A-Z, or )]}> for ([{<, or the input symbol if it
383 * is not a valid opening symbol.
388 public static char getMatchingClosingParenthesis(char c)
390 if ('A' <= c && c <= 'Z')
392 return (char) (c + 'a' - 'A');
409 public static SequenceFeature[] getHelixMap(CharSequence rnaAnnotation)
410 throws WUSSParseException
412 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
414 int helix = 0; // Number of helices/current helix
415 int lastopen = 0; // Position of last open bracket reviewed
416 int lastclose = 9999999; // Position of last close bracket reviewed
418 Map<Integer, Integer> helices = new HashMap<Integer, Integer>();
419 // Keep track of helix number for each position
421 // Go through each base pair and assign positions a helix
422 List<SimpleBP> bps = getSimpleBPs(rnaAnnotation);
423 for (SimpleBP basePair : bps)
425 final int open = basePair.getBP5();
426 final int close = basePair.getBP3();
428 // System.out.println("open " + open + " close " + close);
429 // System.out.println("lastclose " + lastclose + " lastopen " + lastopen);
431 // we're moving from right to left based on closing pair
433 * catch things like <<..>>..<<..>> |
435 if (open > lastclose)
441 * catch things like <<..<<..>>..<<..>>>> |
443 int j = bps.size() - 1;
446 int popen = bps.get(j).getBP5();
448 // System.out.println("j " + j + " popen " + popen + " lastopen "
449 // +lastopen + " open " + open);
450 if ((popen < lastopen) && (popen > open))
452 if (helices.containsValue(popen)
453 && ((helices.get(popen)) == helix))
466 // Put positions and helix information into the hashtable
467 helices.put(open, helix);
468 helices.put(close, helix);
470 // Record helix as featuregroup
471 result.add(new SequenceFeature("RNA helix", "", open, close,
472 String.valueOf(helix)));
478 return result.toArray(new SequenceFeature[result.size()]);