Skip to content

Commit

Permalink
Use importlib-resources to load data in older Python versions
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Nov 25, 2023
1 parent 42a5762 commit bfaddea
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
10 changes: 7 additions & 3 deletions mini3di/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import functools
import struct
import typing
import importlib.resources

import numpy

from .utils import normalize
from ._unkerasify import KerasifyParser, Layer

try:
from importlib.resources import files as resource_files
except ImportError:
from importlib_resources import files as resource_files

if typing.TYPE_CHECKING:
from Bio.PDB import Chain
from .utils import ArrayN, ArrayNx2, ArrayNx3, ArrayNx10
Expand Down Expand Up @@ -109,8 +113,8 @@ def calc_conformation_descriptors(

class FeatureEncoder:
@classmethod
def load(cls):
path = importlib.resources.files(__package__).joinpath("encoder_weights_3di.kerasify")
def load(cls) -> FeatureEncoder:
path = resource_files(__package__).joinpath("encoder_weights_3di.kerasify")
with path.open("rb") as f:
parser = KerasifyParser(f)
layers = list(parser)
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ python_requires = >=3.7
test_suite = tests
include_package_data = true
setup_requires = setuptools >=46.4
install_requires =
importlib-resources >=1.3 ; python_version < '3.8'

[options.package_data]
mini3di = py.typed, *.kerasify
Expand Down
10 changes: 7 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import unittest
import importlib.resources

import Bio.PDB
from mini3di import Encoder

try:
from importlib.resources import files as resource_files
except ImportError:
from importlib_resources import files as resource_files


class TestEncoder(unittest.TestCase):
@classmethod
Expand All @@ -13,7 +17,7 @@ def setUpClass(cls):

@classmethod
def get_structure(cls, name):
path = importlib.resources.files(__package__).joinpath("data", name).with_suffix(".pdb")
path = resource_files(__package__).joinpath("data", name).with_suffix(".pdb")
return cls.parser.get_structure(name, path)

def test_encode_3bww(self):
Expand Down Expand Up @@ -58,4 +62,4 @@ def test_encode_8crb(self):
"DPCVLVVLVLQLVLVVLLLVVVVVVLVVCVVVLFKDWQDPVHDWQLACVSPDHDCPDCCSVPGSNN"
"VQQCPKPLDDVTATNQSVQQIDDGDLDHDDDDDTIQGCPPPVRCSVVVVVVSVVSVVVSVVSCVVS"
"VVVVVVD",
)
)
64 changes: 62 additions & 2 deletions tests/test_encoder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
import unittest

import numpy
import Bio.PDB
from mini3di import Encoder

import mini3di
try:
from importlib.resources import files as resource_files
except ImportError:
from importlib_resources import files as resource_files


class TestEncoder(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.encoder = Encoder()
cls.parser = Bio.PDB.PDBParser(QUIET=True)

@classmethod
def get_structure(cls, name):
path = resource_files(__package__).joinpath("data", name).with_suffix(".pdb")
return cls.parser.get_structure(name, path)

def test_encode_3bww(self):
structure = self.get_structure("3bww")
states = self.encoder.encode_chain(structure[0]["A"])
sequence = self.encoder.build_sequence(states)
self.assertEqual(
sequence,
"DKDFFEAAEDDLVCLVVLLPPPACPQRQAYEDALVVQVPDDPVSVVSVVNSLVHHAYAYEYEAQQL"
"LDDPQGDVVSLVSVLVCCVVSVPQEYEYENDPPDADALDVVSLVSSLVSQLVSCVSSVGAYAYEDA"
"ADQDHDPRHPDDVLVSRQSNCVSNVHAHAYELVRLVRCCVRPVPDDSLVSLVRHPLQRHQHYEYQV"
"VSVVSVLVNLVDHQAHHYYYHDYPDDVVVNSVVRVVSRVSNVVSCVVVVHYIDMD",
)

def test_encode_8crb(self):
structure = self.get_structure("8crb")

states = self.encoder.encode_chain(structure[0]["A"])
sequence = self.encoder.build_sequence(states)
self.assertEqual(
sequence,
"DWAKDKDWADEDAAQAKTKIKMATPPDLLQDFFKFKWFDAPPDDIDGQAPGACPSPPLADDVHHHH"
"GKGWHDDSVRRMIMIMGGNDDQVVFGKMKMFTADDADPQVVVPDGDDTDDMHDIDTYGHPPDDFFA"
"WDKDKDQDDPVPCPVQKPKIKMKTDDGDDDDKDKAWLVNPGDPQKDDFDWDADPVRGIIDMIIGMD"
"GNVCFQVGFTKIWMAGVVVRDIDIDGGHD",
)

states = self.encoder.encode_chain(structure[0]["B"])
sequence = self.encoder.build_sequence(states)
self.assertEqual(
sequence,
"DAAKDFDQQEEEAAQAKDKGWIFAADVPPVPDAFWKWWDAPPDDIDTAADPNQAGDPVDHSQKGWD"
"ADHGITIIMGGRDDNSRQGFIWRAQPDDPDHNGHTDDTHGYYHCPDDQDDKDKDWDDAAVVVLVVL"
"FGKTKIKIDDGDDPPKDKFKDLQNHTDDAQWDWDDWDLDPVRTIMTMIIRRDGVVSCVVSQKMKMW"
"IDDDVHTDIDMDGNVVHD",
)

states = self.encoder.encode_chain(structure[0]["C"])
sequence = self.encoder.build_sequence(states)
self.assertEqual(
sequence,
"DPCVLVVLVLQLVLVVLLLVVVVVVLVVCVVVLFKDWQDPVHDWQLACVSPDHDCPDCCSVPGSNN"
"VQQCPKPLDDVTATNQSVQQIDDGDLDHDDDDDTIQGCPPPVRCSVVVVVVSVVSVVVSVVSCVVS"
"VVVVVVD",
)

0 comments on commit bfaddea

Please sign in to comment.