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

Aligning two sequences #90

Open
cmfield opened this issue Sep 13, 2024 · 4 comments
Open

Aligning two sequences #90

cmfield opened this issue Sep 13, 2024 · 4 comments

Comments

@cmfield
Copy link

cmfield commented Sep 13, 2024

If I am using subplots to plot multiple sequences, is it possible to offset the plot in the x-axis? Then I can align parts of the plot that belong together.

I've tried changing first_index, modifying the xlims of individual axes, but I think I'm missing something obvious.

@veghp
Copy link
Member

veghp commented Sep 16, 2024

Hi thanks for trying this package. I'm not aware of an easy or built-in way of doing this.
A workaround hack would be to add (random) bases to the beginning of the shorter sequence to align it up with the longer one. Another option is to try and do this through matplotlib: https://stackoverflow.com/questions/37241029/can-i-move-about-the-axes-in-a-matplotilb-subplot

@cmfield
Copy link
Author

cmfield commented Sep 16, 2024

I found a hack in the end. After making the graphic records, I located the feature I wanted to line up in each record and its position and orientation. I flipped every sequence record that was the wrong way around and remade the graphic records. Then the longest sequence gave me an anchor position, and I shifted every other graphic record feature by the difference between the anchor and its feature-of-interest position, then changed .first_index to this offset.

Then in matplotlib subplots, I use sharex=False, and fix the xlimits to be the same for every plot. It worked.. though I'm not sure how robustly :)

@veghp
Copy link
Member

veghp commented Sep 16, 2024

Thank you for describing this, it will be a good reference for the future.

@Zulko
Copy link
Member

Zulko commented Sep 16, 2024

You can use Matplotlib's fig.add_gridspec(number_of_sequences, number_of_columns) to create shifted subplots. It will require some math to position the plots but the result is neat and it lets you plot whatever dna_features_viewer can plot without changing your records.

import matplotlib.pyplot as plt
from dna_features_viewer import GraphicFeature, GraphicRecord

fig = plt.figure(figsize=(12, 2))  # Set a large width to allow for shifting
grid = fig.add_gridspec(2, 200)  # Two rows, 200 column or as many as necessary

# PLOT 1

features1 = [
    GraphicFeature(start=0, end=50, strand=+1, color="#ffcccc", label="Feature 1"),
    GraphicFeature(start=100, end=150, strand=-1, color="#ccccff", label="Feature 2"),
]
record1 = GraphicRecord(sequence_length=200, features=features1)
ax1 = fig.add_subplot(grid[0, 0:150])
record1.plot(ax=ax1, with_ruler=True)

# PLOT 2

features2 = [
    GraphicFeature(start=10, end=60, strand=+1, color="#ccffcc", label="Feature 3"),
    GraphicFeature(start=120, end=180, strand=-1, color="#ffcc99", label="Feature 4"),
]
record2 = GraphicRecord(sequence_length=200, features=features2)
ax2 = fig.add_subplot(grid[1, 50:200])
record2.plot(ax=ax2, with_ruler=True)

image

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

3 participants