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