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 obsolete: 
 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:
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 obsolete 75 76 from Bio.ParserSupport import AbstractConsumer 77 from numpy import * 78
79 -class CelScanner:
80 """Scanner for Affymetrix CEL files (OBSOLETE) 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 OBSOLETE; please use the read() function in this module. 94 """
95 - def feed(self, handle, consumer):
96 """scanner.feed(handle, consumer) 97 98 Feed in a handle to a Cel file for scanning. handle is a file-like 99 object that contains the Cel file. consumer is a Consumer 100 object that will receive events as the report is scanned. 101 """ 102 section = "" 103 for line in handle: 104 if line.strip()=="": continue 105 if line[0]=="[": 106 section = "" 107 if line[:8]=="[HEADER]": 108 section = "HEADER" 109 elif line[:11]=="[INTENSITY]": 110 section = "INTENSITY" 111 consumer.StartIntensity() 112 continue 113 if section=="HEADER": 114 keyword, value = line.split("=", 1) 115 if keyword=="Cols": consumer.Cols(value) 116 if keyword=="Rows": consumer.Rows(value) 117 continue 118 elif section=="INTENSITY": 119 if "=" in line: continue 120 consumer.ReadIntensity(line)
121 122
123 -class CelConsumer(AbstractConsumer):
124 """Consumer for Affymetrix CEL files (OBSOLETE) 125 126 This class is OBSOLETE; please use the read() function in this module. 127 """ 128
129 - def __init__(self):
130 self._mean = None 131 self._stdev = None 132 self._npix = None
133
134 - def Cols(self, value):
135 self._cols = int(value)
136
137 - def Rows(self, value):
138 self._rows = int(value)
139
140 - def StartIntensity(self):
141 self._mean = zeros((self._rows, self._cols)) 142 self._stdev = zeros((self._rows, self._cols)) 143 self._npix = zeros((self._rows, self._cols), int)
144
145 - def ReadIntensity(self, line):
146 y, x, mean, stdev, npix = map(float, line.split()) 147 x = int(x) 148 y = int(y) 149 self._mean[x,y] = mean 150 self._stdev[x,y] = stdev 151 self._npix[x,y] = int(npix)
152
153 -class CelRecord:
154 """ 155 Stores the information in a cel file (OBSOLETE). 156 157 Needs error handling. 158 Needs to know the chip design. 159 160 This class is OBSOLETE; please use the Record class instead. 161 """ 162 163
164 - def __init__(self, data_dict):
165 """ 166 Pass the data attributes as a dictionary. 167 """ 168 from copy import deepcopy as dcopy 169 170 self._intensities = dcopy(data_dict['intensities']) 171 self._stdevs = dcopy(data_dict['stdevs']) 172 self._npix = dcopy(data_dict['npix']) 173 174 self._nrows, self._ncols = self._intensities.shape
175 176
177 - def intensities(self):
178 """ 179 Return a two dimensional array of probe cell intensities. 180 Dimension 1 -> rows 181 Dimension 2 -> columns 182 """ 183 return self._intensities
184 185
186 - def stdevs(self):
187 """ 188 Return a two dimensional array of probe cell standard deviations. 189 Dimension 1 -> rows 190 Dimension 2 -> columns 191 """ 192 return self._stdevs
193 194
195 - def npix(self):
196 """ 197 Return a two dimensional array of the number of pixels in a probe cell. 198 Dimension 1 -> rows 199 Dimension 2 -> columns 200 """ 201 return self._npix
202 203
204 - def nrows(self):
205 """ 206 The number of rows of probe cells in an array. 207 """ 208 return self._nrows
209
210 - def ncols(self):
211 """ 212 The number of columns of probe cells in an array. 213 """ 214 return self._ncols
215
216 - def size(self):
217 """ 218 The size of the probe cell array as a tuple (nrows,ncols). 219 """ 220 return self._nrows, self._ncols
221 222 223
224 -class CelParser:
225 """ 226 Takes a handle to an Affymetrix cel file, parses the file and 227 returns an instance of a CelRecord 228 229 This class needs error handling. 230 231 This class is OBSOLETE; please use the read() function in this module 232 instead. 233 """ 234
235 - def __init__(self, handle=None):
236 """ 237 Usually load the class with the cel file (not file name) as 238 an argument. 239 """ 240 241 self._intensities = None 242 self._stdevs = None 243 self._npix = None 244 245 if handle is not None: self.parse(handle)
246 247
248 - def parse(self, handle):
249 """ 250 Takes a handle to a cel file, parses it 251 and stores it in the three arrays. 252 253 There is more information in the cel file that could be retrieved 254 and stored in CelRecord. The chip type should be a priority. 255 """ 256 257 # (self._intensities, self._stdevs, self._npix) = _cel.parse(data) 258 scanner = CelScanner() 259 consumer = CelConsumer() 260 scanner.feed(handle, consumer) 261 self._intensities = consumer._mean 262 self._stdevs = consumer._stdev 263 self._npix = consumer._npix 264 self._nrows = self._intensities.shape[0] 265 self._ncols = self._intensities.shape[1]
266 267
268 - def __call__(self):
269 """ 270 Returns the parsed data as a CelRecord. 271 """ 272 273 record_dict = {} 274 record_dict['intensities'] = self._intensities 275 record_dict['stdevs'] = self._stdevs 276 record_dict['npix'] = self._npix 277 278 return CelRecord(record_dict)
279