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.Hashtable;
35 import java.util.List;
36 import java.util.Stack;
37 import java.util.Vector;
43 * Answers true if the character is a valid open pair rna secondary structure
44 * symbol. Currently accepts A-Z, ([{<
49 public static boolean isOpeningParenthesis(char c)
51 return ('A' <= c && c <= 'Z' || 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 == '}'
82 * Answers true if the string is a valid close pair rna secondary structure
83 * symbol. Currently accepts a-z, )]}>
88 public static boolean isClosingParenthesis(String s)
90 return s != null && s.length() == 1
91 && isClosingParenthesis(s.charAt(0));
95 * Returns the matching open pair symbol for the given closing symbol.
96 * Currently returns A-Z for a-z, or ([{< for )]}>, or the input symbol if it
97 * is not a valid closing symbol.
102 public static char getMatchingOpeningParenthesis(char c)
104 if ('a' <= c && c <= 'z')
106 return (char) (c + 'A' - 'a');
124 * Based off of RALEE code ralee-get-base-pairs. Keeps track of open bracket
125 * positions in "stack" vector. When a close bracket is reached, pair this
126 * with the last matching element in the "stack" vector and store in "pairs"
127 * vector. Remove last element in the "stack" vector. Continue in this manner
128 * until the whole string is processed. Parse errors are thrown as exceptions
129 * wrapping the error location - position of the first unmatched closing
130 * bracket, or string length if there is an unmatched opening bracket.
133 * Secondary structure line of an RNA Stockholm file
135 * @throw {@link WUSSParseException}
137 public static Vector<SimpleBP> getSimpleBPs(CharSequence line)
138 throws WUSSParseException
140 Hashtable<Character, Stack<Integer>> stacks = new Hashtable<Character, Stack<Integer>>();
141 Vector<SimpleBP> pairs = new Vector<SimpleBP>();
143 while (i < line.length())
145 char base = line.charAt(i);
147 if (isOpeningParenthesis(base))
149 if (!stacks.containsKey(base))
151 stacks.put(base, new Stack<Integer>());
153 stacks.get(base).push(i);
156 else if (isClosingParenthesis(base))
159 char opening = getMatchingOpeningParenthesis(base);
161 if (!stacks.containsKey(opening))
163 throw new WUSSParseException(MessageManager.formatMessage(
164 "exception.mismatched_unseen_closing_char", new String[]
165 { String.valueOf(base) }), i);
168 Stack<Integer> stack = stacks.get(opening);
171 // error whilst parsing i'th position. pass back
172 throw new WUSSParseException(MessageManager.formatMessage(
173 "exception.mismatched_closing_char", new String[]
174 { String.valueOf(base) }), i);
176 int temp = stack.pop();
178 pairs.add(new SimpleBP(temp, i));
182 for (char opening : stacks.keySet())
184 Stack<Integer> stack = stacks.get(opening);
188 * we have an unmatched opening bracket; report error as at
189 * i (length of input string)
191 throw new WUSSParseException(MessageManager.formatMessage(
192 "exception.mismatched_opening_char", new String[]
193 { String.valueOf(opening), String.valueOf(stack.pop()) }),
200 public static SequenceFeature[] getBasePairs(List<SimpleBP> bps)
201 throws WUSSParseException
203 SequenceFeature[] outPairs = new SequenceFeature[bps.size()];
204 for (int p = 0; p < bps.size(); p++)
206 SimpleBP bp = bps.get(p);
207 outPairs[p] = new SequenceFeature("RNA helix", "", "", bp.getBP5(),
213 public static List<SimpleBP> getModeleBP(CharSequence line)
214 throws WUSSParseException
216 Vector<SimpleBP> bps = getSimpleBPs(line);
217 return new ArrayList<SimpleBP>(bps);
221 * Function to get the end position corresponding to a given start position
224 * - start position of a base pair
225 * @return - end position of a base pair
228 * makes no sense at the moment :( public int findEnd(int indice){ //TODO:
229 * Probably extend this to find the start to a given end? //could be done by
230 * putting everything twice to the hash ArrayList<Integer> pair = new
231 * ArrayList<Integer>(); return pairHash.get(indice); }
235 * Figures out which helix each position belongs to and stores the helix
236 * number in the 'featureGroup' member of a SequenceFeature Based off of RALEE
237 * code ralee-helix-map.
240 * Array of SequenceFeature (output from Rna.GetBasePairs)
242 public static void HelixMap(SequenceFeature[] pairs)
245 int helix = 0; // Number of helices/current helix
246 int lastopen = 0; // Position of last open bracket reviewed
247 int lastclose = 9999999; // Position of last close bracket reviewed
248 int i = pairs.length; // Number of pairs
250 int open; // Position of an open bracket under review
251 int close; // Position of a close bracket under review
254 Hashtable<Integer, Integer> helices = new Hashtable<Integer, Integer>();
255 // Keep track of helix number for each position
257 // Go through each base pair and assign positions a helix
258 for (i = 0; i < pairs.length; i++)
261 open = pairs[i].getBegin();
262 close = pairs[i].getEnd();
264 // System.out.println("open " + open + " close " + close);
265 // System.out.println("lastclose " + lastclose + " lastopen " + lastopen);
267 // we're moving from right to left based on closing pair
269 * catch things like <<..>>..<<..>> |
271 if (open > lastclose)
277 * catch things like <<..<<..>>..<<..>>>> |
279 j = pairs.length - 1;
282 int popen = pairs[j].getBegin();
284 // System.out.println("j " + j + " popen " + popen + " lastopen "
285 // +lastopen + " open " + open);
286 if ((popen < lastopen) && (popen > open))
288 if (helices.containsValue(popen)
289 && ((helices.get(popen)) == helix))
303 // Put positions and helix information into the hashtable
304 helices.put(open, helix);
305 helices.put(close, helix);
307 // Record helix as featuregroup
308 pairs[i].setFeatureGroup(Integer.toString(helix));
317 * Answers true if the character is a recognised symbol for RNA secondary
318 * structure. Currently accepts a-z, A-Z, ()[]{}<>.
323 public static boolean isRnaSecondaryStructureSymbol(char c)
325 return isOpeningParenthesis(c) || isClosingParenthesis(c);
329 * Answers true if the string is a recognised symbol for RNA secondary
330 * structure. Currently accepts a-z, A-Z, ()[]{}<>.
335 public static boolean isRnaSecondaryStructureSymbol(String s)
337 return isOpeningParenthesis(s) || isClosingParenthesis(s);
341 * Translates a string to RNA secondary structure representation. Returns the
342 * string with any non-SS characters changed to spaces. Accepted characters
343 * are a-z, A-Z, and (){}[]<> brackets.
348 public static String getRNASecStrucState(String ssString)
350 if (ssString == null)
354 StringBuilder result = new StringBuilder(ssString.length());
355 for (int i = 0; i < ssString.length(); i++)
357 char c = ssString.charAt(i);
358 result.append(isRnaSecondaryStructureSymbol(c) ? c : " ");
360 return result.toString();
364 * Answers true if the base-pair is either a Watson-Crick (A:T/U, C:G) or a
365 * wobble (G:T/U) pair (either way round), else false
371 public static boolean isCanonicalOrWobblePair(char first, char second)
422 * Answers true if the base-pair is Watson-Crick - (A:T/U or C:G, either way
429 public static boolean isCanonicalPair(char first, char second)
478 * Returns the matching close pair symbol for the given opening symbol.
479 * Currently returns a-z for A-Z, or )]}> for ([{<, or the input symbol if it
480 * is not a valid opening symbol.
485 public static char getMatchingClosingParenthesis(char c)
487 if ('A' <= c && c <= 'Z')
489 return (char) (c + 'a' - 'A');