Skip to content

Commit 427e4e6

Browse files
committed
Export PDF task basically working. Refs #348
1 parent 44179b3 commit 427e4e6

File tree

12 files changed

+224
-58
lines changed

12 files changed

+224
-58
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/CommandModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.baderlab.csplugins.enrichmentmap.commands.EMBuildCommandTask;
1111
import org.baderlab.csplugins.enrichmentmap.commands.EMGseaCommandTask;
1212
import org.baderlab.csplugins.enrichmentmap.commands.ExportModelJsonCommandTask;
13+
import org.baderlab.csplugins.enrichmentmap.commands.ExportPDFCommandTask;
1314
import org.baderlab.csplugins.enrichmentmap.commands.PAKnownSignatureCommandTask;
1415
import org.baderlab.csplugins.enrichmentmap.commands.ResolverCommandTask;
1516
import org.baderlab.csplugins.enrichmentmap.commands.TableCommandTask;
@@ -34,6 +35,7 @@ public class CommandModule extends AbstractModule {
3435
@BindingAnnotation @Retention(RUNTIME) public @interface DatasetShowCommand { }
3536
@BindingAnnotation @Retention(RUNTIME) public @interface DatasetHideCommand { }
3637
@BindingAnnotation @Retention(RUNTIME) public @interface ChartCommand { }
38+
@BindingAnnotation @Retention(RUNTIME) public @interface ExportPDFCommand { }
3739

3840
@Override
3941
protected void configure() {
@@ -84,6 +86,10 @@ public TaskFactory provideChart(Provider<ChartCommandTask> taskFactory) {
8486
return createTaskFactory(taskFactory);
8587
}
8688

89+
@Provides @ExportPDFCommand
90+
public TaskFactory provideExportPDF(Provider<ExportPDFCommandTask> taskProvider) {
91+
return createTaskFactory(taskProvider);
92+
}
8793

8894

8995
private static TaskFactory createTaskFactory(Provider<? extends Task> taskProvider, Task ... tasks) {

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/CyActivator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.baderlab.csplugins.enrichmentmap.CommandModule.ChartCommand;
1414
import org.baderlab.csplugins.enrichmentmap.CommandModule.DatasetHideCommand;
1515
import org.baderlab.csplugins.enrichmentmap.CommandModule.DatasetShowCommand;
16+
import org.baderlab.csplugins.enrichmentmap.CommandModule.ExportPDFCommand;
1617
import org.baderlab.csplugins.enrichmentmap.CommandModule.GSEACommand;
1718
import org.baderlab.csplugins.enrichmentmap.CommandModule.JsonCommand;
1819
import org.baderlab.csplugins.enrichmentmap.CommandModule.PACommand;
@@ -122,11 +123,12 @@ private void initializeCommands(BundleContext bc) {
122123
registerCommand(bc, "gseabuild", injector.getInstance(Key.get(TaskFactory.class, GSEACommand.class)));
123124
registerCommand(bc, "mastermap", injector.getInstance(Key.get(TaskFactory.class, ResolveCommand.class)));
124125
registerCommand(bc, "pa", injector.getInstance(Key.get(TaskFactory.class, PACommand.class)));
125-
registerCommand(bc, "export-model", injector.getInstance(Key.get(TaskFactory.class, JsonCommand.class)));
126+
registerCommand(bc, "export model", injector.getInstance(Key.get(TaskFactory.class, JsonCommand.class)));
126127
registerCommand(bc, "build-table", injector.getInstance(Key.get(TaskFactory.class, BuildTableCommand.class)));
127128
registerCommand(bc, "dataset show", injector.getInstance(Key.get(TaskFactory.class, DatasetShowCommand.class)));
128129
registerCommand(bc, "dataset hide", injector.getInstance(Key.get(TaskFactory.class, DatasetHideCommand.class)));
129130
registerCommand(bc, "chart", injector.getInstance(Key.get(TaskFactory.class, ChartCommand.class)));
131+
registerCommand(bc, "export pdf", injector.getInstance(Key.get(TaskFactory.class, ExportPDFCommand.class)));
130132
registerService(bc, new MannWhitRanksTunableHandlerFactory(), StringTunableHandlerFactory.class);
131133
}
132134

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/commands/ChartCommandTask.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.baderlab.csplugins.enrichmentmap.commands;
22

3+
import static org.baderlab.csplugins.enrichmentmap.commands.tunables.CommandUtil.lssFromEnum;
34
import static org.baderlab.csplugins.enrichmentmap.style.ChartData.DATA_SET;
45
import static org.baderlab.csplugins.enrichmentmap.style.ChartData.EXPRESSION_DATA;
56
import static org.baderlab.csplugins.enrichmentmap.style.ChartData.FDR_VALUE;
@@ -12,8 +13,6 @@
1213
import static org.baderlab.csplugins.enrichmentmap.style.ChartType.HEAT_STRIPS;
1314
import static org.baderlab.csplugins.enrichmentmap.style.ChartType.RADIAL_HEAT_MAP;
1415

15-
import java.util.ArrayList;
16-
import java.util.List;
1716
import java.util.Map;
1817

1918
import org.baderlab.csplugins.enrichmentmap.commands.tunables.NetworkTunable;
@@ -90,14 +89,4 @@ public void run(TaskMonitor taskMonitor) {
9089
controlPanelMediator.reset(params);
9190
}
9291

93-
94-
private static ListSingleSelection<String> lssFromEnum(Enum<?> ... values) {
95-
List<String> names = new ArrayList<>(values.length);
96-
for(Enum<?> value : values) {
97-
names.add(value.name());
98-
}
99-
return new ListSingleSelection<>(names);
100-
}
101-
102-
10392
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package org.baderlab.csplugins.enrichmentmap.commands;
2+
3+
import static org.baderlab.csplugins.enrichmentmap.commands.tunables.CommandUtil.lssFromEnum;
4+
5+
import java.io.File;
6+
import java.util.ArrayList;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
10+
import org.baderlab.csplugins.enrichmentmap.commands.tunables.NetworkTunable;
11+
import org.baderlab.csplugins.enrichmentmap.model.Compress;
12+
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap;
13+
import org.baderlab.csplugins.enrichmentmap.model.Transform;
14+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.ExportPDFTask;
15+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.HeatMapMediator;
16+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.RankingOption;
17+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.HeatMapTableModel;
18+
import org.baderlab.csplugins.enrichmentmap.view.util.OpenPDFViewerTask;
19+
import org.cytoscape.application.CyUserLog;
20+
import org.cytoscape.model.CyEdge;
21+
import org.cytoscape.model.CyNetwork;
22+
import org.cytoscape.model.CyNode;
23+
import org.cytoscape.model.CyTableUtil;
24+
import org.cytoscape.work.AbstractTask;
25+
import org.cytoscape.work.ContainsTunables;
26+
import org.cytoscape.work.TaskIterator;
27+
import org.cytoscape.work.TaskMonitor;
28+
import org.cytoscape.work.Tunable;
29+
import org.cytoscape.work.util.ListSingleSelection;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
import com.google.inject.Inject;
34+
35+
public class ExportPDFCommandTask extends AbstractTask {
36+
37+
@ContainsTunables @Inject
38+
public NetworkTunable networkTunable;
39+
40+
@Tunable(required=true)
41+
public File file;
42+
43+
@Tunable(description="true (default) for only selected nodes and edges, false for all nodes and edges")
44+
public boolean selectedOnly = true;
45+
46+
@Tunable
47+
public boolean showValues = false;
48+
49+
@Tunable
50+
public ListSingleSelection<String> transform = lssFromEnum(Transform.values());
51+
52+
@Tunable
53+
public ListSingleSelection<String> compress = lssFromEnum(Compress.values());
54+
55+
@Tunable
56+
public ListSingleSelection<String> operator = new ListSingleSelection<>("union", "intersection");
57+
58+
@Tunable(description="If true attempts to open a system PDF viewer on the exported file, does not work in headless mode.")
59+
public boolean openViewer = false;
60+
61+
62+
@Override
63+
public void run(TaskMonitor taskMonitor) {
64+
if(file == null)
65+
throw new IllegalArgumentException("file parameter required");
66+
67+
// validate network
68+
CyNetwork network = networkTunable.getNetwork();
69+
EnrichmentMap map = networkTunable.getEnrichmentMap();
70+
if(network == null || map == null)
71+
throw new IllegalArgumentException("network is not an EnrichmentMap network");
72+
73+
Transform transformValue = Transform.valueOf(transform.getSelectedValue());
74+
Compress compressValue = Compress.valueOf(compress.getSelectedValue());
75+
76+
List<String> genes = getGenes();
77+
78+
HeatMapTableModel model = new HeatMapTableModel(network, map, null, genes, transformValue, compressValue);
79+
ExportPDFTask exportTask = new ExportPDFTask(file, model, RankingOption.none(), showValues);
80+
81+
TaskIterator moreTasks = new TaskIterator(exportTask);
82+
if(openViewer)
83+
moreTasks.append(new OpenPDFViewerTask(file));
84+
85+
logOutput();
86+
insertTasksAfterCurrentTask(moreTasks);
87+
}
88+
89+
90+
private List<String> getGenes() {
91+
CyNetwork network = networkTunable.getNetwork();
92+
EnrichmentMap map = networkTunable.getEnrichmentMap();
93+
String prefix = map.getParams().getAttributePrefix();
94+
95+
List<CyNode> nodes;
96+
List<CyEdge> edges;
97+
if(selectedOnly) {
98+
nodes = CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true);
99+
edges = CyTableUtil.getEdgesInState(network, CyNetwork.SELECTED, true);
100+
} else {
101+
nodes = network.getNodeList();
102+
edges = network.getEdgeList();
103+
}
104+
105+
List<String> genes;
106+
if("union".equals(operator.getSelectedValue())) {
107+
genes = new ArrayList<>(HeatMapMediator.unionGenesets(network, nodes, edges, prefix));
108+
} else {
109+
genes = new ArrayList<>(HeatMapMediator.intersectionGenesets(network, nodes, edges, prefix));
110+
}
111+
genes.sort(Comparator.naturalOrder());
112+
113+
return genes;
114+
}
115+
116+
117+
private void logOutput() {
118+
Logger logger = LoggerFactory.getLogger(CyUserLog.NAME);
119+
logger.info("Exporting HeatMap PDF to: " + file.getAbsolutePath());
120+
logger.info("selectedOnly=" + selectedOnly);
121+
logger.info("transform=" + transform.getSelectedValue());
122+
logger.info("compress=" + compress.getSelectedValue());
123+
logger.info("operator=" + operator.getSelectedValue());
124+
logger.info("showValues=" + showValues);
125+
}
126+
127+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baderlab.csplugins.enrichmentmap.commands.tunables;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.cytoscape.work.util.ListSingleSelection;
7+
8+
public class CommandUtil {
9+
10+
public static ListSingleSelection<String> lssFromEnum(Enum<?> ... values) {
11+
List<String> names = new ArrayList<>(values.length);
12+
for(Enum<?> value : values) {
13+
names.add(value.name());
14+
}
15+
return new ListSingleSelection<>(names);
16+
}
17+
18+
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import java.awt.event.ActionEvent;
44
import java.io.File;
55
import java.util.Optional;
6+
import java.util.function.BooleanSupplier;
67
import java.util.function.Supplier;
78

89
import javax.swing.AbstractAction;
910
import javax.swing.JFrame;
1011
import javax.swing.JTable;
1112

13+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.HeatMapTableModel;
1214
import org.baderlab.csplugins.enrichmentmap.view.util.FileBrowser;
1315
import org.baderlab.csplugins.enrichmentmap.view.util.OpenPDFViewerTask;
1416
import org.cytoscape.util.swing.FileUtil;
@@ -29,29 +31,31 @@ public class ExportPDFAction extends AbstractAction {
2931

3032
private final JTable table;
3133
private final Supplier<RankingOption> rankingSupplier;
34+
private final BooleanSupplier showValuesSupplier;
3235

3336
public interface Factory {
34-
ExportPDFAction create(JTable table, Supplier<RankingOption> rankingSupplier);
37+
ExportPDFAction create(JTable table, Supplier<RankingOption> rankingSupplier, BooleanSupplier showValuesSupplier);
3538
}
3639

3740
@Inject
38-
public ExportPDFAction(@Assisted JTable table, @Assisted Supplier<RankingOption> rankingSupplier) {
41+
public ExportPDFAction(@Assisted JTable table, @Assisted Supplier<RankingOption> rankingSupplier, @Assisted BooleanSupplier showValuesSupplier) {
3942
super("Export to PDF");
4043
this.table = table;
4144
this.rankingSupplier = rankingSupplier;
45+
this.showValuesSupplier = showValuesSupplier;
4246
}
4347

4448
@Override
4549
public void actionPerformed(ActionEvent e) {
4650
Optional<File> file = FileBrowser.promptForPdfExport(fileUtil, jframeProvider.get());
4751
if(file.isPresent()) {
48-
ExportPDFTask exportPdfTask = new ExportPDFTask(file.get(), table, rankingSupplier.get());
52+
HeatMapTableModel model = (HeatMapTableModel) table.getModel();
53+
ExportPDFTask exportPdfTask = new ExportPDFTask(file.get(), model, rankingSupplier.get(), showValuesSupplier.getAsBoolean());
54+
exportPdfTask.setRowToModelRow(table::convertRowIndexToModel);
4955
Task openPdfViewerTask = new OpenPDFViewerTask(file.get());
5056
dialogTaskManager.execute(new TaskIterator(exportPdfTask, openPdfViewerTask));
5157
}
5258
}
5359

5460

5561
}
56-
57-

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import java.io.FileOutputStream;
66
import java.io.IOException;
77
import java.util.List;
8-
9-
import javax.swing.JTable;
8+
import java.util.function.IntUnaryOperator;
109

1110
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet;
1211
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.HeatMapCellRenderer;
@@ -35,22 +34,24 @@ public class ExportPDFTask extends AbstractTask {
3534
private static final BaseColor HEADER_BACKGROUND = BaseColor.LIGHT_GRAY;
3635

3736
private final File file;
38-
private final JTable jTable;
37+
private final HeatMapTableModel model;
3938
private final RankingOption ranking;
39+
private final boolean showValues;
40+
41+
private IntUnaryOperator rowToModel = x -> x;
4042

41-
public ExportPDFTask(File file, JTable jTable, RankingOption ranking) {
43+
44+
public ExportPDFTask(File file, HeatMapTableModel model, RankingOption ranking, boolean showValues) {
4245
this.file = file;
43-
this.jTable = jTable;
46+
this.model = model;
4447
this.ranking = ranking;
48+
this.showValues = showValues;
4549
}
4650

47-
private HeatMapTableModel getModel() {
48-
return (HeatMapTableModel) jTable.getModel();
51+
public void setRowToModelRow(IntUnaryOperator rowToModel) {
52+
this.rowToModel = rowToModel;
4953
}
5054

51-
private HeatMapCellRenderer getCellRenderer() {
52-
return (HeatMapCellRenderer) jTable.getDefaultRenderer(Double.class);
53-
}
5455

5556
@Override
5657
public void run(TaskMonitor taskMonitor) throws IOException, DocumentException {
@@ -75,7 +76,6 @@ public void run(TaskMonitor taskMonitor) throws IOException, DocumentException {
7576

7677

7778
private PdfPTable createTable() {
78-
HeatMapTableModel model = getModel();
7979
int colCount = model.getColumnCount();
8080

8181
PdfPTable table = new PdfPTable(colCount);
@@ -108,7 +108,7 @@ private PdfPTable createTable() {
108108
}
109109

110110
for(int row = 0; row < model.getRowCount(); row++) {
111-
int modelRow = jTable.convertRowIndexToModel(row);
111+
int modelRow = rowToModel.applyAsInt(row);
112112

113113
table.addCell(createGeneCell(modelRow));
114114
table.addCell(createDescriptionCell(modelRow));
@@ -123,13 +123,13 @@ private PdfPTable createTable() {
123123

124124

125125
private PdfPCell createGeneHeader() {
126-
PdfPCell cell = new PdfPCell(new Phrase(getModel().getColumnName(HeatMapTableModel.GENE_COL)));
126+
PdfPCell cell = new PdfPCell(new Phrase(model.getColumnName(HeatMapTableModel.GENE_COL)));
127127
cell.setBackgroundColor(HEADER_BACKGROUND);
128128
return cell;
129129
}
130130

131131
private PdfPCell createDesciptionHeader() {
132-
PdfPCell cell = new PdfPCell(new Phrase(getModel().getColumnName(HeatMapTableModel.DESC_COL)));
132+
PdfPCell cell = new PdfPCell(new Phrase(model.getColumnName(HeatMapTableModel.DESC_COL)));
133133
cell.setBackgroundColor(HEADER_BACKGROUND);
134134
return cell;
135135
}
@@ -155,7 +155,7 @@ private PdfPCell createDataSetHeader(EMDataSet dataset) {
155155
}
156156

157157
private PdfPCell createExpressionHeader(int col) {
158-
String text = getModel().getColumnName(col);
158+
String text = model.getColumnName(col);
159159
text = SwingUtil.abbreviate(text, 14);
160160
PdfPCell cell = new PdfPCell(new Phrase(text));
161161
cell.setRotation(90);
@@ -164,18 +164,18 @@ private PdfPCell createExpressionHeader(int col) {
164164
}
165165

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

171171
private PdfPCell createDescriptionCell(int modelRow) {
172-
String text = ExportTXTTask.getDescriptionText(getModel(), modelRow);
172+
String text = ExportTXTTask.getDescriptionText(model, modelRow);
173173
return new PdfPCell(new Phrase(text));
174174
}
175175

176176
private PdfPCell createRankCell(int modelRow) {
177177
PdfPCell cell = new PdfPCell();
178-
RankValue rankValue = (RankValue) getModel().getValueAt(modelRow, HeatMapTableModel.RANK_COL);
178+
RankValue rankValue = (RankValue) model.getValueAt(modelRow, HeatMapTableModel.RANK_COL);
179179
cell.setPhrase(new Phrase(ExportTXTTask.getRankText(HeatMapCellRenderer.getFormat(), rankValue)));
180180
if(rankValue.isSignificant()) {
181181
cell.setBackgroundColor(color(RankValueRenderer.SIGNIFICANT_COLOR));
@@ -185,10 +185,8 @@ private PdfPCell createRankCell(int modelRow) {
185185

186186

187187
private PdfPCell createExpressionCell(int row, int col) {
188-
double value = (double) getModel().getValueAt(row, col);
189-
HeatMapCellRenderer cellRenderer = getCellRenderer();
190-
Color color = cellRenderer.getColor(getModel(), col, value);
191-
boolean showValues = cellRenderer.getShowValues();
188+
double value = (double) model.getValueAt(row, col);
189+
Color color = HeatMapCellRenderer.getColor(model, col, value);
192190

193191
PdfPCell cell = new PdfPCell();
194192
cell.setBackgroundColor(color(color));

0 commit comments

Comments
 (0)