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
|
# -*- coding: utf-8 -*-
# encoding: utf-8
"""
Class to parse the obo file and set up the accessions library
The OBO parser has been designed to convert MS:xxxxx tags to their appropriate
names. A minimal set of MS accession is used in pymzML, but additional
accessions can easily be queried.
The obo translator is used internally to associate names with MS:xxxxxxx tags.
The OboTranslator Class generates a dictionary and several lookup tables.
e.g.
::
>>> from pymzml.obo import OboTranslator as OT
>>> translator = OT()
>>> translator['MS:1000127']
'centroid mass spectrum'
>>> translator['positive scan']
{'is_a': 'MS:1000465 ! scan polarity', 'id': 'MS:1000130', 'def':
'"Polarity of the scan is positive." [PSI:MS]', 'name': 'positive scan'}
>>> translator['scan']
{'relationship': 'part_of MS:0000000 ! Proteomics Standards Initiative Mass
Spectrometry Ontology', 'id': 'MS:1000441', 'def': '"Function or process of
the mass spectrometer where it records a spectrum." [PSI:MS]', 'name':
'scan'}
>>> translator['unit']
{'relationship': 'part_of MS:0000000 ! Proteomics Standards Initiative Mass
Spectrometry Ontology', 'id': 'MS:1000460', 'def': '"Terms to describe
units." [PSI:MS]', 'name': 'unit'}
pymzML comes with the queryOBO.py script that can be used to interrogate the
OBO file. Please refer to :ref:`example_scripts` for further usage information.
::
$ ./example_scripts/queryOBO.py "scan time"
MS:1000016
scan time
"The time taken for an acquisition by scanning analyzers." [PSI:MS]
Is a: MS:1000503 ! scan attribute
$
::
$ ./example_scripts/queryOBO.py 1000016
MS:1000016
scan time
"The time taken for an acquisition by scanning analyzers." [PSI:MS]
MS:1000503 ! scan attribute
$
"""
# 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 sys
import os
import re
import gzip
import urllib
class OboTranslator(object):
"""
Generates a mapping from MS:xxxxx to names and vice versa for a specific
obo version
Args:
version (str): obo version
"""
def __init__(self, version=None):
self.version = self.__normalize_version(version)
self.all_dicts = []
self.id = {}
self.name = {}
self.definition = {}
self.lookups = [self.id, self.name, self.definition]
self.MS_tag_regex = re.compile(r"MS:[0-9]*")
# Only parse the OBO when necessary, not upon object construction
self.__obo_parsed = False
def __setitem__(self, key, value):
raise TypeError("OBO translator dictionaries only support assignment via .add")
def __getitem__(self, key):
if not self.__obo_parsed:
self.parseOBO()
for lookup in self.lookups:
if key in lookup:
if self.MS_tag_regex.match(key):
try:
return lookup[key]["name"]
except:
pass
return lookup[key]
return None
@staticmethod
def __normalize_version(version):
"""
Ensure that a version has 3 components, defaulting to .0 for the
missing components.
Args:
version (str): The original version to modify.
Returns:
version (str): The version, normalized to ensure that it has 3
parts.
"""
if version is not None:
parts = version.split(".")
missing_parts = 3 - len(parts)
if missing_parts > 0:
version = ".".join(parts + ["0"] * missing_parts)
return version
def download_obo(self, version, obo_file):
uri = f"https://raw.githubusercontent.com/pymzml/psi-ms-CV/v{self.version}/psi-ms.obo"
urllib.request.urlretrieve(uri, obo_file)
with open(obo_file, "rb") as fin, gzip.open(obo_file + ".gz", "wb") as fout:
fout.writelines(fin.readlines())
os.remove(obo_file)
return
def parseOBO(self):
self.__obo_parsed = True
"""
Locate and parse the OBO file in the OBO root directory.
Note:
cx_Freeze friendly. If using cx_Freeze, place the OBO folder at
the location of sys.executable.
"""
# TODO: Try to get all the versions, even those without well-defined
# version numbers, or get remote hosting of all of the versions
# and only download one at will on demand.
# Modify the root for cx_freeze
if getattr(sys, "frozen", False):
obo_root = os.path.dirname(sys.executable)
else:
obo_root = os.path.dirname(__file__)
obo_file = os.path.join(
obo_root,
"obo",
"psi-ms{0}.obo".format("-" + self.version if self.version else ""),
)
if os.path.exists(obo_file):
pass
elif os.path.exists(obo_file + ".gz"):
obo_file = obo_file + ".gz"
else:
self.download_obo(self.version, obo_file)
obo_file += ".gz"
with open(obo_file, "rb") as fin:
# never rely on file extensions!
first_two_bytes = fin.read(2)
# check if file is gzipped by magic bytes
if first_two_bytes == b"\x1f\x8b":
open_func = gzip.open
else:
raise Exception(
"Filename has .gz extension but is missing the gzip magic bytes.\n"
"The file may be corrupted or not gzipped."
)
with open_func(obo_file, "rt", encoding="utf-8") as obo:
collections = {}
collect = False
for line in obo:
if line.strip() in ("[Term]", ""):
collect = True
if not collections:
continue
self.add(collections)
collections = {}
else:
if line.strip() != "" and collect is True:
k = line.find(":")
collections[line[:k]] = line[k + 1 :].strip()
return
def add(self, collection_dict):
"""
Add a new dict to the translator.
Args:
collection_dict (dict): python dict containing MS:xxxxxxx name
mapping.
"""
if not self.__obo_parsed:
self.parseOBO()
self.all_dicts.append(collection_dict)
if "id" in collection_dict.keys():
self.id[collection_dict["id"]] = self.all_dicts[-1]
if "name" in collection_dict.keys():
self.name[collection_dict["name"]] = self.all_dicts[-1]
if "def" in collection_dict.keys():
self.definition[collection_dict["def"]] = self.all_dicts[-1]
return
def checkOBO(self, idTag, name):
"""
Check if idTag equals name in currently used obo version.
Args:
idTag (str): MS accession tag
name (str): trivial name of the accession
Returns:
boolean: True if idTag and name correspond, else False.
"""
if not self.__obo_parsed:
self.parseOBO()
if self.id[idTag]["name"] == name:
return True
else:
return False
if __name__ == "__main__":
print(__doc__)
|