Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Interprets an xml branch as CVParams
:param branch:
:return: controlled value parameter set with values
:rtype: CVParameterSet
"""
out = {}
for cvParam in branch.getElementsByTagName('cvParam'):
acc = cvParam.getAttribute('accession') # accession key
out[acc] = {}
for attribute, value in cvParam.attributes.items(): # pull all the attributes
if attribute != 'accession':
# attempt to convert to integer or float, keep as string otherwise
out[acc][attribute] = stringtodigit(value)
return CVParameterSet(**out)
'UO:0000098': 'milliliter',
'UO:0000106': 'hertz',
'UO:0000110': 'pascal',
'UO:0000112': 'joule',
'UO:0000166': 'parts per notation unit',
'UO:0000169': 'parts per million',
'UO:0000175': 'gram per liter',
'UO:0000185': 'degree',
'UO:0000218': 'volt',
'UO:0000228': 'tesla',
'UO:0000266': 'electronvolt',
'UO:0000268': 'volt per meter',
}
# object for automatic retrieval of unit names from accession values
cv_units = CVParameterSet(
**{acc: {'name': unit_definitions[acc]} for acc in unit_definitions}
)
'MS:1000580': 'MSn spectrum',
'MS:1000581': 'CRM spectrum',
'MS:1000582': 'SIM spectrum',
'MS:1000583': 'SRM spectrum',
}
othertypes = { # other accession keys (non-MS)
'MS:1000620': 'PDA spectrum',
'MS:1000804': 'electromagnetic radiation spectrum',
'MS:1000805': 'emission spectrum',
'MS:1000806': 'absorption spectrum',
}
out = {}
if isinstance(hand, CVParameterSet): # handed a cvparam class object (expected)
p = hand
else: # handed a tree or branch (generate the cvparam class object)
p = CVParameterSet(hand)
for acc in p.keys() & mstypes.keys(): # check for ms spectrum
out['acc'] = acc # accession code
out['name'] = mstypes[acc] # name of spectrum
out['type'] = 'MS' # it is a mass spectrum
out['level'] = p['MS:1000511'].value # ms level
out['window'] = [p['MS:1000501'].value, p['MS:1000500'].value] # scan window
if 'MS:1000129' in p: # negative scan
out['mode'] = '-'
elif 'MS:1000130' in p: # positive scan
out['mode'] = '+'
if 'MS:1000827' in p: # if there is an isolation window target m/z
out['target'] = p['MS:1000827'].value
# if MSn > 2, not sure how to handle this (will have to be hard coded later as I have no examples)
elif out['level'] > 2:
raise ValueError(
'This script has not been coded to handle MSn > 2, please contact the author of the class')
'MS:1000341': 'precursor ion spectrum',
'MS:1000343': 'product ion spectrum',
'MS:1000579': 'MS1 spectrum',
'MS:1000580': 'MSn spectrum',
'MS:1000581': 'CRM spectrum',
'MS:1000582': 'SIM spectrum',
'MS:1000583': 'SRM spectrum',
}
othertypes = { # other accession keys (non-MS)
'MS:1000620': 'PDA spectrum',
'MS:1000804': 'electromagnetic radiation spectrum',
'MS:1000805': 'emission spectrum',
'MS:1000806': 'absorption spectrum',
}
out = {}
if isinstance(hand, CVParameterSet): # handed a cvparam class object (expected)
p = hand
else: # handed a tree or branch (generate the cvparam class object)
p = CVParameterSet(hand)
for acc in p.keys() & mstypes.keys(): # check for ms spectrum
out['acc'] = acc # accession code
out['name'] = mstypes[acc] # name of spectrum
out['type'] = 'MS' # it is a mass spectrum
out['level'] = p['MS:1000511'].value # ms level
out['window'] = [p['MS:1000501'].value, p['MS:1000500'].value] # scan window
if 'MS:1000129' in p: # negative scan
out['mode'] = '-'
elif 'MS:1000130' in p: # positive scan
out['mode'] = '+'
if 'MS:1000827' in p: # if there is an isolation window target m/z
out['target'] = p['MS:1000827'].value
# if MSn > 2, not sure how to handle this (will have to be hard coded later as I have no examples)
:rtype: str
"""
name = name.lower() # avoids case sensitivity
for acc in self.cv_values:
if self.cv_values[acc].name.lower() == name:
return acc
raise KeyError(f'No accession key was found matching the supplied name {name}.')
def print_properties(self, key):
"""Prints the properties of the provided key"""
cv_param_def.print_properties(key) # print the detailed information
if self.cv_values[key].value is not None:
print(f'value: {self.cv_values[key].value}') # followed by the value
class CVParameterDefinitions(CVParameterSet):
def __init__(self,
obo_path=OBOURL,
):
"""
Loads and interprets a PSI-MS obo file into a python-interpretable format.
:param obo_path: file path or url to an obo file
"""
CVParameterSet.__init__(self)
try:
self.obo_file = obonet.read_obo(obo_path) # read the obo file
except (FileNotFoundError, urllib.error.HTTPError):
raise FileNotFoundError(f'An obo file could not be found at the provided path or URL: {obo_path}')
if obo_path == OBOURL: # remind the user to cite the pulication
print('Data was read from PSI-MS, please cite DOI: 10.1093/database/bat009')
self.format_version = self.obo_file.graph['format-version']
def __init__(self,
obo_path=OBOURL,
):
"""
Loads and interprets a PSI-MS obo file into a python-interpretable format.
:param obo_path: file path or url to an obo file
"""
CVParameterSet.__init__(self)
try:
self.obo_file = obonet.read_obo(obo_path) # read the obo file
except (FileNotFoundError, urllib.error.HTTPError):
raise FileNotFoundError(f'An obo file could not be found at the provided path or URL: {obo_path}')
if obo_path == OBOURL: # remind the user to cite the pulication
print('Data was read from PSI-MS, please cite DOI: 10.1093/database/bat009')
self.format_version = self.obo_file.graph['format-version']
self.data_version = self.obo_file.graph['data-version']
for acc in self.obo_file:
dct = self.obo_file.nodes[acc]
if 'def' in dct: # if the invalid key def is in the dictionary, convert and remove
dct['definition'] = dct['def']
del dct['def']
self.cv_values[acc] = CVParam(
id=acc,
**dct,