Skip to content

Instantly share code, notes, and snippets.

@audy
Created January 24, 2018 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audy/7e3cee62897b1eac13dbd4c6c34e3759 to your computer and use it in GitHub Desktop.
Save audy/7e3cee62897b1eac13dbd4c6c34e3759 to your computer and use it in GitHub Desktop.
class DNA:
def __init__(self, sequence: str) -> None:
self.sequence = str(sequence).lower()
@property
def reverse(self) -> 'DNA':
return DNA(self.sequence[::-1])
@property
def complement(self: 'DNA') -> 'DNA':
t = { a: b for a, b in zip('gatc', 'ctag') }
return DNA(''.join(t[i] for i in self.sequence))
def __repr__(self) -> str:
return '<DNA sequence="{}">'.format(self.sequence)
def __contains__(self, other: 'DNA') -> bool:
return other.sequence in self.sequence
def __eq__(self, other: 'DNA') -> object:
return self.sequence == other.sequence
template = DNA('ATGC')
assert template == template
assert template.sequence == DNA('atgc')
assert template.reverse == DNA('cgta')
assert template.reverse.complement == DNA('gcat')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment