-
Notifications
You must be signed in to change notification settings - Fork 991
/
xml.py
273 lines (228 loc) · 9.38 KB
/
xml.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
XML format classes
"""
import logging
import re
from typing import List
from galaxy import util
from galaxy.datatypes.dataproviders.dataset import DatasetDataProvider
from galaxy.datatypes.dataproviders.hierarchy import XMLDataProvider
from galaxy.datatypes.metadata import MetadataElement
from galaxy.datatypes.protocols import DatasetProtocol
from galaxy.datatypes.sniff import (
build_sniff_from_prefix,
disable_parent_class_sniffing,
FilePrefix,
)
from . import (
data,
dataproviders,
)
log = logging.getLogger(__name__)
OWL_MARKER = re.compile(r"\<owl:")
SBML_MARKER = re.compile(r"\<sbml")
@dataproviders.decorators.has_dataproviders
@build_sniff_from_prefix
class GenericXml(data.Text):
"""Base format class for any XML file."""
edam_format = "format_2332"
file_ext = "xml"
def set_peek(self, dataset: DatasetProtocol, **kwd) -> None:
"""Set the peek and blurb text"""
if not dataset.dataset.purged:
dataset.peek = data.get_file_peek(dataset.get_file_name())
dataset.blurb = "XML data"
else:
dataset.peek = "file does not exist"
dataset.blurb = "file purged from disk"
def _has_root_element_in_prefix(self, file_prefix: FilePrefix, root: str) -> bool:
for line in file_prefix.line_iterator():
if not line.startswith("<?"):
break
# pattern match <root or <ns:root for any ns string
pattern = rf"^<(\w*:)?{root}"
return re.match(pattern, line) is not None
def sniff_prefix(self, file_prefix: FilePrefix) -> bool:
"""
Determines whether the file is XML or not
>>> from galaxy.datatypes.sniff import get_test_fname
>>> fname = get_test_fname( 'megablast_xml_parser_test1.blastxml' )
>>> GenericXml().sniff( fname )
True
>>> fname = get_test_fname( 'interval.interval' )
>>> GenericXml().sniff( fname )
False
"""
return file_prefix.startswith("<?xml ")
@staticmethod
def merge(split_files: List[str], output_file: str) -> None:
"""Merging multiple XML files is non-trivial and must be done in subclasses."""
if len(split_files) > 1:
raise NotImplementedError(
"Merging multiple XML files is non-trivial and must be implemented for each XML type"
)
# For one file only, use base class method (move/copy)
data.Text.merge(split_files, output_file)
@dataproviders.decorators.dataprovider_factory("xml", XMLDataProvider.settings)
def xml_dataprovider(self, dataset: DatasetProtocol, **settings) -> XMLDataProvider:
dataset_source = DatasetDataProvider(dataset)
return XMLDataProvider(dataset_source, **settings)
@disable_parent_class_sniffing
class MEMEXml(GenericXml):
"""MEME XML Output data"""
file_ext = "memexml"
def set_peek(self, dataset: DatasetProtocol, **kwd) -> None:
"""Set the peek and blurb text"""
if not dataset.dataset.purged:
dataset.peek = data.get_file_peek(dataset.get_file_name())
dataset.blurb = "MEME XML data"
else:
dataset.peek = "file does not exist"
dataset.blurb = "file purged from disk"
@disable_parent_class_sniffing
class CisML(GenericXml):
"""CisML XML data""" # see: http://www.ncbi.nlm.nih.gov/pubmed/15001475
file_ext = "cisml"
def set_peek(self, dataset: DatasetProtocol, **kwd) -> None:
"""Set the peek and blurb text"""
if not dataset.dataset.purged:
dataset.peek = data.get_file_peek(dataset.get_file_name())
dataset.blurb = "CisML data"
else:
dataset.peek = "file does not exist"
dataset.blurb = "file purged from disk"
class Dzi(GenericXml):
"""
Deep zoom image format, see
https://github.com/openseadragon/openseadragon/wiki/The-DZI-File-Format
"""
# General elements.
MetadataElement(
name="base_name", desc="Base name for this dataset", default="DeepZoomImage", readonly=True, set_in_upload=True
)
MetadataElement(name="format", desc="File format of the tiles", default=None, readonly=True, visible=True)
MetadataElement(name="tile_size", desc="Size of tiles", default=None, readonly=True, visible=True)
# Collection elements.
MetadataElement(
name="max_level", desc="Max pyramid level", default=None, readonly=True, optional=True, visible=True
)
MetadataElement(name="quality", desc="Quality", default=None, readonly=True, optional=True, visible=True)
# Image elements.
MetadataElement(name="height", desc="Size height", default=None, readonly=True, optional=True, visible=True)
MetadataElement(
name="overlap",
desc="Overlap of all four sides of tiles",
default=None,
readonly=True,
optional=True,
visible=True,
)
MetadataElement(name="width", desc="Size width", default=None, readonly=True, optional=True, visible=True)
file_ext = "dzi"
def __init__(self, **kwd):
super().__init__(**kwd)
def set_meta(self, dataset: DatasetProtocol, overwrite: bool = True, **kwd) -> None:
tree = util.parse_xml(dataset.get_file_name())
root = tree.getroot()
dataset.metadata.format = root.get("Format")
dataset.metadata.tile_size = root.get("TileSize")
# DeepZoom image files can include
# xml namespace attributes.
if root.tag.find("Collection") >= 0:
dataset.metadata.max_level = root.get("MaxLevel")
dataset.metadata.quality = root.get("Quality")
elif root.tag.find("Image") >= 0:
dataset.metadata.overlap = root.get("Overlap")
for elem in root:
if elem.tag.find("Size") >= 0:
dataset.metadata.width = elem.get("Width")
dataset.metadata.height = elem.get("Height")
def set_peek(self, dataset: DatasetProtocol, **kwd) -> None:
if not dataset.dataset.purged:
dataset.peek = data.get_file_peek(dataset.get_file_name())
dataset.blurb = "Deep Zoom Image"
else:
dataset.peek = "file does not exist"
dataset.blurb = "file purged from disc"
def sniff_prefix(self, file_prefix: FilePrefix) -> bool:
"""
Checking for keyword - 'Collection' or 'Image' in the first 200 lines.
>>> from galaxy.datatypes.sniff import get_test_fname
>>> fname = get_test_fname('1.dzi')
>>> Dzi().sniff(fname)
True
>>> fname = get_test_fname('megablast_xml_parser_test1.blastxml')
>>> Dzi().sniff(fname)
False
"""
for line in file_prefix.line_iterator():
line = line.lower()
if line.find("<collection") >= 0 or line.find("<image") >= 0:
return True
return False
class Phyloxml(GenericXml):
"""Format for defining phyloxml data http://www.phyloxml.org/"""
edam_data = "data_0872"
edam_format = "format_3159"
file_ext = "phyloxml"
def set_peek(self, dataset: DatasetProtocol, **kwd) -> None:
"""Set the peek and blurb text"""
if not dataset.dataset.purged:
dataset.peek = data.get_file_peek(dataset.get_file_name())
dataset.blurb = "Phyloxml data"
else:
dataset.peek = "file does not exist"
dataset.blurb = "file purged from disk"
def sniff_prefix(self, file_prefix: FilePrefix) -> bool:
""" "Checking for keyword - 'phyloxml' always in lowercase in the first few lines.
>>> from galaxy.datatypes.sniff import get_test_fname
>>> fname = get_test_fname( '1.phyloxml' )
>>> Phyloxml().sniff( fname )
True
>>> fname = get_test_fname( 'interval.interval' )
>>> Phyloxml().sniff( fname )
False
>>> fname = get_test_fname( 'megablast_xml_parser_test1.blastxml' )
>>> Phyloxml().sniff( fname )
False
"""
return self._has_root_element_in_prefix(file_prefix, "phyloxml")
class Owl(GenericXml):
"""
Web Ontology Language OWL format description
http://www.w3.org/TR/owl-ref/
"""
edam_format = "format_3262"
file_ext = "owl"
def set_peek(self, dataset: DatasetProtocol, **kwd) -> None:
if not dataset.dataset.purged:
dataset.peek = data.get_file_peek(dataset.get_file_name())
dataset.blurb = "Web Ontology Language OWL"
else:
dataset.peek = "file does not exist"
dataset.blurb = "file purged from disc"
def sniff_prefix(self, file_prefix: FilePrefix) -> bool:
"""
Checking for keyword - '<owl' in the first 200 lines.
"""
return file_prefix.search(OWL_MARKER)
class Sbml(GenericXml):
"""
System Biology Markup Language
http://sbml.org
"""
file_ext = "sbml"
edam_data = "data_2024"
edam_format = "format_2585"
def set_peek(self, dataset: DatasetProtocol, **kwd) -> None:
if not dataset.dataset.purged:
dataset.peek = data.get_file_peek(dataset.get_file_name())
dataset.blurb = "System Biology Markup Language SBML"
else:
dataset.peek = "file does not exist"
dataset.blurb = "file purged from disc"
def sniff_prefix(self, file_prefix: FilePrefix) -> bool:
"""
Checking for keyword - '<sbml' in the first 200 lines.
"""
return file_prefix.search(SBML_MARKER)