2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
25 import jalview.analysis.*;
26 import jalview.datamodel.*;
27 import jalview.schemes.*;
29 public class AnnotationFile
31 public AnnotationFile()
37 * character used to write newlines
39 protected String newline = System.getProperty("line.separator");
42 * set new line string and reset the output buffer
46 public void setNewlineString(String nl)
52 public String getNewlineString()
61 text = new StringBuffer("JALVIEW_ANNOTATION" + newline + "# Created: "
62 + new java.util.Date() + newline + newline);
68 * convenience method for pre-2.4 feature files which have no view, hidden
69 * columns or hidden row keywords.
74 * @return feature file as a string.
76 public String printAnnotations(AlignmentAnnotation[] annotations,
77 List<SequenceGroup> list, Hashtable properties)
79 return printAnnotations(annotations, list, properties, null);
84 * hold all the information about a particular view definition read from or
85 * written out in an annotations file.
89 public String viewname;
91 public HiddenSequences hidseqs;
93 public ColumnSelection hiddencols;
95 public Vector visibleGroups;
97 public Hashtable hiddenRepSeqs;
99 public ViewDef(String viewname, HiddenSequences hidseqs,
100 ColumnSelection hiddencols, Hashtable hiddenRepSeqs)
102 this.viewname = viewname;
103 this.hidseqs = hidseqs;
104 this.hiddencols = hiddencols;
105 this.hiddenRepSeqs = hiddenRepSeqs;
110 * Prepare an annotation file given a set of annotations, groups, alignment
111 * properties and views.
117 * @return annotation file
119 public String printAnnotations(AlignmentAnnotation[] annotations,
120 List<SequenceGroup> list, Hashtable properties, ViewDef[] views)
122 // TODO: resolve views issue : annotationFile could contain visible region,
123 // or full data + hidden region specifications for a view.
124 if (annotations != null)
126 boolean oneColour = true;
127 AlignmentAnnotation row;
129 SequenceI refSeq = null;
130 SequenceGroup refGroup = null;
132 StringBuffer colours = new StringBuffer();
133 StringBuffer graphLine = new StringBuffer();
134 StringBuffer rowprops = new StringBuffer();
135 Hashtable<Integer,String> graphGroup = new Hashtable<Integer,String>();
136 Hashtable<Integer, Object[]> graphGroup_refs = new Hashtable<Integer,Object[]>();
137 BitSet graphGroupSeen = new BitSet();
139 java.awt.Color color;
141 for (int i = 0; i < annotations.length; i++)
143 row = annotations[i];
145 if (!row.visible && !row.hasScore() && !(row.graphGroup>-1 && graphGroupSeen.get(row.graphGroup)))
153 // mark any sequence references for the row
154 writeSequence_Ref(refSeq ,row.sequenceRef);
155 refSeq = row.sequenceRef;
156 // mark any group references for the row
157 writeGroup_Ref(refGroup, row.groupRef);
158 refGroup = row.groupRef;
160 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
161 // lookahead to check what the annotation row object actually contains.
162 for (int j = 0; row.annotations != null
163 && j < row.annotations.length
164 && (!hasGlyphs || !hasLabels || !hasValues); j++)
166 if (row.annotations[j] != null)
168 hasLabels |= (row.annotations[j].displayCharacter != null
169 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
171 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
172 hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't
175 hasText |= (row.annotations[j].description != null && row.annotations[j].description
180 if (row.graph == AlignmentAnnotation.NO_GRAPH)
182 text.append("NO_GRAPH\t");
183 hasValues = false; // only secondary structure
184 // hasLabels = false; // and annotation description string.
188 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
190 text.append("BAR_GRAPH\t");
191 hasGlyphs = false; // no secondary structure
194 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
196 hasGlyphs = false; // no secondary structure
197 text.append("LINE_GRAPH\t");
200 if (row.getThreshold() != null)
202 graphLine.append("GRAPHLINE\t");
203 graphLine.append(row.label);
204 graphLine.append("\t");
205 graphLine.append(row.getThreshold().value);
206 graphLine.append("\t");
207 graphLine.append(row.getThreshold().label);
208 graphLine.append("\t");
209 graphLine.append(jalview.util.Format.getHexString(row
210 .getThreshold().colour));
211 graphLine.append(newline);
214 if (row.graphGroup > -1)
216 graphGroupSeen.set(row.graphGroup);
217 Integer key = new Integer(row.graphGroup);
218 if (graphGroup.containsKey(key))
220 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
225 graphGroup_refs.put(key, new Object[] { refSeq, refGroup});
226 graphGroup.put(key, row.label);
231 text.append(row.label + "\t");
232 if (row.description != null)
234 text.append(row.description + "\t");
236 for (int j = 0; row.annotations != null
237 && j < row.annotations.length; j++)
240 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
245 if (row.annotations[j] != null)
248 if (hasGlyphs) // could be also hasGlyphs || ...
252 if (row.annotations[j].secondaryStructure != ' ')
254 // only write out the field if its not whitespace.
255 text.append(row.annotations[j].secondaryStructure);
261 if (row.annotations[j].value != Float.NaN)
263 text.append(comma + row.annotations[j].value);
267 System.err.println("Skipping NaN - not valid value.");
268 text.append(comma + 0f);// row.annotations[j].value);
274 // TODO: labels are emitted after values for bar graphs.
275 if // empty labels are allowed, so
276 (row.annotations[j].displayCharacter != null
277 && row.annotations[j].displayCharacter.length() > 0
278 && !row.annotations[j].displayCharacter.equals(" "))
280 text.append(comma + row.annotations[j].displayCharacter);
286 if (row.annotations[j].description != null
287 && row.annotations[j].description.length() > 0
288 && !row.annotations[j].description
289 .equals(row.annotations[j].displayCharacter))
291 text.append(comma + row.annotations[j].description);
295 if (color != null && !color.equals(row.annotations[j].colour))
300 color = row.annotations[j].colour;
302 if (row.annotations[j].colour != null
303 && row.annotations[j].colour != java.awt.Color.black)
307 + jalview.util.Format
308 .getHexString(row.annotations[j].colour)
317 text.append("\t" + row.score);
319 text.append(newline);
321 if (color != null && color != java.awt.Color.black && oneColour)
323 colours.append("COLOUR\t");
324 colours.append(row.label);
325 colours.append("\t");
326 colours.append(jalview.util.Format.getHexString(color));
327 colours.append(newline);
329 if (row.scaleColLabel || row.showAllColLabels
330 || row.centreColLabels)
332 rowprops.append("ROWPROPERTIES\t");
333 rowprops.append(row.label);
334 rowprops.append("\tscaletofit=");
335 rowprops.append(row.scaleColLabel);
336 rowprops.append("\tshowalllabs=");
337 rowprops.append(row.showAllColLabels);
338 rowprops.append("\tcentrelabs=");
339 rowprops.append(row.centreColLabels);
340 rowprops.append(newline);
342 if (graphLine.length()>0) {
343 text.append(graphLine.toString());
344 graphLine.setLength(0);
348 text.append(newline);
350 text.append(colours.toString());
351 if (graphGroup.size() > 0)
353 SequenceI oldRefSeq = refSeq;
354 SequenceGroup oldRefGroup = refGroup;
355 for (Map.Entry<Integer, String> combine_statement:graphGroup.entrySet())
357 Object[] seqRefAndGroup=graphGroup_refs.get(combine_statement.getKey());
359 writeSequence_Ref(refSeq, (SequenceI)seqRefAndGroup[0]);
360 refSeq = (SequenceI)seqRefAndGroup[0];
362 writeGroup_Ref(refGroup, (SequenceGroup)seqRefAndGroup[1]);
363 refGroup = (SequenceGroup)seqRefAndGroup[1];
364 text.append("COMBINE\t");
365 text.append(combine_statement.getValue());
366 text.append(newline);
368 writeSequence_Ref(refSeq, oldRefSeq);
371 writeGroup_Ref(refGroup, oldRefGroup);
372 refGroup = oldRefGroup;
374 text.append(rowprops.toString());
382 if (properties != null)
384 text.append(newline);
385 text.append(newline);
386 text.append("ALIGNMENT");
387 Enumeration en = properties.keys();
388 while (en.hasMoreElements())
390 String key = en.nextElement().toString();
394 text.append(properties.get(key));
396 // TODO: output alignment visualization settings here if required
400 return text.toString();
403 private Object writeGroup_Ref(SequenceGroup refGroup, SequenceGroup next_refGroup)
405 if (next_refGroup == null)
408 if (refGroup != null)
410 text.append(newline);
411 text.append("GROUP_REF\t");
412 text.append("ALIGNMENT");
413 text.append(newline);
419 if (refGroup == null || refGroup != next_refGroup)
421 text.append(newline);
422 text.append("GROUP_REF\t");
423 text.append(next_refGroup.getName());
424 text.append(newline);
431 private boolean writeSequence_Ref(SequenceI refSeq, SequenceI next_refSeq)
434 if (next_refSeq==null)
438 text.append(newline);
439 text.append("SEQUENCE_REF\t");
440 text.append("ALIGNMENT");
441 text.append(newline);
447 if (refSeq == null || refSeq != next_refSeq)
449 text.append(newline);
450 text.append("SEQUENCE_REF\t");
451 text.append(next_refSeq.getName());
452 text.append(newline);
459 public void printGroups(List<SequenceGroup> list)
461 SequenceI seqrep = null;
462 for (SequenceGroup sg : list)
466 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
467 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
473 seqrep = sg.getSeqrep();
474 text.append("SEQUENCE_REF\t");
475 text.append(seqrep.getName());
476 text.append(newline);
477 text.append("SEQUENCE_GROUP\t");
478 text.append(sg.getName());
480 text.append((seqrep.findPosition(sg.getStartRes())));
482 text.append((seqrep.findPosition(sg.getEndRes())));
486 for (int s = 0; s < sg.getSize(); s++)
488 text.append(sg.getSequenceAt(s).getName());
491 text.append(newline);
492 text.append("PROPERTIES\t");
493 text.append(sg.getName());
496 if (sg.getDescription() != null)
498 text.append("description=");
499 text.append(sg.getDescription());
504 text.append("colour=");
505 text.append(ColourSchemeProperty.getColourName(sg.cs));
507 if (sg.cs.getThreshold() != 0)
509 text.append("pidThreshold=");
510 text.append(sg.cs.getThreshold());
512 if (sg.cs.conservationApplied())
514 text.append("consThreshold=");
515 text.append(sg.cs.getConservationInc());
519 text.append("outlineColour=");
520 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
523 text.append("displayBoxes=");
524 text.append(sg.getDisplayBoxes());
526 text.append("displayText=");
527 text.append(sg.getDisplayText());
529 text.append("colourText=");
530 text.append(sg.getColourText());
532 text.append("showUnconserved=");
533 text.append(sg.getShowNonconserved());
535 if (sg.textColour != java.awt.Color.black)
537 text.append("textCol1=");
538 text.append(jalview.util.Format.getHexString(sg.textColour));
541 if (sg.textColour2 != java.awt.Color.white)
543 text.append("textCol2=");
544 text.append(jalview.util.Format.getHexString(sg.textColour2));
547 if (sg.thresholdTextColour != 0)
549 text.append("textColThreshold=");
550 text.append(sg.thresholdTextColour);
553 if (sg.idColour != null)
555 text.append("idColour=");
556 text.append(jalview.util.Format.getHexString(sg.idColour));
561 text.append("hide=true\t");
565 text.append("hidecols=true\t");
569 // terminate the last line and clear the sequence ref for the group
570 text.append(newline);
571 text.append("SEQUENCE_REF");
573 text.append(newline);
574 text.append(newline);
579 SequenceI refSeq = null;
581 String refSeqId = null;
583 public boolean readAnnotationFile(AlignmentI al, String file,
586 BufferedReader in = null;
589 if (protocol.equals(AppletFormatAdapter.FILE))
591 in = new BufferedReader(new FileReader(file));
593 else if (protocol.equals(AppletFormatAdapter.URL))
595 URL url = new URL(file);
596 in = new BufferedReader(new InputStreamReader(url.openStream()));
598 else if (protocol.equals(AppletFormatAdapter.PASTE))
600 in = new BufferedReader(new StringReader(file));
602 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
604 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
607 in = new BufferedReader(new java.io.InputStreamReader(is));
612 return parseAnnotationFrom(al, in);
615 } catch (Exception ex)
617 ex.printStackTrace();
618 System.out.println("Problem reading annotation file: " + ex);
620 System.out.println("Last read line "+nlinesread+": '"+lastread+"' (first 80 chars) ...");
628 private static String GRAPHLINE="GRAPHLINE", COMBINE="COMBINE";
629 public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)
633 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
634 ArrayList<Object[]> deferredAnnotation_calls = new ArrayList<Object[]>();
635 boolean modified = false;
636 String groupRef = null;
637 Hashtable groupRefRows = new Hashtable();
639 Hashtable autoAnnots = new Hashtable();
641 String line, label, description, token;
642 int graphStyle, index;
644 int existingAnnotations = 0;
645 // when true - will add new rows regardless of whether they are duplicate
646 // auto-annotation like consensus or conservation graphs
647 boolean overrideAutoAnnot = false;
648 if (al.getAlignmentAnnotation() != null)
650 existingAnnotations = al.getAlignmentAnnotation().length;
651 if (existingAnnotations > 0)
653 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
654 for (int aai = 0; aai < aa.length; aai++)
656 if (aa[aai].autoCalculated)
658 // make a note of the name and description
660 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
661 (aa[aai].groupRef == null ? null
662 : aa[aai].groupRef.getName())),
669 int alWidth = al.getWidth();
672 Annotation[] annotations;
673 AlignmentAnnotation annotation = null;
675 // First confirm this is an Annotation file
676 boolean jvAnnotationFile = false;
677 while ((line = in.readLine()) != null)
679 nlinesread++;lastread = new String(line);
680 if (line.indexOf("#") == 0)
685 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
687 jvAnnotationFile = true;
692 if (!jvAnnotationFile)
698 while ((line = in.readLine()) != null)
700 nlinesread++;lastread = new String(line);
701 if (line.indexOf("#") == 0
702 || line.indexOf("JALVIEW_ANNOTATION") > -1
703 || line.length() == 0)
708 st = new StringTokenizer(line, "\t");
709 token = st.nextToken();
710 if (token.equalsIgnoreCase("COLOUR"))
712 // TODO: use graduated colour def'n here too
713 colourAnnotations(al, st.nextToken(), st.nextToken());
718 else if (token.equalsIgnoreCase(COMBINE))
720 // keep a record of current state and resolve groupRef at end
721 combineAnnotation_calls.add(new Object[] { st, refSeq, groupRef});
725 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
727 addRowProperties(al, st);
731 else if (token.equalsIgnoreCase(GRAPHLINE))
734 deferredAnnotation_calls.add(new Object[] { GRAPHLINE, st, refSeq, groupRef});
739 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
741 if (st.hasMoreTokens())
743 refSeq = al.findName(refSeqId = st.nextToken());
750 refSeqIndex = Integer.parseInt(st.nextToken());
755 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
757 } catch (Exception ex)
769 else if (token.equalsIgnoreCase("GROUP_REF"))
771 // Group references could be forward or backwards, so they are
772 // resolved after the whole file is read in
774 if (st.hasMoreTokens())
776 groupRef = st.nextToken();
777 if (groupRef.length() < 1)
779 groupRef = null; // empty string
783 if (groupRefRows.get(groupRef) == null)
785 groupRefRows.put(groupRef, new Vector());
791 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
798 else if (token.equalsIgnoreCase("PROPERTIES"))
800 addProperties(al, st);
805 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
807 setBelowAlignment(al, st);
811 else if (token.equalsIgnoreCase("ALIGNMENT"))
813 addAlignmentDetails(al, st);
818 // Parse out the annotation row
819 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
820 label = st.nextToken();
823 annotations = new Annotation[alWidth];
825 float score = Float.NaN;
827 if (st.hasMoreTokens())
829 line = st.nextToken();
831 if (line.indexOf("|") == -1)
834 if (st.hasMoreTokens())
835 line = st.nextToken();
838 if (st.hasMoreTokens())
840 // This must be the score
841 score = Float.valueOf(st.nextToken()).floatValue();
844 st = new StringTokenizer(line, "|", true);
846 boolean emptyColumn = true;
847 boolean onlyOneElement = (st.countTokens() == 1);
849 while (st.hasMoreElements() && index < alWidth)
851 token = st.nextToken().trim();
857 score = Float.valueOf(token).floatValue();
859 } catch (NumberFormatException ex)
864 if (token.equals("|"))
875 annotations[index++] = parseAnnotation(token, graphStyle);
882 annotation = new AlignmentAnnotation(label, description,
883 (index == 0) ? null : annotations, 0, 0, graphStyle);
885 annotation.score = score;
886 if (!overrideAutoAnnot
887 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
890 // skip - we've already got an automatic annotation of this type.
897 annotation.belowAlignment = false;
898 // make a copy of refSeq so we can find other matches in the alignment
899 SequenceI referedSeq = refSeq;
902 // copy before we do any mapping business.
903 // TODO: verify that undo/redo with 1:many sequence associated
904 // annotations can be undone correctly
905 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
907 .createSequenceMapping(referedSeq, refSeqIndex, false);
908 annotation.adjustForAlignment();
909 referedSeq.addAlignmentAnnotation(annotation);
910 al.addAnnotation(annotation);
911 al.setAnnotationIndex(annotation,
912 al.getAlignmentAnnotation().length
913 - existingAnnotations - 1);
914 if (groupRef != null)
916 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
918 // and recover our virgin copy to use again if necessary.
921 } while (refSeqId != null
922 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
926 al.addAnnotation(annotation);
927 al.setAnnotationIndex(annotation,
928 al.getAlignmentAnnotation().length - existingAnnotations
930 if (groupRef != null)
932 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
935 // and set modification flag
938 // Resolve the groupRefs
939 Hashtable <String,SequenceGroup> groupRefLookup=new Hashtable<String,SequenceGroup>();
940 Enumeration en = groupRefRows.keys();
942 while (en.hasMoreElements())
944 groupRef = (String) en.nextElement();
945 boolean matched = false;
946 // Resolve group: TODO: add a getGroupByName method to alignments
947 for (SequenceGroup theGroup : al.getGroups())
949 if (theGroup.getName().equals(groupRef))
953 // TODO: specify and implement duplication of alignment annotation
954 // for multiple group references.
956 .println("Ignoring 1:many group reference mappings for group name '"
962 Vector rowset = (Vector) groupRefRows.get(groupRef);
963 groupRefLookup.put(groupRef, theGroup);
964 if (rowset != null && rowset.size() > 0)
966 AlignmentAnnotation alan = null;
967 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
969 alan = (AlignmentAnnotation) rowset.elementAt(elm);
970 alan.groupRef = theGroup;
976 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
978 // process any deferred attribute settings for each context
979 for (Object[] _deferred_args : deferredAnnotation_calls)
981 if (_deferred_args[0] == GRAPHLINE)
984 (StringTokenizer) _deferred_args[1], // st
985 (SequenceI) _deferred_args[2], // refSeq
986 (_deferred_args[3] == null) ? null : groupRefLookup
987 .get((String) _deferred_args[3]) // the reference
993 // finally, combine all the annotation rows within each context.
995 * number of combine statements in this annotation file. Used to create new groups for combined annotation graphs without disturbing existing ones
997 int combinecount = 0;
998 for (Object[] _combine_args:combineAnnotation_calls) {
999 combineAnnotations(al,
1001 (StringTokenizer) _combine_args[0], // st
1002 (SequenceI) _combine_args[1], // refSeq
1003 (_combine_args[2]==null) ? null : groupRefLookup.get((String)_combine_args[2]) // the reference group, or null
1010 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1011 SequenceI refSeq, String groupRef)
1013 return annotation.graph + "\t" + annotation.label + "\t"
1014 + annotation.description + "\t"
1015 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1018 Annotation parseAnnotation(String string, int graphStyle)
1020 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
1029 String desc = null, displayChar = null;
1030 char ss = ' '; // secondaryStructure
1032 boolean parsedValue = false, dcset = false;
1035 java.awt.Color colour = null;
1036 int i = string.indexOf("[");
1037 int j = string.indexOf("]");
1038 if (i > -1 && j > -1)
1040 UserColourScheme ucs = new UserColourScheme();
1042 colour = ucs.getColourFromString(string.substring(i + 1, j));
1043 if (i > 0 && string.charAt(i - 1) == ',')
1045 // clip the preceding comma as well
1048 string = string.substring(0, i) + string.substring(j + 1);
1051 StringTokenizer st = new StringTokenizer(string, ",", true);
1053 boolean seenContent = false;
1055 while (st.hasMoreTokens())
1058 token = st.nextToken().trim();
1059 if (token.equals(","))
1061 if (!seenContent && parsedValue && !dcset)
1063 // allow the value below the bar/line to be empty
1067 seenContent = false;
1079 displayChar = token;
1081 value = new Float(token).floatValue();
1084 } catch (NumberFormatException ex)
1090 if (token.length() == 1)
1092 displayChar = token;
1096 && (token.equals("H") || token.equals("E")
1097 || token.equals("S") || token.equals(" ")))
1099 // Either this character represents a helix or sheet
1100 // or an integer which can be displayed
1101 ss = token.charAt(0);
1102 if (displayChar.equals(token.substring(0, 1)))
1107 else if (desc == null || (parsedValue && pass > 2))
1113 // if (!dcset && string.charAt(string.length() - 1) == ',')
1115 // displayChar = " "; // empty display char symbol.
1117 if (displayChar != null && desc != null && desc.length() == 1)
1119 if (displayChar.length() > 1)
1121 // switch desc and displayChar - legacy support
1122 String tmp = displayChar;
1128 if (displayChar.equals(desc))
1130 // duplicate label - hangover from the 'robust parser' above
1135 Annotation anot = new Annotation(displayChar, desc, ss, value);
1137 anot.colour = colour;
1142 void colourAnnotations(AlignmentI al, String label, String colour)
1144 UserColourScheme ucs = new UserColourScheme(colour);
1145 Annotation[] annotations;
1146 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1148 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1150 annotations = al.getAlignmentAnnotation()[i].annotations;
1151 for (int j = 0; j < annotations.length; j++)
1153 if (annotations[j] != null)
1155 annotations[j].colour = ucs.findColour('A');
1162 void combineAnnotations(AlignmentI al, int combineCount, StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1164 String group = st.nextToken();
1165 // First make sure we are not overwriting the graphIndex
1167 if (al.getAlignmentAnnotation() != null)
1169 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1171 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1173 if (aa.graphGroup>graphGroup)
1175 // try to number graphGroups in order of occurence.
1176 graphGroup=aa.graphGroup+1;
1178 if (aa.sequenceRef==seqRef && aa.groupRef==groupRef && aa.label.equalsIgnoreCase(group))
1180 if (aa.graphGroup>-1)
1182 graphGroup = aa.graphGroup;
1184 if (graphGroup <= combineCount)
1186 graphGroup=combineCount+1;
1188 aa.graphGroup = graphGroup;
1194 // Now update groups
1195 while (st.hasMoreTokens())
1197 group = st.nextToken();
1198 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1200 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1201 if (aa.sequenceRef==seqRef && aa.groupRef==groupRef && aa.label.equalsIgnoreCase(group))
1203 aa.graphGroup = graphGroup;
1212 .println("Couldn't combine annotations. None are added to alignment yet!");
1216 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1218 String group = st.nextToken();
1219 AlignmentAnnotation annotation = null, alannot[] = al
1220 .getAlignmentAnnotation();
1221 float value = new Float(st.nextToken()).floatValue();
1222 String label = st.hasMoreTokens() ? st.nextToken() : null;
1223 java.awt.Color colour = null;
1224 if (st.hasMoreTokens())
1226 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1227 colour = ucs.findColour('A');
1229 if (alannot != null)
1231 for (int i = 0; i < alannot.length; i++)
1233 if (alannot[i].label.equalsIgnoreCase(group) && (seqRef==null || alannot[i].sequenceRef==seqRef) && (groupRef==null || alannot[i].groupRef==groupRef))
1235 alannot[i].setThreshold(new GraphLine(value, label, colour));
1239 if (annotation == null)
1245 void addGroup(AlignmentI al, StringTokenizer st)
1247 SequenceGroup sg = new SequenceGroup();
1248 sg.setName(st.nextToken());
1252 rng = st.nextToken();
1253 if (rng.length() > 0 && !rng.startsWith("*"))
1255 sg.setStartRes(Integer.parseInt(rng) - 1);
1261 rng = st.nextToken();
1262 if (rng.length() > 0 && !rng.startsWith("*"))
1264 sg.setEndRes(Integer.parseInt(rng) - 1);
1268 sg.setEndRes(al.getWidth() - 1);
1270 } catch (Exception e)
1273 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1274 + rng + "' - assuming alignment width for group.");
1275 // assume group is full width
1277 sg.setEndRes(al.getWidth() - 1);
1280 String index = st.nextToken();
1281 if (index.equals("-1"))
1283 while (st.hasMoreElements())
1285 sg.addSequence(al.findName(st.nextToken()), false);
1290 StringTokenizer st2 = new StringTokenizer(index, ",");
1292 while (st2.hasMoreTokens())
1294 String tmp = st2.nextToken();
1295 if (tmp.equals("*"))
1297 for (int i = 0; i < al.getHeight(); i++)
1299 sg.addSequence(al.getSequenceAt(i), false);
1302 else if (tmp.indexOf("-") >= 0)
1304 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1306 int start = (Integer.parseInt(st3.nextToken()));
1307 int end = (Integer.parseInt(st3.nextToken()));
1311 for (int i = start; i <= end; i++)
1313 sg.addSequence(al.getSequenceAt(i - 1), false);
1319 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1326 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1327 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1328 sg.setSeqrep(refSeq);
1331 if (sg.getSize() > 0)
1337 void addRowProperties(AlignmentI al, StringTokenizer st)
1339 String label = st.nextToken(), keyValue, key, value;
1340 boolean scaletofit = false, centerlab = false, showalllabs = false;
1341 while (st.hasMoreTokens())
1343 keyValue = st.nextToken();
1344 key = keyValue.substring(0, keyValue.indexOf("="));
1345 value = keyValue.substring(keyValue.indexOf("=") + 1);
1346 if (key.equalsIgnoreCase("scaletofit"))
1348 scaletofit = Boolean.valueOf(value).booleanValue();
1350 if (key.equalsIgnoreCase("showalllabs"))
1352 showalllabs = Boolean.valueOf(value).booleanValue();
1354 if (key.equalsIgnoreCase("centrelabs"))
1356 centerlab = Boolean.valueOf(value).booleanValue();
1358 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1361 for (int i = 0; i < alr.length; i++)
1363 if (alr[i].label.equalsIgnoreCase(label))
1365 alr[i].centreColLabels = centerlab;
1366 alr[i].scaleColLabel = scaletofit;
1367 alr[i].showAllColLabels = showalllabs;
1374 void addProperties(AlignmentI al, StringTokenizer st)
1377 // So far we have only added groups to the annotationHash,
1378 // the idea is in the future properties can be added to
1379 // alignments, other annotations etc
1380 if (al.getGroups() == null)
1385 String name = st.nextToken();
1386 SequenceGroup sg = null;
1387 for (SequenceGroup _sg : al.getGroups())
1389 if ((sg = _sg).getName().equals(name))
1401 String keyValue, key, value;
1402 ColourSchemeI def = sg.cs;
1404 while (st.hasMoreTokens())
1406 keyValue = st.nextToken();
1407 key = keyValue.substring(0, keyValue.indexOf("="));
1408 value = keyValue.substring(keyValue.indexOf("=") + 1);
1410 if (key.equalsIgnoreCase("description"))
1412 sg.setDescription(value);
1414 else if (key.equalsIgnoreCase("colour"))
1416 sg.cs = ColourSchemeProperty.getColour(al, value);
1418 else if (key.equalsIgnoreCase("pidThreshold"))
1420 sg.cs.setThreshold(Integer.parseInt(value), true);
1423 else if (key.equalsIgnoreCase("consThreshold"))
1425 sg.cs.setConservationInc(Integer.parseInt(value));
1426 Conservation c = new Conservation("Group",
1427 ResidueProperties.propHash, 3, sg.getSequences(null),
1428 sg.getStartRes(), sg.getEndRes() + 1);
1431 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1433 sg.cs.setConservation(c);
1436 else if (key.equalsIgnoreCase("outlineColour"))
1438 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1440 else if (key.equalsIgnoreCase("displayBoxes"))
1442 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1444 else if (key.equalsIgnoreCase("showUnconserved"))
1446 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1448 else if (key.equalsIgnoreCase("displayText"))
1450 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1452 else if (key.equalsIgnoreCase("colourText"))
1454 sg.setColourText(Boolean.valueOf(value).booleanValue());
1456 else if (key.equalsIgnoreCase("textCol1"))
1458 sg.textColour = new UserColourScheme(value).findColour('A');
1460 else if (key.equalsIgnoreCase("textCol2"))
1462 sg.textColour2 = new UserColourScheme(value).findColour('A');
1464 else if (key.equalsIgnoreCase("textColThreshold"))
1466 sg.thresholdTextColour = Integer.parseInt(value);
1468 else if (key.equalsIgnoreCase("idColour"))
1470 // consider warning if colour doesn't resolve to a real colour
1471 sg.setIdColour((def = new UserColourScheme(value))
1474 else if (key.equalsIgnoreCase("hide"))
1476 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1477 sg.setHidereps(true);
1479 else if (key.equalsIgnoreCase("hidecols"))
1481 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1482 sg.setHideCols(true);
1484 sg.recalcConservation();
1493 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1496 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1500 .print("Warning - no annotation to set below for sequence associated annotation:");
1502 while (st.hasMoreTokens())
1504 token = st.nextToken();
1507 System.err.print(" " + token);
1511 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1513 aa = al.getAlignmentAnnotation()[i];
1514 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1516 aa.belowAlignment = true;
1523 System.err.print("\n");
1527 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1529 String keyValue, key, value;
1530 while (st.hasMoreTokens())
1532 keyValue = st.nextToken();
1533 key = keyValue.substring(0, keyValue.indexOf("="));
1534 value = keyValue.substring(keyValue.indexOf("=") + 1);
1535 al.setProperty(key, value);
1540 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1543 * @param annotations
1544 * @return CSV file as a string.
1546 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1548 StringBuffer sp = new StringBuffer();
1549 for (int i = 0; i < annotations.length; i++)
1551 String atos = annotations[i].toString();
1555 int cp = atos.indexOf("\n", p);
1556 sp.append(annotations[i].label);
1560 sp.append(atos.substring(p, cp + 1));
1564 sp.append(atos.substring(p));
1570 return sp.toString();