Skip to content

Commit c7576ff

Browse files
committed
Merge branch 'main' into readme
2 parents 3b52732 + 56e903c commit c7576ff

11 files changed

Lines changed: 1271 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for datasets and their loaders."""

pyaptamer/experiments/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Base classes for experiments."""
2+
3+
__all__ = ["Aptamer"]
4+
5+
from pyaptamer.experiments._aptamer import Aptamer

pyaptamer/experiments/_aptamer.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
__author__ = ["nennomp"]
2+
__all__ = ["Aptamer"]
3+
4+
import torch
5+
from skbase.base import BaseObject
6+
7+
from pyaptamer.utils import rna2vec
8+
9+
10+
class Aptamer(BaseObject):
11+
"""Candidate aptamer evaluation for a given target protein.
12+
13+
Parameters
14+
----------
15+
target_encoded : torch.Tensor
16+
Encoded target sequence tensor.
17+
target : str, optional
18+
Target sequence string.
19+
model : torch.nn.Module
20+
Model to use for assigning scores.
21+
device : torch.device
22+
Device to run the model on.
23+
24+
Examples
25+
--------
26+
from pyaptamer.experiment import Aptamer
27+
>>> experiment = Aptamer(target_encoded, target, model, device)
28+
>>> score = experiment.run(aptamer_candidate)
29+
"""
30+
31+
def __init__(
32+
self,
33+
target_encoded: torch.Tensor,
34+
target: str,
35+
model: torch.nn.Module,
36+
device: torch.device,
37+
) -> None:
38+
"""
39+
Parameters
40+
----------
41+
target_encoded : torch.Tensor
42+
Encoded target sequence tensor.
43+
target : str, optional
44+
Target sequence string.
45+
model : torch.nn.Module
46+
Model to use for assigning scores.
47+
device : torch.device
48+
Device to run the model on.
49+
"""
50+
self.target_encoded = target_encoded.to(device)
51+
self.target = target
52+
self.model = model
53+
self.device = device
54+
55+
super().__init__()
56+
57+
def _inputnames(self) -> list[str]:
58+
"""Return the inputs of the experiment."""
59+
return ["aptamer_candidate"]
60+
61+
def _reconstruct(self, sequence: str = "") -> torch.Tensor:
62+
"""Reconstruct the actual aptamer sequence from the encoded representation.
63+
64+
The encoding uses pairs like 'A_' (add A to left) and '_A' (add A to right).
65+
This method converts these pairs back to the actual sequence. Then, from its
66+
RNA sequence representation it is converted to a vector.
67+
68+
Parameters
69+
----------
70+
seq : str
71+
Encoded sequence with direction markers (underscores).
72+
73+
Returns
74+
-------
75+
torch.Tensor
76+
The reconstructed RNA sequence as a vector.
77+
"""
78+
result = ""
79+
for i in range(0, len(sequence), 2):
80+
match sequence[i]:
81+
case "_":
82+
# append the next values
83+
result = result + sequence[i + 1]
84+
case _:
85+
# prepend the current value
86+
result = sequence[i] + result
87+
88+
return torch.tensor(rna2vec([result]))
89+
90+
@torch.no_grad()
91+
def evaluate(self, aptamer_candidate: str) -> None:
92+
"""Evaluate the given aptamer candidate by assigning a score.
93+
94+
Parameters
95+
----------
96+
aptamer_candidate : str
97+
The aptamer candidate to evaluate. It should be a string consisting of
98+
letters representing nucleotides: 'A_', '_A', 'C_', '_C', 'G_', '_G', 'U_',
99+
'_U'. Underscores indicate whether the nucleotides are supposed to be (e.
100+
g., 'A_') prepended or appended (e.g., '_A)'to the sequence.
101+
102+
Returns
103+
-------
104+
torch.Tensor
105+
The score assigned to the aptamer candidate.
106+
"""
107+
aptamer_candidate = self._reconstruct(aptamer_candidate)
108+
109+
self.model.eval()
110+
return self.model(
111+
aptamer_candidate.to(self.device),
112+
self.target_encoded,
113+
)

pyaptamer/mcts/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Monte Carlo Tree Search (MCTS) algorithm for string optimization."""
2+
3+
__all__ = ["MCTS"]
4+
5+
from pyaptamer.mcts._algorithm import MCTS

0 commit comments

Comments
 (0)