View Javadoc

1   /*
2    *                Doelan development code
3    *
4    * This code may be freely distributed and modified under the
5    * terms of the GNU General Public Licence.  This should
6    * be distributed with the code. If you do not have a copy,
7    * see:
8    *
9    *      http://www.gnu.org/copyleft/gpl.txt
10   *
11   * Copyright (c) 2004-2005 ENS Microarray Platform
12   * Copyright for this code is held jointly by the individual
13   * authors.  These should be listed in @author doc comments.
14   *
15   * For more information on the Doelan project and its aims,
16   * or to join the Doelan mailing list, visit the home page
17   * at:
18   *
19   *      http://www.transcriptome.ens.fr/doelan
20   */
21  
22  package fr.ens.transcriptome.doelan.tests;
23  
24  import org.apache.commons.collections.primitives.ArrayDoubleList;
25  
26  import fr.ens.transcriptome.doelan.Defaults;
27  import fr.ens.transcriptome.doelan.DoelanChart;
28  import fr.ens.transcriptome.doelan.DoelanRegistery;
29  import fr.ens.transcriptome.doelan.algorithms.QualityUnitTest;
30  import fr.ens.transcriptome.doelan.data.QualityUnitTestResult;
31  import fr.ens.transcriptome.doelan.data.QualityUnitTestResult.SummaryResult;
32  import fr.ens.transcriptome.nividic.om.BioAssay;
33  import fr.ens.transcriptome.nividic.om.SpotIterator;
34  import fr.ens.transcriptome.nividic.platform.PlatformException;
35  import fr.ens.transcriptome.nividic.platform.module.AboutModule;
36  import fr.ens.transcriptome.nividic.platform.module.Module;
37  import fr.ens.transcriptome.nividic.platform.module.ModuleDescription;
38  import fr.ens.transcriptome.nividic.util.SystemUtils;
39  import fr.ens.transcriptome.nividic.util.parameter.FixedParameters;
40  import fr.ens.transcriptome.nividic.util.parameter.Parameter;
41  import fr.ens.transcriptome.nividic.util.parameter.ParameterBuilder;
42  import fr.ens.transcriptome.nividic.util.parameter.ParameterException;
43  import fr.ens.transcriptome.nividic.util.parameter.Parameters;
44  
45  /***
46   * This class defines a quality test based on spot diameter.
47   * @author Laurent Jourdren
48   */
49  public class MaxDiameterFeatureTest extends QualityUnitTest implements Module {
50  
51    private static final int DEFAULT_BINS = 16;
52    private static AboutModule aboutModule;
53  
54    /***
55     * Get the description of the module.
56     * @return The description of the module
57     */
58    public AboutModule aboutModule() {
59  
60      if (aboutModule == null) {
61  
62        ModuleDescription md = null;
63        try {
64          md = new ModuleDescription("MaxDiameterFeatureTest",
65              "Test maximal diameter of features");
66          md.setWebsite(DoelanRegistery.getAppURL());
67          md.setHTMLDocumentation(SystemUtils.readTextRessource("/files/test-"
68              + SystemUtils.getClassShortName(this.getClass()) + ".html"));
69          md.setStability(AboutModule.STATE_STABLE);
70          md.setVersion(Defaults.DEFAULT_TEST_VERSION);
71        } catch (PlatformException e) {
72          getLogger().error("Unable to create the module description");
73        }
74  
75        aboutModule = md;
76      }
77  
78      return aboutModule;
79    }
80  
81    /***
82     * Set the parameters of the element.
83     * @return The defaults parameters to set.
84     */
85    protected Parameters defineParameters() {
86  
87      try {
88  
89        final Parameter threshold = new ParameterBuilder().withName("threshold")
90            .withLongName("Maximal threshold of bad spots").withType(
91                Parameter.DATATYPE_DOUBLE).withDescription(
92                "Threshold of invalid spots to reject the chip")
93            .withGreaterThanValue(0).withLowerThanValue(1).withDefaultValue("10")
94            .withUnit("%").getParameter();
95  
96        final Parameter max = new ParameterBuilder().withName("max")
97            .withLongName("Maximal diameter value").withType(
98                Parameter.DATATYPE_INTEGER).withDescription(
99                "Maximal value of the diameter for a spot (excluded)")
100           .withGreaterThanValue(0).withDefaultValue("140").withUnit("µm")
101           .getParameter();
102 
103       final Parameter filterFlags = new ParameterBuilder().withName(
104           "filterFlags").withType(Parameter.DATATYPE_YESNO).withLongName(
105           "Remove bad spots from output array list").withDescription(
106           "Remove invalid spots in output arraylist file.").withDefaultValue(
107           "yes").getParameter();
108 
109       final FixedParameters params = new FixedParameters();
110       params.addParameter(max);
111       params.addParameter(threshold);
112       params.addParameter(filterFlags);
113 
114       return params;
115 
116     } catch (ParameterException e) {
117       getLogger().error("Error while creating parameters: " + e);
118     }
119 
120     return null;
121   }
122 
123   /***
124    * Test the quality of the bioassay.
125    * @param bioassay BioAssay to test
126    * @param arrayList The array list
127    * @param parameters parameters of the test
128    * @return A QualityObjectResultTest Object
129    * @throws PlatformException if an error occurs while executing the test.
130    */
131   public QualityUnitTestResult test(final BioAssay bioassay,
132       final BioAssay arrayList, final Parameters parameters)
133       throws PlatformException {
134 
135     QualityUnitTestResult result = null;
136 
137     try {
138       final int max = parameters.getParameter("max").getIntValue();
139 
140       final double threshold = parameters.getParameter("threshold")
141           .getDoubleValue();
142 
143       final boolean filterFlags = parameters.getParameter("filterFlags")
144           .getBooleanValue();
145 
146       final boolean[] results = new boolean[bioassay.size()];
147       // final int[] flags = bioassay.getFlags();
148       final int[] dia = bioassay.getDataFieldInt("Dia.");
149 
150       if (dia == null)
151         throw new PlatformException("Diameter field doesn't exits");
152 
153       int countBadDiameter = 0;
154       int countRealSpot = 0;
155       // Dataset dataset = new
156       ArrayDoubleList adl = new ArrayDoubleList();
157       // Data data = new Data();
158 
159       SpotIterator si = bioassay.iterator();
160       int i = 0;
161 
162       while (si.hasNext()) {
163         si.next();
164         if (si.isEmpty() || si.isFlagAbscent()) {
165           i++;
166           continue;
167         }
168 
169         // data.add(dia[i], 1);
170         adl.add(dia[i]);
171         if (dia[i] < max)
172           results[i] = true;
173         else {
174           results[i] = false;
175           countBadDiameter++;
176         }
177 
178         countRealSpot++;
179         i++;
180       }
181 
182       final double ratio = ((double) countBadDiameter)
183           / ((double) countRealSpot) * 100;
184 
185       result = new QualityUnitTestResult(bioassay, this);
186 
187       long maxThreshold = (long) (countRealSpot * threshold / 100.0);
188 
189       result.setMessage("Bad diameter features: " + countBadDiameter + "/"
190           + countRealSpot + " features (threshold: " + maxThreshold
191           + " features)");
192 
193       DoelanChart gu = new DoelanChart();
194       gu.setData(adl.toArray());
195       gu.setTitle("Diameter distribution");
196       gu.setXAxisLegend("Spot diameter (pixels)");
197       gu.setYAxisLegend("#Spots");
198       gu.setHistCaption("Spots");
199       gu.setThreshold(max);
200       gu.setWidth(DoelanRegistery.getDoelanChartWidth());
201       gu.setHeight(DoelanRegistery.getDoelanChartHeigth());
202       gu.setBins(DEFAULT_BINS);
203 
204       result.setImage(gu.getImage());
205 
206       result.setGlobalResultType(true);
207       result.setNewFlags(results);
208       result.setFilterFlags(filterFlags);
209       SummaryResult rac = result.getResultAllChannels();
210       rac.setPercent(true);
211       rac.setThresholdEqualityType("<=");
212       rac.setUnit("%");
213       rac.setThreshold(threshold);
214       rac.setValue(ratio);
215       rac.setPass(ratio <= threshold);
216 
217     } catch (ParameterException e) {
218 
219       throw new PlatformException("Error while creating parameters ("
220           + this.getClass().getName() + "): " + e.getMessage());
221     }
222 
223     return result;
224   }
225 
226   //
227   // Constructor
228   //
229 
230   /***
231    * Public constructor.
232    * @throws PlatformException If the name or the version of the element is
233    *           <b>null </b>.
234    */
235   public MaxDiameterFeatureTest() throws PlatformException {
236     // MUST BE EMPTY
237   }
238 
239 }