pdbe.py 3.6 KB

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