Wrap alignment added to preferences
[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     boolean parsedValue = false;\r
174     StringTokenizer st = new StringTokenizer(string, ",");\r
175     String token;\r
176     while(st.hasMoreTokens())\r
177     {\r
178       token = st.nextToken().trim();\r
179       if(token.length()==0)\r
180         continue;\r
181 \r
182       if(!parsedValue)\r
183       {\r
184         try{\r
185           value = new Float(token).floatValue();\r
186           displayChar = token;\r
187           parsedValue = true;\r
188         }catch(NumberFormatException ex){}\r
189       }\r
190 \r
191       if(token.equals("H") || token.equals("E"))\r
192       {\r
193         // Either this character represents a helix or sheet\r
194         // or an integer which can be displayed\r
195         ss = token.charAt(0);\r
196       }\r
197       else if(desc.length()<1)\r
198         desc = token;\r
199 \r
200     }\r
201 \r
202     return new Annotation(displayChar, desc, ss, value);\r
203   }\r
204 \r
205   void colourAnnotations(AlignmentI al, String label, String colour)\r
206   {\r
207     UserColourScheme ucs = new UserColourScheme(colour);\r
208     Annotation[] annotations;\r
209     for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
210     {\r
211       if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))\r
212       {\r
213         annotations = al.getAlignmentAnnotation()[i].annotations;\r
214         for(int j=0; j<annotations.length; j++)\r
215         {\r
216           if(annotations[j]!=null)\r
217             annotations[j].colour = ucs.findColour("A");\r
218         }\r
219       }\r
220     }\r
221   }\r
222 \r
223   void combineAnnotations(AlignmentI al, StringTokenizer st)\r
224   {\r
225     int graphGroup = -1;\r
226     String group = st.nextToken();\r
227     //First make sure we are not overwriting the graphIndex\r
228     for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
229     {\r
230       if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
231       {\r
232         graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;\r
233         al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
234         break;\r
235       }\r
236     }\r
237 \r
238     //Now update groups\r
239     while(st.hasMoreTokens())\r
240     {\r
241       group = st.nextToken();\r
242       for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
243       {\r
244         if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
245         {\r
246           al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
247           break;\r
248         }\r
249       }\r
250     }\r
251   }\r
252 \r
253   void addLine(AlignmentI al, StringTokenizer st)\r
254   {\r
255     String group = st.nextToken();\r
256     AlignmentAnnotation annotation = null;\r
257 \r
258     for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
259     {\r
260       if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
261       {\r
262         annotation = al.getAlignmentAnnotation()[i];\r
263         break;\r
264       }\r
265     }\r
266 \r
267     if(annotation==null)\r
268       return;\r
269     float value = new Float(st.nextToken()).floatValue();\r
270     String label = st.hasMoreTokens() ?  st.nextToken() : null;\r
271     java.awt.Color colour = null;\r
272     if(st.hasMoreTokens())\r
273     {\r
274       UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
275       colour = ucs.findColour("A");\r
276     }\r
277 \r
278     annotation.setThreshold(new GraphLine(value, label, colour));\r
279 \r
280   }\r
281 }\r