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,
\r
173 BufferedReader in = null;
\r
174 if (protocol.equals(AppletFormatAdapter.FILE))
\r
176 in = new BufferedReader(new FileReader(file));
\r
178 else if (protocol.equals(AppletFormatAdapter.URL))
\r
180 URL url = new URL(file);
\r
181 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
183 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
185 in = new BufferedReader(new StringReader(file));
\r
187 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
189 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
192 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
196 String line, label, description, token;
\r
197 int graphStyle, index;
\r
198 SequenceI refSeq = null;
\r
199 int refSeqIndex = 1;
\r
200 int existingAnnotations = 0;
\r
201 if(al.getAlignmentAnnotation()!=null)
\r
202 existingAnnotations = al.getAlignmentAnnotation().length;
\r
204 int alWidth = al.getWidth();
\r
206 StringTokenizer st;
\r
207 Annotation[] annotations;
\r
208 AlignmentAnnotation annotation = null;
\r
210 // First confirm this is an Annotation file
\r
211 boolean jvAnnotationFile = false;
\r
212 while ( (line = in.readLine()) != null)
\r
214 if (line.indexOf("#") == 0 )
\r
217 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
219 jvAnnotationFile = true;
\r
224 if(!jvAnnotationFile)
\r
230 while ( (line = in.readLine()) != null)
\r
232 if(line.indexOf("#")==0
\r
233 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
234 || line.length()==0)
\r
237 st = new StringTokenizer(line, "\t");
\r
238 token = st.nextToken();
\r
239 if(token.equalsIgnoreCase("COLOUR"))
\r
241 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
245 if(token.equalsIgnoreCase("COMBINE") )
\r
247 combineAnnotations(al, st);
\r
251 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
258 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
260 refSeq = al.findName(st.nextToken());
\r
262 refSeqIndex = Integer.parseInt(st.nextToken());
\r
266 System.out.println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
269 catch(Exception ex)
\r
278 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
279 label = st.nextToken();
\r
281 if(st.countTokens()>1)
\r
282 description = st.nextToken();
\r
284 description = null;
\r
286 line = st.nextToken();
\r
288 st = new StringTokenizer(line, "|", true);
\r
289 annotations = new Annotation[alWidth];
\r
292 boolean emptyColumn = true;
\r
295 while (st.hasMoreElements() && index<alWidth)
\r
297 token = st.nextToken().trim();
\r
298 if(token.equals("|"))
\r
303 emptyColumn = true;
\r
307 annotations[index++] = parseAnnotation(token);
\r
308 emptyColumn = false;
\r
312 annotation = new AlignmentAnnotation(label,
\r
321 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
322 refSeq.addAlignmentAnnotation(annotation);
\r
325 al.addAnnotation(annotation);
\r
327 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
330 }catch(Exception ex)
\r
332 ex.printStackTrace();
\r
333 System.out.println("Problem reading annotation file: "+ex);
\r
339 Annotation parseAnnotation(String string)
\r
341 String desc = null, displayChar="";
\r
342 char ss = ' '; // secondaryStructure
\r
344 boolean parsedValue = false;
\r
345 StringTokenizer st = new StringTokenizer(string, ",");
\r
347 while(st.hasMoreTokens())
\r
349 token = st.nextToken().trim();
\r
350 if(token.length()==0)
\r
356 displayChar = token;
\r
357 value = new Float(token).floatValue();
\r
358 parsedValue = true;
\r
360 }catch(NumberFormatException ex){}
\r
363 if(token.equals("H") || token.equals("E"))
\r
365 // Either this character represents a helix or sheet
\r
366 // or an integer which can be displayed
\r
367 ss = token.charAt(0);
\r
368 if(displayChar.equals(token.substring(0,1)))
\r
371 else if(desc==null)
\r
379 if(displayChar.length()>1 && desc.length()==1)
\r
381 String tmp = displayChar;
\r
382 displayChar = desc;
\r
386 return new Annotation(displayChar, desc, ss, value);
\r
389 void colourAnnotations(AlignmentI al, String label, String colour)
\r
391 UserColourScheme ucs = new UserColourScheme(colour);
\r
392 Annotation[] annotations;
\r
393 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
395 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
397 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
398 for(int j=0; j<annotations.length; j++)
\r
400 if(annotations[j]!=null)
\r
401 annotations[j].colour = ucs.findColour('A');
\r
407 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
409 int graphGroup = -1;
\r
410 String group = st.nextToken();
\r
411 //First make sure we are not overwriting the graphIndex
\r
412 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
414 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
416 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
417 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
422 //Now update groups
\r
423 while(st.hasMoreTokens())
\r
425 group = st.nextToken();
\r
426 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
428 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
430 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
437 void addLine(AlignmentI al, StringTokenizer st)
\r
439 String group = st.nextToken();
\r
440 AlignmentAnnotation annotation = null;
\r
442 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
444 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
446 annotation = al.getAlignmentAnnotation()[i];
\r
451 if(annotation==null)
\r
453 float value = new Float(st.nextToken()).floatValue();
\r
454 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
455 java.awt.Color colour = null;
\r
456 if(st.hasMoreTokens())
\r
458 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
459 colour = ucs.findColour('A');
\r
462 annotation.setThreshold(new GraphLine(value, label, colour));
\r