1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
37 if (startIndexOnGenome > endIndexOnGenome)
38 this.isReverse = true;
39
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
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 }