|
| 1 | +package org.baderlab.csplugins.enrichmentmap.view.creation; |
| 2 | + |
| 3 | +import static org.baderlab.csplugins.enrichmentmap.EMBuildProps.HELP_URL_AUTO; |
| 4 | + |
| 5 | +import java.awt.BorderLayout; |
| 6 | +import java.awt.Dimension; |
| 7 | +import java.awt.GridBagConstraints; |
| 8 | +import java.awt.GridBagLayout; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import javax.swing.BorderFactory; |
| 13 | +import javax.swing.JButton; |
| 14 | +import javax.swing.JDialog; |
| 15 | +import javax.swing.JLabel; |
| 16 | +import javax.swing.JOptionPane; |
| 17 | +import javax.swing.JPanel; |
| 18 | +import javax.swing.JScrollPane; |
| 19 | +import javax.swing.JTextArea; |
| 20 | +import javax.swing.UIManager; |
| 21 | +import javax.swing.WindowConstants; |
| 22 | + |
| 23 | +import org.baderlab.csplugins.enrichmentmap.commands.tunables.FilterTunables; |
| 24 | +import org.baderlab.csplugins.enrichmentmap.model.DataSetParameters; |
| 25 | +import org.baderlab.csplugins.enrichmentmap.model.EMCreationParameters; |
| 26 | +import org.baderlab.csplugins.enrichmentmap.model.EMCreationParameters.SimilarityMetric; |
| 27 | +import org.baderlab.csplugins.enrichmentmap.util.PathUtil; |
| 28 | +import org.baderlab.csplugins.enrichmentmap.view.util.GBCFactory; |
| 29 | +import org.baderlab.csplugins.enrichmentmap.view.util.OpenBrowser; |
| 30 | +import org.baderlab.csplugins.enrichmentmap.view.util.SwingUtil; |
| 31 | +import org.cytoscape.util.swing.LookAndFeelUtil; |
| 32 | + |
| 33 | +import com.google.inject.Inject; |
| 34 | + |
| 35 | +@SuppressWarnings("serial") |
| 36 | +public class CommandDisplayMediator { |
| 37 | + |
| 38 | + @Inject private OpenBrowser openBrowser; |
| 39 | + |
| 40 | + |
| 41 | + public void showCommand( |
| 42 | + JDialog parent, |
| 43 | + EMCreationParameters params, |
| 44 | + List<DataSetParameters> dataSets, |
| 45 | + String commonExprFile, |
| 46 | + String commonGMTFile, |
| 47 | + String commonClassFile |
| 48 | + ) { |
| 49 | + Path root = getCommonRoot(dataSets); |
| 50 | + if(root == null) { |
| 51 | + showFailDialog(parent); |
| 52 | + } else { |
| 53 | + String command = generateCommand(root, params, dataSets, commonExprFile, commonGMTFile, commonClassFile); |
| 54 | + CommandDisplayDialog dialog = new CommandDisplayDialog(parent, command); |
| 55 | + dialog.showDialog(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static void showFailDialog(JDialog parent) { |
| 60 | + String message = "Data Set files do not have a common root folder."; |
| 61 | + String title = "Create EnrichmentMap Command"; |
| 62 | + JOptionPane.showMessageDialog(parent, message, title, JOptionPane.WARNING_MESSAGE); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + private static Path getCommonRoot(List<DataSetParameters> dataSets) { |
| 67 | + List<Path> dataSetRoots = PathUtil.dataSetsRoots(dataSets); |
| 68 | + Path r = PathUtil.commonRoot(dataSetRoots); |
| 69 | + |
| 70 | + if(r == null || r.getNameCount() == 1) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + for(Path p : dataSetRoots) { |
| 74 | + if(!(p.startsWith(r) && p.getNameCount() - r.getNameCount() < 2)) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + } |
| 78 | + return r; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + private static String generateCommand( |
| 83 | + Path root, |
| 84 | + EMCreationParameters params, |
| 85 | + List<DataSetParameters> dataSets, |
| 86 | + String commonExprFile, |
| 87 | + String commonGMTFile, |
| 88 | + String commonClassFile |
| 89 | + ) { |
| 90 | + StringBuilder command = new StringBuilder("enrichmentmap mastermap "); |
| 91 | + command.append("rootFolder=\"").append(root).append("\" "); |
| 92 | + |
| 93 | + FilterTunables defaults = new FilterTunables(); |
| 94 | + |
| 95 | + if(params.getSimilarityMetric() != defaults.getSimilarityMetric()) { |
| 96 | + command.append("coefficients=").append(params.getSimilarityMetric().name()).append(" "); |
| 97 | + } |
| 98 | + if(params.getPvalue() != defaults.pvalue) { |
| 99 | + command.append("pvalue=").append(params.getPvalue()).append(" "); |
| 100 | + } |
| 101 | + if(params.getQvalue() != defaults.qvalue) { |
| 102 | + command.append("qvalue=").append(params.getQvalue()).append(" "); |
| 103 | + } |
| 104 | + if(params.getSimilarityCutoff() != defaults.similaritycutoff) { |
| 105 | + command.append("similaritycutoff=").append(params.getSimilarityCutoff()).append(" "); |
| 106 | + } |
| 107 | + if(params.getSimilarityMetric() == SimilarityMetric.COMBINED) { |
| 108 | + command.append("combinedConstant=").append(params.getCombinedConstant()).append(" "); |
| 109 | + } |
| 110 | + if(params.getEdgeStrategy() != defaults.getEdgeStrategy()) { |
| 111 | + command.append("edgeStrategy=").append(params.getEdgeStrategy().name()).append(" "); |
| 112 | + } |
| 113 | + if(params.getMinExperiments().isPresent()) { |
| 114 | + command.append("minExperiments=").append(params.getMinExperiments().get()).append(" "); |
| 115 | + } |
| 116 | + if(params.getNESFilter() != defaults.getNesFilter()) { |
| 117 | + command.append("nesFilter=").append(params.getNESFilter().name()).append(" "); |
| 118 | + } |
| 119 | + if(params.isFilterByExpressions() != defaults.filterByExpressions) { |
| 120 | + command.append("filterByExpressions=").append(params.isFilterByExpressions()).append(" "); |
| 121 | + } |
| 122 | + if(params.isParseBaderlabGeneSets() != defaults.parseBaderlabNames) { |
| 123 | + command.append("parseBaderlabNames=").append(params.isParseBaderlabGeneSets()).append(" "); |
| 124 | + } |
| 125 | + if(params.getNetworkName() != null) { |
| 126 | + command.append("networkName=\"").append(params.getNetworkName()).append("\" "); |
| 127 | + } |
| 128 | + if(commonGMTFile != null) { |
| 129 | + command.append("commonGMTFile=\"").append(commonGMTFile).append("\" "); |
| 130 | + } |
| 131 | + if(commonExprFile != null) { |
| 132 | + command.append("commonExpressionFile=\"").append(commonExprFile).append("\" "); |
| 133 | + } |
| 134 | + if(commonClassFile != null) { |
| 135 | + command.append("commonClassFile=\"").append(commonClassFile).append("\" "); |
| 136 | + } |
| 137 | + |
| 138 | + return command.toString(); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + private class CommandDisplayDialog extends JDialog { |
| 143 | + |
| 144 | + public CommandDisplayDialog(JDialog parent, String command) { |
| 145 | + super(parent); |
| 146 | + setResizable(true); |
| 147 | + setTitle("EnrichmentMap: Command"); |
| 148 | + setMinimumSize(new Dimension(500, 300)); |
| 149 | + setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 150 | + |
| 151 | + JPanel messagePanel = createMessagePanel(); |
| 152 | + JPanel commandPanel = createCommandPanel(command); |
| 153 | + JPanel buttonPanel = createButtonPanel(); |
| 154 | + |
| 155 | + messagePanel.setBorder(BorderFactory.createEmptyBorder(8,8,8,8)); |
| 156 | + commandPanel.setBorder(BorderFactory.createEmptyBorder(8,8,8,8)); |
| 157 | + buttonPanel .setBorder(BorderFactory.createMatteBorder(1,0,0,0, UIManager.getColor("Separator.foreground"))); |
| 158 | + |
| 159 | + JPanel panel = new JPanel(new BorderLayout()); |
| 160 | + panel.add(messagePanel, BorderLayout.NORTH); |
| 161 | + panel.add(commandPanel, BorderLayout.CENTER); |
| 162 | + panel.add(buttonPanel, BorderLayout.SOUTH); |
| 163 | + setContentPane(panel); |
| 164 | + } |
| 165 | + |
| 166 | + public void showDialog() { |
| 167 | + setLocationRelativeTo(getParent()); |
| 168 | + pack(); |
| 169 | + setVisible(true); |
| 170 | + } |
| 171 | + |
| 172 | + private JPanel createMessagePanel() { |
| 173 | + String message = |
| 174 | + "<html>The command below can be used in a script to automate the<br>" |
| 175 | + + "creation of an EnrichmentMap network. Data files are expected to<br>" |
| 176 | + + "be under a common root folder. The command arguments are based<br>" |
| 177 | + + "on the values currently entered in the Create EnrichmentMap Dialog.</html>"; |
| 178 | + JLabel label = new JLabel(message); |
| 179 | + JButton link = SwingUtil.createLinkButton(openBrowser, "View online help", HELP_URL_AUTO); |
| 180 | + |
| 181 | + JPanel panel = new JPanel(new GridBagLayout()); |
| 182 | + panel.add(label, GBCFactory.grid(0,0).weightx(1.0).anchor(GridBagConstraints.WEST).get()); |
| 183 | + panel.add(link, GBCFactory.grid(0,1).fill(GridBagConstraints.NONE).anchor(GridBagConstraints.WEST).get()); |
| 184 | + return panel; |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + private JPanel createCommandPanel(String command) { |
| 189 | + JTextArea textArea = new JTextArea(command); |
| 190 | + textArea.setEditable(false); |
| 191 | + textArea.setLineWrap(true); |
| 192 | + textArea.setWrapStyleWord(true); |
| 193 | + JScrollPane scrollPane = new JScrollPane(textArea); |
| 194 | + |
| 195 | + JPanel panel = new JPanel(new BorderLayout()); |
| 196 | + panel.add(scrollPane, BorderLayout.CENTER); |
| 197 | + return panel; |
| 198 | + } |
| 199 | + |
| 200 | + private JPanel createButtonPanel() { |
| 201 | + JButton okButton = new JButton("Close"); |
| 202 | + okButton.addActionListener(e -> dispose()); |
| 203 | + return LookAndFeelUtil.createOkCancelPanel(okButton, null); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + |
| 208 | +} |
0 commit comments