Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pubmed.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from abc import ABC
  17. from Bio import Entrez
  18. import pandas as pd
  19. import xml.etree.ElementTree as ET
  20. from agent.settings import DEBUG
  21. from agent.component.base import ComponentBase, ComponentParamBase
  22. class PubMedParam(ComponentParamBase):
  23. """
  24. Define the PubMed component parameters.
  25. """
  26. def __init__(self):
  27. super().__init__()
  28. self.top_n = 5
  29. self.email = "A.N.Other@example.com"
  30. def check(self):
  31. self.check_positive_integer(self.top_n, "Top N")
  32. class PubMed(ComponentBase, ABC):
  33. component_name = "PubMed"
  34. def _run(self, history, **kwargs):
  35. ans = self.get_input()
  36. ans = " - ".join(ans["content"]) if "content" in ans else ""
  37. if not ans:
  38. return PubMed.be_output("")
  39. try:
  40. Entrez.email = self._param.email
  41. pubmedids = Entrez.read(Entrez.esearch(db='pubmed', retmax=self._param.top_n, term=ans))['IdList']
  42. pubmedcnt = ET.fromstring(
  43. Entrez.efetch(db='pubmed', id=",".join(pubmedids), retmode="xml").read().decode("utf-8"))
  44. pubmed_res = [{"content": 'Title:' + child.find("MedlineCitation").find("Article").find(
  45. "ArticleTitle").text + '\nUrl:<a href=" https://pubmed.ncbi.nlm.nih.gov/' + child.find(
  46. "MedlineCitation").find("PMID").text + '">' + '</a>\n' + 'Abstract:' + child.find(
  47. "MedlineCitation").find("Article").find("Abstract").find("AbstractText").text} for child in
  48. pubmedcnt.findall("PubmedArticle")]
  49. except Exception as e:
  50. return PubMed.be_output("**ERROR**: " + str(e))
  51. if not pubmed_res:
  52. return PubMed.be_output("")
  53. df = pd.DataFrame(pubmed_res)
  54. if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
  55. return df