New files
[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 jalview.gui.FeatureRenderer;\r
8 \r
9 public class AnnotationReader\r
10 {\r
11   public boolean readGroupsFile(FeatureRenderer fr, AlignmentI al, String file)\r
12   {\r
13     try\r
14     {\r
15       BufferedReader in = new BufferedReader(new FileReader(file));\r
16       SequenceI seq = null;\r
17       String line, type, desc, token;\r
18 \r
19       int index, start, end;\r
20       StringTokenizer st;\r
21       SequenceFeature sf;\r
22       int lineNo = 0;\r
23       while ( (line = in.readLine()) != null)\r
24       {\r
25         lineNo++;\r
26         st = new StringTokenizer(line, "\t");\r
27         if (st.countTokens() == 2)\r
28         {\r
29           type = st.nextToken();\r
30           UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
31           fr.setColour(type, ucs.findColour("A"));\r
32           continue;\r
33         }\r
34 \r
35         while (st.hasMoreElements())\r
36         {\r
37           desc = st.nextToken();\r
38           token = st.nextToken();\r
39           if (!token.equals("ID_NOT_SPECIFIED"))\r
40           {\r
41             index = al.findIndex(al.findName(token));\r
42             st.nextToken();\r
43           }\r
44           else\r
45           {\r
46             index = Integer.parseInt(st.nextToken());\r
47           }\r
48 \r
49           start = Integer.parseInt(st.nextToken());\r
50           end = Integer.parseInt(st.nextToken());\r
51 \r
52           seq = al.getSequenceAt(index);\r
53           start = seq.findIndex(start) - 1;\r
54           end = seq.findIndex(end) - 1;\r
55 \r
56           type = st.nextToken();\r
57 \r
58           if (fr.getColour(type) == null)\r
59           {\r
60             // Probably the old style groups file\r
61             UserColourScheme ucs = new UserColourScheme(type);\r
62             fr.setColour(type, ucs.findColour("A"));\r
63           }\r
64 \r
65           sf = new SequenceFeature(type, desc, "", start, end);\r
66 \r
67           seq.getDatasetSequence().addSequenceFeature(sf);\r
68         }\r
69       }\r
70     }\r
71     catch (Exception ex)\r
72     {\r
73       System.out.println("Error parsing groups file: " + ex);\r
74       return false;\r
75     }\r
76 \r
77     return true;\r
78   }\r
79 \r
80   public boolean readAnnotationFile(AlignmentI al, String file)\r
81   {\r
82     try\r
83     {\r
84       BufferedReader in = new BufferedReader(new FileReader(file));\r
85       String line, label, description, token;\r
86       int graphStyle, index;\r
87       SequenceI refSeq = null;\r
88       int refSeqIndex = -1;\r
89 \r
90 \r
91       StringTokenizer st;\r
92       Annotation[] annotations;\r
93       AlignmentAnnotation annotation = null;\r
94 \r
95       // First confirm this is an Annotation file\r
96       boolean jvAnnotationFile = false;\r
97       while ( (line = in.readLine()) != null)\r
98       {\r
99         if (line.indexOf("#") == 0 )\r
100           continue;\r
101 \r
102         if (line.indexOf("JALVIEW_ANNOTATION") > -1)\r
103         {\r
104           jvAnnotationFile = true;\r
105           break;\r
106         }\r
107       }\r
108 \r
109       if(!jvAnnotationFile)\r
110       {\r
111         in.close();\r
112         return false;\r
113       }\r
114 \r
115       while ( (line = in.readLine()) != null)\r
116       {\r
117         if(line.indexOf("#")==0\r
118            || line.indexOf("JALVIEW_ANNOTATION")>-1\r
119            || line.length()==0)\r
120           continue;\r
121 \r
122         st = new StringTokenizer(line, "\t");\r
123         token = st.nextToken();\r
124         if(token.equalsIgnoreCase("COLOUR"))\r
125         {\r
126           colourAnnotations(al, st.nextToken(), st.nextToken());\r
127           continue;\r
128         }\r
129 \r
130         if(token.equalsIgnoreCase("COMBINE") )\r
131         {\r
132           combineAnnotations(al, st);\r
133           continue;\r
134         }\r
135 \r
136         if (token.equalsIgnoreCase("GRAPHLINE"))\r
137         {\r
138           addLine(al, st);\r
139           continue;\r
140         }\r
141 \r
142 \r
143         if(token.equalsIgnoreCase("SEQUENCE_REF") )\r
144         {\r
145           refSeq = al.findName(st.nextToken());\r
146           try{\r
147             refSeqIndex = Integer.parseInt(st.nextToken());\r
148           }\r
149           catch(NumberFormatException ex)\r
150           {\r
151             refSeqIndex = 0;\r
152           }\r
153 \r
154           continue;\r
155         }\r
156 \r
157         graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
158         label = description = st.nextToken();\r
159 \r
160         line = st.nextToken();\r
161         st = new StringTokenizer(line, "|", true);\r
162         annotations = new Annotation[st.countTokens()];\r
163         index = 0;\r
164         boolean emptyColumn = true;\r
165         while (st.hasMoreElements())\r
166         {\r
167           token = st.nextToken().trim();\r
168 \r
169           if(token.equals("|"))\r
170           {\r
171             if(emptyColumn)\r
172               annotations[index++] = parseAnnotation("");\r
173 \r
174             emptyColumn = true;\r
175           }\r
176           else\r
177           {\r
178             annotations[index++] = parseAnnotation(token);\r
179             emptyColumn = false;\r
180           }\r
181         }\r
182 \r
183        annotation = new AlignmentAnnotation(label,\r
184                                           description,\r
185                                           annotations,\r
186                                           0,\r
187                                           0,\r
188                                           graphStyle);\r
189 \r
190        al.addAnnotation(annotation, refSeq, refSeqIndex);\r
191 \r
192       }\r
193     }catch(Exception ex)\r
194     {ex.printStackTrace();\r
195     //  System.out.println("Problem reading annotation file: "+ex);\r
196       return false;\r
197     }\r
198     return true;\r
199   }\r
200 \r
201   Annotation parseAnnotation(String string)\r
202   {\r
203     String desc = "", displayChar="";\r
204     char ss = ' '; // secondaryStructure\r
205     float value = 0;\r
206     StringTokenizer st = new StringTokenizer(string, ",");\r
207     String token;\r
208     while(st.hasMoreTokens())\r
209     {\r
210       token = st.nextToken().trim();\r
211       if(token.length()==0)\r
212         continue;\r
213 \r
214       if(value==0)\r
215       {  try{\r
216           value = Float.parseFloat(token);\r
217         }catch(NumberFormatException ex){}\r
218       }\r
219 \r
220       if(token.length()==1)\r
221       {\r
222         // Either this character represents a helix or sheet\r
223         // or an integer which can be displayed\r
224         if(token.equals("H") || token.equals("E"))\r
225         {\r
226           ss = token.charAt(0);\r
227         }\r
228         else //if(value!=0)\r
229         {\r
230           displayChar = token;\r
231         }\r
232       }\r
233       else if(desc.length()<1)\r
234         desc = token;\r
235       else\r
236         displayChar = token;\r
237     }\r
238 \r
239     return new Annotation(displayChar, desc, ss, value);\r
240   }\r
241 \r
242   void colourAnnotations(AlignmentI al, String label, String colour)\r
243   {\r
244     UserColourScheme ucs = new UserColourScheme(colour);\r
245     Annotation[] annotations;\r
246     for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
247     {\r
248       if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))\r
249       {\r
250         annotations = al.getAlignmentAnnotation()[i].annotations;\r
251         for(int j=0; j<annotations.length; j++)\r
252         {\r
253           if(annotations[j]!=null)\r
254             annotations[j].colour = ucs.findColour("A");\r
255         }\r
256       }\r
257     }\r
258   }\r
259 \r
260   void combineAnnotations(AlignmentI al, StringTokenizer st)\r
261   {\r
262     int graphGroup = -1;\r
263     String group = st.nextToken();\r
264     //First make sure we are not overwriting the graphIndex\r
265     for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
266     {\r
267       if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
268       {\r
269         graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;\r
270         al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
271         break;\r
272       }\r
273     }\r
274 \r
275     //Now update groups\r
276     while(st.hasMoreTokens())\r
277     {\r
278       group = st.nextToken();\r
279       for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
280       {\r
281         if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
282         {\r
283           al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
284           break;\r
285         }\r
286       }\r
287     }\r
288   }\r
289 \r
290   void addLine(AlignmentI al, StringTokenizer st)\r
291   {\r
292     String group = st.nextToken();\r
293     AlignmentAnnotation annotation = null;\r
294 \r
295     for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
296     {\r
297       if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
298       {\r
299         annotation = al.getAlignmentAnnotation()[i];\r
300         break;\r
301       }\r
302     }\r
303 \r
304     if(annotation==null)\r
305       return;\r
306 \r
307     float value = Float.parseFloat(st.nextToken());\r
308     String label = st.hasMoreTokens() ?  st.nextToken() : null;\r
309     java.awt.Color colour = null;\r
310     if(st.hasMoreTokens())\r
311     {\r
312       UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
313       colour = ucs.findColour("A");\r
314     }\r
315 \r
316     annotation.addGraphLine(new GraphLine(value, label, colour));\r
317 \r
318   }\r
319 }\r