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 java.net.URL;
\r
26 import jalview.schemes.*;
\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
168 SequenceI refSeq = null;
\r
169 Hashtable annotationsHash = new Hashtable();
\r
170 public boolean readAnnotationFile(AlignmentI al,
\r
176 BufferedReader in = null;
\r
177 if (protocol.equals(AppletFormatAdapter.FILE))
\r
179 in = new BufferedReader(new FileReader(file));
\r
181 else if (protocol.equals(AppletFormatAdapter.URL))
\r
183 URL url = new URL(file);
\r
184 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
186 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
188 in = new BufferedReader(new StringReader(file));
\r
190 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
192 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
195 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
199 String line, label, description, token;
\r
200 int graphStyle, index;
\r
201 int refSeqIndex = 1;
\r
202 int existingAnnotations = 0;
\r
203 if(al.getAlignmentAnnotation()!=null)
\r
204 existingAnnotations = al.getAlignmentAnnotation().length;
\r
206 int alWidth = al.getWidth();
\r
208 StringTokenizer st;
\r
209 Annotation[] annotations;
\r
210 AlignmentAnnotation annotation = null;
\r
212 // First confirm this is an Annotation file
\r
213 boolean jvAnnotationFile = false;
\r
214 while ( (line = in.readLine()) != null)
\r
216 if (line.indexOf("#") == 0 )
\r
219 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
221 jvAnnotationFile = true;
\r
226 if(!jvAnnotationFile)
\r
232 while ( (line = in.readLine()) != null)
\r
234 if(line.indexOf("#")==0
\r
235 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
236 || line.length()==0)
\r
239 st = new StringTokenizer(line, "\t");
\r
240 token = st.nextToken();
\r
241 if(token.equalsIgnoreCase("COLOUR"))
\r
243 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
247 else if(token.equalsIgnoreCase("COMBINE") )
\r
249 combineAnnotations(al, st);
\r
253 else if (token.equalsIgnoreCase("GRAPHLINE"))
\r
260 else if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
262 refSeq = al.findName(st.nextToken());
\r
264 refSeqIndex = Integer.parseInt(st.nextToken());
\r
268 System.out.println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
271 catch(Exception ex)
\r
279 else if(token.equalsIgnoreCase("SEQUENCE_GROUP"))
\r
285 else if(token.equalsIgnoreCase("PROPERTIES"))
\r
287 addProperties(al, st);
\r
292 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
293 label = st.nextToken();
\r
295 if(st.countTokens()>1)
\r
296 description = st.nextToken();
\r
298 description = null;
\r
300 line = st.nextToken();
\r
302 st = new StringTokenizer(line, "|", true);
\r
303 annotations = new Annotation[alWidth];
\r
306 boolean emptyColumn = true;
\r
309 while (st.hasMoreElements() && index<alWidth)
\r
311 token = st.nextToken().trim();
\r
312 if(token.equals("|"))
\r
317 emptyColumn = true;
\r
321 annotations[index++] = parseAnnotation(token);
\r
322 emptyColumn = false;
\r
326 annotation = new AlignmentAnnotation(label,
\r
335 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
336 refSeq.addAlignmentAnnotation(annotation);
\r
339 al.addAnnotation(annotation);
\r
341 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
344 }catch(Exception ex)
\r
346 ex.printStackTrace();
\r
347 System.out.println("Problem reading annotation file: "+ex);
\r
353 Annotation parseAnnotation(String string)
\r
355 String desc = null, displayChar="";
\r
356 char ss = ' '; // secondaryStructure
\r
358 boolean parsedValue = false;
\r
359 StringTokenizer st = new StringTokenizer(string, ",");
\r
361 while(st.hasMoreTokens())
\r
363 token = st.nextToken().trim();
\r
364 if(token.length()==0)
\r
370 displayChar = token;
\r
371 value = new Float(token).floatValue();
\r
372 parsedValue = true;
\r
374 }catch(NumberFormatException ex){}
\r
377 if(token.equals("H") || token.equals("E"))
\r
379 // Either this character represents a helix or sheet
\r
380 // or an integer which can be displayed
\r
381 ss = token.charAt(0);
\r
382 if(displayChar.equals(token.substring(0,1)))
\r
385 else if(desc==null)
\r
393 if(displayChar.length()>1 && desc.length()==1)
\r
395 String tmp = displayChar;
\r
396 displayChar = desc;
\r
400 return new Annotation(displayChar, desc, ss, value);
\r
403 void colourAnnotations(AlignmentI al, String label, String colour)
\r
405 UserColourScheme ucs = new UserColourScheme(colour);
\r
406 Annotation[] annotations;
\r
407 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
409 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
411 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
412 for(int j=0; j<annotations.length; j++)
\r
414 if(annotations[j]!=null)
\r
415 annotations[j].colour = ucs.findColour('A');
\r
421 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
423 int graphGroup = -1;
\r
424 String group = st.nextToken();
\r
425 //First make sure we are not overwriting the graphIndex
\r
426 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
428 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
430 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
431 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
436 //Now update groups
\r
437 while(st.hasMoreTokens())
\r
439 group = st.nextToken();
\r
440 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
442 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
444 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
451 void addLine(AlignmentI al, StringTokenizer st)
\r
453 String group = st.nextToken();
\r
454 AlignmentAnnotation annotation = null;
\r
456 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
458 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
460 annotation = al.getAlignmentAnnotation()[i];
\r
465 if(annotation==null)
\r
467 float value = new Float(st.nextToken()).floatValue();
\r
468 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
469 java.awt.Color colour = null;
\r
470 if(st.hasMoreTokens())
\r
472 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
473 colour = ucs.findColour('A');
\r
476 annotation.setThreshold(new GraphLine(value, label, colour));
\r
479 void addGroup(AlignmentI al, StringTokenizer st)
\r
481 SequenceGroup sg = new SequenceGroup();
\r
482 sg.setName(st.nextToken());
\r
483 sg.setStartRes(Integer.parseInt(st.nextToken())-1);
\r
484 sg.setEndRes(Integer.parseInt(st.nextToken())-1);
\r
486 String index = st.nextToken();
\r
487 if(index.equals("-1"))
\r
489 while (st.hasMoreElements())
\r
491 sg.addSequence(al.findName(st.nextToken()), false);
\r
496 StringTokenizer st2 = new StringTokenizer(index, ",");
\r
498 while (st2.hasMoreTokens())
\r
500 String tmp = st2.nextToken();
\r
501 if (tmp.equals("*"))
\r
503 for (int i = 0; i < al.getHeight(); i++)
\r
505 sg.addSequence(al.getSequenceAt(i), false);
\r
508 else if (tmp.indexOf("-") >= 0)
\r
510 StringTokenizer st3 = new StringTokenizer(tmp, "-");
\r
512 int start = (Integer.parseInt(st3.nextToken()));
\r
513 int end = (Integer.parseInt(st3.nextToken()));
\r
516 for (int i = start; i <= end; i++)
\r
517 sg.addSequence(al.getSequenceAt(i-1), false);
\r
521 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp)-1 ), false);
\r
528 sg.setStartRes( refSeq.findIndex( sg.getStartRes()+1 )-1 );
\r
529 sg.setEndRes( refSeq.findIndex( sg.getEndRes() +1) -1 );
\r
534 annotationsHash.put(sg.getName(), sg);
\r
537 void addProperties(AlignmentI al, StringTokenizer st)
\r
540 //So far we have only added groups to the annotationHash,
\r
541 //the idea is in the future properties can be added to
\r
542 //alignments, other annotations etc
\r
543 SequenceGroup sg = (SequenceGroup)annotationsHash.get(st.nextToken());
\r
547 String keyValue, key, value;
\r
548 while(st.hasMoreTokens())
\r
550 keyValue = st.nextToken();
\r
551 key = keyValue.substring(0,keyValue.indexOf("="));
\r
552 value=keyValue.substring(keyValue.indexOf("=")+1);
\r
554 if(key.equalsIgnoreCase("description"))
\r
555 sg.setDescription(value);
\r
556 else if(key.equalsIgnoreCase("colour"))
\r
557 sg.cs = ColourSchemeProperty.getColour(al, value);
\r
558 else if (key.equalsIgnoreCase("pidThreshold"))
\r
559 sg.cs.setThreshold(Integer.parseInt(value), false);//
\r
560 else if (key.equalsIgnoreCase("outlineColour"))
\r
562 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
\r
565 //Boolean.valueOf(string).booleanValue();
\r
566 else if (key.equalsIgnoreCase("displayBoxes"))
\r
567 sg.setDisplayBoxes( Boolean.valueOf(value).booleanValue() );
\r
568 else if (key.equalsIgnoreCase("displayText"))
\r
569 sg.setDisplayText( Boolean.valueOf(value).booleanValue() );
\r
570 else if (key.equalsIgnoreCase("colourText"))
\r
571 sg.setColourText( Boolean.valueOf(value).booleanValue() );
\r
572 else if (key.equalsIgnoreCase("textCol1"))
\r
574 sg.textColour = new UserColourScheme(value).findColour('A');
\r
576 else if (key.equalsIgnoreCase("textCol2"))
\r
578 sg.textColour2 = new UserColourScheme(value).findColour('A');
\r
580 else if (key.equalsIgnoreCase("textColThreshold"))
\r
581 sg.thresholdTextColour = Integer.parseInt(value);
\r