pdbe.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """
  2. PDBe (Protein Data Bank in Europe)
  3. @website https://www.ebi.ac.uk/pdbe
  4. @provide-api yes (https://www.ebi.ac.uk/pdbe/api/doc/search.html),
  5. unlimited
  6. @using-api yes
  7. @results python dictionary (from json)
  8. @stable yes
  9. @parse url, title, content, img_src
  10. """
  11. from json import loads
  12. from flask_babel import gettext
  13. categories = ['science']
  14. hide_obsolete = False
  15. # status codes of unpublished entries
  16. pdb_unpublished_codes = ['HPUB', 'HOLD', 'PROC', 'WAIT', 'AUTH', 'AUCO', 'REPL', 'POLC', 'REFI', 'TRSF', 'WDRN']
  17. # url for api query
  18. pdbe_solr_url = 'https://www.ebi.ac.uk/pdbe/search/pdb/select?'
  19. # base url for results
  20. pdbe_entry_url = 'https://www.ebi.ac.uk/pdbe/entry/pdb/{pdb_id}'
  21. # link to preview image of structure
  22. pdbe_preview_url = 'https://www.ebi.ac.uk/pdbe/static/entry/{pdb_id}_deposited_chain_front_image-200x200.png'
  23. def request(query, params):
  24. params['url'] = pdbe_solr_url
  25. params['method'] = 'POST'
  26. params['data'] = {
  27. 'q': query,
  28. 'wt': "json" # request response in parsable format
  29. }
  30. return params
  31. def construct_body(result):
  32. # set title
  33. title = result['title']
  34. # construct content body
  35. content = """{title}<br />{authors} {journal} <strong>{volume}</strong>&nbsp;{page} ({year})"""
  36. # replace placeholders with actual content
  37. try:
  38. if result['journal']:
  39. content = content.format(
  40. title=result['citation_title'],
  41. authors=result['entry_author_list'][0], journal=result['journal'], volume=result['journal_volume'],
  42. page=result['journal_page'], year=result['citation_year'])
  43. else:
  44. content = content.format(
  45. title=result['citation_title'],
  46. authors=result['entry_author_list'][0], journal='', volume='', page='', year=result['release_year'])
  47. img_src = pdbe_preview_url.format(pdb_id=result['pdb_id'])
  48. except (KeyError):
  49. content = None
  50. img_src = None
  51. # construct url for preview image
  52. try:
  53. img_src = pdbe_preview_url.format(pdb_id=result['pdb_id'])
  54. except (KeyError):
  55. img_src = None
  56. return [title, content, img_src]
  57. def response(resp):
  58. results = []
  59. json = loads(resp.text)['response']['docs']
  60. # parse results
  61. for result in json:
  62. # catch obsolete entries and mark them accordingly
  63. if result['status'] in pdb_unpublished_codes:
  64. continue
  65. if hide_obsolete:
  66. continue
  67. if result['status'] == 'OBS':
  68. # expand title to add some sort of warning message
  69. title = gettext('{title}&nbsp;(OBSOLETE)').format(title=result['title'])
  70. superseded_url = pdbe_entry_url.format(pdb_id=result['superseded_by'])
  71. # since we can't construct a proper body from the response, we'll make up our own
  72. msg_superseded = gettext("This entry has been superseded by")
  73. content = '<em>{msg_superseded} \<a href="{url}">{pdb_id}</a></em>'.format(
  74. msg_superseded=msg_superseded,
  75. url=superseded_url,
  76. pdb_id=result['superseded_by'], )
  77. # obsoleted entries don't have preview images
  78. img_src = None
  79. else:
  80. title, content, img_src = construct_body(result)
  81. results.append({
  82. 'url': pdbe_entry_url.format(pdb_id=result['pdb_id']),
  83. 'title': title,
  84. 'content': content,
  85. 'img_src': img_src
  86. })
  87. return results