OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
remote.py
Go to the documentation of this file.
1 #------------------------------------------------------------------------------
2 # This file is part of the OpenStructure project <www.openstructure.org>
3 #
4 # Copyright (C) 2008-2011 by the OpenStructure authors
5 #
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; either version 3.0 of the License, or (at your option)
9 # any later version.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #------------------------------------------------------------------------------
19 
20 import urllib2
21 import tempfile
22 
23 from ost.io import LoadPDB, LoadMMCIF
24 
26  """
27  A remote repository represents a structural database accessible through the
28  internet, e.g. the PDB or SWISS-MODEL template library.
29  """
30  def __init__(self, name, url_pattern, type, id_transform='upper'):
31  self.name = name
32  self.url_pattern = url_pattern
33  self.type = type
34  if type not in ('cif', 'pdb'):
35  raise ValueError('only cif and pdb types are supported')
36  self.id_transform = id_transform
37 
38  def URLForID(self, id):
39  if self.id_transform == 'upper':
40  id = id.upper()
41  if self.id_transform == 'lower':
42  id = id.lower()
43  return self.url_pattern.replace('$ID', id)
44 
45  def Get(self, id):
46  remote_url = self.URLForID(id)
47  tmp_file_suffix = '.%s' % self.type
48  if remote_url.endswith('.gz'):
49  tmp_file_suffix+='.gz'
50 
51  try:
52  connection = urllib2.urlopen(remote_url)
53  if hasattr(connection, 'code'):
54  status = connection.code
55  else:
56  status = connection.getcode()
57  except urllib2.HTTPError, e:
58  status = e.code
59  msg = 'Could not load %s from %s (status code %d)'
60  if status!=200:
61  raise IOError(msg % (id , self.name ,status))
62  tmp_file = tempfile.NamedTemporaryFile(suffix=tmp_file_suffix)
63  contents = ''.join(connection)
64  tmp_file.write(contents)
65  tmp_file.flush()
66  return tmp_file
67 
68  def Load(self, id):
69  tmp_file = self.Get(id)
70  if self.type == 'pdb':
71  return LoadPDB(tmp_file.name)
72  if self.type == 'cif':
73  return LoadMMCIF(tmp_file.name)
74 
75 REMOTE_REPOSITORIES = {
76  'pdb' : RemoteRepository('pdb.org (PDB)', 'http://www.pdb.org/pdb/files/$ID.ent.gz',
77  type='pdb', id_transform='upper'),
78  'smtl' : RemoteRepository('SMTL', 'http://beta.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://www.cmbi.ru.nl/pdb_redo/$ID/$ID_besttls.pdb',
83  type='pdb'),
84 }
85 
86 def RemoteGet(id, from_repo='pdb'):
87  remote_repo = REMOTE_REPOSITORIES.get(from_repo, None)
88  if not remote_repo:
89  raise ValueError('%s is not a valid repository' % from_repo)
90  return remote_repo.Get(id)
91 
92 def RemoteLoad(id ,from_repo='pdb'):
93  remote_repo = REMOTE_REPOSITORIES.get(from_repo, None)
94  if not remote_repo:
95  raise ValueError('%s is not a valid repository' % from_repo)
96  return remote_repo.Load(id)