23 from ost.io import LoadPDB, LoadMMCIF
27 A remote repository represents a structural database accessible through the
28 internet, e.g. the PDB or SWISS-MODEL template library.
30 def __init__(self, name, url_pattern, type, id_transform='upper'):
34 if type
not in (
'cif',
'pdb'):
35 raise ValueError(
'only cif and pdb types are supported')
43 return self.url_pattern.replace(
'$ID', id)
47 tmp_file_suffix =
'.%s' % self.
type
48 if remote_url.endswith(
'.gz'):
49 tmp_file_suffix+=
'.gz'
52 connection = urllib2.urlopen(remote_url)
53 if hasattr(connection,
'code'):
54 status = connection.code
56 status = connection.getcode()
57 except urllib2.HTTPError, e:
60 raise IOError(
'Could not load %s from %s (status code %d, url %s)' \
61 % (id, self.
name, status, remote_url))
62 tmp_file = tempfile.NamedTemporaryFile(suffix=tmp_file_suffix)
63 contents =
''.join(connection)
64 tmp_file.write(contents)
69 tmp_file = self.
Get(id)
70 if self.
type ==
'pdb':
72 if self.
type ==
'cif':
75 REMOTE_REPOSITORIES = {
76 'pdb' :
RemoteRepository(
'pdb.org (PDB)',
'http://www.pdb.org/pdb/files/$ID.pdb.gz',
77 type=
'pdb', id_transform=
'upper'),
78 'smtl' :
RemoteRepository(
'SMTL',
'http://swissmodel.expasy.org/templates/$ID.pdb',
79 type=
'pdb', id_transform=
'lower'),
80 'cif' :
RemoteRepository(
'pdb.org (mmCIF)',
'http://www.pdb.org/pdb/files/$ID.cif.gz',
81 type=
'cif', id_transform=
'lower'),
82 'pdb_redo' :
RemoteRepository(
'pdbredo',
'http://pdb-redo.eu/db/$ID/$ID_besttls.pdb.gz',
83 type=
'pdb', id_transform=
'lower'),
87 remote_repo = REMOTE_REPOSITORIES.get(from_repo,
None)
89 raise ValueError(
'%s is not a valid repository' % from_repo)
90 return remote_repo.Get(id)
93 remote_repo = REMOTE_REPOSITORIES.get(from_repo,
None)
95 raise ValueError(
'%s is not a valid repository' % from_repo)
96 return remote_repo.Load(id)