def parse(cls, string: str) -> Lipid: """ Parse a string represntation of a lipid and create Lipid class. :param string: The input string. :return: An instance of Lipid """ # TODO refactor, too much code repeated in the two if blocks # identify abbreviation type if '(' in string and ')' in string: string = string.strip() if not string.endswith(')'): raise TypeError(f"Cannot parse abbreviation {string}") lipid_class_name = string.split('(', 1)[0] # second part of split at first ( is residue string, add leading ( again! residue_string = '(' + string.split('(', 1)[1] lipidclass = LipidClass.parse(lipid_class_name) residuelist = ResidueList.parse(residue_string) lipid = cls(lipidclass, residuelist) lipid._input = string return lipid # CE 22:4;0 elif ' ' in string: lipid_class_name, residue_string = string.split(' ', 1) lipidclass = LipidClass.parse(lipid_class_name) residuelist = ResidueList.parse(residue_string) lipid = cls(lipidclass, residuelist) lipid._input = string return lipid else: lipid = Lipid(LipidClass(string)) lipid._input = string return lipid
def test_lipidclass_preferred_class_mapping(): assert LipidClass.parse('TAG').name == 'TG' assert LipidClass.parse('DAG').name == 'DG'