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.renderer;
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.HiddenColumns;
25 import jalview.datamodel.SequenceI;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
32 * Calculate and display alignment rulers
37 public class ScaleRenderer
40 * Represents one major or minor scale mark
42 public final class ScaleMark
45 * true for a major scale mark, false for minor
47 public final boolean major;
50 * visible column position (0..) e.g. 19
52 public final int column;
55 * text (if any) to show e.g. "20"
57 public final String text;
59 ScaleMark(boolean isMajor, int col, String txt)
68 * calculate positions markers on the alignment ruler
72 * left-most column in visible view
74 * - right-most column in visible view
75 * @return List of ScaleMark holding boolean: true/false for major/minor mark,
76 * marker position in alignment column coords, a String to be rendered
77 * at the position (or null)
79 public List<ScaleMark> calculateMarks(AlignViewportI av, int startx,
82 int scalestartx = (startx / 10) * 10;
84 SequenceI refSeq = av.getAlignment().getSeqrep();
89 HiddenColumns hc = av.getAlignment().getHiddenColumns();
93 // find bounds and set origin appropriately
94 // locate first residue in sequence which is not hidden
95 Iterator<int[]> it = hc.iterator();
96 int index = refSeq.firstResidueOutsideIterator(it);
97 refSp = hc.absoluteToVisibleColumn(index);
99 refStartI = refSeq.findIndex(refSeq.getStart()) - 1;
101 int seqlength = refSeq.getLength();
102 // get sequence position past the end of the sequence
103 int pastEndPos = refSeq.findPosition(seqlength + 1);
104 refEndI = refSeq.findIndex(pastEndPos - 1) - 1;
106 scalestartx = refSp + ((scalestartx - refSp) / 10) * 10;
109 if (refSeq == null && scalestartx % 10 == 0)
113 List<ScaleMark> marks = new ArrayList<>();
116 // todo: add a 'reference origin column' to set column number relative to
117 for (int i = scalestartx; i <= endx; i += 5)
119 if (((i - refSp) % 10) == 0)
123 iadj = hc.visibleToAbsoluteColumn(i - 1) + 1;
124 string = String.valueOf(iadj);
128 iadj = hc.visibleToAbsoluteColumn(i - 1);
129 refN = refSeq.findPosition(iadj);
130 // TODO show bounds if position is a gap
131 // - ie L--R -> "1L|2R" for
133 if (iadj < refStartI)
135 string = String.valueOf(iadj - refStartI);
137 else if (iadj > refEndI)
139 string = "+" + String.valueOf(iadj - refEndI);
143 string = String.valueOf(refN) + refSeq.getCharAt(iadj);
146 marks.add(new ScaleMark(true, i - startx - 1, string));
150 marks.add(new ScaleMark(false, i - startx - 1, null));