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];
149 && !(row.graphGroup > -1 && graphGroupSeen
150 .get(row.graphGroup)))
158 // mark any sequence references for the row
159 writeSequence_Ref(refSeq, row.sequenceRef);
160 refSeq = row.sequenceRef;
161 // mark any group references for the row
162 writeGroup_Ref(refGroup, row.groupRef);
163 refGroup = row.groupRef;
165 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
166 // lookahead to check what the annotation row object actually contains.
167 for (int j = 0; row.annotations != null
168 && j < row.annotations.length
169 && (!hasGlyphs || !hasLabels || !hasValues); j++)
171 if (row.annotations[j] != null)
173 hasLabels |= (row.annotations[j].displayCharacter != null
174 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
176 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
177 hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't
180 hasText |= (row.annotations[j].description != null && row.annotations[j].description
185 if (row.graph == AlignmentAnnotation.NO_GRAPH)
187 text.append("NO_GRAPH\t");
188 hasValues = false; // only secondary structure
189 // hasLabels = false; // and annotation description string.
193 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
195 text.append("BAR_GRAPH\t");
196 hasGlyphs = false; // no secondary structure
199 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
201 hasGlyphs = false; // no secondary structure
202 text.append("LINE_GRAPH\t");
205 if (row.getThreshold() != null)
207 graphLine.append("GRAPHLINE\t");
208 graphLine.append(row.label);
209 graphLine.append("\t");
210 graphLine.append(row.getThreshold().value);
211 graphLine.append("\t");
212 graphLine.append(row.getThreshold().label);
213 graphLine.append("\t");
214 graphLine.append(jalview.util.Format.getHexString(row
215 .getThreshold().colour));
216 graphLine.append(newline);
219 if (row.graphGroup > -1)
221 graphGroupSeen.set(row.graphGroup);
222 Integer key = new Integer(row.graphGroup);
223 if (graphGroup.containsKey(key))
225 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
230 graphGroup_refs.put(key, new Object[]
231 { refSeq, refGroup });
232 graphGroup.put(key, row.label);
237 text.append(row.label + "\t");
238 if (row.description != null)
240 text.append(row.description + "\t");
242 for (int j = 0; row.annotations != null
243 && j < row.annotations.length; j++)
246 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
251 if (row.annotations[j] != null)
254 if (hasGlyphs) // could be also hasGlyphs || ...
258 if (row.annotations[j].secondaryStructure != ' ')
260 // only write out the field if its not whitespace.
261 text.append(row.annotations[j].secondaryStructure);
267 if (row.annotations[j].value != Float.NaN)
269 text.append(comma + row.annotations[j].value);
273 System.err.println("Skipping NaN - not valid value.");
274 text.append(comma + 0f);// row.annotations[j].value);
280 // TODO: labels are emitted after values for bar graphs.
281 if // empty labels are allowed, so
282 (row.annotations[j].displayCharacter != null
283 && row.annotations[j].displayCharacter.length() > 0
284 && !row.annotations[j].displayCharacter.equals(" "))
286 text.append(comma + row.annotations[j].displayCharacter);
292 if (row.annotations[j].description != null
293 && row.annotations[j].description.length() > 0
294 && !row.annotations[j].description
295 .equals(row.annotations[j].displayCharacter))
297 text.append(comma + row.annotations[j].description);
301 if (color != null && !color.equals(row.annotations[j].colour))
306 color = row.annotations[j].colour;
308 if (row.annotations[j].colour != null
309 && row.annotations[j].colour != java.awt.Color.black)
313 + jalview.util.Format
314 .getHexString(row.annotations[j].colour)
323 text.append("\t" + row.score);
325 text.append(newline);
327 if (color != null && color != java.awt.Color.black && oneColour)
329 colours.append("COLOUR\t");
330 colours.append(row.label);
331 colours.append("\t");
332 colours.append(jalview.util.Format.getHexString(color));
333 colours.append(newline);
335 if (row.scaleColLabel || row.showAllColLabels
336 || row.centreColLabels)
338 rowprops.append("ROWPROPERTIES\t");
339 rowprops.append(row.label);
340 rowprops.append("\tscaletofit=");
341 rowprops.append(row.scaleColLabel);
342 rowprops.append("\tshowalllabs=");
343 rowprops.append(row.showAllColLabels);
344 rowprops.append("\tcentrelabs=");
345 rowprops.append(row.centreColLabels);
346 rowprops.append(newline);
348 if (graphLine.length() > 0)
350 text.append(graphLine.toString());
351 graphLine.setLength(0);
355 text.append(newline);
357 text.append(colours.toString());
358 if (graphGroup.size() > 0)
360 SequenceI oldRefSeq = refSeq;
361 SequenceGroup oldRefGroup = refGroup;
362 for (Map.Entry<Integer, String> combine_statement : graphGroup
365 Object[] seqRefAndGroup = graphGroup_refs.get(combine_statement
368 writeSequence_Ref(refSeq, (SequenceI) seqRefAndGroup[0]);
369 refSeq = (SequenceI) seqRefAndGroup[0];
371 writeGroup_Ref(refGroup, (SequenceGroup) seqRefAndGroup[1]);
372 refGroup = (SequenceGroup) seqRefAndGroup[1];
373 text.append("COMBINE\t");
374 text.append(combine_statement.getValue());
375 text.append(newline);
377 writeSequence_Ref(refSeq, oldRefSeq);
380 writeGroup_Ref(refGroup, oldRefGroup);
381 refGroup = oldRefGroup;
383 text.append(rowprops.toString());
391 if (properties != null)
393 text.append(newline);
394 text.append(newline);
395 text.append("ALIGNMENT");
396 Enumeration en = properties.keys();
397 while (en.hasMoreElements())
399 String key = en.nextElement().toString();
403 text.append(properties.get(key));
405 // TODO: output alignment visualization settings here if required
409 return text.toString();
412 private Object writeGroup_Ref(SequenceGroup refGroup,
413 SequenceGroup next_refGroup)
415 if (next_refGroup == null)
418 if (refGroup != null)
420 text.append(newline);
421 text.append("GROUP_REF\t");
422 text.append("ALIGNMENT");
423 text.append(newline);
429 if (refGroup == null || refGroup != next_refGroup)
431 text.append(newline);
432 text.append("GROUP_REF\t");
433 text.append(next_refGroup.getName());
434 text.append(newline);
441 private boolean writeSequence_Ref(SequenceI refSeq, SequenceI next_refSeq)
444 if (next_refSeq == null)
448 text.append(newline);
449 text.append("SEQUENCE_REF\t");
450 text.append("ALIGNMENT");
451 text.append(newline);
457 if (refSeq == null || refSeq != next_refSeq)
459 text.append(newline);
460 text.append("SEQUENCE_REF\t");
461 text.append(next_refSeq.getName());
462 text.append(newline);
469 public void printGroups(List<SequenceGroup> list)
471 SequenceI seqrep = null;
472 for (SequenceGroup sg : list)
476 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
477 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
483 seqrep = sg.getSeqrep();
484 text.append("SEQUENCE_REF\t");
485 text.append(seqrep.getName());
486 text.append(newline);
487 text.append("SEQUENCE_GROUP\t");
488 text.append(sg.getName());
490 text.append((seqrep.findPosition(sg.getStartRes())));
492 text.append((seqrep.findPosition(sg.getEndRes())));
496 for (int s = 0; s < sg.getSize(); s++)
498 text.append(sg.getSequenceAt(s).getName());
501 text.append(newline);
502 text.append("PROPERTIES\t");
503 text.append(sg.getName());
506 if (sg.getDescription() != null)
508 text.append("description=");
509 text.append(sg.getDescription());
514 text.append("colour=");
515 text.append(ColourSchemeProperty.getColourName(sg.cs));
517 if (sg.cs.getThreshold() != 0)
519 text.append("pidThreshold=");
520 text.append(sg.cs.getThreshold());
522 if (sg.cs.conservationApplied())
524 text.append("consThreshold=");
525 text.append(sg.cs.getConservationInc());
529 text.append("outlineColour=");
530 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
533 text.append("displayBoxes=");
534 text.append(sg.getDisplayBoxes());
536 text.append("displayText=");
537 text.append(sg.getDisplayText());
539 text.append("colourText=");
540 text.append(sg.getColourText());
542 text.append("showUnconserved=");
543 text.append(sg.getShowNonconserved());
545 if (sg.textColour != java.awt.Color.black)
547 text.append("textCol1=");
548 text.append(jalview.util.Format.getHexString(sg.textColour));
551 if (sg.textColour2 != java.awt.Color.white)
553 text.append("textCol2=");
554 text.append(jalview.util.Format.getHexString(sg.textColour2));
557 if (sg.thresholdTextColour != 0)
559 text.append("textColThreshold=");
560 text.append(sg.thresholdTextColour);
563 if (sg.idColour != null)
565 text.append("idColour=");
566 text.append(jalview.util.Format.getHexString(sg.idColour));
571 text.append("hide=true\t");
575 text.append("hidecols=true\t");
579 // terminate the last line and clear the sequence ref for the group
580 text.append(newline);
581 text.append("SEQUENCE_REF");
583 text.append(newline);
584 text.append(newline);
589 SequenceI refSeq = null;
591 String refSeqId = null;
593 public boolean readAnnotationFile(AlignmentI al, String file,
596 BufferedReader in = null;
599 if (protocol.equals(AppletFormatAdapter.FILE))
601 in = new BufferedReader(new FileReader(file));
603 else if (protocol.equals(AppletFormatAdapter.URL))
605 URL url = new URL(file);
606 in = new BufferedReader(new InputStreamReader(url.openStream()));
608 else if (protocol.equals(AppletFormatAdapter.PASTE))
610 in = new BufferedReader(new StringReader(file));
612 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
614 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
617 in = new BufferedReader(new java.io.InputStreamReader(is));
622 return parseAnnotationFrom(al, in);
625 } catch (Exception ex)
627 ex.printStackTrace();
628 System.out.println("Problem reading annotation file: " + ex);
631 System.out.println("Last read line " + nlinesread + ": '"
632 + lastread + "' (first 80 chars) ...");
641 String lastread = "";
643 private static String GRAPHLINE = "GRAPHLINE", COMBINE = "COMBINE";
645 public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)
649 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
650 ArrayList<Object[]> deferredAnnotation_calls = new ArrayList<Object[]>();
651 boolean modified = false;
652 String groupRef = null;
653 Hashtable groupRefRows = new Hashtable();
655 Hashtable autoAnnots = new Hashtable();
657 String line, label, description, token;
658 int graphStyle, index;
660 int existingAnnotations = 0;
661 // when true - will add new rows regardless of whether they are duplicate
662 // auto-annotation like consensus or conservation graphs
663 boolean overrideAutoAnnot = false;
664 if (al.getAlignmentAnnotation() != null)
666 existingAnnotations = al.getAlignmentAnnotation().length;
667 if (existingAnnotations > 0)
669 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
670 for (int aai = 0; aai < aa.length; aai++)
672 if (aa[aai].autoCalculated)
674 // make a note of the name and description
676 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
677 (aa[aai].groupRef == null ? null
678 : aa[aai].groupRef.getName())),
685 int alWidth = al.getWidth();
688 Annotation[] annotations;
689 AlignmentAnnotation annotation = null;
691 // First confirm this is an Annotation file
692 boolean jvAnnotationFile = false;
693 while ((line = in.readLine()) != null)
696 lastread = new String(line);
697 if (line.indexOf("#") == 0)
702 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
704 jvAnnotationFile = true;
709 if (!jvAnnotationFile)
715 while ((line = in.readLine()) != null)
718 lastread = new String(line);
719 if (line.indexOf("#") == 0
720 || line.indexOf("JALVIEW_ANNOTATION") > -1
721 || line.length() == 0)
726 st = new StringTokenizer(line, "\t");
727 token = st.nextToken();
728 if (token.equalsIgnoreCase("COLOUR"))
730 // TODO: use graduated colour def'n here too
731 colourAnnotations(al, st.nextToken(), st.nextToken());
736 else if (token.equalsIgnoreCase(COMBINE))
738 // keep a record of current state and resolve groupRef at end
739 combineAnnotation_calls.add(new Object[]
740 { st, refSeq, groupRef });
744 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
746 addRowProperties(al, st);
750 else if (token.equalsIgnoreCase(GRAPHLINE))
753 deferredAnnotation_calls.add(new Object[]
754 { GRAPHLINE, st, refSeq, groupRef });
759 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
761 if (st.hasMoreTokens())
763 refSeq = al.findName(refSeqId = st.nextToken());
770 refSeqIndex = Integer.parseInt(st.nextToken());
775 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
777 } catch (Exception ex)
789 else if (token.equalsIgnoreCase("GROUP_REF"))
791 // Group references could be forward or backwards, so they are
792 // resolved after the whole file is read in
794 if (st.hasMoreTokens())
796 groupRef = st.nextToken();
797 if (groupRef.length() < 1)
799 groupRef = null; // empty string
803 if (groupRefRows.get(groupRef) == null)
805 groupRefRows.put(groupRef, new Vector());
811 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
818 else if (token.equalsIgnoreCase("PROPERTIES"))
820 addProperties(al, st);
825 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
827 setBelowAlignment(al, st);
831 else if (token.equalsIgnoreCase("ALIGNMENT"))
833 addAlignmentDetails(al, st);
838 // Parse out the annotation row
839 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
840 label = st.nextToken();
843 annotations = new Annotation[alWidth];
845 float score = Float.NaN;
847 if (st.hasMoreTokens())
849 line = st.nextToken();
851 if (line.indexOf("|") == -1)
854 if (st.hasMoreTokens())
855 line = st.nextToken();
858 if (st.hasMoreTokens())
860 // This must be the score
861 score = Float.valueOf(st.nextToken()).floatValue();
864 st = new StringTokenizer(line, "|", true);
866 boolean emptyColumn = true;
867 boolean onlyOneElement = (st.countTokens() == 1);
869 while (st.hasMoreElements() && index < alWidth)
871 token = st.nextToken().trim();
877 score = Float.valueOf(token).floatValue();
879 } catch (NumberFormatException ex)
884 if (token.equals("|"))
895 annotations[index++] = parseAnnotation(token, graphStyle);
902 annotation = new AlignmentAnnotation(label, description,
903 (index == 0) ? null : annotations, 0, 0, graphStyle);
905 annotation.score = score;
906 if (!overrideAutoAnnot
907 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
910 // skip - we've already got an automatic annotation of this type.
917 annotation.belowAlignment = false;
918 // make a copy of refSeq so we can find other matches in the alignment
919 SequenceI referedSeq = refSeq;
922 // copy before we do any mapping business.
923 // TODO: verify that undo/redo with 1:many sequence associated
924 // annotations can be undone correctly
925 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
927 .createSequenceMapping(referedSeq, refSeqIndex, false);
928 annotation.adjustForAlignment();
929 referedSeq.addAlignmentAnnotation(annotation);
930 al.addAnnotation(annotation);
931 al.setAnnotationIndex(annotation,
932 al.getAlignmentAnnotation().length
933 - existingAnnotations - 1);
934 if (groupRef != null)
936 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
938 // and recover our virgin copy to use again if necessary.
941 } while (refSeqId != null
942 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
946 al.addAnnotation(annotation);
947 al.setAnnotationIndex(annotation,
948 al.getAlignmentAnnotation().length - existingAnnotations
950 if (groupRef != null)
952 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
955 // and set modification flag
958 // Resolve the groupRefs
959 Hashtable<String, SequenceGroup> groupRefLookup = new Hashtable<String, SequenceGroup>();
960 Enumeration en = groupRefRows.keys();
962 while (en.hasMoreElements())
964 groupRef = (String) en.nextElement();
965 boolean matched = false;
966 // Resolve group: TODO: add a getGroupByName method to alignments
967 for (SequenceGroup theGroup : al.getGroups())
969 if (theGroup.getName().equals(groupRef))
973 // TODO: specify and implement duplication of alignment annotation
974 // for multiple group references.
976 .println("Ignoring 1:many group reference mappings for group name '"
982 Vector rowset = (Vector) groupRefRows.get(groupRef);
983 groupRefLookup.put(groupRef, theGroup);
984 if (rowset != null && rowset.size() > 0)
986 AlignmentAnnotation alan = null;
987 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
989 alan = (AlignmentAnnotation) rowset.elementAt(elm);
990 alan.groupRef = theGroup;
996 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
998 // process any deferred attribute settings for each context
999 for (Object[] _deferred_args : deferredAnnotation_calls)
1001 if (_deferred_args[0] == GRAPHLINE)
1004 (StringTokenizer) _deferred_args[1], // st
1005 (SequenceI) _deferred_args[2], // refSeq
1006 (_deferred_args[3] == null) ? null : groupRefLookup
1007 .get((String) _deferred_args[3]) // the reference
1013 // finally, combine all the annotation rows within each context.
1015 * number of combine statements in this annotation file. Used to create
1016 * new groups for combined annotation graphs without disturbing existing
1019 int combinecount = 0;
1020 for (Object[] _combine_args : combineAnnotation_calls)
1022 combineAnnotations(al,
1024 (StringTokenizer) _combine_args[0], // st
1025 (SequenceI) _combine_args[1], // refSeq
1026 (_combine_args[2] == null) ? null : groupRefLookup
1027 .get((String) _combine_args[2]) // the reference group,
1035 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1036 SequenceI refSeq, String groupRef)
1038 return annotation.graph + "\t" + annotation.label + "\t"
1039 + annotation.description + "\t"
1040 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1043 Annotation parseAnnotation(String string, int graphStyle)
1045 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
1054 String desc = null, displayChar = null;
1055 char ss = ' '; // secondaryStructure
1057 boolean parsedValue = false, dcset = false;
1060 java.awt.Color colour = null;
1061 int i = string.indexOf("[");
1062 int j = string.indexOf("]");
1063 if (i > -1 && j > -1)
1065 UserColourScheme ucs = new UserColourScheme();
1067 colour = ucs.getColourFromString(string.substring(i + 1, j));
1068 if (i > 0 && string.charAt(i - 1) == ',')
1070 // clip the preceding comma as well
1073 string = string.substring(0, i) + string.substring(j + 1);
1076 StringTokenizer st = new StringTokenizer(string, ",", true);
1078 boolean seenContent = false;
1080 while (st.hasMoreTokens())
1083 token = st.nextToken().trim();
1084 if (token.equals(","))
1086 if (!seenContent && parsedValue && !dcset)
1088 // allow the value below the bar/line to be empty
1092 seenContent = false;
1104 displayChar = token;
1106 value = new Float(token).floatValue();
1109 } catch (NumberFormatException ex)
1115 if (token.length() == 1)
1117 displayChar = token;
1121 && (token.equals("H") || token.equals("E")
1122 || token.equals("S") || token.equals(" ")))
1124 // Either this character represents a helix or sheet
1125 // or an integer which can be displayed
1126 ss = token.charAt(0);
1127 if (displayChar.equals(token.substring(0, 1)))
1132 else if (desc == null || (parsedValue && pass > 2))
1138 // if (!dcset && string.charAt(string.length() - 1) == ',')
1140 // displayChar = " "; // empty display char symbol.
1142 if (displayChar != null && desc != null && desc.length() == 1)
1144 if (displayChar.length() > 1)
1146 // switch desc and displayChar - legacy support
1147 String tmp = displayChar;
1153 if (displayChar.equals(desc))
1155 // duplicate label - hangover from the 'robust parser' above
1160 Annotation anot = new Annotation(displayChar, desc, ss, value);
1162 anot.colour = colour;
1167 void colourAnnotations(AlignmentI al, String label, String colour)
1169 UserColourScheme ucs = new UserColourScheme(colour);
1170 Annotation[] annotations;
1171 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1173 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1175 annotations = al.getAlignmentAnnotation()[i].annotations;
1176 for (int j = 0; j < annotations.length; j++)
1178 if (annotations[j] != null)
1180 annotations[j].colour = ucs.findColour('A');
1187 void combineAnnotations(AlignmentI al, int combineCount,
1188 StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1190 String group = st.nextToken();
1191 // First make sure we are not overwriting the graphIndex
1193 if (al.getAlignmentAnnotation() != null)
1195 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1197 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1199 if (aa.graphGroup > graphGroup)
1201 // try to number graphGroups in order of occurence.
1202 graphGroup = aa.graphGroup + 1;
1204 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1205 && aa.label.equalsIgnoreCase(group))
1207 if (aa.graphGroup > -1)
1209 graphGroup = aa.graphGroup;
1213 if (graphGroup <= combineCount)
1215 graphGroup = combineCount + 1;
1217 aa.graphGroup = graphGroup;
1223 // Now update groups
1224 while (st.hasMoreTokens())
1226 group = st.nextToken();
1227 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1229 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1230 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1231 && aa.label.equalsIgnoreCase(group))
1233 aa.graphGroup = graphGroup;
1242 .println("Couldn't combine annotations. None are added to alignment yet!");
1246 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef,
1247 SequenceGroup groupRef)
1249 String group = st.nextToken();
1250 AlignmentAnnotation annotation = null, alannot[] = al
1251 .getAlignmentAnnotation();
1252 float value = new Float(st.nextToken()).floatValue();
1253 String label = st.hasMoreTokens() ? st.nextToken() : null;
1254 java.awt.Color colour = null;
1255 if (st.hasMoreTokens())
1257 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1258 colour = ucs.findColour('A');
1260 if (alannot != null)
1262 for (int i = 0; i < alannot.length; i++)
1264 if (alannot[i].label.equalsIgnoreCase(group)
1265 && (seqRef == null || alannot[i].sequenceRef == seqRef)
1266 && (groupRef == null || alannot[i].groupRef == groupRef))
1268 alannot[i].setThreshold(new GraphLine(value, label, colour));
1272 if (annotation == null)
1278 void addGroup(AlignmentI al, StringTokenizer st)
1280 SequenceGroup sg = new SequenceGroup();
1281 sg.setName(st.nextToken());
1285 rng = st.nextToken();
1286 if (rng.length() > 0 && !rng.startsWith("*"))
1288 sg.setStartRes(Integer.parseInt(rng) - 1);
1294 rng = st.nextToken();
1295 if (rng.length() > 0 && !rng.startsWith("*"))
1297 sg.setEndRes(Integer.parseInt(rng) - 1);
1301 sg.setEndRes(al.getWidth() - 1);
1303 } catch (Exception e)
1306 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1307 + rng + "' - assuming alignment width for group.");
1308 // assume group is full width
1310 sg.setEndRes(al.getWidth() - 1);
1313 String index = st.nextToken();
1314 if (index.equals("-1"))
1316 while (st.hasMoreElements())
1318 sg.addSequence(al.findName(st.nextToken()), false);
1323 StringTokenizer st2 = new StringTokenizer(index, ",");
1325 while (st2.hasMoreTokens())
1327 String tmp = st2.nextToken();
1328 if (tmp.equals("*"))
1330 for (int i = 0; i < al.getHeight(); i++)
1332 sg.addSequence(al.getSequenceAt(i), false);
1335 else if (tmp.indexOf("-") >= 0)
1337 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1339 int start = (Integer.parseInt(st3.nextToken()));
1340 int end = (Integer.parseInt(st3.nextToken()));
1344 for (int i = start; i <= end; i++)
1346 sg.addSequence(al.getSequenceAt(i - 1), false);
1352 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1359 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1360 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1361 sg.setSeqrep(refSeq);
1364 if (sg.getSize() > 0)
1370 void addRowProperties(AlignmentI al, StringTokenizer st)
1372 String label = st.nextToken(), keyValue, key, value;
1373 boolean scaletofit = false, centerlab = false, showalllabs = false;
1374 while (st.hasMoreTokens())
1376 keyValue = st.nextToken();
1377 key = keyValue.substring(0, keyValue.indexOf("="));
1378 value = keyValue.substring(keyValue.indexOf("=") + 1);
1379 if (key.equalsIgnoreCase("scaletofit"))
1381 scaletofit = Boolean.valueOf(value).booleanValue();
1383 if (key.equalsIgnoreCase("showalllabs"))
1385 showalllabs = Boolean.valueOf(value).booleanValue();
1387 if (key.equalsIgnoreCase("centrelabs"))
1389 centerlab = Boolean.valueOf(value).booleanValue();
1391 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1394 for (int i = 0; i < alr.length; i++)
1396 if (alr[i].label.equalsIgnoreCase(label))
1398 alr[i].centreColLabels = centerlab;
1399 alr[i].scaleColLabel = scaletofit;
1400 alr[i].showAllColLabels = showalllabs;
1407 void addProperties(AlignmentI al, StringTokenizer st)
1410 // So far we have only added groups to the annotationHash,
1411 // the idea is in the future properties can be added to
1412 // alignments, other annotations etc
1413 if (al.getGroups() == null)
1418 String name = st.nextToken();
1419 SequenceGroup sg = null;
1420 for (SequenceGroup _sg : al.getGroups())
1422 if ((sg = _sg).getName().equals(name))
1434 String keyValue, key, value;
1435 ColourSchemeI def = sg.cs;
1437 while (st.hasMoreTokens())
1439 keyValue = st.nextToken();
1440 key = keyValue.substring(0, keyValue.indexOf("="));
1441 value = keyValue.substring(keyValue.indexOf("=") + 1);
1443 if (key.equalsIgnoreCase("description"))
1445 sg.setDescription(value);
1447 else if (key.equalsIgnoreCase("colour"))
1449 sg.cs = ColourSchemeProperty.getColour(al, value);
1451 else if (key.equalsIgnoreCase("pidThreshold"))
1453 sg.cs.setThreshold(Integer.parseInt(value), true);
1456 else if (key.equalsIgnoreCase("consThreshold"))
1458 sg.cs.setConservationInc(Integer.parseInt(value));
1459 Conservation c = new Conservation("Group",
1460 ResidueProperties.propHash, 3, sg.getSequences(null),
1461 sg.getStartRes(), sg.getEndRes() + 1);
1464 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1466 sg.cs.setConservation(c);
1469 else if (key.equalsIgnoreCase("outlineColour"))
1471 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1473 else if (key.equalsIgnoreCase("displayBoxes"))
1475 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1477 else if (key.equalsIgnoreCase("showUnconserved"))
1479 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1481 else if (key.equalsIgnoreCase("displayText"))
1483 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1485 else if (key.equalsIgnoreCase("colourText"))
1487 sg.setColourText(Boolean.valueOf(value).booleanValue());
1489 else if (key.equalsIgnoreCase("textCol1"))
1491 sg.textColour = new UserColourScheme(value).findColour('A');
1493 else if (key.equalsIgnoreCase("textCol2"))
1495 sg.textColour2 = new UserColourScheme(value).findColour('A');
1497 else if (key.equalsIgnoreCase("textColThreshold"))
1499 sg.thresholdTextColour = Integer.parseInt(value);
1501 else if (key.equalsIgnoreCase("idColour"))
1503 // consider warning if colour doesn't resolve to a real colour
1504 sg.setIdColour((def = new UserColourScheme(value))
1507 else if (key.equalsIgnoreCase("hide"))
1509 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1510 sg.setHidereps(true);
1512 else if (key.equalsIgnoreCase("hidecols"))
1514 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1515 sg.setHideCols(true);
1517 sg.recalcConservation();
1526 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1529 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1533 .print("Warning - no annotation to set below for sequence associated annotation:");
1535 while (st.hasMoreTokens())
1537 token = st.nextToken();
1540 System.err.print(" " + token);
1544 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1546 aa = al.getAlignmentAnnotation()[i];
1547 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1549 aa.belowAlignment = true;
1556 System.err.print("\n");
1560 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1562 String keyValue, key, value;
1563 while (st.hasMoreTokens())
1565 keyValue = st.nextToken();
1566 key = keyValue.substring(0, keyValue.indexOf("="));
1567 value = keyValue.substring(keyValue.indexOf("=") + 1);
1568 al.setProperty(key, value);
1573 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1576 * @param annotations
1577 * @return CSV file as a string.
1579 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1581 StringBuffer sp = new StringBuffer();
1582 for (int i = 0; i < annotations.length; i++)
1584 String atos = annotations[i].toString();
1588 int cp = atos.indexOf("\n", p);
1589 sp.append(annotations[i].label);
1593 sp.append(atos.substring(p, cp + 1));
1597 sp.append(atos.substring(p));
1603 return sp.toString();