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.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
33 import java.util.Map.Entry;
41 public class AlignmentAnnotation
43 private static final String ANNOTATION_ID_PREFIX = "ann";
46 * Identifers for different types of profile data
48 public static final int SEQUENCE_PROFILE = 0;
50 public static final int STRUCTURE_PROFILE = 1;
52 public static final int CDNA_PROFILE = 2;
54 private static long counter = 0;
57 * If true, this annotations is calculated every edit, eg consensus, quality
58 * or conservation graphs
60 public boolean autoCalculated = false;
63 * unique ID for this annotation, used to match up the same annotation row
64 * shown in multiple views and alignments
66 public String annotationId;
69 * the sequence this annotation is associated with (or null)
71 public SequenceI sequenceRef;
73 /** label shown in dropdown menus and in the annotation label area */
76 /** longer description text shown as a tooltip */
77 public String description;
79 /** Array of annotations placed in the current coordinate system */
80 public Annotation[] annotations;
82 public List<SimpleBP> bps = null;
85 * RNA secondary structure contact positions
87 public SequenceFeature[] _rnasecstr = null;
90 * position of annotation resulting in invalid WUSS parsing or -1. -2 means
91 * there was no RNA structure in this annotation
93 private long invalidrnastruc = -2;
96 * Updates the _rnasecstr field Determines the positions that base pair and
97 * the positions of helices based on secondary structure from a Stockholm file
99 * @param rnaAnnotation
101 private void _updateRnaSecStr(CharSequence rnaAnnotation)
105 _rnasecstr = Rna.getHelixMap(rnaAnnotation);
106 invalidrnastruc = -1;
107 } catch (WUSSParseException px)
109 // DEBUG System.out.println(px);
110 invalidrnastruc = px.getProblemPos();
112 if (invalidrnastruc > -1)
117 if (_rnasecstr != null && _rnasecstr.length > 0)
119 // show all the RNA secondary structure annotation symbols.
121 showAllColLabels = true;
122 scaleColLabel = true;
125 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
129 private void _markRnaHelices()
132 // Figure out number of helices
133 // Length of rnasecstr is the number of pairs of positions that base pair
134 // with each other in the secondary structure
135 for (int x = 0; x < _rnasecstr.length; x++)
139 * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
140 * this.annotation._rnasecstr[x].getBegin());
142 // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
146 val = Integer.valueOf(_rnasecstr[x].getFeatureGroup());
151 } catch (NumberFormatException q)
156 annotations[_rnasecstr[x].getBegin()].value = val;
157 annotations[_rnasecstr[x].getEnd()].value = val;
159 // annotations[_rnasecstr[x].getBegin()].displayCharacter = "" + val;
160 // annotations[_rnasecstr[x].getEnd()].displayCharacter = "" + val;
166 * map of positions in the associated annotation
168 private Map<Integer, Annotation> sequenceMapping;
171 * lower range for quantitative data
173 public float graphMin;
176 * Upper range for quantitative data
178 public float graphMax;
181 * Score associated with label and description.
183 public double score = Double.NaN;
186 * flag indicating if annotation has a score.
188 public boolean hasScore = false;
190 public GraphLine threshold;
192 // Graphical hints and tips
194 /** Can this row be edited by the user ? */
195 public boolean editable = false;
197 /** Indicates if annotation has a graphical symbol track */
198 public boolean hasIcons; //
200 /** Indicates if annotation has a text character label */
201 public boolean hasText;
203 /** is the row visible */
204 public boolean visible = true;
206 public int graphGroup = -1;
208 /** Displayed height of row in pixels */
209 public int height = 0;
211 public int graph = 0;
213 public int graphHeight = 40;
215 public boolean padGaps = false;
217 public static final int NO_GRAPH = 0;
219 public static final int BAR_GRAPH = 1;
221 public static final int LINE_GRAPH = 2;
223 public boolean belowAlignment = true;
225 public SequenceGroup groupRef = null;
228 * display every column label, even if there is a row of identical labels
230 public boolean showAllColLabels = false;
233 * scale the column label to fit within the alignment column.
235 public boolean scaleColLabel = false;
238 * centre the column labels relative to the alignment column
240 public boolean centreColLabels = false;
242 private boolean isrna;
244 public static int getGraphValueFromString(String string)
246 if (string.equalsIgnoreCase("BAR_GRAPH"))
250 else if (string.equalsIgnoreCase("LINE_GRAPH"))
261 * Creates a new AlignmentAnnotation object.
264 * short label shown under sequence labels
266 * text displayed on mouseover
268 * set of positional annotation elements
270 public AlignmentAnnotation(String label, String description,
271 Annotation[] annotations)
277 this.description = description;
278 this.annotations = annotations;
280 validateRangeAndDisplay();
284 * Checks if annotation labels represent secondary structures
287 void areLabelsSecondaryStructure()
289 boolean nonSSLabel = false;
291 StringBuffer rnastring = new StringBuffer();
294 for (int i = 0; i < annotations.length; i++)
296 if (annotations[i] == null)
300 if (annotations[i].secondaryStructure == 'H'
301 || annotations[i].secondaryStructure == 'E')
306 // Check for RNA secondary structure
308 // System.out.println(annotations[i].secondaryStructure);
309 // TODO: 2.8.2 should this ss symbol validation check be a function in
310 // RNA/ResidueProperties ?
311 if (annotations[i].secondaryStructure == '('
312 || annotations[i].secondaryStructure == '['
313 || annotations[i].secondaryStructure == '<'
314 || annotations[i].secondaryStructure == '{'
315 || annotations[i].secondaryStructure == 'A'
316 || annotations[i].secondaryStructure == 'B'
317 || annotations[i].secondaryStructure == 'C'
318 || annotations[i].secondaryStructure == 'D'
319 || annotations[i].secondaryStructure == 'E'
320 || annotations[i].secondaryStructure == 'F'
321 || annotations[i].secondaryStructure == 'G'
322 || annotations[i].secondaryStructure == 'H'
323 || annotations[i].secondaryStructure == 'I'
324 || annotations[i].secondaryStructure == 'J'
325 || annotations[i].secondaryStructure == 'K'
326 || annotations[i].secondaryStructure == 'L'
327 || annotations[i].secondaryStructure == 'M'
328 || annotations[i].secondaryStructure == 'N'
329 || annotations[i].secondaryStructure == 'O'
330 || annotations[i].secondaryStructure == 'P'
331 || annotations[i].secondaryStructure == 'Q'
332 || annotations[i].secondaryStructure == 'R'
333 || annotations[i].secondaryStructure == 'S'
334 || annotations[i].secondaryStructure == 'T'
335 || annotations[i].secondaryStructure == 'U'
336 || annotations[i].secondaryStructure == 'V'
337 || annotations[i].secondaryStructure == 'W'
338 || annotations[i].secondaryStructure == 'X'
339 || annotations[i].secondaryStructure == 'Y'
340 || annotations[i].secondaryStructure == 'Z')
347 // System.out.println("displaychar " + annotations[i].displayCharacter);
349 if (annotations[i].displayCharacter == null
350 || annotations[i].displayCharacter.length() == 0)
352 rnastring.append('.');
355 if (annotations[i].displayCharacter.length() == 1)
357 firstChar = annotations[i].displayCharacter.charAt(0);
358 // check to see if it looks like a sequence or is secondary structure
360 if (annotations[i].secondaryStructure != ' ' && !hasIcons &&
361 // Uncomment to only catch case where
362 // displayCharacter==secondary
364 // to correctly redisplay SS annotation imported from Stockholm,
365 // exported to JalviewXML and read back in again.
367 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
368 firstChar != ' ' && firstChar != '$' && firstChar != 0xCE
369 && firstChar != '(' && firstChar != '[' && firstChar != '>'
370 && firstChar != '{' && firstChar != 'A' && firstChar != 'B'
371 && firstChar != 'C' && firstChar != 'D' && firstChar != 'E'
372 && firstChar != 'F' && firstChar != 'G' && firstChar != 'H'
373 && firstChar != 'I' && firstChar != 'J' && firstChar != 'K'
374 && firstChar != 'L' && firstChar != 'M' && firstChar != 'N'
375 && firstChar != 'O' && firstChar != 'P' && firstChar != 'Q'
376 && firstChar != 'R' && firstChar != 'S' && firstChar != 'T'
377 && firstChar != 'U' && firstChar != 'V' && firstChar != 'W'
378 && firstChar != 'X' && firstChar != 'Y' && firstChar != 'Z'
380 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
382 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
395 rnastring.append(annotations[i].displayCharacter.charAt(1));
398 if (annotations[i].displayCharacter.length() > 0)
407 for (int j = 0; j < annotations.length; j++)
409 if (annotations[j] != null
410 && annotations[j].secondaryStructure != ' ')
412 annotations[j].displayCharacter = String
413 .valueOf(annotations[j].secondaryStructure);
414 annotations[j].secondaryStructure = ' ';
423 _updateRnaSecStr(new AnnotCharSequence());
429 * flyweight access to positions in the alignment annotation row for RNA
435 private class AnnotCharSequence implements CharSequence
441 public AnnotCharSequence()
443 this(0, annotations.length);
446 AnnotCharSequence(int start, int end)
453 public CharSequence subSequence(int start, int end)
455 return new AnnotCharSequence(offset + start, offset + end);
465 public char charAt(int index)
467 return ((index + offset < 0) || (index + offset) >= max
468 || annotations[index + offset] == null
469 || (annotations[index + offset].secondaryStructure <= ' ')
471 : annotations[index + offset].displayCharacter == null
473 + offset].displayCharacter
476 + offset].secondaryStructure
478 + offset].displayCharacter
483 public String toString()
485 char[] string = new char[max - offset];
486 int mx = annotations.length;
488 for (int i = offset; i < mx; i++)
490 string[i] = (annotations[i] == null
491 || (annotations[i].secondaryStructure <= 32))
493 : (annotations[i].displayCharacter == null
494 || annotations[i].displayCharacter
496 ? annotations[i].secondaryStructure
497 : annotations[i].displayCharacter
500 return new String(string);
504 private long _lastrnaannot = -1;
506 public String getRNAStruc()
510 String rnastruc = new AnnotCharSequence().toString();
511 if (_lastrnaannot != rnastruc.hashCode())
513 // ensure rna structure contacts are up to date
514 _lastrnaannot = rnastruc.hashCode();
515 _updateRnaSecStr(rnastruc);
523 * Creates a new AlignmentAnnotation object.
538 public AlignmentAnnotation(String label, String description,
539 Annotation[] annotations, float min, float max, int graphType)
542 // graphs are not editable
543 editable = graphType == 0;
546 this.description = description;
547 this.annotations = annotations;
551 validateRangeAndDisplay();
555 * checks graphMin and graphMax, secondary structure symbols, sets graphType
556 * appropriately, sets null labels to the empty string if appropriate.
558 public void validateRangeAndDisplay()
561 if (annotations == null)
563 visible = false; // try to prevent renderer from displaying.
564 invalidrnastruc = -1;
565 return; // this is a non-annotation row annotation - ie a sequence score.
568 int graphType = graph;
569 float min = graphMin;
570 float max = graphMax;
571 boolean drawValues = true;
576 for (int i = 0; i < annotations.length; i++)
578 if (annotations[i] == null)
583 if (drawValues && annotations[i].displayCharacter != null
584 && annotations[i].displayCharacter.length() > 1)
589 if (annotations[i].value > max)
591 max = annotations[i].value;
594 if (annotations[i].value < min)
596 min = annotations[i].value;
598 if (_linecolour == null && annotations[i].colour != null)
600 _linecolour = annotations[i].colour;
603 // ensure zero is origin for min/max ranges on only one side of zero
620 areLabelsSecondaryStructure();
622 if (!drawValues && graphType != NO_GRAPH)
624 for (int i = 0; i < annotations.length; i++)
626 if (annotations[i] != null)
628 annotations[i].displayCharacter = "";
635 * Copy constructor creates a new independent annotation row with the same
636 * associated sequenceRef
640 public AlignmentAnnotation(AlignmentAnnotation annotation)
643 this.label = new String(annotation.label);
644 if (annotation.description != null)
646 this.description = new String(annotation.description);
648 this.graphMin = annotation.graphMin;
649 this.graphMax = annotation.graphMax;
650 this.graph = annotation.graph;
651 this.graphHeight = annotation.graphHeight;
652 this.graphGroup = annotation.graphGroup;
653 this.groupRef = annotation.groupRef;
654 this.editable = annotation.editable;
655 this.autoCalculated = annotation.autoCalculated;
656 this.hasIcons = annotation.hasIcons;
657 this.hasText = annotation.hasText;
658 this.height = annotation.height;
659 this.label = annotation.label;
660 this.padGaps = annotation.padGaps;
661 this.visible = annotation.visible;
662 this.centreColLabels = annotation.centreColLabels;
663 this.scaleColLabel = annotation.scaleColLabel;
664 this.showAllColLabels = annotation.showAllColLabels;
665 this.calcId = annotation.calcId;
666 if (annotation.properties != null)
668 properties = new HashMap<String, String>();
669 for (Map.Entry<String, String> val : annotation.properties.entrySet())
671 properties.put(val.getKey(), val.getValue());
674 if (this.hasScore = annotation.hasScore)
676 this.score = annotation.score;
678 if (annotation.threshold != null)
680 threshold = new GraphLine(annotation.threshold);
682 Annotation[] ann = annotation.annotations;
683 if (annotation.annotations != null)
685 this.annotations = new Annotation[ann.length];
686 for (int i = 0; i < ann.length; i++)
690 annotations[i] = new Annotation(ann[i]);
691 if (_linecolour != null)
693 _linecolour = annotations[i].colour;
698 if (annotation.sequenceRef != null)
700 this.sequenceRef = annotation.sequenceRef;
701 if (annotation.sequenceMapping != null)
704 sequenceMapping = new HashMap<Integer, Annotation>();
705 Iterator<Integer> pos = annotation.sequenceMapping.keySet()
707 while (pos.hasNext())
709 // could optimise this!
711 Annotation a = annotation.sequenceMapping.get(p);
718 for (int i = 0; i < ann.length; i++)
722 sequenceMapping.put(p, annotations[i]);
730 this.sequenceMapping = null;
733 // TODO: check if we need to do this: JAL-952
734 // if (this.isrna=annotation.isrna)
736 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
738 validateRangeAndDisplay(); // construct hashcodes, etc.
742 * clip the annotation to the columns given by startRes and endRes (inclusive)
743 * and prune any existing sequenceMapping to just those columns.
748 public void restrict(int startRes, int endRes)
750 if (annotations == null)
759 if (startRes >= annotations.length)
761 startRes = annotations.length - 1;
763 if (endRes >= annotations.length)
765 endRes = annotations.length - 1;
767 if (annotations == null)
771 Annotation[] temp = new Annotation[endRes - startRes + 1];
772 if (startRes < annotations.length)
774 System.arraycopy(annotations, startRes, temp, 0,
775 endRes - startRes + 1);
777 if (sequenceRef != null)
779 // Clip the mapping, if it exists.
780 int spos = sequenceRef.findPosition(startRes);
781 int epos = sequenceRef.findPosition(endRes);
782 if (sequenceMapping != null)
784 Map<Integer, Annotation> newmapping = new HashMap<Integer, Annotation>();
785 Iterator<Integer> e = sequenceMapping.keySet().iterator();
788 Integer pos = e.next();
789 if (pos.intValue() >= spos && pos.intValue() <= epos)
791 newmapping.put(pos, sequenceMapping.get(pos));
794 sequenceMapping.clear();
795 sequenceMapping = newmapping;
802 * set the annotation row to be at least length Annotations
805 * minimum number of columns required in the annotation row
806 * @return false if the annotation row is greater than length
808 public boolean padAnnotation(int length)
810 if (annotations == null)
812 return true; // annotation row is correct - null == not visible and
815 if (annotations.length < length)
817 Annotation[] na = new Annotation[length];
818 System.arraycopy(annotations, 0, na, 0, annotations.length);
822 return annotations.length > length;
829 * @return DOCUMENT ME!
832 public String toString()
834 if (annotations == null)
838 StringBuilder buffer = new StringBuilder(256);
840 for (int i = 0; i < annotations.length; i++)
842 if (annotations[i] != null)
846 buffer.append(annotations[i].value);
850 buffer.append(annotations[i].secondaryStructure);
854 buffer.append(annotations[i].displayCharacter);
860 // TODO: remove disgusting hack for 'special' treatment of consensus line.
861 if (label.indexOf("Consensus") == 0)
865 for (int i = 0; i < annotations.length; i++)
867 if (annotations[i] != null)
869 buffer.append(annotations[i].description);
876 return buffer.toString();
879 public void setThreshold(GraphLine line)
884 public GraphLine getThreshold()
890 * Attach the annotation to seqRef, starting from startRes position. If
891 * alreadyMapped is true then the indices of the annotation[] array are
892 * sequence positions rather than alignment column positions.
896 * @param alreadyMapped
898 public void createSequenceMapping(SequenceI seqRef, int startRes,
899 boolean alreadyMapped)
906 sequenceRef = seqRef;
907 if (annotations == null)
911 sequenceMapping = new HashMap<Integer, Annotation>();
915 for (int i = 0; i < annotations.length; i++)
917 if (annotations[i] != null)
921 seqPos = seqRef.findPosition(i);
925 seqPos = i + startRes;
928 sequenceMapping.put(new Integer(seqPos), annotations[i]);
935 * When positional annotation and a sequence reference is present, clears and
936 * resizes the annotations array to the current alignment width, and adds
937 * annotation according to aligned positions of the sequenceRef given by
940 public void adjustForAlignment()
942 if (sequenceRef == null)
947 if (annotations == null)
952 int a = 0, aSize = sequenceRef.getLength();
961 Annotation[] temp = new Annotation[aSize];
963 if (sequenceMapping != null)
965 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
967 index = new Integer(a);
968 Annotation annot = sequenceMapping.get(index);
971 position = sequenceRef.findIndex(a) - 1;
973 temp[position] = annot;
981 * remove any null entries in annotation row and return the number of non-null
982 * annotation elements.
986 public int compactAnnotationArray()
988 int i = 0, iSize = annotations.length;
991 if (annotations[i] == null)
995 System.arraycopy(annotations, i + 1, annotations, i,
1005 Annotation[] ann = annotations;
1006 annotations = new Annotation[i];
1007 System.arraycopy(ann, 0, annotations, 0, i);
1013 * Associate this annotation with the aligned residues of a particular
1014 * sequence. sequenceMapping will be updated in the following way: null
1015 * sequenceI - existing mapping will be discarded but annotations left in
1016 * mapped positions. valid sequenceI not equal to current sequenceRef: mapping
1017 * is discarded and rebuilt assuming 1:1 correspondence TODO: overload with
1018 * parameter to specify correspondence between current and new sequenceRef
1022 public void setSequenceRef(SequenceI sequenceI)
1024 if (sequenceI != null)
1026 if (sequenceRef != null)
1028 boolean rIsDs = sequenceRef.getDatasetSequence() == null,
1029 tIsDs = sequenceI.getDatasetSequence() == null;
1030 if (sequenceRef != sequenceI
1032 && sequenceRef != sequenceI.getDatasetSequence())
1034 && sequenceRef.getDatasetSequence() != sequenceI)
1035 && (!rIsDs && !tIsDs
1036 && sequenceRef.getDatasetSequence() != sequenceI
1037 .getDatasetSequence())
1038 && !sequenceRef.equals(sequenceI))
1040 // if sequenceRef isn't intersecting with sequenceI
1041 // throw away old mapping and reconstruct.
1043 if (sequenceMapping != null)
1045 sequenceMapping = null;
1046 // compactAnnotationArray();
1048 createSequenceMapping(sequenceI, 1, true);
1049 adjustForAlignment();
1053 // Mapping carried over
1054 sequenceRef = sequenceI;
1059 // No mapping exists
1060 createSequenceMapping(sequenceI, 1, true);
1061 adjustForAlignment();
1066 // throw away the mapping without compacting.
1067 sequenceMapping = null;
1075 public double getScore()
1084 public void setScore(double score)
1092 * @return true if annotation has an associated score
1094 public boolean hasScore()
1096 return hasScore || !Double.isNaN(score);
1100 * Score only annotation
1103 * @param description
1106 public AlignmentAnnotation(String label, String description, double score)
1108 this(label, description, null);
1113 * copy constructor with edit based on the hidden columns marked in colSel
1115 * @param alignmentAnnotation
1118 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1119 HiddenColumns hidden)
1121 this(alignmentAnnotation);
1122 if (annotations == null)
1126 hidden.makeVisibleAnnotation(this);
1129 public void setPadGaps(boolean padgaps, char gapchar)
1131 this.padGaps = padgaps;
1135 for (int i = 0; i < annotations.length; i++)
1137 if (annotations[i] == null)
1139 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1142 else if (annotations[i].displayCharacter == null
1143 || annotations[i].displayCharacter.equals(" "))
1145 annotations[i].displayCharacter = String.valueOf(gapchar);
1152 * format description string for display
1155 * @return Get the annotation description string optionally prefixed by
1156 * associated sequence name (if any)
1158 public String getDescription(boolean seqname)
1160 if (seqname && this.sequenceRef != null)
1162 int i = description.toLowerCase().indexOf("<html>");
1165 // move the html tag to before the sequence reference.
1166 return "<html>" + sequenceRef.getName() + " : "
1167 + description.substring(i + 6);
1169 return sequenceRef.getName() + " : " + description;
1174 public boolean isValidStruc()
1176 return invalidrnastruc == -1;
1179 public long getInvalidStrucPos()
1181 return invalidrnastruc;
1185 * machine readable ID string indicating what generated this annotation
1187 protected String calcId = "";
1190 * properties associated with the calcId
1192 protected Map<String, String> properties = new HashMap<String, String>();
1195 * base colour for line graphs. If null, will be set automatically by
1196 * searching the alignment annotation
1198 public java.awt.Color _linecolour;
1200 public String getCalcId()
1205 public void setCalcId(String calcId)
1207 this.calcId = calcId;
1210 public boolean isRNA()
1216 * transfer annotation to the given sequence using the given mapping from the
1217 * current positions or an existing sequence mapping
1221 * map involving sq as To or From
1223 public void liftOver(SequenceI sq, Mapping sp2sq)
1225 if (sp2sq.getMappedWidth() != sp2sq.getWidth())
1227 // TODO: employ getWord/MappedWord to transfer annotation between cDNA and
1228 // Protein reference frames
1230 "liftOver currently not implemented for transfer of annotation between different types of seqeunce");
1232 boolean mapIsTo = (sp2sq != null)
1233 ? (sp2sq.getTo() == sq
1234 || sp2sq.getTo() == sq.getDatasetSequence())
1237 // TODO build a better annotation element map and get rid of annotations[]
1238 Map<Integer, Annotation> mapForsq = new HashMap<Integer, Annotation>();
1239 if (sequenceMapping != null)
1243 for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
1245 Integer mpos = Integer
1246 .valueOf(mapIsTo ? sp2sq.getMappedPosition(ie.getKey())
1247 : sp2sq.getPosition(ie.getKey()));
1248 if (mpos >= sq.getStart() && mpos <= sq.getEnd())
1250 mapForsq.put(mpos, ie.getValue());
1253 sequenceMapping = mapForsq;
1255 adjustForAlignment();
1265 * like liftOver but more general.
1267 * Takes an array of int pairs that will be used to update the internal
1268 * sequenceMapping and so shuffle the annotated positions
1271 * - new sequence reference for the annotation row - if null,
1272 * sequenceRef is left unchanged
1274 * array of ints containing corresponding positions
1276 * - column for current coordinate system (-1 for index+1)
1278 * - column for destination coordinate system (-1 for index+1)
1280 * - offset added to index when referencing either coordinate system
1281 * @note no checks are made as to whether from and/or to are sensible
1282 * @note caller should add the remapped annotation to newref if they have not
1285 public void remap(SequenceI newref, HashMap<Integer, int[]> mapping,
1286 int from, int to, int idxoffset)
1288 if (mapping != null)
1290 Map<Integer, Annotation> old = sequenceMapping;
1291 Map<Integer, Annotation> remap = new HashMap<Integer, Annotation>();
1293 for (int mp[] : mapping.values())
1299 Annotation ann = null;
1302 ann = sequenceMapping.get(Integer.valueOf(idxoffset + index));
1306 if (mp != null && mp.length > from)
1308 ann = sequenceMapping.get(Integer.valueOf(mp[from]));
1315 remap.put(Integer.valueOf(idxoffset + index), ann);
1319 if (to > -1 && to < mp.length)
1321 remap.put(Integer.valueOf(mp[to]), ann);
1326 sequenceMapping = remap;
1330 sequenceRef = newref;
1332 adjustForAlignment();
1336 public String getProperty(String property)
1338 if (properties == null)
1342 return properties.get(property);
1345 public void setProperty(String property, String value)
1347 if (properties == null)
1349 properties = new HashMap<String, String>();
1351 properties.put(property, value);
1354 public boolean hasProperties()
1356 return properties != null && properties.size() > 0;
1359 public Collection<String> getProperties()
1361 if (properties == null)
1363 return Collections.emptyList();
1365 return properties.keySet();
1369 * Returns the Annotation for the given sequence position (base 1) if any,
1375 public Annotation getAnnotationForPosition(int position)
1377 return sequenceMapping == null ? null : sequenceMapping.get(position);
1382 * Set the id to "ann" followed by a counter that increments so as to be
1383 * unique for the lifetime of the JVM
1385 protected final void setAnnotationId()
1387 this.annotationId = ANNOTATION_ID_PREFIX + Long.toString(nextId());
1391 * Returns the match for the last unmatched opening RNA helix pair symbol
1392 * preceding the given column, or '(' if nothing found to match.
1397 public String getDefaultRnaHelixSymbol(int column)
1399 String result = "(";
1400 if (annotations == null)
1406 * for each preceding column, if it contains an open bracket,
1407 * count whether it is still unmatched at column, if so return its pair
1408 * (likely faster than the fancy alternative using stacks)
1410 for (int col = column - 1; col >= 0; col--)
1412 Annotation annotation = annotations[col];
1413 if (annotation == null)
1417 String displayed = annotation.displayCharacter;
1418 if (displayed == null || displayed.length() != 1)
1422 char symbol = displayed.charAt(0);
1423 if (!Rna.isOpeningParenthesis(symbol))
1429 * found an opening bracket symbol
1430 * count (closing-opening) symbols of this type that follow it,
1431 * up to and excluding the target column; if the count is less
1432 * than 1, the opening bracket is unmatched, so return its match
1434 String closer = String
1435 .valueOf(Rna.getMatchingClosingParenthesis(symbol));
1436 String opener = String.valueOf(symbol);
1438 for (int j = col + 1; j < column; j++)
1440 if (annotations[j] != null)
1442 String s = annotations[j].displayCharacter;
1443 if (closer.equals(s))
1447 else if (opener.equals(s))
1461 protected static synchronized long nextId()
1468 * @return true for rows that have a range of values in their annotation set
1470 public boolean isQuantitative()
1472 return graphMin < graphMax;