Skip to content

Handle empty GFF and alignment files gracefully#28

Merged
Adamtaranto merged 5 commits into
masterfrom
copilot/fix-empty-gff-error-handling
Jan 29, 2026
Merged

Handle empty GFF and alignment files gracefully#28
Adamtaranto merged 5 commits into
masterfrom
copilot/fix-empty-gff-error-handling

Conversation

Copilot AI commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

FlexiDot crashes with ValueError: number sections must be larger than 0 when processing empty GFF files. The error occurs in matplotlib's legend creation when ncol evaluates to 0 from an empty colors list.

Changes

  • GFF parsing: Skip legend generation when no features are parsed. Added guard in legend_figure() to return early if colors list is empty.
  • Alignment parsing: Added warnings for empty BLAST6/PAF files (already returned empty lists correctly, now user-visible).
  • Logging: Warning messages indicate when plots are generated without annotations/overlays.

Example

# Previously crashed
feat_dict = read_gffs('empty.gff3')  
# ValueError: number sections must be larger than 0

# Now logs warning and returns empty dict
feat_dict = read_gffs('empty.gff3')
# WARNING: No annotation records found in GFF file(s). Plot will be generated without annotations.
# Returns: {}

Useful for automated pipelines where some annotation/alignment files may be empty or filtered out.

Original prompt

This section details on the original issue you should resolve

<issue_title>Error if --gff is empty</issue_title>
<issue_description>Throws error if no annotation records in GFF3 input.

Add checks for empty gff or alignment files and handle gracefully.
Log warning if no annotations of alignments are imported.
If sequences are available, generate empty plot with no annotations or alignments.

  ax[1].legend(
Traceback (most recent call last):
  File "/Users/adamtaranto/.condaenvs/flexidot/bin/flexidot", line 8, in <module>
    sys.exit(main())
             ~~~~^^
  File "/Users/adamtaranto/.condaenvs/flexidot/lib/python3.13/site-packages/flexidot/app.py", line 284, in main
    list_of_png_names = polydotplot(
        seq_list,
    ...<27 lines>...
        line_width=line_width,
    )
  File "/Users/adamtaranto/.condaenvs/flexidot/lib/python3.13/site-packages/flexidot/plotting.py", line 1070, in polydotplot
    feat_dict = read_gffs(
        gff_files,
    ...<3 lines>...
        filetype=filetype,
    )
  File "/Users/adamtaranto/.condaenvs/flexidot/lib/python3.13/site-packages/flexidot/utils/file_handling.py", line 350, in read_gffs
    legend_figure(
    ~~~~~~~~~~~~~^
        colors=colors,
        ^^^^^^^^^^^^^^
    ...<6 lines>...
        filetype=filetype,
        ^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/adamtaranto/.condaenvs/flexidot/lib/python3.13/site-packages/flexidot/utils/file_handling.py", line 761, in legend_figure
    ax[1].legend(
    ~~~~~~~~~~~~^
        ncol=((len(colors) - 1) // max_legend_length_row) + 1, framealpha=1
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )  # vertical legend
    ^
  File "/Users/adamtaranto/.condaenvs/flexidot/lib/python3.13/site-packages/matplotlib/axes/_axes.py", line 337, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
                   ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/adamtaranto/.condaenvs/flexidot/lib/python3.13/site-packages/matplotlib/legend.py", line 550, in __init__
    self._init_legend_box(handles, labels, markerfirst)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/adamtaranto/.condaenvs/flexidot/lib/python3.13/site-packages/matplotlib/legend.py", line 907, in _init_legend_box
    len, np.array_split(handles_and_labels, self._ncols)):
         ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/adamtaranto/.condaenvs/flexidot/lib/python3.13/site-packages/numpy/lib/_shape_base_impl.py", line 782, in array_split
    raise ValueError('number sections must be larger than 0.') from None
ValueError: number sections must be larger than 0.
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits January 28, 2026 13:24
Co-authored-by: Adamtaranto <2160099+Adamtaranto@users.noreply.github.com>
Co-authored-by: Adamtaranto <2160099+Adamtaranto@users.noreply.github.com>
Co-authored-by: Adamtaranto <2160099+Adamtaranto@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix error handling for empty GFF input Handle empty GFF and alignment files gracefully Jan 28, 2026
Copilot AI requested a review from Adamtaranto January 28, 2026 13:31
@Adamtaranto Adamtaranto marked this pull request as ready for review January 29, 2026 02:42
@Adamtaranto Adamtaranto merged commit 2414fd3 into master Jan 29, 2026
@Adamtaranto Adamtaranto deleted the copilot/fix-empty-gff-error-handling branch January 29, 2026 02:50
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

Successfully merging this pull request may close these issues.

Error if --gff is empty

2 participants