View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // GenomeWindow.java
20  // Since: Jan 22, 2008
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.graphics;
26  
27  public class GenomeWindow {
28  	public final long startIndexOnGenome;
29  	public final long endIndexOnGenome;
30  	private long range;
31  	private boolean isReverse = false;
32  
33  	public GenomeWindow(long startIndexOnGenome, long endIndexOnGenome) {
34  		this.startIndexOnGenome = startIndexOnGenome;
35  		this.endIndexOnGenome = endIndexOnGenome;
36  		// reverse ?
37  		if (startIndexOnGenome > endIndexOnGenome)
38  			this.isReverse = true;
39  		// inclusive
40  		range = width(endIndexOnGenome, startIndexOnGenome) + 1;
41  	}
42  
43  	public int toGenomeLength(int pixelLength, int windowWidth) {
44  		double genomeLengthPerPixel = (double) (endIndexOnGenome - startIndexOnGenome) / (double) windowWidth;
45  		return (int) genomeLengthPerPixel * pixelLength;
46  	}
47  
48  	public int pixelPositionOnWindow(long indexOnGenome, int windowWidth) {
49  		double v = (indexOnGenome - startIndexOnGenome) * (double) windowWidth;
50  		double v2 = v / (double) (endIndexOnGenome - startIndexOnGenome);
51  		return (int) v2;
52  	}
53  
54  	public int calcGenomePosition(int xOnWindow, int windowWidth) {
55  		if (startIndexOnGenome <= endIndexOnGenome) {
56  			double genomeLengthPerBit = (double) (endIndexOnGenome - startIndexOnGenome) / (double) windowWidth;
57  			return (int) (startIndexOnGenome + (double) xOnWindow * genomeLengthPerBit);
58  		}
59  		else {
60  			// reverse strand
61  			double genomeLengthPerBit = (double) (startIndexOnGenome - endIndexOnGenome) / (double) windowWidth;
62  			return (int) (endIndexOnGenome + (double) (windowWidth - xOnWindow) * genomeLengthPerBit);
63  		}
64  	}
65  
66  	public static long width(long x1, long x2) {
67  		return (x1 < x2) ? x2 - x1 : x1 - x2;
68  	}
69  
70  	public int getXPosOnWindow(long indexOnGenome, int canvasWidth) {
71  		double v = (indexOnGenome - startIndexOnGenome) * (double) canvasWidth;
72  
73  		if (isReverse)
74  			v = canvasWidth - v;
75  
76  		double v2 = v / (double) range;
77  		return (int) v2;
78  	}
79  
80  	public long getGenomeRange() {
81  		return range;
82  	}
83  
84  	public boolean getReverse() {
85  		return isReverse;
86  	}
87  
88  	public boolean hasOverlap(long startOnGenome, long endOnGenome) {
89  		if (isReverse) {
90  			return startOnGenome >= this.endIndexOnGenome && endOnGenome <= this.startIndexOnGenome;
91  		}
92  		else {
93  			return startOnGenome <= this.endIndexOnGenome && endOnGenome >= this.startIndexOnGenome;
94  		}
95  	}
96  }