mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/svm/SVMOneOutViewer.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2004, 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: SVMOneOutViewer.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.8 $
2 26 Feb 07 jari 8  * $Date: 2006/05/02 16:57:36 $
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
2 26 Feb 07 jari 13 package org.tigr.microarray.mev.cluster.gui.impl.svm;
2 26 Feb 07 jari 14
2 26 Feb 07 jari 15 import java.awt.Dimension;
2 26 Feb 07 jari 16 import java.awt.GridBagConstraints;
2 26 Feb 07 jari 17 import java.awt.Insets;
2 26 Feb 07 jari 18 import java.beans.Expression;
2 26 Feb 07 jari 19 import java.io.BufferedWriter;
2 26 Feb 07 jari 20 import java.io.File;
2 26 Feb 07 jari 21 import java.io.FileOutputStream;
2 26 Feb 07 jari 22 import java.io.OutputStreamWriter;
2 26 Feb 07 jari 23 import java.text.DecimalFormat;
2 26 Feb 07 jari 24
2 26 Feb 07 jari 25 import javax.swing.JComponent;
2 26 Feb 07 jari 26 import javax.swing.JFileChooser;
2 26 Feb 07 jari 27 import javax.swing.JOptionPane;
2 26 Feb 07 jari 28 import javax.swing.JScrollPane;
2 26 Feb 07 jari 29 import javax.swing.JTable;
2 26 Feb 07 jari 30 import javax.swing.table.AbstractTableModel;
2 26 Feb 07 jari 31
2 26 Feb 07 jari 32 import org.tigr.microarray.mev.TMEV;
2 26 Feb 07 jari 33 import org.tigr.microarray.mev.cluster.gui.Experiment;
2 26 Feb 07 jari 34 import org.tigr.microarray.mev.cluster.gui.IFramework;
2 26 Feb 07 jari 35 import org.tigr.microarray.mev.cluster.gui.helpers.ExpressionFileFilter;
2 26 Feb 07 jari 36 import org.tigr.microarray.mev.cluster.gui.helpers.ExpressionFileView;
2 26 Feb 07 jari 37 import org.tigr.microarray.mev.cluster.gui.impl.svm.SVMResultViewer.MyListener;
2 26 Feb 07 jari 38 import org.tigr.util.FloatMatrix;
2 26 Feb 07 jari 39
2 26 Feb 07 jari 40 public class SVMOneOutViewer extends SVMResultViewer {
2 26 Feb 07 jari 41     
2 26 Feb 07 jari 42     private FloatMatrix discriminant;
2 26 Feb 07 jari 43
2 26 Feb 07 jari 44     private boolean classifyGenes;
2 26 Feb 07 jari 45     float [] classes;
2 26 Feb 07 jari 46     float [] discr;
2 26 Feb 07 jari 47     int [] initClasses;
2 26 Feb 07 jari 48     float [] classMatch;
2 26 Feb 07 jari 49     
2 26 Feb 07 jari 50     int [] elementScores;
2 26 Feb 07 jari 51     int [] iterationScores;
2 26 Feb 07 jari 52     int nonNeuts;
2 26 Feb 07 jari 53     
2 26 Feb 07 jari 54     DecimalFormat floatFormat, intFormat, indexFormat;
2 26 Feb 07 jari 55     
2 26 Feb 07 jari 56     OneOutViewerTableModel ovtm;
2 26 Feb 07 jari 57     
2 26 Feb 07 jari 58     /**
2 26 Feb 07 jari 59      * Create a new SVMOneOutViewer to display provided data in discriminant
2 26 Feb 07 jari 60      */
2 26 Feb 07 jari 61     public SVMOneOutViewer(Experiment experiment, FloatMatrix discriminant, boolean classifyGenes, int [] initClasses, int [] elementScores, int [] iterationScores, int nonNeuts){
2 26 Feb 07 jari 62        super(experiment);
2 26 Feb 07 jari 63         this.discriminant = discriminant;
2 26 Feb 07 jari 64          FloatMatrix M = discriminant.transpose();
2 26 Feb 07 jari 65          init(M.A[0], M.A[1], classifyGenes, initClasses, elementScores, iterationScores, nonNeuts);
2 26 Feb 07 jari 66     }
2 26 Feb 07 jari 67     
2 26 Feb 07 jari 68     public SVMOneOutViewer(Experiment experiment, FloatMatrix discriminant, Boolean classifyGenes, int [] initClasses, int [] elementScores, int [] iterationScores, Integer nonNeuts){
2 26 Feb 07 jari 69       this(experiment, discriminant, classifyGenes.booleanValue(), initClasses, elementScores, iterationScores, nonNeuts.intValue());
2 26 Feb 07 jari 70     }
2 26 Feb 07 jari 71     /**
2 26 Feb 07 jari 72      * @inheritDoc
2 26 Feb 07 jari 73      */
2 26 Feb 07 jari 74     public Expression getExpression(){
2 26 Feb 07 jari 75       return new Expression(this, this.getClass(), "new", 
2 26 Feb 07 jari 76           new Object[]{super.getExpression().getArguments()[0], discriminant, new Boolean(classifyGenes), initClasses, elementScores, iterationScores, new Integer(nonNeuts)});
2 26 Feb 07 jari 77     }
2 26 Feb 07 jari 78     private void init(float[] classes, float[] discr, boolean classifyGenes, int[] initClasses, int[] elementScores, int[] iterationScores, int nonNeuts){
2 26 Feb 07 jari 79       this.classes = classes;
2 26 Feb 07 jari 80       this.discr = discr;
2 26 Feb 07 jari 81       
2 26 Feb 07 jari 82         this.classifyGenes = classifyGenes;
2 26 Feb 07 jari 83         this.initClasses = initClasses;
2 26 Feb 07 jari 84         this.elementScores = elementScores;
2 26 Feb 07 jari 85         this.iterationScores = iterationScores;
2 26 Feb 07 jari 86         this.nonNeuts = nonNeuts;
2 26 Feb 07 jari 87         
2 26 Feb 07 jari 88         floatFormat = new DecimalFormat();
2 26 Feb 07 jari 89         floatFormat.setMaximumFractionDigits(4);
2 26 Feb 07 jari 90         floatFormat.setMinimumFractionDigits(4);
2 26 Feb 07 jari 91         floatFormat.setGroupingUsed(false);
2 26 Feb 07 jari 92     
2 26 Feb 07 jari 93         intFormat = new DecimalFormat();
2 26 Feb 07 jari 94         intFormat.setMinimumFractionDigits(0);
2 26 Feb 07 jari 95         intFormat.setMaximumFractionDigits(0);
2 26 Feb 07 jari 96         intFormat.setGroupingUsed(false);
2 26 Feb 07 jari 97         
2 26 Feb 07 jari 98         indexFormat = new DecimalFormat();
2 26 Feb 07 jari 99         indexFormat = new DecimalFormat();
2 26 Feb 07 jari 100         indexFormat.setMinimumFractionDigits(0);
2 26 Feb 07 jari 101         indexFormat.setMaximumFractionDigits(0);
2 26 Feb 07 jari 102         
2 26 Feb 07 jari 103         ovtm = new OneOutViewerTableModel();
2 26 Feb 07 jari 104         
2 26 Feb 07 jari 105        this.resultTable = new JTable(ovtm);
2 26 Feb 07 jari 106        this.add(new JScrollPane(resultTable), new GridBagConstraints(0,0,1,1,1.0,1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0,0));
2 26 Feb 07 jari 107         
2 26 Feb 07 jari 108         MyListener listener = new MyListener();
2 26 Feb 07 jari 109         resultTable.addMouseListener(listener);
2 26 Feb 07 jari 110         resultTable.addMouseMotionListener(listener);
2 26 Feb 07 jari 111     }
2 26 Feb 07 jari 112     
2 26 Feb 07 jari 113     class OneOutViewerTableModel extends AbstractTableModel{
2 26 Feb 07 jari 114       String[] header = new String[]{"Index", "Init", "Class", "Discriminant", "Element Score", "%", "Iteration Score", "%", annotationLabel};
2 26 Feb 07 jari 115         public OneOutViewerTableModel(){
2 26 Feb 07 jari 116         
2 26 Feb 07 jari 117       }
2 26 Feb 07 jari 118       public String getColumnName(int col){
2 26 Feb 07 jari 119         return header[col];
2 26 Feb 07 jari 120       }
2 26 Feb 07 jari 121       public Object getValueAt(int row, int col){
2 26 Feb 07 jari 122         if(col == 0){
2 26 Feb 07 jari 123           return indexFormat.format(row+1);
2 26 Feb 07 jari 124         } else if (col == 1) {
2 26 Feb 07 jari 125             if(initClasses[row] == -1.0f)
2 26 Feb 07 jari 126             return "none";
2 26 Feb 07 jari 127             return intFormat.format(initClasses[row]);
2 26 Feb 07 jari 128             } else if (col == 2) {
2 26 Feb 07 jari 129             if(classes[row] == -1.0f)
2 26 Feb 07 jari 130             return "none";
2 26 Feb 07 jari 131           return intFormat.format(classes[row]).toString();
2 26 Feb 07 jari 132         } else if (col == 3) {
2 26 Feb 07 jari 133           return floatFormat.format(discr[row]);
2 26 Feb 07 jari 134         } else if (col == 4) {
2 26 Feb 07 jari 135           return (intFormat.format(elementScores[row]) + "/" + intFormat.format(elementScores.length)).toString();
2 26 Feb 07 jari 136         } else if (col == 5) {
2 26 Feb 07 jari 137           return intFormat.format((elementScores[row] / elementScores.length) * 100);
2 26 Feb 07 jari 138         } else if (col == 6) {
2 26 Feb 07 jari 139           return (intFormat.format(iterationScores[row]) + "/" + intFormat.format(iterationScores.length)).toString();
2 26 Feb 07 jari 140         } else if (col == 7) {
2 26 Feb 07 jari 141           return intFormat.format((iterationScores[row] / iterationScores.length) * 100);
2 26 Feb 07 jari 142           } else if (col == 8) {
2 26 Feb 07 jari 143             try {
2 26 Feb 07 jari 144           if(classifyGenes)
2 26 Feb 07 jari 145           return iData.getElementAttribute(getMultipleArrayDataRow(row), labelIndex);
2 26 Feb 07 jari 146         else 
2 26 Feb 07 jari 147           return iData.getSampleName(row);
2 26 Feb 07 jari 148             } catch (NullPointerException npe){
2 26 Feb 07 jari 149               npe.printStackTrace();
2 26 Feb 07 jari 150               return  "";
2 26 Feb 07 jari 151     }    
2 26 Feb 07 jari 152         }
2 26 Feb 07 jari 153         return new String("");
2 26 Feb 07 jari 154       }  
2 26 Feb 07 jari 155       public boolean isCellEditable(int row, int col) { return false; }
2 26 Feb 07 jari 156
2 26 Feb 07 jari 157     public int getColumnCount() {
2 26 Feb 07 jari 158       return header.length;
2 26 Feb 07 jari 159     }
2 26 Feb 07 jari 160     
2 26 Feb 07 jari 161     public int getRowCount() {
2 26 Feb 07 jari 162       return classes.length;
2 26 Feb 07 jari 163     }
2 26 Feb 07 jari 164     }
2 26 Feb 07 jari 165     public void onSelected(IFramework frm) {
2 26 Feb 07 jari 166       super.onSelected(frm);
2 26 Feb 07 jari 167         this.framework = frm;
2 26 Feb 07 jari 168         onMenuChanged(frm.getDisplayMenu());
2 26 Feb 07 jari 169     }    
2 26 Feb 07 jari 170     
2 26 Feb 07 jari 171     /**
2 26 Feb 07 jari 172      * Displays data.
2 26 Feb 07 jari 173      */
2 26 Feb 07 jari 174     protected void displayData() {
2 26 Feb 07 jari 175         displayResult(this.classifyGenes);
2 26 Feb 07 jari 176         //  this.resultPanel.repaint();
2 26 Feb 07 jari 177     }
2 26 Feb 07 jari 178     
2 26 Feb 07 jari 179     private void displayResult(boolean genes){
2 26 Feb 07 jari 180
2 26 Feb 07 jari 181     }
2 26 Feb 07 jari 182     
2 26 Feb 07 jari 183     /**
2 26 Feb 07 jari 184      * Saves train result to a file.
2 26 Feb 07 jari 185      */
2 26 Feb 07 jari 186     protected void onSaveResult() {
2 26 Feb 07 jari 187         File SVMFile;
2 26 Feb 07 jari 188         final JFileChooser fc = new JFileChooser(TMEV.getFile("data/"));
2 26 Feb 07 jari 189         fc.addChoosableFileFilter(new ExpressionFileFilter());
2 26 Feb 07 jari 190         fc.setFileView(new ExpressionFileView());
2 26 Feb 07 jari 191         int returnVal = fc.showSaveDialog(JOptionPane.getFrameForComponent(this));
2 26 Feb 07 jari 192         if (returnVal == JFileChooser.APPROVE_OPTION) {
2 26 Feb 07 jari 193             SVMFile = fc.getSelectedFile();
2 26 Feb 07 jari 194         } else return;
2 26 Feb 07 jari 195         try {
2 26 Feb 07 jari 196             BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(SVMFile)));
2 26 Feb 07 jari 197             for(int row=0; row<ovtm.getRowCount(); row++){
2 26 Feb 07 jari 198               for(int col=0; col<ovtm.getColumnCount(); col++){
2 26 Feb 07 jari 199                 out.write(ovtm.getValueAt(row, col).toString() + '\t');
2 26 Feb 07 jari 200             }
2 26 Feb 07 jari 201               out.write('\n');
2 26 Feb 07 jari 202             }
2 26 Feb 07 jari 203             out.flush();
2 26 Feb 07 jari 204             out.close();
2 26 Feb 07 jari 205             out = null;
2 26 Feb 07 jari 206         } catch (Exception ex) {
2 26 Feb 07 jari 207             ex.printStackTrace();
2 26 Feb 07 jari 208             JOptionPane.showMessageDialog( this, "Error writing to file "+SVMFile.getPath()+"!","Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 209         }
2 26 Feb 07 jari 210     }
2 26 Feb 07 jari 211     
2 26 Feb 07 jari 212     
2 26 Feb 07 jari 213     protected Dimension updateSize(){
2 26 Feb 07 jari 214         return new Dimension(120, getHeight());
2 26 Feb 07 jari 215     }
2 26 Feb 07 jari 216     
2 26 Feb 07 jari 217     /** Returns a component to be inserted into the scroll pane row header
2 26 Feb 07 jari 218      */
2 26 Feb 07 jari 219     public JComponent getRowHeaderComponent() {
2 26 Feb 07 jari 220         return null;
2 26 Feb 07 jari 221     }    
2 26 Feb 07 jari 222     
2 26 Feb 07 jari 223     /** Returns the corner component corresponding to the indicated corner,
2 26 Feb 07 jari 224      * posibly null
2 26 Feb 07 jari 225      */
2 26 Feb 07 jari 226     public JComponent getCornerComponent(int cornerIndex) {
2 26 Feb 07 jari 227         return null;
2 26 Feb 07 jari 228     }    
2 26 Feb 07 jari 229     
2 26 Feb 07 jari 230       /**
2 26 Feb 07 jari 231    * @return Returns the classifyGenes.
2 26 Feb 07 jari 232    */
2 26 Feb 07 jari 233   public boolean isClassifyGenes() {
2 26 Feb 07 jari 234     return classifyGenes;
2 26 Feb 07 jari 235             }
2 26 Feb 07 jari 236   /**
2 26 Feb 07 jari 237    * @return Returns the discr.
2 26 Feb 07 jari 238    */
2 26 Feb 07 jari 239   public float[] getDiscr() {
2 26 Feb 07 jari 240     return discr;
2 26 Feb 07 jari 241             }
2 26 Feb 07 jari 242   /**
2 26 Feb 07 jari 243    * @return Returns the nonNeuts.
2 26 Feb 07 jari 244    */
2 26 Feb 07 jari 245   public int getNonNeuts() {
2 26 Feb 07 jari 246     return nonNeuts;
2 26 Feb 07 jari 247         }
2 26 Feb 07 jari 248   /**
2 26 Feb 07 jari 249    * @return Returns the elementScores.
2 26 Feb 07 jari 250    */
2 26 Feb 07 jari 251   public int[] getElementScores() {
2 26 Feb 07 jari 252     return elementScores;
2 26 Feb 07 jari 253             }
2 26 Feb 07 jari 254   /**
2 26 Feb 07 jari 255    * @return Returns the iterationScores.
2 26 Feb 07 jari 256    */
2 26 Feb 07 jari 257   public int[] getIterationScores() {
2 26 Feb 07 jari 258     return iterationScores;
2 26 Feb 07 jari 259         }
2 26 Feb 07 jari 260   /**
2 26 Feb 07 jari 261    * @return Returns the initClasses.
2 26 Feb 07 jari 262    */
2 26 Feb 07 jari 263   public int[] getInitClasses() {
2 26 Feb 07 jari 264     return initClasses;
2 26 Feb 07 jari 265     }
2 26 Feb 07 jari 266     
2 26 Feb 07 jari 267     public float[] getClasses(){return classes;}
2 26 Feb 07 jari 268 }
2 26 Feb 07 jari 269