-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
164 lines (127 loc) · 4.7 KB
/
main.cpp
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
#include <fstream>
#include <vector>
#include "cpdb/cpdb.h"
#include "cartoon.h"
#define USEOMP 1
#if USEOMP
#include <omp.h>
#endif
#include <chrono>
#include <iomanip>
using namespace std;
#define TIMEIT 1
/// Tool to create cartoon meshes for secondary structures of proteins
/// Can be used as a library by calling computeCartoonMesh or as a standalone tool by calling the executable on a PDB file
/// Secondary structure code : 0 = COIL / 1 = HELIX / 2 = STRAND
/// Example: computeCartoonMesh(numberOfChains, (int *)numberOfResiduePerChain, (float *)CA_O_Positions, (char *)ssTypePerResidue);
/// Utility function to write meshes to an OBJ file
void writeToObj(string fileName, vector<Mesh> &meshes) {
FILE *fich = NULL;
cerr << "Writting to " << fileName << endl;
fich = fopen(fileName.c_str(), "w");
if (fich == NULL)
{
printf("Error opening file '%s'!\n", fileName.c_str());
exit(1);
}
for (int i = 0; i < meshes.size(); i++) {
vector<v3> verts = meshes[i].vertices;
vector<v3> colors = meshes[i].colors;
for (int j = 0; j < verts.size(); j++) {
// myfile << "v "<<meshes[i].vertices[j].x<<" "<<meshes[i].vertices[j].y<<" "<<meshes[i].vertices[j].z<<endl;
fprintf(fich, "v %.3f %.3f %.3f %.3f %.3f %.3f\n", verts[j].x, verts[j].y, verts[j].z, colors[j].x, colors[j].y, colors[j].z);
}
}
int cpt = 1;
for (int i = 0; i < meshes.size(); i++) {
vector<int> tri = meshes[i].triangles;
for (int j = 0; j < tri.size(); j += 3) {
// myfile << "f "<<cpt+tri[j]<<" "<<cpt+tri[j+1]<<" "<<cpt+tri[j+2]<<endl;
fprintf(fich, "f %d %d %d\n", cpt + tri[j], cpt + tri[j + 1], cpt + tri[j + 2] );
}
cpt += meshes[i].vertices.size();
}
fclose(fich);
cerr << "Wrote " << meshes.size() << " meshes to " << fileName << endl;
}
//Assuming there are always 2 atoms per residue
vector<Mesh> computeCartoonMesh(int nbChain, int *nbResPerChain, float *CA_OPositions, char *ssTypePerRes) {
vector<Mesh> meshes(nbChain);
if (nbChain <= 0) {
return meshes;
}
#if USEOMP
#pragma omp parallel for num_threads(nbChain)
for (int chainId = 0; chainId < nbChain; chainId++) {
Mesh m = createChainMesh(chainId, nbResPerChain, CA_OPositions, ssTypePerRes);
meshes[omp_get_thread_num()] = m;
}
#else
for (int chainId = 0; chainId < nbChain; chainId++) {
Mesh m = createChainMesh(chainId, nbResPerChain, CA_OPositions, ssTypePerRes);
meshes[chainId] = m;
}
#endif
return meshes;
}
int main(int argc, char const *argv[]) {
if (argc != 3) {
std::cout << "Usage : " << argv[0] << " file.pdb output.obj" << endl;
exit(-1);
}
string outputPath = argv[2];
pdb *P;
P = initPDB();
parsePDB((char *)argv[1], P, (char *)"");
#if TIMEIT
auto start = std::chrono::high_resolution_clock::now();
#endif
#if 0 //Previous implementation
// vector<Mesh> meshes;
vector<Mesh> meshes(P->size);
#pragma omp parallel for num_threads(P->size)
for (int chainId = 0; chainId < P->size; chainId++) {
chain C = P->chains[chainId];
Mesh m = createChainMesh(C);
// meshes.push_back(m);
meshes[omp_get_thread_num()] = m;
}
#else
//Get atom positions and call computeCartoonMesh
vector<int> nbResPerChain(P->size);
vector<float> CAOPos;
vector<char> allSS;
for (int chainId = 0; chainId < P->size; chainId++) {
chain *C = &P->chains[chainId];
nbResPerChain[chainId] = C->size;
for (int r = 0; r < C->size; r++) {
residue *R = &C->residues[r];
atom *CA = getAtom(*R, (char *)"CA");
atom *O = getAtom(*R, (char *)"O");
char ss = R->ss;
if (CA == NULL || O == NULL) {
cerr << "CA or O not found in chain " << C->id << " residue " << R->type << "_" << R->id << endl;
exit(-1);
}
CAOPos.push_back(CA->coor.x);
CAOPos.push_back(CA->coor.y);
CAOPos.push_back(CA->coor.z);
CAOPos.push_back(O->coor.x);
CAOPos.push_back(O->coor.y);
CAOPos.push_back(O->coor.z);
allSS.push_back(ss);
}
}
cout << "Starting" << endl;
fflush(stdout);
vector<Mesh> meshes = computeCartoonMesh(P->size, &nbResPerChain[0], &CAOPos[0], &allSS[0]);
#endif
#if TIMEIT
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>( stop - start ).count();
cerr << duration << " ms" << endl;
#endif
//Write the meshes to an OBJ file
writeToObj(outputPath, meshes);
return 0;
}