Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pubmed.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. import random
  17. from abc import ABC
  18. from functools import partial
  19. from Bio import Entrez
  20. import pandas as pd
  21. import xml.etree.ElementTree as ET
  22. from graph.settings import DEBUG
  23. from graph.component.base import ComponentBase, ComponentParamBase
  24. class PubMedParam(ComponentParamBase):
  25. """
  26. Define the PubMed component parameters.
  27. """
  28. def __init__(self):
  29. super().__init__()
  30. self.top_n = 5
  31. self.email = "A.N.Other@example.com"
  32. def check(self):
  33. self.check_positive_integer(self.top_n, "Top N")
  34. class PubMed(ComponentBase, ABC):
  35. component_name = "PubMed"
  36. def _run(self, history, **kwargs):
  37. ans = self.get_input()
  38. ans = " - ".join(ans["content"]) if "content" in ans else ""
  39. if not ans:
  40. return PubMed.be_output("")
  41. Entrez.email = self._param.email
  42. pubmedids = Entrez.read(Entrez.esearch(db='pubmed', retmax=self._param.top_n, term=ans))['IdList']
  43. pubmedcnt = ET.fromstring(
  44. Entrez.efetch(db='pubmed', id=",".join(pubmedids), retmode="xml").read().decode("utf-8"))
  45. pubmed_res = [{"content": 'Title:' + child.find("MedlineCitation").find("Article").find(
  46. "ArticleTitle").text + '\nUrl:<a href=" https://pubmed.ncbi.nlm.nih.gov/' + child.find(
  47. "MedlineCitation").find("PMID").text + '">' + '</a>\n' + 'Abstract:' + child.find("MedlineCitation").find(
  48. "Article").find("Abstract").find("AbstractText").text} for child in pubmedcnt.findall("PubmedArticle")]
  49. if not pubmed_res:
  50. return PubMed.be_output("")
  51. df = pd.DataFrame(pubmed_res)
  52. if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
  53. return df