Skip to content

Commit c02543a

Browse files
committed
Add Command for exporting PNG. Code cleanup.
Refs #385
1 parent ad3ed03 commit c02543a

File tree

8 files changed

+120
-176
lines changed

8 files changed

+120
-176
lines changed

EnrichmentMapPlugin/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@
189189
<version>2.0</version>
190190
<scope>provided</scope>
191191
</dependency>
192+
<dependency>
193+
<groupId>org.glassfish.jersey.media</groupId>
194+
<artifactId>jersey-media-multipart</artifactId>
195+
<version>2.0</version>
196+
<scope>provided</scope>
197+
</dependency>
192198
<dependency>
193199
<groupId>io.swagger</groupId>
194200
<artifactId>swagger-annotations</artifactId>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.baderlab.csplugins.enrichmentmap.commands.EMBuildCommandTask;
77
import org.baderlab.csplugins.enrichmentmap.commands.EMGseaCommandTask;
88
import org.baderlab.csplugins.enrichmentmap.commands.ExportModelJsonCommandTask;
9+
import org.baderlab.csplugins.enrichmentmap.commands.ExportNetworkImageCommandTask;
910
import org.baderlab.csplugins.enrichmentmap.commands.ExportPDFCommandTask;
1011
import org.baderlab.csplugins.enrichmentmap.commands.MastermapCommandTask;
1112
import org.baderlab.csplugins.enrichmentmap.commands.MastermapListCommandTask;
@@ -101,5 +102,11 @@ public CommandTaskFactory provideExportPDF(Provider<ExportPDFCommandTask> taskPr
101102
String desc = "Exports the contents of the Heat Map panel to a PDF file.";
102103
return CommandTaskFactory.create("export pdf", desc, null, taskProvider);
103104
}
105+
106+
@ProvidesIntoSet
107+
public CommandTaskFactory provideExportImage(Provider<ExportNetworkImageCommandTask> taskProvider) {
108+
String desc = "Exports the network view to an image file in the users home directory.";
109+
return CommandTaskFactory.create("export png", desc, null, taskProvider);
110+
}
104111

105112
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import org.baderlab.csplugins.enrichmentmap.commands.tunables.MannWhitRanksTunableHandlerFactory;
1515
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMapManager;
1616
import org.baderlab.csplugins.enrichmentmap.model.io.SessionListener;
17-
import org.baderlab.csplugins.enrichmentmap.rest.BuildResource;
1817
import org.baderlab.csplugins.enrichmentmap.rest.ExpressionsResource;
1918
import org.baderlab.csplugins.enrichmentmap.rest.ModelResource;
19+
import org.baderlab.csplugins.enrichmentmap.rest.UploadFileResource;
2020
import org.baderlab.csplugins.enrichmentmap.style.ChartFactoryManager;
2121
import org.baderlab.csplugins.enrichmentmap.style.EMStyleBuilder;
2222
import org.baderlab.csplugins.enrichmentmap.style.charts.radialheatmap.RadialHeatMapChartFactory;
@@ -76,7 +76,7 @@ public void start(BundleContext bc) {
7676
// jax-rs (CyREST) resources
7777
registerService(bc, injector.getInstance(ExpressionsResource.class), ExpressionsResource.class);
7878
registerService(bc, injector.getInstance(ModelResource.class), ModelResource.class);
79-
registerService(bc, injector.getInstance(BuildResource.class), BuildResource.class);
79+
registerService(bc, injector.getInstance(UploadFileResource.class), UploadFileResource.class);
8080

8181
// CyProperty
8282
CyProperty<Properties> cyProperty = injector.getInstance(Key.get(new TypeLiteral<CyProperty<Properties>>(){}));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.baderlab.csplugins.enrichmentmap.commands;
2+
3+
import java.nio.file.Paths;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.UUID;
7+
8+
import org.cytoscape.command.CommandExecutorTaskFactory;
9+
import org.cytoscape.work.AbstractTask;
10+
import org.cytoscape.work.TaskIterator;
11+
import org.cytoscape.work.TaskMonitor;
12+
13+
import com.google.inject.Inject;
14+
15+
/**
16+
* There is already a command in Cytoscape to export a network image. This command
17+
* is different in that it always exports to the user's home directory, and it
18+
* never prompts for overwrite permission.
19+
*/
20+
public class ExportNetworkImageCommandTask extends AbstractTask {
21+
22+
@Inject private CommandExecutorTaskFactory commandTaskFactory;
23+
24+
@Override
25+
public void run(TaskMonitor taskMonitor) {
26+
String fileName = "em_network_" + UUID.randomUUID() + ".png";
27+
String homeDir = System.getProperty("user.home");
28+
String filePath = Paths.get(homeDir, fileName).toString();
29+
30+
Map<String,Object> args = new HashMap<>();
31+
args.put("options", "PNG (*.png)");
32+
args.put("outputFile", filePath);
33+
args.put("view", "CURRENT"); // required
34+
35+
TaskIterator commandTasks = commandTaskFactory.createTaskIterator("view", "export", args, null);
36+
insertTasksAfterCurrentTask(commandTasks);
37+
}
38+
39+
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/rest/BuildResource.java

Lines changed: 0 additions & 116 deletions
This file was deleted.

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/rest/EMJsonData.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/rest/EnrichmentEntry.java

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.baderlab.csplugins.enrichmentmap.rest;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.InputStream;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.attribute.FileAttribute;
9+
10+
import javax.ws.rs.Consumes;
11+
import javax.ws.rs.POST;
12+
import javax.ws.rs.Produces;
13+
import javax.ws.rs.core.MediaType;
14+
import javax.ws.rs.core.Response;
15+
16+
import org.glassfish.jersey.media.multipart.FormDataParam;
17+
18+
import io.swagger.annotations.Api;
19+
import io.swagger.annotations.ApiOperation;
20+
21+
22+
@Api(tags="Apps: EnrichmentMap")
23+
@javax.ws.rs.Path("/enrichmentmap/fileupload")
24+
public class UploadFileResource {
25+
26+
private Path tempDir = null;
27+
28+
29+
@POST
30+
@javax.ws.rs.Path("/")
31+
@Consumes(MediaType.MULTIPART_FORM_DATA)
32+
@Produces(MediaType.APPLICATION_JSON)
33+
@ApiOperation(value="Upload a file.",
34+
notes=""
35+
)
36+
public Response uploadFile(@FormDataParam("file") InputStream in) {
37+
try {
38+
synchronized(this) {
39+
if(tempDir == null) {
40+
tempDir = Files.createTempDirectory("em_fileupload_", new FileAttribute<?>[0]);
41+
}
42+
}
43+
44+
File tempFile = Files.createTempFile(tempDir, "em_", "", new FileAttribute<?>[0]).toFile();
45+
tempFile.deleteOnExit();
46+
47+
try(FileOutputStream out = new FileOutputStream(tempFile)) {
48+
int read = 0;
49+
byte[] bytes = new byte[1024];
50+
while((read = in.read(bytes)) != -1) {
51+
out.write(bytes, 0, read);
52+
}
53+
out.flush();
54+
}
55+
56+
String absPath = tempFile.getAbsolutePath();
57+
String response = String.format("{\"path\" : \"%s\"}", absPath);
58+
return Response.status(200).entity(response).build();
59+
60+
} catch(Exception e) {
61+
e.printStackTrace();
62+
return Response.serverError().build();
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
 (0)