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 SequenceI 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 text.append("\nSEQUENCE_REF\tALIGNMENT\n");
\r
63 else if (seqref == null || seqref != row.sequenceRef)
\r
65 seqref = row.sequenceRef;
\r
66 text.append("\nSEQUENCE_REF\t" + seqref.getName() + "\n");
\r
70 if( row.graph == AlignmentAnnotation.NO_GRAPH)
\r
72 text.append("NO_GRAPH\t");
\r
76 if( row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
77 text.append("BAR_GRAPH\t");
\r
78 else if(row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
79 text.append("LINE_GRAPH\t");
\r
81 if(row.getThreshold()!=null)
\r
82 graphLine.append("GRAPHLINE\t"
\r
84 + row.getThreshold().value + "\t"
\r
85 + row.getThreshold().label + "\t"
\r
86 + jalview.util.Format.getHexString(
\r
87 row.getThreshold().colour)+"\n"
\r
90 if(row.graphGroup>-1)
\r
92 String key = String.valueOf(row.graphGroup);
\r
93 if(graphGroup.containsKey(key))
\r
94 graphGroup.put(key, graphGroup.get(key)
\r
97 graphGroup.put(key, row.label);
\r
101 text.append(row.label+"\t");
\r
103 for(int j=0; j<row.annotations.length; j++)
\r
105 if (seqref != null && jalview.util.Comparison.isGap(seqref.getCharAt(j)))
\r
108 if(row.annotations[j]!=null)
\r
111 if (row.annotations[j].secondaryStructure!=' ')
\r
113 text.append(comma + row.annotations[j].secondaryStructure);
\r
116 if (row.annotations[j].displayCharacter.length() > 0
\r
117 && !row.annotations[j].displayCharacter.equals(" "))
\r
119 text.append(comma + row.annotations[j].displayCharacter);
\r
123 if (row.annotations[j]!=null)
\r
125 color = row.annotations[j].colour;
\r
126 if (row.annotations[j].value!=0f)
\r
127 text.append(comma + row.annotations[j].value);
\r
135 if(color!=null && color!=java.awt.Color.black)
\r
137 colours.append("COLOUR\t"
\r
139 +jalview.util.Format.getHexString(color)+"\n");
\r
146 text.append(colours.toString());
\r
147 text.append(graphLine.toString());
\r
148 if(graphGroup.size()>0)
\r
150 text.append("COMBINE\t");
\r
151 Enumeration en = graphGroup.elements();
\r
152 while(en.hasMoreElements())
\r
154 text.append(en.nextElement()+"\n");
\r
158 return text.toString();
\r
161 public boolean readAnnotationFile(AlignmentI al, String file)
\r
165 BufferedReader in = null;
\r
166 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
169 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
175 URL url = new URL(file);
\r
176 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
178 catch (java.net.MalformedURLException ex)
\r
180 in = new BufferedReader(new FileReader(file));
\r
184 String line, label, description, token;
\r
185 int graphStyle, index;
\r
186 SequenceI refSeq = null;
\r
187 int refSeqIndex = 1;
\r
188 int existingAnnotations = 0;
\r
189 if(al.getAlignmentAnnotation()!=null)
\r
190 existingAnnotations = al.getAlignmentAnnotation().length;
\r
192 int alWidth = al.getWidth();
\r
194 StringTokenizer st;
\r
195 Annotation[] annotations;
\r
196 AlignmentAnnotation annotation = null;
\r
198 // First confirm this is an Annotation file
\r
199 boolean jvAnnotationFile = false;
\r
200 while ( (line = in.readLine()) != null)
\r
202 if (line.indexOf("#") == 0 )
\r
205 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
207 jvAnnotationFile = true;
\r
212 if(!jvAnnotationFile)
\r
218 while ( (line = in.readLine()) != null)
\r
220 if(line.indexOf("#")==0
\r
221 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
222 || line.length()==0)
\r
225 st = new StringTokenizer(line, "\t");
\r
226 token = st.nextToken();
\r
227 if(token.equalsIgnoreCase("COLOUR"))
\r
229 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
233 if(token.equalsIgnoreCase("COMBINE") )
\r
235 combineAnnotations(al, st);
\r
239 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
246 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
248 refSeq = al.findName(st.nextToken());
\r
250 refSeqIndex = Integer.parseInt(st.nextToken());
\r
254 System.out.println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
257 catch(Exception ex)
\r
266 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
267 label = description = st.nextToken();
\r
269 line = st.nextToken();
\r
271 st = new StringTokenizer(line, "|", true);
\r
272 annotations = new Annotation[alWidth];
\r
275 boolean emptyColumn = true;
\r
278 while (st.hasMoreElements() && index<alWidth)
\r
280 token = st.nextToken().trim();
\r
281 if(token.equals("|"))
\r
286 emptyColumn = true;
\r
290 annotations[index++] = parseAnnotation(token);
\r
291 emptyColumn = false;
\r
295 annotation = new AlignmentAnnotation(label,
\r
304 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
305 refSeq.addAlignmentAnnotation(annotation);
\r
308 al.addAnnotation(annotation);
\r
310 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
313 }catch(Exception ex)
\r
315 ex.printStackTrace();
\r
316 System.out.println("Problem reading annotation file: "+ex);
\r
322 Annotation parseAnnotation(String string)
\r
324 String desc = null, displayChar="";
\r
325 char ss = ' '; // secondaryStructure
\r
327 boolean parsedValue = false;
\r
328 StringTokenizer st = new StringTokenizer(string, ",");
\r
330 while(st.hasMoreTokens())
\r
332 token = st.nextToken().trim();
\r
333 if(token.length()==0)
\r
339 displayChar = token;
\r
340 value = new Float(token).floatValue();
\r
341 parsedValue = true;
\r
343 }catch(NumberFormatException ex){}
\r
346 if(token.equals("H") || token.equals("E"))
\r
348 // Either this character represents a helix or sheet
\r
349 // or an integer which can be displayed
\r
350 ss = token.charAt(0);
\r
351 if(displayChar.equals(token.substring(0,1)))
\r
354 else if(desc==null)
\r
362 if(displayChar.length()>1 && desc.length()==1)
\r
364 String tmp = displayChar;
\r
365 displayChar = desc;
\r
369 return new Annotation(displayChar, desc, ss, value);
\r
372 void colourAnnotations(AlignmentI al, String label, String colour)
\r
374 UserColourScheme ucs = new UserColourScheme(colour);
\r
375 Annotation[] annotations;
\r
376 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
378 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
380 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
381 for(int j=0; j<annotations.length; j++)
\r
383 if(annotations[j]!=null)
\r
384 annotations[j].colour = ucs.findColour('A');
\r
390 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
392 int graphGroup = -1;
\r
393 String group = st.nextToken();
\r
394 //First make sure we are not overwriting the graphIndex
\r
395 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
397 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
399 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
400 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
405 //Now update groups
\r
406 while(st.hasMoreTokens())
\r
408 group = st.nextToken();
\r
409 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
411 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
413 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
420 void addLine(AlignmentI al, StringTokenizer st)
\r
422 String group = st.nextToken();
\r
423 AlignmentAnnotation annotation = null;
\r
425 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
427 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
429 annotation = al.getAlignmentAnnotation()[i];
\r
434 if(annotation==null)
\r
436 float value = new Float(st.nextToken()).floatValue();
\r
437 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
438 java.awt.Color colour = null;
\r
439 if(st.hasMoreTokens())
\r
441 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
442 colour = ucs.findColour('A');
\r
445 annotation.setThreshold(new GraphLine(value, label, colour));
\r