Skip to content

Instantly share code, notes, and snippets.

@mdjones
Last active March 10, 2018 23:42
Show Gist options
  • Save mdjones/2c164fafc0e9158c0024c2ab107bd50e to your computer and use it in GitHub Desktop.
Save mdjones/2c164fafc0e9158c0024c2ab107bd50e to your computer and use it in GitHub Desktop.
SimplePeptide class for matching [IL] isobaric peptides
class SimplePeptide:
"""
This allows comparisons between peptide sequences that are nearly identical
except for the Isboaric Isoleucine and Leucine [IL]. MS Proteomics is based on
identifying peptides based on their fragment mass profiles. Since [IL] have the
same mass they can not be differentiated.
The equals is acomplished by a simple replacement of all r'[IL]' in the sequence with
the string [IL]. Then an exact match (No Regex matching needed) is the most efficient
way to match two peptides.
You could also use the complied pattern to do a RegEx pattern search against a larger sequence.
This was really for putting the object into a Pandas DF
pdDF['SIMPLE_PEP'] = pdDF['Sequence'].apply(lambda x: SimplePeptide(x))
"""
def __init__(self, sequence):
self.sequence = sequence
self.pattern_string = re.sub(r'[IL]', '[IL]', sequence)
self.pattern = re.compile(self.pattern_string)
self.myhash = hash(self.pattern_string)
def __eq__(self, other):
if isinstance(self, other.__class__):
return bool(self.pattern_string == other.pattern_string)
return False
def __hash__(self):
return self.myhash
def __lt__(self, other):
return self.pattern_string < other.pattern_string
def __str__(self):
return self.sequence
pep1 = SimplePeptide('SDFSDFSDFLSDFISDFR')
pep2 = SimplePeptide('SDFSDFSDFLSDFISDFR')
pep3 = SimplePeptide('SDFSDFSDFISDFLSDFR')
pep4 = SimplePeptide('XZCZXCZXCIZXCLSDFR')
print('{} = {} ? {}'.format(pep1, pep2, (pep1==pep2))) # SDFSDFSDFLSDFISDFR = SDFSDFSDFLSDFISDFR ? True
print('{} = {} ? {}'.format(pep1, pep3, (pep1==pep3))) # SDFSDFSDFLSDFISDFR = SDFSDFSDFISDFLSDFR ? True
print('{} = {} ? {}'.format(pep1, pep4, (pep1==pep4))) # SDFSDFSDFLSDFISDFR = XZCZXCZXCIZXCLSDFR ? False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment