Skip to content

Commit

Permalink
Bug #144. Save and restore classes properly. Fix HeatMap column title
Browse files Browse the repository at this point in the history
colors.
  • Loading branch information
mikekucera committed Nov 28, 2016
1 parent 5c8703a commit e5fbfb0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
String tmpDir = System.getProperty("java.io.tmpdir");
System.out.println("java.io.tmpdir: [" + tmpDir + "]");

String prop_file_content = "";
StringBuilder prop_file_content = new StringBuilder();

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

prop_file_content = prop_file_content + "Version\t2.0\n";
prop_file_content = prop_file_content + params.toString();
prop_file_content.append("Version\t2.0\n");
prop_file_content.append(params.toString());

try{
if (!em.getSignatureGenesets().isEmpty() ) {
BufferedWriter sigGmtwriter = new BufferedWriter(new FileWriter(siggmt));
Expand All @@ -474,7 +475,7 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
HashMap<String, DataSet> all_datasets = em.getDatasets();

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

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

prop_file_content = prop_file_content + em.getDataset(current).getSetofgenesets().toString(current);
prop_file_content.append(em.getDataset(current).getSetofgenesets().toString(current));

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

prop_file_content = prop_file_content + em.getDataset(current).getEnrichments().toString(current);
prop_file_content.append(em.getDataset(current).getEnrichments().toString(current));

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

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

//save all the rank files
if(!em.getDataset(current).getExpressionSets().getRanks().isEmpty()){
Expand Down Expand Up @@ -553,7 +554,7 @@ public void handleEvent(SessionAboutToBeSavedEvent e) {
}
}
BufferedWriter writer = new BufferedWriter(new FileWriter(session_prop_file));
writer.write(prop_file_content);
writer.write(prop_file_content.toString());
writer.close();
pFileList.add(session_prop_file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@

package org.baderlab.csplugins.enrichmentmap.heatmap;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.Optional;

import javax.swing.Icon;
import javax.swing.ImageIcon;
Expand All @@ -66,41 +68,54 @@
* <p>
* Flips column headers to vertical position
*/
@SuppressWarnings("serial")
public class ColumnHeaderVerticalRenderer extends DefaultTableCellRenderer {

private Optional<Color> labelBackgroundColor;

public ColumnHeaderVerticalRenderer(Color labelBackgroundColor) {
this.labelBackgroundColor = Optional.ofNullable(labelBackgroundColor);
}

public ColumnHeaderVerticalRenderer() {
this.labelBackgroundColor = Optional.empty();
}


@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {

JLabel label = new JLabel();

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

Icon icon = VerticalCaption.getVerticalCaption(label, value.toString(), false);
Icon icon = getVerticalCaption(label, value.toString(), false);

label.setIcon(icon);
label.setVerticalAlignment(JLabel.BOTTOM);
label.setHorizontalAlignment(JLabel.CENTER);

return label;
}
}

class VerticalCaption {

static Icon getVerticalCaption(JComponent component, String caption, boolean clockwise) {

private Icon getVerticalCaption(JComponent component, String caption, boolean clockwise) {
Font f = component.getFont();
FontMetrics fm = component.getFontMetrics(f);
int captionHeight = fm.getHeight();
int captionWidth = fm.stringWidth(caption);
BufferedImage bi = new BufferedImage(captionHeight + 4, captionWidth + 4, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) bi.getGraphics();

if(labelBackgroundColor.isPresent()) {
g.setColor(labelBackgroundColor.get());
g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
}

g.setColor(component.getForeground());
g.setFont(f);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

package org.baderlab.csplugins.enrichmentmap.model;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -409,7 +410,7 @@ public String toString(String ds) {
paramVariables.append(ds + "%" + simpleName + "%expressionUniverse\t" + expressionUniverse + "\n");

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

return paramVariables.toString();
}
Expand All @@ -418,14 +419,41 @@ public String toString(String ds) {
* Restores parameters saved in the session file.
* Note, most of this object is restored by the ExpressionFileReaderTask.
*/
public void restoreProps(String ds, Map<String, String> props) {
public void restoreProps(String ds, Map<String, String> props) {
String simpleName = this.getClass().getSimpleName();
String val = props.get(ds + "%" + simpleName + "%expressionUniverse");
if(val != null) {
try {
expressionUniverse = Integer.parseInt(val);
} catch(NumberFormatException e) {}
}

if(props.containsKey(ds + "%" + simpleName + "%phenotype1")) {
String prop = props.get(ds + "%" + simpleName + "%phenotype1");
if(!"null".equals(prop)) {
this.phenotype1 = prop;
}
}
if(props.containsKey(ds + "%" + simpleName + "%phenotype2")) {
String prop = props.get(ds + "%" + simpleName + "%phenotype2");
if(!"null".equals(prop)) {
this.phenotype2 = prop;
}
}

String[] col_names = getColumnNames();

if(props.containsKey(ds + "%" + simpleName + "%phenotypes") && col_names != null && col_names.length > 0) {
try {
String prop = props.get(ds + "%" + simpleName + "%phenotypes");
if(!prop.startsWith("[Ljava.lang.String")) { // avoid bug #144
String[] restored_phenotypes = prop.replace("[", "").replace("]", "").split(", ");
if(restored_phenotypes.length == col_names.length-2) {
this.phenotypes = restored_phenotypes;
}
}
} catch(Exception e) { }
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,9 @@ public void updatePanel() {
table.setCellSelectionEnabled(true);

//set the table header renderer to the vertical renderer
ColumnHeaderVerticalRenderer pheno1_renderer = new ColumnHeaderVerticalRenderer();
pheno1_renderer.setBackground(EnrichmentMapVisualStyle.LIGHTEST_PHENOTYPE_1);
ColumnHeaderVerticalRenderer pheno2_renderer = new ColumnHeaderVerticalRenderer();
pheno2_renderer.setBackground(EnrichmentMapVisualStyle.LIGHTEST_PHENOTYPE_2);

ColumnHeaderVerticalRenderer pheno1_renderer = new ColumnHeaderVerticalRenderer(EnrichmentMapVisualStyle.LIGHTEST_PHENOTYPE_1);
ColumnHeaderVerticalRenderer pheno2_renderer = new ColumnHeaderVerticalRenderer(EnrichmentMapVisualStyle.LIGHTEST_PHENOTYPE_2);
ColumnHeaderVerticalRenderer default_renderer = new ColumnHeaderVerticalRenderer();
default_renderer.setBackground(Color.white);

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

0 comments on commit e5fbfb0

Please sign in to comment.