forked from enviPath/enviPy
Compare commits
10 Commits
44698602c1
...
aaa87c5f2f
| Author | SHA1 | Date | |
|---|---|---|---|
| aaa87c5f2f | |||
| 3241bc2648 | |||
| d5d7779d8e | |||
| 91d27025b9 | |||
| a17751be1f | |||
| 701bb3dd5f | |||
| 7632b3a029 | |||
| d657c0285a | |||
| f4c198981b | |||
| cdd51fc7aa |
@ -357,6 +357,7 @@ DEFAULT_MODEL_PARAMS = {
|
|||||||
DEFAULT_MAX_NUMBER_OF_NODES = 9999
|
DEFAULT_MAX_NUMBER_OF_NODES = 9999
|
||||||
DEFAULT_MAX_DEPTH = 8
|
DEFAULT_MAX_DEPTH = 8
|
||||||
DEFAULT_MODEL_THRESHOLD = 0.25
|
DEFAULT_MODEL_THRESHOLD = 0.25
|
||||||
|
BATCH_PREDICT_MAX_COMPOUNDS = 150
|
||||||
|
|
||||||
# Loading Plugins
|
# Loading Plugins
|
||||||
PLUGINS_ENABLED = os.environ.get("PLUGINS_ENABLED", "False") == "True"
|
PLUGINS_ENABLED = os.environ.get("PLUGINS_ENABLED", "False") == "True"
|
||||||
|
|||||||
@ -46,7 +46,7 @@ class EPDBURLParser:
|
|||||||
MODEL_PATTERNS = {
|
MODEL_PATTERNS = {
|
||||||
"epdb.User": re.compile(rf"^.*/user/{UUID_PATTERN}"),
|
"epdb.User": re.compile(rf"^.*/user/{UUID_PATTERN}"),
|
||||||
"epdb.Group": re.compile(rf"^.*/group/{UUID_PATTERN}"),
|
"epdb.Group": re.compile(rf"^.*/group/{UUID_PATTERN}"),
|
||||||
"epdb.Package": re.compile(rf"^.*/package/{UUID_PATTERN}"),
|
s.EPDB_PACKAGE_MODEL: re.compile(rf"^.*/package/{UUID_PATTERN}"),
|
||||||
"epdb.Compound": re.compile(rf"^.*/package/{UUID_PATTERN}/compound/{UUID_PATTERN}"),
|
"epdb.Compound": re.compile(rf"^.*/package/{UUID_PATTERN}/compound/{UUID_PATTERN}"),
|
||||||
"epdb.CompoundStructure": re.compile(
|
"epdb.CompoundStructure": re.compile(
|
||||||
rf"^.*/package/{UUID_PATTERN}/compound/{UUID_PATTERN}/structure/{UUID_PATTERN}"
|
rf"^.*/package/{UUID_PATTERN}/compound/{UUID_PATTERN}/structure/{UUID_PATTERN}"
|
||||||
@ -96,7 +96,7 @@ class EPDBURLParser:
|
|||||||
|
|
||||||
def contains_package_url(self):
|
def contains_package_url(self):
|
||||||
return (
|
return (
|
||||||
bool(self.MODEL_PATTERNS["epdb.Package"].findall(self.url))
|
bool(self.MODEL_PATTERNS[s.EPDB_PACKAGE_MODEL].findall(self.url))
|
||||||
and not self.is_package_url()
|
and not self.is_package_url()
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ class EPDBURLParser:
|
|||||||
"epdb.EPModel",
|
"epdb.EPModel",
|
||||||
"epdb.Pathway",
|
"epdb.Pathway",
|
||||||
# 1st level
|
# 1st level
|
||||||
"epdb.Package",
|
s.EPDB_PACKAGE_MODEL,
|
||||||
"epdb.Setting",
|
"epdb.Setting",
|
||||||
"epdb.Group",
|
"epdb.Group",
|
||||||
"epdb.User",
|
"epdb.User",
|
||||||
@ -146,7 +146,7 @@ class EPDBURLParser:
|
|||||||
|
|
||||||
hierarchy_order = [
|
hierarchy_order = [
|
||||||
# 1st level
|
# 1st level
|
||||||
"epdb.Package",
|
s.EPDB_PACKAGE_MODEL,
|
||||||
"epdb.Setting",
|
"epdb.Setting",
|
||||||
"epdb.Group",
|
"epdb.Group",
|
||||||
"epdb.User",
|
"epdb.User",
|
||||||
@ -1896,12 +1896,51 @@ class SPathway(object):
|
|||||||
|
|
||||||
logger.info("Update done!")
|
logger.info("Update done!")
|
||||||
|
|
||||||
|
def compute_bayes_probabilities(self) -> Dict[SEdge, float]:
|
||||||
|
"""
|
||||||
|
Computes Bayes-adjusted probabilities for all edges in the pathway
|
||||||
|
by iterating level by level from depth 0 upwards, keyed on educt depth.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A dict mapping each SEdge to its Bayes-adjusted probability.
|
||||||
|
"""
|
||||||
|
bayes_probs: Dict[SEdge, float] = {}
|
||||||
|
|
||||||
|
# Group edges by their educt depth
|
||||||
|
edges_by_depth: Dict[int, List[SEdge]] = {}
|
||||||
|
for edge in self.edges:
|
||||||
|
d = edge.educts[0].depth
|
||||||
|
edges_by_depth.setdefault(d, []).append(edge)
|
||||||
|
|
||||||
|
for depth in sorted(edges_by_depth.keys()):
|
||||||
|
for edge in edges_by_depth[depth]:
|
||||||
|
if depth == 0:
|
||||||
|
bayes_probs[edge] = edge.probability
|
||||||
|
else:
|
||||||
|
predecessor_edges = [e for e in self.edges if edge.educts[0] in e.products]
|
||||||
|
|
||||||
|
if not predecessor_edges or not all(
|
||||||
|
e in bayes_probs for e in predecessor_edges
|
||||||
|
):
|
||||||
|
# Predecessor not computed yet (e.g. same-depth product),
|
||||||
|
# fall back to raw probability
|
||||||
|
bayes_probs[edge] = edge.probability
|
||||||
|
else:
|
||||||
|
predecessor_avg = sum(bayes_probs[e] for e in predecessor_edges) / len(
|
||||||
|
predecessor_edges
|
||||||
|
)
|
||||||
|
bayes_probs[edge] = predecessor_avg * edge.probability
|
||||||
|
|
||||||
|
return bayes_probs
|
||||||
|
|
||||||
def to_json(self):
|
def to_json(self):
|
||||||
nodes = []
|
nodes = []
|
||||||
edges = []
|
edges = []
|
||||||
|
|
||||||
idx_lookup = {}
|
idx_lookup = {}
|
||||||
|
|
||||||
|
bayes_probs = self.compute_bayes_probabilities()
|
||||||
|
|
||||||
for i, smiles in enumerate(self.smiles_to_node):
|
for i, smiles in enumerate(self.smiles_to_node):
|
||||||
n = self.smiles_to_node[smiles]
|
n = self.smiles_to_node[smiles]
|
||||||
idx_lookup[smiles] = i
|
idx_lookup[smiles] = i
|
||||||
@ -1922,6 +1961,7 @@ class SPathway(object):
|
|||||||
|
|
||||||
if edge.probability:
|
if edge.probability:
|
||||||
e["probability"] = edge.probability
|
e["probability"] = edge.probability
|
||||||
|
e["multiGenProbability"] = bayes_probs[edge]
|
||||||
|
|
||||||
edges.append(e)
|
edges.append(e)
|
||||||
|
|
||||||
|
|||||||
@ -3014,7 +3014,14 @@ class PackageBasedModel(EPModel):
|
|||||||
|
|
||||||
prec, rec = dict(), dict()
|
prec, rec = dict(), dict()
|
||||||
|
|
||||||
for t in np.arange(0, 1.05, 0.05):
|
thresholds = list(np.arange(0, 1.05, 0.05))
|
||||||
|
|
||||||
|
# Add specific threshold set during object creation if not already present
|
||||||
|
if np.float64(threshold) not in thresholds:
|
||||||
|
thresholds.append(np.float64(threshold))
|
||||||
|
thresholds.sort()
|
||||||
|
|
||||||
|
for t in thresholds:
|
||||||
temp_thresholded = (y_pred_filtered >= t).astype(int)
|
temp_thresholded = (y_pred_filtered >= t).astype(int)
|
||||||
prec[f"{t:.2f}"] = precision_score(
|
prec[f"{t:.2f}"] = precision_score(
|
||||||
y_test_filtered, temp_thresholded, zero_division=0
|
y_test_filtered, temp_thresholded, zero_division=0
|
||||||
@ -3024,7 +3031,14 @@ class PackageBasedModel(EPModel):
|
|||||||
return acc, prec, rec
|
return acc, prec, rec
|
||||||
|
|
||||||
def evaluate_mg(model, pathways: Union[QuerySet["Pathway"] | List["Pathway"]], threshold):
|
def evaluate_mg(model, pathways: Union[QuerySet["Pathway"] | List["Pathway"]], threshold):
|
||||||
thresholds = np.arange(0.1, 1.1, 0.1)
|
thresholds = list(np.arange(0, 1.05, 0.05))
|
||||||
|
|
||||||
|
# Add specific threshold set during object creation if not already present
|
||||||
|
if np.float64(threshold) not in thresholds:
|
||||||
|
thresholds.append(np.float64(threshold))
|
||||||
|
thresholds.sort()
|
||||||
|
|
||||||
|
logger.info(f"Thresholds: {thresholds}")
|
||||||
|
|
||||||
precision = {f"{t:.2f}": [] for t in thresholds}
|
precision = {f"{t:.2f}": [] for t in thresholds}
|
||||||
recall = {f"{t:.2f}": [] for t in thresholds}
|
recall = {f"{t:.2f}": [] for t in thresholds}
|
||||||
@ -3050,7 +3064,7 @@ class PackageBasedModel(EPModel):
|
|||||||
|
|
||||||
s = Setting()
|
s = Setting()
|
||||||
s.model = mod
|
s.model = mod
|
||||||
s.model_threshold = thresholds.min()
|
s.model_threshold = 0.0
|
||||||
s.max_depth = 10
|
s.max_depth = 10
|
||||||
s.max_nodes = 50
|
s.max_nodes = 50
|
||||||
|
|
||||||
|
|||||||
@ -540,6 +540,7 @@ def batch_predict_pathway(request):
|
|||||||
context = get_base_context(request)
|
context = get_base_context(request)
|
||||||
context["title"] = "enviPath - Batch Predict Pathway"
|
context["title"] = "enviPath - Batch Predict Pathway"
|
||||||
context["meta"]["current_package"] = context["meta"]["user"].default_package
|
context["meta"]["current_package"] = context["meta"]["user"].default_package
|
||||||
|
context["batch_predict_max_compounds"] = s.BATCH_PREDICT_MAX_COMPOUNDS
|
||||||
|
|
||||||
return render(request, "batch_predict_pathway.html", context)
|
return render(request, "batch_predict_pathway.html", context)
|
||||||
|
|
||||||
@ -1964,6 +1965,21 @@ def package_reactions(request, package_uuid):
|
|||||||
reaction_name = request.POST.get("reaction-name")
|
reaction_name = request.POST.get("reaction-name")
|
||||||
reaction_description = request.POST.get("reaction-description")
|
reaction_description = request.POST.get("reaction-description")
|
||||||
reaction_smiles = request.POST.get("reaction-smiles")
|
reaction_smiles = request.POST.get("reaction-smiles")
|
||||||
|
|
||||||
|
if reaction_smiles is None or reaction_smiles.strip() == "":
|
||||||
|
return error(
|
||||||
|
request,
|
||||||
|
"Reaction SMILES is empty / missing",
|
||||||
|
"No reaction SMILES provided. Please provide a SMILES for the reaction.",
|
||||||
|
)
|
||||||
|
|
||||||
|
if not FormatConverter.is_valid_smirks(reaction_smiles):
|
||||||
|
return error(
|
||||||
|
request,
|
||||||
|
"Reaction SMILES is invalid",
|
||||||
|
f"The provided reactions SMILES {reaction_smiles} is invalid",
|
||||||
|
)
|
||||||
|
|
||||||
educts = reaction_smiles.split(">>")[0].split(".")
|
educts = reaction_smiles.split(">>")[0].split(".")
|
||||||
products = reaction_smiles.split(">>")[1].split(".")
|
products = reaction_smiles.split(">>")[1].split(".")
|
||||||
|
|
||||||
|
|||||||
@ -120,13 +120,6 @@ class PathwayMapper:
|
|||||||
)
|
)
|
||||||
bundle.reference_substances.append(ref_sub)
|
bundle.reference_substances.append(ref_sub)
|
||||||
|
|
||||||
sub = IUCLIDSubstanceData(
|
|
||||||
uuid=sub_uuid,
|
|
||||||
name=compound.name,
|
|
||||||
reference_substance_uuid=ref_sub_uuid,
|
|
||||||
)
|
|
||||||
bundle.substances.append(sub)
|
|
||||||
|
|
||||||
if not export.compounds:
|
if not export.compounds:
|
||||||
return bundle
|
return bundle
|
||||||
|
|
||||||
@ -145,6 +138,16 @@ class PathwayMapper:
|
|||||||
if not root_compound_pks:
|
if not root_compound_pks:
|
||||||
return bundle
|
return bundle
|
||||||
|
|
||||||
|
for root_pk in root_compound_pks:
|
||||||
|
root_sub_uuid, root_ref_uuid = seen_compounds[root_pk]
|
||||||
|
bundle.substances.append(
|
||||||
|
IUCLIDSubstanceData(
|
||||||
|
uuid=root_sub_uuid,
|
||||||
|
name=compound_names[root_pk],
|
||||||
|
reference_substance_uuid=root_ref_uuid,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
edge_templates: list[tuple[UUID, frozenset[int], tuple[int, ...], tuple[UUID, ...]]] = []
|
edge_templates: list[tuple[UUID, frozenset[int], tuple[int, ...], tuple[UUID, ...]]] = []
|
||||||
for edge in sorted(export.edges, key=lambda item: str(item.edge_uuid)):
|
for edge in sorted(export.edges, key=lambda item: str(item.edge_uuid)):
|
||||||
parent_compound_pks = sorted(
|
parent_compound_pks = sorted(
|
||||||
|
|||||||
@ -70,8 +70,7 @@ class IUCLIDExportAPITest(TestCase):
|
|||||||
names = zf.namelist()
|
names = zf.namelist()
|
||||||
self.assertIn("manifest.xml", names)
|
self.assertIn("manifest.xml", names)
|
||||||
i6d_files = [n for n in names if n.endswith(".i6d")]
|
i6d_files = [n for n in names if n.endswith(".i6d")]
|
||||||
# 2 substances + 2 ref substances + 1 ESR = 5 i6d files
|
self.assertEqual(len(i6d_files), 4)
|
||||||
self.assertEqual(len(i6d_files), 5)
|
|
||||||
|
|
||||||
def test_anonymous_returns_401(self):
|
def test_anonymous_returns_401(self):
|
||||||
self.client.logout()
|
self.client.logout()
|
||||||
|
|||||||
@ -7,6 +7,11 @@ from uuid import uuid4
|
|||||||
|
|
||||||
from django.test import SimpleTestCase, tag
|
from django.test import SimpleTestCase, tag
|
||||||
|
|
||||||
|
from epapi.v1.interfaces.iuclid.dto import (
|
||||||
|
PathwayCompoundDTO,
|
||||||
|
PathwayEdgeDTO,
|
||||||
|
PathwayExportDTO,
|
||||||
|
)
|
||||||
from epiuclid.serializers.i6z import I6ZSerializer
|
from epiuclid.serializers.i6z import I6ZSerializer
|
||||||
from epiuclid.serializers.pathway_mapper import (
|
from epiuclid.serializers.pathway_mapper import (
|
||||||
IUCLIDDocumentBundle,
|
IUCLIDDocumentBundle,
|
||||||
@ -14,9 +19,24 @@ from epiuclid.serializers.pathway_mapper import (
|
|||||||
IUCLIDReferenceSubstanceData,
|
IUCLIDReferenceSubstanceData,
|
||||||
IUCLIDSubstanceData,
|
IUCLIDSubstanceData,
|
||||||
IUCLIDTransformationProductEntry,
|
IUCLIDTransformationProductEntry,
|
||||||
|
PathwayMapper,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _unlinked_documents(manifest_xml: str) -> list[tuple[str | None, str]]:
|
||||||
|
ns = "http://iuclid6.echa.europa.eu/namespaces/manifest/v1"
|
||||||
|
root = ET.fromstring(manifest_xml)
|
||||||
|
base = root.findtext(f"{{{ns}}}base-document-uuid")
|
||||||
|
linked_targets: set[str | None] = {base}
|
||||||
|
docs: dict[str, str | None] = {}
|
||||||
|
for doc in root.findall(f".//{{{ns}}}document"):
|
||||||
|
uuid = doc.findtext(f"{{{ns}}}uuid")
|
||||||
|
docs[uuid] = doc.findtext(f"{{{ns}}}type")
|
||||||
|
for link in doc.findall(f"{{{ns}}}links/{{{ns}}}link"):
|
||||||
|
linked_targets.add(link.findtext(f"{{{ns}}}ref-uuid"))
|
||||||
|
return [(doc_type, uuid) for uuid, doc_type in docs.items() if uuid not in linked_targets]
|
||||||
|
|
||||||
|
|
||||||
def _make_bundle() -> IUCLIDDocumentBundle:
|
def _make_bundle() -> IUCLIDDocumentBundle:
|
||||||
ref_uuid = uuid4()
|
ref_uuid = uuid4()
|
||||||
sub_uuid = uuid4()
|
sub_uuid = uuid4()
|
||||||
@ -197,3 +217,29 @@ class I6ZSerializerTest(SimpleTestCase):
|
|||||||
}
|
}
|
||||||
self.assertIn(parent_ref_key, reference_links)
|
self.assertIn(parent_ref_key, reference_links)
|
||||||
self.assertIn(product_ref_key, reference_links)
|
self.assertIn(product_ref_key, reference_links)
|
||||||
|
|
||||||
|
def test_multi_compound_pathway_has_no_unlinked_documents(self):
|
||||||
|
compounds = [
|
||||||
|
PathwayCompoundDTO(pk=1, name="Root", smiles="c1ccccc1"),
|
||||||
|
PathwayCompoundDTO(pk=2, name="P1", smiles="CCO"),
|
||||||
|
PathwayCompoundDTO(pk=3, name="P2", smiles="CCN"),
|
||||||
|
PathwayCompoundDTO(pk=4, name="P3", smiles="CCC"),
|
||||||
|
]
|
||||||
|
export = PathwayExportDTO(
|
||||||
|
pathway_uuid=uuid4(),
|
||||||
|
pathway_name="Regression Pathway",
|
||||||
|
compounds=compounds,
|
||||||
|
edges=[
|
||||||
|
PathwayEdgeDTO(edge_uuid=uuid4(), start_compound_pks=[1], end_compound_pks=[2]),
|
||||||
|
PathwayEdgeDTO(edge_uuid=uuid4(), start_compound_pks=[1], end_compound_pks=[3]),
|
||||||
|
PathwayEdgeDTO(edge_uuid=uuid4(), start_compound_pks=[2], end_compound_pks=[4]),
|
||||||
|
],
|
||||||
|
root_compound_pks=[1],
|
||||||
|
)
|
||||||
|
bundle = PathwayMapper().map(export)
|
||||||
|
data = I6ZSerializer().serialize(bundle)
|
||||||
|
|
||||||
|
with zipfile.ZipFile(io.BytesIO(data)) as zf:
|
||||||
|
manifest_xml = zf.read("manifest.xml").decode("utf-8")
|
||||||
|
|
||||||
|
self.assertEqual(_unlinked_documents(manifest_xml), [])
|
||||||
|
|||||||
@ -31,7 +31,7 @@ class PathwayMapperTest(SimpleTestCase):
|
|||||||
)
|
)
|
||||||
bundle = PathwayMapper().map(export)
|
bundle = PathwayMapper().map(export)
|
||||||
|
|
||||||
self.assertEqual(len(bundle.substances), 2)
|
self.assertEqual(len(bundle.substances), 1)
|
||||||
self.assertEqual(len(bundle.reference_substances), 2)
|
self.assertEqual(len(bundle.reference_substances), 2)
|
||||||
self.assertEqual(len(bundle.endpoint_study_records), 1)
|
self.assertEqual(len(bundle.endpoint_study_records), 1)
|
||||||
|
|
||||||
@ -49,8 +49,7 @@ class PathwayMapperTest(SimpleTestCase):
|
|||||||
)
|
)
|
||||||
bundle = PathwayMapper().map(export)
|
bundle = PathwayMapper().map(export)
|
||||||
|
|
||||||
# 2 unique compounds -> 2 substances, 2 ref substances
|
self.assertEqual(len(bundle.substances), 1)
|
||||||
self.assertEqual(len(bundle.substances), 2)
|
|
||||||
self.assertEqual(len(bundle.reference_substances), 2)
|
self.assertEqual(len(bundle.reference_substances), 2)
|
||||||
# One endpoint study record per pathway
|
# One endpoint study record per pathway
|
||||||
self.assertEqual(len(bundle.endpoint_study_records), 1)
|
self.assertEqual(len(bundle.endpoint_study_records), 1)
|
||||||
|
|||||||
@ -37,7 +37,8 @@
|
|||||||
class="text-xs text-base-content/50 border-t border-base-300 pt-3"
|
class="text-xs text-base-content/50 border-t border-base-300 pt-3"
|
||||||
>
|
>
|
||||||
<strong>Format:</strong> First column = SMILES, Second column =
|
<strong>Format:</strong> First column = SMILES, Second column =
|
||||||
Name (headers optional) • Maximum 30 rows
|
Name (headers optional) • Maximum
|
||||||
|
{{ batch_predict_max_compoundss|default:150 }} rows
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -195,8 +196,7 @@
|
|||||||
// Function to populate table from CSV data
|
// Function to populate table from CSV data
|
||||||
function populateTableFromCSV(csvData) {
|
function populateTableFromCSV(csvData) {
|
||||||
const lines = csvData.trim().split("\n");
|
const lines = csvData.trim().split("\n");
|
||||||
const maxRows = 30;
|
const maxRows = Number("{{ batch_predict_max_compounds|default:150 }}");
|
||||||
|
|
||||||
// Clear existing table
|
// Clear existing table
|
||||||
clearTable();
|
clearTable();
|
||||||
|
|
||||||
|
|||||||
@ -51,7 +51,10 @@
|
|||||||
</svg>
|
</svg>
|
||||||
Go Home
|
Go Home
|
||||||
</a>
|
</a>
|
||||||
<button onclick="window.history.back()" class="btn btn-outline">
|
<button
|
||||||
|
onclick="window.location.href = document.referrer"
|
||||||
|
class="btn btn-outline"
|
||||||
|
>
|
||||||
<svg
|
<svg
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
class="mr-2 h-5 w-5"
|
class="mr-2 h-5 w-5"
|
||||||
|
|||||||
Reference in New Issue
Block a user