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"))
797 else if (token.equalsIgnoreCase("PROPERTIES"))
799 addProperties(al, st);
804 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
806 setBelowAlignment(al, st);
810 else if (token.equalsIgnoreCase("ALIGNMENT"))
812 addAlignmentDetails(al, st);
817 // Parse out the annotation row
818 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
819 label = st.nextToken();
822 annotations = new Annotation[alWidth];
824 float score = Float.NaN;
826 if (st.hasMoreTokens())
828 line = st.nextToken();
830 if (line.indexOf("|") == -1)
833 if (st.hasMoreTokens())
834 line = st.nextToken();
837 if (st.hasMoreTokens())
839 // This must be the score
840 score = Float.valueOf(st.nextToken()).floatValue();
843 st = new StringTokenizer(line, "|", true);
845 boolean emptyColumn = true;
846 boolean onlyOneElement = (st.countTokens() == 1);
848 while (st.hasMoreElements() && index < alWidth)
850 token = st.nextToken().trim();
856 score = Float.valueOf(token).floatValue();
858 } catch (NumberFormatException ex)
863 if (token.equals("|"))
874 annotations[index++] = parseAnnotation(token, graphStyle);
881 annotation = new AlignmentAnnotation(label, description,
882 (index == 0) ? null : annotations, 0, 0, graphStyle);
884 annotation.score = score;
885 if (!overrideAutoAnnot
886 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
889 // skip - we've already got an automatic annotation of this type.
896 annotation.belowAlignment = false;
897 // make a copy of refSeq so we can find other matches in the alignment
898 SequenceI referedSeq = refSeq;
901 // copy before we do any mapping business.
902 // TODO: verify that undo/redo with 1:many sequence associated
903 // annotations can be undone correctly
904 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
906 .createSequenceMapping(referedSeq, refSeqIndex, false);
907 annotation.adjustForAlignment();
908 referedSeq.addAlignmentAnnotation(annotation);
909 al.addAnnotation(annotation);
910 al.setAnnotationIndex(annotation,
911 al.getAlignmentAnnotation().length
912 - existingAnnotations - 1);
913 if (groupRef != null)
915 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
917 // and recover our virgin copy to use again if necessary.
920 } while (refSeqId != null
921 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
925 al.addAnnotation(annotation);
926 al.setAnnotationIndex(annotation,
927 al.getAlignmentAnnotation().length - existingAnnotations
929 if (groupRef != null)
931 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
934 // and set modification flag
937 // Resolve the groupRefs
938 Hashtable <String,SequenceGroup> groupRefLookup=new Hashtable<String,SequenceGroup>();
939 Enumeration en = groupRefRows.keys();
941 while (en.hasMoreElements())
943 groupRef = (String) en.nextElement();
944 boolean matched = false;
945 // Resolve group: TODO: add a getGroupByName method to alignments
946 for (SequenceGroup theGroup : al.getGroups())
948 if (theGroup.getName().equals(groupRef))
952 // TODO: specify and implement duplication of alignment annotation
953 // for multiple group references.
955 .println("Ignoring 1:many group reference mappings for group name '"
961 Vector rowset = (Vector) groupRefRows.get(groupRef);
962 groupRefLookup.put(groupRef, theGroup);
963 if (rowset != null && rowset.size() > 0)
965 AlignmentAnnotation alan = null;
966 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
968 alan = (AlignmentAnnotation) rowset.elementAt(elm);
969 alan.groupRef = theGroup;
975 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
977 // process any deferred attribute settings for each context
978 for (Object[] _deferred_args : deferredAnnotation_calls)
980 if (_deferred_args[0] == GRAPHLINE)
983 (StringTokenizer) _deferred_args[1], // st
984 (SequenceI) _deferred_args[2], // refSeq
985 (_deferred_args[3] == null) ? null : groupRefLookup
986 .get((String) _deferred_args[3]) // the reference
992 // finally, combine all the annotation rows within each context.
994 * number of combine statements in this annotation file. Used to create new groups for combined annotation graphs without disturbing existing ones
996 int combinecount = 0;
997 for (Object[] _combine_args:combineAnnotation_calls) {
998 combineAnnotations(al,
1000 (StringTokenizer) _combine_args[0], // st
1001 (SequenceI) _combine_args[1], // refSeq
1002 (_combine_args[2]==null) ? null : groupRefLookup.get((String)_combine_args[2]) // the reference group, or null
1009 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1010 SequenceI refSeq, String groupRef)
1012 return annotation.graph + "\t" + annotation.label + "\t"
1013 + annotation.description + "\t"
1014 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1017 Annotation parseAnnotation(String string, int graphStyle)
1019 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
1028 String desc = null, displayChar = null;
1029 char ss = ' '; // secondaryStructure
1031 boolean parsedValue = false, dcset = false;
1034 java.awt.Color colour = null;
1035 int i = string.indexOf("[");
1036 int j = string.indexOf("]");
1037 if (i > -1 && j > -1)
1039 UserColourScheme ucs = new UserColourScheme();
1041 colour = ucs.getColourFromString(string.substring(i + 1, j));
1042 if (i > 0 && string.charAt(i - 1) == ',')
1044 // clip the preceding comma as well
1047 string = string.substring(0, i) + string.substring(j + 1);
1050 StringTokenizer st = new StringTokenizer(string, ",", true);
1052 boolean seenContent = false;
1054 while (st.hasMoreTokens())
1057 token = st.nextToken().trim();
1058 if (token.equals(","))
1060 if (!seenContent && parsedValue && !dcset)
1062 // allow the value below the bar/line to be empty
1066 seenContent = false;
1078 displayChar = token;
1080 value = new Float(token).floatValue();
1083 } catch (NumberFormatException ex)
1089 if (token.length() == 1)
1091 displayChar = token;
1095 && (token.equals("H") || token.equals("E")
1096 || token.equals("S") || token.equals(" ")))
1098 // Either this character represents a helix or sheet
1099 // or an integer which can be displayed
1100 ss = token.charAt(0);
1101 if (displayChar.equals(token.substring(0, 1)))
1106 else if (desc == null || (parsedValue && pass > 2))
1112 // if (!dcset && string.charAt(string.length() - 1) == ',')
1114 // displayChar = " "; // empty display char symbol.
1116 if (displayChar != null && desc != null && desc.length() == 1)
1118 if (displayChar.length() > 1)
1120 // switch desc and displayChar - legacy support
1121 String tmp = displayChar;
1127 if (displayChar.equals(desc))
1129 // duplicate label - hangover from the 'robust parser' above
1134 Annotation anot = new Annotation(displayChar, desc, ss, value);
1136 anot.colour = colour;
1141 void colourAnnotations(AlignmentI al, String label, String colour)
1143 UserColourScheme ucs = new UserColourScheme(colour);
1144 Annotation[] annotations;
1145 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1147 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1149 annotations = al.getAlignmentAnnotation()[i].annotations;
1150 for (int j = 0; j < annotations.length; j++)
1152 if (annotations[j] != null)
1154 annotations[j].colour = ucs.findColour('A');
1161 void combineAnnotations(AlignmentI al, int combineCount, StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1163 String group = st.nextToken();
1164 // First make sure we are not overwriting the graphIndex
1166 if (al.getAlignmentAnnotation() != null)
1168 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1170 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1172 if (aa.graphGroup>graphGroup)
1174 // try to number graphGroups in order of occurence.
1175 graphGroup=aa.graphGroup+1;
1177 if (aa.sequenceRef==seqRef && aa.groupRef==groupRef && aa.label.equalsIgnoreCase(group))
1179 if (aa.graphGroup>-1)
1181 graphGroup = aa.graphGroup;
1183 if (graphGroup <= combineCount)
1185 graphGroup=combineCount+1;
1187 aa.graphGroup = graphGroup;
1193 // Now update groups
1194 while (st.hasMoreTokens())
1196 group = st.nextToken();
1197 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1199 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1200 if (aa.sequenceRef==seqRef && aa.groupRef==groupRef && aa.label.equalsIgnoreCase(group))
1202 aa.graphGroup = graphGroup;
1211 .println("Couldn't combine annotations. None are added to alignment yet!");
1215 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1217 String group = st.nextToken();
1218 AlignmentAnnotation annotation = null, alannot[] = al
1219 .getAlignmentAnnotation();
1220 float value = new Float(st.nextToken()).floatValue();
1221 String label = st.hasMoreTokens() ? st.nextToken() : null;
1222 java.awt.Color colour = null;
1223 if (st.hasMoreTokens())
1225 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1226 colour = ucs.findColour('A');
1228 if (alannot != null)
1230 for (int i = 0; i < alannot.length; i++)
1232 if (alannot[i].label.equalsIgnoreCase(group) && (seqRef==null || alannot[i].sequenceRef==seqRef) && (groupRef==null || alannot[i].groupRef==groupRef))
1234 alannot[i].setThreshold(new GraphLine(value, label, colour));
1238 if (annotation == null)
1244 void addGroup(AlignmentI al, StringTokenizer st)
1246 SequenceGroup sg = new SequenceGroup();
1247 sg.setName(st.nextToken());
1251 rng = st.nextToken();
1252 if (rng.length() > 0 && !rng.startsWith("*"))
1254 sg.setStartRes(Integer.parseInt(rng) - 1);
1260 rng = st.nextToken();
1261 if (rng.length() > 0 && !rng.startsWith("*"))
1263 sg.setEndRes(Integer.parseInt(rng) - 1);
1267 sg.setEndRes(al.getWidth() - 1);
1269 } catch (Exception e)
1272 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1273 + rng + "' - assuming alignment width for group.");
1274 // assume group is full width
1276 sg.setEndRes(al.getWidth() - 1);
1279 String index = st.nextToken();
1280 if (index.equals("-1"))
1282 while (st.hasMoreElements())
1284 sg.addSequence(al.findName(st.nextToken()), false);
1289 StringTokenizer st2 = new StringTokenizer(index, ",");
1291 while (st2.hasMoreTokens())
1293 String tmp = st2.nextToken();
1294 if (tmp.equals("*"))
1296 for (int i = 0; i < al.getHeight(); i++)
1298 sg.addSequence(al.getSequenceAt(i), false);
1301 else if (tmp.indexOf("-") >= 0)
1303 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1305 int start = (Integer.parseInt(st3.nextToken()));
1306 int end = (Integer.parseInt(st3.nextToken()));
1310 for (int i = start; i <= end; i++)
1312 sg.addSequence(al.getSequenceAt(i - 1), false);
1318 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1325 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1326 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1327 sg.setSeqrep(refSeq);
1330 if (sg.getSize() > 0)
1336 void addRowProperties(AlignmentI al, StringTokenizer st)
1338 String label = st.nextToken(), keyValue, key, value;
1339 boolean scaletofit = false, centerlab = false, showalllabs = false;
1340 while (st.hasMoreTokens())
1342 keyValue = st.nextToken();
1343 key = keyValue.substring(0, keyValue.indexOf("="));
1344 value = keyValue.substring(keyValue.indexOf("=") + 1);
1345 if (key.equalsIgnoreCase("scaletofit"))
1347 scaletofit = Boolean.valueOf(value).booleanValue();
1349 if (key.equalsIgnoreCase("showalllabs"))
1351 showalllabs = Boolean.valueOf(value).booleanValue();
1353 if (key.equalsIgnoreCase("centrelabs"))
1355 centerlab = Boolean.valueOf(value).booleanValue();
1357 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1360 for (int i = 0; i < alr.length; i++)
1362 if (alr[i].label.equalsIgnoreCase(label))
1364 alr[i].centreColLabels = centerlab;
1365 alr[i].scaleColLabel = scaletofit;
1366 alr[i].showAllColLabels = showalllabs;
1373 void addProperties(AlignmentI al, StringTokenizer st)
1376 // So far we have only added groups to the annotationHash,
1377 // the idea is in the future properties can be added to
1378 // alignments, other annotations etc
1379 if (al.getGroups() == null)
1384 String name = st.nextToken();
1385 SequenceGroup sg = null;
1386 for (SequenceGroup _sg : al.getGroups())
1388 if ((sg = _sg).getName().equals(name))
1400 String keyValue, key, value;
1401 ColourSchemeI def = sg.cs;
1403 while (st.hasMoreTokens())
1405 keyValue = st.nextToken();
1406 key = keyValue.substring(0, keyValue.indexOf("="));
1407 value = keyValue.substring(keyValue.indexOf("=") + 1);
1409 if (key.equalsIgnoreCase("description"))
1411 sg.setDescription(value);
1413 else if (key.equalsIgnoreCase("colour"))
1415 sg.cs = ColourSchemeProperty.getColour(al, value);
1417 else if (key.equalsIgnoreCase("pidThreshold"))
1419 sg.cs.setThreshold(Integer.parseInt(value), true);
1422 else if (key.equalsIgnoreCase("consThreshold"))
1424 sg.cs.setConservationInc(Integer.parseInt(value));
1425 Conservation c = new Conservation("Group",
1426 ResidueProperties.propHash, 3, sg.getSequences(null),
1427 sg.getStartRes(), sg.getEndRes() + 1);
1430 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1432 sg.cs.setConservation(c);
1435 else if (key.equalsIgnoreCase("outlineColour"))
1437 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1439 else if (key.equalsIgnoreCase("displayBoxes"))
1441 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1443 else if (key.equalsIgnoreCase("showUnconserved"))
1445 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1447 else if (key.equalsIgnoreCase("displayText"))
1449 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1451 else if (key.equalsIgnoreCase("colourText"))
1453 sg.setColourText(Boolean.valueOf(value).booleanValue());
1455 else if (key.equalsIgnoreCase("textCol1"))
1457 sg.textColour = new UserColourScheme(value).findColour('A');
1459 else if (key.equalsIgnoreCase("textCol2"))
1461 sg.textColour2 = new UserColourScheme(value).findColour('A');
1463 else if (key.equalsIgnoreCase("textColThreshold"))
1465 sg.thresholdTextColour = Integer.parseInt(value);
1467 else if (key.equalsIgnoreCase("idColour"))
1469 // consider warning if colour doesn't resolve to a real colour
1470 sg.setIdColour((def = new UserColourScheme(value))
1473 else if (key.equalsIgnoreCase("hide"))
1475 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1476 sg.setHidereps(true);
1478 else if (key.equalsIgnoreCase("hidecols"))
1480 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1481 sg.setHideCols(true);
1483 sg.recalcConservation();
1492 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1495 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1499 .print("Warning - no annotation to set below for sequence associated annotation:");
1501 while (st.hasMoreTokens())
1503 token = st.nextToken();
1506 System.err.print(" " + token);
1510 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1512 aa = al.getAlignmentAnnotation()[i];
1513 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1515 aa.belowAlignment = true;
1522 System.err.print("\n");
1526 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1528 String keyValue, key, value;
1529 while (st.hasMoreTokens())
1531 keyValue = st.nextToken();
1532 key = keyValue.substring(0, keyValue.indexOf("="));
1533 value = keyValue.substring(keyValue.indexOf("=") + 1);
1534 al.setProperty(key, value);
1539 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1542 * @param annotations
1543 * @return CSV file as a string.
1545 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1547 StringBuffer sp = new StringBuffer();
1548 for (int i = 0; i < annotations.length; i++)
1550 String atos = annotations[i].toString();
1554 int cp = atos.indexOf("\n", p);
1555 sp.append(annotations[i].label);
1559 sp.append(atos.substring(p, cp + 1));
1563 sp.append(atos.substring(p));
1569 return sp.toString();