Hashtable size must allow for total-start
[jalview.git] / src / jalview / analysis / Conservation.java
1 /* Jalview - a java multiple alignment editor\r
2  * Copyright (C) 1998  Michele Clamp\r
3  *\r
4  * This program is free software; you can redistribute it and/or\r
5  * modify it under the terms of the GNU General Public License\r
6  * as published by the Free Software Foundation; either version 2\r
7  * of the License, or (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program; if not, write to the Free Software\r
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
17  */\r
18 package jalview.analysis;\r
19 \r
20 \r
21 import java.util.*;\r
22 import jalview.gui.*;\r
23 import jalview.datamodel.*;\r
24 \r
25 \r
26 public class Conservation {\r
27   Vector sequences;\r
28   int    start;\r
29   int    end;\r
30 \r
31   Vector total = new Vector();\r
32 \r
33   String consString = "";\r
34 \r
35   DrawableSequence consSequence;\r
36   Hashtable        propHash;\r
37   int              threshold;\r
38   Hashtable[]      freqs;\r
39 \r
40   String name = "";\r
41 \r
42   public Conservation(String name,Hashtable[] freqs,Hashtable propHash, int threshold, Vector sequences, int start, int end) {\r
43     this.name      = name;\r
44     this.freqs     = freqs;\r
45     this.propHash  = propHash;\r
46     this.threshold = threshold;\r
47     this.sequences = sequences;\r
48     this.start     = start;\r
49     this.end       = end;\r
50   }\r
51 \r
52 \r
53   public void  calculate() {\r
54 \r
55     for (int i = start;i <= end; i++) {\r
56       Hashtable resultHash  = null;\r
57       Hashtable residueHash = null;\r
58 \r
59       resultHash  = new Hashtable();\r
60       residueHash = new Hashtable();\r
61 \r
62       for (int j=0; j < sequences.size(); j++) {\r
63 \r
64         if (sequences.elementAt(j) instanceof Sequence) {\r
65           Sequence s = (Sequence)sequences.elementAt(j);\r
66 \r
67           if (s.getLength() > i) {\r
68             String res = s.getSequence().substring(i,i+1);\r
69 \r
70             if (residueHash.containsKey(res)) {\r
71               int count = ((Integer)residueHash.get(res)).intValue() ;\r
72               count++;\r
73               residueHash.put(res,new Integer(count));\r
74             } else {\r
75               residueHash.put(res,new Integer(1));\r
76             }\r
77           } else {\r
78             if (residueHash.containsKey("-")) {\r
79               int count = ((Integer)residueHash.get("-")).intValue() ;\r
80               count++;\r
81               residueHash.put("-",new Integer(count));\r
82             } else {\r
83               residueHash.put("-",new Integer(1));\r
84             }\r
85           }\r
86         }\r
87       }\r
88 \r
89       //What is the count threshold to count the residues in residueHash()\r
90       int thresh = threshold*(sequences.size())/100;\r
91 \r
92       //loop over all the found residues\r
93       Enumeration e = residueHash.keys();\r
94 \r
95       while (e.hasMoreElements()) {\r
96 \r
97         String res = (String)e.nextElement();\r
98         if (((Integer)residueHash.get(res)).intValue() > thresh) {\r
99 \r
100           //Now loop over the properties\r
101           Enumeration e2 = propHash.keys();\r
102 \r
103           while (e2.hasMoreElements()) {\r
104             String    type = (String)e2.nextElement();\r
105             Hashtable ht   = (Hashtable)propHash.get(type);\r
106 \r
107             //Have we ticked this before?\r
108             if (! resultHash.containsKey(type)) {\r
109               if (ht.containsKey(res)) {\r
110                 resultHash.put(type,ht.get(res));\r
111               } else {\r
112                 resultHash.put(type,ht.get("-"));\r
113               }\r
114             } else if ( ((Integer)resultHash.get(type)).equals((Integer)ht.get(res)) == false) {\r
115               resultHash.put(type,new Integer(-1));\r
116             }\r
117           }\r
118         }\r
119       }\r
120       total.addElement(resultHash);\r
121     }\r
122   }\r
123 \r
124   public int countGaps(int j) {\r
125     int count = 0;\r
126 \r
127     for (int i = 0; i < sequences.size();i++) {\r
128       if( j+1 > ((Sequence)sequences.elementAt(i)).getSequence().length())\r
129       {  count++; continue;}\r
130 \r
131       String tmp = ((Sequence)sequences.elementAt(i)).getSequence().substring(j,j+1);\r
132       if (tmp.equals(" ") || tmp.equals(".") || tmp.equals("-")) {\r
133         count++;\r
134       }\r
135     }\r
136     return count;\r
137   }\r
138 \r
139   public  void  verdict(boolean consflag, float percentageGaps) {\r
140     String consString = "";\r
141 \r
142     for (int i=start; i <= end; i++) {\r
143       int totGaps = countGaps(i);\r
144       float pgaps = (float)totGaps*100/(float)sequences.size();\r
145 \r
146       if (percentageGaps > pgaps)\r
147       {\r
148         Hashtable resultHash = (Hashtable)total.elementAt(i-start);\r
149 \r
150         //Now find the verdict\r
151         int         count = 0;\r
152         Enumeration e3    = resultHash.keys();\r
153 \r
154         while (e3.hasMoreElements())\r
155         {\r
156           String type    = (String)e3.nextElement();\r
157           Integer result = (Integer)resultHash.get(type);\r
158 \r
159           //Do we want to count +ve conservation or +ve and -ve cons.?\r
160 \r
161           if (consflag)\r
162           {\r
163             if (result.intValue() == 1)\r
164               count++;\r
165           }\r
166           else\r
167           {\r
168             if (result.intValue() != -1)\r
169               count++;\r
170           }\r
171         }\r
172 \r
173         if (count < 10)\r
174           consString = consString + String.valueOf(count);\r
175         else\r
176           consString = consString + "*";\r
177 \r
178       }\r
179       else\r
180       {\r
181         consString = consString + "-";\r
182       }\r
183     }\r
184 \r
185     consSequence = new DrawableSequence(name,consString,start,end);\r
186   }\r
187 \r
188   public jalview.gui.DrawableSequence getConsSequence() {\r
189     return consSequence;\r
190   }\r
191 \r
192 }\r