plugins/base1/se.lu.onk.ReplicateError/trunk/src/replicateerror/WithinAssays/ReplicateErrorWA.java

Code
Comments
Other
Rev Date Author Line
43 21 Nov 05 enell 1 /*
784 18 Sep 08 jari 2   $Id$
784 18 Sep 08 jari 3
784 18 Sep 08 jari 4   Copyright (C) 2005 Johan Enell
793 25 Sep 08 jari 5   Copyright (C) 2008 Jari Häkkinen
784 18 Sep 08 jari 6
784 18 Sep 08 jari 7   This file is part of the se.lu.onk.ReplicateError plug-in for
784 18 Sep 08 jari 8   BASE. Available at http://baseplugins.thep.lu.se/ and BASE web
784 18 Sep 08 jari 9   site is http://base.thep.lu.se
784 18 Sep 08 jari 10
784 18 Sep 08 jari 11   This is free software; you can redistribute it and/or modify it
784 18 Sep 08 jari 12   under the terms of the GNU General Public License as published by
784 18 Sep 08 jari 13   the Free Software Foundation; either version 3 of the License, or
784 18 Sep 08 jari 14   (at your option) any later version.
784 18 Sep 08 jari 15
784 18 Sep 08 jari 16   The software is distributed in the hope that it will be useful, but
784 18 Sep 08 jari 17   WITHOUT ANY WARRANTY; without even the implied warranty of
784 18 Sep 08 jari 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
784 18 Sep 08 jari 19   General Public License for more details.
784 18 Sep 08 jari 20
784 18 Sep 08 jari 21   You should have received a copy of the GNU General Public License
784 18 Sep 08 jari 22   along with BASE. If not, see <http://www.gnu.org/licenses/>.
784 18 Sep 08 jari 23 */
43 21 Nov 05 enell 24 package replicateerror.WithinAssays;
43 21 Nov 05 enell 25
43 21 Nov 05 enell 26 import java.io.File;
43 21 Nov 05 enell 27 import java.io.IOException;
43 21 Nov 05 enell 28 import java.io.PrintStream;
43 21 Nov 05 enell 29 import java.util.HashMap;
43 21 Nov 05 enell 30 import java.util.Iterator;
43 21 Nov 05 enell 31 import java.util.List;
43 21 Nov 05 enell 32
43 21 Nov 05 enell 33 import basefile.BASEFileReader;
43 21 Nov 05 enell 34 import basefile.BASEFileSection;
43 21 Nov 05 enell 35 import basefile.PluginException;
52 25 Nov 05 enell 36 import replicateerror.R;
43 21 Nov 05 enell 37 import replicateerror.Replicate;
43 21 Nov 05 enell 38 import replicateerror.ReplicateError;
80 24 Mar 06 enell 39 import replicateerror.Spot;
43 21 Nov 05 enell 40 import replicateerror.AcrossAssays.HTML;
43 21 Nov 05 enell 41
43 21 Nov 05 enell 42 public class ReplicateErrorWA extends ReplicateError
43 21 Nov 05 enell 43 {
80 24 Mar 06 enell 44   private HashMap<Integer, String> assayName;
43 21 Nov 05 enell 45
43 21 Nov 05 enell 46
43 21 Nov 05 enell 47   public ReplicateErrorWA()
43 21 Nov 05 enell 48   {
43 21 Nov 05 enell 49     super();
80 24 Mar 06 enell 50     assayName = new HashMap<Integer, String>();
43 21 Nov 05 enell 51     File dataDir = new File("data");
43 21 Nov 05 enell 52     dataDir.mkdir();
43 21 Nov 05 enell 53   }
43 21 Nov 05 enell 54   
43 21 Nov 05 enell 55   @Override
43 21 Nov 05 enell 56   public void extractAssays(BASEFileSection section, BASEFileReader bfr) throws PluginException
43 21 Nov 05 enell 57   {
43 21 Nov 05 enell 58     int nameCol = section.findFieldList("columns").indexOf("name");
43 21 Nov 05 enell 59     int idCol = section.findFieldList("columns").indexOf("id");
43 21 Nov 05 enell 60     
43 21 Nov 05 enell 61     try
43 21 Nov 05 enell 62     {
43 21 Nov 05 enell 63       String[] vals;
43 21 Nov 05 enell 64       while ((vals = bfr.readDataRow()) != null)
43 21 Nov 05 enell 65       {
80 24 Mar 06 enell 66         assayName.put(new Integer(vals[idCol]), vals[nameCol]);
43 21 Nov 05 enell 67       }
43 21 Nov 05 enell 68     }
43 21 Nov 05 enell 69     catch (IOException e)
43 21 Nov 05 enell 70     {
43 21 Nov 05 enell 71       e.printStackTrace();
43 21 Nov 05 enell 72     }
43 21 Nov 05 enell 73   }
43 21 Nov 05 enell 74
43 21 Nov 05 enell 75   @Override
43 21 Nov 05 enell 76   public void extractSpots(BASEFileSection section, BASEFileReader bfr)
43 21 Nov 05 enell 77   {
43 21 Nov 05 enell 78     List<String> columns = section.findFieldList("columns");
43 21 Nov 05 enell 79     List<String> assayFields = section.findFieldList("assayFields");
80 24 Mar 06 enell 80     List<Integer> assays = section.findFieldIntList("assays");
43 21 Nov 05 enell 81
43 21 Nov 05 enell 82     int posCol = columns.indexOf("position");
43 21 Nov 05 enell 83     int repCol = columns.indexOf("reporter");
43 21 Nov 05 enell 84     int assayData = columns.indexOf("assayData");
43 21 Nov 05 enell 85     int int1Col = assayFields.indexOf("intensity1");
43 21 Nov 05 enell 86     int int2Col = assayFields.indexOf("intensity2");
43 21 Nov 05 enell 87
80 24 Mar 06 enell 88     HashMap<Integer, Integer> assayColumns = new HashMap<Integer, Integer>();
43 21 Nov 05 enell 89     for (int i = 0; i < assays.size(); i++)
43 21 Nov 05 enell 90     {
43 21 Nov 05 enell 91       assayColumns.put(assays.get(i), assayData + assayFields.size() * i);
43 21 Nov 05 enell 92     }
43 21 Nov 05 enell 93
43 21 Nov 05 enell 94     BASEFileSection spotSection = new BASEFileSection();
43 21 Nov 05 enell 95     spotSection.setHeader("section", "spots");
43 21 Nov 05 enell 96     spotSection.setHeader("channels", "2");
43 21 Nov 05 enell 97     spotSection.setHeader("assayFields", "intensity1\tintensity2");
43 21 Nov 05 enell 98     spotSection.setHeader("columns", "position\treporter\tassayData");
43 21 Nov 05 enell 99     spotSection.setHeader("assays", "");
43 21 Nov 05 enell 100
43 21 Nov 05 enell 101     try
43 21 Nov 05 enell 102     {
43 21 Nov 05 enell 103       HTML.openPlotsTable(center.toString());
80 24 Mar 06 enell 104       for (Integer assay : assays)
43 21 Nov 05 enell 105       {
43 21 Nov 05 enell 106         HashMap<Integer, Replicate> replicates = new HashMap<Integer, Replicate>();
80 24 Mar 06 enell 107         HashMap<Integer, Replicate> singletons = new HashMap<Integer, Replicate>();
43 21 Nov 05 enell 108         
43 21 Nov 05 enell 109         String[] vals;
43 21 Nov 05 enell 110         while ((vals = bfr.readDataRow()) != null)
43 21 Nov 05 enell 111         {
80 24 Mar 06 enell 112           Integer reporter = new Integer(vals[repCol]);
43 21 Nov 05 enell 113
43 21 Nov 05 enell 114           Replicate replicate = replicates.get(reporter);
43 21 Nov 05 enell 115           if (replicate == null)
43 21 Nov 05 enell 116           {
80 24 Mar 06 enell 117             replicate = new Replicate();
43 21 Nov 05 enell 118           }
43 21 Nov 05 enell 119           try
43 21 Nov 05 enell 120           {
80 24 Mar 06 enell 121             int position = new Integer(vals[posCol]);
43 21 Nov 05 enell 122             float int1 = new Float(vals[assayColumns.get(assay) + int1Col]);
43 21 Nov 05 enell 123             float int2 = new Float(vals[assayColumns.get(assay) + int2Col]);
43 21 Nov 05 enell 124             if (int1 > 0 && int2 > 0)
43 21 Nov 05 enell 125             {
80 24 Mar 06 enell 126               Spot s = new Spot(new Integer(assay));
80 24 Mar 06 enell 127               s.add(position, int1, int2);
80 24 Mar 06 enell 128               replicate.add(s);
43 21 Nov 05 enell 129               replicates.put(reporter, replicate);
43 21 Nov 05 enell 130             }
43 21 Nov 05 enell 131           }
43 21 Nov 05 enell 132           catch (NumberFormatException e)
43 21 Nov 05 enell 133           {}
43 21 Nov 05 enell 134         }
43 21 Nov 05 enell 135         
52 25 Nov 05 enell 136         // remove unusable replicates
791 24 Sep 08 jari 137         purgeUnusableReplicates(replicates, singletons);
43 21 Nov 05 enell 138         
795 01 Oct 08 jari 139         // calculate mean or median before filter
795 01 Oct 08 jari 140         List<Float> mList = getMList(replicates.values());
43 21 Nov 05 enell 141         float averageBefore;
795 01 Oct 08 jari 142         int nbrBefore = mList.size();
43 21 Nov 05 enell 143         if (center == Center.median)
43 21 Nov 05 enell 144         {
43 21 Nov 05 enell 145           averageBefore = median(mList);
43 21 Nov 05 enell 146         }
43 21 Nov 05 enell 147         else
43 21 Nov 05 enell 148         {
43 21 Nov 05 enell 149           averageBefore = mean(mList);
43 21 Nov 05 enell 150         }
43 21 Nov 05 enell 151         // Calculate standard deviation
43 21 Nov 05 enell 152         float sdBefore = standardDiviation(mList, mean(mList));
43 21 Nov 05 enell 153
80 24 Mar 06 enell 154         R.setABRBefore(assayName.get(assay), caluculateABR(replicates.values()));
52 25 Nov 05 enell 155         
791 24 Sep 08 jari 156         // print result to file for later use in R
80 24 Mar 06 enell 157         printDuplicates(replicates.values(), assayName.get(assay) + ".data");
43 21 Nov 05 enell 158         
80 24 Mar 06 enell 159         // filter replicates abs(M - mean) against 'sdLimit' number of sd's
2011 09 Jul 13 olle 160         // A negative 'sdLimit' indicates no filtering
2011 09 Jul 13 olle 161         if (sdLimit > 0f)
43 21 Nov 05 enell 162         {
2011 09 Jul 13 olle 163           for (Replicate r : replicates.values())
2011 09 Jul 13 olle 164           {
2011 09 Jul 13 olle 165             r.filter(averageBefore, sdLimit * sdBefore);
2011 09 Jul 13 olle 166           }
43 21 Nov 05 enell 167         }
791 24 Sep 08 jari 168         // print result to file for later use in R
80 24 Mar 06 enell 169         printDuplicates(replicates.values(), assayName.get(assay) + "good.data");
43 21 Nov 05 enell 170         
43 21 Nov 05 enell 171         // calculate mean
80 24 Mar 06 enell 172         mList = getMList(replicates.values());
43 21 Nov 05 enell 173         float averageAfter;
43 21 Nov 05 enell 174         if (center == Center.median)
43 21 Nov 05 enell 175         {
43 21 Nov 05 enell 176           averageAfter = median(mList);
43 21 Nov 05 enell 177         }
43 21 Nov 05 enell 178         else
43 21 Nov 05 enell 179         {
43 21 Nov 05 enell 180           averageAfter = mean(mList);
43 21 Nov 05 enell 181         }
43 21 Nov 05 enell 182         // Calculate standard deviation
43 21 Nov 05 enell 183         float sdAfter = standardDiviation(mList, mean(mList));
43 21 Nov 05 enell 184         
80 24 Mar 06 enell 185         R.setABRAfter(assayName.get(assay), caluculateABR(replicates.values()));
80 24 Mar 06 enell 186
80 24 Mar 06 enell 187         // print to stdout
80 24 Mar 06 enell 188         spotSection.setHeader("assays", assay);
80 24 Mar 06 enell 189         System.out.println(spotSection);
80 24 Mar 06 enell 190         printSpots(assay, replicates);
80 24 Mar 06 enell 191         printSpots(assay, singletons);
80 24 Mar 06 enell 192         System.out.println();
43 21 Nov 05 enell 193         
795 01 Oct 08 jari 194         HTML.addPlotsTable(assayName.get(assay), averageBefore, sdBefore,
795 01 Oct 08 jari 195                            nbrBefore, averageAfter, sdAfter, mList.size());
795 01 Oct 08 jari 196
43 21 Nov 05 enell 197         bfr.resetDataPosition(section);
43 21 Nov 05 enell 198       }
43 21 Nov 05 enell 199       HTML.closePlotsTable();
52 25 Nov 05 enell 200       
52 25 Nov 05 enell 201       R.printPlotFile();
43 21 Nov 05 enell 202     }
43 21 Nov 05 enell 203     catch (IOException e)
43 21 Nov 05 enell 204     {
43 21 Nov 05 enell 205       e.printStackTrace();
43 21 Nov 05 enell 206     }
43 21 Nov 05 enell 207   }
43 21 Nov 05 enell 208 }