Package Bio :: Package Geo
[hide private]
[frames] | no frames]

Source Code for Package Bio.Geo

 1  # Copyright 2001 by Katharine Lindner.  All rights reserved. 
 2  # Copyright 2006 by PeterC.  All rights reserved. 
 3  # Copyright 2007 by Michiel de Hoon.  All rights reserved. 
 4  # This code is part of the Biopython distribution and governed by its 
 5  # license.  Please see the LICENSE file that should have been included 
 6  # as part of this package. 
 7  """Parser for files from NCBI's Gene Expression Omnibus (GEO). 
 8   
 9  http://www.ncbi.nlm.nih.gov/geo/ 
10  """ 
11   
12  import Record 
13   
14   
15 -def _read_key_value(line):
16 words = line[1:].split("=", 1) 17 try: 18 key, value = words 19 value = value.strip() 20 except ValueError: 21 key = words[0] 22 value = "" 23 key = key.strip() 24 return key, value
25 26
27 -def parse(handle):
28 record = None 29 for line in handle: 30 line = line.strip('\n').strip('\r') 31 if not line: continue # Ignore empty lines 32 c = line[0] 33 if c=='^': 34 if record: yield record 35 record = Record.Record() 36 record.entity_type, record.entity_id = _read_key_value(line) 37 elif c=='!': 38 if line in ('!Sample_table_begin', 39 '!Sample_table_end', 40 '!Platform_table_begin', 41 '!Platform_table_end'): 42 continue 43 key, value = _read_key_value(line) 44 if key in record.entity_attributes: 45 if type(record.entity_attributes[key])==list: 46 record.entity_attributes[key].append(value) 47 else: 48 existing = record.entity_attributes[key] 49 record.entity_attributes[key] = [existing, value] 50 else: 51 record.entity_attributes[key] = value 52 elif c=='#': 53 key, value = _read_key_value(line) 54 assert key not in record.col_defs 55 record.col_defs[key] = value 56 else: 57 row = line.split("\t") 58 record.table_rows.append(row) 59 yield record
60 61
62 -class Iterator:
63 """Iterator interface to move over a file of Geo entries one at a time. 64 65 Uses the fact that each GEO record begins with a line starting ^ (caret). 66 """
67 - def __init__(self, handle, parser = None):
68 """Initialize the iterator. 69 70 Arguments: 71 o handle - A handle with GEO entries to iterate through. 72 returning them. If None, then the raw entry will be returned. 73 """ 74 import warnings 75 warnings.warn("Bio.Geo.Iterator(handle, parser) is deprecated. Please use Bio.Geo.parse(handle) instead. It also returns an iterator.""", 76 DeprecationWarning) 77 self.records = parse(handle)
78
79 - def next(self):
80 return self.records.next()
81
82 - def __iter__(self):
83 return iter(self.next, None)
84