Author: shirleyh
Date: 2008-09-23 12:08:11 -0700 (Tue, 23 Sep 2008)
New Revision: 14944

Added:
   
csplugins/trunk/mskcc/summerstudents/ExpressionCorrelation/src/org/mskcc/csplugins/ExpressionCorrelation/CorrelateTask.java
   
csplugins/trunk/mskcc/summerstudents/ExpressionCorrelation/src/org/mskcc/csplugins/ExpressionCorrelation/SpearmanRank.java
Log:
New files for Expression Correlation Plugin to work with Cytoscape 2.6.0

Added: 
csplugins/trunk/mskcc/summerstudents/ExpressionCorrelation/src/org/mskcc/csplugins/ExpressionCorrelation/CorrelateTask.java
===================================================================
--- 
csplugins/trunk/mskcc/summerstudents/ExpressionCorrelation/src/org/mskcc/csplugins/ExpressionCorrelation/CorrelateTask.java
 2008-09-23 19:07:05 UTC (rev 14943)
+++ 
csplugins/trunk/mskcc/summerstudents/ExpressionCorrelation/src/org/mskcc/csplugins/ExpressionCorrelation/CorrelateTask.java
 2008-09-23 19:08:11 UTC (rev 14944)
@@ -0,0 +1,215 @@
+package org.mskcc.csplugins.ExpressionCorrelation;
+
+import cytoscape.task.Task;
+import cytoscape.task.TaskMonitor;
+import cytoscape.Cytoscape;
+import cytoscape.CyNetwork;
+
+/**
+ * Copyright (c) 2007
+ * *
+ * * Code written by: Shirley Hui
+ * * Authors: Gary Bader, Elena Potylitsine, Chris Sander, Weston Whitaker
+ * *
+ * * This library is free software; you can redistribute it and/or modify it
+ * * under the terms of the GNU Lesser General Public License as published
+ * * by the Free Software Foundation; either version 2.1 of the License, or
+ * * any later version.
+ * *
+ * * This library is distributed in the hope that it will be useful, but
+ * * WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+ * * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+ * * documentation provided hereunder is on an "as is" basis, and
+ * * Memorial Sloan-Kettering Cancer Center
+ * * has no obligations to provide maintenance, support,
+ * * updates, enhancements or modifications.  In no event shall the
+ * * Memorial Sloan-Kettering Cancer Center
+ * * be liable to any party for direct, indirect, special,
+ * * incidental or consequential damages, including lost profits, arising
+ * * out of the use of this software and its documentation, even if
+ * * Memorial Sloan-Kettering Cancer Center
+ * * has been advised of the possibility of such damage.  See
+ * * the GNU Lesser General Public License for more details.
+ * *
+ * * You should have received a copy of the GNU Lesser General Public License
+ * * along with this library; if not, write to the Free Software Foundation,
+ * * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ * *
+ * * User: Shirley Hui
+ * * Date: Apr 4, 2007
+ * * Time: 3:35:11 April 7, 2004
+ * * Description: CorrelateTask
+ * *
+ * *
+ */
+
+
+/**
+ * This class represents a task that calls methods to calculate a correlation 
network and/or corresponding histogram
+ */
+public class CorrelateTask implements Task {
+
+    private CorrelateSimilarityNetwork network;
+    private TaskMonitor taskMonitor = null;
+    private CorrelateHistogramWindow histogram = null;
+    
+    boolean colDone = false;         // True when the column network is 
complete
+
+    /**
+     * Sets which event thread to run and time
+     * case 1: Run and time both the column and row networks
+     * case 2: Run and time the column network
+     * case 3: Run and time the column histogram
+     * case 4: Run and time the row network
+     * case 5: Run and time the row histogram
+     */
+    public int event = 0;
+
+    /**
+     * Constructor.
+     *
+     * @param event Indicates which event to run.
+     * @param network The network upon which to calculate correlations
+     */
+    public CorrelateTask(int event, CorrelateSimilarityNetwork network) {
+
+        this.network = network;
+        this.event = event;
+    }
+
+    /**
+     * Perform the correlation calculations on the network.
+     */
+    public void run() {
+        if (network.getTaskMonitor() == null) {
+            throw new IllegalStateException("Task Monitor is not set.");
+        }
+        
+        switch (event) {
+            case 1:
+                colRun();
+                rowRun();
+                break;
+            case 2:
+                colRun();
+                break;
+            case 3:
+                colHistogram();
+                break;
+            case 4:
+                rowRun();
+                break;
+            case 5:
+                rowHistogram();
+                break;
+        }
+    }
+
+
+    private void cleanup(String networkId)
+    {
+        System.out.println("Removing network with id: " + networkId);
+        if (networkId != null)
+        {
+            CyNetwork destroyNetwork = Cytoscape.getNetwork(networkId);
+            Cytoscape.destroyNetwork( destroyNetwork );
+        }
+
+        System.out.println("Finished destroying Network");
+
+    }
+
+    /**
+        * Halts the Task: Not Currently Implemented.
+        */
+       public void halt() {
+               // Task can not currently be halted.
+        network.cancel();
+        System.out.println("User cancelled task.");
+        if (histogram!=null)
+        {
+            System.out.println("Disposing histogram dialog");
+            histogram.dispose();
+            histogram.setVisible(false);
+        }
+    }
+
+    /**
+     * Sets the Task Monitor.
+     *
+     * @param taskMonitor TaskMonitor Object.
+     */
+    public void setTaskMonitor(TaskMonitor taskMonitor) {
+        if (this.taskMonitor != null) {
+            throw new IllegalStateException("Task Monitor is already set.");
+        }
+        network.setTaskMonitor(taskMonitor);
+    }
+
+    /**
+     * Gets the Task Title.
+     *
+     * @return human readable task title.
+     */
+    public String getTitle() {
+        return new String("Performing correlation calculations");
+    }
+
+    /**
+     * The condition matrix calculation
+     */
+    public void colRun() {
+        if (Cytoscape.getExpressionData() != null) {
+            System.out.println("Starting colRun");
+            
+            CyNetwork cyNetwork = network.calcCols();
+            String conditionNetworkId = cyNetwork.getIdentifier();
+            if (network.cancelled())
+            {
+                cleanup(conditionNetworkId);
+            }
+            System.out.println("Finished colRun");
+        }
+    }
+
+    /**
+     * The gene matrix calculation
+     */
+    public void rowRun() {
+
+        if (Cytoscape.getExpressionData() != null) {
+            System.out.println("Starting rowRun");
+            CyNetwork cyNetwork = network.calcRows();
+            String geneNetworkId = cyNetwork.getIdentifier();
+            if (network.cancelled())
+            {
+                cleanup(geneNetworkId);
+            }
+            System.out.println("Finished rowRun");
+        }
+    }
+
+    /**
+     * The condition histogram calculation
+     */
+    public void colHistogram() {
+        if (Cytoscape.getExpressionData() != null) {
+            System.out.println("Starting colHistogram");
+            histogram = new CorrelateHistogramWindow(Cytoscape.getDesktop(), 
false, network); //not row histogram
+            histogram.pack();
+            histogram.setVisible(true);
+        }
+    }
+
+    /**
+     * The gene histogram calculation
+     */
+    public void rowHistogram() {
+        if (Cytoscape.getExpressionData() != null) {
+            System.out.println("Starting rowHistogram");
+            histogram = new CorrelateHistogramWindow(Cytoscape.getDesktop(), 
true, network); //row histogram
+            histogram.pack();
+            histogram.setVisible(true);
+        }
+    }
+}
\ No newline at end of file

Added: 
csplugins/trunk/mskcc/summerstudents/ExpressionCorrelation/src/org/mskcc/csplugins/ExpressionCorrelation/SpearmanRank.java
===================================================================
--- 
csplugins/trunk/mskcc/summerstudents/ExpressionCorrelation/src/org/mskcc/csplugins/ExpressionCorrelation/SpearmanRank.java
  2008-09-23 19:07:05 UTC (rev 14943)
+++ 
csplugins/trunk/mskcc/summerstudents/ExpressionCorrelation/src/org/mskcc/csplugins/ExpressionCorrelation/SpearmanRank.java
  2008-09-23 19:08:11 UTC (rev 14944)
@@ -0,0 +1,201 @@
+//
+//  SpearmanRank.java
+//  
+//
+//  Created by Shirley Hui on 08/05/07.
+//  Copyright 2007 __MyCompanyName__. All rights reserved.
+//
+package org.mskcc.csplugins.ExpressionCorrelation;
+
+import cern.colt.matrix.*;
+import cern.colt.matrix.linalg.Algebra;
+import cern.colt.matrix.impl.*;
+import cern.colt.matrix.doublealgo.*;
+import java.util.*;
+import cern.jet.math.Functions;
+import cern.colt.matrix.doublealgo.Statistic;
+
+public class SpearmanRank {
+
+       private List rankInfoList;
+    private DoubleMatrix2D distanceMatrix;
+
+    public SpearmanRank(DoubleMatrix2D data)
+       {
+        System.out.println("Initializing Spearman Rank...");
+        init(data);
+       }
+       
+       // Perform quicksort on 2 d matrix v1, but sort wrt to first column
+       private DoubleMatrix2D sort(DoubleMatrix2D v1)
+       {
+               DoubleMatrix2D view = Sorting.quickSort.sort(v1,0);
+               return view;
+       }
+
+       private RankInfo rank(double[] x)
+       {
+               int j = 0;
+               int ji;
+               int jt;
+               double t;
+               int n = x.length;
+               double r;
+               double s = 0.0;
+               double[] w = new double[n];
+               
+               for (int i = 0;i < n;i++)
+               {
+                       w[i] = x[i];
+               }
+               
+               while (j < n)
+               {
+                       if (j == n-1)
+                               break;
+                       // not a tie
+                       if (w[j+1] != w[j] && j < n)
+                       {
+                               w[j] = j;
+                               j = j + 1;
+                       } else
+                       {
+                               for (jt = j+ 1; jt < n && w[jt]==w[j];jt++);
+                               r = 0.5*(j+jt-1);
+                               for (ji=j;ji <=(jt-1);ji++) w[ji] = r;
+                               t = jt-j;
+                               s = s + t*t*t-1;
+                               j = jt;
+                       }
+               }
+               if (j==n-1) w[n-1] = n-1;
+               
+               /*for (int i = 0; i < w.length;i++)
+               {
+                       System.out.print( w[i] + ", ");
+               }
+               System.out.println();
+               */
+               return new RankInfo(w,s);
+               //return w;
+       }
+       
+       private static DoubleMatrix2D addIndices(DoubleMatrix1D x)
+       {
+               int n = x.size();
+               // Create a 2D matrix.  The first column is v1, the second 
column is [1..v1.length]
+               DenseDoubleMatrix2D v = new DenseDoubleMatrix2D(n,2);
+               
+               for (int i = 0; i < n; i++) 
+               {
+                       for (int j = 0;j < 2;j++)
+                       {
+                               double xValue = x.get(i);
+                               
+                               if (j == 0)
+                               {
+                                       //System.out.print(x.get(i));
+                                       v.setQuick(i, j, xValue);
+                               }
+                               else
+                                       v.setQuick(i,j,i);
+                       }
+               }
+               
+               //DoubleMatrix2D view =Sorting.quickSort.sort(v1,0);
+               //System.out.println(view); 
+               return v;
+       }
+       
+       private void init(DoubleMatrix2D rdata)
+       {
+               int n = rdata.rows();
+               rankInfoList = new ArrayList();
+               for (int i = 0; i < n;i++)
+               {
+                       DoubleMatrix1D x = rdata.viewRow(i);
+                       DoubleMatrix2D v1 = addIndices(x);
+                       // Sort v1 wrt to first column 
+                       DoubleMatrix2D sortedV1 = sort(v1);
+                       // v1 first column are the sorted data values
+                       DoubleMatrix1D sortedColV1 = sortedV1.viewColumn(0);
+                       // v1 second column are the original indices sorted 
according to first column
+                       DoubleMatrix1D sortedIndicesV1 = sortedV1.viewColumn(1);
+                       
+                       double[] vv2 = sortedColV1.toArray();
+                       RankInfo rr1 = rank(vv2);
+                       rr1.setIndices(sortedIndicesV1.toArray());
+                       
+                       rankInfoList.add(rr1);
+               }
+
+        System.out.println("rankInfoList contains: " + rankInfoList.size() + " 
number of RankInfos");
+        Algebra A = new Algebra();
+        distanceMatrix = 
Statistic.distance(A.transpose(rdata),Statistic.EUCLID);
+        System.out.println("The distance matrix is: " + distanceMatrix.rows() 
+ " rows by " + distanceMatrix.columns() + "columns");
+    }
+       
+       /** 
+               * calculate the spearman correlation for two vectors d and d2
+               **/
+       public double corr(int xindex, int yindex)
+       {
+               if (rankInfoList.size()==0)
+                       return -2.0;
+                       
+               RankInfo xrank = (RankInfo) rankInfoList.get(xindex);
+               RankInfo yrank = (RankInfo) rankInfoList.get(yindex);
+               
+               double[] r1 = xrank.ranking;
+               double sf = xrank.s;
+               double[] r1index = xrank.sortedIndices;
+               
+               double[] r2 = yrank.ranking;
+               double sg = yrank.s;
+               double[] r2index = yrank.sortedIndices;
+
+        // compute sum of square diffs
+        //DoubleMatrix1D d1 = new DenseDoubleMatrix1D(r1);
+        //DoubleMatrix1D d2 = new DenseDoubleMatrix1D(r2);
+
+        //double sumOfDiffSquared = d1.aggregate(d2, Functions.plus, 
Functions.chain(Functions.square,Functions.minus));
+
+        int n = r1.length;
+        double d1 = 0.0;
+        /*
+               for (int j = 0; j <n;j++)
+               {
+            double diff = r1[(int)r1index[j]]-r2[(int)r2index[j]];
+            //double diff = 0.0;
+            double diffSquared = diff*diff;
+            d1 = d1 + diffSquared; 
//Math.pow(r1[(int)r1index[j]]-r2[(int)r2index[j]],2);
+               } */
+               double en = (double)n;
+               double en3n = en*en*en-en;
+               /*double fac = (1.0-sf/en3n)*(1.0-sg/en3n);
+               double p = (1.0 - (6.0/en3n)*(d1+(sf+sg)/12.0))/Math.sqrt(fac);
+               */
+        double p = 1.0 - (6.0 * d1)/(en3n-n);
+        return p;
+       }
+       
+       private class RankInfo
+       {
+               double[] ranking;
+               double s = 0.0;
+               double[] sortedIndices;
+               
+               public RankInfo(double[] ranking, double s)
+               {
+                       this.ranking = ranking;
+                       this.s = s;
+               }
+               
+               public void setIndices(double[] sortedIndices)
+               {
+                       this.sortedIndices = sortedIndices;
+               }
+       }
+       
+       
+}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to