Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
okkevaneck authored Nov 10, 2022
2 parents 30e5705 + 8537f87 commit 8ad32ad
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 8 deletions.
Binary file modified archives/prospr_core.tar.gz
Binary file not shown.
Binary file modified archives/prospr_core.zip
Binary file not shown.
3 changes: 3 additions & 0 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ all keep track of exactly.
p_2d.solutions_checked
>>> 0
p_2d.aminos_placed
>>> 1
p_2d.bond_values
>>> {"HH": -1}
Expand Down
2 changes: 2 additions & 0 deletions prospr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from prospr_core import AminoAcid, Protein, depth_first, depth_first_bnb
from .datasets import load_vanEck250, load_vanEck1000
from .visualize import plot_protein

__all__ = [
"AminoAcid",
Expand All @@ -8,4 +9,5 @@
"depth_first_bnb",
"load_vanEck250",
"load_vanEck1000",
"plot_protein",
]
2 changes: 2 additions & 0 deletions prospr/core/core_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ PYBIND11_MODULE(prospr_core, m) {
py::arg("bond_symmetry")=true)
.def_property_readonly("solutions_checked",
&Protein::get_solutions_checked)
.def_property_readonly("aminos_placed",
&Protein::get_aminos_placed)
.def_property_readonly("cur_len", &Protein::get_cur_len)
.def_property_readonly("dim", &Protein::get_dim)
.def_property_readonly("bond_values", &Protein::get_bond_values)
Expand Down
2 changes: 1 addition & 1 deletion prospr/core/src/depth_first_bnb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/* Returns true if the branch cannot produce a better score. */
bool prune_branch(Protein* protein, int max_length, int no_neighbors,
int move, int best_score) {
protein->place_amino(move);
protein->place_amino(move, false);

int cur_len = protein->get_cur_len();
int cur_score = protein->get_score();
Expand Down
18 changes: 15 additions & 3 deletions prospr/core/src/protein.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Protein::Protein(std::string sequence, int dim, std::string model,
last_pos.assign(dim, 0);
score = 0;
solutions_checked = 0;
aminos_placed = 0;

/* Deduct what model to use, or apply custom one. */
if (model == "HP") {
Expand Down Expand Up @@ -94,6 +95,7 @@ Protein::Protein(std::string sequence, int dim, std::string model,
if (sequence.size() != 0) {
space[last_pos] = amino_acids[0];
cur_len++;
aminos_placed++;
}
}

Expand Down Expand Up @@ -147,6 +149,11 @@ int Protein::get_solutions_checked() {
return solutions_checked;
}

/* Returns the number of amino acids placed. */
int Protein::get_aminos_placed() {
return aminos_placed;
}

/* Returns if the amino acid at the given index is weighted. */
bool Protein::is_weighted(int index) {
return weighted_amino_acids.find(sequence[index]) != std::string::npos;
Expand Down Expand Up @@ -176,6 +183,7 @@ void Protein::reset() {
last_move = 0;
score = 0;
solutions_checked = 0;
aminos_placed = 0;

space[last_pos] = amino_acids[0];
}
Expand Down Expand Up @@ -227,9 +235,13 @@ void Protein::place_amino(int move, bool track) {

cur_len++;

/* Update number of found solutions. */
if (track && cur_len == (int)sequence.size()) {
solutions_checked++;
/* Update number of found solutions and amino acids placed. */
if (track) {
aminos_placed++;

if (cur_len == (int)sequence.size()) {
solutions_checked++;
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions prospr/core/src/protein.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class Protein {
/* Returns the number of performed changes. */
int get_solutions_checked();

/* Returns the number of amino acids placed. */
int get_aminos_placed();

/* Returns if the amino acid at the given index is weighted. */
bool is_weighted(int index);

Expand Down Expand Up @@ -91,6 +94,7 @@ class Protein {
int last_move;
std::vector<int> last_pos;
int score;
int aminos_placed;
int solutions_checked;
std::vector<AminoAcid*> amino_acids;

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from setuptools import setup
from pybind11.setup_helpers import Pybind11Extension, build_ext

__version__ = "0.2a4"
__version__ = "0.2a5"

# Define core module extension.
ext_modules = [
Expand Down Expand Up @@ -43,7 +43,7 @@
packages=["prospr"],
package_data={"prospr": ["data/*/*.csv"]},
platforms=["any"],
python_requires=">=3.6",
python_requires=">=3.9",
zip_safe=False,
install_requires=[
"matplotlib",
Expand Down
4 changes: 4 additions & 0 deletions tests/core/test_depth_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ def test_protein_2d_depth_first(self, protein_2d):
"""
p = depth_first(protein_2d)
assert p.score == -3
assert p.solutions_checked == 1000
assert p.aminos_placed == 1574

def test_protein_3d_depth_first(self, protein_3d):
"""
Test if a 3D protein is folded correctly using depth_first search.
"""
p = depth_first(protein_3d)
assert p.score == -4
assert p.solutions_checked == 186455
assert p.aminos_placed == 235818
4 changes: 4 additions & 0 deletions tests/core/test_depth_first_bnb.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ def test_protein_2d_depth_first_bnb(self, protein_2d):
"""
p = depth_first_bnb(protein_2d)
assert p.score == -3
assert p.solutions_checked == 4
assert p.aminos_placed == 53

def test_protein_3d_depth_first_bnb(self, protein_3d):
"""
Test if a 3D protein is folded correctly using depth_first_bnb search.
"""
p = depth_first_bnb(protein_3d)
assert p.score == -4
assert p.solutions_checked == 5
assert p.aminos_placed == 49368
10 changes: 8 additions & 2 deletions tests/core/test_protein.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_protein_2d_generation(self, protein_2d):
assert protein_2d.last_pos == [0, 0]
assert protein_2d.score == 0
assert protein_2d.solutions_checked == 0
assert protein_2d.aminos_placed == 1

def test_protein_3d_generation(self, protein_3d):
"""Test if a 3D protein is generated correctly."""
Expand All @@ -44,32 +45,37 @@ def test_protein_3d_generation(self, protein_3d):
assert protein_3d.last_pos == [0, 0, 0]
assert protein_3d.score == 0
assert protein_3d.solutions_checked == 0
assert protein_3d.aminos_placed == 1

def test_protein_2d_place_moves(self, protein_2d):
"""Test if a 2D protein can move in all directions."""
assert protein_2d.cur_len == 1
moves = [1, 2, -1, -1, -2]
scores = [0, 0, -1, -1, -1]
track_placed = [1, 0, 1, 0, 1]

for i, m in enumerate(moves):
protein_2d.place_amino(m)
protein_2d.place_amino(m, track=bool(track_placed[i]))
assert protein_2d.hash_fold() == moves[: i + 1]
assert protein_2d.cur_len == len(moves[: i + 1]) + 1
assert protein_2d.last_move == m
assert protein_2d.score == scores[i]
assert protein_2d.aminos_placed == 1 + sum(track_placed[: i + 1])

def test_protein_3d_place_moves(self, protein_3d):
"""Test if a 3D protein can move in all directions."""
assert protein_3d.cur_len == 1
moves = [1, 2, -1, 3, -2, -1, -3]
scores = [0, 0, -1, -1, -1, -1, -2]
track_placed = [1, 0, 1, 0, 1, 0, 1]

for i, m in enumerate(moves):
protein_3d.place_amino(m)
protein_3d.place_amino(m, track=bool(track_placed[i]))
assert protein_3d.hash_fold() == moves[: i + 1]
assert protein_3d.cur_len == len(moves[: i + 1]) + 1
assert protein_3d.last_move == m
assert protein_3d.score == scores[i]
assert protein_3d.aminos_placed == 1 + sum(track_placed[: i + 1])

def test_protein_2d_undo_moves(self, protein_2d):
"""Test if a 2D protein can remove amino acids in all directions."""
Expand Down

0 comments on commit 8ad32ad

Please sign in to comment.