Build, manipulate, export and import networks
This notebook provides the code to create a network, manipulate it, complete it, visualize it, export it to a file (.sif) and import it from a file (.sif).
[1]:
from neko.core.network import Network
from neko._visual.visualize_network import NetworkVisualizer
from neko._outputs.exports import Exports
(CORNETO) Oct 28 05:36:25 PM - WARNING : No backend found. You can install one of the supported backend by `pip install cvxpy` or `pip install picos`.
1. Define a set of genes we are interested in
[2]:
genes = ["FAK", "NOTCH1", "CDH1", "CDH2", "VIM", "MAP4K4", "LATS1", "LATS2"]
executed in 0ms, finished 16:15:41 2024-10-28
2. Build the network from OmniPath
[3]:
new_net1 = Network(genes, resources='omnipath')
executed in 2.87s, finished 16:15:48 2024-10-28
[4]:
new_net1.complete_connection(algorithm='bfs', only_signed=True, connect_with_bias=True)
executed in 22.40s, finished 16:16:10 2024-10-28
In the previous cell, we have built a network from the genes we are interested in. We have used the OmniPath database to retrieve the interactions. We have then completed the network using a breadth-first search algorithm, considering only signed interactions. The option connect_with_bias
allows us to connect the nodes prioritizing already existing interactions in the network, over new ones. This means that the resulting network will have a higher density of interactions between the nodes
that were already connected. Moreover, our implementation of the bsf algorithm contains a random component, so the resulting network may vary each time you run the code.
3. Visualize the network
[5]:
visualizer1 = NetworkVisualizer(new_net1, color_by='effect')
visualizer1.render()
executed in 0ms, finished 16:16:10 2024-10-28
4. Manipulate the network: remove a node
We can remove a node from the network using the method remove_node
from the Network object. When a Node is removed, all the edges connected to it are also removed.
[6]:
new_net1.remove_node("SRC")
executed in 0ms, finished 16:16:26 2024-10-28
[7]:
visualizer1 = NetworkVisualizer(new_net1, color_by='effect')
visualizer1.render()
executed in 0ms, finished 16:16:27 2024-10-28
5. Export the network to a SIF file
We can export the network to a file using the method export_sif
from the Exports object. The file will be saved in the current directory. The Exports object requires a Network object as input, and it allows us to export the network to different formats, including Bnet.
[8]:
exporter = Exports(new_net1)
executed in 0ms, finished 16:18:36 2024-10-28
[9]:
exporter.export_sif("simple_interaction_format.sif")
executed in 0ms, finished 16:18:36 2024-10-28
6. Import the network from a SIF file
We can import a network from a file using the method Network
from the Network object. The file must be in SIF format. The Network object requires the path to the file as input.
[10]:
new_net2 = Network(sif_file="simple_interaction_format.sif", resources='omnipath')
executed in 1.75s, finished 16:18:42 2024-10-28
[11]:
visualizer2 = NetworkVisualizer(new_net2, color_by='effect')
visualizer2.render()
executed in 0ms, finished 16:18:42 2024-10-28
7. Manipulate the network: add a node
Just for demonstration purposes, we can add a node to the network using the method add_node
from the Network object. The node can be connected later to the rest of the network using the same or different algorithms.
[12]:
new_net2.add_node("SRC")
executed in 0ms, finished 16:20:33 2024-10-28
[13]:
new_net2.nodes
executed in 0ms, finished 16:20:34 2024-10-28
[13]:
Genesymbol | Uniprot | Type | |
---|---|---|---|
0 | EPHA2 | P29317 | NaN |
1 | COMPLEX:P06493_P14635 | COMPLEX:P06493_P14635 | NaN |
2 | LATS1 | O95835 | NaN |
3 | PLK1 | P53350 | NaN |
4 | GADD45A | P24522 | NaN |
5 | CDH1 | P12830 | NaN |
6 | CDK1 | P06493 | NaN |
7 | INCENP | Q9NQS7 | NaN |
8 | LATS2 | Q9NRM7 | NaN |
9 | MAP3K1 | Q13233 | NaN |
10 | PTK2 | Q05397 | NaN |
11 | MAP3K7 | O43318 | NaN |
12 | NF2 | P35240 | NaN |
13 | GSK3B | P49841 | NaN |
14 | PRKCA | P17252 | NaN |
15 | CDH2 | P19022 | NaN |
16 | VIM | P08670 | NaN |
17 | FLT4 | P35916 | NaN |
18 | MAP4K4 | O95819 | NaN |
19 | RAP1A | P62834 | NaN |
20 | FOS | P01100 | NaN |
21 | CDC42 | P60953 | NaN |
22 | ITGB1 | P05556 | NaN |
23 | PTPN1 | P18031 | NaN |
24 | INSR | P06213 | NaN |
25 | TP53 | P04637 | NaN |
26 | NOTCH1 | P46531 | NaN |
27 | SRC | P12931 | NaN |
[14]:
visualizer2 = NetworkVisualizer(new_net2, color_by='effect')
visualizer2.render()
executed in 0ms, finished 16:20:36 2024-10-28
8. Manipulate the network: complete the network
We can now complete the network and connect the disconnected node that we have previously added. Moreover, by choosing a different algorithm, we can obtain a different network structure, and get interactions that we have not considered before.
[15]:
new_net2.complete_connection(maxlen=2, algorithm='dfs', only_signed=True)
executed in 13.11s, finished 16:22:36 2024-10-28
[16]:
visualizer2 = NetworkVisualizer(new_net2, color_by='effect')
visualizer2.render()
executed in 1.56s, finished 16:22:37 2024-10-28
[17]:
new_net2.edges
executed in 0ms, finished 16:22:37 2024-10-28
[17]:
source | target | Type | Effect | References | |
---|---|---|---|---|---|
0 | P46531 | P35916 | None | stimulation | SIF file; nan |
1 | P35916 | Q05397 | None | stimulation | SIF file; HPRD:16452200 |
2 | P49841 | P46531 | None | stimulation | SIF file; ACSN:11967263;ACSN:14663202;ACSN:150... |
3 | P49841 | P12830 | None | stimulation | SIF file; ACSN:10671551;ACSN:10671552;ACSN:112... |
4 | P12830 | P29317 | None | stimulation | SIF file; nan |
... | ... | ... | ... | ... | ... |
187 | P29353 | P62993 | None | stimulation | Adhesome:10049786;Adhesome:10080542;Adhesome:1... |
188 | P62993 | P29353 | None | stimulation | Adhesome:10049786;Adhesome:10080542;Adhesome:1... |
189 | P62993 | P19174 | None | stimulation | Adhesome:10940929;Adhesome:15953601;Adhesome:7... |
190 | P19174 | P17612 | None | bimodal | Adhesome:2479646 |
191 | P17612 | P19174 | None | inhibition | Adhesome:2479646;CA1:2479646;ProtMapper:137047... |
192 rows × 5 columns
As we can see the Network contains bimodal interactions. We can remove them from the network if we consider that they are not relevant for our analysis.
[18]:
new_net2.edges[new_net2.edges["Effect"] == "bimodal"]
executed in 0ms, finished 16:23:22 2024-10-28
[18]:
source | target | Type | Effect | References | |
---|---|---|---|---|---|
16 | P17252 | P08670 | None | bimodal | SIF file; Adhesome:2500966;HPRD-phos:11895474;... |
22 | P18031 | P06213 | None | bimodal | SIF file; Adhesome:10592173;Adhesome:10751417;... |
23 | P06213 | P18031 | None | bimodal | SIF file; Adhesome:10592173;Adhesome:10751417;... |
38 | P49841 | P04637 | None | bimodal | SIF file; ELM:11483158;HPRD-phos:11483158;HPRD... |
50 | P06493 | P08670 | None | bimodal | SIF file; HPRD-phos:15345747;HPRD-phos:1656522... |
74 | P12931 | P29317 | None | bimodal | ProtMapper:24457997;ProtMapper:26788993;ProtMa... |
91 | P17612 | P35240 | None | bimodal | KEA:15378014;KEA:18071304;PhosphoSite:11703924... |
110 | P17612 | P49841 | None | bimodal | HPRD-phos:12054501;HPRD-phos:18669648;HPRD-pho... |
111 | P17252 | P12931 | None | bimodal | ACSN:11313945;ACSN:11447289;ACSN:11940581;ACSN... |
118 | P17252 | P19174 | None | bimodal | Adhesome:10592173;Adhesome:1370476;Adhesome:28... |
132 | P17612 | P62834 | None | bimodal | CA1:8463283;CA1:9867809;HPRD-phos:9867809;HPRD... |
145 | P05556 | P29353 | None | bimodal | Adhesome:10964917;Adhesome:19889638;InnateDB:1... |
149 | P18031 | P12931 | None | bimodal | Adhesome:11007774;Adhesome:12857726;Adhesome:1... |
162 | P12931 | P61586 | None | bimodal | Adhesome:10592173;SIGNOR:23027962 |
163 | P12931 | P20936 | None | bimodal | Adhesome:11389730;Adhesome:1717825;BioGRID:171... |
170 | P63000 | P61586 | None | bimodal | Adhesome:10592173 |
181 | P56945 | P61586 | None | bimodal | NaN |
183 | P17612 | P61586 | None | bimodal | Adhesome:12654918;Adhesome:8599934;HPRD-phos:1... |
190 | P19174 | P17612 | None | bimodal | Adhesome:2479646 |
9. Manipulate the network: remove bimodal interactions
[19]:
import pandas as pd
executed in 0ms, finished 16:26:13 2024-10-28
[20]:
# remove from the Network object all the interaction that are bimodal with less than X references (each reference is separated by ;)
number_of_references = 55
for index, row in new_net2.edges[new_net2.edges["Effect"] == "bimodal"].iterrows():
if pd.isna(row["References"]) or len(row["References"].split(";")) < number_of_references:
print("Removing edge between", row["source"], "and", row["target"])
new_net2.remove_edge(row["source"], row["target"])
executed in 0ms, finished 16:26:17 2024-10-28
Removing edge between P17252 and P08670
Removing edge between P49841 and P04637
Removing edge between P12931 and P29317
Removing edge between P17612 and P35240
Removing edge between P17612 and P49841
Removing edge between P17252 and P12931
Removing edge between P17252 and P19174
Removing edge between P17612 and P62834
Removing edge between P05556 and P29353
Removing edge between P18031 and P12931
Removing edge between P12931 and P61586
Removing edge between P12931 and P20936
Removing edge between P63000 and P61586
Removing edge between P56945 and P61586
Removing edge between P17612 and P61586
Removing edge between P19174 and P17612
[21]:
new_net2.edges[new_net2.edges["Effect"] == "bimodal"]
executed in 0ms, finished 16:27:29 2024-10-28
[21]:
source | target | Type | Effect | References | |
---|---|---|---|---|---|
22 | P18031 | P06213 | None | bimodal | SIF file; Adhesome:10592173;Adhesome:10751417;... |
23 | P06213 | P18031 | None | bimodal | SIF file; Adhesome:10592173;Adhesome:10751417;... |
50 | P06493 | P08670 | None | bimodal | SIF file; HPRD-phos:15345747;HPRD-phos:1656522... |
[22]:
visualizer2 = NetworkVisualizer(new_net2, color_by='effect')
visualizer2.render()
executed in 1.46s, finished 16:27:32 2024-10-28
10. Export the network to a Bnet file
We can export the network to a file using the method export_bnet
from the Exports object. The file will be saved in the current directory. The user must specify the name of the file WITHOUT the extension. In case the Network contains bimodal interactions, the function will export a file for each possible permutation of the interactions. This number scales exponentially with the number of bimodal interactions in the network, resulting in a large number of files. BE CAREFUL when exporting a
network with bimodal interactions!
[23]:
exporter2 = Exports(new_net2)
executed in 0ms, finished 15:53:38 2024-10-28
[24]:
exporter2.export_bnet("./logical_models/logical_model")
executed in 0ms, finished 15:53:40 2024-10-28
Warning: The network has 3 BIMODAL interaction(s).
Bimodal interactions:
PTPN1 -> INSR
Reference: SIF file; Adhesome:10592173;Adhesome:10751417;Adhesome:10807907;Adhesome:11163213;Adhesome:11506178;Adhesome:11579209;Adhesome:11726652;Adhesome:12237455;Adhesome:12612081;Adhesome:12634852;Adhesome:14722096;Adhesome:15588987;Adhesome:16271887;Adhesome:16582879;Adhesome:16926280;Adhesome:17092689;Adhesome:17159996;Adhesome:17481567;Adhesome:19029027;Adhesome:21806020;Adhesome:8702689;Adhesome:8999839;CA1:14722096;DEPOD:11163213;DEPOD:11579209;DEPOD:12237455;DEPOD:12634852;DEPOD:14722096;DEPOD:15588987;DEPOD:16271887;DEPOD:16582879;DEPOD:17159996;DEPOD:17481567;DEPOD:8826975;DEPOD:8999839;DIP:16271887;DOMINO:11163213;DOMINO:11579209;DOMINO:12237455;DOMINO:12634852;DOMINO:15588987;DOMINO:16926280;DOMINO:17481567;HPRD-phos:16582879;HPRD:11506178;HPRD:12237455;HPRD:16582879;HPRD:8826975;HPRD:9355745;InnateDB:11579209;InnateDB:12237455;InnateDB:12612081;InnateDB:14722096;InnateDB:17481567;IntAct:11163213;IntAct:11506178;IntAct:11579209;IntAct:12237455;IntAct:12634852;IntAct:14722096;IntAct:15588987;IntAct:16582879;IntAct:16926280;IntAct:17092689;IntAct:17159996;IntAct:17481567;IntAct:19029027;IntAct:21806020;IntAct:8702689;IntAct:8999839;KEA:10852715;KEA:11850117;Lit-BM-17:11163213;Lit-BM-17:11506178;Lit-BM-17:11579209;Lit-BM-17:11726652;Lit-BM-17:12237455;Lit-BM-17:12634852;Lit-BM-17:14722096;Lit-BM-17:15588987;Lit-BM-17:16271887;Lit-BM-17:16582879;Lit-BM-17:16926280;Lit-BM-17:17092689;Lit-BM-17:17159996;Lit-BM-17:17481567;Lit-BM-17:19029027;Lit-BM-17:21806020;Lit-BM-17:8999839;ProtMapper:10066179;ProtMapper:11579209;ProtMapper:12612081;ProtMapper:1321126;ProtMapper:15192089;ProtMapper:15632081;ProtMapper:1599438;ProtMapper:16582879;ProtMapper:8244979;SIGNOR:11579209;SIGNOR:16582879;SPIKE_LC:8999839;SignaLink3:11506178;SignaLink3:12237455;SignaLink3:15632081;SignaLink3:16582879;SignaLink3:23331499;SignaLink3:8826975;SignaLink3:9355745
INSR -> PTPN1
Reference: SIF file; Adhesome:10592173;Adhesome:10751417;Adhesome:10807907;Adhesome:11163213;Adhesome:11506178;Adhesome:11579209;Adhesome:11726652;Adhesome:12237455;Adhesome:12612081;Adhesome:12634852;Adhesome:14722096;Adhesome:15588987;Adhesome:16271887;Adhesome:16582879;Adhesome:16926280;Adhesome:17092689;Adhesome:17159996;Adhesome:17481567;Adhesome:19029027;Adhesome:21806020;Adhesome:8702689;Adhesome:8999839;DIP:16271887;DOMINO:11163213;DOMINO:11579209;DOMINO:12237455;DOMINO:12634852;DOMINO:15588987;DOMINO:16926280;DOMINO:17481567;HPRD-phos:11506178;HPRD-phos:9355745;HPRD:11506178;HPRD:12237455;HPRD:16582879;HPRD:8826975;HPRD:9355745;InnateDB:11579209;InnateDB:12237455;InnateDB:12612081;InnateDB:14722096;InnateDB:17481567;IntAct:11163213;IntAct:11506178;IntAct:11579209;IntAct:12237455;IntAct:12634852;IntAct:14722096;IntAct:15588987;IntAct:16582879;IntAct:16926280;IntAct:17092689;IntAct:17159996;IntAct:17481567;IntAct:19029027;IntAct:21806020;IntAct:8702689;IntAct:8999839;KEA:11106648;KEA:11506178;KEA:17570479;KEA:8999839;KEA:9355745;Lit-BM-17:11163213;Lit-BM-17:11506178;Lit-BM-17:11579209;Lit-BM-17:11726652;Lit-BM-17:12237455;Lit-BM-17:12634852;Lit-BM-17:14722096;Lit-BM-17:15588987;Lit-BM-17:16271887;Lit-BM-17:16582879;Lit-BM-17:16926280;Lit-BM-17:17092689;Lit-BM-17:17159996;Lit-BM-17:17481567;Lit-BM-17:19029027;Lit-BM-17:21806020;Lit-BM-17:8999839;ProtMapper:11506178;ProtMapper:11579209;ProtMapper:15212693;SIGNOR:11506178;SPIKE_LC:8999839;iPTMnet:11106648;iPTMnet:11506178;iPTMnet:9355745;phosphoELM:11106648
CDK1 -> VIM
Reference: SIF file; HPRD-phos:15345747;HPRD-phos:16565220;HPRD-phos:16964243;HPRD-phos:17287340;HPRD-phos:17924679;HPRD-phos:18212344;HPRD-phos:18491316;HPRD-phos:18578522;HPRD-phos:18669648;HPRD-phos:18691976;HPRD-phos:18707149;HPRD-phos:18767875;HPRD-phos:19007248;HPRD-phos:19413330;HPRD-phos:19415658;HPRD-phos:19651622;HPRD-phos:19664995;HPRD-phos:19691289;HPRD-phos:20058876;HPRD-phos:20068230;HPRD-phos:20068231;HPRD-phos:7983050;HPRD:15345747;HPRD:7983050;KEA:12761892;KEA:15345747;KEA:15713670;KEA:15766329;KEA:16565220;KEA:16964243;KEA:17081983;KEA:17570479;KEA:7983050;PhosphoSite:19584300;ProtMapper:16260496;ProtMapper:16542212;ProtMapper:21422740;ProtMapper:22120848;ProtMapper:24073199;ProtMapper:25184044;ProtMapper:25960391;ProtMapper:27603133;ProtMapper:7983050;ProtMapper:9168797;SIGNOR:7983050;dbPTM:16565220;dbPTM:16964243;dbPTM:17081983;dbPTM:17287340;dbPTM:17924679;dbPTM:18669648;dbPTM:18691976;dbPTM:19007248;dbPTM:19369195;dbPTM:19413330;dbPTM:19690332;dbPTM:21465480;iPTMnet:15345747;iPTMnet:16260496;iPTMnet:16565220;iPTMnet:16964243;iPTMnet:17287340;iPTMnet:17924679;iPTMnet:18212344;iPTMnet:18491316;iPTMnet:18578522;iPTMnet:18669648;iPTMnet:18691976;iPTMnet:18707149;iPTMnet:18767875;iPTMnet:19007248;iPTMnet:19413330;iPTMnet:19415658;iPTMnet:19651622;iPTMnet:19664995;iPTMnet:19691289;iPTMnet:20058876;iPTMnet:20068230;iPTMnet:20068231;iPTMnet:27603133;iPTMnet:7983050;phosphoELM:7983050
Created BNet file: ./logical_models/logical_model_1.bnet
Created BNet file: ./logical_models/logical_model_2.bnet
Created BNet file: ./logical_models/logical_model_3.bnet
Created BNet file: ./logical_models/logical_model_4.bnet
Created BNet file: ./logical_models/logical_model_5.bnet
Created BNet file: ./logical_models/logical_model_6.bnet
Created BNet file: ./logical_models/logical_model_7.bnet
Created BNet file: ./logical_models/logical_model_8.bnet
Generated 8 BNet files.
[ ]: