Skip to content

Commit e46d7d7

Browse files
committed
Refactor heat map header code.
1 parent c0a0b94 commit e46d7d7

File tree

8 files changed

+149
-124
lines changed

8 files changed

+149
-124
lines changed

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

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

33
import java.util.ArrayList;
4-
import java.util.Iterator;
54
import java.util.LinkedHashSet;
65
import java.util.List;
76
import java.util.Optional;
@@ -20,19 +19,20 @@ public class CompressedClass implements ExpressionData {
2019
public CompressedClass(EnrichmentMap map, List<EMDataSet> datasets, ExpressionCache expressionCache) {
2120
this.expressionCache = expressionCache;
2221

23-
// Special case. There is more than one dataset in the map, and they have different phenotypes,
24-
// but the expression values are the same and we don't want to repeat them in the table.
25-
if(map != null && map.isCommonExpressionValues() && !phenotypesAreCommon(map)) {
22+
// Special case. There is more than one dataset in the map, and they have the same expressions,
23+
// but the pheontypes are different. We want to show the phenotypes for the selected data sets
24+
// but we don't want to repeat all the expression data.
25+
if(ExpressionData.commonExpressionsButDifferentPhenotypes(map)) {
2626
// we only get passed one dataset because expressions are the same
2727
var dataset = datasets.get(0);
28-
var classPhenos = getPhenotypesFromClassFile(dataset);
29-
var highlightPhenos = getPhenotypesToHighlight(map.getDataSetList()); // get the phenotypes from all the data sets
28+
var classPhenos = ExpressionData.getPhenotypesFromClassFile(dataset);
29+
var highlightPhenos = ExpressionData.getPhenotypesToHighlight(map.getDataSetList()); // get the phenotypes from all the data sets
3030
addHeaders(dataset, classPhenos, highlightPhenos);
3131

3232
} else {
3333
for(var dataset : datasets) {
34-
var classPhenos = getPhenotypesFromClassFile(dataset);
35-
var highlightPhenos = getPhenotypesToHighlight(List.of(dataset));
34+
var classPhenos = ExpressionData.getPhenotypesFromClassFile(dataset);
35+
var highlightPhenos = ExpressionData.getPhenotypesToHighlight(dataset);
3636
addHeaders(dataset, classPhenos, highlightPhenos);
3737
}
3838
}
@@ -55,66 +55,6 @@ private void addHeaders(EMDataSet mainDataSet, LinkedHashSet<String> classPhenos
5555
}
5656
}
5757

58-
/**
59-
* These are the phenotypes entered into the "Phenotypes" fields in the creation dialo
60-
* They need to be highlighted in the table.
61-
*/
62-
private static List<Phenotype> getPhenotypesToHighlight(List<EMDataSet> datasets) {
63-
List<Phenotype> phenos = new ArrayList<>();
64-
65-
for(var dataset : datasets) {
66-
var enrichments = dataset.getEnrichments();
67-
68-
String pheno1 = enrichments.getPhenotype1();
69-
if(pheno1 != null) {
70-
phenos.add(new Phenotype(dataset, pheno1, Type.POSITIVE));
71-
}
72-
String pheno2 = enrichments.getPhenotype2();
73-
if(pheno2 != null) {
74-
phenos.add(new Phenotype(dataset, pheno2, Type.NEGATIVE));
75-
}
76-
}
77-
78-
return phenos;
79-
}
80-
81-
/**
82-
* Returns unique classes from the class file in the same order as the file.
83-
*/
84-
private static LinkedHashSet<String> getPhenotypesFromClassFile(EMDataSet dataset) {
85-
var enrichments = dataset.getEnrichments();
86-
LinkedHashSet<String> uniquePhenos = new LinkedHashSet<>();
87-
88-
String[] phenotypes = enrichments.getPhenotypes();
89-
if(phenotypes != null) {
90-
for(String pheno : phenotypes) {
91-
if(pheno != null) {
92-
uniquePhenos.add(pheno);
93-
}
94-
}
95-
}
96-
97-
return uniquePhenos;
98-
}
99-
100-
101-
private static boolean phenotypesAreCommon(EnrichmentMap map) {
102-
Iterator<EMDataSet> iter = map.getDataSets().values().iterator();
103-
SetOfEnrichmentResults r = iter.next().getEnrichments();
104-
String p1 = r.getPhenotype1();
105-
String p2 = r.getPhenotype2();
106-
107-
while(iter.hasNext()) {
108-
SetOfEnrichmentResults r2 = iter.next().getEnrichments();
109-
if(!p1.equals(r2.getPhenotype1()))
110-
return false;
111-
if(!p2.equals(r2.getPhenotype2()))
112-
return false;
113-
}
114-
115-
return true;
116-
}
117-
11858

11959
@Override
12060
public EMDataSet getDataSet(int idx) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class CompressedDataSet implements ExpressionData {
99
private final List<EMDataSet> datasets;
1010
private final boolean isDistinctExpressionSets;
1111

12-
public CompressedDataSet(List<EMDataSet> datasets, ExpressionCache expressionCache, boolean isDistinctExpressionSets) {
12+
public CompressedDataSet(EnrichmentMap map, List<EMDataSet> datasets, ExpressionCache expressionCache) {
1313
this.datasets = datasets;
1414
this.expressionCache = expressionCache;
15-
this.isDistinctExpressionSets = isDistinctExpressionSets;
15+
this.isDistinctExpressionSets = map != null && map.isDistinctExpressionSets();
1616
}
1717

1818
@Override

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

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

3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.LinkedHashSet;
6+
import java.util.List;
7+
8+
import org.baderlab.csplugins.enrichmentmap.model.Phenotype.Type;
9+
310
/**
411
* Common interface for different levels of compression.
512
*/
@@ -16,4 +23,81 @@ public default Phenotype getPhenotype(int col) {
1623
};
1724

1825
int getSize();
26+
27+
28+
29+
/**
30+
* Special case. There is more than one dataset in the map, and they have the same expressions,
31+
* but the pheontypes are different. We want to show the phenotypes for the selected data sets
32+
* but we don't want to repeat all the expression data.
33+
*/
34+
static boolean commonExpressionsButDifferentPhenotypes(EnrichmentMap map) {
35+
return map != null && map.isCommonExpressionValues() && !phenotypesAreCommon(map);
36+
}
37+
38+
39+
/**
40+
* These are the phenotypes entered into the "Phenotypes" fields in the creation dialog.
41+
* They need to be highlighted in the table.
42+
*/
43+
static List<Phenotype> getPhenotypesToHighlight(List<EMDataSet> datasets) {
44+
List<Phenotype> phenos = new ArrayList<>();
45+
46+
for(var dataset : datasets) {
47+
var enrichments = dataset.getEnrichments();
48+
49+
String pheno1 = enrichments.getPhenotype1();
50+
if(pheno1 != null) {
51+
phenos.add(new Phenotype(dataset, pheno1, Type.POSITIVE));
52+
}
53+
String pheno2 = enrichments.getPhenotype2();
54+
if(pheno2 != null) {
55+
phenos.add(new Phenotype(dataset, pheno2, Type.NEGATIVE));
56+
}
57+
}
58+
59+
return phenos;
60+
}
61+
62+
static List<Phenotype> getPhenotypesToHighlight(EMDataSet dataset) {
63+
return getPhenotypesToHighlight(List.of(dataset));
64+
}
65+
66+
67+
/**
68+
* Returns unique classes from the class file in the same order as the file.
69+
*/
70+
static LinkedHashSet<String> getPhenotypesFromClassFile(EMDataSet dataset) {
71+
var enrichments = dataset.getEnrichments();
72+
LinkedHashSet<String> uniquePhenos = new LinkedHashSet<>();
73+
74+
String[] phenotypes = enrichments.getPhenotypes();
75+
if(phenotypes != null) {
76+
for(String pheno : phenotypes) {
77+
if(pheno != null) {
78+
uniquePhenos.add(pheno);
79+
}
80+
}
81+
}
82+
83+
return uniquePhenos;
84+
}
85+
86+
87+
static boolean phenotypesAreCommon(EnrichmentMap map) {
88+
Iterator<EMDataSet> iter = map.getDataSets().values().iterator();
89+
SetOfEnrichmentResults r = iter.next().getEnrichments();
90+
String p1 = r.getPhenotype1();
91+
String p2 = r.getPhenotype2();
92+
93+
while(iter.hasNext()) {
94+
SetOfEnrichmentResults r2 = iter.next().getEnrichments();
95+
if(!p1.equals(r2.getPhenotype1()))
96+
return false;
97+
if(!p2.equals(r2.getPhenotype2()))
98+
return false;
99+
}
100+
101+
return true;
102+
}
19103
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public class Uncompressed implements ExpressionData {
1313
private final NavigableMap<Integer, EMDataSet> colToDataSet = new TreeMap<>();
1414
private final int expressionCount;
1515

16-
public Uncompressed(List<EMDataSet> datasets, ExpressionCache expressionCache) {
16+
public Uncompressed(EnrichmentMap map, List<EMDataSet> datasets, ExpressionCache expressionCache) {
1717
this.expressionCache = expressionCache;
1818
int rangeFloor = 0;
1919
colToDataSet.put(0, null);
2020

21-
for (EMDataSet dataset : datasets) {
21+
for(EMDataSet dataset : datasets) {
2222
GeneExpressionMatrix matrix = dataset.getExpressionSets();
2323
colToDataSet.put(rangeFloor, dataset);
2424
rangeFloor += matrix.getNumConditions() - 2;
@@ -34,23 +34,21 @@ public EMDataSet getDataSet(int idx) {
3434

3535
private int getIndexInDataSet(int idx) {
3636
int start = colToDataSet.floorKey(idx);
37-
3837
return idx - start;
3938
}
4039

4140
@Override
4241
public double getValue(int geneID, int idx, Compress compress, Transform transform) {
4342
EMDataSet dataset = getDataSet(idx);
44-
45-
return expressionCache.getExpression(geneID, dataset, transform, getIndexInDataSet(idx));
43+
int indexInDataSet = getIndexInDataSet(idx);
44+
return expressionCache.getExpression(geneID, dataset, transform, indexInDataSet);
4645
}
4746

4847
@Override
4948
public String getName(int idx) {
5049
EMDataSet dataset = getDataSet(idx);
5150
String[] columns = dataset.getExpressionSets().getColumnNames();
5251
int index = getIndexInDataSet(idx) + 2;
53-
5452
return columns[index];
5553
}
5654

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/control/ControlPanelMediator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,16 +893,15 @@ private ExpressionData createExpressionData(EnrichmentMap map, List<EMDataSet> d
893893
case DATASET_MEDIAN:
894894
case DATASET_MAX:
895895
case DATASET_MIN:
896-
boolean isDistinctExpressionSets = map != null && map.isDistinctExpressionSets();
897-
exp = new CompressedDataSet(datasets, cache, isDistinctExpressionSets);
896+
exp = new CompressedDataSet(map, datasets, cache);
898897
break;
899898
case CLASS_MEDIAN:
900899
case CLASS_MAX:
901900
case CLASS_MIN:
902901
exp = new CompressedClass(map, datasets, cache);
903902
break;
904903
default:
905-
exp = new Uncompressed(datasets, cache);
904+
exp = new Uncompressed(map, datasets, cache);
906905
break;
907906
}
908907

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@
4141
import org.baderlab.csplugins.enrichmentmap.model.Compress;
4242
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet;
4343
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap;
44-
import org.baderlab.csplugins.enrichmentmap.model.Phenotype;
45-
import org.baderlab.csplugins.enrichmentmap.model.Phenotype.Type;
4644
import org.baderlab.csplugins.enrichmentmap.model.Transform;
47-
import org.baderlab.csplugins.enrichmentmap.style.EMStyleBuilder;
4845
import org.baderlab.csplugins.enrichmentmap.view.heatmap.HeatMapParams.Distance;
4946
import org.baderlab.csplugins.enrichmentmap.view.heatmap.HeatMapParams.Operator;
5047
import org.baderlab.csplugins.enrichmentmap.view.heatmap.RankingResult.SortSuggestion;
@@ -147,30 +144,19 @@ void updateTableHeader(boolean showValues) {
147144
HeatMapTableModel tableModel = (HeatMapTableModel) getTable().getModel();
148145
TableColumnModel columnModel = getTable().getColumnModel();
149146

150-
var vertRenderer = verticalRendererFactory.create(null);
151-
var vertRendererPos = verticalRendererFactory.create(EMStyleBuilder.Colors.HEAT_MAP_HIGHLIGHT_POS);
152-
var vertRendererNeg = verticalRendererFactory.create(EMStyleBuilder.Colors.HEAT_MAP_HIGHLIGHT_NEG);
153-
154147
TableColumn rankColumn = columnModel.getColumn(RANK_COL);
155-
156148
rankColumn.setHeaderRenderer(columnHeaderRankOptionRendererFactory.create(this, RANK_COL));
157149
rankColumn.setPreferredWidth(100);
158150

159-
// Iterate over the gene expression columns
151+
// Iterate over the gene expression columns
160152
int colCount = tableModel.getColumnCount();
161153
for (int col = HeatMapTableModel.DESC_COL_COUNT; col < colCount; col++) {
162-
Phenotype pheno = tableModel.getPhenotype(col);
163-
164-
TableCellRenderer renderer;
165-
if(pheno != null && pheno.getType() == Type.POSITIVE)
166-
renderer = vertRendererPos;
167-
else if(pheno != null && pheno.getType() == Type.NEGATIVE)
168-
renderer = vertRendererNeg;
169-
else
170-
renderer = vertRenderer;
171-
172154
TableColumn column = columnModel.getColumn(col);
173-
column.setHeaderRenderer(renderer);
155+
156+
// TODO: Don't use a separate instance of vertRenderer for every column.
157+
// Only doing this because the column header height doesn't have to be pre-calcuated this way.
158+
var vertRenderer = verticalRendererFactory.create();
159+
column.setHeaderRenderer(vertRenderer);
174160
column.setPreferredWidth(expressionColumnWidth);
175161
}
176162
}

0 commit comments

Comments
 (0)