Skip to content

Plot

chromatinhd.models.pred.plot

Copredictivity

Bases: Panel

Plot co-predictivity of a gene.

Source code in src/chromatinhd/models/pred/plot/copredictivity.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Copredictivity(polyptich.grid.Panel):
    """
    Plot co-predictivity of a gene.
    """

    def __init__(self, plotdata, width):
        super().__init__((width, width / 2))

        norm = mpl.colors.CenteredNorm(0, np.abs(plotdata["cor"]).max())
        cmap = mpl.cm.RdBu_r

        chromatinhd.plot.matshow45(
            self.ax,
            plotdata.set_index(["window_mid1", "window_mid2"])["cor"],
            cmap=cmap,
            norm=norm,
            radius=50,
        )
        self.ax.invert_yaxis()

        panel_copredictivity_legend = self.add_inset(
            polyptich.grid.Panel((0.05, 0.8)), pos=(0.0, 0.0), offset=(0.0, 0.2)
        )
        plt.colorbar(
            mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
            cax=panel_copredictivity_legend.ax,
            orientation="vertical",
        )
        panel_copredictivity_legend.ax.set_ylabel(
            "Co-predictivity\n(cor $\\Delta$cor)",
            rotation=0,
            ha="right",
            va="center",
        )
        panel_copredictivity_legend.ax.yaxis.set_ticks_position("left")
        panel_copredictivity_legend.ax.yaxis.set_label_position("left")

    @classmethod
    def from_regionpairwindow(cls, regionpairwindow, gene, width):
        """
        Plot co-predictivity of a gene using a RegionPairWindow object.
        """
        plotdata = regionpairwindow.get_plotdata(gene).reset_index()
        return cls(plotdata, width)

from_regionpairwindow(regionpairwindow, gene, width) classmethod

Plot co-predictivity of a gene using a RegionPairWindow object.

Source code in src/chromatinhd/models/pred/plot/copredictivity.py
49
50
51
52
53
54
55
@classmethod
def from_regionpairwindow(cls, regionpairwindow, gene, width):
    """
    Plot co-predictivity of a gene using a RegionPairWindow object.
    """
    plotdata = regionpairwindow.get_plotdata(gene).reset_index()
    return cls(plotdata, width)

Pileup

Bases: Panel

Source code in src/chromatinhd/models/pred/plot/predictivity.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
class Pileup(polyptich.grid.Panel):
    def __init__(
        self,
        plotdata,
        window,
        width,
        ymax=2.0,
    ):
        super().__init__((width, 0.5))
        if "position" not in plotdata.columns:
            plotdata = plotdata.reset_index()

        ax = self.ax
        ax.set_xlim(*window)
        ax.plot(
            plotdata["position"],
            plotdata["lost"],
            color="#333",
            lw=1,
        )
        ax.fill_between(
            plotdata["position"],
            plotdata["lost"],
            0,
            color="#333",
            alpha=0.2,
            lw=0,
        )
        ax.set_xlim(ax.get_xlim())
        ax.set_ylabel(
            "# fragments\nper 1kb\nper 1k cells",
            rotation=0,
            ha="right",
            va="center",
        )

        # change vertical alignment of last y tick to bottom
        ax.set_yticks([0, ymax])
        ax.get_yticklabels()[-1].set_verticalalignment("top")
        ax.get_yticklabels()[0].set_verticalalignment("bottom")

        # vline at tss
        ax.axvline(0, color="#888888", lw=0.5, zorder=-1, dashes=(2, 2))

        ax.set_xticks([])
        ax.set_ylim(0, ymax)

    @classmethod
    def from_regionmultiwindow(cls, regionmultiwindow, gene, width, window=None):
        """
        Plot pileup of a specific gene using a regionmultiwindow object
        """
        plotdata = regionmultiwindow.get_plotdata(gene).reset_index()
        if window is None:
            window = np.array([plotdata["position"].min(), plotdata["position"].max()])
        return cls(plotdata, width=width, window=window)

from_regionmultiwindow(regionmultiwindow, gene, width, window=None) classmethod

Plot pileup of a specific gene using a regionmultiwindow object

Source code in src/chromatinhd/models/pred/plot/predictivity.py
195
196
197
198
199
200
201
202
203
@classmethod
def from_regionmultiwindow(cls, regionmultiwindow, gene, width, window=None):
    """
    Plot pileup of a specific gene using a regionmultiwindow object
    """
    plotdata = regionmultiwindow.get_plotdata(gene).reset_index()
    if window is None:
        window = np.array([plotdata["position"].min(), plotdata["position"].max()])
    return cls(plotdata, width=width, window=window)

Predictivity

Bases: Panel

Plot predictivity of a gene.

Source code in src/chromatinhd/models/pred/plot/predictivity.py
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class Predictivity(polyptich.grid.Panel):
    """
    Plot predictivity of a gene.
    """

    def __init__(
        self,
        plotdata,
        window,
        width,
        show_accessibility=False,
        color_by_effect=True,
        limit=-0.05,
        label_y=True,
        height=0.5,
    ):
        super().__init__((width, height))

        if "position" not in plotdata.columns:
            plotdata = plotdata.reset_index()

        plotdata["effect_sign"] = np.sign(plotdata["effect"])
        plotdata["segment"] = plotdata["effect_sign"].diff().ne(0).cumsum()

        ax = self.ax
        ax.set_xlim(*window)

        for segment, segment_data in plotdata.groupby("segment"):
            if color_by_effect:
                color = "tomato" if segment_data["effect"].iloc[0] > 0 else "#0074D9"
            else:
                color = "#333"
            ax.plot(
                segment_data["position"],
                segment_data["deltacor"],
                lw=1,
                color=color,
            )
            ax.fill_between(
                segment_data["position"],
                segment_data["deltacor"],
                0,
                alpha=0.2,
                lw=0,
                color=color,
            )

        # ax.plot(
        #     plotdata["position"],
        #     plotdata["deltacor"],
        #     color="#333",
        #     lw=1,
        # )
        # ax.fill_between(
        #     plotdata["position"],
        #     plotdata["deltacor"],
        #     0,
        #     color="#333",
        #     alpha=0.2,
        #     lw=0,
        # )

        if label_y is True:
            label_y = "Predictivity\n($\\Delta$ cor)"
        ax.set_ylabel(
            label_y,
            rotation=0,
            ha="right",
            va="center",
        )

        ax.set_xticks([])
        ax.invert_yaxis()
        ax.set_ylim(0, max(limit, ax.get_ylim()[1]))

        if show_accessibility:
            ax2 = self.add_twinx()
            ax2.plot(
                plotdata["position"],
                plotdata["lost"],
                color="tomato",
                # color="#333",
                lw=1,
            )
            ax2.fill_between(
                plotdata["position"],
                plotdata["lost"],
                0,
                color="tomato",
                alpha=0.2,
                lw=0,
            )
            ax2.set_xlim(ax.get_xlim())
            ax2.set_ylabel(
                "# fragments\nper 1kb\nper 1k cells",
                rotation=0,
                ha="left",
                va="center",
                color="tomato",
            )
            ax2.tick_params(axis="y", colors="tomato")
            ax2.set_ylim(
                0,
                plotdata["lost"].max() / (plotdata["deltacor"].min() / ax.get_ylim()[1]),
            )

        # change vertical alignment of last y tick to bottom
        ax.set_yticks([0, ax.get_ylim()[1]])
        ax.get_yticklabels()[-1].set_verticalalignment("top")
        ax.get_yticklabels()[0].set_verticalalignment("bottom")

        # vline at tss
        ax.axvline(0, color="#888888", lw=0.5, zorder=-1, dashes=(2, 2))

    @classmethod
    def from_regionmultiwindow(cls, regionmultiwindow, gene, width, show_accessibility=False, window=None):
        """
        Plot predictivity of a specific gene using a RegionMultiWindow object
        """
        plotdata = regionmultiwindow.get_plotdata(gene).reset_index()
        if window is None:
            window = np.array([plotdata["position"].min(), plotdata["position"].max()])
        return cls(plotdata, width=width, show_accessibility=show_accessibility, window=window)

    def add_arrow(self, position, y=0.5, orientation="left"):
        ax = self.ax
        trans = mpl.transforms.blended_transform_factory(x_transform=ax.transData, y_transform=ax.transAxes)
        if orientation == "left":
            xytext = (15, 15)
        elif orientation == "right":
            xytext = (-15, 15)
        ax.annotate(
            text="",
            xy=(position, y),
            xytext=xytext,
            textcoords="offset points",
            xycoords=trans,
            arrowprops=dict(arrowstyle="->", color="black", lw=1, connectionstyle="arc3"),
        )

from_regionmultiwindow(regionmultiwindow, gene, width, show_accessibility=False, window=None) classmethod

Plot predictivity of a specific gene using a RegionMultiWindow object

Source code in src/chromatinhd/models/pred/plot/predictivity.py
121
122
123
124
125
126
127
128
129
@classmethod
def from_regionmultiwindow(cls, regionmultiwindow, gene, width, show_accessibility=False, window=None):
    """
    Plot predictivity of a specific gene using a RegionMultiWindow object
    """
    plotdata = regionmultiwindow.get_plotdata(gene).reset_index()
    if window is None:
        window = np.array([plotdata["position"].min(), plotdata["position"].max()])
    return cls(plotdata, width=width, show_accessibility=show_accessibility, window=window)