mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/terrain/TerrainGUI.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2003, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4  */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * $RCSfile: TerrainGUI.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.7 $
2 26 Feb 07 jari 8  * $Date: 2006/03/24 15:52:04 $
2 26 Feb 07 jari 9  * $Author: eleanorahowe $
2 26 Feb 07 jari 10  * $State: Exp $
2 26 Feb 07 jari 11  */
2 26 Feb 07 jari 12 package org.tigr.microarray.mev.cluster.gui.impl.terrain;
2 26 Feb 07 jari 13
2 26 Feb 07 jari 14 import java.awt.event.ActionEvent;
2 26 Feb 07 jari 15 import java.awt.event.WindowEvent;
2 26 Feb 07 jari 16
2 26 Feb 07 jari 17 import javax.swing.JOptionPane;
2 26 Feb 07 jari 18 import javax.swing.tree.DefaultMutableTreeNode;
2 26 Feb 07 jari 19
2 26 Feb 07 jari 20 import org.tigr.microarray.mev.cluster.Cluster;
2 26 Feb 07 jari 21 import org.tigr.microarray.mev.cluster.NodeList;
2 26 Feb 07 jari 22 import org.tigr.microarray.mev.cluster.algorithm.AbortException;
2 26 Feb 07 jari 23 import org.tigr.microarray.mev.cluster.algorithm.Algorithm;
2 26 Feb 07 jari 24 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmData;
2 26 Feb 07 jari 25 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmEvent;
2 26 Feb 07 jari 26 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmException;
2 26 Feb 07 jari 27 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmListener;
2 26 Feb 07 jari 28 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmParameters;
2 26 Feb 07 jari 29 import org.tigr.microarray.mev.cluster.gui.Experiment;
2 26 Feb 07 jari 30 import org.tigr.microarray.mev.cluster.gui.IClusterGUI;
2 26 Feb 07 jari 31 import org.tigr.microarray.mev.cluster.gui.IFramework;
2 26 Feb 07 jari 32 import org.tigr.microarray.mev.cluster.gui.LeafInfo;
2 26 Feb 07 jari 33 import org.tigr.microarray.mev.cluster.gui.impl.dialogs.DialogListener;
2 26 Feb 07 jari 34 import org.tigr.microarray.mev.cluster.gui.impl.dialogs.Progress;
2 26 Feb 07 jari 35 import org.tigr.microarray.mev.script.scriptGUI.IScriptGUI;
2 26 Feb 07 jari 36 import org.tigr.util.FloatMatrix;
2 26 Feb 07 jari 37
2 26 Feb 07 jari 38 public class TerrainGUI implements IClusterGUI, IScriptGUI {
2 26 Feb 07 jari 39     
2 26 Feb 07 jari 40     private Algorithm algorithm;
2 26 Feb 07 jari 41     private Progress progress;
2 26 Feb 07 jari 42     
2 26 Feb 07 jari 43     /**
2 26 Feb 07 jari 44      * This method returns the terrain calculation result tree or null,
2 26 Feb 07 jari 45      * if analysis start was canceled.
2 26 Feb 07 jari 46      *
2 26 Feb 07 jari 47      * @param framework the reference to <code>IFramework</code> implementation,
2 26 Feb 07 jari 48      *        which is used to obtain an initial analysis data and parameters.
2 26 Feb 07 jari 49      * @throws AlgorithmException if calculation was failed.
2 26 Feb 07 jari 50      * @throws AbortException if calculation was canceled.
2 26 Feb 07 jari 51      * @see IFramework
2 26 Feb 07 jari 52      */
2 26 Feb 07 jari 53     public DefaultMutableTreeNode execute(IFramework framework) throws AlgorithmException {
2 26 Feb 07 jari 54         TerrainInitDialog dialog = new TerrainInitDialog(framework.getFrame());
2 26 Feb 07 jari 55         if (dialog.showModal() != JOptionPane.OK_OPTION)
2 26 Feb 07 jari 56             return null; // canceled
2 26 Feb 07 jari 57         
2 26 Feb 07 jari 58         boolean use_genes = dialog.isGenes();
2 26 Feb 07 jari 59         int neighbours = dialog.getNeighbours();
2 26 Feb 07 jari 60         
2 26 Feb 07 jari 61         // prepare the algo data
2 26 Feb 07 jari 62         AlgorithmData data = new AlgorithmData();
2 26 Feb 07 jari 63         FloatMatrix experiment = framework.getData().getExperiment().getMatrix();
2 26 Feb 07 jari 64         data.addMatrix("experiment", experiment);
2 26 Feb 07 jari 65         data.addParam("distance-factor", String.valueOf(1.0f));
2 26 Feb 07 jari 66         data.addParam("distance-absolute", String.valueOf(framework.getDistanceMenu().isAbsoluteDistance()));
2 26 Feb 07 jari 67         int function = framework.getDistanceMenu().getDistanceFunction();
2 26 Feb 07 jari 68         function = (function == Algorithm.DEFAULT) ? Algorithm.PEARSONSQARED : function;
2 26 Feb 07 jari 69         data.addParam("distance-function", String.valueOf(function));
2 26 Feb 07 jari 70         data.addParam("neighbors", String.valueOf(neighbours));
2 26 Feb 07 jari 71         data.addParam("use-genes", String.valueOf(use_genes));
2 26 Feb 07 jari 72         // the result general info
2 26 Feb 07 jari 73         GeneralInfo gi = new GeneralInfo();
2 26 Feb 07 jari 74         gi.function = framework.getDistanceMenu().getFunctionName(function);
2 26 Feb 07 jari 75         gi.absolute = framework.getDistanceMenu().isAbsoluteDistance();
2 26 Feb 07 jari 76         gi.neighbours = neighbours;
2 26 Feb 07 jari 77         gi.isGenes = use_genes;
2 26 Feb 07 jari 78         
2 26 Feb 07 jari 79         Listener listener = new Listener();
2 26 Feb 07 jari 80         this.progress = new Progress(framework.getFrame(), "Calculating Terrain", listener);
2 26 Feb 07 jari 81         this.progress.show();
2 26 Feb 07 jari 82         try {
2 26 Feb 07 jari 83             this.algorithm = framework.getAlgorithmFactory().getAlgorithm("Terrain");
2 26 Feb 07 jari 84             this.algorithm.addAlgorithmListener(listener);
2 26 Feb 07 jari 85             
2 26 Feb 07 jari 86             long start = System.currentTimeMillis();
2 26 Feb 07 jari 87             AlgorithmData result = algorithm.execute(data);
2 26 Feb 07 jari 88             gi.time = System.currentTimeMillis() - start;
2 26 Feb 07 jari 89             
2 26 Feb 07 jari 90             this.progress.setDescription("Creating 3D View...");
2 26 Feb 07 jari 91             return createResultTree(result, gi, framework);
2 26 Feb 07 jari 92         } finally {
2 26 Feb 07 jari 93             if (this.algorithm != null) {
2 26 Feb 07 jari 94                 this.algorithm.removeAlgorithmListener(listener);
2 26 Feb 07 jari 95             }
2 26 Feb 07 jari 96             if (this.progress != null) {
2 26 Feb 07 jari 97                 this.progress.dispose();
2 26 Feb 07 jari 98             }
2 26 Feb 07 jari 99         }
2 26 Feb 07 jari 100     }
2 26 Feb 07 jari 101     
2 26 Feb 07 jari 102     
2 26 Feb 07 jari 103     public AlgorithmData getScriptParameters(IFramework framework) {
2 26 Feb 07 jari 104         TerrainInitDialog dialog = new TerrainInitDialog(framework.getFrame());
2 26 Feb 07 jari 105         if (dialog.showModal() != JOptionPane.OK_OPTION)
2 26 Feb 07 jari 106             return null; // canceled
2 26 Feb 07 jari 107         
2 26 Feb 07 jari 108         boolean use_genes = dialog.isGenes();
2 26 Feb 07 jari 109         int neighbours = dialog.getNeighbours();
2 26 Feb 07 jari 110         
2 26 Feb 07 jari 111         // prepare the algo data
2 26 Feb 07 jari 112         AlgorithmData data = new AlgorithmData();
2 26 Feb 07 jari 113         data.addParam("distance-factor", String.valueOf(1.0f));
2 26 Feb 07 jari 114         data.addParam("distance-absolute", String.valueOf(framework.getDistanceMenu().isAbsoluteDistance()));
2 26 Feb 07 jari 115         int function = framework.getDistanceMenu().getDistanceFunction();
2 26 Feb 07 jari 116         function = (function == Algorithm.DEFAULT) ? Algorithm.PEARSONSQARED : function;
2 26 Feb 07 jari 117         data.addParam("distance-function", String.valueOf(function));
2 26 Feb 07 jari 118         data.addParam("neighbors", String.valueOf(neighbours));
2 26 Feb 07 jari 119         data.addParam("use-genes", String.valueOf(use_genes));
2 26 Feb 07 jari 120         
2 26 Feb 07 jari 121         //script control parameters
2 26 Feb 07 jari 122         
2 26 Feb 07 jari 123         // alg name
2 26 Feb 07 jari 124         data.addParam("name", "TRN");
2 26 Feb 07 jari 125         
2 26 Feb 07 jari 126         // alg type
2 26 Feb 07 jari 127         data.addParam("alg-type", "data-visualization");
2 26 Feb 07 jari 128         
2 26 Feb 07 jari 129         // output class
2 26 Feb 07 jari 130         data.addParam("output-class", "single-output");
2 26 Feb 07 jari 131         
2 26 Feb 07 jari 132         //output nodes
2 26 Feb 07 jari 133         String [] outputNodes = new String[1];
2 26 Feb 07 jari 134         outputNodes[0] = "Data Visualization";
2 26 Feb 07 jari 135         data.addStringArray("output-nodes", outputNodes);
2 26 Feb 07 jari 136         
2 26 Feb 07 jari 137         return data;
2 26 Feb 07 jari 138     }
2 26 Feb 07 jari 139     
2 26 Feb 07 jari 140     
2 26 Feb 07 jari 141     public DefaultMutableTreeNode executeScript(IFramework framework, AlgorithmData algData, Experiment experiment) throws AlgorithmException {
2 26 Feb 07 jari 142         FloatMatrix matrix = framework.getData().getExperiment().getMatrix();
2 26 Feb 07 jari 143         algData.addMatrix("experiment", matrix);
2 26 Feb 07 jari 144         
2 26 Feb 07 jari 145         // the result general info
2 26 Feb 07 jari 146         GeneralInfo gi = new GeneralInfo();
2 26 Feb 07 jari 147         AlgorithmParameters params = algData.getParams();
2 26 Feb 07 jari 148         gi.function = framework.getDistanceMenu().getFunctionName(params.getInt("distance-function"));
2 26 Feb 07 jari 149         gi.absolute = params.getBoolean("distance-absolute");
2 26 Feb 07 jari 150         gi.neighbours = params.getInt("neighbors");
2 26 Feb 07 jari 151         gi.isGenes = params.getBoolean("use-genes");
2 26 Feb 07 jari 152         
2 26 Feb 07 jari 153         Listener listener = new Listener();
2 26 Feb 07 jari 154         
2 26 Feb 07 jari 155         this.progress = new Progress(framework.getFrame(), "Calculating Terrain", listener);
2 26 Feb 07 jari 156         this.progress.show();
2 26 Feb 07 jari 157         try {
2 26 Feb 07 jari 158             this.algorithm = framework.getAlgorithmFactory().getAlgorithm("Terrain");
2 26 Feb 07 jari 159             this.algorithm.addAlgorithmListener(listener);
2 26 Feb 07 jari 160             
2 26 Feb 07 jari 161             long start = System.currentTimeMillis();
2 26 Feb 07 jari 162             AlgorithmData result = algorithm.execute(algData);
2 26 Feb 07 jari 163             gi.time = System.currentTimeMillis() - start;
2 26 Feb 07 jari 164             
2 26 Feb 07 jari 165             this.progress.setDescription("Creating 3D View...");
2 26 Feb 07 jari 166             return createResultTree(result, gi, framework);
2 26 Feb 07 jari 167         } finally {
2 26 Feb 07 jari 168             if (this.algorithm != null) {
2 26 Feb 07 jari 169                 this.algorithm.removeAlgorithmListener(listener);
2 26 Feb 07 jari 170             }
2 26 Feb 07 jari 171             if (this.progress != null) {
2 26 Feb 07 jari 172                 this.progress.dispose();
2 26 Feb 07 jari 173             }
2 26 Feb 07 jari 174         }
2 26 Feb 07 jari 175     }
2 26 Feb 07 jari 176     
2 26 Feb 07 jari 177     
2 26 Feb 07 jari 178     
2 26 Feb 07 jari 179     /**
2 26 Feb 07 jari 180      * Creates a result tree to be inserted into the framework analysis node.
2 26 Feb 07 jari 181      */
2 26 Feb 07 jari 182     private DefaultMutableTreeNode createResultTree(AlgorithmData result, GeneralInfo gi, IFramework framework) throws AlgorithmException {
2 26 Feb 07 jari 183         DefaultMutableTreeNode root = new DefaultMutableTreeNode("Terrain");
2 26 Feb 07 jari 184         addTerrainView(root, result, framework, gi);
2 26 Feb 07 jari 185         addGeneralInfo(root, gi);
2 26 Feb 07 jari 186         return root;
2 26 Feb 07 jari 187     }
2 26 Feb 07 jari 188     
2 26 Feb 07 jari 189     private void addTerrainView(DefaultMutableTreeNode root, AlgorithmData result, IFramework framework, GeneralInfo gi) throws AlgorithmException {
2 26 Feb 07 jari 190         FloatMatrix locations = result.getMatrix("locations");
2 26 Feb 07 jari 191         checkLocations(locations.A);
2 26 Feb 07 jari 192         Cluster cluster = result.getCluster("links");
2 26 Feb 07 jari 193         int[][] clusters = convert2int(cluster);
2 26 Feb 07 jari 194         float[][] weights = convert2float(cluster);
2 26 Feb 07 jari 195         AlgorithmParameters params = result.getParams();
2 26 Feb 07 jari 196         float sigma = params.getFloat("sigma");
2 26 Feb 07 jari 197         //EH: replaced SerializedTerrainViewer with TerrainViewer because state-saving has been
2 26 Feb 07 jari 198         //changed so the SerializedTerrainViewer wrapper is no longer needed.
2 26 Feb 07 jari 199 //        SerializedTerrainViewer viewer = new SerializedTerrainViewer(gi.isGenes, framework, clusters, weights, locations.A, sigma);
2 26 Feb 07 jari 200         TerrainViewer viewer = new TerrainViewer(gi.isGenes, framework.getData().getExperiment(), clusters, weights, locations.A, sigma, framework.getDisplayMenu().getLabelIndex());
2 26 Feb 07 jari 201
2 26 Feb 07 jari 202         root.add(new DefaultMutableTreeNode(new LeafInfo("Map", viewer)));
2 26 Feb 07 jari 203     }
2 26 Feb 07 jari 204     
2 26 Feb 07 jari 205     private void checkLocations(float[][] locations) throws AlgorithmException {
2 26 Feb 07 jari 206         if (locations == null)
2 26 Feb 07 jari 207             throw new AlgorithmException("Locations is null.");
2 26 Feb 07 jari 208         for (int i=0; i<locations.length; i++)
2 26 Feb 07 jari 209             for (int j=0; j<locations[i].length; j++)
2 26 Feb 07 jari 210                 if (Float.isNaN(locations[i][j]))
2 26 Feb 07 jari 211                     throw new AlgorithmException("Location["+i+"]["+j+"] is NaN.");
2 26 Feb 07 jari 212     }
2 26 Feb 07 jari 213     
2 26 Feb 07 jari 214     /**
2 26 Feb 07 jari 215      * Adds node with general iformation.
2 26 Feb 07 jari 216      */
2 26 Feb 07 jari 217     private void addGeneralInfo(DefaultMutableTreeNode root, GeneralInfo gi) {
2 26 Feb 07 jari 218         DefaultMutableTreeNode node = new DefaultMutableTreeNode("General Information");
2 26 Feb 07 jari 219         node.add(new DefaultMutableTreeNode("Time: "+String.valueOf(gi.time)+" ms"));
2 26 Feb 07 jari 220         node.add(new DefaultMutableTreeNode("Distance: "+gi.function));
2 26 Feb 07 jari 221         node.add(new DefaultMutableTreeNode("Absolute: "+gi.absolute));
2 26 Feb 07 jari 222         node.add(new DefaultMutableTreeNode("Neighbours: "+gi.neighbours));
2 26 Feb 07 jari 223         node.add(new DefaultMutableTreeNode("Data Type: "+(gi.isGenes ? "Genes" : "Samples")));
2 26 Feb 07 jari 224         root.add(node);
2 26 Feb 07 jari 225     }
2 26 Feb 07 jari 226     
2 26 Feb 07 jari 227     /**
2 26 Feb 07 jari 228      * Converts a passed cluster into a two dimensional int array.
2 26 Feb 07 jari 229      */
2 26 Feb 07 jari 230     private int[][] convert2int(Cluster cluster) throws AlgorithmException {
2 26 Feb 07 jari 231         NodeList nodeList = cluster.getNodeList();
2 26 Feb 07 jari 232         final int nodeListSize = nodeList.getSize();
2 26 Feb 07 jari 233         int[][] result = new int[nodeListSize][];
2 26 Feb 07 jari 234         for (int i=0; i<nodeListSize; i++) {
2 26 Feb 07 jari 235             result[i] = nodeList.getNode(i).getFeaturesIndexes();
2 26 Feb 07 jari 236             if (result[i] == null) {
2 26 Feb 07 jari 237                 throw new AlgorithmException("Cluster "+i+" does not contain indices.");
2 26 Feb 07 jari 238             }
2 26 Feb 07 jari 239         }
2 26 Feb 07 jari 240         return result;
2 26 Feb 07 jari 241     }
2 26 Feb 07 jari 242     
2 26 Feb 07 jari 243     private float[][] convert2float(Cluster cluster) throws AlgorithmException {
2 26 Feb 07 jari 244         NodeList nodeList = cluster.getNodeList();
2 26 Feb 07 jari 245         final int nodeListSize = nodeList.getSize();
2 26 Feb 07 jari 246         float[][] result = new float[nodeListSize][];
2 26 Feb 07 jari 247         int i=0;
2 26 Feb 07 jari 248         try {
2 26 Feb 07 jari 249             for (i=0; i<nodeListSize; i++) {
2 26 Feb 07 jari 250                 result[i] = (float[])nodeList.getNode(i).getValues().getNodeValue(0).value;
2 26 Feb 07 jari 251                 if (result[i] == null)
2 26 Feb 07 jari 252                     throw new Exception();
2 26 Feb 07 jari 253             }
2 26 Feb 07 jari 254         } catch (Exception e) {
2 26 Feb 07 jari 255             throw new AlgorithmException("Cluster "+i+" does not contain weights.");
2 26 Feb 07 jari 256         }
2 26 Feb 07 jari 257         return result;
2 26 Feb 07 jari 258     }
2 26 Feb 07 jari 259     
2 26 Feb 07 jari 260     
2 26 Feb 07 jari 261     
2 26 Feb 07 jari 262     /**
2 26 Feb 07 jari 263      * The class to listen to progress, monitor and algorithms events.
2 26 Feb 07 jari 264      */
2 26 Feb 07 jari 265     private class Listener extends DialogListener implements AlgorithmListener {
2 26 Feb 07 jari 266         
2 26 Feb 07 jari 267         public void valueChanged(AlgorithmEvent event) {
2 26 Feb 07 jari 268             switch (event.getId()) {
2 26 Feb 07 jari 269                 case AlgorithmEvent.SET_UNITS:
2 26 Feb 07 jari 270                     TerrainGUI.this.progress.setUnits(event.getIntValue());
2 26 Feb 07 jari 271                     TerrainGUI.this.progress.setDescription(event.getDescription());
2 26 Feb 07 jari 272                     break;
2 26 Feb 07 jari 273                 case AlgorithmEvent.PROGRESS_VALUE:
2 26 Feb 07 jari 274                     TerrainGUI.this.progress.setValue(event.getIntValue());
2 26 Feb 07 jari 275                     TerrainGUI.this.progress.setDescription(event.getDescription());
2 26 Feb 07 jari 276                     break;
2 26 Feb 07 jari 277             }
2 26 Feb 07 jari 278         }
2 26 Feb 07 jari 279         
2 26 Feb 07 jari 280         public void actionPerformed(ActionEvent e) {
2 26 Feb 07 jari 281             String command = e.getActionCommand();
2 26 Feb 07 jari 282             if (command.equals("cancel-command")) {
2 26 Feb 07 jari 283                 TerrainGUI.this.algorithm.abort();
2 26 Feb 07 jari 284                 TerrainGUI.this.progress.dispose();
2 26 Feb 07 jari 285             }
2 26 Feb 07 jari 286         }
2 26 Feb 07 jari 287         
2 26 Feb 07 jari 288         public void windowClosing(WindowEvent e) {
2 26 Feb 07 jari 289             TerrainGUI.this.algorithm.abort();
2 26 Feb 07 jari 290             TerrainGUI.this.progress.dispose();
2 26 Feb 07 jari 291         }
2 26 Feb 07 jari 292     }
2 26 Feb 07 jari 293     
2 26 Feb 07 jari 294     
2 26 Feb 07 jari 295     private class GeneralInfo {
2 26 Feb 07 jari 296         public long time;
2 26 Feb 07 jari 297         public String function;
2 26 Feb 07 jari 298         public boolean absolute;
2 26 Feb 07 jari 299         public int neighbours;
2 26 Feb 07 jari 300         public boolean isGenes;
2 26 Feb 07 jari 301     }
2 26 Feb 07 jari 302 }
2 26 Feb 07 jari 303