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 jalview.analysis.Rna;
24 import jalview.analysis.SecStrConsensus.SimpleBP;
25 import jalview.analysis.WUSSParseException;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.Map.Entry;
42 public class AlignmentAnnotation
44 private static final String ANNOTATION_ID_PREFIX = "ann";
47 * Identifers for different types of profile data
49 public static final int SEQUENCE_PROFILE = 0;
51 public static final int STRUCTURE_PROFILE = 1;
53 public static final int CDNA_PROFILE = 2;
55 private static long counter = 0;
58 * If true, this annotations is calculated every edit, eg consensus, quality
59 * or conservation graphs
61 public boolean autoCalculated = false;
64 * unique ID for this annotation, used to match up the same annotation row
65 * shown in multiple views and alignments
67 public String annotationId;
70 * the sequence this annotation is associated with (or null)
72 public SequenceI sequenceRef;
74 /** label shown in dropdown menus and in the annotation label area */
77 /** longer description text shown as a tooltip */
78 public String description;
80 /** Array of annotations placed in the current coordinate system */
81 public Annotation[] annotations;
83 public List<SimpleBP> bps = null;
86 * RNA secondary structure contact positions
88 public SequenceFeature[] _rnasecstr = null;
91 * position of annotation resulting in invalid WUSS parsing or -1. -2 means
92 * there was no RNA structure in this annotation
94 private long invalidrnastruc = -2;
97 * Updates the _rnasecstr field Determines the positions that base pair and
98 * the positions of helices based on secondary structure from a Stockholm file
100 * @param rnaAnnotation
102 private void _updateRnaSecStr(CharSequence rnaAnnotation)
106 _rnasecstr = Rna.getHelixMap(rnaAnnotation);
107 invalidrnastruc = -1;
108 } catch (WUSSParseException px)
110 // DEBUG System.out.println(px);
111 invalidrnastruc = px.getProblemPos();
113 if (invalidrnastruc > -1)
118 if (_rnasecstr != null && _rnasecstr.length > 0)
120 // show all the RNA secondary structure annotation symbols.
122 showAllColLabels = true;
123 scaleColLabel = true;
126 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
130 private void _markRnaHelices()
133 // Figure out number of helices
134 // Length of rnasecstr is the number of pairs of positions that base pair
135 // with each other in the secondary structure
136 for (int x = 0; x < _rnasecstr.length; x++)
140 * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
141 * this.annotation._rnasecstr[x].getBegin());
143 // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
147 val = Integer.valueOf(_rnasecstr[x].getFeatureGroup());
152 } catch (NumberFormatException q)
157 annotations[_rnasecstr[x].getBegin()].value = val;
158 annotations[_rnasecstr[x].getEnd()].value = val;
160 // annotations[_rnasecstr[x].getBegin()].displayCharacter = "" + val;
161 // annotations[_rnasecstr[x].getEnd()].displayCharacter = "" + val;
167 * map of positions in the associated annotation
169 private Map<Integer, Annotation> sequenceMapping;
172 * lower range for quantitative data
174 public float graphMin;
177 * Upper range for quantitative data
179 public float graphMax;
182 * Score associated with label and description.
184 public double score = Double.NaN;
187 * flag indicating if annotation has a score.
189 public boolean hasScore = false;
191 public GraphLine threshold;
193 // Graphical hints and tips
195 /** Can this row be edited by the user ? */
196 public boolean editable = false;
198 /** Indicates if annotation has a graphical symbol track */
199 public boolean hasIcons; //
201 /** Indicates if annotation has a text character label */
202 public boolean hasText;
204 /** is the row visible */
205 public boolean visible = true;
207 public int graphGroup = -1;
209 /** Displayed height of row in pixels */
210 public int height = 0;
212 public int graph = 0;
214 public int graphHeight = 40;
216 public boolean padGaps = false;
218 public static final int NO_GRAPH = 0;
220 public static final int BAR_GRAPH = 1;
222 public static final int LINE_GRAPH = 2;
224 public boolean belowAlignment = true;
226 public SequenceGroup groupRef = null;
229 * display every column label, even if there is a row of identical labels
231 public boolean showAllColLabels = false;
234 * scale the column label to fit within the alignment column.
236 public boolean scaleColLabel = false;
239 * centre the column labels relative to the alignment column
241 public boolean centreColLabels = false;
243 private boolean isrna;
245 public static int getGraphValueFromString(String string)
247 if (string.equalsIgnoreCase("BAR_GRAPH"))
251 else if (string.equalsIgnoreCase("LINE_GRAPH"))
262 * Creates a new AlignmentAnnotation object.
265 * short label shown under sequence labels
267 * text displayed on mouseover
269 * set of positional annotation elements
271 public AlignmentAnnotation(String label, String description,
272 Annotation[] annotations)
278 this.description = description;
279 this.annotations = annotations;
281 validateRangeAndDisplay();
285 * Checks if annotation labels represent secondary structures
288 void areLabelsSecondaryStructure()
290 boolean nonSSLabel = false;
292 StringBuffer rnastring = new StringBuffer();
295 for (int i = 0; i < annotations.length; i++)
297 // DEBUG System.out.println(i + ": " + annotations[i]);
298 if (annotations[i] == null)
302 if (annotations[i].secondaryStructure == 'H'
303 || annotations[i].secondaryStructure == 'E')
305 // DEBUG System.out.println( "/H|E/ '" +
306 // annotations[i].secondaryStructure + "'");
310 // Check for RNA secondary structure
312 // DEBUG System.out.println( "/else/ '" +
313 // annotations[i].secondaryStructure + "'");
314 // TODO: 2.8.2 should this ss symbol validation check be a function in
315 // RNA/ResidueProperties ?
316 if (annotations[i].secondaryStructure == '('
317 || annotations[i].secondaryStructure == '['
318 || annotations[i].secondaryStructure == '<'
319 || annotations[i].secondaryStructure == '{'
320 || annotations[i].secondaryStructure == 'A'
321 || annotations[i].secondaryStructure == 'B'
322 || annotations[i].secondaryStructure == 'C'
323 || annotations[i].secondaryStructure == 'D'
324 // || annotations[i].secondaryStructure == 'E'
325 || annotations[i].secondaryStructure == 'F'
326 || annotations[i].secondaryStructure == 'G'
327 // || annotations[i].secondaryStructure == 'H'
328 || annotations[i].secondaryStructure == 'I'
329 || annotations[i].secondaryStructure == 'J'
330 || annotations[i].secondaryStructure == 'K'
331 || annotations[i].secondaryStructure == 'L'
332 || annotations[i].secondaryStructure == 'M'
333 || annotations[i].secondaryStructure == 'N'
334 || annotations[i].secondaryStructure == 'O'
335 || annotations[i].secondaryStructure == 'P'
336 || annotations[i].secondaryStructure == 'Q'
337 || annotations[i].secondaryStructure == 'R'
338 || annotations[i].secondaryStructure == 'S'
339 || annotations[i].secondaryStructure == 'T'
340 || annotations[i].secondaryStructure == 'U'
341 || annotations[i].secondaryStructure == 'V'
342 || annotations[i].secondaryStructure == 'W'
343 || annotations[i].secondaryStructure == 'X'
344 || annotations[i].secondaryStructure == 'Y'
345 || annotations[i].secondaryStructure == 'Z')
352 // System.out.println("displaychar " + annotations[i].displayCharacter);
354 if (annotations[i].displayCharacter == null
355 || annotations[i].displayCharacter.length() == 0)
357 rnastring.append('.');
360 if (annotations[i].displayCharacter.length() == 1)
362 firstChar = annotations[i].displayCharacter.charAt(0);
363 // check to see if it looks like a sequence or is secondary structure
365 if (annotations[i].secondaryStructure != ' ' && !hasIcons &&
366 // Uncomment to only catch case where
367 // displayCharacter==secondary
369 // to correctly redisplay SS annotation imported from Stockholm,
370 // exported to JalviewXML and read back in again.
372 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
373 firstChar != ' ' && firstChar != '$' && firstChar != 0xCE
374 && firstChar != '(' && firstChar != '[' && firstChar != '<'
375 && firstChar != '{' && firstChar != 'A' && firstChar != 'B'
376 && firstChar != 'C' && firstChar != 'D' && firstChar != 'E'
377 && firstChar != 'F' && firstChar != 'G' && firstChar != 'H'
378 && firstChar != 'I' && firstChar != 'J' && firstChar != 'K'
379 && firstChar != 'L' && firstChar != 'M' && firstChar != 'N'
380 && firstChar != 'O' && firstChar != 'P' && firstChar != 'Q'
381 && firstChar != 'R' && firstChar != 'S' && firstChar != 'T'
382 && firstChar != 'U' && firstChar != 'V' && firstChar != 'W'
383 && firstChar != 'X' && firstChar != 'Y' && firstChar != 'Z'
385 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
387 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
400 rnastring.append(annotations[i].displayCharacter.charAt(1));
403 if (annotations[i].displayCharacter.length() > 0)
412 for (int j = 0; j < annotations.length; j++)
414 if (annotations[j] != null
415 && annotations[j].secondaryStructure != ' ')
417 annotations[j].displayCharacter = String
418 .valueOf(annotations[j].secondaryStructure);
419 annotations[j].secondaryStructure = ' ';
428 _updateRnaSecStr(new AnnotCharSequence());
434 * flyweight access to positions in the alignment annotation row for RNA
440 private class AnnotCharSequence implements CharSequence
446 public AnnotCharSequence()
448 this(0, annotations.length);
451 AnnotCharSequence(int start, int end)
458 public CharSequence subSequence(int start, int end)
460 return new AnnotCharSequence(offset + start, offset + end);
470 public char charAt(int index)
472 return ((index + offset < 0) || (index + offset) >= max
473 || annotations[index + offset] == null
474 || (annotations[index + offset].secondaryStructure <= ' ')
476 : annotations[index + offset].displayCharacter == null
478 + offset].displayCharacter
481 + offset].secondaryStructure
483 + offset].displayCharacter
488 public String toString()
490 char[] string = new char[max - offset];
491 int mx = annotations.length;
493 for (int i = offset; i < mx; i++)
495 string[i] = (annotations[i] == null
496 || (annotations[i].secondaryStructure <= 32))
498 : (annotations[i].displayCharacter == null
499 || annotations[i].displayCharacter
501 ? annotations[i].secondaryStructure
502 : annotations[i].displayCharacter
505 return new String(string);
509 private long _lastrnaannot = -1;
511 public String getRNAStruc()
515 String rnastruc = new AnnotCharSequence().toString();
516 if (_lastrnaannot != rnastruc.hashCode())
518 // ensure rna structure contacts are up to date
519 _lastrnaannot = rnastruc.hashCode();
520 _updateRnaSecStr(rnastruc);
528 * Creates a new AlignmentAnnotation object.
543 public AlignmentAnnotation(String label, String description,
544 Annotation[] annotations, float min, float max, int graphType)
547 // graphs are not editable
548 editable = graphType == 0;
551 this.description = description;
552 this.annotations = annotations;
556 validateRangeAndDisplay();
560 * checks graphMin and graphMax, secondary structure symbols, sets graphType
561 * appropriately, sets null labels to the empty string if appropriate.
563 public void validateRangeAndDisplay()
566 if (annotations == null)
568 visible = false; // try to prevent renderer from displaying.
569 invalidrnastruc = -1;
570 return; // this is a non-annotation row annotation - ie a sequence score.
573 int graphType = graph;
574 float min = graphMin;
575 float max = graphMax;
576 boolean drawValues = true;
581 for (int i = 0; i < annotations.length; i++)
583 if (annotations[i] == null)
588 if (drawValues && annotations[i].displayCharacter != null
589 && annotations[i].displayCharacter.length() > 1)
594 if (annotations[i].value > max)
596 max = annotations[i].value;
599 if (annotations[i].value < min)
601 min = annotations[i].value;
603 if (_linecolour == null && annotations[i].colour != null)
605 _linecolour = annotations[i].colour;
608 // ensure zero is origin for min/max ranges on only one side of zero
625 areLabelsSecondaryStructure();
627 if (!drawValues && graphType != NO_GRAPH)
629 for (int i = 0; i < annotations.length; i++)
631 if (annotations[i] != null)
633 annotations[i].displayCharacter = "";
640 * Copy constructor creates a new independent annotation row with the same
641 * associated sequenceRef
645 public AlignmentAnnotation(AlignmentAnnotation annotation)
648 this.label = new String(annotation.label);
649 if (annotation.description != null)
651 this.description = new String(annotation.description);
653 this.graphMin = annotation.graphMin;
654 this.graphMax = annotation.graphMax;
655 this.graph = annotation.graph;
656 this.graphHeight = annotation.graphHeight;
657 this.graphGroup = annotation.graphGroup;
658 this.groupRef = annotation.groupRef;
659 this.editable = annotation.editable;
660 this.autoCalculated = annotation.autoCalculated;
661 this.hasIcons = annotation.hasIcons;
662 this.hasText = annotation.hasText;
663 this.height = annotation.height;
664 this.label = annotation.label;
665 this.padGaps = annotation.padGaps;
666 this.visible = annotation.visible;
667 this.centreColLabels = annotation.centreColLabels;
668 this.scaleColLabel = annotation.scaleColLabel;
669 this.showAllColLabels = annotation.showAllColLabels;
670 this.calcId = annotation.calcId;
671 if (annotation.properties != null)
673 properties = new HashMap<>();
674 for (Map.Entry<String, String> val : annotation.properties.entrySet())
676 properties.put(val.getKey(), val.getValue());
679 if (this.hasScore = annotation.hasScore)
681 this.score = annotation.score;
683 if (annotation.threshold != null)
685 threshold = new GraphLine(annotation.threshold);
687 Annotation[] ann = annotation.annotations;
688 if (annotation.annotations != null)
690 this.annotations = new Annotation[ann.length];
691 for (int i = 0; i < ann.length; i++)
695 annotations[i] = new Annotation(ann[i]);
696 if (_linecolour != null)
698 _linecolour = annotations[i].colour;
703 if (annotation.sequenceRef != null)
705 this.sequenceRef = annotation.sequenceRef;
706 if (annotation.sequenceMapping != null)
709 sequenceMapping = new HashMap<>();
710 Iterator<Integer> pos = annotation.sequenceMapping.keySet()
712 while (pos.hasNext())
714 // could optimise this!
716 Annotation a = annotation.sequenceMapping.get(p);
723 for (int i = 0; i < ann.length; i++)
727 sequenceMapping.put(p, annotations[i]);
735 this.sequenceMapping = null;
738 // TODO: check if we need to do this: JAL-952
739 // if (this.isrna=annotation.isrna)
741 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
743 validateRangeAndDisplay(); // construct hashcodes, etc.
747 * clip the annotation to the columns given by startRes and endRes (inclusive)
748 * and prune any existing sequenceMapping to just those columns.
753 public void restrict(int startRes, int endRes)
755 if (annotations == null)
764 if (startRes >= annotations.length)
766 startRes = annotations.length - 1;
768 if (endRes >= annotations.length)
770 endRes = annotations.length - 1;
772 if (annotations == null)
776 Annotation[] temp = new Annotation[endRes - startRes + 1];
777 if (startRes < annotations.length)
779 System.arraycopy(annotations, startRes, temp, 0,
780 endRes - startRes + 1);
782 if (sequenceRef != null)
784 // Clip the mapping, if it exists.
785 int spos = sequenceRef.findPosition(startRes);
786 int epos = sequenceRef.findPosition(endRes);
787 if (sequenceMapping != null)
789 Map<Integer, Annotation> newmapping = new HashMap<>();
790 Iterator<Integer> e = sequenceMapping.keySet().iterator();
793 Integer pos = e.next();
794 if (pos.intValue() >= spos && pos.intValue() <= epos)
796 newmapping.put(pos, sequenceMapping.get(pos));
799 sequenceMapping.clear();
800 sequenceMapping = newmapping;
807 * set the annotation row to be at least length Annotations
810 * minimum number of columns required in the annotation row
811 * @return false if the annotation row is greater than length
813 public boolean padAnnotation(int length)
815 if (annotations == null)
817 return true; // annotation row is correct - null == not visible and
820 if (annotations.length < length)
822 Annotation[] na = new Annotation[length];
823 System.arraycopy(annotations, 0, na, 0, annotations.length);
827 return annotations.length > length;
834 * @return DOCUMENT ME!
837 public String toString()
839 if (annotations == null)
843 StringBuilder buffer = new StringBuilder(256);
845 for (int i = 0; i < annotations.length; i++)
847 if (annotations[i] != null)
851 buffer.append(annotations[i].value);
855 buffer.append(annotations[i].secondaryStructure);
859 buffer.append(annotations[i].displayCharacter);
865 // TODO: remove disgusting hack for 'special' treatment of consensus line.
866 if (label.indexOf("Consensus") == 0)
870 for (int i = 0; i < annotations.length; i++)
872 if (annotations[i] != null)
874 buffer.append(annotations[i].description);
881 return buffer.toString();
884 public void setThreshold(GraphLine line)
889 public GraphLine getThreshold()
895 * Attach the annotation to seqRef, starting from startRes position. If
896 * alreadyMapped is true then the indices of the annotation[] array are
897 * sequence positions rather than alignment column positions.
901 * @param alreadyMapped
903 public void createSequenceMapping(SequenceI seqRef, int startRes,
904 boolean alreadyMapped)
911 sequenceRef = seqRef;
912 if (annotations == null)
916 sequenceMapping = new HashMap<>();
920 for (int i = 0; i < annotations.length; i++)
922 if (annotations[i] != null)
926 seqPos = seqRef.findPosition(i);
930 seqPos = i + startRes;
933 sequenceMapping.put(new Integer(seqPos), annotations[i]);
940 * When positional annotation and a sequence reference is present, clears and
941 * resizes the annotations array to the current alignment width, and adds
942 * annotation according to aligned positions of the sequenceRef given by
945 public void adjustForAlignment()
947 if (sequenceRef == null)
952 if (annotations == null)
957 int a = 0, aSize = sequenceRef.getLength();
966 Annotation[] temp = new Annotation[aSize];
968 if (sequenceMapping != null)
970 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
972 index = new Integer(a);
973 Annotation annot = sequenceMapping.get(index);
976 position = sequenceRef.findIndex(a) - 1;
978 temp[position] = annot;
986 * remove any null entries in annotation row and return the number of non-null
987 * annotation elements.
991 public int compactAnnotationArray()
993 int i = 0, iSize = annotations.length;
996 if (annotations[i] == null)
1000 System.arraycopy(annotations, i + 1, annotations, i,
1010 Annotation[] ann = annotations;
1011 annotations = new Annotation[i];
1012 System.arraycopy(ann, 0, annotations, 0, i);
1018 * Associate this annotation with the aligned residues of a particular
1019 * sequence. sequenceMapping will be updated in the following way: null
1020 * sequenceI - existing mapping will be discarded but annotations left in
1021 * mapped positions. valid sequenceI not equal to current sequenceRef: mapping
1022 * is discarded and rebuilt assuming 1:1 correspondence TODO: overload with
1023 * parameter to specify correspondence between current and new sequenceRef
1027 public void setSequenceRef(SequenceI sequenceI)
1029 if (sequenceI != null)
1031 if (sequenceRef != null)
1033 boolean rIsDs = sequenceRef.getDatasetSequence() == null,
1034 tIsDs = sequenceI.getDatasetSequence() == null;
1035 if (sequenceRef != sequenceI
1037 && sequenceRef != sequenceI.getDatasetSequence())
1039 && sequenceRef.getDatasetSequence() != sequenceI)
1040 && (!rIsDs && !tIsDs
1041 && sequenceRef.getDatasetSequence() != sequenceI
1042 .getDatasetSequence())
1043 && !sequenceRef.equals(sequenceI))
1045 // if sequenceRef isn't intersecting with sequenceI
1046 // throw away old mapping and reconstruct.
1048 if (sequenceMapping != null)
1050 sequenceMapping = null;
1051 // compactAnnotationArray();
1053 createSequenceMapping(sequenceI, 1, true);
1054 adjustForAlignment();
1058 // Mapping carried over
1059 sequenceRef = sequenceI;
1064 // No mapping exists
1065 createSequenceMapping(sequenceI, 1, true);
1066 adjustForAlignment();
1071 // throw away the mapping without compacting.
1072 sequenceMapping = null;
1080 public double getScore()
1089 public void setScore(double score)
1097 * @return true if annotation has an associated score
1099 public boolean hasScore()
1101 return hasScore || !Double.isNaN(score);
1105 * Score only annotation
1108 * @param description
1111 public AlignmentAnnotation(String label, String description, double score)
1113 this(label, description, null);
1118 * copy constructor with edit based on the hidden columns marked in colSel
1120 * @param alignmentAnnotation
1123 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1124 HiddenColumns hidden)
1126 this(alignmentAnnotation);
1127 if (annotations == null)
1131 makeVisibleAnnotation(hidden);
1134 public void setPadGaps(boolean padgaps, char gapchar)
1136 this.padGaps = padgaps;
1140 for (int i = 0; i < annotations.length; i++)
1142 if (annotations[i] == null)
1144 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1147 else if (annotations[i].displayCharacter == null
1148 || annotations[i].displayCharacter.equals(" "))
1150 annotations[i].displayCharacter = String.valueOf(gapchar);
1157 * format description string for display
1160 * @return Get the annotation description string optionally prefixed by
1161 * associated sequence name (if any)
1163 public String getDescription(boolean seqname)
1165 if (seqname && this.sequenceRef != null)
1167 int i = description.toLowerCase().indexOf("<html>");
1170 // move the html tag to before the sequence reference.
1171 return "<html>" + sequenceRef.getName() + " : "
1172 + description.substring(i + 6);
1174 return sequenceRef.getName() + " : " + description;
1179 public boolean isValidStruc()
1181 return invalidrnastruc == -1;
1184 public long getInvalidStrucPos()
1186 return invalidrnastruc;
1190 * machine readable ID string indicating what generated this annotation
1192 protected String calcId = "";
1195 * properties associated with the calcId
1197 protected Map<String, String> properties = new HashMap<>();
1200 * base colour for line graphs. If null, will be set automatically by
1201 * searching the alignment annotation
1203 public java.awt.Color _linecolour;
1205 public String getCalcId()
1210 public void setCalcId(String calcId)
1212 this.calcId = calcId;
1215 public boolean isRNA()
1221 * transfer annotation to the given sequence using the given mapping from the
1222 * current positions or an existing sequence mapping
1226 * map involving sq as To or From
1228 public void liftOver(SequenceI sq, Mapping sp2sq)
1230 if (sp2sq.getMappedWidth() != sp2sq.getWidth())
1232 // TODO: employ getWord/MappedWord to transfer annotation between cDNA and
1233 // Protein reference frames
1235 "liftOver currently not implemented for transfer of annotation between different types of seqeunce");
1237 boolean mapIsTo = (sp2sq != null)
1238 ? (sp2sq.getTo() == sq
1239 || sp2sq.getTo() == sq.getDatasetSequence())
1242 // TODO build a better annotation element map and get rid of annotations[]
1243 Map<Integer, Annotation> mapForsq = new HashMap<>();
1244 if (sequenceMapping != null)
1248 for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
1250 Integer mpos = Integer
1251 .valueOf(mapIsTo ? sp2sq.getMappedPosition(ie.getKey())
1252 : sp2sq.getPosition(ie.getKey()));
1253 if (mpos >= sq.getStart() && mpos <= sq.getEnd())
1255 mapForsq.put(mpos, ie.getValue());
1258 sequenceMapping = mapForsq;
1260 adjustForAlignment();
1270 * like liftOver but more general.
1272 * Takes an array of int pairs that will be used to update the internal
1273 * sequenceMapping and so shuffle the annotated positions
1276 * - new sequence reference for the annotation row - if null,
1277 * sequenceRef is left unchanged
1279 * array of ints containing corresponding positions
1281 * - column for current coordinate system (-1 for index+1)
1283 * - column for destination coordinate system (-1 for index+1)
1285 * - offset added to index when referencing either coordinate system
1286 * @note no checks are made as to whether from and/or to are sensible
1287 * @note caller should add the remapped annotation to newref if they have not
1290 public void remap(SequenceI newref, HashMap<Integer, int[]> mapping,
1291 int from, int to, int idxoffset)
1293 if (mapping != null)
1295 Map<Integer, Annotation> old = sequenceMapping;
1296 Map<Integer, Annotation> remap = new HashMap<>();
1298 for (int mp[] : mapping.values())
1304 Annotation ann = null;
1307 ann = sequenceMapping.get(Integer.valueOf(idxoffset + index));
1311 if (mp != null && mp.length > from)
1313 ann = sequenceMapping.get(Integer.valueOf(mp[from]));
1320 remap.put(Integer.valueOf(idxoffset + index), ann);
1324 if (to > -1 && to < mp.length)
1326 remap.put(Integer.valueOf(mp[to]), ann);
1331 sequenceMapping = remap;
1335 sequenceRef = newref;
1337 adjustForAlignment();
1341 public String getProperty(String property)
1343 if (properties == null)
1347 return properties.get(property);
1350 public void setProperty(String property, String value)
1352 if (properties == null)
1354 properties = new HashMap<>();
1356 properties.put(property, value);
1359 public boolean hasProperties()
1361 return properties != null && properties.size() > 0;
1364 public Collection<String> getProperties()
1366 if (properties == null)
1368 return Collections.emptyList();
1370 return properties.keySet();
1374 * Returns the Annotation for the given sequence position (base 1) if any,
1380 public Annotation getAnnotationForPosition(int position)
1382 return sequenceMapping == null ? null : sequenceMapping.get(position);
1387 * Set the id to "ann" followed by a counter that increments so as to be
1388 * unique for the lifetime of the JVM
1390 protected final void setAnnotationId()
1392 this.annotationId = ANNOTATION_ID_PREFIX + Long.toString(nextId());
1396 * Returns the match for the last unmatched opening RNA helix pair symbol
1397 * preceding the given column, or '(' if nothing found to match.
1402 public String getDefaultRnaHelixSymbol(int column)
1404 String result = "(";
1405 if (annotations == null)
1411 * for each preceding column, if it contains an open bracket,
1412 * count whether it is still unmatched at column, if so return its pair
1413 * (likely faster than the fancy alternative using stacks)
1415 for (int col = column - 1; col >= 0; col--)
1417 Annotation annotation = annotations[col];
1418 if (annotation == null)
1422 String displayed = annotation.displayCharacter;
1423 if (displayed == null || displayed.length() != 1)
1427 char symbol = displayed.charAt(0);
1428 if (!Rna.isOpeningParenthesis(symbol))
1434 * found an opening bracket symbol
1435 * count (closing-opening) symbols of this type that follow it,
1436 * up to and excluding the target column; if the count is less
1437 * than 1, the opening bracket is unmatched, so return its match
1439 String closer = String
1440 .valueOf(Rna.getMatchingClosingParenthesis(symbol));
1441 String opener = String.valueOf(symbol);
1443 for (int j = col + 1; j < column; j++)
1445 if (annotations[j] != null)
1447 String s = annotations[j].displayCharacter;
1448 if (closer.equals(s))
1452 else if (opener.equals(s))
1466 protected static synchronized long nextId()
1473 * @return true for rows that have a range of values in their annotation set
1475 public boolean isQuantitative()
1477 return graphMin < graphMax;
1481 * delete any columns in alignmentAnnotation that are hidden (including
1482 * sequence associated annotation).
1484 * @param hiddenColumns
1485 * the set of hidden columns
1487 public void makeVisibleAnnotation(HiddenColumns hiddenColumns)
1489 if (annotations != null)
1491 makeVisibleAnnotation(0, annotations.length, hiddenColumns);
1496 * delete any columns in alignmentAnnotation that are hidden (including
1497 * sequence associated annotation).
1500 * remove any annotation to the right of this column
1502 * remove any annotation to the left of this column
1503 * @param hiddenColumns
1504 * the set of hidden columns
1506 public void makeVisibleAnnotation(int start, int end,
1507 HiddenColumns hiddenColumns)
1509 if (annotations != null)
1511 if (hiddenColumns.hasHiddenColumns())
1513 removeHiddenAnnotation(start, end, hiddenColumns);
1517 restrict(start, end);
1523 * The actual implementation of deleting hidden annotation columns
1526 * remove any annotation to the right of this column
1528 * remove any annotation to the left of this column
1529 * @param hiddenColumns
1530 * the set of hidden columns
1532 private void removeHiddenAnnotation(int start, int end,
1533 HiddenColumns hiddenColumns)
1535 // mangle the alignmentAnnotation annotation array
1536 ArrayList<Annotation[]> annels = new ArrayList<>();
1537 Annotation[] els = null;
1541 Iterator<int[]> blocks = hiddenColumns.getVisContigsIterator(start,
1545 int annotationLength;
1546 while (blocks.hasNext())
1548 int[] block = blocks.next();
1549 annotationLength = block[1] - block[0] + 1;
1551 if (blocks.hasNext())
1553 // copy just the visible segment of the annotation row
1554 copylength = annotationLength;
1558 if (annotationLength + block[0] <= annotations.length)
1560 // copy just the visible segment of the annotation row
1561 copylength = annotationLength;
1565 // copy to the end of the annotation row
1566 copylength = annotations.length - block[0];
1570 els = new Annotation[annotationLength];
1572 System.arraycopy(annotations, block[0], els, 0, copylength);
1573 w += annotationLength;
1578 annotations = new Annotation[w];
1581 for (Annotation[] chnk : annels)
1583 System.arraycopy(chnk, 0, annotations, w, chnk.length);
1589 public static Iterable<AlignmentAnnotation> findAnnotations(
1590 Iterable<AlignmentAnnotation> list, SequenceI seq, String calcId,
1594 ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
1595 for (AlignmentAnnotation ann : list)
1597 if ((calcId == null || (ann.getCalcId() != null
1598 && ann.getCalcId().equals(calcId)))
1599 && (seq == null || (ann.sequenceRef != null
1600 && ann.sequenceRef == seq))
1602 || (ann.label != null && ann.label.equals(label))))
1611 * Answer true if any annotation matches the calcId passed in (if not null).
1614 * annotation to search
1618 public static boolean hasAnnotation(List<AlignmentAnnotation> list,
1622 if (calcId != null && !"".equals(calcId))
1624 for (AlignmentAnnotation a : list)
1626 if (a.getCalcId() == calcId)
1635 public static Iterable<AlignmentAnnotation> findAnnotation(
1636 List<AlignmentAnnotation> list, String calcId)
1639 List<AlignmentAnnotation> aa = new ArrayList<>();
1644 for (AlignmentAnnotation a : list)
1647 if (a.getCalcId() == calcId || (a.getCalcId() != null
1648 && calcId != null && a.getCalcId().equals(calcId)))