f2ef11887a7c1f55d334b0bb89b47a058e61d463
[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.Hashtable;
25 import java.util.Vector;
26
27 import jalview.datamodel.SequenceFeature;
28
29 public class Rna
30 {
31   /**
32    * Based off of RALEE code ralee-get-base-pairs. Keeps track of open bracket
33    * positions in "stack" vector. When a close bracket is reached, pair this
34    * with the last element in the "stack" vector and store in "pairs" vector.
35    * Remove last element in the "stack" vector. Continue in this manner until
36    * the whole string is processed.
37    * 
38    * @param line
39    *          Secondary structure line of an RNA Stockholm file
40    * @return Array of SequenceFeature; type = RNA helix, begin is open base
41    *         pair, end is close base pair
42    */
43   public static SequenceFeature[] GetBasePairs(String line)
44   {
45
46     Vector stack = new Vector();
47     Vector pairs = new Vector();
48
49     int i = 0;
50     while (i < line.length())
51     {
52       char base = line.charAt(i);
53
54       if ((base == '<') || (base == '(') || (base == '{') || (base == '['))
55       {
56         stack.addElement(i);
57       }
58       else if ((base == '>') || (base == ')') || (base == '}')
59               || (base == ']'))
60       {
61
62         Object temp = stack.lastElement();
63         stack.remove(stack.size() - 1);
64         pairs.addElement(temp);
65         pairs.addElement(i);
66       }
67
68       i++;
69     }
70
71     int numpairs = pairs.size() / 2;
72     SequenceFeature[] outPairs = new SequenceFeature[numpairs];
73
74     // Convert pairs to array
75     for (int p = 0; p < pairs.size(); p += 2)
76     {
77       int begin = Integer.parseInt(pairs.elementAt(p).toString());
78       int end = Integer.parseInt(pairs.elementAt(p + 1).toString());
79       
80        outPairs[p / 2] = new SequenceFeature("RNA helix", "", "", begin,
81               end, "");
82     }
83
84     return outPairs;
85   }
86
87   /**
88    * Figures out which helix each position belongs to and stores the helix
89    * number in the 'featureGroup' member of a SequenceFeature Based off of RALEE
90    * code ralee-helix-map.
91    * 
92    * @param pairs
93    *          Array of SequenceFeature (output from Rna.GetBasePairs)
94    */
95   public static void HelixMap(SequenceFeature[] pairs)
96   {
97
98     int helix = 0; // Number of helices/current helix
99     int lastopen = 0; // Position of last open bracket reviewed
100     int lastclose = 9999999; // Position of last close bracket reviewed
101     int i = pairs.length; // Number of pairs
102
103     int open; // Position of an open bracket under review
104     int close; // Position of a close bracket under review
105     int j; // Counter
106
107     Hashtable helices = new Hashtable(); // Keep track of helix number for each
108                                          // position
109
110     // Go through each base pair and assign positions a helix
111     for (i = 0; i < pairs.length; i++)
112     {
113
114       open = pairs[i].getBegin();
115       close = pairs[i].getEnd();
116
117       // System.out.println("open " + open + " close " + close);
118       // System.out.println("lastclose " + lastclose + " lastopen " + lastopen);
119
120       // we're moving from right to left based on closing pair
121       /*
122        * catch things like <<..>>..<<..>> |
123        */
124       if (open > lastclose)
125       {
126         helix++;
127       }
128
129       /*
130        * catch things like <<..<<..>>..<<..>>>> |
131        */
132       j = pairs.length - 1;
133       while (j >= 0)
134       {
135         int popen = pairs[j].getBegin();
136
137         // System.out.println("j " + j + " popen " + popen + " lastopen "
138         // +lastopen + " open " + open);
139         if ((popen < lastopen) && (popen > open))
140         {
141           if (helices.containsValue(popen)
142                   && (((Integer) helices.get(popen)) == helix))
143           {
144             continue;
145           }
146           else
147           {
148             helix++;
149             break;
150           }
151         }
152
153         j -= 1;
154       }
155
156       // Put positions and helix information into the hashtable
157       helices.put(open, helix);
158       helices.put(close, helix);
159
160       // Record helix as featuregroup
161       pairs[i].setFeatureGroup(Integer.toString(helix));
162
163       lastopen = open;
164       lastclose = close;
165
166     }
167   }
168 }