Back to index

python-biopython  1.60
Record.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 # This code is part of the Biopython distribution and governed by its
00004 # license.  Please see the LICENSE file that should have been included
00005 # as part of this package.
00006 
00007 """
00008 Hold GEO data in a straightforward format.
00009 
00010 classes:
00011 o Record - All of the information in an GEO record.
00012 
00013 See http://www.ncbi.nlm.nih.gov/geo/
00014 """
00015 
00016 class Record(object):
00017     """Hold GEO information in a format similar to the original record.
00018 
00019     The Record class is meant to make data easy to get to when you are
00020     just interested in looking at GEO data.
00021 
00022     Attributes:
00023     entity_type
00024     entity_id
00025     entity_attributes
00026     col_defs
00027     table_rows
00028 
00029     """
00030     def __init__(self):
00031         self.entity_type = ''
00032         self.entity_id = ''
00033         self.entity_attributes = {}
00034         self.col_defs = {}
00035         self.table_rows = []
00036 
00037     def __str__( self ):
00038         output = ''
00039         output = output + 'GEO Type: %s\n' % self.entity_type
00040         output = output + 'GEO Id: %s\n' % self.entity_id
00041         att_keys = self.entity_attributes.keys()
00042         att_keys.sort()
00043         for key in att_keys:
00044             contents = self.entity_attributes[ key ]
00045             if( type( contents ) == type( [] ) ):
00046                 for item in contents:
00047                     try:
00048                         output = output + '%s: %s\n' % ( key, item[ :40 ] )
00049                         output = output + out_block( item[ 40: ] )
00050                     except:
00051                         pass
00052             elif( type( contents ) == type( '' ) ):
00053                 output = output + '%s: %s\n' % ( key, contents[ :40 ] )
00054                 output = output + out_block( contents[ 40: ] )
00055             else:
00056                 print contents
00057                 output = output + '%s: %s\n' % ( key, val[ :40 ] )
00058                 output = output + out_block( val[ 40: ] )
00059         col_keys = self.col_defs.keys()
00060         col_keys.sort()
00061         output = output + 'Column Header Definitions\n'
00062         for key in col_keys:
00063             val = self.col_defs[ key ]
00064             output = output + '    %s: %s\n' % ( key, val[ :40 ] )
00065             output = output + out_block( val[ 40: ], '    ' )
00066         #May have to display VERY large tables,
00067         #so only show the first 20 lines of data
00068         MAX_ROWS = 20+1 # include header in count
00069         for row in self.table_rows[0:MAX_ROWS]:
00070             output = output + '%s: ' % self.table_rows.index( row )
00071             for col in row:
00072                 output = output + '%s\t' % col
00073             output = output + '\n'
00074         if len(self.table_rows) > MAX_ROWS:
00075             output = output + '...\n'
00076             row = self.table_rows[-1]
00077             output = output + '%s: ' % self.table_rows.index( row )
00078             for col in row:
00079                 output = output + '%s\t' % col
00080             output = output + '\n'
00081             
00082         return output
00083 
00084 def out_block( text, prefix = '' ):
00085     output = ''
00086     for j in range( 0, len( text ), 80 ):
00087         output = output + '%s%s\n'  % ( prefix, text[ j: j + 80 ] )
00088     output = output + '\n'
00089     return output