liveBook · Manning

Telomere

As explained in Regular Expression Puzzles and AI Coding Assistants

A telomere is a specialized repetitive DNA sequence at the end of a chromosome or other DNA sequence.

A DNA string ends with a long run of TTAGGG. Where does its protein-encoding portion stop? For this simplified FASTA task, a telomere is the repeat TTAGGG at a sequence end; three or more consecutive repeats mark the terminus. The desired result is every preceding nucleotide, but none of the terminal repeats. section 2.13

The trap is that a sequence can contain plenty of plausible DNA before a nearly telomeric tail. A single altered base among trailing repeats invalidates the match, as does an N unknown nucleotide anywhere in the proposed encoding region. Valid input therefore contains only C, A, T, and G before a complete terminal run of at least three TTAGGG units. section 2.13

A boundary-aware regular expression

Use ^([CATG]+?)(?=(TTAGGG){3,}$). The initial anchor prevents matching merely a valid suffix. [CATG]+? captures the shortest DNA prefix that permits the lookahead to see three or more TTAGGG repeats through the end. The lookahead checks the terminus without consuming it, so the captured group is the encoding region rather than encoding-plus-telomeres. section 2.14

>>> from textwrap import wrap
>>> print('\n'.join(wrap(valid, 60)))
CCCTGAATAATCAAGGTCACAGACCAGTTAGAATGGTTTAGTGTGGAAAGCGGGAAACGA
AAAGCCTCTCTGAATCCTGCGCACCGAGATTCTCCCAAGGCAAGGCGAGGGGCTGTATTG
CAGGGTTCAACTGCAGCGTCGCAACTCAAATGCAGCATTCCTAATGCACACATGACACCC
AAAATATAACAGACATATTACTCATGGAGGGTGAGGGTGAGGGTGAGGGTTAGGGTTAGG
GTTAGGGTTAGGGTTAGGGTTAGGGTTAGGGTTAGGGTTAGGGTTAGGG

Inspect the candidate sequence: its final TTAGGG run is visually long enough to be a terminator, while the desired extraction ends immediately before that run. section 2.13

A pattern that merely looks plausible can still fail the actual boundary test:

  • Do not omit ^. Without it, a malformed prefix can be skipped and a valid-looking tail can still match. section 2.14
  • Do not greedily capture the coding bases. A greedy prefix can swallow telomere units that should be excluded. section 2.14
  • Do not accept imperfect repeats or unknown bases. A tail with altered TTAGGG units, or a proposed region containing N, is not a match. section 2.13
  • Do not mistake a generated solution for a verified one. One proposed pattern uses TTAGGG(?:TTAGGG)+$, which requires only two repeats and can return telomere sequence as part of the encoding region. section 2.15

The larger lesson is pleasantly unforgiving: “ends in repeats” is not enough. State the count, require an unbroken terminus, constrain the allowed prefix alphabet, and decide whether the terminator belongs in the result. Then test the almost-right strings—not only the cheerful successful one. section 2.13 section 2.15

How Regular Expression Puzzles and AI Coding Assistants covers it

How deep it goes
Works through an anchored, non-greedy regular expression and boundary failures without modeling full FASTA semantics.
Examples
Tests a valid DNA sequence against malformed telomere repeats and an N unknown-nucleotide case.
What it assumes
Assumes familiarity with DNA base symbols while defining the simplified FASTA symbols and telomere rule.
What you can do after
Apply it via Python regular expressions to extract prefixes only when a strict terminal TTAGGG repeat run qualifies.
How it connects
Connects telomeres with FASTA, regular expressions, quantifiers, lookahead, anchors, and non-greedy matching.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Regular Expression Puzzles and AI Coding Assistants. The section links point to the parts of the book it draws on.