mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/usc/USCCrossValidation.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2  * http://genomebiology.com/2003/4/12/R83
2 26 Feb 07 jari 3  * Created on Oct 28, 2004
2 26 Feb 07 jari 4  */
2 26 Feb 07 jari 5 package org.tigr.microarray.mev.cluster.gui.impl.usc;
2 26 Feb 07 jari 6
2 26 Feb 07 jari 7 import java.awt.Cursor;
2 26 Feb 07 jari 8 import java.awt.Dimension;
2 26 Feb 07 jari 9 import java.awt.Frame;
2 26 Feb 07 jari 10 import java.awt.Toolkit;
2 26 Feb 07 jari 11 import java.util.Arrays;
2 26 Feb 07 jari 12 import java.util.BitSet;
2 26 Feb 07 jari 13 import java.util.Vector;
2 26 Feb 07 jari 14
2 26 Feb 07 jari 15 import javax.swing.BoxLayout;
2 26 Feb 07 jari 16 import javax.swing.JFrame;
2 26 Feb 07 jari 17 import javax.swing.JLabel;
2 26 Feb 07 jari 18 import javax.swing.JPanel;
2 26 Feb 07 jari 19 import javax.swing.JProgressBar;
2 26 Feb 07 jari 20 import javax.swing.SpringLayout;
2 26 Feb 07 jari 21
2 26 Feb 07 jari 22 /**
2 26 Feb 07 jari 23  * Uncorrelated Shrunken Centroid Algorithm as published in <a
2 26 Feb 07 jari 24  * href="http://genomebiology.com/2003/4/12/R83">Genome Biology </a> by Kayee
2 26 Feb 07 jari 25  * Yeung. <br>
2 26 Feb 07 jari 26  * <br>
2 26 Feb 07 jari 27  * This code works only for single measurements (no replicate support). It is
2 26 Feb 07 jari 28  * assumed that the incoming hybs are all sorted identically and that there are
2 26 Feb 07 jari 29  * no null or NaN values as ratios <br>
2 26 Feb 07 jari 30  * <br>
2 26 Feb 07 jari 31  * The main steps are: <br>
2 26 Feb 07 jari 32  * <br>
2 26 Feb 07 jari 33  * 1. Split the full set of training hybs into a training subset and a test
2 26 Feb 07 jari 34  * subset. The cross validation will be run numFold times, wherein each hyb
2 26 Feb 07 jari 35  * needs to be used as a test hyb once and only once against all the other
2 26 Feb 07 jari 36  * training hybs. Eg. if there are 10 hybs and numFold is 5, 2 hybs are randomly
2 26 Feb 07 jari 37  * selected to be used as test hybs for each fold run. During the next fold run,
2 26 Feb 07 jari 38  * 2 others are randomly selected. This is repeated numFold times such that all
2 26 Feb 07 jari 39  * hybs were tested once. Remainder hybs are thrown into the last fold's test
2 26 Feb 07 jari 40  * set <br>
2 26 Feb 07 jari 41  * <br>
2 26 Feb 07 jari 42  * 2. Calculate the gene centroid ( the mean ratio for all hybs for each gene )
2 26 Feb 07 jari 43  * <br>
2 26 Feb 07 jari 44  * <br>
2 26 Feb 07 jari 45  * 3. Calculate the class centroids ( the mean ratio for the hybs in each class )
2 26 Feb 07 jari 46  * <br>
2 26 Feb 07 jari 47  * <br>
2 26 Feb 07 jari 48  * 4. Calculate Mk values ( standardizing factor )<br>
2 26 Feb 07 jari 49  * <br>
2 26 Feb 07 jari 50  * 5. Calculate S values ( sum of intra-class standard deviations )<br>
2 26 Feb 07 jari 51  * <br>
2 26 Feb 07 jari 52  * 6. Calculate s0 ( median S value )<br>
2 26 Feb 07 jari 53  * <br>
2 26 Feb 07 jari 54  * 7. Compute Relative Difference ( difference between class centroid and gene
2 26 Feb 07 jari 55  * centroid standardized by Mk, S, and s0 )<br>
2 26 Feb 07 jari 56  * <br>
2 26 Feb 07 jari 57  * 8. Shrink | Relative Difference | by delta ( delta is a random value between
2 26 Feb 07 jari 58  * 0 and deltaMax incremented by deltaStep )<br>
2 26 Feb 07 jari 59  * <br>
2 26 Feb 07 jari 60  * 9. Do soft thresholding on Shrunken Relative Difference ( if subtracting
2 26 Feb 07 jari 61  * delta from the absolute value of Relative Difference becomes negative, remove
2 26 Feb 07 jari 62  * that gene from the analysis because it is not significantly different from
2 26 Feb 07 jari 63  * the gene centroid )<br>
2 26 Feb 07 jari 64  * <br>
2 26 Feb 07 jari 65  * 10. Compute Shrunken Class Centroid ( class Centroid + standardized Shrunken
2 26 Feb 07 jari 66  * Relative Difference )<br>
2 26 Feb 07 jari 67  * <br>
2 26 Feb 07 jari 68  * 11. Sort the remaining genes from greatest to least Shrunken Relative
2 26 Feb 07 jari 69  * Difference <br>
2 26 Feb 07 jari 70  * <br>
2 26 Feb 07 jari 71  * 12. Compute pairwise correlation between each gene and the gene with the next
2 26 Feb 07 jari 72  * greatest Shrunken Relative Difference. <br>
2 26 Feb 07 jari 73  * <br>
2 26 Feb 07 jari 74  * 13. Remove from testing if correlation is less than rho ( rho is .5, .6, .7.
2 26 Feb 07 jari 75  * 8. 9, 1.0 )<br>
2 26 Feb 07 jari 76  * <br>
2 26 Feb 07 jari 77  * 14. Compute a new discriminant score for a test hyb against each class <br>
2 26 Feb 07 jari 78  * <br>
2 26 Feb 07 jari 79  * 15. Assign the new test hyb to the class with the minimum discriminant score
2 26 Feb 07 jari 80  * 
2 26 Feb 07 jari 81  * @author vu
2 26 Feb 07 jari 82  */
2 26 Feb 07 jari 83 public class USCCrossValidation {
2 26 Feb 07 jari 84     private int deltaKount;
2 26 Feb 07 jari 85     private int deltaMax;
2 26 Feb 07 jari 86     private int foldKount;
2 26 Feb 07 jari 87     private int xValKount;
2 26 Feb 07 jari 88     private double rhoMin;
2 26 Feb 07 jari 89     private double rhoMax;
2 26 Feb 07 jari 90     private double rhoStep;
2 26 Feb 07 jari 91     private double deltaStep;
2 26 Feb 07 jari 92
2 26 Feb 07 jari 93
2 26 Feb 07 jari 94     /**
2 26 Feb 07 jari 95      * Default and sole constructor
2 26 Feb 07 jari 96      * 
2 26 Feb 07 jari 97      * @param hybSetP The training hyb set
2 26 Feb 07 jari 98      * @param numDeltasP
2 26 Feb 07 jari 99      * @param deltaMaxP
2 26 Feb 07 jari 100      * @param rhoMinP
2 26 Feb 07 jari 101      * @param rhoMaxP
2 26 Feb 07 jari 102      * @param rhoStepP
2 26 Feb 07 jari 103      * @param numFoldP
2 26 Feb 07 jari 104      */
2 26 Feb 07 jari 105     public USCCrossValidation(int numDeltasP, int deltaMaxP, double rhoMinP,
2 26 Feb 07 jari 106             double rhoMaxP, double rhoStepP, int numFoldP, int xValKountP) {
2 26 Feb 07 jari 107         this.deltaKount = numDeltasP;
2 26 Feb 07 jari 108         this.deltaMax = deltaMaxP;
2 26 Feb 07 jari 109         this.rhoMin = rhoMinP;
2 26 Feb 07 jari 110         this.rhoMax = rhoMaxP;
2 26 Feb 07 jari 111         this.rhoStep = rhoStepP;
2 26 Feb 07 jari 112         this.foldKount = numFoldP;
2 26 Feb 07 jari 113         this.xValKount = xValKountP;
2 26 Feb 07 jari 114
2 26 Feb 07 jari 115         double dDelta = this.deltaMax;
2 26 Feb 07 jari 116         double dNum = this.deltaKount;
2 26 Feb 07 jari 117         this.deltaStep = dDelta / dNum;
2 26 Feb 07 jari 118     }//end constructor
2 26 Feb 07 jari 119
2 26 Feb 07 jari 120
2 26 Feb 07 jari 121     /**
2 26 Feb 07 jari 122      * Cross Validate the data.
2 26 Feb 07 jari 123      * 
2 26 Feb 07 jari 124      * The number of results is the # of Delta/Rho possibilties ( i.e. numDeltas *
2 26 Feb 07 jari 125      * 6 )
2 26 Feb 07 jari 126      * 
2 26 Feb 07 jari 127      * @param fullSet
2 26 Feb 07 jari 128      * @return
2 26 Feb 07 jari 129      */
2 26 Feb 07 jari 130     public USCDeltaRhoResult[][][] crossValidate(USCHybSet fullSet, Frame frame) {
2 26 Feb 07 jari 131         frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
2 26 Feb 07 jari 132         
2 26 Feb 07 jari 133         //[ xVal ][ fold ][ result ]
2 26 Feb 07 jari 134         USCDeltaRhoResult[][][] xResult = null;
2 26 Feb 07 jari 135
2 26 Feb 07 jari 136         //display a progress bar
2 26 Feb 07 jari 137         JPanel mainPanel = new JPanel();
2 26 Feb 07 jari 138         mainPanel.setLayout(new SpringLayout());
2 26 Feb 07 jari 139         JPanel leftPanel = new JPanel();
2 26 Feb 07 jari 140         leftPanel.add(new JLabel("     "));
2 26 Feb 07 jari 141         JPanel rightPanel = new JPanel();
2 26 Feb 07 jari 142         rightPanel.add(new JLabel("     "));
2 26 Feb 07 jari 143
2 26 Feb 07 jari 144         JPanel midPanel = new JPanel();
2 26 Feb 07 jari 145         BoxLayout midBox = new BoxLayout(midPanel, BoxLayout.Y_AXIS);
2 26 Feb 07 jari 146         midPanel.setLayout(midBox);
2 26 Feb 07 jari 147         JLabel label = new JLabel("Cross Validating... Please Wait");
2 26 Feb 07 jari 148         JLabel label2 = new JLabel("This will take a few minutes");
2 26 Feb 07 jari 149         JLabel foldLabel = new JLabel("Fold/CrossVal runs");
2 26 Feb 07 jari 150         JLabel deltaLabel = new JLabel("Deltas");
2 26 Feb 07 jari 151         JLabel rhoLabel = new JLabel("Rhos");
2 26 Feb 07 jari 152         JLabel corrLabel = new JLabel("Pairwise Genes");
2 26 Feb 07 jari 153         JLabel blankLabel = new JLabel(" ");
2 26 Feb 07 jari 154         midPanel.add(label);
2 26 Feb 07 jari 155         midPanel.add(label2);
2 26 Feb 07 jari 156         midPanel.add(blankLabel);
2 26 Feb 07 jari 157         JProgressBar foldBar = new JProgressBar(0, ( this.foldKount * this.xValKount ));
2 26 Feb 07 jari 158         foldBar.setIndeterminate(false);
2 26 Feb 07 jari 159         foldBar.setStringPainted(true);
2 26 Feb 07 jari 160         JProgressBar deltaBar = new JProgressBar(0, this.deltaKount);
2 26 Feb 07 jari 161         deltaBar.setIndeterminate(false);
2 26 Feb 07 jari 162         deltaBar.setStringPainted(true);
2 26 Feb 07 jari 163         JProgressBar rhoBar = new JProgressBar(5, 11);
2 26 Feb 07 jari 164         rhoBar.setIndeterminate(false);
2 26 Feb 07 jari 165         rhoBar.setStringPainted(true);
2 26 Feb 07 jari 166         JProgressBar corrBar = new JProgressBar(0, fullSet.getNumGenes());
2 26 Feb 07 jari 167         corrBar.setIndeterminate(false);
2 26 Feb 07 jari 168         corrBar.setStringPainted(true);
2 26 Feb 07 jari 169         midPanel.add(foldLabel);
2 26 Feb 07 jari 170         midPanel.add(foldBar);
2 26 Feb 07 jari 171         midPanel.add(deltaLabel);
2 26 Feb 07 jari 172         midPanel.add(deltaBar);
2 26 Feb 07 jari 173         midPanel.add(rhoLabel);
2 26 Feb 07 jari 174         midPanel.add(rhoBar);
2 26 Feb 07 jari 175         midPanel.add(corrLabel);
2 26 Feb 07 jari 176         midPanel.add(corrBar);
2 26 Feb 07 jari 177
2 26 Feb 07 jari 178         mainPanel.add(leftPanel);
2 26 Feb 07 jari 179         mainPanel.add(midPanel);
2 26 Feb 07 jari 180         mainPanel.add(rightPanel);
2 26 Feb 07 jari 181         SpringUtilities.makeCompactGrid(mainPanel, 1, 3, 0, 0, 0, 0);
2 26 Feb 07 jari 182         JFrame jf = new JFrame("Cross Validating");
2 26 Feb 07 jari 183         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2 26 Feb 07 jari 184         jf.getContentPane().add(mainPanel);
2 26 Feb 07 jari 185         jf.setSize(250, 250);
2 26 Feb 07 jari 186         jf.show();
2 26 Feb 07 jari 187         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
2 26 Feb 07 jari 188         jf.setLocation(( screenSize.width - 200 ) / 2,
2 26 Feb 07 jari 189                 ( screenSize.height - 100 ) / 2);
2 26 Feb 07 jari 190         int iProgress = 0;
2 26 Feb 07 jari 191         
2 26 Feb 07 jari 192         //figure out how many Delta/Rho combos there are
2 26 Feb 07 jari 193         int iRho = 0;
2 26 Feb 07 jari 194         double currentRho = this.rhoMin;
2 26 Feb 07 jari 195         while(currentRho < this.rhoMax) {
2 26 Feb 07 jari 196             iRho ++;
2 26 Feb 07 jari 197             currentRho += this.rhoStep;
2 26 Feb 07 jari 198         }
2 26 Feb 07 jari 199         int iTrainStep = this.foldKount * this.xValKount;
2 26 Feb 07 jari 200         int resultKount = this.deltaKount * iRho;
2 26 Feb 07 jari 201         
2 26 Feb 07 jari 202       xResult = new USCDeltaRhoResult[ this.xValKount ][ this.foldKount ][];
2 26 Feb 07 jari 203       int xResultKount = 0;
2 26 Feb 07 jari 204         
2 26 Feb 07 jari 205         //do multiple cross validations (added 1.30.05)
2 26 Feb 07 jari 206         //*** Assuming that there will be a result for each CV Run ***//
2 26 Feb 07 jari 207         for( int m = 0; m < this.xValKount; m++ ) {
2 26 Feb 07 jari 208             //loop through the folds, during each fold, a different set of Hybs
2 26 Feb 07 jari 209             // are left out
2 26 Feb 07 jari 210             for( int f = 0; f < this.foldKount; f++ ) {
2 26 Feb 07 jari 211                 deltaBar.setValue(0);
2 26 Feb 07 jari 212
2 26 Feb 07 jari 213                 //hold the results from this fold
2 26 Feb 07 jari 214                 xResult[ m ][ f ] = new USCDeltaRhoResult[resultKount];
2 26 Feb 07 jari 215                 int iResult = 0;
2 26 Feb 07 jari 216
2 26 Feb 07 jari 217                 //arrays of USCHyb objects
2 26 Feb 07 jari 218                 USCHyb[] subTestArray = fullSet.getTestArray(f);
2 26 Feb 07 jari 219                 USCHyb[] subTrainArray = fullSet.getTrainArray(f);
2 26 Feb 07 jari 220
2 26 Feb 07 jari 221                 //loop through the deltas
2 26 Feb 07 jari 222                 double delta = 0.0f;
2 26 Feb 07 jari 223                 for( int d = 0; d < this.deltaKount; d++ ) {
2 26 Feb 07 jari 224                   rhoBar.setValue( 5 );
2 26 Feb 07 jari 225                   
2 26 Feb 07 jari 226                     //loop through the rhos
2 26 Feb 07 jari 227                     double rho;
2 26 Feb 07 jari 228                     
2 26 Feb 07 jari 229                     for( int r = 5; r < 11; r++ ) {
2 26 Feb 07 jari 230                         rho = ( double ) r * 0.1f;
2 26 Feb 07 jari 231
2 26 Feb 07 jari 232                         USCDeltaRhoResult drResult = this.doDR(subTrainArray,
2 26 Feb 07 jari 233                                 subTestArray, delta, rho, fullSet.getNumGenes(),
2 26 Feb 07 jari 234                                 fullSet.getNumClasses(), fullSet
2 26 Feb 07 jari 235                                         .getUniqueClasses(), corrBar, r);
2 26 Feb 07 jari 236                         if( drResult == null ) {
2 26 Feb 07 jari 237                             //do nothing
2 26 Feb 07 jari 238                           xResult[ m ][ f ][ iResult ] = new USCDeltaRhoResult();
2 26 Feb 07 jari 239                         } else {
2 26 Feb 07 jari 240                             //store it
2 26 Feb 07 jari 241                           xResult[ m ][ f ][ iResult ] = drResult;
2 26 Feb 07 jari 242                             iResult++;
2 26 Feb 07 jari 243                         }
2 26 Feb 07 jari 244                         
2 26 Feb 07 jari 245                         rhoBar.setValue( r );
2 26 Feb 07 jari 246                     }//end r (rho)
2 26 Feb 07 jari 247
2 26 Feb 07 jari 248                     delta += this.deltaStep;
2 26 Feb 07 jari 249                     deltaBar.setValue(d + 1);
2 26 Feb 07 jari 250                 }//end d (deltas)
2 26 Feb 07 jari 251                 
2 26 Feb 07 jari 252                 xResultKount ++;
2 26 Feb 07 jari 253
2 26 Feb 07 jari 254                 //update progress bar
2 26 Feb 07 jari 255                 foldBar.setIndeterminate(false);
2 26 Feb 07 jari 256                 iProgress++;
2 26 Feb 07 jari 257                 foldBar.setValue(iProgress);
2 26 Feb 07 jari 258                 foldBar.setStringPainted(true);
2 26 Feb 07 jari 259             }//end f (folds)
2 26 Feb 07 jari 260
2 26 Feb 07 jari 261             //toReturn.setFoldResult(resultArray, m);
2 26 Feb 07 jari 262         }//end m (xValKount)
2 26 Feb 07 jari 263
2 26 Feb 07 jari 264         jf.dispose();
2 26 Feb 07 jari 265         frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
2 26 Feb 07 jari 266         
2 26 Feb 07 jari 267         return xResult;
2 26 Feb 07 jari 268     }//end crossValidate
2 26 Feb 07 jari 269
2 26 Feb 07 jari 270
2 26 Feb 07 jari 271     /**
2 26 Feb 07 jari 272      * Does Discriminant Score Class Assignments to hybs in testArray for one
2 26 Feb 07 jari 273      * d/r
2 26 Feb 07 jari 274      * 
2 26 Feb 07 jari 275      * @param trainArray USCHyb[] of the hybs used for Training
2 26 Feb 07 jari 276      * @param testArray USCHyb[] of the hybs to be classified
2 26 Feb 07 jari 277      * @param delta amount by which Dik should be shrunk
2 26 Feb 07 jari 278      * @param rho Low Correlation threshold
2 26 Feb 07 jari 279      * @param numGenes Number of Genes in hybs
2 26 Feb 07 jari 280      * @param numClasses Number of Classes in set of hybs
2 26 Feb 07 jari 281      * @param uniqueClasses String[] of the labels of each class in set of hybs
2 26 Feb 07 jari 282      * @return double[ numTestHybs ][ numClasses ]
2 26 Feb 07 jari 283      */
2 26 Feb 07 jari 284     public USCDeltaRhoResult doDR(USCHyb[] trainArray, USCHyb[] testArray,
2 26 Feb 07 jari 285             double delta, double rho, int numGenes, int numClasses,
2 26 Feb 07 jari 286             String[] uniqueClassLabels, JProgressBar rhoBar, int iRho) {
2 26 Feb 07 jari 287         //keep track of gene order by creating a USCOrder object for each gene
2 26 Feb 07 jari 288         USCOrder[] order = new USCOrder[numGenes];
2 26 Feb 07 jari 289         for( int g = 0; g < numGenes; g++ ) {
2 26 Feb 07 jari 290             order[g] = new USCOrder(g);
2 26 Feb 07 jari 291         }
2 26 Feb 07 jari 292
2 26 Feb 07 jari 293         //each gene has a centroid (mean)
2 26 Feb 07 jari 294         double[] geneCentroids = this
2 26 Feb 07 jari 295                 .computeGeneCentroids(trainArray, numGenes);
2 26 Feb 07 jari 296
2 26 Feb 07 jari 297         //each class in each gene has a centroid (class mean) [ classes ][
2 26 Feb 07 jari 298         // genes ]
2 26 Feb 07 jari 299         double[][] classCentroids = this.computeClassCentroids(trainArray,
2 26 Feb 07 jari 300                 uniqueClassLabels, numGenes);
2 26 Feb 07 jari 301
2 26 Feb 07 jari 302         //each class has an mk value [ numClasses ]
2 26 Feb 07 jari 303         double[] mks = this.computeMks(trainArray, uniqueClassLabels);
2 26 Feb 07 jari 304
2 26 Feb 07 jari 305         //each gene has an si value [ numGenes ]
2 26 Feb 07 jari 306         double[] sis = this.computeSis(trainArray, classCentroids,
2 26 Feb 07 jari 307                 uniqueClassLabels, numGenes);
2 26 Feb 07 jari 308
2 26 Feb 07 jari 309         //s0 is the median si
2 26 Feb 07 jari 310         double s0 = this.computeMedian(sis);
2 26 Feb 07 jari 311
2 26 Feb 07 jari 312         //dik is the relative difference of each classCentroid from the
2 26 Feb 07 jari 313         // geneCentroid (standardized) [ class ][ gene ]
2 26 Feb 07 jari 314         double[][] dik = this.computeRelativeDifferences(classCentroids,
2 26 Feb 07 jari 315                 geneCentroids, mks, sis, s0);
2 26 Feb 07 jari 316
2 26 Feb 07 jari 317         //do soft thresholding on diks ( shrunken relative difference )
2 26 Feb 07 jari 318         double[][] dikShrunk = this.shrinkDiks(delta, dik);
2 26 Feb 07 jari 319
2 26 Feb 07 jari 320         //compute shrunken class centroids (do for all, deal with removed genes
2 26 Feb 07 jari 321         // later)
2 26 Feb 07 jari 322         double[][] shrunkenCentroids = this.computeShrunkenClassCentroid(
2 26 Feb 07 jari 323                 geneCentroids, mks, sis, s0, dikShrunk);
2 26 Feb 07 jari 324
2 26 Feb 07 jari 325         //find and store Beta (greatest |dikShrunk|)
2 26 Feb 07 jari 326         for( int g = 0; g < numGenes; g++ ) {
2 26 Feb 07 jari 327             double maxDik = 0;
2 26 Feb 07 jari 328             for( int c = 0; c < numClasses; c++ ) {
2 26 Feb 07 jari 329                 //double toTest = dikShrunk[ c ][ g ];
2 26 Feb 07 jari 330                 double toTest = Math.abs(dikShrunk[c][g]);
2 26 Feb 07 jari 331                 if( toTest > maxDik ) {
2 26 Feb 07 jari 332                     maxDik = toTest;
2 26 Feb 07 jari 333                 }
2 26 Feb 07 jari 334             }//end c (classes)
2 26 Feb 07 jari 335             order[g].setBeta(maxDik);
2 26 Feb 07 jari 336         }//end g (genes)
2 26 Feb 07 jari 337
2 26 Feb 07 jari 338         //figure out which genes to keep using and store their indices in a
2 26 Feb 07 jari 339         // BitSet
2 26 Feb 07 jari 340         //get a BitSet where each gene is marked true when it is relevant
2 26 Feb 07 jari 341         BitSet bsUse = this.findRelevantGenes(dikShrunk, order);
2 26 Feb 07 jari 342         int numRelevant = 0;
2 26 Feb 07 jari 343         for( int g = 0; g < numGenes; g++ ) {
2 26 Feb 07 jari 344             if( bsUse.get(g) == true ) {
2 26 Feb 07 jari 345                 //order[g].setRelevant(true);
2 26 Feb 07 jari 346                 numRelevant++;
2 26 Feb 07 jari 347             } else {
2 26 Feb 07 jari 348                 //order[g].setRelevant(false);
2 26 Feb 07 jari 349             }
2 26 Feb 07 jari 350         }//end g (genes)
2 26 Feb 07 jari 351
2 26 Feb 07 jari 352         //sort the genes based on Beta from greatest to least
2 26 Feb 07 jari 353         Arrays.sort(order, new USCRelevanceComparator());
2 26 Feb 07 jari 354         //from this point on, order[ numgenes] is sorted by beta
2 26 Feb 07 jari 355
2 26 Feb 07 jari 356         //store the new order in case we need it later
2 26 Feb 07 jari 357         for( int g = 0; g < order.length; g++ ) {
2 26 Feb 07 jari 358             order[g].setIRelevant(g);
2 26 Feb 07 jari 359         }//end g
2 26 Feb 07 jari 360
2 26 Feb 07 jari 361         //do pairwise correlation testing if there are any relevant genes
2 26 Feb 07 jari 362         if( numRelevant > 0 ) {
2 26 Feb 07 jari 363             this.doCorrelationTesting(order, trainArray, rho, rhoBar, geneCentroids, iRho);
2 26 Feb 07 jari 364         }//end if( numRelevant > 0 )
2 26 Feb 07 jari 365
2 26 Feb 07 jari 366         //compute discriminant scores
2 26 Feb 07 jari 367         double[][] discScores = this.computeDiscriminantScores(trainArray, testArray, 
2 26 Feb 07 jari 368             shrunkenCentroids, order, sis, s0, uniqueClassLabels);
2 26 Feb 07 jari 369
2 26 Feb 07 jari 370         //return a result if there is one
2 26 Feb 07 jari 371         if( discScores == null ) {
2 26 Feb 07 jari 372             //System.out.println( "Null Discriminant Scores\tDelta = " + delta
2 26 Feb 07 jari 373             // + "\tRho = " + rho );
2 26 Feb 07 jari 374           return null;
2 26 Feb 07 jari 375         } else {
2 26 Feb 07 jari 376             //count the number of genes used
2 26 Feb 07 jari 377             int iGenes = 0;
2 26 Feb 07 jari 378             for( int g = 0; g < order.length; g++ ) {
2 26 Feb 07 jari 379                 if( order[g].use() ) {
2 26 Feb 07 jari 380                     iGenes++;
2 26 Feb 07 jari 381                 }
2 26 Feb 07 jari 382             }//end g
2 26 Feb 07 jari 383             
2 26 Feb 07 jari 384             //tally up scores and return
2 26 Feb 07 jari 385             int numWrong = 0;
2 26 Feb 07 jari 386             int numRight = 0;
2 26 Feb 07 jari 387             for(int h = 0; h < testArray.length; h ++) {
2 26 Feb 07 jari 388                 USCHyb hyb = testArray[ h ];
2 26 Feb 07 jari 389                 String label = hyb.getHybLabel();
2 26 Feb 07 jari 390                 
2 26 Feb 07 jari 391                 double dLow = 9999999;
2 26 Feb 07 jari 392                 int iMin = 0;
2 26 Feb 07 jari 393                 for(int c = 0; c < discScores[ h ].length; c ++) {
2 26 Feb 07 jari 394                     if( discScores[ h ][ c ] < dLow ) {
2 26 Feb 07 jari 395                       dLow = discScores[ h ][ c ];
2 26 Feb 07 jari 396                       iMin = c;
2 26 Feb 07 jari 397                     }
2 26 Feb 07 jari 398                 }
2 26 Feb 07 jari 399                 
2 26 Feb 07 jari 400                 if( uniqueClassLabels[ iMin ].equals(label)) {
2 26 Feb 07 jari 401                   numRight ++;
2 26 Feb 07 jari 402                   //System.out.println("Correct:" + uniqueClassLabels[ iMin ] + " = " + label);
2 26 Feb 07 jari 403                 } else {
2 26 Feb 07 jari 404                   numWrong ++;
2 26 Feb 07 jari 405                 }//c
2 26 Feb 07 jari 406             }//h
2 26 Feb 07 jari 407             
2 26 Feb 07 jari 408             return new USCDeltaRhoResult(delta, rho, numWrong, numRight, iGenes);
2 26 Feb 07 jari 409         }
2 26 Feb 07 jari 410     }//end trainTrain()
2 26 Feb 07 jari 411
2 26 Feb 07 jari 412
2 26 Feb 07 jari 413     /**
2 26 Feb 07 jari 414      * Does Discriminant Score Class Assignments to hybs in testArray for one
2 26 Feb 07 jari 415      * d/r
2 26 Feb 07 jari 416      * 
2 26 Feb 07 jari 417      * @param trainArray
2 26 Feb 07 jari 418      *            USCHyb[] of the hybs used for Training
2 26 Feb 07 jari 419      * @param testArray
2 26 Feb 07 jari 420      *            USCHyb[] of the hybs to be classified
2 26 Feb 07 jari 421      * @param delta
2 26 Feb 07 jari 422      *            amount by which Dik should be shrunk
2 26 Feb 07 jari 423      * @param rho
2 26 Feb 07 jari 424      *            Low Correlation threshold
2 26 Feb 07 jari 425      * @param numGenes
2 26 Feb 07 jari 426      *            Number of Genes in hybs
2 26 Feb 07 jari 427      * @param numClasses
2 26 Feb 07 jari 428      *            Number of Classes in set of hybs
2 26 Feb 07 jari 429      * @param uniqueClasses
2 26 Feb 07 jari 430      *            String[] of the labels of each class in set of hybs
2 26 Feb 07 jari 431      * @return double[ numTestHybs ][ numClasses ]
2 26 Feb 07 jari 432      */
2 26 Feb 07 jari 433     public USCResult testTest(USCHyb[] trainArray, USCHyb[] testArray,
2 26 Feb 07 jari 434             double delta, double rho, int numGenes, int numClasses,
2 26 Feb 07 jari 435             String[] uniqueClassLabels, JProgressBar corrBar, int iRho) {
2 26 Feb 07 jari 436         //keep track of gene order by creating a USCOrder object for each gene
2 26 Feb 07 jari 437         USCOrder[] order = new USCOrder[numGenes];
2 26 Feb 07 jari 438         for( int g = 0; g < numGenes; g++ ) {
2 26 Feb 07 jari 439             order[g] = new USCOrder(g);
2 26 Feb 07 jari 440         }
2 26 Feb 07 jari 441
2 26 Feb 07 jari 442         //each gene has a centroid (mean)
2 26 Feb 07 jari 443         double[] geneCentroids = this
2 26 Feb 07 jari 444                 .computeGeneCentroids(trainArray, numGenes);
2 26 Feb 07 jari 445
2 26 Feb 07 jari 446         //each class in each gene has a centroid (class mean) [ classes ][
2 26 Feb 07 jari 447         // genes ]
2 26 Feb 07 jari 448         double[][] classCentroids = this.computeClassCentroids(trainArray,
2 26 Feb 07 jari 449                 uniqueClassLabels, numGenes);
2 26 Feb 07 jari 450
2 26 Feb 07 jari 451         //each class has an mk value [ numClasses ]
2 26 Feb 07 jari 452         double[] mks = this.computeMks(trainArray, uniqueClassLabels);
2 26 Feb 07 jari 453
2 26 Feb 07 jari 454         //each gene has an si value [ numGenes ]
2 26 Feb 07 jari 455         double[] sis = this.computeSis(trainArray, classCentroids,
2 26 Feb 07 jari 456                 uniqueClassLabels, numGenes);
2 26 Feb 07 jari 457
2 26 Feb 07 jari 458         //s0 is the median si
2 26 Feb 07 jari 459         double s0 = this.computeMedian(sis);
2 26 Feb 07 jari 460
2 26 Feb 07 jari 461         //dik is the relative difference of each classCentroid from the
2 26 Feb 07 jari 462         // geneCentroid (standardized) [ class ][ gene ]
2 26 Feb 07 jari 463         double[][] dik = this.computeRelativeDifferences(classCentroids,
2 26 Feb 07 jari 464                 geneCentroids, mks, sis, s0);
2 26 Feb 07 jari 465
2 26 Feb 07 jari 466         //do soft thresholding on diks ( shrunken relative difference )
2 26 Feb 07 jari 467         double[][] dikShrunk = this.shrinkDiks(delta, dik);
2 26 Feb 07 jari 468
2 26 Feb 07 jari 469         //compute shrunken class centroids (do for all, deal with removed genes
2 26 Feb 07 jari 470         // later)
2 26 Feb 07 jari 471         double[][] shrunkenCentroids = this.computeShrunkenClassCentroid(
2 26 Feb 07 jari 472                 geneCentroids, mks, sis, s0, dikShrunk);
2 26 Feb 07 jari 473
2 26 Feb 07 jari 474         //find and store Beta (greatest |dikShrunk|)
2 26 Feb 07 jari 475         for( int g = 0; g < numGenes; g++ ) {
2 26 Feb 07 jari 476             double maxDik = 0;
2 26 Feb 07 jari 477             for( int c = 0; c < numClasses; c++ ) {
2 26 Feb 07 jari 478                 //double toTest = dikShrunk[ c ][ g ];
2 26 Feb 07 jari 479                 double toTest = Math.abs(dikShrunk[c][g]);
2 26 Feb 07 jari 480                 if( toTest > maxDik ) {
2 26 Feb 07 jari 481                     maxDik = toTest;
2 26 Feb 07 jari 482                 }
2 26 Feb 07 jari 483             }//end c (classes)
2 26 Feb 07 jari 484             order[g].setBeta(maxDik);
2 26 Feb 07 jari 485         }//end g (genes)
2 26 Feb 07 jari 486
2 26 Feb 07 jari 487         //figure out which genes to keep using and store their indices in a
2 26 Feb 07 jari 488         // BitSet
2 26 Feb 07 jari 489         //get a BitSet where each gene is marked true when it is relevant
2 26 Feb 07 jari 490         BitSet bsUse = this.findRelevantGenes(dikShrunk, order);
2 26 Feb 07 jari 491         int numRelevant = 0;
2 26 Feb 07 jari 492         for( int g = 0; g < numGenes; g++ ) {
2 26 Feb 07 jari 493             if( bsUse.get(g) == true ) {
2 26 Feb 07 jari 494                 //order[g].setRelevant(true);
2 26 Feb 07 jari 495                 numRelevant++;
2 26 Feb 07 jari 496             } else {
2 26 Feb 07 jari 497                 //order[g].setRelevant(false);
2 26 Feb 07 jari 498             }
2 26 Feb 07 jari 499         }//end g (genes)
2 26 Feb 07 jari 500
2 26 Feb 07 jari 501         //sort the genes based on Beta from greatest to least
2 26 Feb 07 jari 502         Arrays.sort(order, new USCRelevanceComparator());
2 26 Feb 07 jari 503         //from this point on, order[ numgenes] is sorted by beta
2 26 Feb 07 jari 504
2 26 Feb 07 jari 505         //store the new order in case we need it later
2 26 Feb 07 jari 506         for( int g = 0; g < order.length; g++ ) {
2 26 Feb 07 jari 507             order[g].setIRelevant(g);
2 26 Feb 07 jari 508         }//end g
2 26 Feb 07 jari 509
2 26 Feb 07 jari 510         //do pairwise correlation testing if there are any relevant genes
2 26 Feb 07 jari 511         if( numRelevant > 0 ) {
2 26 Feb 07 jari 512             this.doCorrelationTesting(order, trainArray, rho, corrBar,
2 26 Feb 07 jari 513                     geneCentroids, iRho);
2 26 Feb 07 jari 514         }//end if( numRelevant > 0 )
2 26 Feb 07 jari 515
2 26 Feb 07 jari 516         //compute discriminant scores
2 26 Feb 07 jari 517         double[][] discScores = this.computeDiscriminantScores(trainArray,
2 26 Feb 07 jari 518                 testArray, shrunkenCentroids, order, sis, s0, uniqueClassLabels);
2 26 Feb 07 jari 519
2 26 Feb 07 jari 520         //return a result if there is one
2 26 Feb 07 jari 521         if( discScores == null ) {
2 26 Feb 07 jari 522             //System.out.println( "Null Discriminant Scores\tDelta = " + delta
2 26 Feb 07 jari 523             // + "\tRho = " + rho );
2 26 Feb 07 jari 524             return null;
2 26 Feb 07 jari 525         } else {
2 26 Feb 07 jari 526             //count the number of genes used
2 26 Feb 07 jari 527             int iGenes = 0;
2 26 Feb 07 jari 528             for( int g = 0; g < order.length; g++ ) {
2 26 Feb 07 jari 529                 if( order[g].use() ) {
2 26 Feb 07 jari 530                     iGenes++;
2 26 Feb 07 jari 531                 }
2 26 Feb 07 jari 532             }//end g
2 26 Feb 07 jari 533
2 26 Feb 07 jari 534             return new USCResult(discScores, iGenes, delta, rho, order);
2 26 Feb 07 jari 535         }
2 26 Feb 07 jari 536     }//end trainTrain()
2 26 Feb 07 jari 537
2 26 Feb 07 jari 538
2 26 Feb 07 jari 539     /**
2 26 Feb 07 jari 540      * Computes the discriminant score for a new hyb compared to a trained class
2 26 Feb 07 jari 541      * by comparing the ratios of the new hyb to the shrunken class centroids
2 26 Feb 07 jari 542      * 
2 26 Feb 07 jari 543      * @param testArray ratios of the new test hyb
2 26 Feb 07 jari 544      * @param shrkClsCntrds shrunkenClassCentroids
2 26 Feb 07 jari 545      * @param order USCOrder[ numGenes ] contains sorted & original indices
2 26 Feb 07 jari 546      * @param sis s values
2 26 Feb 07 jari 547      * @param s0 s0
2 26 Feb 07 jari 548      * @param numHybs total # of hybs in training set
2 26 Feb 07 jari 549      * @return double[ numTestHybs ][ numClasses ]
2 26 Feb 07 jari 550      */
2 26 Feb 07 jari 551     private double[][] computeDiscriminantScores(USCHyb[] trainArray,
2 26 Feb 07 jari 552             USCHyb[] testArray, double[][] shrkClsCntrds, USCOrder[] order,
2 26 Feb 07 jari 553             double[] sis, double s0, String[] uniqueClasses) {
2 26 Feb 07 jari 554         double[][] toReturn = new double[testArray.length][uniqueClasses.length];
2 26 Feb 07 jari 555         
2 26 Feb 07 jari 556         //loop through the test hybs
2 26 Feb 07 jari 557         for( int h = 0; h < testArray.length; h++ ) {
2 26 Feb 07 jari 558             //compute a discriminant score for this hyb against each class
2 26 Feb 07 jari 559
2 26 Feb 07 jari 560             //loop through classes
2 26 Feb 07 jari 561             for( int c = 0; c < shrkClsCntrds.length; c++ ) {
2 26 Feb 07 jari 562                 //sum up the standardized square distances between gene &
2 26 Feb 07 jari 563                 // shrkClsCntrds for all genes 
2 26 Feb 07 jari 564                 double classScore = 0.0f;
2 26 Feb 07 jari 565                 double classProb = 0.0f;
2 26 Feb 07 jari 566
2 26 Feb 07 jari 567                 //need the class prior probability for this class
2 26 Feb 07 jari 568                 double fHybsInClassKount = ( double ) this
2 26 Feb 07 jari 569                         .getNumClassHybsInUSCHybArray(trainArray,
2 26 Feb 07 jari 570                                 uniqueClasses[c]);
2 26 Feb 07 jari 571                 double fHybKount = ( double ) trainArray.length;
2 26 Feb 07 jari 572                 classProb = Math.log(fHybsInClassKount / fHybKount);
2 26 Feb 07 jari 573
2 26 Feb 07 jari 574                 //loop through genes
2 26 Feb 07 jari 575                 for( int g = 0; g < order.length; g++ ) {
2 26 Feb 07 jari 576                     //only do computation if gene is relevant and uncorrelated
2 26 Feb 07 jari 577                     if( order[g].use() ) {
2 26 Feb 07 jari 578                         //compute square diff between gene ratio &
2 26 Feb 07 jari 579                         // shrkClsCntrds
2 26 Feb 07 jari 580
2 26 Feb 07 jari 581                         //need to know the index in unsorted array for gene of
2 26 Feb 07 jari 582                         // interest
2 26 Feb 07 jari 583                         int iOrig = order[g].getIOriginal();
2 26 Feb 07 jari 584
2 26 Feb 07 jari 585                         //get the ratio of the gene
2 26 Feb 07 jari 586                         double ratio = testArray[h].getRatio(iOrig);
2 26 Feb 07 jari 587
2 26 Feb 07 jari 588                         //calculate diffSquare
2 26 Feb 07 jari 589                         double diffSquare = ( ratio - shrkClsCntrds[c][iOrig] )
2 26 Feb 07 jari 590                                 * ( ratio - shrkClsCntrds[c][iOrig] );
2 26 Feb 07 jari 591
2 26 Feb 07 jari 592                         //divide by standardizing factor
2 26 Feb 07 jari 593                         double denom = ( sis[iOrig] + s0 ) * ( sis[iOrig] + s0 );
2 26 Feb 07 jari 594
2 26 Feb 07 jari 595                         //calculate the score for this gene
2 26 Feb 07 jari 596                         double geneScore = ( diffSquare / denom );
2 26 Feb 07 jari 597                         classScore = classScore + geneScore;
2 26 Feb 07 jari 598                     } else {
2 26 Feb 07 jari 599                         //System.out.println( "gene:" + order[ g
2 26 Feb 07 jari 600                         // ].getIOriginal() + " not used" );
2 26 Feb 07 jari 601                     }
2 26 Feb 07 jari 602                 }//end g (genes)
2 26 Feb 07 jari 603
2 26 Feb 07 jari 604                 toReturn[h][c] = ( classScore - ( 2 * classProb ) );
2 26 Feb 07 jari 605             }//end c (classes)
2 26 Feb 07 jari 606         }//end h (hybs)
2 26 Feb 07 jari 607
2 26 Feb 07 jari 608         return toReturn;
2 26 Feb 07 jari 609     }//end computeDiscriminantScore()
2 26 Feb 07 jari 610
2 26 Feb 07 jari 611
2 26 Feb 07 jari 612     /**
2 26 Feb 07 jari 613      * Having sorted the genes from Greatest to Least Beta, compare pairwise
2 26 Feb 07 jari 614      * correlation.
2 26 Feb 07 jari 615      * 
2 26 Feb 07 jari 616      * @param order
2 26 Feb 07 jari 617      * @param trainArray
2 26 Feb 07 jari 618      * @param rho
2 26 Feb 07 jari 619      */
2 26 Feb 07 jari 620     private void doCorrelationTesting(USCOrder[] order, USCHyb[] trainArray,
2 26 Feb 07 jari 621             double rho, JProgressBar corrBar, double[] geneCentroids, int iRho) {
2 26 Feb 07 jari 622         //loop through the rho values
2 26 Feb 07 jari 623         for( int r = 9; r >= iRho; r -- ) {
2 26 Feb 07 jari 624             //System.out.println("r = " + r);
2 26 Feb 07 jari 625             double dRho = (double) r / 10.0;
2 26 Feb 07 jari 626             
2 26 Feb 07 jari 627           //loop through the ordered genes
2 26 Feb 07 jari 628           for( int o = 0; o < order.length; o++ ) {
2 26 Feb 07 jari 629               //don't test if this is irrelevant or has already been deemed correlated
2 26 Feb 07 jari 630               if( order[o].use() ) {
2 26 Feb 07 jari 631                   int iFirstGene = order[o].getIOriginal();
2 26 Feb 07 jari 632   
2 26 Feb 07 jari 633                   //now test it against every other gene
2 26 Feb 07 jari 634                   for( int j = ( o + 1 ); j < order.length; j++ ) {
2 26 Feb 07 jari 635                       //only worry about it if it's relevant and hasn't already been tossed
2 26 Feb 07 jari 636                       if( order[j].use() ) {
2 26 Feb 07 jari 637                           int iSecondGene = order[j].getIOriginal();
2 26 Feb 07 jari 638   
2 26 Feb 07 jari 639                           if( iFirstGene != iSecondGene ) {
2 26 Feb 07 jari 640                               double correlation = Math.abs(this
2 26 Feb 07 jari 641                                       .computeCorrelation(trainArray, iFirstGene,
2 26 Feb 07 jari 642                                               iSecondGene, geneCentroids));
2 26 Feb 07 jari 643   
2 26 Feb 07 jari 644                               if( ( correlation > dRho ) || ( correlation == dRho ) ) {
2 26 Feb 07 jari 645                                   //System.out.println( "gene:" + order[ j
2 26 Feb 07 jari 646                                   // ].getIOriginal() + " - " + correlation + " > " + rho);
2 26 Feb 07 jari 647                                   order[j].setCorrelated(true);
2 26 Feb 07 jari 648                               } else {
2 26 Feb 07 jari 649                                   //System.out.println( "gene" + order[ j ].getIOriginal() 
2 26 Feb 07 jari 650                                   //        + " - " +correlation + " Less Than " + rho);
2 26 Feb 07 jari 651                                   order[j].setCorrelated(false);
2 26 Feb 07 jari 652                               }
2 26 Feb 07 jari 653                           }
2 26 Feb 07 jari 654                       }
2 26 Feb 07 jari 655                   }//end j
2 26 Feb 07 jari 656               }//end if
2 26 Feb 07 jari 657   
2 26 Feb 07 jari 658               corrBar.setValue(o);
2 26 Feb 07 jari 659               corrBar.setStringPainted(true);
2 26 Feb 07 jari 660           }//end o
2 26 Feb 07 jari 661         }//end r
2 26 Feb 07 jari 662     }//doCorrelationTesting()
2 26 Feb 07 jari 663
2 26 Feb 07 jari 664
2 26 Feb 07 jari 665     /**
2 26 Feb 07 jari 666      * Computes pairwise correlation between geneX and geneY
2 26 Feb 07 jari 667      * 
2 26 Feb 07 jari 668      * @param trainArray
2 26 Feb 07 jari 669      *            USCHyb[] training set
2 26 Feb 07 jari 670      * @param geneCentroids
2 26 Feb 07 jari 671      *            Overall centroid for each gene
2 26 Feb 07 jari 672      * @param iGeneX
2 26 Feb 07 jari 673      *            Original gene index of 1st gene
2 26 Feb 07 jari 674      * @param iGeneY
2 26 Feb 07 jari 675      *            Original gene index of 2nd gene
2 26 Feb 07 jari 676      * @return
2 26 Feb 07 jari 677      */
2 26 Feb 07 jari 678     private double computeCorrelation(USCHyb[] trainArray, int iGeneX,
2 26 Feb 07 jari 679             int iGeneY, double[] geneCentroids) {
2 26 Feb 07 jari 680         double toReturn = 0;
2 26 Feb 07 jari 681
2 26 Feb 07 jari 682         double[] xRatios = new double[trainArray.length];
2 26 Feb 07 jari 683         double[] yRatios = new double[trainArray.length];
2 26 Feb 07 jari 684
2 26 Feb 07 jari 685         //loop through hybs (create an array of hyb ratios)
2 26 Feb 07 jari 686         for( int i = 0; i < trainArray.length; i++ ) {
2 26 Feb 07 jari 687             USCHyb hyb = trainArray[i];
2 26 Feb 07 jari 688             xRatios[i] = hyb.getRatio(iGeneX);
2 26 Feb 07 jari 689             yRatios[i] = hyb.getRatio(iGeneY);
2 26 Feb 07 jari 690         }//end i (hybs)
2 26 Feb 07 jari 691
2 26 Feb 07 jari 692         double xMean = geneCentroids[iGeneX];
2 26 Feb 07 jari 693         double yMean = geneCentroids[iGeneY];
2 26 Feb 07 jari 694
2 26 Feb 07 jari 695         double numSum = 0;
2 26 Feb 07 jari 696         double xSum = 0;
2 26 Feb 07 jari 697         double ySum = 0;
2 26 Feb 07 jari 698
2 26 Feb 07 jari 699         for( int i = 0; i < trainArray.length; i++ ) {
2 26 Feb 07 jari 700             USCHyb hyb = trainArray[i];
2 26 Feb 07 jari 701             numSum += ( ( hyb.getRatio(iGeneX) - xMean ) * ( hyb
2 26 Feb 07 jari 702                     .getRatio(iGeneY) - yMean ) );
2 26 Feb 07 jari 703             xSum += ( ( xRatios[i] - xMean ) * ( xRatios[i] - xMean ) );
2 26 Feb 07 jari 704             ySum += ( ( yRatios[i] - yMean ) * ( yRatios[i] - yMean ) );
2 26 Feb 07 jari 705         }
2 26 Feb 07 jari 706
2 26 Feb 07 jari 707         toReturn = numSum / ( double ) Math.sqrt(xSum * ySum);
2 26 Feb 07 jari 708         return toReturn;
2 26 Feb 07 jari 709     }//end computeCorrelation()
2 26 Feb 07 jari 710
2 26 Feb 07 jari 711
2 26 Feb 07 jari 712     /**
2 26 Feb 07 jari 713      * Calculates the shrunken class centroid = class centroid + mk( si + s0
2 26 Feb 07 jari 714      * )dikShrunk
2 26 Feb 07 jari 715      * 
2 26 Feb 07 jari 716      * @param geneCentroids
2 26 Feb 07 jari 717      * @param mks
2 26 Feb 07 jari 718      * @param sis
2 26 Feb 07 jari 719      * @param s0
2 26 Feb 07 jari 720      * @param dikShrunk
2 26 Feb 07 jari 721      * @return
2 26 Feb 07 jari 722      */
2 26 Feb 07 jari 723     private double[][] computeShrunkenClassCentroid(double[] geneCentroids,
2 26 Feb 07 jari 724             double[] mks, double[] sis, double s0, double[][] dikShrunk) {
2 26 Feb 07 jari 725         double[][] toReturn = new double[mks.length][geneCentroids.length];
2 26 Feb 07 jari 726
2 26 Feb 07 jari 727         //loop through the classes
2 26 Feb 07 jari 728         for( int c = 0; c < mks.length; c++ ) {
2 26 Feb 07 jari 729             //loop through the genes
2 26 Feb 07 jari 730             for( int g = 0; g < geneCentroids.length; g++ ) {
2 26 Feb 07 jari 731                 toReturn[c][g] = geneCentroids[g]
2 26 Feb 07 jari 732                         + ( mks[c] * dikShrunk[c][g] * ( sis[g] + s0 ) );
2 26 Feb 07 jari 733             }//end j (genes)
2 26 Feb 07 jari 734         }//end i (classes)
2 26 Feb 07 jari 735
2 26 Feb 07 jari 736         return toReturn;
2 26 Feb 07 jari 737     }//end computeShrunkenClassCentroid()
2 26 Feb 07 jari 738
2 26 Feb 07 jari 739
2 26 Feb 07 jari 740     /**
2 26 Feb 07 jari 741      * Finds the shrunken dik value whose absolute value is greatest
2 26 Feb 07 jari 742      * 
2 26 Feb 07 jari 743      * @param dikSig
2 26 Feb 07 jari 744      * @return
2 26 Feb 07 jari 745      */
2 26 Feb 07 jari 746     private double[] findBeta(double[][] dikSig) {
2 26 Feb 07 jari 747         double[] toReturn = new double[dikSig[0].length];
2 26 Feb 07 jari 748
2 26 Feb 07 jari 749         //loop through genes
2 26 Feb 07 jari 750         for( int i = 0; i < toReturn.length; i++ ) {
2 26 Feb 07 jari 751             double currentHigh = 0;
2 26 Feb 07 jari 752
2 26 Feb 07 jari 753             //loop through class shrunken diks
2 26 Feb 07 jari 754             for( int j = 0; j < dikSig.length; j++ ) {
2 26 Feb 07 jari 755                 if( dikSig[j][i] > currentHigh ) {
2 26 Feb 07 jari 756                     currentHigh = dikSig[j][i];
2 26 Feb 07 jari 757                 }
2 26 Feb 07 jari 758             }//end j
2 26 Feb 07 jari 759
2 26 Feb 07 jari 760             toReturn[i] = currentHigh;
2 26 Feb 07 jari 761         }//end i
2 26 Feb 07 jari 762
2 26 Feb 07 jari 763         return toReturn;
2 26 Feb 07 jari 764     }//end findBeta()
2 26 Feb 07 jari 765
2 26 Feb 07 jari 766
2 26 Feb 07 jari 767     /**
2 26 Feb 07 jari 768      * Removes the genes identified by index in vRemove
2 26 Feb 07 jari 769      * 
2 26 Feb 07 jari 770      * @param dikShrunk
2 26 Feb 07 jari 771      *            double[ class ][ genes ]
2 26 Feb 07 jari 772      * @param vRemove
2 26 Feb 07 jari 773      *            Vector of Integer objects representing gene index of genes to
2 26 Feb 07 jari 774      *            remove
2 26 Feb 07 jari 775      * @return new double[ class ][ genes ] with insignificant genes removed
2 26 Feb 07 jari 776      */
2 26 Feb 07 jari 777     private double[][] removeInsignificantGenes(double[][] dikShrunk,
2 26 Feb 07 jari 778             Vector vRemove) {
2 26 Feb 07 jari 779         int numGenes = dikShrunk[0].length;
2 26 Feb 07 jari 780         int iSignificant = numGenes - vRemove.size();
2 26 Feb 07 jari 781         int index = 0;
2 26 Feb 07 jari 782         double[][] toReturn = new double[dikShrunk.length][iSignificant];
2 26 Feb 07 jari 783
2 26 Feb 07 jari 784         for( int i = 0; i < numGenes; i++ ) {
2 26 Feb 07 jari 785             boolean include = true;
2 26 Feb 07 jari 786
2 26 Feb 07 jari 787             //loop through the remove indices
2 26 Feb 07 jari 788             for( int j = 0; j < vRemove.size(); j++ ) {
2 26 Feb 07 jari 789                 Integer IRemove = ( Integer ) vRemove.elementAt(j);
2 26 Feb 07 jari 790                 if( i == IRemove.intValue() ) {
2 26 Feb 07 jari 791                     //this is a gene that should be removed
2 26 Feb 07 jari 792                     include = false;
2 26 Feb 07 jari 793                     break;
2 26 Feb 07 jari 794                 }
2 26 Feb 07 jari 795             }//end j (remove indices)
2 26 Feb 07 jari 796
2 26 Feb 07 jari 797             if( include ) {
2 26 Feb 07 jari 798                 //loop through classes
2 26 Feb 07 jari 799                 for( int j = 0; j < dikShrunk.length; j++ ) {
2 26 Feb 07 jari 800                     //System.out.println( "i:" + i + "\tj:" + j + "\tindex:" +
2 26 Feb 07 jari 801                     // index );
2 26 Feb 07 jari 802                     toReturn[j][index] = dikShrunk[j][i];
2 26 Feb 07 jari 803                 }//end j
2 26 Feb 07 jari 804                 index++;
2 26 Feb 07 jari 805             }
2 26 Feb 07 jari 806         }
2 26 Feb 07 jari 807
2 26 Feb 07 jari 808         return toReturn;
2 26 Feb 07 jari 809     }//end removeInsignificantGenes()
2 26 Feb 07 jari 810
2 26 Feb 07 jari 811
2 26 Feb 07 jari 812     /**
2 26 Feb 07 jari 813      * Looks through the Shrunken d values (dikStrunk) for those that DO NOT
2 26 Feb 07 jari 814      * have at least 1 non zero dikShrunk
2 26 Feb 07 jari 815      * 
2 26 Feb 07 jari 816      * @param dikShrunk
2 26 Feb 07 jari 817      * @return BitSet 0 should be removed. 1 should be used. to be removed.
2 26 Feb 07 jari 818      */
2 26 Feb 07 jari 819     private BitSet findRelevantGenes(double[][] dikShrunk, USCOrder[] order) {
2 26 Feb 07 jari 820         BitSet toReturn = new BitSet(dikShrunk[0].length);
2 26 Feb 07 jari 821
2 26 Feb 07 jari 822         //loop through the genes
2 26 Feb 07 jari 823         for( int i = 0; i < dikShrunk[0].length; i++ ) {
2 26 Feb 07 jari 824             //loop through the classes, looking for at least 1 positive
2 26 Feb 07 jari 825             // absolute value
2 26 Feb 07 jari 826             for( int j = 0; j < dikShrunk.length; j++ ) {
2 26 Feb 07 jari 827                 if( Math.abs(dikShrunk[j][i]) > 0.000000000000000 ) {
2 26 Feb 07 jari 828                     //System.out.println("Gene " + i + " is relevant");
2 26 Feb 07 jari 829                     toReturn.flip(i);
2 26 Feb 07 jari 830                     order[ i ].setRelevant(true);
2 26 Feb 07 jari 831                     break;
2 26 Feb 07 jari 832                 }
2 26 Feb 07 jari 833             }//end j (classes)
2 26 Feb 07 jari 834         }//end i (genes)
2 26 Feb 07 jari 835
2 26 Feb 07 jari 836         return toReturn;
2 26 Feb 07 jari 837     }//end removeCorrelatedGene()
2 26 Feb 07 jari 838
2 26 Feb 07 jari 839
2 26 Feb 07 jari 840     /**
2 26 Feb 07 jari 841      * Computes the mean ratio value of all the hybs in each gene
2 26 Feb 07 jari 842      * 
2 26 Feb 07 jari 843      * @param trainArray
2 26 Feb 07 jari 844      *            USCHyb[] consisting of training hybs
2 26 Feb 07 jari 845      * @param numGenes
2 26 Feb 07 jari 846      *            The number of genes in these hybs
2 26 Feb 07 jari 847      * @return double[ numGenes ] of mean ratio
2 26 Feb 07 jari 848      */
2 26 Feb 07 jari 849     private double[] computeGeneCentroids(USCHyb[] trainArray, int numGenes) {
2 26 Feb 07 jari 850         double[] toReturn = new double[numGenes];
2 26 Feb 07 jari 851
2 26 Feb 07 jari 852         //loop through the genes
2 26 Feb 07 jari 853         for( int i = 0; i < numGenes; i++ ) {
2 26 Feb 07 jari 854             double ratioTotal = 0;
2 26 Feb 07 jari 855
2 26 Feb 07 jari 856             //loop through the hybs
2 26 Feb 07 jari 857             for( int j = 0; j < trainArray.length; j++ ) {
2 26 Feb 07 jari 858                 ratioTotal = ratioTotal + trainArray[j].getRatio(i);
2 26 Feb 07 jari 859             }
2 26 Feb 07 jari 860
2 26 Feb 07 jari 861             toReturn[i] = ( ratioTotal / ( double ) trainArray.length );
2 26 Feb 07 jari 862             //System.out.println( "geneCentroid[ " + i + " ] = " + toReturn[ i
2 26 Feb 07 jari 863             // ] );
2 26 Feb 07 jari 864         }
2 26 Feb 07 jari 865
2 26 Feb 07 jari 866         return toReturn;
2 26 Feb 07 jari 867     }//end computeOverallCentroid()
2 26 Feb 07 jari 868
2 26 Feb 07 jari 869
2 26 Feb 07 jari 870     /**
2 26 Feb 07 jari 871      * Computes the mean ratio value of the hybs in each class for each gene
2 26 Feb 07 jari 872      * 
2 26 Feb 07 jari 873      * @param trainArray
2 26 Feb 07 jari 874      *            USCHyb[] consisting of training hybs
2 26 Feb 07 jari 875      * @param classLabels
2 26 Feb 07 jari 876      *            String[] of unique class labels
2 26 Feb 07 jari 877      * @param numGenes
2 26 Feb 07 jari 878      *            Number of genes
2 26 Feb 07 jari 879      * @return double[ class ][ genes ] of class mean ratios
2 26 Feb 07 jari 880      */
2 26 Feb 07 jari 881     private double[][] computeClassCentroids(USCHyb[] trainArray,
2 26 Feb 07 jari 882             String[] classLabels, int numGenes) {
2 26 Feb 07 jari 883         double[][] toReturn = new double[classLabels.length][numGenes];
2 26 Feb 07 jari 884
2 26 Feb 07 jari 885         //loop through the genes
2 26 Feb 07 jari 886         for( int i = 0; i < numGenes; i++ ) {
2 26 Feb 07 jari 887             //loop through the classes
2 26 Feb 07 jari 888             for( int j = 0; j < classLabels.length; j++ ) {
2 26 Feb 07 jari 889                 double total = 0;
2 26 Feb 07 jari 890                 int kount = 0;
2 26 Feb 07 jari 891
2 26 Feb 07 jari 892                 //loop through the hybs in the training array
2 26 Feb 07 jari 893                 for( int k = 0; k < trainArray.length; k++ ) {
2 26 Feb 07 jari 894                     if( trainArray[k].getHybLabel().equalsIgnoreCase(
2 26 Feb 07 jari 895                             classLabels[j]) ) {
2 26 Feb 07 jari 896                         //found one
2 26 Feb 07 jari 897                         total = total + trainArray[k].getRatio(i);
2 26 Feb 07 jari 898                         kount++;
2 26 Feb 07 jari 899                     }
2 26 Feb 07 jari 900                 }//end k(train hybs)
2 26 Feb 07 jari 901
2 26 Feb 07 jari 902                 toReturn[j][i] = ( total / ( double ) kount );
2 26 Feb 07 jari 903                 //System.out.println( "classCentroid[ " + j + " ][ " + i + " ]
2 26 Feb 07 jari 904                 // = " + toReturn[ j ][ i ] );
2 26 Feb 07 jari 905             }//end j(classes)
2 26 Feb 07 jari 906         }//end i(genes)
2 26 Feb 07 jari 907
2 26 Feb 07 jari 908         return toReturn;
2 26 Feb 07 jari 909     }//end computeClassCentroid()
2 26 Feb 07 jari 910
2 26 Feb 07 jari 911
2 26 Feb 07 jari 912     /**
2 26 Feb 07 jari 913      * Computes the Relative Difference between a class a gene centroid
2 26 Feb 07 jari 914      * 
2 26 Feb 07 jari 915      * @param classCentroids
2 26 Feb 07 jari 916      * @param geneCentroids
2 26 Feb 07 jari 917      * @param mks
2 26 Feb 07 jari 918      * @param sis
2 26 Feb 07 jari 919      * @param s0
2 26 Feb 07 jari 920      * @return
2 26 Feb 07 jari 921      */
2 26 Feb 07 jari 922     private double[][] computeRelativeDifferences(double[][] classCentroids,
2 26 Feb 07 jari 923             double[] geneCentroids, double[] mks, double[] sis, double s0) {
2 26 Feb 07 jari 924         double[][] toReturn = new double[classCentroids.length][classCentroids[0].length];
2 26 Feb 07 jari 925
2 26 Feb 07 jari 926         //loop through the classes
2 26 Feb 07 jari 927         for( int c = 0; c < classCentroids.length; c++ ) {
2 26 Feb 07 jari 928
2 26 Feb 07 jari 929             //loop through the genes
2 26 Feb 07 jari 930             for( int g = 0; g < classCentroids[0].length; g++ ) {
2 26 Feb 07 jari 931                 toReturn[c][g] = ( classCentroids[c][g] - geneCentroids[g] )
2 26 Feb 07 jari 932                         / ( mks[c] * ( sis[g] + s0 ) );
2 26 Feb 07 jari 933                 //System.out.println( "[ " + i + " ][ " + j + " ]( " +
2 26 Feb 07 jari 934                 // classCentroids[ i ][ j ] + " - "
2 26 Feb 07 jari 935                 //+ geneCentroids[ j ] + " ) / ( " + mks[ i ] + " * ( " + sis[
2 26 Feb 07 jari 936                 // j ] + " + " + s0 + " ) ) = " + toReturn[ i ][ j ] );
2 26 Feb 07 jari 937                 //System.out.println( "relDiff[ " + i + " ][ " + j + " ] = " +
2 26 Feb 07 jari 938                 // toReturn[ i ][ j ] );
2 26 Feb 07 jari 939             }//end j(genes)
2 26 Feb 07 jari 940         }//end i(classes)
2 26 Feb 07 jari 941
2 26 Feb 07 jari 942         return toReturn;
2 26 Feb 07 jari 943     }//end computeRelativeDifferences()
2 26 Feb 07 jari 944
2 26 Feb 07 jari 945
2 26 Feb 07 jari 946     /**
2 26 Feb 07 jari 947      * Computes and sums the within class standard deviations of classes of a
2 26 Feb 07 jari 948      * gene
2 26 Feb 07 jari 949      * 
2 26 Feb 07 jari 950      * @param trainArray
2 26 Feb 07 jari 951      * @param classCentroids
2 26 Feb 07 jari 952      * @param classLabels
2 26 Feb 07 jari 953      * @param numGenes
2 26 Feb 07 jari 954      * @return
2 26 Feb 07 jari 955      */
2 26 Feb 07 jari 956     private double[] computeSis(USCHyb[] trainArray, double[][] classCentroids,
2 26 Feb 07 jari 957             String[] classLabels, int numGenes) {
2 26 Feb 07 jari 958         double firstTerm = 1.00f / ( ( double ) trainArray.length - ( double ) classLabels.length );
2 26 Feb 07 jari 959         //System.out.println( "firstTerm:" + firstTerm );
2 26 Feb 07 jari 960
2 26 Feb 07 jari 961         double[] toReturn = new double[numGenes];
2 26 Feb 07 jari 962
2 26 Feb 07 jari 963         //loop through genes
2 26 Feb 07 jari 964         for( int i = 0; i < numGenes; i++ ) {
2 26 Feb 07 jari 965             double geneSum = 0;
2 26 Feb 07 jari 966
2 26 Feb 07 jari 967             //loop through the classes
2 26 Feb 07 jari 968             for( int j = 0; j < classLabels.length; j++ ) {
2 26 Feb 07 jari 969                 double classDiffSquareSum = 0;
2 26 Feb 07 jari 970
2 26 Feb 07 jari 971                 //loop through the hybs in trainArray
2 26 Feb 07 jari 972                 for( int k = 0; k < trainArray.length; k++ ) {
2 26 Feb 07 jari 973                     //deal with this class's hybs only
2 26 Feb 07 jari 974                     if( trainArray[k].getHybLabel().equalsIgnoreCase(
2 26 Feb 07 jari 975                             classLabels[j]) ) {
2 26 Feb 07 jari 976                         double difference = trainArray[k].getRatio(i)
2 26 Feb 07 jari 977                                 - classCentroids[j][i];
2 26 Feb 07 jari 978                         double diffSquare = difference * difference;
2 26 Feb 07 jari 979                         classDiffSquareSum = classDiffSquareSum + diffSquare;
2 26 Feb 07 jari 980                     }
2 26 Feb 07 jari 981                 }//end k(hybs in class)
2 26 Feb 07 jari 982
2 26 Feb 07 jari 983                 geneSum = geneSum + classDiffSquareSum;
2 26 Feb 07 jari 984             }//end j(classes)
2 26 Feb 07 jari 985
2 26 Feb 07 jari 986             toReturn[i] = ( double ) Math.sqrt(firstTerm * geneSum);
2 26 Feb 07 jari 987             //System.out.println( "si[ " + i + " ] = " + toReturn[ i ] );
2 26 Feb 07 jari 988         }//end i(genes)
2 26 Feb 07 jari 989
2 26 Feb 07 jari 990         return toReturn;
2 26 Feb 07 jari 991     }//end computeSis()
2 26 Feb 07 jari 992
2 26 Feb 07 jari 993
2 26 Feb 07 jari 994     /**
2 26 Feb 07 jari 995      * Computes Mk
2 26 Feb 07 jari 996      * 
2 26 Feb 07 jari 997      * @param trainArray
2 26 Feb 07 jari 998      * @param classLabels
2 26 Feb 07 jari 999      * @return
2 26 Feb 07 jari 1000      */
2 26 Feb 07 jari 1001     private double[] computeMks(USCHyb[] trainArray, String[] classLabels) {
2 26 Feb 07 jari 1002         double[] toReturn = new double[classLabels.length];
2 26 Feb 07 jari 1003
2 26 Feb 07 jari 1004         //loop through the classes
2 26 Feb 07 jari 1005         for( int i = 0; i < classLabels.length; i++ ) {
2 26 Feb 07 jari 1006             int kount = 0;
2 26 Feb 07 jari 1007
2 26 Feb 07 jari 1008             //find out how many hybs in trainArray are in class[ i ]
2 26 Feb 07 jari 1009             for( int j = 0; j < trainArray.length; j++ ) {
2 26 Feb 07 jari 1010                 if( trainArray[j].getHybLabel()
2 26 Feb 07 jari 1011                         .equalsIgnoreCase(classLabels[i]) ) {
2 26 Feb 07 jari 1012                     kount++;
2 26 Feb 07 jari 1013                 }
2 26 Feb 07 jari 1014             }//end j
2 26 Feb 07 jari 1015
2 26 Feb 07 jari 1016             double firstTerm = 1.00f / ( double ) kount;
2 26 Feb 07 jari 1017             double secondTerm = 1.00f / ( double ) trainArray.length;
2 26 Feb 07 jari 1018             //System.out.println( "kount = " + kount + "\ttrainArray.length = "
2 26 Feb 07 jari 1019             // + trainArray.length );
2 26 Feb 07 jari 1020
2 26 Feb 07 jari 1021             toReturn[i] = ( double ) Math.sqrt(firstTerm + secondTerm);
2 26 Feb 07 jari 1022             //System.out.println( "mk[ " + i + " ] = " + toReturn[ i ] );
2 26 Feb 07 jari 1023         }//end i
2 26 Feb 07 jari 1024
2 26 Feb 07 jari 1025         return toReturn;
2 26 Feb 07 jari 1026     }//end computeMks
2 26 Feb 07 jari 1027
2 26 Feb 07 jari 1028
2 26 Feb 07 jari 1029     /**
2 26 Feb 07 jari 1030      * Computes the average value of the doubles in array
2 26 Feb 07 jari 1031      * 
2 26 Feb 07 jari 1032      * @param array
2 26 Feb 07 jari 1033      * @return
2 26 Feb 07 jari 1034      */
2 26 Feb 07 jari 1035     private double computeMean(double[] array) {
2 26 Feb 07 jari 1036         double toReturn = 0;
2 26 Feb 07 jari 1037
2 26 Feb 07 jari 1038         for( int i = 0; i < array.length; i++ ) {
2 26 Feb 07 jari 1039             toReturn = toReturn + array[i];
2 26 Feb 07 jari 1040         }
2 26 Feb 07 jari 1041
2 26 Feb 07 jari 1042         return toReturn / ( double ) array.length;
2 26 Feb 07 jari 1043     }//end computeMean()
2 26 Feb 07 jari 1044
2 26 Feb 07 jari 1045
2 26 Feb 07 jari 1046     /**
2 26 Feb 07 jari 1047      * Finds or computes the median value in array
2 26 Feb 07 jari 1048      * 
2 26 Feb 07 jari 1049      * @param array
2 26 Feb 07 jari 1050      * @return
2 26 Feb 07 jari 1051      */
2 26 Feb 07 jari 1052     private double computeMedian(double[] array) {
2 26 Feb 07 jari 1053         //create a new array so we don't mess with the order of the original
2 26 Feb 07 jari 1054         double[] copy = new double[array.length];
2 26 Feb 07 jari 1055         for( int i = 0; i < array.length; i++ ) {
2 26 Feb 07 jari 1056             copy[i] = array[i];
2 26 Feb 07 jari 1057         }
2 26 Feb 07 jari 1058
2 26 Feb 07 jari 1059         //sort the array first
2 26 Feb 07 jari 1060         Arrays.sort(copy);
2 26 Feb 07 jari 1061
2 26 Feb 07 jari 1062         //find the middle value
2 26 Feb 07 jari 1063         int half = copy.length / 2;
2 26 Feb 07 jari 1064         int remainder = copy.length % 2;
2 26 Feb 07 jari 1065         if( remainder == 0 ) {
2 26 Feb 07 jari 1066             //even number, use mean of 2 middle values
2 26 Feb 07 jari 1067             return copy[half];
2 26 Feb 07 jari 1068             //return ( copy[half - 1] + copy[half] ) / 2;
2 26 Feb 07 jari 1069         } else {
2 26 Feb 07 jari 1070             //odd number, use middle value
2 26 Feb 07 jari 1071             return copy[half];
2 26 Feb 07 jari 1072         }
2 26 Feb 07 jari 1073     }//end findMedian()
2 26 Feb 07 jari 1074
2 26 Feb 07 jari 1075
2 26 Feb 07 jari 1076     /**
2 26 Feb 07 jari 1077      * Does soft thresholding on Dik values by subtracting delta from the
2 26 Feb 07 jari 1078      * absolute value of Dik and then reattaching the sign or replacing with 0
2 26 Feb 07 jari 1079      * if the subtraction is negative
2 26 Feb 07 jari 1080      * 
2 26 Feb 07 jari 1081      * @param delta
2 26 Feb 07 jari 1082      * @param diks
2 26 Feb 07 jari 1083      * @return
2 26 Feb 07 jari 1084      */
2 26 Feb 07 jari 1085     private double[][] shrinkDiks(double delta, double[][] diks) {
2 26 Feb 07 jari 1086         double[][] toReturn = new double[diks.length][diks[0].length];
2 26 Feb 07 jari 1087
2 26 Feb 07 jari 1088         //classes
2 26 Feb 07 jari 1089         for( int i = 0; i < diks.length; i++ ) {
2 26 Feb 07 jari 1090             //genes
2 26 Feb 07 jari 1091             for( int j = 0; j < diks[0].length; j++ ) {
2 26 Feb 07 jari 1092                 toReturn[i][j] = this.shrinkDik(delta, diks[i][j]);
2 26 Feb 07 jari 1093             }
2 26 Feb 07 jari 1094         }
2 26 Feb 07 jari 1095
2 26 Feb 07 jari 1096         return toReturn;
2 26 Feb 07 jari 1097     }//end shrinkDiks()
2 26 Feb 07 jari 1098
2 26 Feb 07 jari 1099
2 26 Feb 07 jari 1100     /**
2 26 Feb 07 jari 1101      * Shrink dik
2 26 Feb 07 jari 1102      * 
2 26 Feb 07 jari 1103      * @param delta
2 26 Feb 07 jari 1104      * @param dik
2 26 Feb 07 jari 1105      * @return
2 26 Feb 07 jari 1106      */
2 26 Feb 07 jari 1107     private double shrinkDik(double delta, double dik) {
2 26 Feb 07 jari 1108         double toReturn = 0;
2 26 Feb 07 jari 1109
2 26 Feb 07 jari 1110         if( dik < 0 ) {
2 26 Feb 07 jari 1111             toReturn = -dik - delta;
2 26 Feb 07 jari 1112         } else {
2 26 Feb 07 jari 1113             toReturn = dik - delta;
2 26 Feb 07 jari 1114         }
2 26 Feb 07 jari 1115
2 26 Feb 07 jari 1116         if( toReturn < 0 ) {
2 26 Feb 07 jari 1117             toReturn = 0;
2 26 Feb 07 jari 1118         } else if( dik < 0 ) {
2 26 Feb 07 jari 1119             toReturn = -toReturn;
2 26 Feb 07 jari 1120         }
2 26 Feb 07 jari 1121
2 26 Feb 07 jari 1122         return toReturn;
2 26 Feb 07 jari 1123     }//end shrinkCentroid()
2 26 Feb 07 jari 1124
2 26 Feb 07 jari 1125
2 26 Feb 07 jari 1126     /**
2 26 Feb 07 jari 1127      * Wierd and stupid. Knee deep in my own shit.
2 26 Feb 07 jari 1128      * 
2 26 Feb 07 jari 1129      * @param trainArray
2 26 Feb 07 jari 1130      * @param sClassLabel
2 26 Feb 07 jari 1131      * @return
2 26 Feb 07 jari 1132      */
2 26 Feb 07 jari 1133     public int[] findHybIndicesForClass(USCHyb[] trainArray, int classIndex,
2 26 Feb 07 jari 1134             USCHybSet hybSet) {
2 26 Feb 07 jari 1135         Vector vHybInClass = new Vector();
2 26 Feb 07 jari 1136
2 26 Feb 07 jari 1137         for( int i = 0; i < trainArray.length; i++ ) {
2 26 Feb 07 jari 1138             USCHyb hyb = trainArray[i];
2 26 Feb 07 jari 1139             if( hyb.getHybLabel().equals(hybSet.getUniqueClass(classIndex)) ) {
2 26 Feb 07 jari 1140                 vHybInClass.add(new Integer(i));
2 26 Feb 07 jari 1141             }
2 26 Feb 07 jari 1142         }
2 26 Feb 07 jari 1143
2 26 Feb 07 jari 1144         int[] toReturn = new int[vHybInClass.size()];
2 26 Feb 07 jari 1145         for( int i = 0; i < toReturn.length; i++ ) {
2 26 Feb 07 jari 1146             Integer I = ( Integer ) vHybInClass.elementAt(i);
2 26 Feb 07 jari 1147             toReturn[i] = I.intValue();
2 26 Feb 07 jari 1148         }
2 26 Feb 07 jari 1149
2 26 Feb 07 jari 1150         return toReturn;
2 26 Feb 07 jari 1151     }//end findHybIndicesForClass()
2 26 Feb 07 jari 1152
2 26 Feb 07 jari 1153
2 26 Feb 07 jari 1154     /**
2 26 Feb 07 jari 1155      * Kounts the # of hybs that belong to the a class
2 26 Feb 07 jari 1156      * 
2 26 Feb 07 jari 1157      * @param hybs
2 26 Feb 07 jari 1158      * @param label
2 26 Feb 07 jari 1159      * @return
2 26 Feb 07 jari 1160      */
2 26 Feb 07 jari 1161     private int getNumClassHybsInUSCHybArray(USCHyb[] hybs, String label) {
2 26 Feb 07 jari 1162         int kount = 0;
2 26 Feb 07 jari 1163
2 26 Feb 07 jari 1164         for( int h = 0; h < hybs.length; h++ ) {
2 26 Feb 07 jari 1165             USCHyb hyb = hybs[h];
2 26 Feb 07 jari 1166
2 26 Feb 07 jari 1167             if( hyb.getHybLabel().equals(label) ) {
2 26 Feb 07 jari 1168                 kount++;
2 26 Feb 07 jari 1169             }
2 26 Feb 07 jari 1170         }
2 26 Feb 07 jari 1171
2 26 Feb 07 jari 1172         return kount;
2 26 Feb 07 jari 1173     }//end getNumClassHybsInUSCHybArray()
2 26 Feb 07 jari 1174
2 26 Feb 07 jari 1175
2 26 Feb 07 jari 1176     /**
2 26 Feb 07 jari 1177      * Creates and returns an array of all the USCHybs that belong to a class
2 26 Feb 07 jari 1178      * 
2 26 Feb 07 jari 1179      * @param hybs
2 26 Feb 07 jari 1180      * @param label
2 26 Feb 07 jari 1181      * @return
2 26 Feb 07 jari 1182      */
2 26 Feb 07 jari 1183     private USCHyb[] getClassHybsInUSCHybArray(USCHyb[] hybs, String label) {
2 26 Feb 07 jari 1184         USCHyb[] toReturn = new USCHyb[this.getNumClassHybsInUSCHybArray(hybs,
2 26 Feb 07 jari 1185                 label)];
2 26 Feb 07 jari 1186         int kount = 0;
2 26 Feb 07 jari 1187
2 26 Feb 07 jari 1188         for( int h = 0; h < toReturn.length; h++ ) {
2 26 Feb 07 jari 1189             USCHyb hyb = hybs[h];
2 26 Feb 07 jari 1190
2 26 Feb 07 jari 1191             if( hyb.getHybLabel().equals(label) ) {
2 26 Feb 07 jari 1192                 toReturn[kount] = hyb;
2 26 Feb 07 jari 1193                 kount++;
2 26 Feb 07 jari 1194             }
2 26 Feb 07 jari 1195         }
2 26 Feb 07 jari 1196
2 26 Feb 07 jari 1197         return toReturn;
2 26 Feb 07 jari 1198     }//end getClassHybsInUSCHybArray()
2 26 Feb 07 jari 1199
2 26 Feb 07 jari 1200
2 26 Feb 07 jari 1201     /**
2 26 Feb 07 jari 1202      * Computes log base 10 of x
2 26 Feb 07 jari 1203      * 
2 26 Feb 07 jari 1204      * @param x
2 26 Feb 07 jari 1205      * @return
2 26 Feb 07 jari 1206      */
2 26 Feb 07 jari 1207     private double computeCommonLog(double x) {
2 26 Feb 07 jari 1208         double toReturn = 0.0f;
2 26 Feb 07 jari 1209         toReturn = ( double ) Math.log(x) / ( double ) Math.log(10);
2 26 Feb 07 jari 1210         return toReturn;
2 26 Feb 07 jari 1211     }//computeCommonLog()
2 26 Feb 07 jari 1212 }//end class
2 26 Feb 07 jari 1213
2 26 Feb 07 jari 1214 /*
2 26 Feb 07 jari 1215  * //loop through the testHybs assignments [ numTestHybs ] USCClassAssignment[]
2 26 Feb 07 jari 1216  * assigns = this.trainTrain( trainArray, testArray, delta, rho,
2 26 Feb 07 jari 1217  * fullSet.getNumGenes(), fullSet.getNumClasses(), fullSet.getUniqueClasses() );
2 26 Feb 07 jari 1218  * 
2 26 Feb 07 jari 1219  * 
2 26 Feb 07 jari 1220  * //make sure there was at least 1 nonzero discriminant score if( assigns ==
2 26 Feb 07 jari 1221  * null ) { //do nothing System.out.println( "Assigns null for delta = " + delta +
2 26 Feb 07 jari 1222  * "\trho = " + rho ); } else { //during cross validation, we need to know the
2 26 Feb 07 jari 1223  * real hyb class for( int h = 0; h < assigns.length; h ++ ) { String hybLabel =
2 26 Feb 07 jari 1224  * testArray[ h ].getHybLabel(); int iLabel; for( int u = 0; u <
2 26 Feb 07 jari 1225  * fullSet.getNumClasses(); u ++ ) { if( hybLabel.equals(
2 26 Feb 07 jari 1226  * fullSet.getUniqueClass( u ) ) ) { iLabel = u; assigns[ h
2 26 Feb 07 jari 1227  * ].setUniqueClassIndex( u ); } }//end u (classes) }//end h (hybs) //sb.append(
2 26 Feb 07 jari 1228  * f + 1 + "\t" + d + "\t" + delta + "\t" + rho);
2 26 Feb 07 jari 1229  * 
2 26 Feb 07 jari 1230  * //keep track of how many genes were used to make this call USCOrder[] order =
2 26 Feb 07 jari 1231  * assigns[ 0 ].getOrder(); int iUsed = 0; for( int o = 0; o < order.length; o ++ ) {
2 26 Feb 07 jari 1232  * if( order[ o ].isRelevant() && ! order[ o ].isCorrelated() ) { iUsed ++; } }
2 26 Feb 07 jari 1233  * //sb.append( "\t" + iUsed );
2 26 Feb 07 jari 1234  * 
2 26 Feb 07 jari 1235  * //loop through assignments to create file for( int a = 0; a < assigns.length;
2 26 Feb 07 jari 1236  * a ++ ) { USCHyb hyb = testArray[ a ];
2 26 Feb 07 jari 1237  * 
2 26 Feb 07 jari 1238  * //sb.append( "\t" + hyb.getHybName() ); //sb.append( "\t" + assigns[ a
2 26 Feb 07 jari 1239  * ].getUniqueClassIndex() ); //sb.append( "\t" + assigns[ a
2 26 Feb 07 jari 1240  * ].getAssignedClassIndex() ); //sb.append( "\t" + hyb.getHybLabel() );
2 26 Feb 07 jari 1241  * //sb.append( "\t" + fullSet.getUniqueClass( assigns[ a
2 26 Feb 07 jari 1242  * ].getAssignedClassIndex() ) );
2 26 Feb 07 jari 1243  * 
2 26 Feb 07 jari 1244  * for( int q = 0; q < assigns[ a ].getDiscriminantScoreArray().length; q ++ ) {
2 26 Feb 07 jari 1245  * //sb.append( "\t" + assigns[ a ].getDiscriminantScore( q ) ); }
2 26 Feb 07 jari 1246  * 
2 26 Feb 07 jari 1247  * //System.out.println( testArray[ a ].getHybName() + " assigned to " +
2 26 Feb 07 jari 1248  * //assigns[ a ].getAssignedClassIndex() ); }//end a (assigns)
2 26 Feb 07 jari 1249  * 
2 26 Feb 07 jari 1250  * //sb.append( "\r\n" ); }//end else
2 26 Feb 07 jari 1251  */
2 26 Feb 07 jari 1252 /*
2 26 Feb 07 jari 1253  * for( int i = 0; i < testArray.length; i ++ ) { System.out.println( "test\t" +
2 26 Feb 07 jari 1254  * testArray[ i ].getHybName() ); }
2 26 Feb 07 jari 1255  * 
2 26 Feb 07 jari 1256  * //how many genes, how many classes int numGenes = this.fullSet.getNumGenes();
2 26 Feb 07 jari 1257  * int numClasses = this.fullSet.getNumClasses();
2 26 Feb 07 jari 1258  * 
2 26 Feb 07 jari 1259  * //need to keep track of gene sort order USCOrder[] orderArray = new USCOrder[
2 26 Feb 07 jari 1260  * numGenes ]; for( int g = 0; g < numGenes; g ++ ) { orderArray[ g ] = new
2 26 Feb 07 jari 1261  * USCOrder( g ); }
2 26 Feb 07 jari 1262  * 
2 26 Feb 07 jari 1263  * //each gene has a centroid (mean) double[] geneCentroids =
2 26 Feb 07 jari 1264  * this.computeGeneCentroids( trainArray, numGenes );
2 26 Feb 07 jari 1265  * 
2 26 Feb 07 jari 1266  * //each class in each gene has a centroid (class mean) [ classes ][ genes ]
2 26 Feb 07 jari 1267  * double[][] classCentroids = this.computeClassCentroids( trainArray,
2 26 Feb 07 jari 1268  * this.fullSet.getUniqueClasses(), numGenes );
2 26 Feb 07 jari 1269  * 
2 26 Feb 07 jari 1270  * //each class has an mk value [ numClasses ] double[] mks = this.computeMks(
2 26 Feb 07 jari 1271  * trainArray, this.fullSet.getUniqueClasses() );
2 26 Feb 07 jari 1272  * 
2 26 Feb 07 jari 1273  * //each gene has an si value [ numGenes ] double[] sis = this.computeSis(
2 26 Feb 07 jari 1274  * trainArray, classCentroids, this.fullSet.getUniqueClasses(), numGenes );
2 26 Feb 07 jari 1275  * 
2 26 Feb 07 jari 1276  * //there is 1 s0 double s0 = this.computeMedian( sis );
2 26 Feb 07 jari 1277  * 
2 26 Feb 07 jari 1278  * //dik is the relative difference of each classCentroid from the geneCentroid
2 26 Feb 07 jari 1279  * (standardized) [ class ][ gene ] double[][] dik =
2 26 Feb 07 jari 1280  * this.computeRelativeDifferences( classCentroids, geneCentroids, mks, sis, s0 );
2 26 Feb 07 jari 1281  * 
2 26 Feb 07 jari 1282  * //now we have all we need to start
2 26 Feb 07 jari 1283  * testing---------------------------------------- double delta = 0.0; for( int
2 26 Feb 07 jari 1284  * d = 0; d < this.numDeltas; d ++ ) { //for( int d = 0; d < 10; d ++ ) { delta =
2 26 Feb 07 jari 1285  * delta + this.deltaStep; //System.out.println( "delta = " + delta );
2 26 Feb 07 jari 1286  * 
2 26 Feb 07 jari 1287  * //shrink the centroids by delta double[][] dikShrunk = this.shrinkDiks(
2 26 Feb 07 jari 1288  * delta, dik );
2 26 Feb 07 jari 1289  * 
2 26 Feb 07 jari 1290  * //figure out which ones to keep using and store their indices in a BitSet
2 26 Feb 07 jari 1291  * //Beta is the greatest positive dikShrunk value BitSet bsUse =
2 26 Feb 07 jari 1292  * this.findRelavantGenes( dikShrunk ); int kount = 0; for( int g = 0; g <
2 26 Feb 07 jari 1293  * numGenes; g ++ ) { if( bsUse.get( g ) == true ) { kount ++; } }
2 26 Feb 07 jari 1294  * 
2 26 Feb 07 jari 1295  * //sort the genes based on greatest Beta value USCOrder[] significantOrder =
2 26 Feb 07 jari 1296  * new USCOrder[ kount ]; int sigKount = 0; for( int g = 0; g < numGenes; g ++ ) {
2 26 Feb 07 jari 1297  * 
2 26 Feb 07 jari 1298  * orderArray[ g ].setSignificant( bsUse.get( g ) );
2 26 Feb 07 jari 1299  * 
2 26 Feb 07 jari 1300  * double maxDik = 0; for( int c = 0; c < numClasses; c ++ ) { if( dikShrunk[ c ][
2 26 Feb 07 jari 1301  * g ] > maxDik ) { maxDik = dikShrunk[ c ][ g ]; } }//end c (classes)
2 26 Feb 07 jari 1302  * 
2 26 Feb 07 jari 1303  * orderArray[ g ].setBeta( maxDik ); //System.out.println( "maxDik = " + maxDik );
2 26 Feb 07 jari 1304  * 
2 26 Feb 07 jari 1305  * if( bsUse.get( g ) == true ) { significantOrder[ sigKount ] = orderArray[ g ];
2 26 Feb 07 jari 1306  * //System.out.println( significantOrder[ sigKount ].getBeta() ); sigKount ++; }
2 26 Feb 07 jari 1307  * }//end g (genes)
2 26 Feb 07 jari 1308  * 
2 26 Feb 07 jari 1309  * //compute shrunken class centroids (do for all, deal with removed genes
2 26 Feb 07 jari 1310  * later) double[][] shrunkenCentroids = this.computeShrunkenClassCentroid(
2 26 Feb 07 jari 1311  * geneCentroids, mks, sis, s0, dikShrunk );
2 26 Feb 07 jari 1312  * 
2 26 Feb 07 jari 1313  * //sort based on Beta, and set the iSorted variable with new index
2 26 Feb 07 jari 1314  * Arrays.sort( significantOrder, new USCOrderComparator() ); for( int g = 0; g <
2 26 Feb 07 jari 1315  * significantOrder.length; g ++ ) { significantOrder[ g ].setISorted( g ); }
2 26 Feb 07 jari 1316  * 
2 26 Feb 07 jari 1317  * //try uncorrelation for rho = .5 - 1.0 for( double r = 0.5; r <= 1.0; r = r +
2 26 Feb 07 jari 1318  * .1 ) { //System.out.println( "delta = " + delta + "\trho = " + r ); //do
2 26 Feb 07 jari 1319  * pairwise comparison to find correlated Genes for( int g = 0; g < (
2 26 Feb 07 jari 1320  * significantOrder.length - 1 ); g ++ ) { double correlation =
2 26 Feb 07 jari 1321  * this.computeCorrelation( trainArray, geneCentroids, significantOrder[ g
2 26 Feb 07 jari 1322  * ].getIOriginal(), significantOrder[ g + 1 ].getIOriginal() ); if( correlation <
2 26 Feb 07 jari 1323  * r ) { significantOrder[ g ].setCorrelated( true ); } //System.out.println(
2 26 Feb 07 jari 1324  * "correlation ( " + g + ", " + ( g + 1 ) + " ) = " + correlation ); }//end g
2 26 Feb 07 jari 1325  * (genes)
2 26 Feb 07 jari 1326  * 
2 26 Feb 07 jari 1327  * //compute discriminant score double[][] discScores =
2 26 Feb 07 jari 1328  * this.computeDiscriminantScore( testArray, shrunkenCentroids,
2 26 Feb 07 jari 1329  * significantOrder, sis, s0, this.fullSet.getUniqueClasses() );
2 26 Feb 07 jari 1330  * 
2 26 Feb 07 jari 1331  * //assign test hybs to classes for( int h = 0; h < testArray.length; h ++ ) {
2 26 Feb 07 jari 1332  * double min = 999999999; int iAssign = 0;
2 26 Feb 07 jari 1333  * 
2 26 Feb 07 jari 1334  * for( int c = 0; c < numClasses; c ++ ) { if( discScores[ c ][ h ] < min ) {
2 26 Feb 07 jari 1335  * min = discScores[ c ][ h ]; iAssign = c; } }
2 26 Feb 07 jari 1336  * 
2 26 Feb 07 jari 1337  * System.out.println( testArray[ h ].getHybName() + " is in class " + iAssign +
2 26 Feb 07 jari 1338  * "\twhen delta = " + delta + "\trho = " + r ); } }//end r (rho) }//end d
2 26 Feb 07 jari 1339  * (deltas)
2 26 Feb 07 jari 1340  */
2 26 Feb 07 jari 1341
2 26 Feb 07 jari 1342 /*
2 26 Feb 07 jari 1343  * USCHyb[] testArray; USCHyb[] trainArray;
2 26 Feb 07 jari 1344  * 
2 26 Feb 07 jari 1345  * //separate the loaded file into a training set and a test set if( i == (
2 26 Feb 07 jari 1346  * this.numFold - 1 ) ) { //last fold, test remaining hybs } else { //test
2 26 Feb 07 jari 1347  * hybPerFold hybs }//
2 26 Feb 07 jari 1348  * 
2 26 Feb 07 jari 1349  * if( i == ( this.numFold - 1 ) ) { //last run, do remaining test hybs
2 26 Feb 07 jari 1350  * testArray = new USCHyb[ hybPerFold + hybRemainder ]; for( int j = 0; j <
2 26 Feb 07 jari 1351  * testArray.length; j ++ ) { testArray[ j ] = this.fullSet.getHyb(
2 26 Feb 07 jari 1352  * permutation.getIndex( ( i * hybPerFold ) + j ) ); System.out.println(
2 26 Feb 07 jari 1353  * testArray[ j ].getHybName() + " added to testArray" ); } //get the training
2 26 Feb 07 jari 1354  * hybs for this fold trainArray = this.getOtherHybs( this.fullSet, testArray ); }
2 26 Feb 07 jari 1355  * else { //get the test hybs for this fold testArray = new USCHyb[ hybPerFold ];
2 26 Feb 07 jari 1356  * for( int j = 0; j < testArray.length; j ++ ) { testArray[ j ] =
2 26 Feb 07 jari 1357  * this.fullSet.getHyb( permutation.getIndex( ( i * hybPerFold ) + j ) );
2 26 Feb 07 jari 1358  * System.out.println( testArray[ j ].getHybName() + " added to testArray" ); }
2 26 Feb 07 jari 1359  * //get the training hybs for this fold trainArray = this.getOtherHybs(
2 26 Feb 07 jari 1360  * this.fullSet, testArray ); }//end else
2 26 Feb 07 jari 1361  * 
2 26 Feb 07 jari 1362  * USCHybSet testSet = new USCHybSet( testArray, USCHybSet.SUB_TEST ); USCHybSet
2 26 Feb 07 jari 1363  * trainSet = new USCHybSet( trainArray, USCHybSet.SUB_TRAINING );
2 26 Feb 07 jari 1364  */
2 26 Feb 07 jari 1365 /*
2 26 Feb 07 jari 1366  * Randomnly permutes a set of Hybs, returning 'fold' number of new USCHybSets
2 26 Feb 07 jari 1367  * <br> USCHybSet[ i ][ j ] is a 2 D array where i = Fold, j = 2. For each fold,
2 26 Feb 07 jari 1368  * 2 subsets of the USCHybSet are created. The first (j=0) is the Subset of hybs
2 26 Feb 07 jari 1369  * to be used to train the algorithm. The 2nd (j=1) is the Subset of hybs to be
2 26 Feb 07 jari 1370  * tested through Cross Validation. @param hybs The complete hyb set read from
2 26 Feb 07 jari 1371  * the training file @param fold The # of times to run Cross Validation @return
2 26 Feb 07 jari 1372  * USCHybSet[ fold ][ 2 ] where j = 0 element is Training Subset and the j = 1
2 26 Feb 07 jari 1373  * element is the Test Subset
2 26 Feb 07 jari 1374  */
2 26 Feb 07 jari 1375 /*
2 26 Feb 07 jari 1376  * private USCHybSet[][] permuteHybs( USCHybSet hybs, int fold ) { Vector
2 26 Feb 07 jari 1377  * vTested = new Vector();
2 26 Feb 07 jari 1378  * 
2 26 Feb 07 jari 1379  * USCHybSet[][] toReturn = new USCHybSet[ fold ][ 2 ];
2 26 Feb 07 jari 1380  * 
2 26 Feb 07 jari 1381  * int hybPerFold = hybs.getHybKount() / fold; int hybRemainder =
2 26 Feb 07 jari 1382  * hybs.getHybKount() % fold;
2 26 Feb 07 jari 1383  * 
2 26 Feb 07 jari 1384  * for( int i = 0; i < fold; i ++ ) { System.out.println( "Fold:" + i );
2 26 Feb 07 jari 1385  * 
2 26 Feb 07 jari 1386  * USCHyb[] trainArray = null; USCHyb[] testArray = null; int trainIndex = 0;
2 26 Feb 07 jari 1387  * int testIndex = 0;
2 26 Feb 07 jari 1388  * 
2 26 Feb 07 jari 1389  * //the last test set may need to have extra hybs so that all hybs are tested
2 26 Feb 07 jari 1390  * if( i == ( fold - 1 ) ) { //IMPORTANT testSetIndices are 1-based!!! int[]
2 26 Feb 07 jari 1391  * testSetIndices = this.getTestSetIndices( vTested, ( hybPerFold + hybRemainder ),
2 26 Feb 07 jari 1392  * hybs.getHybKount() );
2 26 Feb 07 jari 1393  * 
2 26 Feb 07 jari 1394  * testArray = new USCHyb[ ( hybPerFold + hybRemainder ) ]; trainArray = new
2 26 Feb 07 jari 1395  * USCHyb[ hybs.getHybKount() - ( hybPerFold + hybRemainder ) ];
2 26 Feb 07 jari 1396  * 
2 26 Feb 07 jari 1397  * for( int j = 0; j < testSetIndices.length; j ++ ) { testArray[ j ] =
2 26 Feb 07 jari 1398  * hybs.getHyb( testSetIndices[ j ] - 1 ); vTested.add( new Integer(
2 26 Feb 07 jari 1399  * testSetIndices[ j ] - 1 ) ); //System.out.println( hybs.getHyb(
2 26 Feb 07 jari 1400  * testSetIndices[ j ] ).getHybName() + " added to testArray" ); }//end i
2 26 Feb 07 jari 1401  * 
2 26 Feb 07 jari 1402  * int iTrain = 0; for( int j = 0; j < hybs.getHybKount(); j ++ ) { boolean
2 26 Feb 07 jari 1403  * isTrain = true;
2 26 Feb 07 jari 1404  * 
2 26 Feb 07 jari 1405  * for( int k = 0; k < testSetIndices.length; k ++ ) { if( j == (
2 26 Feb 07 jari 1406  * testSetIndices[ k ] - 1 ) ) { isTrain = false; break; } }//end k
2 26 Feb 07 jari 1407  * 
2 26 Feb 07 jari 1408  * if( isTrain ) { trainArray[ iTrain ] = hybs.getHyb( j );
2 26 Feb 07 jari 1409  * //System.out.println( hybs.getHyb( j ).getHybName() + " added to trainArray" );
2 26 Feb 07 jari 1410  * iTrain ++; } }//end j } else { //IMPORTANT testSetIndices are 1-based!!!
2 26 Feb 07 jari 1411  * int[] testSetIndices = this.getTestSetIndices( vTested, hybPerFold,
2 26 Feb 07 jari 1412  * hybs.getHybKount() );
2 26 Feb 07 jari 1413  * 
2 26 Feb 07 jari 1414  * testArray = new USCHyb[ hybPerFold ]; trainArray = new USCHyb[
2 26 Feb 07 jari 1415  * hybs.getHybKount() - hybPerFold ];
2 26 Feb 07 jari 1416  * 
2 26 Feb 07 jari 1417  * for( int j = 0; j < testSetIndices.length; j ++ ) { testArray[ j ] =
2 26 Feb 07 jari 1418  * hybs.getHyb( testSetIndices[ j ] - 1 ); vTested.add( new Integer(
2 26 Feb 07 jari 1419  * testSetIndices[ j ] - 1 ) ); //System.out.println( hybs.getHyb(
2 26 Feb 07 jari 1420  * testSetIndices[ j ] ).getHybName() + " added to testArray" ); }//end i
2 26 Feb 07 jari 1421  * 
2 26 Feb 07 jari 1422  * int iTrain = 0;
2 26 Feb 07 jari 1423  * 
2 26 Feb 07 jari 1424  * for( int j = 0; j < hybs.getHybKount(); j ++ ) { boolean isTrain = true;
2 26 Feb 07 jari 1425  * 
2 26 Feb 07 jari 1426  * for( int k = 0; k < testSetIndices.length; k ++ ) { if( j == (
2 26 Feb 07 jari 1427  * testSetIndices[ k ] - 1 ) ) { isTrain = false; break; } }//end k
2 26 Feb 07 jari 1428  * 
2 26 Feb 07 jari 1429  * if( isTrain ) { trainArray[ iTrain ] = hybs.getHyb( j );
2 26 Feb 07 jari 1430  * //System.out.println( hybs.getHyb( j ).getHybName() + " added to trainArray" );
2 26 Feb 07 jari 1431  * iTrain ++; } }//end j
2 26 Feb 07 jari 1432  *  }
2 26 Feb 07 jari 1433  * 
2 26 Feb 07 jari 1434  * toReturn[ i ][ 0 ] = new USCHybSet( testArray, USCHybSet.SUB_TEST );
2 26 Feb 07 jari 1435  * toReturn[ i ][ 1 ] = new USCHybSet( trainArray, USCHybSet.SUB_TRAINING );
2 26 Feb 07 jari 1436  * }//end i
2 26 Feb 07 jari 1437  * 
2 26 Feb 07 jari 1438  * return toReturn; }//end permuteHybs() private int[] getTestSetIndices( Vector
2 26 Feb 07 jari 1439  * vTested, int testKount, int total ) { int[] toReturn = new int[ testKount ];
2 26 Feb 07 jari 1440  * System.out.println( toReturn.length ); Random r = new Random(); int iAdded =
2 26 Feb 07 jari 1441  * 0; int iTry = 0;
2 26 Feb 07 jari 1442  * 
2 26 Feb 07 jari 1443  * while( iAdded < testKount ) { iTry = r.nextInt( total ) + 1;
2 26 Feb 07 jari 1444  * 
2 26 Feb 07 jari 1445  * if( ! this.alreadyExist( iTry, vTested ) ) { if( ! this.alreadyAdded( iTry,
2 26 Feb 07 jari 1446  * toReturn ) ) { //System.out.println( iTry + " wasn't added" );
2 26 Feb 07 jari 1447  * 
2 26 Feb 07 jari 1448  * toReturn[ iAdded ] = iTry; iAdded ++; } } }
2 26 Feb 07 jari 1449  * 
2 26 Feb 07 jari 1450  * return toReturn; }//end getTestSetIndices() private boolean alreadyExist( int
2 26 Feb 07 jari 1451  * iTest, Vector v ) { boolean toReturn = false;
2 26 Feb 07 jari 1452  * 
2 26 Feb 07 jari 1453  * for( int i = 0; i < v.size(); i ++ ) { Integer I = ( Integer ) v.elementAt( i );
2 26 Feb 07 jari 1454  * if( iTest == ( I.intValue() + 1 ) ) { toReturn = true; break; } }//end i
2 26 Feb 07 jari 1455  * 
2 26 Feb 07 jari 1456  * return toReturn; } private boolean alreadyAdded( int iTest, int[] test ) {
2 26 Feb 07 jari 1457  * boolean toReturn = false;
2 26 Feb 07 jari 1458  * 
2 26 Feb 07 jari 1459  * for( int i = 0; i < test.length; i ++ ) { if( iTest == test[ i ] ) {
2 26 Feb 07 jari 1460  * System.out.println( iTest + " = " + test[ i ] ); toReturn = true; break; } }
2 26 Feb 07 jari 1461  * 
2 26 Feb 07 jari 1462  * return toReturn; }
2 26 Feb 07 jari 1463  */
2 26 Feb 07 jari 1464
2 26 Feb 07 jari 1465 /*
2 26 Feb 07 jari 1466  * private Vector generateRandomPermutations( int kount, int iSeed, Vector
2 26 Feb 07 jari 1467  * vTested ) { //first generate a Vector of random numbers of kount size Vector
2 26 Feb 07 jari 1468  * vReturn = new Vector();
2 26 Feb 07 jari 1469  * 
2 26 Feb 07 jari 1470  * Long L = new Long( iSeed ); Random r = new Random( L.longValue() );
2 26 Feb 07 jari 1471  * 
2 26 Feb 07 jari 1472  * for( int i = 0; i < kount; i ++ ) { boolean isAdded = false;
2 26 Feb 07 jari 1473  * 
2 26 Feb 07 jari 1474  * while( ! isAdded ) { Integer IAdd = new Integer( r.nextInt( kount ) );
2 26 Feb 07 jari 1475  * 
2 26 Feb 07 jari 1476  * if( isNovel( IAdd.intValue(), vReturn ) ) { vReturn.add( IAdd ); isAdded =
2 26 Feb 07 jari 1477  * true; //System.out.println( IAdd + " added" ); } } }//end i
2 26 Feb 07 jari 1478  * 
2 26 Feb 07 jari 1479  * //System.out.println( vReturn.size() + " hybs randomnly permuted\r\n" );
2 26 Feb 07 jari 1480  * 
2 26 Feb 07 jari 1481  * return vReturn; }//end generateRandomPermutations() private boolean isNovel(
2 26 Feb 07 jari 1482  * int testInt, Vector vInt ) { boolean toReturn = true;
2 26 Feb 07 jari 1483  * 
2 26 Feb 07 jari 1484  * for( int i = 0; i < vInt.size(); i ++ ) { Integer I = ( Integer )
2 26 Feb 07 jari 1485  * vInt.elementAt( i ); if( I.intValue() == testInt ) { toReturn = false; break; } }
2 26 Feb 07 jari 1486  * 
2 26 Feb 07 jari 1487  * return toReturn; }//end isNovel()
2 26 Feb 07 jari 1488  */
2 26 Feb 07 jari 1489
2 26 Feb 07 jari 1490 /*
2 26 Feb 07 jari 1491  * int[] testSetIndices = this.getTestSetIndices( vTested, hybPerFold,
2 26 Feb 07 jari 1492  * hybs.getHybKount() );
2 26 Feb 07 jari 1493  * 
2 26 Feb 07 jari 1494  * testArray = new USCHyb[ hybPerFold + hybRemainder ]; trainArray = new USCHyb[
2 26 Feb 07 jari 1495  * hybs.getHybKount() - ( hybPerFold + hybRemainder ) ];
2 26 Feb 07 jari 1496  * 
2 26 Feb 07 jari 1497  * //last time, use the rest of the hybs for( int j = 0; j < vPermuted.size(); j ++ ) {
2 26 Feb 07 jari 1498  * Integer I = ( Integer ) vPermuted.elementAt( j ); int iHyb = I.intValue();
2 26 Feb 07 jari 1499  * 
2 26 Feb 07 jari 1500  * //the first hybPerFold hybs will be used as test if( j < hybPerFold +
2 26 Feb 07 jari 1501  * hybRemainder ) { //add to testSet testArray[ testIndex ] = hybs.getHyb( iHyb );
2 26 Feb 07 jari 1502  * vTested.add( I ); //System.out.println( testArray[ testIndex ].getHybName() + "
2 26 Feb 07 jari 1503  * added to testArray[ " + testIndex + " ]" ); testIndex ++; } else { //add to
2 26 Feb 07 jari 1504  * trainSet trainArray[ trainIndex ] = hybs.getHyb( iHyb );
2 26 Feb 07 jari 1505  * //System.out.println( trainArray[ trainIndex ].getHybName() + " added to
2 26 Feb 07 jari 1506  * trainArray[ " + trainIndex + " ]" ); trainIndex ++; } }//end j
2 26 Feb 07 jari 1507  */
2 26 Feb 07 jari 1508 /*
2 26 Feb 07 jari 1509  * for( int j = 0; j < vPermuted.size(); j ++ ) { Integer I = ( Integer )
2 26 Feb 07 jari 1510  * vPermuted.elementAt( j ); int iHyb = I.intValue();
2 26 Feb 07 jari 1511  * 
2 26 Feb 07 jari 1512  * if( j < hybPerFold ) { //add to testSet testArray[ testIndex ] = hybs.getHyb(
2 26 Feb 07 jari 1513  * iHyb ); vTested.add( I ); //System.out.println( testArray[ testIndex
2 26 Feb 07 jari 1514  * ].getHybName() + " added to testArray[ " + testIndex + " ]" ); testIndex ++; }
2 26 Feb 07 jari 1515  * else { //add to trainSet trainArray[ trainIndex ] = hybs.getHyb( iHyb );
2 26 Feb 07 jari 1516  * //System.out.println( trainArray[ trainIndex ].getHybName() + " added to
2 26 Feb 07 jari 1517  * trainArray[ " + trainIndex + " ]" ); trainIndex ++; } }//end j
2 26 Feb 07 jari 1518  */
2 26 Feb 07 jari 1519
2 26 Feb 07 jari 1520 //--------------------
2 26 Feb 07 jari 1521
2 26 Feb 07 jari 1522 /*
2 26 Feb 07 jari 1523  * //significantOrder is a subset of relevantOrder, having tossed out irrelevant
2 26 Feb 07 jari 1524  * genes USCOrder[] significantOrder = new USCOrder[ numRelevant ]; int
2 26 Feb 07 jari 1525  * numSignificant = 0; for( int g = 0; g < numGenes; g ++ ) { double maxDik = 0;
2 26 Feb 07 jari 1526  * for( int c = 0; c < numClasses; c ++ ) { if( dikShrunk[ c ][ g ] > maxDik ) {
2 26 Feb 07 jari 1527  * maxDik = dikShrunk[ c ][ g ]; } }//end c (classes)
2 26 Feb 07 jari 1528  * 
2 26 Feb 07 jari 1529  * relevantOrder[ g ].setBeta( maxDik ); //System.out.println( "maxDik = " +
2 26 Feb 07 jari 1530  * maxDik );
2 26 Feb 07 jari 1531  * 
2 26 Feb 07 jari 1532  * if( bsUse.get( g ) == true ) { significantOrder[ numSignificant ] =
2 26 Feb 07 jari 1533  * relevantOrder[ g ]; //System.out.println( significantOrder[ sigKount
2 26 Feb 07 jari 1534  * ].getBeta() ); numSignificant ++; } }//end g (genes)
2 26 Feb 07 jari 1535  * 
2 26 Feb 07 jari 1536  * //sort based on Beta, and set the iSorted variable with new index
2 26 Feb 07 jari 1537  * Arrays.sort( significantOrder, new USCRelevanceComparator() ); for( int g =
2 26 Feb 07 jari 1538  * 0; g < significantOrder.length; g ++ ) { significantOrder[ g ].setIRelevant(
2 26 Feb 07 jari 1539  * g ); }
2 26 Feb 07 jari 1540  * 
2 26 Feb 07 jari 1541  * //do pairwise comparison to find correlated Genes //be careful here to make
2 26 Feb 07 jari 1542  * sure you only compare relevant genes for( int g = 0; g < (
2 26 Feb 07 jari 1543  * significantOrder.length - 1 ); g ++ ) { double correlation =
2 26 Feb 07 jari 1544  * this.computeCorrelation( trainArray, significantOrder[ g ].getIOriginal(),
2 26 Feb 07 jari 1545  * significantOrder[ g + 1 ].getIOriginal() ); if( correlation < rho ) {
2 26 Feb 07 jari 1546  * significantOrder[ g ].setCorrelated( true ); } //System.out.println(
2 26 Feb 07 jari 1547  * "correlation ( " + g + ", " + ( g + 1 ) + " ) = " + correlation ); }//end g
2 26 Feb 07 jari 1548  * (genes)
2 26 Feb 07 jari 1549  * 
2 26 Feb 07 jari 1550  * //compute discriminant score double[][] discScores =
2 26 Feb 07 jari 1551  * this.computeDiscriminantScores( testArray, shrunkenCentroids,
2 26 Feb 07 jari 1552  * significantOrder, sis, s0, this.fullSet.getUniqueClasses() );
2 26 Feb 07 jari 1553  * 
2 26 Feb 07 jari 1554  * //assign test hybs to classes and store in USCClassAssignment object for( int
2 26 Feb 07 jari 1555  * h = 0; h < testArray.length; h ++ ) { double min = 999999999; int iAssign =
2 26 Feb 07 jari 1556  * 0;
2 26 Feb 07 jari 1557  * 
2 26 Feb 07 jari 1558  * double[] classDiscScoreArray = new double[ numClasses ]; for( int c = 0; c <
2 26 Feb 07 jari 1559  * numClasses; c ++ ) { classDiscScoreArray[ c ] = discScores[ c ][ h ];
2 26 Feb 07 jari 1560  * 
2 26 Feb 07 jari 1561  * if( discScores[ c ][ h ] < min ) { min = discScores[ c ][ h ]; iAssign = c; } }
2 26 Feb 07 jari 1562  * 
2 26 Feb 07 jari 1563  * USCClassAssignment assignment = new USCClassAssignment( min, iAssign,
2 26 Feb 07 jari 1564  * classDiscScoreArray ); assignments[ h ] = assignment;
2 26 Feb 07 jari 1565  * 
2 26 Feb 07 jari 1566  * //System.out.println( testArray[ h ].getHybName() + " is in class " + iAssign );
2 26 Feb 07 jari 1567  * }//end h (testHybs) private int computeCombinations( int n, int c ) { int
2 26 Feb 07 jari 1568  * toReturn = 0;
2 26 Feb 07 jari 1569  * 
2 26 Feb 07 jari 1570  * int nFactorial = n; for( int i = n - 1; i > 0; i -- ) { nFactorial =
2 26 Feb 07 jari 1571  * nFactorial * i; }
2 26 Feb 07 jari 1572  * 
2 26 Feb 07 jari 1573  * int cFactorial = c; for( int i = ( c - 1 ); i > 0; i -- ) { cFactorial =
2 26 Feb 07 jari 1574  * cFactorial * i; }
2 26 Feb 07 jari 1575  * 
2 26 Feb 07 jari 1576  * int diff = ( n - c ); int diffFactorial = diff; for( int i = ( diff - 1 ); i >
2 26 Feb 07 jari 1577  * 0; i -- ) { diffFactorial = diffFactorial * i; }
2 26 Feb 07 jari 1578  * 
2 26 Feb 07 jari 1579  * toReturn = ( nFactorial / ( cFactorial * diffFactorial ) );
2 26 Feb 07 jari 1580  * System.out.println( toReturn + " combinations of " + n );
2 26 Feb 07 jari 1581  * 
2 26 Feb 07 jari 1582  * return toReturn; }//computeCombinations
2 26 Feb 07 jari 1583  */
2 26 Feb 07 jari 1584
2 26 Feb 07 jari 1585 /*
2 26 Feb 07 jari 1586 public USCXValResult crossValidate(USCHybSet fullSet, Frame frame) {
2 26 Feb 07 jari 1587     frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
2 26 Feb 07 jari 1588
2 26 Feb 07 jari 1589     USCXValResult toReturn = new USCXValResult(this.xValKount);
2 26 Feb 07 jari 1590
2 26 Feb 07 jari 1591     //display a progress bar
2 26 Feb 07 jari 1592     JPanel mainPanel = new JPanel();
2 26 Feb 07 jari 1593     mainPanel.setLayout(new SpringLayout());
2 26 Feb 07 jari 1594     JPanel leftPanel = new JPanel();
2 26 Feb 07 jari 1595     leftPanel.add(new JLabel("     "));
2 26 Feb 07 jari 1596     JPanel rightPanel = new JPanel();
2 26 Feb 07 jari 1597     rightPanel.add(new JLabel("     "));
2 26 Feb 07 jari 1598
2 26 Feb 07 jari 1599     JPanel midPanel = new JPanel();
2 26 Feb 07 jari 1600     BoxLayout midBox = new BoxLayout(midPanel, BoxLayout.Y_AXIS);
2 26 Feb 07 jari 1601     midPanel.setLayout(midBox);
2 26 Feb 07 jari 1602     JLabel label = new JLabel("Cross Validating... Please Wait");
2 26 Feb 07 jari 1603     JLabel label2 = new JLabel("This will take a few minutes");
2 26 Feb 07 jari 1604     JLabel foldLabel = new JLabel("Fold/CrossVal runs");
2 26 Feb 07 jari 1605     JLabel deltaLabel = new JLabel("Deltas");
2 26 Feb 07 jari 1606     JLabel corrLabel = new JLabel("Pairwise Genes");
2 26 Feb 07 jari 1607     JLabel blankLabel = new JLabel(" ");
2 26 Feb 07 jari 1608     midPanel.add(label);
2 26 Feb 07 jari 1609     midPanel.add(label2);
2 26 Feb 07 jari 1610     midPanel.add(blankLabel);
2 26 Feb 07 jari 1611     JProgressBar foldBar = new JProgressBar(0, ( this.foldKount * this.xValKount ));
2 26 Feb 07 jari 1612     foldBar.setIndeterminate(false);
2 26 Feb 07 jari 1613     foldBar.setStringPainted(true);
2 26 Feb 07 jari 1614     JProgressBar deltaBar = new JProgressBar(0, this.deltaKount);
2 26 Feb 07 jari 1615     deltaBar.setIndeterminate(false);
2 26 Feb 07 jari 1616     deltaBar.setStringPainted(true);
2 26 Feb 07 jari 1617     JProgressBar corrBar = new JProgressBar(0, fullSet.getNumGenes());
2 26 Feb 07 jari 1618     corrBar.setIndeterminate(false);
2 26 Feb 07 jari 1619     corrBar.setStringPainted(true);
2 26 Feb 07 jari 1620     midPanel.add(foldLabel);
2 26 Feb 07 jari 1621     midPanel.add(foldBar);
2 26 Feb 07 jari 1622     midPanel.add(deltaLabel);
2 26 Feb 07 jari 1623     midPanel.add(deltaBar);
2 26 Feb 07 jari 1624     midPanel.add(corrLabel);
2 26 Feb 07 jari 1625     midPanel.add(corrBar);
2 26 Feb 07 jari 1626
2 26 Feb 07 jari 1627     mainPanel.add(leftPanel);
2 26 Feb 07 jari 1628     mainPanel.add(midPanel);
2 26 Feb 07 jari 1629     mainPanel.add(rightPanel);
2 26 Feb 07 jari 1630     SpringUtilities.makeCompactGrid(mainPanel, 1, 3, 0, 0, 0, 0);
2 26 Feb 07 jari 1631     JFrame jf = new JFrame("Cross Validating");
2 26 Feb 07 jari 1632     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2 26 Feb 07 jari 1633     jf.getContentPane().add(mainPanel);
2 26 Feb 07 jari 1634     jf.setSize(250, 200);
2 26 Feb 07 jari 1635     jf.show();
2 26 Feb 07 jari 1636     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
2 26 Feb 07 jari 1637     jf.setLocation(( screenSize.width - 200 ) / 2,
2 26 Feb 07 jari 1638             ( screenSize.height - 100 ) / 2);
2 26 Feb 07 jari 1639     int iProgress = 0;
2 26 Feb 07 jari 1640
2 26 Feb 07 jari 1641     //do multiple cross validations (added 1.30.05)
2 26 Feb 07 jari 1642     for( int m = 0; m < this.xValKount; m++ ) {
2 26 Feb 07 jari 1643         USCFoldResult[] resultArray = new USCFoldResult[this.foldKount];
2 26 Feb 07 jari 1644
2 26 Feb 07 jari 1645         //loop through the folds, during each fold, a different set of Hybs
2 26 Feb 07 jari 1646         // are left out
2 26 Feb 07 jari 1647         for( int f = 0; f < this.foldKount; f++ ) {
2 26 Feb 07 jari 1648             deltaBar.setValue(0);
2 26 Feb 07 jari 1649
2 26 Feb 07 jari 1650             //System.out.println(
2 26 Feb 07 jari 1651             // "\r\n\r\n\r\n------------------------------------------------"
2 26 Feb 07 jari 1652             // +
2 26 Feb 07 jari 1653             //"------------------------------------------------------------------------FOLD:"
2 26 Feb 07 jari 1654             // + f );
2 26 Feb 07 jari 1655
2 26 Feb 07 jari 1656             //create a USCFoldResult that will hold the results from this
2 26 Feb 07 jari 1657             // fold
2 26 Feb 07 jari 1658             int resultKount = ( this.deltaKount * 6 );
2 26 Feb 07 jari 1659             USCFoldResult foldResults = new USCFoldResult(resultKount);
2 26 Feb 07 jari 1660             int iResult = 0;
2 26 Feb 07 jari 1661
2 26 Feb 07 jari 1662             //arrays of USCHyb objects
2 26 Feb 07 jari 1663             USCHyb[] subTestArray = fullSet.getTestArray(f);
2 26 Feb 07 jari 1664             USCHyb[] subTrainArray = fullSet.getTrainArray(f);
2 26 Feb 07 jari 1665
2 26 Feb 07 jari 1666             //loop through the deltas
2 26 Feb 07 jari 1667             double delta = 0.0f;
2 26 Feb 07 jari 1668             for( int d = 0; d < this.deltaKount; d++ ) {
2 26 Feb 07 jari 1669                 //loop through the rhos
2 26 Feb 07 jari 1670                 double rho;
2 26 Feb 07 jari 1671                 for( int r = 5; r < 11; r++ ) {
2 26 Feb 07 jari 1672                     rho = ( double ) r * 0.1f;
2 26 Feb 07 jari 1673
2 26 Feb 07 jari 1674                     USCResult result = this.testTest(subTrainArray,
2 26 Feb 07 jari 1675                             subTestArray, delta, rho, fullSet.getNumGenes(),
2 26 Feb 07 jari 1676                             fullSet.getNumClasses(), fullSet
2 26 Feb 07 jari 1677                                     .getUniqueClasses(), corrBar, r);
2 26 Feb 07 jari 1678                    
2 26 Feb 07 jari 1679                     if( result == null ) {
2 26 Feb 07 jari 1680                         //do nothing
2 26 Feb 07 jari 1681                     } else {
2 26 Feb 07 jari 1682                         //store it
2 26 Feb 07 jari 1683                         foldResults.setResult(result, iResult);
2 26 Feb 07 jari 1684                         foldResults.setTestArray(subTestArray);
2 26 Feb 07 jari 1685                         foldResults.setUniqueClassArray(fullSet
2 26 Feb 07 jari 1686                                 .getUniqueClasses());
2 26 Feb 07 jari 1687                         iResult++;
2 26 Feb 07 jari 1688                     }
2 26 Feb 07 jari 1689                 }//end r (rho)
2 26 Feb 07 jari 1690
2 26 Feb 07 jari 1691                 delta += this.deltaStep;
2 26 Feb 07 jari 1692                 deltaBar.setValue(d + 1);
2 26 Feb 07 jari 1693             }//end d (deltas)
2 26 Feb 07 jari 1694
2 26 Feb 07 jari 1695             //toReturn[ f ] = foldResults;
2 26 Feb 07 jari 1696             resultArray[f] = foldResults;
2 26 Feb 07 jari 1697
2 26 Feb 07 jari 1698             //update progress bar
2 26 Feb 07 jari 1699             foldBar.setIndeterminate(false);
2 26 Feb 07 jari 1700             iProgress++;
2 26 Feb 07 jari 1701             foldBar.setValue(iProgress);
2 26 Feb 07 jari 1702             foldBar.setStringPainted(true);
2 26 Feb 07 jari 1703         }//end f (folds)
2 26 Feb 07 jari 1704
2 26 Feb 07 jari 1705         toReturn.setFoldResult(resultArray, m);
2 26 Feb 07 jari 1706     }//end m (xValKount)
2 26 Feb 07 jari 1707
2 26 Feb 07 jari 1708     //System.out.println( "Done With Cross Validation" );
2 26 Feb 07 jari 1709
2 26 Feb 07 jari 1710     jf.dispose();
2 26 Feb 07 jari 1711     frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
2 26 Feb 07 jari 1712
2 26 Feb 07 jari 1713     return toReturn;
2 26 Feb 07 jari 1714 }//end crossValidate
2 26 Feb 07 jari 1715 */