I need to import the Python file to Neo4j and for that I need to create nodes and labels. But I can not do it. What would it be like?
This is my code:
from neo4jrestclient.client import GraphDatabase
import time
from Bio import Entrez
db = GraphDatabase("http://localhost:7474", username="neo4j", password="mypassword")
def search(query):
Entrez.email = '[email protected]'
handle = Entrez.esearch(db='pubmed',
sort='relevance',
retmax='20000000',
retmode='xml',
term=query)
results = Entrez.read(handle)
return results
def fetch_details(id_list):
ids = ','.join(id_list)
Entrez.email = '[email protected]'
handle = Entrez.efetch(db='pubmed',
retmode='xml',
id=ids)
results = Entrez.read(handle)
return results
if __name__ == '__main__':
results = search('1900[PDAT]:2020[PDAT],nature[JOURNAL]')
id_list = results['IdList']
print(len(id_list))
if (len(id_list)>0):
papers = fetch_details(id_list)
print(papers)
for i, paper in enumerate(papers['PubmedArticle']):
#the api lets you import 10,000 record each time, so i made a sleep function for 3 minutes each iteration
if(i%9995==0):
time.sleep(3*60)
print("%d) %s" % (i + 1, paper['MedlineCitation']['Article']['ArticleTitle']))
paperDate = paper['MedlineCitation']['Article']['ArticleDate']
if(len(paperDate)==0):
try:
print(paper['MedlineCitation']['Article']['Journal']['JournalIssue']['PubDate']['Year'])
except KeyError:
print("No Journal Year")
else:
for paperYear in enumerate(paperDate):
print("%d) %s" % (i + 1, paperYear[1]['Year']))
try:
authorList = paper['MedlineCitation']['Article']['AuthorList']
except KeyError:
print("No Artist List")
continue
for author in authorList:
try:
print ("%d) %s" % (i + 1, "LastName: %s , Forename: %s , Initials: %s" % (
author['LastName'], author['ForeName'], author['Initials'])))
try:
print("Affiliation: %s" % (author['AffiliationInfo'][0]['Affiliation']))
except (IndexError,KeyError):
print("No Affiliation")
except KeyError:
print("No Author name")
else:
print("No ID")
# Create some nodes
paper = db.node.create("Paper")
author = db.node.create("Author")
# Author-Wrote->Paper relationships
author.relationships.create("Wrote", paper)