Skip to content

Commit 5ea57ad

Browse files
committed
add support for transparent background 2-color HM
TwoColorHeatMapWindow -add checkbox for toggling background transparency TwoColorHeatMapOutput -pass along parameter flagging transparent background -remove leftover print statement (for output status flag) TwoColorHeatMap -add boolean parameter to toggle transparency of background by determining mincolor -minor fix to account for NaN values when determining quantiles TwoColorHeatMapCLI -add flag to support transparent background heatmaps -Remove extension requirements for Galaxy compatibility
1 parent 73ca966 commit 5ea57ad

4 files changed

Lines changed: 28 additions & 29 deletions

File tree

src/cli/Figure_Generation/TwoColorHeatMapCLI.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ static class ColorGroup {
6767
"--color" }, description = "For custom color: type hexadecimal string to represent colors (e.g. \"FF0000\" is hexadecimal for red).\n See <http://www.javascripter.net/faq/rgbtohex.htm> for some color options with their corresponding hex strings.\n")
6868
private String custom = null;
6969
}
70-
@Option(names = { "-t",
71-
"--transparent" }, description = "Value indicating transparency of heatmap, 0 to 255 (default=255)\\n")
70+
@Option(names = { "-t", "--transparent" }, description = "Value indicating transparency of heatmap, 0 to 255 (default=255)\n")
7271
private int alpha = 255;
7372

73+
@Option(names = { "-b", "--background" }, description = "Set a transparent background for the heatmap minimum values (default=white)\n")
74+
private boolean transparentBackground = false;
75+
76+
7477
String scaleType = "treeview";
7578
Color MAXCOLOR = Color.BLACK;
7679

@@ -86,7 +89,7 @@ public Integer call() throws Exception {
8689

8790
// Generate HeatMap
8891
TwoColorHeatMap script_object = new TwoColorHeatMap(CDT, MAXCOLOR, startROW, startCOL, pixelHeight, pixelWidth,
89-
scaleType, absolute, percentile, output, true);
92+
scaleType, absolute, percentile, output, true, transparentBackground);
9093
script_object.run();
9194

9295
System.err.println("Image Generated.");
@@ -110,26 +113,12 @@ private String validateInput() throws IOException {
110113
r += "(!)CDT file does not exist: " + CDT.getName() + "\n";
111114
return (r);
112115
}
113-
// check input extensions
114-
if (!"cdt".equals(ExtensionFileFilter.getExtension(CDT))) {
115-
r += "(!)Is this a CDT file? Check extension: " + CDT.getName() + "\n";
116-
}
117116
// set default output filename
118117
if (output == null) {
119118
String NAME = ExtensionFileFilter.stripExtension(CDT);
120119
output = new File(NAME + "_" + scaleType + ".png");
121120
// check output filename is valid
122121
} else {
123-
// check ext
124-
try {
125-
if (!"png".equals(ExtensionFileFilter.getExtension(output))) {
126-
r += "(!)Use PNG extension for output filename. Try: " + ExtensionFileFilter.stripExtension(output)
127-
+ ".png\n";
128-
}
129-
} catch (NullPointerException e) {
130-
r += "(!)Output filename must have extension: use PNG extension for output filename. Try: " + output
131-
+ ".png\n";
132-
}
133122
// check directory
134123
if (output.getParent() == null) {
135124
// System.err.println("default to current directory");

src/scripts/Figure_Generation/TwoColorHeatMap.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class TwoColorHeatMap {
4747
private JLabel picLabel = null;
4848

4949
public TwoColorHeatMap(File in, Color c, int startR, int startC, int pHeight, int pWidth, String scale, double abs,
50-
double quant, File output, boolean outstatus) {
50+
double quant, File output, boolean outstatus, boolean trans) {
5151

5252
SAMPLE = in;
5353
MAXCOLOR = c;
@@ -62,6 +62,10 @@ public TwoColorHeatMap(File in, Color c, int startR, int startC, int pHeight, in
6262

6363
OUTFILE = output;
6464
OUTPUTSTATUS = outstatus;
65+
MINCOLOR = new Color(255, 255, 255, 255);
66+
if (trans) {
67+
MINCOLOR = new Color(MAXCOLOR.getRed(), MAXCOLOR.getGreen(), MAXCOLOR.getBlue(), 0);
68+
}
6569
}
6670

6771
public void run() throws IOException {
@@ -120,7 +124,7 @@ public static BufferedImage generateHeatMap(ArrayList<double[]> matrix) throws F
120124
BufferedImage im = new BufferedImage(pixwidth, pixheight, BufferedImage.TYPE_INT_ARGB);
121125
Graphics g = im.getGraphics();
122126
Graphics2D g2 = (Graphics2D) g;
123-
g2.setColor(new Color(255, 255, 255, 0));
127+
g2.setColor(MINCOLOR);
124128
g2.fillRect(0, 0, pixwidth, pixheight);
125129

126130
int count = 0;
@@ -137,7 +141,7 @@ public static BufferedImage generateHeatMap(ArrayList<double[]> matrix) throws F
137141
int alpha = (int) (MAXCOLOR.getAlpha() * sVal + MINCOLOR.getAlpha() * (1 - sVal));
138142
g.setColor(new Color(red, green, blue, alpha));
139143
} else {
140-
g.setColor(Color.WHITE);
144+
g.setColor(MINCOLOR);
141145
}
142146
g.fillRect(j * width, count * height, width, height);
143147
}
@@ -353,7 +357,7 @@ public static double getQuantile(ArrayList<double[]> matrix, double percent) {
353357
ArrayList<Double> nonZero = new ArrayList<Double>();
354358
for (int x = 0; x < matrix.size(); x++) {
355359
for (int y = 0; y < matrix.get(x).length; y++) {
356-
if (matrix.get(x)[y] != 0) {
360+
if (matrix.get(x)[y] != 0 && !Double.isNaN(matrix.get(x)[y])) {
357361
nonZero.add(Double.valueOf(matrix.get(x)[y]));
358362
}
359363
}

src/window_interface/Figure_Generation/TwoColorHeatMapOutput.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class TwoColorHeatMapOutput extends JFrame {
2828
protected static double quantile = 0.9;
2929
protected static double absolute = -999;
3030

31-
public static Color MINCOLOR = new Color(255, 255, 255);
3231
public static Color MAXCOLOR = new Color(255, 0, 0);
32+
public boolean transparentBackground = false;
3333

3434
protected static boolean OUTPUTSTATUS = false;
3535
protected static File OUT_DIR = null;
@@ -39,7 +39,7 @@ public class TwoColorHeatMapOutput extends JFrame {
3939
JTabbedPane newpane;
4040

4141
public TwoColorHeatMapOutput(ArrayList<File> in, Color c, int startR, int startC, int pHeight, int pWidth,
42-
String scale, double abs, double quant, File out_dir, boolean outstatus) {
42+
String scale, double abs, double quant, File out_dir, boolean outstatus, boolean trans) {
4343
setTitle("Heatmap");
4444
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
4545
setBounds(150, 150, 600, 800);
@@ -49,6 +49,7 @@ public TwoColorHeatMapOutput(ArrayList<File> in, Color c, int startR, int startC
4949

5050
SAMPLE = in;
5151
MAXCOLOR = c;
52+
transparentBackground = trans;
5253
startROW = startR;
5354
startCOL = startC;
5455
pixelHeight = pHeight;
@@ -60,7 +61,6 @@ public TwoColorHeatMapOutput(ArrayList<File> in, Color c, int startR, int startC
6061

6162
OUT_DIR = out_dir;
6263
OUTPUTSTATUS = outstatus;
63-
System.out.println(OUTPUTSTATUS);
6464
}
6565

6666
public void run() throws IOException {
@@ -72,7 +72,7 @@ public void run() throws IOException {
7272

7373
// Execute script
7474
TwoColorHeatMap script_object = new TwoColorHeatMap(SAMPLE.get(x), MAXCOLOR, startROW, startCOL,
75-
pixelHeight, pixelWidth, scaleType, absolute, quantile, OUTPUT, OUTPUTSTATUS);
75+
pixelHeight, pixelWidth, scaleType, absolute, quantile, OUTPUT, OUTPUTSTATUS, transparentBackground);
7676
script_object.run();
7777
JLabel picLabel = script_object.getImg();
7878

src/window_interface/Figure_Generation/TwoColorHeatMapWindow.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class TwoColorHeatMapWindow extends JFrame implements ActionListener, Pro
6060
private JTextField txtHeight;
6161
private JTextField txtWidth;
6262
private JButton btnColor;
63+
private JCheckBox chckbxTransparentBackground;
6364
private JRadioButton rdbtnAbsoluteValue;
6465
private JRadioButton rdbtnPercentileValue;
6566
private JRadioButton rdbtnTreeview;
@@ -125,7 +126,7 @@ public Void doInBackground() throws IOException {
125126
double quantile = Double.parseDouble(txtPercent.getText());
126127

127128
TwoColorHeatMapOutput heat = new TwoColorHeatMapOutput(txtFiles, COLOR, startR, startC, pHeight, pWidth,
128-
scaletype, absolute, quantile, OUT_DIR, chckbxOutputHeatmap.isSelected());
129+
scaletype, absolute, quantile, OUT_DIR, chckbxOutputHeatmap.isSelected(), chckbxTransparentBackground.isSelected());
129130

130131
heat.addPropertyChangeListener("heat", new PropertyChangeListener() {
131132
public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
@@ -333,9 +334,14 @@ public void itemStateChanged(ItemEvent e) {
333334
}
334335
});
335336

337+
chckbxTransparentBackground = new JCheckBox("Use transparent background");
338+
sl_contentPane.putConstraint(SpringLayout.NORTH, chckbxTransparentBackground, 8, SpringLayout.SOUTH, lblSelectColor);
339+
sl_contentPane.putConstraint(SpringLayout.WEST, chckbxTransparentBackground, 10, SpringLayout.WEST, lblSelectColor);
340+
contentPane.add(chckbxTransparentBackground);
341+
336342
JLabel lblPixelHeight = new JLabel("Image Height:");
337-
sl_contentPane.putConstraint(SpringLayout.NORTH, lblPixelHeight, 20, SpringLayout.SOUTH, btnColor);
338-
sl_contentPane.putConstraint(SpringLayout.WEST, lblPixelHeight, 0, SpringLayout.WEST, lblSelectColor);
343+
sl_contentPane.putConstraint(SpringLayout.NORTH, lblPixelHeight, 8, SpringLayout.SOUTH, chckbxTransparentBackground);
344+
sl_contentPane.putConstraint(SpringLayout.WEST, lblPixelHeight, 15, SpringLayout.WEST, contentPane);
339345
contentPane.add(lblPixelHeight);
340346

341347
JLabel lblPixelWidth = new JLabel("Image Width:");
@@ -425,7 +431,7 @@ public void itemStateChanged(ItemEvent e) {
425431
});
426432

427433
JLabel lblImageCompression = new JLabel("Image Compression:");
428-
sl_contentPane.putConstraint(SpringLayout.NORTH, lblImageCompression, 10, SpringLayout.SOUTH, txtAbsolute);
434+
sl_contentPane.putConstraint(SpringLayout.NORTH, lblImageCompression, 5, SpringLayout.SOUTH, txtAbsolute);
429435
sl_contentPane.putConstraint(SpringLayout.WEST, lblImageCompression, 10, SpringLayout.WEST, contentPane);
430436
contentPane.add(lblImageCompression);
431437

0 commit comments

Comments
 (0)