Coverage for biobb_pmx/pmxbiobb/common.py: 61%

31 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-23 10:10 +0000

1"""Common functions for package biobb_pmx.pmx""" 

2 

3import re 

4from typing import Iterable, Mapping, Optional, Union 

5 

6MUTATION_DICT = { 

7 "ALA": "A", 

8 "ARG": "R", 

9 "ASN": "N", 

10 "ASP": "D", 

11 "ASPH": "B", 

12 "ASPP": "B", 

13 "ASH": "B", 

14 "CYS": "C", 

15 "CYS2": "C", 

16 "CYN": "C", 

17 "CYX": "CX", 

18 "CYM": "CM", 

19 "CYSH": "C", 

20 "GLU": "E", 

21 "GLUH": "J", 

22 "GLUP": "J", 

23 "GLH": "J", 

24 "GLN": "Q", 

25 "GLY": "G", 

26 "HIS": "H", 

27 "HIE": "X", 

28 "HISE": "X", 

29 "HSE": "X", 

30 "HIP": "Z", 

31 "HSP": "Z", 

32 "HISH": "Z", 

33 "HID": "H", 

34 "HSD": "H", 

35 "ILE": "I", 

36 "LEU": "L", 

37 "LYS": "K", 

38 "LYSH": "K", 

39 "LYP": "K", 

40 "LYN": "O", 

41 "LSN": "O", 

42 "MET": "M", 

43 "PHE": "F", 

44 "PRO": "P", 

45 "SER": "S", 

46 "SP1": "SP1", 

47 "SP2": "SP2", 

48 "THR": "T", 

49 "TRP": "W", 

50 "TYR": "Y", 

51 "VAL": "V", 

52 "A": "A", 

53 "T": "T", 

54 "C": "C", 

55 "G": "G", 

56 "U": "U", 

57} 

58 

59 

60def create_mutations_file( 

61 input_mutations_path: str, 

62 mutation_list: Union[str, Iterable[str]], 

63 mutation_dict: Mapping, 

64) -> str: 

65 try: 

66 # Check if self.mutation_list is a string 

67 mutation_list = mutation_list.replace(" ", "").split(",") # type: ignore 

68 except AttributeError: 

69 pass 

70 pattern = re.compile(r"(?P<chain>[a-zA-Z])*:?(?P<resnum>\d+)(?P<mt>[a-zA-Z0-9]+)") 

71 with open(input_mutations_path, "w") as mut_file: 

72 for mut in mutation_list: 

73 match = pattern.match(mut.strip()) 

74 if match: 

75 mut_groups_dict = match.groupdict() 

76 if mut_groups_dict.get("chain"): 

77 mut_file.write(mut_groups_dict.get("chain", "") + " ") 

78 mut_file.write(mut_groups_dict.get("resnum", "") + " ") 

79 if mut_groups_dict.get("mt", "").upper() not in mutation_dict.keys(): 

80 raise TypeError( 

81 f"{mut_groups_dict.get('mt','').upper()} is not a valid AA code or NA code. Possible values are {mutation_dict.keys()}" 

82 ) 

83 mut_file.write(mutation_dict[mut_groups_dict.get("mt", "").upper()]) 

84 mut_file.write("\n") 

85 

86 return input_mutations_path 

87 

88 

89# TODO: Move this function to biobb_common.tools.file_utils 

90def _from_string_to_list(input_data: Optional[Union[str, list[str]]]) -> list[str]: 

91 """ 

92 Converts a string to a list, splitting by commas or spaces. If the input is already a list, returns it as is. 

93 Returns an empty list if input_data is None. 

94 

95 Parameters: 

96 input_data (str, list, or None): The string, list, or None value to convert. 

97 

98 Returns: 

99 list: A list of string elements or an empty list if input_data is None. 

100 """ 

101 if input_data is None: 

102 return [] 

103 

104 if isinstance(input_data, list): 

105 # If input is already a list, return it 

106 return input_data 

107 

108 # If input is a string, determine the delimiter based on presence of commas 

109 delimiter = "," if "," in input_data else " " 

110 items = input_data.split(delimiter) 

111 

112 # Remove whitespace from each item and ignore empty strings 

113 processed_items = [item.strip() for item in items if item.strip()] 

114 

115 return processed_items