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  # We use print in the doctests 
 17  from __future__ import print_function 
 18   
 19  try: 
 20      import numpy 
 21  except ImportError: 
 22      from Bio import MissingPythonDependencyError 
 23      raise MissingPythonDependencyError( 
 24          "Install NumPy if you want to use Bio.Affy.CelFile") 
 25   
 26  __docformat__ = "restructuredtext en" 
 27   
 28   
29 -class Record(object):
30 """Stores the information in a cel file 31 32 Example usage: 33 34 >>> from Bio.Affy import CelFile 35 >>> with open('Affy/affy_v3_example.CEL') as handle: 36 ... c = CelFile.read(handle) 37 ... 38 >>> print(c.ncols, c.nrows) 39 5 5 40 >>> print(c.intensities) 41 [[ 234. 170. 22177. 164. 22104.] 42 [ 188. 188. 21871. 168. 21883.] 43 [ 188. 193. 21455. 198. 21300.] 44 [ 188. 182. 21438. 188. 20945.] 45 [ 193. 20370. 174. 20605. 168.]] 46 >>> print(c.stdevs) 47 [[ 24. 34.5 2669. 19.7 3661.2] 48 [ 29.8 29.8 2795.9 67.9 2792.4] 49 [ 29.8 88.7 2976.5 62. 2914.5] 50 [ 29.8 76.2 2759.5 49.2 2762. ] 51 [ 38.8 2611.8 26.6 2810.7 24.1]] 52 >>> print(c.npix) 53 [[25 25 25 25 25] 54 [25 25 25 25 25] 55 [25 25 25 25 25] 56 [25 25 25 25 25] 57 [25 25 25 25 25]] 58 59 """
60 - def __init__(self):
61 self.version = None 62 self.GridCornerUL = None 63 self.GridCornerUR = None 64 self.GridCornerLR = None 65 self.GridCornerLL = None 66 self.DatHeader = None 67 self.Algorithm = None 68 self.AlgorithmParameters = None 69 self.NumberCells = None 70 self.intensities = None 71 self.stdevs = None 72 self.npix = None 73 self.nrows = None 74 self.ncols = None 75 self.nmask = None 76 self.mask = None 77 self.noutliers = None 78 self.outliers = None 79 self.modified = None
80 81
82 -def read(handle):
83 """ 84 Read the information in a cel file, and store it in a Record. 85 """ 86 # Needs error handling. 87 # Needs to know the chip design. 88 record = Record() 89 section = "" 90 for line in handle: 91 if not line.strip(): 92 continue 93 # Set current section 94 if line[:5] == "[CEL]": 95 section = "CEL" 96 elif line[:8] == "[HEADER]": 97 section = "HEADER" 98 elif line[:11] == "[INTENSITY]": 99 section = "INTENSITY" 100 record.intensities = numpy.zeros((record.nrows, record.ncols)) 101 record.stdevs = numpy.zeros((record.nrows, record.ncols)) 102 record.npix = numpy.zeros((record.nrows, record.ncols), int) 103 elif line[:7] == "[MASKS]": 104 section = "MASKS" 105 record.mask = numpy.zeros((record.nrows, record.ncols)) 106 elif line[:10] == "[OUTLIERS]": 107 section = "OUTLIERS" 108 record.outliers = numpy.zeros((record.nrows, record.ncols)) 109 elif line[:10] == "[MODIFIED]": 110 section = "MODIFIED" 111 record.modified = numpy.zeros((record.nrows, record.ncols)) 112 elif line[0] == "[": 113 # This would be an unknown section 114 section = "" 115 elif section == "CEL": 116 keyword, value = line.split("=", 1) 117 if keyword == 'Version': 118 record.version = int(value) 119 elif section == "HEADER": 120 # Set record.ncols and record.nrows, remaining data goes into 121 # record.header dict 122 keyword, value = line.split("=", 1) 123 if keyword == "Cols": 124 record.ncols = int(value) 125 elif keyword == "Rows": 126 record.nrows = int(value) 127 elif keyword == 'GridCornerUL': 128 x, y = value.split() 129 record.GridCornerUL = (int(x), int(y)) 130 elif keyword == 'GridCornerUR': 131 x, y = value.split() 132 record.GridCornerUR = (int(x), int(y)) 133 elif keyword == 'GridCornerLR': 134 x, y = value.split() 135 record.GridCornerLR = (int(x), int(y)) 136 elif keyword == 'GridCornerLL': 137 x, y = value.split() 138 record.GridCornerLL = (int(x), int(y)) 139 elif keyword == 'DatHeader': 140 record.DatHeader = value.strip('\n\r') 141 elif keyword == 'Algorithm': 142 record.Algorithm = value.strip('\n\r') 143 elif keyword == 'AlgorithmParameters': 144 record.AlgorithmParameters = value.strip('\n\r') 145 elif section == "INTENSITY": 146 if "NumberCells" in line: 147 record.NumberCells = int(line.split("=", 1)[1]) 148 elif "CellHeader" in line: 149 pass 150 else: 151 words = line.split() 152 y = int(words[0]) 153 x = int(words[1]) 154 record.intensities[x, y] = float(words[2]) 155 record.stdevs[x, y] = float(words[3]) 156 record.npix[x, y] = int(words[4]) 157 elif section == "MASKS": 158 if "NumberCells" in line: 159 record.nmask = int(line.split("=", 1)[1]) 160 elif "CellHeader" in line: 161 pass 162 else: 163 words = line.split() 164 y = int(words[0]) 165 x = int(words[1]) 166 record.mask[x, y] = int(1) 167 elif section == "OUTLIERS": 168 if "NumberCells" in line: 169 record.noutliers = int(line.split("=", 1)[1]) 170 elif "CellHeader" in line: 171 pass 172 else: 173 words = line.split() 174 y = int(words[0]) 175 x = int(words[1]) 176 record.outliers[x, y] = int(1) 177 elif section == "MODIFIED": 178 if "NumberCells" in line: 179 record.nmodified = int(line.split("=", 1)[1]) 180 elif "CellHeader" in line: 181 pass 182 else: 183 words = line.split() 184 y = int(words[0]) 185 x = int(words[1]) 186 record.modified[x, y] = float(words[2]) 187 else: 188 continue 189 return record
190 191 if __name__ == "__main__": 192 from Bio._utils import run_doctest 193 run_doctest() 194