corems.molecular_formula.input.masslist_ref
1__author__ = "Yuri E. Corilo" 2__date__ = "Oct 24, 2019" 3 4import json 5import re 6import sys 7from pathlib import Path 8from typing import List 9 10from corems.encapsulation.constant import Atoms, Labels 11from corems.molecular_formula.factory.MolecularFormulaFactory import MolecularFormula 12 13 14class MolecularFormulaLinkProxy: 15 """Proxy class for MolecularFormulaLink to be used in the molecular formula ref file import 16 17 Parameters 18 ---------- 19 molecular_formula : MolecularFormula 20 corems MolecularFormula object 21 mz : float 22 target m/z 23 24 Attributes 25 ---------- 26 C : int 27 number of carbon atoms 28 H : int 29 number of hydrogen atoms 30 H_C : float 31 ratio of hydrogen to carbon atoms 32 class_label : str 33 molecular formula class label 34 mz_calc : float 35 calculated m/z 36 dbe : int 37 double bond equivalent 38 formula_dict : dict 39 molecular formula dictionary 40 41 Methods 42 ------- 43 * to_dict(). 44 return molecular formula dictionary 45 46 """ 47 48 def __init__(self, molecular_formula, mz): 49 self.C = molecular_formula.get("C") 50 self.H = molecular_formula.get("H") 51 self.H_C = molecular_formula.get("H") / molecular_formula.get("C") 52 self.class_label = json.dumps(molecular_formula.class_dict) 53 self.mz_calc = float(mz) 54 self.dbe = molecular_formula.dbe 55 self.formula_dict = molecular_formula.to_dict() 56 57 def to_dict(self): 58 return self.formula_dict 59 60 61class ImportMassListRef: # Thread 62 """Import Mass List from Reference File 63 64 Parameters 65 ---------- 66 ref_file_location : str 67 path to the reference file 68 69 Attributes 70 ---------- 71 ref_file_location : str 72 path to the reference file 73 74 Methods 75 ------- 76 * molecular_formula_ref(mz, molecular_formula). 77 Return MolecularFormulaLinkProxy object 78 * from_lcms_lib_file(ion_charge, ion_types). 79 Unsupported stub; raises NotImplementedError 80 * from_bruker_ref_file(). 81 Return List[MolecularFormula] from Bruker reference file 82 * from_corems_ref_file(delimiter). 83 Return List[MolecularFormula] from CoreMS reference file 84 * split(delimiters, string, maxsplit). 85 Splits a string using a list of delimiters. 86 * mformula_s_to_dict(s_mformulatring, iontype). 87 Converts a molecular formula string to a dict 88 """ 89 90 def __init__(self, ref_file_location): 91 # Thread.__init__(self) 92 93 self.ref_file_location = Path(ref_file_location) 94 95 if not self.ref_file_location.exists(): 96 tb = sys.exc_info()[2] 97 raise FileNotFoundError(ref_file_location).with_traceback(tb) 98 99 def molecular_formula_ref(self, mz, molecular_formula): 100 """Instantiate a MolecularFormulaLinkProxy object 101 102 Parameters 103 ---------- 104 mz : float 105 target m/z 106 molecular_formula : MolecularFormula 107 corems MolecularFormula object 108 109 Returns 110 ------- 111 MolecularFormulaLinkProxy 112 MolecularFormulaLinkProxy object 113 """ 114 return MolecularFormulaLinkProxy(molecular_formula, mz) 115 116 def from_lcms_lib_file(self, ion_charge: float, ion_types: List[str]): 117 """Unsupported LC-MS library reference import. 118 119 Previously returned library-linked formula objects. That path is no 120 longer supported; use combinatorial / SQL molecular formula search and 121 :class:`~corems.molecular_formula.factory.MolecularFormulaFactory.MolecularFormula` 122 instead. This method will be removed in the next major release. 123 124 Parameters 125 ---------- 126 ion_charge : float 127 ion charge (ignored) 128 ion_types : List[str] 129 list of ion types (ignored) 130 131 Raises 132 ------ 133 NotImplementedError 134 Always; the LC-MS library formula import path is removed. 135 """ 136 raise NotImplementedError( 137 "ImportMassListRef.from_lcms_lib_file is no longer supported. " 138 "Use MolecularFormula with the combinatorial / SQL formula search path. " 139 "This stub will be removed in the next major release." 140 ) 141 142 def from_bruker_ref_file(self) -> List[MolecularFormula]: 143 """Create a list of MolecularFormula objects from Bruker reference file 144 145 Returns 146 ------- 147 List[MolecularFormula] 148 List of MolecularFormula objects from Bruker reference file 149 """ 150 151 152 list_mf_obj = [] 153 154 with open(self.ref_file_location) as ref_f: 155 labels = ref_f.readline().strip("\n").split(";") 156 157 for line in ref_f.readlines(): 158 if line != "\n": 159 list_ref = line.strip("\n").split(" ") 160 161 if list_ref[2][-1] == "+": 162 ion_charge = int(list_ref[2][:-1]) 163 164 else: 165 ion_charge = -1 * int(list_ref[2][:-1]) 166 167 ion_mol_formula = list_ref[0] 168 mz = float(list_ref[1]) 169 formula_dict = self.mformula_s_to_dict(ion_mol_formula) 170 171 list_mf_obj.append( 172 MolecularFormula(formula_dict, ion_charge, external_mz=mz) 173 ) 174 175 return list_mf_obj 176 177 def from_corems_ref_file(self, delimiter="\t"): # pragma: no cover 178 """Create a list of MolecularFormula objects from CoreMS reference file 179 180 Not being used 181 182 Parameters 183 ---------- 184 delimiter : str 185 delimiter used in the reference file 186 187 Returns 188 ------- 189 List[MolecularFormula] 190 List of MolecularFormula objects from CoreMS reference file 191 """ 192 # not being used 193 194 list_mf_obj = [] 195 196 with open("res/RefMassLists/Crude-Pos-ESI.ref") as ref_f: 197 labels = ref_f.readline().strip("\n").split(delimiter) 198 199 for line in ref_f.readlines(): 200 if line != "\n": 201 list_ref = line.strip("\n").split(delimiter) 202 203 formula_string = list_ref[0] 204 ion_charge = int(list_ref[1]) 205 ion_type = list_ref[2] 206 207 molform = MolecularFormula( 208 formula_string, ion_charge, ion_type=ion_type 209 ) 210 211 list_mf_obj.append(self.molecular_formula_ref(molform)) 212 213 return list_mf_obj 214 215 def split(self, delimiters, string, maxsplit=0): # pragma: no cover 216 """Splits a string using a list of delimiters. 217 218 Does not work when formula has atoms with same characters, i.e - C10H21NNa 219 220 Parameters 221 ---------- 222 delimiters : list 223 list of delimiters 224 string : str 225 string to be split 226 maxsplit : int, optional 227 maximum number of splits. Default is 0 228 229 Returns 230 ------- 231 list 232 list of strings obtained after splitting the string 233 list 234 list of counts obtained after splitting the string 235 """ 236 regexPattern = "|".join(map(re.escape, delimiters)) # pragma: no cover 237 isotopes = re.findall(regexPattern, string) # pragma: no cover 238 counts = re.split(regexPattern, string, maxsplit) # pragma: no cover 239 return isotopes, counts 240 241 def mformula_s_to_dict(self, s_mformulatring, iontype="unknown"): 242 """Converts a molecular formula string to a dict 243 244 Parameters 245 ---------- 246 s_mformulatring : str 247 molecular formula string, i.e. 'C10H21NNa' 248 iontype : str, optional 249 ion type. Default is 'unknown' 250 251 Returns 252 ------- 253 dict 254 molecular formula dictionary 255 256 Notes 257 ----- 258 Does not work if the atomic mass number is passed i.e. 37Cl, 81Br, convention follow the light isotope labeling 35Cl is Cl, 12C is C, etc. 259 If you need to use heavy isotopes please use another reference file format that separate the formula string by a blank space and parse it using the function corems_ref_file 260 261 Raises 262 ------ 263 TypeError 264 Atom does not exist in Atoms.atoms_order list 265 Exception 266 Empty molecular formula 267 """ 268 if s_mformulatring: 269 # find the case C122 270 all_atoms = re.findall(r"[A-Z]{1}[0-9]{1,10000}", s_mformulatring) 271 272 # find the case Br2 273 all_atoms2 = re.findall(r"[A-Z]{1}[a-z]{1}[0-9]{1,10000}", s_mformulatring) 274 # find the case N 275 single_digit_atoms_one = re.findall( 276 r"[A-Z]{1}(?![0-9])(?![a-z])", s_mformulatring 277 ) 278 # print(single_digit_atoms_one) 279 # find the case Na 280 due_digit_atoms_one = re.findall( 281 r"[A-Z]{1}[a-z]{1}(?![0-9])", s_mformulatring 282 ) 283 284 all_atoms = ( 285 all_atoms + all_atoms2 + due_digit_atoms_one + single_digit_atoms_one 286 ) 287 288 dict_res = {} 289 290 for each_atom_count in all_atoms: 291 count = re.findall(r"[0-9]{1,10000}", each_atom_count) 292 atom = "".join(re.findall(r"[A-z]", each_atom_count)) 293 294 if atom in Atoms.atoms_order: 295 if count: 296 dict_res[atom] = int(count[0]) 297 else: 298 dict_res[atom] = 1 299 300 else: 301 tb = sys.exc_info()[2] 302 raise TypeError( 303 "Atom %s does not exist in Atoms.atoms_order list" % atom 304 ).with_traceback(tb) 305 306 dict_res[Labels.ion_type] = iontype 307 308 return dict_res 309 310 else: 311 tb = sys.exc_info()[2] 312 raise Exception("Empty molecular formula").with_traceback(tb)
15class MolecularFormulaLinkProxy: 16 """Proxy class for MolecularFormulaLink to be used in the molecular formula ref file import 17 18 Parameters 19 ---------- 20 molecular_formula : MolecularFormula 21 corems MolecularFormula object 22 mz : float 23 target m/z 24 25 Attributes 26 ---------- 27 C : int 28 number of carbon atoms 29 H : int 30 number of hydrogen atoms 31 H_C : float 32 ratio of hydrogen to carbon atoms 33 class_label : str 34 molecular formula class label 35 mz_calc : float 36 calculated m/z 37 dbe : int 38 double bond equivalent 39 formula_dict : dict 40 molecular formula dictionary 41 42 Methods 43 ------- 44 * to_dict(). 45 return molecular formula dictionary 46 47 """ 48 49 def __init__(self, molecular_formula, mz): 50 self.C = molecular_formula.get("C") 51 self.H = molecular_formula.get("H") 52 self.H_C = molecular_formula.get("H") / molecular_formula.get("C") 53 self.class_label = json.dumps(molecular_formula.class_dict) 54 self.mz_calc = float(mz) 55 self.dbe = molecular_formula.dbe 56 self.formula_dict = molecular_formula.to_dict() 57 58 def to_dict(self): 59 return self.formula_dict
Proxy class for MolecularFormulaLink to be used in the molecular formula ref file import
Parameters
- molecular_formula (MolecularFormula): corems MolecularFormula object
- mz (float): target m/z
Attributes
- C (int): number of carbon atoms
- H (int): number of hydrogen atoms
- H_C (float): ratio of hydrogen to carbon atoms
- class_label (str): molecular formula class label
- mz_calc (float): calculated m/z
- dbe (int): double bond equivalent
- formula_dict (dict): molecular formula dictionary
Methods
- to_dict(). return molecular formula dictionary
49 def __init__(self, molecular_formula, mz): 50 self.C = molecular_formula.get("C") 51 self.H = molecular_formula.get("H") 52 self.H_C = molecular_formula.get("H") / molecular_formula.get("C") 53 self.class_label = json.dumps(molecular_formula.class_dict) 54 self.mz_calc = float(mz) 55 self.dbe = molecular_formula.dbe 56 self.formula_dict = molecular_formula.to_dict()
62class ImportMassListRef: # Thread 63 """Import Mass List from Reference File 64 65 Parameters 66 ---------- 67 ref_file_location : str 68 path to the reference file 69 70 Attributes 71 ---------- 72 ref_file_location : str 73 path to the reference file 74 75 Methods 76 ------- 77 * molecular_formula_ref(mz, molecular_formula). 78 Return MolecularFormulaLinkProxy object 79 * from_lcms_lib_file(ion_charge, ion_types). 80 Unsupported stub; raises NotImplementedError 81 * from_bruker_ref_file(). 82 Return List[MolecularFormula] from Bruker reference file 83 * from_corems_ref_file(delimiter). 84 Return List[MolecularFormula] from CoreMS reference file 85 * split(delimiters, string, maxsplit). 86 Splits a string using a list of delimiters. 87 * mformula_s_to_dict(s_mformulatring, iontype). 88 Converts a molecular formula string to a dict 89 """ 90 91 def __init__(self, ref_file_location): 92 # Thread.__init__(self) 93 94 self.ref_file_location = Path(ref_file_location) 95 96 if not self.ref_file_location.exists(): 97 tb = sys.exc_info()[2] 98 raise FileNotFoundError(ref_file_location).with_traceback(tb) 99 100 def molecular_formula_ref(self, mz, molecular_formula): 101 """Instantiate a MolecularFormulaLinkProxy object 102 103 Parameters 104 ---------- 105 mz : float 106 target m/z 107 molecular_formula : MolecularFormula 108 corems MolecularFormula object 109 110 Returns 111 ------- 112 MolecularFormulaLinkProxy 113 MolecularFormulaLinkProxy object 114 """ 115 return MolecularFormulaLinkProxy(molecular_formula, mz) 116 117 def from_lcms_lib_file(self, ion_charge: float, ion_types: List[str]): 118 """Unsupported LC-MS library reference import. 119 120 Previously returned library-linked formula objects. That path is no 121 longer supported; use combinatorial / SQL molecular formula search and 122 :class:`~corems.molecular_formula.factory.MolecularFormulaFactory.MolecularFormula` 123 instead. This method will be removed in the next major release. 124 125 Parameters 126 ---------- 127 ion_charge : float 128 ion charge (ignored) 129 ion_types : List[str] 130 list of ion types (ignored) 131 132 Raises 133 ------ 134 NotImplementedError 135 Always; the LC-MS library formula import path is removed. 136 """ 137 raise NotImplementedError( 138 "ImportMassListRef.from_lcms_lib_file is no longer supported. " 139 "Use MolecularFormula with the combinatorial / SQL formula search path. " 140 "This stub will be removed in the next major release." 141 ) 142 143 def from_bruker_ref_file(self) -> List[MolecularFormula]: 144 """Create a list of MolecularFormula objects from Bruker reference file 145 146 Returns 147 ------- 148 List[MolecularFormula] 149 List of MolecularFormula objects from Bruker reference file 150 """ 151 152 153 list_mf_obj = [] 154 155 with open(self.ref_file_location) as ref_f: 156 labels = ref_f.readline().strip("\n").split(";") 157 158 for line in ref_f.readlines(): 159 if line != "\n": 160 list_ref = line.strip("\n").split(" ") 161 162 if list_ref[2][-1] == "+": 163 ion_charge = int(list_ref[2][:-1]) 164 165 else: 166 ion_charge = -1 * int(list_ref[2][:-1]) 167 168 ion_mol_formula = list_ref[0] 169 mz = float(list_ref[1]) 170 formula_dict = self.mformula_s_to_dict(ion_mol_formula) 171 172 list_mf_obj.append( 173 MolecularFormula(formula_dict, ion_charge, external_mz=mz) 174 ) 175 176 return list_mf_obj 177 178 def from_corems_ref_file(self, delimiter="\t"): # pragma: no cover 179 """Create a list of MolecularFormula objects from CoreMS reference file 180 181 Not being used 182 183 Parameters 184 ---------- 185 delimiter : str 186 delimiter used in the reference file 187 188 Returns 189 ------- 190 List[MolecularFormula] 191 List of MolecularFormula objects from CoreMS reference file 192 """ 193 # not being used 194 195 list_mf_obj = [] 196 197 with open("res/RefMassLists/Crude-Pos-ESI.ref") as ref_f: 198 labels = ref_f.readline().strip("\n").split(delimiter) 199 200 for line in ref_f.readlines(): 201 if line != "\n": 202 list_ref = line.strip("\n").split(delimiter) 203 204 formula_string = list_ref[0] 205 ion_charge = int(list_ref[1]) 206 ion_type = list_ref[2] 207 208 molform = MolecularFormula( 209 formula_string, ion_charge, ion_type=ion_type 210 ) 211 212 list_mf_obj.append(self.molecular_formula_ref(molform)) 213 214 return list_mf_obj 215 216 def split(self, delimiters, string, maxsplit=0): # pragma: no cover 217 """Splits a string using a list of delimiters. 218 219 Does not work when formula has atoms with same characters, i.e - C10H21NNa 220 221 Parameters 222 ---------- 223 delimiters : list 224 list of delimiters 225 string : str 226 string to be split 227 maxsplit : int, optional 228 maximum number of splits. Default is 0 229 230 Returns 231 ------- 232 list 233 list of strings obtained after splitting the string 234 list 235 list of counts obtained after splitting the string 236 """ 237 regexPattern = "|".join(map(re.escape, delimiters)) # pragma: no cover 238 isotopes = re.findall(regexPattern, string) # pragma: no cover 239 counts = re.split(regexPattern, string, maxsplit) # pragma: no cover 240 return isotopes, counts 241 242 def mformula_s_to_dict(self, s_mformulatring, iontype="unknown"): 243 """Converts a molecular formula string to a dict 244 245 Parameters 246 ---------- 247 s_mformulatring : str 248 molecular formula string, i.e. 'C10H21NNa' 249 iontype : str, optional 250 ion type. Default is 'unknown' 251 252 Returns 253 ------- 254 dict 255 molecular formula dictionary 256 257 Notes 258 ----- 259 Does not work if the atomic mass number is passed i.e. 37Cl, 81Br, convention follow the light isotope labeling 35Cl is Cl, 12C is C, etc. 260 If you need to use heavy isotopes please use another reference file format that separate the formula string by a blank space and parse it using the function corems_ref_file 261 262 Raises 263 ------ 264 TypeError 265 Atom does not exist in Atoms.atoms_order list 266 Exception 267 Empty molecular formula 268 """ 269 if s_mformulatring: 270 # find the case C122 271 all_atoms = re.findall(r"[A-Z]{1}[0-9]{1,10000}", s_mformulatring) 272 273 # find the case Br2 274 all_atoms2 = re.findall(r"[A-Z]{1}[a-z]{1}[0-9]{1,10000}", s_mformulatring) 275 # find the case N 276 single_digit_atoms_one = re.findall( 277 r"[A-Z]{1}(?![0-9])(?![a-z])", s_mformulatring 278 ) 279 # print(single_digit_atoms_one) 280 # find the case Na 281 due_digit_atoms_one = re.findall( 282 r"[A-Z]{1}[a-z]{1}(?![0-9])", s_mformulatring 283 ) 284 285 all_atoms = ( 286 all_atoms + all_atoms2 + due_digit_atoms_one + single_digit_atoms_one 287 ) 288 289 dict_res = {} 290 291 for each_atom_count in all_atoms: 292 count = re.findall(r"[0-9]{1,10000}", each_atom_count) 293 atom = "".join(re.findall(r"[A-z]", each_atom_count)) 294 295 if atom in Atoms.atoms_order: 296 if count: 297 dict_res[atom] = int(count[0]) 298 else: 299 dict_res[atom] = 1 300 301 else: 302 tb = sys.exc_info()[2] 303 raise TypeError( 304 "Atom %s does not exist in Atoms.atoms_order list" % atom 305 ).with_traceback(tb) 306 307 dict_res[Labels.ion_type] = iontype 308 309 return dict_res 310 311 else: 312 tb = sys.exc_info()[2] 313 raise Exception("Empty molecular formula").with_traceback(tb)
Import Mass List from Reference File
Parameters
- ref_file_location (str): path to the reference file
Attributes
- ref_file_location (str): path to the reference file
Methods
- molecular_formula_ref(mz, molecular_formula). Return MolecularFormulaLinkProxy object
- from_lcms_lib_file(ion_charge, ion_types). Unsupported stub; raises NotImplementedError
- from_bruker_ref_file(). Return List[MolecularFormula] from Bruker reference file
- from_corems_ref_file(delimiter). Return List[MolecularFormula] from CoreMS reference file
- split(delimiters, string, maxsplit). Splits a string using a list of delimiters.
- mformula_s_to_dict(s_mformulatring, iontype). Converts a molecular formula string to a dict
100 def molecular_formula_ref(self, mz, molecular_formula): 101 """Instantiate a MolecularFormulaLinkProxy object 102 103 Parameters 104 ---------- 105 mz : float 106 target m/z 107 molecular_formula : MolecularFormula 108 corems MolecularFormula object 109 110 Returns 111 ------- 112 MolecularFormulaLinkProxy 113 MolecularFormulaLinkProxy object 114 """ 115 return MolecularFormulaLinkProxy(molecular_formula, mz)
Instantiate a MolecularFormulaLinkProxy object
Parameters
- mz (float): target m/z
- molecular_formula (MolecularFormula): corems MolecularFormula object
Returns
- MolecularFormulaLinkProxy: MolecularFormulaLinkProxy object
117 def from_lcms_lib_file(self, ion_charge: float, ion_types: List[str]): 118 """Unsupported LC-MS library reference import. 119 120 Previously returned library-linked formula objects. That path is no 121 longer supported; use combinatorial / SQL molecular formula search and 122 :class:`~corems.molecular_formula.factory.MolecularFormulaFactory.MolecularFormula` 123 instead. This method will be removed in the next major release. 124 125 Parameters 126 ---------- 127 ion_charge : float 128 ion charge (ignored) 129 ion_types : List[str] 130 list of ion types (ignored) 131 132 Raises 133 ------ 134 NotImplementedError 135 Always; the LC-MS library formula import path is removed. 136 """ 137 raise NotImplementedError( 138 "ImportMassListRef.from_lcms_lib_file is no longer supported. " 139 "Use MolecularFormula with the combinatorial / SQL formula search path. " 140 "This stub will be removed in the next major release." 141 )
Unsupported LC-MS library reference import.
Previously returned library-linked formula objects. That path is no
longer supported; use combinatorial / SQL molecular formula search and
~corems.molecular_formula.factory.MolecularFormulaFactory.MolecularFormula
instead. This method will be removed in the next major release.
Parameters
- ion_charge (float): ion charge (ignored)
- ion_types (List[str]): list of ion types (ignored)
Raises
- NotImplementedError: Always; the LC-MS library formula import path is removed.
143 def from_bruker_ref_file(self) -> List[MolecularFormula]: 144 """Create a list of MolecularFormula objects from Bruker reference file 145 146 Returns 147 ------- 148 List[MolecularFormula] 149 List of MolecularFormula objects from Bruker reference file 150 """ 151 152 153 list_mf_obj = [] 154 155 with open(self.ref_file_location) as ref_f: 156 labels = ref_f.readline().strip("\n").split(";") 157 158 for line in ref_f.readlines(): 159 if line != "\n": 160 list_ref = line.strip("\n").split(" ") 161 162 if list_ref[2][-1] == "+": 163 ion_charge = int(list_ref[2][:-1]) 164 165 else: 166 ion_charge = -1 * int(list_ref[2][:-1]) 167 168 ion_mol_formula = list_ref[0] 169 mz = float(list_ref[1]) 170 formula_dict = self.mformula_s_to_dict(ion_mol_formula) 171 172 list_mf_obj.append( 173 MolecularFormula(formula_dict, ion_charge, external_mz=mz) 174 ) 175 176 return list_mf_obj
Create a list of MolecularFormula objects from Bruker reference file
Returns
- List[MolecularFormula]: List of MolecularFormula objects from Bruker reference file
178 def from_corems_ref_file(self, delimiter="\t"): # pragma: no cover 179 """Create a list of MolecularFormula objects from CoreMS reference file 180 181 Not being used 182 183 Parameters 184 ---------- 185 delimiter : str 186 delimiter used in the reference file 187 188 Returns 189 ------- 190 List[MolecularFormula] 191 List of MolecularFormula objects from CoreMS reference file 192 """ 193 # not being used 194 195 list_mf_obj = [] 196 197 with open("res/RefMassLists/Crude-Pos-ESI.ref") as ref_f: 198 labels = ref_f.readline().strip("\n").split(delimiter) 199 200 for line in ref_f.readlines(): 201 if line != "\n": 202 list_ref = line.strip("\n").split(delimiter) 203 204 formula_string = list_ref[0] 205 ion_charge = int(list_ref[1]) 206 ion_type = list_ref[2] 207 208 molform = MolecularFormula( 209 formula_string, ion_charge, ion_type=ion_type 210 ) 211 212 list_mf_obj.append(self.molecular_formula_ref(molform)) 213 214 return list_mf_obj
Create a list of MolecularFormula objects from CoreMS reference file
Not being used
Parameters
- delimiter (str): delimiter used in the reference file
Returns
- List[MolecularFormula]: List of MolecularFormula objects from CoreMS reference file
216 def split(self, delimiters, string, maxsplit=0): # pragma: no cover 217 """Splits a string using a list of delimiters. 218 219 Does not work when formula has atoms with same characters, i.e - C10H21NNa 220 221 Parameters 222 ---------- 223 delimiters : list 224 list of delimiters 225 string : str 226 string to be split 227 maxsplit : int, optional 228 maximum number of splits. Default is 0 229 230 Returns 231 ------- 232 list 233 list of strings obtained after splitting the string 234 list 235 list of counts obtained after splitting the string 236 """ 237 regexPattern = "|".join(map(re.escape, delimiters)) # pragma: no cover 238 isotopes = re.findall(regexPattern, string) # pragma: no cover 239 counts = re.split(regexPattern, string, maxsplit) # pragma: no cover 240 return isotopes, counts
Splits a string using a list of delimiters.
Does not work when formula has atoms with same characters, i.e - C10H21NNa
Parameters
- delimiters (list): list of delimiters
- string (str): string to be split
- maxsplit (int, optional): maximum number of splits. Default is 0
Returns
- list: list of strings obtained after splitting the string
- list: list of counts obtained after splitting the string
242 def mformula_s_to_dict(self, s_mformulatring, iontype="unknown"): 243 """Converts a molecular formula string to a dict 244 245 Parameters 246 ---------- 247 s_mformulatring : str 248 molecular formula string, i.e. 'C10H21NNa' 249 iontype : str, optional 250 ion type. Default is 'unknown' 251 252 Returns 253 ------- 254 dict 255 molecular formula dictionary 256 257 Notes 258 ----- 259 Does not work if the atomic mass number is passed i.e. 37Cl, 81Br, convention follow the light isotope labeling 35Cl is Cl, 12C is C, etc. 260 If you need to use heavy isotopes please use another reference file format that separate the formula string by a blank space and parse it using the function corems_ref_file 261 262 Raises 263 ------ 264 TypeError 265 Atom does not exist in Atoms.atoms_order list 266 Exception 267 Empty molecular formula 268 """ 269 if s_mformulatring: 270 # find the case C122 271 all_atoms = re.findall(r"[A-Z]{1}[0-9]{1,10000}", s_mformulatring) 272 273 # find the case Br2 274 all_atoms2 = re.findall(r"[A-Z]{1}[a-z]{1}[0-9]{1,10000}", s_mformulatring) 275 # find the case N 276 single_digit_atoms_one = re.findall( 277 r"[A-Z]{1}(?![0-9])(?![a-z])", s_mformulatring 278 ) 279 # print(single_digit_atoms_one) 280 # find the case Na 281 due_digit_atoms_one = re.findall( 282 r"[A-Z]{1}[a-z]{1}(?![0-9])", s_mformulatring 283 ) 284 285 all_atoms = ( 286 all_atoms + all_atoms2 + due_digit_atoms_one + single_digit_atoms_one 287 ) 288 289 dict_res = {} 290 291 for each_atom_count in all_atoms: 292 count = re.findall(r"[0-9]{1,10000}", each_atom_count) 293 atom = "".join(re.findall(r"[A-z]", each_atom_count)) 294 295 if atom in Atoms.atoms_order: 296 if count: 297 dict_res[atom] = int(count[0]) 298 else: 299 dict_res[atom] = 1 300 301 else: 302 tb = sys.exc_info()[2] 303 raise TypeError( 304 "Atom %s does not exist in Atoms.atoms_order list" % atom 305 ).with_traceback(tb) 306 307 dict_res[Labels.ion_type] = iontype 308 309 return dict_res 310 311 else: 312 tb = sys.exc_info()[2] 313 raise Exception("Empty molecular formula").with_traceback(tb)
Converts a molecular formula string to a dict
Parameters
- s_mformulatring (str): molecular formula string, i.e. 'C10H21NNa'
- iontype (str, optional): ion type. Default is 'unknown'
Returns
- dict: molecular formula dictionary
Notes
Does not work if the atomic mass number is passed i.e. 37Cl, 81Br, convention follow the light isotope labeling 35Cl is Cl, 12C is C, etc. If you need to use heavy isotopes please use another reference file format that separate the formula string by a blank space and parse it using the function corems_ref_file
Raises
- TypeError: Atom does not exist in Atoms.atoms_order list
- Exception: Empty molecular formula