forked from nlichtenberg/OpenGL_Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProteinLoader.cpp
More file actions
280 lines (232 loc) · 8.07 KB
/
ProteinLoader.cpp
File metadata and controls
280 lines (232 loc) · 8.07 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//============================================================================
// Distributed under the MIT License. Author: Adrian Derstroff
//============================================================================
#include "ProteinLoader.h"
ProteinLoader::ProteinLoader()
{
m_currentProteinIdx = 0;
}
ProteinLoader::~ProteinLoader()
{
for (int i = 0; i < m_proteins.size(); i++) {
delete m_proteins.at(i);
}
m_proteins.clear();
}
int ProteinLoader::getNumberOfProteins()
{
return m_proteins.size();
}
std::vector<SimpleProtein*> ProteinLoader::getProteins()
{
return m_proteins;
}
SimpleProtein* ProteinLoader::getProteinAt(int i)
{
return m_proteins.at(i);
}
void ProteinLoader::updateAtoms()
{
m_allAtoms.clear();
for (int i = 0; i < getNumberOfProteins(); i++) {
SimpleProtein* protein = getProteinAt(i);
m_allAtoms.insert(std::end(m_allAtoms), std::begin(protein->atoms), std::end(protein->atoms));
}
}
std::vector<SimpleAtom> ProteinLoader::getAllAtoms()
{
updateAtoms();
return m_allAtoms;
}
int ProteinLoader::getNumberOfAllAtoms()
{
return m_allAtoms.size();
}
/*
* LOADING PROTEIN
*/
SimpleProtein* ProteinLoader::loadProtein(std::string fileName)
{
/*
* extracting protein name from file name
*/
std::string proteinName = fileName;
int lastSlash = proteinName.find_last_of("/");
if (lastSlash >= 0) {
proteinName = proteinName.substr(lastSlash+1, proteinName.size());
}
int lastDot = proteinName.find_last_of(".");
if (lastDot >= 0) {
proteinName = proteinName.substr(0, lastDot);
}
/*
* concatenate full path
*/
std::string subfolder = "/molecules/";
std::string filePath = RESOURCES_PATH + subfolder + fileName;
/*
* load protein from pdb file
*/
SimpleProtein* protein = new SimpleProtein;
protein->name = proteinName;
loadPDB(filePath, *protein, protein->bbMin, protein->bbMax);
m_proteins.push_back(protein);
return m_proteins.at(m_proteins.size()-1);
}
void ProteinLoader::loadPDB(std::string filePath, SimpleProtein &protein, glm::vec3 &minPosition, glm::vec3 &maxPosition)
{
/*
* set start values of min and max position
*/
double min = std::numeric_limits<double>::min();
double max = std::numeric_limits<double>::max();
minPosition = glm::vec3(max, max, max);
maxPosition = glm::vec3(min, min, min);
float maxRadius = 0;
/*
* the informations we need to create our atoms
*/
std::vector<std::string> names;
std::vector<std::string> elementNames;
std::vector<glm::vec3> positions;
std::vector<float> radii;
/*
* init mdtraj wrapper and load pdb file as pyobject
*/
MdTrajWrapper mdTrajWrapper;
PyObject* pdbFile = mdTrajWrapper.loadFilePDB(filePath);
/*
* Atom positions
*/
PyObject* xyz_py = mdTrajWrapper.getXYZ(pdbFile);
PyObject* topo = mdTrajWrapper.getTopology(pdbFile);
Py_DECREF(pdbFile);
PyArrayObject* xyz_pyarray = reinterpret_cast<PyArrayObject*>(xyz_py);
//get number of frames, atoms and components
long long numAtom{ PyArray_SHAPE(xyz_pyarray)[1] };
long long numComponents{ PyArray_SHAPE(xyz_pyarray)[2] };
int numAtoms = (int)numAtom;
// cast the 3D numpy array to a 1D c array
float* xyz_carray;
xyz_carray = reinterpret_cast<float*>(PyArray_DATA(xyz_pyarray));
float positionX;
float positionY;
float positionZ;
std::vector<glm::vec3> frameHolder;
for (int a = 0; a < numAtoms; a++)
{
int id = a * numComponents;
positionX = xyz_carray[id] * 10;
positionY = xyz_carray[id + 1] * 10;
positionZ = xyz_carray[id + 2] * 10;
glm::vec3 position(positionX, positionY, positionZ);
positions.push_back(position);
/*
* get min and max
*/
minPosition.x = (positionX < minPosition.x) ? positionX : minPosition.x;
minPosition.y = (positionY < minPosition.y) ? positionY : minPosition.y;
minPosition.z = (positionZ < minPosition.z) ? positionZ : minPosition.z;
maxPosition.x = (positionX > maxPosition.x) ? positionX : maxPosition.x;
maxPosition.y = (positionY > maxPosition.y) ? positionY : maxPosition.y;
maxPosition.z = (positionZ > maxPosition.z) ? positionZ : maxPosition.z;
}
Py_DECREF(xyz_py);
/*
* Atom properties
*/
PyObject* atom_py = PyObject_GetAttrString(topo, "atoms");
PyObject* atom_iterator_py = PyObject_GetIter(atom_py);
Py_DECREF(atom_py);
PyObject* atom;
PyObject* name_py;
PyObject* element_py;
PyObject* element_name_py;
PyObject* atom_radius_py;
atom = PyIter_Next(atom_iterator_py);
while ((atom != NULL)) {
name_py = PyObject_GetAttrString(atom, "name");
element_py = PyObject_GetAttrString(atom, "element");
element_name_py = PyObject_GetAttrString(element_py, "name");
atom_radius_py = PyObject_GetAttrString(element_py, "radius");
names.push_back(PyUnicode_AsUTF8(name_py));
elementNames.push_back(PyUnicode_AsUTF8(element_name_py));
double radius = PyFloat_AsDouble(atom_radius_py) * 10;
radii.push_back(radius);
/*
* find the atom with the biggest radius
*/
maxRadius = std::max((float)radius, maxRadius);
Py_DECREF(name_py);
Py_DECREF(element_py);
Py_DECREF(element_name_py);
Py_DECREF(atom_radius_py);
atom = PyIter_Next(atom_iterator_py);
}
Py_DECREF(atom_iterator_py);
/*
* extent the bounding box by the radius of the biggest atom
*/
minPosition -= maxRadius;
maxPosition += maxRadius;
/*
* Create atoms and add them to the protein
* and to the allAtoms vector
*/
if ((positions.size() == names.size()) && (positions.size() == elementNames.size()) && (positions.size() == radii.size())) {
for (int i = 0; i < names.size(); i++) {
std::string name = names.at(i);
std::string elementName = elementNames.at(i);
glm::vec3 position = positions.at(i);
float radius = radii.at(i);
/*
* create atom
*/
SimpleAtom atom;
atom.pos = position;
atom.radius = radius;
atom.proteinID = glm::vec4(m_currentProteinIdx, m_currentProteinIdx, m_currentProteinIdx, m_currentProteinIdx);
/*
* add atom to both protein and all atoms
*/
protein.atoms.push_back(atom);
m_allAtoms.push_back(atom);
}
} else {
std::cerr << "Size of atom positions " << positions.size() << " and properties " << names.size() << ", " << elementNames.size() << ", " << radii.size() << " dont match" << std::endl;
}
/*
* increment protein idx
*/
m_currentProteinIdx++;
}
void ProteinLoader::getBoundingBoxAroundProteins(glm::vec3& min, glm::vec3& max)
{
if (m_proteins.size() > 0) {
min = glm::vec3(FLOAT_MAX, FLOAT_MAX, FLOAT_MAX);
max = glm::vec3(FLOAT_MIN, FLOAT_MIN, FLOAT_MIN);
for (int i = 0; i < m_proteins.size(); i++)
{
SimpleProtein* protein = m_proteins.at(i);
min.x = glm::min(min.x, protein->bbMin.x);
min.y = glm::min(min.y, protein->bbMin.y);
min.z = glm::min(min.z, protein->bbMin.z);
max.x = glm::max(max.x, protein->bbMax.x);
max.y = glm::max(max.y, protein->bbMax.y);
max.z = glm::max(max.z, protein->bbMax.z);
}
} else {
Logger::instance().print("No proteins there to calculate bounding box!", Logger::Mode::WARNING);
min = glm::vec3(0,0,0);
max = glm::vec3(0,0,0);
}
}
void ProteinLoader::getCenteredBoundingBoxAroundProteins(glm::vec3& min, glm::vec3& max)
{
getBoundingBoxAroundProteins(min, max);
glm::vec3 extent = max - min;
glm::vec3 center = (max + min) / 2;
float longestSideHalf = std::max(std::max(extent.x, extent.y), extent.z) / 2;
min = center - longestSideHalf;
max = center + longestSideHalf;
}