-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitema.py
129 lines (100 loc) · 3.79 KB
/
citema.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
import numpy as np
import urllib
import xml.etree.ElementTree as ET
import os
import json
import httplib
import random
# Main method
def makeMap(inipmid,levels=2):
output = map2json(buildMap([inipmid],levels=levels))
# output json file
filename = str(inipmid)+'.json'
s = open(filename, 'w')
s.write(json.dumps(output, indent=4, separators=(',', ': ')))
s.close()
return filename
# Output JSON from buildmap output
def map2json(ids,mincite=1):
output = {}
keycount = {}
for key in ids.keys():
keycount[key] = 0
# build links
output['links'] = []
for key in ids.keys():
for target in ids[key]['IDs']:
keycount[key] += 1
# build nodes
output['nodes'] = []
key2ind = []
for key in ids.keys():
if keycount[key] >= mincite:
output['nodes'].append({'name':str(key),'group':(ids[key]['level']+1),'url':'http://www.ncbi.nlm.nih.gov/pubmed/'+str(key),'value': keycount[key]})
key2ind.append(key)
for key in ids.keys():
for target in ids[key]['IDs']:
if keycount[target] >= mincite and keycount[key] >= mincite:
output['links'].append({'source': key2ind.index(key),'target': key2ind.index(target),'value': (keycount[key]+keycount[target])})
return output
# Returns dictionary of PMID keys and PMIDs values
def buildMap(inipmid,levels=3,ids={}):
if levels > 1:
curIDs = getIDs(inipmid)
for curpmid in curIDs:
if curpmid[0] not in ids:
ids[curpmid[0]] = {'IDs':curpmid[1:],'level':levels}
curIDs = sum(curIDs, [])
curIDs = [item for item in curIDs if item not in ids]
ids = buildMap(curIDs,levels=levels-1,ids=ids)
else:
for curpmid in inipmid:
if curpmid not in ids:
ids[curpmid] = {'IDs':[],'level':levels}
return ids
# Returns pmids from citation pmid, if any.
# If none exist, returns None
# uses POST method
def getIDs(pmid):
# get local xml from pubmed
filename = url2xml(pmid)
# find the PMID field
xmlroot = ET.parse(filename).getroot()
PMIDs = []
# Make list of lists
for curid in xmlroot.getiterator('LinkSet'):
thislist = [int(curid.find('IdList').find('Id').text)]
for linkset in curid.findall('LinkSetDb'):
if linkset.find('LinkName').text:
if "citedin" in linkset.find('LinkName').text:
for link in linkset.findall('Link'):
thislist.append(int(link.find('Id').text))
# now append them
PMIDs.append(thislist)
# Delete that ugly xml file
killfile(filename)
return PMIDs
# Returns URL for pmid pubmed xml file
# Deprecated Oct 13, 2013
# All requests go through POST
def getURL(pmid):
return 'http://www.ncbi.nlm.nih.gov/pubmed/'+str(pmid)+'?report=xml'
# Makes a local XML file by POSTing pmids in e-utils PUBMED,
# and returns the filename, directory of resultant XML file
def url2xml(pmid):
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept":"text/plain"}
datadict = (('dbfrom','pubmed'),('db','pubmed'))
for curid in pmid:
datadict = datadict + (('id',curid),)
data = urllib.urlencode(datadict)
h = httplib.HTTPConnection('eutils.ncbi.nlm.nih.gov')
h.request('POST','/entrez/eutils/elink.fcgi',data,headers)
f = h.getresponse()
filename = '.tmp.'+str(random.randint(0,1000000))+'.xml'
s = open(filename, 'w')
s.write(f.read())
s.close()
return filename
def killfile(filename):
os.remove(filename)
return None