2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
27 import jalview.analysis.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
31 public class AnnotationFile
33 public AnnotationFile()
39 * character used to write newlines
41 protected String newline = System.getProperty("line.separator");
44 * set new line string and reset the output buffer
48 public void setNewlineString(String nl)
54 public String getNewlineString()
63 text = new StringBuffer("JALVIEW_ANNOTATION" + newline + "# Created: "
64 + new java.util.Date() + newline + newline);
70 * convenience method for pre-2.4 feature files which have no view, hidden
71 * columns or hidden row keywords.
76 * @return feature file as a string.
78 public String printAnnotations(AlignmentAnnotation[] annotations,
79 List<SequenceGroup> list, Hashtable properties)
81 return printAnnotations(annotations, list, properties, null);
86 * hold all the information about a particular view definition read from or
87 * written out in an annotations file.
91 public String viewname;
93 public HiddenSequences hidseqs;
95 public ColumnSelection hiddencols;
97 public Vector visibleGroups;
99 public Hashtable hiddenRepSeqs;
101 public ViewDef(String viewname, HiddenSequences hidseqs,
102 ColumnSelection hiddencols, Hashtable hiddenRepSeqs)
104 this.viewname = viewname;
105 this.hidseqs = hidseqs;
106 this.hiddencols = hiddencols;
107 this.hiddenRepSeqs = hiddenRepSeqs;
112 * Prepare an annotation file given a set of annotations, groups, alignment
113 * properties and views.
119 * @return annotation file
121 public String printAnnotations(AlignmentAnnotation[] annotations,
122 List<SequenceGroup> list, Hashtable properties, ViewDef[] views)
124 // TODO: resolve views issue : annotationFile could contain visible region,
125 // or full data + hidden region specifications for a view.
126 if (annotations != null)
128 boolean oneColour = true;
129 AlignmentAnnotation row;
131 SequenceI refSeq = null;
132 SequenceGroup refGroup = null;
134 StringBuffer colours = new StringBuffer();
135 StringBuffer graphLine = new StringBuffer();
136 StringBuffer rowprops = new StringBuffer();
137 Hashtable<Integer,String> graphGroup = new Hashtable<Integer,String>();
138 Hashtable<Integer, Object[]> graphGroup_refs = new Hashtable<Integer,Object[]>();
139 BitSet graphGroupSeen = new BitSet();
141 java.awt.Color color;
143 for (int i = 0; i < annotations.length; i++)
145 row = annotations[i];
147 if (!row.visible && !row.hasScore() && !(row.graphGroup>-1 && graphGroupSeen.get(row.graphGroup)))
155 // mark any sequence references for the row
156 writeSequence_Ref(refSeq ,row.sequenceRef);
157 refSeq = row.sequenceRef;
158 // mark any group references for the row
159 writeGroup_Ref(refGroup, row.groupRef);
160 refGroup = row.groupRef;
162 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
163 // lookahead to check what the annotation row object actually contains.
164 for (int j = 0; row.annotations != null
165 && j < row.annotations.length
166 && (!hasGlyphs || !hasLabels || !hasValues); j++)
168 if (row.annotations[j] != null)
170 hasLabels |= (row.annotations[j].displayCharacter != null
171 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
173 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
174 hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't
177 hasText |= (row.annotations[j].description != null && row.annotations[j].description
182 if (row.graph == AlignmentAnnotation.NO_GRAPH)
184 text.append("NO_GRAPH\t");
185 hasValues = false; // only secondary structure
186 // hasLabels = false; // and annotation description string.
190 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
192 text.append("BAR_GRAPH\t");
193 hasGlyphs = false; // no secondary structure
196 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
198 hasGlyphs = false; // no secondary structure
199 text.append("LINE_GRAPH\t");
202 if (row.getThreshold() != null)
204 graphLine.append("GRAPHLINE\t");
205 graphLine.append(row.label);
206 graphLine.append("\t");
207 graphLine.append(row.getThreshold().value);
208 graphLine.append("\t");
209 graphLine.append(row.getThreshold().label);
210 graphLine.append("\t");
211 graphLine.append(jalview.util.Format.getHexString(row
212 .getThreshold().colour));
213 graphLine.append(newline);
216 if (row.graphGroup > -1)
218 graphGroupSeen.set(row.graphGroup);
219 Integer key = new Integer(row.graphGroup);
220 if (graphGroup.containsKey(key))
222 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
227 graphGroup_refs.put(key, new Object[] { refSeq, refGroup});
228 graphGroup.put(key, row.label);
233 text.append(row.label + "\t");
234 if (row.description != null)
236 text.append(row.description + "\t");
238 for (int j = 0; row.annotations != null
239 && j < row.annotations.length; j++)
242 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
247 if (row.annotations[j] != null)
250 if (hasGlyphs) // could be also hasGlyphs || ...
254 if (row.annotations[j].secondaryStructure != ' ')
256 // only write out the field if its not whitespace.
257 text.append(row.annotations[j].secondaryStructure);
263 if (row.annotations[j].value != Float.NaN)
265 text.append(comma + row.annotations[j].value);
269 System.err.println("Skipping NaN - not valid value.");
270 text.append(comma + 0f);// row.annotations[j].value);
276 // TODO: labels are emitted after values for bar graphs.
277 if // empty labels are allowed, so
278 (row.annotations[j].displayCharacter != null
279 && row.annotations[j].displayCharacter.length() > 0
280 && !row.annotations[j].displayCharacter.equals(" "))
282 text.append(comma + row.annotations[j].displayCharacter);
288 if (row.annotations[j].description != null
289 && row.annotations[j].description.length() > 0
290 && !row.annotations[j].description
291 .equals(row.annotations[j].displayCharacter))
293 text.append(comma + row.annotations[j].description);
297 if (color != null && !color.equals(row.annotations[j].colour))
302 color = row.annotations[j].colour;
304 if (row.annotations[j].colour != null
305 && row.annotations[j].colour != java.awt.Color.black)
309 + jalview.util.Format
310 .getHexString(row.annotations[j].colour)
319 text.append("\t" + row.score);
321 text.append(newline);
323 if (color != null && color != java.awt.Color.black && oneColour)
325 colours.append("COLOUR\t");
326 colours.append(row.label);
327 colours.append("\t");
328 colours.append(jalview.util.Format.getHexString(color));
329 colours.append(newline);
331 if (row.scaleColLabel || row.showAllColLabels
332 || row.centreColLabels)
334 rowprops.append("ROWPROPERTIES\t");
335 rowprops.append(row.label);
336 rowprops.append("\tscaletofit=");
337 rowprops.append(row.scaleColLabel);
338 rowprops.append("\tshowalllabs=");
339 rowprops.append(row.showAllColLabels);
340 rowprops.append("\tcentrelabs=");
341 rowprops.append(row.centreColLabels);
342 rowprops.append(newline);
344 if (graphLine.length()>0) {
345 text.append(graphLine.toString());
346 graphLine.setLength(0);
350 text.append(newline);
352 text.append(colours.toString());
353 if (graphGroup.size() > 0)
355 SequenceI oldRefSeq = refSeq;
356 SequenceGroup oldRefGroup = refGroup;
357 for (Map.Entry<Integer, String> combine_statement:graphGroup.entrySet())
359 Object[] seqRefAndGroup=graphGroup_refs.get(combine_statement.getKey());
361 writeSequence_Ref(refSeq, (SequenceI)seqRefAndGroup[0]);
362 refSeq = (SequenceI)seqRefAndGroup[0];
364 writeGroup_Ref(refGroup, (SequenceGroup)seqRefAndGroup[1]);
365 refGroup = (SequenceGroup)seqRefAndGroup[1];
366 text.append("COMBINE\t");
367 text.append(combine_statement.getValue());
368 text.append(newline);
370 writeSequence_Ref(refSeq, oldRefSeq);
373 writeGroup_Ref(refGroup, oldRefGroup);
374 refGroup = oldRefGroup;
376 text.append(rowprops.toString());
384 if (properties != null)
386 text.append(newline);
387 text.append(newline);
388 text.append("ALIGNMENT");
389 Enumeration en = properties.keys();
390 while (en.hasMoreElements())
392 String key = en.nextElement().toString();
396 text.append(properties.get(key));
398 // TODO: output alignment visualization settings here if required
402 return text.toString();
405 private Object writeGroup_Ref(SequenceGroup refGroup, SequenceGroup next_refGroup)
407 if (next_refGroup == null)
410 if (refGroup != null)
412 text.append(newline);
413 text.append("GROUP_REF\t");
414 text.append("ALIGNMENT");
415 text.append(newline);
421 if (refGroup == null || refGroup != next_refGroup)
423 text.append(newline);
424 text.append("GROUP_REF\t");
425 text.append(next_refGroup.getName());
426 text.append(newline);
433 private boolean writeSequence_Ref(SequenceI refSeq, SequenceI next_refSeq)
436 if (next_refSeq==null)
440 text.append(newline);
441 text.append("SEQUENCE_REF\t");
442 text.append("ALIGNMENT");
443 text.append(newline);
449 if (refSeq == null || refSeq != next_refSeq)
451 text.append(newline);
452 text.append("SEQUENCE_REF\t");
453 text.append(next_refSeq.getName());
454 text.append(newline);
461 public void printGroups(List<SequenceGroup> list)
463 SequenceI seqrep = null;
464 for (SequenceGroup sg : list)
468 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
469 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
475 seqrep = sg.getSeqrep();
476 text.append("SEQUENCE_REF\t");
477 text.append(seqrep.getName());
478 text.append(newline);
479 text.append("SEQUENCE_GROUP\t");
480 text.append(sg.getName());
482 text.append((seqrep.findPosition(sg.getStartRes())));
484 text.append((seqrep.findPosition(sg.getEndRes())));
488 for (int s = 0; s < sg.getSize(); s++)
490 text.append(sg.getSequenceAt(s).getName());
493 text.append(newline);
494 text.append("PROPERTIES\t");
495 text.append(sg.getName());
498 if (sg.getDescription() != null)
500 text.append("description=");
501 text.append(sg.getDescription());
506 text.append("colour=");
507 text.append(ColourSchemeProperty.getColourName(sg.cs));
509 if (sg.cs.getThreshold() != 0)
511 text.append("pidThreshold=");
512 text.append(sg.cs.getThreshold());
514 if (sg.cs.conservationApplied())
516 text.append("consThreshold=");
517 text.append(sg.cs.getConservationInc());
521 text.append("outlineColour=");
522 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
525 text.append("displayBoxes=");
526 text.append(sg.getDisplayBoxes());
528 text.append("displayText=");
529 text.append(sg.getDisplayText());
531 text.append("colourText=");
532 text.append(sg.getColourText());
534 text.append("showUnconserved=");
535 text.append(sg.getShowNonconserved());
537 if (sg.textColour != java.awt.Color.black)
539 text.append("textCol1=");
540 text.append(jalview.util.Format.getHexString(sg.textColour));
543 if (sg.textColour2 != java.awt.Color.white)
545 text.append("textCol2=");
546 text.append(jalview.util.Format.getHexString(sg.textColour2));
549 if (sg.thresholdTextColour != 0)
551 text.append("textColThreshold=");
552 text.append(sg.thresholdTextColour);
555 if (sg.idColour != null)
557 text.append("idColour=");
558 text.append(jalview.util.Format.getHexString(sg.idColour));
563 text.append("hide=true\t");
567 text.append("hidecols=true\t");
571 // terminate the last line and clear the sequence ref for the group
572 text.append(newline);
573 text.append("SEQUENCE_REF");
575 text.append(newline);
576 text.append(newline);
581 SequenceI refSeq = null;
583 String refSeqId = null;
585 public boolean readAnnotationFile(AlignmentI al, String file,
588 BufferedReader in = null;
591 if (protocol.equals(AppletFormatAdapter.FILE))
593 in = new BufferedReader(new FileReader(file));
595 else if (protocol.equals(AppletFormatAdapter.URL))
597 URL url = new URL(file);
598 in = new BufferedReader(new InputStreamReader(url.openStream()));
600 else if (protocol.equals(AppletFormatAdapter.PASTE))
602 in = new BufferedReader(new StringReader(file));
604 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
606 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
609 in = new BufferedReader(new java.io.InputStreamReader(is));
614 return parseAnnotationFrom(al, in);
617 } catch (Exception ex)
619 ex.printStackTrace();
620 System.out.println("Problem reading annotation file: " + ex);
622 System.out.println("Last read line "+nlinesread+": '"+lastread+"' (first 80 chars) ...");
630 private static String GRAPHLINE="GRAPHLINE", COMBINE="COMBINE";
631 public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)
635 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
636 ArrayList<Object[]> deferredAnnotation_calls = new ArrayList<Object[]>();
637 boolean modified = false;
638 String groupRef = null;
639 Hashtable groupRefRows = new Hashtable();
641 Hashtable autoAnnots = new Hashtable();
643 String line, label, description, token;
644 int graphStyle, index;
646 int existingAnnotations = 0;
647 // when true - will add new rows regardless of whether they are duplicate
648 // auto-annotation like consensus or conservation graphs
649 boolean overrideAutoAnnot = false;
650 if (al.getAlignmentAnnotation() != null)
652 existingAnnotations = al.getAlignmentAnnotation().length;
653 if (existingAnnotations > 0)
655 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
656 for (int aai = 0; aai < aa.length; aai++)
658 if (aa[aai].autoCalculated)
660 // make a note of the name and description
662 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
663 (aa[aai].groupRef == null ? null
664 : aa[aai].groupRef.getName())),
671 int alWidth = al.getWidth();
674 Annotation[] annotations;
675 AlignmentAnnotation annotation = null;
677 // First confirm this is an Annotation file
678 boolean jvAnnotationFile = false;
679 while ((line = in.readLine()) != null)
681 nlinesread++;lastread = new String(line);
682 if (line.indexOf("#") == 0)
687 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
689 jvAnnotationFile = true;
694 if (!jvAnnotationFile)
700 while ((line = in.readLine()) != null)
702 nlinesread++;lastread = new String(line);
703 if (line.indexOf("#") == 0
704 || line.indexOf("JALVIEW_ANNOTATION") > -1
705 || line.length() == 0)
710 st = new StringTokenizer(line, "\t");
711 token = st.nextToken();
712 if (token.equalsIgnoreCase("COLOUR"))
714 // TODO: use graduated colour def'n here too
715 colourAnnotations(al, st.nextToken(), st.nextToken());
720 else if (token.equalsIgnoreCase(COMBINE))
722 // keep a record of current state and resolve groupRef at end
723 combineAnnotation_calls.add(new Object[] { st, refSeq, groupRef});
727 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
729 addRowProperties(al, st);
733 else if (token.equalsIgnoreCase(GRAPHLINE))
736 deferredAnnotation_calls.add(new Object[] { GRAPHLINE, st, refSeq, groupRef});
741 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
743 if (st.hasMoreTokens())
745 refSeq = al.findName(refSeqId = st.nextToken());
752 refSeqIndex = Integer.parseInt(st.nextToken());
757 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
759 } catch (Exception ex)
771 else if (token.equalsIgnoreCase("GROUP_REF"))
773 // Group references could be forward or backwards, so they are
774 // resolved after the whole file is read in
776 if (st.hasMoreTokens())
778 groupRef = st.nextToken();
779 if (groupRef.length() < 1)
781 groupRef = null; // empty string
785 if (groupRefRows.get(groupRef) == null)
787 groupRefRows.put(groupRef, new Vector());
793 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
800 else if (token.equalsIgnoreCase("PROPERTIES"))
802 addProperties(al, st);
807 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
809 setBelowAlignment(al, st);
813 else if (token.equalsIgnoreCase("ALIGNMENT"))
815 addAlignmentDetails(al, st);
820 // Parse out the annotation row
821 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
822 label = st.nextToken();
825 annotations = new Annotation[alWidth];
827 float score = Float.NaN;
829 if (st.hasMoreTokens())
831 line = st.nextToken();
833 if (line.indexOf("|") == -1)
836 if (st.hasMoreTokens())
837 line = st.nextToken();
840 if (st.hasMoreTokens())
842 // This must be the score
843 score = Float.valueOf(st.nextToken()).floatValue();
846 st = new StringTokenizer(line, "|", true);
848 boolean emptyColumn = true;
849 boolean onlyOneElement = (st.countTokens() == 1);
851 while (st.hasMoreElements() && index < alWidth)
853 token = st.nextToken().trim();
859 score = Float.valueOf(token).floatValue();
861 } catch (NumberFormatException ex)
866 if (token.equals("|"))
877 annotations[index++] = parseAnnotation(token, graphStyle);
884 annotation = new AlignmentAnnotation(label, description,
885 (index == 0) ? null : annotations, 0, 0, graphStyle);
887 annotation.score = score;
888 if (!overrideAutoAnnot
889 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
892 // skip - we've already got an automatic annotation of this type.
899 annotation.belowAlignment = false;
900 // make a copy of refSeq so we can find other matches in the alignment
901 SequenceI referedSeq = refSeq;
904 // copy before we do any mapping business.
905 // TODO: verify that undo/redo with 1:many sequence associated
906 // annotations can be undone correctly
907 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
909 .createSequenceMapping(referedSeq, refSeqIndex, false);
910 annotation.adjustForAlignment();
911 referedSeq.addAlignmentAnnotation(annotation);
912 al.addAnnotation(annotation);
913 al.setAnnotationIndex(annotation,
914 al.getAlignmentAnnotation().length
915 - existingAnnotations - 1);
916 if (groupRef != null)
918 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
920 // and recover our virgin copy to use again if necessary.
923 } while (refSeqId != null
924 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
928 al.addAnnotation(annotation);
929 al.setAnnotationIndex(annotation,
930 al.getAlignmentAnnotation().length - existingAnnotations
932 if (groupRef != null)
934 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
937 // and set modification flag
940 // Resolve the groupRefs
941 Hashtable <String,SequenceGroup> groupRefLookup=new Hashtable<String,SequenceGroup>();
942 Enumeration en = groupRefRows.keys();
944 while (en.hasMoreElements())
946 groupRef = (String) en.nextElement();
947 boolean matched = false;
948 // Resolve group: TODO: add a getGroupByName method to alignments
949 for (SequenceGroup theGroup : al.getGroups())
951 if (theGroup.getName().equals(groupRef))
955 // TODO: specify and implement duplication of alignment annotation
956 // for multiple group references.
958 .println("Ignoring 1:many group reference mappings for group name '"
964 Vector rowset = (Vector) groupRefRows.get(groupRef);
965 groupRefLookup.put(groupRef, theGroup);
966 if (rowset != null && rowset.size() > 0)
968 AlignmentAnnotation alan = null;
969 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
971 alan = (AlignmentAnnotation) rowset.elementAt(elm);
972 alan.groupRef = theGroup;
978 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
980 // process any deferred attribute settings for each context
981 for (Object[] _deferred_args : deferredAnnotation_calls)
983 if (_deferred_args[0] == GRAPHLINE)
986 (StringTokenizer) _deferred_args[1], // st
987 (SequenceI) _deferred_args[2], // refSeq
988 (_deferred_args[3] == null) ? null : groupRefLookup
989 .get((String) _deferred_args[3]) // the reference
995 // finally, combine all the annotation rows within each context.
997 * number of combine statements in this annotation file. Used to create new groups for combined annotation graphs without disturbing existing ones
999 int combinecount = 0;
1000 for (Object[] _combine_args:combineAnnotation_calls) {
1001 combineAnnotations(al,
1003 (StringTokenizer) _combine_args[0], // st
1004 (SequenceI) _combine_args[1], // refSeq
1005 (_combine_args[2]==null) ? null : groupRefLookup.get((String)_combine_args[2]) // the reference group, or null
1012 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1013 SequenceI refSeq, String groupRef)
1015 return annotation.graph + "\t" + annotation.label + "\t"
1016 + annotation.description + "\t"
1017 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1020 Annotation parseAnnotation(String string, int graphStyle)
1022 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
1031 String desc = null, displayChar = null;
1032 char ss = ' '; // secondaryStructure
1034 boolean parsedValue = false, dcset = false;
1037 java.awt.Color colour = null;
1038 int i = string.indexOf("[");
1039 int j = string.indexOf("]");
1040 if (i > -1 && j > -1)
1042 UserColourScheme ucs = new UserColourScheme();
1044 colour = ucs.getColourFromString(string.substring(i + 1, j));
1045 if (i > 0 && string.charAt(i - 1) == ',')
1047 // clip the preceding comma as well
1050 string = string.substring(0, i) + string.substring(j + 1);
1053 StringTokenizer st = new StringTokenizer(string, ",", true);
1055 boolean seenContent = false;
1057 while (st.hasMoreTokens())
1060 token = st.nextToken().trim();
1061 if (token.equals(","))
1063 if (!seenContent && parsedValue && !dcset)
1065 // allow the value below the bar/line to be empty
1069 seenContent = false;
1081 displayChar = token;
1083 value = new Float(token).floatValue();
1086 } catch (NumberFormatException ex)
1092 if (token.length() == 1)
1094 displayChar = token;
1098 && (token.equals("H") || token.equals("E")
1099 || token.equals("S") || token.equals(" ")))
1101 // Either this character represents a helix or sheet
1102 // or an integer which can be displayed
1103 ss = token.charAt(0);
1104 if (displayChar.equals(token.substring(0, 1)))
1109 else if (desc == null || (parsedValue && pass > 2))
1115 // if (!dcset && string.charAt(string.length() - 1) == ',')
1117 // displayChar = " "; // empty display char symbol.
1119 if (displayChar != null && desc != null && desc.length() == 1)
1121 if (displayChar.length() > 1)
1123 // switch desc and displayChar - legacy support
1124 String tmp = displayChar;
1130 if (displayChar.equals(desc))
1132 // duplicate label - hangover from the 'robust parser' above
1137 Annotation anot = new Annotation(displayChar, desc, ss, value);
1139 anot.colour = colour;
1144 void colourAnnotations(AlignmentI al, String label, String colour)
1146 UserColourScheme ucs = new UserColourScheme(colour);
1147 Annotation[] annotations;
1148 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1150 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1152 annotations = al.getAlignmentAnnotation()[i].annotations;
1153 for (int j = 0; j < annotations.length; j++)
1155 if (annotations[j] != null)
1157 annotations[j].colour = ucs.findColour('A');
1164 void combineAnnotations(AlignmentI al, int combineCount, StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1166 String group = st.nextToken();
1167 // First make sure we are not overwriting the graphIndex
1169 if (al.getAlignmentAnnotation() != null)
1171 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1173 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1175 if (aa.graphGroup>graphGroup)
1177 // try to number graphGroups in order of occurence.
1178 graphGroup=aa.graphGroup+1;
1180 if (aa.sequenceRef==seqRef && aa.groupRef==groupRef && aa.label.equalsIgnoreCase(group))
1182 if (aa.graphGroup>-1)
1184 graphGroup = aa.graphGroup;
1186 if (graphGroup <= combineCount)
1188 graphGroup=combineCount+1;
1190 aa.graphGroup = graphGroup;
1196 // Now update groups
1197 while (st.hasMoreTokens())
1199 group = st.nextToken();
1200 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1202 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1203 if (aa.sequenceRef==seqRef && aa.groupRef==groupRef && aa.label.equalsIgnoreCase(group))
1205 aa.graphGroup = graphGroup;
1214 .println("Couldn't combine annotations. None are added to alignment yet!");
1218 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1220 String group = st.nextToken();
1221 AlignmentAnnotation annotation = null, alannot[] = al
1222 .getAlignmentAnnotation();
1223 float value = new Float(st.nextToken()).floatValue();
1224 String label = st.hasMoreTokens() ? st.nextToken() : null;
1225 java.awt.Color colour = null;
1226 if (st.hasMoreTokens())
1228 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1229 colour = ucs.findColour('A');
1231 if (alannot != null)
1233 for (int i = 0; i < alannot.length; i++)
1235 if (alannot[i].label.equalsIgnoreCase(group) && (seqRef==null || alannot[i].sequenceRef==seqRef) && (groupRef==null || alannot[i].groupRef==groupRef))
1237 alannot[i].setThreshold(new GraphLine(value, label, colour));
1241 if (annotation == null)
1247 void addGroup(AlignmentI al, StringTokenizer st)
1249 SequenceGroup sg = new SequenceGroup();
1250 sg.setName(st.nextToken());
1254 rng = st.nextToken();
1255 if (rng.length() > 0 && !rng.startsWith("*"))
1257 sg.setStartRes(Integer.parseInt(rng) - 1);
1263 rng = st.nextToken();
1264 if (rng.length() > 0 && !rng.startsWith("*"))
1266 sg.setEndRes(Integer.parseInt(rng) - 1);
1270 sg.setEndRes(al.getWidth() - 1);
1272 } catch (Exception e)
1275 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1276 + rng + "' - assuming alignment width for group.");
1277 // assume group is full width
1279 sg.setEndRes(al.getWidth() - 1);
1282 String index = st.nextToken();
1283 if (index.equals("-1"))
1285 while (st.hasMoreElements())
1287 sg.addSequence(al.findName(st.nextToken()), false);
1292 StringTokenizer st2 = new StringTokenizer(index, ",");
1294 while (st2.hasMoreTokens())
1296 String tmp = st2.nextToken();
1297 if (tmp.equals("*"))
1299 for (int i = 0; i < al.getHeight(); i++)
1301 sg.addSequence(al.getSequenceAt(i), false);
1304 else if (tmp.indexOf("-") >= 0)
1306 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1308 int start = (Integer.parseInt(st3.nextToken()));
1309 int end = (Integer.parseInt(st3.nextToken()));
1313 for (int i = start; i <= end; i++)
1315 sg.addSequence(al.getSequenceAt(i - 1), false);
1321 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1328 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1329 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1330 sg.setSeqrep(refSeq);
1333 if (sg.getSize() > 0)
1339 void addRowProperties(AlignmentI al, StringTokenizer st)
1341 String label = st.nextToken(), keyValue, key, value;
1342 boolean scaletofit = false, centerlab = false, showalllabs = false;
1343 while (st.hasMoreTokens())
1345 keyValue = st.nextToken();
1346 key = keyValue.substring(0, keyValue.indexOf("="));
1347 value = keyValue.substring(keyValue.indexOf("=") + 1);
1348 if (key.equalsIgnoreCase("scaletofit"))
1350 scaletofit = Boolean.valueOf(value).booleanValue();
1352 if (key.equalsIgnoreCase("showalllabs"))
1354 showalllabs = Boolean.valueOf(value).booleanValue();
1356 if (key.equalsIgnoreCase("centrelabs"))
1358 centerlab = Boolean.valueOf(value).booleanValue();
1360 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1363 for (int i = 0; i < alr.length; i++)
1365 if (alr[i].label.equalsIgnoreCase(label))
1367 alr[i].centreColLabels = centerlab;
1368 alr[i].scaleColLabel = scaletofit;
1369 alr[i].showAllColLabels = showalllabs;
1376 void addProperties(AlignmentI al, StringTokenizer st)
1379 // So far we have only added groups to the annotationHash,
1380 // the idea is in the future properties can be added to
1381 // alignments, other annotations etc
1382 if (al.getGroups() == null)
1387 String name = st.nextToken();
1388 SequenceGroup sg = null;
1389 for (SequenceGroup _sg : al.getGroups())
1391 if ((sg = _sg).getName().equals(name))
1403 String keyValue, key, value;
1404 ColourSchemeI def = sg.cs;
1406 while (st.hasMoreTokens())
1408 keyValue = st.nextToken();
1409 key = keyValue.substring(0, keyValue.indexOf("="));
1410 value = keyValue.substring(keyValue.indexOf("=") + 1);
1412 if (key.equalsIgnoreCase("description"))
1414 sg.setDescription(value);
1416 else if (key.equalsIgnoreCase("colour"))
1418 sg.cs = ColourSchemeProperty.getColour(al, value);
1420 else if (key.equalsIgnoreCase("pidThreshold"))
1422 sg.cs.setThreshold(Integer.parseInt(value), true);
1425 else if (key.equalsIgnoreCase("consThreshold"))
1427 sg.cs.setConservationInc(Integer.parseInt(value));
1428 Conservation c = new Conservation("Group",
1429 ResidueProperties.propHash, 3, sg.getSequences(null),
1430 sg.getStartRes(), sg.getEndRes() + 1);
1433 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1435 sg.cs.setConservation(c);
1438 else if (key.equalsIgnoreCase("outlineColour"))
1440 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1442 else if (key.equalsIgnoreCase("displayBoxes"))
1444 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1446 else if (key.equalsIgnoreCase("showUnconserved"))
1448 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1450 else if (key.equalsIgnoreCase("displayText"))
1452 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1454 else if (key.equalsIgnoreCase("colourText"))
1456 sg.setColourText(Boolean.valueOf(value).booleanValue());
1458 else if (key.equalsIgnoreCase("textCol1"))
1460 sg.textColour = new UserColourScheme(value).findColour('A');
1462 else if (key.equalsIgnoreCase("textCol2"))
1464 sg.textColour2 = new UserColourScheme(value).findColour('A');
1466 else if (key.equalsIgnoreCase("textColThreshold"))
1468 sg.thresholdTextColour = Integer.parseInt(value);
1470 else if (key.equalsIgnoreCase("idColour"))
1472 // consider warning if colour doesn't resolve to a real colour
1473 sg.setIdColour((def = new UserColourScheme(value))
1476 else if (key.equalsIgnoreCase("hide"))
1478 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1479 sg.setHidereps(true);
1481 else if (key.equalsIgnoreCase("hidecols"))
1483 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1484 sg.setHideCols(true);
1486 sg.recalcConservation();
1495 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1498 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1502 .print("Warning - no annotation to set below for sequence associated annotation:");
1504 while (st.hasMoreTokens())
1506 token = st.nextToken();
1509 System.err.print(" " + token);
1513 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1515 aa = al.getAlignmentAnnotation()[i];
1516 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1518 aa.belowAlignment = true;
1525 System.err.print("\n");
1529 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1531 String keyValue, key, value;
1532 while (st.hasMoreTokens())
1534 keyValue = st.nextToken();
1535 key = keyValue.substring(0, keyValue.indexOf("="));
1536 value = keyValue.substring(keyValue.indexOf("=") + 1);
1537 al.setProperty(key, value);
1542 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1545 * @param annotations
1546 * @return CSV file as a string.
1548 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1550 StringBuffer sp = new StringBuffer();
1551 for (int i = 0; i < annotations.length; i++)
1553 String atos = annotations[i].toString();
1557 int cp = atos.indexOf("\n", p);
1558 sp.append(annotations[i].label);
1562 sp.append(atos.substring(p, cp + 1));
1566 sp.append(atos.substring(p));
1572 return sp.toString();