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 == '{' || c == '<');
55 * Answers true if the string is a valid open pair rna secondary structure
56 * symbol. Currently accepts A-Z, ([{<
61 public static boolean isOpeningParenthesis(String s)
63 return s != null && s.length() == 1
64 && isOpeningParenthesis(s.charAt(0));
68 * Answers true if the character is a valid close pair rna secondary structure
69 * symbol. Currently accepts a-z, )]}>
74 public static boolean isClosingParenthesis(char c)
76 return ('a' <= c && c <= 'z' || c == ')' || c == ']' || c == '}' || c == '>');
80 * Answers true if the string is a valid close pair rna secondary structure
81 * symbol. Currently accepts a-z, )]}>
86 public static boolean isClosingParenthesis(String s)
88 return s != null && s.length() == 1
89 && isClosingParenthesis(s.charAt(0));
93 * Returns the matching open pair symbol for the given closing symbol.
94 * Currently returns A-Z for a-z, or ([{< for )]}>, or the input symbol if it
95 * is not a valid closing symbol.
100 public static char getMatchingOpeningParenthesis(char c)
102 if ('a' <= c && c <= 'z')
104 return (char) (c + 'A' - 'a');
122 * Based off of RALEE code ralee-get-base-pairs. Keeps track of open bracket
123 * positions in "stack" vector. When a close bracket is reached, pair this
124 * with the last matching element in the "stack" vector and store in "pairs"
125 * vector. Remove last element in the "stack" vector. Continue in this manner
126 * until the whole string is processed. Parse errors are thrown as exceptions
127 * wrapping the error location - position of the first unmatched closing
128 * bracket, or string length if there is an unmatched opening bracket.
131 * Secondary structure line of an RNA Stockholm file
133 * @throw {@link WUSSParseException}
135 public static Vector<SimpleBP> getSimpleBPs(CharSequence line)
136 throws WUSSParseException
138 Hashtable<Character, Stack<Integer>> stacks = new Hashtable<Character, Stack<Integer>>();
139 Vector<SimpleBP> pairs = new Vector<SimpleBP>();
141 while (i < line.length())
143 char base = line.charAt(i);
145 if (isOpeningParenthesis(base))
147 if (!stacks.containsKey(base))
149 stacks.put(base, new Stack<Integer>());
151 stacks.get(base).push(i);
154 else if (isClosingParenthesis(base))
157 char opening = getMatchingOpeningParenthesis(base);
159 if (!stacks.containsKey(opening))
161 throw new WUSSParseException(MessageManager.formatMessage(
162 "exception.mismatched_unseen_closing_char",
163 new String[] { String.valueOf(base) }), i);
166 Stack<Integer> stack = stacks.get(opening);
169 // error whilst parsing i'th position. pass back
170 throw new WUSSParseException(MessageManager.formatMessage(
171 "exception.mismatched_closing_char",
172 new String[] { String.valueOf(base) }), i);
174 int temp = stack.pop();
176 pairs.add(new SimpleBP(temp, i));
180 for (char opening : stacks.keySet())
182 Stack<Integer> stack = stacks.get(opening);
186 * we have an unmatched opening bracket; report error as at
187 * i (length of input string)
189 throw new WUSSParseException(MessageManager.formatMessage(
190 "exception.mismatched_opening_char",
191 new String[] { String.valueOf(opening),
192 String.valueOf(stack.pop()) }), i);
198 public static SequenceFeature[] getBasePairs(List<SimpleBP> bps)
199 throws WUSSParseException
201 SequenceFeature[] outPairs = new SequenceFeature[bps.size()];
202 for (int p = 0; p < bps.size(); p++)
204 SimpleBP bp = bps.get(p);
205 outPairs[p] = new SequenceFeature("RNA helix", "", "", bp.getBP5(),
211 public static List<SimpleBP> getModeleBP(CharSequence line)
212 throws WUSSParseException
214 Vector<SimpleBP> bps = getSimpleBPs(line);
215 return new ArrayList<SimpleBP>(bps);
219 * Function to get the end position corresponding to a given start position
222 * - start position of a base pair
223 * @return - end position of a base pair
226 * makes no sense at the moment :( public int findEnd(int indice){ //TODO:
227 * Probably extend this to find the start to a given end? //could be done by
228 * putting everything twice to the hash ArrayList<Integer> pair = new
229 * ArrayList<Integer>(); return pairHash.get(indice); }
233 * Figures out which helix each position belongs to and stores the helix
234 * number in the 'featureGroup' member of a SequenceFeature Based off of RALEE
235 * code ralee-helix-map.
238 * Array of SequenceFeature (output from Rna.GetBasePairs)
240 public static void HelixMap(SequenceFeature[] pairs)
243 int helix = 0; // Number of helices/current helix
244 int lastopen = 0; // Position of last open bracket reviewed
245 int lastclose = 9999999; // Position of last close bracket reviewed
246 int i = pairs.length; // Number of pairs
248 int open; // Position of an open bracket under review
249 int close; // Position of a close bracket under review
252 Hashtable<Integer, Integer> helices = new Hashtable<Integer, Integer>();
253 // Keep track of helix number for each position
255 // Go through each base pair and assign positions a helix
256 for (i = 0; i < pairs.length; i++)
259 open = pairs[i].getBegin();
260 close = pairs[i].getEnd();
262 // System.out.println("open " + open + " close " + close);
263 // System.out.println("lastclose " + lastclose + " lastopen " + lastopen);
265 // we're moving from right to left based on closing pair
267 * catch things like <<..>>..<<..>> |
269 if (open > lastclose)
275 * catch things like <<..<<..>>..<<..>>>> |
277 j = pairs.length - 1;
280 int popen = pairs[j].getBegin();
282 // System.out.println("j " + j + " popen " + popen + " lastopen "
283 // +lastopen + " open " + open);
284 if ((popen < lastopen) && (popen > open))
286 if (helices.containsValue(popen)
287 && ((helices.get(popen)) == helix))
301 // Put positions and helix information into the hashtable
302 helices.put(open, helix);
303 helices.put(close, helix);
305 // Record helix as featuregroup
306 pairs[i].setFeatureGroup(Integer.toString(helix));
315 * Answers true if the character is a recognised symbol for RNA secondary
316 * structure. Currently accepts a-z, A-Z, ()[]{}<>.
321 public static boolean isRnaSecondaryStructureSymbol(char c)
323 return isOpeningParenthesis(c) || isClosingParenthesis(c);
327 * Answers true if the string is a recognised symbol for RNA secondary
328 * structure. Currently accepts a-z, A-Z, ()[]{}<>.
333 public static boolean isRnaSecondaryStructureSymbol(String s)
335 return isOpeningParenthesis(s) || isClosingParenthesis(s);
339 * Translates a string to RNA secondary structure representation. Returns the
340 * string with any non-SS characters changed to spaces. Accepted characters
341 * are a-z, A-Z, and (){}[]<> brackets.
346 public static String getRNASecStrucState(String ssString)
348 if (ssString == null)
352 StringBuilder result = new StringBuilder(ssString.length());
353 for (int i = 0; i < ssString.length(); i++)
355 char c = ssString.charAt(i);
356 result.append(isRnaSecondaryStructureSymbol(c) ? c : " ");
358 return result.toString();
362 * Answers true if the base-pair is either a Watson-Crick (A:T/U, C:G) or a
363 * wobble (G:T/U) pair (either way round), else false
369 public static boolean isCanonicalOrWobblePair(char first, char second)
420 * Answers true if the base-pair is Watson-Crick - (A:T/U or C:G, either way
427 public static boolean isCanonicalPair(char first, char second)
476 * Returns the matching close pair symbol for the given opening symbol.
477 * Currently returns a-z for A-Z, or )]}> for ([{<, or the input symbol if it
478 * is not a valid opening symbol.
483 public static char getMatchingClosingParenthesis(char c)
485 if ('A' <= c && c <= 'Z')
487 return (char) (c + 'a' - 'A');