Changeset 246


Ignore:
Timestamp:
Mar 13, 2007, 4:20:05 PM (17 years ago)
Author:
Johan Enell
Message:

RankProduct? calculates the rp correct

Location:
trunk/se/lu/onk/OneClass/src/oneclass/rankproduct
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/se/lu/onk/OneClass/src/oneclass/rankproduct/RankProduct.java

    r244 r246  
    2424package oneclass.rankproduct;
    2525
    26 import java.util.Arrays;
    2726import java.util.Comparator;
    2827
     
    3130public class RankProduct
    3231{
    33   public void calculate(BASEFileSpotSection<Reporter, Spot> bfss)
     32  public void calculate(BASEFileSpotSection<Reporter, Spot> bfss, boolean up)
    3433  {
    35     // Calucalte assay sizes
    36     int[] assaySizes = new int[bfss.getWidth()];
    37     Arrays.fill(assaySizes, bfss.getHeight());
    38     for (int i = 0; i < bfss.getHeight(); ++i)
    39     {
    40       for (int j = 0; j < bfss.getWidth(); ++j)
    41       {
    42         if (Double.isNaN(bfss.getSpot(i + j).getM()))
    43         {
    44           --assaySizes[j];
    45         }
    46       }
    47     }
    48 
    4934    for (int i = 0; i < bfss.getWidth(); ++i)
    5035    {
    5136      // to be able use i in the comparator it has to be final
    5237      final int finali = i;
     38      final boolean finalup = up;
     39     
    5340      bfss.sortReporter(new Comparator<Reporter>()
    5441      {
    5542        public int compare(Reporter r1, Reporter r2)
    5643        {
    57           double s1 = r1.getSpots()[finali].getM();
    58           double s2 = r2.getSpots()[finali].getM();
     44          double s1 = r1.getSpot(finali).getM();
     45          double s2 = r2.getSpot(finali).getM();
    5946         
    60           return Double.compare(s1, s2);
     47          int ret = 0;
     48          if (!Double.isNaN(s1) && !Double.isNaN(s2))
     49          {
     50            ret = Double.compare(s1, s2);
     51            if (finalup)
     52              ret *= -1;
     53          }
     54          else if (Double.isNaN(s1) && Double.isNaN(s2))
     55            ret = 0;
     56          else if (Double.isNaN(s1))
     57            ret = 1;
     58          else if (Double.isNaN(s2))
     59            ret = -1;
     60          return ret;
    6161        }
    6262      });
    6363     
    64       for (int j = 0; j < assaySizes[i]; ++j)
     64      // set the rank
     65      for (int j = 0; j < bfss.getHeight(); ++j)
    6566      {
    66         Reporter r = bfss.getReporter(j);
    67         r.addRank((j+1.0) / assaySizes[i]);
    68         if (r.getRankProduct() > 1)
     67        Spot s = bfss.getReporter(j).getSpot(i);
     68        if (!Double.isNaN(s.getM()))
    6969        {
    70           System.err.println(j + " " + assaySizes[i]);
    71           System.exit(0);
     70          s.setRank(j+1);
    7271        }
    7372      }
    7473    }
    75    
    76     // Calculate M and "size" for each reporter
    77     for (int i = 0; i < bfss.getHeight(); ++i)
    78     {
    79       Reporter r = bfss.getReporter(i);
    80       double avg = 0;
    81       int size = 0;
    82       for (int j = 0; j < bfss.getWidth(); ++j)
    83       {
    84         if (!Double.isNaN(r.getSpots()[j].getM()))
    85         {
    86           avg = r.getSpots()[j].getM();
    87           ++size;
    88         }
    89       }
    90       r.setM(avg / size);
    91       r.setSize(size);
    92     }
    9374  }
    9475}
  • trunk/se/lu/onk/OneClass/src/oneclass/rankproduct/Reporter.java

    r244 r246  
    3131  private final int id;
    3232  private final Spot[] spots;
    33   private double rankProduct = 1;
    3433  private double a;
    35   private double m;
    36   private int size;
     34  private double m = 0;
     35  private int size = 0;;
    3736 
    3837  public Reporter(int id, String reporter, String symbol, String locusLink, double a, Spot ... spots)
     
    4443    this.a = a;
    4544    this.spots = spots;
     45   
     46    for (Spot s : spots)
     47    {
     48      if (!Double.isNaN(s.getM()))
     49      {
     50        m += s.getM();
     51        ++size;
     52      }
     53    }
     54    m = m/size;
    4655  }
    4756
     
    6877  public final double getRankProduct()
    6978  {
    70     return rankProduct;
     79    double rankProduct = 1;
     80   
     81    for (Spot s : spots)
     82    {
     83      if (!Double.isNaN(s.getM()))
     84      {
     85        rankProduct *= s.getRank();
     86      }
     87    }
     88   
     89    return Math.pow(rankProduct, 1.0/getSize());
    7190  }
    7291 
     
    7493  {
    7594    return reporter;
     95  }
     96
     97  public final Spot getSpot(int i)
     98  {
     99    return spots[i];
    76100  }
    77101
     
    86110  }
    87111
    88   public void setSize(int size)
    89   {
    90     this.size = size;
    91   }
    92 
    93112  public int getSize()
    94113  {
    95114    return size;
    96115  }
    97  
    98   public final void setM(double m)
    99   {
    100     this.m = m;
    101   }
    102  
    103   public void addRank(double rank)
    104   {
    105     rankProduct *= rank;
    106   }
    107116}
  • trunk/se/lu/onk/OneClass/src/oneclass/rankproduct/Spot.java

    r244 r246  
    2828  private double m;
    2929 
     30  private int rank = -1;
     31 
    3032  public Spot(Double m)
    3133  {
     
    3840  }
    3941 
     42  public final int getRank()
     43  {
     44    return rank;
     45  }
     46 
     47  public final void setRank(int rank)
     48  {
     49    this.rank = rank;
     50  }
     51 
    4052  @Override
    4153  public String toString()
  • trunk/se/lu/onk/OneClass/src/oneclass/rankproduct/Start.java

    r244 r246  
    103103        data = bfr.readDataRow(dataLength);
    104104      }
    105       rankproduct.calculate(bfss);
    106       bfss.sortReporter(new Comparator<Reporter>()
    107         {
    108 
    109           public int compare(Reporter r1, Reporter r2)
    110           {
    111             if (r1.getRankProduct() < r2.getRankProduct()) return -1;
    112             else if (r1.getRankProduct() == r2.getRankProduct()) return 0;
    113             else return 1;
    114           }
    115         });
    116       print(bfss);
    117     }
    118     catch (FileNotFoundException e)
    119     {
    120       System.err.println("Can't find the file " + (args.length == 1 ? args[0] : "stdin.txt"));
    121     }
    122     catch (BadFormatException e)
    123     {
    124       System.err.println((args.length == 1 ? args[0] : "stdin.txt") + " is not a basefile");
    125     }
    126     catch (IOException e)
    127     {
    128       System.err.println("An error when reading file " + (args.length == 1 ? args[0] : "stdin.txt"));
    129       e.printStackTrace();
    130     }
    131     catch (BASEFileException e)
    132     {
    133       e.printStackTrace();
    134     }
    135   }
    136 
    137   private static void print(BASEFileSpotSection<Reporter, Spot> bfss)
    138   {
    139 
    140     try
    141     {
    142       XYSeries oSeries = new XYSeries("Observed");
    143       XYSeries eSeries = new XYSeries("Expected");
    144       XYSeries fdrSeries = new XYSeries("FDR");
    145       HistogramDataset mHist = new HistogramDataset("M", 0.1);
    146 
    147       PrintWriter html = new PrintWriter(new File("index.html"));
    148       PrintWriter tab = new PrintWriter(new File("result.tsv"));
    149       int maxRank = bfss.getHeight();
    150      
    151       Object[] tableRow = {"Reporter","GeneSymbol","LocusLink","AverageM","up/down","Rank","Rank product","NumberOfValues"};
    152 
     105     
    153106      System.out.println("BASEfile");
    154107      System.out.println("section\tassays");
     
    157110      System.out.println("count\t1");
    158111      System.out.println("%");
    159       System.out.println("1\tztest\t"+bfss.findStringOpt("assays").replaceAll("\t", "/"));
     112      System.out.println("1\tRankProduct down\t"+bfss.findStringOpt("assays").replaceAll("\t", "/"));
     113      System.out.println("2\tRankProduct up\t"+bfss.findStringOpt("assays").replaceAll("\t", "/"));
    160114      System.out.println();
     115     
     116      rankproduct.calculate(bfss, false);
     117
    161118      System.out.println("section\tspots");
    162119      System.out.println("channels\t2");
     
    167124      System.out.println("count\t"+bfss.getReporterSize());
    168125      System.out.println("%");
     126     
     127      print(bfss, "down");
     128
     129      rankproduct.calculate(bfss, true);
     130
     131      System.out.println("section\tspots");
     132      System.out.println("channels\t2");
     133      System.out.println("setExtraFloats\tJE_nbrOfElements\tJE_rank\tJE_rankProduct");
     134      System.out.println("assayFields\tl2ratio1_2\tl10intgmean1_2\tJJE_nbrOfElements\tJE_rank\tE_rankProduct");
     135      System.out.println("columns\tposition\treporter\tassayData");
     136      System.out.println("assays\t2");
     137      System.out.println("count\t"+bfss.getReporterSize());
     138      System.out.println("%");
     139     
     140      print(bfss, "up");
     141     
     142    }
     143    catch (FileNotFoundException e)
     144    {
     145      System.err.println("Can't find the file " + (args.length == 1 ? args[0] : "stdin.txt"));
     146    }
     147    catch (BadFormatException e)
     148    {
     149      System.err.println((args.length == 1 ? args[0] : "stdin.txt") + " is not a basefile");
     150    }
     151    catch (IOException e)
     152    {
     153      System.err.println("An error when reading file " + (args.length == 1 ? args[0] : "stdin.txt"));
     154      e.printStackTrace();
     155    }
     156    catch (BASEFileException e)
     157    {
     158      e.printStackTrace();
     159    }
     160  }
     161
     162  private static void print(BASEFileSpotSection<Reporter, Spot> bfss, String prefix)
     163  {
     164    try
     165    {
     166      bfss.sortReporter(new Comparator<Reporter>()
     167        {
     168          public int compare(Reporter r1, Reporter r2)
     169          {
     170            return Double.compare(r1.getRankProduct(), r2.getRankProduct());
     171          }
     172        });
     173      XYSeries oSeries = new XYSeries("Observed");
     174      XYSeries eSeries = new XYSeries("Expected");
     175      XYSeries fdrSeries = new XYSeries("FDR");
     176      HistogramDataset mHist = new HistogramDataset("M", 0.1);
     177
     178      PrintWriter html = new PrintWriter(new File(prefix+"index.html"));
     179      PrintWriter tab = new PrintWriter(new File(prefix+"result.tsv"));
     180     
     181      Object[] tableRow = {"Reporter","GeneSymbol","LocusLink","AverageM","up/down","Rank","Rank product","NumberOfValues"};
     182
    169183     
    170184      html.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">");
     
    184198        int rank = i + 1;
    185199        Reporter r = bfss.getReporter(i);
    186         double rankProduct = r.getRankProduct() * maxRank;
     200        double rankProduct = r.getRankProduct();
    187201       
    188202        System.out.printf("%d\t%d\t", (-1*rank), r.getId());
     
    221235      tab.close();
    222236     
    223       Plot.plotFDR(fdrSeries);
    224       Plot.plotOE(oSeries, eSeries);
    225       Plot.plotOE_FDR(oSeries, eSeries, fdrSeries);
     237//      Plot.plotFDR(fdrSeries);
     238//      Plot.plotOE(oSeries, eSeries);
     239//      Plot.plotOE_FDR(oSeries, eSeries, fdrSeries);
    226240//      Plot.plotMHist(mHist, ztest.getTotalMean(), ztest.getTotalSD());
    227241    }
Note: See TracChangeset for help on using the changeset viewer.