%load_ext autoreload
%autoreload 2
from seqlike.SequenceLike import SequenceLike
from itertools import product
import matplotlib.pyplot as plt
How to use the SequenceLike
class
In this notebook, we will show you how to use the SequenceLike
class.
tl;dr example
We can generate SequenceLike classes from sequences that are more complicated in computational form than simple strings. One example is a codon sequence, which comes in triplets of letters.
sequence = ["ACC", "CAT", "GCA", "AAA", "ATA", "AAA", "ACC", "CAT"]
By passing it into the SequenceLike
constructor,
we can gain access to many of the convenient methods available for SeqLike objects.
s = SequenceLike(sequence)
For example, it's possible to count the number of times a sequence element is found in the sequence:
s.count(["ACC"])
The alphabet is also inferred directly from the sequence. (Unless explicitly specified, of course!)
s.alphabet
codon_alphabet = [f"{l1}{l2}{l3}" for l1, l2, l3 in product("ATGC", "ATGC", "ATGC")]
s2 = SequenceLike(sequence, alphabet=codon_alphabet)
s2.alphabet[0:10]
We can obtain matrix representations of the sequence as well.
s.to_index()
By comparison, if the alphabet is defined, the indexing will be different:
s2.to_index()
Same goes for the one-hot representation:
plt.imshow(s.to_onehot())
plt.imshow(s2.to_onehot())
The string representation also provides a sane default:
str(s)
s.to_str()