e386e5c4cdb733c888043e7cf447ce5aec0043fa
[jalview.git] / src / jalview / analysis / Rna.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  *  
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.
15  * 
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  */
18 /* Author: Lauren Michelle Lui 
19  * Methods are based on RALEE methods http://personalpages.manchester.ac.uk/staff/sam.griffiths-jones/software/ralee/
20  * */
21
22 package jalview.analysis;
23
24 import java.util.Hashtable;
25 import java.util.Stack;
26 import java.util.Vector;
27
28 import jalview.datamodel.SequenceFeature;
29
30 public class Rna
31 {
32   static Hashtable<Integer, Integer> pairHash = new Hashtable();
33
34   /**
35    * Based off of RALEE code ralee-get-base-pairs. Keeps track of open bracket
36    * positions in "stack" vector. When a close bracket is reached, pair this
37    * with the last element in the "stack" vector and store in "pairs" vector.
38    * Remove last element in the "stack" vector. Continue in this manner until
39    * the whole string is processed.
40    * 
41    * @param line
42    *          Secondary structure line of an RNA Stockholm file
43    * @return Array of SequenceFeature; type = RNA helix, begin is open base
44    *         pair, end is close base pair
45    */
46   public static SequenceFeature[] GetBasePairs(CharSequence line)
47           throws WUSSParseException
48   {
49     Stack stack = new Stack();
50     Vector pairs = new Vector();
51
52     int i = 0;
53     while (i < line.length())
54     {
55       char base = line.charAt(i);
56
57       if ((base == '<') || (base == '(') || (base == '{') || (base == '['))
58       {
59         stack.push(i);
60       }
61       else if ((base == '>') || (base == ')') || (base == '}')
62               || (base == ']'))
63       {
64
65         if (stack.isEmpty())
66         {
67           // error whilst parsing i'th position. pass back
68           throw new WUSSParseException("Mismatched closing bracket", i);
69         }
70         Object temp = stack.pop();
71         pairs.addElement(temp);
72         pairs.addElement(i);
73       }
74
75       i++;
76     }
77
78     int numpairs = pairs.size() / 2;
79     SequenceFeature[] outPairs = new SequenceFeature[numpairs];
80
81     // Convert pairs to array
82     for (int p = 0; p < pairs.size(); p += 2)
83     {
84       int begin = Integer.parseInt(pairs.elementAt(p).toString());
85       int end = Integer.parseInt(pairs.elementAt(p + 1).toString());
86
87       outPairs[p / 2] = new SequenceFeature("RNA helix", "", "", begin,
88               end, "");
89       // pairHash.put(begin, end);
90
91     }
92
93     return outPairs;
94   }
95
96   /**
97    * Function to get the end position corresponding to a given start position
98    * 
99    * @param indice
100    *          - start position of a base pair
101    * @return - end position of a base pair
102    */
103   /*
104    * makes no sense at the moment :( public int findEnd(int indice){ //TODO:
105    * Probably extend this to find the start to a given end? //could be done by
106    * putting everything twice to the hash ArrayList<Integer> pair = new
107    * ArrayList<Integer>(); return pairHash.get(indice); }
108    */
109
110   /**
111    * Figures out which helix each position belongs to and stores the helix
112    * number in the 'featureGroup' member of a SequenceFeature Based off of RALEE
113    * code ralee-helix-map.
114    * 
115    * @param pairs
116    *          Array of SequenceFeature (output from Rna.GetBasePairs)
117    */
118   public static void HelixMap(SequenceFeature[] pairs)
119   {
120
121     int helix = 0; // Number of helices/current helix
122     int lastopen = 0; // Position of last open bracket reviewed
123     int lastclose = 9999999; // Position of last close bracket reviewed
124     int i = pairs.length; // Number of pairs
125
126     int open; // Position of an open bracket under review
127     int close; // Position of a close bracket under review
128     int j; // Counter
129
130     Hashtable helices = new Hashtable(); // Keep track of helix number for each
131                                          // position
132
133     // Go through each base pair and assign positions a helix
134     for (i = 0; i < pairs.length; i++)
135     {
136
137       open = pairs[i].getBegin();
138       close = pairs[i].getEnd();
139
140       // System.out.println("open " + open + " close " + close);
141       // System.out.println("lastclose " + lastclose + " lastopen " + lastopen);
142
143       // we're moving from right to left based on closing pair
144       /*
145        * catch things like <<..>>..<<..>> |
146        */
147       if (open > lastclose)
148       {
149         helix++;
150       }
151
152       /*
153        * catch things like <<..<<..>>..<<..>>>> |
154        */
155       j = pairs.length - 1;
156       while (j >= 0)
157       {
158         int popen = pairs[j].getBegin();
159
160         // System.out.println("j " + j + " popen " + popen + " lastopen "
161         // +lastopen + " open " + open);
162         if ((popen < lastopen) && (popen > open))
163         {
164           if (helices.containsValue(popen)
165                   && (((Integer) helices.get(popen)) == helix))
166           {
167             continue;
168           }
169           else
170           {
171             helix++;
172             break;
173           }
174         }
175
176         j -= 1;
177       }
178
179       // Put positions and helix information into the hashtable
180       helices.put(open, helix);
181       helices.put(close, helix);
182
183       // Record helix as featuregroup
184       pairs[i].setFeatureGroup(Integer.toString(helix));
185
186       lastopen = open;
187       lastclose = close;
188
189     }
190   }
191 }