Skip to content

Commit

Permalink
fix: Update method name - pep8 compliance #371
Browse files Browse the repository at this point in the history
  • Loading branch information
CS76 committed Sep 6, 2023
1 parent 34d31f1 commit 5d6ffc5
Show file tree
Hide file tree
Showing 21 changed files with 287 additions and 285 deletions.
6 changes: 4 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from fastapi.responses import RedirectResponse
from fastapi_versioning import VersionedFastAPI

from .routers import tools, depict, converters, chem, ocsr
from .routers import tools, depict, converters, chem

# , ocsr
from fastapi.middleware.cors import CORSMiddleware

from prometheus_fastapi_instrumentator import Instrumentator
Expand All @@ -30,7 +32,7 @@
app.include_router(converters.router)
app.include_router(depict.router)
app.include_router(tools.router)
app.include_router(ocsr.router)
# app.include_router(ocsr.router)

app = VersionedFastAPI(
app,
Expand Down
50 changes: 25 additions & 25 deletions app/modules/all_descriptors.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from rdkit.Chem import Descriptors, QED, Lipinski, rdMolDescriptors, rdmolops
from typing import Union
from app.modules.toolkits.rdkit_wrapper import (
checkRo5Violations,
getTanimotoSimilarityRDKit,
check_RO5_violations,
get_tanimoto_similarity_rdkit,
)
from app.modules.toolkits.cdk_wrapper import (
getCDKSDG,
get_CDK_SDG,
JClass,
cdk_base,
getAromaticRingCount,
getTanimotoSimilarityCDK,
getVanderWaalsVolume,
get_aromatic_ring_count,
get_tanimoto_similarity_CDK,
get_vander_waals_volume,
)
from app.modules.toolkits.helpers import parseInput
from app.modules.toolkits.helpers import parse_input


def getAllRDKitDescriptors(molecule: any) -> Union[tuple, str]:
def get_all_rdkit_descriptors(molecule: any) -> Union[tuple, str]:
"""
Calculate a selected set of molecular descriptors using RDKit.
This function takes an input SMILES string and calculates various molecular descriptors
Expand All @@ -41,7 +41,7 @@ def getAllRDKitDescriptors(molecule: any) -> Union[tuple, str]:
HBD = Descriptors.NumHDonors(molecule)
Lipinski_HBA = Lipinski.NumHAcceptors(molecule)
Lipinski_HBD = Lipinski.NumHDonors(molecule)
Ro5Violations = checkRo5Violations(molecule)
Ro5Violations = check_RO5_violations(molecule)
AromaticRings = rdMolDescriptors.CalcNumAromaticRings(molecule)
QEDWeighted = "%.2f" % QED.qed(molecule)
FormalCharge = rdmolops.GetFormalCharge(molecule)
Expand Down Expand Up @@ -73,7 +73,7 @@ def getAllRDKitDescriptors(molecule: any) -> Union[tuple, str]:
return "Error reading SMILES string, check again."


def getAllCDKDescriptors(molecule: any) -> Union[tuple, str]:
def get_all_cdk_descriptors(molecule: any) -> Union[tuple, str]:
"""
Calculate a set of molecular descriptors using the CDK.
This function takes a SMILES string as input and calculates various molecular descriptors
Expand All @@ -85,7 +85,7 @@ def getAllCDKDescriptors(molecule: any) -> Union[tuple, str]:
Returns:
tuple: A tuple containing calculated molecular descriptors. If an error occurs during processing, an error message is returned.
"""
SDGMol = getCDKSDG(molecule)
SDGMol = get_CDK_SDG(molecule)
if SDGMol:
AtomCountDescriptor = (
JClass(cdk_base + ".qsar.descriptors.molecular.AtomCountDescriptor")()
Expand Down Expand Up @@ -142,7 +142,7 @@ def getAllCDKDescriptors(molecule: any) -> Union[tuple, str]:
.calculate(SDGMol)
.getValue()
)
AromaticRings = getAromaticRingCount(SDGMol)
AromaticRings = get_aromatic_ring_count(SDGMol)
QEDWeighted = None
FormalCharge = JClass(
cdk_base + ".tools.manipulator.AtomContainerManipulator"
Expand All @@ -154,7 +154,7 @@ def getAllCDKDescriptors(molecule: any) -> Union[tuple, str]:
.toString()
)
NumRings = JClass(cdk_base + ".graph.Cycles").mcb(SDGMol).numberOfCycles()
VABCVolume = getVanderWaalsVolume(SDGMol)
VABCVolume = get_vander_waals_volume(SDGMol)

return (
int(str(AtomCountDescriptor)),
Expand All @@ -181,7 +181,7 @@ def getAllCDKDescriptors(molecule: any) -> Union[tuple, str]:
return "Error reading SMILES string, check again."


def getCDKRDKitcombinedDescriptors(
def get_cdk_rdkit_combined_descriptors(
smiles: str,
) -> Union[dict, str]:
"""
Expand All @@ -197,10 +197,10 @@ def getCDKRDKitcombinedDescriptors(
- If unsuccessful due to descriptor calculation errors, returns an error message as a string.
"""
# Calculate RDKit and CDK descriptors
rdkitMol = parseInput(smiles, "rdkit", False)
rdkit_descriptors = getAllRDKitDescriptors(rdkitMol)
cdkMol = parseInput(smiles, "cdk", False)
cdk_descriptors = getAllCDKDescriptors(cdkMol)
rdkitMol = parse_input(smiles, "rdkit", False)
rdkit_descriptors = get_all_rdkit_descriptors(rdkitMol)
cdkMol = parse_input(smiles, "cdk", False)
cdk_descriptors = get_all_cdk_descriptors(cdkMol)

# List of descriptors to calculate
all_descriptors = (
Expand Down Expand Up @@ -267,7 +267,7 @@ def get_table(tanimoto_values: list) -> str:
return table_html


def getTanimotoSimilarity(smileslist: str, toolkit: str = "cdk") -> list:
def get_tanimoto_similarity(smileslist: str, toolkit: str = "cdk") -> list:
"""
Calculate the Tanimoto similarity index between pairs of SMILES strings.
Expand Down Expand Up @@ -298,13 +298,13 @@ def getTanimotoSimilarity(smileslist: str, toolkit: str = "cdk") -> list:
for i in range(len(smiles_list)):
for j in range(len(smiles_list)):
if toolkit == "rdkit":
mol1 = parseInput(smiles_list[i], "rdkit", False)
mol2 = parseInput(smiles_list[j], "rdkit", False)
matrix[i][j] = getTanimotoSimilarityRDKit(mol1, mol2)
mol1 = parse_input(smiles_list[i], "rdkit", False)
mol2 = parse_input(smiles_list[j], "rdkit", False)
matrix[i][j] = get_tanimoto_similarity_rdkit(mol1, mol2)
elif toolkit == "cdk":
mol1 = parseInput(smiles_list[i], "cdk", False)
mol2 = parseInput(smiles_list[j], "cdk", False)
matrix[i][j] = getTanimotoSimilarityCDK(mol1, mol2)
mol1 = parse_input(smiles_list[i], "cdk", False)
mol2 = parse_input(smiles_list[j], "cdk", False)
matrix[i][j] = get_tanimoto_similarity_CDK(mol1, mol2)
else:
raise ValueError("Unsupported toolkit:", toolkit)

Expand Down
38 changes: 19 additions & 19 deletions app/modules/coconut/descriptors.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from app.modules.toolkits.rdkit_wrapper import getRDKitDescriptors
from app.modules.toolkits.rdkit_wrapper import get_rdkit_descriptors
from typing import Dict, Union
from app.modules.toolkits.cdk_wrapper import (
getMurkoFramework,
getCDKDescriptors,
get_murko_framework,
get_CDK_descriptors,
)
from app.modules.all_descriptors import getCDKRDKitcombinedDescriptors
from app.modules.npscorer import getNPScore
from app.modules.tools.sugar_removal import getSugarInfo
from app.modules.toolkits.helpers import parseInput
from app.modules.all_descriptors import get_cdk_rdkit_combined_descriptors
from app.modules.npscorer import get_np_score
from app.modules.tools.sugar_removal import get_sugar_info
from app.modules.toolkits.helpers import parse_input


def getDescriptors(smiles: str, toolkit: str) -> Union[tuple, str]:
def get_descriptors(smiles: str, toolkit: str) -> Union[tuple, str]:
"""
Calculate descriptors using RDKit or CDK toolkit for the given SMILES.
Expand All @@ -23,17 +23,17 @@ def getDescriptors(smiles: str, toolkit: str) -> Union[tuple, str]:
or an error message if the toolkit choice is invalid or SMILES is invalid.
"""

mol = parseInput(smiles, toolkit, False)
mol = parse_input(smiles, toolkit, False)
if mol:
if toolkit == "rdkit":
Descriptors = getRDKitDescriptors(mol)
Descriptors = get_rdkit_descriptors(mol)
return Descriptors
elif toolkit == "cdk":
Descriptors = getCDKDescriptors(mol)
Descriptors = get_CDK_descriptors(mol)
return Descriptors


def getCOCONUTDescriptors(smiles: str, toolkit: str) -> Union[Dict[str, float], str]:
def get_COCONUT_descriptors(smiles: str, toolkit: str) -> Union[Dict[str, float], str]:
"""
Calculate COCONUT descriptors using RDKit or CDK toolkit for the given SMILES.
Expand All @@ -46,17 +46,17 @@ def getCOCONUTDescriptors(smiles: str, toolkit: str) -> Union[Dict[str, float],
or an error message if the toolkit choice is invalid or SMILES is invalid.
"""
if toolkit == "all":
AllDescriptors = getCDKRDKitcombinedDescriptors(smiles)
AllDescriptors = get_cdk_rdkit_combined_descriptors(smiles)
return AllDescriptors
else:
Descriptors = getDescriptors(smiles, toolkit)
Descriptors = get_descriptors(smiles, toolkit)

rdkitMolecule = parseInput(smiles, "rdkit", False)
nplikeliness = float(getNPScore(rdkitMolecule))
rdkitMolecule = parse_input(smiles, "rdkit", False)
nplikeliness = float(get_np_score(rdkitMolecule))

cdkMolecule = parseInput(smiles, "cdk", False)
hasLinearSugar, hasCircularSugars = getSugarInfo(cdkMolecule)
framework = getMurkoFramework(cdkMolecule)
cdkMolecule = parse_input(smiles, "cdk", False)
hasLinearSugar, hasCircularSugars = get_sugar_info(cdkMolecule)
framework = get_murko_framework(cdkMolecule)

CombinedDescriptors = list(Descriptors)
CombinedDescriptors.extend(
Expand Down
54 changes: 27 additions & 27 deletions app/modules/coconut/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import app.modules.toolkits.rdkit_wrapper as rdkitmodules
from chembl_structure_pipeline import standardizer
import app.modules.toolkits.cdk_wrapper as cdk
from app.modules.coconut.descriptors import getCOCONUTDescriptors
from app.modules.toolkits.helpers import parseInput
from app.modules.coconut.descriptors import get_COCONUT_descriptors
from app.modules.toolkits.helpers import parse_input


def getMolBlock(input_text: str) -> str:
def get_mol_block(input_text: str) -> str:
"""
Generate a Molblock from input text using CDK.
Expand All @@ -23,16 +23,16 @@ def getMolBlock(input_text: str) -> str:
check = rdkitmodules.is_valid_molecule(input_text)

if check == "smiles":
molecule = parseInput(input_text, "cdk", False)
mol_block = cdk.getCDKSDGMol(molecule, V3000=False).replace("$$$$\n", "")
molecule = parse_input(input_text, "cdk", False)
mol_block = cdk.get_CDK_SDG_mol(molecule, V3000=False).replace("$$$$\n", "")
return mol_block
elif check == "mol":
return input_text
else:
return "Error!, Check the input text."


def getMoleculeHash(molecule: any) -> dict:
def get_molecule_hash(molecule: any) -> dict:
"""
Return various molecule hashes for the provided SMILES.
Expand All @@ -58,7 +58,7 @@ def getMoleculeHash(molecule: any) -> dict:
return {"Error": "Check input SMILES"}


def getRepresentations(molecule: any) -> dict:
def get_representations(molecule: any) -> dict:
"""
Return COCONUT representations for the provided SMILES.
Expand All @@ -72,14 +72,14 @@ def getRepresentations(molecule: any) -> dict:
if molecule:
InChI = Chem.inchi.MolToInchi(molecule)
InChI_Key = Chem.inchi.MolToInchiKey(molecule)
cdkMolecule = parseInput(Chem.MolToSmiles(molecule), "cdk", False)
Murko = cdk.getMurkoFramework(cdkMolecule)
cdkMolecule = parse_input(Chem.MolToSmiles(molecule), "cdk", False)
Murko = cdk.get_murko_framework(cdkMolecule)
return {"InChI": InChI, "InChI_Key": InChI_Key, "Murko": Murko}
else:
return {"Error": "Check input SMILES"}


def getCOCONUTpreprocessing(input_text: str) -> dict:
def get_COCONUT_preprocessing(input_text: str) -> dict:
"""
Preprocess user input text suitable for the COCONUT database submission data.
Expand All @@ -90,42 +90,42 @@ def getCOCONUTpreprocessing(input_text: str) -> dict:
dict: COCONUT preprocessed data.
"""
original_mol = getMolBlock(input_text)
original_mol = get_mol_block(input_text)
standarised_mol_block = standardizer.standardize_molblock(original_mol)
standardised_SMILES = Chem.MolToSmiles(
Chem.MolFromMolBlock(standarised_mol_block), kekuleSmiles=True
)

rdkitMol = parseInput(standardised_SMILES, "rdkit", False)
molecule_hash = getMoleculeHash(rdkitMol)
rdkitMol = parse_input(standardised_SMILES, "rdkit", False)
molecule_hash = get_molecule_hash(rdkitMol)

parent_canonical_smiles = molecule_hash["Canonical_SMILES"]
cdkParentMol = parseInput(parent_canonical_smiles, "cdk", False)
parent_2D_molblock = cdk.getCDKSDGMol(cdkParentMol, V3000=False).replace(
cdkParentMol = parse_input(parent_canonical_smiles, "cdk", False)
parent_2D_molblock = cdk.get_CDK_SDG_mol(cdkParentMol, V3000=False).replace(
"$$$$\n", ""
)
parent_2D_molblock_v3 = cdk.getCDKSDGMol(cdkParentMol, V3000=True).replace(
parent_2D_molblock_v3 = cdk.get_CDK_SDG_mol(cdkParentMol, V3000=True).replace(
"$$$$\n", ""
)
rdkitParentMol = parseInput(parent_canonical_smiles, "rdkit", False)
parent_3D_molblock = rdkitmodules.get3Dconformers(rdkitParentMol)
rdkitParentMol = parse_input(parent_canonical_smiles, "rdkit", False)
parent_3D_molblock = rdkitmodules.get_3d_conformers(rdkitParentMol)

parent_representations = getRepresentations(rdkitParentMol)
parent_descriptors = getCOCONUTDescriptors(parent_canonical_smiles, "rdkit")
parent_representations = get_representations(rdkitParentMol)
parent_descriptors = get_COCONUT_descriptors(parent_canonical_smiles, "rdkit")

if rdkitmodules.has_stereochemistry(rdkitMol):
variant_isomeric_smiles = molecule_hash["Isomeric_SMILES"]
cdkVariantMol = parseInput(variant_isomeric_smiles, "cdk", False)
variant_2D_molblock = cdk.getCDKSDGMol(cdkVariantMol, V3000=False).replace(
cdkVariantMol = parse_input(variant_isomeric_smiles, "cdk", False)
variant_2D_molblock = cdk.get_CDK_SDG_mol(cdkVariantMol, V3000=False).replace(
"$$$$\n", ""
)
variant_2D_molblock_v3 = cdk.getCDKSDGMol(cdkVariantMol, V3000=True).replace(
variant_2D_molblock_v3 = cdk.get_CDK_SDG_mol(cdkVariantMol, V3000=True).replace(
"$$$$\n", ""
)
rdkitVariantMol = parseInput(standardised_SMILES, "rdkit", False)
variant_3D_molblock = rdkitmodules.get3Dconformers(rdkitVariantMol)
variant_representations = getRepresentations(rdkitVariantMol)
variant_descriptors = getCOCONUTDescriptors(variant_isomeric_smiles, "rdkit")
rdkitVariantMol = parse_input(standardised_SMILES, "rdkit", False)
variant_3D_molblock = rdkitmodules.get_3d_conformers(rdkitVariantMol)
variant_representations = get_representations(rdkitVariantMol)
variant_descriptors = get_COCONUT_descriptors(variant_isomeric_smiles, "rdkit")

return {
"original_mol": original_mol,
Expand Down
6 changes: 3 additions & 3 deletions app/modules/decimer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_segments(path: str) -> tuple:
return image_name, segments


def getPredictedSegments(path: str) -> str:
def get_predicted_segments(path: str) -> str:
"""
Get predicted SMILES representations for segments within an image.
Expand Down Expand Up @@ -102,7 +102,7 @@ def getPredictedSegments(path: str) -> str:
return ".".join(smiles_predicted)


def getPredictedSegmentsFromFile(content: any, filename: str) -> tuple:
def get_predicted_segments_from_file(content: any, filename: str) -> tuple:
"""
Takes an image file path and returns a set of paths and image names of segmented images.
Expand All @@ -116,6 +116,6 @@ def getPredictedSegmentsFromFile(content: any, filename: str) -> tuple:

with open(filename, "wb") as f:
f.write(content)
smiles = getPredictedSegments(filename)
smiles = get_predicted_segments(filename)
os.remove(filename)
return smiles
Loading

0 comments on commit 5d6ffc5

Please sign in to comment.