4 import jalview.datamodel.*;
\r
5 import java.util.StringTokenizer;
\r
6 import jalview.schemes.UserColourScheme;
\r
9 public class AnnotationReader
\r
11 public boolean readAnnotationFile(AlignmentI al, String file)
\r
17 BufferedReader in = null;
\r
19 URL url = new URL(file);
\r
20 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
22 catch(java.net.MalformedURLException ex)
\r
24 in = new BufferedReader(new FileReader(file));
\r
28 String line, label, description, token;
\r
29 int graphStyle, index;
\r
30 SequenceI refSeq = null;
\r
31 int refSeqIndex = 0;
\r
32 int existingAnnotations = 0;
\r
33 if(al.getAlignmentAnnotation()!=null)
\r
34 existingAnnotations = al.getAlignmentAnnotation().length;
\r
37 Annotation[] annotations;
\r
38 AlignmentAnnotation annotation = null;
\r
40 // First confirm this is an Annotation file
\r
41 boolean jvAnnotationFile = false;
\r
42 while ( (line = in.readLine()) != null)
\r
44 if (line.indexOf("#") == 0 )
\r
47 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
49 jvAnnotationFile = true;
\r
54 if(!jvAnnotationFile)
\r
60 while ( (line = in.readLine()) != null)
\r
62 if(line.indexOf("#")==0
\r
63 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
64 || line.length()==0)
\r
67 st = new StringTokenizer(line, "\t");
\r
68 token = st.nextToken();
\r
69 if(token.equalsIgnoreCase("COLOUR"))
\r
71 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
75 if(token.equalsIgnoreCase("COMBINE") )
\r
77 combineAnnotations(al, st);
\r
81 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
88 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
90 refSeq = al.findName(st.nextToken());
\r
92 refSeqIndex = Integer.parseInt(st.nextToken());
\r
102 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
103 label = description = st.nextToken();
\r
105 line = st.nextToken();
\r
106 st = new StringTokenizer(line, "|", true);
\r
107 annotations = new Annotation[st.countTokens()+refSeqIndex];
\r
108 index = refSeqIndex;
\r
109 boolean emptyColumn = true;
\r
110 while (st.hasMoreElements())
\r
112 token = st.nextToken().trim();
\r
114 if(token.equals("|"))
\r
117 annotations[index++] = parseAnnotation("");
\r
119 emptyColumn = true;
\r
123 annotations[index++] = parseAnnotation(token);
\r
124 emptyColumn = false;
\r
128 annotation = new AlignmentAnnotation(label,
\r
135 annotation = al.addAnnotation(annotation, refSeq);
\r
136 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
139 al.adjustSequenceAnnotations();
\r
142 }catch(Exception ex)
\r
144 ex.printStackTrace();
\r
145 System.out.println("Problem reading annotation file: "+ex);
\r
151 Annotation parseAnnotation(String string)
\r
153 String desc = "", displayChar="";
\r
154 char ss = ' '; // secondaryStructure
\r
156 StringTokenizer st = new StringTokenizer(string, ",");
\r
158 while(st.hasMoreTokens())
\r
160 token = st.nextToken().trim();
\r
161 if(token.length()==0)
\r
166 value = new Float(token).floatValue();
\r
167 }catch(NumberFormatException ex){}
\r
170 if(token.length()==1)
\r
172 // Either this character represents a helix or sheet
\r
173 // or an integer which can be displayed
\r
174 if(token.equals("H") || token.equals("E"))
\r
176 ss = token.charAt(0);
\r
178 else //if(value!=0)
\r
180 displayChar = token;
\r
183 else if(desc.length()<1)
\r
186 displayChar = token;
\r
189 return new Annotation(displayChar, desc, ss, value);
\r
192 void colourAnnotations(AlignmentI al, String label, String colour)
\r
194 UserColourScheme ucs = new UserColourScheme(colour);
\r
195 Annotation[] annotations;
\r
196 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
198 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
200 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
201 for(int j=0; j<annotations.length; j++)
\r
203 if(annotations[j]!=null)
\r
204 annotations[j].colour = ucs.findColour("A");
\r
210 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
212 int graphGroup = -1;
\r
213 String group = st.nextToken();
\r
214 //First make sure we are not overwriting the graphIndex
\r
215 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
217 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
219 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
220 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
225 //Now update groups
\r
226 while(st.hasMoreTokens())
\r
228 group = st.nextToken();
\r
229 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
231 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
233 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
240 void addLine(AlignmentI al, StringTokenizer st)
\r
242 String group = st.nextToken();
\r
243 AlignmentAnnotation annotation = null;
\r
245 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
247 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
249 annotation = al.getAlignmentAnnotation()[i];
\r
254 if(annotation==null)
\r
256 float value = new Float(st.nextToken()).floatValue();
\r
257 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
258 java.awt.Color colour = null;
\r
259 if(st.hasMoreTokens())
\r
261 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
262 colour = ucs.findColour("A");
\r
265 annotation.setThreshold(new GraphLine(value, label, colour));
\r