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
27 import jalview.analysis.Conservation;
\r
30 public class AnnotationFile
\r
32 StringBuffer text = new StringBuffer(
\r
33 "JALVIEW_ANNOTATION\n"
\r
35 +new java.util.Date()+"\n\n");
\r
37 public String printAnnotations(AlignmentAnnotation [] annotations,
\r
40 if(annotations!=null)
\r
42 AlignmentAnnotation row;
\r
44 SequenceI seqref = null;
\r
46 StringBuffer colours = new StringBuffer();
\r
47 StringBuffer graphLine = new StringBuffer();
\r
49 Hashtable graphGroup = new Hashtable();
\r
51 java.awt.Color color;
\r
53 for (int i = 0; i < annotations.length; i++)
\r
55 row = annotations[i];
\r
62 if (row.sequenceRef == null)
\r
65 text.append("\nSEQUENCE_REF\tALIGNMENT\n");
\r
70 else if (seqref == null || seqref != row.sequenceRef)
\r
72 seqref = row.sequenceRef;
\r
73 text.append("\nSEQUENCE_REF\t" + seqref.getName() + "\n");
\r
76 if (row.graph == AlignmentAnnotation.NO_GRAPH)
\r
78 text.append("NO_GRAPH\t");
\r
82 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
83 text.append("BAR_GRAPH\t");
\r
84 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
85 text.append("LINE_GRAPH\t");
\r
87 if (row.getThreshold() != null)
\r
88 graphLine.append("GRAPHLINE\t"
\r
90 + row.getThreshold().value + "\t"
\r
91 + row.getThreshold().label + "\t"
\r
92 + jalview.util.Format.getHexString(
\r
93 row.getThreshold().colour) + "\n"
\r
96 if (row.graphGroup > -1)
\r
98 String key = String.valueOf(row.graphGroup);
\r
99 if (graphGroup.containsKey(key))
\r
100 graphGroup.put(key, graphGroup.get(key)
\r
101 + "\t" + row.label);
\r
103 graphGroup.put(key, row.label);
\r
107 text.append(row.label + "\t");
\r
108 if (row.description != null)
\r
109 text.append(row.description + "\t");
\r
111 for (int j = 0; j < row.annotations.length; j++)
\r
113 if (seqref != null &&
\r
114 jalview.util.Comparison.isGap(seqref.getCharAt(j)))
\r
117 if (row.annotations[j] != null)
\r
120 if (row.annotations[j].secondaryStructure != ' ')
\r
122 text.append(comma + row.annotations[j].secondaryStructure);
\r
125 if (row.annotations[j].displayCharacter.length() > 0
\r
126 && !row.annotations[j].displayCharacter.equals(" "))
\r
128 text.append(comma + row.annotations[j].displayCharacter);
\r
132 if (row.annotations[j] != null)
\r
134 color = row.annotations[j].colour;
\r
135 if (row.annotations[j].value != 0f)
\r
136 text.append(comma + row.annotations[j].value);
\r
144 if (color != null && color != java.awt.Color.black)
\r
146 colours.append("COLOUR\t"
\r
148 + jalview.util.Format.getHexString(color) + "\n");
\r
155 text.append(colours.toString());
\r
156 text.append(graphLine.toString());
\r
157 if (graphGroup.size() > 0)
\r
159 text.append("COMBINE\t");
\r
160 Enumeration en = graphGroup.elements();
\r
161 while (en.hasMoreElements())
\r
163 text.append(en.nextElement() + "\n");
\r
171 printGroups(groups);
\r
174 return text.toString();
\r
178 public void printGroups(Vector sequenceGroups)
\r
181 for (int i = 0; i < sequenceGroups.size(); i++)
\r
183 sg = (SequenceGroup) sequenceGroups.elementAt(i);
\r
184 text.append("SEQUENCE_GROUP\t"
\r
185 + sg.getName() + "\t"
\r
186 + (sg.getStartRes()+1) + "\t"
\r
187 + (sg.getEndRes()+1) + "\t" + "-1\t");
\r
188 for(int s=0; s<sg.getSize(); s++)
\r
190 text.append(sg.getSequenceAt(s).getName()+"\t");
\r
193 text.append("\nPROPERTIES\t"+sg.getName()+"\t");
\r
195 if(sg.getDescription()!=null)
\r
196 text.append("description="+sg.getDescription()+"\t");
\r
199 text.append("colour="+ColourSchemeProperty.getColourName(sg.cs)+"\t");
\r
200 if(sg.cs.getThreshold()!=0)
\r
201 text.append("pidThreshold="+sg.cs.getThreshold());
\r
202 if(sg.cs.conservationApplied())
\r
203 text.append("consThreshold="+sg.cs.getConservationInc()+"\t");
\r
205 text.append("outlineColour="+
\r
206 jalview.util.Format.getHexString(sg.getOutlineColour())+"\t");
\r
208 text.append("displayBoxes=" + sg.getDisplayBoxes() + "\t");
\r
209 text.append("displayText=" + sg.getDisplayText() + "\t");
\r
210 text.append("colourText=" + sg.getColourText() + "\t");
\r
212 if (sg.textColour != java.awt.Color.black)
\r
213 text.append("textCol1=" +
\r
214 jalview.util.Format.getHexString(sg.textColour) + "\t");
\r
215 if (sg.textColour2 != java.awt.Color.white)
\r
216 text.append("textCol2=" +
\r
217 jalview.util.Format.getHexString(sg.textColour2) + "\t");
\r
218 if(sg.thresholdTextColour!=0)
\r
219 text.append("textColThreshold="+sg.thresholdTextColour);
\r
221 text.append("\n\n");
\r
227 SequenceI refSeq = null;
\r
228 Hashtable annotationsHash = new Hashtable();
\r
229 public boolean readAnnotationFile(AlignmentI al,
\r
235 BufferedReader in = null;
\r
236 if (protocol.equals(AppletFormatAdapter.FILE))
\r
238 in = new BufferedReader(new FileReader(file));
\r
240 else if (protocol.equals(AppletFormatAdapter.URL))
\r
242 URL url = new URL(file);
\r
243 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
245 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
247 in = new BufferedReader(new StringReader(file));
\r
249 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
251 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
254 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
258 String line, label, description, token;
\r
259 int graphStyle, index;
\r
260 int refSeqIndex = 1;
\r
261 int existingAnnotations = 0;
\r
262 if(al.getAlignmentAnnotation()!=null)
\r
263 existingAnnotations = al.getAlignmentAnnotation().length;
\r
265 int alWidth = al.getWidth();
\r
267 StringTokenizer st;
\r
268 Annotation[] annotations;
\r
269 AlignmentAnnotation annotation = null;
\r
271 // First confirm this is an Annotation file
\r
272 boolean jvAnnotationFile = false;
\r
273 while ( (line = in.readLine()) != null)
\r
275 if (line.indexOf("#") == 0 )
\r
278 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
280 jvAnnotationFile = true;
\r
285 if(!jvAnnotationFile)
\r
291 while ( (line = in.readLine()) != null)
\r
293 if(line.indexOf("#")==0
\r
294 || line.indexOf("JALVIEW_ANNOTATION")>-1
\r
295 || line.length()==0)
\r
298 st = new StringTokenizer(line, "\t");
\r
299 token = st.nextToken();
\r
300 if(token.equalsIgnoreCase("COLOUR"))
\r
302 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
306 else if(token.equalsIgnoreCase("COMBINE") )
\r
308 combineAnnotations(al, st);
\r
312 else if (token.equalsIgnoreCase("GRAPHLINE"))
\r
319 else if(token.equalsIgnoreCase("SEQUENCE_REF") )
\r
321 refSeq = al.findName(st.nextToken());
\r
323 refSeqIndex = Integer.parseInt(st.nextToken());
\r
327 System.out.println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
330 catch(Exception ex)
\r
338 else if(token.equalsIgnoreCase("SEQUENCE_GROUP"))
\r
344 else if(token.equalsIgnoreCase("PROPERTIES"))
\r
346 addProperties(al, st);
\r
351 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
352 label = st.nextToken();
\r
354 if(st.countTokens()>1)
\r
355 description = st.nextToken();
\r
357 description = null;
\r
359 line = st.nextToken();
\r
361 st = new StringTokenizer(line, "|", true);
\r
362 annotations = new Annotation[alWidth];
\r
365 boolean emptyColumn = true;
\r
368 while (st.hasMoreElements() && index<alWidth)
\r
370 token = st.nextToken().trim();
\r
371 if(token.equals("|"))
\r
376 emptyColumn = true;
\r
380 annotations[index++] = parseAnnotation(token);
\r
381 emptyColumn = false;
\r
385 annotation = new AlignmentAnnotation(label,
\r
394 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
395 refSeq.addAlignmentAnnotation(annotation);
\r
398 al.addAnnotation(annotation);
\r
400 al.setAnnotationIndex(annotation, al.getAlignmentAnnotation().length - existingAnnotations-1);
\r
403 }catch(Exception ex)
\r
405 ex.printStackTrace();
\r
406 System.out.println("Problem reading annotation file: "+ex);
\r
412 Annotation parseAnnotation(String string)
\r
414 String desc = null, displayChar="";
\r
415 char ss = ' '; // secondaryStructure
\r
417 boolean parsedValue = false;
\r
418 StringTokenizer st = new StringTokenizer(string, ",");
\r
420 while(st.hasMoreTokens())
\r
422 token = st.nextToken().trim();
\r
423 if(token.length()==0)
\r
429 displayChar = token;
\r
430 value = new Float(token).floatValue();
\r
431 parsedValue = true;
\r
433 }catch(NumberFormatException ex){}
\r
436 if(token.equals("H") || token.equals("E"))
\r
438 // Either this character represents a helix or sheet
\r
439 // or an integer which can be displayed
\r
440 ss = token.charAt(0);
\r
441 if(displayChar.equals(token.substring(0,1)))
\r
444 else if(desc==null)
\r
452 if(displayChar.length()>1 && desc.length()==1)
\r
454 String tmp = displayChar;
\r
455 displayChar = desc;
\r
459 return new Annotation(displayChar, desc, ss, value);
\r
462 void colourAnnotations(AlignmentI al, String label, String colour)
\r
464 UserColourScheme ucs = new UserColourScheme(colour);
\r
465 Annotation[] annotations;
\r
466 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
468 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
470 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
471 for(int j=0; j<annotations.length; j++)
\r
473 if(annotations[j]!=null)
\r
474 annotations[j].colour = ucs.findColour('A');
\r
480 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
482 int graphGroup = -1;
\r
483 String group = st.nextToken();
\r
484 //First make sure we are not overwriting the graphIndex
\r
485 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
487 if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
489 graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;
\r
490 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
495 //Now update groups
\r
496 while(st.hasMoreTokens())
\r
498 group = st.nextToken();
\r
499 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
501 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
503 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
510 void addLine(AlignmentI al, StringTokenizer st)
\r
512 String group = st.nextToken();
\r
513 AlignmentAnnotation annotation = null;
\r
515 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
517 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
519 annotation = al.getAlignmentAnnotation()[i];
\r
524 if(annotation==null)
\r
526 float value = new Float(st.nextToken()).floatValue();
\r
527 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
528 java.awt.Color colour = null;
\r
529 if(st.hasMoreTokens())
\r
531 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
532 colour = ucs.findColour('A');
\r
535 annotation.setThreshold(new GraphLine(value, label, colour));
\r
538 void addGroup(AlignmentI al, StringTokenizer st)
\r
540 SequenceGroup sg = new SequenceGroup();
\r
541 sg.setName(st.nextToken());
\r
542 sg.setStartRes(Integer.parseInt(st.nextToken())-1);
\r
543 sg.setEndRes(Integer.parseInt(st.nextToken())-1);
\r
545 String index = st.nextToken();
\r
546 if(index.equals("-1"))
\r
548 while (st.hasMoreElements())
\r
550 sg.addSequence(al.findName(st.nextToken()), false);
\r
555 StringTokenizer st2 = new StringTokenizer(index, ",");
\r
557 while (st2.hasMoreTokens())
\r
559 String tmp = st2.nextToken();
\r
560 if (tmp.equals("*"))
\r
562 for (int i = 0; i < al.getHeight(); i++)
\r
564 sg.addSequence(al.getSequenceAt(i), false);
\r
567 else if (tmp.indexOf("-") >= 0)
\r
569 StringTokenizer st3 = new StringTokenizer(tmp, "-");
\r
571 int start = (Integer.parseInt(st3.nextToken()));
\r
572 int end = (Integer.parseInt(st3.nextToken()));
\r
575 for (int i = start; i <= end; i++)
\r
576 sg.addSequence(al.getSequenceAt(i-1), false);
\r
580 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp)-1 ), false);
\r
587 sg.setStartRes( refSeq.findIndex( sg.getStartRes()+1 )-1 );
\r
588 sg.setEndRes( refSeq.findIndex( sg.getEndRes() +1) -1 );
\r
594 annotationsHash.put(sg.getName(), sg);
\r
598 void addProperties(AlignmentI al, StringTokenizer st)
\r
601 //So far we have only added groups to the annotationHash,
\r
602 //the idea is in the future properties can be added to
\r
603 //alignments, other annotations etc
\r
604 SequenceGroup sg = (SequenceGroup)annotationsHash.get(st.nextToken());
\r
608 String keyValue, key, value;
\r
609 while(st.hasMoreTokens())
\r
611 keyValue = st.nextToken();
\r
612 key = keyValue.substring(0,keyValue.indexOf("="));
\r
613 value=keyValue.substring(keyValue.indexOf("=")+1);
\r
615 if(key.equalsIgnoreCase("description"))
\r
616 sg.setDescription(value);
\r
617 else if(key.equalsIgnoreCase("colour"))
\r
618 sg.cs = ColourSchemeProperty.getColour(al, value);
\r
619 else if (key.equalsIgnoreCase("pidThreshold"))
\r
621 sg.cs.setThreshold(Integer.parseInt(value), true);
\r
624 else if (key.equalsIgnoreCase("consThreshold"))
\r
626 sg.cs.setConservationInc(Integer.parseInt(value));
\r
627 Conservation c = new Conservation("Group",
\r
628 ResidueProperties.propHash, 3,
\r
629 sg.getSequences(null),
\r
631 sg.getEndRes() + 1);
\r
634 c.verdict(false, 25);
\r
636 sg.cs.setConservation(c);
\r
639 else if (key.equalsIgnoreCase("outlineColour"))
\r
641 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
\r
643 else if (key.equalsIgnoreCase("displayBoxes"))
\r
644 sg.setDisplayBoxes( Boolean.valueOf(value).booleanValue() );
\r
645 else if (key.equalsIgnoreCase("displayText"))
\r
646 sg.setDisplayText( Boolean.valueOf(value).booleanValue() );
\r
647 else if (key.equalsIgnoreCase("colourText"))
\r
648 sg.setColourText( Boolean.valueOf(value).booleanValue() );
\r
649 else if (key.equalsIgnoreCase("textCol1"))
\r
651 sg.textColour = new UserColourScheme(value).findColour('A');
\r
653 else if (key.equalsIgnoreCase("textCol2"))
\r
655 sg.textColour2 = new UserColourScheme(value).findColour('A');
\r
657 else if (key.equalsIgnoreCase("textColThreshold"))
\r
658 sg.thresholdTextColour = Integer.parseInt(value);
\r
660 sg.recalcConservation();
\r