This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubmedSearch.py
226 lines (201 loc) · 8.51 KB
/
pubmedSearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# -*- coding: utf-8 -*-
import sys
sys.path.append('/home/bi2_pg6/public_html/Inficio Raptum/Python/')
from Bio import Entrez
from Bio import Medline
import MySQLdb as con
import logging
def index(comp,org):
TERM1 = comp
TERM2 = org
if TERM1 and TERM2:
compound = newCompound(TERM1,TERM2)
class newCompound(object):
def __init__(self, TERM1, TERM2):
logging.basicConfig(filename='/home/bi2_pg6/public_html/Inficio Raptum/Python/app.log',level=logging.DEBUG)
self.TERM1 = TERM1
self.TERM2 = TERM2
self.TERMS = '(' +self.TERM1+ ')' + ' AND '+ '('+self.TERM2+ ')'
Entrez.email = "[email protected]" # Always tell NCBI who you are
handle = Entrez.egquery(term=self.TERMS)
record = Entrez.read(handle)
for row in record["eGQueryResult"]:
if row["DbName"]=="pubmed":
print(row["Count"])
handle = Entrez.esearch(db="pubmed", term=self.TERMS, retmax=463)
record = Entrez.read(handle)
ids = record['IdList']
if not ids:
return None
else:
handle = Entrez.efetch(db="pubmed", id=ids, rettype="medline")
records = Medline.parse(handle)
records = list(records)
self.title = []
self.authors = []
self.source = []
self.abstract = []
self.date = []
self.pubmedID = []
self.organismArticle = set()
self.substanceArticle = set()
for record in records:
self.title.append(record.get("TI", "?"))
self.authors.append(record.get("AU", "?"))
self.source.append(record.get("SO", "?"))
self.abstract.append(record.get("AB", "?"))
self.date.append(record.get("DA", "?"))
self.pubmedID.append(record.get("PMID","?"))
# Open database connection
self.dataB=con.connect(host="localhost", # your host, usually localhost
user="bi2_pg6", # your username
passwd="blaat1234", # your password
db="InfiRap") # name of the data base
# prepare a cursor object using cursor() method
cursor1 = self.dataB.cursor()
try:
# Execute the SQL command
cursor1.execute("SELECT chemical_name FROM `Compound` WHERE chemical_name = '"+self.TERM1+"'")
substanceExists = cursor1.rowcount > 0
cursor1.close()
cursor2 = self.dataB.cursor()
Split = self.TERM2.split(" ")
surname = Split[1]
cursor2.execute("SELECT latin_surname FROM `Organism` WHERE latin_surname = '"+surname+"'")
organismExists = cursor2.rowcount > 0
cursor2.close()
if not substanceExists and not organismExists:
self.insertTerm()
self.dataB.commit()
self.insertSubstance()
self.dataB.commit()
self.insertOrganism()
self.dataB.commit()
self.insertAuteur()
self.dataB.commit()
self.insertTussen2()
self.dataB.commit()
self.insertTussen3()
self.dataB.commit()
self.insertArtikelen()
self.dataB.commit()
elif not substanceExists:
self.insertTerm()
self.dataB.commit()
self.insertSubstance()
self.dataB.commit()
self.insertAuteur()
self.dataB.commit()
self.insertTussen2()
self.dataB.commit()
self.insertTussen3()
self.dataB.commit()
self.insertArtikelen()
self.dataB.commit()
elif not organismExists:
self.insertTerm()
self.dataB.commit()
self.insertOrganism()
self.dataB.commit()
self.insertAuteur()
self.dataB.commit()
self.insertTussen2()
self.dataB.commit()
self.insertTussen3()
self.dataB.commit()
self.insertArtikelen()
self.dataB.commit()
else:
self.artikelCheck()
except con.Error, e:
print(e)
logging.debug(e)
cursor1.close()
self.dataB.close()
def artikelCheck(self):
cursor1 = self.dataB.cursor()
cursor1.execute("SELECT Term FROM `Termcheck` WHERE Term = '"+self.TERMS+"'")
termExists = cursor1.rowcount > 0
cursor1.close()
if not termExists:
self.insertTerm()
self.dataB.commit()
self.insertAuteur()
self.dataB.commit()
self.insertTussen2()
self.dataB.commit()
self.insertTussen3()
self.dataB.commit()
self.insertArtikelen()
self.dataB.commit()
def insertTerm(self):
cursor = self.dataB.cursor()
cursor.execute("INSERT INTO `Termcheck`(Term) VALUES( '"+self.TERMS+"')")
cursor.close()
def insertSubstance(self):
cursor = self.dataB.cursor()
cursor.execute("INSERT INTO `Compound`(chemical_name) VALUES ('"+self.TERM1+"')")
cursor.close()
def insertOrganism(self):
cursor = self.dataB.cursor()
Split = self.TERM2.split(" ")
prefix = Split[0]
surname = Split[1]
cursor.execute('INSERT INTO `Organism` (latin_surname, latin_prefix) VALUES("%s", "%s")' % \
(surname,prefix))
cursor.close()
def insertAuteur(self):
cursor = self.dataB.cursor()
i = 0
while(i<len(self.title)):
cursor.close()
cursor = self.dataB.cursor()
_authors = ','.join(self.authors[i]).replace("'","")
cursor.execute("INSERT INTO `Author` (Name) VALUES ('"+_authors+"')")
i+=1
cursor.close()
## def insertTussen1(self):
## cursor = self.dataB.cursor()
## cursor.execute("SELECT substance_id FROM substances WHERE substance = '"+self.TERM1+"'")
## Term1ID = cursor.fetchone()[0]
## cursor.execute('INSERT INTO `Article-Author` (Article_ID, Author_ID) values("%s", "%d")' % \
## (pubmedID, anumm))
## cursor.close()
def insertTussen2(self):
cursor = self.dataB.cursor()
Split = self.TERM2.split(" ")
surname = Split[1]
i = 0
while(i<len(self.title)):
cursor.close()
cursor = self.dataB.cursor()
_pubmedID = self.pubmedID[i].replace("'","")
cursor.execute('INSERT INTO `Article-Organism` (Article_ID, Organism_surname) VALUES("%s","%s")' %\
(_pubmedID,surname))
i+=1
cursor.close()
def insertTussen3(self):
cursor = self.dataB.cursor()
i = 0
while(i<len(self.title)):
cursor.close()
cursor = self.dataB.cursor()
_pubmedID = self.pubmedID[i].replace("'","")
cursor.execute('INSERT INTO `Compound-Article` (Article_ID, Compound_Sysname) VALUES("%s","%s")' %\
(_pubmedID, self.TERM1))
i+=1
cursor.close()
def insertArtikelen(self):
i=0
while(i<len(self.title)):
cursor = self.dataB.cursor()
_title = self.title[i].replace("'","")
magazine = self.source[i].replace("'","")
_date = self.date[i].replace("'","")
_abstract = self.abstract[i].replace("'","")
_pubmedID = self.pubmedID[i].replace("'","")
cursor.execute('INSERT INTO `Article` (articleID, article_title, magazine, publication_date, abstract) VALUES ("%s","%s","%s","%s","%s")' %\
(_pubmedID, _title, magazine, _date, _abstract))
self.dataB.commit()
cursor.close()
i+=1