public static String readableFileSize(long size) { if (size <= 0) return "0"; final String[] units = new String[] {"B", "KB", "MB", "GB", "TB"}; int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; }
public LinePlotTest() { super(new BorderLayout()); setPreferredSize(new Dimension(800, 600)); setBackground(Color.WHITE); // load data from file ---------------------------------------------- // basemean, log2foldchange DataTable nonSigData = new DataTable(Double.class, Double.class); DataTable sigData = new DataTable(Double.class, Double.class); try (CSVReader reader = new CSVReader(new FileReader("rnaSeqExample.csv"))) { String[] nextLine; int row = 0; while ((nextLine = reader.readNext()) != null) { if (row != 0) { // needs to have a p-value if (nextLine[2].equals("NA")) continue; double baseMean = Double.parseDouble(nextLine[1]); double log2Change = Double.parseDouble(nextLine[2]); if (nextLine[6].equals("NA") || Double.parseDouble(nextLine[6]) > 0.05) { nonSigData.add(Math.log10(baseMean), log2Change); } else { sigData.add(Math.log10(baseMean), log2Change); } } row++; } } catch (IOException e) { e.printStackTrace(); } System.out.println("Non-significant: " + nonSigData.getRowCount()); System.out.println("Significant: " + sigData.getRowCount()); XYPlot plot = new XYPlot(nonSigData, sigData); // set point renderer for data PointRenderer points = new DefaultPointRenderer2D(); points.setShape(new Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0)); points.setColor(NONSIG_COLOR); plot.setPointRenderer(nonSigData, points); PointRenderer pointsRed = new DefaultPointRenderer2D(); pointsRed.setShape(new Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0)); pointsRed.setColor(SIG_COLOR); plot.setPointRenderer(sigData, pointsRed); // Display on screen add(new InteractivePanel(plot), BorderLayout.CENTER); }
public void computeMetricScaleExtremes( int UTMZone, String hemisphere, GridElement ge, double size) { if (UTMZone != this.zone) return; if (size < 1 || size > this.maxResolution) return; UTMExtremes levelExtremes = this.extremes[(int) Math.log10(size) - 1]; if (ge.type.equals(GridElement.TYPE_LINE_EASTING) || ge.type.equals(GridElement.TYPE_LINE_EAST) || ge.type.equals(GridElement.TYPE_LINE_WEST)) { levelExtremes.minX = ge.value < levelExtremes.minX ? ge.value : levelExtremes.minX; levelExtremes.maxX = ge.value > levelExtremes.maxX ? ge.value : levelExtremes.maxX; } else if (ge.type.equals(GridElement.TYPE_LINE_NORTHING) || ge.type.equals(GridElement.TYPE_LINE_SOUTH) || ge.type.equals(GridElement.TYPE_LINE_NORTH)) { if (hemisphere.equals(levelExtremes.minYHemisphere)) levelExtremes.minY = ge.value < levelExtremes.minY ? ge.value : levelExtremes.minY; else if (hemisphere.equals(AVKey.SOUTH)) { levelExtremes.minY = ge.value; levelExtremes.minYHemisphere = hemisphere; } if (hemisphere.equals(levelExtremes.maxYHemisphere)) levelExtremes.maxY = ge.value > levelExtremes.maxY ? ge.value : levelExtremes.maxY; else if (hemisphere.equals(AVKey.NORTH)) { levelExtremes.maxY = ge.value; levelExtremes.maxYHemisphere = hemisphere; } } }
private long normalizeMax(long l) { int exp = (int) Math.log10((double) l); long multiple = (long) Math.pow(10.0, exp); int i = (int) (l / multiple); l = (i + 1) * multiple; return l; }
public void clear() { int numLevels = (int) Math.log10(this.maxResolution); this.extremes = new UTMExtremes[numLevels]; for (int i = 0; i < numLevels; i++) { this.extremes[i] = new UTMExtremes(); this.extremes[i].clear(); } }
private boolean needToSplit(DrawContext dc, Sector sector) { Vec4[] corners = sector.computeCornerPoints(dc.getGlobe(), dc.getVerticalExaggeration()); Vec4 centerPoint = sector.computeCenterPoint(dc.getGlobe(), dc.getVerticalExaggeration()); View view = dc.getView(); double d1 = view.getEyePoint().distanceTo3(corners[0]); double d2 = view.getEyePoint().distanceTo3(corners[1]); double d3 = view.getEyePoint().distanceTo3(corners[2]); double d4 = view.getEyePoint().distanceTo3(corners[3]); double d5 = view.getEyePoint().distanceTo3(centerPoint); double minDistance = d1; if (d2 < minDistance) minDistance = d2; if (d3 < minDistance) minDistance = d3; if (d4 < minDistance) minDistance = d4; if (d5 < minDistance) minDistance = d5; double cellSize = (Math.PI * sector.getDeltaLatRadians() * dc.getGlobe().getRadius()) / 20; // TODO return !(Math.log10(cellSize) <= (Math.log10(minDistance) - this.splitScale)); }
/** * Returns the number of digits in x. * * @param x a whole number. * @return 1 if x is 0, otherwise the number of digits. */ public static int numDigits(int x) { if (x == 0) return 1; return (int) (Math.log10(x)) + 1; // 250 -> log250 = 2.something -> 2+1 -> 3 }
/** * Render the track in the given rectangle. * * @param track * @param locusScores * @param context * @param arect */ public synchronized void renderScores( Track track, List<LocusScore> locusScores, RenderContext context, Rectangle arect) { boolean showMissingData = PreferenceManager.getInstance().getAsBoolean(PreferenceManager.SHOW_MISSING_DATA_KEY); Graphics2D noDataGraphics = context.getGraphic2DForColor(UIConstants.NO_DATA_COLOR); Graphics2D tickGraphics = context.getGraphic2DForColor(Color.BLACK); Rectangle adjustedRect = calculateDrawingRect(arect); double origin = context.getOrigin(); double locScale = context.getScale(); Color posColor = track.getColor(); Color negColor = track.getAltColor(); // Get the Y axis definition, consisting of minimum, maximum, and base value. Often // the base value is == min value which is == 0. DataRange dataRange = track.getDataRange(); float maxValue = dataRange.getMaximum(); float baseValue = dataRange.getBaseline(); float minValue = dataRange.getMinimum(); boolean isLog = dataRange.isLog(); if (isLog) { minValue = (float) (minValue == 0 ? 0 : Math.log10(minValue)); maxValue = (float) Math.log10(maxValue); } // Calculate the Y scale factor. double delta = (maxValue - minValue); double yScaleFactor = adjustedRect.getHeight() / delta; // Calculate the Y position in pixels of the base value. Clip to bounds of rectangle double baseDelta = maxValue - baseValue; int baseY = (int) (adjustedRect.getY() + baseDelta * yScaleFactor); if (baseY < adjustedRect.y) { baseY = adjustedRect.y; } else if (baseY > adjustedRect.y + adjustedRect.height) { baseY = adjustedRect.y + adjustedRect.height; } int lastPx = 0; for (LocusScore score : locusScores) { // Note -- don't cast these to an int until the range is checked. // could get an overflow. double pX = ((score.getStart() - origin) / locScale); double dx = Math.ceil((Math.max(1, score.getEnd() - score.getStart())) / locScale) + 1; if ((pX + dx < 0)) { continue; } else if (pX > adjustedRect.getMaxX()) { break; } float dataY = score.getScore(); if (isLog && dataY <= 0) { continue; } if (!Float.isNaN(dataY)) { // Compute the pixel y location. Clip to bounds of rectangle. double dy = isLog ? Math.log10(dataY) - baseValue : (dataY - baseValue); int pY = baseY - (int) (dy * yScaleFactor); if (pY < adjustedRect.y) { pY = adjustedRect.y; } else if (pY > adjustedRect.y + adjustedRect.height) { pY = adjustedRect.y + adjustedRect.height; } Color color = (dataY >= baseValue) ? posColor : negColor; drawDataPoint(color, (int) dx, (int) pX, baseY, pY, context); } if (showMissingData) { // Draw from lastPx + 1 to pX - 1; int w = (int) pX - lastPx - 4; if (w > 0) { noDataGraphics.fillRect(lastPx + 2, (int) arect.getY(), w, (int) arect.getHeight()); } } if (!Float.isNaN(dataY)) { lastPx = (int) pX + (int) dx; } } if (showMissingData) { int w = (int) arect.getMaxX() - lastPx - 4; if (w > 0) { noDataGraphics.fillRect(lastPx + 2, (int) arect.getY(), w, (int) arect.getHeight()); } } }
// Rendering public void draw(DrawContext dc) { GL gl = dc.getGL(); boolean attribsPushed = false; boolean modelviewPushed = false; boolean projectionPushed = false; try { gl.glPushAttrib( GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT | GL.GL_ENABLE_BIT | GL.GL_TEXTURE_BIT | GL.GL_TRANSFORM_BIT | GL.GL_VIEWPORT_BIT | GL.GL_CURRENT_BIT); attribsPushed = true; gl.glDisable(GL.GL_TEXTURE_2D); // no textures gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); gl.glDisable(GL.GL_DEPTH_TEST); double width = this.size.width; double height = this.size.height; // Load a parallel projection with xy dimensions (viewportWidth, viewportHeight) // into the GL projection matrix. java.awt.Rectangle viewport = dc.getView().getViewport(); gl.glMatrixMode(javax.media.opengl.GL.GL_PROJECTION); gl.glPushMatrix(); projectionPushed = true; gl.glLoadIdentity(); double maxwh = width > height ? width : height; gl.glOrtho(0d, viewport.width, 0d, viewport.height, -0.6 * maxwh, 0.6 * maxwh); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glPushMatrix(); modelviewPushed = true; gl.glLoadIdentity(); // Scale to a width x height space // located at the proper position on screen double scale = this.computeScale(viewport); Vec4 locationSW = this.computeLocation(viewport, scale); gl.glTranslated(locationSW.x(), locationSW.y(), locationSW.z()); gl.glScaled(scale, scale, 1); // Compute scale size in real world Position referencePosition = dc.getViewportCenterPosition(); if (referencePosition != null) { Vec4 groundTarget = dc.getGlobe().computePointFromPosition(referencePosition); Double distance = dc.getView().getEyePoint().distanceTo3(groundTarget); this.pixelSize = dc.getView().computePixelSizeAtDistance(distance); Double scaleSize = this.pixelSize * width * scale; // meter String unitLabel = "m"; if (this.unit.equals(UNIT_METRIC)) { if (scaleSize > 10000) { scaleSize /= 1000; unitLabel = "Km"; } } else if (this.unit.equals(UNIT_IMPERIAL)) { scaleSize *= 3.280839895; // feet unitLabel = "ft"; if (scaleSize > 5280) { scaleSize /= 5280; unitLabel = "mile(s)"; } } // Rounded division size int pot = (int) Math.floor(Math.log10(scaleSize)); if (!Double.isNaN(pot)) { int digit = Integer.parseInt(String.format("%.0f", scaleSize).substring(0, 1)); double divSize = digit * Math.pow(10, pot); if (digit >= 5) divSize = 5 * Math.pow(10, pot); else if (digit >= 2) divSize = 2 * Math.pow(10, pot); double divWidth = width * divSize / scaleSize; // Draw scale if (!dc.isPickingMode()) { // Set color using current layer opacity Color backColor = this.getBackgroundColor(this.color); float[] colorRGB = backColor.getRGBColorComponents(null); gl.glColor4d( colorRGB[0], colorRGB[1], colorRGB[2], (double) backColor.getAlpha() / 255d * this.getOpacity()); gl.glTranslated((width - divWidth) / 2, 0d, 0d); this.drawScale(dc, divWidth, height); colorRGB = this.color.getRGBColorComponents(null); gl.glColor4d(colorRGB[0], colorRGB[1], colorRGB[2], this.getOpacity()); gl.glTranslated(-1d / scale, 1d / scale, 0d); this.drawScale(dc, divWidth, height); // Draw label String label = String.format("%.0f ", divSize) + unitLabel; gl.glLoadIdentity(); gl.glDisable(GL.GL_CULL_FACE); drawLabel( dc, label, locationSW.add3( new Vec4(divWidth * scale / 2 + (width - divWidth) / 2, height * scale, 0))); } else { // Picking this.pickSupport.clearPickList(); this.pickSupport.beginPicking(dc); // Draw unique color across the map Color color = dc.getUniquePickColor(); int colorCode = color.getRGB(); // Add our object(s) to the pickable list this.pickSupport.addPickableObject(colorCode, this, referencePosition, false); gl.glColor3ub((byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue()); gl.glTranslated((width - divWidth) / 2, 0d, 0d); this.drawRectangle(dc, divWidth, height); // Done picking this.pickSupport.endPicking(dc); this.pickSupport.resolvePick(dc, dc.getPickPoint(), this); } } } } finally { if (projectionPushed) { gl.glMatrixMode(GL.GL_PROJECTION); gl.glPopMatrix(); } if (modelviewPushed) { gl.glMatrixMode(GL.GL_MODELVIEW); gl.glPopMatrix(); } if (attribsPushed) gl.glPopAttrib(); } }