Skip to content

Add support for terminal_columns in Alignment formatting, issue 5035#5070

Open
data2code wants to merge 10 commits intobiopython:masterfrom
data2code:data2code
Open

Add support for terminal_columns in Alignment formatting, issue 5035#5070
data2code wants to merge 10 commits intobiopython:masterfrom
data2code:data2code

Conversation

@data2code
Copy link

  • [ X] I hereby agree to dual licence this and any previous contributions under both
    the Biopython License Agreement AND the BSD 3-Clause License.

  • [ X] I have read the CONTRIBUTING.rst file, have run pre-commit
    locally, and understand that continuous integration checks will be used to
    confirm the Biopython unit tests and style checks pass with these changes.

  • I have added my name to the alphabetical contributors listings in the files
    NEWS.rst and CONTRIB.rst as part of this pull request, am listed
    already, or do not wish to be listed. (This acknowledgement is optional.)

Closes #5035

Modify Bio.Align.init.py to support terminal_columns. If not specified, use all available terminal columns to show the alignment, otherwise, use user-specified value. Add Tests/test_Align_Alignment_format.py.

@data2code data2code requested a review from mdehoon as a code owner September 19, 2025 20:22
Fix the syntax for assert in test_Align_Alignment_format.py
@mdehoon
Copy link
Contributor

mdehoon commented Sep 20, 2025

@data2code Can you have a look at the failing checks?

@data2code
Copy link
Author

I don't know what's wrong. I ran "pytest Tests/test_Align_Alignment_format.py" and it was clean.

@mdehoon
Copy link
Contributor

mdehoon commented Sep 22, 2025

@data2code Have a look at the failing checks as shown on this web page.
All tests must pass, not just the new test in test_Align_Alignment_format.py.

@data2code
Copy link
Author

I believe I have fixed the style issue in the test script. The error currently is in the main code.
https://ci.appveyor.com/project/biopython/biopython/builds/52789718

I don't feel comfortable to add this new attribute to the Alignment class, as I am unfamiliar with the BioPython code base.
I am not sure I am the right person to create this PR.

@peterjc
Copy link
Member

peterjc commented Sep 23, 2025

The errors look like a problem not creating an attribute when the object is created? eg

File "C:\Py\Lib\site-packages\Bio\Align\__init__.py", line 2305, in _format_pretty
    if self.terminal_columns is None:
       ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Alignment' object has no attribute 'terminal_columns'

Black is still not happy - this could be the version of black:

black....................................................................Failed
- hook id: black
- exit code: 1

would reformat Tests/test_Align_Alignment_format.py

We still use black 24.10.0 for trailing whitespace in doctests.

@mdehoon
Copy link
Contributor

mdehoon commented Sep 23, 2025

The errors look like a problem not creating an attribute when the object is created? eg

File "C:\Py\Lib\site-packages\Bio\Align_init_.py", line 2305, in _format_pretty
> if self.terminal_columns is None:
> ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Alignment' object has no attribute 'terminal_columns'

The Alignment class needs a class variable terminal_columns that is None by default.

@data2code
Copy link
Author

Not sure what I can do here. The Pre-commit.ci gives failures without providing me the accurate feedback.

@mdehoon
Copy link
Contributor

mdehoon commented Sep 24, 2025

@data2code Check the failing tests as shown on this web page, where it says "4 failing checks"

@data2code
Copy link
Author

@mdehoon I only see failure flags without details. I am not a biopython developer; instead, I am a user who made a feature request. I am afraid I have done what I can with the issue. Could someone from the developer team help take over? Thanks.

@mdehoon
Copy link
Contributor

mdehoon commented Sep 24, 2025

@data2code

Could someone from the developer team help take over?

There is no developer team. We are all just volunteers.

What happens if you click on the first failure (where it says "CI / style (pull_request)") ?

@data2code
Copy link
Author

2025-09-24_07-38-39 This is all I see. An error without useful details.

@peterjc
Copy link
Member

peterjc commented Sep 24, 2025

Two files are not following the automated style checking using the tool black, which is part of the pre-commit checks we expect you te run locally. See https://github.com/biopython/biopython/blob/master/CONTRIBUTING.rst

@peterjc
Copy link
Member

peterjc commented Sep 24, 2025

The main tests are looking better though, from AppVeyor which runs the tests on Windows:

======================================================================
ERROR: test_Align_Alignment_format
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\projects\biopython\Tests\run_tests.py", line 266, in runTest
    raise RuntimeError(f"No tests found in {name}")
RuntimeError: No tests found in test_Align_Alignment_format
----------------------------------------------------------------------
Ran 501 tests in 576.243 seconds

import sys
import types
import warnings
import shutil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would ideally be a few lines up (alphabetical sorting of standard library imports).

position_width = 10
line_width = 80

# support custom column size, see issue 5035
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the comment is redundant and could be removed.

@@ -0,0 +1,40 @@
"""Tests for controling the number of terminal columns
Issue #5035
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not formatted in the expected style, but there are bigger issues with your test.

# 0 -||||||...||||--|...|--|...||..|-|....|..|.|--|--|||.||.|-------|--------------|.||.|---|-||---------||-------------||.|.|.|.||-. 129
# query 0 DVQLQESGPGLVKP--SQSQSLTCTVTGYSITSDYAWNWIRQFP--GNKLEWMGYMS-------Y--------------SGSTRY---NPSLRSRISITRDTSKNQFFLQLKSVTTEDTATYFCARG-W 100
#
assert (len(str(best_alignment).split("\n")) == 4), "Expecting alignment to be output as one group."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have not defined any tests using the unittest framework, thus the error No tests found in test_Align_Alignment_format.

See https://biopython.org/docs/latest/Tutorial/chapter_testing.html

Also I think this need not be an entire new file.

@data2code
Copy link
Author

@peterjc Thank you, that's very helpful.

@data2code
Copy link
Author

@peterjc The test looks good on my local:

$ python Tests/test_Align_Alignment_format.py
test_terminal_columns (main.TestAlignmentFormat.test_terminal_columns) ... ok


Ran 1 test in 0.001s

OK

Any pointers for what to work on next? Thank you!

@peterjc
Copy link
Member

peterjc commented Sep 24, 2025

Fix the black style. Move the test into an appropriate existing Bio.Align test file. At that point we can ask GitHub Actions to run the full test suite (and see if the Circle CI apparent timeout is related to your changes or not).

[I have not considered the value of the change itself yet, as others seem to have.]

@data2code
Copy link
Author

I believe the test error was not due to the code I added to the test file. The error was triggered by other tests in the same file.

@codecov
Copy link

codecov bot commented Sep 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.93%. Comparing base (be97b2d) to head (9fba345).
⚠️ Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5070      +/-   ##
==========================================
- Coverage   85.44%   77.93%   -7.52%     
==========================================
  Files         286      282       -4     
  Lines       59908    59430     -478     
==========================================
- Hits        51189    46316    -4873     
- Misses       8719    13114    +4395     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

position_width = 10
line_width = 80

if not hasattr(self, "terminal_columns") or self.terminal_columns is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to have a class variable terminal_columns on the Alignment class.

@data2code
Copy link
Author

@mdehoon Updated. Thanks for the suggestion.

@mdehoon
Copy link
Contributor

mdehoon commented Sep 28, 2025

@data2code
Copy link
Author

Okay, move it to a class attribute. Thanks.

# 120 .|.|.||-. 129
# query 92 TYFCARG-W 100
#
self.assertEqual(len(str(best_alignment).split("\n")), 12)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just test str(best_alignment) explicitly (using triple quotes)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this test should not be checking the exact alignment itself, but instead focus on the row count. The exact alginment might be altered in the future due small changes in alignment algorithm or the naming of the "target", "query" or other small formatting changes. The exact alignment should be checked by other test cases, while this test focuses on row counts. I hope it makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If at some point some bug is introduced that causes the printed alignment to be incorrect, then your test may not capture it, if it only checks the number of lines.

The algorithm by definition generates the optimal alignment. Even if there are any changes to the algorithm, the generated optimal alignment will still be the same.

@data2code
Copy link
Author

@mdehoon You have a valid point in believing the algorithm won't change, however, the formatting itself can change. E.g., someone may decided instead of using query and target, print Q and T instead. Then this test will break. Anyway, this test is to check the alignment layout, not the exact content of the alignment itself. There are other tests that change the validity of the content of the alignment. I think it's best for this test to focus on the formatting not the content. If it is okay with everyone, we should close this thread. I have learned a lot from this PR, thank you and Peter for your patience! Really appreciate all the guidance and feedback.

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.

Bio.Align.PairwiseAligner missing format_alignment in Bio.pairwise2

3 participants