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  from . 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: 32 continue # Ignore empty lines 33 c = line[0] 34 if c=='^': 35 if record: 36 yield record 37 record = Record.Record() 38 record.entity_type, record.entity_id = _read_key_value(line) 39 elif c=='!': 40 if line in ('!Sample_table_begin', 41 '!Sample_table_end', 42 '!Platform_table_begin', 43 '!Platform_table_end'): 44 continue 45 key, value = _read_key_value(line) 46 if key in record.entity_attributes: 47 if isinstance(record.entity_attributes[key], list): 48 record.entity_attributes[key].append(value) 49 else: 50 existing = record.entity_attributes[key] 51 record.entity_attributes[key] = [existing, value] 52 else: 53 record.entity_attributes[key] = value 54 elif c=='#': 55 key, value = _read_key_value(line) 56 assert key not in record.col_defs 57 record.col_defs[key] = value 58 else: 59 row = line.split("\t") 60 record.table_rows.append(row) 61 yield record
62