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 * map of positions in the associated annotation
99 private Map<Integer, Annotation> sequenceMapping;
102 * lower range for quantitative data
104 public float graphMin;
107 * Upper range for quantitative data
109 public float graphMax;
112 * Score associated with label and description.
114 public double score = Double.NaN;
117 * flag indicating if annotation has a score.
119 public boolean hasScore = false;
121 public GraphLine threshold;
123 // Graphical hints and tips
125 /** Can this row be edited by the user ? */
126 public boolean editable = false;
128 /** Indicates if annotation has a graphical symbol track */
129 public boolean hasIcons; //
131 /** Indicates if annotation has a text character label */
132 public boolean hasText;
134 /** is the row visible */
135 public boolean visible = true;
137 public int graphGroup = -1;
139 /** Displayed height of row in pixels */
140 public int height = 0;
142 public int graph = 0;
144 public int graphHeight = 40;
146 public boolean padGaps = false;
148 public static final int NO_GRAPH = 0;
150 public static final int BAR_GRAPH = 1;
152 public static final int LINE_GRAPH = 2;
154 public boolean belowAlignment = true;
156 public SequenceGroup groupRef = null;
159 * display every column label, even if there is a row of identical labels
161 public boolean showAllColLabels = false;
164 * scale the column label to fit within the alignment column.
166 public boolean scaleColLabel = false;
169 * centre the column labels relative to the alignment column
171 public boolean centreColLabels = false;
173 private boolean isrna;
175 public static int getGraphValueFromString(String string)
177 if (string.equalsIgnoreCase("BAR_GRAPH"))
181 else if (string.equalsIgnoreCase("LINE_GRAPH"))
192 * Creates a new AlignmentAnnotation object.
195 * short label shown under sequence labels
197 * text displayed on mouseover
199 * set of positional annotation elements
201 public AlignmentAnnotation(String label, String description,
202 Annotation[] annotations)
208 this.description = description;
209 this.annotations = annotations;
211 validateRangeAndDisplay();
215 * Copy constructor creates a new independent annotation row with the same
216 * associated sequenceRef
220 public AlignmentAnnotation(AlignmentAnnotation annotation)
223 this.label = new String(annotation.label);
224 if (annotation.description != null)
226 this.description = new String(annotation.description);
228 this.graphMin = annotation.graphMin;
229 this.graphMax = annotation.graphMax;
230 this.graph = annotation.graph;
231 this.graphHeight = annotation.graphHeight;
232 this.graphGroup = annotation.graphGroup;
233 this.groupRef = annotation.groupRef;
234 this.editable = annotation.editable;
235 this.autoCalculated = annotation.autoCalculated;
236 this.hasIcons = annotation.hasIcons;
237 this.hasText = annotation.hasText;
238 this.height = annotation.height;
239 this.label = annotation.label;
240 this.padGaps = annotation.padGaps;
241 this.visible = annotation.visible;
242 this.centreColLabels = annotation.centreColLabels;
243 this.scaleColLabel = annotation.scaleColLabel;
244 this.showAllColLabels = annotation.showAllColLabels;
245 this.calcId = annotation.calcId;
246 if (annotation.properties != null)
248 properties = new HashMap<>();
249 for (Map.Entry<String, String> val : annotation.properties.entrySet())
251 properties.put(val.getKey(), val.getValue());
254 if (this.hasScore = annotation.hasScore)
256 this.score = annotation.score;
258 if (annotation.threshold != null)
260 threshold = new GraphLine(annotation.threshold);
262 Annotation[] ann = annotation.annotations;
263 if (annotation.annotations != null)
265 this.annotations = new Annotation[ann.length];
266 for (int i = 0; i < ann.length; i++)
270 annotations[i] = new Annotation(ann[i]);
271 if (_linecolour != null)
273 _linecolour = annotations[i].colour;
278 if (annotation.sequenceRef != null)
280 this.sequenceRef = annotation.sequenceRef;
281 if (annotation.sequenceMapping != null)
284 sequenceMapping = new HashMap<>();
285 Iterator<Integer> pos = annotation.sequenceMapping.keySet()
287 while (pos.hasNext())
289 // could optimise this!
291 Annotation a = annotation.sequenceMapping.get(p);
298 for (int i = 0; i < ann.length; i++)
302 sequenceMapping.put(p, annotations[i]);
310 this.sequenceMapping = null;
313 // TODO: check if we need to do this: JAL-952
314 // if (this.isrna=annotation.isrna)
316 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
318 validateRangeAndDisplay(); // construct hashcodes, etc.
322 * copy constructor with edit based on the hidden columns marked in colSel
324 * @param alignmentAnnotation
327 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
328 HiddenColumns hidden)
330 this(alignmentAnnotation);
331 if (annotations == null)
335 makeVisibleAnnotation(hidden);
339 * Creates a new AlignmentAnnotation object.
354 public AlignmentAnnotation(String label, String description,
355 Annotation[] annotations, float min, float max, int graphType)
358 // graphs are not editable
359 editable = graphType == 0;
362 this.description = description;
363 this.annotations = annotations;
367 validateRangeAndDisplay();
371 * Score only annotation
377 public AlignmentAnnotation(String label, String description, double score)
379 this(label, description, null);
384 * Updates the _rnasecstr field Determines the positions that base pair and
385 * the positions of helices based on secondary structure from a Stockholm file
387 * @param rnaAnnotation
389 private void _updateRnaSecStr(CharSequence rnaAnnotation)
393 _rnasecstr = Rna.getHelixMap(rnaAnnotation);
394 invalidrnastruc = -1;
395 } catch (WUSSParseException px)
397 // DEBUG System.out.println(px);
398 invalidrnastruc = px.getProblemPos();
400 if (invalidrnastruc > -1)
405 if (_rnasecstr != null && _rnasecstr.length > 0)
407 // show all the RNA secondary structure annotation symbols.
409 showAllColLabels = true;
410 scaleColLabel = true;
413 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
417 private void _markRnaHelices()
420 // Figure out number of helices
421 // Length of rnasecstr is the number of pairs of positions that base pair
422 // with each other in the secondary structure
423 for (int x = 0; x < _rnasecstr.length; x++)
427 * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
428 * this.annotation._rnasecstr[x].getBegin());
430 // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
434 val = Integer.valueOf(_rnasecstr[x].getFeatureGroup());
439 } catch (NumberFormatException q)
444 annotations[_rnasecstr[x].getBegin()].value = val;
445 annotations[_rnasecstr[x].getEnd()].value = val;
447 // annotations[_rnasecstr[x].getBegin()].displayCharacter = "" + val;
448 // annotations[_rnasecstr[x].getEnd()].displayCharacter = "" + val;
454 * Checks if annotation labels represent secondary structures
457 void areLabelsSecondaryStructure()
459 boolean nonSSLabel = false;
461 StringBuffer rnastring = new StringBuffer();
464 for (int i = 0; i < annotations.length; i++)
466 if (annotations[i] == null)
470 if (annotations[i].secondaryStructure == 'H'
471 || annotations[i].secondaryStructure == 'E')
476 // Check for RNA secondary structure
478 // System.out.println(annotations[i].secondaryStructure);
479 // TODO: 2.8.2 should this ss symbol validation check be a function in
480 // RNA/ResidueProperties ?
481 if (annotations[i].secondaryStructure == '('
482 || annotations[i].secondaryStructure == '['
483 || annotations[i].secondaryStructure == '<'
484 || annotations[i].secondaryStructure == '{'
485 || annotations[i].secondaryStructure == 'A'
486 || annotations[i].secondaryStructure == 'B'
487 || annotations[i].secondaryStructure == 'C'
488 || annotations[i].secondaryStructure == 'D'
489 || annotations[i].secondaryStructure == 'E'
490 || annotations[i].secondaryStructure == 'F'
491 || annotations[i].secondaryStructure == 'G'
492 || annotations[i].secondaryStructure == 'H'
493 || annotations[i].secondaryStructure == 'I'
494 || annotations[i].secondaryStructure == 'J'
495 || annotations[i].secondaryStructure == 'K'
496 || annotations[i].secondaryStructure == 'L'
497 || annotations[i].secondaryStructure == 'M'
498 || annotations[i].secondaryStructure == 'N'
499 || annotations[i].secondaryStructure == 'O'
500 || annotations[i].secondaryStructure == 'P'
501 || annotations[i].secondaryStructure == 'Q'
502 || annotations[i].secondaryStructure == 'R'
503 || annotations[i].secondaryStructure == 'S'
504 || annotations[i].secondaryStructure == 'T'
505 || annotations[i].secondaryStructure == 'U'
506 || annotations[i].secondaryStructure == 'V'
507 || annotations[i].secondaryStructure == 'W'
508 || annotations[i].secondaryStructure == 'X'
509 || annotations[i].secondaryStructure == 'Y'
510 || annotations[i].secondaryStructure == 'Z')
517 // System.out.println("displaychar " + annotations[i].displayCharacter);
519 if (annotations[i].displayCharacter == null
520 || annotations[i].displayCharacter.length() == 0)
522 rnastring.append('.');
525 if (annotations[i].displayCharacter.length() == 1)
527 firstChar = annotations[i].displayCharacter.charAt(0);
528 // check to see if it looks like a sequence or is secondary structure
530 if (annotations[i].secondaryStructure != ' ' && !hasIcons &&
531 // Uncomment to only catch case where
532 // displayCharacter==secondary
534 // to correctly redisplay SS annotation imported from Stockholm,
535 // exported to JalviewXML and read back in again.
537 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
538 firstChar != ' ' && firstChar != '$' && firstChar != 0xCE
539 && firstChar != '(' && firstChar != '[' && firstChar != '>'
540 && firstChar != '{' && firstChar != 'A' && firstChar != 'B'
541 && firstChar != 'C' && firstChar != 'D' && firstChar != 'E'
542 && firstChar != 'F' && firstChar != 'G' && firstChar != 'H'
543 && firstChar != 'I' && firstChar != 'J' && firstChar != 'K'
544 && firstChar != 'L' && firstChar != 'M' && firstChar != 'N'
545 && firstChar != 'O' && firstChar != 'P' && firstChar != 'Q'
546 && firstChar != 'R' && firstChar != 'S' && firstChar != 'T'
547 && firstChar != 'U' && firstChar != 'V' && firstChar != 'W'
548 && firstChar != 'X' && firstChar != 'Y' && firstChar != 'Z'
550 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
552 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
565 rnastring.append(annotations[i].displayCharacter.charAt(1));
568 if (annotations[i].displayCharacter.length() > 0)
577 for (int j = 0; j < annotations.length; j++)
579 if (annotations[j] != null
580 && annotations[j].secondaryStructure != ' ')
582 annotations[j].displayCharacter = String
583 .valueOf(annotations[j].secondaryStructure);
584 annotations[j].secondaryStructure = ' ';
593 _updateRnaSecStr(new AnnotCharSequence());
599 * flyweight access to positions in the alignment annotation row for RNA
605 private class AnnotCharSequence implements CharSequence
611 public AnnotCharSequence()
613 this(0, annotations.length);
616 AnnotCharSequence(int start, int end)
623 public CharSequence subSequence(int start, int end)
625 return new AnnotCharSequence(offset + start, offset + end);
635 public char charAt(int index)
637 return ((index + offset < 0) || (index + offset) >= max
638 || annotations[index + offset] == null
639 || (annotations[index + offset].secondaryStructure <= ' ')
641 : annotations[index + offset].displayCharacter == null
643 + offset].displayCharacter
646 + offset].secondaryStructure
648 + offset].displayCharacter
653 public String toString()
655 char[] string = new char[max - offset];
656 int mx = annotations.length;
658 for (int i = offset; i < mx; i++)
660 string[i] = (annotations[i] == null
661 || (annotations[i].secondaryStructure <= 32))
663 : (annotations[i].displayCharacter == null
664 || annotations[i].displayCharacter
666 ? annotations[i].secondaryStructure
667 : annotations[i].displayCharacter
670 return new String(string);
674 private long _lastrnaannot = -1;
676 public String getRNAStruc()
680 String rnastruc = new AnnotCharSequence().toString();
681 if (_lastrnaannot != rnastruc.hashCode())
683 // ensure rna structure contacts are up to date
684 _lastrnaannot = rnastruc.hashCode();
685 _updateRnaSecStr(rnastruc);
693 * checks graphMin and graphMax, secondary structure symbols, sets graphType
694 * appropriately, sets null labels to the empty string if appropriate.
696 public void validateRangeAndDisplay()
699 if (annotations == null)
701 visible = false; // try to prevent renderer from displaying.
702 invalidrnastruc = -1;
703 return; // this is a non-annotation row annotation - ie a sequence score.
706 int graphType = graph;
707 float min = graphMin;
708 float max = graphMax;
709 boolean drawValues = true;
714 for (int i = 0; i < annotations.length; i++)
716 if (annotations[i] == null)
721 if (drawValues && annotations[i].displayCharacter != null
722 && annotations[i].displayCharacter.length() > 1)
727 if (annotations[i].value > max)
729 max = annotations[i].value;
732 if (annotations[i].value < min)
734 min = annotations[i].value;
736 if (_linecolour == null && annotations[i].colour != null)
738 _linecolour = annotations[i].colour;
741 // ensure zero is origin for min/max ranges on only one side of zero
758 areLabelsSecondaryStructure();
760 if (!drawValues && graphType != NO_GRAPH)
762 for (int i = 0; i < annotations.length; i++)
764 if (annotations[i] != null)
766 annotations[i].displayCharacter = "";
773 * clip the annotation to the columns given by startRes and endRes (inclusive)
774 * and prune any existing sequenceMapping to just those columns.
779 public void restrict(int startRes, int endRes)
781 if (annotations == null)
790 if (startRes >= annotations.length)
792 startRes = annotations.length - 1;
794 if (endRes >= annotations.length)
796 endRes = annotations.length - 1;
798 if (annotations == null)
802 Annotation[] temp = new Annotation[endRes - startRes + 1];
803 if (startRes < annotations.length)
805 System.arraycopy(annotations, startRes, temp, 0,
806 endRes - startRes + 1);
808 if (sequenceRef != null)
810 // Clip the mapping, if it exists.
811 int spos = sequenceRef.findPosition(startRes);
812 int epos = sequenceRef.findPosition(endRes);
813 if (sequenceMapping != null)
815 Map<Integer, Annotation> newmapping = new HashMap<>();
816 Iterator<Integer> e = sequenceMapping.keySet().iterator();
819 Integer pos = e.next();
820 if (pos.intValue() >= spos && pos.intValue() <= epos)
822 newmapping.put(pos, sequenceMapping.get(pos));
825 sequenceMapping.clear();
826 sequenceMapping = newmapping;
833 * set the annotation row to be at least length Annotations
836 * minimum number of columns required in the annotation row
837 * @return false if the annotation row is greater than length
839 public boolean padAnnotation(int length)
841 if (annotations == null)
843 return true; // annotation row is correct - null == not visible and
846 if (annotations.length < length)
848 Annotation[] na = new Annotation[length];
849 System.arraycopy(annotations, 0, na, 0, annotations.length);
853 return annotations.length > length;
860 * @return DOCUMENT ME!
863 public String toString()
865 if (annotations == null)
869 StringBuilder buffer = new StringBuilder(256);
871 for (int i = 0; i < annotations.length; i++)
873 if (annotations[i] != null)
877 buffer.append(annotations[i].value);
881 buffer.append(annotations[i].secondaryStructure);
885 buffer.append(annotations[i].displayCharacter);
891 // TODO: remove disgusting hack for 'special' treatment of consensus line.
892 if (label.indexOf("Consensus") == 0)
896 for (int i = 0; i < annotations.length; i++)
898 if (annotations[i] != null)
900 buffer.append(annotations[i].description);
907 return buffer.toString();
910 public void setThreshold(GraphLine line)
915 public GraphLine getThreshold()
921 * Attach the annotation to seqRef, starting from startRes position. If
922 * alreadyMapped is true then the indices of the annotation[] array are
923 * sequence positions rather than alignment column positions.
927 * @param alreadyMapped
929 public void createSequenceMapping(SequenceI seqRef, int startRes,
930 boolean alreadyMapped)
937 sequenceRef = seqRef;
938 if (annotations == null)
942 sequenceMapping = new HashMap<>();
946 for (int i = 0; i < annotations.length; i++)
948 if (annotations[i] != null)
952 seqPos = seqRef.findPosition(i);
956 seqPos = i + startRes;
959 sequenceMapping.put(new Integer(seqPos), annotations[i]);
966 * When positional annotation and a sequence reference is present, clears and
967 * resizes the annotations array to the current alignment width, and adds
968 * annotation according to aligned positions of the sequenceRef given by
971 public void adjustForAlignment()
973 if (sequenceRef == null)
978 if (annotations == null)
983 int a = 0, aSize = sequenceRef.getLength();
992 Annotation[] temp = new Annotation[aSize];
994 if (sequenceMapping != null)
996 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
998 index = new Integer(a);
999 Annotation annot = sequenceMapping.get(index);
1002 position = sequenceRef.findIndex(a) - 1;
1004 temp[position] = annot;
1012 * remove any null entries in annotation row and return the number of non-null
1013 * annotation elements.
1017 public int compactAnnotationArray()
1019 int i = 0, iSize = annotations.length;
1022 if (annotations[i] == null)
1026 System.arraycopy(annotations, i + 1, annotations, i,
1036 Annotation[] ann = annotations;
1037 annotations = new Annotation[i];
1038 System.arraycopy(ann, 0, annotations, 0, i);
1044 * Associate this annotation with the aligned residues of a particular
1045 * sequence. sequenceMapping will be updated in the following way: null
1046 * sequenceI - existing mapping will be discarded but annotations left in
1047 * mapped positions. valid sequenceI not equal to current sequenceRef: mapping
1048 * is discarded and rebuilt assuming 1:1 correspondence TODO: overload with
1049 * parameter to specify correspondence between current and new sequenceRef
1053 public void setSequenceRef(SequenceI sequenceI)
1055 if (sequenceI != null)
1057 if (sequenceRef != null)
1059 boolean rIsDs = sequenceRef.getDatasetSequence() == null,
1060 tIsDs = sequenceI.getDatasetSequence() == null;
1061 if (sequenceRef != sequenceI
1063 && sequenceRef != sequenceI.getDatasetSequence())
1065 && sequenceRef.getDatasetSequence() != sequenceI)
1066 && (!rIsDs && !tIsDs
1067 && sequenceRef.getDatasetSequence() != sequenceI
1068 .getDatasetSequence())
1069 && !sequenceRef.equals(sequenceI))
1071 // if sequenceRef isn't intersecting with sequenceI
1072 // throw away old mapping and reconstruct.
1074 if (sequenceMapping != null)
1076 sequenceMapping = null;
1077 // compactAnnotationArray();
1079 createSequenceMapping(sequenceI, 1, true);
1080 adjustForAlignment();
1084 // Mapping carried over
1085 sequenceRef = sequenceI;
1090 // No mapping exists
1091 createSequenceMapping(sequenceI, 1, true);
1092 adjustForAlignment();
1097 // throw away the mapping without compacting.
1098 sequenceMapping = null;
1106 public double getScore()
1115 public void setScore(double score)
1123 * @return true if annotation has an associated score
1125 public boolean hasScore()
1127 return hasScore || !Double.isNaN(score);
1130 public void setPadGaps(boolean padgaps, char gapchar)
1132 this.padGaps = padgaps;
1136 for (int i = 0; i < annotations.length; i++)
1138 if (annotations[i] == null)
1140 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1143 else if (annotations[i].displayCharacter == null
1144 || annotations[i].displayCharacter.equals(" "))
1146 annotations[i].displayCharacter = String.valueOf(gapchar);
1153 * format description string for display
1156 * @return Get the annotation description string optionally prefixed by
1157 * associated sequence name (if any)
1159 public String getDescription(boolean seqname)
1161 if (seqname && this.sequenceRef != null)
1163 int i = description.toLowerCase().indexOf("<html>");
1166 // move the html tag to before the sequence reference.
1167 return "<html>" + sequenceRef.getName() + " : "
1168 + description.substring(i + 6);
1170 return sequenceRef.getName() + " : " + description;
1175 public boolean isValidStruc()
1177 return invalidrnastruc == -1;
1180 public long getInvalidStrucPos()
1182 return invalidrnastruc;
1186 * machine readable ID string indicating what generated this annotation
1188 private String calcId = "";
1191 * properties associated with the calcId
1193 protected Map<String, String> properties = new HashMap<>();
1196 * base colour for line graphs. If null, will be set automatically by
1197 * searching the alignment annotation
1199 public java.awt.Color _linecolour;
1201 public String getCalcId()
1206 public void setCalcId(String calcId)
1208 this.calcId = calcId;
1211 public boolean isRNA()
1217 * transfer annotation to the given sequence using the given mapping from the
1218 * current positions or an existing sequence mapping
1222 * map involving sq as To or From
1224 public void liftOver(SequenceI sq, Mapping sp2sq)
1226 if (sp2sq.getMappedWidth() != sp2sq.getWidth())
1228 // TODO: employ getWord/MappedWord to transfer annotation between cDNA and
1229 // Protein reference frames
1231 "liftOver currently not implemented for transfer of annotation between different types of seqeunce");
1233 boolean mapIsTo = (sp2sq != null)
1234 ? (sp2sq.getTo() == sq
1235 || sp2sq.getTo() == sq.getDatasetSequence())
1238 // TODO build a better annotation element map and get rid of annotations[]
1239 Map<Integer, Annotation> mapForsq = new HashMap<>();
1240 if (sequenceMapping != null)
1244 for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
1246 Integer mpos = Integer
1247 .valueOf(mapIsTo ? sp2sq.getMappedPosition(ie.getKey())
1248 : sp2sq.getPosition(ie.getKey()));
1249 if (mpos >= sq.getStart() && mpos <= sq.getEnd())
1251 mapForsq.put(mpos, ie.getValue());
1254 sequenceMapping = mapForsq;
1256 adjustForAlignment();
1266 * like liftOver but more general.
1268 * Takes an array of int pairs that will be used to update the internal
1269 * sequenceMapping and so shuffle the annotated positions
1272 * - new sequence reference for the annotation row - if null,
1273 * sequenceRef is left unchanged
1275 * array of ints containing corresponding positions
1277 * - column for current coordinate system (-1 for index+1)
1279 * - column for destination coordinate system (-1 for index+1)
1281 * - offset added to index when referencing either coordinate system
1282 * @note no checks are made as to whether from and/or to are sensible
1283 * @note caller should add the remapped annotation to newref if they have not
1286 public void remap(SequenceI newref, HashMap<Integer, int[]> mapping,
1287 int from, int to, int idxoffset)
1289 if (mapping != null)
1291 Map<Integer, Annotation> old = sequenceMapping;
1292 Map<Integer, Annotation> remap = new HashMap<>();
1294 for (int mp[] : mapping.values())
1300 Annotation ann = null;
1303 ann = sequenceMapping.get(Integer.valueOf(idxoffset + index));
1307 if (mp != null && mp.length > from)
1309 ann = sequenceMapping.get(Integer.valueOf(mp[from]));
1316 remap.put(Integer.valueOf(idxoffset + index), ann);
1320 if (to > -1 && to < mp.length)
1322 remap.put(Integer.valueOf(mp[to]), ann);
1327 sequenceMapping = remap;
1331 sequenceRef = newref;
1333 adjustForAlignment();
1337 public String getProperty(String property)
1339 if (properties == null)
1343 return properties.get(property);
1346 public void setProperty(String property, String value)
1348 if (properties == null)
1350 properties = new HashMap<>();
1352 properties.put(property, value);
1355 public boolean hasProperties()
1357 return properties != null && properties.size() > 0;
1360 public Collection<String> getProperties()
1362 if (properties == null)
1364 return Collections.emptyList();
1366 return properties.keySet();
1370 * Returns the Annotation for the given sequence position (base 1) if any,
1376 public Annotation getAnnotationForPosition(int position)
1378 return sequenceMapping == null ? null : sequenceMapping.get(position);
1383 * Set the id to "ann" followed by a counter that increments so as to be
1384 * unique for the lifetime of the JVM
1386 protected final void setAnnotationId()
1388 this.annotationId = ANNOTATION_ID_PREFIX + Long.toString(nextId());
1392 * Returns the match for the last unmatched opening RNA helix pair symbol
1393 * preceding the given column, or '(' if nothing found to match.
1398 public String getDefaultRnaHelixSymbol(int column)
1400 String result = "(";
1401 if (annotations == null)
1407 * for each preceding column, if it contains an open bracket,
1408 * count whether it is still unmatched at column, if so return its pair
1409 * (likely faster than the fancy alternative using stacks)
1411 for (int col = column - 1; col >= 0; col--)
1413 Annotation annotation = annotations[col];
1414 if (annotation == null)
1418 String displayed = annotation.displayCharacter;
1419 if (displayed == null || displayed.length() != 1)
1423 char symbol = displayed.charAt(0);
1424 if (!Rna.isOpeningParenthesis(symbol))
1430 * found an opening bracket symbol
1431 * count (closing-opening) symbols of this type that follow it,
1432 * up to and excluding the target column; if the count is less
1433 * than 1, the opening bracket is unmatched, so return its match
1435 String closer = String
1436 .valueOf(Rna.getMatchingClosingParenthesis(symbol));
1437 String opener = String.valueOf(symbol);
1439 for (int j = col + 1; j < column; j++)
1441 if (annotations[j] != null)
1443 String s = annotations[j].displayCharacter;
1444 if (closer.equals(s))
1448 else if (opener.equals(s))
1462 protected static synchronized long nextId()
1469 * @return true for rows that have a range of values in their annotation set
1471 public boolean isQuantitative()
1473 return graphMin < graphMax;
1477 * delete any columns in alignmentAnnotation that are hidden (including
1478 * sequence associated annotation).
1480 * @param hiddenColumns
1481 * the set of hidden columns
1483 public void makeVisibleAnnotation(HiddenColumns hiddenColumns)
1485 if (annotations != null)
1487 makeVisibleAnnotation(0, annotations.length, hiddenColumns);
1492 * delete any columns in alignmentAnnotation that are hidden (including
1493 * sequence associated annotation).
1496 * remove any annotation to the right of this column
1498 * remove any annotation to the left of this column
1499 * @param hiddenColumns
1500 * the set of hidden columns
1502 public void makeVisibleAnnotation(int start, int end,
1503 HiddenColumns hiddenColumns)
1505 if (annotations != null)
1507 if (hiddenColumns.hasHiddenColumns())
1509 removeHiddenAnnotation(start, end, hiddenColumns);
1513 restrict(start, end);
1519 * The actual implementation of deleting hidden annotation columns
1522 * remove any annotation to the right of this column
1524 * remove any annotation to the left of this column
1525 * @param hiddenColumns
1526 * the set of hidden columns
1528 private void removeHiddenAnnotation(int start, int end,
1529 HiddenColumns hiddenColumns)
1531 // mangle the alignmentAnnotation annotation array
1532 ArrayList<Annotation[]> annels = new ArrayList<>();
1533 Annotation[] els = null;
1537 Iterator<int[]> blocks = hiddenColumns.getVisContigsIterator(start,
1541 int annotationLength;
1542 while (blocks.hasNext())
1544 int[] block = blocks.next();
1545 annotationLength = block[1] - block[0] + 1;
1547 if (blocks.hasNext())
1549 // copy just the visible segment of the annotation row
1550 copylength = annotationLength;
1554 if (annotationLength + block[0] <= annotations.length)
1556 // copy just the visible segment of the annotation row
1557 copylength = annotationLength;
1561 // copy to the end of the annotation row
1562 copylength = annotations.length - block[0];
1566 els = new Annotation[annotationLength];
1568 System.arraycopy(annotations, block[0], els, 0, copylength);
1569 w += annotationLength;
1574 annotations = new Annotation[w];
1577 for (Annotation[] chnk : annels)
1579 System.arraycopy(chnk, 0, annotations, w, chnk.length);
1585 public static Iterable<AlignmentAnnotation> findAnnotations(
1586 Iterable<AlignmentAnnotation> list, SequenceI seq, String calcId,
1589 List<AlignmentAnnotation> aa = new ArrayList<>();
1590 for (AlignmentAnnotation ann : list)
1592 if ((calcId == null || (ann.getCalcId() != null
1593 && ann.getCalcId().equals(calcId)))
1594 && (seq == null || (ann.sequenceRef != null
1595 && ann.sequenceRef == seq))
1597 || (ann.label != null && ann.label.equals(label))))
1606 * Answer true if any annotation matches the calcId passed in (if not null).
1609 * annotation to search
1613 public static boolean hasAnnotation(List<AlignmentAnnotation> list,
1617 if (calcId != null && !"".equals(calcId))
1619 for (AlignmentAnnotation a : list)
1621 if (a.getCalcId() == calcId)
1630 public static Iterable<AlignmentAnnotation> findAnnotation(
1631 List<AlignmentAnnotation> list, String calcId)
1633 List<AlignmentAnnotation> aa = new ArrayList<>();
1638 for (AlignmentAnnotation a : list)
1641 if (a.getCalcId() == calcId || (a.getCalcId() != null
1642 && calcId != null && a.getCalcId().equals(calcId)))