Skip to content

Commit

Permalink
Fix bug in zoom calculation.
Browse files Browse the repository at this point in the history
Fixing a rounding error  in calculateZoom.  Instead of rounding to the closest integer zoom level I believe
we should be rounding up to the nearest zoom level which can contain the given window.

This fixes a display issue with the ZoomSliderPanel.  I'm not 100% sure I understand the potential consequences
of changing thhis for other code that relies on it, so it would be good to have a second pair of eyes on it.
It seems intuitively to be correct now but it's possible the use case of "zoom level which is closest to the width of the given window"
was really what was wanted.  I haven't found any obvious issues with some manual testing of this change.
  • Loading branch information
lbergelson committed Oct 22, 2023
1 parent 2d82d8e commit 0138f1a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/main/java/org/broad/igv/ui/panel/ReferenceFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
package org.broad.igv.ui.panel;

import com.google.common.primitives.Ints;
import org.broad.igv.logging.*;
import org.broad.igv.Globals;
import org.broad.igv.event.IGVEventBus;
Expand All @@ -54,7 +55,7 @@
*/
public class ReferenceFrame {

private static Logger log = LogManager.getLogger(ReferenceFrame.class);
private static final Logger log = LogManager.getLogger(ReferenceFrame.class);

IGVEventBus eventBus;

Expand Down Expand Up @@ -721,12 +722,17 @@ private void beforeScaleZoom(Locus locus) {
* @param end
* @return
*/
public int calculateZoom(double start, double end) {
public int
calculateZoom(double start, double end) {
final double windowLength = Math.min(end - start, getChromosomeLength());
return (int) Math.round(Globals.log2((getChromosomeLength() / windowLength) * (((double) widthInPixels) / binsPerTile)));
final double exactZoom = Globals.log2((getChromosomeLength() / windowLength) * (((double) widthInPixels) / binsPerTile));
//round up so that you get a zoom level which contains the given window
return (int) Math.ceil(exactZoom);
}




private static int getChromosomeLength(String chrName) {
Genome genome = getGenome();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/ui/panel/ZoomSliderPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private void paintVisibilityThresholds(final Graphics2D transGraphics) {
.sorted()
.distinct()
.map(threshold -> this.getReferenceFrame().calculateZoom(0, threshold))
.collect(Collectors.toList());
.toList();

transGraphics.setColor(TRANSPARENT_BLUE);
Rectangle maxZoom = zoomLevelRects[zoomLevelRects.length - 1];
Expand Down

0 comments on commit 0138f1a

Please sign in to comment.