Back to index

python-biopython  1.60
__init__.py
Go to the documentation of this file.
00001 # Copyright 2001 by Katharine Lindner.  All rights reserved.
00002 # Copyright 2006 by PeterC.  All rights reserved.
00003 # Copyright 2007 by Michiel de Hoon.  All rights reserved.
00004 # This code is part of the Biopython distribution and governed by its
00005 # license.  Please see the LICENSE file that should have been included
00006 # as part of this package.
00007 """Parser for files from NCBI's Gene Expression Omnibus (GEO).
00008 
00009 http://www.ncbi.nlm.nih.gov/geo/
00010 """
00011 
00012 import Record
00013 
00014 
00015 def _read_key_value(line):
00016     words = line[1:].split("=", 1)
00017     try:
00018         key, value = words
00019         value = value.strip()
00020     except ValueError:
00021         key = words[0]
00022         value = ""
00023     key = key.strip()
00024     return key, value
00025 
00026 
00027 def parse(handle):
00028     record = None
00029     for line in handle:
00030         line = line.strip('\n').strip('\r')
00031         if not line: continue # Ignore empty lines
00032         c = line[0]
00033         if c=='^':
00034             if record: yield record
00035             record = Record.Record()
00036             record.entity_type, record.entity_id = _read_key_value(line)
00037         elif c=='!':
00038             if line in ('!Sample_table_begin',
00039                         '!Sample_table_end',
00040                         '!Platform_table_begin',
00041                         '!Platform_table_end'):
00042                 continue
00043             key, value = _read_key_value(line)
00044             if key in record.entity_attributes:
00045                 if type(record.entity_attributes[key])==list:
00046                     record.entity_attributes[key].append(value)
00047                 else:
00048                     existing = record.entity_attributes[key]
00049                     record.entity_attributes[key] = [existing, value]
00050             else:
00051                 record.entity_attributes[key] = value
00052         elif c=='#':
00053             key, value = _read_key_value(line)
00054             assert key not in record.col_defs
00055             record.col_defs[key] = value
00056         else:
00057             row = line.split("\t")
00058             record.table_rows.append(row)
00059     yield record