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.
23 import jalview.analysis.Conservation;
24 import jalview.api.AlignViewportI;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.GraphLine;
30 import jalview.datamodel.HiddenSequences;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.schemes.ColourSchemeI;
34 import jalview.schemes.ColourSchemeProperty;
35 import jalview.schemes.ResidueProperties;
36 import jalview.schemes.UserColourScheme;
38 import java.io.BufferedReader;
39 import java.io.FileReader;
40 import java.io.InputStreamReader;
41 import java.io.StringReader;
43 import java.util.ArrayList;
44 import java.util.BitSet;
45 import java.util.Enumeration;
46 import java.util.Hashtable;
47 import java.util.List;
49 import java.util.StringTokenizer;
50 import java.util.Vector;
52 public class AnnotationFile
54 public AnnotationFile()
60 * character used to write newlines
62 protected String newline = System.getProperty("line.separator");
65 * set new line string and reset the output buffer
69 public void setNewlineString(String nl)
75 public String getNewlineString()
84 text = new StringBuffer("JALVIEW_ANNOTATION" + newline + "# Created: "
85 + new java.util.Date() + newline + newline);
91 * convenience method for pre-2.9 annotation files which have no view, hidden
92 * columns or hidden row keywords.
97 * @return annotation file as a string.
99 public String printAnnotations(AlignmentAnnotation[] annotations,
100 List<SequenceGroup> list, Hashtable properties)
102 return printAnnotations(annotations, list, properties, null);
107 * hold all the information about a particular view definition read from or
108 * written out in an annotations file.
112 public String viewname;
114 public HiddenSequences hidseqs;
116 public ColumnSelection hiddencols;
118 public Vector visibleGroups;
120 public Hashtable hiddenRepSeqs;
122 public ViewDef(String viewname, HiddenSequences hidseqs,
123 ColumnSelection hiddencols, Hashtable hiddenRepSeqs)
125 this.viewname = viewname;
126 this.hidseqs = hidseqs;
127 this.hiddencols = hiddencols;
128 this.hiddenRepSeqs = hiddenRepSeqs;
133 * Prepare an annotation file given a set of annotations, groups, alignment
134 * properties and views.
140 * @return annotation file
142 public String printAnnotations(AlignmentAnnotation[] annotations,
143 List<SequenceGroup> list, Hashtable properties, ViewDef[] views)
145 // TODO: resolve views issue : annotationFile could contain visible region,
146 // or full data + hidden region specifications for a view.
149 // are views defined and then annotation added to alignment or the other
153 if (annotations != null)
155 boolean oneColour = true;
156 AlignmentAnnotation row;
158 SequenceI refSeq = null;
159 SequenceGroup refGroup = null;
161 StringBuffer colours = new StringBuffer();
162 StringBuffer graphLine = new StringBuffer();
163 StringBuffer rowprops = new StringBuffer();
164 Hashtable<Integer, String> graphGroup = new Hashtable<Integer, String>();
165 Hashtable<Integer, Object[]> graphGroup_refs = new Hashtable<Integer, Object[]>();
166 BitSet graphGroupSeen = new BitSet();
168 java.awt.Color color;
170 for (int i = 0; i < annotations.length; i++)
172 row = annotations[i];
176 && !(row.graphGroup > -1 && graphGroupSeen
177 .get(row.graphGroup)))
185 // mark any sequence references for the row
186 writeSequence_Ref(refSeq, row.sequenceRef);
187 refSeq = row.sequenceRef;
188 // mark any group references for the row
189 writeGroup_Ref(refGroup, row.groupRef);
190 refGroup = row.groupRef;
192 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
193 // lookahead to check what the annotation row object actually contains.
194 for (int j = 0; row.annotations != null
195 && j < row.annotations.length
196 && (!hasGlyphs || !hasLabels || !hasValues); j++)
198 if (row.annotations[j] != null)
200 hasLabels |= (row.annotations[j].displayCharacter != null
201 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
203 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
204 hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't
207 hasText |= (row.annotations[j].description != null && row.annotations[j].description
212 if (row.graph == AlignmentAnnotation.NO_GRAPH)
214 text.append("NO_GRAPH\t");
215 hasValues = false; // only secondary structure
216 // hasLabels = false; // and annotation description string.
220 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
222 text.append("BAR_GRAPH\t");
223 hasGlyphs = false; // no secondary structure
226 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
228 hasGlyphs = false; // no secondary structure
229 text.append("LINE_GRAPH\t");
232 if (row.getThreshold() != null)
234 graphLine.append("GRAPHLINE\t");
235 graphLine.append(row.label);
236 graphLine.append("\t");
237 graphLine.append(row.getThreshold().value);
238 graphLine.append("\t");
239 graphLine.append(row.getThreshold().label);
240 graphLine.append("\t");
241 graphLine.append(jalview.util.Format.getHexString(row
242 .getThreshold().colour));
243 graphLine.append(newline);
246 if (row.graphGroup > -1)
248 graphGroupSeen.set(row.graphGroup);
249 Integer key = new Integer(row.graphGroup);
250 if (graphGroup.containsKey(key))
252 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
257 graphGroup_refs.put(key, new Object[]
258 { refSeq, refGroup });
259 graphGroup.put(key, row.label);
264 text.append(row.label + "\t");
265 if (row.description != null)
267 text.append(row.description + "\t");
269 for (int j = 0; row.annotations != null
270 && j < row.annotations.length; j++)
273 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
278 if (row.annotations[j] != null)
281 if (hasGlyphs) // could be also hasGlyphs || ...
285 if (row.annotations[j].secondaryStructure != ' ')
287 // only write out the field if its not whitespace.
288 text.append(row.annotations[j].secondaryStructure);
294 if (row.annotations[j].value != Float.NaN)
296 text.append(comma + row.annotations[j].value);
300 System.err.println("Skipping NaN - not valid value.");
301 text.append(comma + 0f);// row.annotations[j].value);
307 // TODO: labels are emitted after values for bar graphs.
308 if // empty labels are allowed, so
309 (row.annotations[j].displayCharacter != null
310 && row.annotations[j].displayCharacter.length() > 0
311 && !row.annotations[j].displayCharacter.equals(" "))
313 text.append(comma + row.annotations[j].displayCharacter);
319 if (row.annotations[j].description != null
320 && row.annotations[j].description.length() > 0
321 && !row.annotations[j].description
322 .equals(row.annotations[j].displayCharacter))
324 text.append(comma + row.annotations[j].description);
328 if (color != null && !color.equals(row.annotations[j].colour))
333 color = row.annotations[j].colour;
335 if (row.annotations[j].colour != null
336 && row.annotations[j].colour != java.awt.Color.black)
340 + jalview.util.Format
341 .getHexString(row.annotations[j].colour)
351 text.append("\t" + row.score);
354 text.append(newline);
356 if (color != null && color != java.awt.Color.black && oneColour)
358 colours.append("COLOUR\t");
359 colours.append(row.label);
360 colours.append("\t");
361 colours.append(jalview.util.Format.getHexString(color));
362 colours.append(newline);
364 if (row.scaleColLabel || row.showAllColLabels
365 || row.centreColLabels)
367 rowprops.append("ROWPROPERTIES\t");
368 rowprops.append(row.label);
369 rowprops.append("\tscaletofit=");
370 rowprops.append(row.scaleColLabel);
371 rowprops.append("\tshowalllabs=");
372 rowprops.append(row.showAllColLabels);
373 rowprops.append("\tcentrelabs=");
374 rowprops.append(row.centreColLabels);
375 rowprops.append(newline);
377 if (graphLine.length() > 0)
379 text.append(graphLine.toString());
380 graphLine.setLength(0);
384 text.append(newline);
386 text.append(colours.toString());
387 if (graphGroup.size() > 0)
389 SequenceI oldRefSeq = refSeq;
390 SequenceGroup oldRefGroup = refGroup;
391 for (Map.Entry<Integer, String> combine_statement : graphGroup
394 Object[] seqRefAndGroup = graphGroup_refs.get(combine_statement
397 writeSequence_Ref(refSeq, (SequenceI) seqRefAndGroup[0]);
398 refSeq = (SequenceI) seqRefAndGroup[0];
400 writeGroup_Ref(refGroup, (SequenceGroup) seqRefAndGroup[1]);
401 refGroup = (SequenceGroup) seqRefAndGroup[1];
402 text.append("COMBINE\t");
403 text.append(combine_statement.getValue());
404 text.append(newline);
406 writeSequence_Ref(refSeq, oldRefSeq);
409 writeGroup_Ref(refGroup, oldRefGroup);
410 refGroup = oldRefGroup;
412 text.append(rowprops.toString());
420 if (properties != null)
422 text.append(newline);
423 text.append(newline);
424 text.append("ALIGNMENT");
425 Enumeration en = properties.keys();
426 while (en.hasMoreElements())
428 String key = en.nextElement().toString();
432 text.append(properties.get(key));
434 // TODO: output alignment visualization settings here if required
438 return text.toString();
441 private Object writeGroup_Ref(SequenceGroup refGroup,
442 SequenceGroup next_refGroup)
444 if (next_refGroup == null)
447 if (refGroup != null)
449 text.append(newline);
450 text.append("GROUP_REF\t");
451 text.append("ALIGNMENT");
452 text.append(newline);
458 if (refGroup == null || refGroup != next_refGroup)
460 text.append(newline);
461 text.append("GROUP_REF\t");
462 text.append(next_refGroup.getName());
463 text.append(newline);
470 private boolean writeSequence_Ref(SequenceI refSeq, SequenceI next_refSeq)
473 if (next_refSeq == null)
477 text.append(newline);
478 text.append("SEQUENCE_REF\t");
479 text.append("ALIGNMENT");
480 text.append(newline);
486 if (refSeq == null || refSeq != next_refSeq)
488 text.append(newline);
489 text.append("SEQUENCE_REF\t");
490 text.append(next_refSeq.getName());
491 text.append(newline);
498 public void printGroups(List<SequenceGroup> list)
500 SequenceI seqrep = null;
501 for (SequenceGroup sg : list)
505 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
506 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
512 seqrep = sg.getSeqrep();
513 text.append("SEQUENCE_REF\t");
514 text.append(seqrep.getName());
515 text.append(newline);
516 text.append("SEQUENCE_GROUP\t");
517 text.append(sg.getName());
519 text.append((seqrep.findPosition(sg.getStartRes())));
521 text.append((seqrep.findPosition(sg.getEndRes())));
525 for (int s = 0; s < sg.getSize(); s++)
527 text.append(sg.getSequenceAt(s).getName());
530 text.append(newline);
531 text.append("PROPERTIES\t");
532 text.append(sg.getName());
535 if (sg.getDescription() != null)
537 text.append("description=");
538 text.append(sg.getDescription());
543 text.append("colour=");
544 text.append(ColourSchemeProperty.getColourName(sg.cs));
546 if (sg.cs.getThreshold() != 0)
548 text.append("pidThreshold=");
549 text.append(sg.cs.getThreshold());
551 if (sg.cs.conservationApplied())
553 text.append("consThreshold=");
554 text.append(sg.cs.getConservationInc());
558 text.append("outlineColour=");
559 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
562 text.append("displayBoxes=");
563 text.append(sg.getDisplayBoxes());
565 text.append("displayText=");
566 text.append(sg.getDisplayText());
568 text.append("colourText=");
569 text.append(sg.getColourText());
571 text.append("showUnconserved=");
572 text.append(sg.getShowNonconserved());
574 if (sg.textColour != java.awt.Color.black)
576 text.append("textCol1=");
577 text.append(jalview.util.Format.getHexString(sg.textColour));
580 if (sg.textColour2 != java.awt.Color.white)
582 text.append("textCol2=");
583 text.append(jalview.util.Format.getHexString(sg.textColour2));
586 if (sg.thresholdTextColour != 0)
588 text.append("textColThreshold=");
589 text.append(sg.thresholdTextColour);
592 if (sg.idColour != null)
594 text.append("idColour=");
595 text.append(jalview.util.Format.getHexString(sg.idColour));
600 text.append("hide=true\t");
604 text.append("hidecols=true\t");
608 // terminate the last line and clear the sequence ref for the group
609 text.append(newline);
610 text.append("SEQUENCE_REF");
612 text.append(newline);
613 text.append(newline);
618 SequenceI refSeq = null;
620 String refSeqId = null;
622 public boolean readAnnotationFile(AlignmentI al, String file,
625 BufferedReader in = null;
628 if (protocol.equals(AppletFormatAdapter.FILE))
630 in = new BufferedReader(new FileReader(file));
632 else if (protocol.equals(AppletFormatAdapter.URL))
634 URL url = new URL(file);
635 in = new BufferedReader(new InputStreamReader(url.openStream()));
637 else if (protocol.equals(AppletFormatAdapter.PASTE))
639 in = new BufferedReader(new StringReader(file));
641 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
643 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
646 in = new BufferedReader(new java.io.InputStreamReader(is));
651 return parseAnnotationFrom(al, colSel, in);
654 } catch (Exception ex)
656 ex.printStackTrace();
657 System.out.println("Problem reading annotation file: " + ex);
660 System.out.println("Last read line " + nlinesread + ": '"
661 + lastread + "' (first 80 chars) ...");
670 String lastread = "";
672 private static String GRAPHLINE = "GRAPHLINE", COMBINE = "COMBINE";
674 public boolean parseAnnotationFrom(AlignmentI al, ColumnSelection colSel,
679 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
680 ArrayList<Object[]> deferredAnnotation_calls = new ArrayList<Object[]>();
681 boolean modified = false;
682 String groupRef = null;
683 Hashtable groupRefRows = new Hashtable();
685 Hashtable autoAnnots = new Hashtable();
687 String line, label, description, token;
688 int graphStyle, index;
690 int existingAnnotations = 0;
691 // when true - will add new rows regardless of whether they are duplicate
692 // auto-annotation like consensus or conservation graphs
693 boolean overrideAutoAnnot = false;
694 if (al.getAlignmentAnnotation() != null)
696 existingAnnotations = al.getAlignmentAnnotation().length;
697 if (existingAnnotations > 0)
699 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
700 for (int aai = 0; aai < aa.length; aai++)
702 if (aa[aai].autoCalculated)
704 // make a note of the name and description
706 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
707 (aa[aai].groupRef == null ? null
708 : aa[aai].groupRef.getName())),
715 int alWidth = al.getWidth();
718 Annotation[] annotations;
719 AlignmentAnnotation annotation = null;
721 // First confirm this is an Annotation file
722 boolean jvAnnotationFile = false;
723 while ((line = in.readLine()) != null)
726 lastread = new String(line);
727 if (line.indexOf("#") == 0)
732 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
734 jvAnnotationFile = true;
739 if (!jvAnnotationFile)
745 while ((line = in.readLine()) != null)
748 lastread = new String(line);
749 if (line.indexOf("#") == 0
750 || line.indexOf("JALVIEW_ANNOTATION") > -1
751 || line.length() == 0)
756 st = new StringTokenizer(line, "\t");
757 token = st.nextToken();
758 if (token.equalsIgnoreCase("COLOUR"))
760 // TODO: use graduated colour def'n here too
761 colourAnnotations(al, st.nextToken(), st.nextToken());
766 else if (token.equalsIgnoreCase(COMBINE))
768 // keep a record of current state and resolve groupRef at end
769 combineAnnotation_calls.add(new Object[]
770 { st, refSeq, groupRef });
774 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
776 addRowProperties(al, st);
780 else if (token.equalsIgnoreCase(GRAPHLINE))
783 deferredAnnotation_calls.add(new Object[]
784 { GRAPHLINE, st, refSeq, groupRef });
789 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
791 if (st.hasMoreTokens())
793 refSeq = al.findName(refSeqId = st.nextToken());
800 refSeqIndex = Integer.parseInt(st.nextToken());
805 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
807 } catch (Exception ex)
819 else if (token.equalsIgnoreCase("GROUP_REF"))
821 // Group references could be forward or backwards, so they are
822 // resolved after the whole file is read in
824 if (st.hasMoreTokens())
826 groupRef = st.nextToken();
827 if (groupRef.length() < 1)
829 groupRef = null; // empty string
833 if (groupRefRows.get(groupRef) == null)
835 groupRefRows.put(groupRef, new Vector());
841 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
848 else if (token.equalsIgnoreCase("PROPERTIES"))
850 addProperties(al, st);
855 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
857 setBelowAlignment(al, st);
861 else if (token.equalsIgnoreCase("ALIGNMENT"))
863 addAlignmentDetails(al, st);
868 // Parse out the annotation row
869 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
870 label = st.nextToken();
873 annotations = new Annotation[alWidth];
875 float score = Float.NaN;
877 if (st.hasMoreTokens())
879 line = st.nextToken();
881 if (line.indexOf("|") == -1)
884 if (st.hasMoreTokens())
886 line = st.nextToken();
890 if (st.hasMoreTokens())
892 // This must be the score
893 score = Float.valueOf(st.nextToken()).floatValue();
896 st = new StringTokenizer(line, "|", true);
898 boolean emptyColumn = true;
899 boolean onlyOneElement = (st.countTokens() == 1);
901 while (st.hasMoreElements() && index < alWidth)
903 token = st.nextToken().trim();
909 score = Float.valueOf(token).floatValue();
911 } catch (NumberFormatException ex)
916 if (token.equals("|"))
927 annotations[index++] = parseAnnotation(token, graphStyle);
934 annotation = new AlignmentAnnotation(label, description,
935 (index == 0) ? null : annotations, 0, 0, graphStyle);
937 annotation.score = score;
938 if (!overrideAutoAnnot
939 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
942 // skip - we've already got an automatic annotation of this type.
949 annotation.belowAlignment = false;
950 // make a copy of refSeq so we can find other matches in the alignment
951 SequenceI referedSeq = refSeq;
954 // copy before we do any mapping business.
955 // TODO: verify that undo/redo with 1:many sequence associated
956 // annotations can be undone correctly
957 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
959 .createSequenceMapping(referedSeq, refSeqIndex, false);
960 annotation.adjustForAlignment();
961 referedSeq.addAlignmentAnnotation(annotation);
962 al.addAnnotation(annotation);
963 al.setAnnotationIndex(annotation,
964 al.getAlignmentAnnotation().length
965 - existingAnnotations - 1);
966 if (groupRef != null)
968 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
970 // and recover our virgin copy to use again if necessary.
973 } while (refSeqId != null
974 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
978 al.addAnnotation(annotation);
979 al.setAnnotationIndex(annotation,
980 al.getAlignmentAnnotation().length - existingAnnotations
982 if (groupRef != null)
984 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
987 // and set modification flag
990 // Resolve the groupRefs
991 Hashtable<String, SequenceGroup> groupRefLookup = new Hashtable<String, SequenceGroup>();
992 Enumeration en = groupRefRows.keys();
994 while (en.hasMoreElements())
996 groupRef = (String) en.nextElement();
997 boolean matched = false;
998 // Resolve group: TODO: add a getGroupByName method to alignments
999 for (SequenceGroup theGroup : al.getGroups())
1001 if (theGroup.getName().equals(groupRef))
1005 // TODO: specify and implement duplication of alignment annotation
1006 // for multiple group references.
1008 .println("Ignoring 1:many group reference mappings for group name '"
1014 Vector rowset = (Vector) groupRefRows.get(groupRef);
1015 groupRefLookup.put(groupRef, theGroup);
1016 if (rowset != null && rowset.size() > 0)
1018 AlignmentAnnotation alan = null;
1019 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
1021 alan = (AlignmentAnnotation) rowset.elementAt(elm);
1022 alan.groupRef = theGroup;
1028 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
1030 // process any deferred attribute settings for each context
1031 for (Object[] _deferred_args : deferredAnnotation_calls)
1033 if (_deferred_args[0] == GRAPHLINE)
1036 (StringTokenizer) _deferred_args[1], // st
1037 (SequenceI) _deferred_args[2], // refSeq
1038 (_deferred_args[3] == null) ? null : groupRefLookup
1039 .get(_deferred_args[3]) // the reference
1045 // finally, combine all the annotation rows within each context.
1047 * number of combine statements in this annotation file. Used to create
1048 * new groups for combined annotation graphs without disturbing existing
1051 int combinecount = 0;
1052 for (Object[] _combine_args : combineAnnotation_calls)
1054 combineAnnotations(al,
1056 (StringTokenizer) _combine_args[0], // st
1057 (SequenceI) _combine_args[1], // refSeq
1058 (_combine_args[2] == null) ? null : groupRefLookup
1059 .get(_combine_args[2]) // the reference group,
1067 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1068 SequenceI refSeq, String groupRef)
1070 return annotation.graph + "\t" + annotation.label + "\t"
1071 + annotation.description + "\t"
1072 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1075 Annotation parseAnnotation(String string, int graphStyle)
1077 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
1086 String desc = null, displayChar = null;
1087 char ss = ' '; // secondaryStructure
1089 boolean parsedValue = false, dcset = false;
1092 java.awt.Color colour = null;
1093 int i = string.indexOf("[");
1094 int j = string.indexOf("]");
1095 if (i > -1 && j > -1)
1097 UserColourScheme ucs = new UserColourScheme();
1099 colour = ucs.getColourFromString(string.substring(i + 1, j));
1100 if (i > 0 && string.charAt(i - 1) == ',')
1102 // clip the preceding comma as well
1105 string = string.substring(0, i) + string.substring(j + 1);
1108 StringTokenizer st = new StringTokenizer(string, ",", true);
1110 boolean seenContent = false;
1112 while (st.hasMoreTokens())
1115 token = st.nextToken().trim();
1116 if (token.equals(","))
1118 if (!seenContent && parsedValue && !dcset)
1120 // allow the value below the bar/line to be empty
1124 seenContent = false;
1136 displayChar = token;
1138 value = new Float(token).floatValue();
1141 } catch (NumberFormatException ex)
1147 if (token.length() == 1)
1149 displayChar = token;
1153 && (token.equals("H") || token.equals("E")
1154 || token.equals("S") || token.equals(" ")))
1156 // Either this character represents a helix or sheet
1157 // or an integer which can be displayed
1158 ss = token.charAt(0);
1159 if (displayChar.equals(token.substring(0, 1)))
1164 else if (desc == null || (parsedValue && pass > 2))
1170 // if (!dcset && string.charAt(string.length() - 1) == ',')
1172 // displayChar = " "; // empty display char symbol.
1174 if (displayChar != null && desc != null && desc.length() == 1)
1176 if (displayChar.length() > 1)
1178 // switch desc and displayChar - legacy support
1179 String tmp = displayChar;
1185 if (displayChar.equals(desc))
1187 // duplicate label - hangover from the 'robust parser' above
1192 Annotation anot = new Annotation(displayChar, desc, ss, value);
1194 anot.colour = colour;
1199 void colourAnnotations(AlignmentI al, String label, String colour)
1201 UserColourScheme ucs = new UserColourScheme(colour);
1202 Annotation[] annotations;
1203 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1205 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1207 annotations = al.getAlignmentAnnotation()[i].annotations;
1208 for (int j = 0; j < annotations.length; j++)
1210 if (annotations[j] != null)
1212 annotations[j].colour = ucs.findColour('A');
1219 void combineAnnotations(AlignmentI al, int combineCount,
1220 StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1222 String group = st.nextToken();
1223 // First make sure we are not overwriting the graphIndex
1225 if (al.getAlignmentAnnotation() != null)
1227 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1229 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1231 if (aa.graphGroup > graphGroup)
1233 // try to number graphGroups in order of occurence.
1234 graphGroup = aa.graphGroup + 1;
1236 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1237 && aa.label.equalsIgnoreCase(group))
1239 if (aa.graphGroup > -1)
1241 graphGroup = aa.graphGroup;
1245 if (graphGroup <= combineCount)
1247 graphGroup = combineCount + 1;
1249 aa.graphGroup = graphGroup;
1255 // Now update groups
1256 while (st.hasMoreTokens())
1258 group = st.nextToken();
1259 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1261 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1262 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1263 && aa.label.equalsIgnoreCase(group))
1265 aa.graphGroup = graphGroup;
1274 .println("Couldn't combine annotations. None are added to alignment yet!");
1278 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef,
1279 SequenceGroup groupRef)
1281 String group = st.nextToken();
1282 AlignmentAnnotation annotation = null, alannot[] = al
1283 .getAlignmentAnnotation();
1284 float value = new Float(st.nextToken()).floatValue();
1285 String label = st.hasMoreTokens() ? st.nextToken() : null;
1286 java.awt.Color colour = null;
1287 if (st.hasMoreTokens())
1289 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1290 colour = ucs.findColour('A');
1292 if (alannot != null)
1294 for (int i = 0; i < alannot.length; i++)
1296 if (alannot[i].label.equalsIgnoreCase(group)
1297 && (seqRef == null || alannot[i].sequenceRef == seqRef)
1298 && (groupRef == null || alannot[i].groupRef == groupRef))
1300 alannot[i].setThreshold(new GraphLine(value, label, colour));
1304 if (annotation == null)
1310 void addGroup(AlignmentI al, StringTokenizer st)
1312 SequenceGroup sg = new SequenceGroup();
1313 sg.setName(st.nextToken());
1317 rng = st.nextToken();
1318 if (rng.length() > 0 && !rng.startsWith("*"))
1320 sg.setStartRes(Integer.parseInt(rng) - 1);
1326 rng = st.nextToken();
1327 if (rng.length() > 0 && !rng.startsWith("*"))
1329 sg.setEndRes(Integer.parseInt(rng) - 1);
1333 sg.setEndRes(al.getWidth() - 1);
1335 } catch (Exception e)
1338 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1339 + rng + "' - assuming alignment width for group.");
1340 // assume group is full width
1342 sg.setEndRes(al.getWidth() - 1);
1345 String index = st.nextToken();
1346 if (index.equals("-1"))
1348 while (st.hasMoreElements())
1350 sg.addSequence(al.findName(st.nextToken()), false);
1355 StringTokenizer st2 = new StringTokenizer(index, ",");
1357 while (st2.hasMoreTokens())
1359 String tmp = st2.nextToken();
1360 if (tmp.equals("*"))
1362 for (int i = 0; i < al.getHeight(); i++)
1364 sg.addSequence(al.getSequenceAt(i), false);
1367 else if (tmp.indexOf("-") >= 0)
1369 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1371 int start = (Integer.parseInt(st3.nextToken()));
1372 int end = (Integer.parseInt(st3.nextToken()));
1376 for (int i = start; i <= end; i++)
1378 sg.addSequence(al.getSequenceAt(i - 1), false);
1384 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1391 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1392 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1393 sg.setSeqrep(refSeq);
1396 if (sg.getSize() > 0)
1402 void addRowProperties(AlignmentI al, StringTokenizer st)
1404 String label = st.nextToken(), keyValue, key, value;
1405 boolean scaletofit = false, centerlab = false, showalllabs = false;
1406 while (st.hasMoreTokens())
1408 keyValue = st.nextToken();
1409 key = keyValue.substring(0, keyValue.indexOf("="));
1410 value = keyValue.substring(keyValue.indexOf("=") + 1);
1411 if (key.equalsIgnoreCase("scaletofit"))
1413 scaletofit = Boolean.valueOf(value).booleanValue();
1415 if (key.equalsIgnoreCase("showalllabs"))
1417 showalllabs = Boolean.valueOf(value).booleanValue();
1419 if (key.equalsIgnoreCase("centrelabs"))
1421 centerlab = Boolean.valueOf(value).booleanValue();
1423 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1426 for (int i = 0; i < alr.length; i++)
1428 if (alr[i].label.equalsIgnoreCase(label))
1430 alr[i].centreColLabels = centerlab;
1431 alr[i].scaleColLabel = scaletofit;
1432 alr[i].showAllColLabels = showalllabs;
1439 void addProperties(AlignmentI al, StringTokenizer st)
1442 // So far we have only added groups to the annotationHash,
1443 // the idea is in the future properties can be added to
1444 // alignments, other annotations etc
1445 if (al.getGroups() == null)
1450 String name = st.nextToken();
1451 SequenceGroup sg = null;
1452 for (SequenceGroup _sg : al.getGroups())
1454 if ((sg = _sg).getName().equals(name))
1466 String keyValue, key, value;
1467 ColourSchemeI def = sg.cs;
1469 while (st.hasMoreTokens())
1471 keyValue = st.nextToken();
1472 key = keyValue.substring(0, keyValue.indexOf("="));
1473 value = keyValue.substring(keyValue.indexOf("=") + 1);
1475 if (key.equalsIgnoreCase("description"))
1477 sg.setDescription(value);
1479 else if (key.equalsIgnoreCase("colour"))
1481 sg.cs = ColourSchemeProperty.getColour(al, value);
1483 else if (key.equalsIgnoreCase("pidThreshold"))
1485 sg.cs.setThreshold(Integer.parseInt(value), true);
1488 else if (key.equalsIgnoreCase("consThreshold"))
1490 sg.cs.setConservationInc(Integer.parseInt(value));
1491 Conservation c = new Conservation("Group",
1492 ResidueProperties.propHash, 3, sg.getSequences(null),
1493 sg.getStartRes(), sg.getEndRes() + 1);
1496 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1498 sg.cs.setConservation(c);
1501 else if (key.equalsIgnoreCase("outlineColour"))
1503 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1505 else if (key.equalsIgnoreCase("displayBoxes"))
1507 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1509 else if (key.equalsIgnoreCase("showUnconserved"))
1511 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1513 else if (key.equalsIgnoreCase("displayText"))
1515 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1517 else if (key.equalsIgnoreCase("colourText"))
1519 sg.setColourText(Boolean.valueOf(value).booleanValue());
1521 else if (key.equalsIgnoreCase("textCol1"))
1523 sg.textColour = new UserColourScheme(value).findColour('A');
1525 else if (key.equalsIgnoreCase("textCol2"))
1527 sg.textColour2 = new UserColourScheme(value).findColour('A');
1529 else if (key.equalsIgnoreCase("textColThreshold"))
1531 sg.thresholdTextColour = Integer.parseInt(value);
1533 else if (key.equalsIgnoreCase("idColour"))
1535 // consider warning if colour doesn't resolve to a real colour
1536 sg.setIdColour((def = new UserColourScheme(value))
1539 else if (key.equalsIgnoreCase("hide"))
1541 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1542 sg.setHidereps(true);
1544 else if (key.equalsIgnoreCase("hidecols"))
1546 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1547 sg.setHideCols(true);
1549 sg.recalcConservation();
1558 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1561 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1565 .print("Warning - no annotation to set below for sequence associated annotation:");
1567 while (st.hasMoreTokens())
1569 token = st.nextToken();
1572 System.err.print(" " + token);
1576 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1578 aa = al.getAlignmentAnnotation()[i];
1579 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1581 aa.belowAlignment = true;
1588 System.err.print("\n");
1592 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1594 String keyValue, key, value;
1595 while (st.hasMoreTokens())
1597 keyValue = st.nextToken();
1598 key = keyValue.substring(0, keyValue.indexOf("="));
1599 value = keyValue.substring(keyValue.indexOf("=") + 1);
1600 al.setProperty(key, value);
1605 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1608 * @param annotations
1609 * @return CSV file as a string.
1611 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1613 StringBuffer sp = new StringBuffer();
1614 for (int i = 0; i < annotations.length; i++)
1616 String atos = annotations[i].toString();
1620 int cp = atos.indexOf("\n", p);
1621 sp.append(annotations[i].label);
1625 sp.append(atos.substring(p, cp + 1));
1629 sp.append(atos.substring(p));
1635 return sp.toString();