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.survival.kaplanmeier.figure; 022 023import org.biojava.nbio.survival.cox.StrataInfo; 024import org.biojava.nbio.survival.cox.SurvFitInfo; 025 026import javax.swing.*; 027import java.awt.*; 028import java.util.ArrayList; 029import java.util.Collections; 030import java.util.LinkedHashMap; 031 032/** 033 * 034 * @author Scooter Willis <willishf at gmail dot com> 035 */ 036public class NumbersAtRiskPanel extends JPanel { 037 038 private static final long serialVersionUID = 1L; 039 040 KaplanMeierFigure kmf = null; 041 Double timePercentage = .20; 042 043 public NumbersAtRiskPanel() { 044 this.setPreferredSize(new Dimension(400,100)); 045 this.setSize(400, 100); 046 } 047 048 /** 049 * Pick up needed info and details from the KM Figure 050 * @param kmf 051 */ 052 public void setKaplanMeierFigure(KaplanMeierFigure kmf) { 053 this.kmf = kmf; 054 055 int numRows = kmf.getSurvivalFitInfo().getStrataInfoHashMap().size(); 056 int height = (numRows + 1) * getFontMetrics(getFont()).getHeight(); 057 int width = kmf.getWidth(); 058 setPreferredSize(new Dimension(width,height)); 059 this.setSize(width, height); 060 061 } 062 063 private void paintTable(Graphics g) { 064 065 if(kmf == null) 066 return; 067 KMFigureInfo kmfi = kmf.getKMFigureInfo(); 068 Graphics2D g2 = (Graphics2D) g; 069 g2.setStroke(kmfi.kmStroke); 070 SurvFitInfo sfi = kmf.getSurvivalFitInfo(); 071 072 LinkedHashMap<String, StrataInfo> sfiHashMap = new LinkedHashMap<String, StrataInfo>(); 073 if(sfi.isWeighted()){ 074 sfiHashMap = sfi.getUnweightedStrataInfoHashMap(); 075 }else{ 076 sfiHashMap = sfi.getStrataInfoHashMap(); 077 } 078 079 if(sfiHashMap.size() == 0) 080 return; 081 //int height = this.getHeight(); 082 083 int row = 0; 084 int left = kmf.getLeft(); 085 //int right = kmf.getRight(); 086 //int width = right - left; 087 Font f = g2.getFont(); 088 Font nf = new Font(f.getName(), Font.BOLD, f.getSize()); 089 g2.setFont(nf); 090 FontMetrics fm = getFontMetrics(nf); 091 int index = 0; 092 int fontHeight = getFontMetrics(getFont()).getHeight(); 093 int increment = fontHeight; 094 ArrayList<Double> xaxisTimeValues = kmf.getxAxisTimeValues(); 095 ArrayList<Integer> xAxisTimeCoordinates = kmf.getxAxisTimeCoordinates(); 096 097 ArrayList<String> labels = new ArrayList<String>(sfiHashMap.keySet()); 098 Collections.sort(labels); 099 100 for (String group : labels) { 101 row = row + increment; 102 g2.setColor(kmfi.getColor(index)); 103 index++; 104 g2.drawLine(15, row - fontHeight/2, left - 5, row - fontHeight/2); 105 g2.setColor(Color.BLACK); 106 StrataInfo si = sfiHashMap.get(group); 107 if(kmf.title.toString().equals("[APOBEC1 Transhera Observation Arm]")){ 108 //int dummy = 1; 109 } 110// System.out.println(kmf.title + " Group " + group); 111// System.out.println(si); 112 for(int i = 0; i < xaxisTimeValues.size(); i++){ 113 Double time = xaxisTimeValues.get(i); 114 int xvalue = xAxisTimeCoordinates.get(i); 115 Double value = si.getNearestAtRisk(time); 116 String nrisk = ""; 117 if(value == null){ 118 nrisk = ""; 119 }else{ 120 nrisk = String.valueOf(value.intValue()); 121 } 122 if(time == 0.0){ 123 g2.drawString(nrisk , xvalue, row); 124 }else{ 125 int w = fm.stringWidth(nrisk ); 126 g2.drawString(nrisk , xvalue - w/2, row); 127 } 128 } 129 130 } 131 } 132 133 @Override 134 protected void paintComponent(Graphics g) { 135 136 super.paintComponent(g); //To change body of generated methods, choose Tools | Templates. 137 g.setColor(Color.white); 138 g.fillRect(0, 0, this.getWidth(), this.getHeight()); 139 140 this.paintTable(g); 141 } 142 143 /** 144 * @param args the command line arguments 145 */ 146 public static void main(String[] args) { 147 // TODO code application logic here 148 } 149}