JAL-2759 Various minor HiddenColumns updates after review
[jalview.git] / src / jalview / renderer / ScaleRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.renderer;
22
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.SequenceI;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 /**
30  * Calculate and display alignment rulers
31  * 
32  * @author jprocter
33  *
34  */
35 public class ScaleRenderer
36 {
37   /**
38    * Represents one major or minor scale mark
39    */
40   public final class ScaleMark
41   {
42     /**
43      * true for a major scale mark, false for minor
44      */
45     public final boolean major;
46
47     /**
48      * visible column position (0..) e.g. 19
49      */
50     public final int column;
51
52     /**
53      * text (if any) to show e.g. "20"
54      */
55     public final String text;
56
57     ScaleMark(boolean isMajor, int col, String txt)
58     {
59       major = isMajor;
60       column = col;
61       text = txt;
62     }
63   }
64
65   /**
66    * calculate positions markers on the alignment ruler
67    * 
68    * @param av
69    * @param startx
70    *          left-most column in visible view
71    * @param endx
72    *          - right-most column in visible view
73    * @return List of ScaleMark holding boolean: true/false for major/minor mark,
74    *         marker position in alignment column coords, a String to be rendered
75    *         at the position (or null)
76    */
77   public List<ScaleMark> calculateMarks(AlignViewportI av, int startx,
78           int endx)
79   {
80     int scalestartx = (startx / 10) * 10;
81
82     SequenceI refSeq = av.getAlignment().getSeqrep();
83     int refSp = 0;
84     int refStartI = 0;
85     int refEndI = -1;
86     if (refSeq != null)
87     {
88       // find bounds and set origin appropriately
89       // locate first visible position for this sequence
90       refSp = av.getAlignment().getHiddenColumns()
91               .locateVisibleStartOfSequence(refSeq);
92
93       refStartI = refSeq.findIndex(refSeq.getStart()) - 1;
94
95       int seqlength = refSeq.getLength();
96       // get sequence position past the end of the sequence
97       int pastEndPos = refSeq.findPosition(seqlength + 1);
98       refEndI = refSeq.findIndex(pastEndPos - 1) - 1;
99
100       scalestartx = refSp + ((scalestartx - refSp) / 10) * 10;
101     }
102
103     if (refSeq == null && scalestartx % 10 == 0)
104     {
105       scalestartx += 5;
106     }
107     List<ScaleMark> marks = new ArrayList<>();
108     String string;
109     int refN, iadj;
110     // todo: add a 'reference origin column' to set column number relative to
111     for (int i = scalestartx; i <= endx; i += 5)
112     {
113       if (((i - refSp) % 10) == 0)
114       {
115         if (refSeq == null)
116         {
117           iadj = av.getAlignment().getHiddenColumns()
118                   .visibleToAbsoluteColumn(i - 1) + 1;
119           string = String.valueOf(iadj);
120         }
121         else
122         {
123           iadj = av.getAlignment().getHiddenColumns()
124                   .visibleToAbsoluteColumn(i - 1);
125           refN = refSeq.findPosition(iadj);
126           // TODO show bounds if position is a gap
127           // - ie L--R -> "1L|2R" for
128           // marker
129           if (iadj < refStartI)
130           {
131             string = String.valueOf(iadj - refStartI);
132           }
133           else if (iadj > refEndI)
134           {
135             string = "+" + String.valueOf(iadj - refEndI);
136           }
137           else
138           {
139             string = String.valueOf(refN) + refSeq.getCharAt(iadj);
140           }
141         }
142         marks.add(new ScaleMark(true, i - startx - 1, string));
143       }
144       else
145       {
146         marks.add(new ScaleMark(false, i - startx - 1, null));
147       }
148     }
149     return marks;
150   }
151
152 }