Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 0 additions & 104 deletions plasnet/alt_label_propagation.py

This file was deleted.

5 changes: 0 additions & 5 deletions plasnet/base_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,3 @@ def load(cls: Type[BaseGraphType], filepath: Path) -> BaseGraphType:
def write_classification(self, typing_fh: TextIO) -> None:
for node in self.nodes:
typing_fh.write(f"{node}\t{self.label}\n")

def compare_classification(self, prev_typing: dict, typing_fh: TextIO) -> None:
for node in self.nodes:
if node in prev_typing.keys():
typing_fh.write(f"{node}\t{self.label}\t{prev_typing[node]}\n")
114 changes: 0 additions & 114 deletions plasnet/clustering_dists.py

This file was deleted.

89 changes: 0 additions & 89 deletions plasnet/community_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import Optional

from plasnet.alt_label_propagation import appendable_lpa_communities

import networkx as nx

from plasnet.ColorPicker import ColorPicker
Expand Down Expand Up @@ -98,93 +96,6 @@ def split_graph_into_subcommunities(
self._node_to_colour[node] = colour

return Subcommunities(subcommunities)

def split_graph_given_labels(
self, small_subcommunity_size_threshold: int, typings: list[dict]
) -> Subcommunities:

old_plasmids = [plasmid for typing in typings for plasmid in typing.keys() if plasmid in self.nodes]
new_plasmids = [plasmid for plasmid in self.nodes if plasmid not in old_plasmids]
new_subcommunities_nodes: list[set[str]] = list(
nx.community.asyn_lpa_communities(G=self.subgraph(new_plasmids), seed=42)
)

label = 0
map = {}
for typing in typings:
for i, subcomm in enumerate(typing.values()):
map[subcomm] = label + i
label = label + len(typing.keys())
initial_labels = {n: map[typing[n]] for n in old_plasmids}
for subcomm in new_subcommunities_nodes:
for plasmid in list(subcomm):
initial_labels[plasmid] = label
label = label + 1
subcommunities_nodes: list[set[str]] = list(
appendable_lpa_communities(G=self, initial_labels=initial_labels, seed=42)
)
subcommunities_nodes = self._fix_small_subcommunities(
subcommunities_nodes, small_subcommunity_size_threshold
)

subcommunities = []
for subcommunity_index, subcommunity_nodes in enumerate(subcommunities_nodes):
colour = ColorPicker.get_color_given_index(subcommunity_index)

subcommunity = SubcommunityGraph(
self.subgraph(subcommunity_nodes),
self._hub_connectivity_threshold,
self._edge_density,
label=f"{self.label}_subcommunity_{subcommunity_index}",
colour=colour,
)
subcommunities.append(subcommunity)

for node in subcommunity_nodes:
self._node_to_colour[node] = colour

return Subcommunities(subcommunities)

def nearest_neighbour(
self, typing, new_plasmids
) -> Subcommunities:
subcommunity_names = set(typing["type"].to_list())
subcommunity_labels = {subcomm:[plasmid for plasmid in typing[typing["type"]==subcomm]["plasmid"].values] for subcomm in list(subcommunity_names) if subcomm.split("_")[1]==self.label.split("_")[1]} #select only those that are in this community
max_label = len(subcommunity_labels.keys())

for plasmid in new_plasmids:
if plasmid in self.nodes:
neighbours = [n for n in self[plasmid] if n not in new_plasmids]
if len(neighbours)==0:
subcommunity_labels[f"community_{self.label}_subcommunity_{max_label}"] = [plasmid]
max_label = max_label + 1
else:
neighbours = sorted(neighbours, key=lambda n: self.edges[n,plasmid][DistanceTags.SplitDistanceTag.value])
min_dist = self.edges[neighbours[0],plasmid][DistanceTags.SplitDistanceTag.value]
nearest = [neighbour for neighbour in neighbours if self.edges[neighbour,plasmid][DistanceTags.SplitDistanceTag.value]==min_dist]
nearest = sorted(nearest, key=lambda n: len(typing[typing["type"]==typing[typing["plasmid"]==n]["type"].values[0]]))
nn = nearest[-1] #select nearest neighbour with largest subcommunity size
subcommunity_labels[typing[typing["plasmid"]==nn]["type"].values[0]].append(plasmid)


subcommunities = []
for subcommunity_label in subcommunity_labels.keys():
subcommunity_index = int(subcommunity_label.split("_")[-1])
colour = ColorPicker.get_color_given_index(subcommunity_index)

subcommunity = SubcommunityGraph(
self.subgraph(subcommunity_labels[subcommunity_label]),
self._hub_connectivity_threshold,
self._edge_density,
label=subcommunity_label, #reuse old labels here!
colour=colour,
)
subcommunities.append(subcommunity)

for node in subcommunity_labels[subcommunity_label]:
self._node_to_colour[node] = colour

return Subcommunities(subcommunities)

def _get_libs_relative_path(self) -> str:
return ".."
Expand Down
10 changes: 3 additions & 7 deletions plasnet/list_of_graphs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pickle
from pathlib import Path
from typing import Generator, cast, Optional
from typing import Generator, cast

from plasnet.base_graph import BaseGraphType

Expand All @@ -27,15 +27,11 @@ def save_graph_as_text(self, filepath: Path) -> None:
for graph_as_text in self._get_each_graph_as_list_of_nodes_in_text_format():
print(graph_as_text, file=fh)

def save_classification(self, filepath: Path, header: str, prev_typing: Optional[dict]=None) -> None:
def save_classification(self, filepath: Path, header: str) -> None:
with open(filepath, "w") as fh:
print(header, file=fh)
for subgraph in self:
if prev_typing:
subgraph.compare_classification(prev_typing, fh)
else:
subgraph.write_classification(fh)

subgraph.write_classification(fh)

def get_graphs_sorted_by_size(self) -> "ListOfGraphs[BaseGraphType]":
return ListOfGraphs(sorted(self, key=lambda graph: graph.number_of_nodes(), reverse=True))
Loading
Loading