mev-4.0.01/source/org/tigr/microarray/mev/cluster/algorithm/impl/FOM.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: FOM.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.5 $
2 26 Feb 07 jari 8  * $Date: 2005/03/10 15:45:27 $
2 26 Feb 07 jari 9  * $Author: braistedj $
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.algorithm.impl;
2 26 Feb 07 jari 13
2 26 Feb 07 jari 14 import org.tigr.microarray.mev.cluster.Cluster;
2 26 Feb 07 jari 15 import org.tigr.microarray.mev.cluster.NodeList;
2 26 Feb 07 jari 16 import org.tigr.microarray.mev.cluster.algorithm.AbortException;
2 26 Feb 07 jari 17 import org.tigr.microarray.mev.cluster.algorithm.AbstractAlgorithm;
2 26 Feb 07 jari 18 import org.tigr.microarray.mev.cluster.algorithm.Algorithm;
2 26 Feb 07 jari 19 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmData;
2 26 Feb 07 jari 20 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmEvent;
2 26 Feb 07 jari 21 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmException;
2 26 Feb 07 jari 22 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmParameters;
2 26 Feb 07 jari 23 import org.tigr.util.FloatMatrix;
2 26 Feb 07 jari 24
2 26 Feb 07 jari 25 public class FOM extends AbstractAlgorithm {
2 26 Feb 07 jari 26     
2 26 Feb 07 jari 27     private boolean stop = false;
2 26 Feb 07 jari 28     private boolean clusterGenes;
2 26 Feb 07 jari 29     /**
2 26 Feb 07 jari 30      * This method execute calculation and return result,
2 26 Feb 07 jari 31      * stored in <code>AlgorithmData</code> class.
2 26 Feb 07 jari 32      *
2 26 Feb 07 jari 33      * @param data the data to be calculated.
2 26 Feb 07 jari 34      */
2 26 Feb 07 jari 35     public AlgorithmData execute(AlgorithmData data) throws AlgorithmException {
2 26 Feb 07 jari 36         
2 26 Feb 07 jari 37         AlgorithmParameters map = data.getParams();
2 26 Feb 07 jari 38         
2 26 Feb 07 jari 39         int function = map.getInt("distance-function", EUCLIDEAN);
2 26 Feb 07 jari 40         float factor   = map.getFloat("distance-factor", 1.0f);
2 26 Feb 07 jari 41         boolean absolute = map.getBoolean("distance-absolute", false);
2 26 Feb 07 jari 42         
2 26 Feb 07 jari 43         clusterGenes = map.getBoolean("cluster-genes", true);
2 26 Feb 07 jari 44         int fomIterations = map.getInt("fom-iterations", 1);
2 26 Feb 07 jari 45         int method      = map.getInt("method", 2);
2 26 Feb 07 jari 46         float interval  = map.getFloat("interval", 0.1f);
2 26 Feb 07 jari 47         int iterations  = map.getInt("iterations", 50);
2 26 Feb 07 jari 48         int maxNumClusters = map.getInt("number-of-clusters", 20);
2 26 Feb 07 jari 49         boolean average = map.getBoolean("average", true);
2 26 Feb 07 jari 50         boolean calculateMeans = map.getBoolean("calculate-means", true);
2 26 Feb 07 jari 51         
2 26 Feb 07 jari 52         FloatMatrix expMatrix = data.getMatrix("experiment");
2 26 Feb 07 jari 53         
2 26 Feb 07 jari 54         int number_of_genes   = expMatrix.getRowDimension();
2 26 Feb 07 jari 55         int number_of_samples = expMatrix.getColumnDimension();
2 26 Feb 07 jari 56         
2 26 Feb 07 jari 57         Algorithm sub_algo = null;
2 26 Feb 07 jari 58         if (method == 2) sub_algo = new KMC();
2 26 Feb 07 jari 59         else sub_algo = new CastClust(true);
2 26 Feb 07 jari 60         
2 26 Feb 07 jari 61         AlgorithmData sub_algo_data = new AlgorithmData();
2 26 Feb 07 jari 62         sub_algo_data.addMatrix("experiment", expMatrix);
2 26 Feb 07 jari 63         sub_algo_data.addParam("distance-factor", String.valueOf(factor));
2 26 Feb 07 jari 64         sub_algo_data.addParam("distance-absolute", String.valueOf(absolute));
2 26 Feb 07 jari 65         sub_algo_data.addParam("distance-function", String.valueOf(function));
2 26 Feb 07 jari 66         sub_algo_data.addParam("number-of-iterations", String.valueOf(iterations));
2 26 Feb 07 jari 67         sub_algo_data.addParam("calculate-means", String.valueOf(calculateMeans));
2 26 Feb 07 jari 68         
2 26 Feb 07 jari 69         /*
2 26 Feb 07 jari 70         AlgorithmEvent event = new AlgorithmEvent(this, AlgorithmEvent.SET_UNITS, iterations);
2 26 Feb 07 jari 71         fireValueChanged(event);
2 26 Feb 07 jari 72         event.setId(AlgorithmEvent.PROGRESS_VALUE);
2 26 Feb 07 jari 73          */
2 26 Feb 07 jari 74         AlgorithmData sub_algo_result = null;
2 26 Feb 07 jari 75         Cluster sub_algo_clusters = new Cluster();
2 26 Feb 07 jari 76         float[] fom_values = null;
2 26 Feb 07 jari 77         FloatMatrix fomResults = null;
2 26 Feb 07 jari 78         int times = 0;
2 26 Feb 07 jari 79         int[] numOfCastClusters = new int[1]; // just to initialize
2 26 Feb 07 jari 80         
2 26 Feb 07 jari 81         if (method == 2) {   //FOM FOR K-MEANS
2 26 Feb 07 jari 82             AlgorithmEvent event = new AlgorithmEvent(this, AlgorithmEvent.SET_UNITS, maxNumClusters);
2 26 Feb 07 jari 83             fireValueChanged(event);
2 26 Feb 07 jari 84             event.setId(AlgorithmEvent.PROGRESS_VALUE);
2 26 Feb 07 jari 85             sub_algo_data.addParam("kmc-cluster-genes", String.valueOf(clusterGenes));
2 26 Feb 07 jari 86             fom_values = new float[maxNumClusters];
2 26 Feb 07 jari 87             
2 26 Feb 07 jari 88             fomResults = new FloatMatrix(fomIterations, maxNumClusters);
2 26 Feb 07 jari 89             for (int fomi = 0; fomi < fomIterations; fomi++) {
2 26 Feb 07 jari 90                 event.setDescription("Calculating FOMs for FOM iteration "+String.valueOf(fomi+1)+" of "+String.valueOf(fomIterations)+" iterations.");
2 26 Feb 07 jari 91                 fireValueChanged(event);
2 26 Feb 07 jari 92                 for (int i=0; i<maxNumClusters; i++) {
2 26 Feb 07 jari 93                     if (stop) {
2 26 Feb 07 jari 94                         throw new AbortException();
2 26 Feb 07 jari 95                     }
2 26 Feb 07 jari 96                     
2 26 Feb 07 jari 97                     event.setIntValue(i);
2 26 Feb 07 jari 98                     fireValueChanged(event);
2 26 Feb 07 jari 99                     
2 26 Feb 07 jari 100                     sub_algo_data.addParam("number-of-clusters", String.valueOf(i+1));
2 26 Feb 07 jari 101                     sub_algo_result = sub_algo.execute(sub_algo_data);
2 26 Feb 07 jari 102                     
2 26 Feb 07 jari 103                     sub_algo_clusters = sub_algo_result.getCluster("cluster");
2 26 Feb 07 jari 104                     fomResults.set(fomi, i, (float)getFOM(expMatrix, sub_algo_clusters, number_of_genes, number_of_samples));
2 26 Feb 07 jari 105                 }
2 26 Feb 07 jari 106             }
2 26 Feb 07 jari 107         }  else { //FOM FOR CAST
2 26 Feb 07 jari 108             times = (int)(1/interval);
2 26 Feb 07 jari 109             AlgorithmEvent event = new AlgorithmEvent(this, AlgorithmEvent.SET_UNITS, times);
2 26 Feb 07 jari 110             fireValueChanged(event);
2 26 Feb 07 jari 111             event.setId(AlgorithmEvent.PROGRESS_VALUE);
2 26 Feb 07 jari 112             //sub_algo.runInside = true;
2 26 Feb 07 jari 113             //System.out.println("times = " + times);
2 26 Feb 07 jari 114             sub_algo_data.addParam("cast-cluster-genes", String.valueOf(clusterGenes));
2 26 Feb 07 jari 115             numOfCastClusters = new int[times];
2 26 Feb 07 jari 116             float threshold = 0;
2 26 Feb 07 jari 117             fom_values = new float[times];
2 26 Feb 07 jari 118             for (int i = 0; i < times; i++) {
2 26 Feb 07 jari 119                 threshold = threshold + interval;
2 26 Feb 07 jari 120                 if (threshold > 1.0f) threshold = 1.0f;
2 26 Feb 07 jari 121
2 26 Feb 07 jari 122                 if (stop) {
2 26 Feb 07 jari 123                     throw new AbortException();
2 26 Feb 07 jari 124                 }
2 26 Feb 07 jari 125                 event.setIntValue(i);
2 26 Feb 07 jari 126                 event.setDescription("calculating for threshold of "+String.valueOf(threshold));
2 26 Feb 07 jari 127                 fireValueChanged(event);
2 26 Feb 07 jari 128                 
2 26 Feb 07 jari 129                 sub_algo_data.addParam("threshold", String.valueOf(threshold));
2 26 Feb 07 jari 130                 sub_algo_result = sub_algo.execute(sub_algo_data);
2 26 Feb 07 jari 131                 sub_algo_clusters = sub_algo_result.getCluster("cluster");
2 26 Feb 07 jari 132                 
2 26 Feb 07 jari 133                 fom_values[i] = (float)getFOM(expMatrix, sub_algo_clusters, number_of_genes, number_of_samples);   
2 26 Feb 07 jari 134                 numOfCastClusters[i] = sub_algo_clusters.getNodeList().getSize();
2 26 Feb 07 jari 135             }
2 26 Feb 07 jari 136         }
2 26 Feb 07 jari 137         
2 26 Feb 07 jari 138         AlgorithmData result = new AlgorithmData();
2 26 Feb 07 jari 139         if(method == 2)
2 26 Feb 07 jari 140             result.addMatrix("fom-matrix", fomResults);
2 26 Feb 07 jari 141         else
2 26 Feb 07 jari 142             result.addMatrix("fom-values", new FloatMatrix(fom_values, 1));
2 26 Feb 07 jari 143         result.addIntArray("numOfCastClusters", numOfCastClusters);
2 26 Feb 07 jari 144         return result;
2 26 Feb 07 jari 145     }
2 26 Feb 07 jari 146     
2 26 Feb 07 jari 147     public double getFOM(FloatMatrix expMatrix, Cluster clusters, final int number_of_genes, final int number_of_samples) {
2 26 Feb 07 jari 148         
2 26 Feb 07 jari 149         int[] cluster;
2 26 Feb 07 jari 150         float value;
2 26 Feb 07 jari 151         int n = 0;
2 26 Feb 07 jari 152         NodeList nodeList = clusters.getNodeList();
2 26 Feb 07 jari 153         final int number_of_clusters = nodeList.getSize();
2 26 Feb 07 jari 154         double[][] means = new double[number_of_samples][number_of_clusters];
2 26 Feb 07 jari 155         for (int i=0; i<number_of_samples; i++) {
2 26 Feb 07 jari 156             for (int j=0; j<number_of_clusters; j++) {
2 26 Feb 07 jari 157                 means[i][j] = 0.0;
2 26 Feb 07 jari 158                 cluster = nodeList.getNode(j).getFeaturesIndexes();
2 26 Feb 07 jari 159                 n = 0;
2 26 Feb 07 jari 160                 for (int p=0; p<cluster.length; p++) {
2 26 Feb 07 jari 161                     value = expMatrix.get(cluster[p], i);
2 26 Feb 07 jari 162                     if (!Float.isNaN(value)) {
2 26 Feb 07 jari 163                         means[i][j] += (double)value;
2 26 Feb 07 jari 164                         n++;
2 26 Feb 07 jari 165                     }
2 26 Feb 07 jari 166                 }
2 26 Feb 07 jari 167                 if(n > 0)
2 26 Feb 07 jari 168                     means[i][j] /= (double)n;
2 26 Feb 07 jari 169                 else
2 26 Feb 07 jari 170                     means[i][j] = 0;
2 26 Feb 07 jari 171             }
2 26 Feb 07 jari 172         }
2 26 Feb 07 jari 173         
2 26 Feb 07 jari 174         double[] tFOM = new double[number_of_samples];
2 26 Feb 07 jari 175         double factor;
2 26 Feb 07 jari 176         
2 26 Feb 07 jari 177         /*
2 26 Feb 07 jari 178         if(number_of_clusters == 2) {
2 26 Feb 07 jari 179             System.out.println("***********************");
2 26 Feb 07 jari 180             float [][] m = new float[means.length][means[0].length];
2 26 Feb 07 jari 181             for(int i = 0; i < means.length; i++) {
2 26 Feb 07 jari 182                 for(int j = 0; j < means[i].length; j++){
2 26 Feb 07 jari 183                     m[i][j] = (float)means[i][j];
2 26 Feb 07 jari 184                 }
2 26 Feb 07 jari 185             }
2 26 Feb 07 jari 186             FloatMatrix meanFM = new FloatMatrix(m);
2 26 Feb 07 jari 187             meanFM.print(2,5);
2 26 Feb 07 jari 188         }
2 26 Feb 07 jari 189          */
2 26 Feb 07 jari 190         //if(this.clusterGenes)
2 26 Feb 07 jari 191         //  factor = Math.sqrt((double)(number_of_genes-number_of_samples)/(double)number_of_genes);
2 26 Feb 07 jari 192         // else
2 26 Feb 07 jari 193         //   factor = Math.sqrt((double)(number_of_samples-number_of_genes)/(double)number_of_samples);
2 26 Feb 07 jari 194         // factor = Math.sqrt(((double)(number_of_genes-number_of_clusters))/(double)number_of_genes);
2 26 Feb 07 jari 195         factor = 1;
2 26 Feb 07 jari 196         //float tot = 0;
2 26 Feb 07 jari 197         for (int i=0; i<number_of_samples; i++) {
2 26 Feb 07 jari 198             tFOM[i] = 0.0;
2 26 Feb 07 jari 199             for (int j=0; j<number_of_clusters; j++) {
2 26 Feb 07 jari 200                 cluster = nodeList.getNode(j).getFeaturesIndexes();
2 26 Feb 07 jari 201                 for (int p=0; p<cluster.length; p++) {
2 26 Feb 07 jari 202                     value = expMatrix.get(cluster[p], i);
2 26 Feb 07 jari 203                     if (!Float.isNaN(value)) {
2 26 Feb 07 jari 204                         tFOM[i] += Math.pow(((double)value-means[i][j]), 2);
2 26 Feb 07 jari 205            
2 26 Feb 07 jari 206                         //check sos, tot += Math.pow(((double)value-means[i][j]), 2);
2 26 Feb 07 jari 207                     }
2 26 Feb 07 jari 208                 }
2 26 Feb 07 jari 209             }              
2 26 Feb 07 jari 210         tFOM[i] = Math.sqrt(tFOM[i]/(double)number_of_genes);      
2 26 Feb 07 jari 211     }
2 26 Feb 07 jari 212     
2 26 Feb 07 jari 213     double fom_value = 0d;
2 26 Feb 07 jari 214     for (int i=0; i<number_of_samples; i++) {
2 26 Feb 07 jari 215         fom_value += tFOM[i];
2 26 Feb 07 jari 216     }
2 26 Feb 07 jari 217
2 26 Feb 07 jari 218     /*  
2 26 Feb 07 jari 219     if(number_of_clusters == 2){
2 26 Feb 07 jari 220         System.out.println("tot ="+ tot);
2 26 Feb 07 jari 221         System.out.println("fom value ="+fom_value);
2 26 Feb 07 jari 222     }
2 26 Feb 07 jari 223     */
2 26 Feb 07 jari 224     return fom_value;
2 26 Feb 07 jari 225 }
2 26 Feb 07 jari 226
2 26 Feb 07 jari 227 /**
2 26 Feb 07 jari 228  * This method should interrupt the calculation.
2 26 Feb 07 jari 229  */
2 26 Feb 07 jari 230 public void abort() {
2 26 Feb 07 jari 231     this.stop = true;
2 26 Feb 07 jari 232 }
2 26 Feb 07 jari 233 }