def getSBMLFromBiomodelsURN(urn): """ Get the SBML from a given BioModels URN. Searches for a BioModels identifier in the given urn and retrieves the SBML from biomodels. :param urn: :type urn: :return: SBML string fro given model urn :rtype: str """ pattern = "((BIOMD|MODEL)\d{10})|(BMID\d{12})" match = re.search(pattern, urn) mid = match.group(0) try: # read SBML via bioservices import bioservices biomodels = bioservices.BioModels() sbml = biomodels.getModelSBMLById(mid) sbml = sbml.encode('utf8') except ImportError: # if there are issues with bioservice import, do a workaround # see https://github.com/sys-bio/tellurium/issues/125 import httplib conn = httplib.HTTPConnection("www.ebi.ac.uk") conn.request("GET", "/biomodels-main/download?mid=" + mid) r1 = conn.getresponse() # print(r1.status, r1.reason) sbml = r1.read() conn.close() return str(sbml)
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 compressionDistroAnalysis(directory): ''' analyses the model compression distribution (number of molecule types)/total species acccording to what the model does ''' with open('{0}/sortedD.dump'.format(directory),'rb') as evaluationFile: ev1 = pickle.load(evaluationFile) ev2 = [] for x in ev1: try: ev2.append([x['index'],x['nreactions'],x['nspecies'],x['compression']]) except: continue number,rulesLength,speciesLength,evaluation2 = zip(*ev2) trueRatio = [] #with open('ratomization.dump','rb') as f: # ratomizationDict = pickle.load(f) for x,z,w in zip(rulesLength,number,evaluation2): if x>=0: trueRatio.append([z,1-w]) bio = bioservices.BioModels() modelAnnotationArray = [] for element,compression in trueRatio: try: info = bio.getSimpleModelsByIds('BIOMD%010i' % element) parsedInfo = bioservices.Service('name').easyXML(info) modelName = removeTags(str(parsedInfo['modelname'][0])) modelName = modelName.split(' - ') if len(modelName) > 1: modelName = modelName[1] else: modelName = modelName[0].split('_') if len(modelName) > 1: modelName = ' '.join(modelName[1:]) else: modelName = modelName[0] modelAnnotationArray.append([element,compression,modelName]) print modelAnnotationArray[-1] except: print parsedInfo break with open('compressionAnnotation.dump','wb') as f: pickle.dump(modelAnnotationArray,f)
def download_models(): bio = bioservices.BioModels() print 'The number of models in biomodels right now is {}'.format(len(bio)) m = bio.getAllCuratedModelsId() print 'The number of curated models in biomodels is: {}'.format(len(m)) model_dct = {} skipped = 0 for i in m: os.chdir(d) dire = os.path.join(d, i) if os.path.isdir(dire) == False: os.mkdir(dire) else: skipped += 1 continue models_to_skip = ['BIOMD0000000241', 'BIOMD0000000148'] if i in models_to_skip: ''' This file is broken and doesn't simulate with CopasiSE ''' continue try: name = bio.getModelNameById(i) strings = '\[\]_\{\}' name = re.sub(strings, '_', name) model_dct[name] = bio.getModelSBMLById(i) print 'downloading {}:\t{}'.format(i, name.encode('utf8')) fle = os.path.join(dire, name + '.xml') if os.path.isfile(fle) != True: with open(fle, 'w') as f: f.write(model_dct[name].encode('utf8')) time.sleep(0.25) except: continue print 'You have downloaded {} out of {} models'.format( len(model_dct.keys()), len(m)) print 'you have skipped {} models because you already have a folder for them'.format( skipped) df = pandas.DataFrame.from_dict(model_dct.keys()) xlsx = os.path.join(d, 'ModelsMap.xlsx') df.to_excel(xlsx, index=True, header=True) return df
def download_models(directory, percent=100, SKIP_ALREADY_DOWNLOADED=True): ''' download curated models from biomodels curated section args: directory: Name of directory to download models too =============================================================== Returns: df: containing models pickle: save to file and contains models ''' if percent > 100 or percent < 0: raise TypeError('percent should be between 0 and 100') try: import bioservices except ImportError: ## install bioservices if it doesn't already exist ##May need to do this with admin rights os.system('pip install bioservices') import bioservices except WindowsError: raise ImportError( 'Need bioservices module to download biomodels database. Use pip install bioservices with admin rights' ) ## create directory if not exist if os.path.isdir(directory) != True: os.makedirs(directory) ## change to directory os.chdir(directory) ## get BioModels service bio = bioservices.BioModels() print 'The number of models in biomodels right now is {}'.format(len(bio)) model = bio.getAllCuratedModelsId() print 'The number of curated models in biomodels is: {}'.format(len(model)) per = len(model) // 100.0 * percent print 'You are about to download {} models'.format(per) model_dct = {} model_files = [] skipped = 0 if percent == 100: models = model else: models = model[:int(per)] for i in enumerate(models): os.chdir(directory) author = bio.getAuthorsByModelId(i[1]) author = RemoveNonAscii(author[0]).filter dire = os.path.join(directory, i[1] + author) # if SKIP_ALREADY_DOWNLOADED: if os.path.isdir(dire) == False: os.mkdir(dire) else: if SKIP_ALREADY_DOWNLOADED: skipped += 1 continue models_to_skip = ['BIOMD0000000241', 'BIOMD0000000148'] #these cause python to crash if i[1] in models_to_skip: ''' These file is broken and doesn't simulate with CopasiSE ''' continue try: model_dct[author] = bio.getModelSBMLById(i[1]) print 'downloading model {} of {}: {}:\t{}'.format( i[0], per, i[1], author.encode('utf8')) fle = os.path.join(dire, author + '.xml') print fle if os.path.isfile(fle) != True: with open(fle, 'w') as f: f.write(model_dct[author].encode('utf8')) time.sleep(0.25) model_files.append(fle) print 'saved to : {}'.format(fle) except UnicodeEncodeError: print 'model with author {} skipped as the name contains non-ascii characters'.format( author) continue print 'You have downloaded {} out of {} models'.format( len(model_dct.keys()), len(model)) print 'you have skipped {} models because you already have a folder for them'.format( skipped) df = pandas.DataFrame(model_files) pickle_file = os.path.join(directory, 'BioModelsFilesPickle.pickle') df.to_pickle(pickle_file) return df