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  No version number yet. 
  8   
  9  Classes for accessing the information in Affymetrix cel files. 
 10   
 11  class CelParser: parses cel files 
 12  class CelRecord: stores the information from a cel file 
 13   
 14  """ 
 15   
 16  # import _cel 
 17   
 18  from Bio.ParserSupport import AbstractConsumer 
 19  from Numeric import * 
 20   
21 -class CelScanner:
22 """Scannner for Affymetrix CEL files. 23 24 Methods: 25 feed Feed data into the scanner. 26 27 The scanner generates (and calls the consumer) the following 28 types of events: 29 30 Rows - the number of rows on the microarray 31 Cols - the number of columns on the microarray 32 StartIntensity - generated when the section [INTENSITY] is found 33 ReadIntensity - one line in the section [INTENSITY] 34 35 """
36 - def feed(self, handle, consumer):
37 """scanner.feed(handle, consumer) 38 39 Feed in a handle to a Cel file for scanning. handle is a file-like 40 object that contains the Cel file. consumer is a Consumer 41 object that will receive events as the report is scanned. 42 """ 43 section = "" 44 for line in handle: 45 if line.strip()=="": continue 46 if line[0]=="[": 47 section = "" 48 if line[:8]=="[HEADER]": 49 section = "HEADER" 50 elif line[:11]=="[INTENSITY]": 51 section = "INTENSITY" 52 consumer.StartIntensity() 53 continue 54 if section=="HEADER": 55 keyword, value = line.split("=", 1) 56 if keyword=="Cols": consumer.Cols(value) 57 if keyword=="Rows": consumer.Rows(value) 58 continue 59 elif section=="INTENSITY": 60 if "=" in line: continue 61 consumer.ReadIntensity(line)
62 63
64 -class CelConsumer(AbstractConsumer):
65
66 - def __init__(self):
67 self._mean = None 68 self._stdev = None 69 self._npix = None
70
71 - def Cols(self, value):
72 self._cols = int(value)
73
74 - def Rows(self, value):
75 self._rows = int(value)
76
77 - def StartIntensity(self):
78 self._mean = zeros((self._rows, self._cols), Float) 79 self._stdev = zeros((self._rows, self._cols), Float) 80 self._npix = zeros((self._rows, self._cols), Int)
81
82 - def ReadIntensity(self, line):
83 y, x, mean, stdev, npix = map(float, line.split()) 84 x = int(x) 85 y = int(y) 86 self._mean[x,y] = mean 87 self._stdev[x,y] = stdev 88 self._npix[x,y] = int(npix)
89
90 -class CelRecord:
91 """ 92 Stores the information in a cel file 93 94 Needs error handling. 95 Needs to know the chip design. 96 """ 97 98
99 - def __init__(self, data_dict):
100 """ 101 Pass the data attributes as a dictionary. 102 """ 103 from copy import deepcopy as dcopy 104 105 self._intensities = dcopy(data_dict['intensities']) 106 self._stdevs = dcopy(data_dict['stdevs']) 107 self._npix = dcopy(data_dict['npix']) 108 109 self._nrows, self._ncols = self._intensities.shape
110 111
112 - def intensities(self):
113 """ 114 Return a two dimensional array of probe cell intensities. 115 Dimension 1 -> rows 116 Dimension 2 -> columns 117 """ 118 return self._intensities
119 120
121 - def stdevs(self):
122 """ 123 Return a two dimensional array of probe cell standard deviations. 124 Dimension 1 -> rows 125 Dimension 2 -> columns 126 """ 127 return self._stdevs
128 129
130 - def npix(self):
131 """ 132 Return a two dimensional array of the number of pixels in a probe cell. 133 Dimension 1 -> rows 134 Dimension 2 -> columns 135 """ 136 return self._npix
137 138
139 - def nrows(self):
140 """ 141 The number of rows of probe cells in an array. 142 """ 143 return self._nrows
144
145 - def ncols(self):
146 """ 147 The number of columns of probe cells in an array. 148 """ 149 return self._ncols
150
151 - def size(self):
152 """ 153 The size of the probe cell array as a tuple (nrows,ncols). 154 """ 155 return self._nrows, self._ncols
156 157 158
159 -class CelParser:
160 """ 161 Takes a handle to an Affymetrix cel file, parses the file and 162 returns an instance of a CelRecord 163 164 This class needs error handling. 165 """ 166
167 - def __init__(self, handle=None):
168 """ 169 Usually load the class with the cel file (not file name) as 170 an argument. 171 """ 172 173 self._intensities = None 174 self._stdevs = None 175 self._npix = None 176 177 if handle is not None: self.parse(handle)
178 179
180 - def parse(self, handle):
181 """ 182 Takes a handle to a cel file, parses it 183 and stores it in the three arrays. 184 185 There is more information in the cel file that could be retrieved 186 and stored in CelRecord. The chip type should be a priority. 187 """ 188 189 # (self._intensities, self._stdevs, self._npix) = _cel.parse(data) 190 scanner = CelScanner() 191 consumer = CelConsumer() 192 scanner.feed(handle, consumer) 193 self._intensities = consumer._mean 194 self._stdevs = consumer._stdev 195 self._npix = consumer._npix 196 self._nrows = self._intensities.shape[0] 197 self._ncols = self._intensities.shape[1]
198 199
200 - def __call__(self):
201 """ 202 Returns the parsed data as a CelRecord. 203 """ 204 205 record_dict = {} 206 record_dict['intensities'] = self._intensities 207 record_dict['stdevs'] = self._stdevs 208 record_dict['npix'] = self._npix 209 210 return CelRecord(record_dict)
211