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