-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_linked.py
70 lines (54 loc) · 2.34 KB
/
extract_linked.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
import gzip
import sys
import xmltodict
import json
import jsonpickle
with gzip.open(sys.argv[1], 'r') as f:
xml_dict = xmltodict.parse(f.read())
pubmed_articles = xml_dict['PubmedArticleSet']['PubmedArticle']
ids={}
for pa in pubmed_articles:
pmid = pa['MedlineCitation']['PMID']['#text']
article = pa['MedlineCitation']['Article']
nctids = []
if 'DataBankList' in article:
journal = article["Journal"]["Title"]
year = pa["PubmedData"]["History"]["PubMedPubDate"][-1]["Year"]
for k,v in article['DataBankList'].items():
if k=='DataBank':
if type(v) == list:
for vv in v:
for k1,v1 in vv['AccessionNumberList'].items():
if v1 == None:
continue
if type(v1) == str:
if v1.lower().startswith('nct'):
nctids.append(v1)
else:
for v11 in v1:
#print("l:",v11)
if type(v11)==str and v11.lower().startswith('nct'):
nctids.append(v11)
else:
for k1,v1 in v['AccessionNumberList'].items():
if v1 == None:
continue
#print(v1, type(v1))
if type(v1) == str:
if v1.lower().startswith('nct'):
nctids.append(v1)
else:
for v11 in v1:
#print("nl:",v11)
if type(v11)==str and v11.lower().startswith('nct'):
nctids.append(v11)
if len(nctids) > 0:
ids[pmid]= { "nctids": nctids }
ids[pmid]["journal"] = journal
ids[pmid]["year"] = year
if 'MeshHeadingList' in pa['MedlineCitation']:
ids[pmid]["MeSH"] = pa['MedlineCitation']['MeshHeadingList']
if 'PublicationTypeList' in article:
ids[pmid]["PT"] = article["PublicationTypeList"]
with open('./'+sys.argv[1]+'.json', 'w') as f:
json.dump(jsonpickle.encode(ids), f)