def test_get_references_secondary(self): '''COMMENT''' this_chebi_entity = ChebiEntity('22182') this_reference = Reference('WO2006008754', 'Patent', '', 'NOVEL INTERMEDIATES FOR LINEZOLID ' 'AND RELATED COMPOUNDS') self.assertTrue(this_reference in this_chebi_entity.get_references())
def __get_mol_file(self, read_id, retrieved_id): '''COMMENT''' mol_read = _read_mol_file(read_id) this_chebi_entity = ChebiEntity(str(retrieved_id)) textfile_retrieved = open(this_chebi_entity.get_mol_filename(), 'r') mol_retrieved = textfile_retrieved.read() textfile_retrieved.close() self.assertEquals(mol_read, mol_retrieved)
def test_get_comments_secondary(self): '''COMMENT''' this_chebi_entity = ChebiEntity('11505') this_comment = Comment('General', 'General', 'The substituent name \'3-oxoprop-2-enyl\' is ' 'incorrect but is used in various databases.', datetime.datetime.strptime('2005-03-18', '%Y-%M-%d')) self.assertTrue(this_comment in this_chebi_entity.get_comments())
def gen_ancestor_info(entity: ChebiEntity, relationships: List[str] = None, max_generations: int = None, starting_generation_: int = 0, seen_: Set[HashableChebiEntity] = None) \ -> Generator[Tuple[HashableChebiEntity, str, int], None, None]: """ Retrieves all ancestors ("outgoing" links). Args: entity: starting entity relationships: list of valid relationship types, e.g. "has_role" max_generations: maximum number of generations to pursue, or ``None`` for unlimited starting_generation_: for internal use only, for recursion seen_: for internal use only, for recursion Returns: list: of tuples ``entity, relationship, n_generations_above_start`` """ if max_generations is not None and starting_generation_ >= max_generations: return assert starting_generation_ == 0 or seen_ is not None seen_ = seen_ or set() # type: Set[HashableChebiEntity] relationships = relationships or DEFAULT_ANCESTOR_RELATIONSHIPS log.debug(f"Finding ancestors of {brief_description(entity)} " f"(generation {starting_generation_}) " f"via relationships {relationships!r}") for rel in entity.get_outgoings(): # type: Relation if rel.get_type() in relationships: target = HashableChebiEntity(rel.get_target_chebi_id()) log.debug(f"... found {brief_description(target)}") if target in seen_: # log.debug(f"Skipping {target!r}") continue seen_.add(target) yield target, rel.get_type(), starting_generation_ + 1 yield from gen_ancestor_info( entity=target, relationships=relationships, starting_generation_=starting_generation_ + 1, seen_=seen_, )
def has_roles(self): if self.chebi_id: entity = ChebiEntity('CHEBI:' + str(self.chebi_id)) outdict = dict() for relation in entity.get_outgoings(): if relation.get_type() == 'has_role': tid = relation.get_target_chebi_id() t = ChebiEntity(tid) s = re.findall(r'\d+', tid) outdict[t.get_name()] = int(s[0]) return outdict else: return ''
def get_chebi_entity(compound, user=None): chebi_array = [] for com_obj in compound.compound_objects: if 'object_type' in com_obj: if com_obj['object_type'].lower() in [ 'chebi entity', 'chebi', 'chebi object' ]: chebi_array.append(com_obj['object']) if chebi_array: return chebi_array for chebi_id in get_chebi_id(compound, user=user): chebi_object = ChebiEntity(chebi_id.upper()) gnomics.objects.compound.Compound.add_object( compound, obj=chebi_object, object_type="ChEBI Entity") chebi_array.append(chebi_object) return chebi_array
def save_model(self, request, obj, form, change): if form.cleaned_data['chebi_id']: chebi_id = form.cleaned_data['chebi_id'] chebi_comb = ChebiEntity('CHEBI:' + str(chebi_id)) parent_id = chebi_comb.get_parent_id() if parent_id: s = re.findall(r'\d+', parent_id) chebi_id = int(s[0]) chebi_comb = ChebiEntity('CHEBI:' + str(chebi_id)) obj.chebi_id = chebi_id obj.chebi_name = chebi_comb.get_name() else: obj.chebi_name = None if form.cleaned_data['pubchem_id']: comp = Compound.from_cid(form.cleaned_data['pubchem_id']) obj.pubchem_name = comp.synonyms[0] else: obj.pubchem_name = None obj.save()
def check_line_for_carboxylate(line): """Check a line from the CHEBI compounds file for carboxylic acid. Returns name of the protonated form. """ # USING OFFLINE FILE -- NOT COMPLETE # ID, _, _, _, parent_id, name, notes, _, _, star = line # print(name) # print(notes) # if 'Conjugate base of ' in notes: # if name[-3:] == 'ate': # acid_name = notes.replace("Conjugate base of ", '') # acid_name = acid_name.split('acid')[0] + 'acid' # print('Conjugate base of:', acid_name, '<<<<<<<<<<') # return acid_name # Using libchebipy -- Online: ID, _, _, _, parent_id, name, notes, _, _, star = line entity = ChebiEntity(ID) outgoings = entity.get_outgoings() for out in outgoings: type = out.get_type() id = out.get_target_chebi_id() if type == 'is_conjugate_base_of': print(out) # use regular expression to remove any non alphabetic # characters # that may follow the name only_alph = sub('[^A-Za-z]', '', name) print(only_alph) if only_alph[-3:] == 'ate' and only_alph[-5:] != 'phate': acid_ID = id.replace("CHEBI:", "") print('acid ID:', acid_ID) acid_entity = ChebiEntity(acid_ID) acid_name = acid_entity.get_name() print('Conjugate base of:', acid_name, '<<<<<<<<<<') return acid_name return None
def hier_name_search(molecule, property, option=False): """Search for molecule property in PUBCHEM using a hierarchy of name spaces Order: 1 - pubchem ID 2 - KEGG ID 3 - chebiID 4 - chebiID to InChIKey 5 - IUPAC name 6 - name Properties: CanononicalSMILES IUPACName XLogP complexity InChiKey Tutorial: https://pubchemdocs.ncbi.nlm.nih.gov/ pug-rest-tutorial$_Toc458584413 """ QUERY_URL = ( 'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/' ) try: if molecule.PubChemID is not None: QUERY_URL_fin = QUERY_URL + molecule.PubChemID QUERY_URL_fin += '/property/'+property+'/TXT' result = run_request(query=QUERY_URL_fin) if result is not None: print('passed pubchemID') return result except (AttributeError, ValueError): print('failed pubchemID') pass try: if molecule.KEGG_ID is not None: QUERY_URL_fin = QUERY_URL + molecule.KEGG_ID QUERY_URL_fin += '/property/'+property+'/TXT' result = run_request(query=QUERY_URL_fin) if result is not None: print('passed KEGG ID') return result except (AttributeError, ValueError): print('failed KEGG ID') pass try: if molecule.chebiID is not None: QUERY_URL_fin = QUERY_URL + 'chebi:'+molecule.chebiID QUERY_URL_fin += '/property/'+property+'/TXT' result = run_request(query=QUERY_URL_fin) if result is not None: print('passed chebiID') return result except (AttributeError, ValueError): print('failed chebiID') pass try: if molecule.chebiID is not None: if molecule.InChiKey is None: # try using the CHEBI API # libChEBIpy (https://github.com/libChEBI/libChEBIpy) print('using libchebipy to get InChiKey...') from libchebipy import ChebiEntity entity = ChebiEntity(molecule.chebiID) iKEY = entity.get_inchi_key() print(iKEY) else: iKEY = molecule.InChiKey QUERY_URL = ( 'https://pubchem.ncbi.nlm.nih.gov/rest/' 'pug/compound/inchikey/' ) QUERY_URL_fin = QUERY_URL + iKEY QUERY_URL_fin += '/property/'+property+'/TXT' result = run_request(query=QUERY_URL_fin) if result is not None: print('passed chebiID/inchiKey') return result except (AttributeError, ValueError): print('failed chebiID/inchiKey') pass try: if molecule.InChiKey is not None: iKEY = molecule.InChiKey QUERY_URL = ( 'https://pubchem.ncbi.nlm.nih.gov/rest/' 'pug/compound/inchikey/' ) QUERY_URL_fin = QUERY_URL + iKEY QUERY_URL_fin += '/property/'+property+'/TXT' result = run_request(query=QUERY_URL_fin) if result is not None: print('passed inchiKey') return result except (AttributeError, ValueError): print('failed inchiKey') pass try: if molecule.iupac_name is not None: QUERY_URL = ( 'https://pubchem.ncbi.nlm.nih.gov/rest/pug' '/compound/name/' ) QUERY_URL_fin = QUERY_URL + molecule.iupac_name QUERY_URL_fin += '/property/'+property+'/TXT' if property == 'CanonicalSMILES': result = run_request(query=QUERY_URL_fin, smiles=True) if type(result) == tuple: # handle new line errors in SMILES text, boolean = result if boolean is True: # pick the uncharged SMILES for option, smi in enumerate(text.split('\n')): print('smiles1:', smi) if check_charge_on_SMILES(smi): # charged continue return smi, option elif type(result) == str and result is not None: print('passed name') return result else: result = run_request( query=QUERY_URL_fin, option=option ) if result is not None: print('passed IUPAC name') return result except (AttributeError, ValueError): print('failed IUPAC name') pass try: if molecule.name is not None: QUERY_URL_fin = QUERY_URL + molecule.name QUERY_URL_fin += '/property/'+property+'/TXT' if property == 'CanonicalSMILES': result = run_request(query=QUERY_URL_fin, smiles=True) if type(result) == tuple: # handle new line errors in SMILES text, boolean = result if boolean is True: # pick the uncharged SMILES for option, smi in enumerate(text.split('\n')): print('smiles1:', smi) if check_charge_on_SMILES(smi): # charged continue return smi, option elif type(result) == str and result is not None: print('passed name') return result else: result = run_request( query=QUERY_URL_fin, option=option ) if result is not None: print('passed name') return result except (AttributeError, ValueError): print('failed name') import sys sys.exit() return None
def hier_name_search_pcp(molecule, property, option=False): """Search for molecule property in PUBCHEM using a hierarchy of name spaces using pubchempy. Property can now be a list. Order: 1 - pubchem ID 2 - KEGG ID 3 - chebiID 4 - chebiID to InChIKey 5 - IUPAC name 6 - name Properties: CanononicalSMILES IUPACName XLogP complexity InChiKey PubChemID synonyms if option is not False we want to use the 'name' search only to recreate conditions of original search that gave option. """ if type(property) is not list: property = [property] try: if molecule.PubChemID is not None and option is False: result = run_request_pcp(ident=molecule.PubChemID, namespace='cid') if result is not None: print('> passed pubchemID') if len(property) > 1: return [ extract_property(i, result) for i in property ] else: return [ extract_property(i, result) for i in property ][0] except (AttributeError, ValueError): pass try: if molecule.KEGG_ID is not None and option is False: result = run_request_pcp(ident=molecule.KEGG_ID, namespace='name') if result is not None: print('> passed KEGG ID') if len(property) > 1: return [ extract_property(i, result) for i in property ] else: return [ extract_property(i, result) for i in property ][0] except (AttributeError, ValueError): pass try: if molecule.chebiID is not None and option is False: result = run_request_pcp(ident='chebi:'+molecule.chebiID, namespace='name') if result is not None: print('> passed chebiID') if len(property) > 1: return [ extract_property(i, result) for i in property ] else: return [ extract_property(i, result) for i in property ][0] except (AttributeError, ValueError): pass try: if molecule.chebiID is not None and option is False: if molecule.InChiKey is None: # try using the CHEBI API # libChEBIpy (https://github.com/libChEBI/libChEBIpy) print('> attempting libchebipy to get InChiKey...') from libchebipy import ChebiEntity entity = ChebiEntity(molecule.chebiID) iKEY = entity.get_inchi_key() print(iKEY) else: iKEY = molecule.InChiKey result = run_request_pcp(ident=iKEY, namespace='inchikey') if result is not None: print('> passed chebiID/inchiKey') if len(property) > 1: return [ extract_property(i, result) for i in property ] else: return [ extract_property(i, result) for i in property ][0] except (AttributeError, ValueError): pass try: if molecule.InChiKey is not None and option is False: result = run_request_pcp(ident=molecule.InChiKey, namespace='inchikey') if result is not None: print('> passed inchiKey') if len(property) > 1: return [ extract_property(i, result) for i in property ] else: return [ extract_property(i, result) for i in property ][0] except (AttributeError, ValueError): pass try: if molecule.iupac_name is not None and option is False: result = run_request_pcp(ident=molecule.iupac_name, namespace='name', option=option) if result is not None: print('> passed IUPAC name') if len(property) > 1: return [ extract_property(i, result) for i in property ] else: return [ extract_property(i, result) for i in property ][0] except (AttributeError, ValueError): pass try: print('> trying name... for:', property) if molecule.name is not None: if property[0] == 'CanonicalSMILES': result = run_request_pcp(ident=molecule.name, namespace='name', smiles=True) if type(result) == tuple: # handle new line errors in SMILES text, boolean = result if boolean is True: # pick the uncharged SMILES for option, Compound in enumerate(text): synon = [ i.lower() for i in Compound.synonyms ] if molecule.name.lower() in synon: # ignore charged species smi = Compound.canonical_smiles if check_charge_on_SMILES(smi): continue return smi, option return None elif type(result) == str and result is not None: print('> passed name') print('I am interested in what this result is:') print(result) import sys sys.exit() return result else: result = run_request_pcp(ident=molecule.name, namespace='name', option=option) if result is not None: print('> passed name') if len(property) > 1: return [ extract_property(i, result) for i in property ] else: return [ extract_property(i, result) for i in property ][0] except (AttributeError, ValueError): print('> failed all searches...') return None
def test_get_definition_secondary(self): '''COMMENT''' this_chebi_entity = ChebiEntity('41140') self.assertEqual(this_chebi_entity.get_definition(), 'D-Glucopyranose with beta configuration at the ' 'anomeric centre.')
def test_get_mol_existing(self): '''COMMENT''' chebi_id = 73938 this_chebi_entity = ChebiEntity(str(chebi_id)) self.assertEqual(this_chebi_entity.get_mol(), _read_mol_file(chebi_id))
kegggmt = Path(organism[1].iloc[0][:5] + "_format_KEGG.gmt") chebilines = [] hmdblines = [] kegglines = [] for i in pathways: path = organism[organism[3] == i] cpds = path[0].values.tolist() chebiids = list(map(str, cpds)) chebiids = ['CHEBI:' + chebiid for chebiid in chebiids] hmdbids = [] keggids = [] for chebiid in chebiids: try: chebi_entity = ChebiEntity(chebiid) except ChebiException: print('CHEBI:' + chebiid + ' invalid') for db in chebi_entity.get_database_accessions(): if db.get_type() == 'HMDB accession': hmdbids.append(db.get_accession_number().replace( "HMDB00", "HMDB")) if db.get_type() == 'KEGG COMPOUND accession': keggids.append(db.get_accession_number()) # chebiids.insert(0, i) # chebiids.insert(0, path[1].unique()[0]) # chebilines.append("\t".join(chebiids)) if len(set(hmdbids)) > threshold: hmdbids = sorted(set(hmdbids), key=hmdbids.index)
def test_search_aspirin(self): '''Test search method for aspirin.''' results = libchebipy.search('aspirin', False) self.assertIn(ChebiEntity('CHEBI:15365'), results)
def get_cmpd_information(molec): """Get information from CHEBI Database of a compound from CHEBI ID. Online using libChEBIpy (https://github.com/libChEBI/libChEBIpy) """ if molec.chebiID is None and molec.iupac_name is not None: # try one more time for chebi ID chebiID = get_chebiID(mol_name=molec.name, iupac_name=molec.iupac_name) if chebiID is None: print('cannot get structure from chebi') return None molec.chebiID = [chebiID] # at this point, molec.chebiID will be a list - iterarte over it # the iteration stops if any CHEBI ID produces a structure for CID in molec.chebiID: if CID == '' or ' ' in CID or 'null' in CID: print(CID, '- not a real CHEBI ID') continue # get entity with chebiID entity = ChebiEntity(CID) # check for parent ID entity, CID = convert_entity_to_parent(entity, ID=CID, CID=CID) # attemp to get structure # SMILES smile = entity.get_smiles() print('libchebipy result:', smile) if smile is not None: rdkitmol = Chem.MolFromSmiles(smile) if rdkitmol is None: print('structure could not be deciphered') molec.SMILES = smile molec.mol = None continue else: rdkitmol.Compute2DCoords() molec.SMILES = smile # remove molecules with generalised atoms if '*' in smile: molec.mol = None else: molec.mol = rdkitmol elif smile is None: print('molecule does not have recorded structure in CHEBI DB') print('probably a generic structure - skipping.') molec.SMILES = smile molec.mol = None continue # set passed = True if this chebi ID produced a structure # would not get up to this point if it didnt # if not CIDs pass then the chebiIDs remain a list and will # fail the # next step passed = True # set molecule properties if passed: molec.chebiID = CID molec.DB_ID = CID # save InChiKey iKEY = entity.get_inchi_key() if iKEY is not None: molec.InChiKey = iKEY # save inchi inchi = entity.get_inchi() if inchi is not None: molec.InChi = inchi # set name if name is only a code at this point try: if molec.change_name is True: molec.name = entity.get_name() molec.change_name = False except AttributeError: molec.change_name = False return None
def setUp(self): '''COMMENT''' self.__existing = ChebiEntity('4167') self.__secondary = ChebiEntity('CHEBI:5585')
def test_search_hexenal_inexact(self): '''Test search method for (E)-2-Hexenal.''' results = libchebipy.search('(E)-2-Hexenal', False) self.assertIn(ChebiEntity('CHEBI:28913'), results)
def get_cmpd_information_offline(molec): """Get information from CHEBI Database of a compound from CHEBI ID. Done Offline unless necessary. molec must have attribute 'chebiID' as integer. """ DB_prop = DB_functions.get_DB_prop('CHEBI') compounds_file = DB_prop[0] + DB_prop[1]['cmpds_file'] names_file = DB_prop[0] + DB_prop[1]['names_file'] structures_file = DB_prop[0] + DB_prop[1]['strct_file'] # set name by searching compound file res = search_for_compound_by_id(compounds_file, molec.chebiID) if res is None: print('chebiID not found:', molec.chebiID) print('no match in DB - ' 'this should not happen for CHEBI ID search') print('check this!') print('Exitting....') import sys sys.exit() else: ID, parent_id, name, star = res molec.name = name molec.change_name = False # make sure is parent compound if parent_id != 'null': res = convert_nameID_to_parent(compounds_file, nameID=ID) if res is None: print("this should not happen - error with cross reference") print('check this!') print('Exitting....') import sys sys.exit() ID, parent_id, name, star = res molec.name = name molec.change_name = False molec.chebiID = int(ID) # get structure using CHEBI ID # structures.csv - read in, get COMPOUND ID match then extract the # get SMILES structure, s_type = get_structure(structures_file, molec.chebiID) print(structure, s_type) if structure is not None: # is structure a MolBlock or Smiles if s_type == 'mol': # convert structure to SMILEs rdkitmol = Chem.MolFromMolBlock(structure) if rdkitmol is None: print('structure could not be deciphered') smile = None molec.SMILES = smile molec.mol = None print('probably a polymeric structure - skipping.') else: rdkitmol.Compute2DCoords() smile = Chem.MolToSmiles(rdkitmol) molec.SMILES = smile # remove molecules with generalised atoms if '*' in smile: molec.mol = None else: molec.mol = rdkitmol elif s_type == 'SMILES': smile = structure rdkitmol = Chem.MolFromSmiles(smile) if rdkitmol is None: print('structure could not be deciphered') molec.SMILES = smile molec.mol = None else: rdkitmol.Compute2DCoords() molec.SMILES = smile # remove molecules with generalised atoms if '*' in smile: molec.mol = None else: molec.mol = rdkitmol elif s_type == 'InChI': rdkitmol = Chem.MolFromInchi(structure) rdkitmol.Compute2DCoords() smile = Chem.MolToSmiles(rdkitmol) molec.SMILES = smile # remove molecules with generalised atoms if '*' in smile: molec.mol = None else: molec.mol = rdkitmol elif s_type == 'InChIKey': rdkitmol = Chem.MolFromInchi(structure) rdkitmol.Compute2DCoords() smile = None molec.SMILES = smile molec.mol = None print('molecule given as InChIKey - ambiguous') print('probably a generic structure - skipping.') else: # try using the CHEBI API # libChEBIpy (https://github.com/libChEBI/libChEBIpy) print('testing libchebipy...') entity = ChebiEntity(molec.chebiID) smile = entity.get_smiles() print('libchebipy result:', smile) if smile is not None: rdkitmol = Chem.MolFromSmiles(smile) if rdkitmol is None: print('structure could not be deciphered') molec.SMILES = smile molec.mol = None else: rdkitmol.Compute2DCoords() molec.SMILES = smile # remove molecules with generalised atoms if '*' in smile: molec.mol = None else: molec.mol = rdkitmol elif smile is None: molec.SMILES = smile molec.mol = None print('molecule does not have recorded structure in CHEBI DB') print('probably a generic structure - skipping.') # save InChiKey iKEY = entity.get_inchi_key() if iKEY is not None: molec.InChiKey = iKEY
def test_get_mol_existing(self): '''COMMENT''' chebi_id = 73938 this_chebi_entity = ChebiEntity(str(chebi_id)) self.assertEqual(this_chebi_entity.get_mol(), _read_mol_file(chebi_id))
def data(request, domain, id): file_header = '' if domain == 'papers': paper = get_object_or_404(Paper, pk=id) datasets = paper.dataset_set file_header = u'# Paper: %s (PMID %s)\n' % (paper, paper.pmid) if domain == 'datasets': # datasets = get_object_or_404(Dataset, pk=id) datasets = Dataset.objects.filter(pk=id) dataset = datasets.first() file_header = u'# Paper: %s (PMID %s)\n# Dataset: %s\n' % ( dataset.paper, dataset.paper.pmid, dataset) if domain == 'conditions': conditiontype = get_object_or_404(ConditionType, pk=id) datasets = conditiontype.datasets() file_header = u'# Condition: %s (ID %s)\n' % (conditiontype, conditiontype.id) if domain == 'chebi': chebi_entity = ChebiEntity('CHEBI:' + str(id)) children = [] for relation in chebi_entity.get_incomings(): if relation.get_type() == 'has_role': tid = relation.get_target_chebi_id() tid = int(filter(str.isdigit, tid)) children.append(tid) datasets = Dataset.objects.filter( conditionset__conditions__type__chebi_id__in=children) file_header = u'# Data for conditions annotated as %s (ChEBI:%s)\n' % ( chebi_entity.get_name(), id) if domain == 'phenotypes': phenotype = get_object_or_404(Observable, pk=id) datasets = phenotype.datasets() file_header = u'# Phenotype: %s (ID %s)\n' % (phenotype, phenotype.id) data = Data.objects.filter(dataset_id__in=datasets.values('id')).all() orfs = list(data.values_list('orf', flat=True).distinct()) datasets_ids = list( data.values_list('dataset_id', flat=True).order_by('dataset__paper').distinct()) matrix = [[None] * len(datasets_ids) for i in orfs] for datapoint in data: i = orfs.index(datapoint.orf) j = datasets_ids.index(datapoint.dataset_id) matrix[i][j] = datapoint.value column_headers = '\t' + '\t'.join([ u'%s' % get_object_or_404(Dataset, pk=dataset_id) for dataset_id in datasets_ids ]) + '\n' data_row = [] for i, orf in enumerate(orfs): new_row = orf + '\t' + '\t'.join([str(val) for val in matrix[i]]) print(new_row) data_row.append(new_row) txt3 = '\n'.join(data_row) response = HttpResponse(file_header + column_headers + txt3, content_type='text/plain') response[ 'Content-Disposition'] = 'attachment; filename="%s_%s_%s_data.txt"' % ( settings.DOWNLOAD_PREFIX, domain, id) return response
def test_get_charge_secondary2(self): '''COMMENT''' self.assertEquals(-2, ChebiEntity('43474').get_charge())
class TestChebiEntity(unittest.TestCase): '''COMMENT''' def setUp(self): '''COMMENT''' self.__existing = ChebiEntity('4167') self.__secondary = ChebiEntity('CHEBI:5585') def test_get_non_existing(self): '''COMMENT''' self.assertRaises(ChebiException, ChebiEntity, '-1') def test_get_id_existing(self): '''COMMENT''' self.assertTrue(self.__existing.get_id() == 'CHEBI:4167') def test_get_id_secondary(self): '''COMMENT''' self.assertTrue(self.__secondary.get_id() == 'CHEBI:5585') def test_get_formulae_existing(self): '''COMMENT''' this_formula = Formula('C6H12O6', 'KEGG COMPOUND') self.assertTrue(this_formula in self.__existing.get_formulae()) def test_get_formulae_secondary(self): '''COMMENT''' this_formula = Formula('H2O', 'ChEBI') self.assertTrue(this_formula in self.__secondary.get_formulae()) def test_get_formula_existing(self): '''COMMENT''' self.assertTrue(self.__existing.get_formula() == 'C6H12O6') def test_get_formula_secondary(self): '''COMMENT''' self.assertTrue(self.__secondary.get_formula() == 'H2O') def test_get_mass_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_mass(), 180.15588) def test_get_mass_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_mass(), 18.01530) def test_get_charge_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_charge(), 0) def test_get_charge_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_charge(), 0) def test_get_charge_secondary2(self): '''COMMENT''' self.assertEquals(-2, ChebiEntity('43474').get_charge()) def test_get_comments_existing(self): '''COMMENT''' this_chebi_entity = ChebiEntity('29044') this_comment = Comment('General', 'General', 'The substituent name \'3-oxoprop-2-enyl\' is ' 'incorrect but is used in various databases.', datetime.datetime.strptime('2005-03-18', '%Y-%M-%d')) self.assertTrue(this_comment in this_chebi_entity.get_comments()) def test_get_comments_secondary(self): '''COMMENT''' this_chebi_entity = ChebiEntity('11505') this_comment = Comment('General', 'General', 'The substituent name \'3-oxoprop-2-enyl\' is ' 'incorrect but is used in various databases.', datetime.datetime.strptime('2005-03-18', '%Y-%M-%d')) self.assertTrue(this_comment in this_chebi_entity.get_comments()) def test_get_source_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_source(), 'KEGG COMPOUND') def test_get_source_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_source(), 'KEGG COMPOUND') def test_get_prnt_id_existing(self): '''COMMENT''' self.assertIsNone(self.__existing.get_parent_id()) def test_get_prnt_id_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_parent_id(), 'CHEBI:15377') def test_get_name_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_name(), 'D-glucopyranose') def test_get_name_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_name(), 'water') def test_get_definition_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_definition(), 'A glucopyranose having D-configuration.') def test_get_definition_secondary(self): '''COMMENT''' this_chebi_entity = ChebiEntity('41140') self.assertEqual(this_chebi_entity.get_definition(), 'D-Glucopyranose with beta configuration at the ' 'anomeric centre.') def test_get_mod_on_existing(self): '''COMMENT''' self.assertTrue(self.__existing.get_modified_on() > datetime.datetime.strptime('2014-01-01', '%Y-%M-%d')) def test_get_mod_on_secondary(self): '''COMMENT''' self.assertIsNotNone(self.__secondary.get_modified_on()) def test_get_created_by_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_created_by(), 'CHEBI') def test_get_created_by_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_created_by(), 'ops$mennis') def test_get_star_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_star(), 3) def test_get_star_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_star(), 3) def test_get_db_acc_existing(self): '''COMMENT''' dat_acc = DatabaseAccession('MetaCyc accession', 'D-Glucose', 'MetaCyc') self.assertTrue(dat_acc in self.__existing.get_database_accessions()) def test_get_db_acc_secondary(self): '''COMMENT''' dat_acc = DatabaseAccession('MetaCyc accession', 'WATER', 'MetaCyc') self.assertTrue(dat_acc in self.__secondary.get_database_accessions()) def test_get_inchi_existing(self): '''COMMENT''' inchi = 'InChI=1S/C6H12O6/c7-1-2-3(8)4(9)5(10)6(11)12-2/' + \ 'h2-11H,1H2/t2-,3-,4+,5-,6?/m1/s1' self.assertEqual(self.__existing.get_inchi(), inchi) def test_get_inchi_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_inchi(), 'InChI=1S/H2O/h1H2') def test_get_inchi_key_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_inchi_key(), 'WQZGKKKJIJFFOK-GASJEMHNSA-N') def test_get_inchi_key_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_inchi_key(), 'XLYOFNOQVPJJNP-UHFFFAOYSA-N') def test_get_smiles_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_smiles(), 'OC[C@H]1OC(O)[C@H](O)[C@@H](O)[C@@H]1O') def test_get_smiles_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_smiles(), '[H]O[H]') def test_get_mol_existing(self): '''COMMENT''' chebi_id = 73938 this_chebi_entity = ChebiEntity(str(chebi_id)) self.assertEqual(this_chebi_entity.get_mol(), _read_mol_file(chebi_id)) def test_get_mol_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_mol(), _read_mol_file(15377)) def test_get_mol_file_existing(self): '''COMMENT''' chebi_id = 73938 self.__get_mol_file(chebi_id, chebi_id) def test_get_mol_file_secondary(self): '''COMMENT''' read_id = 15377 retrieved_id = 42857 self.__get_mol_file(read_id, retrieved_id) def test_get_names_existing(self): '''COMMENT''' this_name = Name('Grape sugar', 'SYNONYM', 'KEGG COMPOUND', False, 'en') self.assertTrue(this_name in self.__existing.get_names()) def test_get_names_secondary(self): '''COMMENT''' this_name = Name('eau', 'SYNONYM', 'ChEBI', False, 'fr') self.assertTrue(this_name in self.__secondary.get_names()) def test_get_references_existing(self): '''COMMENT''' this_chebi_entity = ChebiEntity('15347') this_reference = Reference('WO2006008754', 'Patent', '', 'NOVEL INTERMEDIATES FOR LINEZOLID ' 'AND RELATED COMPOUNDS') self.assertTrue(this_reference in this_chebi_entity.get_references()) def test_get_references_secondary(self): '''COMMENT''' this_chebi_entity = ChebiEntity('22182') this_reference = Reference('WO2006008754', 'Patent', '', 'NOVEL INTERMEDIATES FOR LINEZOLID ' 'AND RELATED COMPOUNDS') self.assertTrue(this_reference in this_chebi_entity.get_references()) def test_get_cmp_orig_existing(self): '''COMMENT''' this_compound_origin = CompoundOrigin('H**o sapiens', 'NCBI:9606', None, None, None, None, 'DOI', '10.1038/nbt.2488', None) self.assertTrue(this_compound_origin in self.__existing.get_compound_origins()) def test_get_cmp_orig_secondary(self): '''COMMENT''' this_compound_origin = CompoundOrigin('H**o sapiens', 'NCBI:9606', None, None, None, None, 'DOI', '10.1038/nbt.2488', None) self.assertTrue(this_compound_origin in self.__secondary.get_compound_origins()) def test_get_out_existing(self): '''COMMENT''' this_relation = Relation('is_a', '17634', 'C') self.assertTrue(this_relation in self.__existing.get_outgoings()) def test_get_out_secondary(self): '''COMMENT''' this_relation = Relation('has_role', 'CHEBI:48360', 'C') self.assertTrue(this_relation in self.__secondary.get_outgoings()) def test_get_in_existing(self): '''COMMENT''' this_relation = Relation('has_functional_parent', 'CHEBI:15866', 'C') self.assertTrue(this_relation in self.__existing.get_incomings()) def test_get_in_secondary(self): '''COMMENT''' this_relation = Relation('is_conjugate_acid_of', '29412', 'C') self.assertTrue(this_relation in self.__secondary.get_incomings()) def __get_mol_file(self, read_id, retrieved_id): '''COMMENT''' mol_read = _read_mol_file(read_id) this_chebi_entity = ChebiEntity(str(retrieved_id)) textfile_retrieved = open(this_chebi_entity.get_mol_filename(), 'r') mol_retrieved = textfile_retrieved.read() textfile_retrieved.close() self.assertEquals(mol_read, mol_retrieved)
def test_search_hexenal_exact(self): '''Test search method for (E)-2-Hexenal.''' results = libchebipy.search('(E)-2-Hexenal', True) self.assertTrue(ChebiEntity('CHEBI:28913'), results[0] if results else None)
def setUp(self): '''COMMENT''' self.__existing = ChebiEntity('4167') self.__secondary = ChebiEntity('CHEBI:5585')
class TestChebiEntity(unittest.TestCase): '''COMMENT''' def setUp(self): '''COMMENT''' self.__existing = ChebiEntity('4167') self.__secondary = ChebiEntity('CHEBI:5585') def test_get_non_existing(self): '''COMMENT''' self.assertRaises(ChebiException, ChebiEntity, '-1') def test_get_id_existing(self): '''COMMENT''' self.assertTrue(self.__existing.get_id() == 'CHEBI:4167') def test_get_id_secondary(self): '''COMMENT''' self.assertTrue(self.__secondary.get_id() == 'CHEBI:5585') def test_get_formulae_existing(self): '''COMMENT''' this_formula = Formula('C6H12O6', 'KEGG COMPOUND') self.assertTrue(this_formula in self.__existing.get_formulae()) def test_get_formulae_secondary(self): '''COMMENT''' this_formula = Formula('H2O', 'ChEBI') self.assertTrue(this_formula in self.__secondary.get_formulae()) def test_get_formula_existing(self): '''COMMENT''' self.assertTrue(self.__existing.get_formula() == 'C6H12O6') def test_get_formula_secondary(self): '''COMMENT''' self.assertTrue(self.__secondary.get_formula() == 'H2O') def test_get_mass_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_mass(), 180.15588) def test_get_mass_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_mass(), 18.01530) def test_get_charge_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_charge(), 0) def test_get_charge_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_charge(), 0) def test_get_charge_secondary2(self): '''COMMENT''' self.assertEquals(-2, ChebiEntity('43474').get_charge()) def test_get_comments_existing(self): '''COMMENT''' this_chebi_entity = ChebiEntity('29044') this_comment = Comment( 'General', 'General', 'The substituent name \'3-oxoprop-2-enyl\' is ' 'incorrect but is used in various databases.', datetime.datetime.strptime('2005-03-18', '%Y-%M-%d')) self.assertTrue(this_comment in this_chebi_entity.get_comments()) def test_get_comments_secondary(self): '''COMMENT''' this_chebi_entity = ChebiEntity('11505') this_comment = Comment( 'General', 'General', 'The substituent name \'3-oxoprop-2-enyl\' is ' 'incorrect but is used in various databases.', datetime.datetime.strptime('2005-03-18', '%Y-%M-%d')) self.assertTrue(this_comment in this_chebi_entity.get_comments()) def test_get_source_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_source(), 'KEGG COMPOUND') def test_get_source_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_source(), 'KEGG COMPOUND') def test_get_prnt_id_existing(self): '''COMMENT''' self.assertIsNone(self.__existing.get_parent_id()) def test_get_prnt_id_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_parent_id(), 'CHEBI:15377') def test_get_name_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_name(), 'D-glucopyranose') def test_get_name_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_name(), 'water') def test_get_definition_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_definition(), 'A glucopyranose having D-configuration.') def test_get_definition_secondary(self): '''COMMENT''' this_chebi_entity = ChebiEntity('41140') self.assertEqual( this_chebi_entity.get_definition(), 'D-Glucopyranose with beta configuration at the ' 'anomeric centre.') def test_get_mod_on_existing(self): '''COMMENT''' self.assertTrue(self.__existing.get_modified_on() > datetime.datetime.strptime('2014-01-01', '%Y-%M-%d')) def test_get_mod_on_secondary(self): '''COMMENT''' self.assertIsNotNone(self.__secondary.get_modified_on()) def test_get_created_by_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_created_by(), 'CHEBI') def test_get_created_by_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_created_by(), 'ops$mennis') def test_get_star_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_star(), 3) def test_get_star_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_star(), 3) def test_get_db_acc_existing(self): '''COMMENT''' dat_acc = DatabaseAccession('MetaCyc accession', 'D-Glucose', 'MetaCyc') self.assertTrue(dat_acc in self.__existing.get_database_accessions()) def test_get_db_acc_secondary(self): '''COMMENT''' dat_acc = DatabaseAccession('MetaCyc accession', 'WATER', 'MetaCyc') self.assertTrue(dat_acc in self.__secondary.get_database_accessions()) def test_get_inchi_existing(self): '''COMMENT''' inchi = 'InChI=1S/C6H12O6/c7-1-2-3(8)4(9)5(10)6(11)12-2/' + \ 'h2-11H,1H2/t2-,3-,4+,5-,6?/m1/s1' self.assertEqual(self.__existing.get_inchi(), inchi) def test_get_inchi_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_inchi(), 'InChI=1S/H2O/h1H2') def test_get_inchi_key_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_inchi_key(), 'WQZGKKKJIJFFOK-GASJEMHNSA-N') def test_get_inchi_key_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_inchi_key(), 'XLYOFNOQVPJJNP-UHFFFAOYSA-N') def test_get_smiles_existing(self): '''COMMENT''' self.assertEqual(self.__existing.get_smiles(), 'OC[C@H]1OC(O)[C@H](O)[C@@H](O)[C@@H]1O') def test_get_smiles_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_smiles(), '[H]O[H]') def test_get_mol_existing(self): '''COMMENT''' chebi_id = 73938 this_chebi_entity = ChebiEntity(str(chebi_id)) self.assertEqual(this_chebi_entity.get_mol(), _read_mol_file(chebi_id)) def test_get_mol_secondary(self): '''COMMENT''' self.assertEqual(self.__secondary.get_mol(), _read_mol_file(15377)) def test_get_mol_file_existing(self): '''COMMENT''' chebi_id = 73938 self.__get_mol_file(chebi_id, chebi_id) def test_get_mol_file_secondary(self): '''COMMENT''' read_id = 15377 retrieved_id = 42857 self.__get_mol_file(read_id, retrieved_id) def test_get_names_existing(self): '''COMMENT''' this_name = Name('Grape sugar', 'SYNONYM', 'KEGG COMPOUND', False, 'en') self.assertTrue(this_name in self.__existing.get_names()) def test_get_names_secondary(self): '''COMMENT''' this_name = Name('eau', 'SYNONYM', 'ChEBI', False, 'fr') self.assertTrue(this_name in self.__secondary.get_names()) def test_get_references_existing(self): '''COMMENT''' this_chebi_entity = ChebiEntity('15347') this_reference = Reference( 'WO2006008754', 'Patent', '', 'NOVEL INTERMEDIATES FOR LINEZOLID ' 'AND RELATED COMPOUNDS') self.assertTrue(this_reference in this_chebi_entity.get_references()) def test_get_references_secondary(self): '''COMMENT''' this_chebi_entity = ChebiEntity('22182') this_reference = Reference( 'WO2006008754', 'Patent', '', 'NOVEL INTERMEDIATES FOR LINEZOLID ' 'AND RELATED COMPOUNDS') self.assertTrue(this_reference in this_chebi_entity.get_references()) def test_get_cmp_orig_existing(self): '''COMMENT''' this_compound_origin = CompoundOrigin('H**o sapiens', 'NCBI:9606', None, None, None, None, 'DOI', '10.1038/nbt.2488', None) self.assertTrue( this_compound_origin in self.__existing.get_compound_origins()) def test_get_cmp_orig_secondary(self): '''COMMENT''' this_compound_origin = CompoundOrigin('H**o sapiens', 'NCBI:9606', None, None, None, None, 'DOI', '10.1038/nbt.2488', None) self.assertTrue( this_compound_origin in self.__secondary.get_compound_origins()) def test_get_out_existing(self): '''COMMENT''' this_relation = Relation('is_a', '17634', 'C') self.assertTrue(this_relation in self.__existing.get_outgoings()) def test_get_out_secondary(self): '''COMMENT''' this_relation = Relation('has_role', 'CHEBI:48360', 'C') self.assertTrue(this_relation in self.__secondary.get_outgoings()) def test_get_in_existing(self): '''COMMENT''' this_relation = Relation('has_functional_parent', 'CHEBI:15866', 'C') self.assertTrue(this_relation in self.__existing.get_incomings()) def test_get_in_secondary(self): '''COMMENT''' this_relation = Relation('is_conjugate_acid_of', '29412', 'C') self.assertTrue(this_relation in self.__secondary.get_incomings()) def __get_mol_file(self, read_id, retrieved_id): '''COMMENT''' mol_read = _read_mol_file(read_id) this_chebi_entity = ChebiEntity(str(retrieved_id)) textfile_retrieved = open(this_chebi_entity.get_mol_filename(), 'r') mol_retrieved = textfile_retrieved.read() textfile_retrieved.close() self.assertEquals(mol_read, mol_retrieved)
def convert_Chebi2formula(ChebiID): CE = ChebiEntity(ChebiID) formula = CE.get_formula() return(formula)
def definition(self): if self.chebi_id: entity = ChebiEntity('CHEBI:' + str(self.chebi_id)) return entity.get_definition() else: return ''