src/core/net/sf/basedb/util/importer/spotdata/MatrixSpotIntensityEventHandler.java

Code
Comments
Other
Rev Date Author Line
5227 01 Feb 10 nicklas 1 /**
5227 01 Feb 10 nicklas 2   $Id$
5227 01 Feb 10 nicklas 3
5227 01 Feb 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5227 01 Feb 10 nicklas 5
5227 01 Feb 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5227 01 Feb 10 nicklas 7   Available at http://base.thep.lu.se/
5227 01 Feb 10 nicklas 8
5227 01 Feb 10 nicklas 9   BASE is free software; you can redistribute it and/or
5227 01 Feb 10 nicklas 10   modify it under the terms of the GNU General Public License
5227 01 Feb 10 nicklas 11   as published by the Free Software Foundation; either version 3
5227 01 Feb 10 nicklas 12   of the License, or (at your option) any later version.
5227 01 Feb 10 nicklas 13
5227 01 Feb 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5227 01 Feb 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5227 01 Feb 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5227 01 Feb 10 nicklas 17   GNU General Public License for more details.
5227 01 Feb 10 nicklas 18
5227 01 Feb 10 nicklas 19   You should have received a copy of the GNU General Public License
5227 01 Feb 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5227 01 Feb 10 nicklas 21 */
5227 01 Feb 10 nicklas 22 package net.sf.basedb.util.importer.spotdata;
5227 01 Feb 10 nicklas 23
5227 01 Feb 10 nicklas 24 import net.sf.basedb.core.BioAssay;
5227 01 Feb 10 nicklas 25 import net.sf.basedb.core.SpotBatcher;
5227 01 Feb 10 nicklas 26 import net.sf.basedb.util.Values;
5227 01 Feb 10 nicklas 27 import net.sf.basedb.util.bfs.BfsParser;
5227 01 Feb 10 nicklas 28 import net.sf.basedb.util.bfs.DataParser;
5227 01 Feb 10 nicklas 29 import net.sf.basedb.util.bfs.EventHandler;
5227 01 Feb 10 nicklas 30 import net.sf.basedb.util.bfs.EventType;
5227 01 Feb 10 nicklas 31 import net.sf.basedb.util.importer.spotdata.SynchronizedSpotDataParser.SynchronizedData;
5227 01 Feb 10 nicklas 32
5227 01 Feb 10 nicklas 33 /**
5227 01 Feb 10 nicklas 34   Event handler that extracts spot intensity information from matrix BFS
5227 01 Feb 10 nicklas 35   data files and inserts the values to a spot batcher. In the matrix format 
5227 01 Feb 10 nicklas 36   there is one data file per channel. In each file there should be one data 
5227 01 Feb 10 nicklas 37   column for each assay. All data files should have the same 
5227 01 Feb 10 nicklas 38   column layout and the same number of lines. For example, file1.txt 
5227 01 Feb 10 nicklas 39   contains data for channel 1 and file2.txt contains data for channel 2.
5227 01 Feb 10 nicklas 40   In both files the first column (index=0) has intensity for assay #1
5227 01 Feb 10 nicklas 41   and the second column (index=1) has intensity for assay #2.
5227 01 Feb 10 nicklas 42   <p>
5227 01 Feb 10 nicklas 43   This event handler is designed to work together with a {@link SynchronizedSpotDataParser}
5227 01 Feb 10 nicklas 44   and will only react to {@link SynchronizedSpotDataParser#DATA_EVENT} events.
5227 01 Feb 10 nicklas 45   
5227 01 Feb 10 nicklas 46   @author Nicklas
5227 01 Feb 10 nicklas 47   @version 2.15
5227 01 Feb 10 nicklas 48   @base.modified $Date$
5227 01 Feb 10 nicklas 49 */
5227 01 Feb 10 nicklas 50 public class MatrixSpotIntensityEventHandler
5227 01 Feb 10 nicklas 51   implements EventHandler
5227 01 Feb 10 nicklas 52 {
5227 01 Feb 10 nicklas 53
5227 01 Feb 10 nicklas 54   private final SpotBatcher spotBatcher;
5227 01 Feb 10 nicklas 55   private final PositionEventHandler positionHandler;
5227 01 Feb 10 nicklas 56   private final BioAssay[] assays;
5227 01 Feb 10 nicklas 57   private final int[] chIndex;
5227 01 Feb 10 nicklas 58   private float[] ch;
5227 01 Feb 10 nicklas 59   
5227 01 Feb 10 nicklas 60   /**
5227 01 Feb 10 nicklas 61     Create a new spot instensity event handler.
5227 01 Feb 10 nicklas 62     @param spotBatcher The spot batcher that should receive the intensity data
5227 01 Feb 10 nicklas 63     @param positionHandler The event handler that is responsible for extracting
5227 01 Feb 10 nicklas 64       position information from the reporter annotations file
5227 01 Feb 10 nicklas 65     @param assays An array with all assays in the same order that they
5227 01 Feb 10 nicklas 66       appeared in the pdata file
5227 01 Feb 10 nicklas 67     @param chIndex An array with file indexes for channel intensities,
5227 01 Feb 10 nicklas 68       first value is the file index for channel 1. The first file has
5227 01 Feb 10 nicklas 69       index = 0. The size of this array should match the number of channels
5227 01 Feb 10 nicklas 70       in the experiment.
5227 01 Feb 10 nicklas 71   */
5227 01 Feb 10 nicklas 72   public MatrixSpotIntensityEventHandler(SpotBatcher spotBatcher, 
5227 01 Feb 10 nicklas 73     PositionEventHandler positionHandler, BioAssay[] assays, int[] chIndex)
5227 01 Feb 10 nicklas 74   {
5227 01 Feb 10 nicklas 75     if (spotBatcher == null) throw new NullPointerException("spotBatcher");
5227 01 Feb 10 nicklas 76     if (positionHandler == null) throw new NullPointerException("positionHandler");
5227 01 Feb 10 nicklas 77     if (assays == null) throw new NullPointerException("assays");
5227 01 Feb 10 nicklas 78     if (chIndex == null) throw new NullPointerException("chIndex");
5227 01 Feb 10 nicklas 79     this.spotBatcher = spotBatcher;
5227 01 Feb 10 nicklas 80     this.positionHandler = positionHandler;
5227 01 Feb 10 nicklas 81     this.assays = assays;
5227 01 Feb 10 nicklas 82     this.chIndex = chIndex;
5227 01 Feb 10 nicklas 83     this.ch = new float[chIndex.length];
5227 01 Feb 10 nicklas 84   }
5227 01 Feb 10 nicklas 85   
5227 01 Feb 10 nicklas 86   /**
5227 01 Feb 10 nicklas 87     Extract the spot intensity values from the data files and batch insert
5227 01 Feb 10 nicklas 88     them using the spot batcher. If all channels are empty, no data will be
5227 01 Feb 10 nicklas 89     inserted for that spot, otherwise a null value will be inserted for
5227 01 Feb 10 nicklas 90     the missing channels. The parser will treat invalid values as
5227 01 Feb 10 nicklas 91     empty columns.
5227 01 Feb 10 nicklas 92   */
6875 20 Apr 15 nicklas 93   @SuppressWarnings("rawtypes")
5227 01 Feb 10 nicklas 94   @Override
5227 01 Feb 10 nicklas 95   public void handleEvent(EventType eventType, Object eventData, BfsParser parser)
5227 01 Feb 10 nicklas 96   {
5227 01 Feb 10 nicklas 97     if (eventType == SynchronizedSpotDataParser.DATA_EVENT)
5227 01 Feb 10 nicklas 98     {
5227 01 Feb 10 nicklas 99       SynchronizedData[] data = (SynchronizedData[])eventData;
5227 01 Feb 10 nicklas 100       int position = positionHandler.getCurrentPosition();
5227 01 Feb 10 nicklas 101       for (int i = 0; i < assays.length; ++i)
5227 01 Feb 10 nicklas 102       {
5227 01 Feb 10 nicklas 103         boolean allNaN = true;
5227 01 Feb 10 nicklas 104         for (int j = 0; j < ch.length; ++j)
5227 01 Feb 10 nicklas 105         {
5227 01 Feb 10 nicklas 106           SynchronizedData assayData = data[chIndex[j]+1];
5227 01 Feb 10 nicklas 107           ch[j] = Values.getFloat(assayData.getData()[i], Float.NaN);
5227 01 Feb 10 nicklas 108           allNaN &= Float.isNaN(ch[j]);
5227 01 Feb 10 nicklas 109         }
5227 01 Feb 10 nicklas 110         if (!allNaN) 
5227 01 Feb 10 nicklas 111         {
5227 01 Feb 10 nicklas 112           spotBatcher.insert(assays[i].getDataCubeColumnNo(), position, ch);
5227 01 Feb 10 nicklas 113         }
5227 01 Feb 10 nicklas 114       }
5227 01 Feb 10 nicklas 115     }
5227 01 Feb 10 nicklas 116     else if (eventType == DataParser.END_OF_FILE_EVENT)
5227 01 Feb 10 nicklas 117     {
5227 01 Feb 10 nicklas 118       spotBatcher.flush();
5227 01 Feb 10 nicklas 119     }
5227 01 Feb 10 nicklas 120   }
5227 01 Feb 10 nicklas 121
5227 01 Feb 10 nicklas 122 }