forked from enviPath/enviPy
[Feature] Sync Features from Client Repo (#425)
- New PackageImporter - Fix persisting Molfile on Structure creation - Add NonPersistent Prediction - Pathway View Options - Edit Node Fix Co-authored-by: Tim Lorsbach <tim@lorsba.ch> Reviewed-on: enviPath/enviPy#425
This commit is contained in:
@ -4,3 +4,7 @@ class InvalidSMILESException(Exception):
|
||||
|
||||
class InvalidMolfileException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class PackageImportException(Exception):
|
||||
pass
|
||||
|
||||
@ -1635,24 +1635,10 @@ class PathwayNode(Schema):
|
||||
# TODO
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def resolve_engineered_intermediate(obj: Node):
|
||||
# TODO
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def resolve_image(obj: Node):
|
||||
return f"{obj.default_node_label.url}?image=svg"
|
||||
|
||||
@staticmethod
|
||||
def resolve_image_size(obj: Node):
|
||||
return 400
|
||||
|
||||
@staticmethod
|
||||
def resolve_proposed_intermediate(obj: Node):
|
||||
# TODO
|
||||
return []
|
||||
|
||||
|
||||
class PathwaySchema(Schema):
|
||||
aliases: List[str] = Field([], alias="aliases")
|
||||
@ -2282,3 +2268,26 @@ def get_setting(request, setting_uuid):
|
||||
return 403, {
|
||||
"message": f"Getting Setting with id {setting_uuid} failed due to insufficient rights!"
|
||||
}
|
||||
|
||||
|
||||
########
|
||||
# Util #
|
||||
########
|
||||
class NonPersistent(Schema):
|
||||
smiles: str
|
||||
setting_url: str = Field(..., alias="settingUri")
|
||||
|
||||
|
||||
@router.post("/util", response={200: Any, 403: Error})
|
||||
def predict(request, np: Form[NonPersistent]):
|
||||
try:
|
||||
from epdb.logic import SPathway
|
||||
|
||||
setting = SettingManager.get_setting_by_url(request.user, np.setting_url)
|
||||
spw = SPathway(prediction_setting=setting, root_nodes=[np.smiles])
|
||||
spw.predict()
|
||||
return spw.to_json()
|
||||
except ValueError:
|
||||
return 403, {
|
||||
"message": f"Getting Setting with id {np.setting_url} failed due to insufficient rights!"
|
||||
}
|
||||
|
||||
@ -1011,7 +1011,7 @@ class PackageManager(object):
|
||||
add_import_timestamp=True,
|
||||
trust_reviewed=False,
|
||||
) -> Package:
|
||||
importer = PackageImporter(data, preserve_uuids, add_import_timestamp, trust_reviewed)
|
||||
importer = PackageImporter(data, preserve_uuids)
|
||||
imported_package = importer.do_import()
|
||||
|
||||
up = UserPackagePermission()
|
||||
@ -1828,6 +1828,11 @@ class SPathway(object):
|
||||
"to": to_indices,
|
||||
}
|
||||
|
||||
if edge.rule:
|
||||
e["rule"] = edge.rule.simple_json()
|
||||
if edge.probability:
|
||||
e["probability"] = edge.probability
|
||||
|
||||
edges.append(e)
|
||||
|
||||
return {
|
||||
|
||||
@ -1241,8 +1241,12 @@ class CompoundStructure(
|
||||
if description is not None:
|
||||
cs.description = nh3.clean(description, tags=s.ALLOWED_HTML_TAGS).strip()
|
||||
|
||||
cs.smiles = smiles
|
||||
cs.compound = compound
|
||||
cs.smiles = smiles
|
||||
|
||||
# Check if molfile is present and valid
|
||||
if molfile is not None and molfile.strip() != "":
|
||||
cs.molfile = molfile
|
||||
|
||||
if "normalized_structure" in kwargs:
|
||||
cs.normalized_structure = kwargs["normalized_structure"]
|
||||
@ -2023,6 +2027,7 @@ class Pathway(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMix
|
||||
# add links start -> pseudo
|
||||
new_link = {
|
||||
"name": link["name"],
|
||||
"plain_name": link["plain_name"],
|
||||
"id": link["id"],
|
||||
"url": link["url"],
|
||||
"image": link["image"],
|
||||
@ -2032,6 +2037,7 @@ class Pathway(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMix
|
||||
"source": node_url_to_idx[link["start_node_urls"][0]],
|
||||
"target": pseudo_idx,
|
||||
"app_domain": link.get("app_domain", None),
|
||||
"to_pseudo": True,
|
||||
}
|
||||
adjusted_links.append(new_link)
|
||||
|
||||
@ -2039,6 +2045,7 @@ class Pathway(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMix
|
||||
for target in link["end_node_urls"]:
|
||||
new_link = {
|
||||
"name": link["name"],
|
||||
"plain_name": link["plain_name"],
|
||||
"id": link["id"],
|
||||
"url": link["url"],
|
||||
"image": link["image"],
|
||||
@ -2049,6 +2056,7 @@ class Pathway(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMix
|
||||
"target": node_url_to_idx[target],
|
||||
"app_domain": link.get("app_domain", None),
|
||||
"multi_step": link["multi_step"],
|
||||
"from_pseudo": True,
|
||||
}
|
||||
adjusted_links.append(new_link)
|
||||
|
||||
@ -2359,17 +2367,19 @@ class Node(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMixin)
|
||||
def _url(self):
|
||||
return "{}/node/{}".format(self.pathway.url, self.uuid)
|
||||
|
||||
def get_name(self):
|
||||
def get_name(self, include_suffix=True):
|
||||
non_generic_name = True
|
||||
|
||||
if self.name == "no name":
|
||||
if self.name is None or self.name == "no name":
|
||||
non_generic_name = False
|
||||
|
||||
return (
|
||||
self.name
|
||||
if non_generic_name
|
||||
else f"{self.default_node_label.name} (taken from underlying structure)"
|
||||
)
|
||||
if non_generic_name:
|
||||
return self.name
|
||||
else:
|
||||
if include_suffix:
|
||||
return f"{self.default_node_label.name} (taken from underlying structure)"
|
||||
else:
|
||||
return self.default_node_label.name
|
||||
|
||||
def d3_json(self):
|
||||
app_domain_data = self.get_app_domain_assessment_data()
|
||||
@ -2399,6 +2409,7 @@ class Node(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMixin)
|
||||
),
|
||||
"image_type": "svg",
|
||||
"name": self.get_name(),
|
||||
"plain_name": self.get_name(include_suffix=False),
|
||||
"smiles": self.default_node_label.smiles,
|
||||
"scenarios": [{"name": s.get_name(), "url": s.url} for s in self.scenarios.all()],
|
||||
"app_domain": {
|
||||
@ -2505,6 +2516,28 @@ class Node(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMixin)
|
||||
|
||||
return res
|
||||
|
||||
def is_proposed_intermediate(self):
|
||||
collected = defaultdict(dict)
|
||||
for ai in self.additional_information.filter(
|
||||
type__in=["ProposedIntermediate", "TransformationProductImportance", "Confidence"],
|
||||
scenario__isnull=False,
|
||||
):
|
||||
collected[str(ai.scenario.uuid)]["scenarioId"] = ai.scenario.url
|
||||
collected[str(ai.scenario.uuid)]["scenarioName"] = ai.scenario.name
|
||||
|
||||
if ai.type == "ProposedIntermediate":
|
||||
collected[str(ai.scenario.uuid)]["proposed"] = True
|
||||
|
||||
if ai.type == "Confidence":
|
||||
collected[str(ai.scenario.uuid)]["Confidence"] = ai.get().level
|
||||
|
||||
if ai.type == "TransformationProductImportance":
|
||||
collected[str(ai.scenario.uuid)]["Transformation product importance"] = (
|
||||
ai.get().importance.value
|
||||
)
|
||||
|
||||
return list(collected.values())
|
||||
|
||||
|
||||
class Edge(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMixin):
|
||||
pathway = models.ForeignKey(
|
||||
@ -2526,6 +2559,7 @@ class Edge(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMixin)
|
||||
def d3_json(self):
|
||||
edge_json = {
|
||||
"name": self.get_name(),
|
||||
"plain_name": self.get_name(include_suffix=False),
|
||||
"id": self.url,
|
||||
"url": self.url,
|
||||
"image": self.url + "?image=svg",
|
||||
@ -2640,17 +2674,19 @@ class Edge(EnviPathModel, AliasMixin, ScenarioMixin, AdditionalInformationMixin)
|
||||
|
||||
return res
|
||||
|
||||
def get_name(self):
|
||||
def get_name(self, include_suffix=True):
|
||||
non_generic_name = True
|
||||
|
||||
if self.name == "no name":
|
||||
non_generic_name = False
|
||||
|
||||
return (
|
||||
self.name
|
||||
if non_generic_name
|
||||
else f"{self.edge_label.name} (taken from underlying reaction)"
|
||||
)
|
||||
if non_generic_name:
|
||||
return self.name
|
||||
else:
|
||||
if include_suffix:
|
||||
return f"{self.edge_label.name} (taken from underlying reaction)"
|
||||
else:
|
||||
return self.edge_label.name
|
||||
|
||||
|
||||
class EPModel(PolymorphicModel, EnviPathModel, AdditionalInformationMixin):
|
||||
|
||||
@ -775,13 +775,11 @@ def models(request):
|
||||
}
|
||||
|
||||
if s.ENVIFORMER_PRESENT:
|
||||
context["model_types"]["EnviFormer"] = (
|
||||
{
|
||||
"type": "enviformer",
|
||||
"requires_rule_packages": False,
|
||||
"requires_data_packages": True,
|
||||
},
|
||||
)
|
||||
context["model_types"]["EnviFormer"] = {
|
||||
"type": "enviformer",
|
||||
"requires_rule_packages": False,
|
||||
"requires_data_packages": True,
|
||||
}
|
||||
|
||||
if s.FLAGS.get("PLUGINS", False):
|
||||
for k, v in s.CLASSIFIER_PLUGINS.items():
|
||||
@ -2102,12 +2100,14 @@ def package_pathways(request, package_uuid):
|
||||
else:
|
||||
prediction_setting = current_user.prediction_settings()
|
||||
|
||||
is_predict_mode = pw_mode in {"predict", "incremental"}
|
||||
|
||||
pw = Pathway.create(
|
||||
current_package,
|
||||
stand_smiles,
|
||||
stand_smiles if is_predict_mode else smiles,
|
||||
name=name,
|
||||
description=description,
|
||||
predicted=pw_mode in {"predict", "incremental"},
|
||||
predicted=is_predict_mode,
|
||||
)
|
||||
|
||||
# set mode
|
||||
@ -2466,7 +2466,26 @@ def package_pathway_node(request, package_uuid, pathway_uuid, node_uuid):
|
||||
|
||||
return JsonResponse({"success": current_node.url})
|
||||
|
||||
return HttpResponseBadRequest()
|
||||
new_node_name = request.POST.get("node-name")
|
||||
new_node_description = request.POST.get("node-description")
|
||||
|
||||
if any([new_node_name, new_node_description]):
|
||||
if new_node_name is not None and new_node_name.strip() != "":
|
||||
new_node_name = nh3.clean(new_node_name.strip(), tags=s.ALLOWED_HTML_TAGS).strip()
|
||||
current_node.name = new_node_name
|
||||
|
||||
if new_node_description is not None and new_node_description.strip() != "":
|
||||
new_node_description = nh3.clean(
|
||||
new_node_description.strip(), tags=s.ALLOWED_HTML_TAGS
|
||||
).strip()
|
||||
current_node.description = new_node_description
|
||||
|
||||
current_node.save()
|
||||
|
||||
return redirect(current_node.url)
|
||||
|
||||
return error(request, "Node update failed!", "No changes were made to the node")
|
||||
|
||||
else:
|
||||
return HttpResponseNotAllowed(["GET", "POST"])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user