82536d4f53aed6280fe490e3044f61bb464e3e05
[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   public final class ScaleMark
38   {
39     public final boolean major;
40
41     public final int column;
42
43     public final String text;
44
45     ScaleMark(boolean isMajor, int col, String txt)
46     {
47       major = isMajor;
48       column = col;
49       text = txt;
50     }
51   }
52
53   /**
54    * calculate positions markers on the alignment ruler
55    * 
56    * @param av
57    * @param startx
58    *          left-most column in visible view
59    * @param endx
60    *          - right-most column in visible view
61    * @return List of ScaleMark holding boolean: true/false for major/minor mark,
62    *         marker position in alignment column coords, a String to be rendered
63    *         at the position (or null)
64    */
65   public List<ScaleMark> calculateMarks(AlignViewportI av, int startx,
66           int endx)
67   {
68     int scalestartx = (startx / 10) * 10;
69
70     SequenceI refSeq = av.getAlignment().getSeqrep();
71     int refSp = 0, refStartI = 0, refEndI = -1;
72     if (refSeq != null)
73     {
74       // find bounds and set origin appopriately
75       // locate first visible position for this sequence
76       int[] refbounds = av.getColumnSelection()
77               .locateVisibleBoundsOfSequence(refSeq);
78
79       refSp = refbounds[0];
80       refStartI = refbounds[4];
81       refEndI = refbounds[5];
82       scalestartx = refSp + ((scalestartx - refSp) / 10) * 10;
83     }
84
85     if (refSeq == null && scalestartx % 10 == 0)
86     {
87       scalestartx += 5;
88     }
89     List<ScaleMark> marks = new ArrayList<ScaleMark>();
90     String string;
91     int refN, iadj;
92     // todo: add a 'reference origin column' to set column number relative to
93     for (int i = scalestartx; i < endx; i += 5)
94     {
95       if (((i - refSp) % 10) == 0)
96       {
97         if (refSeq == null)
98         {
99           iadj = av.getColumnSelection().adjustForHiddenColumns(i - 1) + 1;
100           string = String.valueOf(iadj);
101         }
102         else
103         {
104           iadj = av.getColumnSelection().adjustForHiddenColumns(i - 1);
105           refN = refSeq.findPosition(iadj);
106           // TODO show bounds if position is a gap
107           // - ie L--R -> "1L|2R" for
108           // marker
109           if (iadj < refStartI)
110           {
111             string = String.valueOf(iadj - refStartI);
112           }
113           else if (iadj > refEndI)
114           {
115             string = "+" + String.valueOf(iadj - refEndI);
116           }
117           else
118           {
119             string = String.valueOf(refN) + refSeq.getCharAt(iadj);
120           }
121         }
122         marks.add(new ScaleMark(true, i - startx - 1, string));
123       }
124       else
125       {
126         marks.add(new ScaleMark(false, i - startx - 1, null));
127       }
128     }
129     return marks;
130   }
131
132 }