src/core/net/sf/basedb/util/affymetrix/CelValidator.java

Code
Comments
Other
Rev Date Author Line
5623 06 May 11 nicklas 1 /**
5623 06 May 11 nicklas 2   $Id$
5623 06 May 11 nicklas 3
5623 06 May 11 nicklas 4   Copyright (C) 2011 Nicklas Nordborg
5623 06 May 11 nicklas 5
5623 06 May 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5623 06 May 11 nicklas 7   Available at http://base.thep.lu.se/
5623 06 May 11 nicklas 8
5623 06 May 11 nicklas 9   BASE is free software; you can redistribute it and/or
5623 06 May 11 nicklas 10   modify it under the terms of the GNU General Public License
5623 06 May 11 nicklas 11   as published by the Free Software Foundation; either version 3
5623 06 May 11 nicklas 12   of the License, or (at your option) any later version.
5623 06 May 11 nicklas 13
5623 06 May 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5623 06 May 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5623 06 May 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5623 06 May 11 nicklas 17   GNU General Public License for more details.
5623 06 May 11 nicklas 18
5623 06 May 11 nicklas 19   You should have received a copy of the GNU General Public License
5623 06 May 11 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5623 06 May 11 nicklas 21 */
5623 06 May 11 nicklas 22 package net.sf.basedb.util.affymetrix;
5623 06 May 11 nicklas 23
5623 06 May 11 nicklas 24 import affymetrix.fusion.cdf.FusionCDFData;
5623 06 May 11 nicklas 25 import affymetrix.fusion.cel.FusionCELData;
5623 06 May 11 nicklas 26 import net.sf.basedb.core.File;
5623 06 May 11 nicklas 27 import net.sf.basedb.core.InvalidDataException;
5623 06 May 11 nicklas 28 import net.sf.basedb.core.InvalidUseOfNullException;
5623 06 May 11 nicklas 29 import net.sf.basedb.core.ItemNotFoundException;
5623 06 May 11 nicklas 30 import net.sf.basedb.core.RawBioAssay;
5623 06 May 11 nicklas 31 import net.sf.basedb.core.StringUtil;
5623 06 May 11 nicklas 32
5623 06 May 11 nicklas 33 /**
5623 06 May 11 nicklas 34   Helper class for working with Affymetrix CEL files.
5623 06 May 11 nicklas 35
5623 06 May 11 nicklas 36   @author Nicklas
5623 06 May 11 nicklas 37   @since 3.0
5623 06 May 11 nicklas 38   @base.modified $Date$
5623 06 May 11 nicklas 39 */
5623 06 May 11 nicklas 40 public class CelValidator
5623 06 May 11 nicklas 41 {
5623 06 May 11 nicklas 42
5623 06 May 11 nicklas 43   /**
5623 06 May 11 nicklas 44     Create a new instance.
5623 06 May 11 nicklas 45   */
5623 06 May 11 nicklas 46   public CelValidator()
5623 06 May 11 nicklas 47   {}
5623 06 May 11 nicklas 48
5623 06 May 11 nicklas 49   private void setHeader(RawBioAssay rba, String name, String value)
5623 06 May 11 nicklas 50   {
5623 06 May 11 nicklas 51     if (name == null || value == null) return;
5623 06 May 11 nicklas 52     value = StringUtil.trimString(value, RawBioAssay.MAX_HEADER_VALUE_LENGTH);
5623 06 May 11 nicklas 53     rba.setHeader(name, value);
5623 06 May 11 nicklas 54   }
5623 06 May 11 nicklas 55
5623 06 May 11 nicklas 56   /**
5623 06 May 11 nicklas 57     Load a CEL file using the Affymetric Fusion SDK. This method
5623 06 May 11 nicklas 58     checks that the file exists, and read all headers.
5623 06 May 11 nicklas 59     
5623 06 May 11 nicklas 60     @param celFile The file to load
5623 06 May 11 nicklas 61     @return A FusionCELData object representing the CEL file
5623 06 May 11 nicklas 62     @throws ItemNotFoundException If the actual file is not on the server
5623 06 May 11 nicklas 63     @throws InvalidDataException If the file is not a CEL file
5623 06 May 11 nicklas 64   */
5623 06 May 11 nicklas 65   public FusionCELData loadCelFile(File celFile)
5623 06 May 11 nicklas 66   {
5623 06 May 11 nicklas 67     if (celFile == null) throw new InvalidUseOfNullException("celFile");
5623 06 May 11 nicklas 68     if (!celFile.getLocation().isDownloadable())
5623 06 May 11 nicklas 69     {
5623 06 May 11 nicklas 70       throw new ItemNotFoundException("Can't download file data for file '" + 
5623 06 May 11 nicklas 71           celFile.getName() + "'; location=" + celFile.getLocation());
5623 06 May 11 nicklas 72     }
5623 06 May 11 nicklas 73     FusionCELData cel = new FusionCELData();
5623 06 May 11 nicklas 74     cel.setFileName(celFile.getName());
5623 06 May 11 nicklas 75     cel.setInputStream(celFile.getDownloadStream(0));
5623 06 May 11 nicklas 76     if (!cel.readHeader())
5623 06 May 11 nicklas 77     {
5623 06 May 11 nicklas 78       throw new InvalidDataException("Could not read CEL file '" +
5623 06 May 11 nicklas 79           celFile.getPath()+"': " + cel.getError());
5623 06 May 11 nicklas 80     }
5623 06 May 11 nicklas 81     return cel;
5623 06 May 11 nicklas 82   }
5623 06 May 11 nicklas 83
5623 06 May 11 nicklas 84   /**
5623 06 May 11 nicklas 85     Copy metadata from the CEL file to the raw bioassay.
5623 06 May 11 nicklas 86     This will set the mumber of spots and headers from the
5623 06 May 11 nicklas 87     file.
5623 06 May 11 nicklas 88     
5623 06 May 11 nicklas 89     @param cel The CEL file
5623 06 May 11 nicklas 90     @param rba The raw bioassay
5623 06 May 11 nicklas 91   */
5623 06 May 11 nicklas 92   public void copyMetadata(FusionCELData cel, RawBioAssay rba)
5623 06 May 11 nicklas 93   {
5623 06 May 11 nicklas 94     rba.setNumFileSpots(cel.getCells());
5623 06 May 11 nicklas 95     
5623 06 May 11 nicklas 96     setHeader(rba, "Algorithm", cel.getAlg());
5623 06 May 11 nicklas 97     for (int i = 0; i < cel.getNumberAlgorithmParameters(); ++i)
5623 06 May 11 nicklas 98     {
5623 06 May 11 nicklas 99       String name = cel.getAlgorithmParameterTag(i);
5623 06 May 11 nicklas 100       setHeader(rba, "Algorithm parameter: " + name, 
5623 06 May 11 nicklas 101           cel.getAlgorithmParameter(name));
5623 06 May 11 nicklas 102     }
5623 06 May 11 nicklas 103     setHeader(rba, "Dat header", cel.getDatHeader());
5623 06 May 11 nicklas 104     setHeader(rba, "Chip type", cel.getChipType());
5623 06 May 11 nicklas 105   }
5623 06 May 11 nicklas 106   
5623 06 May 11 nicklas 107   /**
5623 06 May 11 nicklas 108     Reset metadata on the given raw bioassay. This set the
5623 06 May 11 nicklas 109     number of spots to 0 and remove all headers.
5623 06 May 11 nicklas 110     @param rba The raw bioassay
5623 06 May 11 nicklas 111   */
5623 06 May 11 nicklas 112   public void resetMetadata(RawBioAssay rba)
5623 06 May 11 nicklas 113   {
5623 06 May 11 nicklas 114     rba.setNumFileSpots(0);
5623 06 May 11 nicklas 115     rba.removeHeaders();
5623 06 May 11 nicklas 116   }
5623 06 May 11 nicklas 117   
5623 06 May 11 nicklas 118   /**
5623 06 May 11 nicklas 119     Check if the loaded CEL and CDF files matches. The chip type is
5623 06 May 11 nicklas 120     verified. Unfortunately the chip type is not stored inside the CDF
5623 06 May 11 nicklas 121     file, but has to be given as an extra parameter. Typically, this
5623 06 May 11 nicklas 122     should be the name of the file. This method also verify that the number 
5623 06 May 11 nicklas 123     rows and columns in the two files matches.
5623 06 May 11 nicklas 124     
5623 06 May 11 nicklas 125     @param cel The CEL fila data
5623 06 May 11 nicklas 126     @param cdf The CDF file data
5623 06 May 11 nicklas 127     @param cdfChipType The CDF chip type
5623 06 May 11 nicklas 128   */
5623 06 May 11 nicklas 129   public void validateCelAndCdf(FusionCELData cel, FusionCDFData cdf, String cdfChipType)
5623 06 May 11 nicklas 130     throws InvalidDataException
5623 06 May 11 nicklas 131   {
5623 06 May 11 nicklas 132     String celChipType = cel.getChipType();
5623 06 May 11 nicklas 133     if (!cdfChipType.startsWith(celChipType))
5623 06 May 11 nicklas 134     {
5623 06 May 11 nicklas 135       throw new InvalidDataException("CEL chip type (" + celChipType + 
5623 06 May 11 nicklas 136         ") doesn't match CDF chip type (" + cdfChipType + ")");
5623 06 May 11 nicklas 137     }
5623 06 May 11 nicklas 138     
5623 06 May 11 nicklas 139     if (cel.getRows() != cdf.getHeader().getRows() || cel.getCols() != cdf.getHeader().getRows())
5623 06 May 11 nicklas 140     {
5623 06 May 11 nicklas 141       throw new InvalidDataException("CEL size (rows=" + cel.getRows() + ", cols=" + cel.getCols() +
5623 06 May 11 nicklas 142         ") doesn't match CDF size (rows=" + 
5623 06 May 11 nicklas 143         cdf.getHeader().getRows() + ", cols=" + cdf.getHeader().getCols()+")");
5623 06 May 11 nicklas 144     }    
5623 06 May 11 nicklas 145   }
5623 06 May 11 nicklas 146
5623 06 May 11 nicklas 147   
5623 06 May 11 nicklas 148   
5623 06 May 11 nicklas 149 }