-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreference_title_citation_update.py
178 lines (129 loc) · 4.87 KB
/
reference_title_citation_update.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
import logging
import os
from datetime import datetime
import time
import sys
reload(sys) # Reload does the trick!
sys.setdefaultencoding('UTF8')
from src.models import Dbentity, Referencedbentity, Journal
from scripts.loading.database_session import get_session
from scripts.loading.reference.pubmed import get_pubmed_record_from_xml, \
get_abstracts, set_cite
__author__ = 'sweng66'
logging.basicConfig(format='%(message)s')
log = logging.getLogger()
log.setLevel(logging.INFO)
CREATED_BY = os.environ['DEFAULT_USER']
MAX = 500
MAX_4_CONNECTION = 5000
SLEEP_TIME = 2
PUBLISHED_STATUS = 'Published'
EPUB_STATUS = 'Epub ahead of print'
EPUB = 'aheadofprint'
PUBLISH = 'ppublish'
SRC = 'NCBI'
AUTHOR_TYPE = 'Author'
PMC_URL_TYPE = 'PMC full text'
DOI_URL_TYPE = 'DOI full text'
PMC_ROOT = 'http://www.ncbi.nlm.nih.gov/pmc/articles/'
DOI_ROOT = 'http://dx.doi.org/'
def update_reference_data(log_file):
nex_session = get_session()
journal_id_to_abbrev = dict([(x.journal_id, x.med_abbr) for x in nex_session.query(Journal).all()])
fw = open(log_file,"w")
fw.write(str(datetime.now()) + "\n")
fw.write("Getting PMID list...\n")
log.info(str(datetime.now()))
log.info("Getting data from the database...")
pmids_all = []
pmid_to_reference = {}
for x in nex_session.query(Referencedbentity).all():
if x.pmid:
pmids_all.append(x.pmid)
pmid_to_reference[x.pmid] = { 'dbentity_id': x.dbentity_id,
'publication_status': x.publication_status,
'date_revised': x.date_revised }
###########################
nex_session.close()
nex_session = get_session()
###########################
fw.write(str(datetime.now()) + "\n")
fw.write("Getting Pubmed records...\n")
# log.info(datetime.now())
log.info("Getting Pubmed records and updating the database...")
pmids = []
j = 0
i = 0
for pmid in pmids_all:
if pmid is None or pmid in [26842620, 27823544, 11483584]:
continue
i = i + 1
j = j + 1
if j >= MAX_4_CONNECTION:
###########################
nex_session.close()
nex_session = get_session()
###########################
log.info("Reference updated: " + str(i))
j = 0
# print "PMID: ", pmid
if len(pmids) >= MAX:
records = get_pubmed_record_from_xml(','.join(pmids))
update_database_batch(nex_session, fw, records,
pmid_to_reference,
journal_id_to_abbrev)
pmids = []
time.sleep(SLEEP_TIME)
pmids.append(str(pmid))
if len(pmids) > 0:
records = get_pubmed_record_from_xml(','.join(pmids))
update_database_batch(nex_session, fw, records,
pmid_to_reference,
journal_id_to_abbrev)
nex_session.commit()
log.info("Reference updated: " + str(i))
log.info(str(datetime.now()))
log.info("Done!")
fw.close()
def update_database_batch(nex_session, fw, records, pmid_to_reference, journal_id_to_abbrev):
for record in records:
pmid = record.get('pmid')
if pmid is None:
continue
x = pmid_to_reference.get(pmid)
if x is None:
continue
print "Updating data for PMID: ", pmid
dbentity_id = x['dbentity_id']
### UPDATE REFERENCEDBENTITY TABLE
update_reference(nex_session, fw, record, pmid, x, journal_id_to_abbrev)
def update_reference(nex_session, fw, record, pmid, x, journal_id_to_abbrev):
x = nex_session.query(Referencedbentity).filter_by(pmid=pmid).one_or_none()
if x is None:
return
journal = journal_id_to_abbrev[x.journal_id]
authors = record.get('authors', [])
title = record.get('title', '')
year = x.year
volume = x.volume
issue = x.issue
page = x.page
citation = set_cite(title, authors, year, journal, volume, issue, page)
### update reference table
has_update = 0
if citation != x.citation:
print "PMID:", pmid, "update Citation - NEW:", citation
print "PMID:", pmid, "update Citation - OLD:", x.citation
x.citation = citation
has_update = 1
if title != x.title:
print "PMID:", pmid, "update Title - NEW:", title
print "PMID:", pmid, "update Title - OLD:",x.title
x.title = title
has_update = 1
if has_update == 1:
nex_session.add(x)
nex_session.commit()
if __name__ == '__main__':
log_file = "scripts/loading/reference/logs/reference_title_citation_update.log"
update_reference_data(log_file)