2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 /* Author: Lauren Michelle Lui
20 * Methods are based on RALEE methods http://personalpages.manchester.ac.uk/staff/sam.griffiths-jones/software/ralee/
21 * Additional Author: Jan Engelhart (2011) - Structure consensus and bug fixing
22 * Additional Author: Anne Menard (2012) - Pseudoknot support and secondary structure consensus
25 package jalview.analysis;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashSet;
31 import java.util.Hashtable;
32 import java.util.Stack;
33 import java.util.Vector;
36 import jalview.analysis.SecStrConsensus.SimpleBP;
37 import jalview.datamodel.SequenceFeature;
42 static Hashtable<Integer, Integer> pairHash = new Hashtable();
44 private static final Character[] openingPars = {'(','[','{','<','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
45 private static final Character[] closingPars = {')',']','}','>','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
47 private static HashSet<Character> openingParsSet = new HashSet<Character>(Arrays.asList(openingPars));
48 private static HashSet<Character> closingParsSet = new HashSet<Character>(Arrays.asList(closingPars));
49 private static Hashtable<Character,Character> closingToOpening = new Hashtable<Character,Character>()
50 // Initializing final data structure
52 private static final long serialVersionUID = 1L;
54 for(int i=0;i<openingPars.length;i++)
56 System.out.println(closingPars[i]+"->"+openingPars[i]);
57 put(closingPars[i],openingPars[i]);
61 private static boolean isOpeningParenthesis(char c)
63 return openingParsSet.contains(c);
66 private static boolean isClosingParenthesis(char c)
68 return closingParsSet.contains(c);
71 private static char matchingOpeningParenthesis(char closingParenthesis) throws WUSSParseException
73 if (!isClosingParenthesis(closingParenthesis))
75 throw new WUSSParseException("Querying matching opening parenthesis for non-closing parenthesis character "+closingParenthesis, -1);
78 return closingToOpening.get(closingParenthesis);
82 * Based off of RALEE code ralee-get-base-pairs. Keeps track of open bracket
83 * positions in "stack" vector. When a close bracket is reached, pair this
84 * with the last element in the "stack" vector and store in "pairs" vector.
85 * Remove last element in the "stack" vector. Continue in this manner until
86 * the whole string is processed.
89 * Secondary structure line of an RNA Stockholm file
90 * @return Array of SequenceFeature; type = RNA helix, begin is open base
91 * pair, end is close base pair
93 public static Vector<SimpleBP> GetSimpleBPs(CharSequence line) throws WUSSParseException
95 System.out.println(line);
96 Hashtable<Character,Stack<Integer>> stacks = new Hashtable<Character,Stack<Integer>>();
97 Vector<SimpleBP> pairs = new Vector<SimpleBP>();
99 while (i < line.length())
101 char base = line.charAt(i);
103 if (isOpeningParenthesis(base))
105 if (!stacks.containsKey(base)){
106 stacks.put(base, new Stack<Integer>());
108 stacks.get(base).push(i);
111 else if (isClosingParenthesis(base))
114 char opening = matchingOpeningParenthesis(base);
116 if (!stacks.containsKey(opening)){
117 throw new WUSSParseException("Mismatched (unseen) closing character "+base, i);
120 Stack<Integer> stack = stacks.get(opening);
123 // error whilst parsing i'th position. pass back
124 throw new WUSSParseException("Mismatched closing character "+base, i);
126 int temp = stack.pop();
128 pairs.add(new SimpleBP(temp,i));
132 for(char opening: stacks.keySet())
134 Stack<Integer> stack = stacks.get(opening);
137 throw new WUSSParseException("Mismatched opening character "+opening+" at "+stack.pop(), i);
143 public static SequenceFeature[] GetBasePairs(CharSequence line) throws WUSSParseException
145 Vector<SimpleBP> bps = GetSimpleBPs(line);
146 SequenceFeature[] outPairs = new SequenceFeature[bps.size()];
147 for (int p = 0; p < bps.size(); p++)
149 SimpleBP bp = bps.elementAt(p);
150 outPairs[p] = new SequenceFeature("RNA helix", "", "", bp.getBP5(),bp.getBP3(), "");
156 public static ArrayList<SimpleBP> GetModeleBP(CharSequence line) throws WUSSParseException
158 Vector<SimpleBP> bps = GetSimpleBPs(line);
159 return new ArrayList<SimpleBP>(bps);
164 * Function to get the end position corresponding to a given start position
165 * @param indice - start position of a base pair
166 * @return - end position of a base pair
168 /*makes no sense at the moment :(
169 public int findEnd(int indice){
170 //TODO: Probably extend this to find the start to a given end?
171 //could be done by putting everything twice to the hash
172 ArrayList<Integer> pair = new ArrayList<Integer>();
173 return pairHash.get(indice);
178 * Figures out which helix each position belongs to and stores the helix
179 * number in the 'featureGroup' member of a SequenceFeature Based off of RALEE
180 * code ralee-helix-map.
183 * Array of SequenceFeature (output from Rna.GetBasePairs)
185 public static void HelixMap(SequenceFeature[] pairs)
188 int helix = 0; // Number of helices/current helix
189 int lastopen = 0; // Position of last open bracket reviewed
190 int lastclose = 9999999; // Position of last close bracket reviewed
191 int i = pairs.length; // Number of pairs
193 int open; // Position of an open bracket under review
194 int close; // Position of a close bracket under review
197 Hashtable helices = new Hashtable(); // Keep track of helix number for each
200 // Go through each base pair and assign positions a helix
201 for (i = 0; i < pairs.length; i++)
204 open = pairs[i].getBegin();
205 close = pairs[i].getEnd();
207 // System.out.println("open " + open + " close " + close);
208 // System.out.println("lastclose " + lastclose + " lastopen " + lastopen);
210 // we're moving from right to left based on closing pair
212 * catch things like <<..>>..<<..>> |
214 if (open > lastclose)
220 * catch things like <<..<<..>>..<<..>>>> |
222 j = pairs.length - 1;
225 int popen = pairs[j].getBegin();
227 // System.out.println("j " + j + " popen " + popen + " lastopen "
228 // +lastopen + " open " + open);
229 if ((popen < lastopen) && (popen > open))
231 if (helices.containsValue(popen)
232 && (((Integer) helices.get(popen)) == helix))
246 // Put positions and helix information into the hashtable
247 helices.put(open, helix);
248 helices.put(close, helix);
250 // Record helix as featuregroup
251 pairs[i].setFeatureGroup(Integer.toString(helix));