Package Bio :: Package GFF :: Module GenericTools
[hide private]
[frames] | no frames]

Source Code for Module Bio.GFF.GenericTools

  1  #!/usr/bin/env python 
  2  # 
  3  # Copyright 2002 by Michael Hoffman.  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   
  8  """A set of generic bits of code under Bio.GFF (DEPRECATED). 
  9   
 10  This is part of the "old" Bio.GFF module by Michael Hoffman, which offered 
 11  access to a MySQL database holding GFF data loaded by BioPerl. This code has 
 12  now been deprecated, and will probably be removed in order to free the Bio.GFF 
 13  namespace for a new GFF parser in Biopython (including GFF3 support). 
 14  """ 
 15   
 16  import exceptions 
 17  import os 
 18  import sys 
 19  import tempfile 
 20   
21 -class AppendableListDictionary(dict):
22 """ 23 a dictionary of lists 24 """
25 - def append_to(self, key, value):
26 try: 27 dict.__getitem__(self, key).append(value) 28 except KeyError: 29 self[key] = [value]
30
31 -class ForgivingDictionary(AppendableListDictionary):
32 - def __getitem__(self, key):
33 try: 34 return dict.__getitem__(self, key) 35 except KeyError: 36 return None
37
38 -class VerboseDict(dict):
39 - def __str__(self):
40 dict_copy = {} 41 for key in self: 42 dict_copy[key] = str(self[key]) 43 return str(dict_copy)
44
45 -class VerboseList(list):
46 - def __str__(self):
47 return str(map(lambda x: str(x), self))
48
49 -class TempFile(file):
50 - def __init__(self, suffix = ".python-temp", keep = 0):
51 self.removed = 0 52 self.keep = keep 53 # XXX: this is a race condition: 54 file.__init__(self, tempfile.mktemp(suffix), "w")
55
56 - def __del__(self):
57 self.remove()
58
59 - def remove(self):
60 if self.keep == 0: 61 if self.removed == 0: 62 try: 63 try: 64 self.close() 65 os.remove(self.name) 66 finally: 67 self.removed = 1 68 except exceptions.OSError: 69 pass
70
71 -class SurrogateNotInitedError(exceptions.AttributeError):
72 pass
73
74 -class Surrogate(object):
75 """ 76 the data is stored in _data 77 """
78 - def __init__(self, data):
79 self._data = data
80
81 - def __getattr__(self, name):
82 if name == "_data": 83 raise SurrogateNotInitedError(name) 84 else: 85 try: 86 return getattr(self._data, name) 87 except SurrogateNotInitedError: 88 raise SurrogateNotInitedError(name)
89 90
91 -def defline_text(defline):
92 if defline[0] == ">": 93 return defline[1:] 94 else: 95 return defline
96
97 -def is_nestable(x):
98 """ 99 Returns 1 if x is a tuple or list (sequence types that can nest) 100 Returns 0 otherwise 101 102 >>> is_nestable("string") 103 0 104 >>> is_nestable((0,)) 105 1 106 >>> is_nestable(range(5)) 107 1 108 """ 109 return isinstance(x, (tuple, list))
110
111 -def dump_list(l):
112 """ 113 returns strings of list 114 """ 115 try: 116 return '[%s]' % ', '.join(map(str, l)) 117 except TypeError: 118 return str(l)
119
120 -def reverse_text(text):
121 """ 122 >>> reverse_text('abracadabra') 123 'arbadacarba' 124 """ 125 l = list(text) 126 l.reverse() 127 return ''.join(l)
128
129 -class ArgsParser(object):
130 """ 131 >>> unparsed_args = ["moocow"] 132 >>> args = ArgsParser(unparsed_args, [('infile', 'defaultin'), ('outfile', 'defaultout')]) 133 >>> args.infile 134 'moocow' 135 >>> args.outfile 136 'defaultout' 137 """
138 - def __init__(self, args, defaults):
139 for i, default in enumerate(defaults): 140 try: 141 self.__dict__[default[0]] = args[i] 142 continue 143 except TypeError: 144 pass 145 except IndexError: 146 pass 147 self.__dict__[default[0]] = default[1]
148
149 -def all(iterator):
150 return [item for item in iterator]
151
152 -def _test():
153 """Run the Bio.GFF.GenericTools module's doctests (PRIVATE). 154 155 This will try and locate the unit tests directory, and run the doctests 156 from there in order that the relative paths used in the examples work. 157 """ 158 import doctest 159 print "Runing doctests..." 160 doctest.testmod() 161 print "Done"
162 163 if __name__ == "__main__": 164 if __debug__: 165 _test() 166