Package: drugbank-downloader
drugbank_downloaderDon't worry about DrugBank licensing and distribution rules - just usedrugbank_downloaderto write code that knows
how to download it and use it automatically.Installation$pipinstalldrugbank-downloaderDownload A Specific Versionimportosfromdrugbank_downloaderimportdownload_drugbankusername=...# suggestion: load from environment with os.getenv('DRUGBANK_USERNAME')password=...path=download_drugbank(version='5.1.7',username=username,password=password)# This is where it gets downloaded: ~/.data/drugbank/5.1.7/full database.xml.zipexpected_path=os.path.join(os.path.expanduser('~'),'.data','drugbank','5.1.7','full database.xml.zip')assertexpected_path==path.as_posix()After it's been downloaded once, it's smart and doesn't need to download again. It gets stored
usingpystowautomatically in the~/.data/drugbankdirectory.Automating Configuration of DrugBank CredentialsThere are two ways to automatically set the username and password so you don't have to worry about getting it and
passing it around in your python code:SetDRUGBANK_USERNAMEandDRUGBANK_PASSWORDin the environmentCreate~/.config/drugbank.iniand set in the[drugbank]section ausernameandpasswordkey.fromdrugbank_downloaderimportdownload_drugbank# Same path as beforepath=download_drugbank(version='5.1.7')Theusernameandpasswordkeyword arguments are available for all functions in this package, but will be omitted in
the tutorial for brevity.Download the Latest VersionFirst, you'll have to installbioversionswithpip install bioversions, whose job it is to look up the latest version of many databases. Then, you can modify
the previous code slightly by omitting theversionkeyword argument:importosfromdrugbank_downloaderimportdownload_drugbankpath=download_drugbank()# This is where it gets downloaded: ~/.data/drugbank/5.1.7/full database.xml.zip based on the latest# version, as of December 14th, 2020.expected_path=os.path.join(os.path.expanduser('~'),'.data','drugbank','5.1.7','full database.xml.zip')assertexpected_path==path.as_posix()Theversionkeyword argument is available for all functions in this package, but like the username and password will
be omitted for brevity.Don't Bother Unpacking - readfull database.xml.zipDirectlyDrugBank is a single XML (could be JSON in a better future) file inside a zip archive. Normally, people manually unzip
this folder then do something with the resulting file. Don't do this, it's not reproducible!
Instead, it can be opened as a file object in Python with the following code.importzipfilefromdrugbank_downloaderimportdownload_drugbankpath=download_drugbank()withzipfile.ZipFile(path)aszip_file:withzip_file.open('full database.xml')asfile:pass# do something with the fileYou don't have time to remember this. Just usedrugbank_downloader.open_drugbank()instead:fromdrugbank_downloaderimportopen_drugbankwithopen_drugbank()asfile:pass# do something with the file, same as aboveReading DrugBank's XMLAfter you've opened the file, you probably want to read it with an XML parser like:fromxml.etreeimportElementTreefromdrugbank_downloaderimportopen_drugbankwithopen_drugbank()asfile:tree=ElementTree.parse(file)You don't have time to remember this either. Just usedrugbank_downloader.parse_drugbank()instead:fromxml.etreeimportElementTreefromdrugbank_downloaderimportparse_drugbanktree=parse_drugbank()root=tree.getroot()If your first thing to do to the tree is always to get its root, just usedrugbank_downloader.get_drugbank_root():fromdrugbank_downloaderimportget_drugbank_rootroot=get_drugbank_root()You now know everything I can teach you. Please use these tools to do re-usable, reproducible
science!Store in a Different PlaceIf you want to store the data elsewhere usingpystow(e.g., inpyoboI also keep a copy of this file), you can use theprefixargument.importosfromdrugbank_downloaderimportdownload_drugbankpath=download_drugbank(prefix=['pyobo','raw','drugbank'])# This is where it gets downloaded: ~/.data/pyobo/raw/drugbank/5.1.7/full database.xml.zipexpected_path=os.path.join(os.path.expanduser('~'),'.data','pyobo','raw','drugbank','5.1.7','full database.xml.zip')assertexpected_path==path.as_posix()See thepystowdocumentationon configuring the storage
location further.Download via CLIAfter installing, run the following CLI command to ensure it and send the path to stdout$drugbank_downloaderIf you haven't pre-configured the username and password, you can specify them with the--usernameand--passwordoptions.