This commit was manufactured by cvs2svn to create branch 'VamJalview'.
[jalview.git] / src / jalview / io / AnnotationReader.java
1 package jalview.io;\r
2 \r
3 import java.io.*;\r
4 import jalview.datamodel.*;\r
5 import java.util.StringTokenizer;\r
6 import jalview.schemes.UserColourScheme;\r
7 import java.net.URL;\r
8 \r
9 public class AnnotationReader\r
10 {\r
11   public boolean readAnnotationFile(AlignmentI al, String file)\r
12   {\r
13 \r
14     try\r
15     {\r
16       BufferedReader in = null;\r
17       java.io.InputStream is = getClass().getResourceAsStream("/" + file);\r
18       if (is != null)\r
19       {\r
20         in = new BufferedReader(new java.io.InputStreamReader(is));\r
21       }\r
22       else\r
23       {\r
24         try\r
25         {\r
26           URL url = new URL(file);\r
27           in = new BufferedReader(new InputStreamReader(url.openStream()));\r
28         }\r
29         catch (java.net.MalformedURLException ex)\r
30         {\r
31           in = new BufferedReader(new FileReader(file));\r
32         }\r
33       }\r
34 \r
35       String line, label, description, token;\r
36       int graphStyle, index;\r
37       SequenceI refSeq = null;\r
38       int refSeqIndex = 0;\r
39       int existingAnnotations = 0;\r
40       if(al.getAlignmentAnnotation()!=null)\r
41        existingAnnotations = al.getAlignmentAnnotation().length;\r
42 \r
43       StringTokenizer st;\r
44       Annotation[] annotations;\r
45       AlignmentAnnotation annotation = null;\r
46 \r
47       // First confirm this is an Annotation file\r
48       boolean jvAnnotationFile = false;\r
49       while ( (line = in.readLine()) != null)\r
50       {\r
51         if (line.indexOf("#") == 0 )\r
52           continue;\r
53 \r
54         if (line.indexOf("JALVIEW_ANNOTATION") > -1)\r
55         {\r
56           jvAnnotationFile = true;\r
57           break;\r
58         }\r
59       }\r
60 \r
61       if(!jvAnnotationFile)\r
62       {\r
63         in.close();\r
64         return false;\r
65       }\r
66 \r
67       while ( (line = in.readLine()) != null)\r
68       {\r
69         if(line.indexOf("#")==0\r
70            || line.indexOf("JALVIEW_ANNOTATION")>-1\r
71            || line.length()==0)\r
72           continue;\r
73 \r
74         st = new StringTokenizer(line, "\t");\r
75         token = st.nextToken();\r
76         if(token.equalsIgnoreCase("COLOUR"))\r
77         {\r
78           colourAnnotations(al, st.nextToken(), st.nextToken());\r
79           continue;\r
80         }\r
81 \r
82         if(token.equalsIgnoreCase("COMBINE") )\r
83         {\r
84           combineAnnotations(al, st);\r
85           continue;\r
86         }\r
87 \r
88         if (token.equalsIgnoreCase("GRAPHLINE"))\r
89         {\r
90           addLine(al, st);\r
91           continue;\r
92         }\r
93 \r
94 \r
95         if(token.equalsIgnoreCase("SEQUENCE_REF") )\r
96         {\r
97           refSeq = al.findName(st.nextToken());\r
98           try{\r
99             refSeqIndex = Integer.parseInt(st.nextToken());\r
100           }\r
101           catch(Exception ex)\r
102           {\r
103             refSeqIndex = 0;\r
104           }\r
105 \r
106           continue;\r
107         }\r
108 \r
109         graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
110         label = description = st.nextToken();\r
111 \r
112         line = st.nextToken();\r
113         st = new StringTokenizer(line, "|", true);\r
114         annotations = new Annotation[st.countTokens()+refSeqIndex];\r
115         index = refSeqIndex;\r
116         boolean emptyColumn = true;\r
117         while (st.hasMoreElements())\r
118         {\r
119           token = st.nextToken().trim();\r
120 \r
121           if(token.equals("|"))\r
122           {\r
123             if(emptyColumn)\r
124               annotations[index++] = parseAnnotation("");\r
125 \r
126             emptyColumn = true;\r
127           }\r
128           else\r
129           {\r
130             annotations[index++] = parseAnnotation(token);\r
131             emptyColumn = false;\r
132           }\r
133         }\r
134 \r
135        annotation = new AlignmentAnnotation(label,\r
136                                           description,\r
137                                           annotations,\r
138                                           0,\r
139                                           0,\r
140                                           graphStyle);\r
141 \r
142        annotation = al.addAnnotation(annotation, refSeq);\r
143        al.setAnnotationIndex(annotation,  al.getAlignmentAnnotation().length - existingAnnotations-1);\r
144       }\r
145 \r
146        al.adjustSequenceAnnotations();\r
147 \r
148 \r
149     }catch(Exception ex)\r
150     {\r
151       ex.printStackTrace();\r
152       System.out.println("Problem reading annotation file: "+ex);\r
153       return false;\r
154     }\r
155     return true;\r
156   }\r
157 \r
158   Annotation parseAnnotation(String string)\r
159   {\r
160     String desc = "", displayChar="";\r
161     char ss = ' '; // secondaryStructure\r
162     float value = 0;\r
163     StringTokenizer st = new StringTokenizer(string, ",");\r
164     String token;\r
165     while(st.hasMoreTokens())\r
166     {\r
167       token = st.nextToken().trim();\r
168       if(token.length()==0)\r
169         continue;\r
170 \r
171       if(value==0)\r
172       {  try{\r
173           value =  new Float(token).floatValue();\r
174         }catch(NumberFormatException ex){}\r
175       }\r
176 \r
177       if(token.length()==1)\r
178       {\r
179         // Either this character represents a helix or sheet\r
180         // or an integer which can be displayed\r
181         if(token.equals("H") || token.equals("E"))\r
182         {\r
183           ss = token.charAt(0);\r
184         }\r
185         else //if(value!=0)\r
186         {\r
187           displayChar = token;\r
188         }\r
189       }\r
190       else if(desc.length()<1)\r
191         desc = token;\r
192       else\r
193         displayChar = token;\r
194     }\r
195 \r
196     return new Annotation(displayChar, desc, ss, value);\r
197   }\r
198 \r
199   void colourAnnotations(AlignmentI al, String label, String colour)\r
200   {\r
201     UserColourScheme ucs = new UserColourScheme(colour);\r
202     Annotation[] annotations;\r
203     for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
204     {\r
205       if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))\r
206       {\r
207         annotations = al.getAlignmentAnnotation()[i].annotations;\r
208         for(int j=0; j<annotations.length; j++)\r
209         {\r
210           if(annotations[j]!=null)\r
211             annotations[j].colour = ucs.findColour("A");\r
212         }\r
213       }\r
214     }\r
215   }\r
216 \r
217   void combineAnnotations(AlignmentI al, StringTokenizer st)\r
218   {\r
219     int graphGroup = -1;\r
220     String group = st.nextToken();\r
221     //First make sure we are not overwriting the graphIndex\r
222     for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
223     {\r
224       if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
225       {\r
226         graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;\r
227         al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
228         break;\r
229       }\r
230     }\r
231 \r
232     //Now update groups\r
233     while(st.hasMoreTokens())\r
234     {\r
235       group = st.nextToken();\r
236       for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
237       {\r
238         if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
239         {\r
240           al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
241           break;\r
242         }\r
243       }\r
244     }\r
245   }\r
246 \r
247   void addLine(AlignmentI al, StringTokenizer st)\r
248   {\r
249     String group = st.nextToken();\r
250     AlignmentAnnotation annotation = null;\r
251 \r
252     for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
253     {\r
254       if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
255       {\r
256         annotation = al.getAlignmentAnnotation()[i];\r
257         break;\r
258       }\r
259     }\r
260 \r
261     if(annotation==null)\r
262       return;\r
263     float value = new Float(st.nextToken()).floatValue();\r
264     String label = st.hasMoreTokens() ?  st.nextToken() : null;\r
265     java.awt.Color colour = null;\r
266     if(st.hasMoreTokens())\r
267     {\r
268       UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
269       colour = ucs.findColour("A");\r
270     }\r
271 \r
272     annotation.setThreshold(new GraphLine(value, label, colour));\r
273 \r
274   }\r
275 }\r