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

e5nszbig  于 4个月前  发布在  Python
关注(0)|答案(1)|浏览(95)

我试图使用PubmedID从Pubmed提取6万篇文章的摘要。我试图将摘要导出到字典中。我想我使用的代码有一些问题,特别是在解析Pubmed ID时。请帮助纠正代码,让我知道哪里出错了。

from Bio import Entrez
import sys

Entrez.email = '[email protected]'

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 Ids的一些例子是:

17284678
15531828
11791095
10708056


我得到的结果,但抽象或空字典几行。是否有可能出口的结果到制表符分隔的文本文件从字典?
任何建议将不胜感激
谢谢你

zrfyljdw

zrfyljdw1#

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

from Bio import Entrez
import sys
import csv

Entrez.email = '[email protected]'
 
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.

相关问题