Skip to content

Commit 8ee1af3

Browse files
committed
Save very large EM networks to a file in the session.
Refs #482
1 parent fc119d1 commit 8ee1af3

File tree

4 files changed

+233
-54
lines changed

4 files changed

+233
-54
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
public class CyActivator extends AbstractCyActivator {
5353

5454
public static final String APP_NAME = "EnrichmentMap";
55+
public static final String SESSION_DATA_FOLDER = "EnrichmentMap.Data.1";
5556

5657
private Injector injector;
5758

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/model/io/ModelSerializer.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package org.baderlab.csplugins.enrichmentmap.model.io;
22

33
import java.awt.Color;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.io.FileWriter;
7+
import java.io.IOException;
8+
import java.io.Reader;
9+
import java.io.StringReader;
10+
import java.io.StringWriter;
411
import java.lang.reflect.Type;
512
import java.nio.file.Path;
613
import java.nio.file.Paths;
@@ -46,6 +53,18 @@ public static String serialize(EnrichmentMap map) {
4653
}
4754

4855
public static String serialize(EnrichmentMap map, boolean pretty) {
56+
var writer = new StringWriter();
57+
serialize(map, pretty, writer);
58+
return writer.toString();
59+
}
60+
61+
public static void serialize(EnrichmentMap map, File file) throws IOException {
62+
try(var writer = new FileWriter(file)) {
63+
serialize(map, true, writer);
64+
}
65+
}
66+
67+
private static void serialize(EnrichmentMap map, boolean pretty, Appendable writer) {
4968
// When saving to the session file DO NOT enable pretty printing, the Cytoscape
5069
// CSV parser is very slow for multi-line text
5170
GsonBuilder builder = new GsonBuilder()
@@ -54,16 +73,28 @@ public static String serialize(EnrichmentMap map, boolean pretty) {
5473
.registerTypeHierarchyAdapter(Color.class, new ColorAdapter())
5574
.serializeSpecialFloatingPointValues(); // really important, we allow NaN in expression files
5675

57-
if (pretty) {
76+
if(pretty)
5877
builder.setPrettyPrinting();
59-
}
6078

6179
Gson gson = builder.create();
62-
String json = gson.toJson(map);
63-
return json;
80+
gson.toJson(map, writer);
81+
82+
}
83+
84+
public static EnrichmentMap deserialize(File file) throws IOException {
85+
try(var reader = new FileReader(file)) {
86+
return deserialize(reader);
87+
}
6488
}
6589

6690
public static EnrichmentMap deserialize(String json) {
91+
try(var reader = new StringReader(json)) {
92+
return deserialize(reader);
93+
}
94+
}
95+
96+
97+
private static EnrichmentMap deserialize(Reader reader) {
6798
Type immutableIntSetType = new TypeToken<ImmutableSet<Integer>>() {}.getType();
6899

69100
Gson gson = new GsonBuilder()
@@ -74,7 +105,7 @@ public static EnrichmentMap deserialize(String json) {
74105
.registerTypeAdapter(immutableIntSetType, new ImmutableIntSetAdapter()).create();
75106

76107
try {
77-
EnrichmentMap map = gson.fromJson(json, EnrichmentMap.class);
108+
EnrichmentMap map = gson.fromJson(reader, EnrichmentMap.class);
78109
for (EMDataSet dataset : map.getDataSetList()) {
79110
dataset.setParent(map);
80111
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/model/io/SessionListener.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.baderlab.csplugins.enrichmentmap.model.io;
22

3+
import java.io.File;
4+
import java.util.List;
5+
36
import javax.swing.JFrame;
47
import javax.swing.JOptionPane;
58
import javax.swing.SwingUtilities;
@@ -44,7 +47,14 @@ public void handleEvent(SessionLoadedEvent event) {
4447
@Override
4548
public void handleEvent(SessionAboutToBeSavedEvent event) {
4649
if(sessionIsActuallySaving()) {
47-
save();
50+
List<File> files = save();
51+
if(files != null && !files.isEmpty()) {
52+
try {
53+
event.addAppFiles(CyActivator.SESSION_DATA_FOLDER, files);
54+
} catch (Exception e) {
55+
e.printStackTrace();
56+
}
57+
}
4858
}
4959
}
5060

@@ -60,11 +70,12 @@ private boolean sessionIsActuallySaving() {
6070
return false;
6171
}
6272

63-
public void save() {
64-
modelIO.saveModel();
73+
public List<File> save() {
6574
if(!headless) {
6675
viewIO.saveView();
6776
}
77+
List<File> files = modelIO.saveModel();
78+
return files;
6879
}
6980

7081
public void restore(CySession session) {

0 commit comments

Comments
 (0)