Coverage for binette/contig_manager.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-14 14:36 +0000

1from collections.abc import Iterable 

2from typing import Any 

3 

4import pyfastx 

5 

6 

7def parse_fasta_file(fasta_file: str, index_file: str) -> pyfastx.Fasta: 

8 """ 

9 Parse a FASTA file and return a pyfastx.Fasta object. 

10 

11 :param fasta_file: The path to the FASTA file. 

12 

13 :return: A pyfastx.Fasta object representing the parsed FASTA file. 

14 """ 

15 fa = pyfastx.Fasta(fasta_file, build_index=True, index_file=index_file) 

16 return fa 

17 

18 

19def make_contig_index(contigs: set[str]) -> dict[str, int]: 

20 """ 

21 Create an index mapping for contigs. 

22 

23 :param contigs: A list of contig names. 

24 

25 :return: A tuple containing the contig index mapping dictionaries (contig_to_index, index_to_contig). 

26 """ 

27 contig_to_index = {contig: index for index, contig in enumerate(contigs)} 

28 return contig_to_index 

29 

30 

31def apply_contig_index( 

32 contig_to_index: dict[str, int], contig_to_info: dict[str, Any] 

33) -> dict[int, Any | Iterable[Any]]: 

34 """ 

35 Apply the contig index mapping to the contig info dictionary. 

36 

37 :param contig_to_index: A dictionary mapping contig names to their corresponding index. 

38 :param contig_to_info: A dictionary mapping contig names to their associated information. 

39 

40 :return: A dictionary mapping contig indices to their associated information. 

41 """ 

42 return {contig_to_index[contig]: info for contig, info in contig_to_info.items()}