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 if (annotations[i] == null)
301 if (annotations[i].secondaryStructure == 'H'
302 || annotations[i].secondaryStructure == 'E')
307 // Check for RNA secondary structure
309 // System.out.println(annotations[i].secondaryStructure);
310 // TODO: 2.8.2 should this ss symbol validation check be a function in
311 // RNA/ResidueProperties ?
312 if (annotations[i].secondaryStructure == '('
313 || annotations[i].secondaryStructure == '['
314 || annotations[i].secondaryStructure == '<'
315 || annotations[i].secondaryStructure == '{'
316 || annotations[i].secondaryStructure == 'A'
317 || annotations[i].secondaryStructure == 'B'
318 || annotations[i].secondaryStructure == 'C'
319 || annotations[i].secondaryStructure == 'D'
320 || annotations[i].secondaryStructure == 'E'
321 || annotations[i].secondaryStructure == 'F'
322 || annotations[i].secondaryStructure == 'G'
323 || annotations[i].secondaryStructure == 'H'
324 || annotations[i].secondaryStructure == 'I'
325 || annotations[i].secondaryStructure == 'J'
326 || annotations[i].secondaryStructure == 'K'
327 || annotations[i].secondaryStructure == 'L'
328 || annotations[i].secondaryStructure == 'M'
329 || annotations[i].secondaryStructure == 'N'
330 || annotations[i].secondaryStructure == 'O'
331 || annotations[i].secondaryStructure == 'P'
332 || annotations[i].secondaryStructure == 'Q'
333 || annotations[i].secondaryStructure == 'R'
334 || annotations[i].secondaryStructure == 'S'
335 || annotations[i].secondaryStructure == 'T'
336 || annotations[i].secondaryStructure == 'U'
337 || annotations[i].secondaryStructure == 'V'
338 || annotations[i].secondaryStructure == 'W'
339 || annotations[i].secondaryStructure == 'X'
340 || annotations[i].secondaryStructure == 'Y'
341 || annotations[i].secondaryStructure == 'Z')
348 // System.out.println("displaychar " + annotations[i].displayCharacter);
350 if (annotations[i].displayCharacter == null
351 || annotations[i].displayCharacter.length() == 0)
353 rnastring.append('.');
356 if (annotations[i].displayCharacter.length() == 1)
358 firstChar = annotations[i].displayCharacter.charAt(0);
359 // check to see if it looks like a sequence or is secondary structure
361 if (annotations[i].secondaryStructure != ' ' && !hasIcons &&
362 // Uncomment to only catch case where
363 // displayCharacter==secondary
365 // to correctly redisplay SS annotation imported from Stockholm,
366 // exported to JalviewXML and read back in again.
368 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
369 firstChar != ' ' && firstChar != '$' && firstChar != 0xCE
370 && firstChar != '(' && firstChar != '[' && firstChar != '>'
371 && firstChar != '{' && firstChar != 'A' && firstChar != 'B'
372 && firstChar != 'C' && firstChar != 'D' && firstChar != 'E'
373 && firstChar != 'F' && firstChar != 'G' && firstChar != 'H'
374 && firstChar != 'I' && firstChar != 'J' && firstChar != 'K'
375 && firstChar != 'L' && firstChar != 'M' && firstChar != 'N'
376 && firstChar != 'O' && firstChar != 'P' && firstChar != 'Q'
377 && firstChar != 'R' && firstChar != 'S' && firstChar != 'T'
378 && firstChar != 'U' && firstChar != 'V' && firstChar != 'W'
379 && firstChar != 'X' && firstChar != 'Y' && firstChar != 'Z'
381 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
383 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
396 rnastring.append(annotations[i].displayCharacter.charAt(1));
399 if (annotations[i].displayCharacter.length() > 0)
408 for (int j = 0; j < annotations.length; j++)
410 if (annotations[j] != null
411 && annotations[j].secondaryStructure != ' ')
413 annotations[j].displayCharacter = String
414 .valueOf(annotations[j].secondaryStructure);
415 annotations[j].secondaryStructure = ' ';
424 _updateRnaSecStr(new AnnotCharSequence());
430 * flyweight access to positions in the alignment annotation row for RNA
436 private class AnnotCharSequence implements CharSequence
442 public AnnotCharSequence()
444 this(0, annotations.length);
447 AnnotCharSequence(int start, int end)
454 public CharSequence subSequence(int start, int end)
456 return new AnnotCharSequence(offset + start, offset + end);
466 public char charAt(int index)
468 return ((index + offset < 0) || (index + offset) >= max
469 || annotations[index + offset] == null
470 || (annotations[index + offset].secondaryStructure <= ' ')
472 : annotations[index + offset].displayCharacter == null
474 + offset].displayCharacter
477 + offset].secondaryStructure
479 + offset].displayCharacter
484 public String toString()
486 char[] string = new char[max - offset];
487 int mx = annotations.length;
489 for (int i = offset; i < mx; i++)
491 string[i] = (annotations[i] == null
492 || (annotations[i].secondaryStructure <= 32))
494 : (annotations[i].displayCharacter == null
495 || annotations[i].displayCharacter
497 ? annotations[i].secondaryStructure
498 : annotations[i].displayCharacter
501 return new String(string);
505 private long _lastrnaannot = -1;
507 public String getRNAStruc()
511 String rnastruc = new AnnotCharSequence().toString();
512 if (_lastrnaannot != rnastruc.hashCode())
514 // ensure rna structure contacts are up to date
515 _lastrnaannot = rnastruc.hashCode();
516 _updateRnaSecStr(rnastruc);
524 * Creates a new AlignmentAnnotation object.
539 public AlignmentAnnotation(String label, String description,
540 Annotation[] annotations, float min, float max, int graphType)
543 // graphs are not editable
544 editable = graphType == 0;
547 this.description = description;
548 this.annotations = annotations;
552 validateRangeAndDisplay();
556 * checks graphMin and graphMax, secondary structure symbols, sets graphType
557 * appropriately, sets null labels to the empty string if appropriate.
559 public void validateRangeAndDisplay()
562 if (annotations == null)
564 visible = false; // try to prevent renderer from displaying.
565 invalidrnastruc = -1;
566 return; // this is a non-annotation row annotation - ie a sequence score.
569 int graphType = graph;
570 float min = graphMin;
571 float max = graphMax;
572 boolean drawValues = true;
577 for (int i = 0; i < annotations.length; i++)
579 if (annotations[i] == null)
584 if (drawValues && annotations[i].displayCharacter != null
585 && annotations[i].displayCharacter.length() > 1)
590 if (annotations[i].value > max)
592 max = annotations[i].value;
595 if (annotations[i].value < min)
597 min = annotations[i].value;
599 if (_linecolour == null && annotations[i].colour != null)
601 _linecolour = annotations[i].colour;
604 // ensure zero is origin for min/max ranges on only one side of zero
621 areLabelsSecondaryStructure();
623 if (!drawValues && graphType != NO_GRAPH)
625 for (int i = 0; i < annotations.length; i++)
627 if (annotations[i] != null)
629 annotations[i].displayCharacter = "";
636 * Copy constructor creates a new independent annotation row with the same
637 * associated sequenceRef
641 public AlignmentAnnotation(AlignmentAnnotation annotation)
644 this.label = new String(annotation.label);
645 if (annotation.description != null)
647 this.description = new String(annotation.description);
649 this.graphMin = annotation.graphMin;
650 this.graphMax = annotation.graphMax;
651 this.graph = annotation.graph;
652 this.graphHeight = annotation.graphHeight;
653 this.graphGroup = annotation.graphGroup;
654 this.groupRef = annotation.groupRef;
655 this.editable = annotation.editable;
656 this.autoCalculated = annotation.autoCalculated;
657 this.hasIcons = annotation.hasIcons;
658 this.hasText = annotation.hasText;
659 this.height = annotation.height;
660 this.label = annotation.label;
661 this.padGaps = annotation.padGaps;
662 this.visible = annotation.visible;
663 this.centreColLabels = annotation.centreColLabels;
664 this.scaleColLabel = annotation.scaleColLabel;
665 this.showAllColLabels = annotation.showAllColLabels;
666 this.calcId = annotation.calcId;
667 if (annotation.properties != null)
669 properties = new HashMap<>();
670 for (Map.Entry<String, String> val : annotation.properties.entrySet())
672 properties.put(val.getKey(), val.getValue());
675 if (this.hasScore = annotation.hasScore)
677 this.score = annotation.score;
679 if (annotation.threshold != null)
681 threshold = new GraphLine(annotation.threshold);
683 Annotation[] ann = annotation.annotations;
684 if (annotation.annotations != null)
686 this.annotations = new Annotation[ann.length];
687 for (int i = 0; i < ann.length; i++)
691 annotations[i] = new Annotation(ann[i]);
692 if (_linecolour != null)
694 _linecolour = annotations[i].colour;
699 if (annotation.sequenceRef != null)
701 this.sequenceRef = annotation.sequenceRef;
702 if (annotation.sequenceMapping != null)
705 sequenceMapping = new HashMap<>();
706 Iterator<Integer> pos = annotation.sequenceMapping.keySet()
708 while (pos.hasNext())
710 // could optimise this!
712 Annotation a = annotation.sequenceMapping.get(p);
719 for (int i = 0; i < ann.length; i++)
723 sequenceMapping.put(p, annotations[i]);
731 this.sequenceMapping = null;
734 // TODO: check if we need to do this: JAL-952
735 // if (this.isrna=annotation.isrna)
737 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
739 validateRangeAndDisplay(); // construct hashcodes, etc.
743 * clip the annotation to the columns given by startRes and endRes (inclusive)
744 * and prune any existing sequenceMapping to just those columns.
749 public void restrict(int startRes, int endRes)
751 if (annotations == null)
760 if (startRes >= annotations.length)
762 startRes = annotations.length - 1;
764 if (endRes >= annotations.length)
766 endRes = annotations.length - 1;
768 if (annotations == null)
772 Annotation[] temp = new Annotation[endRes - startRes + 1];
773 if (startRes < annotations.length)
775 System.arraycopy(annotations, startRes, temp, 0,
776 endRes - startRes + 1);
778 if (sequenceRef != null)
780 // Clip the mapping, if it exists.
781 int spos = sequenceRef.findPosition(startRes);
782 int epos = sequenceRef.findPosition(endRes);
783 if (sequenceMapping != null)
785 Map<Integer, Annotation> newmapping = new HashMap<>();
786 Iterator<Integer> e = sequenceMapping.keySet().iterator();
789 Integer pos = e.next();
790 if (pos.intValue() >= spos && pos.intValue() <= epos)
792 newmapping.put(pos, sequenceMapping.get(pos));
795 sequenceMapping.clear();
796 sequenceMapping = newmapping;
803 * set the annotation row to be at least length Annotations
806 * minimum number of columns required in the annotation row
807 * @return false if the annotation row is greater than length
809 public boolean padAnnotation(int length)
811 if (annotations == null)
813 return true; // annotation row is correct - null == not visible and
816 if (annotations.length < length)
818 Annotation[] na = new Annotation[length];
819 System.arraycopy(annotations, 0, na, 0, annotations.length);
823 return annotations.length > length;
830 * @return DOCUMENT ME!
833 public String toString()
835 if (annotations == null)
839 StringBuilder buffer = new StringBuilder(256);
841 for (int i = 0; i < annotations.length; i++)
843 if (annotations[i] != null)
847 buffer.append(annotations[i].value);
851 buffer.append(annotations[i].secondaryStructure);
855 buffer.append(annotations[i].displayCharacter);
861 // TODO: remove disgusting hack for 'special' treatment of consensus line.
862 if (label.indexOf("Consensus") == 0)
866 for (int i = 0; i < annotations.length; i++)
868 if (annotations[i] != null)
870 buffer.append(annotations[i].description);
877 return buffer.toString();
880 public void setThreshold(GraphLine line)
885 public GraphLine getThreshold()
891 * Attach the annotation to seqRef, starting from startRes position. If
892 * alreadyMapped is true then the indices of the annotation[] array are
893 * sequence positions rather than alignment column positions.
897 * @param alreadyMapped
898 * - annotation are at aligned columns
900 public void createSequenceMapping(SequenceI seqRef, int startRes,
901 boolean alreadyMapped)
908 sequenceRef = seqRef;
909 if (annotations == null)
913 sequenceMapping = new HashMap<>();
917 for (int i = 0; i < annotations.length; i++)
919 if (annotations[i] != null)
923 seqPos = seqRef.findPosition(i);
927 seqPos = i + startRes;
930 sequenceMapping.put(new Integer(seqPos), annotations[i]);
937 * When positional annotation and a sequence reference is present, clears and
938 * resizes the annotations array to the current alignment width, and adds
939 * annotation according to aligned positions of the sequenceRef given by
942 public void adjustForAlignment()
944 if (sequenceRef == null)
949 if (annotations == null)
954 int a = 0, aSize = sequenceRef.getLength();
963 Annotation[] temp = new Annotation[aSize];
965 if (sequenceMapping != null)
967 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
969 index = new Integer(a);
970 Annotation annot = sequenceMapping.get(index);
973 position = sequenceRef.findIndex(a) - 1;
975 temp[position] = annot;
983 * remove any null entries in annotation row and return the number of non-null
984 * annotation elements.
988 public int compactAnnotationArray()
990 int i = 0, iSize = annotations.length;
993 if (annotations[i] == null)
997 System.arraycopy(annotations, i + 1, annotations, i,
1007 Annotation[] ann = annotations;
1008 annotations = new Annotation[i];
1009 System.arraycopy(ann, 0, annotations, 0, i);
1015 * Associate this annotation with the aligned residues of a particular
1016 * sequence. sequenceMapping will be updated in the following way: null
1017 * sequenceI - existing mapping will be discarded but annotations left in
1018 * mapped positions. valid sequenceI not equal to current sequenceRef: mapping
1019 * is discarded and rebuilt assuming 1:1 correspondence TODO: overload with
1020 * parameter to specify correspondence between current and new sequenceRef
1024 public void setSequenceRef(SequenceI sequenceI)
1026 if (sequenceI != null)
1028 if (sequenceRef != null)
1030 boolean rIsDs = sequenceRef.getDatasetSequence() == null,
1031 tIsDs = sequenceI.getDatasetSequence() == null;
1032 if (sequenceRef != sequenceI
1034 && sequenceRef != sequenceI.getDatasetSequence())
1036 && sequenceRef.getDatasetSequence() != sequenceI)
1037 && (!rIsDs && !tIsDs
1038 && sequenceRef.getDatasetSequence() != sequenceI
1039 .getDatasetSequence())
1040 && !sequenceRef.equals(sequenceI))
1042 // if sequenceRef isn't intersecting with sequenceI
1043 // throw away old mapping and reconstruct.
1045 if (sequenceMapping != null)
1047 sequenceMapping = null;
1048 // compactAnnotationArray();
1050 createSequenceMapping(sequenceI, 1, true);
1051 adjustForAlignment();
1055 // Mapping carried over
1056 sequenceRef = sequenceI;
1061 // No mapping exists
1062 createSequenceMapping(sequenceI, 1, true);
1063 adjustForAlignment();
1068 // throw away the mapping without compacting.
1069 sequenceMapping = null;
1077 public double getScore()
1086 public void setScore(double score)
1094 * @return true if annotation has an associated score
1096 public boolean hasScore()
1098 return hasScore || !Double.isNaN(score);
1102 * Score only annotation
1105 * @param description
1108 public AlignmentAnnotation(String label, String description, double score)
1110 this(label, description, null);
1115 * copy constructor with edit based on the hidden columns marked in colSel
1117 * @param alignmentAnnotation
1120 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1121 HiddenColumns hidden)
1123 this(alignmentAnnotation);
1124 if (annotations == null)
1128 makeVisibleAnnotation(hidden);
1131 public void setPadGaps(boolean padgaps, char gapchar)
1133 this.padGaps = padgaps;
1137 for (int i = 0; i < annotations.length; i++)
1139 if (annotations[i] == null)
1141 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1144 else if (annotations[i].displayCharacter == null
1145 || annotations[i].displayCharacter.equals(" "))
1147 annotations[i].displayCharacter = String.valueOf(gapchar);
1154 * format description string for display
1157 * @return Get the annotation description string optionally prefixed by
1158 * associated sequence name (if any)
1160 public String getDescription(boolean seqname)
1162 if (seqname && this.sequenceRef != null)
1164 int i = description.toLowerCase().indexOf("<html>");
1167 // move the html tag to before the sequence reference.
1168 return "<html>" + sequenceRef.getName() + " : "
1169 + description.substring(i + 6);
1171 return sequenceRef.getName() + " : " + description;
1176 public boolean isValidStruc()
1178 return invalidrnastruc == -1;
1181 public long getInvalidStrucPos()
1183 return invalidrnastruc;
1187 * machine readable ID string indicating what generated this annotation
1189 protected String calcId = "";
1192 * properties associated with the calcId
1194 protected Map<String, String> properties = new HashMap<>();
1197 * base colour for line graphs. If null, will be set automatically by
1198 * searching the alignment annotation
1200 public java.awt.Color _linecolour;
1202 public String getCalcId()
1207 public void setCalcId(String calcId)
1209 this.calcId = calcId;
1212 public boolean isRNA()
1218 * transfer annotation to the given sequence using the given mapping from the
1219 * current positions or an existing sequence mapping
1223 * map involving sq as To or From
1225 public void liftOver(SequenceI sq, Mapping sp2sq)
1227 if (sp2sq.getMappedWidth() != sp2sq.getWidth())
1229 // TODO: employ getWord/MappedWord to transfer annotation between cDNA and
1230 // Protein reference frames
1232 "liftOver currently not implemented for transfer of annotation between different types of seqeunce");
1234 boolean mapIsTo = (sp2sq != null)
1235 ? (sp2sq.getTo() == sq
1236 || sp2sq.getTo() == sq.getDatasetSequence())
1239 // TODO build a better annotation element map and get rid of annotations[]
1240 Map<Integer, Annotation> mapForsq = new HashMap<>();
1241 if (sequenceMapping != null)
1245 for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
1247 Integer mpos = Integer
1248 .valueOf(mapIsTo ? sp2sq.getMappedPosition(ie.getKey())
1249 : sp2sq.getPosition(ie.getKey()));
1250 if (mpos >= sq.getStart() && mpos <= sq.getEnd())
1252 mapForsq.put(mpos, ie.getValue());
1255 sequenceMapping = mapForsq;
1257 adjustForAlignment();
1267 * like liftOver but more general.
1269 * Takes an array of int pairs that will be used to update the internal
1270 * sequenceMapping and so shuffle the annotated positions
1273 * - new sequence reference for the annotation row - if null,
1274 * sequenceRef is left unchanged
1276 * array of ints containing corresponding positions
1278 * - column for current coordinate system (-1 for index+1)
1280 * - column for destination coordinate system (-1 for index+1)
1282 * - offset added to index when referencing either coordinate system
1283 * @note no checks are made as to whether from and/or to are sensible
1284 * @note caller should add the remapped annotation to newref if they have not
1287 public void remap(SequenceI newref, HashMap<Integer, int[]> mapping,
1288 int from, int to, int idxoffset)
1290 if (mapping != null)
1292 Map<Integer, Annotation> old = sequenceMapping;
1293 Map<Integer, Annotation> remap = new HashMap<>();
1295 for (int mp[] : mapping.values())
1301 Annotation ann = null;
1304 ann = sequenceMapping.get(Integer.valueOf(idxoffset + index));
1308 if (mp != null && mp.length > from)
1310 ann = sequenceMapping.get(Integer.valueOf(mp[from]));
1317 remap.put(Integer.valueOf(idxoffset + index), ann);
1321 if (to > -1 && to < mp.length)
1323 remap.put(Integer.valueOf(mp[to]), ann);
1328 sequenceMapping = remap;
1332 sequenceRef = newref;
1334 adjustForAlignment();
1338 public String getProperty(String property)
1340 if (properties == null)
1344 return properties.get(property);
1347 public void setProperty(String property, String value)
1349 if (properties == null)
1351 properties = new HashMap<>();
1353 properties.put(property, value);
1356 public boolean hasProperties()
1358 return properties != null && properties.size() > 0;
1361 public Collection<String> getProperties()
1363 if (properties == null)
1365 return Collections.emptyList();
1367 return properties.keySet();
1371 * Returns the Annotation for the given sequence position (base 1) if any,
1377 public Annotation getAnnotationForPosition(int position)
1379 return sequenceMapping == null ? null : sequenceMapping.get(position);
1384 * Set the id to "ann" followed by a counter that increments so as to be
1385 * unique for the lifetime of the JVM
1387 protected final void setAnnotationId()
1389 this.annotationId = ANNOTATION_ID_PREFIX + Long.toString(nextId());
1393 * Returns the match for the last unmatched opening RNA helix pair symbol
1394 * preceding the given column, or '(' if nothing found to match.
1399 public String getDefaultRnaHelixSymbol(int column)
1401 String result = "(";
1402 if (annotations == null)
1408 * for each preceding column, if it contains an open bracket,
1409 * count whether it is still unmatched at column, if so return its pair
1410 * (likely faster than the fancy alternative using stacks)
1412 for (int col = column - 1; col >= 0; col--)
1414 Annotation annotation = annotations[col];
1415 if (annotation == null)
1419 String displayed = annotation.displayCharacter;
1420 if (displayed == null || displayed.length() != 1)
1424 char symbol = displayed.charAt(0);
1425 if (!Rna.isOpeningParenthesis(symbol))
1431 * found an opening bracket symbol
1432 * count (closing-opening) symbols of this type that follow it,
1433 * up to and excluding the target column; if the count is less
1434 * than 1, the opening bracket is unmatched, so return its match
1436 String closer = String
1437 .valueOf(Rna.getMatchingClosingParenthesis(symbol));
1438 String opener = String.valueOf(symbol);
1440 for (int j = col + 1; j < column; j++)
1442 if (annotations[j] != null)
1444 String s = annotations[j].displayCharacter;
1445 if (closer.equals(s))
1449 else if (opener.equals(s))
1463 protected static synchronized long nextId()
1470 * @return true for rows that have a range of values in their annotation set
1472 public boolean isQuantitative()
1474 return graphMin < graphMax;
1478 * delete any columns in alignmentAnnotation that are hidden (including
1479 * sequence associated annotation).
1481 * @param hiddenColumns
1482 * the set of hidden columns
1484 public void makeVisibleAnnotation(HiddenColumns hiddenColumns)
1486 if (annotations != null)
1488 makeVisibleAnnotation(0, annotations.length, hiddenColumns);
1493 * delete any columns in alignmentAnnotation that are hidden (including
1494 * sequence associated annotation).
1497 * remove any annotation to the right of this column
1499 * remove any annotation to the left of this column
1500 * @param hiddenColumns
1501 * the set of hidden columns
1503 public void makeVisibleAnnotation(int start, int end,
1504 HiddenColumns hiddenColumns)
1506 if (annotations != null)
1508 if (hiddenColumns.hasHiddenColumns())
1510 removeHiddenAnnotation(start, end, hiddenColumns);
1514 restrict(start, end);
1520 * The actual implementation of deleting hidden annotation columns
1523 * remove any annotation to the right of this column
1525 * remove any annotation to the left of this column
1526 * @param hiddenColumns
1527 * the set of hidden columns
1529 private void removeHiddenAnnotation(int start, int end,
1530 HiddenColumns hiddenColumns)
1532 // mangle the alignmentAnnotation annotation array
1533 ArrayList<Annotation[]> annels = new ArrayList<>();
1534 Annotation[] els = null;
1538 Iterator<int[]> blocks = hiddenColumns.getVisContigsIterator(start,
1542 int annotationLength;
1543 while (blocks.hasNext())
1545 int[] block = blocks.next();
1546 annotationLength = block[1] - block[0] + 1;
1548 if (blocks.hasNext())
1550 // copy just the visible segment of the annotation row
1551 copylength = annotationLength;
1555 if (annotationLength + block[0] <= annotations.length)
1557 // copy just the visible segment of the annotation row
1558 copylength = annotationLength;
1562 // copy to the end of the annotation row
1563 copylength = annotations.length - block[0];
1567 els = new Annotation[annotationLength];
1569 System.arraycopy(annotations, block[0], els, 0, copylength);
1570 w += annotationLength;
1575 annotations = new Annotation[w];
1578 for (Annotation[] chnk : annels)
1580 System.arraycopy(chnk, 0, annotations, w, chnk.length);
1586 public static Iterable<AlignmentAnnotation> findAnnotations(
1587 Iterable<AlignmentAnnotation> list, SequenceI seq, String calcId,
1591 ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
1592 for (AlignmentAnnotation ann : list)
1594 if ((calcId == null || (ann.getCalcId() != null
1595 && ann.getCalcId().equals(calcId)))
1596 && (seq == null || (ann.sequenceRef != null
1597 && ann.sequenceRef == seq))
1599 || (ann.label != null && ann.label.equals(label))))
1608 * Answer true if any annotation matches the calcId passed in (if not null).
1611 * annotation to search
1615 public static boolean hasAnnotation(List<AlignmentAnnotation> list,
1619 if (calcId != null && !"".equals(calcId))
1621 for (AlignmentAnnotation a : list)
1623 if (a.getCalcId() == calcId)
1632 public static Iterable<AlignmentAnnotation> findAnnotation(
1633 List<AlignmentAnnotation> list, String calcId)
1636 List<AlignmentAnnotation> aa = new ArrayList<>();
1641 for (AlignmentAnnotation a : list)
1644 if (a.getCalcId() == calcId || (a.getCalcId() != null
1645 && calcId != null && a.getCalcId().equals(calcId)))