Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request: incorrect arrow inference when zoom in to a small region of a large genome #91

Open
Hocnonsense opened this issue Dec 30, 2024 · 2 comments

Comments

@Hocnonsense
Copy link

Hocnonsense commented Dec 30, 2024

Hi, thanks for this usefull tool, it's useful for me.

However, I want to use it to plot a small region (~1k bp) of the genome (1M bp), and I've found there is no arrow on the genome. It is caused by the misuseage of self.sequence_length in L160:

if is_undirected or head_is_cut:
head_length = 0.001
else:
width_pixel = self._get_ax_width(ax, unit="pixel")
head_length = 0.5 * width_pixel * feature.length / self.sequence_length
head_length = min(head_length, 0.6 * feature.thickness)

Example

  1. a small genome
record = GraphicRecord(
    sequence=("N" * 2000000)[1999000-1000:2000000],
    features=[
        GraphicFeature(start=1999000-1999000 + 1000, end=1999500-1999000 + 1000, strand=+1, color="#ffcccc"),
        GraphicFeature(start=1999500-1999000 + 1000, end=2000000-1999000 + 1000, strand=+1, color="#ccccff"),
    ],
)

ax, _ = record.plot(figure_width=6, x_lim=(1000, None))
assert isinstance(ax.figure, plt.Figure)
ax.figure.savefig("sequence_and_translation.png", bbox_inches="tight")

image

record = GraphicRecord(
    sequence=("N" * 2000000)[1999000-10000:2000000],
    features=[
        GraphicFeature(start=1999000-1999000 + 10000, end=1999500-1999000 + 10000, strand=+1, color="#ffcccc"),
        GraphicFeature(start=1999500-1999000 + 10000, end=2000000-1999000 + 10000, strand=+1, color="#ccccff"),
    ],
)

ax, _ = record.plot(figure_width=6, x_lim=(10000, None))
assert isinstance(ax.figure, plt.Figure)
ax.figure.savefig("sequence_and_translation.png", bbox_inches="tight")

image

  1. a bigger one
record = GraphicRecord(
    sequence=("N" * 2000000)[1999000-10000:2000000],
    features=[
        GraphicFeature(start=1999000-1999000 + 10000, end=1999500-1999000 + 10000, strand=+1, color="#ffcccc"),
        GraphicFeature(start=1999500-1999000 + 10000, end=2000000-1999000 + 10000, strand=+1, color="#ccccff"),
    ],
)

ax, _ = record.plot(figure_width=6, x_lim=(10000, None))
assert isinstance(ax.figure, plt.Figure)
ax.figure.savefig("sequence_and_translation.png", bbox_inches="tight")

image

  1. a large one
record = GraphicRecord(
    sequence=("N" * 2000000)[1999000-100000:2000000],
    features=[
        GraphicFeature(start=1999000-1999000 + 100000, end=1999500-1999000 + 100000, strand=+1, color="#ffcccc"),
        GraphicFeature(start=1999500-1999000 + 100000, end=2000000-1999000 + 100000, strand=+1, color="#ccccff"),
    ],
)

ax, _ = record.plot(figure_width=6, x_lim=(100000, None))
assert isinstance(ax.figure, plt.Figure)
ax.figure.savefig("sequence_and_translation.png", bbox_inches="tight")

image

record = GraphicRecord(
    sequence=("N" * 2000000)[:2000000],
    features=[
        GraphicFeature(start=1999000, end=1999500, strand=+1, color="#ffcccc"),
        GraphicFeature(start=1999500, end=2000000, strand=+1, color="#ccccff"),
    ],
)

ax, _ = record.plot(figure_width=6, x_lim=(1999000, None))
assert isinstance(ax.figure, plt.Figure)
ax.figure.savefig("sequence_and_translation.png", bbox_inches="tight")

image

Supposed change

just use ax.get_xlim() to get the actual plotted genome range.

        if is_undirected or head_is_cut:
            head_length = 0.001
        else:
            width_pixel = self._get_ax_width(ax, unit="pixel")
            head_length = 0.5 * width_pixel * feature.length / (ax.get_xlim()[1] - ax.get_xlim()[0])
            head_length = min(head_length, 0.6 * feature.thickness)

image

Thanks for your consideration!

veghp added a commit that referenced this issue Jan 9, 2025
@veghp
Copy link
Member

veghp commented Jan 9, 2025

Thank you for brining attention to this and for the proposed solution. I implemented the proposed change. The test outputs all looked fine.

Here is variant of the example code, for future reference:

n = 100000
s = 500

record = GraphicRecord(
    sequence=("N" * (n)),
    features=[
        GraphicFeature(start=n-2*s, end=n-s, strand=+1, color="#ffcccc"),
        GraphicFeature(start=n-s, end=n, strand=+1, color="#ccccff"),
    ],
)
ax, _ = record.plot(figure_width=6, x_lim=(n-2*s, None))

@Hocnonsense
Copy link
Author

Thanks for your responce!

Here I also record another way to plot it, potentially more elegant in my case, sorry for my late reply:

n = 100000
s = 500

record = GraphicRecord(
    sequence=("N" * (n)),
    features=[
        GraphicFeature(start=n-2*s, end=n-s, strand=+1, color="#ffcccc"),
        GraphicFeature(start=n-s, end=n, strand=+1, color="#ccccff"),
    ],
)
ax, _ = record.crop(n-2*s, n).plot(figure_width=6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants