Skip to content

Commit 0488872

Browse files
committed
Add columns to TXT export. Refactoring. Fixes #295.
1 parent 7b25b9e commit 0488872

File tree

7 files changed

+187
-100
lines changed

7 files changed

+187
-100
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/ExportPDFAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public ExportPDFAction(@Assisted JTable table, @Assisted Supplier<RankingOption>
4747
public void actionPerformed(ActionEvent e) {
4848
Optional<File> file = FileBrowser.promptForPdfExport(fileUtil, jframeProvider.get());
4949
if(file.isPresent()) {
50-
ExportHeatMapPDFTask exportPdfTask = new ExportHeatMapPDFTask(file.get(), table, rankingSupplier.get());
50+
ExportPDFTask exportPdfTask = new ExportPDFTask(file.get(), table, rankingSupplier.get());
5151
Task openPdfViewerTask = getOpenPdfViewerTask(file.get());
5252
dialogTaskManager.execute(new TaskIterator(exportPdfTask, openPdfViewerTask));
5353
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import com.itextpdf.text.pdf.PdfPTable;
2929
import com.itextpdf.text.pdf.PdfWriter;
3030

31-
public class ExportHeatMapPDFTask extends AbstractTask {
31+
public class ExportPDFTask extends AbstractTask {
3232

3333
private static final int MARGIN = 20;
3434

@@ -38,7 +38,7 @@ public class ExportHeatMapPDFTask extends AbstractTask {
3838
private final JTable jTable;
3939
private final RankingOption ranking;
4040

41-
public ExportHeatMapPDFTask(File file, JTable jTable, RankingOption ranking) {
41+
public ExportPDFTask(File file, JTable jTable, RankingOption ranking) {
4242
this.file = file;
4343
this.jTable = jTable;
4444
this.ranking = ranking;
@@ -164,21 +164,19 @@ private PdfPCell createExpressionHeader(int col) {
164164
}
165165

166166
private PdfPCell createGeneCell(int modelRow) {
167-
String text = String.valueOf(getModel().getValueAt(modelRow, HeatMapTableModel.GENE_COL));
167+
String text = ExportTXTTask.getGeneText(getModel(), modelRow);
168168
return new PdfPCell(new Phrase(text));
169169
}
170170

171171
private PdfPCell createDescriptionCell(int modelRow) {
172-
Object value = getModel().getValueAt(modelRow, HeatMapTableModel.DESC_COL);
173-
String text = value == null ? "" : String.valueOf(value);
174-
text = SwingUtil.abbreviate(text, 40);
172+
String text = ExportTXTTask.getDescriptionText(getModel(), modelRow);
175173
return new PdfPCell(new Phrase(text));
176174
}
177175

178176
private PdfPCell createRankCell(int modelRow) {
179177
PdfPCell cell = new PdfPCell();
180178
RankValue rankValue = (RankValue) getModel().getValueAt(modelRow, HeatMapTableModel.RANK_COL);
181-
cell.setPhrase(new Phrase(RankValueRenderer.getRankText(getCellRenderer().getFormat(), rankValue)));
179+
cell.setPhrase(new Phrase(ExportTXTTask.getRankText(HeatMapCellRenderer.getFormat(), rankValue)));
182180
if(rankValue.isSignificant()) {
183181
cell.setBackgroundColor(color(RankValueRenderer.SIGNIFICANT_COLOR));
184182
}
@@ -193,7 +191,7 @@ private PdfPCell createExpressionCell(int row, int col) {
193191
PdfPCell cell = new PdfPCell();
194192
cell.setBackgroundColor(color(color));
195193
if(Double.isFinite(value)) {
196-
cell.setPhrase(new Phrase(getCellRenderer().getFormat().format(value)));
194+
cell.setPhrase(new Phrase(ExportTXTTask.getExpressionText(value)));
197195
}
198196

199197
return cell;

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/ExportTXTAction.java

Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package org.baderlab.csplugins.enrichmentmap.view.heatmap;
22

3+
import java.awt.GridBagLayout;
34
import java.awt.event.ActionEvent;
4-
import java.io.BufferedWriter;
55
import java.io.File;
6-
import java.io.FileWriter;
7-
import java.io.IOException;
8-
import java.util.Collections;
9-
import java.util.List;
6+
import java.util.Optional;
107

118
import javax.swing.AbstractAction;
9+
import javax.swing.ButtonGroup;
1210
import javax.swing.JFrame;
11+
import javax.swing.JLabel;
1312
import javax.swing.JOptionPane;
13+
import javax.swing.JPanel;
14+
import javax.swing.JRadioButton;
1415
import javax.swing.JTable;
15-
import javax.swing.RowSorter;
1616

1717
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.HeatMapTableModel;
18-
import org.cytoscape.util.swing.FileChooserFilter;
18+
import org.baderlab.csplugins.enrichmentmap.view.util.FileBrowser;
19+
import org.baderlab.csplugins.enrichmentmap.view.util.GBCFactory;
20+
import org.baderlab.csplugins.enrichmentmap.view.util.SwingUtil;
1921
import org.cytoscape.util.swing.FileUtil;
22+
import org.cytoscape.work.TaskIterator;
23+
import org.cytoscape.work.swing.DialogTaskManager;
2024

2125
import com.google.inject.Inject;
2226
import com.google.inject.Provider;
@@ -27,6 +31,7 @@ public class ExportTXTAction extends AbstractAction {
2731

2832
@Inject private FileUtil fileUtil;
2933
@Inject private Provider<JFrame> jframeProvider;
34+
@Inject private DialogTaskManager dialogTaskManager;
3035

3136
private final JTable table;
3237

@@ -40,71 +45,40 @@ public ExportTXTAction(@Assisted JTable table) {
4045
this.table = table;
4146
}
4247

43-
private File promptForFile() {
44-
List<FileChooserFilter> filter = Collections.singletonList(new FileChooserFilter("txt Files", "txt"));
45-
File file = fileUtil.getFile(jframeProvider.get(), "Export Heatmap as txt File", FileUtil.SAVE, filter);
46-
if(file == null)
47-
return null;
48-
49-
String fileName = file.toString();
50-
if(!fileName.endsWith(".txt")) {
51-
fileName += ".txt";
52-
file = new File(fileName);
53-
}
54-
return file;
55-
}
5648

5749
private boolean promptForLeadingEdgeOnly(HeatMapTableModel tableModel) {
58-
if(tableModel.hasSignificantRanks()) {
59-
int response = JOptionPane.showConfirmDialog(jframeProvider.get(), "Would you like to save the leading edge only?");
60-
if(response == JOptionPane.YES_OPTION || response == JOptionPane.OK_OPTION)
61-
return true;
50+
if(!tableModel.hasSignificantRanks()) {
51+
return false;
6252
}
63-
return false;
53+
54+
JLabel label = new JLabel("Genes to export:");
55+
JRadioButton allButton = new JRadioButton("All genes");
56+
JRadioButton leadingEdgeButton = new JRadioButton("Leading edge only");
57+
SwingUtil.makeSmall(label, allButton, leadingEdgeButton);
58+
allButton.setSelected(true);
59+
60+
ButtonGroup buttonGroup = new ButtonGroup();
61+
buttonGroup.add(allButton);
62+
buttonGroup.add(leadingEdgeButton);
63+
64+
JPanel panel = new JPanel(new GridBagLayout());
65+
panel.add(label, GBCFactory.grid(0,0).weightx(1.0).get());
66+
panel.add(allButton, GBCFactory.grid(0,1).get());
67+
panel.add(leadingEdgeButton, GBCFactory.grid(0,2).get());
68+
69+
JOptionPane.showMessageDialog(jframeProvider.get(), panel, "Export to TXT", JOptionPane.QUESTION_MESSAGE);
70+
return leadingEdgeButton.isSelected();
6471
}
6572

73+
6674
@Override
6775
public void actionPerformed(ActionEvent ae) {
68-
File file = promptForFile();
69-
if(file == null)
70-
return;
71-
72-
HeatMapTableModel tableModel = (HeatMapTableModel) table.getModel();
73-
boolean leadingEdgeOnly = promptForLeadingEdgeOnly(tableModel);
76+
boolean leadingEdgeOnly = promptForLeadingEdgeOnly((HeatMapTableModel)table.getModel());
7477

75-
try(BufferedWriter out = new BufferedWriter(new FileWriter(file))) {
76-
int numCols = tableModel.getColumnCount();
77-
78-
// Print column headers
79-
out.append(tableModel.getColumnName(HeatMapTableModel.GENE_COL));
80-
out.append("\t");
81-
// Skip the ranks column
82-
for(int col = HeatMapTableModel.DESC_COL_COUNT; col < numCols; col++) {
83-
out.append(tableModel.getColumnName(col));
84-
out.append(col == numCols-1 ? "\n" : "\t");
85-
}
86-
87-
RowSorter<?> sorter = table.getRowSorter();
88-
int numViewRows = sorter.getViewRowCount();
89-
90-
// Print table data
91-
for(int viewRow = 0; viewRow < numViewRows; viewRow++) {
92-
int modelRow = sorter.convertRowIndexToModel(viewRow);
93-
94-
if(leadingEdgeOnly && !tableModel.getRankValue(modelRow).isSignificant())
95-
continue;
96-
97-
out.append(tableModel.getValueAt(modelRow, HeatMapTableModel.GENE_COL).toString());
98-
out.append("\t");
99-
for(int col = HeatMapTableModel.DESC_COL_COUNT; col < numCols; col++) {
100-
out.append(tableModel.getValueAt(modelRow, col).toString());
101-
out.append(col == numCols-1 ? "\n" : "\t");
102-
}
103-
}
104-
105-
} catch(IOException e) {
106-
JOptionPane.showMessageDialog(jframeProvider.get(), e.getMessage(), "Error writing file", JOptionPane.ERROR_MESSAGE);
107-
e.printStackTrace();
78+
Optional<File> file = FileBrowser.promptForTXTExport(fileUtil, jframeProvider.get());
79+
if(file.isPresent()) {
80+
ExportTXTTask task = new ExportTXTTask(file.get(), table, leadingEdgeOnly);
81+
dialogTaskManager.execute(new TaskIterator(task));
10882
}
10983
}
11084

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.baderlab.csplugins.enrichmentmap.view.heatmap;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.File;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.text.DecimalFormat;
8+
9+
import javax.swing.JTable;
10+
import javax.swing.RowSorter;
11+
12+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.HeatMapCellRenderer;
13+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.HeatMapTableModel;
14+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue;
15+
import org.baderlab.csplugins.enrichmentmap.view.util.SwingUtil;
16+
import org.cytoscape.work.AbstractTask;
17+
import org.cytoscape.work.TaskMonitor;
18+
19+
public class ExportTXTTask extends AbstractTask {
20+
21+
private final File file;
22+
private final JTable table;
23+
private final boolean leadingEdgeOnly;
24+
25+
public ExportTXTTask(File file, JTable table, boolean leadingEdgeOnly) {
26+
this.file = file;
27+
this.table = table;
28+
this.leadingEdgeOnly = leadingEdgeOnly;
29+
}
30+
31+
@Override
32+
public void run(TaskMonitor taskMonitor) throws IOException {
33+
taskMonitor.setTitle("Export HeatMap to TXT file");
34+
35+
HeatMapTableModel model = (HeatMapTableModel) table.getModel();
36+
37+
try(BufferedWriter out = new BufferedWriter(new FileWriter(file))) {
38+
int numCols = model.getColumnCount();
39+
40+
// Print column headers
41+
out.append(model.getColumnName(HeatMapTableModel.GENE_COL)).append('\t');
42+
out.append(model.getColumnName(HeatMapTableModel.DESC_COL)).append('\t');
43+
out.append("Ranks").append('\t');
44+
45+
for(int col = HeatMapTableModel.DESC_COL_COUNT; col < numCols; col++) {
46+
out.append(model.getColumnName(col));
47+
out.append(col == numCols-1 ? "\n" : "\t");
48+
}
49+
50+
RowSorter<?> sorter = table.getRowSorter();
51+
int numViewRows = sorter.getViewRowCount();
52+
53+
// Print table data
54+
for(int viewRow = 0; viewRow < numViewRows; viewRow++) {
55+
int row = sorter.convertRowIndexToModel(viewRow);
56+
57+
if(leadingEdgeOnly && !model.getRankValue(row).isSignificant())
58+
continue;
59+
60+
out.append(getGeneText(model, row)).append('\t');
61+
out.append(getDescriptionText(model, row)).append('\t');
62+
out.append(getRankText(model, row)).append('\t');
63+
64+
for(int col = HeatMapTableModel.DESC_COL_COUNT; col < numCols; col++) {
65+
out.append(getExpressionText(model, row, col));
66+
out.append(col == numCols-1 ? "\n" : "\t");
67+
}
68+
}
69+
}
70+
}
71+
72+
73+
public static String getGeneText(HeatMapTableModel model, int modelRow) {
74+
return String.valueOf(model.getValueAt(modelRow, HeatMapTableModel.GENE_COL));
75+
}
76+
77+
public static String getDescriptionText(HeatMapTableModel model, int modelRow) {
78+
Object value = model.getValueAt(modelRow, HeatMapTableModel.DESC_COL);
79+
String text = value == null ? "" : String.valueOf(value);
80+
text = text.replaceAll("\t", " ");
81+
return SwingUtil.abbreviate(text, 40);
82+
}
83+
84+
public static String getRankText(HeatMapTableModel model, int modelRow) {
85+
RankValue rankValue = (RankValue) model.getValueAt(modelRow, HeatMapTableModel.RANK_COL);
86+
return getRankText(HeatMapCellRenderer.getFormat(), rankValue);
87+
}
88+
89+
public static String getRankText(DecimalFormat format, RankValue rankValue) {
90+
Double score = rankValue.getScore();
91+
if(score == null) {
92+
Integer rank = rankValue.getRank();
93+
if(rank != null) {
94+
return String.valueOf(rank);
95+
}
96+
} else {
97+
return format.format(score);
98+
}
99+
return "";
100+
}
101+
102+
public static String getExpressionText(HeatMapTableModel model, int modelRow, int col) {
103+
Number value = (Number) model.getValueAt(modelRow, col);
104+
if(value == null)
105+
return "";
106+
return getExpressionText(value.doubleValue());
107+
}
108+
109+
public static String getExpressionText(double value) {
110+
return HeatMapCellRenderer.getText(value);
111+
}
112+
113+
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/table/HeatMapCellRenderer.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class HeatMapCellRenderer implements TableCellRenderer {
2525

2626
private final Map<Pair<EMDataSet,Transform>,Optional<DataSetColorRange>> colorRanges = new HashMap<>();
27-
private final DecimalFormat format = new DecimalFormat("###.##");
27+
private final static DecimalFormat format = new DecimalFormat("###.##");
2828
private boolean showValue;
2929

3030

@@ -46,24 +46,24 @@ public JLabel getTableCellRendererComponent(JTable table, Object value, boolean
4646
Border border = BorderFactory.createMatteBorder(1, 1, 1, 1, isSelected ? table.getSelectionForeground() : color);
4747
label.setBorder(border);
4848

49-
if(Double.isFinite(d)) {
50-
String text = format.format(d);
51-
label.setToolTipText(text);
52-
if(showValue) {
53-
label.setText(text);
54-
label.setFont(new Font((UIManager.getFont("TableHeader.font")).getName(), Font.PLAIN, (UIManager.getFont("TableHeader.font")).getSize()-2));
55-
label.setHorizontalAlignment(SwingConstants.RIGHT);
56-
}
57-
} else if(Double.isNaN(d)) {
58-
label.setToolTipText("NaN");
49+
String text = getText(d);
50+
label.setToolTipText(text);
51+
52+
if(showValue && Double.isFinite(d)) {
53+
label.setText(text);
54+
label.setFont(new Font((UIManager.getFont("TableHeader.font")).getName(), Font.PLAIN, (UIManager.getFont("TableHeader.font")).getSize()-2));
55+
label.setHorizontalAlignment(SwingConstants.RIGHT);
5956
}
6057
}
6158

6259
return label;
6360
}
6461

62+
public static String getText(double d) {
63+
return Double.isFinite(d) ? format.format(d) : "NaN";
64+
}
6565

66-
public DecimalFormat getFormat() {
66+
public static DecimalFormat getFormat() {
6767
return format;
6868
}
6969

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/table/RankValueRenderer.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import javax.swing.UIManager;
1010
import javax.swing.table.DefaultTableCellRenderer;
1111

12+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.ExportTXTTask;
13+
1214
@SuppressWarnings("serial")
1315
public class RankValueRenderer extends DefaultTableCellRenderer {
1416

@@ -20,7 +22,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
2022
if(value instanceof RankValue) {
2123
RankValue rankValue = (RankValue) value;
2224

23-
JLabel label = new JLabel(getRankText(format, rankValue));
25+
JLabel label = new JLabel(ExportTXTTask.getRankText(format, rankValue));
2426

2527
if (rankValue.isSignificant()) {
2628
label.setBackground(SIGNIFICANT_COLOR);
@@ -37,19 +39,5 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
3739
}
3840
return new JLabel();
3941
}
40-
41-
42-
public static String getRankText(DecimalFormat format, RankValue rankValue) {
43-
Double score = rankValue.getScore();
44-
if(score == null) {
45-
Integer rank = rankValue.getRank();
46-
if(rank != null) {
47-
return String.valueOf(rank);
48-
}
49-
} else {
50-
return format.format(score);
51-
}
52-
return "";
53-
}
5442

5543
}

0 commit comments

Comments
 (0)