pass viewport to fileloader, as we need to firepropertychange once sequences are...
[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 = 1;\r
39       int existingAnnotations = 0;\r
40       if(al.getAlignmentAnnotation()!=null)\r
41        existingAnnotations = al.getAlignmentAnnotation().length;\r
42 \r
43       int alWidth = al.getWidth();\r
44 \r
45       StringTokenizer st;\r
46       Annotation[] annotations;\r
47       AlignmentAnnotation annotation = null;\r
48 \r
49       // First confirm this is an Annotation file\r
50       boolean jvAnnotationFile = false;\r
51       while ( (line = in.readLine()) != null)\r
52       {\r
53         if (line.indexOf("#") == 0 )\r
54           continue;\r
55 \r
56         if (line.indexOf("JALVIEW_ANNOTATION") > -1)\r
57         {\r
58           jvAnnotationFile = true;\r
59           break;\r
60         }\r
61       }\r
62 \r
63       if(!jvAnnotationFile)\r
64       {\r
65         in.close();\r
66         return false;\r
67       }\r
68 \r
69       while ( (line = in.readLine()) != null)\r
70       {\r
71         if(line.indexOf("#")==0\r
72            || line.indexOf("JALVIEW_ANNOTATION")>-1\r
73            || line.length()==0)\r
74           continue;\r
75 \r
76         st = new StringTokenizer(line, "\t");\r
77         token = st.nextToken();\r
78         if(token.equalsIgnoreCase("COLOUR"))\r
79         {\r
80           colourAnnotations(al, st.nextToken(), st.nextToken());\r
81           continue;\r
82         }\r
83 \r
84         if(token.equalsIgnoreCase("COMBINE") )\r
85         {\r
86           combineAnnotations(al, st);\r
87           continue;\r
88         }\r
89 \r
90         if (token.equalsIgnoreCase("GRAPHLINE"))\r
91         {\r
92           addLine(al, st);\r
93           continue;\r
94         }\r
95 \r
96 \r
97         if(token.equalsIgnoreCase("SEQUENCE_REF") )\r
98         {\r
99           refSeq = al.findName(st.nextToken());\r
100           try{\r
101             refSeqIndex = Integer.parseInt(st.nextToken());\r
102           }\r
103           catch(Exception ex)\r
104           {\r
105             refSeqIndex = 1;\r
106           }\r
107 \r
108           continue;\r
109         }\r
110 \r
111 \r
112         graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
113         label = description = st.nextToken();\r
114 \r
115         line = st.nextToken();\r
116 \r
117         st = new StringTokenizer(line, "|", true);\r
118         annotations = new Annotation[alWidth];\r
119 \r
120         index = 0;\r
121         boolean emptyColumn = true;\r
122 \r
123 \r
124         while (st.hasMoreElements() && index<alWidth)\r
125         {\r
126           token = st.nextToken().trim();\r
127           if(token.equals("|"))\r
128           {\r
129             if(emptyColumn)\r
130               index++;\r
131 \r
132             emptyColumn = true;\r
133           }\r
134           else\r
135           {\r
136             annotations[index++] = parseAnnotation(token);\r
137             emptyColumn = false;\r
138           }\r
139         }\r
140 \r
141        annotation = new AlignmentAnnotation(label,\r
142                                           description,\r
143                                           annotations,\r
144                                           0,\r
145                                           0,\r
146                                           graphStyle);\r
147 \r
148        if(refSeq!=null)\r
149        {\r
150          annotation.createSequenceMapping(refSeq, refSeqIndex);\r
151          refSeq.addAlignmentAnnotation(annotation);\r
152        }\r
153 \r
154        al.addAnnotation(annotation);\r
155 \r
156        al.setAnnotationIndex(annotation,  al.getAlignmentAnnotation().length - existingAnnotations-1);\r
157       }\r
158 \r
159     }catch(Exception ex)\r
160     {\r
161       ex.printStackTrace();\r
162       System.out.println("Problem reading annotation file: "+ex);\r
163       return false;\r
164     }\r
165     return true;\r
166   }\r
167 \r
168   Annotation parseAnnotation(String string)\r
169   {\r
170     String desc = "", displayChar="";\r
171     char ss = ' '; // secondaryStructure\r
172     float value = 0;\r
173     StringTokenizer st = new StringTokenizer(string, ",");\r
174     String token;\r
175     while(st.hasMoreTokens())\r
176     {\r
177       token = st.nextToken().trim();\r
178       if(token.length()==0)\r
179         continue;\r
180 \r
181       if(value==0)\r
182       {  try{\r
183           value =  new Float(token).floatValue();\r
184         }catch(NumberFormatException ex){}\r
185       }\r
186 \r
187       if(token.length()==1)\r
188       {\r
189         // Either this character represents a helix or sheet\r
190         // or an integer which can be displayed\r
191         if(token.equals("H") || token.equals("E"))\r
192         {\r
193           ss = token.charAt(0);\r
194         }\r
195         else //if(value!=0)\r
196         {\r
197           displayChar = token;\r
198         }\r
199       }\r
200       else if(desc.length()<1)\r
201         desc = token;\r
202       else\r
203         displayChar = token;\r
204     }\r
205 \r
206     return new Annotation(displayChar, desc, ss, value);\r
207   }\r
208 \r
209   void colourAnnotations(AlignmentI al, String label, String colour)\r
210   {\r
211     UserColourScheme ucs = new UserColourScheme(colour);\r
212     Annotation[] annotations;\r
213     for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
214     {\r
215       if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))\r
216       {\r
217         annotations = al.getAlignmentAnnotation()[i].annotations;\r
218         for(int j=0; j<annotations.length; j++)\r
219         {\r
220           if(annotations[j]!=null)\r
221             annotations[j].colour = ucs.findColour("A");\r
222         }\r
223       }\r
224     }\r
225   }\r
226 \r
227   void combineAnnotations(AlignmentI al, StringTokenizer st)\r
228   {\r
229     int graphGroup = -1;\r
230     String group = st.nextToken();\r
231     //First make sure we are not overwriting the graphIndex\r
232     for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
233     {\r
234       if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
235       {\r
236         graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;\r
237         al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
238         break;\r
239       }\r
240     }\r
241 \r
242     //Now update groups\r
243     while(st.hasMoreTokens())\r
244     {\r
245       group = st.nextToken();\r
246       for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
247       {\r
248         if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
249         {\r
250           al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
251           break;\r
252         }\r
253       }\r
254     }\r
255   }\r
256 \r
257   void addLine(AlignmentI al, StringTokenizer st)\r
258   {\r
259     String group = st.nextToken();\r
260     AlignmentAnnotation annotation = null;\r
261 \r
262     for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
263     {\r
264       if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
265       {\r
266         annotation = al.getAlignmentAnnotation()[i];\r
267         break;\r
268       }\r
269     }\r
270 \r
271     if(annotation==null)\r
272       return;\r
273     float value = new Float(st.nextToken()).floatValue();\r
274     String label = st.hasMoreTokens() ?  st.nextToken() : null;\r
275     java.awt.Color colour = null;\r
276     if(st.hasMoreTokens())\r
277     {\r
278       UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
279       colour = ucs.findColour("A");\r
280     }\r
281 \r
282     annotation.setThreshold(new GraphLine(value, label, colour));\r
283 \r
284   }\r
285 }\r