Jalview Imported Sources
[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.getSequence().length() > 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       String tmp = ((Sequence)sequences.elementAt(i)).getSequence().substring(j,j+1);\r
129       if (tmp.equals(" ") || tmp.equals(".") || tmp.equals("-")) {\r
130         count++;\r
131       }\r
132     }\r
133     return count;\r
134   }\r
135 \r
136   public  void  verdict(boolean consflag, float percentageGaps) {\r
137     String consString = "";\r
138 \r
139     for (int i=start; i <= end; i++) {\r
140       int totGaps = countGaps(i);\r
141       float pgaps = (float)totGaps*100/(float)sequences.size();\r
142 \r
143       if (percentageGaps > pgaps) {\r
144         Hashtable resultHash = (Hashtable)total.elementAt(i);\r
145 \r
146         //Now find the verdict\r
147         int         count = 0;\r
148         Enumeration e3    = resultHash.keys();\r
149 \r
150         while (e3.hasMoreElements()) {\r
151           String type    = (String)e3.nextElement();\r
152           Integer result = (Integer)resultHash.get(type);\r
153 \r
154           //Do we want to count +ve conservation or +ve and -ve cons.?\r
155 \r
156           if (consflag) {\r
157             if (result.intValue() == 1) {\r
158               count++;\r
159             }\r
160           } else {\r
161             if (result.intValue() != -1) {\r
162               count++;\r
163             }\r
164           }\r
165         }\r
166 \r
167         if (count < 10) {\r
168           consString = consString + String.valueOf(count);\r
169         } else {\r
170           consString = consString + "*";\r
171         }\r
172       } else {\r
173         consString = consString + "-";\r
174       }\r
175     }\r
176 \r
177     consSequence = new DrawableSequence(name,consString,start,end);\r
178 \r
179   }\r
180 \r
181   public jalview.gui.DrawableSequence getConsSequence() {\r
182     return consSequence;\r
183   }\r
184 \r
185 }\r