Coverage for model_workflow/tools/generate_lipid_references.py: 72%

29 statements  

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

1from model_workflow.utils.auxiliar import save_json 

2 

3from model_workflow.tools.get_inchi_keys import get_inchi_keys, is_in_swiss_lipids, is_in_LIPID_MAPS 

4from model_workflow.utils.type_hints import * 

5from model_workflow.utils.warnings import warn 

6 

7 

8def generate_lipid_references(structure: 'Structure', 

9 universe: 'Universe', 

10 output_filepath : str, 

11 ) -> List[dict]: 

12 """Generate the lipid references.""" 

13 # Patch case where there no internet 

14 try: 

15 # This would return a ConnectionError 

16 is_in_swiss_lipids('test') 

17 except: 

18 # Then we map the lipids/membrane 

19 warn('There was a problem connecting to the SwissLipids database.') 

20 return None 

21 

22 if not universe.universe.atoms.charges.any(): # AGUS: he añadido .any() porque el error me lo indicaba, pero también me sugería .all() , no sé cuál encaja mejor 

23 print('Topology file does not have charges, cannot generate lipid references.') 

24 return save_json([], output_filepath) 

25 

26 # Get InChI keys of non-proteic/non-nucleic residues 

27 inchi_keys = get_inchi_keys(universe, structure) 

28 

29 lipid_references = [] 

30 for inchikey, res_data in inchi_keys.items(): 

31 SL_data = is_in_swiss_lipids(inchikey) 

32 LM_data = is_in_LIPID_MAPS(inchikey) 

33 

34 # We don't use lipid data for now, if we have it it is present in LIPID MAPS 

35 if SL_data or LM_data: 

36 lipid_references.append({'inchikey': inchikey, 

37 'resname': list(res_data['resname'])[0], 

38 'resindices': list(map(int, res_data['resindices'])), 

39 'indices': structure.select_residue_indices(res_data['resindices']).to_ngl(), 

40 'swisslipids': SL_data, 

41 'lipidmaps': LM_data, 

42 }) 

43 cls = res_data['classification'] 

44 # If the residue is a lipid, we check if it is classified as fatty 

45 if all('fatty' not in classes for classes in cls): 

46 warn(f'The InChIKey {inchikey} of {str(res_data["resname"])} is not ' 

47 f'classified as fatty {cls} but it is a lipid') 

48 

49 # Glucolipids are saved separately to solve a problem with FATSLiM later (ex: A01IR) 

50 if type(cls) == list and all(len(classes) > 1 for classes in cls): 

51 lipid_references[-1]['resgroups'] = list(map(int, res_data['resgroups'])) 

52 

53 else: 

54 # If the InChIKey is not in SwissLipids or LIPID MAPS, we check if it is classified as fatty 

55 if any('fatty' in classes for classes in res_data['classification']): 

56 warn(f'The InChIKey {inchikey} of {str(res_data["resname"])} is ' 

57 f'classified as fatty but is not a lipid.\n' 

58 f'Resindices: {str(res_data["resindices"])}') 

59 

60 save_json(lipid_references, output_filepath) 

61 return lipid_references