1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 from warnings import warn
26
27
28 from lib.structure.mass import centre_of_mass
29 from lib.warnings import RelaxWarning
30 from pipe_control.structure.checks import check_structure
31
32
33 -def pipe_centre_of_mass(atom_id=None, model=None, return_mass=False, verbosity=1, missing_error=True):
34 """Calculate and return the centre of mass of the structures in the current data pipe.
35
36 @keyword atom_id: The molecule, residue, and atom identifier string. Only atoms matching this selection will be used.
37 @type atom_id: str or None
38 @keyword model: Only use a specific model.
39 @type model: int or None
40 @keyword return_mass: A flag which if False will cause only the centre of mass to be returned, but if True will cause the centre of mass and the mass itself to be returned as a tuple.
41 @type return_mass: bool
42 @keyword verbosity: The amount of text to print out. 0 results in no printouts, 1 the full amount.
43 @type verbosity: int
44 @keyword missing_error: A flag which if True will cause an error to be raised if structural data is absent. Otherwise if False, a warning will be given and the CoM of [0, 0, 0] will be returned.
45 @type missing_error: bool
46 @return: The centre of mass vector, and additionally the mass.
47 @rtype: list of 3 floats (or tuple of a list of 3 floats and one float)
48 """
49
50
51 if missing_error:
52 check_structure()
53 else:
54 if not check_structure(escalate=1):
55 return [0.0, 0.0, 0.0]
56
57
58 selection = cdp.structure.selection(atom_id=atom_id)
59
60
61 coord = []
62 element_list = []
63 for mol_name, res_num, res_name, atom_num, atom_name, element, pos in cdp.structure.atom_loop(selection=selection, model_num=model, mol_name_flag=True, res_num_flag=True, res_name_flag=True, atom_num_flag=True, atom_name_flag=True, element_flag=True, pos_flag=True, ave=True):
64
65 id = ''
66
67
68 if mol_name != None:
69 id = id + '#' + mol_name
70
71
72 if res_num != None:
73 id = id + ':' + repr(res_num)
74
75
76 if atom_num != None:
77 id = id + '@' + repr(atom_num)
78
79
80 if element == None:
81 warn(RelaxWarning("Skipping the atom '%s' as the element type cannot be determined." % id))
82 continue
83
84
85 coord.append(pos)
86 element_list.append(element)
87
88
89 com, mass = centre_of_mass(pos=coord, elements=element_list, verbosity=verbosity)
90
91
92 if return_mass:
93 return com, mass
94 else:
95 return com
96