{{ message }}
Repository navigation
Conversation
FASTA sequence files had no lexer at all. The header line is marked with the sequence identifier as a tag and the free-form description as a string, the sequence data as constants, gaps in a low-contrast comment color, and the stop codon marker as a keyword.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What
Fixes #3019 — adds a lexer for FASTA sequence files, which Pygments had no support for at all (there is no
bio/fastamodule in the tree).A FASTA file holds one or more sequences. Every sequence starts with a header line beginning with
>that carries the sequence identifier and an optional free-form description; the sequence itself follows on the next lines, wrapped at an arbitrary width. The newFastaLexermarks:>marker as punctuation, the sequence identifier as a tag, and the description as a string-and.) in a low-contrast comment color, digits as integers, and the*stop-codon marker as a keywordIt is registered under the alias
fastaand the usual extensions (*.fa,*.fasta,*.fas,*.faa,*.fna,*.ffn,*.frn,*.pep,*.seq).analyse_textrecognizes a FASTA file by its header line followed by sequence-only lines, soguess_lexerworks on content as well as on file names.A note on the colouring
The original request also asked for per-residue palettes — one for amino acids and one for nucleotides. I did not implement that part, and I would rather explain why than guess: a per-residue palette needs roughly 24 residue-specific token types, and token types that are not part of the standard set are not styled by the common themes, so in practice they would all render as plain text rather than as a palette.
Correction to the above, after measuring instead of assuming: that statement holds for custom token types, but I had not checked the other route — reusing existing token types as palette slots. Reading the token→style mapping of the stock themes (pygments 2.21.0, 30 candidate existing types) yields 26 distinct colours under
defaultandfriendly, 24 underemacs, 23 underpastie, 15 undervim, 11 undersolarized-dark, 9 undermonokaiand 4 underbw— so 20 standard residues do fit inside the palette the common themes hand out. The price of that route is that it borrows token types whose meaning has nothing to do with residues, and it flattens on themes with a smaller palette.That leaves three shapes: a grouped/semantic palette (what this change does), per-residue custom token types (clean taxonomy, but invisible until someone ships a style), and per-residue reuse of existing token types (per-residue identity under stock themes, at the price of the borrowed taxonomy). @Artoria2e5 has asked for the per-residue variant, so I am glad to rework this change into whichever of the two per-residue shapes is preferred — I did not want to swap the implementation silently, since the taxonomy trade-off is a maintainer-level call. If the maintainers want that, the sensible route is to agree on a small set of standard token types to reuse for the residue groups (and, if needed, to teach the popular themes about them), and I would happily add that on top of this change.
Tests
tests/examplefiles/fasta/fasta.fasta(+ generated golden) covers several records, a long wrapped sequence, a header with pipes and a description, gaps, lowercase residues, ambiguity codes and a stop codon.Verified locally:
pytest -q --ignore=tests/contrast— 5335 passed, 16 skippedpytest tests/examplefiles -k fasta— 1 passedruff check --ignore UP031 .,scripts/check_crlf.py,scripts/detect_missing_analyse_text.py --skip-no-aliases,scripts/check_sources.py,scripts/count_token_references.py— all cleanErrortokens, including for CRLF files, files with a loneCR, an empty file, a header without a sequence, and several records in one fileAI usage disclosure: the lexer and its tests were prepared with AI assistance; all test results above were produced locally and reviewed.