2017-11-20 6 views
1

Ich möchte große wissenschaftliche abstrakte Daten für etwa 2000 Pubmed IDs herunterladen. Mein Python-Code ist schlampig und scheint ziemlich langsam zu arbeiten. Gibt es eine schnelle und effiziente Methode, um diese Abstracts zu ernten?Gibt es eine schnelle und effiziente Möglichkeit, Abstracts von Pubmed zu erhalten?

Wenn dies die schnellste Methode ist, wie kann ich es messen, damit ich im Vergleich zu anderen oder zu Hause gegen Arbeitssituation vergleichen kann (verschiedene ISP kann eine Rolle in der Geschwindigkeit spielen)?

Angehängt mein Code unten.

import sqlite3 
from Bio.Entrez import read,efetch,email,tool 
from metapub import PubMedFetcher 
import pandas as pd 
import requests 
from datetime import date 
import xml.etree.ElementTree as ET 
import time 
import sys 
reload(sys) 
sys.setdefaultencoding('utf8') 
Abstract_data = pd.DataFrame(columns=["name","pmid","abstract"]) 

def abstract_download(self,dict_pmids): 
    """ 
     This method returns abstract for a given pmid and add to the abstract data 
    """ 
    index=0 
    baseUrl = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/" 
    for names in dict_pmids: 
     for pmid in dict_pmids[names]: 
      try: 
       abstract = [] 
       url = baseUrl+"efetch.fcgi?db=pubmed&id="+pmid+"&rettype=xml"+ 
       response=requests.request("GET",url,timeout=500).text 
       response=response.encode('utf-8') 
       root=ET.fromstring(response) 
       root_find=root.findall('./PubmedArticle/MedlineCitation/Article/Abstract/') 
       if len(root_find)==0: 
        root_find=root.findall('./PubmedArticle/MedlineCitation/Article/ArticleTitle') 
       for i in range(len(root_find)): 
        if root_find[i].text != None: 
         abstract.append(root_find[i].text) 
       if abstract is not None: 
        Abstract_data.loc[index]=names,pmid,"".join(abstract) 
       index+=1 
      except: 
       print "Connection Refused" 
       time.sleep(5) 
       continue 
    return Abstract_data 

EDIT: Der allgemeine Fehler, der für diesen Skript auftritt, ist scheinbar eine "Connection Refused". Sehen Sie die Antwort von ZF007 unten, wie das gelöst wurde.

+0

Ich wähle diese Frage zu schließen, wie Off-Topic, weil scheint wie eine bessere Passform für biostars.org oder https://bioinformatics.stackexchange.com/ – Stedy

+2

@Stedy Die Frage, obwohl fehlen einige Informationen wie die importierten Module und der Fehler oder das Problem mit dem angezeigten Code scheint eine 'Anfrage'-Frage zu sein. Der biologische Kontext ist sekundär und hat daher einen Platz im Stack Overflow IMO. – rodgdor

+1

@Nishal Könnten Sie zu Ihren Fragen die importierten Module hinzufügen und auch was scheint der Fehler/das Problem mit Ihrem Code zu sein? – rodgdor

Antwort

1

Der folgende Code funktioniert. Ihr Skript hängt an einer ungültigen URL-Konstruktion. Auch wenn innerhalb des Skripts etwas schief ging, war die Antwort eine abgewiesene Verbindung. Dies war in der Tat nicht der Fall, weil es der Code war, der die Verarbeitung der abgerufenen Daten durchgeführt hat. Ich habe einige Anpassungen vorgenommen, um den Code für mich arbeiten zu lassen und Kommentare an Ort und Stelle zu lassen, die Sie aufgrund des Mangels anpassen müssen dict_pmids Liste.

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import sys, time, requests, sqlite3 

import pandas as pd 
import xml.etree.ElementTree as ET 

from metapub import PubMedFetcher 
from datetime import date 
from Bio.Entrez import read,efetch,email,tool 


def abstract_download(pmids): 

    """ 
     This method returns abstract for a given pmid and add to the abstract data 
    """ 

    index = 0 
    baseUrl = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/" 
    collected_abstract = [] 

    # code below diabled to get general abstract extraction from pubmed working. I don't have the dict_pmid list. 

    """ 
    for names in dict_pmids: 
     for pmid in dict_pmids[names]: 

    move below working code to the right to get it in place with above two requirements prior to providing dict_pmid list. 

    # from here code works upto the next comment. I don't have the dict_pmid list. 
    """ 

    for pmid in pmids: 

     print 'pmid : %s\n' % pmid 

     abstract = [] 
     root = '' 

     try: 

      url  = '%sefetch.fcgi?db=pubmed&id=%s&rettype=xml' % (baseUrl, pmid) 
      # checks my url... line to parse into a webbrowser like firefox. 
      print 'url', url 

      response = requests.request("GET", url, timeout=500).text 
      # check if I got a response. 
      print 'response', response 

#    response = response.encode('utf-8') 
      root  = ET.fromstring(response) 

     except Exception as inst: 
      # besides a refused connection.... the "why" it was connected comes in handly to resolve issues at hand 
      # if and when they happen. 
      print "Connection Refused", inst 

      time.sleep(5) 
      continue 

     root_find = root.findall('./PubmedArticle/MedlineCitation/Article/Abstract/') 

     if len(root_find)==0: 

       root_find = root.findall('./PubmedArticle/MedlineCitation/Article/ArticleTitle') 

     # check if I found something 
     print 'root_find : %s\n\n' % root_find 

     for i in range(len(root_find)): 

      if root_find[i].text != None: 

       abstract.append(root_find[i].text) 

     Abstract_data = pd.DataFrame(columns=["name","pmid","abstract"]) 

     # check if I found something 
     #print 'abstract : %s\n' % abstract 

     # code works up to the print statement ''abstract', abstract' teh rest is disabled because I don't have the dict_pmid list. 

     if abstract is not None: 

#    Abstract_data.loc[index] = names,pmid,"".join(abstract) 

      index += 1 
      collected_abstract.append(abstract) 

    # change back return Abstract_data when dict_pmid list is administered. 
# return Abstract_data 
    return collected_abstract 

if __name__ == '__main__': 

    sys.stdout.flush() 

    reload(sys) 
    sys.setdefaultencoding('utf8') 

    pubmedIDs = range(21491000, 21491001) 

    mydata = abstract_download(pubmedIDs) 

    print 'mydata : %s' % (mydata) 
Verwandte Themen