Merge branch 'releases/Release_2_10_3_Branch'
[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      * String representation for inspection when debugging only
66      */
67     @Override
68     public String toString()
69     {
70       return String.format("%s:%d:%s", major ? "major" : "minor", column,
71               text);
72     }
73   }
74
75   /**
76    * Calculates position markers on the alignment ruler
77    * 
78    * @param av
79    * @param startx
80    *          left-most column in visible view (0..)
81    * @param endx
82    *          - right-most column in visible view (0..)
83    * @return
84    */
85   public List<ScaleMark> calculateMarks(AlignViewportI av, int startx,
86           int endx)
87   {
88     int scalestartx = (startx / 10) * 10;
89
90     SequenceI refSeq = av.getAlignment().getSeqrep();
91     int refSp = 0, refStartI = 0, refEndI = -1;
92     if (refSeq != null)
93     {
94       // find bounds and set origin appopriately
95       // locate first visible position for this sequence
96       int[] refbounds = av.getAlignment().getHiddenColumns()
97               .locateVisibleBoundsOfSequence(refSeq);
98
99       refSp = refbounds[0];
100       refStartI = refbounds[4];
101       refEndI = refbounds[5];
102       scalestartx = refSp + ((scalestartx - refSp) / 10) * 10;
103     }
104
105     if (refSeq == null && scalestartx % 10 == 0)
106     {
107       scalestartx += 5;
108     }
109     List<ScaleMark> marks = new ArrayList<ScaleMark>();
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         String text;
116         if (refSeq == null)
117         {
118           int iadj = av.getAlignment().getHiddenColumns()
119                   .adjustForHiddenColumns(i - 1) + 1;
120           text = String.valueOf(iadj);
121         }
122         else
123         {
124           int iadj = av.getAlignment().getHiddenColumns()
125                   .adjustForHiddenColumns(i - 1);
126           int refN = refSeq.findPosition(iadj);
127           // TODO show bounds if position is a gap
128           // - ie L--R -> "1L|2R" for
129           // marker
130           if (iadj < refStartI)
131           {
132             text = String.valueOf(iadj - refStartI);
133           }
134           else if (iadj > refEndI)
135           {
136             text = "+" + String.valueOf(iadj - refEndI);
137           }
138           else
139           {
140             text = String.valueOf(refN) + refSeq.getCharAt(iadj);
141           }
142         }
143         marks.add(new ScaleMark(true, i - startx - 1, text));
144       }
145       else
146       {
147         marks.add(new ScaleMark(false, i - startx - 1, null));
148       }
149     }
150     return marks;
151   }
152
153 }