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
66 if( row.graph == AlignmentAnnotation.NO_GRAPH)
\r
68 text.append("NO_GRAPH\t");
\r
72 if( row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
73 text.append("BAR_GRAPH\t");
\r
74 else if(row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
75 text.append("LINE_GRAPH\t");
\r
77 if(row.getThreshold()!=null)
\r
78 graphLine.append("GRAPHLINE\t"
\r
80 + row.getThreshold().value + "\t"
\r
81 + row.getThreshold().label + "\t"
\r
82 + jalview.util.Format.getHexString(
\r
83 row.getThreshold().colour)+"\n"
\r
86 if(row.graphGroup>-1)
\r
88 String key = String.valueOf(row.graphGroup);
\r
89 if(graphGroup.containsKey(key))
\r
90 graphGroup.put(key, graphGroup.get(key)
\r
93 graphGroup.put(key, row.label);
\r
97 text.append(row.label+"\t");
\r
99 for(int j=0; j<row.annotations.length; j++)
\r
101 if(row.annotations[j]!=null)
\r
104 if (row.annotations[j].displayCharacter.length() > 0
\r
105 && !row.annotations[j].displayCharacter.equals(" "))
\r
107 text.append(row.annotations[j].displayCharacter);
\r
110 if (row.annotations[j].secondaryStructure!=' ')
\r
112 text.append(comma + row.annotations[j].secondaryStructure);
\r
115 if (row.annotations[j].value!=0f)
\r
117 color = row.annotations[j].colour;
\r
118 text.append(comma + row.annotations[j].value);
\r
126 if(color!=null && color!=java.awt.Color.black)
\r
128 colours.append("COLOUR\t"
\r
130 +jalview.util.Format.getHexString(color)+"\n");
\r
137 text.append(colours.toString());
\r
138 text.append(graphLine.toString());
\r
139 if(graphGroup.size()>0)
\r
141 text.append("COMBINE\t");
\r
142 Enumeration en = graphGroup.elements();
\r
143 while(en.hasMoreElements())
\r
145 text.append(en.nextElement()+"\n");
\r
149 return text.toString();
\r
152 public boolean readAnnotationFile(AlignmentI al, String file)
\r
156 BufferedReader in = null;
\r
157 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
160 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
166 URL url = new URL(file);
\r
167 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
169 catch (java.net.MalformedURLException ex)
\r
171 in = new BufferedReader(new FileReader(file));
\r
175 String line, label, description, token;
\r
176 int graphStyle, index;
\r
177 SequenceI refSeq = null;
\r
178 int refSeqIndex = 1;
\r
179 int existingAnnotations = 0;
\r
180 if(al.getAlignmentAnnotation()!=null)
\r
181 existingAnnotations = al.getAlignmentAnnotation().length;
\r
183 int alWidth = al.getWidth();
\r
185 StringTokenizer st;
\r
186 Annotation[] annotations;
\r
187 AlignmentAnnotation annotation = null;
\r
189 // First confirm this is an Annotation file
\r
190 boolean jvAnnotationFile = false;
\r
191 while ( (line = in.readLine()) != null)
\r
193 if (line.indexOf("#") == 0 )
\r
196 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
198 jvAnnotationFile = true;
\r
203 if(!jvAnnotationFile)
\r
209 while ( (line = in.readLine()) != null)
\r
211 if(line.indexOf("#")==0
\r
212 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
213 || line.length()==0)
\r
216 st = new StringTokenizer(line, "\t");
\r
217 token = st.nextToken();
\r
218 if(token.equalsIgnoreCase("COLOUR"))
\r
220 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
224 if(token.equalsIgnoreCase("COMBINE") )
\r
226 combineAnnotations(al, st);
\r
230 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
237 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
239 refSeq = al.findName(st.nextToken());
\r
241 refSeqIndex = Integer.parseInt(st.nextToken());
\r
243 catch(Exception ex)
\r
252 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
253 label = description = st.nextToken();
\r
255 line = st.nextToken();
\r
257 st = new StringTokenizer(line, "|", true);
\r
258 annotations = new Annotation[alWidth];
\r
261 boolean emptyColumn = true;
\r
264 while (st.hasMoreElements() && index<alWidth)
\r
266 token = st.nextToken().trim();
\r
267 if(token.equals("|"))
\r
272 emptyColumn = true;
\r
276 annotations[index++] = parseAnnotation(token);
\r
277 emptyColumn = false;
\r
281 annotation = new AlignmentAnnotation(label,
\r
290 annotation.createSequenceMapping(refSeq, refSeqIndex);
\r
291 refSeq.addAlignmentAnnotation(annotation);
\r
294 al.addAnnotation(annotation);
\r
296 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
299 }catch(Exception ex)
\r
301 ex.printStackTrace();
\r
302 System.out.println("Problem reading annotation file: "+ex);
\r
308 Annotation parseAnnotation(String string)
\r
310 String desc = "", displayChar="";
\r
311 char ss = ' '; // secondaryStructure
\r
313 boolean parsedValue = false;
\r
314 StringTokenizer st = new StringTokenizer(string, ",");
\r
316 while(st.hasMoreTokens())
\r
318 token = st.nextToken().trim();
\r
319 if(token.length()==0)
\r
325 displayChar = token;
\r
326 value = new Float(token).floatValue();
\r
327 parsedValue = true;
\r
328 }catch(NumberFormatException ex){}
\r
331 if(token.equals("H") || token.equals("E"))
\r
333 // Either this character represents a helix or sheet
\r
334 // or an integer which can be displayed
\r
335 ss = token.charAt(0);
\r
336 if(displayChar.equals(token.substring(0,1)))
\r
339 else if(desc.length()<1)
\r
344 return new Annotation(displayChar, desc, ss, value);
\r
347 void colourAnnotations(AlignmentI al, String label, String colour)
\r
349 UserColourScheme ucs = new UserColourScheme(colour);
\r
350 Annotation[] annotations;
\r
351 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
353 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
355 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
356 for(int j=0; j<annotations.length; j++)
\r
358 if(annotations[j]!=null)
\r
359 annotations[j].colour = ucs.findColour("A");
\r
365 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
367 int graphGroup = -1;
\r
368 String group = st.nextToken();
\r
369 //First make sure we are not overwriting the graphIndex
\r
370 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
372 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
374 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
375 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
380 //Now update groups
\r
381 while(st.hasMoreTokens())
\r
383 group = st.nextToken();
\r
384 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
386 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
388 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
395 void addLine(AlignmentI al, StringTokenizer st)
\r
397 String group = st.nextToken();
\r
398 AlignmentAnnotation annotation = null;
\r
400 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
402 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
404 annotation = al.getAlignmentAnnotation()[i];
\r
409 if(annotation==null)
\r
411 float value = new Float(st.nextToken()).floatValue();
\r
412 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
413 java.awt.Color colour = null;
\r
414 if(st.hasMoreTokens())
\r
416 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
417 colour = ucs.findColour("A");
\r
420 annotation.setThreshold(new GraphLine(value, label, colour));
\r