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
16 BufferedReader in = null;
\r
17 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
20 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
26 URL url = new URL(file);
\r
27 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
29 catch (java.net.MalformedURLException ex)
\r
31 in = new BufferedReader(new FileReader(file));
\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
43 int alWidth = al.getWidth();
\r
46 Annotation[] annotations;
\r
47 AlignmentAnnotation annotation = null;
\r
49 // First confirm this is an Annotation file
\r
50 boolean jvAnnotationFile = false;
\r
51 while ( (line = in.readLine()) != null)
\r
53 if (line.indexOf("#") == 0 )
\r
56 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
58 jvAnnotationFile = true;
\r
63 if(!jvAnnotationFile)
\r
69 while ( (line = in.readLine()) != null)
\r
71 if(line.indexOf("#")==0
\r
72 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
73 || line.length()==0)
\r
76 st = new StringTokenizer(line, "\t");
\r
77 token = st.nextToken();
\r
78 if(token.equalsIgnoreCase("COLOUR"))
\r
80 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
84 if(token.equalsIgnoreCase("COMBINE") )
\r
86 combineAnnotations(al, st);
\r
90 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
97 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
99 refSeq = al.findName(st.nextToken());
\r
101 refSeqIndex = Integer.parseInt(st.nextToken());
\r
103 catch(Exception ex)
\r
112 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
113 label = description = st.nextToken();
\r
115 line = st.nextToken();
\r
117 st = new StringTokenizer(line, "|", true);
\r
118 annotations = new Annotation[alWidth];
\r
121 boolean emptyColumn = true;
\r
124 while (st.hasMoreElements() && index<alWidth)
\r
126 token = st.nextToken().trim();
\r
127 if(token.equals("|"))
\r
132 emptyColumn = true;
\r
136 annotations[index++] = parseAnnotation(token);
\r
137 emptyColumn = false;
\r
141 annotation = new AlignmentAnnotation(label,
\r
150 annotation.createSequenceMapping(refSeq, refSeqIndex);
\r
151 refSeq.addAlignmentAnnotation(annotation);
\r
154 al.addAnnotation(annotation);
\r
156 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
159 }catch(Exception ex)
\r
161 ex.printStackTrace();
\r
162 System.out.println("Problem reading annotation file: "+ex);
\r
168 Annotation parseAnnotation(String string)
\r
170 String desc = "", displayChar="";
\r
171 char ss = ' '; // secondaryStructure
\r
173 boolean parsedValue = false;
\r
174 StringTokenizer st = new StringTokenizer(string, ",");
\r
176 while(st.hasMoreTokens())
\r
178 token = st.nextToken().trim();
\r
179 if(token.length()==0)
\r
185 value = new Float(token).floatValue();
\r
186 displayChar = token;
\r
187 parsedValue = true;
\r
188 }catch(NumberFormatException ex){}
\r
191 if(token.equals("H") || token.equals("E"))
\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
197 else if(desc.length()<1)
\r
202 return new Annotation(displayChar, desc, ss, value);
\r
205 void colourAnnotations(AlignmentI al, String label, String colour)
\r
207 UserColourScheme ucs = new UserColourScheme(colour);
\r
208 Annotation[] annotations;
\r
209 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
211 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
213 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
214 for(int j=0; j<annotations.length; j++)
\r
216 if(annotations[j]!=null)
\r
217 annotations[j].colour = ucs.findColour("A");
\r
223 void combineAnnotations(AlignmentI al, StringTokenizer st)
\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
230 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
232 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
233 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
238 //Now update groups
\r
239 while(st.hasMoreTokens())
\r
241 group = st.nextToken();
\r
242 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
244 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
246 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
253 void addLine(AlignmentI al, StringTokenizer st)
\r
255 String group = st.nextToken();
\r
256 AlignmentAnnotation annotation = null;
\r
258 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
260 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
262 annotation = al.getAlignmentAnnotation()[i];
\r
267 if(annotation==null)
\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
274 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
275 colour = ucs.findColour("A");
\r
278 annotation.setThreshold(new GraphLine(value, label, colour));
\r