def matchSpeciesChebi(s1, s2, logging=False): """ Match two Chebi identifiers. If matching returns the chebi information of the identifier. :param s1: first chebi id :type s1: str :param s2: second chebi id :type s2: str :param logging: log messages to console :type logging: bool :return: dictionary of chebi information, returns None if no match :rtype: dict """ ch = bioservices.ChEBI() ch1 = getChebiId(s1) ch2 = getChebiId(s2) if not ch1 or not ch2: return None if logging: print('Comparing %s (%s) with %s (%s)' % (s1.getId(), ch1, s2.getId(), ch2)) try: entry = ch.getCompleteEntity(ch1) exact = [] if ch1 == ch2: exact.append({'id': s2.getId()}) children = [] if hasattr(entry, 'OntologyChildren'): for child in entry.OntologyChildren: if child['chebiId'] == ch2: children.append({'id': s2.getId(), 'data': child}) parents = [] if (hasattr(entry, 'OntologyParents')): for parent in entry.OntologyParents: if parent['chebiId'] == ch2: parents.append({'id': s2.getId(), 'data': parent}) return { 'id': s1.getId(), 'chebi_name': entry.chebiAsciiName, 'exact': exact, 'children': children, 'parents': parents } except: print("Unexpected error:", sys.exc_info()[0]) return None
def __init__(self, debug=False): """ Creates and displays the search form. """ self.debug = debug self.s = bioservices.BioModels() self.ch = bioservices.ChEBI() # Define widgets # <Search> self.wSearchTerm = w.Text(description='Search biomodels by species:', value="CHEBI:17925") self.wSearchTerm.on_submit(self.searchChebi) self.wSearchButton = w.Button(description='Search') self.wSearchButton.on_click(self.searchChebi) self.wSearchChebi = w.HBox( children=[self.wSearchTerm, self.wSearchButton]) self.wSelectChebis = w.Select(description='Matching ChEBI:', width='600px', height='250px') # FIXME: update the deprecated functions self.wSelectChebis.on_trait_change(self.selectChebi) self.wSelectModels = w.Select(description='Matching BioModels:', width='200px') self.wSelectModels.on_trait_change(self.selectedModel) # <Model> self.wModelId = w.Text(description='Model ID:', value="No model selected") self.wModelCode = w.Text(description='Install Code:') self.wModelImport = w.Text(description='Import module code:') self.wModelSbml = w.Textarea(description='Model SBML:', width='800px', height='300px') # <Container> self.wModel = w.FlexBox(children=[ self.wModelId, self.wModelCode, self.wSelectModels, self.wModelImport, self.wModelSbml ]) for ch in self.wModel.children: ch.font_family = 'monospace' ch.color = '#AAAAAA' ch.background_color = 'black' self.wContainer = w.FlexBox( children=[self.wSearchChebi, self.wSelectChebis, self.wModel]) # display the widgets display(self.wContainer) # clear notebook output clear_output()
def resolveAnnotationHelper(annotation): if not hasattr(resolveAnnotation, "db"): resolveAnnotation.db = {} resolveAnnotation.ch = bioservices.ChEBI(verbose=False) resolveAnnotation.uni = bioservices.UniProt(verbose=False) resolveAnnotation.k = bioservices.kegg.KEGG(verbose=False) resolveAnnotation.qg = bioservices.QuickGO(verbose=False) resolveAnnotation.t = bioservices.Taxon() resolveAnnotation.db[ "http://identifiers.org/uniprot/P62988" ] = "http://identifiers.org/uniprot/P62988" resolveAnnotation.db[ "http://identifiers.org/uniprot/P06842" ] = "http://identifiers.org/uniprot/P06842" resolveAnnotation.db[ "http://identifiers.org/uniprot/P07006" ] = "http://identifiers.org/uniprot/P06842" if annotation in resolveAnnotation.db: return annotation, resolveAnnotation.db[annotation] tAnnotation = annotation.replace("%3A", ":") tAnnotation = annotation.split("/")[-1] # tAnnotation = re.search(':([^:]+:[^:]+$)',tAnnotation).group(1) try: if "obo.go" in annotation or "/go/GO" in annotation: res = resolveAnnotation.qg.Term(tAnnotation) finalArray = [] if type(res) not in [int]: res = bioservices.Service("name").easyXML(res) tmp = res.findAll("name") for x in tmp: try: tagString = str(goGrammar.parseString(str(x))[0]) if tagString not in ["Systematic synonym"]: finalArray.append(str(goGrammar.parseString(str(x))[0])) except pyp.ParseBaseException: continue if len(finalArray) > 0: resolveAnnotation.db[annotation] = finalArray[0] else: resolveAnnotation.db[annotation] = "" finalAnnotation = resolveAnnotation.db[annotation] elif "kegg" in annotation: data = resolveAnnotation.k.get(tAnnotation) dict_data = resolveAnnotation.k.parse(data) if type(dict_data) == int: resolveAnnotation.db[annotation] = "" else: resolveAnnotation.db[annotation] = dict_data["name"] finalAnnotation = resolveAnnotation.db[annotation] elif "uniprot" in annotation: identifier = annotation.split("/")[-1] result = resolveAnnotation.uni.quick_search(identifier) if identifier in result: resolveAnnotation.db[annotation] = result[identifier][ "Protein names" ].split("(")[0] else: finalAnnotation = "" finalAnnotation = resolveAnnotation.db[annotation] elif "chebi" in annotation: tmp = annotation.split("/")[-1] entry = resolveAnnotation.ch.getLiteEntity(tmp) finalAnnotation = "" for element in entry: resolveAnnotation.db[annotation] = str(element["chebiAsciiName"]) finalAnnotation = resolveAnnotation.db[annotation] elif ( "cco" in annotation or "pirsf" in annotation or "pubchem" in annotation or "omim" in annotation ): finalAnnotation = "" elif "taxonomy" in annotation: # uniprot stuff for taxonomy result = resolveAnnotation.t.search_by_taxon(tAnnotation) resolveAnnotation.db[annotation] = result["Scientific Name"] finalAnnotation = resolveAnnotation.db[annotation] """ url = 'http://www.uniprot.org/taxonomy/' params = { 'from':'ACC', 'to':'P_REFSEQ_AC', 'format':'tab', 'query':'P13368 P20806 Q9UM73 P97793 Q17192' } data = urllib.urlencode(params) request = urllib2.Request(url, data) contact = "" # Please set your email address here to help us debug in case of problems. request.add_header('User-Agent', 'Python contact') response = urllib2.urlopen(request) page = response.read(200000) """ else: return annotation, "" # assert(False) finalAnnotation = "" except (IOError, KeyError) as e: return annotation, "" return annotation, finalAnnotation
def resolveAnnotationHelper(annotation): if not hasattr(resolveAnnotation, 'db'): resolveAnnotation.db = {} resolveAnnotation.ch = bioservices.ChEBI(verbose=False) resolveAnnotation.uni = bioservices.UniProt(verbose=False) resolveAnnotation.k = bioservices.kegg.KEGG(verbose=False) resolveAnnotation.qg = bioservices.QuickGO(verbose=False) resolveAnnotation.t = bioservices.Taxon() resolveAnnotation.db[ 'http://identifiers.org/uniprot/P62988'] = 'http://identifiers.org/uniprot/P62988' resolveAnnotation.db[ 'http://identifiers.org/uniprot/P06842'] = 'http://identifiers.org/uniprot/P06842' resolveAnnotation.db[ 'http://identifiers.org/uniprot/P07006'] = 'http://identifiers.org/uniprot/P06842' if annotation in resolveAnnotation.db: return annotation, resolveAnnotation.db[annotation] tAnnotation = annotation.replace('%3A', ':') tAnnotation = annotation.split('/')[-1] #tAnnotation = re.search(':([^:]+:[^:]+$)',tAnnotation).group(1) try: if 'obo.go' in annotation or '/go/GO' in annotation: res = resolveAnnotation.qg.Term(tAnnotation) finalArray = [] if type(res) not in [int]: res = bioservices.Service('name').easyXML(res) tmp = res.findAll('name') for x in tmp: try: tagString = str(goGrammar.parseString(str(x))[0]) if tagString not in ['Systematic synonym']: finalArray.append( str(goGrammar.parseString(str(x))[0])) except pyp.ParseBaseException: continue if len(finalArray) > 0: resolveAnnotation.db[annotation] = finalArray[0] else: resolveAnnotation.db[annotation] = '' finalAnnotation = resolveAnnotation.db[annotation] elif 'kegg' in annotation: data = resolveAnnotation.k.get(tAnnotation) dict_data = resolveAnnotation.k.parse(data) if type(dict_data) == int: resolveAnnotation.db[annotation] = '' else: resolveAnnotation.db[annotation] = dict_data['name'] finalAnnotation = resolveAnnotation.db[annotation] elif 'uniprot' in annotation: identifier = annotation.split('/')[-1] result = resolveAnnotation.uni.quick_search(identifier) if identifier in result: resolveAnnotation.db[annotation] = result[identifier][ 'Protein names'].split('(')[0] else: finalAnnotation = '' finalAnnotation = resolveAnnotation.db[annotation] elif 'chebi' in annotation: tmp = annotation.split('/')[-1] entry = resolveAnnotation.ch.getLiteEntity(tmp) finalAnnotation = '' for element in entry: resolveAnnotation.db[annotation] = str( element['chebiAsciiName']) finalAnnotation = resolveAnnotation.db[annotation] elif 'cco' in annotation or 'pirsf' in annotation or 'pubchem' in annotation or 'omim' in annotation: finalAnnotation = '' elif 'taxonomy' in annotation: #uniprot stuff for taxonomy result = resolveAnnotation.t.search_by_taxon(tAnnotation) resolveAnnotation.db[annotation] = result['Scientific Name'] finalAnnotation = resolveAnnotation.db[annotation] ''' url = 'http://www.uniprot.org/taxonomy/' params = { 'from':'ACC', 'to':'P_REFSEQ_AC', 'format':'tab', 'query':'P13368 P20806 Q9UM73 P97793 Q17192' } data = urllib.urlencode(params) request = urllib2.Request(url, data) contact = "" # Please set your email address here to help us debug in case of problems. request.add_header('User-Agent', 'Python contact') response = urllib2.urlopen(request) page = response.read(200000) ''' else: return annotation, '' #assert(False) finalAnnotation = '' except (IOError, KeyError) as e: return annotation, '' return annotation, finalAnnotation