2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 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
26 import jalview.analysis.*;
\r
27 import jalview.datamodel.*;
\r
28 import jalview.schemes.*;
\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
39 Hashtable properties)
\r
41 if (annotations != null)
\r
43 boolean oneColour = true;
\r
44 AlignmentAnnotation row;
\r
46 SequenceI refSeq = null;
\r
48 StringBuffer colours = new StringBuffer();
\r
49 StringBuffer graphLine = new StringBuffer();
\r
51 Hashtable graphGroup = new Hashtable();
\r
53 java.awt.Color color;
\r
55 for (int i = 0; i < annotations.length; i++)
\r
57 row = annotations[i];
\r
59 if (!row.visible && !row.hasScore())
\r
67 if (row.sequenceRef == null)
\r
71 text.append("\nSEQUENCE_REF\tALIGNMENT\n");
\r
77 else if (refSeq == null || refSeq != row.sequenceRef)
\r
79 refSeq = row.sequenceRef;
\r
80 text.append("\nSEQUENCE_REF\t" + refSeq.getName() + "\n");
\r
83 if (row.graph == AlignmentAnnotation.NO_GRAPH)
\r
85 text.append("NO_GRAPH\t");
\r
89 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
91 text.append("BAR_GRAPH\t");
\r
93 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
95 text.append("LINE_GRAPH\t");
\r
98 if (row.getThreshold() != null)
\r
100 graphLine.append("GRAPHLINE\t"
\r
102 + row.getThreshold().value + "\t"
\r
103 + row.getThreshold().label + "\t"
\r
104 + jalview.util.Format.getHexString(
\r
105 row.getThreshold().colour) + "\n"
\r
109 if (row.graphGroup > -1)
\r
111 String key = String.valueOf(row.graphGroup);
\r
112 if (graphGroup.containsKey(key))
\r
114 graphGroup.put(key, graphGroup.get(key)
\r
115 + "\t" + row.label);
\r
119 graphGroup.put(key, row.label);
\r
124 text.append(row.label + "\t");
\r
125 if (row.description != null)
\r
127 text.append(row.description + "\t");
\r
130 for (int j = 0; row.annotations!=null && j < row.annotations.length; j++)
\r
132 if (refSeq != null &&
\r
133 jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
\r
138 if (row.annotations[j] != null)
\r
141 if (row.annotations[j].secondaryStructure != ' ')
\r
143 text.append(comma + row.annotations[j].secondaryStructure);
\r
146 if (row.annotations[j].displayCharacter!=null
\r
147 && row.annotations[j].displayCharacter.length() > 0
\r
148 && !row.annotations[j].displayCharacter.equals(" "))
\r
150 text.append(comma + row.annotations[j].displayCharacter);
\r
154 if (row.annotations[j] != null)
\r
156 if(color!=null && !color.equals(row.annotations[j].colour))
\r
161 color = row.annotations[j].colour;
\r
162 if (row.annotations[j].value != 0f)
\r
164 text.append(comma + row.annotations[j].value);
\r
168 if(row.annotations[j].colour!=null
\r
169 && row.annotations[j].colour!=java.awt.Color.black)
\r
171 text.append(comma+"["+
\r
172 jalview.util.Format.getHexString(
\r
173 row.annotations[j].colour)+"]");
\r
180 text.append("\t"+row.score);
\r
184 if (color != null && color != java.awt.Color.black && oneColour)
\r
186 colours.append("COLOUR\t"
\r
188 + jalview.util.Format.getHexString(color) + "\n");
\r
195 text.append(colours.toString());
\r
196 text.append(graphLine.toString());
\r
197 if (graphGroup.size() > 0)
\r
199 text.append("COMBINE\t");
\r
200 Enumeration en = graphGroup.elements();
\r
201 while (en.hasMoreElements())
\r
203 text.append(en.nextElement() + "\n");
\r
208 if (groups != null)
\r
210 printGroups(groups);
\r
213 if(properties!=null)
\r
215 text.append("\n\nALIGNMENT");
\r
216 Enumeration en = properties.keys();
\r
217 while(en.hasMoreElements())
\r
219 String key = en.nextElement().toString();
\r
220 text.append("\t"+key+"="+properties.get(key));
\r
225 return text.toString();
\r
228 public void printGroups(Vector sequenceGroups)
\r
231 for (int i = 0; i < sequenceGroups.size(); i++)
\r
233 sg = (SequenceGroup) sequenceGroups.elementAt(i);
\r
234 text.append("SEQUENCE_GROUP\t"
\r
235 + sg.getName() + "\t"
\r
236 + (sg.getStartRes() + 1) + "\t"
\r
237 + (sg.getEndRes() + 1) + "\t" + "-1\t");
\r
238 for (int s = 0; s < sg.getSize(); s++)
\r
240 text.append(sg.getSequenceAt(s).getName() + "\t");
\r
243 text.append("\nPROPERTIES\t" + sg.getName() + "\t");
\r
245 if (sg.getDescription() != null)
\r
247 text.append("description=" + sg.getDescription() + "\t");
\r
251 text.append("colour=" + ColourSchemeProperty.getColourName(sg.cs) +
\r
253 if (sg.cs.getThreshold() != 0)
\r
255 text.append("pidThreshold=" + sg.cs.getThreshold());
\r
257 if (sg.cs.conservationApplied())
\r
259 text.append("consThreshold=" + sg.cs.getConservationInc() + "\t");
\r
262 text.append("outlineColour=" +
\r
263 jalview.util.Format.getHexString(sg.getOutlineColour()) +
\r
266 text.append("displayBoxes=" + sg.getDisplayBoxes() + "\t");
\r
267 text.append("displayText=" + sg.getDisplayText() + "\t");
\r
268 text.append("colourText=" + sg.getColourText() + "\t");
\r
270 if (sg.textColour != java.awt.Color.black)
\r
272 text.append("textCol1=" +
\r
273 jalview.util.Format.getHexString(sg.textColour) + "\t");
\r
275 if (sg.textColour2 != java.awt.Color.white)
\r
277 text.append("textCol2=" +
\r
278 jalview.util.Format.getHexString(sg.textColour2) + "\t");
\r
280 if (sg.thresholdTextColour != 0)
\r
282 text.append("textColThreshold=" + sg.thresholdTextColour);
\r
285 text.append("\n\n");
\r
290 SequenceI refSeq = null;
\r
291 public boolean readAnnotationFile(AlignmentI al,
\r
297 BufferedReader in = null;
\r
298 if (protocol.equals(AppletFormatAdapter.FILE))
\r
300 in = new BufferedReader(new FileReader(file));
\r
302 else if (protocol.equals(AppletFormatAdapter.URL))
\r
304 URL url = new URL(file);
\r
305 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
307 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
309 in = new BufferedReader(new StringReader(file));
\r
311 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
313 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
316 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
320 String line, label, description, token;
\r
321 int graphStyle, index;
\r
322 int refSeqIndex = 1;
\r
323 int existingAnnotations = 0;
\r
324 if (al.getAlignmentAnnotation() != null)
\r
326 existingAnnotations = al.getAlignmentAnnotation().length;
\r
329 int alWidth = al.getWidth();
\r
331 StringTokenizer st;
\r
332 Annotation[] annotations;
\r
333 AlignmentAnnotation annotation = null;
\r
335 // First confirm this is an Annotation file
\r
336 boolean jvAnnotationFile = false;
\r
337 while ( (line = in.readLine()) != null)
\r
339 if (line.indexOf("#") == 0)
\r
344 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
346 jvAnnotationFile = true;
\r
351 if (!jvAnnotationFile)
\r
357 while ( (line = in.readLine()) != null)
\r
359 if (line.indexOf("#") == 0
\r
360 || line.indexOf("JALVIEW_ANNOTATION") > -1
\r
361 || line.length() == 0)
\r
366 st = new StringTokenizer(line, "\t");
\r
367 token = st.nextToken();
\r
368 if (token.equalsIgnoreCase("COLOUR"))
\r
370 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
374 else if (token.equalsIgnoreCase("COMBINE"))
\r
376 combineAnnotations(al, st);
\r
380 else if (token.equalsIgnoreCase("GRAPHLINE"))
\r
386 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
\r
388 refSeq = al.findName(st.nextToken());
\r
391 refSeqIndex = Integer.parseInt(st.nextToken());
\r
392 if (refSeqIndex < 1)
\r
395 System.out.println(
\r
396 "WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
399 catch (Exception ex)
\r
407 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
\r
413 else if (token.equalsIgnoreCase("PROPERTIES"))
\r
415 addProperties(al, st);
\r
419 else if( token.equalsIgnoreCase("BELOW_ALIGNMENT"))
\r
421 setBelowAlignment(al, st);
\r
424 else if( token.equalsIgnoreCase("ALIGNMENT"))
\r
426 addAlignmentDetails(al, st);
\r
430 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
431 label = st.nextToken();
\r
435 annotations = new Annotation[alWidth];
\r
436 description = null;
\r
437 float score = Float.NaN;
\r
439 if(st.hasMoreTokens())
\r
441 line = st.nextToken();
\r
443 if (line.indexOf("|") ==-1)
\r
445 description = line;
\r
446 if (st.hasMoreTokens())
\r
447 line = st.nextToken();
\r
450 if(st.hasMoreTokens())
\r
452 //This must be the score
\r
453 score = Float.valueOf(st.nextToken()).floatValue();
\r
456 st = new StringTokenizer(line, "|", true);
\r
459 boolean emptyColumn = true;
\r
460 boolean onlyOneElement = (st.countTokens()==1);
\r
462 while (st.hasMoreElements() && index < alWidth)
\r
464 token = st.nextToken().trim();
\r
470 score = Float.valueOf(token).floatValue();
\r
473 catch(NumberFormatException ex){}
\r
476 if (token.equals("|"))
\r
483 emptyColumn = true;
\r
487 annotations[index++] = parseAnnotation(token);
\r
488 emptyColumn = false;
\r
494 annotation = new AlignmentAnnotation(label,
\r
496 (index==0) ? null : annotations,
\r
501 annotation.score = score;
\r
503 if (refSeq != null)
\r
505 annotation.belowAlignment=false;
\r
506 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
507 annotation.adjustForAlignment();
\r
508 refSeq.addAlignmentAnnotation(annotation);
\r
511 al.addAnnotation(annotation);
\r
513 al.setAnnotationIndex(annotation,
\r
514 al.getAlignmentAnnotation().length - existingAnnotations -
\r
519 catch (Exception ex)
\r
521 ex.printStackTrace();
\r
522 System.out.println("Problem reading annotation file: " + ex);
\r
528 Annotation parseAnnotation(String string)
\r
530 String desc = null, displayChar = null;
\r
531 char ss = ' '; // secondaryStructure
\r
533 boolean parsedValue = false;
\r
536 java.awt.Color colour = null;
\r
537 int i=string.indexOf("[");
\r
538 int j=string.indexOf("]");
\r
541 UserColourScheme ucs = new UserColourScheme();
\r
543 colour = ucs.getColourFromString(string.substring(i+1,j));
\r
545 string = string.substring(0,i)+string.substring(j+1);
\r
548 StringTokenizer st = new StringTokenizer(string, ",");
\r
550 while (st.hasMoreTokens())
\r
552 token = st.nextToken().trim();
\r
553 if (token.length() == 0)
\r
562 displayChar = token;
\r
563 value = new Float(token).floatValue();
\r
564 parsedValue = true;
\r
567 catch (NumberFormatException ex)
\r
571 if (token.equals("H") || token.equals("E"))
\r
573 // Either this character represents a helix or sheet
\r
574 // or an integer which can be displayed
\r
575 ss = token.charAt(0);
\r
576 if (displayChar.equals(token.substring(0, 1)))
\r
581 else if (desc == null)
\r
588 if (displayChar!=null
\r
589 && displayChar.length() > 1
\r
591 && desc.length() == 1)
\r
593 String tmp = displayChar;
\r
594 displayChar = desc;
\r
598 * In principle, this code will ensure that the Annotation element generated is renderable by any of the applet or application rendering code
\r
599 * but instead we check for null strings when the display character is rendered.
\r
600 if (displayChar==null)
\r
605 Annotation anot = new Annotation(displayChar, desc, ss, value);
\r
607 anot.colour = colour;
\r
612 void colourAnnotations(AlignmentI al, String label, String colour)
\r
614 UserColourScheme ucs = new UserColourScheme(colour);
\r
615 Annotation[] annotations;
\r
616 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
618 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
620 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
621 for (int j = 0; j < annotations.length; j++)
\r
623 if (annotations[j] != null)
\r
625 annotations[j].colour = ucs.findColour('A');
\r
632 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
634 int graphGroup = -1;
\r
635 String group = st.nextToken();
\r
636 //First make sure we are not overwriting the graphIndex
\r
637 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
639 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
641 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
\r
642 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
647 //Now update groups
\r
648 while (st.hasMoreTokens())
\r
650 group = st.nextToken();
\r
651 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
653 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
655 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
662 void addLine(AlignmentI al, StringTokenizer st)
\r
664 String group = st.nextToken();
\r
665 AlignmentAnnotation annotation = null;
\r
667 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
669 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
671 annotation = al.getAlignmentAnnotation()[i];
\r
676 if (annotation == null)
\r
680 float value = new Float(st.nextToken()).floatValue();
\r
681 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
682 java.awt.Color colour = null;
\r
683 if (st.hasMoreTokens())
\r
685 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
686 colour = ucs.findColour('A');
\r
689 annotation.setThreshold(new GraphLine(value, label, colour));
\r
692 void addGroup(AlignmentI al, StringTokenizer st)
\r
694 SequenceGroup sg = new SequenceGroup();
\r
695 sg.setName(st.nextToken());
\r
696 sg.setStartRes(Integer.parseInt(st.nextToken()) - 1);
\r
697 sg.setEndRes(Integer.parseInt(st.nextToken()) - 1);
\r
699 String index = st.nextToken();
\r
700 if (index.equals("-1"))
\r
702 while (st.hasMoreElements())
\r
704 sg.addSequence(al.findName(st.nextToken()), false);
\r
709 StringTokenizer st2 = new StringTokenizer(index, ",");
\r
711 while (st2.hasMoreTokens())
\r
713 String tmp = st2.nextToken();
\r
714 if (tmp.equals("*"))
\r
716 for (int i = 0; i < al.getHeight(); i++)
\r
718 sg.addSequence(al.getSequenceAt(i), false);
\r
721 else if (tmp.indexOf("-") >= 0)
\r
723 StringTokenizer st3 = new StringTokenizer(tmp, "-");
\r
725 int start = (Integer.parseInt(st3.nextToken()));
\r
726 int end = (Integer.parseInt(st3.nextToken()));
\r
730 for (int i = start; i <= end; i++)
\r
732 sg.addSequence(al.getSequenceAt(i - 1), false);
\r
738 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
\r
745 if (refSeq != null)
\r
747 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
\r
748 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
\r
751 if (sg.getSize() > 0)
\r
757 void addProperties(AlignmentI al, StringTokenizer st)
\r
760 //So far we have only added groups to the annotationHash,
\r
761 //the idea is in the future properties can be added to
\r
762 //alignments, other annotations etc
\r
763 if (al.getGroups() == null)
\r
767 SequenceGroup sg = null;
\r
769 String name = st.nextToken();
\r
771 Vector groups = al.getGroups();
\r
772 for (int i = 0; i < groups.size(); i++)
\r
774 sg = (SequenceGroup) groups.elementAt(i);
\r
775 if (sg.getName().equals(name))
\r
787 String keyValue, key, value;
\r
788 while (st.hasMoreTokens())
\r
790 keyValue = st.nextToken();
\r
791 key = keyValue.substring(0, keyValue.indexOf("="));
\r
792 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
794 if (key.equalsIgnoreCase("description"))
\r
796 sg.setDescription(value);
\r
798 else if (key.equalsIgnoreCase("colour"))
\r
800 sg.cs = ColourSchemeProperty.getColour(al, value);
\r
802 else if (key.equalsIgnoreCase("pidThreshold"))
\r
804 sg.cs.setThreshold(Integer.parseInt(value), true);
\r
807 else if (key.equalsIgnoreCase("consThreshold"))
\r
809 sg.cs.setConservationInc(Integer.parseInt(value));
\r
810 Conservation c = new Conservation("Group",
\r
811 ResidueProperties.propHash, 3,
\r
812 sg.getSequences(null),
\r
814 sg.getEndRes() + 1);
\r
817 c.verdict(false, 25);
\r
819 sg.cs.setConservation(c);
\r
822 else if (key.equalsIgnoreCase("outlineColour"))
\r
824 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
\r
826 else if (key.equalsIgnoreCase("displayBoxes"))
\r
828 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
\r
830 else if (key.equalsIgnoreCase("displayText"))
\r
832 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
\r
834 else if (key.equalsIgnoreCase("colourText"))
\r
836 sg.setColourText(Boolean.valueOf(value).booleanValue());
\r
838 else if (key.equalsIgnoreCase("textCol1"))
\r
840 sg.textColour = new UserColourScheme(value).findColour('A');
\r
842 else if (key.equalsIgnoreCase("textCol2"))
\r
844 sg.textColour2 = new UserColourScheme(value).findColour('A');
\r
846 else if (key.equalsIgnoreCase("textColThreshold"))
\r
848 sg.thresholdTextColour = Integer.parseInt(value);
\r
851 sg.recalcConservation();
\r
856 void setBelowAlignment(AlignmentI al, StringTokenizer st)
\r
859 AlignmentAnnotation aa;
\r
860 while(st.hasMoreTokens())
\r
862 token = st.nextToken();
\r
863 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
865 aa = al.getAlignmentAnnotation()[i];
\r
866 if(aa.sequenceRef==refSeq && aa.label.equals(token))
\r
868 aa.belowAlignment = true;
\r
874 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
\r
876 String keyValue, key, value;
\r
877 while (st.hasMoreTokens())
\r
879 keyValue = st.nextToken();
\r
880 key = keyValue.substring(0, keyValue.indexOf("="));
\r
881 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
882 al.setProperty(key,value);
\r
887 * Write annotations as a CSV file of the form 'label, value, value, ...' for each row.
\r
888 * @param annotations
\r
889 * @return CSV file as a string.
\r
891 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
\r
893 StringBuffer sp = new StringBuffer();
\r
894 for (int i=0; i<annotations.length; i++)
\r
896 String atos = annotations[i].toString();
\r
899 int cp = atos.indexOf("\n", p);
\r
900 sp.append(annotations[i].label);
\r
904 sp.append(atos.substring(p, cp+1));
\r
906 sp.append(atos.substring(p));
\r
912 return sp.toString();
\r