GangCaoLab / CoolBox

Jupyter notebook based genomic data visualization toolkit.
https://gangcaolab.github.io/CoolBox/index.html
GNU General Public License v3.0
224 stars 37 forks source link

is there a way to annotate with text #63

Closed jksr closed 2 years ago

jksr commented 2 years ago

is there a way to put some text label over tracks?

Nanguage commented 2 years ago

Yes, it's easy to implement with track customization

image

code:

from coolbox.core.track.base import Track
from coolbox.core.coverage.base import track_to_coverage
from coolbox.utilities import to_gr
import matplotlib.pyplot as plt

class Text(Track):
    def __init__(self, text, fontsize=50, offset=0.45):
        super().__init__({
            "fontsize": fontsize,
            "offset": offset,
            "text": text,
        })

    def fetch_data(self, gr, **kwargs):
        return self.properties['text']

    def plot(self, ax, gr, **kwargs):
        x = gr.start + self.properties['offset'] * (gr.end - gr.start)
        text = self.fetch_data(gr)
        ax.text(x, 0, text, fontsize=self.properties['fontsize'])
        ax.set_xlim(gr.start, gr.end)

TextCov = track_to_coverage(Text)