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