|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +from serializable import Serializable |
| 14 | + |
| 15 | + |
| 16 | +class Protein(Serializable): |
| 17 | + """ |
| 18 | + Lightweight view object exposing the protein identity for a transcript. |
| 19 | + Accessed via :attr:`Transcript.protein`. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, protein_id, protein_version=None): |
| 23 | + self.protein_id = protein_id |
| 24 | + self.protein_version = protein_version |
| 25 | + |
| 26 | + @property |
| 27 | + def id(self): |
| 28 | + """Alias for :attr:`protein_id`.""" |
| 29 | + return self.protein_id |
| 30 | + |
| 31 | + @property |
| 32 | + def version(self): |
| 33 | + """Alias for :attr:`protein_version`.""" |
| 34 | + return self.protein_version |
| 35 | + |
| 36 | + @property |
| 37 | + def versioned_protein_id(self): |
| 38 | + """``protein_id.protein_version`` when available, else ``protein_id``.""" |
| 39 | + if self.protein_version is None: |
| 40 | + return self.protein_id |
| 41 | + return "%s.%d" % (self.protein_id, self.protein_version) |
| 42 | + |
| 43 | + @property |
| 44 | + def versioned_id(self): |
| 45 | + """Alias for :attr:`versioned_protein_id`.""" |
| 46 | + return self.versioned_protein_id |
| 47 | + |
| 48 | + def __eq__(self, other): |
| 49 | + return ( |
| 50 | + other.__class__ is Protein |
| 51 | + and self.protein_id == other.protein_id |
| 52 | + and self.protein_version == other.protein_version |
| 53 | + ) |
| 54 | + |
| 55 | + def __hash__(self): |
| 56 | + return hash((self.protein_id, self.protein_version)) |
| 57 | + |
| 58 | + def __str__(self): |
| 59 | + return "Protein(protein_id='%s', protein_version=%s)" % ( |
| 60 | + self.protein_id, |
| 61 | + self.protein_version, |
| 62 | + ) |
| 63 | + |
| 64 | + def __repr__(self): |
| 65 | + return str(self) |
| 66 | + |
| 67 | + def to_dict(self): |
| 68 | + return { |
| 69 | + "protein_id": self.protein_id, |
| 70 | + "protein_version": self.protein_version, |
| 71 | + } |
0 commit comments