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 annotateAlignmentView(AlignViewportI viewport,
623 String file, String protocol)
625 ColumnSelection colSel = viewport.getColumnSelection();
628 colSel = new ColumnSelection();
630 boolean rslt = readAnnotationFile(viewport.getAlignment(), colSel,
633 && (colSel.hasSelectedColumns() || colSel.hasHiddenColumns()))
635 viewport.setColumnSelection(colSel);
640 public boolean readAnnotationFile(AlignmentI al, String file,
643 return readAnnotationFile(al, null, file, protocol);
646 public boolean readAnnotationFile(AlignmentI al, ColumnSelection colSel,
647 String file, String protocol)
649 BufferedReader in = null;
652 if (protocol.equals(AppletFormatAdapter.FILE))
654 in = new BufferedReader(new FileReader(file));
656 else if (protocol.equals(AppletFormatAdapter.URL))
658 URL url = new URL(file);
659 in = new BufferedReader(new InputStreamReader(url.openStream()));
661 else if (protocol.equals(AppletFormatAdapter.PASTE))
663 in = new BufferedReader(new StringReader(file));
665 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
667 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
670 in = new BufferedReader(new java.io.InputStreamReader(is));
675 return parseAnnotationFrom(al, colSel, in);
678 } catch (Exception ex)
680 ex.printStackTrace();
681 System.out.println("Problem reading annotation file: " + ex);
684 System.out.println("Last read line " + nlinesread + ": '"
685 + lastread + "' (first 80 chars) ...");
694 String lastread = "";
696 private static String GRAPHLINE = "GRAPHLINE", COMBINE = "COMBINE";
698 public boolean parseAnnotationFrom(AlignmentI al, ColumnSelection colSel,
703 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
704 ArrayList<Object[]> deferredAnnotation_calls = new ArrayList<Object[]>();
705 boolean modified = false;
706 String groupRef = null;
707 Hashtable groupRefRows = new Hashtable();
709 Hashtable autoAnnots = new Hashtable();
711 String line, label, description, token;
712 int graphStyle, index;
714 int existingAnnotations = 0;
715 // when true - will add new rows regardless of whether they are duplicate
716 // auto-annotation like consensus or conservation graphs
717 boolean overrideAutoAnnot = false;
718 if (al.getAlignmentAnnotation() != null)
720 existingAnnotations = al.getAlignmentAnnotation().length;
721 if (existingAnnotations > 0)
723 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
724 for (int aai = 0; aai < aa.length; aai++)
726 if (aa[aai].autoCalculated)
728 // make a note of the name and description
730 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
731 (aa[aai].groupRef == null ? null
732 : aa[aai].groupRef.getName())),
739 int alWidth = al.getWidth();
742 Annotation[] annotations;
743 AlignmentAnnotation annotation = null;
745 // First confirm this is an Annotation file
746 boolean jvAnnotationFile = false;
747 while ((line = in.readLine()) != null)
750 lastread = new String(line);
751 if (line.indexOf("#") == 0)
756 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
758 jvAnnotationFile = true;
763 if (!jvAnnotationFile)
769 while ((line = in.readLine()) != null)
772 lastread = new String(line);
773 if (line.indexOf("#") == 0
774 || line.indexOf("JALVIEW_ANNOTATION") > -1
775 || line.length() == 0)
780 st = new StringTokenizer(line, "\t");
781 token = st.nextToken();
782 if (token.equalsIgnoreCase("COLOUR"))
784 // TODO: use graduated colour def'n here too
785 colourAnnotations(al, st.nextToken(), st.nextToken());
790 else if (token.equalsIgnoreCase(COMBINE))
792 // keep a record of current state and resolve groupRef at end
793 combineAnnotation_calls.add(new Object[]
794 { st, refSeq, groupRef });
798 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
800 addRowProperties(al, st);
804 else if (token.equalsIgnoreCase(GRAPHLINE))
807 deferredAnnotation_calls.add(new Object[]
808 { GRAPHLINE, st, refSeq, groupRef });
813 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
815 if (st.hasMoreTokens())
817 refSeq = al.findName(refSeqId = st.nextToken());
824 refSeqIndex = Integer.parseInt(st.nextToken());
829 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
831 } catch (Exception ex)
843 else if (token.equalsIgnoreCase("GROUP_REF"))
845 // Group references could be forward or backwards, so they are
846 // resolved after the whole file is read in
848 if (st.hasMoreTokens())
850 groupRef = st.nextToken();
851 if (groupRef.length() < 1)
853 groupRef = null; // empty string
857 if (groupRefRows.get(groupRef) == null)
859 groupRefRows.put(groupRef, new Vector());
865 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
872 else if (token.equalsIgnoreCase("PROPERTIES"))
874 addProperties(al, st);
879 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
881 setBelowAlignment(al, st);
885 else if (token.equalsIgnoreCase("ALIGNMENT"))
887 addAlignmentDetails(al, st);
891 else if (token.equalsIgnoreCase("VIEW_SETREF"))
895 al.setSeqrep(refSeq);
900 else if (token.equalsIgnoreCase("HIDE_INSERTIONS"))
902 SequenceI sr = refSeq == null ? al.getSeqrep() : refSeq;
905 sr = al.getSequenceAt(0);
912 .println("Cannot process HIDE_INSERTIONS without an alignment view: Ignoring line: "
917 // consider deferring this till after the file has been parsed ?
918 colSel.hideInsertionsFor(sr);
925 // Parse out the annotation row
926 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
927 label = st.nextToken();
930 annotations = new Annotation[alWidth];
932 float score = Float.NaN;
934 if (st.hasMoreTokens())
936 line = st.nextToken();
938 if (line.indexOf("|") == -1)
941 if (st.hasMoreTokens())
943 line = st.nextToken();
947 if (st.hasMoreTokens())
949 // This must be the score
950 score = Float.valueOf(st.nextToken()).floatValue();
953 st = new StringTokenizer(line, "|", true);
955 boolean emptyColumn = true;
956 boolean onlyOneElement = (st.countTokens() == 1);
958 while (st.hasMoreElements() && index < alWidth)
960 token = st.nextToken().trim();
966 score = Float.valueOf(token).floatValue();
968 } catch (NumberFormatException ex)
973 if (token.equals("|"))
984 annotations[index++] = parseAnnotation(token, graphStyle);
991 annotation = new AlignmentAnnotation(label, description,
992 (index == 0) ? null : annotations, 0, 0, graphStyle);
994 annotation.score = score;
995 if (!overrideAutoAnnot
996 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
999 // skip - we've already got an automatic annotation of this type.
1002 // otherwise add it!
1006 annotation.belowAlignment = false;
1007 // make a copy of refSeq so we can find other matches in the alignment
1008 SequenceI referedSeq = refSeq;
1011 // copy before we do any mapping business.
1012 // TODO: verify that undo/redo with 1:many sequence associated
1013 // annotations can be undone correctly
1014 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
1016 .createSequenceMapping(referedSeq, refSeqIndex, false);
1017 annotation.adjustForAlignment();
1018 referedSeq.addAlignmentAnnotation(annotation);
1019 al.addAnnotation(annotation);
1020 al.setAnnotationIndex(annotation,
1021 al.getAlignmentAnnotation().length
1022 - existingAnnotations - 1);
1023 if (groupRef != null)
1025 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1027 // and recover our virgin copy to use again if necessary.
1030 } while (refSeqId != null
1031 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
1035 al.addAnnotation(annotation);
1036 al.setAnnotationIndex(annotation,
1037 al.getAlignmentAnnotation().length - existingAnnotations
1039 if (groupRef != null)
1041 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1044 // and set modification flag
1047 // Resolve the groupRefs
1048 Hashtable<String, SequenceGroup> groupRefLookup = new Hashtable<String, SequenceGroup>();
1049 Enumeration en = groupRefRows.keys();
1051 while (en.hasMoreElements())
1053 groupRef = (String) en.nextElement();
1054 boolean matched = false;
1055 // Resolve group: TODO: add a getGroupByName method to alignments
1056 for (SequenceGroup theGroup : al.getGroups())
1058 if (theGroup.getName().equals(groupRef))
1062 // TODO: specify and implement duplication of alignment annotation
1063 // for multiple group references.
1065 .println("Ignoring 1:many group reference mappings for group name '"
1071 Vector rowset = (Vector) groupRefRows.get(groupRef);
1072 groupRefLookup.put(groupRef, theGroup);
1073 if (rowset != null && rowset.size() > 0)
1075 AlignmentAnnotation alan = null;
1076 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
1078 alan = (AlignmentAnnotation) rowset.elementAt(elm);
1079 alan.groupRef = theGroup;
1085 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
1087 // process any deferred attribute settings for each context
1088 for (Object[] _deferred_args : deferredAnnotation_calls)
1090 if (_deferred_args[0] == GRAPHLINE)
1093 (StringTokenizer) _deferred_args[1], // st
1094 (SequenceI) _deferred_args[2], // refSeq
1095 (_deferred_args[3] == null) ? null : groupRefLookup
1096 .get(_deferred_args[3]) // the reference
1102 // finally, combine all the annotation rows within each context.
1104 * number of combine statements in this annotation file. Used to create
1105 * new groups for combined annotation graphs without disturbing existing
1108 int combinecount = 0;
1109 for (Object[] _combine_args : combineAnnotation_calls)
1111 combineAnnotations(al,
1113 (StringTokenizer) _combine_args[0], // st
1114 (SequenceI) _combine_args[1], // refSeq
1115 (_combine_args[2] == null) ? null : groupRefLookup
1116 .get(_combine_args[2]) // the reference group,
1124 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1125 SequenceI refSeq, String groupRef)
1127 return annotation.graph + "\t" + annotation.label + "\t"
1128 + annotation.description + "\t"
1129 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1132 Annotation parseAnnotation(String string, int graphStyle)
1134 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
1143 String desc = null, displayChar = null;
1144 char ss = ' '; // secondaryStructure
1146 boolean parsedValue = false, dcset = false;
1149 java.awt.Color colour = null;
1150 int i = string.indexOf("[");
1151 int j = string.indexOf("]");
1152 if (i > -1 && j > -1)
1154 UserColourScheme ucs = new UserColourScheme();
1156 colour = ucs.getColourFromString(string.substring(i + 1, j));
1157 if (i > 0 && string.charAt(i - 1) == ',')
1159 // clip the preceding comma as well
1162 string = string.substring(0, i) + string.substring(j + 1);
1165 StringTokenizer st = new StringTokenizer(string, ",", true);
1167 boolean seenContent = false;
1169 while (st.hasMoreTokens())
1172 token = st.nextToken().trim();
1173 if (token.equals(","))
1175 if (!seenContent && parsedValue && !dcset)
1177 // allow the value below the bar/line to be empty
1181 seenContent = false;
1193 displayChar = token;
1195 value = new Float(token).floatValue();
1198 } catch (NumberFormatException ex)
1204 if (token.length() == 1)
1206 displayChar = token;
1210 && (token.equals("H") || token.equals("E")
1211 || token.equals("S") || token.equals(" ")))
1213 // Either this character represents a helix or sheet
1214 // or an integer which can be displayed
1215 ss = token.charAt(0);
1216 if (displayChar.equals(token.substring(0, 1)))
1221 else if (desc == null || (parsedValue && pass > 2))
1227 // if (!dcset && string.charAt(string.length() - 1) == ',')
1229 // displayChar = " "; // empty display char symbol.
1231 if (displayChar != null && desc != null && desc.length() == 1)
1233 if (displayChar.length() > 1)
1235 // switch desc and displayChar - legacy support
1236 String tmp = displayChar;
1242 if (displayChar.equals(desc))
1244 // duplicate label - hangover from the 'robust parser' above
1249 Annotation anot = new Annotation(displayChar, desc, ss, value);
1251 anot.colour = colour;
1256 void colourAnnotations(AlignmentI al, String label, String colour)
1258 UserColourScheme ucs = new UserColourScheme(colour);
1259 Annotation[] annotations;
1260 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1262 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1264 annotations = al.getAlignmentAnnotation()[i].annotations;
1265 for (int j = 0; j < annotations.length; j++)
1267 if (annotations[j] != null)
1269 annotations[j].colour = ucs.findColour('A');
1276 void combineAnnotations(AlignmentI al, int combineCount,
1277 StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1279 String group = st.nextToken();
1280 // First make sure we are not overwriting the graphIndex
1282 if (al.getAlignmentAnnotation() != null)
1284 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1286 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1288 if (aa.graphGroup > graphGroup)
1290 // try to number graphGroups in order of occurence.
1291 graphGroup = aa.graphGroup + 1;
1293 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1294 && aa.label.equalsIgnoreCase(group))
1296 if (aa.graphGroup > -1)
1298 graphGroup = aa.graphGroup;
1302 if (graphGroup <= combineCount)
1304 graphGroup = combineCount + 1;
1306 aa.graphGroup = graphGroup;
1312 // Now update groups
1313 while (st.hasMoreTokens())
1315 group = st.nextToken();
1316 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1318 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1319 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1320 && aa.label.equalsIgnoreCase(group))
1322 aa.graphGroup = graphGroup;
1331 .println("Couldn't combine annotations. None are added to alignment yet!");
1335 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef,
1336 SequenceGroup groupRef)
1338 String group = st.nextToken();
1339 AlignmentAnnotation annotation = null, alannot[] = al
1340 .getAlignmentAnnotation();
1341 float value = new Float(st.nextToken()).floatValue();
1342 String label = st.hasMoreTokens() ? st.nextToken() : null;
1343 java.awt.Color colour = null;
1344 if (st.hasMoreTokens())
1346 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1347 colour = ucs.findColour('A');
1349 if (alannot != null)
1351 for (int i = 0; i < alannot.length; i++)
1353 if (alannot[i].label.equalsIgnoreCase(group)
1354 && (seqRef == null || alannot[i].sequenceRef == seqRef)
1355 && (groupRef == null || alannot[i].groupRef == groupRef))
1357 alannot[i].setThreshold(new GraphLine(value, label, colour));
1361 if (annotation == null)
1367 void addGroup(AlignmentI al, StringTokenizer st)
1369 SequenceGroup sg = new SequenceGroup();
1370 sg.setName(st.nextToken());
1374 rng = st.nextToken();
1375 if (rng.length() > 0 && !rng.startsWith("*"))
1377 sg.setStartRes(Integer.parseInt(rng) - 1);
1383 rng = st.nextToken();
1384 if (rng.length() > 0 && !rng.startsWith("*"))
1386 sg.setEndRes(Integer.parseInt(rng) - 1);
1390 sg.setEndRes(al.getWidth() - 1);
1392 } catch (Exception e)
1395 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1396 + rng + "' - assuming alignment width for group.");
1397 // assume group is full width
1399 sg.setEndRes(al.getWidth() - 1);
1402 String index = st.nextToken();
1403 if (index.equals("-1"))
1405 while (st.hasMoreElements())
1407 sg.addSequence(al.findName(st.nextToken()), false);
1412 StringTokenizer st2 = new StringTokenizer(index, ",");
1414 while (st2.hasMoreTokens())
1416 String tmp = st2.nextToken();
1417 if (tmp.equals("*"))
1419 for (int i = 0; i < al.getHeight(); i++)
1421 sg.addSequence(al.getSequenceAt(i), false);
1424 else if (tmp.indexOf("-") >= 0)
1426 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1428 int start = (Integer.parseInt(st3.nextToken()));
1429 int end = (Integer.parseInt(st3.nextToken()));
1433 for (int i = start; i <= end; i++)
1435 sg.addSequence(al.getSequenceAt(i - 1), false);
1441 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1448 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1449 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1450 sg.setSeqrep(refSeq);
1453 if (sg.getSize() > 0)
1459 void addRowProperties(AlignmentI al, StringTokenizer st)
1461 String label = st.nextToken(), keyValue, key, value;
1462 boolean scaletofit = false, centerlab = false, showalllabs = false;
1463 while (st.hasMoreTokens())
1465 keyValue = st.nextToken();
1466 key = keyValue.substring(0, keyValue.indexOf("="));
1467 value = keyValue.substring(keyValue.indexOf("=") + 1);
1468 if (key.equalsIgnoreCase("scaletofit"))
1470 scaletofit = Boolean.valueOf(value).booleanValue();
1472 if (key.equalsIgnoreCase("showalllabs"))
1474 showalllabs = Boolean.valueOf(value).booleanValue();
1476 if (key.equalsIgnoreCase("centrelabs"))
1478 centerlab = Boolean.valueOf(value).booleanValue();
1480 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1483 for (int i = 0; i < alr.length; i++)
1485 if (alr[i].label.equalsIgnoreCase(label))
1487 alr[i].centreColLabels = centerlab;
1488 alr[i].scaleColLabel = scaletofit;
1489 alr[i].showAllColLabels = showalllabs;
1496 void addProperties(AlignmentI al, StringTokenizer st)
1499 // So far we have only added groups to the annotationHash,
1500 // the idea is in the future properties can be added to
1501 // alignments, other annotations etc
1502 if (al.getGroups() == null)
1507 String name = st.nextToken();
1508 SequenceGroup sg = null;
1509 for (SequenceGroup _sg : al.getGroups())
1511 if ((sg = _sg).getName().equals(name))
1523 String keyValue, key, value;
1524 ColourSchemeI def = sg.cs;
1526 while (st.hasMoreTokens())
1528 keyValue = st.nextToken();
1529 key = keyValue.substring(0, keyValue.indexOf("="));
1530 value = keyValue.substring(keyValue.indexOf("=") + 1);
1532 if (key.equalsIgnoreCase("description"))
1534 sg.setDescription(value);
1536 else if (key.equalsIgnoreCase("colour"))
1538 sg.cs = ColourSchemeProperty.getColour(al, value);
1540 else if (key.equalsIgnoreCase("pidThreshold"))
1542 sg.cs.setThreshold(Integer.parseInt(value), true);
1545 else if (key.equalsIgnoreCase("consThreshold"))
1547 sg.cs.setConservationInc(Integer.parseInt(value));
1548 Conservation c = new Conservation("Group",
1549 ResidueProperties.propHash, 3, sg.getSequences(null),
1550 sg.getStartRes(), sg.getEndRes() + 1);
1553 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1555 sg.cs.setConservation(c);
1558 else if (key.equalsIgnoreCase("outlineColour"))
1560 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1562 else if (key.equalsIgnoreCase("displayBoxes"))
1564 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1566 else if (key.equalsIgnoreCase("showUnconserved"))
1568 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1570 else if (key.equalsIgnoreCase("displayText"))
1572 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1574 else if (key.equalsIgnoreCase("colourText"))
1576 sg.setColourText(Boolean.valueOf(value).booleanValue());
1578 else if (key.equalsIgnoreCase("textCol1"))
1580 sg.textColour = new UserColourScheme(value).findColour('A');
1582 else if (key.equalsIgnoreCase("textCol2"))
1584 sg.textColour2 = new UserColourScheme(value).findColour('A');
1586 else if (key.equalsIgnoreCase("textColThreshold"))
1588 sg.thresholdTextColour = Integer.parseInt(value);
1590 else if (key.equalsIgnoreCase("idColour"))
1592 // consider warning if colour doesn't resolve to a real colour
1593 sg.setIdColour((def = new UserColourScheme(value))
1596 else if (key.equalsIgnoreCase("hide"))
1598 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1599 sg.setHidereps(true);
1601 else if (key.equalsIgnoreCase("hidecols"))
1603 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1604 sg.setHideCols(true);
1606 sg.recalcConservation();
1615 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1618 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1622 .print("Warning - no annotation to set below for sequence associated annotation:");
1624 while (st.hasMoreTokens())
1626 token = st.nextToken();
1629 System.err.print(" " + token);
1633 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1635 aa = al.getAlignmentAnnotation()[i];
1636 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1638 aa.belowAlignment = true;
1645 System.err.print("\n");
1649 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1651 String keyValue, key, value;
1652 while (st.hasMoreTokens())
1654 keyValue = st.nextToken();
1655 key = keyValue.substring(0, keyValue.indexOf("="));
1656 value = keyValue.substring(keyValue.indexOf("=") + 1);
1657 al.setProperty(key, value);
1662 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1665 * @param annotations
1666 * @return CSV file as a string.
1668 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1670 StringBuffer sp = new StringBuffer();
1671 for (int i = 0; i < annotations.length; i++)
1673 String atos = annotations[i].toString();
1677 int cp = atos.indexOf("\n", p);
1678 sp.append(annotations[i].label);
1682 sp.append(atos.substring(p, cp + 1));
1686 sp.append(atos.substring(p));
1692 return sp.toString();
1695 public String printAnnotationsForView(AlignViewportI viewport)
1697 return printAnnotations(viewport.isShowAnnotation() ? viewport
1698 .getAlignment().getAlignmentAnnotation() : null, viewport
1699 .getAlignment().getGroups(), viewport.getAlignment()
1703 public String printAnnotationsForAlignment(AlignmentI al)
1705 return printAnnotations(al.getAlignmentAnnotation(), al.getGroups(),
1706 al.getProperties());