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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
# -*- coding: utf-8 -*-
# encoding: utf-8
"""
The class :py:class:`Reader` has been designed to selectively extract data
from a mzML file and to expose the data as a python object.
Necessary information are read in and stored in a fast
accessible format.
The reader itself is an iterator, thus looping over all spectra
follows the classical pythonian syntax.
Additionally one can random access spectra by their nativeID
if the file if not truncated by a conversion Program.
Note:
The class :py:class:`Writer` is still in development.
"""
# Python mzML module - pymzml
# Copyright (C) 2010-2019 M. Kösters, C. Fufezan
# The MIT License (MIT)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import re
import os
import xml.etree.ElementTree as ElementTree
from collections import defaultdict as ddict
from io import BytesIO
from pathlib import Path
from . import spec
from . import obo
from . import regex_patterns
from .file_interface import FileInterface
from .file_classes.standardMzml import StandardMzml
class Reader(object):
"""
Initialize Reader object for a given mzML file.
Arguments:
path (str): path to the mzml file to parse.
Keyword Arguments:
MS_precisions (dict): measured precisions for the different MS levels.
e.g.::
{
1 : 5e-6,
2 : 20e-6
}
obo_version (str, optional): obo version number as string. If not
specified the version will be extracted from the mzML file
Note:
Setting the precision for MS1 and MSn spectra has changed in version 1.2.
However, the old syntax as kwargs is still compatible ( e.g. 'MS1_Precision=5e-6').
"""
def __init__(
self,
path_or_file,
MS_precisions=None,
obo_version=None,
build_index_from_scratch=False,
skip_chromatogram=True,
index_regex=None,
**kwargs
):
"""Initialize and set required attributes."""
self.index_regex = index_regex
self.build_index_from_scratch = build_index_from_scratch
self.skip_chromatogram = skip_chromatogram
if MS_precisions is None:
MS_precisions = {}
if "MS1_Precision" in kwargs.keys():
MS_precisions[1] = kwargs["MS1_Precision"]
if "MSn_Precision" in kwargs.keys():
MS_precisions[2] = kwargs["MSn_Precision"]
MS_precisions[3] = kwargs["MSn_Precision"]
# Parameters
self.ms_precisions = {
None: 0.0001, # if spectra does not contain ms_level information
# e.g. UV-chromatograms (thanks pyeguy) then ms_level is
# returned as None
0: 0.0001,
1: 5e-6,
2: 20e-6,
3: 20e-6,
}
self.ms_precisions.update(MS_precisions)
# File info
self.info = ddict()
self.path_or_file = path_or_file
if isinstance(self.path_or_file, Path):
self.path_or_file = str(self.path_or_file)
if isinstance(self.path_or_file, str):
self.info["file_name"] = self.path_or_file
self.info["encoding"] = self._determine_file_encoding(self.path_or_file)
else:
self.info["encoding"] = self._guess_encoding(self.path_or_file)
self.info["file_object"] = self._open_file(self.path_or_file)
self.info["offset_dict"] = self.info["file_object"].offset_dict
if obo_version:
self.info["obo_version"] = self._obo_version_validator(obo_version)
else:
# obo version not specified -> try to identify from mzML by self._init_iter
self.info["obo_version"] = None
self.iter = self._init_iter()
self.OT = self._init_obo_translator()
def __next__(self):
"""
Iterator for the class :py:class:`Run`.
Iterates all of the spectra in the file.
Returns:
Spectrum (:py:class:`Spectrum`): a spectrum object with interface
to the original spectrum element.
Example:
>>> for spectrum in Reader:
... print(spectrum.mz, end='\\r')
"""
has_ref_group = self.info.get("referenceable_param_group_list", False)
while True:
event, element = next(self.iter, ("END", "END"))
if event == "end":
if element.tag.endswith("}spectrum"):
spectrum = spec.Spectrum(element)
if has_ref_group:
spectrum._set_params_from_reference_group(
self.info["referenceable_param_group_list_element"]
)
ms_level = spectrum.ms_level
spectrum.measured_precision = self.ms_precisions[ms_level]
spectrum.calling_instance = self
return spectrum
if element.tag.endswith("}chromatogram"):
if self.skip_chromatogram:
continue
spectrum = spec.Chromatogram(element)
# if has_ref_group:
# spectrum._set_params_from_reference_group(
# self.info['referenceable_param_group_list_element']
# )
spectrum.calling_instance = self
return spectrum
elif event == "END":
# reinit iter
self.info["file_object"] = self._open_file(self.path_or_file)
self.iter = self._init_iter()
raise StopIteration
def __getitem__(self, identifier):
"""
Access spectrum with native id 'identifier'.
Arguments:
identifier (str or int): last number in the id tag of the spectrum
element
Returns:
spectrum (Spectrum or Chromatogram): spectrum/chromatogram object
with native id 'identifier'
"""
try:
if int(identifier) > self.get_spectrum_count():
raise Exception("Requested identifier is out of range")
except:
pass
spectrum = self.info["file_object"][identifier]
spectrum.calling_instance = self
if isinstance(spectrum, spec.Spectrum):
spectrum.measured_precision = self.ms_precisions[spectrum.ms_level]
return spectrum
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
@property
def file_class(self):
"""Return file object in use."""
return type(self.info["file_object"].file_handler)
def _open_file(self, path_or_file):
"""
Open the path using the FileInterface class as a wrapper.
Arguments:
path (str): path to the file to parse
Returns:
(FileInterface): Wrapper class for compressed and uncompressed
mzml files
"""
return FileInterface(
path_or_file,
self.info["encoding"],
build_index_from_scratch=self.build_index_from_scratch,
index_regex=self.index_regex
)
def _guess_encoding(self, mzml_file):
"""
Determine the encoding used for the file.
Arguments:
mzml_file (IOBase): an mzml file
Returns:
mzml_encoding (str): encoding type of the file
"""
match = regex_patterns.FILE_ENCODING_PATTERN.search(mzml_file.readline())
if match:
return bytes.decode(match.group("encoding"))
else:
return "utf-8"
def _determine_file_encoding(self, path):
"""
Determine the encoding used for the file in path.
Arguments:
path (str): path to the mzml files
Returns:
mzml_encoding (str): encoding type of the file
"""
if os.path.exists(path):
if path.endswith(".gz") or path.endswith(".igz"):
import gzip
_open = gzip.open
else:
_open = open
with _open(path, "rb") as sniffer:
return self._guess_encoding(sniffer)
@staticmethod
def _obo_version_validator(version):
"""
The obo version should fit file names in the obo folder.
However, some software generate mzML with built in obo version string like:
'23:06:2017' or even newer version that not in obo folder yet.
This is to check obo version and try to fit the best obo version
to the obo version in the mzML file.
Arguments:
version (str): The original version to check.
Returns:
version_fixed (str): The checked obo version.
"""
obo_rgx = re.compile(r"(\d\.\d{1,2}\.\d{1,2})(_[rR][cC]\d{0,2})?")
obo_years_rgx = re.compile(r"20\d\d")
obo_year_version_dct = {
2012: "3.40.0",
2013: "3.50.0",
2014: "3.60.0",
2015: "3.75.0",
2016: "4.0.1",
2017: "4.1.0",
2018: "4.1.10",
2019: "4.1.22",
}
version_fixed = None
if obo_rgx.match(version):
version_fixed = version
else:
if obo_years_rgx.search(version):
years_found = obo_years_rgx.search(version)
if years_found:
try:
year = int(years_found.group(0))
except ValueError:
year = 2000
if year in obo_year_version_dct:
version_fixed = obo_year_version_dct[year]
else:
if year > 2019:
version_fixed = "4.1.0"
if version_fixed:
# Check if the corresponding obo file existed in obo folder
obo_root = os.path.dirname(__file__)
obo_file = os.path.join(
obo_root,
"obo",
"psi-ms{0}.obo".format("-" + version_fixed if version_fixed else ""),
)
if os.path.exists(obo_file) or os.path.exists(obo_file + ".gz"):
pass
else:
version_fixed = "1.1.0"
else:
version_fixed = "1.1.0"
return version_fixed
def _init_obo_translator(self):
"""
Initialize the obo translator with the minimum requirement
and extra Accessions.
Returns:
obo_translator (OboTranslator): translator class to translate
accessions to names
"""
# parse obo, check MS tags and if they are ok in minimum.py (minimum
# required) ...
if self.info.get("obo_version", None) is None:
self.info["obo_version"] = "1.1.0"
obo_translator = obo.OboTranslator(version=self.info["obo_version"])
return obo_translator
def _init_iter(self):
"""
Initalize the iterator for the spectra and sets it to the start
of the spectrumList element.
Returns:
mzml_iter (xml.etree.ElementTree._IterParseIterator): Iterator over
all element in the file starting with the first spectrum
"""
mzml_iter = iter(
ElementTree.iterparse(self.info["file_object"], events=("end", "start"))
) # NOTE: end might be sufficient
_, self.root = next(mzml_iter)
self.info["chromatogram_count"] = None
self.info["spectrum_count"] = None
while True:
event, element = next(mzml_iter, ("END", "END"))
if element.tag.endswith("}mzML"):
if "version" in element.attrib and len(element.attrib["version"]) > 0:
self.info["mzml_version"] = element.attrib["version"]
else:
s = element.attrib[
"{http://www.w3.org/2001/XMLSchema-instance}" "schemaLocation"
]
self.info["mzml_version"] = re.search(
r"[0-9]*\.[0-9]*\.[0-9]*", s
).group()
elif element.tag.endswith("}cv"):
if (
not self.info["obo_version"]
and element.attrib.get("id", None) == "MS"
):
obo_in_mzml = element.attrib.get("version", "1.1.0")
self.info["obo_version"] = self._obo_version_validator(obo_in_mzml)
elif element.tag.endswith("}referenceableParamGroupList"):
self.info["referenceable_param_group_list"] = True
self.info["referenceable_param_group_list_element"] = element
elif element.tag.endswith("}spectrumList"):
spec_cnt = element.attrib.get("count")
self.info["spectrum_count"] = int(spec_cnt) if spec_cnt else None
break
elif element.tag.endswith("}chromatogramList"):
chrom_cnt = element.attrib.get("count", None)
if chrom_cnt:
self.info["chromatogram_count"] = int(chrom_cnt)
break
elif element.tag.endswith("}run"):
run_id = element.attrib.get("id")
start_time = element.attrib.get("startTimeStamp")
self.info["run_id"] = run_id
self.info["start_time"] = start_time
else:
pass
self.root.clear()
return mzml_iter
def __iter__(self):
"""Return self."""
return self
def next(self):
"""Function to return the next Spectrum element."""
return self.__next__()
def get_spectrum_count(self):
"""
Number of spectra in file.
Returns:
spectrum count (int): Number of spectra in file.
"""
return self.info["spectrum_count"]
def get_chromatogram_count(self):
"""
Number of chromatograms in file.
Returns:
chromatogram count (int): Number of chromatograms in file.
"""
return self.info["chromatogram_count"]
def close(self):
self.info["file_object"].close()
if __name__ == "__main__":
print(__doc__)
|