Skip to content

Commit

Permalink
removed plot_slots_graph and plot_connections_graph (now AssemblyMix …
Browse files Browse the repository at this point in the history
…methods)
  • Loading branch information
Zulko committed Dec 4, 2019
1 parent 241faed commit 806f22b
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 170 deletions.
2 changes: 0 additions & 2 deletions dnacauldron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,5 @@
from .reports import (
plot_cuts,
full_assembly_report,
plot_slots_graph,
plot_connections_graph,
full_assembly_plan_report,
)
2 changes: 1 addition & 1 deletion dnacauldron/reports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# __all__ = []

from .plots import plot_cuts, plot_slots_graph, plot_connections_graph
from .plots import plot_cuts
from .full_assembly_report import full_assembly_report
from .full_assembly_plan_report import full_assembly_plan_report

Expand Down
2 changes: 0 additions & 2 deletions dnacauldron/reports/full_assembly_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
)
from .plots import (
plot_cuts,
plot_slots_graph,
AssemblyTranslator,
plot_connections_graph,
)
from ..tools import write_record, record_is_linear

Expand Down
166 changes: 1 addition & 165 deletions dnacauldron/reports/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
CircularGraphicRecord,
GraphicRecord,
)
from matplotlib.patches import Circle
from dnacauldron.tools import annotate_record

try:
Expand Down Expand Up @@ -113,167 +112,4 @@ def compute_feature_color(self, feature):
graphic_record = translator.translate_record(
record, record_class=record_class
)
return graphic_record.plot(ax=ax, figure_width=figure_width)


def plot_slots_graph(
mix,
ax=None,
with_overhangs=False,
directed=True,
show_missing=True,
highlighted_parts=None,
with_arrows=True,
):
"""Plot a map of the different assemblies.
Parameters
----------
mix
A DnaCauldron AssemblyMix object.
ax
A matplotlib ax on which to plot. If none is provided, one is created.
with_overhangs
If true, the overhangs appear in the graph
"""
slots = mix.compute_slots()
highlighted_parts = set(
[] if highlighted_parts is None else highlighted_parts
)
graph = mix.slots_graph(with_overhangs=with_overhangs, directed=directed)

# Positioning - a bit complex to deal with multi-components graphs
pos = {}
undirected_graph = graph.to_undirected()
components = list(nx.components.connected_components(undirected_graph))
if components == []:
raise ValueError(
"Empty connections graph. This probably means your "
"parts were filtered out, possibly because they do "
"not contain the right enzyme sites or something "
"that"
)
max_len = 1.0 * max(len(c) for c in components)
for i, g in enumerate(components):
g = undirected_graph.subgraph(g)
pos.update(
nx.layout.kamada_kawai_layout(
g, center=(0, -i), scale=len(g) / max_len
)
)

parts = [n for n in graph.nodes() if n in slots]

fig, ax = plt.subplots(1, figsize=(13, 0.5 * len(parts)))
nx.draw(
graph,
pos=pos,
node_color="w",
node_size=100,
ax=ax,
edge_color="#eeeeee",
)

# Draw a "highlights graph" above the other graph
def highlight_slot(slot):
return any([part in highlighted_parts for part in slots.get(slot, [])])

highlighted_nodes = [n for n in graph.nodes() if highlight_slot(n)]
if len(highlighted_nodes):
highlighted_subgraph = graph.subgraph(highlighted_nodes)
nx.draw(
highlighted_subgraph,
pos=pos,
node_color="#3a3aad",
node_size=300,
ax=ax,
edge_color="#aaaaaa",
)
legend = []

def polar(xy):
x, y = xy - np.array([0.05, 0.05])
return (np.arctan2(x, -y), -np.sqrt(x ** 2 + y ** 2))

sorted_pos = sorted(pos.items(), key=lambda c: polar(c[1]))
for i, (n, (x, y)) in enumerate(sorted_pos):
if n in parts:
slot_parts = list(slots[n])
color = "w" if highlight_slot(n) else "#3a3aad"
legend.append("\n ".join(sorted(slot_parts)))
fontdict = {}
if highlight_slot(n) or len(slot_parts) > 1:
fontdict = {"weight": "bold"}
ax.text(
x,
y,
len(legend),
ha="center",
va="center",
color=color,
fontdict=fontdict,
)
else:
ax.text(
x,
y,
n,
ha="center",
va="center",
color="#333333",
size=9,
fontdict=dict(family="Inconsolata"),
)
text = "\n".join(
["Parts:"]
+ ["%2s - %s" % (str(i + 1), name) for i, name in enumerate(legend)]
)
if show_missing:
all_mix_parts = set([f.id for f in mix.constructs])
all_slots_parts = set([p for plist in slots.values() for p in plist])
missing_parts = all_mix_parts.difference(all_slots_parts)
if len(missing_parts):
text += "\n!!! Missing parts: " + ", ".join(missing_parts)

ax.text(
1.1,
0.5,
text,
va="center",
transform=ax.transAxes,
fontdict=dict(size=12, family="Inconsolata"),
)
ax.set_aspect("equal")
return ax


def plot_connections_graph(mix, ax=None, figsize=(20, 20)):

graph = mix.uniquified_connection_graph

def fragment_label(i):
fragment = mix.fragments_dict[i]
return "\n".join(
[
str(fragment.seq.left_end),
r"$\bf{%s}$" % fragment.original_construct.id,
str(fragment.seq.right_end),
]
)

labels = {i: fragment_label(i) for i in graph}
if ax is None:
fig, ax = plt.subplots(1, 1, figsize=figsize)
nx.draw_kamada_kawai(
graph,
labels=labels,
font_color="k",
edge_color="grey",
font_size=12,
node_color="w",
node_size=3000,
)
return ax

return graphic_record.plot(ax=ax, figure_width=figure_width)

0 comments on commit 806f22b

Please sign in to comment.