File: Symmetry.cpp

package info (click to toggle)
pymol 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 42,288 kB
  • sloc: cpp: 476,472; python: 76,538; ansic: 29,510; javascript: 6,792; sh: 47; makefile: 24
file content (205 lines) | stat: -rw-r--r-- 5,559 bytes parent folder | download
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/
#include"os_predef.h"
#include"os_python.h"
#include"os_std.h"

#include"Err.h"
#include"Feedback.h"
#include"Symmetry.h"
#include"P.h"
#include"PConv.h"
#ifdef SYM_TO_MAT_LIST_IN_C
#endif

PyObject *SymmetryAsPyList(CSymmetry * I)
{
  PyObject *result = NULL;

  if(I) {
    result = PyList_New(2);
    PyList_SetItem(result, 0, CrystalAsPyList(&I->Crystal));
    PyList_SetItem(result, 1, PyString_FromString(I->spaceGroup()));
  }
  return (PConvAutoNone(result));
}

static int SymmetryFromPyList(CSymmetry * I, PyObject * list)
{
  auto G = I->G;
  int ok = true;
  ov_size ll;
  PyObject *secondval;

  if(ok)
    ok = (I != NULL);
  if(ok)
    ok = (list != NULL);
  if(ok)
    ok = PyList_Check(list);
  if(ok)
    ll = PyList_Size(list);
  if (ok && ll>=2){
    secondval = PyList_GetItem(list, 1);
    if (PyList_Check(secondval)){
      /* if only the crystal, read it */
      if(ok)
	ok = CrystalFromPyList(&I->Crystal, list);    
    } else {
      if(ok){
	CPythonVal *val = CPythonVal_PyList_GetItem(I->G, list, 0);
	ok = CrystalFromPyList(&I->Crystal, val);
	CPythonVal_Free(val);
      }
      if(ok) {
        std::string sg;
        ok = PConvFromPyListItem(G, list, 1, sg);
        I->setSpaceGroup(sg.c_str());
      }
    }
    CPythonVal_Free(secondval);
  }
  /* TO SUPPORT BACKWARDS COMPATIBILITY...
     Always check ll when adding new PyList_GetItem's */
  return (ok);
}

CSymmetry *SymmetryNewFromPyList(PyMOLGlobals * G, PyObject * list)
{
  CSymmetry *I = NULL;
  I = new CSymmetry(G);
  if(I) {
    if(!SymmetryFromPyList(I, list)) {
      delete I;
      I = NULL;
    }
  }
  return (I);
}

#ifndef _PYMOL_NOPY
#ifdef _PYMOL_XRAY
static void SymmetryDump44f(PyMOLGlobals * G, const float *m, const char *prefix)
{
  if(prefix) {
    PRINTF "%s %12.5f %12.5f %12.5f %12.5f\n", prefix, m[0], m[1], m[2], m[3] ENDF(G);
    PRINTF "%s %12.5f %12.5f %12.5f %12.5f\n", prefix, m[4], m[5], m[6], m[7] ENDF(G);
    PRINTF "%s %12.5f %12.5f %12.5f %12.5f\n", prefix, m[8], m[9], m[10], m[11] ENDF(G);
    PRINTF "%s %12.5f %12.5f %12.5f %12.5f\n", prefix, m[12], m[13], m[14], m[15] ENDF(G);
  } else {
    PRINTF "%12.5f %12.5f %12.5f %12.5f\n", m[0], m[1], m[2], m[3] ENDF(G);
    PRINTF "%12.5f %12.5f %12.5f %12.5f\n", m[4], m[5], m[6], m[7] ENDF(G);
    PRINTF "%12.5f %12.5f %12.5f %12.5f\n", m[8], m[9], m[10], m[11] ENDF(G);
    PRINTF "%12.5f %12.5f %12.5f %12.5f\n", m[12], m[13], m[14], m[15] ENDF(G);
  }
}
#endif
#endif

/**
 * Lookup the symmetry operations by space group symbol (from Python with
 * pymol.xray) and populate SymMatVLA.
 *
 * Return false if space group unknown.
 */
bool CSymmetry::updateSymMatVLA() const
{
  auto I = const_cast<CSymmetry*>(this);

  constexpr bool quiet = false;

  if (I->SymMatVLA) {
    // don't re-run unless setSpaceGroup() was called
    return true;
  }

  int ok = false;
#ifdef SYM_TO_MAT_LIST_IN_C
#else
#ifndef _PYMOL_NOPY
#ifdef _PYMOL_XRAY
  /* TAKEN OUT BB 2/2012  SpaceGroup can be blank, 
     sg_sym_to_mat_list has a blank entry with no operations
  if(!I->SpaceGroup[0]) {
    ErrMessage(G, "Symmetry", "Missing space group symbol");
    } else */
  if(P_xray) {
    int blocked = PAutoBlock(G);
    ov_size a, l;
    PyObject *mats;
    mats = PYOBJECT_CALLMETHOD(P_xray, "sg_sym_to_mat_list", "s", spaceGroup());
    if(mats && (mats != Py_None)) {
      l = PyList_Size(mats);
      I->SymMatVLA = pymol::vla<float>(16*l);
      if(!quiet) {
        PRINTFB(G, FB_Symmetry, FB_Details)
        " Symmetry: Found %d symmetry operators.\n", (int) l ENDFB(G);
      }
      for(a = 0; a < l; a++) {
        PConv44PyListTo44f(PyList_GetItem(mats, a), I->SymMatVLA + (a * 16));
        if(!quiet) {
          if(Feedback(G, FB_Symmetry, FB_Blather)) {
            SymmetryDump44f(G, I->SymMatVLA + (a * 16), " Symmetry:");
          }
        }
      }
      ok = true;
      Py_DECREF(mats);
    } else {
      ErrMessage(G, "Symmetry", "Unable to get matrices.");
    }
    PAutoUnblock(G, blocked);
  }
#endif
#endif
#endif

  return (ok);
}

void CSymmetry::setSpaceGroup(const char* sg)
{
  strncpy(SpaceGroup, sg, sizeof(SpaceGroup) - 1);

  SymMatVLA.freeP();
}

int CSymmetry::getNSymMat() const {
  if (!updateSymMatVLA())
    return 0;
  if (!SymMatVLA)
    return 0;
  return SymMatVLA.size() / 16;
}

/**
 * Register a a space group with symmetry operations (if not already registered)
 *
 * @param sg Space group symbol, e.g. "P 1"
 * @param sym_op List of symmetry operations, e.g. ["x,y,z", "-x,-y,z"]
 */
void SymmetrySpaceGroupRegister(PyMOLGlobals * G, const char* sg, const std::vector<std::string>& sym_op) {
#if !defined(_PYMOL_NOPY) && defined(_PYMOL_XRAY)
  if (!P_xray)
    return;

  int blocked = PAutoBlock(G);
  PYOBJECT_CALLMETHOD(P_xray,
      "sg_register_if_unknown", "sN", sg, PConvToPyObject(sym_op));
  PAutoUnblock(G, blocked);
#endif
}