Package Bio :: Package Affy :: Module CelFile
[hide private]
[frames] | no frames]

Source Code for Module Bio.Affy.CelFile

  1  # Copyright 2004 by Harry Zuzan.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  """ 
  7  Classes for accessing the information in Affymetrix cel files. 
  8   
  9  Functions: 
 10  read      Read a cel file and store its contents in a Record 
 11   
 12  Classes: 
 13  Record    Contains the information from a cel file 
 14   
 15   
 16  The following classes are DEPRECATED: 
 17   
 18  class CelParser: parses cel files 
 19  class CelRecord: stores the information from a cel file 
 20   
 21  """ 
 22   
 23  import numpy 
 24   
25 -class Record(object):
26 """ 27 Stores the information in a cel file 28 """
29 - def __init__(self):
30 self.intensities = None 31 self.stdevs = None 32 self.npix = None 33 self.nrows = None 34 self.ncols = None
35 36
37 -def read(handle):
38 """ 39 Read the information in a cel file, and store it in a Record. 40 """ 41 # Needs error handling. 42 # Needs to know the chip design. 43 record = Record() 44 section = "" 45 for line in handle: 46 if not line.strip(): 47 continue 48 if line[:8]=="[HEADER]": 49 section = "HEADER" 50 elif line[:11]=="[INTENSITY]": 51 section = "INTENSITY" 52 record.intensities = numpy.zeros((record.nrows, record.ncols)) 53 record.stdevs = numpy.zeros((record.nrows, record.ncols)) 54 record.npix = numpy.zeros((record.nrows, record.ncols), int) 55 elif line[0]=="[": 56 section = "" 57 elif section=="HEADER": 58 keyword, value = line.split("=", 1) 59 if keyword=="Cols": 60 record.ncols = int(value) 61 elif keyword=="Rows": 62 record.nrows = int(value) 63 elif section=="INTENSITY": 64 if "=" in line: 65 continue 66 words = line.split() 67 y, x = map(int, words[:2]) 68 record.intensities[x,y] = float(words[2]) 69 record.stdevs[x,y] = float(words[3]) 70 record.npix[x,y] = int(words[4]) 71 return record
72 73 74 # Everything below is considered deprecated 75 76 from Bio.ParserSupport import AbstractConsumer 77 from numpy import * 78
79 -class CelScanner(object):
80 """Scanner for Affymetrix CEL files (DEPRECATED) 81 82 Methods: 83 feed Feed data into the scanner. 84 85 The scanner generates (and calls the consumer) the following 86 types of events: 87 88 Rows - the number of rows on the microarray 89 Cols - the number of columns on the microarray 90 StartIntensity - generated when the section [INTENSITY] is found 91 ReadIntensity - one line in the section [INTENSITY] 92 93 This class is DEPRECATED; please use the read() function in this module. 94 """
95 - def __init__(self):
96 import warnings 97 import Bio 98 warnings.warn("Bio.Affy.CelFile.CelScanner is deprecated; please use the read() function in this module instead", 99 Bio.BiopythonDeprecationWarning)
100
101 - def feed(self, handle, consumer):
102 """scanner.feed(handle, consumer) 103 104 Feed in a handle to a Cel file for scanning. handle is a file-like 105 object that contains the Cel file. consumer is a Consumer 106 object that will receive events as the report is scanned. 107 """ 108 section = "" 109 for line in handle: 110 if line.strip()=="": continue 111 if line[0]=="[": 112 section = "" 113 if line[:8]=="[HEADER]": 114 section = "HEADER" 115 elif line[:11]=="[INTENSITY]": 116 section = "INTENSITY" 117 consumer.StartIntensity() 118 continue 119 if section=="HEADER": 120 keyword, value = line.split("=", 1) 121 if keyword=="Cols": consumer.Cols(value) 122 if keyword=="Rows": consumer.Rows(value) 123 continue 124 elif section=="INTENSITY": 125 if "=" in line: continue 126 consumer.ReadIntensity(line)
127 128
129 -class CelConsumer(AbstractConsumer):
130 """Consumer for Affymetrix CEL files (DEPRECATED) 131 132 This class is DEPRECATED; please use the read() function in this module. 133 """ 134
135 - def __init__(self):
136 import warnings 137 import Bio 138 warnings.warn("Bio.Affy.CelFile.CelConsumer is deprecated; please use the read() function in this module instead", 139 Bio.BiopythonDeprecationWarning) 140 141 self._mean = None 142 self._stdev = None 143 self._npix = None
144
145 - def Cols(self, value):
146 self._cols = int(value)
147
148 - def Rows(self, value):
149 self._rows = int(value)
150
151 - def StartIntensity(self):
152 self._mean = zeros((self._rows, self._cols)) 153 self._stdev = zeros((self._rows, self._cols)) 154 self._npix = zeros((self._rows, self._cols), int)
155
156 - def ReadIntensity(self, line):
157 y, x, mean, stdev, npix = map(float, line.split()) 158 x = int(x) 159 y = int(y) 160 self._mean[x,y] = mean 161 self._stdev[x,y] = stdev 162 self._npix[x,y] = int(npix)
163
164 -class CelRecord(object):
165 """ 166 Stores the information in a cel file (DEPRECATED). 167 168 Needs error handling. 169 Needs to know the chip design. 170 171 This class is DEPRECATED; please use the Record class instead. 172 """ 173 174
175 - def __init__(self, data_dict):
176 """ 177 Pass the data attributes as a dictionary. 178 """ 179 import warnings 180 import Bio 181 warnings.warn("Bio.Affy.CelFile.CelRecord is deprecated; please use the read() function in this module instead", 182 Bio.BiopythonDeprecationWarning) 183 184 from copy import deepcopy as dcopy 185 186 self._intensities = dcopy(data_dict['intensities']) 187 self._stdevs = dcopy(data_dict['stdevs']) 188 self._npix = dcopy(data_dict['npix']) 189 190 self._nrows, self._ncols = self._intensities.shape
191 192
193 - def intensities(self):
194 """ 195 Return a two dimensional array of probe cell intensities. 196 Dimension 1 -> rows 197 Dimension 2 -> columns 198 """ 199 return self._intensities
200 201
202 - def stdevs(self):
203 """ 204 Return a two dimensional array of probe cell standard deviations. 205 Dimension 1 -> rows 206 Dimension 2 -> columns 207 """ 208 return self._stdevs
209 210
211 - def npix(self):
212 """ 213 Return a two dimensional array of the number of pixels in a probe cell. 214 Dimension 1 -> rows 215 Dimension 2 -> columns 216 """ 217 return self._npix
218 219
220 - def nrows(self):
221 """ 222 The number of rows of probe cells in an array. 223 """ 224 return self._nrows
225
226 - def ncols(self):
227 """ 228 The number of columns of probe cells in an array. 229 """ 230 return self._ncols
231
232 - def size(self):
233 """ 234 The size of the probe cell array as a tuple (nrows,ncols). 235 """ 236 return self._nrows, self._ncols
237 238 239
240 -class CelParser(object):
241 """ 242 Takes a handle to an Affymetrix cel file, parses the file and 243 returns an instance of a CelRecord 244 245 This class needs error handling. 246 247 This class is DEPRECATED; please use the read() function in this module 248 instead. 249 """ 250
251 - def __init__(self, handle=None):
252 """ 253 Usually load the class with the cel file (not file name) as 254 an argument. 255 """ 256 import warnings 257 import Bio 258 warnings.warn("Bio.Affy.CelFile.CelParser is deprecated; please use the read() function in this module instead", 259 Bio.BiopythonDeprecationWarning) 260 261 self._intensities = None 262 self._stdevs = None 263 self._npix = None 264 265 if handle is not None: self.parse(handle)
266 267
268 - def parse(self, handle):
269 """ 270 Takes a handle to a cel file, parses it 271 and stores it in the three arrays. 272 273 There is more information in the cel file that could be retrieved 274 and stored in CelRecord. The chip type should be a priority. 275 """ 276 277 # (self._intensities, self._stdevs, self._npix) = _cel.parse(data) 278 scanner = CelScanner() 279 consumer = CelConsumer() 280 scanner.feed(handle, consumer) 281 self._intensities = consumer._mean 282 self._stdevs = consumer._stdev 283 self._npix = consumer._npix 284 self._nrows = self._intensities.shape[0] 285 self._ncols = self._intensities.shape[1]
286 287
288 - def __call__(self):
289 """ 290 Returns the parsed data as a CelRecord. 291 """ 292 293 record_dict = {} 294 record_dict['intensities'] = self._intensities 295 record_dict['stdevs'] = self._stdevs 296 record_dict['npix'] = self._npix 297 298 return CelRecord(record_dict)
299