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 * convenience method for pre-2.4 feature files which have no view, hidden columns or hidden row keywords.
\r
38 * @param annotations
\r
41 * @return feature file as a string.
\r
43 public String printAnnotations(AlignmentAnnotation[] annotations,
\r
45 Hashtable properties)
\r
47 return printAnnotations(annotations, groups,
\r
52 * hold all the information about a particular view definition
\r
53 * read from or written out in an annotations file.
\r
55 public class ViewDef {
\r
56 public String viewname;
\r
57 public HiddenSequences hidseqs;
\r
58 public ColumnSelection hiddencols;
\r
59 public Vector visibleGroups;
\r
60 public ViewDef(String viewname, HiddenSequences hidseqs,
\r
61 ColumnSelection hiddencols)
\r
63 this.viewname = viewname;
\r
64 this.hidseqs = hidseqs;
\r
65 this.hiddencols = hiddencols;
\r
68 public String printAnnotations(AlignmentAnnotation[] annotations,
\r
70 Hashtable properties, ViewDef[] views)
\r
72 if (annotations != null)
\r
74 boolean oneColour = true;
\r
75 AlignmentAnnotation row;
\r
77 SequenceI refSeq = null;
\r
79 StringBuffer colours = new StringBuffer();
\r
80 StringBuffer graphLine = new StringBuffer();
\r
82 Hashtable graphGroup = new Hashtable();
\r
84 java.awt.Color color;
\r
86 for (int i = 0; i < annotations.length; i++)
\r
88 row = annotations[i];
\r
90 if (!row.visible && !row.hasScore())
\r
98 if (row.sequenceRef == null)
\r
100 if (refSeq != null)
\r
102 text.append("\nSEQUENCE_REF\tALIGNMENT\n");
\r
108 else if (refSeq == null || refSeq != row.sequenceRef)
\r
110 refSeq = row.sequenceRef;
\r
111 text.append("\nSEQUENCE_REF\t" + refSeq.getName() + "\n");
\r
114 if (row.graph == AlignmentAnnotation.NO_GRAPH)
\r
116 text.append("NO_GRAPH\t");
\r
120 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
122 text.append("BAR_GRAPH\t");
\r
124 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
126 text.append("LINE_GRAPH\t");
\r
129 if (row.getThreshold() != null)
\r
131 graphLine.append("GRAPHLINE\t"
\r
133 + row.getThreshold().value + "\t"
\r
134 + row.getThreshold().label + "\t"
\r
135 + jalview.util.Format.getHexString(
\r
136 row.getThreshold().colour) + "\n"
\r
140 if (row.graphGroup > -1)
\r
142 String key = String.valueOf(row.graphGroup);
\r
143 if (graphGroup.containsKey(key))
\r
145 graphGroup.put(key, graphGroup.get(key)
\r
146 + "\t" + row.label);
\r
150 graphGroup.put(key, row.label);
\r
155 text.append(row.label + "\t");
\r
156 if (row.description != null)
\r
158 text.append(row.description + "\t");
\r
161 for (int j = 0; row.annotations!=null && j < row.annotations.length; j++)
\r
163 if (refSeq != null &&
\r
164 jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
\r
169 if (row.annotations[j] != null)
\r
172 if (row.annotations[j].secondaryStructure != ' ')
\r
174 text.append(comma + row.annotations[j].secondaryStructure);
\r
177 if (row.annotations[j].displayCharacter!=null
\r
178 && row.annotations[j].displayCharacter.length() > 0
\r
179 && !row.annotations[j].displayCharacter.equals(" "))
\r
181 text.append(comma + row.annotations[j].displayCharacter);
\r
185 if (row.annotations[j] != null)
\r
187 if(color!=null && !color.equals(row.annotations[j].colour))
\r
192 color = row.annotations[j].colour;
\r
193 if (row.annotations[j].value != 0f)
\r
195 text.append(comma + row.annotations[j].value);
\r
199 if(row.annotations[j].colour!=null
\r
200 && row.annotations[j].colour!=java.awt.Color.black)
\r
202 text.append(comma+"["+
\r
203 jalview.util.Format.getHexString(
\r
204 row.annotations[j].colour)+"]");
\r
211 text.append("\t"+row.score);
\r
215 if (color != null && color != java.awt.Color.black && oneColour)
\r
217 colours.append("COLOUR\t"
\r
219 + jalview.util.Format.getHexString(color) + "\n");
\r
226 text.append(colours.toString());
\r
227 text.append(graphLine.toString());
\r
228 if (graphGroup.size() > 0)
\r
230 text.append("COMBINE\t");
\r
231 Enumeration en = graphGroup.elements();
\r
232 while (en.hasMoreElements())
\r
234 text.append(en.nextElement() + "\n");
\r
239 if (groups != null)
\r
241 printGroups(groups);
\r
244 if(properties!=null)
\r
246 text.append("\n\nALIGNMENT");
\r
247 Enumeration en = properties.keys();
\r
248 while(en.hasMoreElements())
\r
250 String key = en.nextElement().toString();
\r
251 text.append("\t"+key+"="+properties.get(key));
\r
256 return text.toString();
\r
259 public void printGroups(Vector sequenceGroups)
\r
262 for (int i = 0; i < sequenceGroups.size(); i++)
\r
264 sg = (SequenceGroup) sequenceGroups.elementAt(i);
\r
265 text.append("SEQUENCE_GROUP\t"
\r
266 + sg.getName() + "\t"
\r
267 + (sg.getStartRes() + 1) + "\t"
\r
268 + (sg.getEndRes() + 1) + "\t" + "-1\t");
\r
269 for (int s = 0; s < sg.getSize(); s++)
\r
271 text.append(sg.getSequenceAt(s).getName() + "\t");
\r
274 text.append("\nPROPERTIES\t" + sg.getName() + "\t");
\r
276 if (sg.getDescription() != null)
\r
278 text.append("description=" + sg.getDescription() + "\t");
\r
282 text.append("colour=" + ColourSchemeProperty.getColourName(sg.cs) +
\r
284 if (sg.cs.getThreshold() != 0)
\r
286 text.append("pidThreshold=" + sg.cs.getThreshold());
\r
288 if (sg.cs.conservationApplied())
\r
290 text.append("consThreshold=" + sg.cs.getConservationInc() + "\t");
\r
293 text.append("outlineColour=" +
\r
294 jalview.util.Format.getHexString(sg.getOutlineColour()) +
\r
297 text.append("displayBoxes=" + sg.getDisplayBoxes() + "\t");
\r
298 text.append("displayText=" + sg.getDisplayText() + "\t");
\r
299 text.append("colourText=" + sg.getColourText() + "\t");
\r
301 if (sg.textColour != java.awt.Color.black)
\r
303 text.append("textCol1=" +
\r
304 jalview.util.Format.getHexString(sg.textColour) + "\t");
\r
306 if (sg.textColour2 != java.awt.Color.white)
\r
308 text.append("textCol2=" +
\r
309 jalview.util.Format.getHexString(sg.textColour2) + "\t");
\r
311 if (sg.thresholdTextColour != 0)
\r
313 text.append("textColThreshold=" + sg.thresholdTextColour);
\r
315 if (sg.idColour!=null)
\r
317 text.append("idColour="+jalview.util.Format.getHexString(sg.idColour)+"\t");
\r
320 text.append("\n\n");
\r
325 SequenceI refSeq = null;
\r
326 String refSeqId = null;
\r
327 public boolean readAnnotationFile(AlignmentI al,
\r
333 BufferedReader in = null;
\r
334 if (protocol.equals(AppletFormatAdapter.FILE))
\r
336 in = new BufferedReader(new FileReader(file));
\r
338 else if (protocol.equals(AppletFormatAdapter.URL))
\r
340 URL url = new URL(file);
\r
341 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
343 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
345 in = new BufferedReader(new StringReader(file));
\r
347 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
349 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
352 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
356 String line, label, description, token;
\r
357 int graphStyle, index;
\r
358 int refSeqIndex = 1;
\r
359 int existingAnnotations = 0;
\r
360 if (al.getAlignmentAnnotation() != null)
\r
362 existingAnnotations = al.getAlignmentAnnotation().length;
\r
365 int alWidth = al.getWidth();
\r
367 StringTokenizer st;
\r
368 Annotation[] annotations;
\r
369 AlignmentAnnotation annotation = null;
\r
371 // First confirm this is an Annotation file
\r
372 boolean jvAnnotationFile = false;
\r
373 while ( (line = in.readLine()) != null)
\r
375 if (line.indexOf("#") == 0)
\r
380 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
382 jvAnnotationFile = true;
\r
387 if (!jvAnnotationFile)
\r
393 while ( (line = in.readLine()) != null)
\r
395 if (line.indexOf("#") == 0
\r
396 || line.indexOf("JALVIEW_ANNOTATION") > -1
\r
397 || line.length() == 0)
\r
402 st = new StringTokenizer(line, "\t");
\r
403 token = st.nextToken();
\r
404 if (token.equalsIgnoreCase("COLOUR"))
\r
406 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
410 else if (token.equalsIgnoreCase("COMBINE"))
\r
412 combineAnnotations(al, st);
\r
416 else if (token.equalsIgnoreCase("GRAPHLINE"))
\r
422 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
\r
424 if (st.hasMoreTokens())
\r
426 refSeq = al.findName(refSeqId=st.nextToken());
\r
433 refSeqIndex = Integer.parseInt(st.nextToken());
\r
434 if (refSeqIndex < 1)
\r
437 System.out.println(
\r
438 "WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
441 catch (Exception ex)
\r
452 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
\r
458 else if (token.equalsIgnoreCase("PROPERTIES"))
\r
460 addProperties(al, st);
\r
464 else if( token.equalsIgnoreCase("BELOW_ALIGNMENT"))
\r
466 setBelowAlignment(al, st);
\r
469 else if( token.equalsIgnoreCase("ALIGNMENT"))
\r
471 addAlignmentDetails(al, st);
\r
475 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
476 label = st.nextToken();
\r
480 annotations = new Annotation[alWidth];
\r
481 description = null;
\r
482 float score = Float.NaN;
\r
484 if(st.hasMoreTokens())
\r
486 line = st.nextToken();
\r
488 if (line.indexOf("|") ==-1)
\r
490 description = line;
\r
491 if (st.hasMoreTokens())
\r
492 line = st.nextToken();
\r
495 if(st.hasMoreTokens())
\r
497 //This must be the score
\r
498 score = Float.valueOf(st.nextToken()).floatValue();
\r
501 st = new StringTokenizer(line, "|", true);
\r
504 boolean emptyColumn = true;
\r
505 boolean onlyOneElement = (st.countTokens()==1);
\r
507 while (st.hasMoreElements() && index < alWidth)
\r
509 token = st.nextToken().trim();
\r
515 score = Float.valueOf(token).floatValue();
\r
518 catch(NumberFormatException ex){}
\r
521 if (token.equals("|"))
\r
528 emptyColumn = true;
\r
532 annotations[index++] = parseAnnotation(token);
\r
533 emptyColumn = false;
\r
539 annotation = new AlignmentAnnotation(label,
\r
541 (index==0) ? null : annotations,
\r
546 annotation.score = score;
\r
548 if (refSeq != null)
\r
550 annotation.belowAlignment=false;
\r
552 // copy before we do any mapping business.
\r
553 // TODO: verify that undo/redo with 1:many sequence associated annotations can be undone correctly
\r
554 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
\r
555 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
556 annotation.adjustForAlignment();
\r
557 refSeq.addAlignmentAnnotation(annotation);
\r
558 al.addAnnotation(annotation);
\r
559 al.setAnnotationIndex(annotation,
\r
560 al.getAlignmentAnnotation().length - existingAnnotations -
\r
562 // and recover our virgin copy to use again if necessary.
\r
565 } while (refSeqId!=null && (refSeq=al.findName(refSeq, refSeqId, true))!=null);
\r
567 al.addAnnotation(annotation);
\r
568 al.setAnnotationIndex(annotation,
\r
569 al.getAlignmentAnnotation().length - existingAnnotations -
\r
575 catch (Exception ex)
\r
577 ex.printStackTrace();
\r
578 System.out.println("Problem reading annotation file: " + ex);
\r
584 Annotation parseAnnotation(String string)
\r
586 String desc = null, displayChar = null;
\r
587 char ss = ' '; // secondaryStructure
\r
589 boolean parsedValue = false;
\r
592 java.awt.Color colour = null;
\r
593 int i=string.indexOf("[");
\r
594 int j=string.indexOf("]");
\r
597 UserColourScheme ucs = new UserColourScheme();
\r
599 colour = ucs.getColourFromString(string.substring(i+1,j));
\r
601 string = string.substring(0,i)+string.substring(j+1);
\r
604 StringTokenizer st = new StringTokenizer(string, ",");
\r
606 while (st.hasMoreTokens())
\r
608 token = st.nextToken().trim();
\r
609 if (token.length() == 0)
\r
618 displayChar = token;
\r
619 value = new Float(token).floatValue();
\r
620 parsedValue = true;
\r
623 catch (NumberFormatException ex)
\r
627 if (token.equals("H") || token.equals("E"))
\r
629 // Either this character represents a helix or sheet
\r
630 // or an integer which can be displayed
\r
631 ss = token.charAt(0);
\r
632 if (displayChar.equals(token.substring(0, 1)))
\r
637 else if (desc == null)
\r
644 if (displayChar!=null
\r
645 && displayChar.length() > 1
\r
647 && desc.length() == 1)
\r
649 String tmp = displayChar;
\r
650 displayChar = desc;
\r
654 * In principle, this code will ensure that the Annotation element generated is renderable by any of the applet or application rendering code
\r
655 * but instead we check for null strings when the display character is rendered.
\r
656 if (displayChar==null)
\r
661 Annotation anot = new Annotation(displayChar, desc, ss, value);
\r
663 anot.colour = colour;
\r
668 void colourAnnotations(AlignmentI al, String label, String colour)
\r
670 UserColourScheme ucs = new UserColourScheme(colour);
\r
671 Annotation[] annotations;
\r
672 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
674 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
676 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
677 for (int j = 0; j < annotations.length; j++)
\r
679 if (annotations[j] != null)
\r
681 annotations[j].colour = ucs.findColour('A');
\r
688 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
690 int graphGroup = -1;
\r
691 String group = st.nextToken();
\r
692 //First make sure we are not overwriting the graphIndex
\r
693 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
695 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
697 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
\r
698 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
703 //Now update groups
\r
704 while (st.hasMoreTokens())
\r
706 group = st.nextToken();
\r
707 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
709 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
711 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
718 void addLine(AlignmentI al, StringTokenizer st)
\r
720 String group = st.nextToken();
\r
721 AlignmentAnnotation annotation = null;
\r
723 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
725 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
727 annotation = al.getAlignmentAnnotation()[i];
\r
732 if (annotation == null)
\r
736 float value = new Float(st.nextToken()).floatValue();
\r
737 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
738 java.awt.Color colour = null;
\r
739 if (st.hasMoreTokens())
\r
741 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
742 colour = ucs.findColour('A');
\r
745 annotation.setThreshold(new GraphLine(value, label, colour));
\r
748 void addGroup(AlignmentI al, StringTokenizer st)
\r
750 SequenceGroup sg = new SequenceGroup();
\r
751 sg.setName(st.nextToken());
\r
752 sg.setStartRes(Integer.parseInt(st.nextToken()) - 1);
\r
753 sg.setEndRes(Integer.parseInt(st.nextToken()) - 1);
\r
755 String index = st.nextToken();
\r
756 if (index.equals("-1"))
\r
758 while (st.hasMoreElements())
\r
760 sg.addSequence(al.findName(st.nextToken()), false);
\r
765 StringTokenizer st2 = new StringTokenizer(index, ",");
\r
767 while (st2.hasMoreTokens())
\r
769 String tmp = st2.nextToken();
\r
770 if (tmp.equals("*"))
\r
772 for (int i = 0; i < al.getHeight(); i++)
\r
774 sg.addSequence(al.getSequenceAt(i), false);
\r
777 else if (tmp.indexOf("-") >= 0)
\r
779 StringTokenizer st3 = new StringTokenizer(tmp, "-");
\r
781 int start = (Integer.parseInt(st3.nextToken()));
\r
782 int end = (Integer.parseInt(st3.nextToken()));
\r
786 for (int i = start; i <= end; i++)
\r
788 sg.addSequence(al.getSequenceAt(i - 1), false);
\r
794 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
\r
801 if (refSeq != null)
\r
803 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
\r
804 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
\r
807 if (sg.getSize() > 0)
\r
813 void addProperties(AlignmentI al, StringTokenizer st)
\r
816 //So far we have only added groups to the annotationHash,
\r
817 //the idea is in the future properties can be added to
\r
818 //alignments, other annotations etc
\r
819 if (al.getGroups() == null)
\r
823 SequenceGroup sg = null;
\r
825 String name = st.nextToken();
\r
827 Vector groups = al.getGroups();
\r
828 for (int i = 0; i < groups.size(); i++)
\r
830 sg = (SequenceGroup) groups.elementAt(i);
\r
831 if (sg.getName().equals(name))
\r
843 String keyValue, key, value;
\r
844 ColourSchemeI def = sg.cs;
\r
846 while (st.hasMoreTokens())
\r
848 keyValue = st.nextToken();
\r
849 key = keyValue.substring(0, keyValue.indexOf("="));
\r
850 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
852 if (key.equalsIgnoreCase("description"))
\r
854 sg.setDescription(value);
\r
856 else if (key.equalsIgnoreCase("colour"))
\r
858 sg.cs = ColourSchemeProperty.getColour(al, value);
\r
860 else if (key.equalsIgnoreCase("pidThreshold"))
\r
862 sg.cs.setThreshold(Integer.parseInt(value), true);
\r
865 else if (key.equalsIgnoreCase("consThreshold"))
\r
867 sg.cs.setConservationInc(Integer.parseInt(value));
\r
868 Conservation c = new Conservation("Group",
\r
869 ResidueProperties.propHash, 3,
\r
870 sg.getSequences(null),
\r
872 sg.getEndRes() + 1);
\r
875 c.verdict(false, 25);
\r
877 sg.cs.setConservation(c);
\r
880 else if (key.equalsIgnoreCase("outlineColour"))
\r
882 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
\r
884 else if (key.equalsIgnoreCase("displayBoxes"))
\r
886 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
\r
888 else if (key.equalsIgnoreCase("displayText"))
\r
890 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
\r
892 else if (key.equalsIgnoreCase("colourText"))
\r
894 sg.setColourText(Boolean.valueOf(value).booleanValue());
\r
896 else if (key.equalsIgnoreCase("textCol1"))
\r
898 sg.textColour = new UserColourScheme(value).findColour('A');
\r
900 else if (key.equalsIgnoreCase("textCol2"))
\r
902 sg.textColour2 = new UserColourScheme(value).findColour('A');
\r
904 else if (key.equalsIgnoreCase("textColThreshold"))
\r
906 sg.thresholdTextColour = Integer.parseInt(value);
\r
908 else if (key.equalsIgnoreCase("idColour"))
\r
910 // consider warning if colour doesn't resolve to a real colour
\r
911 sg.setIdColour((def = new UserColourScheme(value)).findColour('A'));
\r
913 sg.recalcConservation();
\r
922 void setBelowAlignment(AlignmentI al, StringTokenizer st)
\r
925 AlignmentAnnotation aa;
\r
926 while(st.hasMoreTokens())
\r
928 token = st.nextToken();
\r
929 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
931 aa = al.getAlignmentAnnotation()[i];
\r
932 if(aa.sequenceRef==refSeq && aa.label.equals(token))
\r
934 aa.belowAlignment = true;
\r
940 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
\r
942 String keyValue, key, value;
\r
943 while (st.hasMoreTokens())
\r
945 keyValue = st.nextToken();
\r
946 key = keyValue.substring(0, keyValue.indexOf("="));
\r
947 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
948 al.setProperty(key,value);
\r
953 * Write annotations as a CSV file of the form 'label, value, value, ...' for each row.
\r
954 * @param annotations
\r
955 * @return CSV file as a string.
\r
957 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
\r
959 StringBuffer sp = new StringBuffer();
\r
960 for (int i=0; i<annotations.length; i++)
\r
962 String atos = annotations[i].toString();
\r
965 int cp = atos.indexOf("\n", p);
\r
966 sp.append(annotations[i].label);
\r
970 sp.append(atos.substring(p, cp+1));
\r
972 sp.append(atos.substring(p));
\r
978 return sp.toString();
\r