Coverage for binette/contig_manager.py: 100%
11 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-06 19:22 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-06 19:22 +0000
1import pyfastx
2from typing import Dict, Iterable, Tuple, Set, Any, Union
5def parse_fasta_file(fasta_file: str, index_file: str) -> pyfastx.Fasta:
6 """
7 Parse a FASTA file and return a pyfastx.Fasta object.
9 :param fasta_file: The path to the FASTA file.
11 :return: A pyfastx.Fasta object representing the parsed FASTA file.
12 """
13 fa = pyfastx.Fasta(fasta_file, build_index=True, index_file=index_file)
14 return fa
17def make_contig_index(contigs: Set[str]) -> Tuple[Dict[str, int], Dict[int, str]]:
18 """
19 Create an index mapping for contigs.
21 :param contigs: A list of contig names.
23 :return: A tuple containing the contig index mapping dictionaries (contig_to_index, index_to_contig).
24 """
25 contig_to_index = {contig: index for index, contig in enumerate(contigs)}
26 index_to_contig = {index: contig for contig, index in contig_to_index.items()}
27 return contig_to_index, index_to_contig
30def apply_contig_index(
31 contig_to_index: Dict[str, int], contig_to_info: Dict[str, Any]
32) -> Dict[int, Union[Any, Iterable[Any]]]:
33 """
34 Apply the contig index mapping to the contig info dictionary.
36 :param contig_to_index: A dictionary mapping contig names to their corresponding index.
37 :param contig_to_info: A dictionary mapping contig names to their associated information.
39 :return: A dictionary mapping contig indices to their associated information.
40 """
41 return {contig_to_index[contig]: info for contig, info in contig_to_info.items()}