Skip to content

Commit 1347610

Browse files
committed
Record edge SUIDs in AbstractDataSet
1 parent a10cce7 commit 1347610

File tree

13 files changed

+162
-99
lines changed

13 files changed

+162
-99
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/model/AbstractDataSet.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package org.baderlab.csplugins.enrichmentmap.model;
22

33
import java.text.Collator;
4-
import java.util.HashMap;
5-
import java.util.Map;
4+
import java.util.Collections;
5+
import java.util.HashSet;
6+
import java.util.Set;
67

78
public abstract class AbstractDataSet implements Comparable<AbstractDataSet> {
89

910
private final String name;
10-
private final Map<String, Long> nodeSuids = new HashMap<>();
11+
private final Set<Long> nodeSuids = new HashSet<>();
12+
private final Set<Long> edgeSuids = new HashSet<>();
13+
1114
/** EnrichmentMap only creates nodes for these genes. */
1215
private SetOfGeneSets geneSetsOfInterest = new SetOfGeneSets();
1316

@@ -22,22 +25,22 @@ public String getName() {
2225
return name;
2326
}
2427

25-
public Map<String, Long> getNodeSuids() {
28+
public Set<Long> getNodeSuids() {
2629
synchronized (lock) {
27-
return new HashMap<>(nodeSuids);
30+
return Collections.unmodifiableSet(nodeSuids);
2831
}
2932
}
3033

31-
public void setNodeSuids(Map<String, Long> newValue) {
34+
public void setNodeSuids(Set<Long> newValue) {
3235
synchronized (lock) {
3336
nodeSuids.clear();
34-
nodeSuids.putAll(newValue);
37+
nodeSuids.addAll(newValue);
3538
}
3639
}
3740

38-
public void addNodeSuid(String key, Long suid) {
41+
public void addNodeSuid(Long suid) {
3942
synchronized (lock) {
40-
nodeSuids.put(key, suid);
43+
nodeSuids.add(suid);
4144
}
4245
}
4346

@@ -47,6 +50,31 @@ public void clearNodeSuids() {
4750
}
4851
}
4952

53+
public Set<Long> getEdgeSuids() {
54+
synchronized (lock) {
55+
return Collections.unmodifiableSet(edgeSuids);
56+
}
57+
}
58+
59+
public void setEdgeSuids(Set<Long> newValue) {
60+
synchronized (lock) {
61+
edgeSuids.clear();
62+
edgeSuids.addAll(newValue);
63+
}
64+
}
65+
66+
public void addEdgeSuid(Long suid) {
67+
synchronized (lock) {
68+
edgeSuids.add(suid);
69+
}
70+
}
71+
72+
public void clearEdgeSuids() {
73+
synchronized (lock) {
74+
edgeSuids.clear();
75+
}
76+
}
77+
5078
public SetOfGeneSets getGeneSetsOfInterest() {
5179
return geneSetsOfInterest;
5280
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/model/EnrichmentMap.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Map;
1414
import java.util.Optional;
1515
import java.util.Set;
16+
import java.util.function.Function;
1617
import java.util.stream.Collectors;
1718

1819
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet.Method;
@@ -359,43 +360,64 @@ public void setNetworkID(long networkID) {
359360
this.networkID = networkID;
360361
}
361362

363+
public static Set<Long> getNodesUnion(Collection<AbstractDataSet> dataSets) {
364+
return getUnion(dataSets, AbstractDataSet::getNodeSuids);
365+
}
366+
367+
public static Set<Long> getNodesIntersection(Collection<? extends AbstractDataSet> dataSets) {
368+
return getIntersection(dataSets, AbstractDataSet::getNodeSuids);
369+
}
370+
362371
/**
363-
* Returns the node SUIDs for all the gene-sets in the given collection of DataSets.
372+
* Returns the SUIDs for all the gene-sets in the given collection of DataSets.
364373
* Each returned gene-set is contained in at least one of the given DataSets.
374+
*
375+
* Note, this will only return distinct edges, not compound edges.
365376
*/
366-
public static Set<Long> getNodesUnion(Collection<AbstractDataSet> dataSets) {
377+
public static Set<Long> getEdgesUnion(Collection<AbstractDataSet> dataSets) {
378+
return getUnion(dataSets, AbstractDataSet::getEdgeSuids);
379+
}
380+
381+
/**
382+
* Returns the SUIDs for all the gene-sets in the given collection of DataSets.
383+
* Each returned gene-set is contained all of the given DataSets.
384+
*
385+
* Note, this will only return distinct edges, not compound edges.
386+
*/
387+
public static Set<Long> getEdgesIntersection(Collection<? extends AbstractDataSet> dataSets) {
388+
return getIntersection(dataSets, AbstractDataSet::getEdgeSuids);
389+
}
390+
391+
private static Set<Long> getUnion(Collection<? extends AbstractDataSet> dataSets, Function<AbstractDataSet,Set<Long>> suidSupplier) {
367392
if (dataSets.isEmpty())
368393
return Collections.emptySet();
369394

370395
Set<Long> suids = new HashSet<>();
371396

372397
for (AbstractDataSet ds : dataSets) {
373-
suids.addAll(ds.getNodeSuids().values());
398+
suids.addAll(suidSupplier.apply(ds));
374399
}
375400

376401
return suids;
377402
}
378403

379-
/**
380-
* Returns the node SUIDs for all the gene-sets in the given collection of DataSets.
381-
* Each returned gene-set is contained all of the given DataSets.
382-
*/
383-
public static Set<Long> getNodesIntersection(Collection<EMDataSet> queryDatasets) {
384-
if (queryDatasets.isEmpty())
404+
private static Set<Long> getIntersection(Collection<? extends AbstractDataSet> dataSets, Function<AbstractDataSet,Set<Long>> suidSupplier) {
405+
if (dataSets.isEmpty())
385406
return Collections.emptySet();
386407

387-
Iterator<EMDataSet> iter = queryDatasets.iterator();
388-
EMDataSet first = iter.next();
389-
Set<Long> suids = new HashSet<>(first.getNodeSuids().values());
408+
Iterator<? extends AbstractDataSet> iter = dataSets.iterator();
409+
AbstractDataSet first = iter.next();
410+
Set<Long> suids = new HashSet<>(suidSupplier.apply(first));
390411

391412
while (iter.hasNext()) {
392-
EMDataSet dataset = iter.next();
393-
suids.retainAll(dataset.getNodeSuids().values());
413+
AbstractDataSet dataset = iter.next();
414+
suids.retainAll(suidSupplier.apply(dataset));
394415
}
395416

396417
return suids;
397418
}
398419

420+
399421
public Set<String> getAllRankNames() {
400422
Set<String> allRankNames = new HashSet<>();
401423

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/model/SimilarityKey.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ public class SimilarityKey {
77
private final String geneSet1;
88
private final String geneSet2;
99
private final String interaction;
10-
// a set of zero indicates a compound edge
11-
private final int set;
10+
private final String name;
1211

1312

14-
public SimilarityKey(String geneSet1, String geneSet2, String interaction, int set) {
13+
public SimilarityKey(String geneSet1, String geneSet2, String interaction, String name) {
1514
Objects.requireNonNull(geneSet1);
1615
Objects.requireNonNull(interaction);
1716
Objects.requireNonNull(geneSet2);
1817
this.geneSet1 = geneSet1;
1918
this.geneSet2 = geneSet2;
2019
this.interaction = interaction;
21-
this.set = set;
20+
this.name = name;
2221
}
2322

2423
public String getGeneSet1() {
@@ -33,27 +32,30 @@ public String getInteraction() {
3332
return interaction;
3433
}
3534

36-
public int getSet() {
37-
return set;
35+
public boolean isCompound() {
36+
return name == null;
3837
}
3938

40-
public boolean isCompound() {
41-
return set == 0;
39+
public String getName() {
40+
return name;
4241
}
4342

4443
@Override
4544
public int hashCode() {
4645
// add the hash codes from the genesets so that we get the same hash code regardless of the order
47-
return Objects.hash(geneSet1.hashCode() + geneSet2.hashCode(), interaction, set);
46+
return Objects.hash(geneSet1.hashCode() + geneSet2.hashCode(), interaction, name);
4847
}
4948

5049
@Override
5150
public boolean equals(Object o) {
5251
if(!(o instanceof SimilarityKey))
5352
return false;
5453
SimilarityKey other = (SimilarityKey)o;
55-
56-
if(set != other.set)
54+
if(name != null && other.name == null)
55+
return false;
56+
if(name == null && other.name != null)
57+
return false;
58+
if(name != null && other.name != null && !name.equals(other.name))
5759
return false;
5860
if(!interaction.equals(other.interaction))
5961
return false;
@@ -65,10 +67,10 @@ public boolean equals(Object o) {
6567

6668
@Override
6769
public String toString() {
68-
if(set == 0)
70+
if(isCompound())
6971
return String.format("%s (%s) %s", geneSet1, interaction, geneSet2);
7072
else
71-
return String.format("%s (%s_set%d) %s", geneSet1, interaction, set, geneSet2);
73+
return String.format("%s (%s_set%s) %s", geneSet1, interaction, name, geneSet2);
7274
}
7375

7476
}

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import java.awt.event.ActionEvent;
44
import java.util.ArrayList;
55
import java.util.Arrays;
6-
import java.util.HashMap;
6+
import java.util.HashSet;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.Set;
910

1011
import org.baderlab.csplugins.enrichmentmap.ApplicationModule.Headless;
1112
import org.baderlab.csplugins.enrichmentmap.CyActivator;
@@ -17,6 +18,8 @@
1718
import org.baderlab.csplugins.enrichmentmap.view.heatmap.HeatMapMediator;
1819
import org.cytoscape.application.swing.AbstractCyAction;
1920
import org.cytoscape.model.CyColumn;
21+
import org.cytoscape.model.CyEdge;
22+
import org.cytoscape.model.CyIdentifiable;
2023
import org.cytoscape.model.CyNetwork;
2124
import org.cytoscape.model.CyNetworkManager;
2225
import org.cytoscape.model.CyNode;
@@ -160,7 +163,7 @@ private boolean restoreModelFromTables(CySession session) {
160163
if(em != null) {
161164
em.setServiceRegistrar(serviceRegistrar);
162165
em.setNetworkID(network.getSUID());
163-
updateNodeSuids(em, session);
166+
updateSuids(em, session);
164167
emManager.registerEnrichmentMap(em);
165168
sessionHasEM = true;
166169
}
@@ -171,24 +174,24 @@ private boolean restoreModelFromTables(CySession session) {
171174
return sessionHasEM;
172175
}
173176

174-
private void updateNodeSuids(EnrichmentMap map, CySession session) {
175-
for (EMDataSet ds : map.getDataSetList()) {
176-
Map<String, Long> oldSuids = ds.getNodeSuids();
177-
Map<String, Long> newSuids = new HashMap<>();
178-
179-
for (String key : oldSuids.keySet()) {
180-
Long suid = oldSuids.get(key);
181-
182-
if (session != null) {
183-
// If we are loading from a session file then we need to re-map the ids
184-
CyNode node = session.getObject(suid, CyNode.class);
185-
suid = node.getSUID();
186-
}
187-
188-
newSuids.put(key, suid);
177+
private static Set<Long> mapSuids(Set<Long> oldSuids, CySession session, Class<? extends CyIdentifiable> type) {
178+
Set<Long> newSuids = new HashSet<>();
179+
180+
for (Long suid : oldSuids) {
181+
if (session != null) {
182+
// If we are loading from a session file then we need to re-map the ids
183+
CyIdentifiable obj = session.getObject(suid, type);
184+
suid = obj.getSUID();
189185
}
190-
191-
ds.setNodeSuids(newSuids);
186+
newSuids.add(suid);
187+
}
188+
return newSuids;
189+
}
190+
191+
private void updateSuids(EnrichmentMap map, CySession session) {
192+
for(EMDataSet ds : map.getDataSetList()) {
193+
ds.setNodeSuids(mapSuids(ds.getNodeSuids(), session, CyNode.class));
194+
ds.setEdgeSuids(mapSuids(ds.getEdgeSuids(), session, CyEdge.class));
192195
}
193196
}
194197

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/style/EMStyleBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ private void setNodeColors(VisualStyle vs, EMStyleOptions options) {
374374
CyNetwork net = netView.getModel();
375375

376376
for (EMSignatureDataSet sds : signatureDataSets) {
377-
for (Long suid : sds.getNodeSuids().values()) {
377+
for (Long suid : sds.getNodeSuids()) {
378378
CyNode node = net.getNode(suid);
379379

380380
if (node != null) {

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/task/ComputeSimilarityTaskParallel.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ private Map<SimilarityKey,GenesetSimilarity> startComputeSimilarities(TaskMonito
8787
continue; //don't compare two identical gene sets
8888

8989
if(distinct) {
90-
int i = 1;
9190
for(EMDataSet dataset : dataSets) {
92-
SimilarityKey key = new SimilarityKey(geneset1Name, geneset2Name, edgeType, i++);
91+
SimilarityKey key = new SimilarityKey(geneset1Name, geneset2Name, edgeType, dataset.getName());
9392

9493
if(!similarities.containsKey(key)) {
9594
Map<String,GeneSet> genesets = dataset.getGeneSetsOfInterest().getGeneSets();
@@ -108,7 +107,7 @@ private Map<SimilarityKey,GenesetSimilarity> startComputeSimilarities(TaskMonito
108107
}
109108

110109
if(compound) {
111-
SimilarityKey key = new SimilarityKey(geneset1Name, geneset2Name, edgeType, 0);
110+
SimilarityKey key = new SimilarityKey(geneset1Name, geneset2Name, edgeType, null);
112111

113112
if(!similarities.containsKey(key)) {
114113
Set<Integer> geneset1 = unionedGenesets.get(geneset1Name);

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/task/CreateDiseaseSignatureTask.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public void buildDiseaseSignature(TaskMonitor taskMonitor) {
330330
continue;
331331

332332
boolean passedCutoff = passesCutoff(edgeName);
333-
createEdge(edgeName, currentNetwork, currentView, prefix, edgeTable, nodeTable, passedCutoff);
333+
createEdge(edgeName, currentNetwork, currentView, prefix, edgeTable, nodeTable, passedCutoff, sigDataSet);
334334
}
335335

336336
widthFunctionProvider.get().setEdgeWidths(currentNetwork, prefix, taskMonitor);
@@ -395,7 +395,7 @@ private boolean createHubNode(
395395
if (hubNode == null) {
396396
hubNode = network.addNode();
397397
taskResult.addNewNode(hubNode);
398-
sigDataSet.addNodeSuid(hubName, hubNode.getSUID());
398+
sigDataSet.addNodeSuid(hubNode.getSUID());
399399
created = true;
400400
}
401401

@@ -452,7 +452,7 @@ private boolean createHubNode(
452452
* returned, if the edge had to be created it will not be returned.
453453
*/
454454
private void createEdge(String edgeName, CyNetwork network, CyNetworkView netView, String prefix, CyTable edgeTable,
455-
CyTable nodeTable, boolean passedCutoff) {
455+
CyTable nodeTable, boolean passedCutoff, EMSignatureDataSet sigDataSet) {
456456
CyEdge edge = NetworkUtil.getEdgeWithValue(network, edgeTable, CyNetwork.NAME, edgeName);
457457
GenesetSimilarity genesetSimilarity = geneSetSimilarities.get(edgeName);
458458

@@ -467,6 +467,7 @@ private void createEdge(String edgeName, CyNetwork network, CyNetworkView netVie
467467
return;
468468

469469
edge = network.addEdge(hubNode, geneSet, false);
470+
sigDataSet.addEdgeSuid(edge.getSUID());
470471
taskResult.addNewEdge(edge);
471472
} else {
472473
return; // edge does not exist and does not pass cutoff, do nothing

0 commit comments

Comments
 (0)