Creating packages#
from enviPath_python import enviPath
from enviPath_python.objects import *
On this tutorial we will create a package and try to replicate the 1[(4-chlorophenyl)phenylmethyl]piperazine (CPP) Pathway.
We start by defining the enviPath class and using https://envipath.org/ as INSTANCE_HOST. In order to create Package we need to assign a Group to it, we browse over all groups available on enviPath and return the “Anonymous” group.
eP = enviPath("https://envipath.org/")
anonymous_group = [group for group in eP.get_groups() if "Anonymous" in group.get_name()][0]
pkg = eP.create_package(
name="Test Package", group=anonymous_group,
description="This package is created as a tutorial for enviPath-python documentation!")
pkg.get_id()
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
File ~/checkouts/readthedocs.org/user_builds/envipath-python/envs/develop/lib/python3.10/site-packages/requests/models.py:976, in Response.json(self, **kwargs)
975 try:
--> 976 return complexjson.loads(self.text, **kwargs)
977 except JSONDecodeError as e:
978 # Catch JSON-related errors and raise as requests.JSONDecodeError
979 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
File ~/.asdf/installs/python/3.10.17/lib/python3.10/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
343 if (cls is None and object_hook is None and
344 parse_int is None and parse_float is None and
345 parse_constant is None and object_pairs_hook is None and not kw):
--> 346 return _default_decoder.decode(s)
347 if cls is None:
File ~/.asdf/installs/python/3.10.17/lib/python3.10/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
333 """Return the Python representation of ``s`` (a ``str`` instance
334 containing a JSON document).
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
File ~/.asdf/installs/python/3.10.17/lib/python3.10/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 3 column 1 (char 2)
During handling of the above exception, another exception occurred:
JSONDecodeError Traceback (most recent call last)
Cell In[2], line 2
1 eP = enviPath("https://envipath.org/")
----> 2 anonymous_group = [group for group in eP.get_groups() if "Anonymous" in group.get_name()][0]
3 pkg = eP.create_package(
4 name="Test Package", group=anonymous_group,
5 description="This package is created as a tutorial for enviPath-python documentation!")
7 pkg.get_id()
File ~/checkouts/readthedocs.org/user_builds/envipath-python/envs/develop/lib/python3.10/site-packages/enviPath_python/enviPath.py:289, in enviPath.get_groups(self)
282 def get_groups(self):
283 """
284 Gets all groups the logged in user has at least read permissions on.
285
286 :return: List of Group objects.
287 :rtype: List
288 """
--> 289 return self.requester.get_objects(self.BASE_URL, Endpoint.GROUP)
File ~/checkouts/readthedocs.org/user_builds/envipath-python/envs/develop/lib/python3.10/site-packages/enviPath_python/enviPath.py:494, in enviPathRequester.get_objects(self, base_url, endpoint)
485 """
486 Generic get method to retrieve objects.
487
(...)
491 :return: List of objects denoted by endpoint.
492 """
493 url = base_url + endpoint.value
--> 494 objs = self.get_request(url).json()
496 # Handly malformed reponse in case there are not entries
497 if len(objs) == 0 or ('object' in objs and len(objs['object']) == 0):
File ~/checkouts/readthedocs.org/user_builds/envipath-python/envs/develop/lib/python3.10/site-packages/requests/models.py:980, in Response.json(self, **kwargs)
976 return complexjson.loads(self.text, **kwargs)
977 except JSONDecodeError as e:
978 # Catch JSON-related errors and raise as requests.JSONDecodeError
979 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 980 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
JSONDecodeError: Expecting value: line 3 column 1 (char 2)
The pathway in question has 3 Nodes and 2 Edges, we add all the necessary information for the nodes and edges on lists of dictionaries and then we will loop over them to add them in the correct order. When we create the Pathway, the label root_node_only=True indicates that we want to add the Pathway manually, indicating as False would trigger a Pathway prediction.
node_list = [{"smiles": "C1=CC=C(C=C1)C(C2=CC=C(C=C2)Cl)N3CCNCC3",
"name": "1[(4-chlorophenyl)phenylmethyl]piperazine", "confidence": 1},
{"smiles": "CC(=O)N1CCN(CC1)C(C2=CC=CC=C2)C3=CC=C(C=C3)Cl",
"name": "CPP_TP_M329", "confidence": 3},
{"smiles": "C1=CC=C(C=C1)C(C2=CC=C(C=C2)Cl)N3CCN(CC3)C(=O)CCC(=O)O",
"name": "CPP_TP_M387", "confidence": 3}]
edge_list = [{"smirks": "C1=CC=C(C=C1)C(C2=CC=C(C=C2)Cl)N3CCNCC3>>CC(=O)N1CCN(CC1)C(C2=CC=CC=C2)C3=CC=C(C=C3)Cl"},
{"smirks": "C1=CC=C(C=C1)C(C2=CC=C(C=C2)Cl)N3CCNCC3>>C1=CC=C(C=C1)C(C2=CC=C(C=C2)Cl)N3CCN(CC3)C(=O)CCC(=O)O"}]
pw = Pathway.create(pkg, smiles=node_list[0]["smiles"],
name=node_list[0]["name"], root_node_only=True)
for node_info in node_list:
pw.add_node(smiles=node_info["smiles"],
name=node_info["name"])
for edge_info in edge_list:
pw.add_edge(smirks=edge_info["smirks"])
Next we want to create the scenario associated with this Pathway, and to do that we have to create a list of all the relevant AdditionalInformation objects. This part might look a bit tedious, but can be easily automatized when your experimental data comes from a flat file.
[<enviPath_python.objects.AcidityAdditionalInformation at 0x7fb489da7f10>,
<enviPath_python.objects.BiologicalTreatmentTechnologyAdditionalInformation at 0x7fb489da7f40>,
<enviPath_python.objects.BioreactorAdditionalInformation at 0x7fb489da7ee0>,
<enviPath_python.objects.FinalCompoundConcentrationAdditionalInformation at 0x7fb489d9b130>,
<enviPath_python.objects.OriginalSludgeAmountAdditionalInformation at 0x7fb489da79a0>,
<enviPath_python.objects.InoculumSourceAdditionalInformation at 0x7fb489d9b220>,
<enviPath_python.objects.LocationAdditionalInformation at 0x7fb489d9b0d0>,
<enviPath_python.objects.PurposeOfWWTPAdditionalInformation at 0x7fb489d9b1c0>,
<enviPath_python.objects.RedoxAdditionalInformation at 0x7fb489d9b040>,
<enviPath_python.objects.SludgeRetentionTimeAdditionalInformation at 0x7fb489d9b280>,
<enviPath_python.objects.SolventForCompoundSolutionAdditionalInformation at 0x7fb489d9b340>,
<enviPath_python.objects.SourceOfLiquidMatrixAdditionalInformation at 0x7fb489d9b2e0>,
<enviPath_python.objects.TemperatureAdditionalInformation at 0x7fb489d9b430>,
<enviPath_python.objects.TSSAdditionalInformation at 0x7fb489d9b2b0>,
<enviPath_python.objects.TypeOfAerationAdditionalInformation at 0x7fb488147dc0>,
<enviPath_python.objects.AerationTypeAdditionalInformation at 0x7fb489d9b460>,
<enviPath_python.objects.TypeOfAdditionAdditionalInformation at 0x7fb488147eb0>]
Eventually we create a main Scenario, where all the common information will be added to and create 3 ReferringScenarioAdditionalInformation that point to the main one. This will copy all the additional information objects from the main scenario and we will only have to change the ConfidenceLevelAdditionalInformation object and append those to the corresponding node of the pathway.
main_scenario = Scenario.create(pkg, additional_information=additional_info_list)
for node_info in node_list:
node = [n for n in pw.get_nodes() if (node_info["smiles"] == n.get_smiles())][0]
ai = ConfidenceLevelAdditionalInformation()
ai.set_radioconfidence(node_info["confidence"])
referral_scenario = Scenario.create(pkg, referring_scenario_id=main_scenario.get_id(), additional_information=[])
referral_scenario.update_scenario(additional_information=[ai])
node.add_scenario(referral_scenario)
To finish and data cleaness purposes, we delete the package. If you download this notebook, you can comment the line below to see the generated pathway, but please make sure to delete the package once you are finished with this tutorial.
pkg.delete()