Skip to content

Commit e5fbfb0

Browse files
committed
Bug #144. Save and restore classes properly. Fix HeatMap column title
colors.
1 parent 5c8703a commit e5fbfb0

File tree

4 files changed

+62
-22
lines changed

4 files changed

+62
-22
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/actions/EnrichmentMapSessionAction.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
415415
String tmpDir = System.getProperty("java.io.tmpdir");
416416
System.out.println("java.io.tmpdir: [" + tmpDir + "]");
417417

418-
String prop_file_content = "";
418+
StringBuilder prop_file_content = new StringBuilder();
419419

420420
//get the networks
421421
HashMap<Long, EnrichmentMap> networks = EnrichmentMapManager.getInstance().getCyNetworkList();
@@ -447,8 +447,9 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
447447
//geneset file for PostAnalysis Signature Genesets
448448
File siggmt = new File(tmpDir,name+".signature.gmt");
449449

450-
prop_file_content = prop_file_content + "Version\t2.0\n";
451-
prop_file_content = prop_file_content + params.toString();
450+
prop_file_content.append("Version\t2.0\n");
451+
prop_file_content.append(params.toString());
452+
452453
try{
453454
if (!em.getSignatureGenesets().isEmpty() ) {
454455
BufferedWriter sigGmtwriter = new BufferedWriter(new FileWriter(siggmt));
@@ -474,7 +475,7 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
474475
HashMap<String, DataSet> all_datasets = em.getDatasets();
475476

476477
//output to the property file how many datasets we have (so we know on reload)
477-
prop_file_content = prop_file_content + "Datasets\t"+ all_datasets.keySet().toString() +"\n";
478+
prop_file_content.append("Datasets\t"+ all_datasets.keySet().toString() + "\n");
478479

479480
for(Iterator<String> k = all_datasets.keySet().iterator(); k.hasNext();){
480481
String dataset_name = k.next().toString();
@@ -499,7 +500,7 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
499500
enr1writer_backcomp.close();
500501
pFileList.add(enrichmentresults_backcomp);
501502

502-
prop_file_content = prop_file_content + em.getDataset(current).getSetofgenesets().toString(current);
503+
prop_file_content.append(em.getDataset(current).getSetofgenesets().toString(current));
503504

504505
//enrichments
505506
File enrichmentresults = new File(tmpDir, name+"." + dataset_name +".ENR.txt");
@@ -509,7 +510,7 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
509510
enr1writer.close();
510511
pFileList.add(enrichmentresults);
511512

512-
prop_file_content = prop_file_content + em.getDataset(current).getEnrichments().toString(current);
513+
prop_file_content.append(em.getDataset(current).getEnrichments().toString(current));
513514

514515
//expression
515516
if(em.getDataset(current).getExpressionSets() != null){
@@ -521,7 +522,7 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
521522
pFileList.add(expression);
522523

523524
//print out the information about the expression files
524-
prop_file_content = prop_file_content + em.getDataset(current).getExpressionSets().toString(current);
525+
prop_file_content.append(em.getDataset(current).getExpressionSets().toString(current));
525526

526527
//save all the rank files
527528
if(!em.getDataset(current).getExpressionSets().getRanks().isEmpty()){
@@ -553,7 +554,7 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
553554
}
554555
}
555556
BufferedWriter writer = new BufferedWriter(new FileWriter(session_prop_file));
556-
writer.write(prop_file_content);
557+
writer.write(prop_file_content.toString());
557558
writer.close();
558559
pFileList.add(session_prop_file);
559560
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/heatmap/ColumnHeaderVerticalRenderer.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343

4444
package org.baderlab.csplugins.enrichmentmap.heatmap;
4545

46+
import java.awt.Color;
4647
import java.awt.Component;
4748
import java.awt.Font;
4849
import java.awt.FontMetrics;
4950
import java.awt.Graphics2D;
5051
import java.awt.RenderingHints;
5152
import java.awt.image.BufferedImage;
53+
import java.util.Optional;
5254

5355
import javax.swing.Icon;
5456
import javax.swing.ImageIcon;
@@ -66,41 +68,54 @@
6668
* <p>
6769
* Flips column headers to vertical position
6870
*/
71+
@SuppressWarnings("serial")
6972
public class ColumnHeaderVerticalRenderer extends DefaultTableCellRenderer {
7073

74+
private Optional<Color> labelBackgroundColor;
75+
76+
public ColumnHeaderVerticalRenderer(Color labelBackgroundColor) {
77+
this.labelBackgroundColor = Optional.ofNullable(labelBackgroundColor);
78+
}
79+
80+
public ColumnHeaderVerticalRenderer() {
81+
this.labelBackgroundColor = Optional.empty();
82+
}
83+
7184

7285
@Override
7386
public Component getTableCellRendererComponent(JTable table,
7487
Object value, boolean isSelected, boolean hasFocus,
7588
int row, int column) {
76-
7789
JLabel label = new JLabel();
7890

7991
label.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
8092
label.setBackground(this.getBackground());
8193
label.setForeground(UIManager.getColor("TableHeader.foreground"));
8294
label.setFont(UIManager.getFont("TableHeader.font"));
8395

84-
Icon icon = VerticalCaption.getVerticalCaption(label, value.toString(), false);
96+
Icon icon = getVerticalCaption(label, value.toString(), false);
8597

8698
label.setIcon(icon);
8799
label.setVerticalAlignment(JLabel.BOTTOM);
88100
label.setHorizontalAlignment(JLabel.CENTER);
89101

90102
return label;
91103
}
92-
}
93-
94-
class VerticalCaption {
95104

96-
static Icon getVerticalCaption(JComponent component, String caption, boolean clockwise) {
105+
106+
private Icon getVerticalCaption(JComponent component, String caption, boolean clockwise) {
97107
Font f = component.getFont();
98108
FontMetrics fm = component.getFontMetrics(f);
99109
int captionHeight = fm.getHeight();
100110
int captionWidth = fm.stringWidth(caption);
101111
BufferedImage bi = new BufferedImage(captionHeight + 4, captionWidth + 4, BufferedImage.TYPE_INT_ARGB);
102112
Graphics2D g = (Graphics2D) bi.getGraphics();
103113

114+
if(labelBackgroundColor.isPresent()) {
115+
g.setColor(labelBackgroundColor.get());
116+
g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
117+
}
118+
104119
g.setColor(component.getForeground());
105120
g.setFont(f);
106121
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
package org.baderlab.csplugins.enrichmentmap.model;
4545

46+
import java.util.Arrays;
4647
import java.util.HashMap;
4748
import java.util.HashSet;
4849
import java.util.Iterator;
@@ -409,7 +410,7 @@ public String toString(String ds) {
409410
paramVariables.append(ds + "%" + simpleName + "%expressionUniverse\t" + expressionUniverse + "\n");
410411

411412
if(phenotypes != null)
412-
paramVariables.append(ds + "%" + simpleName + "%phenotypes\t" + phenotypes.toString() + "\n");
413+
paramVariables.append(ds + "%" + simpleName + "%phenotypes\t" + Arrays.toString(phenotypes) + "\n");
413414

414415
return paramVariables.toString();
415416
}
@@ -418,14 +419,41 @@ public String toString(String ds) {
418419
* Restores parameters saved in the session file.
419420
* Note, most of this object is restored by the ExpressionFileReaderTask.
420421
*/
421-
public void restoreProps(String ds, Map<String, String> props) {
422+
public void restoreProps(String ds, Map<String, String> props) {
422423
String simpleName = this.getClass().getSimpleName();
423424
String val = props.get(ds + "%" + simpleName + "%expressionUniverse");
424425
if(val != null) {
425426
try {
426427
expressionUniverse = Integer.parseInt(val);
427428
} catch(NumberFormatException e) {}
428429
}
430+
431+
if(props.containsKey(ds + "%" + simpleName + "%phenotype1")) {
432+
String prop = props.get(ds + "%" + simpleName + "%phenotype1");
433+
if(!"null".equals(prop)) {
434+
this.phenotype1 = prop;
435+
}
436+
}
437+
if(props.containsKey(ds + "%" + simpleName + "%phenotype2")) {
438+
String prop = props.get(ds + "%" + simpleName + "%phenotype2");
439+
if(!"null".equals(prop)) {
440+
this.phenotype2 = prop;
441+
}
442+
}
443+
444+
String[] col_names = getColumnNames();
445+
446+
if(props.containsKey(ds + "%" + simpleName + "%phenotypes") && col_names != null && col_names.length > 0) {
447+
try {
448+
String prop = props.get(ds + "%" + simpleName + "%phenotypes");
449+
if(!prop.startsWith("[Ljava.lang.String")) { // avoid bug #144
450+
String[] restored_phenotypes = prop.replace("[", "").replace("]", "").split(", ");
451+
if(restored_phenotypes.length == col_names.length-2) {
452+
this.phenotypes = restored_phenotypes;
453+
}
454+
}
455+
} catch(Exception e) { }
456+
}
429457
}
430458

431459

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/HeatMapPanel.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,9 @@ public void updatePanel() {
439439
table.setCellSelectionEnabled(true);
440440

441441
//set the table header renderer to the vertical renderer
442-
ColumnHeaderVerticalRenderer pheno1_renderer = new ColumnHeaderVerticalRenderer();
443-
pheno1_renderer.setBackground(EnrichmentMapVisualStyle.LIGHTEST_PHENOTYPE_1);
444-
ColumnHeaderVerticalRenderer pheno2_renderer = new ColumnHeaderVerticalRenderer();
445-
pheno2_renderer.setBackground(EnrichmentMapVisualStyle.LIGHTEST_PHENOTYPE_2);
446-
442+
ColumnHeaderVerticalRenderer pheno1_renderer = new ColumnHeaderVerticalRenderer(EnrichmentMapVisualStyle.LIGHTEST_PHENOTYPE_1);
443+
ColumnHeaderVerticalRenderer pheno2_renderer = new ColumnHeaderVerticalRenderer(EnrichmentMapVisualStyle.LIGHTEST_PHENOTYPE_2);
447444
ColumnHeaderVerticalRenderer default_renderer = new ColumnHeaderVerticalRenderer();
448-
default_renderer.setBackground(Color.white);
449445

450446
if (params.isData2() && map.getDataset(EnrichmentMap.DATASET2).getExpressionSets() != null && !map.getDataset(EnrichmentMap.DATASET1)
451447
.getExpressionSets().getFilename().equalsIgnoreCase(map.getDataset(EnrichmentMap.DATASET2).getExpressionSets().getFilename())) {

0 commit comments

Comments
 (0)