71ccbd1e4884e4818aa057cbdbcb14c3ed68d2df
[jalview.git] / src / jalview / analysis / Rna.java
1 /* Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
2  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
3  * 
4  * This file is part of Jalview.
5  * 
6  * Jalview is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License 
8  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9  * 
10  * Jalview is distributed in the hope that it will be useful, but 
11  * WITHOUT ANY WARRANTY; without even the implied warranty 
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
13  * PURPOSE.  See the GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
16  */
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.ArrayList;
25 import java.util.Hashtable;
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    * Based off of RALEE code ralee-get-base-pairs. Keeps track of open bracket
35    * positions in "stack" vector. When a close bracket is reached, pair this
36    * with the last element in the "stack" vector and store in "pairs" vector.
37    * Remove last element in the "stack" vector. Continue in this manner until
38    * the whole string is processed.
39    * 
40    * @param line
41    *          Secondary structure line of an RNA Stockholm file
42    * @return Array of SequenceFeature; type = RNA helix, begin is open base
43    *         pair, end is close base pair
44    */
45   public static SequenceFeature[] GetBasePairs(String line)
46   {
47
48     Vector stack = new Vector();
49     Vector pairs = new Vector();
50
51     int i = 0;
52     while (i < line.length())
53     {
54       char base = line.charAt(i);
55
56       if ((base == '<') || (base == '(') || (base == '{') || (base == '['))
57       {
58         stack.addElement(i);
59       }
60       else if ((base == '>') || (base == ')') || (base == '}')
61               || (base == ']'))
62       {
63
64         Object temp = stack.lastElement();
65         stack.remove(stack.size() - 1);
66         pairs.addElement(temp);
67         pairs.addElement(i);        
68       }
69
70       i++;
71     }
72
73     int numpairs = pairs.size() / 2;
74     SequenceFeature[] outPairs = new SequenceFeature[numpairs];
75
76     // Convert pairs to array
77     for (int p = 0; p < pairs.size(); p += 2)
78     {
79       int begin = Integer.parseInt(pairs.elementAt(p).toString());
80       int end = Integer.parseInt(pairs.elementAt(p + 1).toString());
81       
82        outPairs[p / 2] = new SequenceFeature("RNA helix", "", "", begin,
83               end, "");
84        //pairHash.put(begin, end);
85
86     }
87
88     return outPairs;
89   }
90   
91   
92   /**
93    * Function to get the end position corresponding to a given start position
94    * @param indice - start position of a base pair
95    * @return - end position of a base pair
96    */
97   /*makes no sense at the moment :(
98   public int findEnd(int indice){
99           //TODO: Probably extend this to find the start to a given end?
100           //could be done by putting everything twice to the hash
101           ArrayList<Integer> pair = new ArrayList<Integer>();
102           return pairHash.get(indice);
103   }*/
104   
105
106   /**
107    * Figures out which helix each position belongs to and stores the helix
108    * number in the 'featureGroup' member of a SequenceFeature Based off of RALEE
109    * code ralee-helix-map.
110    * 
111    * @param pairs
112    *          Array of SequenceFeature (output from Rna.GetBasePairs)
113    */
114   public static void HelixMap(SequenceFeature[] pairs)
115   {
116
117     int helix = 0; // Number of helices/current helix
118     int lastopen = 0; // Position of last open bracket reviewed
119     int lastclose = 9999999; // Position of last close bracket reviewed
120     int i = pairs.length; // Number of pairs
121
122     int open; // Position of an open bracket under review
123     int close; // Position of a close bracket under review
124     int j; // Counter
125
126     Hashtable helices = new Hashtable(); // Keep track of helix number for each
127                                          // position
128
129     // Go through each base pair and assign positions a helix
130     for (i = 0; i < pairs.length; i++)
131     {
132
133       open = pairs[i].getBegin();
134       close = pairs[i].getEnd();
135
136       // System.out.println("open " + open + " close " + close);
137       // System.out.println("lastclose " + lastclose + " lastopen " + lastopen);
138
139       // we're moving from right to left based on closing pair
140       /*
141        * catch things like <<..>>..<<..>> |
142        */
143       if (open > lastclose)
144       {
145         helix++;
146       }
147
148       /*
149        * catch things like <<..<<..>>..<<..>>>> |
150        */
151       j = pairs.length - 1;
152       while (j >= 0)
153       {
154         int popen = pairs[j].getBegin();
155
156         // System.out.println("j " + j + " popen " + popen + " lastopen "
157         // +lastopen + " open " + open);
158         if ((popen < lastopen) && (popen > open))
159         {
160           if (helices.containsValue(popen)
161                   && (((Integer) helices.get(popen)) == helix))
162           {
163             continue;
164           }
165           else
166           {
167             helix++;
168             break;
169           }
170         }
171
172         j -= 1;
173       }
174
175       // Put positions and helix information into the hashtable
176       helices.put(open, helix);
177       helices.put(close, helix);
178
179       // Record helix as featuregroup
180       pairs[i].setFeatureGroup(Integer.toString(helix));
181
182       lastopen = open;
183       lastclose = close;
184
185     }
186   }
187 }