drawableSequence removed
[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   Sequence consSequence;\r
36   Hashtable        propHash;\r
37   int              threshold;\r
38 \r
39   String name = "";\r
40 \r
41   public Conservation(String name,Hashtable propHash, int threshold, Vector sequences, int start, int end) {\r
42     this.name      = name;\r
43     this.propHash  = propHash;\r
44     this.threshold = threshold;\r
45     this.sequences = sequences;\r
46     this.start     = start;\r
47     this.end       = end;\r
48   }\r
49 \r
50 \r
51   public void  calculate() {\r
52 \r
53     for (int i = start;i <= end; i++) {\r
54       Hashtable resultHash  = null;\r
55       Hashtable residueHash = null;\r
56 \r
57       resultHash  = new Hashtable();\r
58       residueHash = new Hashtable();\r
59 \r
60       for (int j=0; j < sequences.size(); j++) {\r
61 \r
62         if (sequences.elementAt(j) instanceof Sequence) {\r
63           Sequence s = (Sequence)sequences.elementAt(j);\r
64 \r
65           if (s.getLength() > i) {\r
66             String res = s.getSequence().substring(i,i+1);\r
67 \r
68             if (residueHash.containsKey(res)) {\r
69               int count = ((Integer)residueHash.get(res)).intValue() ;\r
70               count++;\r
71               residueHash.put(res,new Integer(count));\r
72             } else {\r
73               residueHash.put(res,new Integer(1));\r
74             }\r
75           } else {\r
76             if (residueHash.containsKey("-")) {\r
77               int count = ((Integer)residueHash.get("-")).intValue() ;\r
78               count++;\r
79               residueHash.put("-",new Integer(count));\r
80             } else {\r
81               residueHash.put("-",new Integer(1));\r
82             }\r
83           }\r
84         }\r
85       }\r
86 \r
87       //What is the count threshold to count the residues in residueHash()\r
88       int thresh = threshold*(sequences.size())/100;\r
89 \r
90       //loop over all the found residues\r
91       Enumeration e = residueHash.keys();\r
92 \r
93       while (e.hasMoreElements()) {\r
94 \r
95         String res = (String)e.nextElement();\r
96         if (((Integer)residueHash.get(res)).intValue() > thresh) {\r
97 \r
98           //Now loop over the properties\r
99           Enumeration e2 = propHash.keys();\r
100 \r
101           while (e2.hasMoreElements()) {\r
102             String    type = (String)e2.nextElement();\r
103             Hashtable ht   = (Hashtable)propHash.get(type);\r
104 \r
105             //Have we ticked this before?\r
106             if (! resultHash.containsKey(type)) {\r
107               if (ht.containsKey(res)) {\r
108                 resultHash.put(type,ht.get(res));\r
109               } else {\r
110                 resultHash.put(type,ht.get("-"));\r
111               }\r
112             } else if ( ((Integer)resultHash.get(type)).equals((Integer)ht.get(res)) == false) {\r
113               resultHash.put(type,new Integer(-1));\r
114             }\r
115           }\r
116         }\r
117       }\r
118       total.addElement(resultHash);\r
119     }\r
120   }\r
121 \r
122   public int countGaps(int j)\r
123   {\r
124     int count = 0;\r
125 \r
126     for (int i = 0; i < sequences.size();i++)\r
127     {\r
128       if( j+1 > ((Sequence)sequences.elementAt(i)).getSequence().length())\r
129       {  count++; continue;}\r
130 \r
131       char c = ((Sequence)sequences.elementAt(i)).getSequence().charAt(j);\r
132       if (jalview.util.Comparison.isGap((c)))\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 Sequence(name,consString,start,end);\r
186   }\r
187 \r
188   public Sequence getConsSequence() {\r
189     return consSequence;\r
190   }\r
191 \r
192 }\r