Coverage for src/python/ensembl/vep/corpus/synthetic/components/sequence.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-18 14:41 +0000

1# See the NOTICE file distributed with this work for additional information 

2# regarding copyright ownership. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14"""Build synthetic sequences.""" 

15 

16from __future__ import annotations 

17 

18import random 

19 

20from ensembl.vep.common.constants import CANONICAL_DNA_BASES 

21 

22__all__ = ["fill_locus_sequence", "random_sequence"] 

23 

24 

25def random_sequence(length: int, random_generator: random.Random) -> str: 

26 """Return a random nucleotide sequence. 

27 

28 Args: 

29 length (int): Number of bases to generate. 

30 random_generator (random.Random): Seeded random generator, for reproducibility. 

31 

32 Returns: 

33 str: A random sequence of ``ACGT`` bases. 

34 """ 

35 return "".join(random_generator.choice(CANONICAL_DNA_BASES) for _ in range(length)) 

36 

37 

38def fill_locus_sequence( 

39 chromosome_chars: list[str], 

40 start: int, 

41 end: int, 

42 random_generator: random.Random, 

43 *, 

44 bases: str = "ACG", 

45) -> None: 

46 """Fill an inclusive locus with bases drawn from ``bases``. 

47 

48 The default alphabet cannot produce a stop codon in either strand or reading frame. Passing 

49 ``ACGT`` produces an unconstrained reference sequence while retaining the same reusable 

50 coordinate handling. 

51 

52 Args: 

53 chromosome_chars (list[str]): Mutable whole-chromosome sequence. 

54 start (int): One-based inclusive locus start. 

55 end (int): One-based inclusive locus end. 

56 random_generator (random.Random): Seeded random generator. 

57 bases (str): Non-empty alphabet to draw from. 

58 

59 Raises: 

60 ValueError: If the locus is outside the chromosome or ``bases`` is empty. 

61 """ 

62 if start < 1 or end < start or end > len(chromosome_chars): 

63 raise ValueError(f"locus {start}-{end} is outside a chromosome of {len(chromosome_chars)} bases") 

64 if not bases: 

65 raise ValueError("locus sequence alphabet must not be empty") 

66 for position in range(start, end + 1): 

67 chromosome_chars[position - 1] = random_generator.choice(bases)