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
316 text.append("\n\n");
\r
321 SequenceI refSeq = null;
\r
322 public boolean readAnnotationFile(AlignmentI al,
\r
328 BufferedReader in = null;
\r
329 if (protocol.equals(AppletFormatAdapter.FILE))
\r
331 in = new BufferedReader(new FileReader(file));
\r
333 else if (protocol.equals(AppletFormatAdapter.URL))
\r
335 URL url = new URL(file);
\r
336 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
338 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
340 in = new BufferedReader(new StringReader(file));
\r
342 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
344 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
347 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
351 String line, label, description, token;
\r
352 int graphStyle, index;
\r
353 int refSeqIndex = 1;
\r
354 int existingAnnotations = 0;
\r
355 if (al.getAlignmentAnnotation() != null)
\r
357 existingAnnotations = al.getAlignmentAnnotation().length;
\r
360 int alWidth = al.getWidth();
\r
362 StringTokenizer st;
\r
363 Annotation[] annotations;
\r
364 AlignmentAnnotation annotation = null;
\r
366 // First confirm this is an Annotation file
\r
367 boolean jvAnnotationFile = false;
\r
368 while ( (line = in.readLine()) != null)
\r
370 if (line.indexOf("#") == 0)
\r
375 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
377 jvAnnotationFile = true;
\r
382 if (!jvAnnotationFile)
\r
388 while ( (line = in.readLine()) != null)
\r
390 if (line.indexOf("#") == 0
\r
391 || line.indexOf("JALVIEW_ANNOTATION") > -1
\r
392 || line.length() == 0)
\r
397 st = new StringTokenizer(line, "\t");
\r
398 token = st.nextToken();
\r
399 if (token.equalsIgnoreCase("COLOUR"))
\r
401 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
405 else if (token.equalsIgnoreCase("COMBINE"))
\r
407 combineAnnotations(al, st);
\r
411 else if (token.equalsIgnoreCase("GRAPHLINE"))
\r
417 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
\r
419 refSeq = al.findName(st.nextToken());
\r
422 refSeqIndex = Integer.parseInt(st.nextToken());
\r
423 if (refSeqIndex < 1)
\r
426 System.out.println(
\r
427 "WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
430 catch (Exception ex)
\r
438 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
\r
444 else if (token.equalsIgnoreCase("PROPERTIES"))
\r
446 addProperties(al, st);
\r
450 else if( token.equalsIgnoreCase("BELOW_ALIGNMENT"))
\r
452 setBelowAlignment(al, st);
\r
455 else if( token.equalsIgnoreCase("ALIGNMENT"))
\r
457 addAlignmentDetails(al, st);
\r
461 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
462 label = st.nextToken();
\r
466 annotations = new Annotation[alWidth];
\r
467 description = null;
\r
468 float score = Float.NaN;
\r
470 if(st.hasMoreTokens())
\r
472 line = st.nextToken();
\r
474 if (line.indexOf("|") ==-1)
\r
476 description = line;
\r
477 if (st.hasMoreTokens())
\r
478 line = st.nextToken();
\r
481 if(st.hasMoreTokens())
\r
483 //This must be the score
\r
484 score = Float.valueOf(st.nextToken()).floatValue();
\r
487 st = new StringTokenizer(line, "|", true);
\r
490 boolean emptyColumn = true;
\r
491 boolean onlyOneElement = (st.countTokens()==1);
\r
493 while (st.hasMoreElements() && index < alWidth)
\r
495 token = st.nextToken().trim();
\r
501 score = Float.valueOf(token).floatValue();
\r
504 catch(NumberFormatException ex){}
\r
507 if (token.equals("|"))
\r
514 emptyColumn = true;
\r
518 annotations[index++] = parseAnnotation(token);
\r
519 emptyColumn = false;
\r
525 annotation = new AlignmentAnnotation(label,
\r
527 (index==0) ? null : annotations,
\r
532 annotation.score = score;
\r
534 if (refSeq != null)
\r
536 annotation.belowAlignment=false;
\r
537 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
538 annotation.adjustForAlignment();
\r
539 refSeq.addAlignmentAnnotation(annotation);
\r
542 al.addAnnotation(annotation);
\r
544 al.setAnnotationIndex(annotation,
\r
545 al.getAlignmentAnnotation().length - existingAnnotations -
\r
550 catch (Exception ex)
\r
552 ex.printStackTrace();
\r
553 System.out.println("Problem reading annotation file: " + ex);
\r
559 Annotation parseAnnotation(String string)
\r
561 String desc = null, displayChar = null;
\r
562 char ss = ' '; // secondaryStructure
\r
564 boolean parsedValue = false;
\r
567 java.awt.Color colour = null;
\r
568 int i=string.indexOf("[");
\r
569 int j=string.indexOf("]");
\r
572 UserColourScheme ucs = new UserColourScheme();
\r
574 colour = ucs.getColourFromString(string.substring(i+1,j));
\r
576 string = string.substring(0,i)+string.substring(j+1);
\r
579 StringTokenizer st = new StringTokenizer(string, ",");
\r
581 while (st.hasMoreTokens())
\r
583 token = st.nextToken().trim();
\r
584 if (token.length() == 0)
\r
593 displayChar = token;
\r
594 value = new Float(token).floatValue();
\r
595 parsedValue = true;
\r
598 catch (NumberFormatException ex)
\r
602 if (token.equals("H") || token.equals("E"))
\r
604 // Either this character represents a helix or sheet
\r
605 // or an integer which can be displayed
\r
606 ss = token.charAt(0);
\r
607 if (displayChar.equals(token.substring(0, 1)))
\r
612 else if (desc == null)
\r
619 if (displayChar!=null
\r
620 && displayChar.length() > 1
\r
622 && desc.length() == 1)
\r
624 String tmp = displayChar;
\r
625 displayChar = desc;
\r
629 * In principle, this code will ensure that the Annotation element generated is renderable by any of the applet or application rendering code
\r
630 * but instead we check for null strings when the display character is rendered.
\r
631 if (displayChar==null)
\r
636 Annotation anot = new Annotation(displayChar, desc, ss, value);
\r
638 anot.colour = colour;
\r
643 void colourAnnotations(AlignmentI al, String label, String colour)
\r
645 UserColourScheme ucs = new UserColourScheme(colour);
\r
646 Annotation[] annotations;
\r
647 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
649 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
651 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
652 for (int j = 0; j < annotations.length; j++)
\r
654 if (annotations[j] != null)
\r
656 annotations[j].colour = ucs.findColour('A');
\r
663 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
665 int graphGroup = -1;
\r
666 String group = st.nextToken();
\r
667 //First make sure we are not overwriting the graphIndex
\r
668 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
670 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
672 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
\r
673 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
678 //Now update groups
\r
679 while (st.hasMoreTokens())
\r
681 group = st.nextToken();
\r
682 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
684 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
686 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
693 void addLine(AlignmentI al, StringTokenizer st)
\r
695 String group = st.nextToken();
\r
696 AlignmentAnnotation annotation = null;
\r
698 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
700 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
702 annotation = al.getAlignmentAnnotation()[i];
\r
707 if (annotation == null)
\r
711 float value = new Float(st.nextToken()).floatValue();
\r
712 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
713 java.awt.Color colour = null;
\r
714 if (st.hasMoreTokens())
\r
716 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
717 colour = ucs.findColour('A');
\r
720 annotation.setThreshold(new GraphLine(value, label, colour));
\r
723 void addGroup(AlignmentI al, StringTokenizer st)
\r
725 SequenceGroup sg = new SequenceGroup();
\r
726 sg.setName(st.nextToken());
\r
727 sg.setStartRes(Integer.parseInt(st.nextToken()) - 1);
\r
728 sg.setEndRes(Integer.parseInt(st.nextToken()) - 1);
\r
730 String index = st.nextToken();
\r
731 if (index.equals("-1"))
\r
733 while (st.hasMoreElements())
\r
735 sg.addSequence(al.findName(st.nextToken()), false);
\r
740 StringTokenizer st2 = new StringTokenizer(index, ",");
\r
742 while (st2.hasMoreTokens())
\r
744 String tmp = st2.nextToken();
\r
745 if (tmp.equals("*"))
\r
747 for (int i = 0; i < al.getHeight(); i++)
\r
749 sg.addSequence(al.getSequenceAt(i), false);
\r
752 else if (tmp.indexOf("-") >= 0)
\r
754 StringTokenizer st3 = new StringTokenizer(tmp, "-");
\r
756 int start = (Integer.parseInt(st3.nextToken()));
\r
757 int end = (Integer.parseInt(st3.nextToken()));
\r
761 for (int i = start; i <= end; i++)
\r
763 sg.addSequence(al.getSequenceAt(i - 1), false);
\r
769 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
\r
776 if (refSeq != null)
\r
778 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
\r
779 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
\r
782 if (sg.getSize() > 0)
\r
788 void addProperties(AlignmentI al, StringTokenizer st)
\r
791 //So far we have only added groups to the annotationHash,
\r
792 //the idea is in the future properties can be added to
\r
793 //alignments, other annotations etc
\r
794 if (al.getGroups() == null)
\r
798 SequenceGroup sg = null;
\r
800 String name = st.nextToken();
\r
802 Vector groups = al.getGroups();
\r
803 for (int i = 0; i < groups.size(); i++)
\r
805 sg = (SequenceGroup) groups.elementAt(i);
\r
806 if (sg.getName().equals(name))
\r
818 String keyValue, key, value;
\r
819 while (st.hasMoreTokens())
\r
821 keyValue = st.nextToken();
\r
822 key = keyValue.substring(0, keyValue.indexOf("="));
\r
823 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
825 if (key.equalsIgnoreCase("description"))
\r
827 sg.setDescription(value);
\r
829 else if (key.equalsIgnoreCase("colour"))
\r
831 sg.cs = ColourSchemeProperty.getColour(al, value);
\r
833 else if (key.equalsIgnoreCase("pidThreshold"))
\r
835 sg.cs.setThreshold(Integer.parseInt(value), true);
\r
838 else if (key.equalsIgnoreCase("consThreshold"))
\r
840 sg.cs.setConservationInc(Integer.parseInt(value));
\r
841 Conservation c = new Conservation("Group",
\r
842 ResidueProperties.propHash, 3,
\r
843 sg.getSequences(null),
\r
845 sg.getEndRes() + 1);
\r
848 c.verdict(false, 25);
\r
850 sg.cs.setConservation(c);
\r
853 else if (key.equalsIgnoreCase("outlineColour"))
\r
855 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
\r
857 else if (key.equalsIgnoreCase("displayBoxes"))
\r
859 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
\r
861 else if (key.equalsIgnoreCase("displayText"))
\r
863 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
\r
865 else if (key.equalsIgnoreCase("colourText"))
\r
867 sg.setColourText(Boolean.valueOf(value).booleanValue());
\r
869 else if (key.equalsIgnoreCase("textCol1"))
\r
871 sg.textColour = new UserColourScheme(value).findColour('A');
\r
873 else if (key.equalsIgnoreCase("textCol2"))
\r
875 sg.textColour2 = new UserColourScheme(value).findColour('A');
\r
877 else if (key.equalsIgnoreCase("textColThreshold"))
\r
879 sg.thresholdTextColour = Integer.parseInt(value);
\r
882 sg.recalcConservation();
\r
887 void setBelowAlignment(AlignmentI al, StringTokenizer st)
\r
890 AlignmentAnnotation aa;
\r
891 while(st.hasMoreTokens())
\r
893 token = st.nextToken();
\r
894 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
896 aa = al.getAlignmentAnnotation()[i];
\r
897 if(aa.sequenceRef==refSeq && aa.label.equals(token))
\r
899 aa.belowAlignment = true;
\r
905 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
\r
907 String keyValue, key, value;
\r
908 while (st.hasMoreTokens())
\r
910 keyValue = st.nextToken();
\r
911 key = keyValue.substring(0, keyValue.indexOf("="));
\r
912 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
913 al.setProperty(key,value);
\r
918 * Write annotations as a CSV file of the form 'label, value, value, ...' for each row.
\r
919 * @param annotations
\r
920 * @return CSV file as a string.
\r
922 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
\r
924 StringBuffer sp = new StringBuffer();
\r
925 for (int i=0; i<annotations.length; i++)
\r
927 String atos = annotations[i].toString();
\r
930 int cp = atos.indexOf("\n", p);
\r
931 sp.append(annotations[i].label);
\r
935 sp.append(atos.substring(p, cp+1));
\r
937 sp.append(atos.substring(p));
\r
943 return sp.toString();
\r