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.Arrays;
35 import java.util.HashSet;
36 import java.util.Hashtable;
37 import java.util.List;
38 import java.util.Stack;
39 import java.util.Vector;
44 static Hashtable<Integer, Integer> pairHash = new Hashtable<Integer, Integer>();
46 private static final Character[] openingPars = { '(', '[', '{', '<', 'A',
47 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
48 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
50 private static final Character[] closingPars = { ')', ']', '}', '>', 'a',
51 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
52 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
54 private static HashSet<Character> openingParsSet = new HashSet<Character>(
55 Arrays.asList(openingPars));
57 private static HashSet<Character> closingParsSet = new HashSet<Character>(
58 Arrays.asList(closingPars));
60 private static Hashtable<Character, Character> closingToOpening = new Hashtable<Character, Character>()
61 // Initializing final data structure
63 private static final long serialVersionUID = 1L;
65 for (int i = 0; i < openingPars.length; i++)
67 // System.out.println(closingPars[i] + "->" + openingPars[i]);
68 put(closingPars[i], openingPars[i]);
73 public static boolean isOpeningParenthesis(char c)
75 return openingParsSet.contains(c);
78 public static boolean isClosingParenthesis(char c)
80 return closingParsSet.contains(c);
83 private static char matchingOpeningParenthesis(char closingParenthesis)
84 throws WUSSParseException
86 if (!isClosingParenthesis(closingParenthesis))
88 throw new WUSSParseException(
89 MessageManager.formatMessage(
90 "exception.querying_matching_opening_parenthesis_for_non_closing_parenthesis",
91 new String[] { String.valueOf(closingParenthesis) }),
95 return closingToOpening.get(closingParenthesis);
99 * Based off of RALEE code ralee-get-base-pairs. Keeps track of open bracket
100 * positions in "stack" vector. When a close bracket is reached, pair this
101 * with the last matching element in the "stack" vector and store in "pairs"
102 * vector. Remove last element in the "stack" vector. Continue in this manner
103 * until the whole string is processed. Parse errors are thrown as exceptions
104 * wrapping the error location - position of the first unmatched closing
105 * bracket, or string length if there is an unmatched opening bracket.
108 * Secondary structure line of an RNA Stockholm file
110 * @throw {@link WUSSParseException}
112 public static Vector<SimpleBP> getSimpleBPs(CharSequence line)
113 throws WUSSParseException
115 Hashtable<Character, Stack<Integer>> stacks = new Hashtable<Character, Stack<Integer>>();
116 Vector<SimpleBP> pairs = new Vector<SimpleBP>();
118 while (i < line.length())
120 char base = line.charAt(i);
122 if (isOpeningParenthesis(base))
124 if (!stacks.containsKey(base))
126 stacks.put(base, new Stack<Integer>());
128 stacks.get(base).push(i);
131 else if (isClosingParenthesis(base))
134 char opening = matchingOpeningParenthesis(base);
136 if (!stacks.containsKey(opening))
138 throw new WUSSParseException(MessageManager.formatMessage(
139 "exception.mismatched_unseen_closing_char",
140 new String[] { String.valueOf(base) }), i);
143 Stack<Integer> stack = stacks.get(opening);
146 // error whilst parsing i'th position. pass back
147 throw new WUSSParseException(MessageManager.formatMessage(
148 "exception.mismatched_closing_char",
149 new String[] { String.valueOf(base) }), i);
151 int temp = stack.pop();
153 pairs.add(new SimpleBP(temp, i));
157 for (char opening : stacks.keySet())
159 Stack<Integer> stack = stacks.get(opening);
163 * we have an unmatched opening bracket; report error as at
164 * i (length of input string)
166 throw new WUSSParseException(MessageManager.formatMessage(
167 "exception.mismatched_opening_char",
168 new String[] { String.valueOf(opening),
169 String.valueOf(stack.pop()) }), i);
175 public static SequenceFeature[] getBasePairs(List<SimpleBP> bps)
176 throws WUSSParseException
178 SequenceFeature[] outPairs = new SequenceFeature[bps.size()];
179 for (int p = 0; p < bps.size(); p++)
181 SimpleBP bp = bps.get(p);
182 outPairs[p] = new SequenceFeature("RNA helix", "", "", bp.getBP5(),
188 public static List<SimpleBP> getModeleBP(CharSequence line)
189 throws WUSSParseException
191 Vector<SimpleBP> bps = getSimpleBPs(line);
192 return new ArrayList<SimpleBP>(bps);
196 * Function to get the end position corresponding to a given start position
199 * - start position of a base pair
200 * @return - end position of a base pair
203 * makes no sense at the moment :( public int findEnd(int indice){ //TODO:
204 * Probably extend this to find the start to a given end? //could be done by
205 * putting everything twice to the hash ArrayList<Integer> pair = new
206 * ArrayList<Integer>(); return pairHash.get(indice); }
210 * Figures out which helix each position belongs to and stores the helix
211 * number in the 'featureGroup' member of a SequenceFeature Based off of RALEE
212 * code ralee-helix-map.
215 * Array of SequenceFeature (output from Rna.GetBasePairs)
217 public static void HelixMap(SequenceFeature[] pairs)
220 int helix = 0; // Number of helices/current helix
221 int lastopen = 0; // Position of last open bracket reviewed
222 int lastclose = 9999999; // Position of last close bracket reviewed
223 int i = pairs.length; // Number of pairs
225 int open; // Position of an open bracket under review
226 int close; // Position of a close bracket under review
229 Hashtable<Integer, Integer> helices = new Hashtable<Integer, Integer>();
230 // Keep track of helix number for each position
232 // Go through each base pair and assign positions a helix
233 for (i = 0; i < pairs.length; i++)
236 open = pairs[i].getBegin();
237 close = pairs[i].getEnd();
239 // System.out.println("open " + open + " close " + close);
240 // System.out.println("lastclose " + lastclose + " lastopen " + lastopen);
242 // we're moving from right to left based on closing pair
244 * catch things like <<..>>..<<..>> |
246 if (open > lastclose)
252 * catch things like <<..<<..>>..<<..>>>> |
254 j = pairs.length - 1;
257 int popen = pairs[j].getBegin();
259 // System.out.println("j " + j + " popen " + popen + " lastopen "
260 // +lastopen + " open " + open);
261 if ((popen < lastopen) && (popen > open))
263 if (helices.containsValue(popen)
264 && ((helices.get(popen)) == helix))
278 // Put positions and helix information into the hashtable
279 helices.put(open, helix);
280 helices.put(close, helix);
282 // Record helix as featuregroup
283 pairs[i].setFeatureGroup(Integer.toString(helix));
292 * Answers true if the character is a recognised symbol for RNA secondary
293 * structure. Currently accepts a-z, A-Z, ()[]{}<>.
298 public static boolean isRnaSecondaryStructureSymbol(char c)
300 return isOpeningParenthesis(c) || isClosingParenthesis(c);
304 * Translates a string to RNA secondary structure representation. Returns the
305 * string with any non-SS characters changed to spaces. Accepted characters
306 * are a-z, A-Z, and (){}[]<> brackets.
311 public static String getRNASecStrucState(String ssString)
313 if (ssString == null)
317 StringBuilder result = new StringBuilder(ssString.length());
318 for (int i = 0; i < ssString.length(); i++)
320 char c = ssString.charAt(i);
321 result.append(isRnaSecondaryStructureSymbol(c) ? c : " ");
323 return result.toString();
327 * Answers true if the base-pair is either a canonical (A-T/U, C-G) or a
328 * wobble (G-T/U) pair (either way round), else false
334 public static boolean isCanonicalOrWobblePair(char first, char second)