Package obitools :: Package format :: Package ontology :: Module go_obo
[hide private]
[frames] | no frames]

Source Code for Module obitools.format.ontology.go_obo

  1  __docformat__ = 'restructuredtext' 
  2   
  3  import re 
  4  import string 
  5  import textwrap 
  6   
  7   
  8  from obitools.obo.go.parser import GOEntryIterator 
  9  from obitools.obo.go.parser import GOTerm 
 10  from obitools.obo.go.parser import GOEntry 
 11   
 12  """ 
 13  go_obo.py : gene_ontology_edit.obo  file parser: 
 14  ---------------------------------------------------- 
 15     
 16  - OBOFile class: open a flat file and return an entry. 
 17               
 18  """ 
19 -class OBOFile(object):
20 """ 21 Iterator over all entries of an OBO file 22 """ 23
24 - def __init__(self,_path):
25 self.file = GOEntryIterator(_path)
26
27 - def __iter__(self):
28 return self
29
30 - def next(self):
31 fiche = self.file.next() 32 33 if isinstance(fiche, GOTerm): 34 self.isaterm=True 35 return Term(fiche) 36 elif isinstance(fiche, GOEntry): 37 self.isaterm=False 38 return Entry(fiche) 39 else: 40 self.isaterm=False 41 return Header(fiche)
42 43 44 ############# tout le reste doit descendre a l'etage obitools/ogo/go/parser.py ########## 45 46 # define an XRef into a go_obo.py script in the microbi pylib
47 -class Xref(object):
48 """ 49 Class Xref 50 Xref.db Xref database 51 Xref.id Xref identifier 52 """ 53
54 - def __init__(self,description):
55 data = description.split(':') 56 self.db = data[0].strip() 57 self.id = data[1].strip()
58 59 # define a RelatedTerm into a go_obo.py script in the microbi pylib
60 -class RelatedTerm(object):
61 """ 62 Class RelatedTerm 63 RelatedTerm.relation RelatedTerm relation 64 RelatedTerm.related_term RelatedTerm GO identifier 65 RelatedTerm.comment all terms have 0 or 1 comment 66 """ 67
68 - def __init__(self,relation,value,comment):
69 self.relation = relation 70 self.related_term = value.strip('GO:') 71 self.comment = comment
72 73 74 # define into a go_obo.py script in the microbi pylib 75 #class Term(object): 76 # """ 77 # class representing an OBO term (entry). 78 # """ 79 # 80 # def __init__(self): 81 # raise RuntimeError('biodb.go_obo is an abstract class') 82 # 83 # def __checkEntry__(self): 84 # minimum=(hasattr(self,'goid') ) 85 # if not minimum: 86 # raise AssertionError('Misconstructed GO Term instance %s' % [x for x in dir(self) if x[0]!='_']) 87
88 -class Term(object):
89 """ 90 Class Term 91 representing a GO term. 92 """ 93
94 - def __init__(self,data=None):
95 """ 96 """ 97 self.data=data 98 self.isaterm = True 99 100 if data: 101 self.__filtreGoid__() 102 self.__filtreName__() 103 self.__filtreComment__() 104 self.__filtreSynonyms__() 105 self.__filtreDef__() 106 self.__filtreParents__() 107 self.__filtreRelationships__() 108 self.__filtreRelation__() 109 self.__filtreObsolete__() 110 self.__filtreAltIds__() 111 self.__filtreXRefs__() 112 self.__filtreSubsets__() 113 114 # check if all required attributes were valued 115 self.__checkEntry__()
116 117
118 - def __checkEntry__(self):
119 minimum=(hasattr(self,'goid') ) 120 if not minimum: 121 raise AssertionError('Misconstructed GO Term instance %s' % [x for x in dir(self) if x[0]!='_'])
122 123
124 - def __filtreGoid__(self):
125 """ 126 Extract GO id. 127 """ 128 self.goid = self.data.id.value.strip('GO:')
129
130 - def __filtreName__(self):
131 """ 132 Extract GO name. 133 """ 134 self.name = self.data.name.value
135
136 - def __filtreSynonyms__(self):
137 """ 138 Extract GO synonym(s). 139 """ 140 self.list_synonyms = {} 141 if self.data.synonyms: 142 for y in self.data.synonyms: 143 self.list_synonyms[y.value] = y.scope
144 145
146 - def __filtreComment__(self):
147 """ 148 manage None comments 149 """ 150 if self.data.comment != None: 151 self.comment = self.data.comment.value 152 else: 153 self.comment = ""
154
155 - def __filtreDef__(self):
156 """ 157 Extract GO definition. 158 """ 159 if self.data.definition != None: 160 self.definition = self.data.definition.value 161 else: 162 self.definition = ""
163
164 - def __filtreParents__(self):
165 """ 166 To make the is_a hierarchy 167 """ 168 if self.data.is_a != None: 169 self.is_a = set([isa.value.strip('GO:') for isa in self.data.is_a]) 170 else: 171 self.is_a = set()
172
173 - def __filtreRelation__(self):
174 """ 175 To make the part_of hierarchy 176 """ 177 self.part_of = set() 178 self.regulates = set() 179 self.negatively_regulates = set() 180 self.positively_regulates = set() 181 182 if self.data.relationship != None: 183 for rel in self.data.relationship: 184 if rel.relationship == "part_of": 185 self.part_of.add(rel.value.strip('GO:')) 186 elif rel.relationship == "regulates": 187 self.regulates.add(rel.value.strip('GO:')) 188 elif rel.relationship == "negatively_regulates": 189 self.negatively_regulates.add(rel.value.strip('GO:')) 190 elif rel.relationship == "positively_regulates": 191 self.positively_regulates.add(rel.value.strip('GO:'))
192 193
194 - def __filtreRelationships__(self):
195 """ 196 Relation list with other GO Terms (is_a, part_of or some regulates relation) 197 """ 198 self.related_term =[] 199 if self.data.relationship != None: 200 for x in self.data.relationship: 201 self.related_term.append(RelatedTerm(x.relationship,x.value,x.__doc__)) 202 #self.related_term.append(RelatedTerm(x.relationship,x.value,x.comment)) 203 if self.data.is_a != None: 204 for x in self.data.is_a: 205 self.related_term.append(RelatedTerm('is_a',x.value,x.__doc__))
206 #self.related_term.append(RelatedTerm('is_a',x.value,x.comment)) 207 208 209
210 - def __filtreObsolete__(self):
211 """ 212 for each obsolete terms corresponds a set of GO Identifiers 213 so that this GO term is consider as others GO Terms 214 """ 215 self.considers = set() 216 self.replaces = set() 217 self.is_obsolete = self.data.is_obsolete 218 if self.data.is_obsolete: 219 if self.data.consider: 220 self.considers = set([considered.value.strip('GO:') for considered in self.data.consider]) 221 if self.data.replaced_by: 222 self.replaces = set([replaced.value.strip('GO:') for replaced in self.data.replaced_by])
223 224
225 - def __filtreAltIds__(self):
226 """ 227 alternate(s) id(s) for this term (= alias in the geneontology schema model!) 228 """ 229 if self.data.alt_ids: 230 self.alt_ids = set([x.value.strip('GO:') for x in self.data.alt_ids]) 231 else: 232 self.alt_ids = set()
233
234 - def __filtreXRefs__(self):
235 """ 236 cross references to other databases 237 """ 238 self.xrefs = set() 239 if self.data.xrefs: 240 self.xrefs = set([Xref(x.value.reference) for x in self.data.xrefs])
241 242
243 - def __filtreSubsets__(self):
244 """ 245 subset label to make smaller sets of GO Terms 246 """ 247 self.subsets = set() 248 if self.data.subsets: 249 self.subsets = set([x.value for x in self.data.subsets])
250 251
252 -class Entry(object):
253 """ 254 a Stanza entry, like [Typedef] for example 255 """
256 - def __init__(self,data=None):
257 self.data=data 258 self.isaterm=False 259 self.isanentry=True
260 261
262 -class Header(object):
263 """ 264 class representing a GO header. 265 """ 266
267 - def __init__(self,data=None):
268 """ 269 """ 270 self.data=data 271 self.isaterm = False
272