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;
247 * @see java.lang.Object#finalize()
250 protected void finalize() throws Throwable
257 public static int getGraphValueFromString(String string)
259 if (string.equalsIgnoreCase("BAR_GRAPH"))
263 else if (string.equalsIgnoreCase("LINE_GRAPH"))
274 * Creates a new AlignmentAnnotation object.
277 * short label shown under sequence labels
279 * text displayed on mouseover
281 * set of positional annotation elements
283 public AlignmentAnnotation(String label, String description,
284 Annotation[] annotations)
290 this.description = description;
291 this.annotations = annotations;
293 validateRangeAndDisplay();
297 * Checks if annotation labels represent secondary structures
300 void areLabelsSecondaryStructure()
302 boolean nonSSLabel = false;
304 StringBuffer rnastring = new StringBuffer();
307 for (int i = 0; i < annotations.length; i++)
309 if (annotations[i] == null)
313 if (annotations[i].secondaryStructure == 'H'
314 || annotations[i].secondaryStructure == 'E')
319 // Check for RNA secondary structure
321 // System.out.println(annotations[i].secondaryStructure);
322 // TODO: 2.8.2 should this ss symbol validation check be a function in
323 // RNA/ResidueProperties ?
324 if (annotations[i].secondaryStructure == '('
325 || annotations[i].secondaryStructure == '['
326 || annotations[i].secondaryStructure == '<'
327 || annotations[i].secondaryStructure == '{'
328 || annotations[i].secondaryStructure == 'A'
329 || annotations[i].secondaryStructure == 'B'
330 || annotations[i].secondaryStructure == 'C'
331 || annotations[i].secondaryStructure == 'D'
332 || annotations[i].secondaryStructure == 'E'
333 || annotations[i].secondaryStructure == 'F'
334 || annotations[i].secondaryStructure == 'G'
335 || annotations[i].secondaryStructure == 'H'
336 || annotations[i].secondaryStructure == 'I'
337 || annotations[i].secondaryStructure == 'J'
338 || annotations[i].secondaryStructure == 'K'
339 || annotations[i].secondaryStructure == 'L'
340 || annotations[i].secondaryStructure == 'M'
341 || annotations[i].secondaryStructure == 'N'
342 || annotations[i].secondaryStructure == 'O'
343 || annotations[i].secondaryStructure == 'P'
344 || annotations[i].secondaryStructure == 'Q'
345 || annotations[i].secondaryStructure == 'R'
346 || annotations[i].secondaryStructure == 'S'
347 || annotations[i].secondaryStructure == 'T'
348 || annotations[i].secondaryStructure == 'U'
349 || annotations[i].secondaryStructure == 'V'
350 || annotations[i].secondaryStructure == 'W'
351 || annotations[i].secondaryStructure == 'X'
352 || annotations[i].secondaryStructure == 'Y'
353 || annotations[i].secondaryStructure == 'Z')
360 // System.out.println("displaychar " + annotations[i].displayCharacter);
362 if (annotations[i].displayCharacter == null
363 || annotations[i].displayCharacter.length() == 0)
365 rnastring.append('.');
368 if (annotations[i].displayCharacter.length() == 1)
370 firstChar = annotations[i].displayCharacter.charAt(0);
371 // check to see if it looks like a sequence or is secondary structure
373 if (annotations[i].secondaryStructure != ' ' && !hasIcons &&
374 // Uncomment to only catch case where
375 // displayCharacter==secondary
377 // to correctly redisplay SS annotation imported from Stockholm,
378 // exported to JalviewXML and read back in again.
380 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
381 firstChar != ' ' && firstChar != '$' && firstChar != 0xCE
382 && firstChar != '(' && firstChar != '[' && firstChar != '>'
383 && firstChar != '{' && firstChar != 'A' && firstChar != 'B'
384 && firstChar != 'C' && firstChar != 'D' && firstChar != 'E'
385 && firstChar != 'F' && firstChar != 'G' && firstChar != 'H'
386 && firstChar != 'I' && firstChar != 'J' && firstChar != 'K'
387 && firstChar != 'L' && firstChar != 'M' && firstChar != 'N'
388 && firstChar != 'O' && firstChar != 'P' && firstChar != 'Q'
389 && firstChar != 'R' && firstChar != 'S' && firstChar != 'T'
390 && firstChar != 'U' && firstChar != 'V' && firstChar != 'W'
391 && firstChar != 'X' && firstChar != 'Y' && firstChar != 'Z'
393 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
395 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
408 rnastring.append(annotations[i].displayCharacter.charAt(1));
411 if (annotations[i].displayCharacter.length() > 0)
420 for (int j = 0; j < annotations.length; j++)
422 if (annotations[j] != null
423 && annotations[j].secondaryStructure != ' ')
425 annotations[j].displayCharacter = String
426 .valueOf(annotations[j].secondaryStructure);
427 annotations[j].secondaryStructure = ' ';
436 _updateRnaSecStr(new AnnotCharSequence());
442 * flyweight access to positions in the alignment annotation row for RNA
448 private class AnnotCharSequence implements CharSequence
454 public AnnotCharSequence()
456 this(0, annotations.length);
459 AnnotCharSequence(int start, int end)
466 public CharSequence subSequence(int start, int end)
468 return new AnnotCharSequence(offset + start, offset + end);
478 public char charAt(int index)
480 return ((index + offset < 0) || (index + offset) >= max
481 || annotations[index + offset] == null
482 || (annotations[index + offset].secondaryStructure <= ' ')
484 : annotations[index + offset].displayCharacter == null
486 + offset].displayCharacter
489 + offset].secondaryStructure
491 + offset].displayCharacter
496 public String toString()
498 char[] string = new char[max - offset];
499 int mx = annotations.length;
501 for (int i = offset; i < mx; i++)
503 string[i] = (annotations[i] == null
504 || (annotations[i].secondaryStructure <= 32))
506 : (annotations[i].displayCharacter == null
507 || annotations[i].displayCharacter
509 ? annotations[i].secondaryStructure
510 : annotations[i].displayCharacter
513 return new String(string);
517 private long _lastrnaannot = -1;
519 public String getRNAStruc()
523 String rnastruc = new AnnotCharSequence().toString();
524 if (_lastrnaannot != rnastruc.hashCode())
526 // ensure rna structure contacts are up to date
527 _lastrnaannot = rnastruc.hashCode();
528 _updateRnaSecStr(rnastruc);
536 * Creates a new AlignmentAnnotation object.
551 public AlignmentAnnotation(String label, String description,
552 Annotation[] annotations, float min, float max, int graphType)
555 // graphs are not editable
556 editable = graphType == 0;
559 this.description = description;
560 this.annotations = annotations;
564 validateRangeAndDisplay();
568 * checks graphMin and graphMax, secondary structure symbols, sets graphType
569 * appropriately, sets null labels to the empty string if appropriate.
571 public void validateRangeAndDisplay()
574 if (annotations == null)
576 visible = false; // try to prevent renderer from displaying.
577 invalidrnastruc = -1;
578 return; // this is a non-annotation row annotation - ie a sequence score.
581 int graphType = graph;
582 float min = graphMin;
583 float max = graphMax;
584 boolean drawValues = true;
589 for (int i = 0; i < annotations.length; i++)
591 if (annotations[i] == null)
596 if (drawValues && annotations[i].displayCharacter != null
597 && annotations[i].displayCharacter.length() > 1)
602 if (annotations[i].value > max)
604 max = annotations[i].value;
607 if (annotations[i].value < min)
609 min = annotations[i].value;
611 if (_linecolour == null && annotations[i].colour != null)
613 _linecolour = annotations[i].colour;
616 // ensure zero is origin for min/max ranges on only one side of zero
633 areLabelsSecondaryStructure();
635 if (!drawValues && graphType != NO_GRAPH)
637 for (int i = 0; i < annotations.length; i++)
639 if (annotations[i] != null)
641 annotations[i].displayCharacter = "";
648 * Copy constructor creates a new independent annotation row with the same
649 * associated sequenceRef
653 public AlignmentAnnotation(AlignmentAnnotation annotation)
656 this.label = new String(annotation.label);
657 if (annotation.description != null)
659 this.description = new String(annotation.description);
661 this.graphMin = annotation.graphMin;
662 this.graphMax = annotation.graphMax;
663 this.graph = annotation.graph;
664 this.graphHeight = annotation.graphHeight;
665 this.graphGroup = annotation.graphGroup;
666 this.groupRef = annotation.groupRef;
667 this.editable = annotation.editable;
668 this.autoCalculated = annotation.autoCalculated;
669 this.hasIcons = annotation.hasIcons;
670 this.hasText = annotation.hasText;
671 this.height = annotation.height;
672 this.label = annotation.label;
673 this.padGaps = annotation.padGaps;
674 this.visible = annotation.visible;
675 this.centreColLabels = annotation.centreColLabels;
676 this.scaleColLabel = annotation.scaleColLabel;
677 this.showAllColLabels = annotation.showAllColLabels;
678 this.calcId = annotation.calcId;
679 if (annotation.properties != null)
681 properties = new HashMap<String, String>();
682 for (Map.Entry<String, String> val : annotation.properties.entrySet())
684 properties.put(val.getKey(), val.getValue());
687 if (this.hasScore = annotation.hasScore)
689 this.score = annotation.score;
691 if (annotation.threshold != null)
693 threshold = new GraphLine(annotation.threshold);
695 Annotation[] ann = annotation.annotations;
696 if (annotation.annotations != null)
698 this.annotations = new Annotation[ann.length];
699 for (int i = 0; i < ann.length; i++)
703 annotations[i] = new Annotation(ann[i]);
704 if (_linecolour != null)
706 _linecolour = annotations[i].colour;
711 if (annotation.sequenceRef != null)
713 this.sequenceRef = annotation.sequenceRef;
714 if (annotation.sequenceMapping != null)
717 sequenceMapping = new HashMap<Integer, Annotation>();
718 Iterator<Integer> pos = annotation.sequenceMapping.keySet()
720 while (pos.hasNext())
722 // could optimise this!
724 Annotation a = annotation.sequenceMapping.get(p);
731 for (int i = 0; i < ann.length; i++)
735 sequenceMapping.put(p, annotations[i]);
743 this.sequenceMapping = null;
746 // TODO: check if we need to do this: JAL-952
747 // if (this.isrna=annotation.isrna)
749 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
751 validateRangeAndDisplay(); // construct hashcodes, etc.
755 * clip the annotation to the columns given by startRes and endRes (inclusive)
756 * and prune any existing sequenceMapping to just those columns.
761 public void restrict(int startRes, int endRes)
763 if (annotations == null)
772 if (startRes >= annotations.length)
774 startRes = annotations.length - 1;
776 if (endRes >= annotations.length)
778 endRes = annotations.length - 1;
780 if (annotations == null)
784 Annotation[] temp = new Annotation[endRes - startRes + 1];
785 if (startRes < annotations.length)
787 System.arraycopy(annotations, startRes, temp, 0,
788 endRes - startRes + 1);
790 if (sequenceRef != null)
792 // Clip the mapping, if it exists.
793 int spos = sequenceRef.findPosition(startRes);
794 int epos = sequenceRef.findPosition(endRes);
795 if (sequenceMapping != null)
797 Map<Integer, Annotation> newmapping = new HashMap<Integer, Annotation>();
798 Iterator<Integer> e = sequenceMapping.keySet().iterator();
801 Integer pos = e.next();
802 if (pos.intValue() >= spos && pos.intValue() <= epos)
804 newmapping.put(pos, sequenceMapping.get(pos));
807 sequenceMapping.clear();
808 sequenceMapping = newmapping;
815 * set the annotation row to be at least length Annotations
818 * minimum number of columns required in the annotation row
819 * @return false if the annotation row is greater than length
821 public boolean padAnnotation(int length)
823 if (annotations == null)
825 return true; // annotation row is correct - null == not visible and
828 if (annotations.length < length)
830 Annotation[] na = new Annotation[length];
831 System.arraycopy(annotations, 0, na, 0, annotations.length);
835 return annotations.length > length;
842 * @return DOCUMENT ME!
845 public String toString()
847 if (annotations == null)
851 StringBuilder buffer = new StringBuilder(256);
853 for (int i = 0; i < annotations.length; i++)
855 if (annotations[i] != null)
859 buffer.append(annotations[i].value);
863 buffer.append(annotations[i].secondaryStructure);
867 buffer.append(annotations[i].displayCharacter);
873 // TODO: remove disgusting hack for 'special' treatment of consensus line.
874 if (label.indexOf("Consensus") == 0)
878 for (int i = 0; i < annotations.length; i++)
880 if (annotations[i] != null)
882 buffer.append(annotations[i].description);
889 return buffer.toString();
892 public void setThreshold(GraphLine line)
897 public GraphLine getThreshold()
903 * Attach the annotation to seqRef, starting from startRes position. If
904 * alreadyMapped is true then the indices of the annotation[] array are
905 * sequence positions rather than alignment column positions.
909 * @param alreadyMapped
910 * - annotation are at aligned columns
912 public void createSequenceMapping(SequenceI seqRef, int startRes,
913 boolean alreadyMapped)
920 sequenceRef = seqRef;
921 if (annotations == null)
925 sequenceMapping = new HashMap<Integer, Annotation>();
929 for (int i = 0; i < annotations.length; i++)
931 if (annotations[i] != null)
935 seqPos = seqRef.findPosition(i);
939 seqPos = i + startRes;
942 sequenceMapping.put(new Integer(seqPos), annotations[i]);
949 * When positional annotation and a sequence reference is present, clears and
950 * resizes the annotations array to the current alignment width, and adds
951 * annotation according to aligned positions of the sequenceRef given by
954 public void adjustForAlignment()
956 if (sequenceRef == null)
961 if (annotations == null)
966 int a = 0, aSize = sequenceRef.getLength();
975 Annotation[] temp = new Annotation[aSize];
977 if (sequenceMapping != null)
979 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
981 index = new Integer(a);
982 Annotation annot = sequenceMapping.get(index);
985 position = sequenceRef.findIndex(a) - 1;
987 temp[position] = annot;
995 * remove any null entries in annotation row and return the number of non-null
996 * annotation elements.
1000 public int compactAnnotationArray()
1002 int i = 0, iSize = annotations.length;
1005 if (annotations[i] == null)
1009 System.arraycopy(annotations, i + 1, annotations, i,
1019 Annotation[] ann = annotations;
1020 annotations = new Annotation[i];
1021 System.arraycopy(ann, 0, annotations, 0, i);
1027 * Associate this annotation with the aligned residues of a particular
1028 * sequence. sequenceMapping will be updated in the following way: null
1029 * sequenceI - existing mapping will be discarded but annotations left in
1030 * mapped positions. valid sequenceI not equal to current sequenceRef: mapping
1031 * is discarded and rebuilt assuming 1:1 correspondence TODO: overload with
1032 * parameter to specify correspondence between current and new sequenceRef
1036 public void setSequenceRef(SequenceI sequenceI)
1038 if (sequenceI != null)
1040 if (sequenceRef != null)
1042 boolean rIsDs = sequenceRef.getDatasetSequence() == null,
1043 tIsDs = sequenceI.getDatasetSequence() == null;
1044 if (sequenceRef != sequenceI
1046 && sequenceRef != sequenceI.getDatasetSequence())
1048 && sequenceRef.getDatasetSequence() != sequenceI)
1049 && (!rIsDs && !tIsDs
1050 && sequenceRef.getDatasetSequence() != sequenceI
1051 .getDatasetSequence())
1052 && !sequenceRef.equals(sequenceI))
1054 // if sequenceRef isn't intersecting with sequenceI
1055 // throw away old mapping and reconstruct.
1057 if (sequenceMapping != null)
1059 sequenceMapping = null;
1060 // compactAnnotationArray();
1062 createSequenceMapping(sequenceI, 1, true);
1063 adjustForAlignment();
1067 // Mapping carried over
1068 sequenceRef = sequenceI;
1073 // No mapping exists
1074 createSequenceMapping(sequenceI, 1, true);
1075 adjustForAlignment();
1080 // throw away the mapping without compacting.
1081 sequenceMapping = null;
1089 public double getScore()
1098 public void setScore(double score)
1106 * @return true if annotation has an associated score
1108 public boolean hasScore()
1110 return hasScore || !Double.isNaN(score);
1114 * Score only annotation
1117 * @param description
1120 public AlignmentAnnotation(String label, String description, double score)
1122 this(label, description, null);
1127 * copy constructor with edit based on the hidden columns marked in colSel
1129 * @param alignmentAnnotation
1132 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1133 HiddenColumns hidden)
1135 this(alignmentAnnotation);
1136 if (annotations == null)
1140 hidden.makeVisibleAnnotation(this);
1143 public void setPadGaps(boolean padgaps, char gapchar)
1145 this.padGaps = padgaps;
1149 for (int i = 0; i < annotations.length; i++)
1151 if (annotations[i] == null)
1153 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1156 else if (annotations[i].displayCharacter == null
1157 || annotations[i].displayCharacter.equals(" "))
1159 annotations[i].displayCharacter = String.valueOf(gapchar);
1166 * format description string for display
1169 * @return Get the annotation description string optionally prefixed by
1170 * associated sequence name (if any)
1172 public String getDescription(boolean seqname)
1174 if (seqname && this.sequenceRef != null)
1176 int i = description.toLowerCase().indexOf("<html>");
1179 // move the html tag to before the sequence reference.
1180 return "<html>" + sequenceRef.getName() + " : "
1181 + description.substring(i + 6);
1183 return sequenceRef.getName() + " : " + description;
1188 public boolean isValidStruc()
1190 return invalidrnastruc == -1;
1193 public long getInvalidStrucPos()
1195 return invalidrnastruc;
1199 * machine readable ID string indicating what generated this annotation
1201 protected String calcId = "";
1204 * properties associated with the calcId
1206 protected Map<String, String> properties = new HashMap<String, String>();
1209 * base colour for line graphs. If null, will be set automatically by
1210 * searching the alignment annotation
1212 public java.awt.Color _linecolour;
1214 public String getCalcId()
1219 public void setCalcId(String calcId)
1221 this.calcId = calcId;
1224 public boolean isRNA()
1230 * transfer annotation to the given sequence using the given mapping from the
1231 * current positions or an existing sequence mapping
1235 * map involving sq as To or From
1237 public void liftOver(SequenceI sq, Mapping sp2sq)
1239 if (sp2sq.getMappedWidth() != sp2sq.getWidth())
1241 // TODO: employ getWord/MappedWord to transfer annotation between cDNA and
1242 // Protein reference frames
1244 "liftOver currently not implemented for transfer of annotation between different types of seqeunce");
1246 boolean mapIsTo = (sp2sq != null)
1247 ? (sp2sq.getTo() == sq
1248 || sp2sq.getTo() == sq.getDatasetSequence())
1251 // TODO build a better annotation element map and get rid of annotations[]
1252 Map<Integer, Annotation> mapForsq = new HashMap<Integer, Annotation>();
1253 if (sequenceMapping != null)
1257 for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
1259 Integer mpos = Integer
1260 .valueOf(mapIsTo ? sp2sq.getMappedPosition(ie.getKey())
1261 : sp2sq.getPosition(ie.getKey()));
1262 if (mpos >= sq.getStart() && mpos <= sq.getEnd())
1264 mapForsq.put(mpos, ie.getValue());
1267 sequenceMapping = mapForsq;
1269 adjustForAlignment();
1279 * like liftOver but more general.
1281 * Takes an array of int pairs that will be used to update the internal
1282 * sequenceMapping and so shuffle the annotated positions
1285 * - new sequence reference for the annotation row - if null,
1286 * sequenceRef is left unchanged
1288 * array of ints containing corresponding positions
1290 * - column for current coordinate system (-1 for index+1)
1292 * - column for destination coordinate system (-1 for index+1)
1294 * - offset added to index when referencing either coordinate system
1295 * @note no checks are made as to whether from and/or to are sensible
1296 * @note caller should add the remapped annotation to newref if they have not
1299 public void remap(SequenceI newref, HashMap<Integer, int[]> mapping,
1300 int from, int to, int idxoffset)
1302 if (mapping != null)
1304 Map<Integer, Annotation> old = sequenceMapping;
1305 Map<Integer, Annotation> remap = new HashMap<Integer, Annotation>();
1307 for (int mp[] : mapping.values())
1313 Annotation ann = null;
1316 ann = sequenceMapping.get(Integer.valueOf(idxoffset + index));
1320 if (mp != null && mp.length > from)
1322 ann = sequenceMapping.get(Integer.valueOf(mp[from]));
1329 remap.put(Integer.valueOf(idxoffset + index), ann);
1333 if (to > -1 && to < mp.length)
1335 remap.put(Integer.valueOf(mp[to]), ann);
1340 sequenceMapping = remap;
1344 sequenceRef = newref;
1346 adjustForAlignment();
1350 public String getProperty(String property)
1352 if (properties == null)
1356 return properties.get(property);
1359 public void setProperty(String property, String value)
1361 if (properties == null)
1363 properties = new HashMap<String, String>();
1365 properties.put(property, value);
1368 public boolean hasProperties()
1370 return properties != null && properties.size() > 0;
1373 public Collection<String> getProperties()
1375 if (properties == null)
1377 return Collections.emptyList();
1379 return properties.keySet();
1383 * Returns the Annotation for the given sequence position (base 1) if any,
1389 public Annotation getAnnotationForPosition(int position)
1391 return sequenceMapping == null ? null : sequenceMapping.get(position);
1396 * Set the id to "ann" followed by a counter that increments so as to be
1397 * unique for the lifetime of the JVM
1399 protected final void setAnnotationId()
1401 this.annotationId = ANNOTATION_ID_PREFIX + Long.toString(nextId());
1405 * Returns the match for the last unmatched opening RNA helix pair symbol
1406 * preceding the given column, or '(' if nothing found to match.
1411 public String getDefaultRnaHelixSymbol(int column)
1413 String result = "(";
1414 if (annotations == null)
1420 * for each preceding column, if it contains an open bracket,
1421 * count whether it is still unmatched at column, if so return its pair
1422 * (likely faster than the fancy alternative using stacks)
1424 for (int col = column - 1; col >= 0; col--)
1426 Annotation annotation = annotations[col];
1427 if (annotation == null)
1431 String displayed = annotation.displayCharacter;
1432 if (displayed == null || displayed.length() != 1)
1436 char symbol = displayed.charAt(0);
1437 if (!Rna.isOpeningParenthesis(symbol))
1443 * found an opening bracket symbol
1444 * count (closing-opening) symbols of this type that follow it,
1445 * up to and excluding the target column; if the count is less
1446 * than 1, the opening bracket is unmatched, so return its match
1448 String closer = String
1449 .valueOf(Rna.getMatchingClosingParenthesis(symbol));
1450 String opener = String.valueOf(symbol);
1452 for (int j = col + 1; j < column; j++)
1454 if (annotations[j] != null)
1456 String s = annotations[j].displayCharacter;
1457 if (closer.equals(s))
1461 else if (opener.equals(s))
1475 protected static synchronized long nextId()
1482 * @return true for rows that have a range of values in their annotation set
1484 public boolean isQuantitative()
1486 return graphMin < graphMax;