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 = 0;
\r
39 int existingAnnotations = 0;
\r
40 if(al.getAlignmentAnnotation()!=null)
\r
41 existingAnnotations = al.getAlignmentAnnotation().length;
\r
44 Annotation[] annotations;
\r
45 AlignmentAnnotation annotation = null;
\r
47 // First confirm this is an Annotation file
\r
48 boolean jvAnnotationFile = false;
\r
49 while ( (line = in.readLine()) != null)
\r
51 if (line.indexOf("#") == 0 )
\r
54 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
56 jvAnnotationFile = true;
\r
61 if(!jvAnnotationFile)
\r
67 while ( (line = in.readLine()) != null)
\r
69 if(line.indexOf("#")==0
\r
70 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
71 || line.length()==0)
\r
74 st = new StringTokenizer(line, "\t");
\r
75 token = st.nextToken();
\r
76 if(token.equalsIgnoreCase("COLOUR"))
\r
78 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
82 if(token.equalsIgnoreCase("COMBINE") )
\r
84 combineAnnotations(al, st);
\r
88 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
95 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
97 refSeq = al.findName(st.nextToken());
\r
99 refSeqIndex = Integer.parseInt(st.nextToken());
\r
101 catch(Exception ex)
\r
109 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
110 label = description = st.nextToken();
\r
112 line = st.nextToken();
\r
113 st = new StringTokenizer(line, "|", true);
\r
114 annotations = new Annotation[st.countTokens()+refSeqIndex];
\r
115 index = refSeqIndex;
\r
116 boolean emptyColumn = true;
\r
117 while (st.hasMoreElements())
\r
119 token = st.nextToken().trim();
\r
121 if(token.equals("|"))
\r
124 annotations[index++] = parseAnnotation("");
\r
126 emptyColumn = true;
\r
130 annotations[index++] = parseAnnotation(token);
\r
131 emptyColumn = false;
\r
135 annotation = new AlignmentAnnotation(label,
\r
142 annotation = al.addAnnotation(annotation, refSeq);
\r
143 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
146 al.adjustSequenceAnnotations();
\r
149 }catch(Exception ex)
\r
151 ex.printStackTrace();
\r
152 System.out.println("Problem reading annotation file: "+ex);
\r
158 Annotation parseAnnotation(String string)
\r
160 String desc = "", displayChar="";
\r
161 char ss = ' '; // secondaryStructure
\r
163 StringTokenizer st = new StringTokenizer(string, ",");
\r
165 while(st.hasMoreTokens())
\r
167 token = st.nextToken().trim();
\r
168 if(token.length()==0)
\r
173 value = new Float(token).floatValue();
\r
174 }catch(NumberFormatException ex){}
\r
177 if(token.length()==1)
\r
179 // Either this character represents a helix or sheet
\r
180 // or an integer which can be displayed
\r
181 if(token.equals("H") || token.equals("E"))
\r
183 ss = token.charAt(0);
\r
185 else //if(value!=0)
\r
187 displayChar = token;
\r
190 else if(desc.length()<1)
\r
193 displayChar = token;
\r
196 return new Annotation(displayChar, desc, ss, value);
\r
199 void colourAnnotations(AlignmentI al, String label, String colour)
\r
201 UserColourScheme ucs = new UserColourScheme(colour);
\r
202 Annotation[] annotations;
\r
203 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
205 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
207 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
208 for(int j=0; j<annotations.length; j++)
\r
210 if(annotations[j]!=null)
\r
211 annotations[j].colour = ucs.findColour("A");
\r
217 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
219 int graphGroup = -1;
\r
220 String group = st.nextToken();
\r
221 //First make sure we are not overwriting the graphIndex
\r
222 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
224 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
226 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
227 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
232 //Now update groups
\r
233 while(st.hasMoreTokens())
\r
235 group = st.nextToken();
\r
236 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
238 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
240 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
247 void addLine(AlignmentI al, StringTokenizer st)
\r
249 String group = st.nextToken();
\r
250 AlignmentAnnotation annotation = null;
\r
252 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
254 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
256 annotation = al.getAlignmentAnnotation()[i];
\r
261 if(annotation==null)
\r
263 float value = new Float(st.nextToken()).floatValue();
\r
264 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
265 java.awt.Color colour = null;
\r
266 if(st.hasMoreTokens())
\r
268 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
269 colour = ucs.findColour("A");
\r
272 annotation.setThreshold(new GraphLine(value, label, colour));
\r