Edinburgh-Genome-Foundry / DnaFeaturesViewer

:eye: Python library to plot DNA sequence features (e.g. from Genbank files)
https://edinburgh-genome-foundry.github.io/DnaFeaturesViewer/
MIT License
598 stars 92 forks source link

How to use `plot_on_multiple_pages` with `ax` attribute ? #54

Open bioinfornatics opened 3 years ago

bioinfornatics commented 3 years ago

Dear I would like to add multiple subplot in order to display annotation from genbank and sample feature from external input. Each sample have a subplot in order to set accordingly a title which a simplified version give:

a4_landscape = (11.69, 8.27)
fig = Figure(figsize=a4_landscape, dpi=300)
gs = GridSpec(no_sample+1, 1)
canvas = FigureCanvasAgg(fig)
axe1 = fig.add_subplot(gs[0])
_ = graphic_annotation.plot_on_multiple_pages(output, nucl_per_line=5000, n_lines=6, plot_sequence=False, ax=axe1, with_ruler=False, strand_in_label_threshold=4)
for sample_name, seq_features in features_variations.items():
    graphic_annotation = AnnotationTranslator().translate_record(annotation)
    variations_record = GraphicRecord(sequence_length=graphic_annotation.sequence_length,
                                          features=graphic_variations,
                                          plots_indexing='genbank')
        axe = fig.add_subplot(gs[1], sharex=axe1, label=sample_name)
        axe.set_title(sample_name)
        with_ruler = sample_number == no_sample  # ruler only on the latest axe
        _ = variations_record.plot(ax=axe, with_ruler=with_ruler, strand_in_label_threshold=4)
    fig.tight_layout()

but that do not work as ax would be define twice one time by my script and another time by the library which give this stacktrace:

  File "…/lib/python3.7/site-packages/fr/cea/cnrgh/variant/variation_viewer/__main__.py", line 183, in create_figure
    _ = graphic_annotation.plot_on_multiple_pages(output, nucl_per_line=5000, n_lines=6, plot_sequence=False, ax=axe1, with_ruler=False, strand_in_label_threshold=4)
  File "…/lib/python3.7/site-packages/dna_features_viewer/GraphicRecord/MultilinePlottableMixin.py", line 150, in plot_on_multiple_pages
    **plot_params
  File "…/lib/python3.7/site-packages/dna_features_viewer/GraphicRecord/MultilinePlottableMixin.py", line 78, in plot_on_multiple_lines
    line_ax = plot_line(line_index)
  File "…/lib/python3.7/site-packages/dna_features_viewer/GraphicRecord/MultilinePlottableMixin.py", line 73, in plot_line
    **plot_params

https://github.com/Edinburgh-Genome-Foundry/DnaFeaturesViewer/blob/6ace5cdff96bf995aa26167868b0dbb47f5f5952/dna_features_viewer/GraphicRecord/MultilinePlottableMixin.py#L62

veghp commented 3 years ago

Hi, thanks, yes I can confirm that ax=axe1 in plot_on_multiple_pages() on line 6 causes the problem. To document the issue: the plot_line() function takes ax as an argument and ax is also passed with **plot_params. Also, this function is used without arguments on line 78 and with ax on line 90. I looked into it but not sure there is a quick fix.

(Would it solve the problem if you used graphic_annotation.plot_on_multiple_lines() and the resulting plot instead?)