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 -def read(handle):
80 """ 81 Read the information in a cel file, and store it in a Record. 82 """ 83 # Needs error handling. 84 # Needs to know the chip design. 85 record = Record() 86 section = "" 87 for line in handle: 88 if not line.strip(): 89 continue 90 # Set current section 91 if line[:5] == "[CEL]": 92 section = "CEL" 93 elif line[:8] == "[HEADER]": 94 section = "HEADER" 95 elif line[:11] == "[INTENSITY]": 96 section = "INTENSITY" 97 record.intensities = numpy.zeros((record.nrows, record.ncols)) 98 record.stdevs = numpy.zeros((record.nrows, record.ncols)) 99 record.npix = numpy.zeros((record.nrows, record.ncols), int) 100 elif line[:7] == "[MASKS]": 101 section = "MASKS" 102 record.mask = numpy.zeros((record.nrows, record.ncols)) 103 elif line[:10] == "[OUTLIERS]": 104 section = "OUTLIERS" 105 record.outliers = numpy.zeros((record.nrows, record.ncols)) 106 elif line[:10] == "[MODIFIED]": 107 section = "MODIFIED" 108 record.modified = numpy.zeros((record.nrows, record.ncols)) 109 elif line[0] == "[": 110 # This would be an unknown section 111 section = "" 112 elif section == "CEL": 113 keyword, value = line.split("=", 1) 114 if keyword == 'Version': 115 record.version = int(value) 116 elif section == "HEADER": 117 # Set record.ncols and record.nrows, remaining data goes into 118 # record.header dict 119 keyword, value = line.split("=", 1) 120 if keyword == "Cols": 121 record.ncols = int(value) 122 elif keyword == "Rows": 123 record.nrows = int(value) 124 elif keyword == 'GridCornerUL': 125 x, y = value.split() 126 record.GridCornerUL = (int(x), int(y)) 127 elif keyword == 'GridCornerUR': 128 x, y = value.split() 129 record.GridCornerUR = (int(x), int(y)) 130 elif keyword == 'GridCornerLR': 131 x, y = value.split() 132 record.GridCornerLR = (int(x), int(y)) 133 elif keyword == 'GridCornerLL': 134 x, y = value.split() 135 record.GridCornerLL = (int(x), int(y)) 136 elif keyword == 'DatHeader': 137 record.DatHeader = value.strip('\n\r') 138 elif keyword == 'Algorithm': 139 record.Algorithm = value.strip('\n\r') 140 elif keyword == 'AlgorithmParameters': 141 record.AlgorithmParameters = value.strip('\n\r') 142 elif section == "INTENSITY": 143 if "NumberCells" in line: 144 record.NumberCells = int(line.split("=", 1)[1]) 145 elif "CellHeader" in line: 146 pass 147 else: 148 words = line.split() 149 y = int(words[0]) 150 x = int(words[1]) 151 record.intensities[x, y] = float(words[2]) 152 record.stdevs[x, y] = float(words[3]) 153 record.npix[x, y] = int(words[4]) 154 elif section == "MASKS": 155 if "NumberCells" in line: 156 record.nmask = int(line.split("=", 1)[1]) 157 elif "CellHeader" in line: 158 pass 159 else: 160 words = line.split() 161 y = int(words[0]) 162 x = int(words[1]) 163 record.mask[x, y] = int(1) 164 elif section == "OUTLIERS": 165 if "NumberCells" in line: 166 record.noutliers = int(line.split("=", 1)[1]) 167 elif "CellHeader" in line: 168 pass 169 else: 170 words = line.split() 171 y = int(words[0]) 172 x = int(words[1]) 173 record.outliers[x, y] = int(1) 174 elif section == "MODIFIED": 175 if "NumberCells" in line: 176 record.nmodified= int(line.split("=", 1)[1]) 177 elif "CellHeader" in line: 178 pass 179 else: 180 words = line.split() 181 y = int(words[0]) 182 x = int(words[1]) 183 record.modified[x, y] = float(words[2]) 184 else: 185 continue 186 return record
187 188 if __name__ == "__main__": 189 from Bio._utils import run_doctest 190 run_doctest() 191