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.algorithms;
23  
24  import java.awt.Color;
25  import java.awt.Image;
26  import java.util.Arrays;
27  import java.util.Iterator;
28  
29  import fr.ens.transcriptome.doelan.Defaults;
30  import fr.ens.transcriptome.doelan.data.DoelanDataUtils;
31  import fr.ens.transcriptome.doelan.data.QualityTestResult;
32  import fr.ens.transcriptome.doelan.data.QualityUnitTestResult;
33  import fr.ens.transcriptome.doelan.data.TestSuiteResult;
34  import fr.ens.transcriptome.nividic.om.BioAssay;
35  import fr.ens.transcriptome.nividic.om.BioAssayFactory;
36  import fr.ens.transcriptome.nividic.om.BioAssayUtils;
37  import fr.ens.transcriptome.nividic.om.GenepixArrayList;
38  import fr.ens.transcriptome.nividic.om.SpotIterator;
39  import fr.ens.transcriptome.nividic.platform.PlatformException;
40  import fr.ens.transcriptome.nividic.platform.module.AboutModule;
41  import fr.ens.transcriptome.nividic.platform.module.Module;
42  import fr.ens.transcriptome.nividic.platform.module.ModuleDescription;
43  import fr.ens.transcriptome.nividic.platform.workflow.Algorithm;
44  import fr.ens.transcriptome.nividic.platform.workflow.Container;
45  import fr.ens.transcriptome.nividic.util.graphics.ArrayPlot;
46  import fr.ens.transcriptome.nividic.util.graphics.DefaultColor;
47  import fr.ens.transcriptome.nividic.util.parameter.Parameters;
48  
49  /***
50   * This algorithm execute all the global tests and compute the ouput array list.
51   * @author Laurent Jourdren
52   */
53  public class DoelanExecuteGlobalTests extends Algorithm implements Module {
54  
55    // For log system
56    // private static Logger log =
57    // Logger.getLogger(DoelanExecuteGlobalTests.class);
58  
59    //
60    // Other methods
61    //
62  
63    /***
64     * Get the description of the module.
65     * @return The description of the module
66     */
67    public AboutModule aboutModule() {
68      ModuleDescription md = null;
69      try {
70        md = new ModuleDescription("DoelanExecuteGlobalTests",
71            "Execute global tests");
72        md.setStability(AboutModule.STATE_STABLE);
73      } catch (PlatformException e) {
74        getLogger().error("Unable to create the module description");
75      }
76      return md;
77    }
78  
79    /***
80     * Set the parameters of the element.
81     * @return The defaults parameters to set.
82     */
83    protected Parameters defineParameters() {
84      return null;
85    }
86  
87    /***
88     * This method contains all the code to manipulate the container <b>c </b> in
89     * this element.
90     * @param c The container to be manipulated
91     * @param parameters Parameters of the elements
92     * @throws PlatformException if an error occurs while executing the test.
93     */
94    protected void doIt(final Container c, final Parameters parameters)
95        throws PlatformException {
96  
97      // Execute all the global tests
98      TestSuiteResult testSuiteResult = DoelanDataUtils.getTestSuiteResult(c);
99      executeGlobalTest(c, testSuiteResult);
100 
101     // Generate the output Array list
102     BioAssay arrayList = DoelanDataUtils.getArrayList(c);
103 
104     if (arrayList != null) {
105 
106       BioAssay bioAssay = DoelanDataUtils.getBioAssay(c);
107 
108       QualityUnitTestResult[] unitResults = DoelanDataUtils
109           .getQualityUnitTestResults(c);
110 
111       BioAssay newArrayList = reFlag(arrayList, bioAssay, unitResults,
112           testSuiteResult.getSpotRejectedId());
113 
114       // Set the arrayplot image
115       Image arrayplot = getArrayPlot(createArrayListForArrayPlot(arrayList,
116           bioAssay, unitResults));
117 
118       if (arrayplot != null)
119         testSuiteResult.setArrayPlot(arrayplot);
120 
121       testSuiteResult.setNewArrayList(newArrayList);
122 
123     }
124   }
125 
126   /***
127    * Execute all the global tests
128    * @param c The container to be manipulated
129    * @param testSuiteResult The result object of the test suite
130    * @throws PlatformException if an error occurs while executing the tests
131    */
132   private void executeGlobalTest(final Container c,
133       final TestSuiteResult testSuiteResult) throws PlatformException {
134 
135     if (testSuiteResult == null)
136       return;
137 
138     Iterator it = testSuiteResult.getGlobalTests().iterator();
139     while (it.hasNext()) {
140       TestSuiteResult.GlobalTest gt = (TestSuiteResult.GlobalTest) it.next();
141       gt.executeTest(c);
142     }
143 
144     computeTestSuiteFinalResult(c);
145   }
146 
147   /***
148    * Compute the final result of the test suite.
149    * @param c The container to be manipulated
150    */
151   private void computeTestSuiteFinalResult(final Container c) {
152 
153     TestSuiteResult globalResult = DoelanDataUtils.getTestSuiteResult(c);
154     QualityTestResult[] results = DoelanDataUtils.getQualityTestResults(c);
155 
156     boolean r = true;
157     for (int i = 0; i < results.length; i++)
158       r = r && results[i].getResult();
159 
160     globalResult.setResult(r);
161   }
162 
163   private boolean[] compileResults(
164       final QualityUnitTestResult[] qualityUnitTestresults, final boolean filter) {
165 
166     if (qualityUnitTestresults == null)
167       return null;
168 
169     boolean[] result = null;
170 
171     for (int i = 0; i < qualityUnitTestresults.length; i++) {
172       QualityUnitTestResult r = qualityUnitTestresults[i];
173 
174       final boolean[] flags = r.getNewFlags();
175 
176       if (!filter || r.isFilterFlags()) {
177 
178         if (result == null)
179           result = (boolean[]) flags.clone();
180         else
181           for (int j = 0; j < flags.length; j++)
182             result[j] = result[j] & flags[j];
183       }
184     }
185 
186     return result;
187   }
188 
189   private BioAssay createArrayListForArrayPlot(final BioAssay gal,
190       final BioAssay gpr, final QualityUnitTestResult[] result) {
191 
192     if (gal == null || gpr == null || result == null || result.length == 0)
193       return null;
194 
195     if (!BioAssayUtils.containLocations(gpr, gal))
196       return null;
197 
198     // Create new Gal object
199     BioAssay bioAssayArrayPlot = BioAssayFactory.createBioAssay();
200     bioAssayArrayPlot.getAnnotation().addProperties(gal.getAnnotation());
201     bioAssayArrayPlot.setSpotEmptyTester(gpr.getSpotEmptyTester());
202 
203     bioAssayArrayPlot.setIds(gal.getIds());
204     bioAssayArrayPlot.setDescriptions(gal.getDescriptions());
205     bioAssayArrayPlot.setLocations(gal.getLocations());
206 
207     // Compile the results
208     boolean[] globalResult = compileResults(result, false);
209     boolean[] globalResultRequest = compileResults(result, true);
210 
211     if (globalResultRequest == null)
212       globalResultRequest = new boolean[gpr.size()];
213 
214     // Set the color of the spots
215     int[] colors = new int[globalResult.length];
216     for (int i = 0; i < colors.length; i++)
217       if (globalResult[i])
218         colors[i] = DefaultColor.LIGHT_GREEN.getRGB();
219       else if (globalResultRequest[i])
220         colors[i] = Color.WHITE.getRGB();
221       else
222         colors[i] = DefaultColor.LIGHT_RED.getRGB();
223 
224     int[] colorsToSet = new int[gal.size()];
225 
226     // Copy the color on the output gal
227     SpotIterator si2 = gpr.iterator();
228     int i = 0;
229     while (si2.hasNext()) {
230 
231       si2.next();
232 
233       int loc = si2.getLocation();
234       int galIndex = gal.getIndexFromALocation(loc);
235 
236       if (galIndex != -1)
237         colorsToSet[galIndex] = colors[i];
238       i++;
239     }
240 
241     bioAssayArrayPlot.setDataFieldInt(ArrayPlot.FIELD_NAME_RGB_COLOR,
242         colorsToSet);
243 
244     bioAssayArrayPlot.setSpotEmptyTester(gpr.getSpotEmptyTester());
245 
246     return bioAssayArrayPlot;
247   }
248 
249   /***
250    * Create a new array list (a GAL BioAssay object) without bad spots.
251    * @param gal initial array list
252    * @param gpr initial BioAssay
253    * @param result Map of the unit tests results
254    * @return an new array list without the bad spots
255    */
256   private BioAssay reFlag(final BioAssay gal, final BioAssay gpr,
257       final QualityUnitTestResult[] result, final String rejectedSpotId) {
258 
259     if (gal == null || gpr == null || result == null)
260       return null;
261 
262     // Test if the gal file is compatible with the gpr data
263     SpotIterator si = gal.iterator();
264     while (si.hasNext()) {
265       si.next();
266       if (gpr.getIndexFromALocation(si.getLocation()) == -1)
267         return null;
268     }
269 
270     // Create new Gal object
271     BioAssay newGal = BioAssayFactory.createBioAssay();
272     newGal.getAnnotation().addProperties(gal.getAnnotation());
273     newGal.setSpotEmptyTester(gpr.getSpotEmptyTester());
274 
275     final String[] newGalIds = (String[]) gal.getIds().clone();
276     final int[] newGalLocations = (int[]) gal.getLocations().clone();
277     final String[] newGalDesc = (String[]) gal.getDescriptions().clone();
278 
279     newGal.setIds(newGalIds);
280     newGal.setLocations(newGalLocations);
281     newGal.setDescriptions(newGalDesc);
282 
283     final int[] gprLocations = gpr.getLocations();
284 
285     boolean[] globalResultRequest = null;
286 
287     for (int i = 0; i < result.length; i++) {
288       QualityUnitTestResult r = result[i];
289 
290       final boolean[] flags = r.getNewFlags();
291 
292       if (globalResultRequest == null) {
293         globalResultRequest = new boolean[flags.length];
294         Arrays.fill(globalResultRequest, true);
295       }
296 
297       if (r.isFilterFlags())
298         for (int j = 0; j < globalResultRequest.length; j++) {
299 
300           if (globalResultRequest[j] && !flags[j]) {
301             globalResultRequest[j] = false;
302 
303             int loc = gprLocations[j];
304             int galIndex = gal.getIndexFromALocation(loc);
305 
306             if (galIndex >= 0) {
307 
308               StringBuffer sb = new StringBuffer();
309               sb.append("id=").append(newGalIds[galIndex]).append(
310                   "; description=").append(newGalDesc[galIndex]).append(
311                   "; fail to ").append(r.getTestDescription());
312 
313               newGalDesc[galIndex] = sb.toString();
314               newGalIds[galIndex] = rejectedSpotId;
315 
316             }
317 
318           } else if (!flags[j]) {
319 
320             int loc = gprLocations[j];
321             int galIndex = gal.getIndexFromALocation(loc);
322 
323             if (galIndex >= 0) {
324 
325               StringBuffer sb = new StringBuffer();
326               sb.append(newGalDesc[galIndex]).append(", ").append(
327                   r.getTestDescription());
328 
329               newGalDesc[galIndex] = sb.toString();
330             }
331           }
332         }
333     }
334 
335     return newGal;
336   }
337 
338   private Image getArrayPlot(final BioAssay gal) {
339 
340     if (gal == null)
341       return null;
342 
343     GenepixArrayList arl = new GenepixArrayList(gal);
344 
345     ArrayPlot ap = new ArrayPlot(arl.getBlocks(), Defaults.ARRAY_PLOT_WIDTH,
346         -1, Defaults.ARRAY_PLOT_MARGIN, gal);
347 
348     return ap.getImage();
349   }
350 
351   //
352   // Constructor
353   //
354 
355   /***
356    * Public constructor.
357    * @throws PlatformException If the name or the version of the element is
358    *           <b>null </b>.
359    */
360   public DoelanExecuteGlobalTests() throws PlatformException {
361     // MUST BE EMPTY
362   }
363 
364 }