From a sequence matrix to a spectrum¶
The key idea is a transform along sequence position, independently for each residue-property channel. The amino-acid alphabet is the lookup axis of the embedding, not the axis being Fourier transformed.
Project-provided illustration, reproduced unchanged. Its three channels and numerical values illustrate the matrix operation; the code below uses actual Kidera factors rather than transcribing the schematic scores.¶
Read the figure from left to right¶
Let a sequence contain \(N\) residues from an alphabet of size \(A=20\). Its one-hot matrix \(O\) has shape \(N\times20\): each real sequence row contains one 1 in the column for that residue. An embedding matrix \(E\) has shape \(20\times C\), with a row of channel scores for each residue. Multiplication selects the appropriate score row:
The figure uses \(C=3\). BLOSUM100 supplies 20 channels; Kidera supplies 10. The embedding can have any supported number of complete residue channels. Protspect performs the same lookup directly without materializing the one-hot matrix, which is useful for memory and speed.
Zero-pad the sequence axis¶
Choose \(L\geq N\). Appending \(L-N\) all-zero rows to the one-hot matrix produces \(\widetilde O\in\mathbb{R}^{L\times20}\). These are padding rows, not one-hot rows for an extra residue. With raw embedding scores:
Padding changes the sampling grid; it does not add residue information. Do not pad alphabet columns or embedding channels. If you enable centering or channel normalization, preprocess the real embedded rows first, then pad with zeros. Otherwise padding would contaminate the mean or become nonzero after centering.
Transform each signal column¶
For the Fourier transform, each channel produces one complex coefficient per sampled frequency:
In array notation this is fft(padded_embedding, axis=0). axis=0 is the
sequence-length axis, shown by the vertical arrow. fftshift(..., axes=0)
orders coefficients from negative to positive frequency; it does not change
which axis is transformed. Magnitude describes coefficient strength, while phase
retains alignment information that magnitude alone discards.
Check the matrix calculation against Protspect¶
import numpy as np
from scipy.fft import fft, fftshift
from protspect import transform_sequence
from protspect.embeddings import kidera_factors
sequence = "IKLFYR"
alphabet = "ACDEFGHIKLMNPQRSTVWY" # Same column ordering as the figure.
embedding = dict(list(kidera_factors().items())[:3])
channels = list(embedding)
N, L = len(sequence), 16
one_hot = np.eye(20)[[alphabet.index(aa) for aa in sequence]]
E = np.array([[embedding[channel][aa] for channel in channels]
for aa in alphabet])
X = one_hot @ E
padded_one_hot = np.pad(one_hot, ((0, L-N), (0, 0)))
padded_X = np.pad(X, ((0, L-N), (0, 0)))
np.testing.assert_allclose(padded_one_hot @ E, padded_X)
matrix_fft = fftshift(fft(padded_X, axis=0), axes=0)
result = transform_sequence(sequence, embedding, method="fourier", length=L)
library_fft = np.column_stack([result.coefficients[c] for c in channels])
np.testing.assert_allclose(matrix_fft, library_fft)
assert one_hot.shape == (6, 20) and E.shape == (20, 3)
assert library_fft.shape == (16, 3)
The same channel signals feed Z, Mellin, and wavelet transforms, although those backends use different kernels and output coordinates. Continue with Choose residue representations, Compare the four transforms, and Export and select coefficient features, or open notebook 01 in Examples and notebooks to change the sequence and matrix dimensions.