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
59 if( row.sequenceRef == null)
\r
62 text.append("\nSEQUENCE_REF\tALIGNMENT\n");
\r
67 else if (seqref == null || seqref != row.sequenceRef)
\r
69 seqref = row.sequenceRef;
\r
70 text.append("\nSEQUENCE_REF\t" + seqref.getName() + "\n");
\r
74 if( row.graph == AlignmentAnnotation.NO_GRAPH)
\r
76 text.append("NO_GRAPH\t");
\r
80 if( row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
81 text.append("BAR_GRAPH\t");
\r
82 else if(row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
83 text.append("LINE_GRAPH\t");
\r
85 if(row.getThreshold()!=null)
\r
86 graphLine.append("GRAPHLINE\t"
\r
88 + row.getThreshold().value + "\t"
\r
89 + row.getThreshold().label + "\t"
\r
90 + jalview.util.Format.getHexString(
\r
91 row.getThreshold().colour)+"\n"
\r
94 if(row.graphGroup>-1)
\r
96 String key = String.valueOf(row.graphGroup);
\r
97 if(graphGroup.containsKey(key))
\r
98 graphGroup.put(key, graphGroup.get(key)
\r
101 graphGroup.put(key, row.label);
\r
105 text.append(row.label+"\t");
\r
106 if(row.description!=null)
\r
107 text.append(row.description+"\t");
\r
109 for(int j=0; j<row.annotations.length; j++)
\r
111 if (seqref != null && jalview.util.Comparison.isGap(seqref.getCharAt(j)))
\r
114 if(row.annotations[j]!=null)
\r
117 if (row.annotations[j].secondaryStructure!=' ')
\r
119 text.append(comma + row.annotations[j].secondaryStructure);
\r
122 if (row.annotations[j].displayCharacter.length() > 0
\r
123 && !row.annotations[j].displayCharacter.equals(" "))
\r
125 text.append(comma + row.annotations[j].displayCharacter);
\r
129 if (row.annotations[j]!=null)
\r
131 color = row.annotations[j].colour;
\r
132 if (row.annotations[j].value!=0f)
\r
133 text.append(comma + row.annotations[j].value);
\r
141 if(color!=null && color!=java.awt.Color.black)
\r
143 colours.append("COLOUR\t"
\r
145 +jalview.util.Format.getHexString(color)+"\n");
\r
152 text.append(colours.toString());
\r
153 text.append(graphLine.toString());
\r
154 if(graphGroup.size()>0)
\r
156 text.append("COMBINE\t");
\r
157 Enumeration en = graphGroup.elements();
\r
158 while(en.hasMoreElements())
\r
160 text.append(en.nextElement()+"\n");
\r
164 return text.toString();
\r
167 public boolean readAnnotationFile(AlignmentI al, String file)
\r
171 BufferedReader in = null;
\r
172 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
175 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
181 URL url = new URL(file);
\r
182 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
184 catch (java.net.MalformedURLException ex)
\r
186 in = new BufferedReader(new FileReader(file));
\r
190 String line, label, description, token;
\r
191 int graphStyle, index;
\r
192 SequenceI refSeq = null;
\r
193 int refSeqIndex = 1;
\r
194 int existingAnnotations = 0;
\r
195 if(al.getAlignmentAnnotation()!=null)
\r
196 existingAnnotations = al.getAlignmentAnnotation().length;
\r
198 int alWidth = al.getWidth();
\r
200 StringTokenizer st;
\r
201 Annotation[] annotations;
\r
202 AlignmentAnnotation annotation = null;
\r
204 // First confirm this is an Annotation file
\r
205 boolean jvAnnotationFile = false;
\r
206 while ( (line = in.readLine()) != null)
\r
208 if (line.indexOf("#") == 0 )
\r
211 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
213 jvAnnotationFile = true;
\r
218 if(!jvAnnotationFile)
\r
224 while ( (line = in.readLine()) != null)
\r
226 if(line.indexOf("#")==0
\r
227 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
228 || line.length()==0)
\r
231 st = new StringTokenizer(line, "\t");
\r
232 token = st.nextToken();
\r
233 if(token.equalsIgnoreCase("COLOUR"))
\r
235 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
239 if(token.equalsIgnoreCase("COMBINE") )
\r
241 combineAnnotations(al, st);
\r
245 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
252 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
254 refSeq = al.findName(st.nextToken());
\r
256 refSeqIndex = Integer.parseInt(st.nextToken());
\r
260 System.out.println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
263 catch(Exception ex)
\r
272 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
273 label = st.nextToken();
\r
275 if(st.countTokens()>1)
\r
276 description = st.nextToken();
\r
278 description = null;
\r
280 line = st.nextToken();
\r
282 st = new StringTokenizer(line, "|", true);
\r
283 annotations = new Annotation[alWidth];
\r
286 boolean emptyColumn = true;
\r
289 while (st.hasMoreElements() && index<alWidth)
\r
291 token = st.nextToken().trim();
\r
292 if(token.equals("|"))
\r
297 emptyColumn = true;
\r
301 annotations[index++] = parseAnnotation(token);
\r
302 emptyColumn = false;
\r
306 annotation = new AlignmentAnnotation(label,
\r
315 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
316 refSeq.addAlignmentAnnotation(annotation);
\r
319 al.addAnnotation(annotation);
\r
321 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
324 }catch(Exception ex)
\r
326 ex.printStackTrace();
\r
327 System.out.println("Problem reading annotation file: "+ex);
\r
333 Annotation parseAnnotation(String string)
\r
335 String desc = null, displayChar="";
\r
336 char ss = ' '; // secondaryStructure
\r
338 boolean parsedValue = false;
\r
339 StringTokenizer st = new StringTokenizer(string, ",");
\r
341 while(st.hasMoreTokens())
\r
343 token = st.nextToken().trim();
\r
344 if(token.length()==0)
\r
350 displayChar = token;
\r
351 value = new Float(token).floatValue();
\r
352 parsedValue = true;
\r
354 }catch(NumberFormatException ex){}
\r
357 if(token.equals("H") || token.equals("E"))
\r
359 // Either this character represents a helix or sheet
\r
360 // or an integer which can be displayed
\r
361 ss = token.charAt(0);
\r
362 if(displayChar.equals(token.substring(0,1)))
\r
365 else if(desc==null)
\r
373 if(displayChar.length()>1 && desc.length()==1)
\r
375 String tmp = displayChar;
\r
376 displayChar = desc;
\r
380 return new Annotation(displayChar, desc, ss, value);
\r
383 void colourAnnotations(AlignmentI al, String label, String colour)
\r
385 UserColourScheme ucs = new UserColourScheme(colour);
\r
386 Annotation[] annotations;
\r
387 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
389 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
391 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
392 for(int j=0; j<annotations.length; j++)
\r
394 if(annotations[j]!=null)
\r
395 annotations[j].colour = ucs.findColour('A');
\r
401 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
403 int graphGroup = -1;
\r
404 String group = st.nextToken();
\r
405 //First make sure we are not overwriting the graphIndex
\r
406 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
408 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
410 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
411 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
416 //Now update groups
\r
417 while(st.hasMoreTokens())
\r
419 group = st.nextToken();
\r
420 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
422 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
424 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
431 void addLine(AlignmentI al, StringTokenizer st)
\r
433 String group = st.nextToken();
\r
434 AlignmentAnnotation annotation = null;
\r
436 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
438 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
440 annotation = al.getAlignmentAnnotation()[i];
\r
445 if(annotation==null)
\r
447 float value = new Float(st.nextToken()).floatValue();
\r
448 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
449 java.awt.Color colour = null;
\r
450 if(st.hasMoreTokens())
\r
452 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
453 colour = ucs.findColour('A');
\r
456 annotation.setThreshold(new GraphLine(value, label, colour));
\r