2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
23 import jalview.datamodel.*;
\r
25 import jalview.schemes.UserColourScheme;
\r
26 import java.net.URL;
\r
29 public class AnnotationFile
\r
32 public String printAnnotations(AlignmentAnnotation [] annotations)
\r
34 StringBuffer text = new StringBuffer(
\r
35 "JALVIEW_ANNOTATION\n"
\r
37 +new java.util.Date()+"\n\n");
\r
39 AlignmentAnnotation row;
\r
41 String seqref = null;
\r
43 StringBuffer colours = new StringBuffer();
\r
44 StringBuffer graphLine = new StringBuffer();
\r
46 Hashtable graphGroup = new Hashtable();
\r
48 java.awt.Color color;
\r
50 for(int i=0; i<annotations.length; i++)
\r
52 row = annotations[i];
\r
55 if( row.sequenceRef == null)
\r
58 else if (seqref == null
\r
59 || !seqref.equals(row.sequenceRef.getName()))
\r
61 seqref = row.sequenceRef.getName();
\r
62 text.append("\nSEQUENCE_REF\t" + seqref + "\n");
\r
68 case AlignmentAnnotation.NO_GRAPH:
\r
69 text.append("NO_GRAPH\t");
\r
71 case AlignmentAnnotation.BAR_GRAPH:
\r
72 text.append("BAR_GRAPH\t");
\r
73 case AlignmentAnnotation.LINE_GRAPH:
\r
74 text.append("LINE_GRAPH\t");
\r
76 if(row.getThreshold()!=null)
\r
77 graphLine.append("GRAPHLINE\t"
\r
79 + row.getThreshold().value + "\t"
\r
80 + row.getThreshold().label + "\t"
\r
81 + jalview.util.Format.getHexString(
\r
82 row.getThreshold().colour)+"\n"
\r
85 if(row.graphGroup>-1)
\r
87 String key = String.valueOf(row.graphGroup);
\r
88 if(graphGroup.containsKey(key))
\r
89 graphGroup.put(key, graphGroup.get(key)
\r
92 graphGroup.put(key, row.label);
\r
96 text.append(row.label+"\t");
\r
98 for(int j=0; j<row.annotations.length; j++)
\r
100 if(row.annotations[j]!=null)
\r
103 if (row.annotations[j].displayCharacter.length() > 0
\r
104 && !row.annotations[j].displayCharacter.equals(" "))
\r
106 text.append(row.annotations[j].displayCharacter);
\r
109 if (row.annotations[j].secondaryStructure!=' ')
\r
111 text.append(comma + row.annotations[j].secondaryStructure);
\r
114 if (row.annotations[j].value!=0f)
\r
116 color = row.annotations[j].colour;
\r
117 text.append(comma + row.annotations[j].value);
\r
125 if(color!=null && color!=java.awt.Color.black)
\r
127 colours.append("COLOUR\t"
\r
129 +jalview.util.Format.getHexString(color)+"\n");
\r
136 text.append(colours.toString());
\r
137 text.append(graphLine.toString());
\r
138 if(graphGroup.size()>0)
\r
140 text.append("COMBINE\t");
\r
141 Enumeration en = graphGroup.elements();
\r
142 while(en.hasMoreElements())
\r
144 text.append(en.nextElement()+"\n");
\r
148 return text.toString();
\r
151 public boolean readAnnotationFile(AlignmentI al, String file)
\r
155 BufferedReader in = null;
\r
156 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
159 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
165 URL url = new URL(file);
\r
166 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
168 catch (java.net.MalformedURLException ex)
\r
170 in = new BufferedReader(new FileReader(file));
\r
174 String line, label, description, token;
\r
175 int graphStyle, index;
\r
176 SequenceI refSeq = null;
\r
177 int refSeqIndex = 1;
\r
178 int existingAnnotations = 0;
\r
179 if(al.getAlignmentAnnotation()!=null)
\r
180 existingAnnotations = al.getAlignmentAnnotation().length;
\r
182 int alWidth = al.getWidth();
\r
184 StringTokenizer st;
\r
185 Annotation[] annotations;
\r
186 AlignmentAnnotation annotation = null;
\r
188 // First confirm this is an Annotation file
\r
189 boolean jvAnnotationFile = false;
\r
190 while ( (line = in.readLine()) != null)
\r
192 if (line.indexOf("#") == 0 )
\r
195 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
197 jvAnnotationFile = true;
\r
202 if(!jvAnnotationFile)
\r
208 while ( (line = in.readLine()) != null)
\r
210 if(line.indexOf("#")==0
\r
211 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
212 || line.length()==0)
\r
215 st = new StringTokenizer(line, "\t");
\r
216 token = st.nextToken();
\r
217 if(token.equalsIgnoreCase("COLOUR"))
\r
219 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
223 if(token.equalsIgnoreCase("COMBINE") )
\r
225 combineAnnotations(al, st);
\r
229 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
236 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
238 refSeq = al.findName(st.nextToken());
\r
240 refSeqIndex = Integer.parseInt(st.nextToken());
\r
242 catch(Exception ex)
\r
251 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
252 label = description = st.nextToken();
\r
254 line = st.nextToken();
\r
256 st = new StringTokenizer(line, "|", true);
\r
257 annotations = new Annotation[alWidth];
\r
260 boolean emptyColumn = true;
\r
263 while (st.hasMoreElements() && index<alWidth)
\r
265 token = st.nextToken().trim();
\r
266 if(token.equals("|"))
\r
271 emptyColumn = true;
\r
275 annotations[index++] = parseAnnotation(token);
\r
276 emptyColumn = false;
\r
280 annotation = new AlignmentAnnotation(label,
\r
289 annotation.createSequenceMapping(refSeq, refSeqIndex);
\r
290 refSeq.addAlignmentAnnotation(annotation);
\r
293 al.addAnnotation(annotation);
\r
295 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
298 }catch(Exception ex)
\r
300 ex.printStackTrace();
\r
301 System.out.println("Problem reading annotation file: "+ex);
\r
307 Annotation parseAnnotation(String string)
\r
309 String desc = "", displayChar="";
\r
310 char ss = ' '; // secondaryStructure
\r
312 boolean parsedValue = false;
\r
313 StringTokenizer st = new StringTokenizer(string, ",");
\r
315 while(st.hasMoreTokens())
\r
317 token = st.nextToken().trim();
\r
318 if(token.length()==0)
\r
324 value = new Float(token).floatValue();
\r
325 displayChar = token;
\r
326 parsedValue = true;
\r
327 }catch(NumberFormatException ex){}
\r
330 if(token.equals("H") || token.equals("E"))
\r
332 // Either this character represents a helix or sheet
\r
333 // or an integer which can be displayed
\r
334 ss = token.charAt(0);
\r
336 else if(desc.length()<1)
\r
341 return new Annotation(displayChar, desc, ss, value);
\r
344 void colourAnnotations(AlignmentI al, String label, String colour)
\r
346 UserColourScheme ucs = new UserColourScheme(colour);
\r
347 Annotation[] annotations;
\r
348 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
350 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
352 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
353 for(int j=0; j<annotations.length; j++)
\r
355 if(annotations[j]!=null)
\r
356 annotations[j].colour = ucs.findColour("A");
\r
362 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
364 int graphGroup = -1;
\r
365 String group = st.nextToken();
\r
366 //First make sure we are not overwriting the graphIndex
\r
367 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
369 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
371 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
372 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
377 //Now update groups
\r
378 while(st.hasMoreTokens())
\r
380 group = st.nextToken();
\r
381 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
383 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
385 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
392 void addLine(AlignmentI al, StringTokenizer st)
\r
394 String group = st.nextToken();
\r
395 AlignmentAnnotation annotation = null;
\r
397 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
399 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
401 annotation = al.getAlignmentAnnotation()[i];
\r
406 if(annotation==null)
\r
408 float value = new Float(st.nextToken()).floatValue();
\r
409 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
410 java.awt.Color colour = null;
\r
411 if(st.hasMoreTokens())
\r
413 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
414 colour = ucs.findColour("A");
\r
417 annotation.setThreshold(new GraphLine(value, label, colour));
\r