Skip to content

Commit 2b3bf65

Browse files
committed
Export heat map to pdf. Closes #24.
1 parent d667dac commit 2b3bf65

File tree

7 files changed

+142
-165
lines changed

7 files changed

+142
-165
lines changed

EnrichmentMapPlugin/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@
116116
<artifactId>commons-math3</artifactId>
117117
<version>3.2</version>
118118
</dependency>
119+
<dependency>
120+
<groupId>com.lowagie</groupId>
121+
<artifactId>itext</artifactId>
122+
<version>2.0.8</version>
123+
</dependency>
119124

120125
<!-- testing -->
121126
<dependency>
@@ -177,7 +182,7 @@
177182
<Bundle-SymbolicName>${bundle.symbolicName}</Bundle-SymbolicName>
178183
<Bundle-Version>${project.version}</Bundle-Version>
179184
<Bundle-Activator>${bundle.namespace}.enrichmentmap.CyActivator</Bundle-Activator>
180-
<Embed-Dependency>colt;inline=true,commons-math3;inline=true</Embed-Dependency>
185+
<Embed-Dependency>colt;inline=true,commons-math3;inline=true,itext;inline=true</Embed-Dependency>
181186
<Private-Package>${bundle.namespace}.*</Private-Package>
182187
<Export-Package>org.mskcc.*;prefuse.*;version="${project.version}"</Export-Package>
183188
<Import-Package>*;resolution:=optional</Import-Package>
@@ -241,6 +246,8 @@
241246
<outputDirectory>${project.build.directory}</outputDirectory>
242247
<libs>
243248
<lib>${java.home}/lib/rt.jar</lib>
249+
<!-- itext has dependency on javax.crypto in jce.jar -->
250+
<lib>${java.home}/lib/jce.jar</lib>
244251
</libs>
245252
</configuration>
246253
</plugin>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.baderlab.csplugins.enrichmentmap.autoannotate.action.DisplayOptionsPanelAction;
1919
import org.baderlab.csplugins.enrichmentmap.commands.EnrichmentMapGSEACommandHandlerTaskFactory;
2020
import org.baderlab.csplugins.enrichmentmap.task.BuildEnrichmentMapTuneableTaskFactory;
21-
import org.baderlab.csplugins.enrichmentmap.task.CreatePublicationVisualStyleTaskRunner;
21+
import org.baderlab.csplugins.enrichmentmap.task.CreatePublicationVisualStyleTaskFactory;
2222
import org.baderlab.csplugins.enrichmentmap.task.EdgeWidthTableColumnTaskFactory;
2323
import org.baderlab.csplugins.enrichmentmap.view.BulkEMCreationPanel;
2424
import org.baderlab.csplugins.enrichmentmap.view.EnrichmentMapInputPanel;
@@ -122,8 +122,8 @@ public void start(BundleContext bc) {
122122
//create two instances of the heatmap panel
123123
HeatMapPanel heatMapPanel_node = new HeatMapPanel(true, cySwingApplicationRef, fileUtil, cyApplicationManagerRef, openBrowserRef,dialogTaskManager,streamUtil);
124124
HeatMapPanel heatMapPanel_edge = new HeatMapPanel(false, cySwingApplicationRef, fileUtil, cyApplicationManagerRef, openBrowserRef,dialogTaskManager,streamUtil);
125-
CreatePublicationVisualStyleTaskRunner taskRunner = new CreatePublicationVisualStyleTaskRunner(cyApplicationManagerRef, visualMappingManagerRef, visualStyleFactoryRef, eventHelper, dialogTaskManager);
126-
ParametersPanel paramsPanel = new ParametersPanel(openBrowserRef, cyApplicationManagerRef, taskRunner);
125+
CreatePublicationVisualStyleTaskFactory taskRunner = new CreatePublicationVisualStyleTaskFactory(cyApplicationManagerRef, visualMappingManagerRef, visualStyleFactoryRef, eventHelper);
126+
ParametersPanel paramsPanel = new ParametersPanel(openBrowserRef, cyApplicationManagerRef, dialogTaskManager, taskRunner);
127127

128128
//Get an instance of EM manager
129129
EnrichmentMapManager manager = EnrichmentMapManager.getInstance();

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/heatmap/HeatMapExporter.java

Lines changed: 0 additions & 103 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.baderlab.csplugins.enrichmentmap.heatmap;
2+
3+
import java.awt.Graphics2D;
4+
import java.io.File;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
8+
import javax.swing.JTable;
9+
import javax.swing.table.JTableHeader;
10+
11+
import org.cytoscape.work.AbstractTask;
12+
import org.cytoscape.work.TaskMonitor;
13+
14+
import com.lowagie.text.Document;
15+
import com.lowagie.text.DocumentException;
16+
import com.lowagie.text.Rectangle;
17+
import com.lowagie.text.pdf.PdfContentByte;
18+
import com.lowagie.text.pdf.PdfWriter;
19+
20+
21+
public class HeatMapExporterTask extends AbstractTask {
22+
23+
final JTable jtable1;
24+
final JTableHeader header;
25+
final File file;
26+
27+
28+
public HeatMapExporterTask(JTable jtable1, JTableHeader header, File file) {
29+
this.jtable1 = jtable1;
30+
this.header = header;
31+
this.file = file;
32+
}
33+
34+
@Override
35+
public void run(TaskMonitor taskMonitor) throws Exception {
36+
taskMonitor.setTitle("EnrichmentMap");
37+
taskMonitor.setStatusMessage("Exporting Heat Map");
38+
39+
int headerHeight = header.getHeight();
40+
Rectangle pageSize = new Rectangle(jtable1.getWidth(), jtable1.getHeight() + (headerHeight * 2));
41+
Document document = new Document(pageSize);
42+
43+
try(FileOutputStream stream = new FileOutputStream(file)) {
44+
PdfWriter writer = PdfWriter.getInstance(document, stream);
45+
try {
46+
document.open();
47+
48+
PdfContentByte canvas = writer.getDirectContent();
49+
float width = pageSize.getWidth();
50+
float height = pageSize.getHeight();
51+
52+
//print the legend
53+
//PdfTemplate legend = cb.createTemplate(width,height_header);
54+
//Graphics2D g = legend.createGraphics(width, height_header);
55+
//double imageScale = width / ((double) legendpanel.getWidth());
56+
//g.scale(imageScale, imageScale);
57+
//legendpanel.paint(g);
58+
//g.dispose();
59+
//cb.addTemplate(legend, 0, (pageSize.getHeight()-25) );
60+
61+
Graphics2D g1 = canvas.createGraphics(width, headerHeight);
62+
header.paint(g1);
63+
g1.dispose();
64+
65+
taskMonitor.setProgress(0.25);
66+
67+
Graphics2D g2 = canvas.createGraphics(width, height);
68+
jtable1.paint(g2);
69+
g2.dispose();
70+
71+
taskMonitor.setProgress(0.5);
72+
73+
Graphics2D g3 = canvas.createGraphics(width, headerHeight);
74+
header.paint(g3);
75+
g3.dispose();
76+
77+
taskMonitor.setProgress(0.75);
78+
79+
} finally {
80+
document.close();
81+
writer.close();
82+
taskMonitor.setProgress(0.99);
83+
}
84+
85+
} catch (DocumentException exp) {
86+
throw new IOException(exp);
87+
}
88+
}
89+
90+
}
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
11
package org.baderlab.csplugins.enrichmentmap.task;
22

3-
import java.awt.event.ActionEvent;
4-
import java.awt.event.ActionListener;
5-
63
import org.cytoscape.application.CyApplicationManager;
74
import org.cytoscape.event.CyEventHelper;
85
import org.cytoscape.view.vizmap.VisualMappingManager;
96
import org.cytoscape.view.vizmap.VisualStyleFactory;
7+
import org.cytoscape.work.TaskFactory;
108
import org.cytoscape.work.TaskIterator;
11-
import org.cytoscape.work.TaskManager;
129

13-
public class CreatePublicationVisualStyleTaskRunner implements ActionListener {
10+
public class CreatePublicationVisualStyleTaskFactory implements TaskFactory {
1411

1512
private final CyApplicationManager applicationManager;
1613
private final VisualMappingManager visualMappingManager;
1714
private final VisualStyleFactory visualStyleFactory;
1815
private final CyEventHelper eventHelper;
1916

20-
private final TaskManager<?,?> taskManager;
2117

22-
public CreatePublicationVisualStyleTaskRunner(
18+
public CreatePublicationVisualStyleTaskFactory(
2319
CyApplicationManager applicationManager,
2420
VisualMappingManager visualMappingManager,
2521
VisualStyleFactory visualStyleFactory,
26-
CyEventHelper eventHelper,
27-
TaskManager<?,?> taskManager) {
22+
CyEventHelper eventHelper) {
2823

2924
this.applicationManager = applicationManager;
3025
this.visualMappingManager = visualMappingManager;
3126
this.visualStyleFactory = visualStyleFactory;
3227
this.eventHelper = eventHelper;
33-
this.taskManager = taskManager;
3428
}
35-
36-
public void run() {
37-
taskManager.execute(new TaskIterator(new CreatePublicationVisualStyleTask(applicationManager, visualMappingManager, visualStyleFactory, eventHelper)));
29+
30+
@Override
31+
public TaskIterator createTaskIterator() {
32+
return new TaskIterator(new CreatePublicationVisualStyleTask(applicationManager, visualMappingManager, visualStyleFactory, eventHelper));
3833
}
3934

4035
@Override
41-
public void actionPerformed(ActionEvent e) {
42-
run();
36+
public boolean isReady() {
37+
return true;
4338
}
4439
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/HeatMapPanel.java

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import java.util.HashMap;
6969
import java.util.HashSet;
7070
import java.util.Iterator;
71+
import java.util.List;
7172
import java.util.Map;
7273

7374
import javax.swing.AbstractAction;
@@ -94,6 +95,7 @@
9495
import org.baderlab.csplugins.enrichmentmap.heatmap.ColumnHeaderVerticalRenderer;
9596
import org.baderlab.csplugins.enrichmentmap.heatmap.ExpressionTableValue;
9697
import org.baderlab.csplugins.enrichmentmap.heatmap.HeatMapActionListener;
98+
import org.baderlab.csplugins.enrichmentmap.heatmap.HeatMapExporterTask;
9799
import org.baderlab.csplugins.enrichmentmap.heatmap.HeatMapParameters;
98100
import org.baderlab.csplugins.enrichmentmap.heatmap.HeatMapParameters.Sort;
99101
import org.baderlab.csplugins.enrichmentmap.heatmap.HeatMapTableActionListener;
@@ -123,6 +125,7 @@
123125
import org.cytoscape.util.swing.FileChooserFilter;
124126
import org.cytoscape.util.swing.FileUtil;
125127
import org.cytoscape.util.swing.OpenBrowser;
128+
import org.cytoscape.work.TaskIterator;
126129
import org.cytoscape.work.swing.DialogTaskManager;
127130
import org.mskcc.colorgradient.ColorGradientRange;
128131
import org.mskcc.colorgradient.ColorGradientTheme;
@@ -1572,44 +1575,21 @@ private void saveExpressionSetActionPerformed(ActionEvent evt) {
15721575
}
15731576

15741577
private void exportExpressionSetActionPerformed(ActionEvent evt) {
1575-
1576-
JOptionPane.showMessageDialog(this, "PDF export currently not available");
1577-
1578-
/*
1579-
* FileChooserFilter filter_pdf = new FileChooserFilter("pdf Files"
1580-
* ,"pdf" );
1581-
*
1582-
* //the set of filter (required by the file util method
1583-
* ArrayList<FileChooserFilter> all_filters = new
1584-
* ArrayList<FileChooserFilter>(); all_filters.add(filter_pdf);
1585-
*
1586-
*
1587-
* java.io.File file = fileUtil.getFile(application.getJFrame(),
1588-
* "Export Heatmap as pdf File", FileUtil.SAVE,all_filters); if (file !=
1589-
* null && file.toString() != null) { String fileName = file.toString();
1590-
* if (!fileName.endsWith(".pdf")) { fileName += ".pdf"; file = new
1591-
* File(fileName); }
1592-
*
1593-
* int response = JOptionPane.OK_OPTION; if(file.exists()) response =
1594-
* JOptionPane.showConfirmDialog(this,
1595-
* "The file already exists. Would you like to overwrite it?");
1596-
* if(response == JOptionPane.NO_OPTION || response ==
1597-
* JOptionPane.CANCEL_OPTION ){
1598-
*
1599-
* } else if(response == JOptionPane.YES_OPTION || response ==
1600-
* JOptionPane.OK_OPTION){ try{ FileOutputStream output = new
1601-
* FileOutputStream(file);
1602-
*
1603-
* //TODO:add heatmap exporter /* HeatMapExporter exporter = new
1604-
* HeatMapExporter();
1605-
* exporter.export(this.getNorthPanel(),this.getjTable1(),this.
1606-
* getTableHeader(), output) ; output.flush(); output.close();
1607-
* JOptionPane.showMessageDialog(this, "File " + fileName + " saved.");
1608-
*/
1609-
/*
1610-
* }catch(IOException e){ JOptionPane.showMessageDialog(this,
1611-
* "unable to write to file " + fileName); } } }
1612-
*/
1578+
//JOptionPane.showMessageDialog(this, "PDF export currently not available");
1579+
1580+
List<FileChooserFilter> filter = Collections.singletonList(new FileChooserFilter("pdf Files", "pdf"));
1581+
File file = fileUtil.getFile(application.getJFrame(), "Export Heatmap as PDF File", FileUtil.SAVE, filter);
1582+
1583+
if (file != null && file.toString() != null) {
1584+
String fileName = file.toString();
1585+
if (!fileName.endsWith(".pdf")) {
1586+
fileName += ".pdf";
1587+
file = new File(fileName);
1588+
}
1589+
1590+
HeatMapExporterTask task = new HeatMapExporterTask(getjTable1(), getTableHeader(), file);
1591+
dialogTaskMonitor.execute(new TaskIterator(task));
1592+
}
16131593
}
16141594

16151595
/**

0 commit comments

Comments
 (0)