Skip to content

Commit cf9a29b

Browse files
committed
JUnits for leading edge calculation
1 parent cda2dfa commit cf9a29b

File tree

16 files changed

+42486
-35
lines changed

16 files changed

+42486
-35
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/task/cluster/HierarchicalClusterTask.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
public class HierarchicalClusterTask extends AbstractTask implements ObservableTask {
2525

26-
private final Collection<String> genes;
26+
private final Collection<Integer> genes;
2727
private final EnrichmentMap map;
2828
private final DistanceMetric distanceMetric;
2929

3030
private Map<Integer,RankValue> results;
3131

3232

33-
public HierarchicalClusterTask(EnrichmentMap map, Collection<String> genes, DistanceMetric distanceMetric) {
33+
public HierarchicalClusterTask(EnrichmentMap map, Collection<Integer> genes, DistanceMetric distanceMetric) {
3434
this.map = map;
3535
this.genes = ImmutableList.copyOf(genes);
3636
this.distanceMetric = distanceMetric;
@@ -51,8 +51,7 @@ public Map<Integer,RankValue> cluster(TaskMonitor tm) {
5151
List<EMDataSet> dataSets = map.getDataSetList();
5252
final int expressionCount = getTotalExpressionCount(dataSets);
5353

54-
for(String gene : genes) {
55-
int geneId = map.getHashFromGene(gene);
54+
for(int geneId : genes) {
5655
double[] vals = new double[expressionCount]; // values all default to 0.0
5756
int valsIndex = 0;
5857

@@ -107,9 +106,6 @@ public Map<Integer,RankValue> cluster(TaskMonitor tm) {
107106
}
108107

109108

110-
111-
112-
113109
private static int getTotalExpressionCount(List<EMDataSet> dataSetList) {
114110
return dataSetList.stream()
115111
.map(EMDataSet::getExpressionSets)

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/BasicRankingOption.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
import java.util.HashMap;
77
import java.util.List;
88
import java.util.Map;
9-
import java.util.Set;
109
import java.util.concurrent.CompletableFuture;
11-
import java.util.stream.Collectors;
1210

1311
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet;
14-
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap;
1512
import org.baderlab.csplugins.enrichmentmap.model.Rank;
1613
import org.baderlab.csplugins.enrichmentmap.model.Ranking;
1714
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue;
@@ -36,17 +33,15 @@ public String toString() {
3633
}
3734

3835
@Override
39-
public CompletableFuture<Map<Integer, RankValue>> computeRanking(Collection<String> genes) {
36+
public CompletableFuture<Map<Integer, RankValue>> computeRanking(Collection<Integer> genes) {
4037
Map<Integer,RankValue> result = new HashMap<>();
4138

4239
for(Map.Entry<Integer,Rank> entry : ranking.getRanking().entrySet()) {
4340
result.put(entry.getKey(), new RankValue(entry.getValue().getRank(), false));
4441
}
4542

4643
// Remove genes that we don't need
47-
EnrichmentMap em = dataset.getMap();
48-
Set<Integer> currentGenes = genes.stream().map(em::getHashFromGene).collect(Collectors.toSet());
49-
result.keySet().retainAll(currentGenes);
44+
result.keySet().retainAll(genes);
5045

5146
normalizeRanks(result);
5247

@@ -56,7 +51,7 @@ public CompletableFuture<Map<Integer, RankValue>> computeRanking(Collection<Stri
5651

5752
public static void normalizeRanks(Map<Integer,RankValue> result) {
5853
List<RankValue> rankValueList = new ArrayList<>(result.values());
59-
rankValueList.sort(Comparator.comparing(RankValue::getRank).reversed());
54+
rankValueList.sort(Comparator.comparing(RankValue::getRank));
6055

6156
// Normalize the ranks so they are of the form 1,2,3,4...
6257
for(int i = 0; i < rankValueList.size(); i++) {

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/ClusterRankingOption.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44
import java.util.Map;
55
import java.util.concurrent.CompletableFuture;
66

7+
import org.baderlab.csplugins.enrichmentmap.CytoscapeServiceModule.Dialog;
78
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap;
89
import org.baderlab.csplugins.enrichmentmap.task.cluster.HierarchicalClusterTask;
910
import org.baderlab.csplugins.enrichmentmap.view.heatmap.HeatMapParams.Distance;
1011
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue;
1112
import org.cytoscape.work.FinishStatus;
1213
import org.cytoscape.work.ObservableTask;
1314
import org.cytoscape.work.TaskIterator;
15+
import org.cytoscape.work.TaskManager;
1416
import org.cytoscape.work.TaskObserver;
15-
import org.cytoscape.work.swing.DialogTaskManager;
1617

1718
import com.google.inject.Inject;
1819
import com.google.inject.assistedinject.Assisted;
1920

2021
public class ClusterRankingOption implements RankingOption {
2122

22-
@Inject private DialogTaskManager taskManager;
23+
private final TaskManager<?,?> taskManager;
2324

2425
private final EnrichmentMap map;
2526
private Distance distance = Distance.PEARSON;
@@ -29,8 +30,9 @@ public interface Factory {
2930
}
3031

3132
@Inject
32-
public ClusterRankingOption(@Assisted EnrichmentMap map) {
33+
public ClusterRankingOption(@Assisted EnrichmentMap map, @Dialog TaskManager<?,?> taskManager) {
3334
this.map = map;
35+
this.taskManager = taskManager;
3436
}
3537

3638
@Override
@@ -48,7 +50,7 @@ public Distance getDistance() {
4850

4951

5052
@Override
51-
public CompletableFuture<Map<Integer,RankValue>> computeRanking(Collection<String> genes) {
53+
public CompletableFuture<Map<Integer,RankValue>> computeRanking(Collection<Integer> genes) {
5254
HierarchicalClusterTask task = new HierarchicalClusterTask(map, genes, distance.getMetric());
5355

5456
CompletableFuture<Map<Integer,RankValue>> future = new CompletableFuture<>();

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/GSEALeadingEdgeRankingOption.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
import java.util.HashMap;
66
import java.util.List;
77
import java.util.Map;
8-
import java.util.Set;
98
import java.util.concurrent.CompletableFuture;
10-
import java.util.stream.Collectors;
119

1210
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet;
1311
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet.Method;
14-
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap;
1512
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentResult;
1613
import org.baderlab.csplugins.enrichmentmap.model.GSEAResult;
1714
import org.baderlab.csplugins.enrichmentmap.model.GeneExpression;
@@ -47,7 +44,7 @@ public String toString() {
4744
}
4845

4946
@Override
50-
public CompletableFuture<Map<Integer,RankValue>> computeRanking(Collection<String> genes) {
47+
public CompletableFuture<Map<Integer,RankValue>> computeRanking(Collection<Integer> genes) {
5148
initializeLeadingEdge();
5249

5350
int topRank = getTopRank();
@@ -99,9 +96,7 @@ else if (ranksSubset[m] >= topRank && isNegative && topRank != 0 && topRank != -
9996
}
10097

10198
// Remove genes that we don't need
102-
EnrichmentMap em = dataset.getMap();
103-
Set<Integer> currentGenes = genes.stream().map(em::getHashFromGene).collect(Collectors.toSet());
104-
result.keySet().retainAll(currentGenes);
99+
result.keySet().retainAll(genes);
105100

106101
BasicRankingOption.normalizeRanks(result);
107102

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/HeatMapMainPanel.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Map;
1414
import java.util.Set;
1515
import java.util.concurrent.CompletableFuture;
16+
import java.util.stream.Collectors;
1617

1718
import javax.swing.GroupLayout;
1819
import javax.swing.GroupLayout.Alignment;
@@ -332,10 +333,14 @@ private void updateSetting_Operator(Operator oper) {
332333
private void updateSetting_RankOption(RankingOption rankOption) {
333334
//rankOptionCombo.setEnabled(false);
334335
List<String> genes = getGenes(getOperator());
335-
CompletableFuture<Map<Integer,RankValue>> rankingFuture = rankOption.computeRanking(genes);
336+
337+
HeatMapTableModel tableModel = (HeatMapTableModel) table.getModel();
338+
EnrichmentMap map = tableModel.getEnrichmentMap();
339+
List<Integer> geneIds = genes.stream().map(map::getHashFromGene).collect(Collectors.toList());
340+
341+
CompletableFuture<Map<Integer,RankValue>> rankingFuture = rankOption.computeRanking(geneIds);
336342
if(rankingFuture != null) {
337343
rankingFuture.whenComplete((ranking, ex) -> {
338-
HeatMapTableModel tableModel = (HeatMapTableModel) table.getModel();
339344
tableModel.setRanking(ranking);
340345
//rankOptionCombo.setEnabled(true);
341346
});

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/RankingOption.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ default String getName() {
1919
* Asynchronously compute the rankings.
2020
* @return Map where keys are geneIDs and value is the rank.
2121
*/
22-
CompletableFuture<Map<Integer,RankValue>> computeRanking(Collection<String> genes);
22+
CompletableFuture<Map<Integer,RankValue>> computeRanking(Collection<Integer> genes);
2323

2424

2525
public static RankingOption none() {
2626
return new RankingOption() {
2727
public String toString() {
2828
return "None";
2929
}
30-
public CompletableFuture<Map<Integer,RankValue>> computeRanking(Collection<String> genes) {
30+
public CompletableFuture<Map<Integer,RankValue>> computeRanking(Collection<Integer> genes) {
3131
return CompletableFuture.completedFuture(null);
3232
}
3333
};

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/table/HeatMapTableModel.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public Transform getTransform() {
7474
return transform;
7575
}
7676

77+
public EnrichmentMap getEnrichmentMap() {
78+
return map;
79+
}
7780

7881
public String getGene(int row) {
7982
return genes.get(row);
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package org.baderlab.csplugins.enrichmentmap.heatmap;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.io.File;
8+
import java.nio.charset.Charset;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Optional;
12+
import java.util.stream.Collectors;
13+
14+
import org.baderlab.csplugins.enrichmentmap.model.DataSetFiles;
15+
import org.baderlab.csplugins.enrichmentmap.model.EMCreationParameters;
16+
import org.baderlab.csplugins.enrichmentmap.model.EMCreationParameters.SimilarityMetric;
17+
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet;
18+
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet.Method;
19+
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap;
20+
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMapManager;
21+
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentResultFilterParams.NESFilter;
22+
import org.baderlab.csplugins.enrichmentmap.model.GeneSet;
23+
import org.baderlab.csplugins.enrichmentmap.model.LegacySupport;
24+
import org.baderlab.csplugins.enrichmentmap.task.BaseNetworkTest;
25+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.GSEALeadingEdgeRankingOption;
26+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.RankingOption;
27+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue;
28+
import org.jukito.JukitoRunner;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
34+
import com.google.common.collect.HashBiMap;
35+
import com.google.common.io.Files;
36+
37+
@RunWith(JukitoRunner.class)
38+
public class HeatMapRanksTest extends BaseNetworkTest {
39+
40+
private static final String PATH = "src/test/resources/org/baderlab/csplugins/enrichmentmap/task/tutorial/";
41+
42+
// All subclasses of BaseNetworkTest must do this
43+
public static class TestModule extends BaseNetworkTest.TestModule {
44+
@Override
45+
protected void configureTest() {
46+
super.configureTest();
47+
}
48+
}
49+
50+
51+
@Before
52+
public void setUp(EnrichmentMapManager emManager) {
53+
DataSetFiles files = new DataSetFiles();
54+
files.setGMTFileName(PATH + "Human_GO_AllPathways_no_GO_iea_April_15_2013_symbol.gmt");
55+
files.setExpressionFileName(PATH + "MCF7_ExprMx_v2_names.gct");
56+
files.setEnrichmentFileName1(PATH + "gsea_report_for_ES12_1473194913081.xls");
57+
files.setEnrichmentFileName2(PATH + "gsea_report_for_NT12_1473194913081.xls");
58+
files.setRankedFile(PATH + "ranked_gene_list_ES12_versus_NT12_1473194913081.xls");
59+
files.setClassFile(PATH + "ES_NT.cls");
60+
61+
EMCreationParameters params = new EMCreationParameters("HeatMapRanks_", 0.005, 0.1, NESFilter.ALL, Optional.empty(), SimilarityMetric.OVERLAP, 0.5, 0.5);
62+
63+
Map<Long, EnrichmentMap> maps = emManager.getAllEnrichmentMaps();
64+
assertEquals(0, maps.size());
65+
66+
buildEnrichmentMap(params, files, Method.GSEA, LegacySupport.DATASET1);
67+
68+
maps = emManager.getAllEnrichmentMaps();
69+
assertEquals(1, maps.size());
70+
}
71+
72+
@After
73+
public void tearDown(EnrichmentMapManager emManager) {
74+
emManager.reset();
75+
}
76+
77+
78+
private static List<Integer> getGeneOrderFromFile(EnrichmentMap map, String path) throws Exception {
79+
List<String> geneNames = Files.readLines(new File(path), Charset.forName("UTF8"));
80+
return geneNames.stream().map(map::getHashFromGene).collect(Collectors.toList());
81+
}
82+
83+
@Test
84+
public void testLeadingEdge(EnrichmentMapManager emManager) throws Exception {
85+
final String geneSetName = "ENVELOPE%GO%GO:0031975";
86+
final int leadingEdgeSize = 170;
87+
88+
// Sanity test
89+
EnrichmentMap map = emManager.getAllEnrichmentMaps().values().iterator().next();
90+
EMDataSet dataset = map.getDataSet(LegacySupport.DATASET1);
91+
GeneSet gs = dataset.getGeneSetsOfInterest().getGeneSets().get(geneSetName);
92+
assertNotNull(gs);
93+
94+
// Run the ranking
95+
RankingOption rankingOption = new GSEALeadingEdgeRankingOption(dataset, geneSetName);
96+
Map<Integer,RankValue> ranks = rankingOption.computeRanking(gs.getGenes()).get();
97+
assertEquals(454, ranks.size());
98+
99+
// Convert to useful collections
100+
Map<RankValue,Integer> rankToGeneId = HashBiMap.create(ranks).inverse();
101+
List<RankValue> sortedRanks = ranks.values().stream().sorted().collect(Collectors.toList());
102+
103+
// Test leading edge
104+
for(int i = 0; i < sortedRanks.size(); i++) {
105+
RankValue v = sortedRanks.get(i);
106+
assertTrue(v.isSignificant() == i < leadingEdgeSize);
107+
}
108+
109+
// Test genes are the same
110+
List<Integer> expectedGeneOrder = getGeneOrderFromFile(map, PATH + "gene_order_leading_edge.txt");
111+
List<Integer> actualGeneOrder = sortedRanks.stream().map(rankToGeneId::get).collect(Collectors.toList());
112+
assertEquals(expectedGeneOrder, actualGeneOrder);
113+
}
114+
115+
116+
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ protected void configureTest() {
8383

8484
// Bind all AssistedInjection factories
8585
install(ApplicationModule.createFactoryModule());
86-
87-
// // MKTODO get rid of these fields in EnrichmentMapManager
88-
// bind(ParametersPanel.class).toProvider(Providers.of(null));
89-
// bind(HeatMapPanel.class).annotatedWith(Edges.class).toProvider(Providers.of(null));
90-
// bind(HeatMapPanel.class).annotatedWith(Nodes.class).toProvider(Providers.of(null));
9186
}
9287
}
9388

EnrichmentMapPlugin/src/test/resources/org/baderlab/csplugins/enrichmentmap/task/tutorial/ES_NT.cls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
18 6 1# ES12 NT12 ES24 NT24 ES48 NT48ES12 ES12 ES12 NT12 NT12 NT12 ES24 ES24 ES24 NT24 NT24 NT24 ES48 ES48 ES48 NT48 NT48 NT48

0 commit comments

Comments
 (0)