Skip to content

Commit

Permalink
Merge pull request #70 from geoneric/gh67
Browse files Browse the repository at this point in the history
Add plot to visualize pathways using horizontal bars
  • Loading branch information
kordejong authored Feb 7, 2025
2 parents caf3dc3 + 1c78b8a commit 67c3461
Show file tree
Hide file tree
Showing 17 changed files with 684 additions and 18 deletions.
2 changes: 1 addition & 1 deletion documentation/use/example/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Examples

This section contains examples for plotting pathway maps using the ``adaptation_pathways``
package. The same plots can be created by storing the information about the actions and sequences
in a text- or binary-formatted dataset and using the ``ap_plot_pathway_map`` command.
in a text- or binary-formatted dataset and using the ``ap_plot_pathway_map`` and ``ap_plot_bars`` commands.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Bar plot for serial sequence
============================
"""

from io import StringIO

import matplotlib.pyplot as plt

from adaptation_pathways.graph import SequenceGraph, sequence_graph_to_pathway_map
from adaptation_pathways.io import text
from adaptation_pathways.plot import (
action_level_by_first_occurrence,
init_axes,
plot_bars,
)


actions, colour_by_action = text.read_actions(
StringIO(
"""
current #ff4c566a
a #ffbf616a
b #ffd08770
c #ffebcb8b
"""
)
)
sequences, tipping_point_by_action = text.read_sequences(
StringIO(
"""
current current 2030
current a 2040
a b 2050
b c 2060
"""
),
actions,
)
sequence_graph = SequenceGraph(sequences)
pathway_map = sequence_graph_to_pathway_map(sequence_graph)

level_by_action = action_level_by_first_occurrence(sequences)
colour_by_action_name = {
action.name: colour for action, colour in colour_by_action.items()
}

pathway_map.assign_tipping_points(tipping_point_by_action)
pathway_map.set_attribute("level_by_action", level_by_action)

arguments = {
"colour_by_action_name": colour_by_action_name,
}

_, axes = plt.subplots(layout="constrained")
init_axes(axes)
plot_bars(axes, pathway_map, arguments=arguments)
plt.show()
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Pathway map for diverging pathways
==================================
"""

from io import StringIO

import matplotlib.pyplot as plt

from adaptation_pathways.graph import SequenceGraph, sequence_graph_to_pathway_map
from adaptation_pathways.io import text
from adaptation_pathways.plot import (
action_level_by_first_occurrence,
init_axes,
plot_bars,
)


actions, colour_by_action = text.read_actions(
StringIO(
"""
current #ff4c566a
a #ffbf616a
b #ffd08770
c #ffebcb8b
"""
)
)
sequences, tipping_point_by_action = text.read_sequences(
StringIO(
"""
current current 2030
current a 2040
current b 2050
current c 2060
"""
),
actions,
)
sequence_graph = SequenceGraph(sequences)
pathway_map = sequence_graph_to_pathway_map(sequence_graph)

level_by_action = action_level_by_first_occurrence(sequences)
colour_by_action_name = {
action.name: colour for action, colour in colour_by_action.items()
}

pathway_map.assign_tipping_points(tipping_point_by_action)
pathway_map.set_attribute("level_by_action", level_by_action)

arguments = {
"colour_by_action_name": colour_by_action_name,
}

_, axes = plt.subplots(layout="constrained")
init_axes(axes)
plot_bars(axes, pathway_map, arguments=arguments)
plt.show()
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Pathway map for converging pathways
===================================
"""

from io import StringIO

import matplotlib.pyplot as plt

from adaptation_pathways.graph import SequenceGraph, sequence_graph_to_pathway_map
from adaptation_pathways.io import text
from adaptation_pathways.plot import (
action_level_by_first_occurrence,
init_axes,
plot_bars,
)


actions, colour_by_action = text.read_actions(
StringIO(
"""
current #ff4c566a
a #ffbf616a
b #ffd08770
c #ffebcb8b
d #ffa3be8c
"""
)
)
sequences, tipping_point_by_action = text.read_sequences(
StringIO(
"""
current current 2030
current a 2040
current b 2050
current c 2060
a d[1] 2070
b d[2] 2070
c d[3] 2070
"""
),
actions,
)
sequence_graph = SequenceGraph(sequences)
pathway_map = sequence_graph_to_pathway_map(sequence_graph)

level_by_action = action_level_by_first_occurrence(sequences)
colour_by_action_name = {
action.name: colour for action, colour in colour_by_action.items()
}

pathway_map.assign_tipping_points(tipping_point_by_action)
pathway_map.set_attribute("level_by_action", level_by_action)

arguments = {
"colour_by_action_name": colour_by_action_name,
}

_, axes = plt.subplots(layout="constrained")
init_axes(axes)
plot_bars(axes, pathway_map, arguments=arguments)
plt.show()
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
Pathway map for an example
==========================
"""

from io import StringIO

import matplotlib.pyplot as plt

from adaptation_pathways.graph import SequenceGraph, sequence_graph_to_pathway_map
from adaptation_pathways.io import text
from adaptation_pathways.plot import (
action_level_by_first_occurrence,
init_axes,
plot_bars,
)


actions, colour_by_action = text.read_actions(
StringIO(
"""
current #ff4c566a
a #ffbf616a
b #ffd08770
c #ffebcb8b
d #ffa3be8c
e #ffb48ead
f #ff5e81ac
"""
)
)
sequences, tipping_point_by_action = text.read_sequences(
StringIO(
"""
current current 2030
current a 2040
a e[1] 2090
current b 2050
b f[1] 2080
current c 2060
c f[2] 2080
current d 2070
d f[3] 2080
f[1] e[2] 2090
f[2] e[3] 2090
f[3] e[4] 2090
"""
),
actions,
)
sequence_graph = SequenceGraph(sequences)
pathway_map = sequence_graph_to_pathway_map(sequence_graph)

level_by_action = action_level_by_first_occurrence(sequences)
colour_by_action_name = {
action.name: colour for action, colour in colour_by_action.items()
}

pathway_map.assign_tipping_points(tipping_point_by_action)
pathway_map.set_attribute("level_by_action", level_by_action)

arguments = {
"colour_by_action_name": colour_by_action_name,
}

_, axes = plt.subplots(layout="constrained")
init_axes(axes)
plot_bars(axes, pathway_map, arguments=arguments)
plt.show()
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Pathway map for an example
==========================
"""

from io import StringIO

import matplotlib.pyplot as plt

from adaptation_pathways.graph import SequenceGraph, sequence_graph_to_pathway_map
from adaptation_pathways.io import text
from adaptation_pathways.plot import (
action_level_by_first_occurrence,
init_axes,
plot_bars,
)


actions, colour_by_action = text.read_actions(
StringIO(
"""
current #ff4c566a
a #ffbf616a
b #ffd08770
c #ffebcb8b
d #ffa3be8c
"""
)
)
sequences, tipping_point_by_action = text.read_sequences(
StringIO(
"""
current current 2030
current a[1] 2100
current b[1] 2040
b[1] a[2] 2100
b[1] c[2] 2050
c[2] b[2] 2070
b[2] a[3] 2100
c[2] a[4] 2100
c[2] d[3] 2100
b[1] d[2] 2100
current c[1] 2050
c[1] b[3] 2070
b[3] a[5] 2100
c[1] a[6] 2100
c[1] d[4] 2100
current d[1] 2100
"""
),
actions,
)
sequence_graph = SequenceGraph(sequences)
pathway_map = sequence_graph_to_pathway_map(sequence_graph)

level_by_action = action_level_by_first_occurrence(sequences)
colour_by_action_name = {
action.name: colour for action, colour in colour_by_action.items()
}

pathway_map.assign_tipping_points(tipping_point_by_action)
pathway_map.set_attribute("level_by_action", level_by_action)

arguments = {
"colour_by_action_name": colour_by_action_name,
}

_, axes = plt.subplots(layout="constrained")
init_axes(axes)
plot_bars(axes, pathway_map, arguments=arguments)
plt.show()
2 changes: 2 additions & 0 deletions documentation/use/example/ex_15_bar_plot/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Examples of bar plots
=====================
Empty file.
29 changes: 25 additions & 4 deletions documentation/use/plot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ Plotting pathways
Examples <example/index>


The package contains two command-line applications. One is called ``ap_plot_graphs`` and can
be used to gain insight into various kinds of graphs used to store information about pathways,
and one is called ``ap_plot_pathway_map`` and can be used to visualize pathway maps in the
so-called :const:`CLASSIC <adaptation_pathways.plot.pathway_map.plot.PathwayMapLayout.CLASSIC>` layout.
The package contains multiple command-line applications.
``ap_plot_graphs`` and can be used to gain insight into various kinds of graphs used to store information
about pathways.
``ap_plot_pathway_map`` can be used to visualize pathway maps in the so-called
:const:`CLASSIC <adaptation_pathways.plot.pathway_map.plot.PathwayMapLayout.CLASSIC>` layout.
``ap_plot_bars`` can be used to visualize pathways using bar plots.


``ap_plot_graphs``
Expand Down Expand Up @@ -54,3 +56,22 @@ Example for creating a plot from ``my_pathways-action.txt`` and ``my_pathways-se
ap_plot_pathway_map my_pathways my_pathways.pdf
For help about the usage of the command type ``ap_plot_pathway_map --help``.


``ap_plot_bars``
----------------
See also:
:mod:`adaptation_pathways.cli.plot_bars`,
:class:`PathwayMap <adaptation_pathways.graph.pathway_map.PathwayMap>`

This command takes information about pathways as input, and outputs a single plot containing
the pathways visualized by horizontal bars.

Example for creating a plot from ``my_pathways-action.txt`` and ``my_pathways-sequence.txt``
(or ``my_pathways.apw``) to ``./my_pathways.pdf``:

.. code-block:: bash
ap_plot_bars my_pathways my_pathways.pdf
For help about the usage of the command type ``ap_plot_bars --help``.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies = [
[project.scripts]
ap_export = "adaptation_pathways.cli.export:main"
ap_import = "adaptation_pathways.cli.import_:main"
ap_plot_bars = "adaptation_pathways.cli.plot_bars:main"
ap_plot_graphs = "adaptation_pathways.cli.plot_graphs:main"
ap_plot_pathway_map = "adaptation_pathways.cli.plot_pathway_map:main"

Expand Down
Loading

0 comments on commit 67c3461

Please sign in to comment.