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