001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.align.gui.aligpanel;
022
023import org.biojava.nbio.structure.align.model.AFPChain;
024
025import java.awt.*;
026
027public class AFPChainCoordManager {
028
029
030        AFPChain afpChain ;
031
032        /** Space on the right side between sequence and legend.
033         *
034         */
035        public static final int DEFAULT_RIGHT_SPACER = 10;
036
037        /** number of chars per line
038         *
039         */
040        public static final int DEFAULT_LINE_LENGTH = 70;
041
042        /** size of space between rows
043         *
044         */
045        public static final int DEFAULT_Y_STEP = 60;
046
047        /** size per character
048         *
049         */
050        public static final int DEFAULT_CHAR_SIZE = 12;
051
052
053        /** separation of line 1 and 2 in alignment
054         *
055         */
056        public static final int DEFAULT_LINE_SEPARATION = 20;
057
058
059        /** left boundary
060         *
061         */
062        public static final int DEFAULT_X_SPACE = 20;
063
064        /** top boundary
065         *
066         */
067        public static final int DEFAULT_Y_SPACE = 40;
068
069        /** Position at which the alignment summary is printed
070         *
071         */
072        public static final int SUMMARY_POS = 20;
073
074        private static final int DEFAULT_LEGEND_SIZE = 50;
075
076
077        public int getSummaryPos(){
078                return SUMMARY_POS;
079        }
080
081        /** X coordinate size
082         *
083         * @return the preferred width
084         */
085        public int getPreferredWidth(){
086                return 2* DEFAULT_X_SPACE + DEFAULT_LINE_LENGTH * DEFAULT_CHAR_SIZE + DEFAULT_LEGEND_SIZE +DEFAULT_RIGHT_SPACER + DEFAULT_LEGEND_SIZE;
087        }
088
089        /** Y coordinate size
090         *
091         * @return the preferred height
092         */
093        public int getPreferredHeight(){
094                return 2* DEFAULT_Y_SPACE + (afpChain.getAlnLength() / DEFAULT_LINE_LENGTH) * DEFAULT_Y_STEP + DEFAULT_LINE_SEPARATION;
095        }
096
097        /** Convert from a X position in the JPanel to alignment position
098         *
099         * @param aligSeq sequence 0 or 1
100         * @param p point on panel
101         * @return the sequence position for a point on the Panel
102         */
103        public int getSeqPos(int aligSeq, Point p) {
104
105                int x = p.x - DEFAULT_X_SPACE - DEFAULT_LEGEND_SIZE;
106                int y = p.y - DEFAULT_Y_SPACE ;
107
108                y -=  (DEFAULT_LINE_SEPARATION * aligSeq) - DEFAULT_CHAR_SIZE  ;
109
110                int lineNr = y / DEFAULT_Y_STEP;
111
112                //System.out.println("line : " + lineNr);
113
114                int linePos = x / DEFAULT_CHAR_SIZE;
115
116                //System.out.println("line : " + lineNr + " pos in line: " + linePos);
117                return lineNr * DEFAULT_LINE_LENGTH + linePos ;
118
119        }
120
121        /** get the position of the sequence position on the Panel
122         *
123         * @param aligSeq  0 or 1 for which of the two sequences to ask for.
124         * @param i sequence position
125         * @return the point on a panel for a sequence position
126         */
127        public Point getPanelPos(int aligSeq, int i) {
128                Point p = new Point();
129
130                // get line
131                // we do integer division since we ignore remainders
132                int lineNr = i / DEFAULT_LINE_LENGTH;
133
134                // but we want to have the reminder for the line position.
135                int linePos = i % DEFAULT_LINE_LENGTH;
136
137                int x = linePos * DEFAULT_CHAR_SIZE + DEFAULT_X_SPACE + DEFAULT_LEGEND_SIZE;
138
139                int y = lineNr * DEFAULT_Y_STEP + DEFAULT_Y_SPACE;
140
141
142                y += DEFAULT_LINE_SEPARATION * aligSeq;
143
144                p.setLocation(x, y);
145                return p;
146        }
147
148        public void setAFPChain(AFPChain afpChain) {
149                this.afpChain = afpChain;
150
151        }
152
153        /** returns the AligSeq (0 or 1) for a point
154         * returns -1 if not over an alig seq.
155         * @param point
156         * @return which of the two sequences a point on the panel corresponds to
157         */
158        public int getAligSeq(Point point) {
159
160
161                int i1 = getSeqPos(0, point);
162
163
164                Point t1 = getPanelPos(0,i1);
165
166                if ( Math.abs(t1.x - point.x) <= DEFAULT_CHAR_SIZE &&
167                                Math.abs(t1.y-point.y) < DEFAULT_CHAR_SIZE ) {
168                        return 0;
169                }
170
171                int i2   = getSeqPos(1,point);
172                Point t2 = getPanelPos(1,i2);
173
174                if ( Math.abs(t2.x - point.x) < DEFAULT_CHAR_SIZE &&
175                                Math.abs(t2.y-point.y) < DEFAULT_CHAR_SIZE ) {
176                        return 1;
177                }
178
179                //System.out.println(" i1: " + i1 +" t1 : " + Math.abs(t1.x - point.x) + " " + Math.abs(t1.y-point.y));
180                //System.out.println(i2);
181
182
183                return -1;
184        }
185
186        /** provide the coordinates for where to draw the legend for line X and if it is chain 1 or 2
187         *
188         * @param lineNr which line is this for
189         * @param chainNr is it chain 0 or 1
190         * @return get the point where to draw the legend
191         */
192        public Point getLegendPosition(int lineNr, int chainNr) {
193                int x = DEFAULT_X_SPACE ;
194
195                int y = lineNr * DEFAULT_Y_STEP + DEFAULT_Y_SPACE;
196
197                y += chainNr * DEFAULT_LINE_SEPARATION;
198
199                Point p = new Point(x,y);
200                return p;
201        }
202
203        public Point getEndLegendPosition(int lineNr, int chainNr) {
204
205                int x = DEFAULT_LINE_LENGTH * DEFAULT_CHAR_SIZE + DEFAULT_X_SPACE + DEFAULT_LEGEND_SIZE + DEFAULT_RIGHT_SPACER;
206
207                int y = lineNr * DEFAULT_Y_STEP + DEFAULT_Y_SPACE;
208
209                y += chainNr * DEFAULT_LINE_SEPARATION;
210
211                Point p = new Point(x,y);
212                return p;
213        }
214
215}