def parse(oboFile, typedefs): """ Parses an OBO (ontology file) creating a correspoinding Ontology object composed of Term objects that relate to the terms found in the ontology file. """ ontology = Ontology() ## Use groupby to group the lines of our file into chunks of text, some ## stanzas, some typedefs, and other metadata to be processed with open(oboFile) as f: for (key, group) in groupby(f, is_data): if key: header = group.next().rstrip('\n') if header.find('[Typedef]') != -1: dataDict = get_data_as_dict(group) ontology.add_typedef(dataDict['name']) elif header.find('[Term]') != -1: dataDict = get_data_as_dict(group, typedefs) ontology.add_term(build_term(dataDict)) else: # We are dealing with ontology metadata that should be # captured in our ontology object. ontology.metadata.append(header) ontology.metadata.extend([x.strip() for x in group]) return ontology