python - 使用 biopython 从外部 pubmed ID 列表中提取多个摘要

标签 python python-3.x dictionary biopython

我正在尝试使用 PubmedID 从 Pubmed 中提取 60K 篇文章的摘要。我正在尝试将摘要导出到字典中。我想我正在使用的代码存在一些问题,尤其是在解析 pubmed ID 时。请帮助更正代码并让我知道哪里出了问题。

from Bio import Entrez
import sys

Entrez.email = 'anonymous@gmail.com'

abstract_dict = {}
without_abstract = []

pub_ids = sys.argv[1]
f = open(pub_ids, "r")
for i in f:
    handle = Entrez.efetch(db="pubmed", id=','.join(map(str, i)),
                        rettype="xml", retmode="text")
    records = Entrez.read(handle)
    abstracts = [pubmed_article['MedlineCitation']['Article']['Abstract']['AbstractText'][0]
        if 'Abstract' in pubmed_article['MedlineCitation']['Article'].keys()
        else pubmed_article['MedlineCitation']['Article']['ArticleTitle']
            for pubmed_article in records['PubmedArticle']]
    abstract_dict = dict(zip(i, abstracts))
print(abstract_dict)

一些示例 Pubmed ID 是:

17284678
15531828
11791095
10708056

我得到的结果只有几行摘要或空字典。 是否可以将结果从字典导出到制表符分隔的文本文件中?

任何建议将不胜感激

谢谢

最佳答案

请注意,Entrez.efetch 只会返回 1000 条记录。既然你说要下载60K的摘要,我修改了你的代码,实现了批量下载摘要。

from Bio import Entrez
import sys
import csv

Entrez.email = 'anonymous@gmail.com'
 
def fetch_abstracts(pub_ids, retmax=1000, output_file='abstracts.csv'):    
    # Make sure requests to NCBI are not too big
    for i in range(0, len(pub_ids), retmax):
        j = i + retmax
        if j >= len(pub_ids):
            j = len(pub_ids)

        print(f"Fetching abstracts from {i} to {j}.")
        handle = Entrez.efetch(db="pubmed", id=','.join(pub_ids[i:j]),
                        rettype="xml", retmode="text", retmax=retmax)
        
        records = Entrez.read(handle)

        abstracts = [pubmed_article['MedlineCitation']['Article']['Abstract']['AbstractText'][0]
                      if 'Abstract' in pubmed_article['MedlineCitation']['Article'].keys()
                      else pubmed_article['MedlineCitation']['Article']['ArticleTitle']
                          for pubmed_article in records['PubmedArticle']]

        abstract_dict = dict(zip(pub_ids[i:j], abstracts))

        with open(output_file, 'a', newline='') as csvfile:
            fieldnames = ['pub_id', 'abstract']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter='\t')
            if i == 0:
              writer.writeheader()
            for pub_id, abstract in abstract_dict.items():
              writer.writerow({'pub_id': pub_id, 'abstract': abstract})

if __name__ == '__main__':
  filename = sys.argv[1]
  pub_ids = open(filename, "r").read().splitlines()
  fetch_abstracts(pub_ids)

如果你这样运行:

stack73000220.py pubids.txt

pubids.txt 看起来像:

17284678
15531828
11791095
10708056

然后您将在 abstracts.csv 中获得以下输出:

pub_id  abstract
17284678    Eimeria tenella is an intracellular protozoan parasite that infects the intestinal tracts of domestic fowl and causes coccidiosis, a serious and sometimes lethal enteritis. Eimeria falls in the same phylum (Apicomplexa) as several human and animal parasites such as Cryptosporidium, Toxoplasma, and the malaria parasite, Plasmodium. Here we report the sequencing and analysis of the first chromosome of E. tenella, a chromosome believed to carry loci associated with drug resistance and known to differ between virulent and attenuated strains of the parasite. The chromosome--which appears to be representative of the genome--is gene-dense and rich in simple-sequence repeats, many of which appear to give rise to repetitive amino acid tracts in the predicted proteins. Most striking is the segmentation of the chromosome into repeat-rich regions peppered with transposon-like elements and telomere-like repeats, alternating with repeat-free regions. Predicted genes differ in character between the two types of segment, and the repeat-rich regions appear to be associated with strain-to-strain variation.
15531828    To study the occurrence of nosocomial diarrhea in pediatric wards and the role of infections in its causation.
11791095    Based on single case reports, parvovirus B19 (B19) has repeatedly been proposed as an etiologic agent in patients with Henoch-Schönlein purpura (HSP), perhaps causing vasculitis by direct invasion of vascular endothelial cells because of the tissue distribution of the cellular B19 receptor. A cohort of children with HSP and other vasculitic diseases was investigated and compared with healthy control children to assess the role of B19 as well as parvovirus V9 (a putative emerging B19-like virus).
10708056    The effects of chemokine and chemokine receptor genetic polymorphisms such as stromal derived factor 1 (SDF1-3'A), CCR2-64I, and CCR5-delta32 associated with HIV-1 transmission and/or rate of disease progression in infected study subjects remain highly controversial and have been analyzed primarily only in adults. We have investigated whether these polymorphisms may provide similar beneficial effects in children exposed to HIV-1 perinatally. The prevalence of CCR2-64I allele was significantly increased (p = .03) and the CCR2-64I genotype distribution was not in Hardy-Weinberg equilibrium, among HIV-1-exposed uninfected infants. Moreover, in the HIV-1-infected group, a delay to AIDS progression was observed among carriers of CCR2-64I allele. This is the first report that suggests a protective role of CCR2-64I allele in mother-to-infant HIV-1 transmission and documents a delay in disease progression, after the child has been infected with HIV-1. However, SDFI-3'A and CCR5-delta32 alleles did not modify the rate of HIV-1 transmission or disease progression in HIV-1-infected children.

关于python - 使用 biopython 从外部 pubmed ID 列表中提取多个摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73000220/

相关文章:

python - 在字典键上使用 set

excel - 检查 VBA 中是否存在嵌套字典键

python - 函数抛出语法错误 : (unicode error)

python - 使用Application Factory实现Flask-WhooshAlchemy

python - 引用API 'forbidden'

python - 如何使用 while + for 循环计算 Big O

python - 如何在张量 session 中在一行中同时运行多个操作?

python - 仅使用特定键从字典字典创建数据框

python - 远程队列消费者在重启后错过了第一条消息

用于多键查找的 C++ map 与 multimap