Skip to content

Commit

Permalink
improve type hinting throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniBodor committed Oct 10, 2023
1 parent bd0cd60 commit 27e3746
Show file tree
Hide file tree
Showing 29 changed files with 323 additions and 307 deletions.
158 changes: 79 additions & 79 deletions deeprank2/dataset.py

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions deeprank2/domain/aminoacidlist.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from deeprank2.molstruct.aminoacid import AminoAcid, Polarity

# All info below sourced from above websites in December 2022 and summarized in deeprank2/domain/aminoacid_summary.xlsx
Expand Down Expand Up @@ -352,7 +350,7 @@
# pyrrolysine,
]

def convert_aa_nomenclature(aa: str, output_type: Optional[int] = None):
def convert_aa_nomenclature(aa: str, output_type: int | None = None):
try:
if len(aa) == 1:
aa: AminoAcid = [entry for entry in amino_acids if entry.one_letter_code.lower() == aa.lower()][0]
Expand Down
8 changes: 4 additions & 4 deletions deeprank2/features/components.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from typing import Optional

import numpy as np

Expand All @@ -12,9 +11,10 @@
_log = logging.getLogger(__name__)

def add_features( # pylint: disable=unused-argument
pdb_path: str, graph: Graph,
single_amino_acid_variant: Optional[SingleResidueVariant] = None
):
pdb_path: str,
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None
):

for node in graph.nodes:
if isinstance(node.id, Residue):
Expand Down
8 changes: 4 additions & 4 deletions deeprank2/features/conservation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import Optional

import numpy as np

Expand All @@ -10,9 +9,10 @@


def add_features( # pylint: disable=unused-argument
pdb_path: str, graph: Graph,
single_amino_acid_variant: Optional[SingleResidueVariant] = None
):
pdb_path: str,
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None
):

profile_amino_acid_order = sorted(amino_acids, key=lambda aa: aa.three_letter_code)

Expand Down
22 changes: 11 additions & 11 deletions deeprank2/features/contact.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import logging
import warnings
from typing import List, Optional, Tuple

import numpy as np
import numpy.typing as npt
from numpy.typing import NDArray
from scipy.spatial import distance_matrix

from deeprank2.domain import edgestorage as Efeat
Expand All @@ -21,21 +20,21 @@
cutoff_14 = 4.2

def _get_nonbonded_energy( #pylint: disable=too-many-locals
atoms: List[Atom],
distances: npt.NDArray[np.float64],
) -> Tuple [npt.NDArray[np.float64], npt.NDArray[np.float64]]:
atoms: list[Atom],
distances: NDArray[np.float64],
) -> tuple [NDArray[np.float64], NDArray[np.float64]]:
"""Calculates all pairwise electrostatic (Coulomb) and Van der Waals (Lennard Jones) potential energies between all atoms in the structure.
Warning: there's no distance cutoff here. The radius of influence is assumed to infinite.
However, the potential tends to 0 at large distance.
Args:
atoms (List[Atom]): list of all atoms in the structure
distances (npt.NDArray[np.float64]): matrix of pairwise distances between all atoms in the structure
atoms (list[Atom]): list of all atoms in the structure
distances (NDArray[np.float64]): matrix of pairwise distances between all atoms in the structure
in the format that is the output of scipy.spatial's distance_matrix (i.e. a diagonally symmetric matrix)
Returns:
Tuple [npt.NDArray[np.float64], npt.NDArray[np.float64]]: matrices in same format as `distances` containing
Tuple [NDArray[np.float64], NDArray[np.float64]]: matrices in same format as `distances` containing
all pairwise electrostatic potential energies and all pairwise Van der Waals potential energies
"""

Expand Down Expand Up @@ -76,9 +75,10 @@ def _get_nonbonded_energy( #pylint: disable=too-many-locals


def add_features( # pylint: disable=unused-argument, too-many-locals
pdb_path: str, graph: Graph,
single_amino_acid_variant: Optional[SingleResidueVariant] = None
):
pdb_path: str,
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None
):

# assign each atoms (from all edges) a unique index
all_atoms = set()
Expand Down
8 changes: 4 additions & 4 deletions deeprank2/features/exposure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import signal
import sys
import warnings
from typing import Optional

import numpy as np
from Bio.PDB.Atom import PDBConstructionWarning
Expand Down Expand Up @@ -34,9 +33,10 @@ def space_if_none(value):


def add_features( # pylint: disable=unused-argument
pdb_path: str, graph: Graph,
single_amino_acid_variant: Optional[SingleResidueVariant] = None
):
pdb_path: str,
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None
):

signal.signal(signal.SIGINT, handle_sigint)
signal.signal(signal.SIGALRM, handle_timeout)
Expand Down
20 changes: 10 additions & 10 deletions deeprank2/features/irc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
from itertools import combinations_with_replacement as combinations
from typing import Dict, List, Optional, Tuple

import pdb2sql

Expand All @@ -14,7 +13,7 @@
_log = logging.getLogger(__name__)


def _id_from_residue(residue: Tuple[str, int, str]) -> str:
def _id_from_residue(residue: tuple[str, int, str]) -> str:
"""Create and id from pdb2sql rendered residues that is similar to the id of residue nodes
Args:
Expand All @@ -32,7 +31,7 @@ class _ContactDensity:
"""Internal class that holds contact density information for a given residue.
"""

def __init__(self, residue: Tuple[str, int, str], polarity: Polarity):
def __init__(self, residue: tuple[str, int, str], polarity: Polarity):
self.res = residue
self.polarity = polarity
self.id = _id_from_residue(self.res)
Expand All @@ -42,12 +41,12 @@ def __init__(self, residue: Tuple[str, int, str], polarity: Polarity):
self.connections['all'] = []


def get_IRCs(pdb_path: str, chains: List[str], cutoff: float = 5.5) -> Dict[str, _ContactDensity]:
def get_IRCs(pdb_path: str, chains: list[str], cutoff: float = 5.5) -> dict[str, _ContactDensity]:
"""Get all close contact residues from the opposite chain.
Args:
pdb_path (str): Path to pdb file to read molecular information from.
chains (Sequence[str]): List (or list-like object) containing strings of the chains to be considered.
chains (Sequence[str]): list (or list-like object) containing strings of the chains to be considered.
cutoff (float, optional): Cutoff distance (in Ångström) to be considered a close contact. Defaults to 10.
Returns:
Expand All @@ -56,7 +55,7 @@ def get_IRCs(pdb_path: str, chains: List[str], cutoff: float = 5.5) -> Dict[str,
items: _ContactDensity objects, containing all contact density information for the residue.
"""

residue_contacts: Dict[str, _ContactDensity] = {}
residue_contacts: dict[str, _ContactDensity] = {}

sql = pdb2sql.interface(pdb_path)
pdb2sql_contacts = sql.get_contact_residues(
Expand Down Expand Up @@ -104,11 +103,12 @@ def get_IRCs(pdb_path: str, chains: List[str], cutoff: float = 5.5) -> Dict[str,


def add_features(
pdb_path: str, graph: Graph,
single_amino_acid_variant: Optional[SingleResidueVariant] = None
):
pdb_path: str,
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None
):

if not single_amino_acid_variant: # VariantQueries do not use this feature
if not single_amino_acid_variant: # VariantQueries do not use this feature
polarity_pairs = list(combinations(Polarity, 2))
polarity_pair_string = [f'irc_{x[0].name.lower()}_{x[1].name.lower()}' for x in polarity_pairs]

Expand Down
9 changes: 4 additions & 5 deletions deeprank2/features/secondary_structure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from enum import Enum
from pathlib import Path
from typing import Dict, List, Optional

import numpy as np
from Bio.PDB import PDBParser
Expand Down Expand Up @@ -31,7 +30,7 @@ def onehot(self):
return t


def _get_records(lines: List[str]):
def _get_records(lines: list[str]):
seen = set()
seen_add = seen.add
return [x.split()[0] for x in lines if not (x in seen or seen_add(x))]
Expand Down Expand Up @@ -82,7 +81,7 @@ def _classify_secstructure(subtype: str):
return None


def _get_secstructure(pdb_path: str) -> Dict:
def _get_secstructure(pdb_path: str) -> dict:
"""Process the DSSP output to extract secondary structure information.
Args:
Expand Down Expand Up @@ -124,8 +123,8 @@ def _get_secstructure(pdb_path: str) -> Dict:
def add_features( # pylint: disable=unused-argument
pdb_path: str,
graph: Graph,
single_amino_acid_variant: Optional[SingleResidueVariant] = None
):
single_amino_acid_variant: SingleResidueVariant | None = None
):

sec_structure_features = _get_secstructure(pdb_path)

Expand Down
8 changes: 4 additions & 4 deletions deeprank2/features/surfacearea.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from typing import Optional

import freesasa
import numpy as np
Expand Down Expand Up @@ -105,9 +104,10 @@ def add_bsa(graph: Graph):


def add_features( # pylint: disable=unused-argument
pdb_path: str, graph: Graph,
single_amino_acid_variant: Optional[SingleResidueVariant] = None
):
pdb_path: str,
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None
):

"""calculates the Buried Surface Area (BSA) and the Solvent Accessible Surface Area (SASA):
BSA: the area of the protein, that only gets exposed in monomeric state"""
Expand Down
5 changes: 3 additions & 2 deletions deeprank2/molstruct/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from enum import Enum

import numpy as np
from numpy.typing import NDArray

from deeprank2.molstruct.residue import Residue

Expand Down Expand Up @@ -31,7 +32,7 @@ def __init__( # pylint: disable=too-many-arguments
residue: Residue,
name: str,
element: AtomicElement,
position: np.array,
position: NDArray,
occupancy: float,
):
"""
Expand Down Expand Up @@ -81,7 +82,7 @@ def occupancy(self) -> float:
return self._occupancy

@property
def position(self) -> np.array:
def position(self) -> NDArray:
return self._position

@property
Expand Down
9 changes: 5 additions & 4 deletions deeprank2/molstruct/residue.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING

import numpy as np
from numpy.typing import NDArray

from deeprank2.molstruct.aminoacid import AminoAcid
from deeprank2.molstruct.structure import Chain
Expand All @@ -25,8 +26,8 @@ def __init__(
self,
chain: Chain,
number: int,
amino_acid: Optional[AminoAcid] = None,
insertion_code: Optional[str] = None,
amino_acid: AminoAcid | None = None,
insertion_code: str | None = None,
):
"""
Args:
Expand Down Expand Up @@ -98,7 +99,7 @@ def __repr__(self) -> str:
def position(self) -> np.array:
return np.mean([atom.position for atom in self._atoms], axis=0)

def get_center(self) -> np.ndarray:
def get_center(self) -> NDArray:
"""Find the center position of a `Residue`.
Center position is found as follows:
Expand Down
10 changes: 5 additions & 5 deletions deeprank2/molstruct/structure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING

from deeprank2.utils.pssmdata import PssmRow

Expand All @@ -18,7 +18,7 @@ class PDBStructure:
particular `AminoAcid` type and in turn consists of a number of `Atom`s.
"""

def __init__(self, id_: Optional[str] = None):
def __init__(self, id_: str | None = None):
"""
Args:
id_ (str, optional): An unique identifier for this structure, can be the pdb accession code.
Expand Down Expand Up @@ -72,7 +72,7 @@ class Chain:
In other words: each `Chain` in a `PDBStructure` is a separate molecule.
"""

def __init__(self, model: PDBStructure, id_: Optional[str]):
def __init__(self, model: PDBStructure, id_: str | None):
"""One chain of a PDBStructure.
Args:
Expand All @@ -99,10 +99,10 @@ def pssm(self, pssm: PssmRow):
def add_residue(self, residue: Residue):
self._residues[(residue.number, residue.insertion_code)] = residue

def has_residue(self, residue_number: int, insertion_code: Optional[str] = None) -> bool:
def has_residue(self, residue_number: int, insertion_code: str | None = None) -> bool:
return (residue_number, insertion_code) in self._residues

def get_residue(self, residue_number: int, insertion_code: Optional[str] = None) -> Residue:
def get_residue(self, residue_number: int, insertion_code: str | None = None) -> Residue:
return self._residues[(residue_number, insertion_code)]

@property
Expand Down
6 changes: 2 additions & 4 deletions deeprank2/neuralnets/cnn/model3d.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Tuple

import torch
import torch.nn
import torch.nn.functional as F
Expand All @@ -25,7 +23,7 @@

class CnnRegression(torch.nn.Module):

def __init__(self, num_features: int, box_shape: Tuple[int]):
def __init__(self, num_features: int, box_shape: tuple[int]):
super().__init__()

self.convlayer_000 = torch.nn.Conv3d(num_features, 4, kernel_size=2)
Expand All @@ -38,7 +36,7 @@ def __init__(self, num_features: int, box_shape: Tuple[int]):
self.fclayer_000 = torch.nn.Linear(size, 84)
self.fclayer_001 = torch.nn.Linear(84, 1)

def _get_conv_output(self, num_features: int, shape: Tuple[int]):
def _get_conv_output(self, num_features: int, shape: tuple[int]):
num_data_points = 2
input_ = Variable(torch.rand(num_data_points, num_features, *shape))
output = self._forward_features(input_)
Expand Down
Loading

0 comments on commit 27e3746

Please sign in to comment.