4 import jalview.datamodel.*;
\r
5 import java.util.StringTokenizer;
\r
6 import jalview.schemes.UserColourScheme;
\r
7 import jalview.gui.FeatureRenderer;
\r
9 public class AnnotationReader
\r
11 public boolean readGroupsFile(FeatureRenderer fr, AlignmentI al, String file)
\r
15 BufferedReader in = new BufferedReader(new FileReader(file));
\r
16 SequenceI seq = null;
\r
17 String line, type, desc, token;
\r
19 int index, start, end;
\r
23 while ( (line = in.readLine()) != null)
\r
26 st = new StringTokenizer(line, "\t");
\r
27 if (st.countTokens() == 2)
\r
29 type = st.nextToken();
\r
30 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
31 fr.setColour(type, ucs.findColour("A"));
\r
35 while (st.hasMoreElements())
\r
37 desc = st.nextToken();
\r
38 token = st.nextToken();
\r
39 if (!token.equals("ID_NOT_SPECIFIED"))
\r
41 index = al.findIndex(al.findName(token));
\r
46 index = Integer.parseInt(st.nextToken());
\r
49 start = Integer.parseInt(st.nextToken());
\r
50 end = Integer.parseInt(st.nextToken());
\r
52 seq = al.getSequenceAt(index);
\r
53 start = seq.findIndex(start) - 1;
\r
54 end = seq.findIndex(end) - 1;
\r
56 type = st.nextToken();
\r
58 if (fr.getColour(type) == null)
\r
60 // Probably the old style groups file
\r
61 UserColourScheme ucs = new UserColourScheme(type);
\r
62 fr.setColour(type, ucs.findColour("A"));
\r
65 sf = new SequenceFeature(type, desc, "", start, end);
\r
67 seq.getDatasetSequence().addSequenceFeature(sf);
\r
71 catch (Exception ex)
\r
73 System.out.println("Error parsing groups file: " + ex);
\r
80 public boolean readAnnotationFile(AlignmentI al, String file)
\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
92 Annotation[] annotations;
\r
93 AlignmentAnnotation annotation = null;
\r
95 // First confirm this is an Annotation file
\r
96 boolean jvAnnotationFile = false;
\r
97 while ( (line = in.readLine()) != null)
\r
99 if (line.indexOf("#") == 0 )
\r
102 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
104 jvAnnotationFile = true;
\r
109 if(!jvAnnotationFile)
\r
115 while ( (line = in.readLine()) != null)
\r
117 if(line.indexOf("#")==0
\r
118 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
119 || line.length()==0)
\r
122 st = new StringTokenizer(line, "\t");
\r
123 token = st.nextToken();
\r
124 if(token.equalsIgnoreCase("COLOUR"))
\r
126 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
130 if(token.equalsIgnoreCase("COMBINE") )
\r
132 combineAnnotations(al, st);
\r
136 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
143 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
145 refSeq = al.findName(st.nextToken());
\r
147 refSeqIndex = Integer.parseInt(st.nextToken());
\r
149 catch(NumberFormatException ex)
\r
157 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
158 label = description = st.nextToken();
\r
160 line = st.nextToken();
\r
161 st = new StringTokenizer(line, "|", true);
\r
162 annotations = new Annotation[st.countTokens()];
\r
164 boolean emptyColumn = true;
\r
165 while (st.hasMoreElements())
\r
167 token = st.nextToken().trim();
\r
169 if(token.equals("|"))
\r
172 annotations[index++] = parseAnnotation("");
\r
174 emptyColumn = true;
\r
178 annotations[index++] = parseAnnotation(token);
\r
179 emptyColumn = false;
\r
183 annotation = new AlignmentAnnotation(label,
\r
190 al.addAnnotation(annotation, refSeq, refSeqIndex);
\r
193 }catch(Exception ex)
\r
194 {ex.printStackTrace();
\r
195 // System.out.println("Problem reading annotation file: "+ex);
\r
201 Annotation parseAnnotation(String string)
\r
203 String desc = "", displayChar="";
\r
204 char ss = ' '; // secondaryStructure
\r
206 StringTokenizer st = new StringTokenizer(string, ",");
\r
208 while(st.hasMoreTokens())
\r
210 token = st.nextToken().trim();
\r
211 if(token.length()==0)
\r
216 value = Float.parseFloat(token);
\r
217 }catch(NumberFormatException ex){}
\r
220 if(token.length()==1)
\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
226 ss = token.charAt(0);
\r
228 else //if(value!=0)
\r
230 displayChar = token;
\r
233 else if(desc.length()<1)
\r
236 displayChar = token;
\r
239 return new Annotation(displayChar, desc, ss, value);
\r
242 void colourAnnotations(AlignmentI al, String label, String colour)
\r
244 UserColourScheme ucs = new UserColourScheme(colour);
\r
245 Annotation[] annotations;
\r
246 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
248 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
250 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
251 for(int j=0; j<annotations.length; j++)
\r
253 if(annotations[j]!=null)
\r
254 annotations[j].colour = ucs.findColour("A");
\r
260 void combineAnnotations(AlignmentI al, StringTokenizer st)
\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
267 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
269 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
270 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
275 //Now update groups
\r
276 while(st.hasMoreTokens())
\r
278 group = st.nextToken();
\r
279 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
281 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
283 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
290 void addLine(AlignmentI al, StringTokenizer st)
\r
292 String group = st.nextToken();
\r
293 AlignmentAnnotation annotation = null;
\r
295 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
297 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
299 annotation = al.getAlignmentAnnotation()[i];
\r
304 if(annotation==null)
\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
312 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
313 colour = ucs.findColour("A");
\r
316 annotation.addGraphLine(new GraphLine(value, label, colour));
\r