Skip to content

Commit d1816ad

Browse files
committed
Fix errors in test suite caused by unpredictable iteration order of
Sets.
1 parent dd32754 commit d1816ad

File tree

6 files changed

+207
-71
lines changed

6 files changed

+207
-71
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package org.baderlab.csplugins.enrichmentmap;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.cytoscape.model.CyEdge;
7+
8+
/**
9+
* Basically just a map of edge name to edge, but it ignores the directionality
10+
* of the edge when looking up by name.
11+
*
12+
*/
13+
public class EdgeSimilarities {
14+
15+
private final Map<Key,CyEdge> edges = new HashMap<>();
16+
17+
public void addEdge(CyEdge edge, String name1, String interaction, String name2) {
18+
edges.put(new Key(name1, name2, interaction), edge);
19+
}
20+
21+
public void addEdge(String fullName, CyEdge edge) {
22+
edges.put(Key.parse(fullName), edge);
23+
}
24+
25+
public boolean containsEdge(String name1, String interaction, String name2) {
26+
Key key = new Key(name1, name2, interaction);
27+
return edges.containsKey(key) || edges.containsKey(key.flip());
28+
}
29+
30+
public boolean containsEdge(String name) {
31+
Key key = Key.parse(name);
32+
return edges.containsKey(key) || edges.containsKey(key.flip());
33+
}
34+
35+
public CyEdge getEdge(String name1, String interaction, String name2) {
36+
return getEdge(new Key(name1, name2, interaction));
37+
}
38+
39+
public CyEdge getEdge(String name) {
40+
return getEdge(Key.parse(name));
41+
}
42+
43+
private CyEdge getEdge(Key key) {
44+
CyEdge edge = edges.get(key);
45+
if(edge == null) {
46+
edge = edges.get(key.flip());
47+
}
48+
return edge;
49+
}
50+
51+
public int size() {
52+
return edges.size();
53+
}
54+
55+
public boolean isEmpty() {
56+
return edges.isEmpty();
57+
}
58+
59+
private static class Key {
60+
final String name1;
61+
final String name2;
62+
final String interaction;
63+
64+
Key(String name1, String name2, String interaction) {
65+
this.name1 = name1;
66+
this.name2 = name2;
67+
this.interaction = interaction;
68+
}
69+
70+
Key flip() {
71+
return new Key(name2, name1, interaction);
72+
}
73+
74+
static Key parse(String name) {
75+
int open = name.indexOf("(");
76+
int close = name.indexOf(")", open);
77+
String name1 = name.substring(0, open).trim();
78+
String name2 = name.substring(close+1, name.length()).trim();
79+
String interaction = name.substring(open+1, close);
80+
return new Key(name1, name2, interaction);
81+
}
82+
83+
@Override
84+
public int hashCode() {
85+
final int prime = 31;
86+
int result = 1;
87+
result = prime * result + ((interaction == null) ? 0 : interaction.hashCode());
88+
result = prime * result + ((name1 == null) ? 0 : name1.hashCode());
89+
result = prime * result + ((name2 == null) ? 0 : name2.hashCode());
90+
return result;
91+
}
92+
93+
@Override
94+
public boolean equals(Object obj) {
95+
if (this == obj)
96+
return true;
97+
if (obj == null)
98+
return false;
99+
if (getClass() != obj.getClass())
100+
return false;
101+
Key other = (Key) obj;
102+
if (interaction == null) {
103+
if (other.interaction != null)
104+
return false;
105+
} else if (!interaction.equals(other.interaction))
106+
return false;
107+
if (name1 == null) {
108+
if (other.name1 != null)
109+
return false;
110+
} else if (!name1.equals(other.name1))
111+
return false;
112+
if (name2 == null) {
113+
if (other.name2 != null)
114+
return false;
115+
} else if (!name2.equals(other.name2))
116+
return false;
117+
return true;
118+
}
119+
}
120+
121+
122+
123+
}

EnrichmentMapPlugin/src/test/java/org/baderlab/csplugins/enrichmentmap/model/GeneSetSimilarityTest.java

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

33
import static org.junit.Assert.*;
4-
import static org.mockito.Mockito.*;
4+
import static org.mockito.Mockito.mock;
55

66
import java.util.HashMap;
77

@@ -95,7 +95,7 @@ public void testJaccardCalculations() throws Exception{
9595
assertTrue(similarity.getOverlapping_genes().contains(genes.get("CASP3")));
9696
}
9797
else{
98-
similarity = similarities.get("APOPTOSIS INDUCED DNA FRAGMENTATION%REACTOME%REACT_1213.4 (Geneset_Overlap) APOPTOTIC CLEAVAGE OF CELL ADHESION PROTEINS%REACTOME%REACT_13579.1");
98+
similarity = similarities.get("APOPTOTIC CLEAVAGE OF CELL ADHESION PROTEINS%REACTOME%REACT_13579.1 (Geneset_Overlap) APOPTOSIS INDUCED DNA FRAGMENTATION%REACTOME%REACT_1213.4");
9999
assertEquals(jaccard,similarity.getSimilarity_coeffecient(),0.0);
100100
assertEquals("APOPTOSIS INDUCED DNA FRAGMENTATION%REACTOME%REACT_1213.4",similarity.getGeneset2_Name());
101101
assertEquals("APOPTOTIC CLEAVAGE OF CELL ADHESION PROTEINS%REACTOME%REACT_13579.1", similarity.getGeneset1_Name());
@@ -172,7 +172,7 @@ public void testOverlapCalculations() throws Exception{
172172
assertTrue(similarity.getOverlapping_genes().contains(genes.get("CASP3")));
173173
}
174174
else{
175-
similarity = similarities.get("APOPTOSIS INDUCED DNA FRAGMENTATION%REACTOME%REACT_1213.4 (Geneset_Overlap) APOPTOTIC CLEAVAGE OF CELL ADHESION PROTEINS%REACTOME%REACT_13579.1");
175+
similarity = similarities.get("APOPTOTIC CLEAVAGE OF CELL ADHESION PROTEINS%REACTOME%REACT_13579.1 (Geneset_Overlap) APOPTOSIS INDUCED DNA FRAGMENTATION%REACTOME%REACT_1213.4");
176176
assertEquals(overlap,similarity.getSimilarity_coeffecient(),0.0);
177177
assertEquals("APOPTOSIS INDUCED DNA FRAGMENTATION%REACTOME%REACT_1213.4",similarity.getGeneset2_Name());
178178
assertEquals("APOPTOTIC CLEAVAGE OF CELL ADHESION PROTEINS%REACTOME%REACT_13579.1", similarity.getGeneset1_Name());
@@ -252,7 +252,7 @@ public void testCombindCalculations() throws Exception{
252252
assertTrue(similarity.getOverlapping_genes().contains(genes.get("CASP3")));
253253
}
254254
else{
255-
similarity = similarities.get("APOPTOSIS INDUCED DNA FRAGMENTATION%REACTOME%REACT_1213.4 (Geneset_Overlap) APOPTOTIC CLEAVAGE OF CELL ADHESION PROTEINS%REACTOME%REACT_13579.1");
255+
similarity = similarities.get("APOPTOTIC CLEAVAGE OF CELL ADHESION PROTEINS%REACTOME%REACT_13579.1 (Geneset_Overlap) APOPTOSIS INDUCED DNA FRAGMENTATION%REACTOME%REACT_1213.4");
256256
assertEquals(combined,similarity.getSimilarity_coeffecient(),0.0);
257257
assertEquals("APOPTOSIS INDUCED DNA FRAGMENTATION%REACTOME%REACT_1213.4",similarity.getGeneset2_Name());
258258
assertEquals("APOPTOTIC CLEAVAGE OF CELL ADHESION PROTEINS%REACTOME%REACT_13579.1", similarity.getGeneset1_Name());

EnrichmentMapPlugin/src/test/java/org/baderlab/csplugins/enrichmentmap/model/GeneSetTest.java

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

3-
import static org.junit.Assert.*;
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.List;
49

510
import org.junit.Test;
611

@@ -57,7 +62,10 @@ public void testCreateGenesetFromStringArray(){
5762
assertEquals("fake geneset", gs.getDescription());
5863

5964
assertEquals(4, gs.getGenes().size());
60-
assertEquals("Gene Set 1\tfake geneset\t0\t10\t12\t-1\t", gs.toString());
65+
66+
List<Integer> geneIds = new ArrayList<>(gs.getGenes());
67+
Collections.sort(geneIds);
68+
assertEquals(Arrays.asList(-1,0,10,12), geneIds);
6169

6270
//test equals function
6371
GeneSet gs2 = new GeneSet("Gene Set 1", "fake geneset");

EnrichmentMapPlugin/src/test/java/org/baderlab/csplugins/enrichmentmap/task/BaseNetworkTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.HashMap;
77
import java.util.Map;
88

9+
import org.baderlab.csplugins.enrichmentmap.EdgeSimilarities;
910
import org.baderlab.csplugins.enrichmentmap.EnrichmentMapManager;
1011
import org.baderlab.csplugins.enrichmentmap.EnrichmentMapParameters;
1112
import org.baderlab.csplugins.enrichmentmap.LogSilenceRule;
@@ -109,6 +110,13 @@ protected Map<String,CyEdge> getEdges(CyNetwork network) {
109110
return edges;
110111
}
111112

113+
protected EdgeSimilarities getEdgeSimilarities(CyNetwork network) {
114+
EdgeSimilarities edges = new EdgeSimilarities();
115+
for(CyEdge edge : network.getEdgeList()) {
116+
edges.addEdge(network.getRow(edge).get("name", String.class), edge);
117+
}
118+
return edges;
119+
}
112120

113121
protected void buildEnrichmentMap(EnrichmentMapParameters emParams) {
114122
EnrichmentMap map = new EnrichmentMap(emParams);

EnrichmentMapPlugin/src/test/java/org/baderlab/csplugins/enrichmentmap/task/LoadDavidResultTest.java

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.baderlab.csplugins.enrichmentmap.task;
22

3-
import static org.junit.Assert.*;
4-
import static org.mockito.Mockito.*;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.mockito.Mockito.mock;
55

66
import org.baderlab.csplugins.enrichmentmap.EnrichmentMapParameters;
77
import org.baderlab.csplugins.enrichmentmap.StreamUtil;
@@ -124,58 +124,53 @@ public void testLoad2DavidResult_withoutexpression() throws Exception{
124124
em.addDataset(EnrichmentMap.DATASET2, dataset2);
125125
//create a DatasetTask
126126
ParseDavidEnrichmentResults enrichmentResultsFiles2Task = new ParseDavidEnrichmentResults(dataset2,(org.cytoscape.io.util.StreamUtil)streamUtil);
127-
enrichmentResultsFiles2Task.run(taskMonitor);
127+
enrichmentResultsFiles2Task.run(taskMonitor);
128+
129+
// check to see if the two datasets are distinct
130+
if (!((dataset.getDatasetGenes().containsAll(dataset2.getDatasetGenes()))
131+
&& (dataset2.getDatasetGenes().containsAll(dataset.getDatasetGenes()))))
132+
params.setTwoDistinctExpressionSets(true);
133+
134+
CreateDummyExpressionTask dummyExpressionTask = new CreateDummyExpressionTask(dataset);
135+
dummyExpressionTask.run(taskMonitor);
136+
CreateDummyExpressionTask dummyExpressionTask2 = new CreateDummyExpressionTask(dataset2);
137+
dummyExpressionTask2.run(taskMonitor);
138+
139+
em.filterGenesets();
140+
141+
InitializeGenesetsOfInterestTask genesets_init = new InitializeGenesetsOfInterestTask(em);
142+
genesets_init.run(taskMonitor);
143+
144+
ComputeSimilarityTask similarities = new ComputeSimilarityTask(em);
145+
similarities.run(taskMonitor);
146+
147+
// check to see if the dataset loaded - there should be 215 genesets
148+
assertEquals(215, dataset.getSetofgenesets().getGenesets().size());
149+
// there should also be 215 enrichments (the genesets are built from the txt file)
150+
assertEquals(215, dataset.getEnrichments().getEnrichments().size());
151+
// there should be 7 genesets in the enrichments of interest
152+
assertEquals(7, dataset.getGenesetsOfInterest().getGenesets().size());
153+
154+
// there should be 114 genes in the geneset "acetylation"
155+
assertEquals(114, em.getAllGenesets().get("ACETYLATION").getGenes().size());
128156

129-
130-
//check to see if the two datasets are distinct
131-
if(!(
132-
(dataset.getDatasetGenes().containsAll(dataset2.getDatasetGenes())) &&
133-
(dataset2.getDatasetGenes().containsAll(dataset.getDatasetGenes()))
134-
))
135-
params.setTwoDistinctExpressionSets(true);
136-
137-
CreateDummyExpressionTask dummyExpressionTask = new CreateDummyExpressionTask(dataset);
138-
dummyExpressionTask.run(taskMonitor);
139-
CreateDummyExpressionTask dummyExpressionTask2 = new CreateDummyExpressionTask(dataset2);
140-
dummyExpressionTask2.run(taskMonitor);
141-
142-
em.filterGenesets();
143-
144-
InitializeGenesetsOfInterestTask genesets_init = new InitializeGenesetsOfInterestTask(em);
145-
genesets_init.run(taskMonitor);
146-
147-
ComputeSimilarityTask similarities = new ComputeSimilarityTask(em);
148-
similarities.run(taskMonitor);
149-
150-
151-
//check to see if the dataset loaded - there should be 215 genesets
152-
assertEquals(215, dataset.getSetofgenesets().getGenesets().size());
153-
//there should also be 215 enrichments (the genesets are built from the txt file)
154-
assertEquals(215, dataset.getEnrichments().getEnrichments().size());
155-
//there should be 7 genesets in the enrichments of interest
156-
assertEquals(7, dataset.getGenesetsOfInterest().getGenesets().size());
157-
158-
//there should be 114 genes in the geneset "acetylation"
159-
assertEquals(114, em.getAllGenesets().get("ACETYLATION").getGenes().size());
160-
161157
dataset2 = em.getDataset(EnrichmentMap.DATASET2);
162-
//check the stats for dataset2
163-
//check to see if the dataset loaded - there should be 263 genesets
158+
// check the stats for dataset2
159+
// check to see if the dataset loaded - there should be 263 genesets
164160
assertEquals(263, dataset2.getSetofgenesets().getGenesets().size());
165-
//there should also be 263 enrichments (the genesets are built from the bgo file)
161+
// there should also be 263 enrichments (the genesets are built from the bgo file)
166162
assertEquals(263, dataset2.getEnrichments().getEnrichments().size());
167-
//there should be 0 genesets in the enrichments of interest
163+
// there should be 0 genesets in the enrichments of interest
168164
assertEquals(0, dataset2.getGenesetsOfInterest().getGenesets().size());
169-
170-
//make sure the dummy expression has values for all the genes
165+
166+
// make sure the dummy expression has values for all the genes
171167
assertEquals(367, dataset2.getExpressionSets().getNumGenes());
172-
assertEquals(367,dataset2.getDatasetGenes().size());
173-
174-
//there should be 20 edges (2 edges for every node because of the distinct expresison sets)
175-
//assertEquals((7*6),em.getGenesetSimilarity().size());
176-
//there should be a total of 366 genes
168+
assertEquals(367, dataset2.getDatasetGenes().size());
169+
170+
// there should be 20 edges (2 edges for every node because of the distinct expresison sets)
171+
// assertEquals((7*6),em.getGenesetSimilarity().size()); there should be a total of 366 genes
177172
assertEquals(661, em.getGenes().size());
178-
173+
179174
}
180175

181176
}

0 commit comments

Comments
 (0)