-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter-pubmed.py
executable file
·172 lines (148 loc) · 5.96 KB
/
filter-pubmed.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
#!/bin/env python
from os import listdir
from os.path import isdir
from sys import stderr
from optparse import OptionParser
from StringIO import StringIO
import re
PMID_PATTERN = re.compile(r'<PMID Version="1">(\d+)</PMID>')
MESH_PATTERN = re.compile(r'<DescriptorName.*UI="(D\d+)".*>.+</DescriptorName>')
XML_HEADER = '''<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE PubmedArticleSet SYSTEM "http://dtd.nlm.nih.gov/ncbi/pubmed/out/pubmed_170101.dtd">
<PubmedArticleSet>
'''
XML_FOOTER = '''
'''
class FilterPubMed(OptionParser):
def __init__(self):
OptionParser.__init__(self, usage='usage: %prog [options]')
self.add_option('--mesh-tree', action='store', type='string', dest='mesh_trees', default='/bibdev/ressources/MeSH/mtrees2013.bin', metavar='FILE', help='read MeSH trees from FILE (default: %default)')
self.add_option('--mesh-filter', action='store', type='string', dest='mesh_filter', metavar='FILE', help='read accepted MeSH indexes from FILE')
self.add_option('--pubmed-xml', action='store', type='string', dest='pubmed_xml', default='/bibdev/corpus/PubMed2015/xml', metavar='DIR', help='read PubMed XML files in directory DIR (default: %default)')
self.add_option('--pmid-filter', action='store', type='string', dest='pmid_filter', metavar='FILE', help='read accepted PMIDs from FILE')
self.add_option('--xml-out', action='store', type='string', dest='xml_out', metavar='FILE', help='write filtered accepted in FILE')
self.add_option('--pmid-out', action='store', type='string', dest='pmid_out', metavar='FILE', help='write accepted PMIDs in FILE')
def _read_pmid_filter(self):
if self.options.pmid_filter is None:
self.pmid_filter = None
return
stderr.write('Reading PMID filter file: %s\n' % self.options.pmid_filter)
f = open(self.options.pmid_filter)
self.pmid_filter = set(line.strip() for line in f)
f.close()
def _read_mesh_filter(self):
if self.options.mesh_filter is None:
self.mesh_filter = None
return
stderr.write('Reading MeSH filter file: %s\n' % self.options.mesh_filter)
f = open(self.options.mesh_filter)
mesh_paths = set(line.strip() for line in f)
f.close()
stderr.write('Reading MeSH trees file: %s\n' % self.options.mesh_trees)
f = open(self.options.mesh_trees)
self.mesh_filter = set()
for line in f:
path, did, dname = line.strip().split('\t')
for p in mesh_paths:
if path.startswith(p):
stderr.write(' %s: %s\n' % (did, dname))
self.mesh_filter.add(did)
break
def filter(self):
self.options, self.args = self.parse_args()
self._read_pmid_filter()
self._read_mesh_filter()
mem = Memory(self.options.xml_out, self.options.pmid_out, self.pmid_filter, self.mesh_filter)
for filename in list_files(self.options.pubmed_xml): #listdir(self.options.pubmed_xml):
if filename.endswith('.xml'):
stderr.write('Reading PubMed XML file: %s\n' % filename)
f = open(filename)
state = mem.init()
for line in f:
state = state(line, mem)
f.close()
mem.close()
def list_files(path):
if isdir(path):
for p in listdir(path):
yield path + '/' + p
yield path
class Memory:
def __init__(self, xml_out, pmid_out, pmid_filter, mesh_filter):
if xml_out is None:
self.xml_out = None
else:
stderr.write('Writing XML into %s\n' % xml_out)
self.xml_out = open(xml_out, 'w')
self.xml_out.write(XML_HEADER)
if pmid_out is None:
self.pmid_out = None
else:
stderr.write('Writing PMIDs into %s\n' % pmid_out)
self.pmid_out = open(pmid_out, 'w')
self.pmid_filter = pmid_filter
self.mesh_filter = mesh_filter
self.init()
def close(self):
if self.xml_out is not None:
self.xml_out.write(XML_FOOTER)
self.xml_out.close()
if self.pmid_out is not None:
self.pmid_out.close()
def init(self):
if self.xml_out is None:
self.buf = None
else:
self.buf = []
self.pmid = None
return state_start
def output(self):
if self.pmid_out is not None:
self.pmid_out.write(self.pmid)
self.pmid_out.write('\n')
if self.xml_out is not None:
for line in self.buf:
self.xml_out.write(line)
return state_output
return self.init()
def write(self, line):
if self.xml_out is not None:
self.xml_out.write(line)
def buffer(self, line):
if self.buf is not None:
self.buf.append(line)
def state_start(line, mem):
if line.strip().startswith('<PubmedArticle>'):
mem.buffer(line)
return state_buffer
return state_start
def state_buffer(line, mem):
mem.buffer(line)
m = PMID_PATTERN.search(line)
if m is not None:
mem.pmid = m.group(1)
if mem.pmid_filter is None or mem.pmid in mem.pmid_filter:
if mem.mesh_filter is None:
return mem.output()
return state_pmid
return state_reject
return state_buffer
def state_reject(line, mem):
if line.strip().startswith('</PubmedArticle>'):
return mem.init()
return state_reject
def state_pmid(line, mem):
if line.strip().startswith('</PubmedArticle>'):
return mem.init()
mem.buffer(line)
m = MESH_PATTERN.search(line)
if m is not None:
if m.group(1) in mem.mesh_filter:
return mem.output()
return state_pmid
def state_output(line, mem):
mem.xml_out.write(line)
if line.strip().startswith('</PubmedArticle>'):
return mem.init()
return state_output
FilterPubMed().filter()