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