def test_lipid_abbreviation(): l = Lipid(LipidClass('CE'), ResidueList([Residue(16, 2)])) assert l.abbreviation() == 'CE(16:2)' l = Lipid(LipidClass('CE'), ResidueList([Residue(16, 2), Residue(18, 1)])) assert l.abbreviation() == 'CE(16:2/18:1)' assert l.abbreviation(summed=True) == 'CE(34:3)' l = Lipid( LipidClass('CE'), ResidueList([ Residue(16, 2, modification=ResidueModification('O-')), Residue(18, 1) ])) assert l.abbreviation() == 'CE(O-16:2/18:1)' assert l.abbreviation(summed=True) == 'CE(O-34:3)' # no residues, assure that abbreviation/summed fails with AttributeError l = Lipid(LipidClass('CE')) assert l.abbreviation() == 'CE' assert l.abbreviation(summed=True) == 'CE'
def some_lipids_plain_list(): """ A handful of well formatted lipids, using all annotation elements. Return as Python list, not as LipidList Object """ # lists of names to draw from lipidclass_names = ['CE', 'Cer', 'TG'] modifications = ['d', 'O-', None, None] lipidlist = [] for i in range(10): lipidclass = LipidClass(random.choice(lipidclass_names)) num_of_residues = random.choice([1, 2, 3]) residuelist = ResidueList() for i in range(0, num_of_residues): residuelist.residues.append( Residue(random.randint(6, 50), random.randint(0, 10), modification=ResidueModification( random.choice(modifications)))) lipidlist.append(Lipid(lipidclass, residuelist)) return lipidlist
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_lipid_check_consistency(): l = Lipid(LipidClass('Cer'), ResidueList([Residue(16, 2), Residue(18, 1)])) # check inconsistent state assert l.residueslist[0].modification == None l.check_consistency() # check consistent state assert l.residueslist[0].modification == CLASS_DEFAULT_MODIFICATION['Cer']
def test_lipidclass_preferred_class_mapping(): assert LipidClass.parse('TAG').name == 'TG' assert LipidClass.parse('DAG').name == 'DG'