Skip to content

Commit

Permalink
Use __getitem__ instead of by_id, always available
Browse files Browse the repository at this point in the history
  • Loading branch information
mobiusklein committed Dec 1, 2023
1 parent 449e141 commit 564d79f
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions pyteomics/proforma.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ class TagTypeEnum(Enum):
group_placeholder = 999


class ModificationTagStyle(Enum):
Unset = 0
ShortId = 1
LongId = 2
ShortName = 3
LongName = 4


_sentinel = object()


Expand Down Expand Up @@ -354,7 +362,7 @@ def resolve(self, name=None, id=None, **kwargs):
if not defn:
raise KeyError(name)
elif id is not None:
defn = self.database.by_id(id)
defn = self.database[id]
if not defn:
raise KeyError(id)
else:
Expand All @@ -365,7 +373,8 @@ def resolve(self, name=None, id=None, **kwargs):
'name': defn['title'],
'id': defn['record_id'],
'mass': defn['mono_mass'],
'provider': self.name
'provider': self.name,
"source": self
}
else:
name = defn.ex_code_name
Expand All @@ -376,7 +385,8 @@ def resolve(self, name=None, id=None, **kwargs):
"name": name,
"id": defn.id,
"mass": defn.monoisotopic_mass,
"provider": self.name
"provider": self.name,
"source": self
}


Expand Down Expand Up @@ -420,7 +430,8 @@ def resolve(self, name=None, id=None, **kwargs):
'composition': composition,
'name': defn.name,
'id': defn.id,
'provider': self.name
'provider': self.name,
"source": self
}


Expand Down Expand Up @@ -453,7 +464,8 @@ def resolve(self, name=None, id=None, **kwargs):
'composition': composition,
'name': defn.name,
'id': defn.id,
'provider': self.name
'provider': self.name,
"source": self
}

# TODO: Implement resolve walking up the graph to get the mass. Can't really
Expand Down Expand Up @@ -565,7 +577,8 @@ def resolve(self, name=None, id=None, **kwargs):
"provider": self.name,
"composition": composition,
"monosaccharides": monosaccharides,
"mass": self.get_mass_from_term(term, raw_mass)
"mass": self.get_mass_from_term(term, raw_mass),
"source": self
}
return rec

Expand Down Expand Up @@ -631,12 +644,15 @@ class ModificationBase(TagBase):
'''

_tag_type = None
__slots__ = ('_definition', )
__slots__ = ('_definition', 'style')

def __init__(self, value, extra=None, group_id=None):
def __init__(self, value, extra=None, group_id=None, style=None):
if style is None:
style = ModificationTagStyle.Unset
super(ModificationBase, self).__init__(
self._tag_type, value, extra, group_id)
self._definition = None
self.style = style

def __eq__(self, other):
if isinstance(other, ModificationToken):
Expand Down Expand Up @@ -720,7 +736,19 @@ def _populate_from_definition(self, definition):
self._definition = definition

def _format_main(self):
return "{self.prefix_name}:{self.value}".format(self=self)
if self.style == ModificationTagStyle.Unset or self.style is None:
return "{self.prefix_name}:{self.value}".format(self=self)
elif self.style == ModificationTagStyle.LongId:
return "{self.prefix_name}:{self.id}".format(self=self)
elif self.style == ModificationTagStyle.ShortId:
return "{self.short_prefix}:{self.id}".format(self=self)
elif self.style == ModificationTagStyle.LongName:
return "{self.prefix_name}:{self.name}".format(self=self)
elif self.style == ModificationTagStyle.ShortName:
return "{self.short_prefix}:{self.name}".format(self=self)
else:
warnings.warn("Unknown formatting style {!r}".format(self.style))
return "{self.prefix_name}:{self.value}".format(self=self)

def resolve(self):
'''Find the term and return it's properties
Expand Down

0 comments on commit 564d79f

Please sign in to comment.