Skip to content

Commit

Permalink
changed struc of naming_dic, removed it
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas Abraham - INFlux committed Aug 14, 2024
1 parent 794030c commit 68dea71
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 45 deletions.
3 changes: 1 addition & 2 deletions docs/examples/network.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
"source": [
"# create the network\n",
"graph = SequenceNetwork(sequences=sequences)\n",
"graph.create_graph()\n",
"graph.update_threshhold(0.4)\n",
"\n",
"# plot the network\n",
Expand Down Expand Up @@ -233,7 +232,7 @@
"\n",
"G = graph.network\n",
"\n",
"# remove isolated nodes\n",
"# remove isolated node\n",
"G.remove_node(\"NPA53530.1\")\n",
"G.remove_node(\"O67275\")\n",
"G.remove_nodes_from(list(nx.isolates(G)))\n",
Expand Down
12 changes: 4 additions & 8 deletions pyeed/network/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ class Config:
default="http://cytoscape:1234/v1",
)

def post_init(self):
self.create_graph()

def add_to_targets(self, target: SequenceRecord):
if target.id not in self.targets:
self.targets.append(target.id)

def create_graph(self, naming_dict=None):
def _create_graph(self):
"""
Initializes the nx.Graph object and adds nodes and edges based on the sequences.
Expand All @@ -90,13 +93,6 @@ def create_graph(self, naming_dict=None):
for sequence in self.sequences:
seq_dict = sequence.to_dict()
seq_id = seq_dict.pop("@id")
if naming_dict is not None:
seq_id = seq_id.split(".")[0]
if seq_id in naming_dict:
seq_id = naming_dict[seq_id]
else:
print(f"Sequence {seq_id} not found in the naming_dict")
continue
node_dict = seq_dict.pop("organism") if "organism" in seq_dict else {}
node_dict["sequence"] = sequence.sequence
node_dict["name"] = sequence.name if sequence.name else ""
Expand Down
36 changes: 1 addition & 35 deletions tests/unit/network_tests/test_newtork_graph_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_general_build_networkx(self):
sequences=mats,
weight="identity",
)
network.create_graph()

def test_graph_build(self):
mat_accessions = [
Expand All @@ -41,37 +40,4 @@ def test_graph_build(self):
weight="identity",
)

network.create_graph()

assert len(list(network.network.nodes)) == len(mats)

def test_graph_naming_dic(self):
mat_accessions = [
"MBP1912539.1",
"SEV92896.1",
"MBO8174569.1",
"WP_042680787.1",
]

naming_dic = {
"MBP1912539": "MAT1",
"SEV92896": "MAT2",
"MBO8174569": "MAT3",
"WP_042680787": "MAT4",
}

mats = ProteinRecord.get_ids(mat_accessions)
# Create network
network = SequenceNetwork(
sequences=mats,
weight="identity",
naming_dict=naming_dic,
)

network.create_graph(naming_dict=naming_dic)

# convert networkx to pandas
data = dict(network.network.nodes(data=True))

assert len(list(network.network.nodes)) == len(mats)
assert 'MAT1' in list(data.keys())
assert len(list(network.network.nodes)) == len(mats)

0 comments on commit 68dea71

Please sign in to comment.