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 else if (seqref == null || seqref != row.sequenceRef)
\r
60 seqref = row.sequenceRef;
\r
61 text.append("\nSEQUENCE_REF\t" + seqref.getName() + "\n");
\r
65 if( row.graph == AlignmentAnnotation.NO_GRAPH)
\r
67 text.append("NO_GRAPH\t");
\r
71 if( row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
72 text.append("BAR_GRAPH\t");
\r
73 else if(row.graph == 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 (seqref != null && jalview.util.Comparison.isGap(seqref.getCharAt(j)))
\r
103 if(row.annotations[j]!=null)
\r
106 if (row.annotations[j].secondaryStructure!=' ')
\r
108 text.append(comma + row.annotations[j].secondaryStructure);
\r
111 if (row.annotations[j].displayCharacter.length() > 0
\r
112 && !row.annotations[j].displayCharacter.equals(" "))
\r
114 text.append(comma + row.annotations[j].displayCharacter);
\r
118 if (row.annotations[j]!=null)
\r
120 color = row.annotations[j].colour;
\r
121 if (row.annotations[j].value!=0f)
\r
122 text.append(comma + row.annotations[j].value);
\r
130 if(color!=null && color!=java.awt.Color.black)
\r
132 colours.append("COLOUR\t"
\r
134 +jalview.util.Format.getHexString(color)+"\n");
\r
141 text.append(colours.toString());
\r
142 text.append(graphLine.toString());
\r
143 if(graphGroup.size()>0)
\r
145 text.append("COMBINE\t");
\r
146 Enumeration en = graphGroup.elements();
\r
147 while(en.hasMoreElements())
\r
149 text.append(en.nextElement()+"\n");
\r
153 return text.toString();
\r
156 public boolean readAnnotationFile(AlignmentI al, String file)
\r
160 BufferedReader in = null;
\r
161 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
164 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
170 URL url = new URL(file);
\r
171 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
173 catch (java.net.MalformedURLException ex)
\r
175 in = new BufferedReader(new FileReader(file));
\r
179 String line, label, description, token;
\r
180 int graphStyle, index;
\r
181 SequenceI refSeq = null;
\r
182 int refSeqIndex = 1;
\r
183 int existingAnnotations = 0;
\r
184 if(al.getAlignmentAnnotation()!=null)
\r
185 existingAnnotations = al.getAlignmentAnnotation().length;
\r
187 int alWidth = al.getWidth();
\r
189 StringTokenizer st;
\r
190 Annotation[] annotations;
\r
191 AlignmentAnnotation annotation = null;
\r
193 // First confirm this is an Annotation file
\r
194 boolean jvAnnotationFile = false;
\r
195 while ( (line = in.readLine()) != null)
\r
197 if (line.indexOf("#") == 0 )
\r
200 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
202 jvAnnotationFile = true;
\r
207 if(!jvAnnotationFile)
\r
213 while ( (line = in.readLine()) != null)
\r
215 if(line.indexOf("#")==0
\r
216 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
217 || line.length()==0)
\r
220 st = new StringTokenizer(line, "\t");
\r
221 token = st.nextToken();
\r
222 if(token.equalsIgnoreCase("COLOUR"))
\r
224 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
228 if(token.equalsIgnoreCase("COMBINE") )
\r
230 combineAnnotations(al, st);
\r
234 if (token.equalsIgnoreCase("GRAPHLINE"))
\r
241 if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
243 refSeq = al.findName(st.nextToken());
\r
245 refSeqIndex = Integer.parseInt(st.nextToken());
\r
249 System.out.println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
252 catch(Exception ex)
\r
261 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
262 label = description = st.nextToken();
\r
264 line = st.nextToken();
\r
266 st = new StringTokenizer(line, "|", true);
\r
267 annotations = new Annotation[alWidth];
\r
270 boolean emptyColumn = true;
\r
273 while (st.hasMoreElements() && index<alWidth)
\r
275 token = st.nextToken().trim();
\r
276 if(token.equals("|"))
\r
281 emptyColumn = true;
\r
285 annotations[index++] = parseAnnotation(token);
\r
286 emptyColumn = false;
\r
290 annotation = new AlignmentAnnotation(label,
\r
299 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
300 refSeq.addAlignmentAnnotation(annotation);
\r
303 al.addAnnotation(annotation);
\r
305 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
308 }catch(Exception ex)
\r
310 ex.printStackTrace();
\r
311 System.out.println("Problem reading annotation file: "+ex);
\r
317 Annotation parseAnnotation(String string)
\r
319 String desc = null, displayChar="";
\r
320 char ss = ' '; // secondaryStructure
\r
322 boolean parsedValue = false;
\r
323 StringTokenizer st = new StringTokenizer(string, ",");
\r
325 while(st.hasMoreTokens())
\r
327 token = st.nextToken().trim();
\r
328 if(token.length()==0)
\r
334 displayChar = token;
\r
335 value = new Float(token).floatValue();
\r
336 parsedValue = true;
\r
338 }catch(NumberFormatException ex){}
\r
341 if(token.equals("H") || token.equals("E"))
\r
343 // Either this character represents a helix or sheet
\r
344 // or an integer which can be displayed
\r
345 ss = token.charAt(0);
\r
346 if(displayChar.equals(token.substring(0,1)))
\r
349 else if(desc==null)
\r
357 if(displayChar.length()>1 && desc.length()==1)
\r
359 String tmp = displayChar;
\r
360 displayChar = desc;
\r
364 return new Annotation(displayChar, desc, ss, value);
\r
367 void colourAnnotations(AlignmentI al, String label, String colour)
\r
369 UserColourScheme ucs = new UserColourScheme(colour);
\r
370 Annotation[] annotations;
\r
371 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
373 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
375 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
376 for(int j=0; j<annotations.length; j++)
\r
378 if(annotations[j]!=null)
\r
379 annotations[j].colour = ucs.findColour('A');
\r
385 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
387 int graphGroup = -1;
\r
388 String group = st.nextToken();
\r
389 //First make sure we are not overwriting the graphIndex
\r
390 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
392 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
394 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
395 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
400 //Now update groups
\r
401 while(st.hasMoreTokens())
\r
403 group = st.nextToken();
\r
404 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
406 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
408 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
415 void addLine(AlignmentI al, StringTokenizer st)
\r
417 String group = st.nextToken();
\r
418 AlignmentAnnotation annotation = null;
\r
420 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
422 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
424 annotation = al.getAlignmentAnnotation()[i];
\r
429 if(annotation==null)
\r
431 float value = new Float(st.nextToken()).floatValue();
\r
432 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
433 java.awt.Color colour = null;
\r
434 if(st.hasMoreTokens())
\r
436 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
437 colour = ucs.findColour('A');
\r
440 annotation.setThreshold(new GraphLine(value, label, colour));
\r