Commit 7f798047 authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Implement resolve functionality

parent 64fc9d32
def get_install_location():
"""Finds the location directory of the tool"""
import os
script_path = os.path.realpath(__file__)
script_dir = os.path.dirname(script_path)
install_dir = os.path.dirname(script_dir)
return install_dir
def load_providers():
import yaml
data = []
with open(get_install_location() + '/providers.yaml') as data_file:
data = yaml.load(data_file)
return index_providers(data)
def index_providers(providers):
index = {}
for p in providers:
for db in p['prefixes']:
index[db] = p
return index
def resolve(strings):
from dbxref.config import load_providers
# read providers.json
providers = load_providers()
results = []
for s in strings:
dbxref = convert_string_to_dbxref(s)
if dbxref['db'] in providers:
provider = providers[dbxref['db']]
urls = []
for r in provider['resources']:
for url_template in provider['resources'][r]:
urls.append( {
'type': r,
'url': url_template.replace('%i', dbxref['id']).replace('%d', dbxref['db'])
} )
results.append({'dbxref': dbxref['db'] + ':' + dbxref['id'], 'locations': urls})
else:
print("No provider found")
return results
def convert_string_to_dbxref(string):
"""
A dbxref is dictionary with two keys: db and id.
"""
split = string.split(':', 1)
return {'db': split[0], 'id': split[1]}
- name: Enzyme
prefixes: [EC, Enzyme]
resources:
html: ["http://enzyme.expasy.org/EC/%i"]
- name: Gene Identifier
prefixes: [GI]
resources:
html: ["http://www.ncbi.nlm.nih.gov/protein/GI:%i"]
xml: ["http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&db=protein&dopt=xml&sort=&val=%i&retmode=file"]
- name: Uniprot
prefixes: ["UniProtKB/TrEMBL", "UniProtKB/Swiss-Prot"]
resources:
html: [ "http://www.uniprot.org/uniprot/%i"]
xml: [ "http://www.uniprot.org/uniprot/%i.xml"]
- name: Taxonomy
prefixes: ["Taxon", "taxon", "taxid"]
resources:
html: ["http://www.uniprot.org/taxonomy/%i"]
xml: ["http://www.uniprot.org/taxonomy/%i.rdf"]
- name: SequenceOntology
prefixes: ["SO"]
resources:
html: ["http://www.sequenceontology.org/browser/current_svn/term/SO:%i"]
- name: RFAM
prefixes: ["RFAM"]
resources:
html: ["http://rfam.xfam.org/family/%i"]
xml: ["http://rfam.xfam.org/family/%i?content-type=text%2Fxml"]
- name: Pubmed
prefixes: ["pubmed", "Pubmed"]
resources:
html: ["http://www.ncbi.nlm.nih.gov/pubmed/%i"]
- name: Protein Families
prefixes: ["PFAM"]
resources:
html: ["http://pfam.xfam.org/family/%i"]
- name: PDB
prefixes: ["PDB"]
resources:
html: ["http://www.rcsb.org/pdb/explore/explore.do?structureId=%i"]
xml: ["http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=xml&compression=NO&structureId=%i"]
- name: InterPro
prefixes: ["InterPro"]
resources:
html: ["http://www.ebi.ac.uk/interpro/entry/%i"]
- name: GeneID
prefixes: ["GeneID"]
resources:
html: ["http://www.ncbi.nlm.nih.gov/gene/%i"]
xml: ["http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&db=gene&dopt=xml&sort=&val=%i&retmode=file"]
- name: Gene Ontology
prefixes: ["GO"]
resources:
html: ["http://www.ebi.ac.uk/QuickGO/GTerm?id=GO:%i"]
xml: ["http://www.ebi.ac.uk/QuickGO/GTerm?id=GO:%i&format=oboxml"]
- name: HTTP
prefixes: ["http", "https"]
resources:
html: ["%d:%i"]
import sys
import os
# append module root directory to sys.path
sys.path.append(
os.path.dirname(
os.path.dirname(
os.path.abspath(__file__)
)
)
)
import tests.env
import unittest
from dbxref import config
class TestConfig(unittest.TestCase):
def test_load_providers_works(self):
self.assertNotEqual(config.load_providers(), [])
def test_index_providers(self):
data = [{'name': 'test', 'prefixes':['a', 'b']}]
self.assertEqual(config.index_providers(data), {'a': data[0], 'b': data[0]})
import tests.env
import unittest
from dbxref import resolver
class TestDbxrefResolve(unittest.TestCase):
def test_conversion_of_string_to_dbxref(self):
data = [
('GO:1234', {'db': 'GO', 'id': '1234'}),
('https://www.google.de', {'db': 'https', 'id': '//www.google.de'}),
('db:sub:id', {'db': 'db', 'id': 'sub:id'}),
]
for d in data:
with self.subTest(d=d):
self.assertEqual(resolver.convert_string_to_dbxref(d[0]), d[1])
def test_resolve_enzyme(self):
self.assertNotEqual(resolver.resolve(["EC:1.1.1.1"]), [])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment