2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.
21 package jalview.datamodel;
23 import java.util.Locale;
25 import jalview.analysis.Rna;
26 import jalview.analysis.SecStrConsensus.SimpleBP;
27 import jalview.analysis.WUSSParseException;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Map.Entry;
45 public class AlignmentAnnotation
47 private static final String ANNOTATION_ID_PREFIX = "ann";
50 * Identifers for different types of profile data
52 public static final int SEQUENCE_PROFILE = 0;
54 public static final int STRUCTURE_PROFILE = 1;
56 public static final int CDNA_PROFILE = 2;
58 private static long counter = 0;
61 * If true, this annotations is calculated every edit, eg consensus, quality
62 * or conservation graphs
64 public boolean autoCalculated = false;
67 * unique ID for this annotation, used to match up the same annotation row
68 * shown in multiple views and alignments
70 public String annotationId;
73 * the sequence this annotation is associated with (or null)
75 public SequenceI sequenceRef;
77 /** label shown in dropdown menus and in the annotation label area */
80 /** longer description text shown as a tooltip */
81 public String description;
83 /** Array of annotations placed in the current coordinate system */
84 public Annotation[] annotations;
86 public List<SimpleBP> bps = null;
89 * RNA secondary structure contact positions
91 public SequenceFeature[] _rnasecstr = null;
94 * position of annotation resulting in invalid WUSS parsing or -1. -2 means
95 * there was no RNA structure in this annotation
97 private long invalidrnastruc = -2;
100 * Updates the _rnasecstr field Determines the positions that base pair and
101 * the positions of helices based on secondary structure from a Stockholm file
103 * @param rnaAnnotation
105 private void _updateRnaSecStr(CharSequence rnaAnnotation)
109 _rnasecstr = Rna.getHelixMap(rnaAnnotation);
110 invalidrnastruc = -1;
111 } catch (WUSSParseException px)
113 // DEBUG System.out.println(px);
114 invalidrnastruc = px.getProblemPos();
116 if (invalidrnastruc > -1)
121 if (_rnasecstr != null && _rnasecstr.length > 0)
123 // show all the RNA secondary structure annotation symbols.
125 showAllColLabels = true;
126 scaleColLabel = true;
129 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
133 private void _markRnaHelices()
136 // Figure out number of helices
137 // Length of rnasecstr is the number of pairs of positions that base pair
138 // with each other in the secondary structure
139 for (int x = 0; x < _rnasecstr.length; x++)
143 * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
144 * this.annotation._rnasecstr[x].getBegin());
146 // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
150 val = Integer.valueOf(_rnasecstr[x].getFeatureGroup());
155 } catch (NumberFormatException q)
160 annotations[_rnasecstr[x].getBegin()].value = val;
161 annotations[_rnasecstr[x].getEnd()].value = val;
163 // annotations[_rnasecstr[x].getBegin()].displayCharacter = "" + val;
164 // annotations[_rnasecstr[x].getEnd()].displayCharacter = "" + val;
170 * Get the RNA Secondary Structure SequenceFeature Array if present
172 public SequenceFeature[] getRnaSecondaryStructure()
174 return this._rnasecstr;
178 * Check the RNA Secondary Structure is equivalent to one in given
179 * AlignmentAnnotation param
181 public boolean rnaSecondaryStructureEquivalent(AlignmentAnnotation that)
183 return rnaSecondaryStructureEquivalent(that, true);
186 public boolean rnaSecondaryStructureEquivalent(AlignmentAnnotation that, boolean compareType)
188 SequenceFeature[] thisSfArray = this.getRnaSecondaryStructure();
189 SequenceFeature[] thatSfArray = that.getRnaSecondaryStructure();
190 if (thisSfArray == null || thatSfArray == null)
192 return thisSfArray == null && thatSfArray == null;
194 if (thisSfArray.length != thatSfArray.length)
198 Arrays.sort(thisSfArray, new SFSortByEnd()); // probably already sorted
200 Arrays.sort(thatSfArray, new SFSortByEnd()); // probably already sorted
202 for (int i=0; i < thisSfArray.length; i++) {
203 SequenceFeature thisSf = thisSfArray[i];
204 SequenceFeature thatSf = thatSfArray[i];
206 if (thisSf.getType() == null || thatSf.getType() == null) {
207 if (thisSf.getType() == null && thatSf.getType() == null) {
213 if (! thisSf.getType().equals(thatSf.getType())) {
217 if (!(thisSf.getBegin() == thatSf.getBegin()
218 && thisSf.getEnd() == thatSf.getEnd()))
228 * map of positions in the associated annotation
230 private Map<Integer, Annotation> sequenceMapping;
233 * lower range for quantitative data
235 public float graphMin;
238 * Upper range for quantitative data
240 public float graphMax;
243 * Score associated with label and description.
245 public double score = Double.NaN;
248 * flag indicating if annotation has a score.
250 public boolean hasScore = false;
252 public GraphLine threshold;
254 // Graphical hints and tips
256 /** Can this row be edited by the user ? */
257 public boolean editable = false;
259 /** Indicates if annotation has a graphical symbol track */
260 public boolean hasIcons; //
262 /** Indicates if annotation has a text character label */
263 public boolean hasText;
265 /** is the row visible */
266 public boolean visible = true;
268 public int graphGroup = -1;
270 /** Displayed height of row in pixels */
271 public int height = 0;
273 public int graph = 0;
275 public int graphHeight = 40;
277 public boolean padGaps = false;
279 public static final int NO_GRAPH = 0;
281 public static final int BAR_GRAPH = 1;
283 public static final int LINE_GRAPH = 2;
285 public boolean belowAlignment = true;
287 public SequenceGroup groupRef = null;
290 * display every column label, even if there is a row of identical labels
292 public boolean showAllColLabels = false;
295 * scale the column label to fit within the alignment column.
297 public boolean scaleColLabel = false;
300 * centre the column labels relative to the alignment column
302 public boolean centreColLabels = false;
304 private boolean isrna;
306 public static int getGraphValueFromString(String string)
308 if (string.equalsIgnoreCase("BAR_GRAPH"))
312 else if (string.equalsIgnoreCase("LINE_GRAPH"))
323 * Creates a new AlignmentAnnotation object.
326 * short label shown under sequence labels
328 * text displayed on mouseover
330 * set of positional annotation elements
332 public AlignmentAnnotation(String label, String description,
333 Annotation[] annotations)
339 this.description = description;
340 this.annotations = annotations;
342 validateRangeAndDisplay();
346 * Checks if annotation labels represent secondary structures
349 void areLabelsSecondaryStructure()
351 boolean nonSSLabel = false;
353 StringBuffer rnastring = new StringBuffer();
356 for (int i = 0; i < annotations.length; i++)
358 // DEBUG System.out.println(i + ": " + annotations[i]);
359 if (annotations[i] == null)
363 if (annotations[i].secondaryStructure == 'H'
364 || annotations[i].secondaryStructure == 'E')
366 // DEBUG System.out.println( "/H|E/ '" +
367 // annotations[i].secondaryStructure + "'");
371 // Check for RNA secondary structure
373 // DEBUG System.out.println( "/else/ '" +
374 // annotations[i].secondaryStructure + "'");
375 // TODO: 2.8.2 should this ss symbol validation check be a function in
376 // RNA/ResidueProperties ?
377 if (annotations[i].secondaryStructure == '('
378 || annotations[i].secondaryStructure == '['
379 || annotations[i].secondaryStructure == '<'
380 || annotations[i].secondaryStructure == '{'
381 || annotations[i].secondaryStructure == 'A'
382 || annotations[i].secondaryStructure == 'B'
383 || annotations[i].secondaryStructure == 'C'
384 || annotations[i].secondaryStructure == 'D'
385 // || annotations[i].secondaryStructure == 'E' // ambiguous on
386 // its own -- already checked above
387 || annotations[i].secondaryStructure == 'F'
388 || annotations[i].secondaryStructure == 'G'
389 // || annotations[i].secondaryStructure == 'H' // ambiguous on
390 // its own -- already checked above
391 || annotations[i].secondaryStructure == 'I'
392 || annotations[i].secondaryStructure == 'J'
393 || annotations[i].secondaryStructure == 'K'
394 || annotations[i].secondaryStructure == 'L'
395 || annotations[i].secondaryStructure == 'M'
396 || annotations[i].secondaryStructure == 'N'
397 || annotations[i].secondaryStructure == 'O'
398 || annotations[i].secondaryStructure == 'P'
399 || annotations[i].secondaryStructure == 'Q'
400 || annotations[i].secondaryStructure == 'R'
401 || annotations[i].secondaryStructure == 'S'
402 || annotations[i].secondaryStructure == 'T'
403 || annotations[i].secondaryStructure == 'U'
404 || annotations[i].secondaryStructure == 'V'
405 || annotations[i].secondaryStructure == 'W'
406 || annotations[i].secondaryStructure == 'X'
407 || annotations[i].secondaryStructure == 'Y'
408 || annotations[i].secondaryStructure == 'Z')
415 // System.out.println("displaychar " + annotations[i].displayCharacter);
417 if (annotations[i].displayCharacter == null
418 || annotations[i].displayCharacter.length() == 0)
420 rnastring.append('.');
423 if (annotations[i].displayCharacter.length() == 1)
425 firstChar = annotations[i].displayCharacter.charAt(0);
426 // check to see if it looks like a sequence or is secondary structure
428 if (annotations[i].secondaryStructure != ' ' && !hasIcons &&
429 // Uncomment to only catch case where
430 // displayCharacter==secondary
432 // to correctly redisplay SS annotation imported from Stockholm,
433 // exported to JalviewXML and read back in again.
435 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
436 firstChar != ' ' && firstChar != '$' && firstChar != 0xCE
437 && firstChar != '(' && firstChar != '[' && firstChar != '<'
438 && firstChar != '{' && firstChar != 'A' && firstChar != 'B'
439 && firstChar != 'C' && firstChar != 'D' && firstChar != 'E'
440 && firstChar != 'F' && firstChar != 'G' && firstChar != 'H'
441 && firstChar != 'I' && firstChar != 'J' && firstChar != 'K'
442 && firstChar != 'L' && firstChar != 'M' && firstChar != 'N'
443 && firstChar != 'O' && firstChar != 'P' && firstChar != 'Q'
444 && firstChar != 'R' && firstChar != 'S' && firstChar != 'T'
445 && firstChar != 'U' && firstChar != 'V' && firstChar != 'W'
446 && firstChar != 'X' && firstChar != 'Y' && firstChar != 'Z'
448 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
450 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
463 rnastring.append(annotations[i].displayCharacter.charAt(1));
466 if (annotations[i].displayCharacter.length() > 0)
475 for (int j = 0; j < annotations.length; j++)
477 if (annotations[j] != null
478 && annotations[j].secondaryStructure != ' ')
480 annotations[j].displayCharacter = String
481 .valueOf(annotations[j].secondaryStructure);
482 annotations[j].secondaryStructure = ' ';
491 _updateRnaSecStr(new AnnotCharSequence());
497 * flyweight access to positions in the alignment annotation row for RNA
503 private class AnnotCharSequence implements CharSequence
509 public AnnotCharSequence()
511 this(0, annotations.length);
514 AnnotCharSequence(int start, int end)
521 public CharSequence subSequence(int start, int end)
523 return new AnnotCharSequence(offset + start, offset + end);
533 public char charAt(int index)
535 return ((index + offset < 0) || (index + offset) >= max
536 || annotations[index + offset] == null
537 || (annotations[index + offset].secondaryStructure <= ' ')
539 : annotations[index + offset].displayCharacter == null
541 + offset].displayCharacter
544 + offset].secondaryStructure
546 + offset].displayCharacter
551 public String toString()
553 char[] string = new char[max - offset];
554 int mx = annotations.length;
556 for (int i = offset; i < mx; i++)
558 string[i] = (annotations[i] == null
559 || (annotations[i].secondaryStructure <= 32))
561 : (annotations[i].displayCharacter == null
562 || annotations[i].displayCharacter
564 ? annotations[i].secondaryStructure
565 : annotations[i].displayCharacter
568 return new String(string);
572 private long _lastrnaannot = -1;
574 public String getRNAStruc()
578 String rnastruc = new AnnotCharSequence().toString();
579 if (_lastrnaannot != rnastruc.hashCode())
581 // ensure rna structure contacts are up to date
582 _lastrnaannot = rnastruc.hashCode();
583 _updateRnaSecStr(rnastruc);
591 * Creates a new AlignmentAnnotation object.
606 public AlignmentAnnotation(String label, String description,
607 Annotation[] annotations, float min, float max, int graphType)
610 // graphs are not editable
611 editable = graphType == 0;
614 this.description = description;
615 this.annotations = annotations;
619 validateRangeAndDisplay();
623 * checks graphMin and graphMax, secondary structure symbols, sets graphType
624 * appropriately, sets null labels to the empty string if appropriate.
626 public void validateRangeAndDisplay()
629 if (annotations == null)
631 visible = false; // try to prevent renderer from displaying.
632 invalidrnastruc = -1;
633 return; // this is a non-annotation row annotation - ie a sequence score.
636 int graphType = graph;
637 float min = graphMin;
638 float max = graphMax;
639 boolean drawValues = true;
644 for (int i = 0; i < annotations.length; i++)
646 if (annotations[i] == null)
651 if (drawValues && annotations[i].displayCharacter != null
652 && annotations[i].displayCharacter.length() > 1)
657 if (annotations[i].value > max)
659 max = annotations[i].value;
662 if (annotations[i].value < min)
664 min = annotations[i].value;
666 if (_linecolour == null && annotations[i].colour != null)
668 _linecolour = annotations[i].colour;
671 // ensure zero is origin for min/max ranges on only one side of zero
688 areLabelsSecondaryStructure();
690 if (!drawValues && graphType != NO_GRAPH)
692 for (int i = 0; i < annotations.length; i++)
694 if (annotations[i] != null)
696 annotations[i].displayCharacter = "";
703 * Copy constructor creates a new independent annotation row with the same
704 * associated sequenceRef
708 public AlignmentAnnotation(AlignmentAnnotation annotation)
711 this.label = new String(annotation.label);
712 if (annotation.description != null)
714 this.description = new String(annotation.description);
716 this.graphMin = annotation.graphMin;
717 this.graphMax = annotation.graphMax;
718 this.graph = annotation.graph;
719 this.graphHeight = annotation.graphHeight;
720 this.graphGroup = annotation.graphGroup;
721 this.groupRef = annotation.groupRef;
722 this.editable = annotation.editable;
723 this.autoCalculated = annotation.autoCalculated;
724 this.hasIcons = annotation.hasIcons;
725 this.hasText = annotation.hasText;
726 this.height = annotation.height;
727 this.label = annotation.label;
728 this.padGaps = annotation.padGaps;
729 this.visible = annotation.visible;
730 this.centreColLabels = annotation.centreColLabels;
731 this.scaleColLabel = annotation.scaleColLabel;
732 this.showAllColLabels = annotation.showAllColLabels;
733 this.calcId = annotation.calcId;
734 if (annotation.properties != null)
736 properties = new HashMap<>();
737 for (Map.Entry<String, String> val : annotation.properties.entrySet())
739 properties.put(val.getKey(), val.getValue());
742 if (this.hasScore = annotation.hasScore)
744 this.score = annotation.score;
746 if (annotation.threshold != null)
748 threshold = new GraphLine(annotation.threshold);
750 Annotation[] ann = annotation.annotations;
751 if (annotation.annotations != null)
753 this.annotations = new Annotation[ann.length];
754 for (int i = 0; i < ann.length; i++)
758 annotations[i] = new Annotation(ann[i]);
759 if (_linecolour != null)
761 _linecolour = annotations[i].colour;
766 if (annotation.sequenceRef != null)
768 this.sequenceRef = annotation.sequenceRef;
769 if (annotation.sequenceMapping != null)
772 sequenceMapping = new HashMap<>();
773 Iterator<Integer> pos = annotation.sequenceMapping.keySet()
775 while (pos.hasNext())
777 // could optimise this!
779 Annotation a = annotation.sequenceMapping.get(p);
786 for (int i = 0; i < ann.length; i++)
790 sequenceMapping.put(p, annotations[i]);
798 this.sequenceMapping = null;
801 // TODO: check if we need to do this: JAL-952
802 // if (this.isrna=annotation.isrna)
804 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
806 validateRangeAndDisplay(); // construct hashcodes, etc.
810 * clip the annotation to the columns given by startRes and endRes (inclusive)
811 * and prune any existing sequenceMapping to just those columns.
816 public void restrict(int startRes, int endRes)
818 if (annotations == null)
827 if (startRes >= annotations.length)
829 startRes = annotations.length - 1;
831 if (endRes >= annotations.length)
833 endRes = annotations.length - 1;
835 if (annotations == null)
839 Annotation[] temp = new Annotation[endRes - startRes + 1];
840 if (startRes < annotations.length)
842 System.arraycopy(annotations, startRes, temp, 0,
843 endRes - startRes + 1);
845 if (sequenceRef != null)
847 // Clip the mapping, if it exists.
848 int spos = sequenceRef.findPosition(startRes);
849 int epos = sequenceRef.findPosition(endRes);
850 if (sequenceMapping != null)
852 Map<Integer, Annotation> newmapping = new HashMap<>();
853 Iterator<Integer> e = sequenceMapping.keySet().iterator();
856 Integer pos = e.next();
857 if (pos.intValue() >= spos && pos.intValue() <= epos)
859 newmapping.put(pos, sequenceMapping.get(pos));
862 sequenceMapping.clear();
863 sequenceMapping = newmapping;
870 * set the annotation row to be at least length Annotations
873 * minimum number of columns required in the annotation row
874 * @return false if the annotation row is greater than length
876 public boolean padAnnotation(int length)
878 if (annotations == null)
880 return true; // annotation row is correct - null == not visible and
883 if (annotations.length < length)
885 Annotation[] na = new Annotation[length];
886 System.arraycopy(annotations, 0, na, 0, annotations.length);
890 return annotations.length > length;
897 * @return DOCUMENT ME!
900 public String toString()
902 if (annotations == null)
906 StringBuilder buffer = new StringBuilder(256);
908 for (int i = 0; i < annotations.length; i++)
910 if (annotations[i] != null)
914 buffer.append(annotations[i].value);
918 buffer.append(annotations[i].secondaryStructure);
922 buffer.append(annotations[i].displayCharacter);
928 // TODO: remove disgusting hack for 'special' treatment of consensus line.
929 if (label.indexOf("Consensus") == 0)
933 for (int i = 0; i < annotations.length; i++)
935 if (annotations[i] != null)
937 buffer.append(annotations[i].description);
944 return buffer.toString();
947 public void setThreshold(GraphLine line)
952 public GraphLine getThreshold()
958 * Attach the annotation to seqRef, starting from startRes position. If
959 * alreadyMapped is true then the indices of the annotation[] array are
960 * sequence positions rather than alignment column positions.
964 * @param alreadyMapped
966 public void createSequenceMapping(SequenceI seqRef, int startRes,
967 boolean alreadyMapped)
974 sequenceRef = seqRef;
975 if (annotations == null)
979 sequenceMapping = new HashMap<>();
983 for (int i = 0; i < annotations.length; i++)
985 if (annotations[i] != null)
989 seqPos = seqRef.findPosition(i);
993 seqPos = i + startRes;
996 sequenceMapping.put(Integer.valueOf(seqPos), annotations[i]);
1003 * When positional annotation and a sequence reference is present, clears and
1004 * resizes the annotations array to the current alignment width, and adds
1005 * annotation according to aligned positions of the sequenceRef given by
1008 public void adjustForAlignment()
1010 if (sequenceRef == null)
1015 if (annotations == null)
1020 int a = 0, aSize = sequenceRef.getLength();
1029 Annotation[] temp = new Annotation[aSize];
1031 if (sequenceMapping != null)
1033 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
1035 index = Integer.valueOf(a);
1036 Annotation annot = sequenceMapping.get(index);
1039 position = sequenceRef.findIndex(a) - 1;
1041 temp[position] = annot;
1049 * remove any null entries in annotation row and return the number of non-null
1050 * annotation elements.
1054 public int compactAnnotationArray()
1056 int i = 0, iSize = annotations.length;
1059 if (annotations[i] == null)
1063 System.arraycopy(annotations, i + 1, annotations, i,
1073 Annotation[] ann = annotations;
1074 annotations = new Annotation[i];
1075 System.arraycopy(ann, 0, annotations, 0, i);
1081 * Associate this annotation with the aligned residues of a particular
1082 * sequence. sequenceMapping will be updated in the following way: null
1083 * sequenceI - existing mapping will be discarded but annotations left in
1084 * mapped positions. valid sequenceI not equal to current sequenceRef: mapping
1085 * is discarded and rebuilt assuming 1:1 correspondence TODO: overload with
1086 * parameter to specify correspondence between current and new sequenceRef
1090 public void setSequenceRef(SequenceI sequenceI)
1092 if (sequenceI != null)
1094 if (sequenceRef != null)
1096 boolean rIsDs = sequenceRef.getDatasetSequence() == null,
1097 tIsDs = sequenceI.getDatasetSequence() == null;
1098 if (sequenceRef != sequenceI
1100 && sequenceRef != sequenceI.getDatasetSequence())
1102 && sequenceRef.getDatasetSequence() != sequenceI)
1103 && (!rIsDs && !tIsDs
1104 && sequenceRef.getDatasetSequence() != sequenceI
1105 .getDatasetSequence())
1106 && !sequenceRef.equals(sequenceI))
1108 // if sequenceRef isn't intersecting with sequenceI
1109 // throw away old mapping and reconstruct.
1111 if (sequenceMapping != null)
1113 sequenceMapping = null;
1114 // compactAnnotationArray();
1116 createSequenceMapping(sequenceI, 1, true);
1117 adjustForAlignment();
1121 // Mapping carried over
1122 sequenceRef = sequenceI;
1127 // No mapping exists
1128 createSequenceMapping(sequenceI, 1, true);
1129 adjustForAlignment();
1134 // throw away the mapping without compacting.
1135 sequenceMapping = null;
1143 public double getScore()
1152 public void setScore(double score)
1160 * @return true if annotation has an associated score
1162 public boolean hasScore()
1164 return hasScore || !Double.isNaN(score);
1168 * Score only annotation
1171 * @param description
1174 public AlignmentAnnotation(String label, String description, double score)
1176 this(label, description, null);
1181 * copy constructor with edit based on the hidden columns marked in colSel
1183 * @param alignmentAnnotation
1186 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1187 HiddenColumns hidden)
1189 this(alignmentAnnotation);
1190 if (annotations == null)
1194 makeVisibleAnnotation(hidden);
1197 public void setPadGaps(boolean padgaps, char gapchar)
1199 this.padGaps = padgaps;
1203 for (int i = 0; i < annotations.length; i++)
1205 if (annotations[i] == null)
1207 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1210 else if (annotations[i].displayCharacter == null
1211 || annotations[i].displayCharacter.equals(" "))
1213 annotations[i].displayCharacter = String.valueOf(gapchar);
1220 * format description string for display
1223 * @return Get the annotation description string optionally prefixed by
1224 * associated sequence name (if any)
1226 public String getDescription(boolean seqname)
1228 if (seqname && this.sequenceRef != null)
1230 int i = description.toLowerCase(Locale.ROOT).indexOf("<html>");
1233 // move the html tag to before the sequence reference.
1234 return "<html>" + sequenceRef.getName() + " : "
1235 + description.substring(i + 6);
1237 return sequenceRef.getName() + " : " + description;
1242 public boolean isValidStruc()
1244 return invalidrnastruc == -1;
1247 public long getInvalidStrucPos()
1249 return invalidrnastruc;
1253 * machine readable ID string indicating what generated this annotation
1255 protected String calcId = "";
1258 * properties associated with the calcId
1260 protected Map<String, String> properties = new HashMap<>();
1263 * base colour for line graphs. If null, will be set automatically by
1264 * searching the alignment annotation
1266 public java.awt.Color _linecolour;
1268 public String getCalcId()
1273 public void setCalcId(String calcId)
1275 this.calcId = calcId;
1278 public boolean isRNA()
1284 * transfer annotation to the given sequence using the given mapping from the
1285 * current positions or an existing sequence mapping
1289 * map involving sq as To or From
1291 public void liftOver(SequenceI sq, Mapping sp2sq)
1293 if (sp2sq.getMappedWidth() != sp2sq.getWidth())
1295 // TODO: employ getWord/MappedWord to transfer annotation between cDNA and
1296 // Protein reference frames
1298 "liftOver currently not implemented for transfer of annotation between different types of seqeunce");
1300 boolean mapIsTo = (sp2sq != null)
1301 ? (sp2sq.getTo() == sq
1302 || sp2sq.getTo() == sq.getDatasetSequence())
1305 // TODO build a better annotation element map and get rid of annotations[]
1306 Map<Integer, Annotation> mapForsq = new HashMap<>();
1307 if (sequenceMapping != null)
1311 for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
1313 Integer mpos = Integer
1314 .valueOf(mapIsTo ? sp2sq.getMappedPosition(ie.getKey())
1315 : sp2sq.getPosition(ie.getKey()));
1316 if (mpos >= sq.getStart() && mpos <= sq.getEnd())
1318 mapForsq.put(mpos, ie.getValue());
1321 sequenceMapping = mapForsq;
1323 adjustForAlignment();
1333 * like liftOver but more general.
1335 * Takes an array of int pairs that will be used to update the internal
1336 * sequenceMapping and so shuffle the annotated positions
1339 * - new sequence reference for the annotation row - if null,
1340 * sequenceRef is left unchanged
1342 * array of ints containing corresponding positions
1344 * - column for current coordinate system (-1 for index+1)
1346 * - column for destination coordinate system (-1 for index+1)
1348 * - offset added to index when referencing either coordinate system
1349 * @note no checks are made as to whether from and/or to are sensible
1350 * @note caller should add the remapped annotation to newref if they have not
1353 public void remap(SequenceI newref, HashMap<Integer, int[]> mapping,
1354 int from, int to, int idxoffset)
1356 if (mapping != null)
1358 Map<Integer, Annotation> old = sequenceMapping;
1359 Map<Integer, Annotation> remap = new HashMap<>();
1361 for (int mp[] : mapping.values())
1367 Annotation ann = null;
1370 ann = sequenceMapping.get(Integer.valueOf(idxoffset + index));
1374 if (mp != null && mp.length > from)
1376 ann = sequenceMapping.get(Integer.valueOf(mp[from]));
1383 remap.put(Integer.valueOf(idxoffset + index), ann);
1387 if (to > -1 && to < mp.length)
1389 remap.put(Integer.valueOf(mp[to]), ann);
1394 sequenceMapping = remap;
1398 sequenceRef = newref;
1400 adjustForAlignment();
1404 public String getProperty(String property)
1406 if (properties == null)
1410 return properties.get(property);
1413 public void setProperty(String property, String value)
1415 if (properties == null)
1417 properties = new HashMap<>();
1419 properties.put(property, value);
1422 public boolean hasProperties()
1424 return properties != null && properties.size() > 0;
1427 public Collection<String> getProperties()
1429 if (properties == null)
1431 return Collections.emptyList();
1433 return properties.keySet();
1437 * Returns the Annotation for the given sequence position (base 1) if any,
1443 public Annotation getAnnotationForPosition(int position)
1445 return sequenceMapping == null ? null : sequenceMapping.get(position);
1450 * Set the id to "ann" followed by a counter that increments so as to be
1451 * unique for the lifetime of the JVM
1453 protected final void setAnnotationId()
1455 this.annotationId = ANNOTATION_ID_PREFIX + Long.toString(nextId());
1459 * Returns the match for the last unmatched opening RNA helix pair symbol
1460 * preceding the given column, or '(' if nothing found to match.
1465 public String getDefaultRnaHelixSymbol(int column)
1467 String result = "(";
1468 if (annotations == null)
1474 * for each preceding column, if it contains an open bracket,
1475 * count whether it is still unmatched at column, if so return its pair
1476 * (likely faster than the fancy alternative using stacks)
1478 for (int col = column - 1; col >= 0; col--)
1480 Annotation annotation = annotations[col];
1481 if (annotation == null)
1485 String displayed = annotation.displayCharacter;
1486 if (displayed == null || displayed.length() != 1)
1490 char symbol = displayed.charAt(0);
1491 if (!Rna.isOpeningParenthesis(symbol))
1497 * found an opening bracket symbol
1498 * count (closing-opening) symbols of this type that follow it,
1499 * up to and excluding the target column; if the count is less
1500 * than 1, the opening bracket is unmatched, so return its match
1502 String closer = String
1503 .valueOf(Rna.getMatchingClosingParenthesis(symbol));
1504 String opener = String.valueOf(symbol);
1506 for (int j = col + 1; j < column; j++)
1508 if (annotations[j] != null)
1510 String s = annotations[j].displayCharacter;
1511 if (closer.equals(s))
1515 else if (opener.equals(s))
1529 protected static synchronized long nextId()
1536 * @return true for rows that have a range of values in their annotation set
1538 public boolean isQuantitative()
1540 return graphMin < graphMax;
1544 * delete any columns in alignmentAnnotation that are hidden (including
1545 * sequence associated annotation).
1547 * @param hiddenColumns
1548 * the set of hidden columns
1550 public void makeVisibleAnnotation(HiddenColumns hiddenColumns)
1552 if (annotations != null)
1554 makeVisibleAnnotation(0, annotations.length, hiddenColumns);
1559 * delete any columns in alignmentAnnotation that are hidden (including
1560 * sequence associated annotation).
1563 * remove any annotation to the right of this column
1565 * remove any annotation to the left of this column
1566 * @param hiddenColumns
1567 * the set of hidden columns
1569 public void makeVisibleAnnotation(int start, int end,
1570 HiddenColumns hiddenColumns)
1572 if (annotations != null)
1574 if (hiddenColumns.hasHiddenColumns())
1576 removeHiddenAnnotation(start, end, hiddenColumns);
1580 restrict(start, end);
1586 * The actual implementation of deleting hidden annotation columns
1589 * remove any annotation to the right of this column
1591 * remove any annotation to the left of this column
1592 * @param hiddenColumns
1593 * the set of hidden columns
1595 private void removeHiddenAnnotation(int start, int end,
1596 HiddenColumns hiddenColumns)
1598 // mangle the alignmentAnnotation annotation array
1599 ArrayList<Annotation[]> annels = new ArrayList<>();
1600 Annotation[] els = null;
1604 Iterator<int[]> blocks = hiddenColumns.getVisContigsIterator(start,
1608 int annotationLength;
1609 while (blocks.hasNext())
1611 int[] block = blocks.next();
1612 annotationLength = block[1] - block[0] + 1;
1614 if (blocks.hasNext())
1616 // copy just the visible segment of the annotation row
1617 copylength = annotationLength;
1621 if (annotationLength + block[0] <= annotations.length)
1623 // copy just the visible segment of the annotation row
1624 copylength = annotationLength;
1628 // copy to the end of the annotation row
1629 copylength = annotations.length - block[0];
1633 els = new Annotation[annotationLength];
1635 System.arraycopy(annotations, block[0], els, 0, copylength);
1636 w += annotationLength;
1641 annotations = new Annotation[w];
1644 for (Annotation[] chnk : annels)
1646 System.arraycopy(chnk, 0, annotations, w, chnk.length);
1652 public static Iterable<AlignmentAnnotation> findAnnotations(
1653 Iterable<AlignmentAnnotation> list, SequenceI seq, String calcId,
1657 ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
1658 for (AlignmentAnnotation ann : list)
1660 if ((calcId == null || (ann.getCalcId() != null
1661 && ann.getCalcId().equals(calcId)))
1662 && (seq == null || (ann.sequenceRef != null
1663 && ann.sequenceRef == seq))
1665 || (ann.label != null && ann.label.equals(label))))
1674 * Answer true if any annotation matches the calcId passed in (if not null).
1677 * annotation to search
1681 public static boolean hasAnnotation(List<AlignmentAnnotation> list,
1685 if (calcId != null && !"".equals(calcId))
1687 for (AlignmentAnnotation a : list)
1689 if (a.getCalcId() == calcId)
1698 public static Iterable<AlignmentAnnotation> findAnnotation(
1699 List<AlignmentAnnotation> list, String calcId)
1702 List<AlignmentAnnotation> aa = new ArrayList<>();
1707 for (AlignmentAnnotation a : list)
1710 if (a.getCalcId() == calcId || (a.getCalcId() != null
1711 && calcId != null && a.getCalcId().equals(calcId)))