mirror of
https://github.com/PDB-REDO/libcifpp.git
synced 2026-06-04 22:14:24 +08:00
Compare commits
820 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ed704f172 | ||
|
|
398c16eac2 | ||
|
|
fa869bdc7d | ||
|
|
c20d0d2a30 | ||
|
|
000f2736c2 | ||
|
|
cfcc81bb62 | ||
|
|
82eae05868 | ||
|
|
e8fb53c49b | ||
|
|
604c97afe1 | ||
|
|
7e60cdf272 | ||
|
|
9ea7cfcc80 | ||
|
|
a7a4a16f79 | ||
|
|
6717059934 | ||
|
|
714747c280 | ||
|
|
81cd305c80 | ||
|
|
5de872bbb3 | ||
|
|
ce6a75a920 | ||
|
|
874a5cb2f2 | ||
|
|
6e2202d4f1 | ||
|
|
bcf33df701 | ||
|
|
3bdcf21c69 | ||
|
|
4b36bdc58c | ||
|
|
6d9008ee8c | ||
|
|
ee93692707 | ||
|
|
2bcc368bce | ||
|
|
6cc4467d53 | ||
|
|
425f98dc07 | ||
|
|
cf34a9f3ad | ||
|
|
3b2f347428 | ||
|
|
bd82c3cc4f | ||
|
|
af319866c7 | ||
|
|
b6ab29398e | ||
|
|
a5bb1797c0 | ||
|
|
a9647671c4 | ||
|
|
63f784e7da | ||
|
|
5da3379e0b | ||
|
|
2f3514689d | ||
|
|
89a3ea4e24 | ||
|
|
467e9555f4 | ||
|
|
5b32ca15f7 | ||
|
|
92402817d2 | ||
|
|
60ad3031d5 | ||
|
|
724cddb481 | ||
|
|
41c0521480 | ||
|
|
85ac2b1f63 | ||
|
|
13a97353aa | ||
|
|
f49c166b9b | ||
|
|
fffa326f80 | ||
|
|
da446adbb2 | ||
|
|
617fec5c69 | ||
|
|
cfefa69c9c | ||
|
|
00638a9e23 | ||
|
|
e241e03a15 | ||
|
|
b1faa3bd48 | ||
|
|
6d28f487ec | ||
|
|
b231f92f76 | ||
|
|
7d33d56c0e | ||
|
|
f86f34e5e1 | ||
|
|
5e7b52b7de | ||
|
|
0459d344e9 | ||
|
|
71e525cd76 | ||
|
|
1480706d8b | ||
|
|
96655b6d80 | ||
|
|
eed2aa0d0d | ||
|
|
de0c078a23 | ||
|
|
321e995a54 | ||
|
|
da9f1f81d7 | ||
|
|
c6d4477a24 | ||
|
|
523b073cdc | ||
|
|
2591bee21b | ||
|
|
d881ca00c9 | ||
|
|
329dbff474 | ||
|
|
d84a9fe6dc | ||
|
|
dcd812a996 | ||
|
|
6750194d9b | ||
|
|
05865c3d9b | ||
|
|
21e224bf00 | ||
|
|
f401d3fd0c | ||
|
|
fd436871f1 | ||
|
|
fcf7864a4b | ||
|
|
c4003956d9 | ||
|
|
de622b6162 | ||
|
|
41b4bdb90e | ||
|
|
af73cb3ad3 | ||
|
|
240b631963 | ||
|
|
c2a747af8c | ||
|
|
5959647826 | ||
|
|
9542e211bc | ||
|
|
d07890db7f | ||
|
|
ca241bd8f2 | ||
|
|
e444092711 | ||
|
|
a96b1e07f4 | ||
|
|
f48c31bcb5 | ||
|
|
d85ab93a35 | ||
|
|
a6804b5aca | ||
|
|
e4dcb211ee | ||
|
|
a5a5f47f7a | ||
|
|
25c900c387 | ||
|
|
4e95f7b83e | ||
|
|
66ad3b0cee | ||
|
|
e853cd1ca0 | ||
|
|
b9544033c6 | ||
|
|
17840cb8cc | ||
|
|
f85b6d94b8 | ||
|
|
6c32a9f198 | ||
|
|
cefeebbfb8 | ||
|
|
941a015b43 | ||
|
|
ae0e9fbe77 | ||
|
|
3484c3dd2e | ||
|
|
5be8f749bd | ||
|
|
cf484707a0 | ||
|
|
f12e529c0b | ||
|
|
01b90a2ba5 | ||
|
|
cd1e952812 | ||
|
|
996f1e4277 | ||
|
|
2d84694f86 | ||
|
|
65718c64cc | ||
|
|
6e30365f55 | ||
|
|
c0555b6d86 | ||
|
|
1ff9b6c071 | ||
|
|
c1a51a1dfa | ||
|
|
bfbbeb90e7 | ||
|
|
588e075325 | ||
|
|
66717fee68 | ||
|
|
844f52c955 | ||
|
|
e679cd05c1 | ||
|
|
1e72ce4830 | ||
|
|
3bb21c5403 | ||
|
|
6d1be23ad0 | ||
|
|
0472b9a4a4 | ||
|
|
c9acff49f9 | ||
|
|
7cab560595 | ||
|
|
ac98531a2f | ||
|
|
917e0ba79c | ||
|
|
3ebceb7522 | ||
|
|
92bd52da12 | ||
|
|
fb56a9cd6e | ||
|
|
a4680f7d38 | ||
|
|
da8a72a8aa | ||
|
|
ac497932b5 | ||
|
|
9927b5061a | ||
|
|
cedaab9642 | ||
|
|
50bf2145ec | ||
|
|
dc77729f50 | ||
|
|
e3330d667a | ||
|
|
9822f397a1 | ||
|
|
a3b5ce9959 | ||
|
|
9eb06e929a | ||
|
|
629e06d647 | ||
|
|
51ccb92184 | ||
|
|
3cd27f13fd | ||
|
|
ae668530c0 | ||
|
|
4a8b1c056c | ||
|
|
d7a5e598bc | ||
|
|
3f1ee32cc6 | ||
|
|
725d6ead98 | ||
|
|
baf70579de | ||
|
|
cd28ab58a3 | ||
|
|
a78fa0a81d | ||
|
|
82130be5f5 | ||
|
|
510ce62dfb | ||
|
|
93375a5087 | ||
|
|
be738e7fb1 | ||
|
|
9c78131df3 | ||
|
|
d94f6f4d19 | ||
|
|
9a3eced350 | ||
|
|
2fed7a76fb | ||
|
|
f02e59df1b | ||
|
|
04147a2fe9 | ||
|
|
0e83bc31dc | ||
|
|
75a5f7960f | ||
|
|
3f93c27b07 | ||
|
|
ab781d4516 | ||
|
|
446438bf8c | ||
|
|
4e012cbd48 | ||
|
|
12ee4a792c | ||
|
|
e59750386f | ||
|
|
4e19d54867 | ||
|
|
db603e5438 | ||
|
|
5320cb123a | ||
|
|
30a2ebdbb4 | ||
|
|
a5d43998a3 | ||
|
|
2792caec70 | ||
|
|
fb2b1e984c | ||
|
|
13ab1caf95 | ||
|
|
5d4534fac4 | ||
|
|
f450643861 | ||
|
|
fc14a65511 | ||
|
|
bbd1e27c5e | ||
|
|
369a83b718 | ||
|
|
afc541b956 | ||
|
|
7e4d2ffb4d | ||
|
|
e09913a94f | ||
|
|
b4d1c4cc04 | ||
|
|
22537c0e7e | ||
|
|
39c0db8d6a | ||
|
|
9db12761f7 | ||
|
|
0f8a7c4817 | ||
|
|
47e59a55c5 | ||
|
|
b3496f4e5d | ||
|
|
e866228afd | ||
|
|
4aeaa5251e | ||
|
|
b36988e64a | ||
|
|
393aefce8f | ||
|
|
227ff1b8be | ||
|
|
82086a93b0 | ||
|
|
abd97cc1c9 | ||
|
|
3315fae83e | ||
|
|
d8c3c3f7f0 | ||
|
|
23459879f8 | ||
|
|
f1ca916d58 | ||
|
|
6aae012ae5 | ||
|
|
516983427a | ||
|
|
05d78c92f9 | ||
|
|
dc57144472 | ||
|
|
dd260ca45e | ||
|
|
3bc2fc4151 | ||
|
|
6c58eaa7e8 | ||
|
|
e1a1c11a01 | ||
|
|
95a6b4264d | ||
|
|
4782a4e07d | ||
|
|
dbc14206dc | ||
|
|
f4296d8858 | ||
|
|
75c4c2286d | ||
|
|
b14237e8e6 | ||
|
|
df3263e4bd | ||
|
|
ff7b413abf | ||
|
|
07224779e6 | ||
|
|
c031a3c24e | ||
|
|
1e74f7912c | ||
|
|
d91f8d0876 | ||
|
|
43d4644fba | ||
|
|
61a924d208 | ||
|
|
2692f2c1bf | ||
|
|
88b3c87bae | ||
|
|
7fd7dfd937 | ||
|
|
9a76f02709 | ||
|
|
81cc1c73c1 | ||
|
|
eabda8f0f7 | ||
|
|
cb3917457b | ||
|
|
2c4f36a8f9 | ||
|
|
d458be99bc | ||
|
|
a7f4531767 | ||
|
|
08f5a1ad06 | ||
|
|
f2de24c851 | ||
|
|
ed1eca8fb5 | ||
|
|
f792e6f13d | ||
|
|
b1782b7a97 | ||
|
|
f3c3966e00 | ||
|
|
8a9f72c96b | ||
|
|
402d3a16ae | ||
|
|
458e228b91 | ||
|
|
a817f09441 | ||
|
|
413ba85538 | ||
|
|
c1807bf1c3 | ||
|
|
09dd65499a | ||
|
|
640552abad | ||
|
|
faee1848e5 | ||
|
|
c7a217fea7 | ||
|
|
e4ae19a625 | ||
|
|
24fe6ee583 | ||
|
|
9cba754eb6 | ||
|
|
9e6bb5e844 | ||
|
|
a74ee255fb | ||
|
|
b9fc0c0365 | ||
|
|
2ce16c74f7 | ||
|
|
6d0ea5c6f9 | ||
|
|
b83ef112a9 | ||
|
|
dd9110a3a8 | ||
|
|
88c23e1b0f | ||
|
|
dbe40e3ad6 | ||
|
|
958d3b05c8 | ||
|
|
4e7b0baafb | ||
|
|
b3dc38f2d8 | ||
|
|
6044d3dce3 | ||
|
|
29446f2122 | ||
|
|
abb8673549 | ||
|
|
ffc8f9dcdf | ||
|
|
288b2bb720 | ||
|
|
fb3b7bda68 | ||
|
|
6d5efe1cbd | ||
|
|
1ceec22184 | ||
|
|
951ff9b953 | ||
|
|
641f06a7e7 | ||
|
|
915ba4ac21 | ||
|
|
824637d83f | ||
|
|
0871406fe3 | ||
|
|
1ad7e47b2e | ||
|
|
f72a2c69d0 | ||
|
|
84d9275cb8 | ||
|
|
1b7c387c8b | ||
|
|
2f1adbd22c | ||
|
|
65031523a6 | ||
|
|
02cc0fa0f6 | ||
|
|
1e1afa023f | ||
|
|
41f343c2cd | ||
|
|
a73c4deaca | ||
|
|
01a21aebc4 | ||
|
|
49434043f2 | ||
|
|
2e23877912 | ||
|
|
b737dd7df4 | ||
|
|
137ffaf768 | ||
|
|
747c6d30d2 | ||
|
|
4585968b11 | ||
|
|
84af564aee | ||
|
|
86d957675e | ||
|
|
bc33e608db | ||
|
|
0b5d28338e | ||
|
|
bdbf22e705 | ||
|
|
160f6016ee | ||
|
|
0855965edf | ||
|
|
fe3cbdab78 | ||
|
|
a8c25f910d | ||
|
|
cb82ec9b01 | ||
|
|
e84282cb9a | ||
|
|
8b2e02e1b0 | ||
|
|
1addd2be89 | ||
|
|
2aebfc29ac | ||
|
|
26a5410b38 | ||
|
|
f44e6d0948 | ||
|
|
d496ebf6dd | ||
|
|
1719ed6979 | ||
|
|
821895bb1b | ||
|
|
3f437277d1 | ||
|
|
39fc56084a | ||
|
|
e2fca07fad | ||
|
|
ec0d75ce95 | ||
|
|
877a64adaa | ||
|
|
0fcf9ed5ad | ||
|
|
c99de817fa | ||
|
|
600c86a185 | ||
|
|
1ae755b0a5 | ||
|
|
7186057dd3 | ||
|
|
768fec9c58 | ||
|
|
7197dd877b | ||
|
|
b7aa7eac9f | ||
|
|
818dc2f952 | ||
|
|
9dc5d11829 | ||
|
|
8565e1b408 | ||
|
|
bfc7133786 | ||
|
|
15a49f1bb4 | ||
|
|
db1dff16fe | ||
|
|
8d7d9d3a31 | ||
|
|
078bf8a559 | ||
|
|
1f314a5e9b | ||
|
|
0adb50ac01 | ||
|
|
d91707cd06 | ||
|
|
c0e7ee4eeb | ||
|
|
c143a7223e | ||
|
|
2c951ba146 | ||
|
|
660aadcd9c | ||
|
|
91d6adb980 | ||
|
|
b79ddd55c5 | ||
|
|
0ca645c634 | ||
|
|
676c0c8dc8 | ||
|
|
5c366ad9b1 | ||
|
|
836aed6ea9 | ||
|
|
50df250415 | ||
|
|
2409fc5b7b | ||
|
|
8a1184a24c | ||
|
|
d2fbc54765 | ||
|
|
1bcb26ba75 | ||
|
|
32f4749d84 | ||
|
|
da12be879a | ||
|
|
94a38ad4e8 | ||
|
|
20ef79a172 | ||
|
|
92bf25476e | ||
|
|
b55e074dd7 | ||
|
|
7b654a837d | ||
|
|
ae9d247d22 | ||
|
|
16b7deafe8 | ||
|
|
f2cfe28458 | ||
|
|
2e8a52949e | ||
|
|
441e142767 | ||
|
|
bf9bdd2aae | ||
|
|
ce14593f0b | ||
|
|
1c02a451e1 | ||
|
|
448855a2d3 | ||
|
|
8ac8e89f2b | ||
|
|
2281f59401 | ||
|
|
4cb0673370 | ||
|
|
76c5706f7c | ||
|
|
2bf4284ff4 | ||
|
|
d9e2fc97f3 | ||
|
|
85dfdf4174 | ||
|
|
1bede3efda | ||
|
|
505f0fdd31 | ||
|
|
eed7ec3a4a | ||
|
|
fdb057e0e2 | ||
|
|
3fddd1a628 | ||
|
|
2440706b87 | ||
|
|
cf628fa95c | ||
|
|
2b0b47d20d | ||
|
|
a8abf2804f | ||
|
|
22d7757949 | ||
|
|
0b0d170c96 | ||
|
|
1e8e9adf62 | ||
|
|
0f03fc31e0 | ||
|
|
518432e0fb | ||
|
|
10ef3464ef | ||
|
|
226abbd577 | ||
|
|
8d66f42ab1 | ||
|
|
0f14d06f9a | ||
|
|
c53be78496 | ||
|
|
a38f31ce48 | ||
|
|
1258bd5047 | ||
|
|
d25cbeb14c | ||
|
|
9b60a07fb6 | ||
|
|
c0dd41ce50 | ||
|
|
4cff92bbcc | ||
|
|
9aa8a223c7 | ||
|
|
fb59adcfdd | ||
|
|
4acca8a3e3 | ||
|
|
c1030d2b08 | ||
|
|
16a185c6c0 | ||
|
|
174e818bd0 | ||
|
|
7f829bf5df | ||
|
|
71908282bb | ||
|
|
db3ae446af | ||
|
|
bc7d291307 | ||
|
|
cfd4702279 | ||
|
|
54eefb546d | ||
|
|
6af0d96a4e | ||
|
|
eb50bee4a3 | ||
|
|
b6143f3652 | ||
|
|
348aa7afb6 | ||
|
|
66912b68cc | ||
|
|
84dd218758 | ||
|
|
106ae38976 | ||
|
|
f1a52245ea | ||
|
|
cea38e5bb2 | ||
|
|
ed5aac358c | ||
|
|
5eb128251e | ||
|
|
cfa46ec954 | ||
|
|
07cc60e264 | ||
|
|
90973dc547 | ||
|
|
12e3d71b00 | ||
|
|
9addc8f873 | ||
|
|
343465cef0 | ||
|
|
bec5159415 | ||
|
|
f8da8360e6 | ||
|
|
fb2ad7b75d | ||
|
|
24aa7a70e5 | ||
|
|
5ade3d6cdd | ||
|
|
0d8e548ffc | ||
|
|
b09650812f | ||
|
|
acc9ad5c08 | ||
|
|
67b6c4bd27 | ||
|
|
7a1d3dbdfa | ||
|
|
4bf10df0c5 | ||
|
|
d84faad109 | ||
|
|
e01ace7ea4 | ||
|
|
e004e1591e | ||
|
|
4613084e1b | ||
|
|
637b795a8f | ||
|
|
4de981a3c0 | ||
|
|
15db026e27 | ||
|
|
d88d520553 | ||
|
|
46cd98ea1d | ||
|
|
d10328d891 | ||
|
|
e418a17256 | ||
|
|
627d3b9df2 | ||
|
|
ba28ade414 | ||
|
|
7c11130357 | ||
|
|
151915beea | ||
|
|
4f9aacb338 | ||
|
|
1f8e491ddc | ||
|
|
05cfa92182 | ||
|
|
e8031aeb49 | ||
|
|
85885406aa | ||
|
|
636f17d78d | ||
|
|
29559a5339 | ||
|
|
19f2fd75c9 | ||
|
|
8a60bae335 | ||
|
|
fa5ff60550 | ||
|
|
f6e0568964 | ||
|
|
fa27a11fea | ||
|
|
19706559cb | ||
|
|
0a06a0a51d | ||
|
|
b045177734 | ||
|
|
7ee5fa8765 | ||
|
|
3e690048a6 | ||
|
|
7ec3bfea9f | ||
|
|
098f3fd496 | ||
|
|
5476eef049 | ||
|
|
33c1eea9a1 | ||
|
|
d3432ed87c | ||
|
|
f05363ea93 | ||
|
|
77389c20a4 | ||
|
|
7c5f1ba85e | ||
|
|
e7c34cc15c | ||
|
|
72fd03a6b2 | ||
|
|
61f464ae4d | ||
|
|
19cdf66f10 | ||
|
|
0c036df6a8 | ||
|
|
4c1b9d83d1 | ||
|
|
b976b4657b | ||
|
|
eba04950d5 | ||
|
|
0c70df27ec | ||
|
|
d83f34722b | ||
|
|
652b6021d3 | ||
|
|
7fe9c87b6e | ||
|
|
9b2ae6d7fd | ||
|
|
8fd5b9a34b | ||
|
|
dffbf52d04 | ||
|
|
57ac5f0112 | ||
|
|
d5a71b0b24 | ||
|
|
d95b7be2e4 | ||
|
|
2f0a23f56a | ||
|
|
13b218f643 | ||
|
|
92a836ecdc | ||
|
|
c88a46f155 | ||
|
|
8882a34984 | ||
|
|
2d4a1731d9 | ||
|
|
be1e3073f1 | ||
|
|
6fe5a04cc0 | ||
|
|
426432885e | ||
|
|
9bc3381814 | ||
|
|
f3a492fd67 | ||
|
|
8cd2aa46b6 | ||
|
|
d638d634ba | ||
|
|
35196789e0 | ||
|
|
e907ce6c29 | ||
|
|
b80bc20d17 | ||
|
|
3a87eaa435 | ||
|
|
143eb57f04 | ||
|
|
6cc550bf18 | ||
|
|
7f5336661b | ||
|
|
e44539ef2c | ||
|
|
a2f5850173 | ||
|
|
283f4883f7 | ||
|
|
ce9842f671 | ||
|
|
b784433fd7 | ||
|
|
8c064e7c0a | ||
|
|
c15a8bd127 | ||
|
|
64e40e7b31 | ||
|
|
06d254e0de | ||
|
|
eaa342ca32 | ||
|
|
782f7c467b | ||
|
|
c45d02cb70 | ||
|
|
5b4c131eea | ||
|
|
bbe71af99e | ||
|
|
49912d019f | ||
|
|
d4758e09d7 | ||
|
|
f0a913cc07 | ||
|
|
8b0b8e3688 | ||
|
|
de4fc8a015 | ||
|
|
bf1e56ec53 | ||
|
|
040b4e4ff9 | ||
|
|
4666ee3145 | ||
|
|
2958c56a92 | ||
|
|
9cff8768ab | ||
|
|
cc671b8006 | ||
|
|
728abe6d0e | ||
|
|
7b8f3f2538 | ||
|
|
98db98f916 | ||
|
|
96a67b23ca | ||
|
|
2c3d7542e5 | ||
|
|
f84d83b723 | ||
|
|
b1837ba029 | ||
|
|
260438fa44 | ||
|
|
23d82beb04 | ||
|
|
19db5d736b | ||
|
|
6946c40657 | ||
|
|
bd3723ee20 | ||
|
|
1f078d4827 | ||
|
|
3c62a38667 | ||
|
|
7ffda74e3d | ||
|
|
560f6debc6 | ||
|
|
ea1ac33de8 | ||
|
|
7ea30237ae | ||
|
|
bc668487e2 | ||
|
|
1769f9864b | ||
|
|
75ffd97802 | ||
|
|
cfd5b7da0f | ||
|
|
26b7d1df26 | ||
|
|
0747929cd6 | ||
|
|
5bcfb102f4 | ||
|
|
908fb1ccea | ||
|
|
af8389baa4 | ||
|
|
24ca1017cd | ||
|
|
85c21aeb01 | ||
|
|
2f249048d9 | ||
|
|
974cb40ab3 | ||
|
|
c01c16ea60 | ||
|
|
bd157c249c | ||
|
|
b0ac33c1b1 | ||
|
|
82e73a9525 | ||
|
|
adc316d671 | ||
|
|
6a0b6b99ac | ||
|
|
08dd9dd5b4 | ||
|
|
557a1c2d00 | ||
|
|
f77bbfedda | ||
|
|
3aa3fe19e2 | ||
|
|
35fcc0493e | ||
|
|
9485bec2fa | ||
|
|
4b759e731c | ||
|
|
7dd6a8a1aa | ||
|
|
96725ae8b9 | ||
|
|
b3a0ded9a8 | ||
|
|
184c491803 | ||
|
|
f944b3ce00 | ||
|
|
2557f41863 | ||
|
|
2b92cee3f7 | ||
|
|
8071768579 | ||
|
|
71c8541b68 | ||
|
|
3d66c77188 | ||
|
|
8701512961 | ||
|
|
b317c780ba | ||
|
|
681aa3bf8b | ||
|
|
a68e053471 | ||
|
|
25a90e3b32 | ||
|
|
2f62759dfe | ||
|
|
cf9ec46ab8 | ||
|
|
ecbef51b10 | ||
|
|
dfff8c9587 | ||
|
|
cc5d52bbf9 | ||
|
|
a9e9f86c93 | ||
|
|
a2c52713b2 | ||
|
|
545aca88d8 | ||
|
|
ac27248784 | ||
|
|
5758bfbaea | ||
|
|
8d3a079774 | ||
|
|
718c138510 | ||
|
|
29aac70e67 | ||
|
|
700575adfe | ||
|
|
9fe6e5df85 | ||
|
|
ce7434a463 | ||
|
|
ad7d876d07 | ||
|
|
0dc19e86fa | ||
|
|
a12acaa5c7 | ||
|
|
ff62efe720 | ||
|
|
2407877184 | ||
|
|
5fde050738 | ||
|
|
a855f88073 | ||
|
|
cfa2acd61d | ||
|
|
d9db2fe2e7 | ||
|
|
15d62cd3b6 | ||
|
|
19a89aeb7e | ||
|
|
677c61c32f | ||
|
|
4dd4f66397 | ||
|
|
04b7828abc | ||
|
|
9c621ecab8 | ||
|
|
ab9c4d9416 | ||
|
|
e5eb62255a | ||
|
|
98ff79432b | ||
|
|
24fa80ba2a | ||
|
|
3999d792ef | ||
|
|
4db3732749 | ||
|
|
07131e8b40 | ||
|
|
39b91e74c9 | ||
|
|
d4bb7ec3bc | ||
|
|
6175b7e359 | ||
|
|
10442d506a | ||
|
|
573a695c3d | ||
|
|
a76bef0d01 | ||
|
|
e20111b566 | ||
|
|
4a1d9c8f75 | ||
|
|
26c86282e3 | ||
|
|
0eaeb1650d | ||
|
|
f4a6533f6b | ||
|
|
df1b6a13e1 | ||
|
|
e8f24f617c | ||
|
|
9454fdc217 | ||
|
|
22543d8fe5 | ||
|
|
60d1dc82e6 | ||
|
|
87486f87ef | ||
|
|
80e7da0f13 | ||
|
|
3745beae66 | ||
|
|
3965840bfa | ||
|
|
a88c6f3d32 | ||
|
|
ed6c6f0026 | ||
|
|
bdda9d72b5 | ||
|
|
fd080e778e | ||
|
|
9f72df2ecd | ||
|
|
617db012f0 | ||
|
|
9d15541237 | ||
|
|
35c99564c6 | ||
|
|
1d8fe334d6 | ||
|
|
d86bb314ac | ||
|
|
0ef8eb59f8 | ||
|
|
b5fe4a9a87 | ||
|
|
11fea31b98 | ||
|
|
f629275ed5 | ||
|
|
a5f6166469 | ||
|
|
501050e591 | ||
|
|
e1b240b2b2 | ||
|
|
3d79278ed7 | ||
|
|
5e0b197a43 | ||
|
|
9c4170d9e2 | ||
|
|
af721eb196 | ||
|
|
788e315f5e | ||
|
|
4a82a8d5a8 | ||
|
|
11019a26f8 | ||
|
|
6f8909dce9 | ||
|
|
5525103aaf | ||
|
|
291ef737b1 | ||
|
|
af125bdd57 | ||
|
|
79089bbb8c | ||
|
|
1f08498d00 | ||
|
|
49ba714a03 | ||
|
|
85fd9296b2 | ||
|
|
1cda14867f | ||
|
|
2d2b26f7dc | ||
|
|
93b33af44a | ||
|
|
eb80490bcd | ||
|
|
ba2b06f5af | ||
|
|
fecc762db1 | ||
|
|
1e406253ab | ||
|
|
6e3b85f43d | ||
|
|
58f1b626e2 | ||
|
|
c104a08e16 | ||
|
|
dd0f6ca1e6 | ||
|
|
f02ea91b51 | ||
|
|
6768a501a3 | ||
|
|
879e15c759 | ||
|
|
89285b4abc | ||
|
|
c584714f91 | ||
|
|
f5016403b7 | ||
|
|
c8f66ae6bb | ||
|
|
858c967e71 | ||
|
|
f9ca5de5bf | ||
|
|
252c3476a1 | ||
|
|
19210df6db | ||
|
|
15c5730749 | ||
|
|
3764adb7ef | ||
|
|
9160adb1cf | ||
|
|
3ebf4338ab | ||
|
|
2eb4b7b39b | ||
|
|
c241e49b48 | ||
|
|
238c881132 | ||
|
|
49dc733536 | ||
|
|
755bd78f60 | ||
|
|
77f80cd51f | ||
|
|
3df6000635 | ||
|
|
5efee2b40d | ||
|
|
f3c2e59184 | ||
|
|
24ab660e6e | ||
|
|
6c0a418068 | ||
|
|
07a180991e | ||
|
|
4732004b67 | ||
|
|
faa9cd0431 | ||
|
|
e0c3c2394d | ||
|
|
2dec584f54 | ||
|
|
5ab2ccae40 | ||
|
|
1017d08626 | ||
|
|
32b1bbd943 | ||
|
|
1abf31ffa5 | ||
|
|
aec60829d2 | ||
|
|
888c3c38c2 | ||
|
|
e2c4648037 | ||
|
|
f7b98c0530 | ||
|
|
d4bd3faa16 | ||
|
|
c4f3b1cd7b | ||
|
|
74add69a83 | ||
|
|
a490b19d24 | ||
|
|
44cfa2c1a2 | ||
|
|
6dd9522b3f | ||
|
|
5e352cb8e4 | ||
|
|
2fad7315b8 | ||
|
|
520759dfe8 | ||
|
|
577b44ae11 | ||
|
|
66f742d6c0 | ||
|
|
7ba9f688c7 | ||
|
|
883f0307a2 | ||
|
|
c9719f873f | ||
|
|
123d25f853 | ||
|
|
56da42db84 | ||
|
|
7f820449ca | ||
|
|
ecb2cf5f11 | ||
|
|
7f27da9b3b | ||
|
|
01eb499c69 | ||
|
|
1ff6f70682 | ||
|
|
ddde996e10 | ||
|
|
1c9212c7e0 | ||
|
|
a568143991 | ||
|
|
2b6f1bd9ee | ||
|
|
2527aa5ea6 | ||
|
|
4c28091ecd | ||
|
|
d49725423e | ||
|
|
fcb4dc61b5 | ||
|
|
b7330c074f | ||
|
|
e8f4123030 | ||
|
|
975057c4c4 | ||
|
|
a0e01668d1 | ||
|
|
2c77491416 | ||
|
|
be19e4a9cb | ||
|
|
61ce91a9d7 | ||
|
|
18f1d07e85 | ||
|
|
b596976194 | ||
|
|
1f6b86d516 | ||
|
|
31499b977d | ||
|
|
f83850e380 | ||
|
|
1a4ccd86fe | ||
|
|
5c3c6fec09 | ||
|
|
f97e742daa | ||
|
|
7f39d401e2 | ||
|
|
af412c284d | ||
|
|
874cd3bae5 | ||
|
|
ea28ebdd13 | ||
|
|
3ba468933f | ||
|
|
45f33e4bea | ||
|
|
021487ed16 | ||
|
|
cb3443ffb1 | ||
|
|
6b2c9dc3e3 | ||
|
|
7513cc1947 | ||
|
|
c98b8ae5c9 | ||
|
|
ab2dd4b75f | ||
|
|
be77316545 | ||
|
|
cdfb0d9497 | ||
|
|
71f7e7c741 | ||
|
|
cff099596e | ||
|
|
e182604455 | ||
|
|
45a7defb7e | ||
|
|
906f6ac1ea | ||
|
|
8d96e513bd | ||
|
|
cdefd063e2 | ||
|
|
8bbcba76cf | ||
|
|
c767e89a5d | ||
|
|
b78a603dca |
65
.github/workflows/build-documentation.yml
vendored
Normal file
65
.github/workflows/build-documentation.yml
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
|
||||
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
|
||||
name: publish docs
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "trunk" ]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
pages: write
|
||||
id-token: write
|
||||
|
||||
concurrency:
|
||||
group: "pages"
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
docs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set reusable strings
|
||||
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
|
||||
id: strings
|
||||
shell: bash
|
||||
run: |
|
||||
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Install dependencies Ubuntu
|
||||
run: sudo apt-get update && sudo apt-get install cmake doxygen
|
||||
|
||||
- uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.9'
|
||||
cache: 'pip' # caching pip dependencies
|
||||
- run: pip install -r docs/requirements.txt
|
||||
|
||||
- name: Configure CMake
|
||||
run: cmake -S . -B ${{ steps.strings.outputs.build-output-dir }} -DBUILD_DOCUMENTATION=ON -DBUILD_TESTING=OFF
|
||||
|
||||
- name: Run Sphinx
|
||||
run: |
|
||||
cmake --build ${{ steps.strings.outputs.build-output-dir }} --target Sphinx-libcifpp
|
||||
ls -l ${{ steps.strings.outputs.build-output-dir }}
|
||||
ls -l ${{ steps.strings.outputs.build-output-dir }}/docs/sphinx
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-pages-artifact@v3
|
||||
with:
|
||||
path: ${{ steps.strings.outputs.build-output-dir }}/docs/sphinx
|
||||
|
||||
deploy:
|
||||
environment:
|
||||
name: github-pages
|
||||
url: ${{ steps.deployment.outputs.page_url }}
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
needs: docs
|
||||
|
||||
steps:
|
||||
- name: Deploy to GitHub Pages
|
||||
id: deployment
|
||||
uses: actions/deploy-pages@v4
|
||||
59
.github/workflows/cmake-multi-platform.yml
vendored
Normal file
59
.github/workflows/cmake-multi-platform.yml
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
name: multi platform test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "trunk", "develop" ]
|
||||
pull_request:
|
||||
branches: [ "trunk" ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
include:
|
||||
- os: windows-latest
|
||||
cpp_compiler: cl
|
||||
- os: ubuntu-latest
|
||||
cpp_compiler: g++
|
||||
- os: macos-latest
|
||||
cpp_compiler: clang++
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Set reusable strings
|
||||
id: strings
|
||||
shell: bash
|
||||
run: echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Install dependencies Ubuntu
|
||||
if: matrix.os == 'ubuntu-latest'
|
||||
run: sudo apt-get update && sudo apt-get install mrc
|
||||
|
||||
- name: Install dependencies Window
|
||||
if: matrix.os == 'windows-latest'
|
||||
run: ./tools/depends.cmd
|
||||
shell: cmd
|
||||
|
||||
- name: Configure CMake
|
||||
run: >
|
||||
cmake -B ${{ steps.strings.outputs.build-output-dir }}
|
||||
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
|
||||
-DCMAKE_BUILD_TYPE=Release
|
||||
-S ${{ github.workspace }}
|
||||
|
||||
- name: Build
|
||||
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config Release
|
||||
|
||||
- name: Test
|
||||
working-directory: ${{ steps.strings.outputs.build-output-dir }}
|
||||
run: ctest --build-config Release --output-on-failure
|
||||
|
||||
- name: Install
|
||||
if: matrix.os != 'windows-latest'
|
||||
run: sudo cmake --install ${{ steps.strings.outputs.build-output-dir }} --config Release
|
||||
17
.gitignore
vendored
17
.gitignore
vendored
@@ -1,14 +1,15 @@
|
||||
build/
|
||||
.vscode/
|
||||
.vs/
|
||||
.pc/
|
||||
tools/symop-map-generator
|
||||
test/unit-test
|
||||
test/pdb2cif-test
|
||||
test/rename-compound-test
|
||||
tools/update-dictionary-script
|
||||
data/
|
||||
tools/update-libcifpp-data
|
||||
rsrc/components.cif*
|
||||
CMakeSettings.json
|
||||
msvc/
|
||||
src/revision.hpp
|
||||
test/test-create_sugar_?.cif
|
||||
Testing/
|
||||
|
||||
include/cif++/exports.hpp
|
||||
docs/api
|
||||
docs/conf.py
|
||||
build_ci/
|
||||
data/components.cif
|
||||
|
||||
22
.readthedocs.yaml
Normal file
22
.readthedocs.yaml
Normal file
@@ -0,0 +1,22 @@
|
||||
version: 2
|
||||
|
||||
build:
|
||||
os: ubuntu-22.04
|
||||
tools:
|
||||
python: "3.11"
|
||||
apt_packages:
|
||||
- doxygen
|
||||
- cmake
|
||||
jobs:
|
||||
pre_build:
|
||||
- cmake -S . -B build -DBUILD_DOCUMENTATION=ON
|
||||
- cmake --build build --target Doxygen
|
||||
|
||||
# Build from the docs/ directory with Sphinx
|
||||
sphinx:
|
||||
configuration: docs/conf.py
|
||||
|
||||
# Explicitly set the version of Python and its requirements
|
||||
python:
|
||||
install:
|
||||
- requirements: docs/requirements.txt
|
||||
33
.travis.yml
33
.travis.yml
@@ -1,33 +0,0 @@
|
||||
language: cpp
|
||||
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
|
||||
dist: focal
|
||||
|
||||
osx_image:
|
||||
- xcode12
|
||||
|
||||
compiler:
|
||||
- gcc
|
||||
- clang
|
||||
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- libboost-all-dev
|
||||
|
||||
before_install:
|
||||
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew install make; fi
|
||||
|
||||
script:
|
||||
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then ./configure --disable-shared --disable-revision --disable-download-ccd ; else ./configure --disable-revision --disable-download-ccd ; fi
|
||||
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then gmake ; else make ; fi
|
||||
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then gmake test ; else make test ; fi
|
||||
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then sudo gmake install ; else sudo make install; fi
|
||||
|
||||
# jobs:
|
||||
# allow_failures:
|
||||
# - os: osx
|
||||
|
||||
953
CMakeLists.txt
953
CMakeLists.txt
File diff suppressed because it is too large
Load Diff
@@ -1,16 +0,0 @@
|
||||
@PACKAGE_INIT@
|
||||
|
||||
include(CMakeFindDependencyMacro)
|
||||
find_dependency(Boost 1.70.0 REQUIRED COMPONENTS system iostreams regex program_options)
|
||||
if(NOT WIN32)
|
||||
find_dependency(ZLIB)
|
||||
find_dependency(BZip2)
|
||||
endif()
|
||||
|
||||
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/cifppTargets.cmake")
|
||||
|
||||
set_and_check(CIFPP_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
|
||||
set_and_check(CIFPP_LIBRARY_DIR "@PACKAGE_LIBRARY_INSTALL_DIR@")
|
||||
set_and_check(CIFPP_SHARE_DIR "@PACKAGE_SHARE_INSTALL_DIR@")
|
||||
|
||||
check_required_components(cifpp)
|
||||
5
LICENSE
5
LICENSE
@@ -1,6 +1,7 @@
|
||||
SPDX-License-Identifier: BSD-2-Clause
|
||||
BSD-2-Clause License
|
||||
|
||||
Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
@@ -20,4 +21,4 @@ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
242
README.md
242
README.md
@@ -1,47 +1,225 @@
|
||||
libcifpp
|
||||
========
|
||||
[](https://github.com/pdb-redo/libcifpp/actions)
|
||||
[](https://github.com/pdb-redo/libcifpp/LICENSE)
|
||||
|
||||
This library contains code to work with mmCIF and PDB files.
|
||||
# libcifpp
|
||||
|
||||
Requirements
|
||||
------------
|
||||
As the name implies, this library was originally written to work with mmCIF files
|
||||
using C++ as programming language. The design of this library leanes heavily on
|
||||
the structure of CIF files. These files can be thought of as a text dump of a
|
||||
relational databank with, often but not always, a very strict schema describing
|
||||
the data. These schema's are called dictionaries.
|
||||
|
||||
Using information from the content of a mmCIF file and an optional schema,
|
||||
libcifpp allows you to access the data in the file as a collection of datablock
|
||||
each containing a collection of categories with rows of data. The categories can
|
||||
be searched for data using queries written in regular C++ syntax. When a dictionary
|
||||
was specified, inserted data is checked for validity. Likewise removal of data
|
||||
may result in cascaded removal of linked data in other categories using
|
||||
parent/child relationship information.
|
||||
|
||||
Since there were still many programs using the legacy PDB format at the time
|
||||
development started, a layer was added that converts data to and from PDB format
|
||||
into mmCIF format. This means you can manipulate PDB files as if they were
|
||||
normal mmCIF files.
|
||||
|
||||
Apart from this basic functionality, libcifpp also offers code to help with
|
||||
symmetry calculations, 3d manipulations and obtaining information from the CCD
|
||||
[Chemical Component Dictionary](https://www.wwpdb.org/data/ccd).
|
||||
|
||||
## Documentation
|
||||
|
||||
The documentation can be found at [github.io](https://pdb-redo.github.io/libcifpp/)
|
||||
|
||||
## Synopsis
|
||||
|
||||
```cpp
|
||||
// A simple program counting residues with an OXT atom
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
#include <cif++.hpp>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
exit(1);
|
||||
|
||||
// Read file, can be PDB or mmCIF and can even be compressed with gzip.
|
||||
cif::file file = cif::pdb::read(argv[1]);
|
||||
|
||||
if (file.empty())
|
||||
{
|
||||
std::cerr << "Empty file\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Take the first datablock in the file
|
||||
auto &db = file.front();
|
||||
|
||||
// Use the atom_site category
|
||||
auto &atom_site = db["atom_site"];
|
||||
|
||||
// Count the atoms with atom-id "OXT"
|
||||
auto n = atom_site.count(cif::key("label_atom_id") == "OXT");
|
||||
|
||||
std::cout << "File contains " << atom_site.size() << " atoms of which "
|
||||
<< n << (n == 1 ? " is" : " are") << " OXT\n"
|
||||
<< "residues with an OXT are:\n";
|
||||
|
||||
// Loop over all atoms with atom-id "OXT" and print out some info.
|
||||
// That info is extracted using structured binding in C++
|
||||
for (const auto &[asym, comp, seqnr] :
|
||||
atom_site.find<std::string, std::string, int>(
|
||||
cif::key("label_atom_id") == "OXT",
|
||||
"label_asym_id", "label_comp_id", "label_seq_id"))
|
||||
{
|
||||
std::cout << asym << ' ' << comp << ' ' << seqnr << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
You might be able to use libcifpp from a package manager used by your
|
||||
OS distribution. But most likely this package will be out-of-date.
|
||||
Therefore it is recommended to build *libcifpp* from code. It is not
|
||||
hard to do. But it is recommended to read the following instructions
|
||||
carefully.
|
||||
|
||||
### Requirements
|
||||
|
||||
The code for this library was written in C++17. You therefore need a
|
||||
recent compiler to build it. For the development gcc 9.3 and clang 9.0
|
||||
recent compiler to build it. For the development gcc >= 9.4 and clang >= 9.0
|
||||
have been used as well as MSVC version 2019.
|
||||
|
||||
Other requirements are:
|
||||
The other requirement you really need to have installed on your computer
|
||||
is a version of [CMake](https://cmake.org). For now the minimum version
|
||||
is 3.16 but that may soon change into a higher version. You should also
|
||||
install the gui version of CMake to set build options easily, on Debian
|
||||
I prefer to use the curses version installed with `cmake-curses-gui`.
|
||||
|
||||
- Boost libraries, at least version 1.70
|
||||
- [mrc](https://github.com/mhekkel/mrc), a resource compiler that
|
||||
allows including data files into the executable making them easier to
|
||||
install. Strictly this is optional, but at the expense of functionality.
|
||||
It is very useful to have [mrc](https://github.com/mhekkel/mrc) available.
|
||||
However, this is only an option if you use Windows or an operating system
|
||||
using the ELF executable format (i.e. Linux or FreeBSD). MRC is a resource
|
||||
compiler that allows including data files into the executable making them
|
||||
easier to install.
|
||||
|
||||
Building
|
||||
--------
|
||||
Other libraries you might want to install beforehand are:
|
||||
|
||||
This library uses [cmake](https://cmake.org). The usual way of building
|
||||
and installing is to create a `build` directory and run cmake there.
|
||||
- [libeigen](https://eigen.tuxfamily.org/index.php?title=Main_Page), a
|
||||
library to do amongst others matrix calculations. This usually can be
|
||||
installed using your package manager, in Debian/Ubuntu it is called
|
||||
`libeigen3-dev`
|
||||
- [zlib](https://github.com/madler/zlib), the development version of this
|
||||
library. On Debian/Ubuntu this is the package `zlib1g-dev`.
|
||||
- [boost](https://www.boost.org), in Debian/Ubuntu this is `libboost-dev`.
|
||||
|
||||
The Boost libraries are only needed in case you are using GCC due to a long
|
||||
standing bug in GNU's implementation of std::regex. It simply crashes
|
||||
on the regular expressions used in the mmcif_pdbx dictionary and so
|
||||
we use the boost regex implementation instead.
|
||||
|
||||
On linux e.g. you would issue the following commands:
|
||||
### Building
|
||||
|
||||
First you need to download the code:
|
||||
|
||||
```console
|
||||
git clone https://github.com/PDB-REDO/libcifpp.git
|
||||
cd libcifpp
|
||||
```
|
||||
git clone https://github.com/PDB-REDO/libcifpp.git
|
||||
cd libcifpp
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake --build . --config Release
|
||||
ctest -C Release
|
||||
cmake --install .
|
||||
|
||||
You should start by considering where to install libcifpp. If you have
|
||||
sufficient permissions on your computer you perhaps should use the
|
||||
default but libcifpp can be configured to be installed anywhere
|
||||
including e.g. *$HOME/.local*.
|
||||
|
||||
Next step is to configure, for this use the CMake gui application. If you
|
||||
installed the curses version of cmake you can type `ccmake`. On Windows
|
||||
you can use `cmake-gui.exe`.
|
||||
|
||||
To install in the default location:
|
||||
|
||||
```console
|
||||
ccmake -S . -B build
|
||||
```
|
||||
This checks out the source code from github, creates a new directory
|
||||
where cmake stores its files. Run a configure, build the code and run
|
||||
tests. And then it installs the library and auxiliary files.
|
||||
|
||||
The default is to install everything in `$HOME/.local` on Linux and
|
||||
`%LOCALAPPDATA%` on Windows (the AppData/Local folder in your home directory).
|
||||
You can change this by specifying the prefix with the
|
||||
[CMAKE_INSTALL_PREFIX](https://cmake.org/cmake/help/v3.21/variable/CMAKE_INSTALL_PREFIX.html)
|
||||
variable.
|
||||
To install elsewhere, e.g. *$HOME/.local*:
|
||||
|
||||
```console
|
||||
ccmake -S . -B build -DCMAKE_INSTALL_PREFIX=$HOME/.local
|
||||
```
|
||||
|
||||
In the cmake window, start the configure command (use button or press 'c').
|
||||
After the first configure step you will see a list of settable options.
|
||||
Alter these to match your preferences. Most options are self explaining
|
||||
and contain a description. Some may need a bit more explanation:
|
||||
|
||||
- CIFPP_DATA_DIR, this directory will be used to store initial versions
|
||||
of the mmcif_pdbx dictionary as well as the optional CCD file.
|
||||
|
||||
- CIFPP_DOWNLOAD_CCD
|
||||
|
||||
The CCD file is huge and perhaps you think you don't
|
||||
need it. In that case you can leave this OFF. But that will limit the
|
||||
use cases.
|
||||
|
||||
- CIFPP_INSTALL_UPDATE_SCRIPT
|
||||
|
||||
The files in CIFPP_DATA_DIR are quickly becoming out of date. On
|
||||
FreeBSD and Linux you can install a script that updates these files
|
||||
on a weekly basis.
|
||||
|
||||
- CIFPP_CRON_DIR
|
||||
|
||||
The directory where the update script is to be installed.
|
||||
|
||||
- CIFPP_ETC_DIR
|
||||
|
||||
The update script will only work if the file called *libcifpp.conf*
|
||||
in this *etc* directory will contain an uncommented line with
|
||||
|
||||
```console
|
||||
update=true
|
||||
```
|
||||
|
||||
- CIFPP_CACHE_DIR
|
||||
|
||||
When you installed and enabled the update script, new files are
|
||||
written to this directory.
|
||||
|
||||
- CIFPP_RECREATE_SYMOP_DATA
|
||||
|
||||
If you had CCP4 sourced into your environment, this option allows
|
||||
you to recreate the symop data file.
|
||||
|
||||
- BUILD_FOR_CCP4
|
||||
|
||||
Build a special version of libcifpp to be installed in the CCP4
|
||||
environment.
|
||||
|
||||
After setting these options you can run the configure step again and
|
||||
then use generate to create the makefiles.
|
||||
|
||||
Building and installing is then as simple as:
|
||||
|
||||
```console
|
||||
cmake --build build
|
||||
cmake --install build
|
||||
```
|
||||
|
||||
If this fails due to lack of permissions, you can try:
|
||||
|
||||
```console
|
||||
sudo cmake --install build
|
||||
```
|
||||
|
||||
Tests are created by default, and to test the code you can run:
|
||||
|
||||
```console
|
||||
ctest --test-dir build
|
||||
```
|
||||
|
||||
250
changelog
250
changelog
@@ -1,3 +1,253 @@
|
||||
Version 9.0.0
|
||||
- Rename fields of cif::mm::polymer to match the naming
|
||||
in mmcif_pdbx.dic. Also, related, fix building mm::structure
|
||||
using the correct mapping between atom_site and residues.
|
||||
- _atom_site.auth_alt_id does not exist, it should be
|
||||
_atom_site.pdbx_auth_alt_id of course.
|
||||
- Added a more lightweight fixup for mmcif_pdbx files
|
||||
that lack certain categories.
|
||||
|
||||
Version 8.0.1
|
||||
- Fix cif::mm::structure::cleanup_empty_categories, removed too much
|
||||
- Add default value for B_iso_or_equiv in residue::create_new_atom
|
||||
- Reconstruct some branch records in bare pdbx files
|
||||
- Fix parsing PDB files (bug due to missing validator in dest. cat.)
|
||||
- Do not fail conversion of PDB files when compound info is missing
|
||||
|
||||
Version 8.0.0
|
||||
- A dictionary is for a datablock and a file can have
|
||||
datablocks with differing dictionaries.
|
||||
|
||||
Version 7.0.10
|
||||
- Deal with missing _entity.type in reconstructing mmCIF files
|
||||
- Replace code creating quaternions from rotation matrices
|
||||
that might sometimes give incorrect results. Or at least,
|
||||
the test code failed on this particular kind of code. Sometimes.
|
||||
- Fix reconstruction to build pdbx_nonpoly_scheme
|
||||
|
||||
Version 7.0.9
|
||||
- Using cif::file::load_dictionary it is now possible to
|
||||
load a dictionary along with its extensions in one go.
|
||||
E.g. file.load_dictionary("mmcif_pdbx;dssp-extension")
|
||||
- Fix in compound factory to avoid errors with lower case
|
||||
compound id's
|
||||
- Fix sac_parser's index to be case insensitive
|
||||
|
||||
Version 7.0.8
|
||||
- Fix PDB Remark 3 parser
|
||||
- Added three way comparison for point
|
||||
|
||||
Version 7.0.7
|
||||
- Set CIFPP_DATA_DIR on target cifpp for use in projects that include
|
||||
libcifpp directly
|
||||
|
||||
Version 7.0.6
|
||||
- Fix linking to std::atomic
|
||||
|
||||
Version 7.0.5
|
||||
- Fix case where category index was not updated for updated value
|
||||
|
||||
Version 7.0.4
|
||||
- Do not install headers and library in case we're not the top project
|
||||
|
||||
Version 7.0.3
|
||||
- Fix installation, write exports.hpp again
|
||||
|
||||
Version 7.0.2
|
||||
- Fix in testing error_code results.
|
||||
|
||||
Version 7.0.1
|
||||
- Various reconstruction fixes
|
||||
- category order in output fixed
|
||||
- better implementation of constructors for file, datablock and category
|
||||
- small optimisation in iterator
|
||||
|
||||
Version 7.0.0
|
||||
- Renaming many methods and parameters to be more
|
||||
consistent with the mmCIF dictionaries.
|
||||
(Most notably, item used to be called column or
|
||||
tag sometimes).
|
||||
- validation_error is now a std::system_error error
|
||||
value. The exception is gone.
|
||||
- Added repairSequenceInfo to repair invalid files
|
||||
|
||||
Version 6.1.0
|
||||
- Add formula weight to entity in pdb2cif
|
||||
- Change order of categories inside a datablock to match order in file
|
||||
- Change default order to write out categories in a file based on
|
||||
parent/child relationship
|
||||
- Added validate_pdbx and recover_pdbx
|
||||
- Fixed a serious bug in category_index when moving categories
|
||||
|
||||
Version 6.0.0
|
||||
- Drop the use of CCP4's monomer library for compound information
|
||||
|
||||
Version 5.2.5
|
||||
- Correctly import the Eigen3 library
|
||||
|
||||
Version 5.2.4
|
||||
- Changes required to build on Windows
|
||||
|
||||
Version 5.2.3
|
||||
- New constructors for cif::item, one taking std::optional values
|
||||
and another taking only a name resulting in a value '.' (i.e. inapplicable).
|
||||
- added cif::cell::get_volume
|
||||
|
||||
Version 5.2.2
|
||||
- Remove dependency on Eigen3 for users of libcifpp
|
||||
- Fix typos in documentation
|
||||
- Do not build latex files in documentation
|
||||
- Fixed conversion from string to integer, would fail on +2 e.g.
|
||||
- sqrt is not constexpr, thus kGoldenRatio should be const, not constexpr
|
||||
|
||||
Version 5.2.1
|
||||
- New versionstring module
|
||||
- small fixes for generating documentation
|
||||
- correctly setting SONAME
|
||||
|
||||
Version 5.2.0
|
||||
- With lots of documentation
|
||||
- Refactored coloured text output
|
||||
- Removed the subdirectory cif++/pdb, there now is a single
|
||||
header file pdb.hpp for I/O of legacy PDB files.
|
||||
|
||||
Version 5.1.3
|
||||
- Dropped pkgconfig support
|
||||
|
||||
Version 5.1.2
|
||||
- New version string code
|
||||
- Added check for Eigen3 in cifppConfig.cmake
|
||||
|
||||
Version 5.1.1
|
||||
- Added missing include <compare> in symmetry.hpp
|
||||
- Added empty() to matrix
|
||||
- Fix for parsing legacy PDB files with a last line that does
|
||||
not end with a new line character.
|
||||
|
||||
Version 5.1
|
||||
- New parser, optimised for speed
|
||||
- Fix in unique ID generator
|
||||
|
||||
Version 5.0.10
|
||||
- Fix in progress_bar, was using too much CPU
|
||||
- Optimised mmCIF parser
|
||||
|
||||
Version 5.0.9
|
||||
- Fix in dihedral angle calculations
|
||||
- Added create_water to model
|
||||
- Writing twin domain info in PDB files and more PDB fixes
|
||||
- remove_atom improved (remove struct_conn records)
|
||||
- Added a specialisation for category::find1<std::optional>
|
||||
- fix memory leak in category
|
||||
|
||||
Version 5.0.8
|
||||
- implemented find_first, find_min, find_max and count in category
|
||||
- find1 now throws an exception if condition does not not exactly match one row
|
||||
- Change in writing out PDB files, now looking up the original auth_seq_num
|
||||
via the pdbx_xxx_scheme categories based on the atom_site.auth_seq_num ->
|
||||
pdbx_xxx_scheme.pdb_seq_num relationship.
|
||||
- fix memory leak in category
|
||||
|
||||
Version 5.0.7.1
|
||||
- Use the implementation from zeep for std::experimental::is_detected
|
||||
|
||||
Version 5.0.7
|
||||
- Reintroduce exports file. For DLL's
|
||||
|
||||
Version 5.0.6
|
||||
- Fix file::contains, using iequals
|
||||
- Fix is_cis
|
||||
|
||||
Version 5.0.5
|
||||
- Fix code to work on 32 bit machines
|
||||
|
||||
Version 5.0.4
|
||||
- Revert removal of CIFPP_SHARE_DIR export
|
||||
|
||||
Version 5.0.3
|
||||
- Fix installation of libcifpp into the correct locations
|
||||
|
||||
Version 5.0.2
|
||||
- Fix export of CISPEP records in PDB format
|
||||
- Better support for exporting package_source
|
||||
|
||||
Version 5.0.1
|
||||
- Fix loading dictionaries
|
||||
- Support for cifv1.0 files
|
||||
|
||||
Version 5.0.0
|
||||
- Total rewrite of cif part
|
||||
- Removed DSSP code, moved into dssp project itself
|
||||
|
||||
Version 4.2.1
|
||||
- Improved REMARK 3 parser (for TLS in large molecules)
|
||||
|
||||
Version 4.2.0
|
||||
- Yet another rewrite of resource loading
|
||||
|
||||
Version 4.1.1
|
||||
- Fall back to zero charge for scattering factors if the atom
|
||||
was not found in the table.
|
||||
- Improve code to locate resources, failing less.
|
||||
|
||||
Version 4.1.0
|
||||
- Some interface changes for mmcif::Atom
|
||||
|
||||
Version 4.0.1
|
||||
- Added a bunch of const methods to Datablock and Category.
|
||||
- Changed PDB writing interface to accept Datablock instead of File.
|
||||
|
||||
Version 4.0.0
|
||||
- getResidue in mmcif::Structure now requires both a
|
||||
sequence ID and an auth sequence ID. As a result the code was cleaned
|
||||
up considerably.
|
||||
|
||||
Version 3.0.5
|
||||
- mmcif::Structure redesign. It is now a wrapper around a cif::Datablock.
|
||||
|
||||
Version 3.0.4
|
||||
- Fix in mmCIF parser, now correctly handles the unquoted
|
||||
string ??
|
||||
|
||||
Version 3.0.3
|
||||
- Better configuration checks, for atomic e.g.
|
||||
- Fixed a problem introduced in refactoring mmcif::Atom
|
||||
- Version string creation
|
||||
|
||||
Version 3.0.2
|
||||
- refactored mmcif::Atom for performance reasons
|
||||
|
||||
Version 3.0.1
|
||||
- Fixed processing of proline restraints file from CCP4, proline
|
||||
is a peptide, really.
|
||||
- Added code to facilitate DSSP
|
||||
|
||||
Version 3.0.0
|
||||
- Replaced many strings in the API with string_view for
|
||||
performance reasons.
|
||||
- Upgraded mmcif::Structure
|
||||
- various other small fixes
|
||||
|
||||
Version 2.0.5
|
||||
- Backporting updated CMakeLists.txt file
|
||||
|
||||
Version 2.0.4
|
||||
- Reverted a too strict test when reading cif files.
|
||||
|
||||
Version 2.0.3
|
||||
- Fixed reading mmCIF files where model numbers are used and
|
||||
model number 1 is missing.
|
||||
|
||||
Version 2.0.2
|
||||
- Added configuration flag to disable downloading CCD data during build
|
||||
Note that there are now two flags for CCD data:
|
||||
DOWNLOAD_CCD to enable downloading during build
|
||||
INSTALL_UPDATE_SCRIPT to install an update mechanism for this file
|
||||
- Updated unit tests to work even if no CCD data is available
|
||||
|
||||
Version 2.0.1
|
||||
- Fixed the generator for the symmetry operator table
|
||||
|
||||
Version 2.0.0
|
||||
- New API interface for accessing query results
|
||||
- Removed bzip2 support
|
||||
|
||||
63
cmake/FindAtomic.cmake
Normal file
63
cmake/FindAtomic.cmake
Normal file
@@ -0,0 +1,63 @@
|
||||
# Simple check to see if we need a library for std::atomic
|
||||
|
||||
if(TARGET std::atomic)
|
||||
return()
|
||||
endif()
|
||||
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
include(CMakePushCheckState)
|
||||
include(CheckIncludeFileCXX)
|
||||
include(CheckCXXSourceRuns)
|
||||
|
||||
cmake_push_check_state()
|
||||
|
||||
check_include_file_cxx("atomic" _CXX_ATOMIC_HAVE_HEADER)
|
||||
mark_as_advanced(_CXX_ATOMIC_HAVE_HEADER)
|
||||
|
||||
set(code [[
|
||||
#include <atomic>
|
||||
int main(int argc, char** argv) {
|
||||
std::atomic<long long> s;
|
||||
++s;
|
||||
return 0;
|
||||
}
|
||||
]])
|
||||
|
||||
check_cxx_source_runs("${code}" _CXX_ATOMIC_BUILTIN)
|
||||
|
||||
if(_CXX_ATOMIC_BUILTIN)
|
||||
set(_found 1)
|
||||
else()
|
||||
list(APPEND CMAKE_REQUIRED_LIBRARIES atomic)
|
||||
list(APPEND FOLLY_LINK_LIBRARIES atomic)
|
||||
|
||||
check_cxx_source_runs("${code}" _CXX_ATOMIC_LIB_NEEDED)
|
||||
if (NOT _CXX_ATOMIC_LIB_NEEDED)
|
||||
message(FATAL_ERROR "unable to link C++ std::atomic code: you may need \
|
||||
to install GNU libatomic")
|
||||
else()
|
||||
set(_found 1)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(_found)
|
||||
add_library(std::atomic INTERFACE IMPORTED)
|
||||
set_property(TARGET std::atomic APPEND PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_14)
|
||||
|
||||
if(_CXX_ATOMIC_BUILTIN)
|
||||
# Nothing to add...
|
||||
elseif(_CXX_ATOMIC_LIB_NEEDED)
|
||||
set_target_properties(std::atomic PROPERTIES IMPORTED_LIBNAME atomic)
|
||||
set(STDCPPATOMIC_LIBRARY atomic)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
cmake_pop_check_state()
|
||||
|
||||
set(Atomic_FOUND ${_found} CACHE BOOL "TRUE if we can run a program using std::atomic" FORCE)
|
||||
mark_as_advanced(Atomic_FOUND)
|
||||
|
||||
if(Atomic_FIND_REQUIRED AND NOT Atomic_FOUND)
|
||||
message(FATAL_ERROR "Cannot run simple program using std::atomic")
|
||||
endif()
|
||||
@@ -1,74 +0,0 @@
|
||||
# Simplistic reimplementation of https://github.com/vector-of-bool/CMakeCM/blob/master/modules/FindFilesystem.cmake
|
||||
|
||||
if(TARGET std::filesystem)
|
||||
return()
|
||||
endif()
|
||||
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
include(CMakePushCheckState)
|
||||
include(CheckIncludeFileCXX)
|
||||
include(CheckCXXSourceCompiles)
|
||||
|
||||
cmake_push_check_state()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
check_include_file_cxx("filesystem" _CXX_FILESYSTEM_HAVE_HEADER)
|
||||
mark_as_advanced(_CXX_FILESYSTEM_HAVE_HEADER)
|
||||
|
||||
set(code [[
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
|
||||
int main() {
|
||||
auto cwd = std::filesystem::current_path();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
]])
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS_EQUAL 8.4.0)
|
||||
# >> https://stackoverflow.com/questions/63902528/program-crashes-when-filesystempath-is-destroyed
|
||||
set(CXX_FILESYSTEM_NO_LINK_NEEDED 0)
|
||||
else()
|
||||
# Check a simple filesystem program without any linker flags
|
||||
check_cxx_source_compiles("${code}" CXX_FILESYSTEM_NO_LINK_NEEDED)
|
||||
endif()
|
||||
|
||||
if(CXX_FILESYSTEM_NO_LINK_NEEDED)
|
||||
set(_found 1)
|
||||
else()
|
||||
set(prev_libraries ${CMAKE_REQUIRED_LIBRARIES})
|
||||
# Add the libstdc++ flag
|
||||
set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lstdc++fs)
|
||||
check_cxx_source_compiles("${code}" CXX_FILESYSTEM_STDCPPFS_NEEDED)
|
||||
set(_found ${CXX_FILESYSTEM_STDCPPFS_NEEDED})
|
||||
if(NOT CXX_FILESYSTEM_STDCPPFS_NEEDED)
|
||||
# Try the libc++ flag
|
||||
set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lc++fs)
|
||||
check_cxx_source_compiles("${code}" CXX_FILESYSTEM_CPPFS_NEEDED)
|
||||
set(_found ${CXX_FILESYSTEM_CPPFS_NEEDED})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(_found)
|
||||
add_library(std::filesystem INTERFACE IMPORTED)
|
||||
set_property(TARGET std::filesystem APPEND PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_17)
|
||||
|
||||
if(CXX_FILESYSTEM_NO_LINK_NEEDED)
|
||||
# Nothing to add...
|
||||
elseif(CXX_FILESYSTEM_STDCPPFS_NEEDED)
|
||||
set_target_properties(std::filesystem PROPERTIES IMPORTED_LIBNAME stdc++fs)
|
||||
elseif(CXX_FILESYSTEM_CPPFS_NEEDED)
|
||||
set_target_properties(std::filesystem PROPERTIES IMPORTED_LIBNAME c++fs)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
cmake_pop_check_state()
|
||||
|
||||
set(Filesystem_FOUND ${_found} CACHE BOOL "TRUE if we can run a program using std::filesystem" FORCE)
|
||||
|
||||
if(Filesystem_FIND_REQUIRED AND NOT Filesystem_FOUND)
|
||||
message(FATAL_ERROR "Cannot run simple program using std::filesystem")
|
||||
endif()
|
||||
|
||||
11
cmake/FindSphinx.cmake
Normal file
11
cmake/FindSphinx.cmake
Normal file
@@ -0,0 +1,11 @@
|
||||
#Look for an executable called sphinx-build
|
||||
find_program(SPHINX_EXECUTABLE
|
||||
NAMES sphinx-build
|
||||
DOC "Path to sphinx-build executable")
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
|
||||
#Handle standard arguments to find_package like REQUIRED and QUIET
|
||||
find_package_handle_standard_args(Sphinx
|
||||
"Failed to find sphinx-build executable"
|
||||
SPHINX_EXECUTABLE)
|
||||
@@ -1,284 +0,0 @@
|
||||
# - Returns a version string from Git
|
||||
#
|
||||
# These functions force a re-configure on each git commit so that you can
|
||||
# trust the values of the variables in your build system.
|
||||
#
|
||||
# get_git_head_revision(<refspecvar> <hashvar> [ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR])
|
||||
#
|
||||
# Returns the refspec and sha hash of the current head revision
|
||||
#
|
||||
# git_describe(<var> [<additional arguments to git describe> ...])
|
||||
#
|
||||
# Returns the results of git describe on the source tree, and adjusting
|
||||
# the output so that it tests false if an error occurs.
|
||||
#
|
||||
# git_describe_working_tree(<var> [<additional arguments to git describe> ...])
|
||||
#
|
||||
# Returns the results of git describe on the working tree (--dirty option),
|
||||
# and adjusting the output so that it tests false if an error occurs.
|
||||
#
|
||||
# git_get_exact_tag(<var> [<additional arguments to git describe> ...])
|
||||
#
|
||||
# Returns the results of git describe --exact-match on the source tree,
|
||||
# and adjusting the output so that it tests false if there was no exact
|
||||
# matching tag.
|
||||
#
|
||||
# git_local_changes(<var>)
|
||||
#
|
||||
# Returns either "CLEAN" or "DIRTY" with respect to uncommitted changes.
|
||||
# Uses the return code of "git diff-index --quiet HEAD --".
|
||||
# Does not regard untracked files.
|
||||
#
|
||||
# Requires CMake 2.6 or newer (uses the 'function' command)
|
||||
#
|
||||
# Original Author:
|
||||
# 2009-2020 Ryan Pavlik <ryan.pavlik@gmail.com> <abiryan@ryand.net>
|
||||
# http://academic.cleardefinition.com
|
||||
#
|
||||
# Copyright 2009-2013, Iowa State University.
|
||||
# Copyright 2013-2020, Ryan Pavlik
|
||||
# Copyright 2013-2020, Contributors
|
||||
# SPDX-License-Identifier: BSL-1.0
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# (See accompanying file LICENSE_1_0.txt or copy at
|
||||
# http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
if(__get_git_revision_description)
|
||||
return()
|
||||
endif()
|
||||
set(__get_git_revision_description YES)
|
||||
|
||||
# We must run the following at "include" time, not at function call time,
|
||||
# to find the path to this module rather than the path to a calling list file
|
||||
get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
|
||||
|
||||
# Function _git_find_closest_git_dir finds the next closest .git directory
|
||||
# that is part of any directory in the path defined by _start_dir.
|
||||
# The result is returned in the parent scope variable whose name is passed
|
||||
# as variable _git_dir_var. If no .git directory can be found, the
|
||||
# function returns an empty string via _git_dir_var.
|
||||
#
|
||||
# Example: Given a path C:/bla/foo/bar and assuming C:/bla/.git exists and
|
||||
# neither foo nor bar contain a file/directory .git. This wil return
|
||||
# C:/bla/.git
|
||||
#
|
||||
function(_git_find_closest_git_dir _start_dir _git_dir_var)
|
||||
set(cur_dir "${_start_dir}")
|
||||
set(git_dir "${_start_dir}/.git")
|
||||
while(NOT EXISTS "${git_dir}")
|
||||
# .git dir not found, search parent directories
|
||||
set(git_previous_parent "${cur_dir}")
|
||||
get_filename_component(cur_dir "${cur_dir}" DIRECTORY)
|
||||
if(cur_dir STREQUAL git_previous_parent)
|
||||
# We have reached the root directory, we are not in git
|
||||
set(${_git_dir_var}
|
||||
""
|
||||
PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
set(git_dir "${cur_dir}/.git")
|
||||
endwhile()
|
||||
set(${_git_dir_var}
|
||||
"${git_dir}"
|
||||
PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function(get_git_head_revision _refspecvar _hashvar)
|
||||
_git_find_closest_git_dir("${CMAKE_CURRENT_SOURCE_DIR}" GIT_DIR)
|
||||
|
||||
if("${ARGN}" STREQUAL "ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR")
|
||||
set(ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR TRUE)
|
||||
else()
|
||||
set(ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR FALSE)
|
||||
endif()
|
||||
if(NOT "${GIT_DIR}" STREQUAL "")
|
||||
file(RELATIVE_PATH _relative_to_source_dir "${CMAKE_SOURCE_DIR}"
|
||||
"${GIT_DIR}")
|
||||
if("${_relative_to_source_dir}" MATCHES "[.][.]" AND NOT ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR)
|
||||
# We've gone above the CMake root dir.
|
||||
set(GIT_DIR "")
|
||||
endif()
|
||||
endif()
|
||||
if("${GIT_DIR}" STREQUAL "")
|
||||
set(${_refspecvar}
|
||||
"GITDIR-NOTFOUND"
|
||||
PARENT_SCOPE)
|
||||
set(${_hashvar}
|
||||
"GITDIR-NOTFOUND"
|
||||
PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
|
||||
# Check if the current source dir is a git submodule or a worktree.
|
||||
# In both cases .git is a file instead of a directory.
|
||||
#
|
||||
if(NOT IS_DIRECTORY ${GIT_DIR})
|
||||
# The following git command will return a non empty string that
|
||||
# points to the super project working tree if the current
|
||||
# source dir is inside a git submodule.
|
||||
# Otherwise the command will return an empty string.
|
||||
#
|
||||
execute_process(
|
||||
COMMAND "${GIT_EXECUTABLE}" rev-parse
|
||||
--show-superproject-working-tree
|
||||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
OUTPUT_VARIABLE out
|
||||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(NOT "${out}" STREQUAL "")
|
||||
# If out is empty, GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a submodule
|
||||
file(READ ${GIT_DIR} submodule)
|
||||
string(REGEX REPLACE "gitdir: (.*)$" "\\1" GIT_DIR_RELATIVE
|
||||
${submodule})
|
||||
string(STRIP ${GIT_DIR_RELATIVE} GIT_DIR_RELATIVE)
|
||||
get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
|
||||
get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE}
|
||||
ABSOLUTE)
|
||||
set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD")
|
||||
else()
|
||||
# GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a worktree
|
||||
file(READ ${GIT_DIR} worktree_ref)
|
||||
# The .git directory contains a path to the worktree information directory
|
||||
# inside the parent git repo of the worktree.
|
||||
#
|
||||
string(REGEX REPLACE "gitdir: (.*)$" "\\1" git_worktree_dir
|
||||
${worktree_ref})
|
||||
string(STRIP ${git_worktree_dir} git_worktree_dir)
|
||||
_git_find_closest_git_dir("${git_worktree_dir}" GIT_DIR)
|
||||
set(HEAD_SOURCE_FILE "${git_worktree_dir}/HEAD")
|
||||
endif()
|
||||
else()
|
||||
set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD")
|
||||
endif()
|
||||
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
|
||||
if(NOT EXISTS "${GIT_DATA}")
|
||||
file(MAKE_DIRECTORY "${GIT_DATA}")
|
||||
endif()
|
||||
|
||||
if(NOT EXISTS "${HEAD_SOURCE_FILE}")
|
||||
return()
|
||||
endif()
|
||||
set(HEAD_FILE "${GIT_DATA}/HEAD")
|
||||
configure_file("${HEAD_SOURCE_FILE}" "${HEAD_FILE}" COPYONLY)
|
||||
|
||||
configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
|
||||
"${GIT_DATA}/grabRef.cmake" @ONLY)
|
||||
include("${GIT_DATA}/grabRef.cmake")
|
||||
|
||||
set(${_refspecvar}
|
||||
"${HEAD_REF}"
|
||||
PARENT_SCOPE)
|
||||
set(${_hashvar}
|
||||
"${HEAD_HASH}"
|
||||
PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function(git_describe _var)
|
||||
if(NOT GIT_FOUND)
|
||||
find_package(Git QUIET)
|
||||
endif()
|
||||
get_git_head_revision(refspec hash)
|
||||
if(NOT GIT_FOUND)
|
||||
set(${_var}
|
||||
"GIT-NOTFOUND"
|
||||
PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
if(NOT hash)
|
||||
set(${_var}
|
||||
"HEAD-HASH-NOTFOUND"
|
||||
PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
|
||||
# TODO sanitize
|
||||
#if((${ARGN}" MATCHES "&&") OR
|
||||
# (ARGN MATCHES "||") OR
|
||||
# (ARGN MATCHES "\\;"))
|
||||
# message("Please report the following error to the project!")
|
||||
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
|
||||
#endif()
|
||||
|
||||
#message(STATUS "Arguments to execute_process: ${ARGN}")
|
||||
|
||||
execute_process(
|
||||
COMMAND "${GIT_EXECUTABLE}" describe --tags --always ${hash} ${ARGN}
|
||||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
RESULT_VARIABLE res
|
||||
OUTPUT_VARIABLE out
|
||||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(NOT res EQUAL 0)
|
||||
set(out "${out}-${res}-NOTFOUND")
|
||||
endif()
|
||||
|
||||
set(${_var}
|
||||
"${out}"
|
||||
PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function(git_describe_working_tree _var)
|
||||
if(NOT GIT_FOUND)
|
||||
find_package(Git QUIET)
|
||||
endif()
|
||||
if(NOT GIT_FOUND)
|
||||
set(${_var}
|
||||
"GIT-NOTFOUND"
|
||||
PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND "${GIT_EXECUTABLE}" describe --dirty ${ARGN}
|
||||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
RESULT_VARIABLE res
|
||||
OUTPUT_VARIABLE out
|
||||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(NOT res EQUAL 0)
|
||||
set(out "${out}-${res}-NOTFOUND")
|
||||
endif()
|
||||
|
||||
set(${_var}
|
||||
"${out}"
|
||||
PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function(git_get_exact_tag _var)
|
||||
git_describe(out --exact-match ${ARGN})
|
||||
set(${_var}
|
||||
"${out}"
|
||||
PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function(git_local_changes _var)
|
||||
if(NOT GIT_FOUND)
|
||||
find_package(Git QUIET)
|
||||
endif()
|
||||
get_git_head_revision(refspec hash)
|
||||
if(NOT GIT_FOUND)
|
||||
set(${_var}
|
||||
"GIT-NOTFOUND"
|
||||
PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
if(NOT hash)
|
||||
set(${_var}
|
||||
"HEAD-HASH-NOTFOUND"
|
||||
PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND "${GIT_EXECUTABLE}" diff-index --quiet HEAD --
|
||||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
RESULT_VARIABLE res
|
||||
OUTPUT_VARIABLE out
|
||||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(res EQUAL 0)
|
||||
set(${_var}
|
||||
"CLEAN"
|
||||
PARENT_SCOPE)
|
||||
else()
|
||||
set(${_var}
|
||||
"DIRTY"
|
||||
PARENT_SCOPE)
|
||||
endif()
|
||||
endfunction()
|
||||
@@ -1,43 +0,0 @@
|
||||
#
|
||||
# Internal file for GetGitRevisionDescription.cmake
|
||||
#
|
||||
# Requires CMake 2.6 or newer (uses the 'function' command)
|
||||
#
|
||||
# Original Author:
|
||||
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
|
||||
# http://academic.cleardefinition.com
|
||||
# Iowa State University HCI Graduate Program/VRAC
|
||||
#
|
||||
# Copyright 2009-2012, Iowa State University
|
||||
# Copyright 2011-2015, Contributors
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# (See accompanying file LICENSE_1_0.txt or copy at
|
||||
# http://www.boost.org/LICENSE_1_0.txt)
|
||||
# SPDX-License-Identifier: BSL-1.0
|
||||
|
||||
set(HEAD_HASH)
|
||||
|
||||
file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024)
|
||||
|
||||
string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS)
|
||||
if(HEAD_CONTENTS MATCHES "ref")
|
||||
# named branch
|
||||
string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}")
|
||||
if(EXISTS "@GIT_DIR@/${HEAD_REF}")
|
||||
configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
|
||||
else()
|
||||
configure_file("@GIT_DIR@/packed-refs" "@GIT_DATA@/packed-refs" COPYONLY)
|
||||
file(READ "@GIT_DATA@/packed-refs" PACKED_REFS)
|
||||
if(${PACKED_REFS} MATCHES "([0-9a-z]*) ${HEAD_REF}")
|
||||
set(HEAD_HASH "${CMAKE_MATCH_1}")
|
||||
endif()
|
||||
endif()
|
||||
else()
|
||||
# detached HEAD
|
||||
configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY)
|
||||
endif()
|
||||
|
||||
if(NOT HEAD_HASH)
|
||||
file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024)
|
||||
string(STRIP "${HEAD_HASH}" HEAD_HASH)
|
||||
endif()
|
||||
275
cmake/VersionString.cmake
Normal file
275
cmake/VersionString.cmake
Normal file
@@ -0,0 +1,275 @@
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
# Copyright (c) 2021-2023 Maarten L. Hekkelman
|
||||
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this
|
||||
# list of conditions and the following disclaimer
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
# This cmake extension writes out a revision.hpp file in a specified directory.
|
||||
# The file will contain a C++ inline function that can be used to write out
|
||||
# version information.
|
||||
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
|
||||
# We want the revision.hpp file to be updated whenever the status of the
|
||||
# git repository changes. Use the same technique as in GetGitRevisionDescription.cmake
|
||||
# from https://github.com/rpavlik/cmake-modules
|
||||
|
||||
|
||||
#[=======================================================================[.rst:
|
||||
.. command:: write_version_header
|
||||
|
||||
Write a file named revision.hpp containing version info::
|
||||
|
||||
write_version_header(<destdir>
|
||||
[FILE_NAME <file-name>]
|
||||
[LIB_NAME <library-name>]
|
||||
)
|
||||
|
||||
This command will generate the code to write a file name
|
||||
revision.hpp in the directory ``<destdir>``.
|
||||
|
||||
``FILE_NAME``
|
||||
Specify the name of the file to create, default is ``revision.hpp``.
|
||||
|
||||
``LIB_NAME``
|
||||
Specify the library name which will be used as a prefix part for the
|
||||
variables contained in the revision file.
|
||||
#]=======================================================================]
|
||||
|
||||
# Record the location of this module now, not at the time the CMakeLists.txt
|
||||
# is being processed
|
||||
get_filename_component(_current_cmake_module_dir ${CMAKE_CURRENT_LIST_FILE} PATH)
|
||||
|
||||
# First locate a .git file or directory.
|
||||
function(_get_git_dir _start_dir _variable)
|
||||
|
||||
set(cur_dir "${_start_dir}")
|
||||
set(git_dir "${_start_dir}/.git")
|
||||
|
||||
while(NOT EXISTS "${git_dir}")
|
||||
# .git dir not found, search parent directories
|
||||
set(prev_dir "${cur_dir}")
|
||||
get_filename_component(cur_dir "${cur_dir}" DIRECTORY)
|
||||
if(cur_dir STREQUAL prev_dir OR cur_dir STREQUAL ${_start_dir})
|
||||
# we are not in git since we either hit root or
|
||||
# the ${_start_dir} which should be the top
|
||||
set(${_variable} "" PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
set(git_dir "${cur_dir}/.git")
|
||||
endwhile()
|
||||
|
||||
set(${_variable} "${git_dir}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# Locate the git refspec hash and load the hash
|
||||
# This code locates the file containing the git refspec/hash
|
||||
# and loads it. Doing it this way assures that each time the git
|
||||
# repository changes the revision.hpp file gets out of date.
|
||||
function(_get_git_hash _data_dir _variable)
|
||||
|
||||
# Be pessimistic
|
||||
set(_variable "" PARENT_SCOPE)
|
||||
|
||||
# Load git package if needed
|
||||
if(NOT GIT_FOUND)
|
||||
find_package(Git QUIET)
|
||||
endif()
|
||||
|
||||
# And fail if not found
|
||||
if(NOT GIT_FOUND)
|
||||
return()
|
||||
endif()
|
||||
|
||||
# Locate the nearest .git file or directory
|
||||
_get_git_dir(${CMAKE_CURRENT_SOURCE_DIR} GIT_DIR)
|
||||
|
||||
# And fail if not found
|
||||
if("${GIT_DIR}" STREQUAL "")
|
||||
return()
|
||||
endif()
|
||||
|
||||
# Check if the current source dir is a git submodule or a worktree.
|
||||
# In both cases .git is a file instead of a directory.
|
||||
#
|
||||
if(IS_DIRECTORY ${GIT_DIR})
|
||||
set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD")
|
||||
else()
|
||||
# The following git command will return a non empty string that
|
||||
# points to the super project working tree if the current
|
||||
# source dir is inside a git submodule.
|
||||
# Otherwise the command will return an empty string.
|
||||
#
|
||||
execute_process(
|
||||
COMMAND "${GIT_EXECUTABLE}" rev-parse
|
||||
--show-superproject-working-tree
|
||||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
OUTPUT_VARIABLE out
|
||||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(NOT "${out}" STREQUAL "")
|
||||
# If out is not empty, GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a submodule
|
||||
file(READ ${GIT_DIR} submodule)
|
||||
string(REGEX REPLACE "gitdir: (.*)$" "\\1" GIT_DIR_RELATIVE
|
||||
${submodule})
|
||||
string(STRIP ${GIT_DIR_RELATIVE} GIT_DIR_RELATIVE)
|
||||
get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
|
||||
get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE}
|
||||
ABSOLUTE)
|
||||
set(HEAD_SOURCE_FILE "${GIT_DIR}/HEAD")
|
||||
else()
|
||||
# GIT_DIR/CMAKE_CURRENT_SOURCE_DIR is in a worktree
|
||||
file(READ ${GIT_DIR} worktree_ref)
|
||||
# The .git directory contains a path to the worktree information directory
|
||||
# inside the parent git repo of the worktree.
|
||||
#
|
||||
string(REGEX REPLACE "gitdir: (.*)$" "\\1" git_worktree_dir
|
||||
${worktree_ref})
|
||||
string(STRIP ${git_worktree_dir} git_worktree_dir)
|
||||
_get_git_dir("${git_worktree_dir}" GIT_DIR)
|
||||
set(HEAD_SOURCE_FILE "${git_worktree_dir}/HEAD")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Fail if the 'head' file was not found
|
||||
if(NOT EXISTS "${HEAD_SOURCE_FILE}")
|
||||
return()
|
||||
endif()
|
||||
|
||||
# Make a copy of the head file
|
||||
set(HEAD_FILE "${_data_dir}/HEAD")
|
||||
configure_file("${HEAD_SOURCE_FILE}" "${HEAD_FILE}" COPYONLY)
|
||||
|
||||
# Now we create a cmake file that will read the contents of this
|
||||
# head file in the appropriate way
|
||||
file(WRITE "${_data_dir}/grab-ref.cmake.in" [[
|
||||
set(HEAD_HASH)
|
||||
|
||||
file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024)
|
||||
|
||||
string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS)
|
||||
if(HEAD_CONTENTS MATCHES "ref")
|
||||
# named branch
|
||||
string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}")
|
||||
if(EXISTS "@GIT_DIR@/${HEAD_REF}")
|
||||
configure_file("@GIT_DIR@/${HEAD_REF}" "@VERSION_STRING_DATA@/head-ref" COPYONLY)
|
||||
else()
|
||||
configure_file("@GIT_DIR@/packed-refs" "@VERSION_STRING_DATA@/packed-refs" COPYONLY)
|
||||
file(READ "@VERSION_STRING_DATA@/packed-refs" PACKED_REFS)
|
||||
if(${PACKED_REFS} MATCHES "([0-9a-z]*) ${HEAD_REF}")
|
||||
set(HEAD_HASH "${CMAKE_MATCH_1}")
|
||||
endif()
|
||||
endif()
|
||||
else()
|
||||
# detached HEAD
|
||||
configure_file("@GIT_DIR@/HEAD" "@VERSION_STRING_DATA@/head-ref" COPYONLY)
|
||||
endif()
|
||||
|
||||
if(NOT HEAD_HASH)
|
||||
file(READ "@VERSION_STRING_DATA@/head-ref" HEAD_HASH LIMIT 1024)
|
||||
string(STRIP "${HEAD_HASH}" HEAD_HASH)
|
||||
endif()
|
||||
]])
|
||||
|
||||
configure_file("${VERSION_STRING_DATA}/grab-ref.cmake.in"
|
||||
"${VERSION_STRING_DATA}/grab-ref.cmake" @ONLY)
|
||||
|
||||
# Include the aforementioned file, this will define
|
||||
# the HEAD_HASH variable we're looking for
|
||||
include("${VERSION_STRING_DATA}/grab-ref.cmake")
|
||||
|
||||
set(${_variable} "${HEAD_HASH}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# Create a revision file, containing the current git version info, if any
|
||||
function(write_version_header dir)
|
||||
|
||||
set(flags )
|
||||
set(options LIB_NAME FILE_NAME)
|
||||
set(sources )
|
||||
cmake_parse_arguments(VERSION_STRING_OPTION "${flags}" "${options}" "${sources}" ${ARGN})
|
||||
|
||||
# parameter check
|
||||
if(NOT IS_DIRECTORY ${dir})
|
||||
message(FATAL_ERROR "First parameter to write_version_header should be a directory where the final revision.hpp file will be placed")
|
||||
endif()
|
||||
|
||||
if(VERSION_STRING_OPTION_FILE_NAME)
|
||||
set(file_name "${VERSION_STRING_OPTION_FILE_NAME}")
|
||||
else()
|
||||
set(file_name "revision.hpp")
|
||||
endif()
|
||||
|
||||
# Where to store intermediate files
|
||||
set(VERSION_STRING_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/VersionString")
|
||||
if(NOT EXISTS "${VERSION_STRING_DATA}")
|
||||
file(MAKE_DIRECTORY "${VERSION_STRING_DATA}")
|
||||
endif()
|
||||
|
||||
# Load the git hash using the wizzard-like code above.
|
||||
_get_git_hash("${VERSION_STRING_DATA}" GIT_HASH)
|
||||
|
||||
# If git was found, fetch the git description string
|
||||
if(GIT_HASH)
|
||||
execute_process(
|
||||
COMMAND "${GIT_EXECUTABLE}" describe --dirty --match=build
|
||||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
RESULT_VARIABLE res
|
||||
OUTPUT_VARIABLE out
|
||||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
|
||||
if(res EQUAL 0)
|
||||
set(REVISION_STRING "${out}")
|
||||
else()
|
||||
message(STATUS "Git hash not found, does this project has a 'build' tag?")
|
||||
endif()
|
||||
else()
|
||||
message(STATUS "Git hash not found")
|
||||
endif()
|
||||
|
||||
# Check the revision string, if it matches we fill in the required info
|
||||
if(REVISION_STRING MATCHES "build-([0-9]+)-g([0-9a-f]+)(-dirty)?")
|
||||
set(BUILD_NUMBER ${CMAKE_MATCH_1})
|
||||
if(CMAKE_MATCH_3)
|
||||
set(REVISION_GIT_TAGREF "${CMAKE_MATCH_2}*")
|
||||
else()
|
||||
set(REVISION_GIT_TAGREF "${CMAKE_MATCH_2}")
|
||||
endif()
|
||||
|
||||
string(TIMESTAMP REVISION_DATE_TIME "%Y-%m-%dT%H:%M:%SZ" UTC)
|
||||
else()
|
||||
set(REVISION_GIT_TAGREF "")
|
||||
set(BUILD_NUMBER 0)
|
||||
set(REVISION_DATE_TIME "")
|
||||
endif()
|
||||
|
||||
if(VERSION_STRING_OPTION_LIB_NAME)
|
||||
set(VAR_PREFIX "${VERSION_STRING_OPTION_LIB_NAME}")
|
||||
set(IDENT_PREFIX "${VERSION_STRING_OPTION_LIB_NAME}_")
|
||||
set(BOOL_IS_MAIN "false")
|
||||
else()
|
||||
set(VAR_PREFIX "")
|
||||
set(IDENT_PREFIX "")
|
||||
set(BOOL_IS_MAIN "true")
|
||||
endif()
|
||||
|
||||
configure_file("${_current_cmake_module_dir}/revision.hpp.in" "${dir}/${file_name}" @ONLY)
|
||||
endfunction()
|
||||
|
||||
13
cmake/cifpp-config.cmake.in
Normal file
13
cmake/cifpp-config.cmake.in
Normal file
@@ -0,0 +1,13 @@
|
||||
@PACKAGE_INIT@
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/cifpp-targets.cmake")
|
||||
|
||||
set_and_check(CIFPP_SHARE_DIR "@PACKAGE_CIFPP_DATA_DIR@")
|
||||
|
||||
include(CMakeFindDependencyMacro)
|
||||
|
||||
find_dependency(Threads)
|
||||
find_dependency(ZLIB REQUIRED)
|
||||
find_dependency(CURL REQUIRED)
|
||||
|
||||
check_required_components(cifpp)
|
||||
121
cmake/revision.hpp.in
Normal file
121
cmake/revision.hpp.in
Normal file
@@ -0,0 +1,121 @@
|
||||
// This file was generated by VersionString.cmake
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
|
||||
constexpr const char k@VAR_PREFIX@ProjectName[] = "@PROJECT_NAME@";
|
||||
constexpr const char k@VAR_PREFIX@VersionNumber[] = "@PROJECT_VERSION@";
|
||||
constexpr int k@VAR_PREFIX@BuildNumber = @BUILD_NUMBER@;
|
||||
constexpr const char k@VAR_PREFIX@RevisionGitTag[] = "@REVISION_GIT_TAGREF@";
|
||||
constexpr const char k@VAR_PREFIX@RevisionDate[] = "@REVISION_DATE_TIME@";
|
||||
|
||||
#ifndef VERSION_INFO_DEFINED
|
||||
#define VERSION_INFO_DEFINED 1
|
||||
|
||||
namespace version_info_v1_1
|
||||
{
|
||||
|
||||
class version_info_base
|
||||
{
|
||||
public:
|
||||
static void write_version_string(std::ostream &os, bool verbose)
|
||||
{
|
||||
auto s_main = registered_main();
|
||||
if (s_main != nullptr)
|
||||
s_main->write(os, verbose);
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
for (auto lib = registered_libraries(); lib != nullptr; lib = lib->m_next)
|
||||
{
|
||||
os << "-\n";
|
||||
lib->write(os, verbose);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
version_info_base(const char *name, const char *version, int build_number, const char *git_tag, const char *revision_date, bool is_main)
|
||||
: m_name(name)
|
||||
, m_version(version)
|
||||
, m_build_number(build_number)
|
||||
, m_git_tag(git_tag)
|
||||
, m_revision_date(revision_date)
|
||||
{
|
||||
if (is_main)
|
||||
registered_main() = this;
|
||||
else
|
||||
{
|
||||
auto &s_head = registered_libraries();
|
||||
m_next = s_head;
|
||||
s_head = this;
|
||||
}
|
||||
}
|
||||
|
||||
void write(std::ostream &os, bool verbose)
|
||||
{
|
||||
os << m_name << " version " << m_version << '\n';
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
if (m_build_number != 0)
|
||||
{
|
||||
os << "build: " << m_build_number << ' ' << m_revision_date << '\n';
|
||||
if (m_git_tag[0] != 0)
|
||||
os << "git tag: " << m_git_tag << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using version_info_ptr = version_info_base *;
|
||||
|
||||
static version_info_ptr ®istered_main()
|
||||
{
|
||||
static version_info_ptr s_main = nullptr;
|
||||
return s_main;
|
||||
}
|
||||
|
||||
static version_info_ptr ®istered_libraries()
|
||||
{
|
||||
static version_info_ptr s_head = nullptr;
|
||||
return s_head;
|
||||
}
|
||||
|
||||
const char *m_name;
|
||||
const char *m_version;
|
||||
int m_build_number;
|
||||
const char *m_git_tag;
|
||||
const char *m_revision_date;
|
||||
version_info_base *m_next = nullptr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class version_info : public version_info_base
|
||||
{
|
||||
public:
|
||||
using implementation_type = T;
|
||||
|
||||
version_info(const char *name, const char *version, int build_number, const char *git_tag, const char *revision_date, bool is_main)
|
||||
: version_info_base(name, version, build_number, git_tag, revision_date, is_main)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace version_info_v1_1
|
||||
|
||||
inline void write_version_string(std::ostream &os, bool verbose)
|
||||
{
|
||||
version_info_v1_1::version_info_base::write_version_string(os, verbose);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
const class version_info_@IDENT_PREFIX@impl : public version_info_v1_1::version_info<version_info_@IDENT_PREFIX@impl>
|
||||
{
|
||||
public:
|
||||
version_info_@IDENT_PREFIX@impl()
|
||||
: version_info(k@VAR_PREFIX@ProjectName, k@VAR_PREFIX@VersionNumber, k@VAR_PREFIX@BuildNumber, k@VAR_PREFIX@RevisionGitTag, k@VAR_PREFIX@RevisionDate, @BOOL_IS_MAIN@)
|
||||
{
|
||||
}
|
||||
} s_version_info_@IDENT_PREFIX@instance;
|
||||
18
cmake/test-rx.cpp
Normal file
18
cmake/test-rx.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
// See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86164
|
||||
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string s(100'000, '*');
|
||||
std::smatch m;
|
||||
std::regex r("^(.*?)$");
|
||||
|
||||
std::regex_search(s, m, r);
|
||||
|
||||
std::cout << s.substr(0, 10) << '\n';
|
||||
std::cout << m.str(1).substr(0, 10) << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
48
docs/CMakeLists.txt
Normal file
48
docs/CMakeLists.txt
Normal file
@@ -0,0 +1,48 @@
|
||||
find_package(Doxygen REQUIRED)
|
||||
find_package(Sphinx REQUIRED)
|
||||
|
||||
# Find all the public headers
|
||||
# get_target_property(CIFPP_PUBLIC_HEADER_DIR libCIFPP INTERFACE_INCLUDE_DIRECTORIES)
|
||||
set(CIFPP_PUBLIC_HEADER_DIR ${PROJECT_SOURCE_DIR}/include)
|
||||
file(GLOB_RECURSE CIFPP_PUBLIC_HEADERS ${CIFPP_PUBLIC_HEADER_DIR}/*.hpp)
|
||||
|
||||
set(DOXYGEN_INPUT_DIR ${CIFPP_PUBLIC_HEADER_DIR})
|
||||
set(DOXYGEN_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/xml)
|
||||
set(DOXYGEN_INDEX_FILE ${DOXYGEN_OUTPUT_DIR}/index.xml)
|
||||
set(DOXYFILE_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
|
||||
set(DOXYFILE_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
|
||||
|
||||
# Replace variables inside @@ with the current values
|
||||
configure_file(${DOXYFILE_IN} ${DOXYFILE_OUT} @ONLY)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${DOXYGEN_OUTPUT_DIR}
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory ${DOXYGEN_OUTPUT_DIR})
|
||||
|
||||
add_custom_command(OUTPUT ${DOXYGEN_INDEX_FILE}
|
||||
BYPRODUCTS ${DOXYGEN_OUTPUT_DIR}
|
||||
DEPENDS ${DOXYGEN_OUTPUT_DIR} ${CIFPP_PUBLIC_HEADERS} ${DOXYFILE_OUT}
|
||||
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYFILE_OUT}
|
||||
MAIN_DEPENDENCY ${DOXYFILE_OUT} ${DOXYFILE_IN}
|
||||
COMMENT "Generating docs")
|
||||
|
||||
add_custom_target("Doxygen-${PROJECT_NAME}" ALL DEPENDS ${DOXYGEN_INDEX_FILE})
|
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/conf.py.in ${CMAKE_CURRENT_SOURCE_DIR}/conf.py @ONLY)
|
||||
|
||||
set(SPHINX_SOURCE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
set(SPHINX_BUILD ${CMAKE_CURRENT_BINARY_DIR}/sphinx)
|
||||
|
||||
add_custom_target("Sphinx-${PROJECT_NAME}" ALL
|
||||
COMMAND ${SPHINX_EXECUTABLE} -b html
|
||||
-Dbreathe_projects.${PROJECT_NAME}=${DOXYGEN_OUTPUT_DIR}
|
||||
${SPHINX_SOURCE} ${SPHINX_BUILD}
|
||||
DEPENDS ${DOXYGEN_INDEX_FILE}
|
||||
BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/api
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
COMMENT "Generating documentation with Sphinx")
|
||||
|
||||
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/sphinx/
|
||||
DESTINATION ${CMAKE_INSTALL_DOCDIR}
|
||||
PATTERN .doctrees EXCLUDE
|
||||
PATTERN .buildinfo EXCLUDE)
|
||||
10
docs/Doxyfile.in
Normal file
10
docs/Doxyfile.in
Normal file
@@ -0,0 +1,10 @@
|
||||
EXCLUDE_SYMBOLS = cif::detail::*, std*
|
||||
FILE_PATTERNS = *.hpp
|
||||
STRIP_FROM_PATH = @DOXYGEN_INPUT_DIR@
|
||||
RECURSIVE = YES
|
||||
GENERATE_XML = YES
|
||||
GENERATE_LATEX = NO
|
||||
PREDEFINED += and=&& or=|| not=! CIFPP_EXPORT= HAVE_LIBCLIPPER=1
|
||||
GENERATE_HTML = NO
|
||||
GENERATE_TODOLIST = NO
|
||||
INPUT = @DOXYGEN_INPUT_DIR@
|
||||
4
docs/_static/.gitignore
vendored
Normal file
4
docs/_static/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# Ignore everything in this directory
|
||||
*
|
||||
# Except this file
|
||||
!.gitignore
|
||||
400
docs/basics.rst
Normal file
400
docs/basics.rst
Normal file
@@ -0,0 +1,400 @@
|
||||
Basic usage
|
||||
===========
|
||||
|
||||
This library, *libcifpp*, is a generic *CIF* library with some specific additions to work with *mmCIF* files. The main focus of this library is to make sure that files read or written are valid. That is, they are syntactically valid *and* their content is valid with respect to a CIF dictionary, if such a dictionary is available and specified.
|
||||
|
||||
Reading a file is as simple as:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cif++.hpp>
|
||||
|
||||
cif::file f("/path/to/file.cif");
|
||||
|
||||
The file may also be compressed using *gzip* which is detected automatically.
|
||||
|
||||
Writing out the file again is also simple, to write out the terminal you can do:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
std::cout << f;
|
||||
|
||||
// or
|
||||
f.save(std::cout);
|
||||
|
||||
// or write a compressed file using gzip compression:
|
||||
f.save("/tmp/f.cif.gz");
|
||||
|
||||
CIF files contain one or more datablocks. To print out the names of all datablocks in our file:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
for (auto &db : f)
|
||||
std::cout << db.name() << '\n';
|
||||
|
||||
Most often *libcifpp* is used to read in structure files in mmCIF format. These files only contain one datablock and so you can safely use code like this:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// get a reference to the first datablock in f
|
||||
auto &db = f.front();
|
||||
|
||||
But if you know the name of the datablock, this also works:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// get a reference to the datablock name '1CBS'
|
||||
auto &db = f["1CBS"];
|
||||
|
||||
Now, each datablock contains categories. To print out all their names:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
for (auto &cat : db)
|
||||
std::cout << cat.name() << '\n';
|
||||
|
||||
But you probably know what category you need to use, so lets fetch it by name:
|
||||
|
||||
.. _atom_site-label:
|
||||
.. code-block:: cpp
|
||||
|
||||
// get a reference to the atom_site category in db
|
||||
auto &atom_site = db["atom_site"];
|
||||
|
||||
// and make sure there's some data in it:
|
||||
assert(not atom_site.empty());
|
||||
|
||||
.. note::
|
||||
|
||||
Note that we omit the leading underscore in the name of the category here.
|
||||
|
||||
Categories contain rows of data and each row has fields or items. Referencing a row in a category results in a :cpp:class:`cif::row_handle` object which you can use to request or manipulate item data.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// Get the first row in atom_site
|
||||
auto rh = atom_site.front();
|
||||
|
||||
// Get the label_atom_id value from this row handle as a std::string
|
||||
std::string atom_id = rh["label_atom_id"].as<std::string>();
|
||||
|
||||
// Get the x, y and z coordinates using structered binding
|
||||
const auto &[x, y, z] = rh.get<float,float,float>("Cartn_x", "Cartn_y", "Cartn_z");
|
||||
|
||||
// Assign a new value to the x coordinate or our atom
|
||||
rh["Cartn_x"] = x + 1;
|
||||
|
||||
Querying
|
||||
--------
|
||||
|
||||
Walking over the rows in a category is often not very useful. More often you are interested in specific rows in a category. The function :cpp:func:`cif::category::find` and friends are here to help.
|
||||
|
||||
What these functions have in common is that they return data based on a query implemented by :cpp:class:`cif::condition`. These condition objects are built in code using regular C++ syntax. The most basic example of a query is:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = cif::key("id") == 1;
|
||||
|
||||
Here the condition is that all rows returned should have a value of 1 in there item named *id*. Likewise you can use other data types and even combine those. Oh, and I said we use regular C++ syntax for conditions, so you may as well use other operators to compare values:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// condition for C-alpha atoms having an occupancy less than 1.0
|
||||
cif::condition c = cif::key("occupancy") < 1.0f and cif::key("label_atom_id") == "CA";
|
||||
|
||||
Using the namespace *cif::literals* that code becomes a little less verbose:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
using namespace cif::literals;
|
||||
cif::condition c = "occupancy"_key < 1.0f and "label_atom_id"_key == "CA";
|
||||
|
||||
Conditions can also be combined:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = "occupancy"_key < 1.0f and "label_atom_id"_key == "CA";
|
||||
|
||||
// extend the condition by requiring the compound ID to be unequal to PRO
|
||||
c = std::move(c) and "label_comp_id"_key != "PRO";
|
||||
|
||||
.. note::
|
||||
|
||||
Note the use of std::move here.
|
||||
|
||||
Using queries constructed in this way is simple:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = ...
|
||||
auto result = atom_site.find(std::move(c));
|
||||
|
||||
// or construct a condition inline:
|
||||
auto result = atom_site.find("label_atom_id"_key == "CA");
|
||||
|
||||
In the example above the result is a range of :cpp:class:`cif::row_handle` objects. Often, using individual field values is more useful:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// Requesting a single item:
|
||||
for (auto id : atom_site.find<std::string>("label_atom_id"_key == "CA", "id"))
|
||||
std::cout << "ID for CA: " << id << '\n';
|
||||
|
||||
// Requesting multiple items:
|
||||
for (const auto &[id, x, y, z] : atom_site.find<std::string,float,float,float>("label_atom_id"_key == "CA",
|
||||
"id", "Cartn_x", "Cartn_y", "Cartn_z"))
|
||||
{
|
||||
std::cout << "Atom " << id << " is at [" << x << ", " << y << ", " z << "]\n";
|
||||
}
|
||||
|
||||
Returning a complete set if often not required, if you only want to have the first you can use :cpp:func:`cif::category::find_first` as shown here:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// return the ID item for the first C-alpha atom
|
||||
std::string v1 = atom_site.find_first<std::string>("label_atom_id"_key == "CA", "id");
|
||||
|
||||
// If you're not sure the row exists, use std::optional
|
||||
auto v2 = atom_site.find_first<std::optional<std::string>>("label_atom_id"_key == "CA", "id");
|
||||
if (v2.has_value())
|
||||
...
|
||||
|
||||
There are cases when you really need exactly one result. The :cpp:func:`cif::category::find1` can be used in that case, it will throw an exception if the query does not result in exactly one row.
|
||||
|
||||
NULL and ANY
|
||||
------------
|
||||
|
||||
Sometimes items may be empty. The trouble is a bit that empty comes in two flavors: unknown and null. Null in *CIF* parlance means the item should not contain a value since it makes no sense in this case, the value stored in the file is a single dot character: ``'.'``. E.g. *atom_site* records may have a NULL value for label_seq_id for atoms that are part of a *non-polymer*.
|
||||
|
||||
The other empty value is indicated by a question mark character: ``'?'``. This means the value is simply unknown.
|
||||
|
||||
Both these are NULL in *libcifpp* conditions and can be searched for using :cpp:var:`cif::null`.
|
||||
|
||||
So you can search for:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = "label_seq_id"_key == cif::null;
|
||||
|
||||
You might also want to look for a certain value and don't care in which item it is stored, in that case you can use :cpp:var:`cif::any`.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = cif::any == "foo";
|
||||
|
||||
And in linked record you might have the items that have a value in both parent and child or both should be NULL. For that, you can request the value to return by find to be of type std::optional and then use that value to build the query. An example to explain this, let's find the location of the atom that is referenced as the first atom in a struct_conn record:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// Take references to the two categories we need
|
||||
auto struct_conn = db["struct_conn"];
|
||||
auto atom_site = db["atom_site"];
|
||||
|
||||
// Loop over all rows in struct_conn taking only the values we need
|
||||
// Note that the label_seq_id is returned as a std::optional<int>
|
||||
// That means it may contain an integer or may be empty
|
||||
for (const auto &[asym1, seqid1, authseqid1, atomid1] :
|
||||
struct_conn.rows<std::string,std::optional<int>,std::string,std::string,std::string>(
|
||||
"ptnr1_label_asym_id", "ptnr1_label_seq_id", "ptnr1_auth_seq_id", "ptnr1_label_atom_id"
|
||||
))
|
||||
{
|
||||
// Find the location of the first atom
|
||||
cif::point p1 = atom_site.find1<float,float,float>(
|
||||
"label_asym_id"_key == asym1 and "label_seq_id"_key == seqid1 and "auth_seq_id"_key == authseqid1 and "label_atom_id"_key == atomid1,
|
||||
"cartn_x", "cartn_y", "cartn_z");
|
||||
}
|
||||
|
||||
|
||||
Validation
|
||||
----------
|
||||
|
||||
CIF files can have a dictionary attached. And based on such a dictionary a :cpp:class:`cif::validator` object can be constructed which in turn can be used to validate the content of the file.
|
||||
|
||||
A simple case:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cif++.hpp>
|
||||
|
||||
cif::file f("1cbs.cif.gz");
|
||||
f.load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
if (not f.is_valid())
|
||||
std::cout << "This file is not valid\n";
|
||||
|
||||
If you want to know why it is not valid, you should set the global variable :cpp:var:`cif::VERBOSE` to something higer than zero. Depending on the value more or less diagnostic output is sent to std::cerr.
|
||||
|
||||
In the case above we load a dictionary based on its name. You can of course also load dictionaries based on a specific file, that's a bit more work:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
std::filesystem::ifstream dictFile("/tmp/my-dictionary.dic");
|
||||
auto &validator = cif::parse_dictionary("my-dictionary", dictFile);
|
||||
|
||||
cif::file f("1cbs.cif.gz");
|
||||
|
||||
// assign the validator
|
||||
f.set_validator(&validator);
|
||||
|
||||
// alternatively, load it by name
|
||||
f.load_dictionary("my-dictionary");
|
||||
|
||||
if (not f.is_valid())
|
||||
std::cout << "This file is not valid\n";
|
||||
|
||||
Creating your own dictionary is a lot of work, especially if you are only extending an existing dictionary with a couple of new categories or items. So, what you can do is extend a loaded validator like this (code taken from DSSP):
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// db is a cif::datablock reference containing an mmCIF file with DSSP annotations
|
||||
auto &validator = const_cast<cif::validator &>(*db.get_validator());
|
||||
if (validator.get_validator_for_category("dssp_struct_summary") == nullptr)
|
||||
{
|
||||
auto dssp_extension = cif::load_resource("dssp-extension.dic");
|
||||
if (dssp_extension)
|
||||
cif::extend_dictionary(validator, *dssp_extension);
|
||||
}
|
||||
|
||||
.. note::
|
||||
|
||||
In the example above we're loading the data using :doc:`/resources`. See the documentation on that for more information.
|
||||
|
||||
If a validator has been assigned to a file, assignments to items are checked for valid data. So the following code will throw an exception (see: :ref:`_atom_site-label`):
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
auto rh = atom_site.front();
|
||||
rh["Cartn_x"] = "foo";
|
||||
|
||||
Linking
|
||||
-------
|
||||
|
||||
Based on information recorded in dictionary files (see :ref:`Validation`) you can locate linked records in parent or child categories.
|
||||
|
||||
To make this example not too complex, lets assume the following example file:
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
data_test
|
||||
loop_
|
||||
_cat_1.id
|
||||
_cat_1.name
|
||||
_cat_1.desc
|
||||
1 aap Aap
|
||||
2 noot Noot
|
||||
3 mies Mies
|
||||
|
||||
loop_
|
||||
_cat_2.id
|
||||
_cat_2.name
|
||||
_cat_2.num
|
||||
_cat_2.desc
|
||||
1 aap 1 'Een dier'
|
||||
2 aap 2 'Een andere aap'
|
||||
3 noot 1 'walnoot bijvoorbeeld'
|
||||
|
||||
And we have a dictionary containing the following link definition:
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
loop_
|
||||
_pdbx_item_linked_group_list.parent_category_id
|
||||
_pdbx_item_linked_group_list.link_group_id
|
||||
_pdbx_item_linked_group_list.parent_name
|
||||
_pdbx_item_linked_group_list.child_name
|
||||
_pdbx_item_linked_group_list.child_category_id
|
||||
cat_1 1 '_cat_1.name' '_cat_2.name' cat_2
|
||||
|
||||
So, there are links between *cat_1* and *cat_2* based on the value in items named *name*. Using this information, we can now locate children and parents:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// Assuming the file was loaded in f:
|
||||
auto &cat1 = f.front()["cat_1"];
|
||||
auto &cat2 = f.front()["cat_2"];
|
||||
auto &cat3 = f.front()["cat_3"];
|
||||
|
||||
// Loop over all ape's in cat2
|
||||
for (auto r : cat1.get_children(cat1.find1("name"_key == "aap"), cat2))
|
||||
std::cout << r.get<std::string>("desc") << '\n';
|
||||
|
||||
Updating a value in an item in a parent category will update the corresponding value in all related children:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
auto r1 = cat1.find1("id"_key == 1);
|
||||
r1["name"] = "aapje";
|
||||
|
||||
auto rs1 = cat2.find("name"_key == "aapje");
|
||||
assert(rs1.size() == 2);
|
||||
|
||||
However, changing a value in a child record will not update the parent. This may result in an invalid file since you may then have a child that has no parent:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
auto r2 = cat2.find1("id"_key == 3);
|
||||
r2["name"] = "wim";
|
||||
|
||||
assert(f.is_valid() == false);
|
||||
|
||||
So you have to fix this yourself by inserting a new item in cat1 with the new value.
|
||||
|
||||
.. _splitting-rows:
|
||||
Another situation is when you change a value in a parent and updating children might introduce a situation where you need to split a child. To give an example, consider this:
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
data_test
|
||||
loop_
|
||||
_cat_1.id
|
||||
_cat_1.name
|
||||
_cat_1.desc
|
||||
1 aap Aap
|
||||
2 noot Noot
|
||||
3 mies Mies
|
||||
|
||||
loop_
|
||||
_cat_2.id
|
||||
_cat_2.name
|
||||
_cat_2.num
|
||||
_cat_2.desc
|
||||
1 aap 1 'Een dier'
|
||||
2 aap 2 'Een andere aap'
|
||||
3 noot 1 'walnoot bijvoorbeeld'
|
||||
|
||||
loop_
|
||||
_cat_3.id
|
||||
_cat_3.name
|
||||
_cat_3.num
|
||||
1 aap 1
|
||||
2 aap 2
|
||||
|
||||
And we have a dictionary containing the following link definition (reversed compared to the previous example):
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
loop_
|
||||
_pdbx_item_linked_group_list.parent_category_id
|
||||
_pdbx_item_linked_group_list.link_group_id
|
||||
_pdbx_item_linked_group_list.parent_name
|
||||
_pdbx_item_linked_group_list.child_name
|
||||
_pdbx_item_linked_group_list.child_category_id
|
||||
cat_2 1 '_cat_2.name' '_cat_1.name' cat_1
|
||||
cat_3 1 '_cat_3.name' '_cat_2.name' cat_2
|
||||
cat_3 1 '_cat_3.num' '_cat_2.num' cat_2
|
||||
|
||||
So *cat3* is a parent of *cat2* and *cat2* is a parent of *cat1*. Now, if you change the *name* value of the first row of *cat3* to 'aapje', the corresponding row in *cat2* is updated as well. But when you update *cat2* you have to update *cat1* too. And simply changing the name field in row 1 of *cat1* is wrong. The default behaviour in libcifpp is to split the record in *cat1* and have a new child with the new name whereas the other remains as is.
|
||||
|
||||
The new *cat1* will thus be like:
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
loop_
|
||||
_cat_1.id
|
||||
_cat_1.name
|
||||
_cat_1.desc
|
||||
1 aapje Aap
|
||||
2 noot Noot
|
||||
3 mies Mies
|
||||
5 aap Aap
|
||||
|
||||
49
docs/bitsandpieces.rst
Normal file
49
docs/bitsandpieces.rst
Normal file
@@ -0,0 +1,49 @@
|
||||
Bits & Pieces
|
||||
=============
|
||||
|
||||
The *libcifpp* library offers some extra code that makes the life of developers a bit easier.
|
||||
|
||||
gzio
|
||||
----
|
||||
|
||||
To work with compressed data files a *std::streambuf* implemenation was added based on the code in `gxrio <https://github.com/mhekkel/gxrio>`_. This allows you to read and write compressed data streams transparently.
|
||||
|
||||
When working with files you can use :cpp:class:`cif::gzio::ifstream` and :cpp:class:`cif::gzio::ofstream`. The selection of whether to use compression or not is based on the file extension. If it is ``.gz`` gzip compression is used:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::gzio::ifstream file("my-file.txt.gz");
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
std::cout << line << '\n';
|
||||
|
||||
Writing is equally easy:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::gzio::ofstream file("/tmp/output.txt.gz");
|
||||
file << "Hello, world!";
|
||||
file.close();
|
||||
|
||||
You can also use the :cpp:class:`cif::gzio::istream` and feed it a *std::streambuf* object that may or may not contain compressed data. In that case the first bytes of the input are sniffed and if it is gzip compressed data, decompression will be done.
|
||||
|
||||
A progress bar
|
||||
--------------
|
||||
|
||||
Applications based on *libcifpp* may have a longer run time. To give some feedback to the user running your application in a terminal you can use the :cpp:class:`cif::progress_bar`. This class will display an ASCII progress bar along with optional status messages, but only if output is to a real TTY (terminal).
|
||||
|
||||
A progress bar is also shown only if the duration is more than two seconds. To avoid having flashing progress bars for short actions.
|
||||
|
||||
The progress bar uses an internal progress counter that starts at zero and ends when the max value has been reached after which it will be removed from the screen. Updating this internal progress counter can be done by adding a number of steps calling :cpp:func:`cif::progress_bar::consumed` or by setting the exact value for the counter by calling :cpp:func:`cif::progress_bar::progress`.
|
||||
|
||||
Colouring output
|
||||
----------------
|
||||
|
||||
It is also nice to emphasise some output in the terminal by using colours. For this you can create output manipulators using :cpp:func:`cif::coloured`. To write a string in white, and bold letters on a red background you can do:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
using namespace cif::colour;
|
||||
std::cout << cif::coloured("Hello, world!", white, red, bold) << '\n';
|
||||
|
||||
33
docs/compound.rst
Normal file
33
docs/compound.rst
Normal file
@@ -0,0 +1,33 @@
|
||||
Chemical Compounds
|
||||
==================
|
||||
|
||||
The data in *CIF* and *mmCIF* files often describes the structure of some chemical compounds. The structure is recorded in the categories *atom_site* and friends. Records in these categories refer to chemical compounds using a compound ID. This compound ID is the ID field of the *chem_comp* category. For all of the known compounds in the PDB there is an entry in the Chemical Compounds Dictionary or `CCD <https://www.wwpdb.org/data/ccd>`_. If *libcifpp* was properly installed you have a copy of this file somewhere on your disk. And if you have installed the update scripts, a fresh version of this file will be retrieved weekly.
|
||||
|
||||
As an alternative to CCD there are the monomer library files from `CCP4 <https://www.ccp4.ac.uk/>`_. These contain somewhat different data but the overlap is good enough for usage in *libcifpp*.
|
||||
|
||||
Information about compounds is captured in the :cpp:class:`cif::compound`. An instance of a compound object for a certain compound ID can be obtained by using the singleton :cpp:class:`cif::compound_factory`.
|
||||
|
||||
If the compound you want to use is not available in the CCD or in CCP4, you can add that information yourself. For this you can use the method :cpp:func:`cif::compound_factory::push_dictionary`.
|
||||
|
||||
So, given that we have CCD, CCP4 monomer library and used defined compound definitions, what will you get when you try to retrieve such a compound by ID? The answer is, the factory has a stack of compound generators. The first thrown on the stack is the one for a CCD file (*components.cif*) if it can be found. Then, if the *CLIBD_MON* environmental variable is defined, a generator for monomer library files is added to the stack. And then all generators for files you added using *push_dictionary* are added in order. The generators are searched in the reverse order in which they were added to see if it creates a compound object for the ID. If no compound was created at all, nullptr is returned.
|
||||
|
||||
Updating CCD
|
||||
------------
|
||||
|
||||
The CCD data is stored in a single file called *components.cif* and can be downloaded from `CCD <https://www.wwpdb.org/data/ccd>`_.
|
||||
|
||||
As can be read in the section on resources (:doc:`/resources`) files in libcifpp are loaded in a specific order. If the CCD datafile was downloaded during installation, a copy can be found in the directory */usr/share/libcifpp/* (if you installed in */usr*). This is a static file and will not be updated until the next installation of libcifpp.
|
||||
|
||||
When configuring libcifpp, you can specify the *CIFPP_INSTALL_UPDATE_SCRIPT* option, as in:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
cmake -S . -B build -DCIFPP_INSTALL_UPDATE_SCRIPT=ON # ... more options?
|
||||
|
||||
This will install a script named *update-libcifpp-data* in */etc/cron.weekly* or */etc/periodic/weekly*. This file uses a config file named */etc/libcifpp.conf* which you then need to edit. In this config file the following line needs to be uncommented:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
# update=true
|
||||
|
||||
After that, the update script will weekly download the latest components.cif file to */var/cache/libcifpp*.
|
||||
66
docs/conf.py.in
Normal file
66
docs/conf.py.in
Normal file
@@ -0,0 +1,66 @@
|
||||
project = '@PROJECT_NAME@'
|
||||
copyright = '2023, Maarten L. Hekkelman'
|
||||
author = 'Maarten L. Hekkelman'
|
||||
release = '@PROJECT_VERSION@'
|
||||
|
||||
# -- General configuration ---------------------------------------------------
|
||||
|
||||
extensions = [
|
||||
"breathe",
|
||||
"exhale",
|
||||
"myst_parser"
|
||||
]
|
||||
|
||||
breathe_projects = {
|
||||
"@PROJECT_NAME@": "../build/docs/xml"
|
||||
}
|
||||
|
||||
myst_enable_extensions = [ "colon_fence" ]
|
||||
breathe_default_project = "@PROJECT_NAME@"
|
||||
|
||||
# Setup the exhale extension
|
||||
exhale_args = {
|
||||
# These arguments are required
|
||||
"containmentFolder": "./api",
|
||||
"rootFileName": "library_root.rst",
|
||||
"doxygenStripFromPath": "../include/",
|
||||
# Heavily encouraged optional argument (see docs)
|
||||
"rootFileTitle": "API Reference",
|
||||
# Suggested optional arguments
|
||||
# "createTreeView": True,
|
||||
# TIP: if using the sphinx-bootstrap-theme, you need
|
||||
# "treeViewIsBootstrap": True,
|
||||
"exhaleExecutesDoxygen": False,
|
||||
"contentsDirectives" : False,
|
||||
|
||||
"verboseBuild": False
|
||||
}
|
||||
|
||||
# Tell sphinx what the primary language being documented is.
|
||||
primary_domain = 'cpp'
|
||||
|
||||
# Tell sphinx what the pygments highlight language should be.
|
||||
highlight_language = 'cpp'
|
||||
|
||||
templates_path = ['_templates']
|
||||
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
|
||||
|
||||
# -- Options for HTML output -------------------------------------------------
|
||||
|
||||
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||
# a list of builtin themes.
|
||||
#
|
||||
html_theme = 'sphinx_rtd_theme'
|
||||
|
||||
# Add any paths that contain custom static files (such as style sheets) here,
|
||||
# relative to this directory. They are copied after the builtin static files,
|
||||
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||
html_static_path = ['_static']
|
||||
|
||||
html_theme_options = {
|
||||
}
|
||||
|
||||
cpp_index_common_prefix = [
|
||||
'cif::'
|
||||
]
|
||||
|
||||
2
docs/genindex.rst
Normal file
2
docs/genindex.rst
Normal file
@@ -0,0 +1,2 @@
|
||||
Index
|
||||
=====
|
||||
46
docs/index.rst
Normal file
46
docs/index.rst
Normal file
@@ -0,0 +1,46 @@
|
||||
Introduction
|
||||
============
|
||||
|
||||
Information on 3D structures of proteins originally came formatted in `PDB <http://www.wwpdb.org/documentation/file-format-content/format33/v3.3.html>`_ files. Although the specification for this format had some real restrictions like a mandatory HEADER and CRYST line, many programs implemented this very poorly often writing out only ATOM records. And users became used to this.
|
||||
|
||||
The legacy PDB format has some severe limitations rendering it useless for all but very small protein structures. A new format called `mmCIF <https://mmcif.wwpdb.org/>`_ has been around for decades and now is the default format for the Protein Data Bank.
|
||||
|
||||
The software developed in the `PDB-REDO <https://pdb-redo.eu/>`_ project aims at improving 3D models based on original experimental data. For this, the tools need to be able to work with both legacy PDB and mmCIF files. A decision was made to make mmCIF leading internally in all programs and convert legacy PDB directly into mmCIF before processing the data. A robust conversion had to be developed to make this possible since, as noted above, files can come with more or less information making it sometimes needed to do a sequence alignment to find out the exact residue numbers.
|
||||
|
||||
And so libcif++ came to life, a library to work with mmCIF files. Work on this library started early 2017 and has developed quite a bit since then. To reduce dependency on other libraries, some functionality was added that is not strictly related to reading and writing mmCIF files but may be useful nonetheless. This is mostly code that is used in 3D calculations and symmetry operations.
|
||||
|
||||
Design
|
||||
------
|
||||
|
||||
The main part of the library is a set of classes that work with mmCIF files. They are:
|
||||
|
||||
* :cpp:class:`cif::file`
|
||||
* :cpp:class:`cif::datablock`
|
||||
* :cpp:class:`cif::category`
|
||||
|
||||
The :cpp:class:`cif::file` class encapsulates the contents of a mmCIF file. In such a file there are one or more :cpp:class:`cif::datablock` objects and each datablock contains one or more :cpp:class:`cif::category` objects.
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
|
||||
Using *libcifpp* is easy, if you are familiar with modern C++:
|
||||
|
||||
.. literalinclude:: ../README.md
|
||||
:language: c++
|
||||
:start-after: ```cpp
|
||||
:end-before: ```
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
:caption: Contents
|
||||
|
||||
self
|
||||
basics.rst
|
||||
compound.rst
|
||||
model.rst
|
||||
resources.rst
|
||||
symmetry.rst
|
||||
bitsandpieces.rst
|
||||
api/library_root.rst
|
||||
genindex.rst
|
||||
|
||||
36
docs/model.rst
Normal file
36
docs/model.rst
Normal file
@@ -0,0 +1,36 @@
|
||||
Molecular Model
|
||||
===============
|
||||
|
||||
Theoretically it is possible to get along with only the classes *cif::file*, *cif::datablock* and *cif::category*. But to keep your data complete and valid you then have to update lots of categories for all but the simplest manipulations. For this *libcifpp* comes with a higher level API modelling atoms, residues, monomers, polymers and complete structures in their respective classes.
|
||||
|
||||
Note that these classes only work properly if you are using *mmCIF* files and have an mmcif_pdbx dictionary available, either compiled in using `mrc <https://github.com/mhekkel/mrc.git>`_ or installed in the proper location.
|
||||
|
||||
.. note::
|
||||
|
||||
This part of *libcifpp* is the least developed part. What is available should work but functionality should eventually be extended.
|
||||
|
||||
Atom
|
||||
----
|
||||
|
||||
The :cpp:class:`cif::mm::atom` is a lightweight proxy class giving access to the data stored in *atom_site* and *atom_site_anisotrop*. It only caches the most often used item data and every modification is directly written back into the *mmCIF* categories.
|
||||
|
||||
Atoms can be copied by value with low cost. The atom class only contains a pointer to an implementation that is reference counted.
|
||||
|
||||
Residue, Monomer and Polymer
|
||||
----------------------------
|
||||
|
||||
The :cpp:class:`cif::mm::residue`, :cpp:class:`cif::mm::monomer` and :cpp:class:`cif::mm::polymer` implement what you'd expect. A monomer is a residue that is part of a polymer and thus has a sequence number and siblings.
|
||||
|
||||
Sugars & Branches
|
||||
-----------------
|
||||
|
||||
There are also classes for modelling sugars and sugar branches. You can create sugar branches
|
||||
|
||||
Structure
|
||||
---------
|
||||
|
||||
The :cpp:class:`cif::mm::structure` can be used to load one of the models from an *mmCIF* file. By default the first model is loaded. (Multiple models are often only available files containing structures defined using NMR).
|
||||
|
||||
A structure holds a reference to a *cif::datablock* and retrieves its data from this datablock and writes any modification back into that datablock.
|
||||
|
||||
One of the most useful parts of the structure class is the ability to create and modify residues. This updates related *chem_comp* and *entity* categories as well.
|
||||
5
docs/requirements.in
Normal file
5
docs/requirements.in
Normal file
@@ -0,0 +1,5 @@
|
||||
sphinx<5
|
||||
exhale==0.3.6
|
||||
myst-parser
|
||||
breathe
|
||||
sphinx_rtd_theme==1.3.0
|
||||
93
docs/requirements.txt
Normal file
93
docs/requirements.txt
Normal file
@@ -0,0 +1,93 @@
|
||||
#
|
||||
# This file is autogenerated by pip-compile with Python 3.10
|
||||
# by the following command:
|
||||
#
|
||||
# pip-compile --output-file=requirements.txt requirements.in
|
||||
#
|
||||
alabaster==0.7.13
|
||||
# via sphinx
|
||||
babel==2.12.1
|
||||
# via sphinx
|
||||
beautifulsoup4==4.12.2
|
||||
# via exhale
|
||||
breathe==4.35.0
|
||||
# via
|
||||
# -r requirements.in
|
||||
# exhale
|
||||
certifi==2023.7.22
|
||||
# via requests
|
||||
charset-normalizer==3.2.0
|
||||
# via requests
|
||||
docutils==0.17.1
|
||||
# via
|
||||
# breathe
|
||||
# exhale
|
||||
# myst-parser
|
||||
# sphinx
|
||||
# sphinx-rtd-theme
|
||||
exhale==0.3.6
|
||||
# via -r requirements.in
|
||||
idna==3.4
|
||||
# via requests
|
||||
imagesize==1.4.1
|
||||
# via sphinx
|
||||
jinja2==3.1.2
|
||||
# via
|
||||
# myst-parser
|
||||
# sphinx
|
||||
lxml==4.9.3
|
||||
# via exhale
|
||||
markdown-it-py==2.2.0
|
||||
# via
|
||||
# mdit-py-plugins
|
||||
# myst-parser
|
||||
markupsafe==2.1.3
|
||||
# via jinja2
|
||||
mdit-py-plugins==0.3.5
|
||||
# via myst-parser
|
||||
mdurl==0.1.2
|
||||
# via markdown-it-py
|
||||
myst-parser==0.18.1
|
||||
# via -r requirements.in
|
||||
packaging==23.1
|
||||
# via sphinx
|
||||
pygments==2.16.1
|
||||
# via sphinx
|
||||
pyyaml==6.0.1
|
||||
# via myst-parser
|
||||
requests==2.31.0
|
||||
# via sphinx
|
||||
six==1.16.0
|
||||
# via exhale
|
||||
snowballstemmer==2.2.0
|
||||
# via sphinx
|
||||
soupsieve==2.4.1
|
||||
# via beautifulsoup4
|
||||
sphinx==4.5.0
|
||||
# via
|
||||
# -r requirements.in
|
||||
# breathe
|
||||
# exhale
|
||||
# myst-parser
|
||||
# sphinx-rtd-theme
|
||||
# sphinxcontrib-jquery
|
||||
sphinx-rtd-theme==1.3.0
|
||||
# via -r requirements.in
|
||||
sphinxcontrib-applehelp==1.0.4
|
||||
# via sphinx
|
||||
sphinxcontrib-devhelp==1.0.2
|
||||
# via sphinx
|
||||
sphinxcontrib-htmlhelp==2.0.1
|
||||
# via sphinx
|
||||
sphinxcontrib-jquery==4.1
|
||||
# via sphinx-rtd-theme
|
||||
sphinxcontrib-jsmath==1.0.1
|
||||
# via sphinx
|
||||
sphinxcontrib-qthelp==1.0.3
|
||||
# via sphinx
|
||||
sphinxcontrib-serializinghtml==1.1.5
|
||||
# via sphinx
|
||||
typing-extensions==4.7.1
|
||||
# via myst-parser
|
||||
urllib3==2.0.4
|
||||
# via requests
|
||||
47
docs/resources.rst
Normal file
47
docs/resources.rst
Normal file
@@ -0,0 +1,47 @@
|
||||
Resources
|
||||
=========
|
||||
|
||||
Programs using libcifpp often need access to common data files. E.g. CIF dictionary files, CCP4 monomer restraints files or the CCD data file. In libcifpp these files are called resources. These files are often also based on external sources that are updated on a regular basis.
|
||||
|
||||
Resources can be compiled into the executable so that the resulting
|
||||
application can be made portable to other machines. For this you
|
||||
need to use `mrc <https://github.com/mhekkel/mrc.git>`_ which only works
|
||||
on Un*x like systems using the ELF executable format or on MS Windows
|
||||
|
||||
But resources may also be located as files on the filesytem at
|
||||
specific locations. And you can specify your own location for
|
||||
files (a directory) or even override named resources with your
|
||||
own data.
|
||||
|
||||
Loading Resources
|
||||
-----------------
|
||||
|
||||
No matter where the resource is located, you should always use the single libcifpp API call :cpp:func:`cif::load_resource` to load them. This function returns a *std::istream* wrapped inside a *std::unique_ptr*.
|
||||
|
||||
The order in which resources are searched for is:
|
||||
|
||||
* Use the resource that was defined by calling :cpp:func:`cif::add_file_resource`
|
||||
for this name.
|
||||
|
||||
* Search the paths specified by :cpp:func:`cif::add_data_directory`, last one
|
||||
added is searched first
|
||||
|
||||
* Search the so-called *CACHE_DIR*. This location is defined
|
||||
at compile time and based on the installation directory of
|
||||
libcifpp. Usually it is */var/cache/libcifpp*.
|
||||
It is in this directory where the cron job for libcifpp will
|
||||
put the updated files weekly.
|
||||
|
||||
* If the *CCP4* environment is available, the
|
||||
*$ENV{CCP4}/share/libcifpp* is searched.
|
||||
|
||||
* If the environment variable *LIBCIFPP_DATA_DIR* is set it
|
||||
is searched
|
||||
|
||||
* The *DATA_DIR* is searched, this is also a variable defined
|
||||
at compile time, also based on the installation directory
|
||||
of libcifpp. It usually is */usr/share/libcifpp*
|
||||
|
||||
* As a last resort an attempt is made to load the data from
|
||||
resources compiled by `mrc <https://github.com/mhekkel/mrc.git>`_.
|
||||
|
||||
108
docs/symmetry.rst
Normal file
108
docs/symmetry.rst
Normal file
@@ -0,0 +1,108 @@
|
||||
Symmetry & Geometry
|
||||
===================
|
||||
|
||||
Although not really a core *CIF* functionality, when working with *mmCIF* files you often need to work with symmetry information. And symmetry works on points in a certain space and thus geometry calculations are also something you need often. Former versions of *libcifpp* used to use `clipper <http://www.ysbl.york.ac.uk/~cowtan/clipper/doc/index.html>`_ to do many of these calculations, but that introduces a dependency and besides, the way clipper numbers symmetry operations is not completely compatible with the way this is done in the PDB.
|
||||
|
||||
Points
|
||||
------
|
||||
|
||||
The most basic type in use is :cpp:type:`cif::point`. It can be thought of as a point in space with three coordinates, but it is also often used as a vector in 3d space. To keep the interface simple there's no separate vector type.
|
||||
|
||||
Many functions are available in :ref:`file_cif++_point.hpp` that work on points. There are functions to calculate the :cpp:func:`cif::distance` between two points and also function to calculate dot products, cross products and dihedral angles between sets of points.
|
||||
|
||||
Quaternions
|
||||
-----------
|
||||
|
||||
All operations inside *libcifpp* that perform some kind of rotation use :cpp:type:`cif::quaternion`. The reason to use Quaternions is not only that they are cool, they are faster than multiplying with a matrix and the results also suffer less from numerical instability.
|
||||
|
||||
Matrix
|
||||
------
|
||||
|
||||
Although Quaternions are the preferred way of doing rotations, not every manipulation is a rotation and thus we need a matrix class as well. Matrices and their operations are encoded as matrix_expressions in *libcifpp* allowing the compiler to generate very fast code. See the :ref:`file_cif++_matrix.hpp` for what is on offer.
|
||||
|
||||
Crystals
|
||||
--------
|
||||
|
||||
The *CIF* and *mmCIF* were initially developed to store crystallographic information on structures. Apart from coordinates and the chemical information the crystallographic information is important. This information can be split into two parts, a unit cell and a set of :ref:`symmetry-ops` making up a spacegroup. The spacegroup number and name are stored in the *symmetry* category. The corresponding symmetry operations can be obtained in *libcifpp* by using the :cpp:class:`cif::spacegroup`. The cell is stored in the category *cell* and likewise can be loaded using the :cpp:class:`cif::cell`. Together these two classes make up a crystal and so we have a :cpp:class:`cif::crystal` which contains both. You can easily create such a crystal object by passing the datablock containing the data to the constructor. As in:
|
||||
|
||||
.. code:: cpp
|
||||
|
||||
// Load the file
|
||||
cif::file f("1cbs.cif.gz");
|
||||
|
||||
auto &db = f.front();
|
||||
cif::crystal c(db);
|
||||
|
||||
.. _symmetry-ops:
|
||||
Symmetry operations
|
||||
-------------------
|
||||
|
||||
Each basic symmetry operation in the crystallographic world consists of a matrix multiplication followed by a translation. To apply such an operation on a carthesian coordinate you first have to convert the point into a fractional coordinate with respect to the unit cell of the crystal, then apply the matrix and translation operations and then convert the result back into carthesian coordinates. This is all done by the proper routines in *libcifpp*.
|
||||
|
||||
Symmetry operations are encoded as a string in *mmCIF* PDBx files. The format is a string with the rotational number followed by an underscore and then the encoded translation in each direction where 5 means no translation. So, the identity operator is ``1_555`` meaning that we have rotational number 1 (which is always the identity rotation, point multiplied with the identity matrix) and a translation of zero in each direction.
|
||||
|
||||
To give an idea how this works, here's a piece of code copied from one of the unit tests in *libcifpp*. It takes the *struct_conn* records in a certain PDB file and checks wether the distances in each row correspond to what we can calculate.
|
||||
|
||||
.. code:: cpp
|
||||
|
||||
using namespace cif::literals;
|
||||
|
||||
// Load the file
|
||||
cif::file f(gTestDir / "2bi3.cif.gz");
|
||||
|
||||
// Simply assume we can use the first datablock
|
||||
auto &db = f.front();
|
||||
|
||||
// Load the crystal information
|
||||
cif::crystal c(db);
|
||||
|
||||
// Take references to the two categories we need
|
||||
auto struct_conn = db["struct_conn"];
|
||||
auto atom_site = db["atom_site"];
|
||||
|
||||
// Loop over all rows in struct_conn taking only the values we need
|
||||
for (const auto &[
|
||||
asym1, seqid1, authseqid1, atomid1, symm1,
|
||||
asym2, seqid2, authseqid2, atomid2, symm2,
|
||||
dist] : struct_conn.find<
|
||||
std::string,std::optional<int>,std::string,std::string,std::string,
|
||||
std::string,std::optional<int>,std::string,std::string,std::string,
|
||||
float>(
|
||||
cif::key("ptnr1_symmetry") != "1_555" or cif::key("ptnr2_symmetry") != "1_555",
|
||||
"ptnr1_label_asym_id", "ptnr1_label_seq_id", "ptnr1_auth_seq_id", "ptnr1_label_atom_id", "ptnr1_symmetry",
|
||||
"ptnr2_label_asym_id", "ptnr2_label_seq_id", "ptnr2_auth_seq_id", "ptnr2_label_atom_id", "ptnr2_symmetry",
|
||||
"pdbx_dist_value"
|
||||
))
|
||||
{
|
||||
// Find the location of the first atom
|
||||
cif::point p1 = atom_site.find1<float,float,float>(
|
||||
"label_asym_id"_key == asym1 and "label_seq_id"_key == seqid1 and "auth_seq_id"_key == authseqid1 and "label_atom_id"_key == atomid1,
|
||||
"cartn_x", "cartn_y", "cartn_z");
|
||||
|
||||
// Find the location of the second atom
|
||||
cif::point p2 = atom_site.find1<float,float,float>(
|
||||
"label_asym_id"_key == asym2 and "label_seq_id"_key == seqid2 and "auth_seq_id"_key == authseqid2 and "label_atom_id"_key == atomid2,
|
||||
"cartn_x", "cartn_y", "cartn_z");
|
||||
|
||||
// Calculate the position of the first atom using the symmetry operator defined in struct_conn
|
||||
auto sa1 = c.symmetry_copy(p1, cif::sym_op(symm1));
|
||||
|
||||
// Calculate the position of the second atom using the symmetry operator defined in struct_conn
|
||||
auto sa2 = c.symmetry_copy(p2, cif::sym_op(symm2));
|
||||
|
||||
// The distance between these symmetry atoms should be equal to the distance in the struct_conn record
|
||||
assert(cif::distance(sa1, sa2) == dist);
|
||||
|
||||
// And to show how you can obtain the closest symmetry copy of an atom near another one:
|
||||
// here we request the symmetry copy of p2 that lies closest to p1
|
||||
const auto &[d, p, so] = c.closest_symmetry_copy(p1, p2);
|
||||
|
||||
// And that should of course be equal to the location in struct_conn for p2
|
||||
assert(p.m_x == sa2.m_x);
|
||||
assert(p.m_y == sa2.m_y);
|
||||
assert(p.m_z == sa2.m_z);
|
||||
|
||||
// Distance and symmetry operator string should also be the same
|
||||
assert(d == dist);
|
||||
assert(so.string() == symm2);
|
||||
}
|
||||
7
examples/CMakeLists.txt
Normal file
7
examples/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
project(cifpp_example LANGUAGES CXX)
|
||||
|
||||
find_package(cifpp REQUIRED)
|
||||
|
||||
add_executable(example example.cpp)
|
||||
target_link_libraries(example cifpp::cifpp)
|
||||
@@ -1,32 +1,37 @@
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
#include <cif++/Cif++.hpp>
|
||||
#include <cif++.hpp>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
int main()
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
fs::path in("1cbs.cif.gz");
|
||||
|
||||
cif::File file;
|
||||
|
||||
file.loadDictionary("mmcif_pdbx_v50");
|
||||
|
||||
file.load("1cbs.cif.gz");
|
||||
|
||||
auto& db = file.firstDatablock()["atom_site"];
|
||||
auto n = db.find(cif::Key("label_atom_id") == "OXT").size();
|
||||
|
||||
std::cout << "File contains " << db.size() << " atoms of which " << n << (n == 1 ? " is" : " are") << " OXT" << std::endl
|
||||
<< "residues with an OXT are:" << std::endl;
|
||||
|
||||
for (const auto& [asym, comp, seqnr]: db.find<std::string,std::string,int>(
|
||||
cif::Key("label_atom_id") == "OXT",
|
||||
{ "label_asym_id", "label_comp_id", "label_seq_id" }
|
||||
))
|
||||
if (argc != 2)
|
||||
{
|
||||
std::cout << asym << ' ' << comp << ' ' << seqnr << std::endl;
|
||||
std::cerr << "Usage: example <inputfile>\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
cif::file file(argv[1]);
|
||||
|
||||
if (file.empty())
|
||||
{
|
||||
std::cerr << "Empty file\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
auto &db = file.front();
|
||||
auto &atom_site = db["atom_site"];
|
||||
auto n = atom_site.find(cif::key("label_atom_id") == "OXT").size();
|
||||
|
||||
std::cout << "File contains " << atom_site.size() << " atoms of which " << n << (n == 1 ? " is" : " are") << " OXT\n"
|
||||
<< "residues with an OXT are:\n";
|
||||
|
||||
for (const auto &[asym, comp, seqnr] : atom_site.find<std::string, std::string, int>(
|
||||
cif::key("label_atom_id") == "OXT", "label_asym_id", "label_comp_id", "label_seq_id"))
|
||||
{
|
||||
std::cout << asym << ' ' << comp << ' ' << seqnr << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
CXX = c++ -std=c++17
|
||||
CXXFLAGS = $(shell pkg-config --cflags libcifpp)
|
||||
LIBS = $(shell pkg-config --libs libcifpp)
|
||||
|
||||
all: example
|
||||
|
||||
example: example.cpp
|
||||
$(CXX) -o $@ $? $(CXXFLAGS) $(LIBS)
|
||||
1573
examples/pdb1cbs.ent
1573
examples/pdb1cbs.ent
File diff suppressed because it is too large
Load Diff
41
include/cif++.hpp
Normal file
41
include/cif++.hpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/utilities.hpp"
|
||||
#include "cif++/file.hpp"
|
||||
#include "cif++/parser.hpp"
|
||||
#include "cif++/format.hpp"
|
||||
|
||||
#include "cif++/compound.hpp"
|
||||
#include "cif++/point.hpp"
|
||||
#include "cif++/symmetry.hpp"
|
||||
|
||||
#include "cif++/model.hpp"
|
||||
|
||||
#include "cif++/pdb.hpp"
|
||||
#include "cif++/gzio.hpp"
|
||||
@@ -1,245 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
// Lib for working with structures as contained in mmCIF and PDB files
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
enum AtomType : uint8_t
|
||||
{
|
||||
Nn = 0, // Unknown
|
||||
|
||||
H = 1, // Hydrogen
|
||||
He = 2, // Helium
|
||||
|
||||
Li = 3, // Lithium
|
||||
Be = 4, // Beryllium
|
||||
B = 5, // Boron
|
||||
C = 6, // Carbon
|
||||
N = 7, // Nitrogen
|
||||
O = 8, // Oxygen
|
||||
F = 9, // Fluorine
|
||||
Ne = 10, // Neon
|
||||
|
||||
Na = 11, // Sodium
|
||||
Mg = 12, // Magnesium
|
||||
Al = 13, // Aluminium
|
||||
Si = 14, // Silicon
|
||||
P = 15, // Phosphorus
|
||||
S = 16, // Sulfur
|
||||
Cl = 17, // Chlorine
|
||||
Ar = 18, // Argon
|
||||
|
||||
K = 19, // Potassium
|
||||
Ca = 20, // Calcium
|
||||
Sc = 21, // Scandium
|
||||
Ti = 22, // Titanium
|
||||
V = 23, // Vanadium
|
||||
Cr = 24, // Chromium
|
||||
Mn = 25, // Manganese
|
||||
Fe = 26, // Iron
|
||||
Co = 27, // Cobalt
|
||||
Ni = 28, // Nickel
|
||||
Cu = 29, // Copper
|
||||
Zn = 30, // Zinc
|
||||
Ga = 31, // Gallium
|
||||
Ge = 32, // Germanium
|
||||
As = 33, // Arsenic
|
||||
Se = 34, // Selenium
|
||||
Br = 35, // Bromine
|
||||
Kr = 36, // Krypton
|
||||
|
||||
Rb = 37, // Rubidium
|
||||
Sr = 38, // Strontium
|
||||
Y = 39, // Yttrium
|
||||
Zr = 40, // Zirconium
|
||||
Nb = 41, // Niobium
|
||||
Mo = 42, // Molybdenum
|
||||
Tc = 43, // Technetium
|
||||
Ru = 44, // Ruthenium
|
||||
Rh = 45, // Rhodium
|
||||
Pd = 46, // Palladium
|
||||
Ag = 47, // Silver
|
||||
Cd = 48, // Cadmium
|
||||
In = 49, // Indium
|
||||
Sn = 50, // Tin
|
||||
Sb = 51, // Antimony
|
||||
Te = 52, // Tellurium
|
||||
I = 53, // Iodine
|
||||
Xe = 54, // Xenon
|
||||
Cs = 55, // Caesium
|
||||
Ba = 56, // Barium
|
||||
La = 57, // Lanthanum
|
||||
|
||||
Hf = 72, // Hafnium
|
||||
Ta = 73, // Tantalum
|
||||
W = 74, // Tungsten
|
||||
Re = 75, // Rhenium
|
||||
Os = 76, // Osmium
|
||||
Ir = 77, // Iridium
|
||||
Pt = 78, // Platinum
|
||||
Au = 79, // Gold
|
||||
Hg = 80, // Mercury
|
||||
Tl = 81, // Thallium
|
||||
Pb = 82, // Lead
|
||||
Bi = 83, // Bismuth
|
||||
Po = 84, // Polonium
|
||||
At = 85, // Astatine
|
||||
Rn = 86, // Radon
|
||||
Fr = 87, // Francium
|
||||
Ra = 88, // Radium
|
||||
Ac = 89, // Actinium
|
||||
|
||||
Rf = 104, // Rutherfordium
|
||||
Db = 105, // Dubnium
|
||||
Sg = 106, // Seaborgium
|
||||
Bh = 107, // Bohrium
|
||||
Hs = 108, // Hassium
|
||||
Mt = 109, // Meitnerium
|
||||
Ds = 110, // Darmstadtium
|
||||
Rg = 111, // Roentgenium
|
||||
Cn = 112, // Copernicium
|
||||
Nh = 113, // Nihonium
|
||||
Fl = 114, // Flerovium
|
||||
Mc = 115, // Moscovium
|
||||
Lv = 116, // Livermorium
|
||||
Ts = 117, // Tennessine
|
||||
Og = 118, // Oganesson
|
||||
|
||||
Ce = 58, // Cerium
|
||||
Pr = 59, // Praseodymium
|
||||
Nd = 60, // Neodymium
|
||||
Pm = 61, // Promethium
|
||||
Sm = 62, // Samarium
|
||||
Eu = 63, // Europium
|
||||
Gd = 64, // Gadolinium
|
||||
Tb = 65, // Terbium
|
||||
Dy = 66, // Dysprosium
|
||||
Ho = 67, // Holmium
|
||||
Er = 68, // Erbium
|
||||
Tm = 69, // Thulium
|
||||
Yb = 70, // Ytterbium
|
||||
Lu = 71, // Lutetium
|
||||
|
||||
Th = 90, // Thorium
|
||||
Pa = 91, // Protactinium
|
||||
U = 92, // Uranium
|
||||
Np = 93, // Neptunium
|
||||
Pu = 94, // Plutonium
|
||||
Am = 95, // Americium
|
||||
Cm = 96, // Curium
|
||||
Bk = 97, // Berkelium
|
||||
Cf = 98, // Californium
|
||||
Es = 99, // Einsteinium
|
||||
Fm = 100, // Fermium
|
||||
Md = 101, // Mendelevium
|
||||
No = 102, // Nobelium
|
||||
Lr = 103, // Lawrencium
|
||||
|
||||
D = 129, // Deuterium
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// AtomTypeInfo
|
||||
|
||||
enum RadiusType {
|
||||
eRadiusCalculated,
|
||||
eRadiusEmpirical,
|
||||
eRadiusCovalentEmpirical,
|
||||
|
||||
eRadiusSingleBond,
|
||||
eRadiusDoubleBond,
|
||||
eRadiusTripleBond,
|
||||
|
||||
eRadiusVanderWaals,
|
||||
|
||||
eRadiusTypeCount
|
||||
};
|
||||
|
||||
struct AtomTypeInfo
|
||||
{
|
||||
AtomType type;
|
||||
std::string name;
|
||||
std::string symbol;
|
||||
float weight;
|
||||
bool metal;
|
||||
float radii[eRadiusTypeCount];
|
||||
};
|
||||
|
||||
extern const AtomTypeInfo kKnownAtoms[];
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// AtomTypeTraits
|
||||
|
||||
class AtomTypeTraits
|
||||
{
|
||||
public:
|
||||
AtomTypeTraits(AtomType a);
|
||||
AtomTypeTraits(const std::string& symbol);
|
||||
|
||||
AtomType type() const { return mInfo->type; }
|
||||
std::string name() const { return mInfo->name; }
|
||||
std::string symbol() const { return mInfo->symbol; }
|
||||
float weight() const { return mInfo->weight; }
|
||||
|
||||
bool isMetal() const { return mInfo->metal; }
|
||||
|
||||
static bool isElement(const std::string& symbol);
|
||||
static bool isMetal(const std::string& symbol);
|
||||
|
||||
float radius(RadiusType type = eRadiusSingleBond) const
|
||||
{
|
||||
if (type >= eRadiusTypeCount)
|
||||
throw std::invalid_argument("invalid radius requested");
|
||||
return mInfo->radii[type] / 100.f;
|
||||
}
|
||||
|
||||
// data type encapsulating the Waasmaier & Kirfel scattering factors
|
||||
// in a simplified form (only a and b).
|
||||
// Added the electrion scattering factors as well
|
||||
struct SFData
|
||||
{
|
||||
double a[6], b[6];
|
||||
};
|
||||
|
||||
// to get the Cval and Siva values, use this constant as charge:
|
||||
enum { kWKSFVal = -99 };
|
||||
|
||||
const SFData& wksf(int charge = 0) const;
|
||||
const SFData& elsf() const;
|
||||
|
||||
private:
|
||||
const struct AtomTypeInfo* mInfo;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "cif++/Structure.hpp"
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
class BondMapException : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
BondMapException(const std::string& msg)
|
||||
: runtime_error(msg) {}
|
||||
};
|
||||
|
||||
class BondMap
|
||||
{
|
||||
public:
|
||||
BondMap(const Structure& p);
|
||||
|
||||
BondMap(const BondMap&) = delete;
|
||||
BondMap& operator=(const BondMap&) = delete;
|
||||
|
||||
bool operator()(const Atom& a, const Atom& b) const
|
||||
{
|
||||
return isBonded(index.at(a.id()), index.at(b.id()));
|
||||
}
|
||||
|
||||
bool is1_4(const Atom& a, const Atom& b) const
|
||||
{
|
||||
uint32_t ixa = index.at(a.id());
|
||||
uint32_t ixb = index.at(b.id());
|
||||
|
||||
return bond_1_4.count(key(ixa, ixb));
|
||||
}
|
||||
|
||||
// links coming from the struct_conn records:
|
||||
std::vector<std::string> linked(const Atom& a) const;
|
||||
|
||||
// This list of atomID's is comming from either CCD or the CCP4 dictionaries loaded
|
||||
static std::vector<std::string> atomIDsForCompound(const std::string& compoundID);
|
||||
|
||||
private:
|
||||
|
||||
bool isBonded(uint32_t ai, uint32_t bi) const
|
||||
{
|
||||
return bond.count(key(ai, bi)) != 0;
|
||||
}
|
||||
|
||||
uint64_t key(uint32_t a, uint32_t b) const
|
||||
{
|
||||
if (a > b)
|
||||
std::swap(a, b);
|
||||
return static_cast<uint64_t>(a) | (static_cast<uint64_t>(b) << 32);
|
||||
}
|
||||
|
||||
std::tuple<uint32_t,uint32_t> dekey(uint64_t k) const
|
||||
{
|
||||
return std::make_tuple(
|
||||
static_cast<uint32_t>(k >> 32),
|
||||
static_cast<uint32_t>(k)
|
||||
);
|
||||
}
|
||||
|
||||
uint32_t dim;
|
||||
std::unordered_map<std::string,uint32_t> index;
|
||||
std::set<uint64_t> bond, bond_1_4;
|
||||
|
||||
std::map<std::string,std::set<std::string>> link;
|
||||
};
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,248 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/Cif++.hpp"
|
||||
|
||||
#include <stack>
|
||||
#include <map>
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class CifParserError : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
CifParserError(uint32_t lineNr, const std::string& message);
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
extern const uint32_t kMaxLineLength;
|
||||
|
||||
extern const uint8_t kCharTraitsTable[128];
|
||||
|
||||
enum CharTraitsMask: uint8_t {
|
||||
kOrdinaryMask = 1 << 0,
|
||||
kNonBlankMask = 1 << 1,
|
||||
kTextLeadMask = 1 << 2,
|
||||
kAnyPrintMask = 1 << 3
|
||||
};
|
||||
|
||||
inline bool isWhite(int ch)
|
||||
{
|
||||
return std::isspace(ch) or ch == '#';
|
||||
}
|
||||
|
||||
inline bool isOrdinary(int ch)
|
||||
{
|
||||
return ch >= 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kOrdinaryMask) != 0;
|
||||
}
|
||||
|
||||
inline bool isNonBlank(int ch)
|
||||
{
|
||||
return ch > 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kNonBlankMask) != 0;
|
||||
}
|
||||
|
||||
inline bool isTextLead(int ch)
|
||||
{
|
||||
return ch >= 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kTextLeadMask) != 0;
|
||||
}
|
||||
|
||||
inline bool isAnyPrint(int ch)
|
||||
{
|
||||
return ch == '\t' or
|
||||
(ch >= 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kAnyPrintMask) != 0);
|
||||
}
|
||||
|
||||
inline bool isUnquotedString(const char* s)
|
||||
{
|
||||
bool result = isOrdinary(*s++);
|
||||
while (result and *s != 0)
|
||||
{
|
||||
result = isNonBlank(*s);
|
||||
++s;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
std::tuple<std::string,std::string> splitTagName(const std::string& tag);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
using DatablockIndex = std::map<std::string,std::size_t>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// sac Parser, analogous to SAX Parser (simple api for xml)
|
||||
|
||||
class SacParser
|
||||
{
|
||||
public:
|
||||
SacParser(std::istream& is, bool init = true);
|
||||
virtual ~SacParser() {}
|
||||
|
||||
enum CIFToken
|
||||
{
|
||||
eCIFTokenUnknown,
|
||||
|
||||
eCIFTokenEOF,
|
||||
|
||||
eCIFTokenDATA,
|
||||
eCIFTokenLOOP,
|
||||
eCIFTokenGLOBAL,
|
||||
eCIFTokenSAVE,
|
||||
eCIFTokenSTOP,
|
||||
eCIFTokenTag,
|
||||
eCIFTokenValue,
|
||||
};
|
||||
|
||||
static const char* kTokenName[];
|
||||
|
||||
enum CIFValueType
|
||||
{
|
||||
eCIFValueInt,
|
||||
eCIFValueFloat,
|
||||
eCIFValueNumeric,
|
||||
eCIFValueString,
|
||||
eCIFValueTextField,
|
||||
eCIFValueInapplicable,
|
||||
eCIFValueUnknown
|
||||
};
|
||||
|
||||
static const char* kValueName[];
|
||||
|
||||
int getNextChar();
|
||||
|
||||
void retract();
|
||||
void restart();
|
||||
|
||||
CIFToken getNextToken();
|
||||
void match(CIFToken token);
|
||||
|
||||
bool parseSingleDatablock(const std::string& datablock);
|
||||
|
||||
DatablockIndex indexDatablocks();
|
||||
bool parseSingleDatablock(const std::string& datablock, const DatablockIndex &index);
|
||||
|
||||
void parseFile();
|
||||
void parseGlobal();
|
||||
void parseDataBlock();
|
||||
|
||||
virtual void parseSaveFrame();
|
||||
|
||||
void parseDictionary();
|
||||
|
||||
void error(const std::string& msg);
|
||||
|
||||
// production methods, these are pure virtual here
|
||||
|
||||
virtual void produceDatablock(const std::string& name) = 0;
|
||||
virtual void produceCategory(const std::string& name) = 0;
|
||||
virtual void produceRow() = 0;
|
||||
virtual void produceItem(const std::string& category, const std::string& item, const std::string& value) = 0;
|
||||
|
||||
protected:
|
||||
|
||||
enum State
|
||||
{
|
||||
eStateStart,
|
||||
eStateWhite,
|
||||
eStateComment,
|
||||
eStateQuestionMark,
|
||||
eStateDot,
|
||||
eStateQuotedString,
|
||||
eStateQuotedStringQuote,
|
||||
eStateUnquotedString,
|
||||
eStateTag,
|
||||
eStateTextField,
|
||||
eStateFloat = 100,
|
||||
eStateInt = 110,
|
||||
// eStateNumericSuffix = 200,
|
||||
eStateValue = 300
|
||||
};
|
||||
|
||||
std::istream& mData;
|
||||
|
||||
// Parser state
|
||||
bool mValidate;
|
||||
uint32_t mLineNr;
|
||||
bool mBol;
|
||||
int mState, mStart;
|
||||
CIFToken mLookahead;
|
||||
std::string mTokenValue;
|
||||
CIFValueType mTokenType;
|
||||
std::stack<int> mBuffer;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class Parser : public SacParser
|
||||
{
|
||||
public:
|
||||
Parser(std::istream& is, File& f, bool init = true);
|
||||
|
||||
virtual void produceDatablock(const std::string& name);
|
||||
virtual void produceCategory(const std::string& name);
|
||||
virtual void produceRow();
|
||||
virtual void produceItem(const std::string& category, const std::string& item, const std::string& value);
|
||||
|
||||
protected:
|
||||
File& mFile;
|
||||
Datablock* mDataBlock;
|
||||
Datablock::iterator mCat;
|
||||
Row mRow;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class DictParser : public Parser
|
||||
{
|
||||
public:
|
||||
|
||||
DictParser(Validator& validator, std::istream& is);
|
||||
~DictParser();
|
||||
|
||||
void loadDictionary();
|
||||
|
||||
private:
|
||||
|
||||
virtual void parseSaveFrame();
|
||||
|
||||
bool collectItemTypes();
|
||||
void linkItems();
|
||||
|
||||
Validator& mValidator;
|
||||
File mFile;
|
||||
struct DictParserDataImpl* mImpl;
|
||||
bool mCollectedItemTypes = false;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,239 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#ifndef STDOUT_FILENO
|
||||
#define STDOUT_FILENO 1
|
||||
#endif
|
||||
|
||||
#if _MSC_VER
|
||||
#include <io.h>
|
||||
#define isatty _isatty
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "cif++/Cif++Export.hpp"
|
||||
|
||||
#if _MSC_VER
|
||||
#pragma warning(disable : 4996) // unsafe function or variable (strcpy e.g.)
|
||||
#pragma warning(disable : 4068) // unknown pragma
|
||||
#pragma warning(disable : 4100) // unreferenced formal parameter
|
||||
#pragma warning(disable : 4101) // unreferenced local variable
|
||||
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING 1
|
||||
#endif
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// the git 'build' number
|
||||
std::string get_version_nr();
|
||||
// std::string get_version_date();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
// some basic utilities: Since we're using ASCII input only, we define for optimisation
|
||||
// our own case conversion routines.
|
||||
|
||||
bool iequals(const std::string &a, const std::string &b);
|
||||
int icompare(const std::string &a, const std::string &b);
|
||||
|
||||
bool iequals(const char *a, const char *b);
|
||||
int icompare(const char *a, const char *b);
|
||||
|
||||
void toLower(std::string &s);
|
||||
std::string toLowerCopy(const std::string &s);
|
||||
|
||||
// To make life easier, we also define iless and iset using iequals
|
||||
|
||||
struct iless
|
||||
{
|
||||
bool operator()(const std::string &a, const std::string &b) const
|
||||
{
|
||||
return icompare(a, b) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::set<std::string, iless> iset;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// This really makes a difference, having our own tolower routines
|
||||
|
||||
extern const uint8_t kCharToLowerMap[256];
|
||||
|
||||
inline char tolower(int ch)
|
||||
{
|
||||
return static_cast<char>(kCharToLowerMap[static_cast<uint8_t>(ch)]);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
std::tuple<std::string, std::string> splitTagName(const std::string &tag);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// generate a cif name, mainly used to generate asym_id's
|
||||
|
||||
std::string cifIdForNumber(int number);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// custom wordwrapping routine
|
||||
|
||||
std::vector<std::string> wordWrap(const std::string &text, size_t width);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Code helping with terminal i/o
|
||||
|
||||
uint32_t get_terminal_width();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Path of the current executable
|
||||
|
||||
std::string get_executable_path();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// some manipulators to write coloured text to terminals
|
||||
|
||||
enum StringColour
|
||||
{
|
||||
scBLACK = 0,
|
||||
scRED,
|
||||
scGREEN,
|
||||
scYELLOW,
|
||||
scBLUE,
|
||||
scMAGENTA,
|
||||
scCYAN,
|
||||
scWHITE,
|
||||
scNONE = 9
|
||||
};
|
||||
|
||||
template <typename String, typename CharT>
|
||||
struct ColouredString
|
||||
{
|
||||
static_assert(std::is_reference<String>::value or std::is_pointer<String>::value, "String type must be pointer or reference");
|
||||
|
||||
ColouredString(String s, StringColour fore, StringColour back, bool bold = true)
|
||||
: m_s(s)
|
||||
, m_fore(fore)
|
||||
, m_back(back)
|
||||
, m_bold(bold)
|
||||
{
|
||||
}
|
||||
|
||||
ColouredString &operator=(const ColouredString &) = delete;
|
||||
|
||||
String m_s;
|
||||
StringColour m_fore, m_back;
|
||||
bool m_bold;
|
||||
};
|
||||
|
||||
template <typename CharT, typename Traits>
|
||||
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const ColouredString<const CharT *, CharT> &s)
|
||||
{
|
||||
if (isatty(STDOUT_FILENO))
|
||||
{
|
||||
std::basic_ostringstream<CharT, Traits> ostr;
|
||||
ostr << "\033[" << (30 + s.m_fore) << ';' << (s.m_bold ? "1" : "22") << ';' << (40 + s.m_back) << 'm'
|
||||
<< s.m_s
|
||||
<< "\033[0m";
|
||||
|
||||
return os << ostr.str();
|
||||
}
|
||||
else
|
||||
return os << s.m_s;
|
||||
}
|
||||
|
||||
template <typename CharT, typename Traits, typename String>
|
||||
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const ColouredString<String, CharT> &s)
|
||||
{
|
||||
if (isatty(STDOUT_FILENO))
|
||||
{
|
||||
std::basic_ostringstream<CharT, Traits> ostr;
|
||||
ostr << "\033[" << (30 + s.m_fore) << ';' << (s.m_bold ? "1" : "22") << ';' << (40 + s.m_back) << 'm'
|
||||
<< s.m_s
|
||||
<< "\033[0m";
|
||||
|
||||
return os << ostr.str();
|
||||
}
|
||||
else
|
||||
return os << s.m_s;
|
||||
}
|
||||
|
||||
template <typename CharT>
|
||||
inline auto coloured(const CharT *s, StringColour fore = scWHITE, StringColour back = scRED, bool bold = true)
|
||||
{
|
||||
return ColouredString<const CharT *, CharT>(s, fore, back, bold);
|
||||
}
|
||||
|
||||
template <typename CharT, typename Traits, typename Alloc>
|
||||
inline auto coloured(const std::basic_string<CharT, Traits, Alloc> &s, StringColour fore = scWHITE, StringColour back = scRED, bool bold = true)
|
||||
{
|
||||
return ColouredString<const std::basic_string<CharT, Traits, Alloc>, CharT>(s, fore, back, bold);
|
||||
}
|
||||
|
||||
template <typename CharT, typename Traits, typename Alloc>
|
||||
inline auto coloured(std::basic_string<CharT, Traits, Alloc> &s, StringColour fore = scWHITE, StringColour back = scRED, bool bold = true)
|
||||
{
|
||||
return ColouredString<std::basic_string<CharT, Traits, Alloc>, CharT>(s, fore, back, bold);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// A progress bar
|
||||
|
||||
class Progress
|
||||
{
|
||||
public:
|
||||
Progress(int64_t inMax, const std::string &inAction);
|
||||
virtual ~Progress();
|
||||
|
||||
void consumed(int64_t inConsumed); // consumed is relative
|
||||
void progress(int64_t inProgress); // progress is absolute
|
||||
|
||||
void message(const std::string &inMessage);
|
||||
|
||||
private:
|
||||
Progress(const Progress &) = delete;
|
||||
Progress &operator=(const Progress &) = delete;
|
||||
|
||||
struct ProgressImpl *mImpl;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Resources
|
||||
|
||||
std::unique_ptr<std::istream> loadResource(std::filesystem::path name);
|
||||
void addFileResource(const std::string &name, std::filesystem::path dataFile);
|
||||
void addDataDirectory(std::filesystem::path dataDir);
|
||||
|
||||
} // namespace cif
|
||||
@@ -1,198 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/Cif++.hpp"
|
||||
|
||||
// duh.. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86164
|
||||
// #include <regex>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include <set>
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
struct ValidateCategory;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class ValidationError : public std::exception
|
||||
{
|
||||
public:
|
||||
ValidationError(const std::string& msg);
|
||||
ValidationError(const std::string& cat, const std::string& item,
|
||||
const std::string& msg);
|
||||
const char* what() const noexcept { return mMsg.c_str(); }
|
||||
std::string mMsg;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
enum class DDL_PrimitiveType
|
||||
{
|
||||
Char, UChar, Numb
|
||||
};
|
||||
|
||||
DDL_PrimitiveType mapToPrimitiveType(const std::string& s);
|
||||
|
||||
struct ValidateType
|
||||
{
|
||||
std::string mName;
|
||||
DDL_PrimitiveType mPrimitiveType;
|
||||
// std::regex mRx;
|
||||
boost::regex mRx;
|
||||
|
||||
bool operator<(const ValidateType& rhs) const
|
||||
{
|
||||
return icompare(mName, rhs.mName) < 0;
|
||||
}
|
||||
|
||||
// compare values based on type
|
||||
// int compare(const std::string& a, const std::string& b) const
|
||||
// {
|
||||
// return compare(a.c_str(), b.c_str());
|
||||
// }
|
||||
|
||||
int compare(const char* a, const char* b) const;
|
||||
};
|
||||
|
||||
struct ValidateItem
|
||||
{
|
||||
std::string mTag;
|
||||
bool mMandatory;
|
||||
const ValidateType* mType;
|
||||
cif::iset mEnums;
|
||||
std::string mDefault;
|
||||
bool mDefaultIsNull;
|
||||
ValidateCategory* mCategory = nullptr;
|
||||
|
||||
// ItemLinked is used for non-key links
|
||||
struct ItemLinked
|
||||
{
|
||||
ValidateItem* mParent;
|
||||
std::string mParentItem;
|
||||
std::string mChildItem;
|
||||
};
|
||||
|
||||
std::vector<ItemLinked> mLinked;
|
||||
|
||||
bool operator<(const ValidateItem& rhs) const
|
||||
{
|
||||
return icompare(mTag, rhs.mTag) < 0;
|
||||
}
|
||||
|
||||
bool operator==(const ValidateItem& rhs) const
|
||||
{
|
||||
return iequals(mTag, rhs.mTag);
|
||||
}
|
||||
|
||||
void operator()(std::string value) const;
|
||||
};
|
||||
|
||||
struct ValidateCategory
|
||||
{
|
||||
std::string mName;
|
||||
std::vector<std::string> mKeys;
|
||||
cif::iset mGroups;
|
||||
cif::iset mMandatoryFields;
|
||||
std::set<ValidateItem> mItemValidators;
|
||||
|
||||
bool operator<(const ValidateCategory& rhs) const
|
||||
{
|
||||
return icompare(mName, rhs.mName) < 0;
|
||||
}
|
||||
|
||||
void addItemValidator(ValidateItem&& v);
|
||||
|
||||
const ValidateItem* getValidatorForItem(std::string tag) const;
|
||||
|
||||
const std::set<ValidateItem>& itemValidators() const
|
||||
{
|
||||
return mItemValidators;
|
||||
}
|
||||
};
|
||||
|
||||
struct ValidateLink
|
||||
{
|
||||
int mLinkGroupID;
|
||||
std::string mParentCategory;
|
||||
std::vector<std::string> mParentKeys;
|
||||
std::string mChildCategory;
|
||||
std::vector<std::string> mChildKeys;
|
||||
std::string mLinkGroupLabel;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class Validator
|
||||
{
|
||||
public:
|
||||
friend class DictParser;
|
||||
|
||||
Validator();
|
||||
~Validator();
|
||||
|
||||
Validator(const Validator& rhs) = delete;
|
||||
Validator& operator=(const Validator& rhs) = delete;
|
||||
|
||||
Validator(Validator&& rhs);
|
||||
Validator& operator=(Validator&& rhs);
|
||||
|
||||
void addTypeValidator(ValidateType&& v);
|
||||
const ValidateType* getValidatorForType(std::string typeCode) const;
|
||||
|
||||
void addCategoryValidator(ValidateCategory&& v);
|
||||
const ValidateCategory* getValidatorForCategory(std::string category) const;
|
||||
|
||||
void addLinkValidator(ValidateLink&& v);
|
||||
std::vector<const ValidateLink*> getLinksForParent(const std::string& category) const;
|
||||
std::vector<const ValidateLink*> getLinksForChild(const std::string& category) const;
|
||||
|
||||
void reportError(const std::string& msg, bool fatal);
|
||||
|
||||
std::string dictName() const { return mName; }
|
||||
void dictName(const std::string& name) { mName = name; }
|
||||
|
||||
std::string dictVersion() const { return mVersion; }
|
||||
void dictVersion(const std::string& version) { mVersion = version; }
|
||||
|
||||
private:
|
||||
|
||||
// name is fully qualified here:
|
||||
ValidateItem* getValidatorForItem(std::string name) const;
|
||||
|
||||
std::string mName;
|
||||
std::string mVersion;
|
||||
bool mStrict = false;
|
||||
// std::set<uint32_t> mSubCategories;
|
||||
std::set<ValidateType> mTypeValidators;
|
||||
std::set<ValidateCategory> mCategoryValidators;
|
||||
std::vector<ValidateLink> mLinkValidators;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,195 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/// \file This file contains the definition for the class Compound, encapsulating
|
||||
/// the information found for compounds in the CCD.
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include "cif++/AtomType.hpp"
|
||||
#include "cif++/Cif++.hpp"
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class Compound;
|
||||
struct CompoundAtom;
|
||||
class CompoundFactoryImpl;
|
||||
|
||||
/// \brief The bond type as defined in the CCD, possible values taken from the mmcif_pdbx_v50 file
|
||||
enum class BondType
|
||||
{
|
||||
sing, // 'single bond'
|
||||
doub, // 'double bond'
|
||||
trip, // 'triple bond'
|
||||
quad, // 'quadruple bond'
|
||||
arom, // 'aromatic bond'
|
||||
poly, // 'polymeric bond'
|
||||
delo, // 'delocalized double bond'
|
||||
pi, // 'pi bond'
|
||||
};
|
||||
|
||||
std::string to_string(BondType bondType);
|
||||
BondType from_string(const std::string& bondType);
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
/// \brief struct containing information about an atom in a chemical compound.
|
||||
/// This is a subset of the available information. Contact the author if you need more fields.
|
||||
|
||||
struct CompoundAtom
|
||||
{
|
||||
std::string id;
|
||||
AtomType typeSymbol;
|
||||
int charge = 0;
|
||||
bool aromatic = false;
|
||||
bool leavingAtom = false;
|
||||
bool stereoConfig = false;
|
||||
float x, y, z;
|
||||
};
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
/// \brief struct containing information about the bonds
|
||||
|
||||
struct CompoundBond
|
||||
{
|
||||
std::string atomID[2];
|
||||
BondType type;
|
||||
bool aromatic = false, stereoConfig = false;
|
||||
};
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
/// \brief a class that contains information about a chemical compound.
|
||||
/// This information is derived from the CDD by default.
|
||||
///
|
||||
/// To create compounds, you use the factory method. You can add your own
|
||||
/// compound definitions by calling the addExtraComponents function and
|
||||
/// pass it a valid CCD formatted file.
|
||||
|
||||
class Compound
|
||||
{
|
||||
public:
|
||||
|
||||
// accessors
|
||||
|
||||
std::string id() const { return mID; }
|
||||
std::string name() const { return mName; }
|
||||
std::string type() const { return mType; }
|
||||
std::string formula() const { return mFormula; }
|
||||
float formulaWeight() const { return mFormulaWeight; }
|
||||
int formalCharge() const { return mFormalCharge; }
|
||||
|
||||
const std::vector<CompoundAtom> &atoms() const { return mAtoms; }
|
||||
const std::vector<CompoundBond> &bonds() const { return mBonds; }
|
||||
|
||||
CompoundAtom getAtomByID(const std::string &atomID) const;
|
||||
|
||||
bool atomsBonded(const std::string &atomId_1, const std::string &atomId_2) const;
|
||||
// float atomBondValue(const std::string &atomId_1, const std::string &atomId_2) const;
|
||||
// float bondAngle(const std::string &atomId_1, const std::string &atomId_2, const std::string &atomId_3) const;
|
||||
// float chiralVolume(const std::string ¢reID) const;
|
||||
|
||||
bool isWater() const
|
||||
{
|
||||
return mID == "HOH" or mID == "H2O" or mID == "WAT";
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
friend class CompoundFactoryImpl;
|
||||
friend class CCDCompoundFactoryImpl;
|
||||
friend class CCP4CompoundFactoryImpl;
|
||||
|
||||
Compound(cif::Datablock &db);
|
||||
Compound(cif::Datablock &db, const std::string &id, const std::string &name, const std::string &type);
|
||||
|
||||
std::string mID;
|
||||
std::string mName;
|
||||
std::string mType;
|
||||
std::string mFormula;
|
||||
float mFormulaWeight = 0;
|
||||
int mFormalCharge = 0;
|
||||
std::vector<CompoundAtom> mAtoms;
|
||||
std::vector<CompoundBond> mBonds;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Factory class for Compound and Link objects
|
||||
|
||||
CIFPP_EXPORT extern const std::map<std::string, char> kAAMap, kBaseMap;
|
||||
|
||||
class CompoundFactory
|
||||
{
|
||||
public:
|
||||
|
||||
/// \brief Initialise a singleton instance.
|
||||
///
|
||||
/// If you have a multithreaded application and want to have different
|
||||
/// compounds in each thread (e.g. a web service processing user requests
|
||||
/// with different sets of compounds) you can set the \a useThreadLocalInstanceOnly
|
||||
/// flag to true.
|
||||
|
||||
static void init(bool useThreadLocalInstanceOnly);
|
||||
static CompoundFactory &instance();
|
||||
static void clear();
|
||||
|
||||
void setDefaultDictionary(const std::filesystem::path &inDictFile);
|
||||
void pushDictionary(const std::filesystem::path &inDictFile);
|
||||
void popDictionary();
|
||||
|
||||
bool isKnownPeptide(const std::string &res_name) const;
|
||||
bool isKnownBase(const std::string &res_name) const;
|
||||
|
||||
/// \brief Create the Compound object for \a id
|
||||
///
|
||||
/// This will create the Compound instance for \a id if it doesn't exist already.
|
||||
/// The result is owned by this factory and should not be deleted by the user.
|
||||
/// \param id The Compound ID, a three letter code usually
|
||||
/// \result The compound, or nullptr if it could not be created (missing info)
|
||||
const Compound *create(std::string id);
|
||||
|
||||
~CompoundFactory();
|
||||
|
||||
private:
|
||||
CompoundFactory();
|
||||
|
||||
CompoundFactory(const CompoundFactory &) = delete;
|
||||
CompoundFactory &operator=(const CompoundFactory &) = delete;
|
||||
|
||||
static std::unique_ptr<CompoundFactory> sInstance;
|
||||
static thread_local std::unique_ptr<CompoundFactory> tlInstance;
|
||||
static bool sUseThreadLocalInstance;
|
||||
|
||||
std::shared_ptr<CompoundFactoryImpl> mImpl;
|
||||
};
|
||||
|
||||
} // namespace mmcif
|
||||
@@ -1,391 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright Maarten L. Hekkelman, Radboud University 2008-2011.
|
||||
* Copyright (c) 2021 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// uBlas compatible matrix types
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
// matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
// element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
|
||||
template <typename T>
|
||||
class MatrixBase
|
||||
{
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
virtual ~MatrixBase() {}
|
||||
|
||||
virtual uint32_t dim_m() const = 0;
|
||||
virtual uint32_t dim_n() const = 0;
|
||||
|
||||
virtual value_type &operator()(uint32_t i, uint32_t j) { throw std::runtime_error("unimplemented method"); }
|
||||
virtual value_type operator()(uint32_t i, uint32_t j) const = 0;
|
||||
|
||||
MatrixBase &operator*=(const value_type &rhs);
|
||||
|
||||
MatrixBase &operator-=(const value_type &rhs);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
MatrixBase<T> &MatrixBase<T>::operator*=(const T &rhs)
|
||||
{
|
||||
for (uint32_t i = 0; i < dim_m(); ++i)
|
||||
{
|
||||
for (uint32_t j = 0; j < dim_n(); ++j)
|
||||
{
|
||||
operator()(i, j) *= rhs;
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
MatrixBase<T> &MatrixBase<T>::operator-=(const T &rhs)
|
||||
{
|
||||
for (uint32_t i = 0; i < dim_m(); ++i)
|
||||
{
|
||||
for (uint32_t j = 0; j < dim_n(); ++j)
|
||||
{
|
||||
operator()(i, j) -= rhs;
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::ostream &operator<<(std::ostream &lhs, const MatrixBase<T> &rhs)
|
||||
{
|
||||
lhs << '[' << rhs.dim_m() << ',' << rhs.dim_n() << ']' << '(';
|
||||
for (uint32_t i = 0; i < rhs.dim_m(); ++i)
|
||||
{
|
||||
lhs << '(';
|
||||
for (uint32_t j = 0; j < rhs.dim_n(); ++j)
|
||||
{
|
||||
if (j > 0)
|
||||
lhs << ',';
|
||||
lhs << rhs(i, j);
|
||||
}
|
||||
lhs << ')';
|
||||
}
|
||||
lhs << ')';
|
||||
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class Matrix : public MatrixBase<T>
|
||||
{
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
template <typename T2>
|
||||
Matrix(const MatrixBase<T2> &m)
|
||||
: m_m(m.dim_m())
|
||||
, m_n(m.dim_n())
|
||||
{
|
||||
m_data = new value_type[m_m * m_n];
|
||||
for (uint32_t i = 0; i < m_m; ++i)
|
||||
{
|
||||
for (uint32_t j = 0; j < m_n; ++j)
|
||||
operator()(i, j) = m(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
Matrix()
|
||||
: m_data(nullptr)
|
||||
, m_m(0)
|
||||
, m_n(0)
|
||||
{
|
||||
}
|
||||
|
||||
Matrix(const Matrix &m)
|
||||
: m_m(m.m_m)
|
||||
, m_n(m.m_n)
|
||||
{
|
||||
m_data = new value_type[m_m * m_n];
|
||||
std::copy(m.m_data, m.m_data + (m_m * m_n), m_data);
|
||||
}
|
||||
|
||||
Matrix &operator=(const Matrix &m)
|
||||
{
|
||||
value_type *t = new value_type[m.m_m * m.m_n];
|
||||
std::copy(m.m_data, m.m_data + (m.m_m * m.m_n), t);
|
||||
|
||||
delete[] m_data;
|
||||
m_data = t;
|
||||
m_m = m.m_m;
|
||||
m_n = m.m_n;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Matrix(uint32_t m, uint32_t n, T v = T())
|
||||
: m_m(m)
|
||||
, m_n(n)
|
||||
{
|
||||
m_data = new value_type[m_m * m_n];
|
||||
std::fill(m_data, m_data + (m_m * m_n), v);
|
||||
}
|
||||
|
||||
virtual ~Matrix()
|
||||
{
|
||||
delete[] m_data;
|
||||
}
|
||||
|
||||
virtual uint32_t dim_m() const { return m_m; }
|
||||
virtual uint32_t dim_n() const { return m_n; }
|
||||
|
||||
virtual value_type operator()(uint32_t i, uint32_t j) const
|
||||
{
|
||||
assert(i < m_m);
|
||||
assert(j < m_n);
|
||||
return m_data[i * m_n + j];
|
||||
}
|
||||
|
||||
virtual value_type &operator()(uint32_t i, uint32_t j)
|
||||
{
|
||||
assert(i < m_m);
|
||||
assert(j < m_n);
|
||||
return m_data[i * m_n + j];
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
void each(Func f)
|
||||
{
|
||||
for (uint32_t i = 0; i < m_m * m_n; ++i)
|
||||
f(m_data[i]);
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
Matrix &operator/=(U v)
|
||||
{
|
||||
for (uint32_t i = 0; i < m_m * m_n; ++i)
|
||||
m_data[i] /= v;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
value_type *m_data;
|
||||
uint32_t m_m, m_n;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
template <typename T>
|
||||
class SymmetricMatrix : public MatrixBase<T>
|
||||
{
|
||||
public:
|
||||
typedef typename MatrixBase<T>::value_type value_type;
|
||||
|
||||
SymmetricMatrix(uint32_t n, T v = T())
|
||||
: m_owner(true)
|
||||
, m_n(n)
|
||||
{
|
||||
uint32_t N = (m_n * (m_n + 1)) / 2;
|
||||
m_data = new value_type[N];
|
||||
std::fill(m_data, m_data + N, v);
|
||||
}
|
||||
|
||||
SymmetricMatrix(const T *data, uint32_t n)
|
||||
: m_owner(false)
|
||||
, m_data(const_cast<T *>(data))
|
||||
, m_n(n)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~SymmetricMatrix()
|
||||
{
|
||||
if (m_owner)
|
||||
delete[] m_data;
|
||||
}
|
||||
|
||||
virtual uint32_t dim_m() const { return m_n; }
|
||||
virtual uint32_t dim_n() const { return m_n; }
|
||||
|
||||
T operator()(uint32_t i, uint32_t j) const;
|
||||
virtual T &operator()(uint32_t i, uint32_t j);
|
||||
|
||||
// erase two rows, add one at the end (for neighbour joining)
|
||||
void erase_2(uint32_t i, uint32_t j);
|
||||
|
||||
template <typename Func>
|
||||
void each(Func f)
|
||||
{
|
||||
uint32_t N = (m_n * (m_n + 1)) / 2;
|
||||
|
||||
for (uint32_t i = 0; i < N; ++i)
|
||||
f(m_data[i]);
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
SymmetricMatrix &operator/=(U v)
|
||||
{
|
||||
uint32_t N = (m_n * (m_n + 1)) / 2;
|
||||
|
||||
for (uint32_t i = 0; i < N; ++i)
|
||||
m_data[i] /= v;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
bool m_owner;
|
||||
value_type *m_data;
|
||||
uint32_t m_n;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline T SymmetricMatrix<T>::operator()(uint32_t i, uint32_t j) const
|
||||
{
|
||||
return i < j
|
||||
? m_data[(j * (j + 1)) / 2 + i]
|
||||
: m_data[(i * (i + 1)) / 2 + j];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T &SymmetricMatrix<T>::operator()(uint32_t i, uint32_t j)
|
||||
{
|
||||
if (i > j)
|
||||
std::swap(i, j);
|
||||
assert(j < m_n);
|
||||
return m_data[(j * (j + 1)) / 2 + i];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void SymmetricMatrix<T>::erase_2(uint32_t di, uint32_t dj)
|
||||
{
|
||||
uint32_t s = 0, d = 0;
|
||||
for (uint32_t i = 0; i < m_n; ++i)
|
||||
{
|
||||
for (uint32_t j = 0; j < i; ++j)
|
||||
{
|
||||
if (i != di and j != dj and i != dj and j != di)
|
||||
{
|
||||
if (s != d)
|
||||
m_data[d] = m_data[s];
|
||||
++d;
|
||||
}
|
||||
|
||||
++s;
|
||||
}
|
||||
}
|
||||
|
||||
--m_n;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class IdentityMatrix : public MatrixBase<T>
|
||||
{
|
||||
public:
|
||||
typedef typename MatrixBase<T>::value_type value_type;
|
||||
|
||||
IdentityMatrix(uint32_t n)
|
||||
: m_n(n)
|
||||
{
|
||||
}
|
||||
|
||||
virtual uint32_t dim_m() const { return m_n; }
|
||||
virtual uint32_t dim_n() const { return m_n; }
|
||||
|
||||
virtual value_type operator()(uint32_t i, uint32_t j) const
|
||||
{
|
||||
value_type result = 0;
|
||||
if (i == j)
|
||||
result = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t m_n;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// matrix functions
|
||||
|
||||
template <typename T>
|
||||
Matrix<T> operator*(const MatrixBase<T> &lhs, const MatrixBase<T> &rhs)
|
||||
{
|
||||
Matrix<T> result(std::min(lhs.dim_m(), rhs.dim_m()), std::min(lhs.dim_n(), rhs.dim_n()));
|
||||
|
||||
for (uint32_t i = 0; i < result.dim_m(); ++i)
|
||||
{
|
||||
for (uint32_t j = 0; j < result.dim_n(); ++j)
|
||||
{
|
||||
for (uint32_t li = 0, rj = 0; li < lhs.dim_m() and rj < rhs.dim_n(); ++li, ++rj)
|
||||
result(i, j) += lhs(li, j) * rhs(i, rj);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Matrix<T> operator*(const MatrixBase<T> &lhs, T rhs)
|
||||
{
|
||||
Matrix<T> result(lhs);
|
||||
result *= rhs;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Matrix<T> operator-(const MatrixBase<T> &lhs, const MatrixBase<T> &rhs)
|
||||
{
|
||||
Matrix<T> result(std::min(lhs.dim_m(), rhs.dim_m()), std::min(lhs.dim_n(), rhs.dim_n()));
|
||||
|
||||
for (uint32_t i = 0; i < result.dim_m(); ++i)
|
||||
{
|
||||
for (uint32_t j = 0; j < result.dim_n(); ++j)
|
||||
{
|
||||
result(i, j) = lhs(i, j) - rhs(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Matrix<T> operator-(const MatrixBase<T> &lhs, T rhs)
|
||||
{
|
||||
Matrix<T> result(lhs.dim_m(), lhs.dim_n());
|
||||
result -= rhs;
|
||||
return result;
|
||||
}
|
||||
|
||||
// template <typename T>
|
||||
// symmetric_matrix<T> hammingDistance(const MatrixBase<T> &lhs, T rhs);
|
||||
|
||||
// template <typename T>
|
||||
// std::vector<T> sum(const MatrixBase<T> &m);
|
||||
@@ -1,428 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#if HAVE_LIBCLIPPER
|
||||
#include <clipper/core/coords.h>
|
||||
#endif
|
||||
|
||||
#include <boost/math/quaternion.hpp>
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
typedef boost::math::quaternion<float> Quaternion;
|
||||
|
||||
const double
|
||||
kPI = 3.141592653589793238462643383279502884;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
// Point, a location with x, y and z coordinates as floating point.
|
||||
// This one is derived from a tuple<float,float,float> so
|
||||
// you can do things like:
|
||||
//
|
||||
// float x, y, z;
|
||||
// tie(x, y, z) = atom.loc();
|
||||
|
||||
template<typename F>
|
||||
struct PointF
|
||||
{
|
||||
typedef F FType;
|
||||
|
||||
FType mX, mY, mZ;
|
||||
|
||||
PointF() : mX(0), mY(0), mZ(0) {}
|
||||
PointF(FType x, FType y, FType z) : mX(x), mY(y), mZ(z) {}
|
||||
|
||||
template<typename PF>
|
||||
PointF(const PointF<PF>& pt)
|
||||
: mX(static_cast<F>(pt.mX))
|
||||
, mY(static_cast<F>(pt.mY))
|
||||
, mZ(static_cast<F>(pt.mZ)) {}
|
||||
|
||||
#if HAVE_LIBCLIPPER
|
||||
PointF(const clipper::Coord_orth& pt): mX(pt[0]), mY(pt[1]), mZ(pt[2]) {}
|
||||
|
||||
PointF& operator=(const clipper::Coord_orth& rhs)
|
||||
{
|
||||
mX = rhs[0];
|
||||
mY = rhs[1];
|
||||
mZ = rhs[2];
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename PF>
|
||||
PointF& operator=(const PointF<PF>& rhs)
|
||||
{
|
||||
mX = static_cast<F>(rhs.mX);
|
||||
mY = static_cast<F>(rhs.mY);
|
||||
mZ = static_cast<F>(rhs.mZ);
|
||||
return *this;
|
||||
}
|
||||
|
||||
FType& getX() { return mX; }
|
||||
FType getX() const { return mX; }
|
||||
void setX(FType x) { mX = x; }
|
||||
|
||||
FType& getY() { return mY; }
|
||||
FType getY() const { return mY; }
|
||||
void setY(FType y) { mY = y; }
|
||||
|
||||
FType& getZ() { return mZ; }
|
||||
FType getZ() const { return mZ; }
|
||||
void setZ(FType z) { mZ = z; }
|
||||
|
||||
PointF& operator+=(const PointF& rhs)
|
||||
{
|
||||
mX += rhs.mX;
|
||||
mY += rhs.mY;
|
||||
mZ += rhs.mZ;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
PointF& operator+=(FType d)
|
||||
{
|
||||
mX += d;
|
||||
mY += d;
|
||||
mZ += d;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
PointF& operator-=(const PointF& rhs)
|
||||
{
|
||||
mX -= rhs.mX;
|
||||
mY -= rhs.mY;
|
||||
mZ -= rhs.mZ;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
PointF& operator-=(FType d)
|
||||
{
|
||||
mX -= d;
|
||||
mY -= d;
|
||||
mZ -= d;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
PointF& operator*=(FType rhs)
|
||||
{
|
||||
mX *= rhs;
|
||||
mY *= rhs;
|
||||
mZ *= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PointF& operator/=(FType rhs)
|
||||
{
|
||||
mX /= rhs;
|
||||
mY /= rhs;
|
||||
mZ /= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
FType normalize()
|
||||
{
|
||||
auto length = mX * mX + mY * mY + mZ * mZ;
|
||||
if (length > 0)
|
||||
{
|
||||
length = std::sqrt(length);
|
||||
operator/=(length);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
void rotate(const boost::math::quaternion<FType>& q)
|
||||
{
|
||||
boost::math::quaternion<FType> p(0, mX, mY, mZ);
|
||||
|
||||
p = q * p * boost::math::conj(q);
|
||||
|
||||
mX = p.R_component_2();
|
||||
mY = p.R_component_3();
|
||||
mZ = p.R_component_4();
|
||||
}
|
||||
|
||||
#if HAVE_LIBCLIPPER
|
||||
operator clipper::Coord_orth() const
|
||||
{
|
||||
return clipper::Coord_orth(mX, mY, mZ);
|
||||
}
|
||||
#endif
|
||||
|
||||
operator std::tuple<const FType&, const FType&, const FType&>() const
|
||||
{
|
||||
return std::make_tuple(std::ref(mX), std::ref(mY), std::ref(mZ));
|
||||
}
|
||||
|
||||
operator std::tuple<FType&,FType&,FType&>()
|
||||
{
|
||||
return std::make_tuple(std::ref(mX), std::ref(mY), std::ref(mZ));
|
||||
}
|
||||
|
||||
bool operator==(const PointF& rhs) const
|
||||
{
|
||||
return mX == rhs.mX and mY == rhs.mY and mZ == rhs.mZ;
|
||||
}
|
||||
|
||||
// consider point as a vector... perhaps I should rename Point?
|
||||
FType lengthsq() const
|
||||
{
|
||||
return mX * mX + mY * mY + mZ * mZ;
|
||||
}
|
||||
|
||||
FType length() const
|
||||
{
|
||||
return sqrt(mX * mX + mY * mY + mZ * mZ);
|
||||
}
|
||||
};
|
||||
|
||||
typedef PointF<float> Point;
|
||||
typedef PointF<double> DPoint;
|
||||
|
||||
template<typename F>
|
||||
inline std::ostream& operator<<(std::ostream& os, const PointF<F>& pt)
|
||||
{
|
||||
os << '(' << pt.mX << ',' << pt.mY << ',' << pt.mZ << ')';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline PointF<F> operator+(const PointF<F>& lhs, const PointF<F>& rhs)
|
||||
{
|
||||
return PointF<F>(lhs.mX + rhs.mX, lhs.mY + rhs.mY, lhs.mZ + rhs.mZ);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline PointF<F> operator-(const PointF<F>& lhs, const PointF<F>& rhs)
|
||||
{
|
||||
return PointF<F>(lhs.mX - rhs.mX, lhs.mY - rhs.mY, lhs.mZ - rhs.mZ);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline PointF<F> operator-(const PointF<F>& pt)
|
||||
{
|
||||
return PointF<F>(-pt.mX, -pt.mY, -pt.mZ);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline PointF<F> operator*(const PointF<F>& pt, F f)
|
||||
{
|
||||
return PointF<F>(pt.mX * f, pt.mY * f, pt.mZ * f);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline PointF<F> operator*(F f, const PointF<F>& pt)
|
||||
{
|
||||
return PointF<F>(pt.mX * f, pt.mY * f, pt.mZ * f);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline PointF<F> operator/(const PointF<F>& pt, F f)
|
||||
{
|
||||
return PointF<F>(pt.mX / f, pt.mY / f, pt.mZ / f);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// several standard 3d operations
|
||||
|
||||
template<typename F>
|
||||
inline double DistanceSquared(const PointF<F>& a, const PointF<F>& b)
|
||||
{
|
||||
return
|
||||
(a.mX - b.mX) * (a.mX - b.mX) +
|
||||
(a.mY - b.mY) * (a.mY - b.mY) +
|
||||
(a.mZ - b.mZ) * (a.mZ - b.mZ);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline double Distance(const PointF<F>& a, const PointF<F>& b)
|
||||
{
|
||||
return sqrt(
|
||||
(a.mX - b.mX) * (a.mX - b.mX) +
|
||||
(a.mY - b.mY) * (a.mY - b.mY) +
|
||||
(a.mZ - b.mZ) * (a.mZ - b.mZ));
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline F DotProduct(const PointF<F>& a, const PointF<F>& b)
|
||||
{
|
||||
return a.mX * b.mX + a.mY * b.mY + a.mZ * b.mZ;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline PointF<F> CrossProduct(const PointF<F>& a, const PointF<F>& b)
|
||||
{
|
||||
return PointF<F>(a.mY * b.mZ - b.mY * a.mZ,
|
||||
a.mZ * b.mX - b.mZ * a.mX,
|
||||
a.mX * b.mY - b.mX * a.mY);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
double Angle(const PointF<F>& p1, const PointF<F>& p2, const PointF<F>& p3)
|
||||
{
|
||||
PointF<F> v1 = p1 - p2;
|
||||
PointF<F> v2 = p3 - p2;
|
||||
|
||||
return std::acos(DotProduct(v1, v2) / (v1.length() * v2.length())) * 180 / kPI;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
double DihedralAngle(const PointF<F>& p1, const PointF<F>& p2, const PointF<F>& p3, const PointF<F>& p4)
|
||||
{
|
||||
PointF<F> v12 = p1 - p2; // vector from p2 to p1
|
||||
PointF<F> v43 = p4 - p3; // vector from p3 to p4
|
||||
|
||||
PointF<F> z = p2 - p3; // vector from p3 to p2
|
||||
|
||||
PointF<F> p = CrossProduct(z, v12);
|
||||
PointF<F> x = CrossProduct(z, v43);
|
||||
PointF<F> y = CrossProduct(z, x);
|
||||
|
||||
double u = DotProduct(x, x);
|
||||
double v = DotProduct(y, y);
|
||||
|
||||
double result = 360;
|
||||
if (u > 0 and v > 0)
|
||||
{
|
||||
u = DotProduct(p, x) / sqrt(u);
|
||||
v = DotProduct(p, y) / sqrt(v);
|
||||
if (u != 0 or v != 0)
|
||||
result = atan2(v, u) * 180 / kPI;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
double CosinusAngle(const PointF<F>& p1, const PointF<F>& p2, const PointF<F>& p3, const PointF<F>& p4)
|
||||
{
|
||||
PointF<F> v12 = p1 - p2;
|
||||
PointF<F> v34 = p3 - p4;
|
||||
|
||||
double result = 0;
|
||||
|
||||
double x = DotProduct(v12, v12) * DotProduct(v34, v34);
|
||||
if (x > 0)
|
||||
result = DotProduct(v12, v34) / sqrt(x);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
auto DistancePointToLine(const PointF<F> &l1, const PointF<F> &l2, const PointF<F> &p)
|
||||
{
|
||||
auto line = l2 - l1;
|
||||
auto p_to_l1 = p - l1;
|
||||
auto p_to_l2 = p - l2;
|
||||
auto cross = CrossProduct(p_to_l1, p_to_l2);
|
||||
return cross.length() / line.length();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// For e.g. simulated annealing, returns a new point that is moved in
|
||||
// a random direction with a distance randomly chosen from a normal
|
||||
// distribution with a stddev of offset.
|
||||
|
||||
template<typename F>
|
||||
PointF<F> Nudge(PointF<F> p, F offset);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// We use quaternions to do rotations in 3d space
|
||||
|
||||
Quaternion Normalize(Quaternion q);
|
||||
|
||||
std::tuple<double,Point> QuaternionToAngleAxis(Quaternion q);
|
||||
Point Centroid(std::vector<Point>& Points);
|
||||
Point CenterPoints(std::vector<Point>& Points);
|
||||
Quaternion AlignPoints(const std::vector<Point>& a, const std::vector<Point>& b);
|
||||
double RMSd(const std::vector<Point>& a, const std::vector<Point>& b);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Helper class to generate evenly divided Points on a sphere
|
||||
// we use a fibonacci sphere to calculate even distribution of the dots
|
||||
|
||||
template<int N>
|
||||
class SphericalDots
|
||||
{
|
||||
public:
|
||||
enum { P = 2 * N + 1 };
|
||||
typedef typename std::array<Point,P> array_type;
|
||||
typedef typename array_type::const_iterator iterator;
|
||||
|
||||
static SphericalDots& instance()
|
||||
{
|
||||
static SphericalDots sInstance;
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
size_t size() const { return mPoints.size(); }
|
||||
const Point operator[](uint32_t inIx) const { return mPoints[inIx]; }
|
||||
iterator begin() const { return mPoints.begin(); }
|
||||
iterator end() const { return mPoints.end(); }
|
||||
|
||||
double weight() const { return mWeight; }
|
||||
|
||||
SphericalDots()
|
||||
{
|
||||
|
||||
const double
|
||||
kGoldenRatio = (1 + std::sqrt(5.0)) / 2;
|
||||
|
||||
mWeight = (4 * kPI) / P;
|
||||
|
||||
auto p = mPoints.begin();
|
||||
|
||||
for (int32_t i = -N; i <= N; ++i)
|
||||
{
|
||||
double lat = std::asin((2.0 * i) / P);
|
||||
double lon = std::fmod(i, kGoldenRatio) * 2 * kPI / kGoldenRatio;
|
||||
|
||||
p->mX = sin(lon) * cos(lat);
|
||||
p->mY = cos(lon) * cos(lat);
|
||||
p->mZ = sin(lat);
|
||||
|
||||
++p;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
array_type mPoints;
|
||||
double mWeight;
|
||||
};
|
||||
|
||||
typedef SphericalDots<50> SphericalDots_50;
|
||||
|
||||
}
|
||||
@@ -1,218 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
// Calculate DSSP-like secondary structure information
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
class Structure;
|
||||
class Monomer;
|
||||
|
||||
struct Res;
|
||||
|
||||
extern const float
|
||||
kCouplingConstant, kMinHBondEnergy, kMaxHBondEnergy;
|
||||
|
||||
enum SecondaryStructureType : char
|
||||
{
|
||||
ssLoop = ' ',
|
||||
ssAlphahelix = 'H',
|
||||
ssBetabridge = 'B',
|
||||
ssStrand = 'E',
|
||||
ssHelix_3 = 'G',
|
||||
ssHelix_5 = 'I',
|
||||
ssHelix_PPII = 'P',
|
||||
ssTurn = 'T',
|
||||
ssBend = 'S'
|
||||
};
|
||||
|
||||
enum class HelixType
|
||||
{
|
||||
rh_3_10, rh_alpha, rh_pi, rh_pp
|
||||
};
|
||||
|
||||
enum class Helix
|
||||
{
|
||||
None, Start, End, StartAndEnd, Middle
|
||||
};
|
||||
|
||||
//struct HBond
|
||||
//{
|
||||
// std::string labelAsymID;
|
||||
// int labelSeqID;
|
||||
// double energy;
|
||||
//};
|
||||
//
|
||||
//struct BridgePartner
|
||||
//{
|
||||
// std::string labelAsymID;
|
||||
// int labelSeqID;
|
||||
// int ladder;
|
||||
// bool parallel;
|
||||
//};
|
||||
|
||||
struct SecondaryStructure
|
||||
{
|
||||
SecondaryStructureType type;
|
||||
// HBond donor[2], acceptor[2];
|
||||
// BridgePartner beta[2];
|
||||
// int sheet;
|
||||
// bool bend;
|
||||
};
|
||||
|
||||
//void CalculateSecondaryStructure(Structure& s);
|
||||
|
||||
const size_t
|
||||
kHistogramSize = 30;
|
||||
|
||||
struct DSSP_Statistics
|
||||
{
|
||||
uint32_t nrOfResidues, nrOfChains, nrOfSSBridges, nrOfIntraChainSSBridges, nrOfHBonds;
|
||||
uint32_t nrOfHBondsInAntiparallelBridges, nrOfHBondsInParallelBridges;
|
||||
uint32_t nrOfHBondsPerDistance[11] = {};
|
||||
double accessibleSurface = 0;
|
||||
|
||||
uint32_t residuesPerAlphaHelixHistogram[kHistogramSize] = {};
|
||||
uint32_t parallelBridgesPerLadderHistogram[kHistogramSize] = {};
|
||||
uint32_t antiparallelBridgesPerLadderHistogram[kHistogramSize] = {};
|
||||
uint32_t laddersPerSheetHistogram[kHistogramSize] = {};
|
||||
};
|
||||
|
||||
enum class ChainBreak
|
||||
{
|
||||
None, NewChain, Gap
|
||||
};
|
||||
|
||||
class DSSP
|
||||
{
|
||||
public:
|
||||
DSSP(const Structure& s, int min_poly_proline_stretch_length, bool calculateSurfaceAccessibility);
|
||||
~DSSP();
|
||||
|
||||
DSSP(const DSSP&) = delete;
|
||||
DSSP& operator=(const DSSP&) = delete;
|
||||
|
||||
SecondaryStructureType operator()(const std::string& inAsymID, int inSeqID) const;
|
||||
SecondaryStructureType operator()(const Monomer& m) const;
|
||||
|
||||
double accessibility(const std::string& inAsymID, int inSeqID) const;
|
||||
double accessibility(const Monomer& m) const;
|
||||
|
||||
bool isAlphaHelixEndBeforeStart(const Monomer& m) const;
|
||||
bool isAlphaHelixEndBeforeStart(const std::string& inAsymID, int inSeqID) const;
|
||||
|
||||
DSSP_Statistics GetStatistics() const;
|
||||
|
||||
class iterator;
|
||||
using res_iterator = typename std::vector<Res>::iterator;
|
||||
|
||||
class ResidueInfo
|
||||
{
|
||||
public:
|
||||
friend class iterator;
|
||||
|
||||
explicit operator bool() const { return not empty(); }
|
||||
bool empty() const { return mImpl == nullptr; }
|
||||
|
||||
const Monomer& residue() const;
|
||||
std::string alt_id() const;
|
||||
|
||||
/// \brief return 0 if not a break, ' ' in case of a new chain and '*' in case of a broken chain
|
||||
ChainBreak chainBreak() const;
|
||||
|
||||
/// \brief the internal number in DSSP
|
||||
int nr() const;
|
||||
|
||||
SecondaryStructureType ss() const;
|
||||
|
||||
int ssBridgeNr() const;
|
||||
|
||||
Helix helix(HelixType helixType) const;
|
||||
|
||||
bool bend() const;
|
||||
|
||||
double accessibility() const;
|
||||
|
||||
/// \brief returns resinfo, ladder and parallel
|
||||
std::tuple<ResidueInfo,int,bool> bridgePartner(int i) const;
|
||||
|
||||
int sheet() const;
|
||||
|
||||
/// \brief return resinfo and the energy of the bond
|
||||
std::tuple<ResidueInfo,double> acceptor(int i) const;
|
||||
std::tuple<ResidueInfo,double> donor(int i) const;
|
||||
|
||||
private:
|
||||
ResidueInfo(Res* res) : mImpl(res) {}
|
||||
|
||||
Res* mImpl;
|
||||
};
|
||||
|
||||
class iterator
|
||||
{
|
||||
public:
|
||||
using iterator_category = std::input_iterator_tag;
|
||||
using value_type = ResidueInfo;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = value_type*;
|
||||
using reference = value_type&;
|
||||
|
||||
iterator(const iterator& i);
|
||||
iterator(Res* res);
|
||||
iterator& operator=(const iterator& i);
|
||||
|
||||
reference operator*() { return mCurrent; }
|
||||
pointer operator->() { return &mCurrent; }
|
||||
|
||||
iterator& operator++();
|
||||
iterator operator++(int)
|
||||
{
|
||||
auto tmp(*this);
|
||||
this->operator++();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool operator==(const iterator& rhs) const { return mCurrent.mImpl == rhs.mCurrent.mImpl; }
|
||||
bool operator!=(const iterator& rhs) const { return mCurrent.mImpl != rhs.mCurrent.mImpl; }
|
||||
|
||||
private:
|
||||
ResidueInfo mCurrent;
|
||||
};
|
||||
|
||||
iterator begin() const;
|
||||
iterator end() const;
|
||||
|
||||
bool empty() const { return begin() == end(); }
|
||||
|
||||
private:
|
||||
struct DSSPImpl* mImpl;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@@ -1,544 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <numeric>
|
||||
|
||||
#include "cif++/AtomType.hpp"
|
||||
#include "cif++/Cif++.hpp"
|
||||
#include "cif++/Compound.hpp"
|
||||
#include "cif++/Point.hpp"
|
||||
|
||||
/*
|
||||
To modify a structure, you will have to use actions.
|
||||
|
||||
The currently supported actions are:
|
||||
|
||||
// - Move atom to new location
|
||||
- Remove atom
|
||||
// - Add new atom that was formerly missing
|
||||
// - Add alternate Residue
|
||||
-
|
||||
|
||||
*/
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
class Atom;
|
||||
class Residue;
|
||||
class Monomer;
|
||||
class Polymer;
|
||||
class Structure;
|
||||
class File;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class Atom
|
||||
{
|
||||
public:
|
||||
Atom();
|
||||
Atom(struct AtomImpl *impl);
|
||||
Atom(const Atom &rhs);
|
||||
|
||||
Atom(cif::Datablock &db, cif::Row &row);
|
||||
|
||||
// a special constructor to create symmetry copies
|
||||
Atom(const Atom &rhs, const Point &symmmetry_location, const std::string &symmetry_operation);
|
||||
|
||||
~Atom();
|
||||
|
||||
explicit operator bool() const { return mImpl_ != nullptr; }
|
||||
|
||||
// return a copy of this atom, with data copied instead of referenced
|
||||
Atom clone() const;
|
||||
|
||||
Atom &operator=(const Atom &rhs);
|
||||
|
||||
const std::string &id() const;
|
||||
AtomType type() const;
|
||||
|
||||
Point location() const;
|
||||
void location(Point p);
|
||||
|
||||
/// \brief Translate the position of this atom by \a t
|
||||
void translate(Point t);
|
||||
|
||||
/// \brief Rotate the position of this atom by \a q
|
||||
void rotate(Quaternion q);
|
||||
|
||||
// for direct access to underlying data, be careful!
|
||||
const cif::Row getRow() const;
|
||||
const cif::Row getRowAniso() const;
|
||||
|
||||
// Atom symmetryCopy(const Point& d, const clipper::RTop_orth& rt);
|
||||
bool isSymmetryCopy() const;
|
||||
std::string symmetry() const;
|
||||
// const clipper::RTop_orth& symop() const;
|
||||
|
||||
const Compound &comp() const;
|
||||
bool isWater() const;
|
||||
int charge() const;
|
||||
|
||||
float uIso() const;
|
||||
bool getAnisoU(float anisou[6]) const;
|
||||
float occupancy() const;
|
||||
|
||||
template <typename T>
|
||||
T property(const std::string &name) const;
|
||||
|
||||
void property(const std::string &name, const std::string &value);
|
||||
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
|
||||
void property(const std::string &name, const T &value)
|
||||
{
|
||||
property(name, std::to_string(value));
|
||||
}
|
||||
|
||||
// specifications
|
||||
std::string labelAtomID() const;
|
||||
std::string labelCompID() const;
|
||||
std::string labelAsymID() const;
|
||||
std::string labelEntityID() const;
|
||||
int labelSeqID() const;
|
||||
std::string labelAltID() const;
|
||||
bool isAlternate() const;
|
||||
|
||||
std::string authAtomID() const;
|
||||
std::string authCompID() const;
|
||||
std::string authAsymID() const;
|
||||
std::string authSeqID() const;
|
||||
std::string pdbxAuthInsCode() const;
|
||||
std::string pdbxAuthAltID() const;
|
||||
|
||||
std::string labelID() const; // label_comp_id + '_' + label_asym_id + '_' + label_seq_id
|
||||
std::string pdbID() const; // auth_comp_id + '_' + auth_asym_id + '_' + auth_seq_id + pdbx_PDB_ins_code
|
||||
|
||||
bool operator==(const Atom &rhs) const;
|
||||
|
||||
// // get clipper format Atom
|
||||
// clipper::Atom toClipper() const;
|
||||
|
||||
// Radius calculation based on integrating the density until perc of electrons is found
|
||||
void calculateRadius(float resHigh, float resLow, float perc);
|
||||
float radius() const;
|
||||
|
||||
// access data in compound for this atom
|
||||
|
||||
// convenience routine
|
||||
bool isBackBone() const
|
||||
{
|
||||
auto atomID = labelAtomID();
|
||||
return atomID == "N" or atomID == "O" or atomID == "C" or atomID == "CA";
|
||||
}
|
||||
|
||||
void swap(Atom &b)
|
||||
{
|
||||
std::swap(mImpl_, b.mImpl_);
|
||||
}
|
||||
|
||||
int compare(const Atom &b) const;
|
||||
|
||||
bool operator<(const Atom &rhs) const
|
||||
{
|
||||
return compare(rhs) < 0;
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Atom &atom);
|
||||
|
||||
private:
|
||||
friend class Structure;
|
||||
void setID(int id);
|
||||
|
||||
AtomImpl *impl();
|
||||
const AtomImpl *impl() const;
|
||||
|
||||
struct AtomImpl *mImpl_;
|
||||
};
|
||||
|
||||
inline void swap(mmcif::Atom &a, mmcif::Atom &b)
|
||||
{
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
inline double Distance(const Atom &a, const Atom &b)
|
||||
{
|
||||
return Distance(a.location(), b.location());
|
||||
}
|
||||
|
||||
inline double DistanceSquared(const Atom &a, const Atom &b)
|
||||
{
|
||||
return DistanceSquared(a.location(), b.location());
|
||||
}
|
||||
|
||||
typedef std::vector<Atom> AtomView;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class Residue
|
||||
{
|
||||
public:
|
||||
// constructors should be private, but that's not possible for now (needed in emplace)
|
||||
|
||||
// constructor for waters
|
||||
Residue(const Structure &structure, const std::string &compoundID,
|
||||
const std::string &asymID, const std::string &authSeqID);
|
||||
|
||||
// constructor for a residue without a sequence number
|
||||
Residue(const Structure &structure, const std::string &compoundID,
|
||||
const std::string &asymID);
|
||||
|
||||
// constructor for a residue with a sequence number
|
||||
Residue(const Structure &structure, const std::string &compoundID,
|
||||
const std::string &asymID, int seqID, const std::string &authSeqID);
|
||||
|
||||
Residue(const Residue &rhs) = delete;
|
||||
Residue &operator=(const Residue &rhs) = delete;
|
||||
|
||||
Residue(Residue &&rhs);
|
||||
Residue &operator=(Residue &&rhs);
|
||||
|
||||
virtual ~Residue();
|
||||
|
||||
const Compound &compound() const;
|
||||
const AtomView &atoms() const;
|
||||
|
||||
/// \brief Unique atoms returns only the atoms without alternates and the first of each alternate atom id.
|
||||
AtomView unique_atoms() const;
|
||||
|
||||
/// \brief The alt ID used for the unique atoms
|
||||
std::string unique_alt_id() const;
|
||||
|
||||
Atom atomByID(const std::string &atomID) const;
|
||||
|
||||
const std::string &compoundID() const { return mCompoundID; }
|
||||
const std::string &asymID() const { return mAsymID; }
|
||||
int seqID() const { return mSeqID; }
|
||||
std::string entityID() const;
|
||||
|
||||
std::string authAsymID() const;
|
||||
std::string authSeqID() const;
|
||||
std::string authInsCode() const;
|
||||
|
||||
// return a human readable PDB-like auth id (chain+seqnr+iCode)
|
||||
std::string authID() const;
|
||||
|
||||
// similar for mmCIF space
|
||||
std::string labelID() const;
|
||||
|
||||
// Is this residue a single entity?
|
||||
bool isEntity() const;
|
||||
|
||||
bool isWater() const { return mCompoundID == "HOH"; }
|
||||
|
||||
const Structure &structure() const { return *mStructure; }
|
||||
|
||||
bool empty() const { return mStructure == nullptr; }
|
||||
|
||||
bool hasAlternateAtoms() const;
|
||||
|
||||
/// \brief Return the list of unique alt ID's present in this residue
|
||||
std::set<std::string> getAlternateIDs() const;
|
||||
|
||||
/// \brief Return the list of unique atom ID's
|
||||
std::set<std::string> getAtomIDs() const;
|
||||
|
||||
/// \brief Return the list of atoms having ID \a atomID
|
||||
AtomView getAtomsByID(const std::string &atomID) const;
|
||||
|
||||
// some routines for 3d work
|
||||
std::tuple<Point, float> centerAndRadius() const;
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Residue &res);
|
||||
|
||||
protected:
|
||||
Residue() {}
|
||||
|
||||
friend class Polymer;
|
||||
|
||||
const Structure *mStructure = nullptr;
|
||||
std::string mCompoundID, mAsymID;
|
||||
int mSeqID = 0;
|
||||
|
||||
// Watch out, this is used only to label waters... The rest of the code relies on
|
||||
// MapLabelToAuth to get this info. Perhaps we should rename this member field.
|
||||
std::string mAuthSeqID;
|
||||
AtomView mAtoms;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// a monomer models a single Residue in a protein chain
|
||||
|
||||
class Monomer : public Residue
|
||||
{
|
||||
public:
|
||||
// Monomer();
|
||||
Monomer(const Monomer &rhs) = delete;
|
||||
Monomer &operator=(const Monomer &rhs) = delete;
|
||||
|
||||
Monomer(Monomer &&rhs);
|
||||
Monomer &operator=(Monomer &&rhs);
|
||||
|
||||
Monomer(const Polymer &polymer, size_t index, int seqID, const std::string &authSeqID,
|
||||
const std::string &compoundID);
|
||||
|
||||
bool is_first_in_chain() const;
|
||||
bool is_last_in_chain() const;
|
||||
|
||||
// convenience
|
||||
bool has_alpha() const;
|
||||
bool has_kappa() const;
|
||||
|
||||
// Assuming this is really an amino acid...
|
||||
|
||||
float phi() const;
|
||||
float psi() const;
|
||||
float alpha() const;
|
||||
float kappa() const;
|
||||
float tco() const;
|
||||
float omega() const;
|
||||
|
||||
// torsion angles
|
||||
size_t nrOfChis() const;
|
||||
float chi(size_t i) const;
|
||||
|
||||
bool isCis() const;
|
||||
|
||||
/// \brief Returns true if the four atoms C, CA, N and O are present
|
||||
bool isComplete() const;
|
||||
|
||||
/// \brief Returns true if any of the backbone atoms has an alternate
|
||||
bool hasAlternateBackboneAtoms() const;
|
||||
|
||||
Atom CAlpha() const { return atomByID("CA"); }
|
||||
Atom C() const { return atomByID("C"); }
|
||||
Atom N() const { return atomByID("N"); }
|
||||
Atom O() const { return atomByID("O"); }
|
||||
Atom H() const { return atomByID("H"); }
|
||||
|
||||
bool isBondedTo(const Monomer &rhs) const
|
||||
{
|
||||
return this != &rhs and areBonded(*this, rhs);
|
||||
}
|
||||
|
||||
static bool areBonded(const Monomer &a, const Monomer &b, float errorMargin = 0.5f);
|
||||
static bool isCis(const Monomer &a, const Monomer &b);
|
||||
static float omega(const Monomer &a, const Monomer &b);
|
||||
|
||||
// for LEU and VAL
|
||||
float chiralVolume() const;
|
||||
|
||||
private:
|
||||
const Polymer *mPolymer;
|
||||
size_t mIndex;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class Polymer : public std::vector<Monomer>
|
||||
{
|
||||
public:
|
||||
Polymer(const Structure &s, const std::string &entityID, const std::string &asymID);
|
||||
|
||||
Polymer(const Polymer &) = delete;
|
||||
Polymer &operator=(const Polymer &) = delete;
|
||||
|
||||
// Polymer(Polymer&& rhs) = delete;
|
||||
// Polymer& operator=(Polymer&& rhs) = de;
|
||||
|
||||
Monomer &getBySeqID(int seqID);
|
||||
const Monomer &getBySeqID(int seqID) const;
|
||||
|
||||
Structure *structure() const { return mStructure; }
|
||||
|
||||
std::string asymID() const { return mAsymID; }
|
||||
std::string entityID() const { return mEntityID; }
|
||||
|
||||
std::string chainID() const;
|
||||
|
||||
int Distance(const Monomer &a, const Monomer &b) const;
|
||||
|
||||
private:
|
||||
Structure *mStructure;
|
||||
std::string mEntityID;
|
||||
std::string mAsymID;
|
||||
cif::RowSet mPolySeq;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// file is a reference to the data stored in e.g. the cif file.
|
||||
// This object is not copyable.
|
||||
|
||||
class File : public std::enable_shared_from_this<File>
|
||||
{
|
||||
public:
|
||||
File();
|
||||
File(const std::filesystem::path &path);
|
||||
File(const char *data, size_t length); // good luck trying to find out what it is...
|
||||
~File();
|
||||
|
||||
File(const File &) = delete;
|
||||
File &operator=(const File &) = delete;
|
||||
|
||||
cif::Datablock& createDatablock(const std::string &name);
|
||||
|
||||
void load(const std::filesystem::path &path);
|
||||
void save(const std::filesystem::path &path);
|
||||
|
||||
Structure *model(size_t nr = 1);
|
||||
|
||||
struct FileImpl &impl() const { return *mImpl; }
|
||||
|
||||
cif::Datablock &data();
|
||||
cif::File &file();
|
||||
|
||||
private:
|
||||
struct FileImpl *mImpl;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
enum class StructureOpenOptions
|
||||
{
|
||||
SkipHydrogen = 1 << 0
|
||||
};
|
||||
|
||||
inline bool operator&(StructureOpenOptions a, StructureOpenOptions b)
|
||||
{
|
||||
return static_cast<int>(a) bitand static_cast<int>(b);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class Structure
|
||||
{
|
||||
public:
|
||||
Structure(File &p, size_t modelNr = 1, StructureOpenOptions options = {});
|
||||
Structure &operator=(const Structure &) = delete;
|
||||
~Structure();
|
||||
|
||||
// Create a read-only clone of the current structure (for multithreaded calculations that move atoms)
|
||||
Structure(const Structure &);
|
||||
|
||||
File &getFile() const;
|
||||
|
||||
const AtomView &atoms() const { return mAtoms; }
|
||||
AtomView waters() const;
|
||||
|
||||
const std::list<Polymer> &polymers() const { return mPolymers; }
|
||||
std::list<Polymer> &polymers() { return mPolymers; }
|
||||
|
||||
const std::vector<Residue> &nonPolymers() const { return mNonPolymers; }
|
||||
const std::vector<Residue> &branchResidues() const { return mBranchResidues; }
|
||||
|
||||
Atom getAtomByID(std::string id) const;
|
||||
// Atom getAtomByLocation(Point pt, float maxDistance) const;
|
||||
|
||||
Atom getAtomByLabel(const std::string &atomID, const std::string &asymID,
|
||||
const std::string &compID, int seqID, const std::string &altID = "");
|
||||
|
||||
/// \brief Get a residue, if \a seqID is zero, the non-polymers are searched
|
||||
const Residue &getResidue(const std::string &asymID, const std::string &compID, int seqID = 0) const;
|
||||
|
||||
// map between auth and label locations
|
||||
|
||||
std::tuple<std::string, int, std::string> MapAuthToLabel(const std::string &asymID,
|
||||
const std::string &seqID, const std::string &compID, const std::string &insCode = "");
|
||||
|
||||
std::tuple<std::string, std::string, std::string, std::string> MapLabelToAuth(
|
||||
const std::string &asymID, int seqID, const std::string &compID);
|
||||
|
||||
// returns chain, seqnr, icode
|
||||
std::tuple<char, int, char> MapLabelToAuth(
|
||||
const std::string &asymID, int seqID) const;
|
||||
|
||||
// returns chain,seqnr,comp,iCode
|
||||
std::tuple<std::string, int, std::string, std::string> MapLabelToPDB(
|
||||
const std::string &asymID, int seqID, const std::string &compID,
|
||||
const std::string &authSeqID) const;
|
||||
|
||||
std::tuple<std::string, int, std::string> MapPDBToLabel(
|
||||
const std::string &asymID, int seqID, const std::string &compID, const std::string &iCode) const;
|
||||
|
||||
// Actions
|
||||
void removeAtom(Atom &a);
|
||||
void swapAtoms(Atom &a1, Atom &a2); // swap the labels for these atoms
|
||||
void moveAtom(Atom &a, Point p); // move atom to a new location
|
||||
void changeResidue(const Residue &res, const std::string &newCompound,
|
||||
const std::vector<std::tuple<std::string, std::string>> &remappedAtoms);
|
||||
|
||||
/// \brief Create a new non-polymer entity, returns new ID
|
||||
/// \param mon_id The mon_id for the new nonpoly, must be an existing and known compound from CCD
|
||||
/// \return The ID of the created entity
|
||||
std::string createNonPolyEntity(const std::string &mon_id);
|
||||
|
||||
/// \brief Create a new NonPolymer struct_asym with atoms constructed from \a atoms, returns asym_id.
|
||||
/// This method assumes you are copying data from one cif file to another.
|
||||
///
|
||||
/// \param entity_id The entity ID of the new nonpoly
|
||||
/// \param atoms The array of atom_site rows containing the data.
|
||||
/// \return The newly create asym ID
|
||||
std::string createNonpoly(const std::string &entity_id, const std::vector<mmcif::Atom> &atoms);
|
||||
|
||||
/// \brief To sort the atoms in order of model > asym-id > res-id > atom-id
|
||||
/// Will asssign new atom_id's to all atoms. Be carefull
|
||||
void sortAtoms();
|
||||
|
||||
/// \brief Translate the coordinates of all atoms in the structure by \a t
|
||||
void translate(Point t);
|
||||
|
||||
/// \brief Rotate the coordinates of all atoms in the structure by \a q
|
||||
void rotate(Quaternion t);
|
||||
|
||||
const std::vector<Residue> &getNonPolymers() const { return mNonPolymers; }
|
||||
const std::vector<Residue> &getBranchResidues() const { return mBranchResidues; }
|
||||
|
||||
void cleanupEmptyCategories();
|
||||
|
||||
private:
|
||||
friend Polymer;
|
||||
friend Residue;
|
||||
// friend residue_view;
|
||||
// friend residue_iterator;
|
||||
|
||||
cif::Category &category(const char *name) const;
|
||||
cif::Datablock &datablock() const;
|
||||
|
||||
std::string insertCompound(const std::string &compoundID, bool isEntity);
|
||||
|
||||
void loadData();
|
||||
void updateAtomIndex();
|
||||
|
||||
File &mFile;
|
||||
size_t mModelNr;
|
||||
AtomView mAtoms;
|
||||
std::vector<size_t> mAtomIndex;
|
||||
std::list<Polymer> mPolymers;
|
||||
std::vector<Residue> mNonPolymers, mBranchResidues;
|
||||
};
|
||||
|
||||
} // namespace mmcif
|
||||
@@ -1,138 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
|
||||
#include "CifUtils.hpp"
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
struct Spacegroup
|
||||
{
|
||||
const char* name;
|
||||
const char* xHM;
|
||||
const char* Hall;
|
||||
int nr;
|
||||
};
|
||||
|
||||
CIFPP_EXPORT extern const Spacegroup kSpaceGroups[];
|
||||
CIFPP_EXPORT extern const std::size_t kNrOfSpaceGroups;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
struct SymopData
|
||||
{
|
||||
constexpr SymopData(const std::array<int,15>& data)
|
||||
: m_packed((data[ 0] & 0x03ULL) << 34 bitor
|
||||
(data[ 1] & 0x03ULL) << 32 bitor
|
||||
(data[ 2] & 0x03ULL) << 30 bitor
|
||||
(data[ 3] & 0x03ULL) << 28 bitor
|
||||
(data[ 4] & 0x03ULL) << 26 bitor
|
||||
(data[ 5] & 0x03ULL) << 24 bitor
|
||||
(data[ 6] & 0x03ULL) << 22 bitor
|
||||
(data[ 7] & 0x03ULL) << 20 bitor
|
||||
(data[ 8] & 0x03ULL) << 18 bitor
|
||||
(data[ 9] & 0x07ULL) << 15 bitor
|
||||
(data[10] & 0x07ULL) << 12 bitor
|
||||
(data[11] & 0x07ULL) << 9 bitor
|
||||
(data[12] & 0x07ULL) << 6 bitor
|
||||
(data[13] & 0x07ULL) << 3 bitor
|
||||
(data[14] & 0x07ULL) << 0)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator==(const SymopData& rhs) const
|
||||
{
|
||||
return m_packed == rhs.m_packed;
|
||||
}
|
||||
|
||||
std::array<int,15> data() const
|
||||
{
|
||||
return {
|
||||
static_cast<int>(m_packed >> 34) bitand 0x03,
|
||||
static_cast<int>(m_packed >> 32) bitand 0x03,
|
||||
static_cast<int>(m_packed >> 30) bitand 0x03,
|
||||
static_cast<int>(m_packed >> 28) bitand 0x03,
|
||||
static_cast<int>(m_packed >> 26) bitand 0x03,
|
||||
static_cast<int>(m_packed >> 24) bitand 0x03,
|
||||
static_cast<int>(m_packed >> 22) bitand 0x03,
|
||||
static_cast<int>(m_packed >> 20) bitand 0x03,
|
||||
static_cast<int>(m_packed >> 18) bitand 0x03,
|
||||
static_cast<int>(m_packed >> 15) bitand 0x07,
|
||||
static_cast<int>(m_packed >> 12) bitand 0x07,
|
||||
static_cast<int>(m_packed >> 9) bitand 0x07,
|
||||
static_cast<int>(m_packed >> 6) bitand 0x07,
|
||||
static_cast<int>(m_packed >> 3) bitand 0x07,
|
||||
static_cast<int>(m_packed >> 0) bitand 0x07,
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
friend struct SymopDataBlock;
|
||||
|
||||
const uint64_t kPackMask = (~0ULL >> (64-36));
|
||||
|
||||
SymopData(uint64_t v)
|
||||
: m_packed(v bitand kPackMask) {}
|
||||
|
||||
uint64_t m_packed;
|
||||
};
|
||||
|
||||
struct SymopDataBlock
|
||||
{
|
||||
constexpr SymopDataBlock(int spacegroup, int rotational_number, const std::array<int,15>& rt_data)
|
||||
: m_v((spacegroup & 0xffffULL) << 48 bitor
|
||||
(rotational_number & 0xffULL) << 40 bitor
|
||||
SymopData(rt_data).m_packed)
|
||||
{
|
||||
}
|
||||
|
||||
uint16_t spacegroup() const { return m_v >> 48; }
|
||||
SymopData symop() const { return SymopData(m_v); }
|
||||
uint8_t rotational_number() const { return (m_v >> 40) bitand 0xff; }
|
||||
|
||||
private:
|
||||
uint64_t m_v;
|
||||
};
|
||||
|
||||
static_assert(sizeof(SymopDataBlock) == sizeof(uint64_t), "Size of SymopData is wrong");
|
||||
|
||||
CIFPP_EXPORT extern const SymopDataBlock kSymopNrTable[];
|
||||
CIFPP_EXPORT extern const std::size_t kSymopNrTableSize;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
int GetSpacegroupNumber(std::string spacegroup); // alternative for clipper's parsing code
|
||||
|
||||
}
|
||||
339
include/cif++/atom_type.hpp
Normal file
339
include/cif++/atom_type.hpp
Normal file
@@ -0,0 +1,339 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/** \file atom_type.hpp
|
||||
*
|
||||
* This file contains information about all known elements
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/exports.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
/** Atom type as an integer. All known elements are available as a constant. */
|
||||
|
||||
enum atom_type : uint8_t
|
||||
{
|
||||
Nn = 0, ///< Unknown
|
||||
|
||||
H = 1, ///< Hydrogen
|
||||
He = 2, ///< Helium
|
||||
|
||||
Li = 3, ///< Lithium
|
||||
Be = 4, ///< Beryllium
|
||||
B = 5, ///< Boron
|
||||
C = 6, ///< Carbon
|
||||
N = 7, ///< Nitrogen
|
||||
O = 8, ///< Oxygen
|
||||
F = 9, ///< Fluorine
|
||||
Ne = 10, ///< Neon
|
||||
|
||||
Na = 11, ///< Sodium
|
||||
Mg = 12, ///< Magnesium
|
||||
Al = 13, ///< Aluminium
|
||||
Si = 14, ///< Silicon
|
||||
P = 15, ///< Phosphorus
|
||||
S = 16, ///< Sulfur
|
||||
Cl = 17, ///< Chlorine
|
||||
Ar = 18, ///< Argon
|
||||
|
||||
K = 19, ///< Potassium
|
||||
Ca = 20, ///< Calcium
|
||||
Sc = 21, ///< Scandium
|
||||
Ti = 22, ///< Titanium
|
||||
V = 23, ///< Vanadium
|
||||
Cr = 24, ///< Chromium
|
||||
Mn = 25, ///< Manganese
|
||||
Fe = 26, ///< Iron
|
||||
Co = 27, ///< Cobalt
|
||||
Ni = 28, ///< Nickel
|
||||
Cu = 29, ///< Copper
|
||||
Zn = 30, ///< Zinc
|
||||
Ga = 31, ///< Gallium
|
||||
Ge = 32, ///< Germanium
|
||||
As = 33, ///< Arsenic
|
||||
Se = 34, ///< Selenium
|
||||
Br = 35, ///< Bromine
|
||||
Kr = 36, ///< Krypton
|
||||
|
||||
Rb = 37, ///< Rubidium
|
||||
Sr = 38, ///< Strontium
|
||||
Y = 39, ///< Yttrium
|
||||
Zr = 40, ///< Zirconium
|
||||
Nb = 41, ///< Niobium
|
||||
Mo = 42, ///< Molybdenum
|
||||
Tc = 43, ///< Technetium
|
||||
Ru = 44, ///< Ruthenium
|
||||
Rh = 45, ///< Rhodium
|
||||
Pd = 46, ///< Palladium
|
||||
Ag = 47, ///< Silver
|
||||
Cd = 48, ///< Cadmium
|
||||
In = 49, ///< Indium
|
||||
Sn = 50, ///< Tin
|
||||
Sb = 51, ///< Antimony
|
||||
Te = 52, ///< Tellurium
|
||||
I = 53, ///< Iodine
|
||||
Xe = 54, ///< Xenon
|
||||
Cs = 55, ///< Caesium
|
||||
Ba = 56, ///< Barium
|
||||
La = 57, ///< Lanthanum
|
||||
|
||||
Hf = 72, ///< Hafnium
|
||||
Ta = 73, ///< Tantalum
|
||||
W = 74, ///< Tungsten
|
||||
Re = 75, ///< Rhenium
|
||||
Os = 76, ///< Osmium
|
||||
Ir = 77, ///< Iridium
|
||||
Pt = 78, ///< Platinum
|
||||
Au = 79, ///< Gold
|
||||
Hg = 80, ///< Mercury
|
||||
Tl = 81, ///< Thallium
|
||||
Pb = 82, ///< Lead
|
||||
Bi = 83, ///< Bismuth
|
||||
Po = 84, ///< Polonium
|
||||
At = 85, ///< Astatine
|
||||
Rn = 86, ///< Radon
|
||||
Fr = 87, ///< Francium
|
||||
Ra = 88, ///< Radium
|
||||
Ac = 89, ///< Actinium
|
||||
|
||||
Rf = 104, ///< Rutherfordium
|
||||
Db = 105, ///< Dubnium
|
||||
Sg = 106, ///< Seaborgium
|
||||
Bh = 107, ///< Bohrium
|
||||
Hs = 108, ///< Hassium
|
||||
Mt = 109, ///< Meitnerium
|
||||
Ds = 110, ///< Darmstadtium
|
||||
Rg = 111, ///< Roentgenium
|
||||
Cn = 112, ///< Copernicium
|
||||
Nh = 113, ///< Nihonium
|
||||
Fl = 114, ///< Flerovium
|
||||
Mc = 115, ///< Moscovium
|
||||
Lv = 116, ///< Livermorium
|
||||
Ts = 117, ///< Tennessine
|
||||
Og = 118, ///< Oganesson
|
||||
|
||||
Ce = 58, ///< Cerium
|
||||
Pr = 59, ///< Praseodymium
|
||||
Nd = 60, ///< Neodymium
|
||||
Pm = 61, ///< Promethium
|
||||
Sm = 62, ///< Samarium
|
||||
Eu = 63, ///< Europium
|
||||
Gd = 64, ///< Gadolinium
|
||||
Tb = 65, ///< Terbium
|
||||
Dy = 66, ///< Dysprosium
|
||||
Ho = 67, ///< Holmium
|
||||
Er = 68, ///< Erbium
|
||||
Tm = 69, ///< Thulium
|
||||
Yb = 70, ///< Ytterbium
|
||||
Lu = 71, ///< Lutetium
|
||||
|
||||
Th = 90, ///< Thorium
|
||||
Pa = 91, ///< Protactinium
|
||||
U = 92, ///< Uranium
|
||||
Np = 93, ///< Neptunium
|
||||
Pu = 94, ///< Plutonium
|
||||
Am = 95, ///< Americium
|
||||
Cm = 96, ///< Curium
|
||||
Bk = 97, ///< Berkelium
|
||||
Cf = 98, ///< Californium
|
||||
Es = 99, ///< Einsteinium
|
||||
Fm = 100, ///< Fermium
|
||||
Md = 101, ///< Mendelevium
|
||||
No = 102, ///< Nobelium
|
||||
Lr = 103, ///< Lawrencium
|
||||
|
||||
D = 119, ///< Deuterium
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// An enum used to select the desired radius for an atom.
|
||||
/// All values are collected from the wikipedia pages on atom radii
|
||||
|
||||
enum class radius_type
|
||||
{
|
||||
calculated, ///< Calculated radius from theoretical models
|
||||
empirical, ///< Empirically measured covalent radii
|
||||
|
||||
/// @deprecated It is a bit unclear where these values came from. So, better not use them
|
||||
covalent_empirical,
|
||||
|
||||
single_bond, ///< Bond length for a single covalent bond calculated using statistically analysis
|
||||
double_bond, ///< Bond length for a double covalent bond calculated using statistically analysis
|
||||
triple_bond, ///< Bond length for a triple covalent bond calculated using statistically analysis
|
||||
|
||||
van_der_waals, ///< Radius of an imaginary hard sphere representing the distance of closest approach for another atom
|
||||
|
||||
type_count ///< Number of radii
|
||||
};
|
||||
|
||||
/// @brief The number of radii per element which can be requested from atom_type_info
|
||||
constexpr std::size_t kRadiusTypeCount = static_cast<std::size_t>(radius_type::type_count);
|
||||
|
||||
/// An enum used to select either the effective or the crystal radius of an ion.
|
||||
/// See explanation on Wikipedia: https://en.wikipedia.org/wiki/Ionic_radius
|
||||
|
||||
enum class ionic_radius_type
|
||||
{
|
||||
effective, ///< Based on distance between ions in a crystal structure as determined by X-ray crystallography
|
||||
crystal ///< Calculated ion radius based on a function of ionic charge and spin
|
||||
};
|
||||
|
||||
/// Requests for an unknown radius value return kNA
|
||||
constexpr float kNA = std::numeric_limits<float>::quiet_NaN();
|
||||
|
||||
/// A struct holding the known information for all elements defined in atom_type
|
||||
|
||||
struct atom_type_info
|
||||
{
|
||||
/// The type as an atom_type
|
||||
atom_type type;
|
||||
|
||||
/// The official name for this element
|
||||
std::string name;
|
||||
|
||||
/// The official symbol for this element
|
||||
std::string symbol;
|
||||
|
||||
/// The weight of this element
|
||||
float weight;
|
||||
|
||||
/// A flag indicating whether the element is a metal
|
||||
bool metal;
|
||||
|
||||
/// Array containing all known radii for this element. A value of kNA is
|
||||
/// stored for unknown values
|
||||
float radii[kRadiusTypeCount];
|
||||
};
|
||||
|
||||
/// Array of atom_type_info struct for each of the defined elements in atom_type
|
||||
|
||||
extern CIFPP_EXPORT const atom_type_info kKnownAtoms[];
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// AtomTypeTraits
|
||||
|
||||
/// A traits class to access information for known elements
|
||||
|
||||
class atom_type_traits
|
||||
{
|
||||
public:
|
||||
/// Constructor taking an atom_type \a a
|
||||
atom_type_traits(atom_type a);
|
||||
|
||||
/// Constructor based on the element as a string in \a symbol
|
||||
atom_type_traits(const std::string &symbol);
|
||||
|
||||
atom_type type() const { return m_info->type; } ///< Returns the atom_type
|
||||
std::string name() const { return m_info->name; } ///< Returns the name of the element
|
||||
std::string symbol() const { return m_info->symbol; } ///< Returns the symbol of the element
|
||||
float weight() const { return m_info->weight; } ///< Returns the average weight of the element
|
||||
|
||||
bool is_metal() const { return m_info->metal; } ///< Returns true if the element is a metal
|
||||
|
||||
/// Return true if the symbol in \a symbol actually exists in the list of known elements in atom_type
|
||||
static bool is_element(const std::string &symbol);
|
||||
|
||||
/// Return true if the symbol in \a symbol exists and is a metal
|
||||
static bool is_metal(const std::string &symbol);
|
||||
|
||||
/// @brief Return the radius for the element, use \a type to select which radius to return
|
||||
/// @param type The selector for which radius to return
|
||||
/// @return The requested radius or kNA if not known (or applicable)
|
||||
float radius(radius_type type = radius_type::single_bond) const
|
||||
{
|
||||
if (type >= radius_type::type_count)
|
||||
throw std::invalid_argument("invalid radius requested");
|
||||
return m_info->radii[static_cast<std::size_t>(type)] / 100.f;
|
||||
}
|
||||
|
||||
/// \brief Return the radius for a charged version of this atom in a solid crystal
|
||||
///
|
||||
/// \param charge The charge of the ion
|
||||
/// \return The radius of the ion
|
||||
float crystal_ionic_radius(int charge) const;
|
||||
|
||||
/// \brief Return the radius for a charged version of this atom in a non-solid environment
|
||||
///
|
||||
/// \param charge The charge of the ion
|
||||
/// \return The radius of the ion
|
||||
float effective_ionic_radius(int charge) const;
|
||||
|
||||
/// \brief Return the radius for a charged version of this atom, returns the effective radius by default
|
||||
///
|
||||
/// \param charge The charge of the ion
|
||||
/// \param type The requested ion radius type
|
||||
/// \return The radius of the ion
|
||||
float ionic_radius(int charge, ionic_radius_type type = ionic_radius_type::effective) const
|
||||
{
|
||||
return type == ionic_radius_type::effective ? effective_ionic_radius(charge) : crystal_ionic_radius(charge);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief data type encapsulating the scattering factors
|
||||
* in a simplified form (only a and b).
|
||||
*/
|
||||
struct SFData
|
||||
{
|
||||
/** @cond */
|
||||
double a[6], b[6];
|
||||
/** @endcond */
|
||||
};
|
||||
|
||||
/// @brief to get the Cval and Siva scattering factor values, use this constant as charge:
|
||||
static constexpr int kWKSFVal = -99;
|
||||
|
||||
/// @brief Return the Waasmaier & Kirfel scattering factor values for the element
|
||||
///
|
||||
/// The coefficients from Waasmaier & Kirfel (1995), Acta Cryst. A51, 416-431.
|
||||
///
|
||||
/// @param charge The charge for which the structure values should be returned, use kWSKFVal to return the *Cval* and *Siva* values
|
||||
/// @return The scattering factors as a SFData struct
|
||||
const SFData &wksf(int charge = 0) const;
|
||||
|
||||
/// @brief Return the electron scattering factor values for the element
|
||||
///
|
||||
/// @return The scattering factors as a SFData struct
|
||||
const SFData &elsf() const;
|
||||
|
||||
/// Clipper doesn't like atoms with charges that do not have a scattering factor. And
|
||||
/// rightly so, but we need to know in advance if this is the case
|
||||
bool has_sf(int charge) const;
|
||||
|
||||
private:
|
||||
const struct atom_type_info *m_info;
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
1283
include/cif++/category.hpp
Normal file
1283
include/cif++/category.hpp
Normal file
File diff suppressed because it is too large
Load Diff
363
include/cif++/compound.hpp
Normal file
363
include/cif++/compound.hpp
Normal file
@@ -0,0 +1,363 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020-2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/atom_type.hpp"
|
||||
#include "cif++/datablock.hpp"
|
||||
#include "cif++/exports.hpp"
|
||||
#include "cif++/point.hpp"
|
||||
#include "cif++/utilities.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
/// \file compound.hpp
|
||||
/// This file contains the definition for the class compound, encapsulating
|
||||
/// the information found for compounds in the CCD.
|
||||
///
|
||||
/// The data is loaded by default from a file called `components.cif`. This file
|
||||
/// is located using load_resource. (See documentation on cif::load_resource for more information)
|
||||
///
|
||||
/// Note that since version 6 the CCP4 monomer library is no longer used.
|
||||
|
||||
/// See also :doc:`/compound` for more information.
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class compound;
|
||||
struct compound_atom;
|
||||
class compound_factory_impl;
|
||||
|
||||
/// \brief The bond type or bond order as defined in the CCD, possible values taken from the mmcif_pdbx file
|
||||
enum class bond_type
|
||||
{
|
||||
sing, ///< single bond
|
||||
doub, ///< double bond
|
||||
trip, ///< triple bond
|
||||
quad, ///< quadruple bond
|
||||
arom, ///< aromatic bond
|
||||
poly, ///< polymeric bond
|
||||
delo, ///< delocalized double bond
|
||||
pi, ///< pi bond
|
||||
};
|
||||
|
||||
/// @brief return the string representation of @a bondType
|
||||
std::string bond_type_to_string(bond_type bondType);
|
||||
|
||||
/// @brief return the cif::bond_type for the string representation @a bondType
|
||||
bond_type parse_bond_type_from_string(const std::string &bondType);
|
||||
|
||||
/// \brief The possible stereo config values for a compound_atom.
|
||||
///
|
||||
/// As the site https://psiberg.com/r-s-nomenclature/ states:
|
||||
///
|
||||
/// > RS nomenclature is currently the preferred system for assigning absolute
|
||||
/// > configuration to chiral molecules. The letters R and S come from the Latin
|
||||
/// > words ‘Rectus‘ and ‘Sinister‘ meaning ‘right’ and ‘left’. Molecules that
|
||||
/// > rotate the plane of polarized light to right are referred to as ‘R isomers’
|
||||
/// > and the molecules that rotate the plane of polarized light to left are
|
||||
/// > referred to ‘S isomers’.
|
||||
enum class stereo_config_type : uint8_t
|
||||
{
|
||||
N = 'N', ///< Not polarizing
|
||||
R = 'R', ///< Rectus
|
||||
S = 'S' ///< Sinister
|
||||
};
|
||||
|
||||
/// @brief return the string representation of @a stereo_config
|
||||
std::string to_string(stereo_config_type stereo_config);
|
||||
|
||||
/// @brief return the cif::stereo_config_type for the string representation @a stereo_config
|
||||
stereo_config_type parse_stereo_config_from_string(const std::string &stereo_config);
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
/// \brief struct containing information about an atom in a chemical compound.
|
||||
/// This is a subset of the available information. Contact the author if you need more fields.
|
||||
|
||||
struct compound_atom
|
||||
{
|
||||
std::string id; ///< Identifier for each atom in the chemical component
|
||||
atom_type type_symbol; ///< The element type for each atom in the chemical component.
|
||||
int charge = 0; ///< The formal charge assigned to each atom in the chemical component.
|
||||
bool aromatic = false; ///< Defines atoms in an aromatic moiety
|
||||
bool leaving_atom = false; ///< Flags atoms with "leaving" capability
|
||||
stereo_config_type stereo_config = stereo_config_type::N; ///< Defines the stereochemical configuration of the chiral center atom.
|
||||
float x, ///< The x component of the coordinates for each atom specified as orthogonal angstroms.
|
||||
y, ///< The y component of the coordinates for each atom specified as orthogonal angstroms.
|
||||
z; ///< The z component of the coordinates for each atom specified as orthogonal angstroms.
|
||||
|
||||
/// Return the location of the atom as a point
|
||||
point get_location() const
|
||||
{
|
||||
return { x, y, z };
|
||||
}
|
||||
};
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
/// \brief struct containing information about the bonds
|
||||
|
||||
struct compound_bond
|
||||
{
|
||||
std::string atom_id[2]; ///< The ID's of the two atoms that define the bond.
|
||||
bond_type type; ///< The bond order of the chemical bond associated with the specified atoms.
|
||||
bool aromatic = false, ///< Defines aromatic bonds.
|
||||
stereo_config = false; ///< Defines stereochemical bonds.
|
||||
};
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
/// \brief a class that contains information about a chemical compound.
|
||||
/// This information is derived from the CDD by default.
|
||||
///
|
||||
/// To create compounds, you use the factory method. You can add your own
|
||||
/// compound definitions by calling the push_dictionary function and
|
||||
/// pass it a valid CCD formatted file.
|
||||
|
||||
class compound
|
||||
{
|
||||
public:
|
||||
// accessors
|
||||
|
||||
std::string id() const { return m_id; } ///< Return the alphanumeric code for the chemical component.
|
||||
std::string name() const { return m_name; } ///< Return the name of the chemical component.
|
||||
std::string type() const { return m_type; } ///< Return the type of monomer.
|
||||
std::string formula() const { return m_formula; } ///< Return the chemical formula of the chemical component.
|
||||
float formula_weight() const { return m_formula_weight; } ///< Return the formula mass of the chemical component in Daltons.
|
||||
int formal_charge() const { return m_formal_charge; } ///< Return the formal charge on the chemical component.
|
||||
|
||||
const std::vector<compound_atom> &atoms() const { return m_atoms; } ///< Return the list of atoms for this compound
|
||||
const std::vector<compound_bond> &bonds() const { return m_bonds; } ///< Return the list of bonds for this compound
|
||||
|
||||
compound_atom get_atom_by_atom_id(const std::string &atom_id) const; ///< Return the atom with id @a atom_id
|
||||
|
||||
bool atoms_bonded(const std::string &atomId_1, const std::string &atomId_2) const; ///< Return true if @a atomId_1 is bonded to @a atomId_2
|
||||
float bond_length(const std::string &atomId_1, const std::string &atomId_2) const; ///< Return the bond length between @a atomId_1 and @a atomId_2
|
||||
|
||||
bool is_water() const ///< Return if the compound is actually a water
|
||||
{
|
||||
return m_id == "HOH" or m_id == "H2O" or m_id == "WAT";
|
||||
}
|
||||
|
||||
/** \brief Return whether this compound has a type of either 'peptide linking' or 'L-peptide linking' */
|
||||
bool is_peptide() const;
|
||||
|
||||
/** \brief Return whether this compound has a type of either 'DNA linking' or 'RNA linking' */
|
||||
bool is_base() const;
|
||||
|
||||
char one_letter_code() const { return m_one_letter_code; }; ///< Return the one letter code to use in a canonical sequence. If unknown the value '\0' is returned
|
||||
std::string parent_id() const { return m_parent_id; }; ///< Return the parent id code in case a parent is specified (e.g. MET for MSE)
|
||||
|
||||
private:
|
||||
friend class compound_factory_impl;
|
||||
friend class local_compound_factory_impl;
|
||||
|
||||
compound(cif::datablock &db);
|
||||
|
||||
std::string m_id;
|
||||
std::string m_name;
|
||||
std::string m_type;
|
||||
std::string m_formula;
|
||||
char m_one_letter_code = 0;
|
||||
std::string m_parent_id;
|
||||
float m_formula_weight = 0;
|
||||
int m_formal_charge = 0;
|
||||
std::vector<compound_atom> m_atoms;
|
||||
std::vector<compound_bond> m_bonds;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Factory class for compound and Link objects
|
||||
|
||||
/// @brief Options available to configure a compound factory
|
||||
struct compound_factory_options
|
||||
{
|
||||
/// If you have a multithreaded application and want to have different
|
||||
/// compounds in each thread (e.g. a web service processing user requests
|
||||
/// with different sets of compounds) you can set this flag to true.
|
||||
bool use_thread_local_instance_only = false;
|
||||
|
||||
#if HAVE_CURL
|
||||
// Various locations for chem_comp data files:
|
||||
// - ftp://files.ebi.ac.uk/pub/databases/pdb/refdata/chem_comp
|
||||
// - https://files.rcsb.org/pub/pdb/refdata/chem_comp/
|
||||
|
||||
std::string remote_chem_comp_url = "ftp://files.ebi.ac.uk/pub/databases/pdb/refdata/chem_comp";
|
||||
#endif
|
||||
};
|
||||
|
||||
/// Use the compound_factory singleton instance to create compound objects
|
||||
|
||||
class compound_factory
|
||||
{
|
||||
public:
|
||||
/// \brief Initialise a singleton instance.
|
||||
///
|
||||
/// If you have a multithreaded application and want to have different
|
||||
/// compounds in each thread (e.g. a web service processing user requests
|
||||
/// with different sets of compounds) you can set the \a useThreadLocalInstanceOnly
|
||||
/// flag to true.
|
||||
|
||||
[[deprecated("Use version with compound_factory_options instead")]]
|
||||
static void init(bool useThreadLocalInstanceOnly);
|
||||
|
||||
/// \brief Initialise a singleton instance.
|
||||
static void init(compound_factory_options options = {});
|
||||
|
||||
/// Return the singleton instance. If initialized with local threads, this is the
|
||||
/// instance for the current thread.
|
||||
static compound_factory &instance();
|
||||
|
||||
/// Delete and reset the singleton instance. If initialized with local threads, this is the
|
||||
/// instance for the current thread.
|
||||
static void clear();
|
||||
|
||||
/// Set the default dictionary file to @a inDictFile
|
||||
void set_default_dictionary(const std::filesystem::path &inDictFile);
|
||||
|
||||
/// Override any previously loaded dictionary with @a inDictFile
|
||||
void push_dictionary(const std::filesystem::path &inDictFile);
|
||||
|
||||
/** @brief Override any previously loaded dictionary with the data in @a file
|
||||
*
|
||||
* @note experimental feature
|
||||
*
|
||||
* Load the file @a file as a source for compound information. This may
|
||||
* be e.g. a regular mmCIF file with extra files containing compound
|
||||
* information.
|
||||
*
|
||||
* Be carefull to remove the block again, best use @ref cif::compound_source
|
||||
* as a stack based object.
|
||||
*/
|
||||
|
||||
void push_dictionary(const file &file);
|
||||
|
||||
/// Remove the last pushed dictionary
|
||||
|
||||
// TODO: check if the popped dict is the correct one
|
||||
void pop_dictionary();
|
||||
|
||||
/// Return whether @a res_name is a valid and known peptide
|
||||
[[deprecated("use is_peptide or is_std_peptide instead)")]]
|
||||
bool is_known_peptide(const std::string &res_name) const;
|
||||
|
||||
/// Return whether @a res_name is a valid and known base
|
||||
[[deprecated("use is_base or is_std_base instead)")]]
|
||||
bool is_known_base(const std::string &res_name) const;
|
||||
|
||||
/// Return whether @a res_name is a peptide
|
||||
bool is_peptide(std::string_view res_name) const;
|
||||
|
||||
/// Return whether @a res_name is a base
|
||||
bool is_base(std::string_view res_name) const;
|
||||
|
||||
/// Return whether @a res_name is one of the standard peptides
|
||||
bool is_std_peptide(std::string_view res_name) const;
|
||||
|
||||
/// Return whether @a res_name is one of the standard bases
|
||||
bool is_std_base(std::string_view res_name) const;
|
||||
|
||||
/// Return whether @a res_name is a monomer (either base or peptide)
|
||||
bool is_monomer(std::string_view res_name) const;
|
||||
|
||||
/// Return whether @a res_name is one of the standard bases or peptides
|
||||
bool is_std_monomer(std::string_view res_name) const
|
||||
{
|
||||
return is_std_base(res_name) or is_std_peptide(res_name);
|
||||
}
|
||||
|
||||
bool is_water(std::string_view res_name) const
|
||||
{
|
||||
return res_name == "HOH" or res_name == "H2O" or res_name == "WAT";
|
||||
}
|
||||
|
||||
/// \brief Create the compound object for \a id
|
||||
///
|
||||
/// This will create the compound instance for \a id if it doesn't exist already.
|
||||
/// The result is owned by this factory and should not be deleted by the user.
|
||||
/// \param id The compound ID, a three letter code usually
|
||||
/// \result The compound, or nullptr if it could not be created (missing info)
|
||||
const compound *create(std::string_view id);
|
||||
|
||||
~compound_factory();
|
||||
|
||||
CIFPP_EXPORT static const std::map<std::string, char> kAAMap, ///< Globally accessible static list of the default amino acids
|
||||
kBaseMap; ///< Globally accessible static list of the default bases
|
||||
|
||||
void report_missing_compound(std::string_view compound_id);
|
||||
|
||||
private:
|
||||
compound_factory();
|
||||
|
||||
compound_factory(const compound_factory &) = delete;
|
||||
compound_factory &operator=(const compound_factory &) = delete;
|
||||
|
||||
static std::unique_ptr<compound_factory> s_instance;
|
||||
static thread_local std::unique_ptr<compound_factory> tl_instance;
|
||||
static compound_factory_options s_options;
|
||||
|
||||
std::shared_ptr<compound_factory_impl> m_impl;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Stack based source for compound info.
|
||||
*
|
||||
* Use this class to temporarily add a compound source to the
|
||||
* compound_factory.
|
||||
*
|
||||
* @code{.cpp}
|
||||
* cif::file f("1cbs-with-custom-rea.cif");
|
||||
* cif::compound_source cs(f);
|
||||
*
|
||||
* auto &cf = cif::compound_factory::instance();
|
||||
* auto rea_compound = cf.create("REA");
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
|
||||
// TODO: check if pushed and popped dicts are the same!
|
||||
|
||||
class compound_source
|
||||
{
|
||||
public:
|
||||
compound_source(const cif::file &file)
|
||||
{
|
||||
cif::compound_factory::instance().push_dictionary(file);
|
||||
}
|
||||
|
||||
~compound_source()
|
||||
{
|
||||
cif::compound_factory::instance().pop_dictionary();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
1359
include/cif++/condition.hpp
Normal file
1359
include/cif++/condition.hpp
Normal file
File diff suppressed because it is too large
Load Diff
253
include/cif++/datablock.hpp
Normal file
253
include/cif++/datablock.hpp
Normal file
@@ -0,0 +1,253 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/category.hpp"
|
||||
#include "cif++/forward_decl.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
/** \file datablock.hpp
|
||||
* Each valid mmCIF file contains at least one @ref cif::datablock.
|
||||
* A datablock has a name and can contain one or more @ref cif::category "categories"
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief A datablock is a list of category objects with some additional features
|
||||
*
|
||||
*/
|
||||
|
||||
class datablock : public std::list<category>
|
||||
{
|
||||
public:
|
||||
datablock() = default;
|
||||
|
||||
/**
|
||||
* @brief Construct a new datablock object with name @a name
|
||||
*
|
||||
* @param name The name for the new datablock
|
||||
*/
|
||||
datablock(std::string_view name)
|
||||
: m_name(name)
|
||||
{
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
datablock(const datablock &);
|
||||
|
||||
datablock(datablock &&db) noexcept
|
||||
{
|
||||
swap_(*this, db);
|
||||
}
|
||||
|
||||
datablock &operator=(datablock db)
|
||||
{
|
||||
swap_(*this, db);
|
||||
return *this;
|
||||
}
|
||||
/** @endcond */
|
||||
|
||||
friend void swap_(datablock &a, datablock &b) noexcept
|
||||
{
|
||||
std::swap(a.m_name, b.m_name);
|
||||
std::swap(a.m_validator, b.m_validator);
|
||||
std::swap(static_cast<std::list<category>&>(a), static_cast<std::list<category>&>(b));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Return the name of this datablock
|
||||
*/
|
||||
const std::string &name() const { return m_name; }
|
||||
|
||||
/**
|
||||
* @brief Set the name of this datablock to @a name
|
||||
*
|
||||
* @param name The new name
|
||||
*/
|
||||
void set_name(std::string_view name)
|
||||
{
|
||||
m_name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Attempt to load the dictionary specified in audit_conform category
|
||||
*
|
||||
*/
|
||||
void load_dictionary();
|
||||
|
||||
/**
|
||||
* @brief Set the validator object to @a v
|
||||
*
|
||||
* @param v The new validator object, may be null
|
||||
*/
|
||||
void set_validator(const validator *v);
|
||||
|
||||
/**
|
||||
* @brief Get the validator object
|
||||
*
|
||||
* @return const validator* The validator or nullptr if there is none
|
||||
*/
|
||||
const validator *get_validator() const;
|
||||
|
||||
/**
|
||||
* @brief Validates the content of this datablock and all its content
|
||||
*
|
||||
* @return true If the content is valid
|
||||
* @return false If the content is not valid
|
||||
*/
|
||||
bool is_valid() const;
|
||||
|
||||
/**
|
||||
* @brief Validates all contained data for valid links between parents and children
|
||||
* as defined in the validator
|
||||
*
|
||||
* @return true If all links are valid
|
||||
* @return false If all links are not valid
|
||||
*/
|
||||
bool validate_links() const;
|
||||
|
||||
/**
|
||||
* @brief Strip removes all categories and items that are invalid according
|
||||
* to the assigned validator. Will also add a valid audit_conform block.
|
||||
*
|
||||
* @return true if the remaining datablock is valid
|
||||
*/
|
||||
bool strip();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Return the category named @a name, will create a new and empty
|
||||
* category named @a name if it does not exist.
|
||||
*
|
||||
* @param name The name of the category to return
|
||||
* @return category& Reference to the named category
|
||||
*/
|
||||
category &operator[](std::string_view name);
|
||||
|
||||
/**
|
||||
* @brief Return the const category named @a name, will return a reference
|
||||
* to a static empty category if it was not found.
|
||||
*
|
||||
* @param name The name of the category to return
|
||||
* @return category& Reference to the named category
|
||||
*/
|
||||
const category &operator[](std::string_view name) const;
|
||||
|
||||
/**
|
||||
* @brief Return a pointer to the category named @a name or nullptr if
|
||||
* it does not exist.
|
||||
*
|
||||
* @param name The name of the category
|
||||
* @return category* Pointer to the category found or nullptr
|
||||
*/
|
||||
category *get(std::string_view name);
|
||||
|
||||
/**
|
||||
* @brief Return a pointer to the category named @a name or nullptr if
|
||||
* it does not exist.
|
||||
*
|
||||
* @param name The name of the category
|
||||
* @return category* Pointer to the category found or nullptr
|
||||
*/
|
||||
const category *get(std::string_view name) const;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Return true if this datablock contains a non-empty category
|
||||
*/
|
||||
bool contains(std::string_view name) const
|
||||
{
|
||||
return get(name) != nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Tries to find a category with name @a name and will create a
|
||||
* new one if it is not found. The result is a tuple of an iterator
|
||||
* pointing to the category and a boolean indicating whether the category
|
||||
* was created or not.
|
||||
*
|
||||
* @param name The name for the category
|
||||
* @return std::tuple<iterator, bool> A tuple containing an iterator pointing
|
||||
* at the category and a boolean indicating whether the category was newly
|
||||
* created.
|
||||
*/
|
||||
std::tuple<iterator, bool> emplace(std::string_view name);
|
||||
|
||||
/**
|
||||
* @brief Get the preferred order of the categories when writing them
|
||||
*/
|
||||
[[deprecated("use get_item_order instead")]]
|
||||
std::vector<std::string> get_tag_order() const
|
||||
{
|
||||
return get_item_order();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the preferred order of the categories when writing them
|
||||
*/
|
||||
std::vector<std::string> get_item_order() const;
|
||||
|
||||
/**
|
||||
* @brief Write out the contents to @a os
|
||||
*/
|
||||
void write(std::ostream &os) const;
|
||||
|
||||
/**
|
||||
* @brief Write out the contents to @a os using the order defined in @a item_name_order
|
||||
*/
|
||||
void write(std::ostream &os, const std::vector<std::string> &item_name_order);
|
||||
|
||||
/**
|
||||
* @brief Friend operator<< to write datablock @a db to std::ostream @a os
|
||||
*/
|
||||
friend std::ostream &operator<<(std::ostream &os, const datablock &db)
|
||||
{
|
||||
db.write(os);
|
||||
return os;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Comparison operator to compare two datablock for equal content
|
||||
*/
|
||||
bool operator==(const datablock &rhs) const;
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
const validator *m_validator = nullptr;
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
@@ -1,17 +1,17 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
@@ -26,32 +26,20 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include "cif++/validate.hpp"
|
||||
|
||||
#include "cif++/Cif++.hpp"
|
||||
/**
|
||||
* @file validate.hpp
|
||||
*
|
||||
* Functions to create and manipulate validator objects
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
extern const int
|
||||
kResidueNrWildcard,
|
||||
kNoSeqNum;
|
||||
|
||||
struct TLSSelection;
|
||||
typedef std::unique_ptr<TLSSelection> TLSSelectionPtr;
|
||||
/**
|
||||
* @brief Parse the contents of @a is and place content in validator @a v
|
||||
*/
|
||||
void parse_dictionary(validator &v, std::istream &is);
|
||||
|
||||
struct TLSResidue;
|
||||
|
||||
struct TLSSelection
|
||||
{
|
||||
virtual ~TLSSelection() {}
|
||||
virtual void CollectResidues(cif::Datablock& db, std::vector<TLSResidue>& residues, std::size_t indentLevel = 0) const = 0;
|
||||
std::vector<std::tuple<std::string,int,int>> GetRanges(cif::Datablock& db, bool pdbNamespace) const;
|
||||
};
|
||||
|
||||
// Low level: get the selections
|
||||
TLSSelectionPtr ParseSelectionDetails(const std::string& program, const std::string& selection);
|
||||
|
||||
}
|
||||
} // namespace cif
|
||||
229
include/cif++/file.hpp
Normal file
229
include/cif++/file.hpp
Normal file
@@ -0,0 +1,229 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
|
||||
#include "cif++/datablock.hpp"
|
||||
#include "cif++/parser.hpp"
|
||||
|
||||
/** \file file.hpp
|
||||
*
|
||||
* The file class defined here encapsulates the contents of an mmCIF file
|
||||
* It is mainly a list of @ref cif::datablock objects
|
||||
*
|
||||
* The class file has methods to load dictionaries. These dictionaries are
|
||||
* loaded from resources (if available) or from disk from several locations.
|
||||
*
|
||||
* See the documentation on load_resource() in file: utilities.hpp for more
|
||||
* information on how data is loaded.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The class file is actually a list of datablock objects
|
||||
*
|
||||
*/
|
||||
|
||||
class file : public std::list<datablock>
|
||||
{
|
||||
public:
|
||||
file() = default;
|
||||
|
||||
/**
|
||||
* @brief Construct a new file object using the data in the file @a p as content
|
||||
*
|
||||
* @param p Path to a file containing the data to load
|
||||
*/
|
||||
explicit file(const std::filesystem::path &p)
|
||||
{
|
||||
load(p);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new file object using the data in the std::istream @a is
|
||||
*
|
||||
* @param is The istream containing the data to load
|
||||
*/
|
||||
explicit file(std::istream &is)
|
||||
{
|
||||
load(is);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new file object with data in the constant string defined
|
||||
* by @a data and @a length
|
||||
*
|
||||
* @param data The pointer to the character string with data to load
|
||||
* @param length The length of the data
|
||||
*/
|
||||
explicit file(const char *data, std::size_t length)
|
||||
{
|
||||
struct membuf : public std::streambuf
|
||||
{
|
||||
membuf(char *text, std::size_t length)
|
||||
{
|
||||
this->setg(text, text, text + length);
|
||||
}
|
||||
} buffer(const_cast<char *>(data), length);
|
||||
|
||||
std::istream is(&buffer);
|
||||
load(is);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
file(const file &rhs)
|
||||
: std::list<datablock>(rhs)
|
||||
{
|
||||
}
|
||||
|
||||
file(file &&rhs)
|
||||
{
|
||||
this->swap(rhs);
|
||||
}
|
||||
|
||||
file &operator=(file f)
|
||||
{
|
||||
this->swap(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief Validate the content and return true if everything was valid.
|
||||
*
|
||||
* Will throw an exception if there is no validator defined.
|
||||
*
|
||||
* If each category was valid, validate_links will also be called.
|
||||
*
|
||||
* @return true If the content is valid
|
||||
* @return false If the content is not valid
|
||||
*/
|
||||
bool is_valid() const;
|
||||
|
||||
/**
|
||||
* @brief Validate the content and return true if everything was valid.
|
||||
*
|
||||
* Will attempt to load the referenced dictionary if none was specified.
|
||||
*
|
||||
* If each category was valid, validate_links will also be called.
|
||||
*
|
||||
* @return true If the content is valid
|
||||
* @return false If the content is not valid
|
||||
*/
|
||||
bool is_valid();
|
||||
|
||||
/**
|
||||
* @brief Validate the links for all datablocks contained.
|
||||
*
|
||||
* Will throw an exception if no validator was specified.
|
||||
*
|
||||
* @return true If all links were valid
|
||||
* @return false If all links were not valid
|
||||
*/
|
||||
bool validate_links() const;
|
||||
|
||||
/**
|
||||
* @brief Return true if a datablock with the name @a name is part of this file
|
||||
*/
|
||||
bool contains(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* @brief return a reference to the first datablock in the file
|
||||
*/
|
||||
datablock &front()
|
||||
{
|
||||
assert(not empty());
|
||||
return std::list<datablock>::front();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief return a const reference to the first datablock in the file
|
||||
*/
|
||||
const datablock &front() const
|
||||
{
|
||||
assert(not empty());
|
||||
return std::list<datablock>::front();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief return a reference to the datablock named @a name
|
||||
*/
|
||||
datablock &operator[](std::string_view name);
|
||||
|
||||
/**
|
||||
* @brief return a const reference to the datablock named @a name
|
||||
*/
|
||||
const datablock &operator[](std::string_view name) const;
|
||||
|
||||
/**
|
||||
* @brief Tries to find a datablock with name @a name and will create a
|
||||
* new one if it is not found. The result is a tuple of an iterator
|
||||
* pointing to the datablock and a boolean indicating whether the datablock
|
||||
* was created or not.
|
||||
*
|
||||
* @param name The name for the datablock
|
||||
* @return std::tuple<iterator, bool> A tuple containing an iterator pointing
|
||||
* at the datablock and a boolean indicating whether the datablock was newly
|
||||
* created.
|
||||
*/
|
||||
std::tuple<iterator, bool> emplace(std::string_view name);
|
||||
|
||||
/** Load the data from the file specified by @a p */
|
||||
void load(const std::filesystem::path &p);
|
||||
|
||||
/** Load the data from @a is */
|
||||
void load(std::istream &is);
|
||||
|
||||
/** Load the data from the file specified by @a p using validator @a v */
|
||||
void load(const std::filesystem::path &p, const validator &v);
|
||||
|
||||
/** Load the data from @a is using validator @a v */
|
||||
void load(std::istream &is, const validator &v);
|
||||
|
||||
/** Save the data to the file specified by @a p */
|
||||
void save(const std::filesystem::path &p) const;
|
||||
|
||||
/** Save the data to @a is */
|
||||
void save(std::ostream &os) const;
|
||||
|
||||
/**
|
||||
* @brief Friend operator<< to write file @a f to std::ostream @a os
|
||||
*/
|
||||
friend std::ostream &operator<<(std::ostream &os, const file &f)
|
||||
{
|
||||
f.save(os);
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
248
include/cif++/format.hpp
Normal file
248
include/cif++/format.hpp
Normal file
@@ -0,0 +1,248 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
/** \file format.hpp
|
||||
*
|
||||
* File containing a basic reimplementation of boost::format
|
||||
* but then a bit more simplistic. Still this allowed me to move my code
|
||||
* from using boost::format to something without external dependency easily.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename T>
|
||||
struct to_varg
|
||||
{
|
||||
using type = T;
|
||||
|
||||
to_varg(const T &v)
|
||||
: m_value(v)
|
||||
{
|
||||
}
|
||||
|
||||
type operator*() { return m_value; }
|
||||
|
||||
T m_value;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct to_varg<const char *>
|
||||
{
|
||||
using type = const char *;
|
||||
|
||||
to_varg(const char *v)
|
||||
: m_value(v)
|
||||
{
|
||||
}
|
||||
|
||||
type operator*() { return m_value.c_str(); }
|
||||
|
||||
std::string m_value;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct to_varg<std::string>
|
||||
{
|
||||
using type = const char *;
|
||||
|
||||
to_varg(const std::string &v)
|
||||
: m_value(v)
|
||||
{
|
||||
}
|
||||
|
||||
type operator*() { return m_value.c_str(); }
|
||||
|
||||
std::string m_value;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
/** @cond */
|
||||
|
||||
template <typename... Args>
|
||||
class format_plus_arg
|
||||
{
|
||||
public:
|
||||
using args_vector_type = std::tuple<detail::to_varg<Args>...>;
|
||||
using vargs_vector_type = std::tuple<typename detail::to_varg<Args>::type...>;
|
||||
|
||||
format_plus_arg(const format_plus_arg &) = delete;
|
||||
format_plus_arg &operator=(const format_plus_arg &) = delete;
|
||||
|
||||
|
||||
format_plus_arg(std::string_view fmt, Args... args)
|
||||
: m_fmt(fmt)
|
||||
, m_args(std::forward<Args>(args)...)
|
||||
{
|
||||
auto ix = std::make_index_sequence<sizeof...(Args)>();
|
||||
copy_vargs(ix);
|
||||
}
|
||||
|
||||
std::string str()
|
||||
{
|
||||
char buffer[1024];
|
||||
std::string::size_type r = std::apply(snprintf, std::tuple_cat(std::make_tuple(buffer, sizeof(buffer), m_fmt.c_str()), m_vargs));
|
||||
return { buffer, r };
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const format_plus_arg &f)
|
||||
{
|
||||
char buffer[1024];
|
||||
std::string::size_type r = std::apply(snprintf, std::tuple_cat(std::make_tuple(buffer, sizeof(buffer), f.m_fmt.c_str()), f.m_vargs));
|
||||
os.write(buffer, r);
|
||||
return os;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
template <std::size_t... I>
|
||||
void copy_vargs(std::index_sequence<I...>)
|
||||
{
|
||||
((std::get<I>(m_vargs) = *std::get<I>(m_args)), ...);
|
||||
}
|
||||
|
||||
std::string m_fmt;
|
||||
args_vector_type m_args;
|
||||
vargs_vector_type m_vargs;
|
||||
};
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief A simplistic reimplementation of boost::format, in fact it is
|
||||
* actually a way to call the C function snprintf to format the arguments
|
||||
* in @a args into the format string @a fmt
|
||||
*
|
||||
* The string in @a fmt should thus be a C style format string.
|
||||
*
|
||||
* TODO: Move to C++23 style of printing.
|
||||
*
|
||||
* @tparam Args The types of the arguments
|
||||
* @param fmt The format string
|
||||
* @param args The arguments
|
||||
* @return An object that can be written out to a std::ostream using operator<<
|
||||
*/
|
||||
|
||||
template <typename... Args>
|
||||
constexpr auto format(std::string_view fmt, Args... args)
|
||||
{
|
||||
return format_plus_arg(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/// A streambuf that fills out lines with spaces up until a specified width
|
||||
|
||||
class fill_out_streambuf : public std::streambuf
|
||||
{
|
||||
public:
|
||||
|
||||
/** @cond */
|
||||
|
||||
using base_type = std::streambuf;
|
||||
using int_type = base_type::int_type;
|
||||
using char_type = base_type::char_type;
|
||||
using traits_type = base_type::traits_type;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief Construct a new fill out streambuf object based on ostream @a os and a
|
||||
* width to fill out to of @a width
|
||||
*/
|
||||
fill_out_streambuf(std::ostream &os, int width = 80)
|
||||
: m_os(os)
|
||||
, m_upstream(os.rdbuf())
|
||||
, m_width(width)
|
||||
{
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
|
||||
~fill_out_streambuf()
|
||||
{
|
||||
m_os.rdbuf(m_upstream);
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief The magic happens here. Write out a couple of spaces when
|
||||
* the last character to write is a newline to make the line as
|
||||
* wide as the requested width.
|
||||
*/
|
||||
|
||||
virtual int_type
|
||||
overflow(int_type ic = traits_type::eof())
|
||||
{
|
||||
char ch = traits_type::to_char_type(ic);
|
||||
|
||||
int_type result = ic;
|
||||
|
||||
if (ch == '\n')
|
||||
{
|
||||
for (int i = m_column_count; result != traits_type::eof() and i < m_width; ++i)
|
||||
result = m_upstream->sputc(' ');
|
||||
}
|
||||
|
||||
if (result != traits_type::eof())
|
||||
result = m_upstream->sputc(ch);
|
||||
|
||||
if (result != traits_type::eof())
|
||||
{
|
||||
if (ch == '\n')
|
||||
{
|
||||
m_column_count = 0;
|
||||
++m_line_count;
|
||||
}
|
||||
else
|
||||
++m_column_count;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Return the upstream streambuf */
|
||||
std::streambuf *get_upstream() const { return m_upstream; }
|
||||
|
||||
/** Return how many lines have been written */
|
||||
int get_line_count() const { return m_line_count; }
|
||||
|
||||
private:
|
||||
std::ostream &m_os;
|
||||
std::streambuf *m_upstream;
|
||||
int m_width;
|
||||
int m_line_count = 0;
|
||||
int m_column_count = 0;
|
||||
};
|
||||
|
||||
} // namespace pdbx
|
||||
@@ -1,17 +1,17 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
@@ -26,14 +26,28 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/Cif++.hpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
void WritePDBFile(std::ostream& pdbFile, cif::File& cifFile);
|
||||
/**
|
||||
* @file forward_decl.hpp
|
||||
*
|
||||
* File containing only forward declarations
|
||||
*
|
||||
*/
|
||||
|
||||
/// \brief Just the HEADER, COMPND, SOURCE and AUTHOR lines
|
||||
void WritePDBHeaderLines(std::ostream& os, cif::File& cifFile);
|
||||
namespace cif
|
||||
{
|
||||
|
||||
std::string GetPDBHEADERLine(cif::File& cifFile, std::string::size_type truncate_at = 127);
|
||||
std::string GetPDBCOMPNDLine(cif::File& cifFile, std::string::size_type truncate_at = 127);
|
||||
std::string GetPDBSOURCELine(cif::File& cifFile, std::string::size_type truncate_at = 127);
|
||||
std::string GetPDBAUTHORLine(cif::File& cifFile, std::string::size_type truncate_at = 127);
|
||||
class category;
|
||||
class datablock;
|
||||
class file;
|
||||
class parser;
|
||||
|
||||
class row;
|
||||
class row_handle;
|
||||
|
||||
class item;
|
||||
struct item_handle;
|
||||
|
||||
} // namespace cif
|
||||
1090
include/cif++/gzio.hpp
Normal file
1090
include/cif++/gzio.hpp
Normal file
File diff suppressed because it is too large
Load Diff
760
include/cif++/item.hpp
Normal file
760
include/cif++/item.hpp
Normal file
@@ -0,0 +1,760 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/exports.hpp"
|
||||
#include "cif++/forward_decl.hpp"
|
||||
#include "cif++/text.hpp"
|
||||
#include "cif++/utilities.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <charconv>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
/** \file item.hpp
|
||||
*
|
||||
* This file contains the declaration of item but also the item_value and item_handle
|
||||
* These handle the storage of and access to the data for a single data item.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/** @brief item is a transient class that is used to pass data into rows
|
||||
* but it also takes care of formatting data.
|
||||
*
|
||||
*
|
||||
*
|
||||
* The class cif::item is often used implicitly when creating a row in a category
|
||||
* using the emplace function.
|
||||
*
|
||||
* @code{.cpp}
|
||||
* cif::category cat("my-cat");
|
||||
* cat.emplace({
|
||||
* { "item-1", 1 }, // <- stores an item with value 1
|
||||
* { "item-2", 1.0, 2 }, // <- stores an item with value 1.00
|
||||
* { "item-3", std::optional<int>() }, // <- stores an item with value ?
|
||||
* { "item-4", std::make_optional<int>(42) }, // <- stores an item with value 42
|
||||
* { "item-5" } // <- stores an item with value .
|
||||
* });
|
||||
*
|
||||
* std::cout << cat << '\n';
|
||||
* @endcode
|
||||
*
|
||||
* Will result in:
|
||||
*
|
||||
* @code{.txt}
|
||||
* _my-cat.item-1 1
|
||||
* _my-cat.item-2 1.00
|
||||
* _my-cat.item-3 ?
|
||||
* _my-cat.item-4 42
|
||||
* _my-cat.item-5 .
|
||||
* @endcode
|
||||
*/
|
||||
class item
|
||||
{
|
||||
public:
|
||||
/// \brief Default constructor, empty item
|
||||
item() = default;
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content the character '.', i.e. an inapplicable value.
|
||||
item(std::string_view name)
|
||||
: m_name(name)
|
||||
, m_value({ '.' })
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content a single character string with content \a value
|
||||
item(std::string_view name, char value)
|
||||
: m_name(name)
|
||||
, m_value({ value })
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content the formatted floating point value \a value with
|
||||
/// precision \a precision
|
||||
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
||||
item(std::string_view name, const T &value, int precision)
|
||||
: m_name(name)
|
||||
{
|
||||
using namespace std;
|
||||
using namespace cif;
|
||||
|
||||
char buffer[32];
|
||||
|
||||
auto r = to_chars(buffer, buffer + sizeof(buffer) - 1, value, chars_format::fixed, precision);
|
||||
if ((bool)r.ec)
|
||||
throw std::runtime_error("Could not format number");
|
||||
|
||||
m_value.assign(buffer, r.ptr - buffer);
|
||||
}
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content a formatted floating point value \a value with
|
||||
/// so-called general formatting
|
||||
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
||||
item(const std::string_view name, const T &value)
|
||||
: m_name(name)
|
||||
{
|
||||
using namespace std;
|
||||
using namespace cif;
|
||||
|
||||
char buffer[32];
|
||||
|
||||
auto r = to_chars(buffer, buffer + sizeof(buffer) - 1, value, chars_format::general);
|
||||
if ((bool)r.ec)
|
||||
throw std::runtime_error("Could not format number");
|
||||
|
||||
m_value.assign(buffer, r.ptr - buffer);
|
||||
}
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content the formatted integral value \a value
|
||||
template <typename T, std::enable_if_t<std::is_integral_v<T> and not std::is_same_v<T, bool>, int> = 0>
|
||||
item(const std::string_view name, const T &value)
|
||||
: m_name(name)
|
||||
{
|
||||
char buffer[32];
|
||||
|
||||
auto r = std::to_chars(buffer, buffer + sizeof(buffer) - 1, value);
|
||||
if ((bool)r.ec)
|
||||
throw std::runtime_error("Could not format number");
|
||||
|
||||
m_value.assign(buffer, r.ptr - buffer);
|
||||
}
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content the formatted boolean value \a value
|
||||
template <typename T, std::enable_if_t<std::is_same_v<T, bool>, int> = 0>
|
||||
item(const std::string_view name, const T &value)
|
||||
: m_name(name)
|
||||
{
|
||||
m_value.assign(value ? "y" : "n");
|
||||
}
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content value \a value
|
||||
item(const std::string_view name, std::string_view value)
|
||||
: m_name(name)
|
||||
, m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content value \a value
|
||||
template<typename T, std::enable_if_t<std::is_same_v<T, std::string>, int> = 0>
|
||||
item(const std::string_view name, T &&value)
|
||||
: m_name(name)
|
||||
, m_value(std::move(value))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content the optional value \a value
|
||||
template <typename T>
|
||||
item(const std::string_view name, const std::optional<T> &value)
|
||||
: m_name(name)
|
||||
{
|
||||
if (value.has_value())
|
||||
{
|
||||
item tmp(name, *value);
|
||||
std::swap(tmp.m_value, m_value);
|
||||
}
|
||||
else
|
||||
m_value.assign("?");
|
||||
}
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content the formatted floating point value \a value with
|
||||
/// precision \a precision
|
||||
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
||||
item(std::string_view name, const std::optional<T> &value, int precision)
|
||||
: m_name(name)
|
||||
{
|
||||
if (value.has_value())
|
||||
{
|
||||
item tmp(name, *value, precision);
|
||||
std::swap(tmp.m_value, m_value);
|
||||
}
|
||||
else
|
||||
m_value.assign("?");
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
item(const item &rhs) = default;
|
||||
item(item &&rhs) noexcept = default;
|
||||
item &operator=(const item &rhs) = default;
|
||||
item &operator=(item &&rhs) noexcept = default;
|
||||
/** @endcond */
|
||||
|
||||
std::string_view name() const { return m_name; } ///< Return the name of the item
|
||||
std::string_view value() const & { return m_value; } ///< Return the value of the item
|
||||
std::string value() const && { return std::move(m_value); } ///< Return the value of the item
|
||||
|
||||
/// \brief replace the content of the stored value with \a v
|
||||
void value(std::string_view v) { m_value = v; }
|
||||
|
||||
/// \brief empty means either null or unknown
|
||||
bool empty() const { return m_value.empty(); }
|
||||
|
||||
/// \brief returns true if the item contains '.'
|
||||
bool is_null() const { return m_value == "."; }
|
||||
|
||||
/// \brief returns true if the item contains '?'
|
||||
bool is_unknown() const { return m_value == "?"; }
|
||||
|
||||
/// \brief the length of the value string
|
||||
std::size_t length() const { return m_value.length(); }
|
||||
|
||||
/// \brief support for structured binding
|
||||
template <std::size_t N>
|
||||
decltype(auto) get() const
|
||||
{
|
||||
if constexpr (N == 0)
|
||||
return name();
|
||||
else if constexpr (N == 1)
|
||||
return value();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string_view m_name;
|
||||
std::string m_value;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/// \brief the internal storage for items in a category
|
||||
///
|
||||
/// Internal storage, strictly forward linked list with minimal space
|
||||
/// requirements. Strings of size 7 or shorter are stored internally.
|
||||
/// Typically, more than 99% of the strings in an mmCIF file are less
|
||||
/// than 8 bytes in length.
|
||||
|
||||
struct item_value
|
||||
{
|
||||
/** @cond */
|
||||
item_value() = default;
|
||||
/** @endcond */
|
||||
|
||||
/// \brief constructor
|
||||
item_value(std::string_view text)
|
||||
: m_length(text.length())
|
||||
, m_storage(0)
|
||||
{
|
||||
if (m_length >= kBufferSize)
|
||||
{
|
||||
m_data = new char[m_length + 1];
|
||||
std::copy(text.begin(), text.end(), m_data);
|
||||
m_data[m_length] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::copy(text.begin(), text.end(), m_local_data);
|
||||
m_local_data[m_length] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
item_value(item_value &&rhs) noexcept
|
||||
: m_length(std::exchange(rhs.m_length, 0))
|
||||
, m_storage(std::exchange(rhs.m_storage, 0))
|
||||
{
|
||||
}
|
||||
|
||||
item_value &operator=(item_value &&rhs) noexcept
|
||||
{
|
||||
std::swap(m_length, rhs.m_length);
|
||||
std::swap(m_storage, rhs.m_storage);
|
||||
return *this;
|
||||
}
|
||||
|
||||
~item_value()
|
||||
{
|
||||
if (m_length >= kBufferSize)
|
||||
delete[] m_data;
|
||||
m_storage = 0;
|
||||
m_length = 0;
|
||||
}
|
||||
|
||||
item_value(const item_value &) = delete;
|
||||
item_value &operator=(const item_value &) = delete;
|
||||
/** @endcond */
|
||||
|
||||
/** operator bool, allows easy checking for empty items */
|
||||
explicit operator bool() const
|
||||
{
|
||||
return m_length != 0;
|
||||
}
|
||||
|
||||
std::size_t m_length = 0; ///< Length of the data
|
||||
union
|
||||
{
|
||||
char m_local_data[8]; ///< Storage area for small strings (strings smaller than kBufferSize)
|
||||
char *m_data; ///< Pointer to a string stored in the heap
|
||||
uint64_t m_storage; ///< Alternative storage of the data, used in move operations
|
||||
};
|
||||
|
||||
/** The maximum length of locally stored strings */
|
||||
static constexpr std::size_t kBufferSize = sizeof(m_local_data);
|
||||
|
||||
// By using std::string_view instead of c_str we obain a
|
||||
// nice performance gain since we avoid many calls to strlen.
|
||||
|
||||
/** Return the content of the item as a std::string_view */
|
||||
constexpr inline std::string_view text() const
|
||||
{
|
||||
return { m_length >= kBufferSize ? m_data : m_local_data, m_length };
|
||||
}
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Transient object to access stored data
|
||||
|
||||
/// \brief This is item_handle, it is used to access the data stored in item_value.
|
||||
|
||||
struct item_handle
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
// conversion helper class
|
||||
template <typename T, typename = void>
|
||||
struct item_value_as;
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief Assign value @a value to the item referenced
|
||||
*
|
||||
* @tparam T Type of the value
|
||||
* @param value The value
|
||||
* @return reference to this item_handle
|
||||
*/
|
||||
template <typename T>
|
||||
item_handle &operator=(const T &value)
|
||||
{
|
||||
assign_value(item{ "", value }.value());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assign value @a value to the item referenced
|
||||
*
|
||||
* @tparam T Type of the value
|
||||
* @param value The value
|
||||
* @return reference to this item_handle
|
||||
*/
|
||||
template <typename T>
|
||||
item_handle &operator=(T &&value)
|
||||
{
|
||||
assign_value(item{ "", std::forward<T>(value) }.value());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assign value @a value to the item referenced
|
||||
*
|
||||
* @tparam T Type of the value
|
||||
* @param value The value
|
||||
* @return reference to this item_handle
|
||||
*/
|
||||
template <std::size_t N>
|
||||
item_handle &operator=(const char (&value)[N])
|
||||
{
|
||||
assign_value(item{ "", std::move(value) }.value());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief A method with a variable number of arguments that will be concatenated and
|
||||
* assigned as a string. Use it like this:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* cif::item_handle ih;
|
||||
* is.os("The result of ", 1, " * ", 42, " is of course ", 42);
|
||||
* @endcode
|
||||
*
|
||||
* And the content will then be `The result of 1 * 42 is of course 42`.
|
||||
*
|
||||
* @tparam Ts Types of the parameters
|
||||
* @param v The parameters to concatenate
|
||||
*/
|
||||
template <typename... Ts>
|
||||
void os(const Ts &...v)
|
||||
{
|
||||
std::ostringstream ss;
|
||||
((ss << v), ...);
|
||||
this->operator=(ss.str());
|
||||
}
|
||||
|
||||
/** Swap contents of this and @a b */
|
||||
void swap(item_handle &b);
|
||||
|
||||
/** Return the contents of this item as type @tparam T */
|
||||
template <typename T = std::string>
|
||||
auto as() const -> T
|
||||
{
|
||||
using value_type = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
return item_value_as<value_type>::convert(*this);
|
||||
}
|
||||
|
||||
/** Return the contents of this item as type @tparam T or, if not
|
||||
* set, use @a dv as the default value.
|
||||
*/
|
||||
template <typename T>
|
||||
auto value_or(const T &dv) const
|
||||
{
|
||||
return empty() ? dv : this->as<T>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compare the contents of this item with value @a value
|
||||
* optionally ignoring character case, if @a icase is true.
|
||||
* Returns 0 if both are equal, -1 if this sorts before @a value
|
||||
* and 1 if this sorts after @a value
|
||||
*
|
||||
* @tparam T Type of the value @a value
|
||||
* @param value The value to compare with
|
||||
* @param icase Flag indicating if we should compare character case sensitive
|
||||
* @return -1, 0 or 1
|
||||
*/
|
||||
template <typename T>
|
||||
int compare(const T &value, bool icase = true) const
|
||||
{
|
||||
return item_value_as<T>::compare(*this, value, icase);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compare the value contained with the value @a value and
|
||||
* return true if both are equal.
|
||||
*/
|
||||
template <typename T>
|
||||
bool operator==(const T &value) const
|
||||
{
|
||||
// TODO: icase or not icase?
|
||||
return item_value_as<T>::compare(*this, value, true) == 0;
|
||||
}
|
||||
|
||||
// We may not have C++20 yet...
|
||||
|
||||
/**
|
||||
* @brief Compare the value contained with the value @a value and
|
||||
* return true if both are not equal.
|
||||
*/
|
||||
template <typename T>
|
||||
bool operator!=(const T &value) const
|
||||
{
|
||||
return not operator==(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if the content string is empty or
|
||||
* only contains '.' meaning null or '?' meaning unknown
|
||||
* in a mmCIF context
|
||||
*/
|
||||
bool empty() const
|
||||
{
|
||||
auto txt = text();
|
||||
return txt.empty() or (txt.length() == 1 and (txt.front() == '.' or txt.front() == '?'));
|
||||
}
|
||||
|
||||
/** Easy way to test for an empty item */
|
||||
explicit operator bool() const { return not empty(); }
|
||||
|
||||
/// is_null return true if the item contains '.'
|
||||
bool is_null() const
|
||||
{
|
||||
auto txt = text();
|
||||
return txt.length() == 1 and txt.front() == '.';
|
||||
}
|
||||
|
||||
/// is_unknown returns true if the item contains '?'
|
||||
bool is_unknown() const
|
||||
{
|
||||
auto txt = text();
|
||||
return txt.length() == 1 and txt.front() == '?';
|
||||
}
|
||||
|
||||
/** Return a std::string_view for the contents */
|
||||
std::string_view text() const;
|
||||
|
||||
/**
|
||||
* @brief Construct a new item handle object
|
||||
*
|
||||
* @param item Item index
|
||||
* @param row Reference to the row
|
||||
*/
|
||||
item_handle(uint16_t item, row_handle &row)
|
||||
: m_item_ix(item)
|
||||
, m_row_handle(row)
|
||||
{
|
||||
}
|
||||
|
||||
/** A variable holding an empty item */
|
||||
CIFPP_EXPORT static const item_handle s_null_item;
|
||||
|
||||
/** friend to swap two item handles */
|
||||
friend void swap(item_handle a, item_handle b)
|
||||
{
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
private:
|
||||
item_handle();
|
||||
|
||||
uint16_t m_item_ix;
|
||||
row_handle &m_row_handle;
|
||||
|
||||
void assign_value(std::string_view value);
|
||||
};
|
||||
|
||||
// So sad that older gcc implementations of from_chars did not support floats yet...
|
||||
|
||||
/** @cond */
|
||||
template <typename T>
|
||||
struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> and not std::is_same_v<T, bool>>>
|
||||
{
|
||||
using value_type = std::remove_reference_t<std::remove_cv_t<T>>;
|
||||
|
||||
static value_type convert(const item_handle &ref)
|
||||
{
|
||||
value_type result = {};
|
||||
|
||||
if (not ref.empty())
|
||||
{
|
||||
auto txt = ref.text();
|
||||
|
||||
auto b = txt.data();
|
||||
auto e = txt.data() + txt.size();
|
||||
|
||||
std::from_chars_result r = (b + 1 < e and *b == '+' and std::isdigit(b[1])) ? selected_charconv<value_type>::from_chars(b + 1, e, result) : selected_charconv<value_type>::from_chars(b, e, result);
|
||||
|
||||
if ((bool)r.ec or r.ptr != e)
|
||||
{
|
||||
result = {};
|
||||
if (cif::VERBOSE)
|
||||
{
|
||||
if (r.ec == std::errc::invalid_argument)
|
||||
std::cerr << "Attempt to convert " << std::quoted(txt) << " into a number\n";
|
||||
else if (r.ec == std::errc::result_out_of_range)
|
||||
std::cerr << "Conversion of " << std::quoted(txt) << " into a type that is too small\n";
|
||||
else
|
||||
std::cerr << "Not a valid number " << std::quoted(txt) << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int compare(const item_handle &ref, const T &value, bool icase)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
auto txt = ref.text();
|
||||
|
||||
if (ref.empty())
|
||||
result = 1;
|
||||
else
|
||||
{
|
||||
value_type v = {};
|
||||
|
||||
auto b = txt.data();
|
||||
auto e = txt.data() + txt.size();
|
||||
|
||||
std::from_chars_result r = (b + 1 < e and *b == '+' and std::isdigit(b[1])) ? selected_charconv<value_type>::from_chars(b + 1, e, v) : selected_charconv<value_type>::from_chars(b, e, v);
|
||||
|
||||
if ((bool)r.ec or r.ptr != e)
|
||||
{
|
||||
if (cif::VERBOSE)
|
||||
{
|
||||
if (r.ec == std::errc::invalid_argument)
|
||||
std::cerr << "Attempt to convert " << std::quoted(txt) << " into a number\n";
|
||||
else if (r.ec == std::errc::result_out_of_range)
|
||||
std::cerr << "Conversion of " << std::quoted(txt) << " into a type that is too small\n";
|
||||
else
|
||||
std::cerr << "Not a valid number " << std::quoted(txt) << '\n';
|
||||
}
|
||||
result = 1;
|
||||
}
|
||||
else if (std::abs(v - value) <= std::numeric_limits<value_type>::epsilon())
|
||||
result = 0;
|
||||
else if (v < value)
|
||||
result = -1;
|
||||
else if (v > value)
|
||||
result = 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct item_handle::item_value_as<std::optional<T>>
|
||||
{
|
||||
static std::optional<T> convert(const item_handle &ref)
|
||||
{
|
||||
std::optional<T> result;
|
||||
if (ref)
|
||||
result = ref.as<T>();
|
||||
return result;
|
||||
}
|
||||
|
||||
static int compare(const item_handle &ref, std::optional<T> value, bool icase)
|
||||
{
|
||||
if (ref.empty() and not value)
|
||||
return 0;
|
||||
|
||||
if (ref.empty())
|
||||
return -1;
|
||||
else if (not value)
|
||||
return 1;
|
||||
else
|
||||
return ref.compare(*value, icase);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct item_handle::item_value_as<T, std::enable_if_t<std::is_same_v<T, bool>>>
|
||||
{
|
||||
static bool convert(const item_handle &ref)
|
||||
{
|
||||
bool result = false;
|
||||
if (not ref.empty())
|
||||
result = iequals(ref.text(), "y");
|
||||
return result;
|
||||
}
|
||||
|
||||
static int compare(const item_handle &ref, bool value, bool icase)
|
||||
{
|
||||
bool rv = convert(ref);
|
||||
return value && rv ? 0
|
||||
: (rv < value ? -1 : 1);
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
struct item_handle::item_value_as<char[N]>
|
||||
{
|
||||
static std::string convert(const item_handle &ref)
|
||||
{
|
||||
if (ref.empty())
|
||||
return {};
|
||||
return { ref.text().data(), ref.text().size() };
|
||||
}
|
||||
|
||||
static int compare(const item_handle &ref, const char (&value)[N], bool icase)
|
||||
{
|
||||
return icase ? cif::icompare(ref.text(), value) : ref.text().compare(value);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct item_handle::item_value_as<T, std::enable_if_t<std::is_same_v<T, const char *>>>
|
||||
{
|
||||
static std::string convert(const item_handle &ref)
|
||||
{
|
||||
if (ref.empty())
|
||||
return {};
|
||||
return { ref.text().data(), ref.text().size() };
|
||||
}
|
||||
|
||||
static int compare(const item_handle &ref, const char *value, bool icase)
|
||||
{
|
||||
return icase ? cif::icompare(ref.text(), value) : ref.text().compare(value);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct item_handle::item_value_as<T, std::enable_if_t<std::is_same_v<T, std::string_view>>>
|
||||
{
|
||||
static std::string convert(const item_handle &ref)
|
||||
{
|
||||
if (ref.empty())
|
||||
return {};
|
||||
return { ref.text().data(), ref.text().size() };
|
||||
}
|
||||
|
||||
static int compare(const item_handle &ref, const std::string_view &value, bool icase)
|
||||
{
|
||||
return icase ? cif::icompare(ref.text(), value) : ref.text().compare(value);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct item_handle::item_value_as<T, std::enable_if_t<std::is_same_v<T, std::string>>>
|
||||
{
|
||||
static std::string convert(const item_handle &ref)
|
||||
{
|
||||
if (ref.empty())
|
||||
return {};
|
||||
return { ref.text().data(), ref.text().size() };
|
||||
}
|
||||
|
||||
static int compare(const item_handle &ref, const std::string &value, bool icase)
|
||||
{
|
||||
return icase ? cif::icompare(ref.text(), value) : ref.text().compare(value);
|
||||
}
|
||||
};
|
||||
|
||||
/** @endcond */
|
||||
|
||||
} // namespace cif
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
||||
/** @cond */
|
||||
|
||||
template <>
|
||||
struct tuple_size<::cif::item>
|
||||
: public std::integral_constant<std::size_t, 2>
|
||||
{
|
||||
};
|
||||
|
||||
template <>
|
||||
struct tuple_element<0, ::cif::item>
|
||||
{
|
||||
using type = decltype(std::declval<::cif::item>().name());
|
||||
};
|
||||
|
||||
template <>
|
||||
struct tuple_element<1, ::cif::item>
|
||||
{
|
||||
using type = decltype(std::declval<::cif::item>().value());
|
||||
};
|
||||
|
||||
/** @endcond */
|
||||
|
||||
} // namespace std
|
||||
739
include/cif++/iterator.hpp
Normal file
739
include/cif++/iterator.hpp
Normal file
@@ -0,0 +1,739 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/row.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
/**
|
||||
* @file iterator.hpp
|
||||
*
|
||||
* This file contains several implementations of generic iterators.
|
||||
*
|
||||
* Using partial specialization we can have implementation for
|
||||
* iterators that return row_handles, a single value or tuples of
|
||||
* multiple values.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Implementation of an iterator that can return
|
||||
* multiple values in a tuple. Of course, that tuple can
|
||||
* then be used in structured binding to receive the values
|
||||
* in a for loop e.g.
|
||||
*
|
||||
* @tparam Category The category for this iterator
|
||||
* @tparam Ts The types this iterator can be dereferenced to
|
||||
*/
|
||||
template <typename Category, typename... Ts>
|
||||
class iterator_impl
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
template <typename, typename...>
|
||||
friend class iterator_impl;
|
||||
|
||||
friend class category;
|
||||
/** @endcond */
|
||||
|
||||
/** variable that contains the number of elements in the tuple */
|
||||
static constexpr std::size_t N = sizeof...(Ts);
|
||||
|
||||
/** @cond */
|
||||
using category_type = std::remove_cv_t<Category>;
|
||||
using row_type = std::conditional_t<std::is_const_v<Category>, const row, row>;
|
||||
|
||||
using tuple_type = std::tuple<Ts...>;
|
||||
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = tuple_type;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = value_type *;
|
||||
using reference = value_type &;
|
||||
|
||||
iterator_impl() = default;
|
||||
|
||||
iterator_impl(const iterator_impl &rhs) = default;
|
||||
iterator_impl(iterator_impl &&rhs) = default;
|
||||
|
||||
template <typename C2, typename... T2s>
|
||||
iterator_impl(const iterator_impl<C2, T2s...> &rhs)
|
||||
: m_current(const_cast<row_handle&>(rhs.m_current))
|
||||
, m_value(rhs.m_value)
|
||||
, m_item_ix(rhs.m_item_ix)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename IRowType>
|
||||
iterator_impl(iterator_impl<IRowType, Ts...> &rhs)
|
||||
: m_current(const_cast<row_handle&>(rhs.m_current))
|
||||
, m_value(rhs.m_value)
|
||||
, m_item_ix(rhs.m_item_ix)
|
||||
{
|
||||
m_value = get(std::make_index_sequence<N>());
|
||||
}
|
||||
|
||||
template <typename IRowType>
|
||||
iterator_impl(const iterator_impl<IRowType> &rhs, const std::array<uint16_t, N> &cix)
|
||||
: m_current(const_cast<row_handle&>(rhs.m_current))
|
||||
, m_item_ix(cix)
|
||||
{
|
||||
m_value = get(std::make_index_sequence<N>());
|
||||
}
|
||||
|
||||
iterator_impl &operator=(iterator_impl i)
|
||||
{
|
||||
std::swap(m_current, i.m_current);
|
||||
std::swap(m_item_ix, i.m_item_ix);
|
||||
std::swap(m_value, i.m_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ~iterator_impl() = default;
|
||||
|
||||
reference operator*()
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
pointer operator->()
|
||||
{
|
||||
return &m_value;
|
||||
}
|
||||
|
||||
operator const row_handle() const
|
||||
{
|
||||
return m_current;
|
||||
}
|
||||
|
||||
operator row_handle()
|
||||
{
|
||||
return m_current;
|
||||
}
|
||||
|
||||
iterator_impl &operator++()
|
||||
{
|
||||
if (m_current)
|
||||
m_current.m_row = m_current.m_row->m_next;
|
||||
|
||||
m_value = get(std::make_index_sequence<N>());
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
iterator_impl operator++(int)
|
||||
{
|
||||
iterator_impl result(*this);
|
||||
this->operator++();
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(const iterator_impl &rhs) const { return m_current == rhs.m_current; }
|
||||
bool operator!=(const iterator_impl &rhs) const { return m_current != rhs.m_current; }
|
||||
|
||||
template <typename IRowType, typename... ITs>
|
||||
bool operator==(const iterator_impl<IRowType, ITs...> &rhs) const
|
||||
{
|
||||
return m_current == rhs.m_current;
|
||||
}
|
||||
|
||||
template <typename IRowType, typename... ITs>
|
||||
bool operator!=(const iterator_impl<IRowType, ITs...> &rhs) const
|
||||
{
|
||||
return m_current != rhs.m_current;
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
private:
|
||||
template <std::size_t... Is>
|
||||
tuple_type get(std::index_sequence<Is...>) const
|
||||
{
|
||||
return m_current ? tuple_type{ m_current[m_item_ix[Is]].template as<Ts>()... } : tuple_type{};
|
||||
}
|
||||
|
||||
row_handle m_current;
|
||||
value_type m_value;
|
||||
std::array<uint16_t, N> m_item_ix;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Implementation of an iterator that returns
|
||||
* only row_handles
|
||||
*
|
||||
* @tparam Category The category for this iterator
|
||||
*/
|
||||
template <typename Category>
|
||||
class iterator_impl<Category>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
|
||||
template <typename, typename...>
|
||||
friend class iterator_impl;
|
||||
|
||||
friend class category;
|
||||
using category_type = std::remove_cv_t<Category>;
|
||||
using row_type = std::conditional_t<std::is_const_v<Category>, const row, row>;
|
||||
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = row_handle;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = value_type *;
|
||||
using reference = value_type &;
|
||||
|
||||
iterator_impl() = default;
|
||||
|
||||
iterator_impl(const iterator_impl &rhs) = default;
|
||||
iterator_impl(iterator_impl &&rhs) = default;
|
||||
|
||||
template <typename C2>
|
||||
iterator_impl(const iterator_impl<C2> &rhs)
|
||||
: m_current(const_cast<row_handle &>(rhs.m_current))
|
||||
{
|
||||
}
|
||||
|
||||
iterator_impl(Category &cat, row *current)
|
||||
: m_current(cat, *current)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename IRowType>
|
||||
iterator_impl(const iterator_impl<IRowType> &rhs, const std::array<uint16_t, 0> &)
|
||||
: m_current(const_cast<row_handle &>(rhs.m_current))
|
||||
{
|
||||
}
|
||||
|
||||
iterator_impl &operator=(iterator_impl i)
|
||||
{
|
||||
std::swap(m_current, i.m_current);
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ~iterator_impl() = default;
|
||||
|
||||
reference operator*()
|
||||
{
|
||||
return m_current;
|
||||
}
|
||||
|
||||
pointer operator->()
|
||||
{
|
||||
return &m_current;
|
||||
}
|
||||
|
||||
operator const row_handle() const
|
||||
{
|
||||
return m_current;
|
||||
}
|
||||
|
||||
operator row_handle()
|
||||
{
|
||||
return m_current;
|
||||
}
|
||||
|
||||
iterator_impl &operator++()
|
||||
{
|
||||
if (m_current)
|
||||
m_current.m_row = m_current.m_row->m_next;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
iterator_impl operator++(int)
|
||||
{
|
||||
iterator_impl result(*this);
|
||||
this->operator++();
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(const iterator_impl &rhs) const { return m_current == rhs.m_current; }
|
||||
bool operator!=(const iterator_impl &rhs) const { return m_current != rhs.m_current; }
|
||||
|
||||
template <typename IRowType, typename... ITs>
|
||||
bool operator==(const iterator_impl<IRowType, ITs...> &rhs) const
|
||||
{
|
||||
return m_current == rhs.m_current;
|
||||
}
|
||||
|
||||
template <typename IRowType, typename... ITs>
|
||||
bool operator!=(const iterator_impl<IRowType, ITs...> &rhs) const
|
||||
{
|
||||
return m_current != rhs.m_current;
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
private:
|
||||
row_handle m_current;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Implementation of an iterator that can return
|
||||
* a single value.
|
||||
*
|
||||
* @tparam Category The category for this iterator
|
||||
* @tparam T The type this iterator can be dereferenced to
|
||||
*/
|
||||
|
||||
template <typename Category, typename T>
|
||||
class iterator_impl<Category, T>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
template <typename, typename...>
|
||||
friend class iterator_impl;
|
||||
|
||||
friend class category;
|
||||
|
||||
using category_type = std::remove_cv_t<Category>;
|
||||
using row_type = std::conditional_t<std::is_const_v<Category>, const row, row>;
|
||||
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = T;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = value_type *;
|
||||
using reference = value_type &;
|
||||
|
||||
iterator_impl() = default;
|
||||
|
||||
iterator_impl(const iterator_impl &rhs) = default;
|
||||
iterator_impl(iterator_impl &&rhs) = default;
|
||||
|
||||
template <typename C2, typename T2>
|
||||
iterator_impl(const iterator_impl<C2, T2> &rhs)
|
||||
: m_current(rhs.m_current)
|
||||
, m_value(rhs.m_value)
|
||||
, m_item_ix(rhs.m_item_ix)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename IRowType>
|
||||
iterator_impl(iterator_impl<IRowType, T> &rhs)
|
||||
: m_current(const_cast<row_handle&>(rhs.m_current))
|
||||
, m_value(rhs.m_value)
|
||||
, m_item_ix(rhs.m_item_ix)
|
||||
{
|
||||
m_value = get();
|
||||
}
|
||||
|
||||
template <typename IRowType>
|
||||
iterator_impl(const iterator_impl<IRowType> &rhs, const std::array<uint16_t, 1> &cix)
|
||||
: m_current(const_cast<row_handle&>(rhs.m_current))
|
||||
, m_item_ix(cix[0])
|
||||
{
|
||||
m_value = get();
|
||||
}
|
||||
|
||||
iterator_impl &operator=(iterator_impl i)
|
||||
{
|
||||
std::swap(m_current, i.m_current);
|
||||
std::swap(m_item_ix, i.m_item_ix);
|
||||
std::swap(m_value, i.m_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ~iterator_impl() = default;
|
||||
|
||||
reference operator*()
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
pointer operator->()
|
||||
{
|
||||
return &m_value;
|
||||
}
|
||||
|
||||
operator const row_handle() const
|
||||
{
|
||||
return m_current;
|
||||
}
|
||||
|
||||
operator row_handle()
|
||||
{
|
||||
return m_current;
|
||||
}
|
||||
|
||||
iterator_impl &operator++()
|
||||
{
|
||||
if (m_current)
|
||||
m_current.m_row = m_current.m_row->m_next;
|
||||
|
||||
m_value = get();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
iterator_impl operator++(int)
|
||||
{
|
||||
iterator_impl result(*this);
|
||||
this->operator++();
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(const iterator_impl &rhs) const { return m_current == rhs.m_current; }
|
||||
bool operator!=(const iterator_impl &rhs) const { return m_current != rhs.m_current; }
|
||||
|
||||
template <typename IRowType, typename... ITs>
|
||||
bool operator==(const iterator_impl<IRowType, ITs...> &rhs) const
|
||||
{
|
||||
return m_current == rhs.m_current;
|
||||
}
|
||||
|
||||
template <typename IRowType, typename... ITs>
|
||||
bool operator!=(const iterator_impl<IRowType, ITs...> &rhs) const
|
||||
{
|
||||
return m_current != rhs.m_current;
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
private:
|
||||
value_type get() const
|
||||
{
|
||||
return m_current ? m_current[m_item_ix].template as<value_type>() : value_type{};
|
||||
}
|
||||
|
||||
row_handle m_current;
|
||||
value_type m_value;
|
||||
uint16_t m_item_ix;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// iterator proxy
|
||||
|
||||
/**
|
||||
* @brief An iterator_proxy is used as a result type for methods that
|
||||
* return a range of values you want to iterate over.
|
||||
*
|
||||
* E.g. the class cif::category contains the method cif::category::rows()
|
||||
* that returns an iterator_proxy that allows you to iterate over
|
||||
* all the rows in the category.
|
||||
*
|
||||
* @tparam Category The category for the iterators
|
||||
* @tparam Ts The types the iterators return. See class: iterator
|
||||
*/
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
class iterator_proxy
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
static constexpr const std::size_t N = sizeof...(Ts);
|
||||
|
||||
using category_type = Category;
|
||||
using row_type = std::conditional_t<std::is_const_v<category_type>, const row, row>;
|
||||
|
||||
using iterator = iterator_impl<category_type, Ts...>;
|
||||
using row_iterator = iterator_impl<category_type>;
|
||||
|
||||
iterator_proxy(category_type &cat, row_iterator pos, char const *const items[N]);
|
||||
iterator_proxy(category_type &cat, row_iterator pos, std::initializer_list<char const *> items);
|
||||
|
||||
iterator_proxy(iterator_proxy &&p);
|
||||
iterator_proxy &operator=(iterator_proxy &&p);
|
||||
|
||||
iterator_proxy(const iterator_proxy &) = delete;
|
||||
iterator_proxy &operator=(const iterator_proxy &) = delete;
|
||||
/** @endcond */
|
||||
|
||||
iterator begin() const { return iterator(m_begin, m_item_ix); } ///< Return the iterator pointing to the first row
|
||||
iterator end() const { return iterator(m_end, m_item_ix); } ///< Return the iterator pointing past the last row
|
||||
|
||||
bool empty() const { return m_begin == m_end; } ///< Return true if the range is empty
|
||||
explicit operator bool() const { return not empty(); } ///< Easy way to detect if the range is empty
|
||||
std::size_t size() const { return std::distance(begin(), end()); } ///< Return size of the range
|
||||
|
||||
// row front() { return *begin(); }
|
||||
// row back() { return *(std::prev(end())); }
|
||||
|
||||
category_type &category() const { return *m_category; } ///< Return the category the iterator belong to
|
||||
|
||||
/** swap */
|
||||
void swap(iterator_proxy &rhs)
|
||||
{
|
||||
std::swap(m_category, rhs.m_category);
|
||||
std::swap(m_begin, rhs.m_begin);
|
||||
std::swap(m_end, rhs.m_end);
|
||||
std::swap(m_item_ix, rhs.m_item_ix);
|
||||
}
|
||||
|
||||
private:
|
||||
category_type *m_category;
|
||||
row_iterator m_begin, m_end;
|
||||
std::array<uint16_t, N> m_item_ix;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// conditional iterator proxy
|
||||
|
||||
/**
|
||||
* @brief A conditional iterator proxy is similar to an iterator_proxy
|
||||
* in that it can be used to return a range of rows you can iterate over.
|
||||
* In the case of an conditional_iterator_proxy a cif::condition is used
|
||||
* to filter out only those rows that match the condition.
|
||||
*
|
||||
* @tparam CategoryType The category the iterators belong to
|
||||
* @tparam Ts The types to which the iterators can be dereferenced
|
||||
*/
|
||||
template <typename CategoryType, typename... Ts>
|
||||
class conditional_iterator_proxy
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
static constexpr const std::size_t N = sizeof...(Ts);
|
||||
|
||||
using category_type = std::remove_cv_t<CategoryType>;
|
||||
|
||||
using base_iterator = iterator_impl<CategoryType, Ts...>;
|
||||
using value_type = typename base_iterator::value_type;
|
||||
using row_type = typename base_iterator::row_type;
|
||||
using row_iterator = iterator_impl<CategoryType>;
|
||||
|
||||
class conditional_iterator_impl
|
||||
{
|
||||
public:
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = conditional_iterator_proxy::value_type;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = value_type *;
|
||||
using reference = value_type;
|
||||
|
||||
conditional_iterator_impl(CategoryType &cat, row_iterator pos, const condition &cond, const std::array<uint16_t, N> &cix);
|
||||
conditional_iterator_impl(const conditional_iterator_impl &i) = default;
|
||||
conditional_iterator_impl &operator=(const conditional_iterator_impl &i) = default;
|
||||
|
||||
virtual ~conditional_iterator_impl() = default;
|
||||
|
||||
reference operator*()
|
||||
{
|
||||
return *m_begin;
|
||||
}
|
||||
|
||||
pointer operator->()
|
||||
{
|
||||
m_current = *m_begin;
|
||||
return &m_current;
|
||||
}
|
||||
|
||||
conditional_iterator_impl &operator++()
|
||||
{
|
||||
while (m_begin != m_end)
|
||||
{
|
||||
if (++m_begin == m_end)
|
||||
break;
|
||||
|
||||
if (m_condition->operator()(m_begin))
|
||||
break;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
conditional_iterator_impl operator++(int)
|
||||
{
|
||||
conditional_iterator_impl result(*this);
|
||||
this->operator++();
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(const conditional_iterator_impl &rhs) const { return m_begin == rhs.m_begin; }
|
||||
bool operator!=(const conditional_iterator_impl &rhs) const { return m_begin != rhs.m_begin; }
|
||||
|
||||
bool operator==(const row_iterator &rhs) const { return m_begin == rhs; }
|
||||
bool operator!=(const row_iterator &rhs) const { return m_begin != rhs; }
|
||||
|
||||
template <typename IRowType, typename... ITs>
|
||||
bool operator==(const iterator_impl<IRowType, ITs...> &rhs) const { return m_begin == rhs; }
|
||||
|
||||
template <typename IRowType, typename... ITs>
|
||||
bool operator!=(const iterator_impl<IRowType, ITs...> &rhs) const { return m_begin != rhs; }
|
||||
|
||||
private:
|
||||
CategoryType *m_cat;
|
||||
base_iterator m_begin, m_end;
|
||||
value_type m_current;
|
||||
const condition *m_condition;
|
||||
};
|
||||
|
||||
using iterator = conditional_iterator_impl;
|
||||
using reference = typename iterator::reference;
|
||||
|
||||
template <typename... Ns>
|
||||
conditional_iterator_proxy(CategoryType &cat, row_iterator pos, condition &&cond, Ns... names);
|
||||
|
||||
conditional_iterator_proxy(conditional_iterator_proxy &&p);
|
||||
conditional_iterator_proxy &operator=(conditional_iterator_proxy &&p);
|
||||
|
||||
conditional_iterator_proxy(const conditional_iterator_proxy &) = delete;
|
||||
conditional_iterator_proxy &operator=(const conditional_iterator_proxy &) = delete;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
iterator begin() const; ///< Return the iterator pointing to the first row
|
||||
iterator end() const; ///< Return the iterator pointing past the last row
|
||||
|
||||
bool empty() const; ///< Return true if the range is empty
|
||||
explicit operator bool() const { return not empty(); } ///< Easy way to detect if the range is empty
|
||||
std::size_t size() const { return std::distance(begin(), end()); } ///< Return size of the range
|
||||
|
||||
row_handle front() { return *begin(); } ///< Return reference to the first row
|
||||
// row_handle back() { return *begin(); }
|
||||
|
||||
CategoryType &category() const { return *m_cat; } ///< Category the iterators belong to
|
||||
|
||||
/** swap */
|
||||
void swap(conditional_iterator_proxy &rhs);
|
||||
|
||||
private:
|
||||
CategoryType *m_cat;
|
||||
condition m_condition;
|
||||
row_iterator mCBegin, mCEnd;
|
||||
std::array<uint16_t, N> mCix;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** @cond */
|
||||
template <typename Category, typename... Ts>
|
||||
iterator_proxy<Category, Ts...>::iterator_proxy(Category &cat, row_iterator pos, char const *const items[N])
|
||||
: m_category(&cat)
|
||||
, m_begin(pos)
|
||||
, m_end(cat.end())
|
||||
{
|
||||
for (uint16_t i = 0; i < N; ++i)
|
||||
m_item_ix[i] = m_category->get_item_ix(items[i]);
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
iterator_proxy<Category, Ts...>::iterator_proxy(Category &cat, row_iterator pos, std::initializer_list<char const *> items)
|
||||
: m_category(&cat)
|
||||
, m_begin(pos)
|
||||
, m_end(cat.end())
|
||||
{
|
||||
// static_assert(items.size() == N, "The list of item names should be exactly the same as the list of requested items");
|
||||
|
||||
std::uint16_t i = 0;
|
||||
for (auto item : items)
|
||||
m_item_ix[i++] = m_category->get_item_ix(item);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
conditional_iterator_proxy<Category, Ts...>::conditional_iterator_impl::conditional_iterator_impl(
|
||||
Category &cat, row_iterator pos, const condition &cond, const std::array<uint16_t, N> &cix)
|
||||
: m_cat(&cat)
|
||||
, m_begin(pos, cix)
|
||||
, m_end(cat.end(), cix)
|
||||
, m_condition(&cond)
|
||||
{
|
||||
if (m_condition == nullptr or m_condition->empty())
|
||||
m_begin = m_end;
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
conditional_iterator_proxy<Category, Ts...>::conditional_iterator_proxy(conditional_iterator_proxy &&p)
|
||||
: m_cat(nullptr)
|
||||
, mCBegin(p.mCBegin)
|
||||
, mCEnd(p.mCEnd)
|
||||
, mCix(p.mCix)
|
||||
{
|
||||
std::swap(m_cat, p.m_cat);
|
||||
std::swap(mCix, p.mCix);
|
||||
m_condition.swap(p.m_condition);
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
template <typename... Ns>
|
||||
conditional_iterator_proxy<Category, Ts...>::conditional_iterator_proxy(Category &cat, row_iterator pos, condition &&cond, Ns... names)
|
||||
: m_cat(&cat)
|
||||
, m_condition(std::move(cond))
|
||||
, mCBegin(pos)
|
||||
, mCEnd(cat.end())
|
||||
{
|
||||
static_assert(sizeof...(Ts) == sizeof...(Ns), "Number of item names should be equal to number of requested value types");
|
||||
|
||||
if (m_condition)
|
||||
{
|
||||
m_condition.prepare(cat);
|
||||
|
||||
while (mCBegin != mCEnd and not m_condition(*mCBegin))
|
||||
++mCBegin;
|
||||
}
|
||||
else
|
||||
mCBegin = mCEnd;
|
||||
|
||||
uint16_t i = 0;
|
||||
((mCix[i++] = m_cat->get_item_ix(names)), ...);
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
conditional_iterator_proxy<Category, Ts...> &conditional_iterator_proxy<Category, Ts...>::operator=(conditional_iterator_proxy &&p)
|
||||
{
|
||||
swap(p);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
typename conditional_iterator_proxy<Category, Ts...>::iterator conditional_iterator_proxy<Category, Ts...>::begin() const
|
||||
{
|
||||
return iterator(*m_cat, mCBegin, m_condition, mCix);
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
typename conditional_iterator_proxy<Category, Ts...>::iterator conditional_iterator_proxy<Category, Ts...>::end() const
|
||||
{
|
||||
return iterator(*m_cat, mCEnd, m_condition, mCix);
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
bool conditional_iterator_proxy<Category, Ts...>::empty() const
|
||||
{
|
||||
return mCBegin == mCEnd;
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
void conditional_iterator_proxy<Category, Ts...>::swap(conditional_iterator_proxy &rhs)
|
||||
{
|
||||
std::swap(m_cat, rhs.m_cat);
|
||||
m_condition.swap(rhs.m_condition);
|
||||
std::swap(mCBegin, rhs.mCBegin);
|
||||
std::swap(mCEnd, rhs.mCEnd);
|
||||
std::swap(mCix, rhs.mCix);
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
} // namespace cif
|
||||
689
include/cif++/matrix.hpp
Normal file
689
include/cif++/matrix.hpp
Normal file
@@ -0,0 +1,689 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2023 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @file matrix.hpp
|
||||
*
|
||||
* Some basic matrix operations and classes to hold matrices.
|
||||
*
|
||||
* We're using expression templates for optimal performance.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
// --------------------------------------------------------------------
|
||||
// We're using expression templates here
|
||||
|
||||
/**
|
||||
* @brief Base for the matrix expression templates
|
||||
* This all uses the Curiously recurring template pattern
|
||||
*
|
||||
* @tparam M The type of the derived class
|
||||
*/
|
||||
template <typename M>
|
||||
class matrix_expression
|
||||
{
|
||||
public:
|
||||
constexpr std::size_t dim_m() const { return static_cast<const M &>(*this).dim_m(); } ///< Return the size (dimension) in direction m
|
||||
constexpr std::size_t dim_n() const { return static_cast<const M &>(*this).dim_n(); } ///< Return the size (dimension) in direction n
|
||||
|
||||
constexpr bool empty() const { return dim_m() == 0 or dim_n() == 0; } ///< Convenient way to test for empty matrices
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr auto &operator()(std::size_t i, std::size_t j)
|
||||
{
|
||||
return static_cast<M &>(*this).operator()(i, j);
|
||||
}
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
return static_cast<const M &>(*this).operator()(i, j);
|
||||
}
|
||||
|
||||
/** Swap the contents of rows @a r1 and @a r2 */
|
||||
void swap_row(std::size_t r1, std::size_t r2)
|
||||
{
|
||||
for (std::size_t c = 0; c < dim_m(); ++c)
|
||||
{
|
||||
auto v = operator()(r1, c);
|
||||
operator()(r1, c) = operator()(r2, c);
|
||||
operator()(r2, c) = v;
|
||||
}
|
||||
}
|
||||
|
||||
/** Swap the contents of columns @a c1 and @a c2 */
|
||||
void swap_col(std::size_t c1, std::size_t c2)
|
||||
{
|
||||
for (std::size_t r = 0; r < dim_n(); ++r)
|
||||
{
|
||||
auto &a = operator()(r, c1);
|
||||
auto &b = operator()(r, c2);
|
||||
std::swap(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
/** write the matrix @a m to std::ostream @a os */
|
||||
friend std::ostream &operator<<(std::ostream &os, const matrix_expression &m)
|
||||
{
|
||||
os << '[';
|
||||
|
||||
for (std::size_t i = 0; i < m.dim_m(); ++i)
|
||||
{
|
||||
os << '[';
|
||||
|
||||
for (std::size_t j = 0; j < m.dim_n(); ++j)
|
||||
{
|
||||
os << m(i, j);
|
||||
if (j + 1 < m.dim_n())
|
||||
os << ", ";
|
||||
}
|
||||
|
||||
if (i + 1 < m.dim_m())
|
||||
os << ", ";
|
||||
|
||||
os << ']';
|
||||
}
|
||||
|
||||
os << ']';
|
||||
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Storage class implementation of matrix_expression.
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
|
||||
template <typename F = float>
|
||||
class matrix : public matrix_expression<matrix<F>>
|
||||
{
|
||||
public:
|
||||
/** The value type */
|
||||
using value_type = F;
|
||||
|
||||
/**
|
||||
* @brief Copy construct a new matrix object using @a m
|
||||
*
|
||||
* @tparam M2 Type of @a m
|
||||
* @param m The matrix expression to copy values from
|
||||
*/
|
||||
template <typename M2>
|
||||
matrix(const matrix_expression<M2> &m)
|
||||
: m_m(m.dim_m())
|
||||
, m_n(m.dim_n())
|
||||
, m_data(m_m * m_n)
|
||||
{
|
||||
for (std::size_t i = 0; i < m_m; ++i)
|
||||
{
|
||||
for (std::size_t j = 0; j < m_n; ++j)
|
||||
operator()(i, j) = m(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new matrix object with dimension @a m and @a n
|
||||
* setting the values to @a v
|
||||
*
|
||||
* @param m Requested dimension M
|
||||
* @param n Requested dimension N
|
||||
* @param v Value to store in each element
|
||||
*/
|
||||
matrix(std::size_t m, std::size_t n, value_type v = 0)
|
||||
: m_m(m)
|
||||
, m_n(n)
|
||||
, m_data(m_m * m_n)
|
||||
{
|
||||
std::fill(m_data.begin(), m_data.end(), v);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
matrix() = default;
|
||||
matrix(matrix &&m) = default;
|
||||
matrix(const matrix &m) = default;
|
||||
matrix &operator=(matrix &&m) = default;
|
||||
matrix &operator=(const matrix &m) = default;
|
||||
/** @endcond */
|
||||
|
||||
constexpr std::size_t dim_m() const { return m_m; } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return m_n; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
assert(i < m_m);
|
||||
assert(j < m_n);
|
||||
return m_data[i * m_n + j];
|
||||
}
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr value_type &operator()(std::size_t i, std::size_t j)
|
||||
{
|
||||
assert(i < m_m);
|
||||
assert(j < m_n);
|
||||
return m_data[i * m_n + j];
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t m_m = 0, m_n = 0;
|
||||
std::vector<value_type> m_data;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// special case, 3x3 matrix
|
||||
|
||||
/**
|
||||
* @brief Storage class implementation of matrix_expression
|
||||
* with compile time fixed size.
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
|
||||
template <typename F, std::size_t M, std::size_t N>
|
||||
class matrix_fixed : public matrix_expression<matrix_fixed<F, M, N>>
|
||||
{
|
||||
public:
|
||||
/** The value type */
|
||||
using value_type = F;
|
||||
|
||||
/** The storage size */
|
||||
static constexpr std::size_t kSize = M * N;
|
||||
|
||||
/** Copy constructor */
|
||||
template <typename M2>
|
||||
matrix_fixed(const M2 &m)
|
||||
{
|
||||
assert(M == m.dim_m() and N == m.dim_n());
|
||||
for (std::size_t i = 0; i < M; ++i)
|
||||
{
|
||||
for (std::size_t j = 0; j < N; ++j)
|
||||
operator()(i, j) = m(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
/** default constructor */
|
||||
matrix_fixed(value_type v = 0)
|
||||
{
|
||||
m_data.fill(v);
|
||||
}
|
||||
|
||||
/** Alternate constructor taking an array of values to store */
|
||||
matrix_fixed(const F (&v)[kSize])
|
||||
{
|
||||
fill(v, std::make_index_sequence<kSize>{});
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
matrix_fixed(matrix_fixed &&m) = default;
|
||||
matrix_fixed(const matrix_fixed &m) = default;
|
||||
matrix_fixed &operator=(matrix_fixed &&m) = default;
|
||||
matrix_fixed &operator=(const matrix_fixed &m) = default;
|
||||
/** @endcond */
|
||||
|
||||
/** Store the values in @a a in the matrix */
|
||||
template<std::size_t... Ixs>
|
||||
matrix_fixed& fill(const F (&a)[kSize], std::index_sequence<Ixs...>)
|
||||
{
|
||||
m_data = { a[Ixs]... };
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr std::size_t dim_m() const { return M; } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return N; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
assert(i < M);
|
||||
assert(j < N);
|
||||
return m_data[i * N + j];
|
||||
}
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr value_type &operator()(std::size_t i, std::size_t j)
|
||||
{
|
||||
assert(i < M);
|
||||
assert(j < N);
|
||||
return m_data[i * N + j];
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<value_type, M * N> m_data;
|
||||
};
|
||||
|
||||
/** typedef of a fixed matrix of size 3x3 */
|
||||
template <typename F>
|
||||
using matrix3x3 = matrix_fixed<F, 3, 3>;
|
||||
|
||||
/** typedef of a fixed matrix of size 4x4 */
|
||||
template <typename F>
|
||||
using matrix4x4 = matrix_fixed<F, 4, 4>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Storage class implementation of symmetric matrix_expression
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
template <typename F = float>
|
||||
class symmetric_matrix : public matrix_expression<symmetric_matrix<F>>
|
||||
{
|
||||
public:
|
||||
/** The value type */
|
||||
using value_type = F;
|
||||
|
||||
/** constructor for a matrix of size @a n x @a n elements with value @a v */
|
||||
symmetric_matrix(std::size_t n, value_type v = 0)
|
||||
: m_n(n)
|
||||
, m_data((m_n * (m_n + 1)) / 2)
|
||||
{
|
||||
std::fill(m_data.begin(), m_data.end(), v);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
symmetric_matrix() = default;
|
||||
symmetric_matrix(symmetric_matrix &&m) = default;
|
||||
symmetric_matrix(const symmetric_matrix &m) = default;
|
||||
symmetric_matrix &operator=(symmetric_matrix &&m) = default;
|
||||
symmetric_matrix &operator=(const symmetric_matrix &m) = default;
|
||||
/** @endcond */
|
||||
|
||||
constexpr std::size_t dim_m() const { return m_n; } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return m_n; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
return i < j
|
||||
? m_data[(j * (j + 1)) / 2 + i]
|
||||
: m_data[(i * (i + 1)) / 2 + j];
|
||||
}
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr value_type &operator()(std::size_t i, std::size_t j)
|
||||
{
|
||||
if (i > j)
|
||||
std::swap(i, j);
|
||||
assert(j < m_n);
|
||||
return m_data[(j * (j + 1)) / 2 + i];
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t m_n;
|
||||
std::vector<value_type> m_data;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Storage class implementation of symmetric matrix_expression
|
||||
* with compile time fixed size.
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
template <typename F, std::size_t M>
|
||||
class symmetric_matrix_fixed : public matrix_expression<symmetric_matrix_fixed<F, M>>
|
||||
{
|
||||
public:
|
||||
/** The value type */
|
||||
using value_type = F;
|
||||
|
||||
/** constructor with all elements set to value @a v */
|
||||
symmetric_matrix_fixed(value_type v = 0)
|
||||
{
|
||||
std::fill(m_data.begin(), m_data.end(), v);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
symmetric_matrix_fixed(symmetric_matrix_fixed &&m) = default;
|
||||
symmetric_matrix_fixed(const symmetric_matrix_fixed &m) = default;
|
||||
symmetric_matrix_fixed &operator=(symmetric_matrix_fixed &&m) = default;
|
||||
symmetric_matrix_fixed &operator=(const symmetric_matrix_fixed &m) = default;
|
||||
/** @endcond */
|
||||
|
||||
constexpr std::size_t dim_m() const { return M; } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return M; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
return i < j
|
||||
? m_data[(j * (j + 1)) / 2 + i]
|
||||
: m_data[(i * (i + 1)) / 2 + j];
|
||||
}
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr value_type &operator()(std::size_t i, std::size_t j)
|
||||
{
|
||||
if (i > j)
|
||||
std::swap(i, j);
|
||||
assert(j < M);
|
||||
return m_data[(j * (j + 1)) / 2 + i];
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<value_type, (M * (M + 1)) / 2> m_data;
|
||||
};
|
||||
|
||||
/** typedef of a fixed symmetric matrix of size 3x3 */
|
||||
template <typename F>
|
||||
using symmetric_matrix3x3 = symmetric_matrix_fixed<F, 3>;
|
||||
|
||||
/** typedef of a fixed symmetric matrix of size 4x4 */
|
||||
template <typename F>
|
||||
using symmetric_matrix4x4 = symmetric_matrix_fixed<F, 4>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief implementation of symmetric matrix_expression with a value
|
||||
* of 1 for the diagonal values and 0 for all the others.
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
template <typename F = float>
|
||||
class identity_matrix : public matrix_expression<identity_matrix<F>>
|
||||
{
|
||||
public:
|
||||
/** the value type */
|
||||
using value_type = F;
|
||||
|
||||
/** constructor taking a dimension @a n */
|
||||
identity_matrix(std::size_t n)
|
||||
: m_n(n)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr std::size_t dim_m() const { return m_n; } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return m_n; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
return static_cast<value_type>(i == j ? 1 : 0);
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t m_n;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// matrix functions, implemented as expression templates
|
||||
|
||||
/**
|
||||
* @brief Implementation of a substraction operation as a matrix expression
|
||||
*
|
||||
* @tparam M1 Type of matrix 1
|
||||
* @tparam M2 Type of matrix 2
|
||||
*/
|
||||
template <typename M1, typename M2>
|
||||
class matrix_subtraction : public matrix_expression<matrix_subtraction<M1, M2>>
|
||||
{
|
||||
public:
|
||||
/** constructor */
|
||||
matrix_subtraction(const M1 &m1, const M2 &m2)
|
||||
: m_m1(m1)
|
||||
, m_m2(m2)
|
||||
{
|
||||
assert(m_m1.dim_m() == m_m2.dim_m());
|
||||
assert(m_m1.dim_n() == m_m2.dim_n());
|
||||
}
|
||||
|
||||
constexpr std::size_t dim_m() const { return m_m1.dim_m(); } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return m_m1.dim_n(); } ///< Return dimension n
|
||||
|
||||
/** Access to the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
return m_m1(i, j) - m_m2(i, j);
|
||||
}
|
||||
|
||||
private:
|
||||
const M1 &m_m1;
|
||||
const M2 &m_m2;
|
||||
};
|
||||
|
||||
/** operator to subtract two matrices and return a matrix expression */
|
||||
template <typename M1, typename M2>
|
||||
auto operator-(const matrix_expression<M1> &m1, const matrix_expression<M2> &m2)
|
||||
{
|
||||
return matrix_subtraction(m1, m2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Implementation of a multiplication operation as a matrix expression
|
||||
*
|
||||
* @tparam M1 Type of matrix 1
|
||||
* @tparam M2 Type of matrix 2
|
||||
*/
|
||||
template <typename M1, typename M2>
|
||||
class matrix_matrix_multiplication : public matrix_expression<matrix_matrix_multiplication<M1, M2>>
|
||||
{
|
||||
public:
|
||||
/** constructor */
|
||||
matrix_matrix_multiplication(const M1 &m1, const M2 &m2)
|
||||
: m_m1(m1)
|
||||
, m_m2(m2)
|
||||
{
|
||||
assert(m1.dim_m() == m2.dim_n());
|
||||
}
|
||||
|
||||
constexpr std::size_t dim_m() const { return m_m1.dim_m(); } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return m_m1.dim_n(); } ///< Return dimension n
|
||||
|
||||
/** Access to the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
using value_type = decltype(m_m1(0, 0));
|
||||
|
||||
value_type result = {};
|
||||
|
||||
for (std::size_t k = 0; k < m_m1.dim_m(); ++k)
|
||||
result += m_m1(i, k) * m_m2(k, j);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
const M1 &m_m1;
|
||||
const M2 &m_m2;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Implementation of a multiplication operation of a matrix and a scalar value as a matrix expression
|
||||
*
|
||||
* @tparam M1 Type of matrix
|
||||
* @tparam M2 Type of scalar value
|
||||
*/
|
||||
template <typename M, typename T>
|
||||
class matrix_scalar_multiplication : public matrix_expression<matrix_scalar_multiplication<M, T>>
|
||||
{
|
||||
public:
|
||||
/** value type */
|
||||
using value_type = T;
|
||||
|
||||
/** constructor */
|
||||
matrix_scalar_multiplication(const M &m, value_type v)
|
||||
: m_m(m)
|
||||
, m_v(v)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr std::size_t dim_m() const { return m_m.dim_m(); } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return m_m.dim_n(); } ///< Return dimension n
|
||||
|
||||
/** Access to the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
return m_m(i, j) * m_v;
|
||||
}
|
||||
|
||||
private:
|
||||
const M &m_m;
|
||||
value_type m_v;
|
||||
};
|
||||
|
||||
/** First implementation of operator*, enabled if the second parameter is a scalar */
|
||||
template <typename M1, typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
||||
auto operator*(const matrix_expression<M1> &m, T v)
|
||||
{
|
||||
return matrix_scalar_multiplication(m, v);
|
||||
}
|
||||
|
||||
/** First implementation of operator*, enabled if the second parameter is not a scalar and thus must be a matrix, right? */
|
||||
template <typename M1, typename M2, std::enable_if_t<not std::is_floating_point_v<M2>, int> = 0>
|
||||
auto operator*(const matrix_expression<M1> &m1, const matrix_expression<M2> &m2)
|
||||
{
|
||||
return matrix_matrix_multiplication(m1, m2);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** Generic routine to calculate the determinant of a matrix
|
||||
*
|
||||
* @note This is currently only implemented for fixed matrices of size 3x3
|
||||
*/
|
||||
template <typename M>
|
||||
auto determinant(const M &m);
|
||||
|
||||
/** Implementation of the determinant function for fixed size matrices of size 3x3 */
|
||||
template <typename F = float>
|
||||
auto determinant(const matrix3x3<F> &m)
|
||||
{
|
||||
return (m(0, 0) * (m(1, 1) * m(2, 2) - m(1, 2) * m(2, 1)) +
|
||||
m(0, 1) * (m(1, 2) * m(2, 0) - m(1, 0) * m(2, 2)) +
|
||||
m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0)));
|
||||
}
|
||||
|
||||
/** Generic routine to calculate the inverse of a matrix
|
||||
*
|
||||
* @note This is currently only implemented for fixed matrices of size 3x3
|
||||
*/
|
||||
template <typename M>
|
||||
M inverse(const M &m);
|
||||
|
||||
/** Implementation of the inverse function for fixed size matrices of size 3x3 */
|
||||
template <typename F = float>
|
||||
matrix3x3<F> inverse(const matrix3x3<F> &m)
|
||||
{
|
||||
F det = determinant(m);
|
||||
|
||||
matrix3x3<F> result;
|
||||
|
||||
result(0, 0) = (m(1, 1) * m(2, 2) - m(1, 2) * m(2, 1)) / det;
|
||||
result(1, 0) = (m(1, 2) * m(2, 0) - m(1, 0) * m(2, 2)) / det;
|
||||
result(2, 0) = (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0)) / det;
|
||||
result(0, 1) = (m(2, 1) * m(0, 2) - m(2, 2) * m(0, 1)) / det;
|
||||
result(1, 1) = (m(2, 2) * m(0, 0) - m(2, 0) * m(0, 2)) / det;
|
||||
result(2, 1) = (m(2, 0) * m(0, 1) - m(2, 1) * m(0, 0)) / det;
|
||||
result(0, 2) = (m(0, 1) * m(1, 2) - m(0, 2) * m(1, 1)) / det;
|
||||
result(1, 2) = (m(0, 2) * m(1, 0) - m(0, 0) * m(1, 2)) / det;
|
||||
result(2, 2) = (m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0)) / det;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Implementation of a cofactor calculation as a matrix expression
|
||||
*
|
||||
* @tparam M Type of matrix
|
||||
*/
|
||||
template <typename M>
|
||||
class matrix_cofactors : public matrix_expression<matrix_cofactors<M>>
|
||||
{
|
||||
public:
|
||||
/** constructor */
|
||||
matrix_cofactors(const M &m)
|
||||
: m_m(m)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr std::size_t dim_m() const { return m_m.dim_m(); } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return m_m.dim_n(); } ///< Return dimension n
|
||||
|
||||
/** Access to the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
const std::size_t ixs[4][3] = {
|
||||
{ 1, 2, 3 },
|
||||
{ 0, 2, 3 },
|
||||
{ 0, 1, 3 },
|
||||
{ 0, 1, 2 }
|
||||
};
|
||||
|
||||
const std::size_t *ix = ixs[i];
|
||||
const std::size_t *iy = ixs[j];
|
||||
|
||||
auto result =
|
||||
m_m(ix[0], iy[0]) * m_m(ix[1], iy[1]) * m_m(ix[2], iy[2]) +
|
||||
m_m(ix[0], iy[1]) * m_m(ix[1], iy[2]) * m_m(ix[2], iy[0]) +
|
||||
m_m(ix[0], iy[2]) * m_m(ix[1], iy[0]) * m_m(ix[2], iy[1]) -
|
||||
m_m(ix[0], iy[2]) * m_m(ix[1], iy[1]) * m_m(ix[2], iy[0]) -
|
||||
m_m(ix[0], iy[1]) * m_m(ix[1], iy[0]) * m_m(ix[2], iy[2]) -
|
||||
m_m(ix[0], iy[0]) * m_m(ix[1], iy[2]) * m_m(ix[2], iy[1]);
|
||||
|
||||
return (i + j) % 2 == 1 ? -result : result;
|
||||
}
|
||||
|
||||
private:
|
||||
const M &m_m;
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
1158
include/cif++/model.hpp
Normal file
1158
include/cif++/model.hpp
Normal file
File diff suppressed because it is too large
Load Diff
346
include/cif++/parser.hpp
Normal file
346
include/cif++/parser.hpp
Normal file
@@ -0,0 +1,346 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/row.hpp"
|
||||
|
||||
#include <map>
|
||||
|
||||
/**
|
||||
* @file parser.hpp
|
||||
*
|
||||
* This file contains the declaration of an mmCIF parser
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
class validator;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** Exception that is thrown when the mmCIF file contains a parsing error */
|
||||
class parse_error : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
/// \brief constructor
|
||||
parse_error(uint32_t line_nr, const std::string &message)
|
||||
: std::runtime_error("parse error at line " + std::to_string(line_nr) + ": " + message)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The sac_parser is a similar to SAX parsers (Simple API for XML,
|
||||
* in our case it is Simple API for CIF)
|
||||
*
|
||||
* This is a hand crafted, optimised parser for reading cif files,
|
||||
* both cif 1.0 and cif 1.1 is supported. But version 2.0 is not.
|
||||
* That means that the content of files strictly contains only
|
||||
* ASCII characters. Anything else will generate an error.
|
||||
*
|
||||
* This class is an abstract base class. Derived classes should
|
||||
* implement the produce_ methods.
|
||||
*/
|
||||
|
||||
// TODO: Need to implement support for transformed long lines
|
||||
|
||||
class sac_parser
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
struct iless_op
|
||||
{
|
||||
bool operator()(std::string_view a, std::string_view b) const
|
||||
{
|
||||
return icompare(a, b) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
using datablock_index = std::map<std::string, std::size_t, iless_op>;
|
||||
|
||||
virtual ~sac_parser() = default;
|
||||
/** @endcond */
|
||||
|
||||
/// \brief The parser only supports ASCII so we can
|
||||
/// create a table with character properties.
|
||||
enum CharTraitsMask : uint8_t
|
||||
{
|
||||
kOrdinaryMask = 1 << 0, ///< The character is in the Ordinary class
|
||||
kNonBlankMask = 1 << 1, ///< The character is in the NonBlank class
|
||||
kTextLeadMask = 1 << 2, ///< The character is in the TextLead class
|
||||
kAnyPrintMask = 1 << 3 ///< The character is in the AnyPrint class
|
||||
};
|
||||
|
||||
/// \brief Return true if the character @a ch is a *space* character
|
||||
static constexpr bool is_space(int ch)
|
||||
{
|
||||
return ch == ' ' or ch == '\t' or ch == '\r' or ch == '\n';
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *white* character
|
||||
static constexpr bool is_white(int ch)
|
||||
{
|
||||
return is_space(ch) or ch == '#';
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *ordinary* character
|
||||
static constexpr bool is_ordinary(int ch)
|
||||
{
|
||||
return ch >= 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kOrdinaryMask) != 0;
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *non_blank* character
|
||||
static constexpr bool is_non_blank(int ch)
|
||||
{
|
||||
return ch > 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kNonBlankMask) != 0;
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *text_lead* character
|
||||
static constexpr bool is_text_lead(int ch)
|
||||
{
|
||||
return ch >= 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kTextLeadMask) != 0;
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *any_print* character
|
||||
static constexpr bool is_any_print(int ch)
|
||||
{
|
||||
return ch == '\t' or
|
||||
(ch >= 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kAnyPrintMask) != 0);
|
||||
}
|
||||
|
||||
/// \brief Return true if the string in @a text can safely be written without quotation
|
||||
static bool is_unquoted_string(std::string_view text);
|
||||
|
||||
protected:
|
||||
/** @cond */
|
||||
|
||||
static constexpr uint8_t kCharTraitsTable[128] = {
|
||||
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
|
||||
14, 15, 14, 14, 14, 15, 15, 14, 15, 15, 15, 15, 15, 15, 15, 15, // 2
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 10, 15, 15, 15, 15, // 3
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, // 4
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 14, 15, 14, 15, 14, // 5
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, // 6
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, // 7
|
||||
};
|
||||
|
||||
enum class CIFToken
|
||||
{
|
||||
UNKNOWN,
|
||||
|
||||
END_OF_FILE,
|
||||
|
||||
DATA,
|
||||
LOOP,
|
||||
GLOBAL,
|
||||
SAVE_,
|
||||
SAVE_NAME,
|
||||
STOP,
|
||||
ITEM_NAME,
|
||||
VALUE
|
||||
};
|
||||
|
||||
static constexpr const char *get_token_name(CIFToken token)
|
||||
{
|
||||
switch (token)
|
||||
{
|
||||
case CIFToken::UNKNOWN: return "Unknown";
|
||||
case CIFToken::END_OF_FILE: return "Eof";
|
||||
case CIFToken::DATA: return "DATA";
|
||||
case CIFToken::LOOP: return "LOOP";
|
||||
case CIFToken::GLOBAL: return "GLOBAL";
|
||||
case CIFToken::SAVE_: return "SAVE";
|
||||
case CIFToken::SAVE_NAME: return "SAVE+name";
|
||||
case CIFToken::STOP: return "STOP";
|
||||
case CIFToken::ITEM_NAME: return "Tag";
|
||||
case CIFToken::VALUE: return "Value";
|
||||
default: return "Invalid token parameter";
|
||||
}
|
||||
}
|
||||
|
||||
// get_next_char takes the next character from the istream.
|
||||
// This function also does carriage/linefeed translation.
|
||||
int get_next_char();
|
||||
|
||||
// Put the last read character back into the istream
|
||||
void retract();
|
||||
|
||||
CIFToken get_next_token();
|
||||
|
||||
void match(CIFToken token);
|
||||
|
||||
/** @endcond */
|
||||
|
||||
public:
|
||||
|
||||
/** \brief Parse only a single datablock in the string @a datablock
|
||||
* The start of the datablock is first located and then data
|
||||
* is parsed up until the next start of a datablock or the end of
|
||||
* the data.
|
||||
* */
|
||||
bool parse_single_datablock(const std::string &datablock);
|
||||
|
||||
/** \brief Return an index for all the datablocks found, that is
|
||||
* the index will contain the names and offsets for each.
|
||||
*/
|
||||
datablock_index index_datablocks();
|
||||
|
||||
/**
|
||||
* @brief Parse the datablock named @a datablock
|
||||
*
|
||||
* This will first lookup the datablock's offset in the index @a index
|
||||
* and then start parsing from that location until the next datablock.
|
||||
*
|
||||
* @param datablock Name of the datablock to parse
|
||||
* @param index The index created using index_datablocks
|
||||
* @return true If the datablock was found
|
||||
* @return false If the datablock was not found
|
||||
*/
|
||||
bool parse_single_datablock(const std::string &datablock, const datablock_index &index);
|
||||
|
||||
/**
|
||||
* @brief Parse the file
|
||||
*
|
||||
*/
|
||||
void parse_file();
|
||||
|
||||
protected:
|
||||
|
||||
/** @cond */
|
||||
|
||||
sac_parser(std::istream &is, bool init = true);
|
||||
|
||||
void parse_global();
|
||||
|
||||
void parse_datablock();
|
||||
|
||||
virtual void parse_save_frame();
|
||||
|
||||
void error(const std::string &msg)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Error parsing mmCIF: " << msg << '\n';
|
||||
|
||||
throw parse_error(m_line_nr, msg);
|
||||
}
|
||||
|
||||
void warning(const std::string &msg)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "parser warning at line " << m_line_nr << ": " << msg << '\n';
|
||||
}
|
||||
|
||||
// production methods, these are pure virtual here
|
||||
|
||||
virtual void produce_datablock(std::string_view name) = 0;
|
||||
virtual void produce_category(std::string_view name) = 0;
|
||||
virtual void produce_row() = 0;
|
||||
virtual void produce_item(std::string_view category, std::string_view item, std::string_view value) = 0;
|
||||
|
||||
protected:
|
||||
|
||||
enum class State
|
||||
{
|
||||
Start,
|
||||
White,
|
||||
Esc,
|
||||
Comment,
|
||||
QuestionMark,
|
||||
Dot,
|
||||
QuotedString,
|
||||
QuotedStringQuote,
|
||||
UnquotedString,
|
||||
ItemName,
|
||||
TextItem,
|
||||
TextItemNL,
|
||||
Reserved,
|
||||
Value
|
||||
};
|
||||
|
||||
std::streambuf &m_source;
|
||||
|
||||
// Parser state
|
||||
uint32_t m_line_nr;
|
||||
bool m_bol;
|
||||
CIFToken m_lookahead;
|
||||
|
||||
// token buffer
|
||||
std::vector<char> m_token_buffer;
|
||||
std::string_view m_token_value;
|
||||
|
||||
/** @endcond */
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief An actual implementation of a sac_parser generating data in a file
|
||||
*
|
||||
* This parser will create the cif::file, cif::datablock and cif::category
|
||||
* objects required to contain all data
|
||||
*/
|
||||
class parser : public sac_parser
|
||||
{
|
||||
public:
|
||||
/// \brief constructor, generates data into @a file from @a is using validator @a v
|
||||
parser(std::istream &is, file &file, const validator *v)
|
||||
: sac_parser(is)
|
||||
, m_file(file)
|
||||
, m_validator(v)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor, generates data into @a file from @a is
|
||||
parser(std::istream &is, file &file)
|
||||
: sac_parser(is)
|
||||
, m_file(file)
|
||||
{
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
void produce_datablock(std::string_view name) override;
|
||||
|
||||
void produce_category(std::string_view name) override;
|
||||
|
||||
void produce_row() override;
|
||||
|
||||
void produce_item(std::string_view category, std::string_view item, std::string_view value) override;
|
||||
|
||||
protected:
|
||||
file &m_file;
|
||||
datablock *m_datablock = nullptr;
|
||||
category *m_category = nullptr;
|
||||
const validator *m_validator = nullptr;
|
||||
row_handle m_row;
|
||||
|
||||
/** @endcond */
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
251
include/cif++/pdb.hpp
Normal file
251
include/cif++/pdb.hpp
Normal file
@@ -0,0 +1,251 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2023 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/file.hpp"
|
||||
#include "cif++/validate.hpp"
|
||||
|
||||
#include <system_error>
|
||||
|
||||
/**
|
||||
* @file pdb.hpp
|
||||
*
|
||||
* This file presents the API to read and write files in the
|
||||
* legacy and ancient PDB format.
|
||||
*
|
||||
* The code works on the basis of best effort since it is
|
||||
* impossible to have correct round trip fidelity.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif::pdb
|
||||
{
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
// PDB to mmCIF
|
||||
|
||||
/** @brief Read a file in either mmCIF or PDB format from file @a file,
|
||||
* compressed or not, depending on the content.
|
||||
*/
|
||||
|
||||
file read(const std::filesystem::path &file);
|
||||
|
||||
/** @brief Read a file in either mmCIF or PDB format from std::istream @a is,
|
||||
* compressed or not, depending on the content.
|
||||
*/
|
||||
|
||||
file read(std::istream &is);
|
||||
|
||||
/**
|
||||
* @brief Read a file in legacy PDB format from std::istream @a is and
|
||||
* put the data into @a cifFile
|
||||
*/
|
||||
file read_pdb_file(std::istream &pdbFile);
|
||||
|
||||
// mmCIF to PDB
|
||||
|
||||
/** @brief Write out the data in @a db in legacy PDB format
|
||||
* to std::ostream @a os
|
||||
*/
|
||||
void write(std::ostream &os, const datablock &db);
|
||||
|
||||
/** @brief Write out the data in @a f in legacy PDB format
|
||||
* to std::ostream @a os
|
||||
*/
|
||||
inline void write(std::ostream &os, const file &f)
|
||||
{
|
||||
write(os, f.front());
|
||||
}
|
||||
|
||||
/** @brief Write out the data in @a db to file @a file
|
||||
* in legacy PDB format or mmCIF format, depending on the
|
||||
* filename extension.
|
||||
*
|
||||
* If extension of @a file is *.gz* the resulting file will
|
||||
* be written in gzip compressed format.
|
||||
*/
|
||||
void write(const std::filesystem::path &file, const datablock &db);
|
||||
|
||||
/** @brief Write out the data in @a f to file @a file
|
||||
* in legacy PDB format or mmCIF format, depending on the
|
||||
* filename extension.
|
||||
*
|
||||
* If extension of @a file is *.gz* the resulting file will
|
||||
* be written in gzip compressed format.
|
||||
*/
|
||||
inline void write(const std::filesystem::path &p, const file &f)
|
||||
{
|
||||
write(p, f.front());
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Quickly fix a PDB file that lacks some often needed categories
|
||||
*
|
||||
* This differs from reconstruct_pdbx which does a much more thorough job
|
||||
*
|
||||
* \param pdbx_file The cif::file that hopefully contains some valid data
|
||||
*/
|
||||
|
||||
void fixup_pdbx(file &pdbx_file);
|
||||
|
||||
/**
|
||||
* @brief Quickly fix a PDB file that lacks some often needed categories
|
||||
*
|
||||
* This differs from reconstruct_pdbx which does a much more thorough job
|
||||
*
|
||||
* \param pdbx_file The cif::file that hopefully contains some valid data
|
||||
* \param v The validator to use
|
||||
*/
|
||||
|
||||
void fixup_pdbx(file &pdbx_file, const validator &v);
|
||||
|
||||
/** \brief Reconstruct all missing categories for an assumed PDBx file.
|
||||
*
|
||||
* Some people believe that simply dumping some atom records is enough.
|
||||
*
|
||||
* This version uses the audit_conform information and falls back to
|
||||
* using mmcif_pdbx.dic if not specified.
|
||||
*
|
||||
* \param pdbx_file The cif::file that hopefully contains some valid data
|
||||
* \result Returns true if the resulting file is valid
|
||||
*/
|
||||
|
||||
bool reconstruct_pdbx(file &pdbx_file);
|
||||
|
||||
/** \brief Reconstruct all missing categories for an assumed PDBx file.
|
||||
*
|
||||
* Some people believe that simply dumping some atom records is enough.
|
||||
*
|
||||
* \param pdbx_file The cif::file that hopefully contains some valid data
|
||||
* \param v The validator to use
|
||||
* \result Returns true if the resulting file is valid
|
||||
*/
|
||||
|
||||
bool reconstruct_pdbx(file &pdbx_file, const validator &v);
|
||||
|
||||
/** \brief This is an extension to cif::validator, use the logic in common
|
||||
* PDBx files to see if the file is internally consistent.
|
||||
*
|
||||
* This function for now checks if the following categories are consistent:
|
||||
*
|
||||
* atom_site -> pdbx_poly_seq_scheme -> entity_poly_seq -> entity_poly -> entity
|
||||
*
|
||||
* Use the common \ref cif::VERBOSE flag to turn on diagnostic messages.
|
||||
*
|
||||
* This function throws a std::system_error in case of an error
|
||||
*
|
||||
* \param pdbx_file The input file
|
||||
* \param v The validator to use
|
||||
* \result Returns true if the file was valid and consistent
|
||||
*/
|
||||
|
||||
bool is_valid_pdbx_file(const file &pdbx_file,
|
||||
const validator &v = validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
|
||||
/** \brief This is an extension to cif::validator, use the logic in common
|
||||
* PDBx files to see if the file is internally consistent.
|
||||
*
|
||||
* This function for now checks if the following categories are consistent:
|
||||
*
|
||||
* atom_site -> pdbx_poly_seq_scheme -> entity_poly_seq -> entity_poly -> entity
|
||||
*
|
||||
* Use the common \ref cif::VERBOSE flag to turn on diagnostic messages.
|
||||
*
|
||||
* The dictionary is assumed to be specified in the file or to be the
|
||||
* default mmcif_pdbx.dic dictionary.
|
||||
*
|
||||
* \param file The input file
|
||||
* \result Returns true if the file was valid and consistent
|
||||
*/
|
||||
|
||||
bool is_valid_pdbx_file(const file &pdbx_file, std::error_code &ec);
|
||||
|
||||
/** \brief This is an extension to cif::validator, use the logic in common
|
||||
* PDBx files to see if the file is internally consistent.
|
||||
*
|
||||
* This function for now checks if the following categories are consistent:
|
||||
*
|
||||
* atom_site -> pdbx_poly_seq_scheme -> entity_poly_seq -> entity_poly -> entity
|
||||
*
|
||||
* Use the common \ref cif::VERBOSE flag to turn on diagnostic messages.
|
||||
*
|
||||
* \param file The input file
|
||||
* \param v The validator to use
|
||||
* \param ec The error_code in case something was wrong
|
||||
* \result Returns true if the file was valid and consistent
|
||||
*/
|
||||
|
||||
bool is_valid_pdbx_file(const file &pdbx_file, const validator &v,
|
||||
std::error_code &ec);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Other I/O related routines
|
||||
|
||||
/** @brief Return the HEADER line for the data in @a data
|
||||
*
|
||||
* The line returned should be compatible with the legacy PDB
|
||||
* format and is e.g. used in the DSSP program.
|
||||
*
|
||||
* @param data The datablock to use as source for the requested data
|
||||
* @param truncate_at The maximum length of the line returned
|
||||
*/
|
||||
|
||||
std::string get_HEADER_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
/** @brief Return the COMPND line for the data in @a data
|
||||
*
|
||||
* The line returned should be compatible with the legacy PDB
|
||||
* format and is e.g. used in the DSSP program.
|
||||
*
|
||||
* @param data The datablock to use as source for the requested data
|
||||
* @param truncate_at The maximum length of the line returned
|
||||
*/
|
||||
|
||||
std::string get_COMPND_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
/** @brief Return the SOURCE line for the data in @a data
|
||||
*
|
||||
* The line returned should be compatible with the legacy PDB
|
||||
* format and is e.g. used in the DSSP program.
|
||||
*
|
||||
* @param data The datablock to use as source for the requested data
|
||||
* @param truncate_at The maximum length of the line returned
|
||||
*/
|
||||
|
||||
std::string get_SOURCE_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
/** @brief Return the AUTHOR line for the data in @a data
|
||||
*
|
||||
* The line returned should be compatible with the legacy PDB
|
||||
* format and is e.g. used in the DSSP program.
|
||||
*
|
||||
* @param data The datablock to use as source for the requested data
|
||||
* @param truncate_at The maximum length of the line returned
|
||||
*/
|
||||
|
||||
std::string get_AUTHOR_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
|
||||
} // namespace cif::pdb
|
||||
33
include/cif++/pdb/cif2pdb.hpp
Normal file
33
include/cif++/pdb/cif2pdb.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/// \file cif2pdb.hpp
|
||||
/// \deprecated This file is no longer used. Please use "cif++/pdb.hpp" instead
|
||||
|
||||
#warning "Use of this file is deprecated, please use "cif++/pdb.hpp"
|
||||
|
||||
32
include/cif++/pdb/io.hpp
Normal file
32
include/cif++/pdb/io.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/// \file io.hpp
|
||||
/// \deprecated This file is no longer used. Please use "cif++/pdb.hpp" instead
|
||||
|
||||
#warning "Use of this file is deprecated, please use "cif++/pdb.hpp"
|
||||
32
include/cif++/pdb/pdb2cif.hpp
Normal file
32
include/cif++/pdb/pdb2cif.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/// \file pdb2cif.hpp
|
||||
/// \deprecated This file is no longer used. Please use "cif++/pdb.hpp" instead
|
||||
|
||||
#warning "Use of this file is deprecated, please use "cif++/pdb.hpp"
|
||||
32
include/cif++/pdb/tls.hpp
Normal file
32
include/cif++/pdb/tls.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/// \file tls.hpp
|
||||
/// \deprecated This code has been moved to libpdb-redo
|
||||
|
||||
#warning "This code has been moved to libpdb-redo"
|
||||
919
include/cif++/point.hpp
Normal file
919
include/cif++/point.hpp
Normal file
@@ -0,0 +1,919 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <complex>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <valarray>
|
||||
|
||||
#if __has_include(<clipper/core/coords.h>)
|
||||
#define HAVE_LIBCLIPPER 1
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
|
||||
#include <clipper/core/coords.h>
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
/** \file point.hpp
|
||||
*
|
||||
* This file contains the definition for *cif::point* as well as
|
||||
* lots of routines and classes that can manipulate points.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Our value for Pi
|
||||
const double
|
||||
kPI = 3.141592653589793238462643383279502884;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/**
|
||||
* @brief A stripped down quaternion implementation, based on boost::math::quaternion
|
||||
*
|
||||
* We use quaternions to do rotations in 3d space. Quaternions are faster than
|
||||
* matrix calculations and they also suffer less from drift caused by rounding
|
||||
* errors.
|
||||
*
|
||||
* Like complex number, quaternions do have a meaningful notion of "real part",
|
||||
* but unlike them there is no meaningful notion of "imaginary part".
|
||||
* Instead there is an "unreal part" which itself is a quaternion, and usually
|
||||
* nothing simpler (as opposed to the complex number case).
|
||||
* However, for practicality, there are accessors for the other components
|
||||
* (these are necessary for the templated copy constructor, for instance).
|
||||
*
|
||||
* @note Quaternion multiplication is *NOT* commutative;
|
||||
* symbolically, "q *= rhs;" means "q = q * rhs;"
|
||||
* and "q /= rhs;" means "q = q * inverse_of(rhs);"
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
class quaternion_type
|
||||
{
|
||||
public:
|
||||
/// \brief the value type of the elements, usually this is float
|
||||
using value_type = T;
|
||||
|
||||
/// \brief constructor with the four members
|
||||
constexpr explicit quaternion_type(value_type const &value_a = {}, value_type const &value_b = {}, value_type const &value_c = {}, value_type const &value_d = {})
|
||||
: a(value_a)
|
||||
, b(value_b)
|
||||
, c(value_c)
|
||||
, d(value_d)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking two complex values as input
|
||||
constexpr explicit quaternion_type(std::complex<value_type> const &z0, std::complex<value_type> const &z1 = std::complex<value_type>())
|
||||
: a(z0.real())
|
||||
, b(z0.imag())
|
||||
, c(z1.real())
|
||||
, d(z1.imag())
|
||||
{
|
||||
}
|
||||
|
||||
constexpr quaternion_type(quaternion_type const &) = default; ///< Copy constructor
|
||||
constexpr quaternion_type(quaternion_type &&) = default; ///< Copy constructor
|
||||
|
||||
/// \brief Copy constructor accepting a quaternion with a different value_type
|
||||
template <typename X>
|
||||
constexpr explicit quaternion_type(quaternion_type<X> const &rhs)
|
||||
: a(static_cast<value_type>(rhs.a))
|
||||
, b(static_cast<value_type>(rhs.b))
|
||||
, c(static_cast<value_type>(rhs.c))
|
||||
, d(static_cast<value_type>(rhs.d))
|
||||
{
|
||||
}
|
||||
|
||||
// accessors
|
||||
|
||||
/// \brief See class description, return the *real* part of the quaternion
|
||||
constexpr value_type real() const
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
/// \brief See class description, return the *unreal* part of the quaternion
|
||||
constexpr quaternion_type unreal() const
|
||||
{
|
||||
return { 0, b, c, d };
|
||||
}
|
||||
|
||||
/// \brief swap
|
||||
constexpr void swap(quaternion_type &o)
|
||||
{
|
||||
std::swap(a, o.a);
|
||||
std::swap(b, o.b);
|
||||
std::swap(c, o.c);
|
||||
std::swap(d, o.d);
|
||||
}
|
||||
|
||||
// assignment operators
|
||||
|
||||
/// \brief Assignment operator accepting a quaternion with optionally another value_type
|
||||
template <typename X>
|
||||
constexpr quaternion_type &operator=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
a = static_cast<value_type>(rhs.a);
|
||||
b = static_cast<value_type>(rhs.b);
|
||||
c = static_cast<value_type>(rhs.c);
|
||||
d = static_cast<value_type>(rhs.d);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Assignment operator
|
||||
constexpr quaternion_type &operator=(quaternion_type const &rhs)
|
||||
{
|
||||
a = rhs.a;
|
||||
b = rhs.b;
|
||||
c = rhs.c;
|
||||
d = rhs.d;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Assignment operator that sets the *real* part to @a rhs and the *unreal* parts to zero
|
||||
constexpr quaternion_type &operator=(value_type const &rhs)
|
||||
{
|
||||
a = rhs;
|
||||
|
||||
b = c = d = static_cast<value_type>(0);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Assignment operator that sets the *real* part to the real part of @a rhs
|
||||
/// and the first *unreal* part to the imaginary part of of @a rhs. The other *unreal*
|
||||
// parts are set to zero.
|
||||
constexpr quaternion_type &operator=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
a = rhs.real();
|
||||
b = rhs.imag();
|
||||
|
||||
c = d = static_cast<value_type>(0);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
// other assignment-related operators
|
||||
|
||||
/// \brief operator += adding value @a rhs to the *real* part
|
||||
constexpr quaternion_type &operator+=(value_type const &rhs)
|
||||
{
|
||||
a += rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator += adding the real part of @a rhs to the *real* part
|
||||
/// and the imaginary part of @a rhs to the first *unreal* part
|
||||
constexpr quaternion_type &operator+=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
a += std::real(rhs);
|
||||
b += std::imag(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator += adding the parts of @a rhs to the equivalent part of this
|
||||
template <class X>
|
||||
constexpr quaternion_type &operator+=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
a += rhs.a;
|
||||
b += rhs.b;
|
||||
c += rhs.c;
|
||||
d += rhs.d;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator -= subtracting value @a rhs from the *real* part
|
||||
constexpr quaternion_type &operator-=(value_type const &rhs)
|
||||
{
|
||||
a -= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator -= subtracting the real part of @a rhs from the *real* part
|
||||
/// and the imaginary part of @a rhs from the first *unreal* part
|
||||
constexpr quaternion_type &operator-=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
a -= std::real(rhs);
|
||||
b -= std::imag(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator -= subtracting the parts of @a rhs from the equivalent part of this
|
||||
template <class X>
|
||||
constexpr quaternion_type &operator-=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
a -= rhs.a;
|
||||
b -= rhs.b;
|
||||
c -= rhs.c;
|
||||
d -= rhs.d;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief multiply all parts with value @a rhs
|
||||
constexpr quaternion_type &operator*=(value_type const &rhs)
|
||||
{
|
||||
a *= rhs;
|
||||
b *= rhs;
|
||||
c *= rhs;
|
||||
d *= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief multiply with complex number @a rhs
|
||||
constexpr quaternion_type &operator*=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
value_type ar = rhs.real();
|
||||
value_type br = rhs.imag();
|
||||
quaternion_type result(a * ar - b * br, a * br + b * ar, c * ar + d * br, -c * br + d * ar);
|
||||
swap(result);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief multiply @a a with @a b and return the result
|
||||
friend constexpr quaternion_type operator*(const quaternion_type &a, const quaternion_type &b)
|
||||
{
|
||||
auto result = a;
|
||||
result *= b;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief multiply with quaternion @a rhs
|
||||
template <typename X>
|
||||
constexpr quaternion_type &operator*=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
value_type ar = static_cast<value_type>(rhs.a);
|
||||
value_type br = static_cast<value_type>(rhs.b);
|
||||
value_type cr = static_cast<value_type>(rhs.c);
|
||||
value_type dr = static_cast<value_type>(rhs.d);
|
||||
|
||||
quaternion_type result(a * ar - b * br - c * cr - d * dr, a * br + b * ar + c * dr - d * cr, a * cr - b * dr + c * ar + d * br, a * dr + b * cr - c * br + d * ar);
|
||||
swap(result);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief divide all parts by @a rhs
|
||||
constexpr quaternion_type &operator/=(value_type const &rhs)
|
||||
{
|
||||
a /= rhs;
|
||||
b /= rhs;
|
||||
c /= rhs;
|
||||
d /= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief divide by complex number @a rhs
|
||||
constexpr quaternion_type &operator/=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
value_type ar = rhs.real();
|
||||
value_type br = rhs.imag();
|
||||
value_type denominator = ar * ar + br * br;
|
||||
quaternion_type result((+a * ar + b * br) / denominator, (-a * br + b * ar) / denominator, (+c * ar - d * br) / denominator, (+c * br + d * ar) / denominator);
|
||||
swap(result);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief divide by quaternion @a rhs
|
||||
template <typename X>
|
||||
constexpr quaternion_type &operator/=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
value_type ar = static_cast<value_type>(rhs.a);
|
||||
value_type br = static_cast<value_type>(rhs.b);
|
||||
value_type cr = static_cast<value_type>(rhs.c);
|
||||
value_type dr = static_cast<value_type>(rhs.d);
|
||||
|
||||
value_type denominator = ar * ar + br * br + cr * cr + dr * dr;
|
||||
quaternion_type result((+a * ar + b * br + c * cr + d * dr) / denominator, (-a * br + b * ar - c * dr + d * cr) / denominator, (-a * cr + b * dr + c * ar - d * br) / denominator, (-a * dr - b * cr + c * br + d * ar) / denominator);
|
||||
swap(result);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief normalise the values so that the length of the result is exactly 1
|
||||
friend constexpr quaternion_type normalize(quaternion_type q)
|
||||
{
|
||||
std::valarray<value_type> t(4);
|
||||
|
||||
t[0] = q.a;
|
||||
t[1] = q.b;
|
||||
t[2] = q.c;
|
||||
t[3] = q.d;
|
||||
|
||||
t *= t;
|
||||
|
||||
value_type length = std::sqrt(t.sum());
|
||||
|
||||
if (length > 0.001)
|
||||
q /= static_cast<value_type>(length);
|
||||
else
|
||||
q = quaternion_type(1, 0, 0, 0);
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
/// \brief return the conjugate of this
|
||||
friend constexpr quaternion_type conj(quaternion_type q)
|
||||
{
|
||||
return quaternion_type{ +q.a, -q.b, -q.c, -q.d };
|
||||
}
|
||||
|
||||
constexpr value_type get_a() const { return a; } ///< Return part a
|
||||
constexpr value_type get_b() const { return b; } ///< Return part b
|
||||
constexpr value_type get_c() const { return c; } ///< Return part c
|
||||
constexpr value_type get_d() const { return d; } ///< Return part d
|
||||
|
||||
/// \brief compare with @a rhs
|
||||
constexpr bool operator==(const quaternion_type &rhs) const
|
||||
{
|
||||
return a == rhs.a and b == rhs.b and c == rhs.c and d == rhs.d;
|
||||
}
|
||||
|
||||
/// \brief compare with @a rhs
|
||||
constexpr bool operator!=(const quaternion_type &rhs) const
|
||||
{
|
||||
return a != rhs.a or b != rhs.b or c != rhs.c or d != rhs.d;
|
||||
}
|
||||
|
||||
/// \brief test for all zero values
|
||||
constexpr operator bool() const
|
||||
{
|
||||
return a != 0 or b != 0 or c != 0 or d != 0;
|
||||
}
|
||||
|
||||
private:
|
||||
value_type a, b, c, d;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief This code is similar to the code in boost so I copy the documentation as well:
|
||||
*
|
||||
* > spherical is a simple transposition of polar, it takes as inputs a (positive)
|
||||
* > magnitude and a point on the hypersphere, given by three angles. The first of
|
||||
* > these, theta has a natural range of -pi to +pi, and the other two have natural
|
||||
* > ranges of -pi/2 to +pi/2 (as is the case with the usual spherical coordinates in
|
||||
* > **R**<sup>3</sup>). Due to the many symmetries and periodicities, nothing untoward happens if
|
||||
* > the magnitude is negative or the angles are outside their natural ranges. The
|
||||
* > expected degeneracies (a magnitude of zero ignores the angles settings...) do
|
||||
* > happen however.
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
inline quaternion_type<T> spherical(T const &rho, T const &theta, T const &phi1, T const &phi2)
|
||||
{
|
||||
T cos_phi1 = std::cos(phi1);
|
||||
T cos_phi2 = std::cos(phi2);
|
||||
|
||||
T a = std::cos(theta) * cos_phi1 * cos_phi2;
|
||||
T b = std::sin(theta) * cos_phi1 * cos_phi2;
|
||||
T c = std::sin(phi1) * cos_phi2;
|
||||
T d = std::sin(phi2);
|
||||
|
||||
quaternion_type result(a, b, c, d);
|
||||
result *= rho;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief By default we use the float version of a quaternion
|
||||
using quaternion = quaternion_type<float>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief 3D point: a location with x, y and z coordinates as floating point.
|
||||
*
|
||||
* Note that you can simply use structured binding to get access to the
|
||||
* individual parts like so:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* float x, y, z;
|
||||
* tie(x, y, z) = atom.get_location();
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
template <typename F>
|
||||
struct point_type
|
||||
{
|
||||
/// \brief the value type of the x, y and z members
|
||||
using value_type = F;
|
||||
|
||||
value_type m_x, ///< The x part of the location
|
||||
m_y, ///< The y part of the location
|
||||
m_z; ///< The z part of the location
|
||||
|
||||
/// \brief default constructor, initialises the values to zero
|
||||
constexpr point_type()
|
||||
: m_x(0)
|
||||
, m_y(0)
|
||||
, m_z(0)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking three values
|
||||
constexpr point_type(value_type x, value_type y, value_type z)
|
||||
: m_x(x)
|
||||
, m_y(y)
|
||||
, m_z(z)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief Copy constructor
|
||||
template <typename PF>
|
||||
constexpr point_type(const point_type<PF> &pt)
|
||||
: m_x(static_cast<F>(pt.m_x))
|
||||
, m_y(static_cast<F>(pt.m_y))
|
||||
, m_z(static_cast<F>(pt.m_z))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking a tuple of three values
|
||||
constexpr point_type(const std::tuple<value_type, value_type, value_type> &pt)
|
||||
: point_type(std::get<0>(pt), std::get<1>(pt), std::get<2>(pt))
|
||||
{
|
||||
}
|
||||
|
||||
#if HAVE_LIBCLIPPER
|
||||
/// \brief Construct a point using the values in clipper coordinate @a pt
|
||||
constexpr point_type(const clipper::Coord_orth &pt)
|
||||
: m_x(pt[0])
|
||||
, m_y(pt[1])
|
||||
, m_z(pt[2])
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief Assign a point using the values in clipper coordinate @a rhs
|
||||
constexpr point_type &operator=(const clipper::Coord_orth &rhs)
|
||||
{
|
||||
m_x = rhs[0];
|
||||
m_y = rhs[1];
|
||||
m_z = rhs[2];
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// \brief Assignment operator
|
||||
template <typename PF>
|
||||
constexpr point_type &operator=(const point_type<PF> &rhs)
|
||||
{
|
||||
m_x = static_cast<F>(rhs.m_x);
|
||||
m_y = static_cast<F>(rhs.m_y);
|
||||
m_z = static_cast<F>(rhs.m_z);
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr value_type &get_x() { return m_x; } ///< Get a reference to x
|
||||
constexpr value_type get_x() const { return m_x; } ///< Get the value of x
|
||||
constexpr void set_x(value_type x) { m_x = x; } ///< Set the value of x to @a x
|
||||
|
||||
constexpr value_type &get_y() { return m_y; } ///< Get a reference to y
|
||||
constexpr value_type get_y() const { return m_y; } ///< Get the value of y
|
||||
constexpr void set_y(value_type y) { m_y = y; } ///< Set the value of y to @a y
|
||||
|
||||
constexpr value_type &get_z() { return m_z; } ///< Get a reference to z
|
||||
constexpr value_type get_z() const { return m_z; } ///< Get the value of z
|
||||
constexpr void set_z(value_type z) { m_z = z; } ///< Set the value of z to @a z
|
||||
|
||||
/// \brief add @a rhs
|
||||
constexpr point_type &operator+=(const point_type &rhs)
|
||||
{
|
||||
m_x += rhs.m_x;
|
||||
m_y += rhs.m_y;
|
||||
m_z += rhs.m_z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief add @a d to all members
|
||||
constexpr point_type &operator+=(value_type d)
|
||||
{
|
||||
m_x += d;
|
||||
m_y += d;
|
||||
m_z += d;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Add the points @a lhs and @a rhs and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator+(const point_type &lhs, const point_type<F2> &rhs)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(lhs.m_x + rhs.m_x, lhs.m_y + rhs.m_y, lhs.m_z + rhs.m_z);
|
||||
}
|
||||
|
||||
/// \brief subtract @a rhs
|
||||
constexpr point_type &operator-=(const point_type &rhs)
|
||||
{
|
||||
m_x -= rhs.m_x;
|
||||
m_y -= rhs.m_y;
|
||||
m_z -= rhs.m_z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief subtract @a d from all members
|
||||
constexpr point_type &operator-=(value_type d)
|
||||
{
|
||||
m_x -= d;
|
||||
m_y -= d;
|
||||
m_z -= d;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Subtract the points @a lhs and @a rhs and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator-(const point_type &lhs, const point_type<F2> &rhs)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(lhs.m_x - rhs.m_x, lhs.m_y - rhs.m_y, lhs.m_z - rhs.m_z);
|
||||
}
|
||||
|
||||
/// \brief Return the negative copy of @a pt
|
||||
friend constexpr point_type operator-(const point_type &pt)
|
||||
{
|
||||
return point_type(-pt.m_x, -pt.m_y, -pt.m_z);
|
||||
}
|
||||
|
||||
/// \brief multiply all members with @a rhs
|
||||
constexpr point_type &operator*=(value_type rhs)
|
||||
{
|
||||
m_x *= rhs;
|
||||
m_y *= rhs;
|
||||
m_z *= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief multiply point @a pt with value @a f and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator*(const point_type &pt, F2 f)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(pt.m_x * f, pt.m_y * f, pt.m_z * f);
|
||||
}
|
||||
|
||||
/// \brief multiply point @a pt with value @a f and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator*(F2 f, const point_type &pt)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(pt.m_x * f, pt.m_y * f, pt.m_z * f);
|
||||
}
|
||||
|
||||
/// \brief divide all members by @a rhs
|
||||
constexpr point_type &operator/=(value_type rhs)
|
||||
{
|
||||
m_x /= rhs;
|
||||
m_y /= rhs;
|
||||
m_z /= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief divide point @a pt by value @a f and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator/(const point_type &pt, F2 f)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(pt.m_x / f, pt.m_y / f, pt.m_z / f);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief looking at this point as a vector, normalise it which
|
||||
* means dividing all members by the length making the length
|
||||
* effectively 1.
|
||||
*
|
||||
* @return The previous length of this vector
|
||||
*/
|
||||
constexpr value_type normalize()
|
||||
{
|
||||
auto length = m_x * m_x + m_y * m_y + m_z * m_z;
|
||||
if (length > 0)
|
||||
{
|
||||
length = std::sqrt(length);
|
||||
operator/=(length);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
/// \brief Rotate this point using the quaterion @a q
|
||||
constexpr void rotate(const quaternion &q)
|
||||
{
|
||||
quaternion_type<value_type> p(0, m_x, m_y, m_z);
|
||||
|
||||
p = q * p * conj(q);
|
||||
|
||||
m_x = p.get_b();
|
||||
m_y = p.get_c();
|
||||
m_z = p.get_d();
|
||||
}
|
||||
|
||||
/// \brief Rotate this point using the quaterion @a q by first
|
||||
/// moving the point to @a pivot and after rotating moving it
|
||||
/// back
|
||||
constexpr void rotate(const quaternion &q, point_type pivot)
|
||||
{
|
||||
operator-=(pivot);
|
||||
rotate(q);
|
||||
operator+=(pivot);
|
||||
}
|
||||
|
||||
#if HAVE_LIBCLIPPER
|
||||
/// \brief Make it possible to pass a point to clipper functions expecting a clipper coordinate
|
||||
operator clipper::Coord_orth() const
|
||||
{
|
||||
return clipper::Coord_orth(m_x, m_y, m_z);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// \brief Allow access to this point as if it is a tuple of three const value_type's
|
||||
constexpr operator std::tuple<const value_type &, const value_type &, const value_type &>() const
|
||||
{
|
||||
return std::make_tuple(std::ref(m_x), std::ref(m_y), std::ref(m_z));
|
||||
}
|
||||
|
||||
/// \brief Allow access to this point as if it is a tuple of three value_type's
|
||||
constexpr operator std::tuple<value_type &, value_type &, value_type &>()
|
||||
{
|
||||
return std::make_tuple(std::ref(m_x), std::ref(m_y), std::ref(m_z));
|
||||
}
|
||||
|
||||
#if defined(__cpp_impl_three_way_comparison)
|
||||
/// \brief a default spaceship operator
|
||||
constexpr auto operator<=>(const point_type &rhs) const = default;
|
||||
#else
|
||||
/// \brief a default equals operator
|
||||
constexpr bool operator==(const point_type &rhs) const
|
||||
{
|
||||
return m_x == rhs.m_x and m_y == rhs.m_y and m_z == rhs.m_z;
|
||||
}
|
||||
|
||||
/// \brief a default not-equals operator
|
||||
constexpr bool operator!=(const point_type &rhs) const
|
||||
{
|
||||
return not operator==(rhs);
|
||||
}
|
||||
#endif
|
||||
|
||||
// consider point as a vector... perhaps I should rename point?
|
||||
|
||||
/// \brief looking at the point as if it is a vector, return the squared length
|
||||
constexpr value_type length_sq() const
|
||||
{
|
||||
return m_x * m_x + m_y * m_y + m_z * m_z;
|
||||
}
|
||||
|
||||
/// \brief looking at the point as if it is a vector, return the length
|
||||
constexpr value_type length() const
|
||||
{
|
||||
return std::sqrt(length_sq());
|
||||
}
|
||||
|
||||
/// \brief Print out the point @a pt to @a os
|
||||
friend std::ostream &operator<<(std::ostream &os, const point_type &pt)
|
||||
{
|
||||
os << '(' << pt.m_x << ',' << pt.m_y << ',' << pt.m_z << ')';
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief By default we use points with float value_type
|
||||
using point = point_type<float>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// several standard 3d operations
|
||||
|
||||
/// \brief return the squared distance between points @a a and @a b
|
||||
template <typename F1, typename F2>
|
||||
constexpr auto distance_squared(const point_type<F1> &a, const point_type<F2> &b)
|
||||
{
|
||||
return (a.m_x - b.m_x) * (a.m_x - b.m_x) +
|
||||
(a.m_y - b.m_y) * (a.m_y - b.m_y) +
|
||||
(a.m_z - b.m_z) * (a.m_z - b.m_z);
|
||||
}
|
||||
|
||||
/// \brief return the distance between points @a a and @a b
|
||||
template <typename F1, typename F2>
|
||||
constexpr auto distance(const point_type<F1> &a, const point_type<F2> &b)
|
||||
{
|
||||
return std::sqrt(
|
||||
(a.m_x - b.m_x) * (a.m_x - b.m_x) +
|
||||
(a.m_y - b.m_y) * (a.m_y - b.m_y) +
|
||||
(a.m_z - b.m_z) * (a.m_z - b.m_z));
|
||||
}
|
||||
|
||||
/// \brief return the dot product between the vectors @a a and @a b
|
||||
template <typename F1, typename F2>
|
||||
inline constexpr auto dot_product(const point_type<F1> &a, const point_type<F2> &b)
|
||||
{
|
||||
return a.m_x * b.m_x + a.m_y * b.m_y + a.m_z * b.m_z;
|
||||
}
|
||||
|
||||
/// \brief return the cross product between the vectors @a a and @a b
|
||||
template <typename F1, typename F2>
|
||||
inline constexpr auto cross_product(const point_type<F1> &a, const point_type<F2> &b)
|
||||
{
|
||||
return point_type<std::common_type_t<F1, F2>>(
|
||||
a.m_y * b.m_z - b.m_y * a.m_z,
|
||||
a.m_z * b.m_x - b.m_z * a.m_x,
|
||||
a.m_x * b.m_y - b.m_x * a.m_y);
|
||||
}
|
||||
|
||||
/// \brief return the angle in degrees between the vectors from point @a p2 to @a p1 and @a p2 to @a p3
|
||||
template <typename F>
|
||||
constexpr auto angle(const point_type<F> &p1, const point_type<F> &p2, const point_type<F> &p3)
|
||||
{
|
||||
point_type<F> v1 = p1 - p2;
|
||||
point_type<F> v2 = p3 - p2;
|
||||
|
||||
return std::acos(dot_product(v1, v2) / (v1.length() * v2.length())) * 180 / kPI;
|
||||
}
|
||||
|
||||
/// \brief return the dihedral angle in degrees for the four points @a p1, @a p2, @a p3 and @a p4
|
||||
///
|
||||
/// See https://en.wikipedia.org/wiki/Dihedral_angle for an explanation of what a dihedral angle is
|
||||
template <typename F>
|
||||
constexpr auto dihedral_angle(const point_type<F> &p1, const point_type<F> &p2, const point_type<F> &p3, const point_type<F> &p4)
|
||||
{
|
||||
point_type<F> v12 = p1 - p2; // vector from p2 to p1
|
||||
point_type<F> v43 = p4 - p3; // vector from p3 to p4
|
||||
|
||||
point_type<F> z = p2 - p3; // vector from p3 to p2
|
||||
|
||||
point_type<F> p = cross_product(z, v12);
|
||||
point_type<F> x = cross_product(z, v43);
|
||||
point_type<F> y = cross_product(z, x);
|
||||
|
||||
auto u = dot_product(x, x);
|
||||
auto v = dot_product(y, y);
|
||||
|
||||
F result = 360;
|
||||
if (u > 0 and v > 0)
|
||||
{
|
||||
u = dot_product(p, x) / std::sqrt(u);
|
||||
v = dot_product(p, y) / std::sqrt(v);
|
||||
if (u != 0 or v != 0)
|
||||
result = std::atan2(v, u) * static_cast<F>(180 / kPI);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief return the cosinus angle for the four points @a p1, @a p2, @a p3 and @a p4
|
||||
template <typename F>
|
||||
constexpr auto cosinus_angle(const point_type<F> &p1, const point_type<F> &p2, const point_type<F> &p3, const point_type<F> &p4)
|
||||
{
|
||||
point_type<F> v12 = p1 - p2;
|
||||
point_type<F> v34 = p3 - p4;
|
||||
|
||||
auto x = dot_product(v12, v12) * dot_product(v34, v34);
|
||||
|
||||
return x > 0 ? dot_product(v12, v34) / std::sqrt(x) : 0;
|
||||
}
|
||||
|
||||
/// \brief return the distance from point @a p to the line from @a l1 to @a l2
|
||||
template <typename F>
|
||||
constexpr auto distance_point_to_line(const point_type<F> &l1, const point_type<F> &l2, const point_type<F> &p)
|
||||
{
|
||||
auto line = l2 - l1;
|
||||
auto p_to_l1 = p - l1;
|
||||
auto p_to_l2 = p - l2;
|
||||
auto cross = cross_product(p_to_l1, p_to_l2);
|
||||
return cross.length() / line.length();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/**
|
||||
* @brief For e.g. simulated annealing, returns a new point that is moved in
|
||||
* a random direction with a distance randomly chosen from a normal
|
||||
* distribution with a stddev of offset.
|
||||
*/
|
||||
point nudge(point p, float offset);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Return a quaternion created from angle @a angle and axis @a axis
|
||||
quaternion construct_from_angle_axis(float angle, point axis);
|
||||
|
||||
/// \brief Return a tuple of an angle and an axis for quaternion @a q
|
||||
std::tuple<double, point> quaternion_to_angle_axis(quaternion q);
|
||||
|
||||
/// @brief Given four points and an angle, return the quaternion required to rotate
|
||||
/// point p4 along the p2-p3 axis and around point p3 to obtain the required within
|
||||
/// an accuracy of esd
|
||||
quaternion construct_for_dihedral_angle(point p1, point p2, point p3, point p4,
|
||||
float angle, float esd);
|
||||
|
||||
/// \brief Return the point that is the centroid of all the points in @a pts
|
||||
point centroid(const std::vector<point> &pts);
|
||||
|
||||
/// \brief Move all the points in @a pts so that their centroid is at the origin
|
||||
/// (0, 0, 0) and return the offset used (the former centroid)
|
||||
point center_points(std::vector<point> &pts);
|
||||
|
||||
/// \brief Returns how the two sets of points \a a and \b b can be aligned
|
||||
///
|
||||
/// \param a The first set of points
|
||||
/// \param b The second set of points
|
||||
/// \result The quaternion which should be applied to the points in \a a to
|
||||
/// obtain the best superposition.
|
||||
quaternion align_points(const std::vector<point> &a, const std::vector<point> &b);
|
||||
|
||||
/// \brief The RMSd for the points in \a a and \a b
|
||||
double RMSd(const std::vector<point> &a, const std::vector<point> &b);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/**
|
||||
* @brief Helper class to generate evenly divided points on a sphere
|
||||
*
|
||||
* We use a fibonacci sphere to calculate even distribution of the dots
|
||||
*
|
||||
* @tparam N The number of points on the sphere is 2 * N + 1
|
||||
*/
|
||||
template <int N>
|
||||
class spherical_dots
|
||||
{
|
||||
public:
|
||||
/// \brief the number of points
|
||||
constexpr static int P = 2 * N * 1;
|
||||
|
||||
/// \brief the *weight* of the fibonacci sphere
|
||||
constexpr static double W = (4 * kPI) / P;
|
||||
|
||||
/// \brief the internal storage type
|
||||
using array_type = typename std::array<point, P>;
|
||||
|
||||
/// \brief iterator type
|
||||
using iterator = typename array_type::const_iterator;
|
||||
|
||||
/// \brief singleton instance
|
||||
static spherical_dots &instance()
|
||||
{
|
||||
static spherical_dots sInstance;
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
/// \brief The number of points
|
||||
std::size_t size() const { return P; }
|
||||
|
||||
/// \brief Access a point by index
|
||||
const point operator[](uint32_t inIx) const { return m_points[inIx]; }
|
||||
|
||||
/// \brief iterator pointing to the first point
|
||||
iterator begin() const { return m_points.begin(); }
|
||||
|
||||
/// \brief iterator pointing past the last point
|
||||
iterator end() const { return m_points.end(); }
|
||||
|
||||
/// \brief return the *weight*,
|
||||
double weight() const { return W; }
|
||||
|
||||
spherical_dots()
|
||||
{
|
||||
const double
|
||||
kGoldenRatio = (1 + std::sqrt(5.0)) / 2;
|
||||
|
||||
auto p = m_points.begin();
|
||||
|
||||
for (int32_t i = -N; i <= N; ++i)
|
||||
{
|
||||
double lat = std::asin((2.0 * i) / P);
|
||||
double lon = std::fmod(i, kGoldenRatio) * 2 * kPI / kGoldenRatio;
|
||||
|
||||
p->m_x = std::sin(lon) * std::cos(lat);
|
||||
p->m_y = std::cos(lon) * std::cos(lat);
|
||||
p->m_z = std::sin(lat);
|
||||
|
||||
++p;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
array_type m_points;
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
429
include/cif++/row.hpp
Normal file
429
include/cif++/row.hpp
Normal file
@@ -0,0 +1,429 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/item.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
/**
|
||||
* @file row.hpp
|
||||
*
|
||||
* The class cif::row should be an opaque type. It is used to store the
|
||||
* internal data per row in a category. You should use cif::row_handle
|
||||
* to get access to the contents in a row.
|
||||
*
|
||||
* One could think of rows as vectors of cif::item. But internally
|
||||
* that's not the case.
|
||||
*
|
||||
* You can access the values of stored items by name or index.
|
||||
* The return value of operator[] is an cif::item_handle object.
|
||||
*
|
||||
* @code {.cpp}
|
||||
* cif::category &atom_site = my_db["atom_site"];
|
||||
* cif::row_handle rh = atom_site.front();
|
||||
*
|
||||
* // by name:
|
||||
* std::string name = rh["label_atom_id"].as<std::string>();
|
||||
*
|
||||
* // by index:
|
||||
* uint16_t ix = atom_site.get_item_ix("label_atom_id");
|
||||
* assert(rh[ix].as<std::string() == name);
|
||||
* @endcode
|
||||
*
|
||||
* There some template magic here to allow easy extracting of data
|
||||
* from rows. This can be done using cif::tie e.g.:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* std::string name;
|
||||
* float x, y, z;
|
||||
*
|
||||
* cif::tie(name, x, y, z) = rh.get("label_atom_id", "cartn_x", "cartn_y", "cartn_z");
|
||||
* @endcode
|
||||
*
|
||||
* However, a more modern way uses structured binding:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* const auto &[name, x, y, z] = rh.get<std::string,float,float,float>("label_atom_id", "cartn_x", "cartn_y", "cartn_z");
|
||||
* @endcode
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
// some helper classes to help create tuple result types
|
||||
template <typename... C>
|
||||
struct get_row_result
|
||||
{
|
||||
static constexpr std::size_t N = sizeof...(C);
|
||||
|
||||
get_row_result(const row_handle &r, std::array<uint16_t, N> &&items)
|
||||
: m_row(r)
|
||||
, m_items(std::move(items))
|
||||
{
|
||||
}
|
||||
|
||||
const item_handle operator[](uint16_t ix) const
|
||||
{
|
||||
return m_row[m_items[ix]];
|
||||
}
|
||||
|
||||
template <typename... Ts, std::enable_if_t<N == sizeof...(Ts), int> = 0>
|
||||
operator std::tuple<Ts...>() const
|
||||
{
|
||||
return get<Ts...>(std::index_sequence_for<Ts...>{});
|
||||
}
|
||||
|
||||
template <typename... Ts, std::size_t... Is>
|
||||
std::tuple<Ts...> get(std::index_sequence<Is...>) const
|
||||
{
|
||||
return std::tuple<Ts...>{ m_row[m_items[Is]].template as<Ts>()... };
|
||||
}
|
||||
|
||||
const row_handle &m_row;
|
||||
std::array<uint16_t, N> m_items;
|
||||
};
|
||||
|
||||
// we want to be able to tie some variables to a get_row_result, for this we use tiewraps
|
||||
template <typename... Ts>
|
||||
struct tie_wrap
|
||||
{
|
||||
tie_wrap(Ts... args)
|
||||
: m_value(args...)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename RR>
|
||||
void operator=(const RR &&rr)
|
||||
{
|
||||
// get_row_result will do the conversion, but only if the types
|
||||
// are compatible. That means the number of parameters to the get()
|
||||
// of the row should be equal to the number of items in the tuple
|
||||
// you are trying to tie.
|
||||
|
||||
using RType = std::tuple<typename std::remove_reference<Ts>::type...>;
|
||||
|
||||
m_value = static_cast<RType>(rr);
|
||||
}
|
||||
|
||||
std::tuple<Ts...> m_value;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/// \brief similar to std::tie, assign values to each element in @a v from the
|
||||
/// result of a get on a row_handle.
|
||||
template <typename... Ts>
|
||||
auto tie(Ts &...v)
|
||||
{
|
||||
return detail::tie_wrap<Ts &...>(std::forward<Ts &>(v)...);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/// \brief the row class, this one is not directly accessible from the outside
|
||||
|
||||
class row : public std::vector<item_value>
|
||||
{
|
||||
public:
|
||||
row() = default;
|
||||
|
||||
/**
|
||||
* @brief Return the item_value pointer for item at index @a ix
|
||||
*/
|
||||
item_value* get(uint16_t ix)
|
||||
{
|
||||
return ix < size() ? &data()[ix] : nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the const item_value pointer for item at index @a ix
|
||||
*/
|
||||
const item_value* get(uint16_t ix) const
|
||||
{
|
||||
return ix < size() ? &data()[ix] : nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class category;
|
||||
friend class category_index;
|
||||
|
||||
template <typename, typename...>
|
||||
friend class iterator_impl;
|
||||
|
||||
void append(uint16_t ix, item_value &&iv)
|
||||
{
|
||||
if (ix >= size())
|
||||
resize(ix + 1);
|
||||
|
||||
at(ix) = std::move(iv);
|
||||
}
|
||||
|
||||
void remove(uint16_t ix)
|
||||
{
|
||||
if (ix < size())
|
||||
at(ix) = item_value{};
|
||||
}
|
||||
|
||||
row *m_next = nullptr;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/// \brief row_handle is the way to access data stored in rows
|
||||
|
||||
class row_handle
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
friend struct item_handle;
|
||||
friend class category;
|
||||
friend class category_index;
|
||||
friend class row_initializer;
|
||||
template <typename, typename...> friend class iterator_impl;
|
||||
|
||||
row_handle() = default;
|
||||
|
||||
row_handle(const row_handle &) = default;
|
||||
row_handle(row_handle &&) = default;
|
||||
|
||||
row_handle &operator=(const row_handle &) = default;
|
||||
row_handle &operator=(row_handle &&) = default;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief constructor taking a category @a cat and a row @a r
|
||||
row_handle(const category &cat, const row &r)
|
||||
: m_category(const_cast<category *>(&cat))
|
||||
, m_row(const_cast<row *>(&r))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief return the category this row belongs to
|
||||
const category &get_category() const
|
||||
{
|
||||
return *m_category;
|
||||
}
|
||||
|
||||
/// \brief Return true if the row is empty or uninitialised
|
||||
bool empty() const
|
||||
{
|
||||
return m_category == nullptr or m_row == nullptr;
|
||||
}
|
||||
|
||||
/// \brief convenience method to test for empty()
|
||||
explicit operator bool() const
|
||||
{
|
||||
return not empty();
|
||||
}
|
||||
|
||||
/// \brief return a cif::item_handle to the item in item @a item_ix
|
||||
item_handle operator[](uint16_t item_ix)
|
||||
{
|
||||
return empty() ? item_handle::s_null_item : item_handle(item_ix, *this);
|
||||
}
|
||||
|
||||
/// \brief return a const cif::item_handle to the item in item @a item_ix
|
||||
const item_handle operator[](uint16_t item_ix) const
|
||||
{
|
||||
return empty() ? item_handle::s_null_item : item_handle(item_ix, const_cast<row_handle &>(*this));
|
||||
}
|
||||
|
||||
/// \brief return a cif::item_handle to the item in the item named @a item_name
|
||||
item_handle operator[](std::string_view item_name)
|
||||
{
|
||||
return empty() ? item_handle::s_null_item : item_handle(add_item(item_name), *this);
|
||||
}
|
||||
|
||||
/// \brief return a const cif::item_handle to the item in the item named @a item_name
|
||||
const item_handle operator[](std::string_view item_name) const
|
||||
{
|
||||
return empty() ? item_handle::s_null_item : item_handle(get_item_ix(item_name), const_cast<row_handle &>(*this));
|
||||
}
|
||||
|
||||
/// \brief Return an object that can be used in combination with cif::tie
|
||||
/// to assign the values for the items @a items
|
||||
template <typename... C>
|
||||
auto get(C... items) const
|
||||
{
|
||||
return detail::get_row_result<C...>(*this, { get_item_ix(items)... });
|
||||
}
|
||||
|
||||
/// \brief Return a tuple of values of types @a Ts for the items @a items
|
||||
template <typename... Ts, typename... C, std::enable_if_t<sizeof...(Ts) == sizeof...(C) and sizeof...(C) != 1, int> = 0>
|
||||
std::tuple<Ts...> get(C... items) const
|
||||
{
|
||||
return detail::get_row_result<Ts...>(*this, { get_item_ix(items)... });
|
||||
}
|
||||
|
||||
/// \brief Get the value of item @a item cast to type @a T
|
||||
template <typename T>
|
||||
T get(const char *item) const
|
||||
{
|
||||
return operator[](get_item_ix(item)).template as<T>();
|
||||
}
|
||||
|
||||
/// \brief Get the value of item @a item cast to type @a T
|
||||
template <typename T>
|
||||
T get(std::string_view item) const
|
||||
{
|
||||
return operator[](get_item_ix(item)).template as<T>();
|
||||
}
|
||||
|
||||
/// \brief assign each of the items named in @a values to their respective value
|
||||
void assign(const std::vector<item> &values)
|
||||
{
|
||||
for (auto &value : values)
|
||||
assign(value, true);
|
||||
}
|
||||
|
||||
/** \brief assign the value @a value to the item named @a name
|
||||
*
|
||||
* If updateLinked it true, linked records are updated as well.
|
||||
* That means that if item @a name is part of the link definition
|
||||
* and the link results in a linked record in another category
|
||||
* this record in the linked category is updated as well.
|
||||
*
|
||||
* If validate is true, which is default, the assigned value is
|
||||
* checked to see if it conforms to the rules defined in the dictionary
|
||||
*/
|
||||
|
||||
void assign(std::string_view name, std::string_view value, bool updateLinked, bool validate = true)
|
||||
{
|
||||
assign(add_item(name), value, updateLinked, validate);
|
||||
}
|
||||
|
||||
/** \brief assign the value @a value to item at index @a item
|
||||
*
|
||||
* If updateLinked it true, linked records are updated as well.
|
||||
* That means that if item @a item is part of the link definition
|
||||
* and the link results in a linked record in another category
|
||||
* this record in the linked category is updated as well.
|
||||
*
|
||||
* If validate is true, which is default, the assigned value is
|
||||
* checked to see if it conforms to the rules defined in the dictionary
|
||||
*/
|
||||
|
||||
void assign(uint16_t item, std::string_view value, bool updateLinked, bool validate = true);
|
||||
|
||||
/// \brief compare two rows
|
||||
bool operator==(const row_handle &rhs) const { return m_category == rhs.m_category and m_row == rhs.m_row; }
|
||||
|
||||
/// \brief compare two rows
|
||||
bool operator!=(const row_handle &rhs) const { return m_category != rhs.m_category or m_row != rhs.m_row; }
|
||||
|
||||
private:
|
||||
uint16_t get_item_ix(std::string_view name) const;
|
||||
std::string_view get_item_name(uint16_t ix) const;
|
||||
|
||||
uint16_t add_item(std::string_view name);
|
||||
|
||||
row *get_row()
|
||||
{
|
||||
return m_row;
|
||||
}
|
||||
|
||||
const row *get_row() const
|
||||
{
|
||||
return m_row;
|
||||
}
|
||||
|
||||
void assign(const item &i, bool updateLinked)
|
||||
{
|
||||
assign(i.name(), i.value(), updateLinked);
|
||||
}
|
||||
|
||||
void swap(uint16_t item, row_handle &r);
|
||||
|
||||
category *m_category = nullptr;
|
||||
row *m_row = nullptr;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The class row_initializer is a list of cif::item's.
|
||||
*
|
||||
* This class is used to construct new rows, it allows to
|
||||
* group a list of item name and value pairs and pass it
|
||||
* in one go to the constructing function.
|
||||
*/
|
||||
class row_initializer : public std::vector<item>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
friend class category;
|
||||
|
||||
row_initializer() = default;
|
||||
row_initializer(const row_initializer &) = default;
|
||||
row_initializer(row_initializer &&) = default;
|
||||
row_initializer &operator=(const row_initializer &) = default;
|
||||
row_initializer &operator=(row_initializer &&) = default;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief constructor taking a std::initializer_list of items
|
||||
row_initializer(std::initializer_list<item> items)
|
||||
: std::vector<item>(items)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking a range of items
|
||||
template <typename ItemIter, std::enable_if_t<std::is_same_v<typename ItemIter::value_type, item>, int> = 0>
|
||||
row_initializer(ItemIter b, ItemIter e)
|
||||
: std::vector<item>(b, e)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking the values of an existing row
|
||||
row_initializer(row_handle rh);
|
||||
|
||||
|
||||
/// \brief set the value for item name @a name to @a value
|
||||
void set_value(std::string_view name, std::string_view value);
|
||||
|
||||
/// \brief set the value for item based on @a i
|
||||
void set_value(const item &i)
|
||||
{
|
||||
set_value(i.name(), i.value());
|
||||
}
|
||||
|
||||
/// \brief set the value for item name @a name to @a value, but only if the item did not have a value already
|
||||
void set_value_if_empty(std::string_view name, std::string_view value);
|
||||
|
||||
/// \brief set the value for item @a i, but only if the item did not have a value already
|
||||
void set_value_if_empty(const item &i)
|
||||
{
|
||||
set_value_if_empty(i.name(), i.value());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
543
include/cif++/symmetry.hpp
Normal file
543
include/cif++/symmetry.hpp
Normal file
@@ -0,0 +1,543 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/exports.hpp"
|
||||
#include "cif++/matrix.hpp"
|
||||
#include "cif++/point.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#if defined(__cpp_impl_three_way_comparison)
|
||||
#include <compare>
|
||||
#endif
|
||||
|
||||
/** \file cif++/symmetry.hpp
|
||||
*
|
||||
* This file contains code to do symmetry operations based on the
|
||||
* operations as specified in the International Tables.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Apply matrix transformation @a m on point @a pt and return the result
|
||||
inline point operator*(const matrix3x3<float> &m, const point &pt)
|
||||
{
|
||||
return {
|
||||
m(0, 0) * pt.m_x + m(0, 1) * pt.m_y + m(0, 2) * pt.m_z,
|
||||
m(1, 0) * pt.m_x + m(1, 1) * pt.m_y + m(1, 2) * pt.m_z,
|
||||
m(2, 0) * pt.m_x + m(2, 1) * pt.m_y + m(2, 2) * pt.m_z
|
||||
};
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief the space groups we know
|
||||
enum class space_group_name
|
||||
{
|
||||
full, ///< The *full* spacegroup
|
||||
xHM, ///< The *xHM* spacegroup
|
||||
Hall ///< The *Hall* spacegroup
|
||||
};
|
||||
|
||||
/// \brief For each known spacegroup we define a structure like this
|
||||
struct space_group
|
||||
{
|
||||
const char *name; ///< The name according to *full*
|
||||
const char *xHM; ///< The name according to *xHM*
|
||||
const char *Hall; ///< The name according to *Hall*
|
||||
int nr; ///< The number for this spacegroup
|
||||
};
|
||||
|
||||
/// \brief Global list of spacegroups
|
||||
extern CIFPP_EXPORT const space_group kSpaceGroups[];
|
||||
|
||||
/// \brief Global for the size of the list of spacegroups
|
||||
extern CIFPP_EXPORT const std::size_t kNrOfSpaceGroups;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Helper class to efficiently pack the data that
|
||||
* makes up a symmetry operation
|
||||
*
|
||||
*/
|
||||
|
||||
struct symop_data
|
||||
{
|
||||
/// \brief constructor
|
||||
constexpr symop_data(const std::array<int, 15> &data)
|
||||
: m_packed((data[0] bitand 0x03ULL) << 34 bitor
|
||||
(data[1] bitand 0x03ULL) << 32 bitor
|
||||
(data[2] bitand 0x03ULL) << 30 bitor
|
||||
(data[3] bitand 0x03ULL) << 28 bitor
|
||||
(data[4] bitand 0x03ULL) << 26 bitor
|
||||
(data[5] bitand 0x03ULL) << 24 bitor
|
||||
(data[6] bitand 0x03ULL) << 22 bitor
|
||||
(data[7] bitand 0x03ULL) << 20 bitor
|
||||
(data[8] bitand 0x03ULL) << 18 bitor
|
||||
(data[9] bitand 0x07ULL) << 15 bitor
|
||||
(data[10] bitand 0x07ULL) << 12 bitor
|
||||
(data[11] bitand 0x07ULL) << 9 bitor
|
||||
(data[12] bitand 0x07ULL) << 6 bitor
|
||||
(data[13] bitand 0x07ULL) << 3 bitor
|
||||
(data[14] bitand 0x07ULL) << 0)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief compare
|
||||
bool operator==(const symop_data &rhs) const
|
||||
{
|
||||
return m_packed == rhs.m_packed;
|
||||
}
|
||||
|
||||
/// \brief sorting order
|
||||
bool operator<(const symop_data &rhs) const
|
||||
{
|
||||
return m_packed < rhs.m_packed;
|
||||
}
|
||||
|
||||
/// \brief return an int representing the value stored in the two bits at offset @a offset
|
||||
inline constexpr int unpack3(int offset) const
|
||||
{
|
||||
int result = (m_packed >> offset) bitand 0x03;
|
||||
return result == 3 ? -1 : result;
|
||||
}
|
||||
|
||||
/// \brief return an int representing the value stored in the three bits at offset @a offset
|
||||
inline constexpr int unpack7(int offset) const
|
||||
{
|
||||
return (m_packed >> offset) bitand 0x07;
|
||||
}
|
||||
|
||||
/// \brief return an array of 15 ints representing the values stored
|
||||
constexpr std::array<int, 15> data() const
|
||||
{
|
||||
return {
|
||||
unpack3(34),
|
||||
unpack3(32),
|
||||
unpack3(30),
|
||||
unpack3(28),
|
||||
unpack3(26),
|
||||
unpack3(24),
|
||||
unpack3(22),
|
||||
unpack3(20),
|
||||
unpack3(18),
|
||||
unpack7(15),
|
||||
unpack7(12),
|
||||
unpack7(9),
|
||||
unpack7(6),
|
||||
unpack7(3),
|
||||
unpack7(0)
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
friend struct symop_datablock;
|
||||
|
||||
const uint64_t kPackMask = (~0ULL >> (64 - 36));
|
||||
|
||||
symop_data(uint64_t v)
|
||||
: m_packed(v bitand kPackMask)
|
||||
{
|
||||
}
|
||||
|
||||
uint64_t m_packed;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief For each symmetry operator defined in the international tables
|
||||
* we have an entry in this struct type. It contains the spacegroup
|
||||
* number, the symmetry operations and the rotational number.
|
||||
*/
|
||||
struct symop_datablock
|
||||
{
|
||||
/// \brief constructor
|
||||
constexpr symop_datablock(int spacegroup, int rotational_number, const std::array<int, 15> &rt_data)
|
||||
: m_v((spacegroup bitand 0xffffULL) << 48 bitor
|
||||
(rotational_number bitand 0xffULL) << 40 bitor
|
||||
symop_data(rt_data).m_packed)
|
||||
{
|
||||
}
|
||||
|
||||
uint16_t spacegroup() const { return m_v >> 48; } ///< Return the spacegroup
|
||||
symop_data symop() const { return symop_data(m_v); } ///< Return the symmetry operation
|
||||
uint8_t rotational_number() const { return (m_v >> 40) bitand 0xff; } ///< Return the rotational_number
|
||||
|
||||
private:
|
||||
uint64_t m_v;
|
||||
};
|
||||
|
||||
static_assert(sizeof(symop_datablock) == sizeof(uint64_t), "Size of symop_data is wrong");
|
||||
|
||||
/// \brief Global containing the list of known symmetry operations
|
||||
extern CIFPP_EXPORT const symop_datablock kSymopNrTable[];
|
||||
|
||||
/// \brief Size of the list of known symmetry operations
|
||||
extern CIFPP_EXPORT const std::size_t kSymopNrTableSize;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Some more symmetry related stuff here.
|
||||
|
||||
class datablock;
|
||||
|
||||
class cell;
|
||||
class spacegroup;
|
||||
class rtop;
|
||||
struct sym_op;
|
||||
|
||||
/** @brief A class that encapsulates the symmetry operations as used in PDB files,
|
||||
* i.e. a rotational number and a translation vector.
|
||||
*
|
||||
* The syntax in string format follows the syntax as used in mmCIF files, i.e.
|
||||
* rotational number followed by underscore and the three translations where 5 is
|
||||
* no movement.
|
||||
*
|
||||
* So the string 1_555 means no symmetry movement at all since the rotational number
|
||||
* 1 always corresponds to the symmetry operation [x, y, z].
|
||||
*/
|
||||
|
||||
struct sym_op
|
||||
{
|
||||
public:
|
||||
/// \brief constructor
|
||||
sym_op(uint8_t nr = 1, uint8_t ta = 5, uint8_t tb = 5, uint8_t tc = 5)
|
||||
: m_nr(nr)
|
||||
, m_ta(ta)
|
||||
, m_tb(tb)
|
||||
, m_tc(tc)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief construct a sym_op based on the contents encoded in string @a s
|
||||
explicit sym_op(std::string_view s);
|
||||
|
||||
/** @cond */
|
||||
sym_op(const sym_op &) = default;
|
||||
sym_op(sym_op &&) = default;
|
||||
sym_op &operator=(const sym_op &) = default;
|
||||
sym_op &operator=(sym_op &&) = default;
|
||||
/** @endcond */
|
||||
|
||||
/// \brief return true if this sym_op is the identity operator
|
||||
constexpr bool is_identity() const
|
||||
{
|
||||
return m_nr == 1 and m_ta == 5 and m_tb == 5 and m_tc == 5;
|
||||
}
|
||||
|
||||
/// \brief quick test for unequal to identity
|
||||
explicit constexpr operator bool() const
|
||||
{
|
||||
return not is_identity();
|
||||
}
|
||||
|
||||
/// \brief return the content encoded in a string
|
||||
std::string string() const;
|
||||
|
||||
#if defined(__cpp_impl_three_way_comparison)
|
||||
/// \brief a default spaceship operator
|
||||
constexpr auto operator<=>(const sym_op &rhs) const = default;
|
||||
#else
|
||||
/// \brief a default equals operator
|
||||
constexpr bool operator==(const sym_op &rhs) const
|
||||
{
|
||||
return m_nr == rhs.m_nr and m_ta == rhs.m_ta and m_tb == rhs.m_tb and m_tc == rhs.m_tc;
|
||||
}
|
||||
|
||||
/// \brief a default not-equals operator
|
||||
constexpr bool operator!=(const sym_op &rhs) const
|
||||
{
|
||||
return not operator==(rhs);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// @cond
|
||||
uint8_t m_nr;
|
||||
uint8_t m_ta, m_tb, m_tc;
|
||||
/// @endcond
|
||||
};
|
||||
|
||||
static_assert(sizeof(sym_op) == 4, "Sym_op should be four bytes");
|
||||
|
||||
namespace literals
|
||||
{
|
||||
/**
|
||||
* @brief This operator allows you to write code like this:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* using namespace cif::literals;
|
||||
*
|
||||
* cif::sym_op so = "1_555"_symop;
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
inline sym_op operator""_symop(const char *text, std::size_t length)
|
||||
{
|
||||
return sym_op({ text, length });
|
||||
}
|
||||
} // namespace literals
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// The transformation class
|
||||
|
||||
/**
|
||||
* @brief A class you can use to apply symmetry transformations on points
|
||||
*
|
||||
* Transformations consist of two operations, a matrix transformation which
|
||||
* is often a rotation followed by a translation.
|
||||
*
|
||||
* In case the matrix transformation is a pure rotation a quaternion
|
||||
* is created to do the actual calculations. That's faster and more
|
||||
* precise.
|
||||
*/
|
||||
class transformation
|
||||
{
|
||||
public:
|
||||
/// \brief constructor taking a symop_data object @a data
|
||||
transformation(const symop_data &data);
|
||||
|
||||
/// \brief constructor taking a rotation matrix @a r and a translation vector @a t
|
||||
transformation(const matrix3x3<float> &r, const cif::point &t);
|
||||
|
||||
/** @cond */
|
||||
transformation(const transformation &) = default;
|
||||
transformation(transformation &&) = default;
|
||||
transformation &operator=(const transformation &) = default;
|
||||
transformation &operator=(transformation &&) = default;
|
||||
/** @endcond */
|
||||
|
||||
/// \brief operator() to perform the transformation on point @a pt and return the result
|
||||
point operator()(point pt) const
|
||||
{
|
||||
if (m_q)
|
||||
pt.rotate(m_q);
|
||||
else
|
||||
pt = m_rotation * pt;
|
||||
|
||||
return pt + m_translation;
|
||||
}
|
||||
|
||||
/// \brief return a transformation object that is the result of applying @a rhs after @a lhs
|
||||
friend transformation operator*(const transformation &lhs, const transformation &rhs);
|
||||
|
||||
/// \brief return the inverse transformation for @a t
|
||||
friend transformation inverse(const transformation &t);
|
||||
|
||||
/// \brief return the inverse tranformation for this
|
||||
transformation operator-() const
|
||||
{
|
||||
return inverse(*this);
|
||||
}
|
||||
|
||||
friend class spacegroup;
|
||||
|
||||
private:
|
||||
// Most rotation matrices provided by the International Tables
|
||||
// are really rotation matrices, in those cases we can construct
|
||||
// a quaternion. Unfortunately, that doesn't work for all of them
|
||||
|
||||
void try_create_quaternion();
|
||||
|
||||
matrix3x3<float> m_rotation;
|
||||
quaternion m_q;
|
||||
point m_translation;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// class cell
|
||||
|
||||
/**
|
||||
* @brief The cell class describes the dimensions and angles of a unit cell
|
||||
* in a crystal
|
||||
*/
|
||||
|
||||
class cell
|
||||
{
|
||||
public:
|
||||
/// \brief constructor
|
||||
cell(float a, float b, float c, float alpha = 90.f, float beta = 90.f, float gamma = 90.f);
|
||||
|
||||
/// \brief constructor that takes the appropriate values from the *cell* category in datablock @a db
|
||||
cell(const datablock &db);
|
||||
|
||||
float get_a() const { return m_a; } ///< return dimension a
|
||||
float get_b() const { return m_b; } ///< return dimension b
|
||||
float get_c() const { return m_c; } ///< return dimension c
|
||||
|
||||
float get_alpha() const { return m_alpha; } ///< return angle alpha
|
||||
float get_beta() const { return m_beta; } ///< return angle beta
|
||||
float get_gamma() const { return m_gamma; } ///< return angle gamma
|
||||
|
||||
float get_volume() const; ///< return the calculated volume for this cell
|
||||
|
||||
matrix3x3<float> get_orthogonal_matrix() const { return m_orthogonal; } ///< return the matrix to use to transform coordinates from fractional to orthogonal
|
||||
matrix3x3<float> get_fractional_matrix() const { return m_fractional; } ///< return the matrix to use to transform coordinates from orthogonal to fractional
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
float m_a, m_b, m_c, m_alpha, m_beta, m_gamma;
|
||||
matrix3x3<float> m_orthogonal, m_fractional;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Return the spacegroup number from the *symmetry* category in datablock @a db
|
||||
int get_space_group_number(const datablock &db);
|
||||
|
||||
/// \brief Return the spacegroup number for spacegroup named @a spacegroup
|
||||
int get_space_group_number(std::string_view spacegroup);
|
||||
|
||||
/// \brief Return the spacegroup number for spacegroup named @a spacegroup assuming space_group_name @a type
|
||||
int get_space_group_number(std::string_view spacegroup, space_group_name type);
|
||||
|
||||
/**
|
||||
* @brief class to encapsulate the list of transformations making up a spacegroup
|
||||
*
|
||||
*/
|
||||
class spacegroup : public std::vector<transformation>
|
||||
{
|
||||
public:
|
||||
/// \brief constructor using the information in the *symmetry* category in datablock @a db
|
||||
spacegroup(const datablock &db)
|
||||
: spacegroup(get_space_group_number(db))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor using the spacegroup named @a name
|
||||
spacegroup(std::string_view name)
|
||||
: spacegroup(get_space_group_number(name))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor using the spacegroup named @a name assuming space_group_name @a type
|
||||
spacegroup(std::string_view name, space_group_name type)
|
||||
: spacegroup(get_space_group_number(name, type))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor using the spacegroup number @a nr
|
||||
spacegroup(int nr);
|
||||
|
||||
int get_nr() const { return m_nr; } ///< Return the nr
|
||||
std::string get_name() const; ///< Return the name
|
||||
|
||||
/** \brief perform a spacegroup operation on point @a pt using
|
||||
* cell @a c and sym_op @a symop
|
||||
*/
|
||||
|
||||
point operator()(const point &pt, const cell &c, sym_op symop) const;
|
||||
|
||||
/** \brief perform an inverse spacegroup operation on point @a pt using
|
||||
* cell @a c and sym_op @a symop
|
||||
*/
|
||||
point inverse(const point &pt, const cell &c, sym_op symop) const;
|
||||
|
||||
private:
|
||||
int m_nr;
|
||||
std::size_t m_index;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/**
|
||||
* @brief A crystal combines a cell and a spacegroup.
|
||||
*
|
||||
* The information in cell and spacegroup together make up all
|
||||
* information you need to do symmetry calculations in a crystal
|
||||
*/
|
||||
|
||||
class crystal
|
||||
{
|
||||
public:
|
||||
/// \brief constructor using the information found in datablock @a db
|
||||
crystal(const datablock &db)
|
||||
: m_cell(db)
|
||||
, m_spacegroup(db)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor using cell @a c and spacegroup @a sg
|
||||
crystal(const cell &c, const spacegroup &sg)
|
||||
: m_cell(c)
|
||||
, m_spacegroup(sg)
|
||||
{
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
crystal(const crystal &) = default;
|
||||
crystal(crystal &&) = default;
|
||||
crystal &operator=(const crystal &) = default;
|
||||
crystal &operator=(crystal &&) = default;
|
||||
/** @endcond */
|
||||
|
||||
const cell &get_cell() const { return m_cell; } ///< Return the cell
|
||||
const spacegroup &get_spacegroup() const { return m_spacegroup; } ///< Return the spacegroup
|
||||
|
||||
/// \brief Return the symmetry copy of point @a pt using symmetry operation @a symop
|
||||
point symmetry_copy(const point &pt, sym_op symop) const
|
||||
{
|
||||
return m_spacegroup(pt, m_cell, symop);
|
||||
}
|
||||
|
||||
/// \brief Return the symmetry copy of point @a pt using the inverse of symmetry operation @a symop
|
||||
point inverse_symmetry_copy(const point &pt, sym_op symop) const
|
||||
{
|
||||
return m_spacegroup.inverse(pt, m_cell, symop);
|
||||
}
|
||||
|
||||
/// \brief Return a tuple consisting of distance, new location and symmetry operation
|
||||
/// for the point @a b with respect to point @a a.
|
||||
std::tuple<float, point, sym_op> closest_symmetry_copy(point a, point b) const;
|
||||
|
||||
private:
|
||||
cell m_cell;
|
||||
spacegroup m_spacegroup;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Symmetry operations on points
|
||||
|
||||
/// \brief convenience function returning the fractional point @a pt in orthogonal coordinates for cell @a c
|
||||
inline point orthogonal(const point &pt, const cell &c)
|
||||
{
|
||||
return c.get_orthogonal_matrix() * pt;
|
||||
}
|
||||
|
||||
/// \brief convenience function returning the orthogonal point @a pt in fractional coordinates for cell @a c
|
||||
inline point fractional(const point &pt, const cell &c)
|
||||
{
|
||||
return c.get_fractional_matrix() * pt;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
} // namespace cif
|
||||
633
include/cif++/text.hpp
Normal file
633
include/cif++/text.hpp
Normal file
@@ -0,0 +1,633 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/exports.hpp"
|
||||
|
||||
#include <charconv>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#if __has_include(<experimental/type_traits>)
|
||||
|
||||
#include <experimental/type_traits>
|
||||
namespace std_experimental = std::experimental;
|
||||
|
||||
#else
|
||||
|
||||
// A quick hack to work around the missing is_detected in MSVC
|
||||
namespace std_experimental
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <class AlwaysVoid, template <class...> class Op, class... Args>
|
||||
struct detector
|
||||
{
|
||||
using value_t = std::false_type;
|
||||
};
|
||||
|
||||
template <template <class...> class Op, class... Args>
|
||||
struct detector<std::void_t<Op<Args...>>, Op, Args...>
|
||||
{
|
||||
using value_t = std::true_type;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
template <template <class...> class Op, class... Args>
|
||||
using is_detected = typename detail::detector<void, Op, Args...>::value_t;
|
||||
|
||||
template <template <class...> class Op, class... Args>
|
||||
const auto is_detected_v = is_detected<Op, Args...>::value;
|
||||
|
||||
} // namespace std_experimental
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \file text.hpp
|
||||
*
|
||||
* Various text manipulating routines
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
// some basic utilities: Since we're using ASCII input only, we define for optimisation
|
||||
// our own case conversion routines.
|
||||
|
||||
/// \brief return whether string @a is equal to string @a b ignoring changes in character case
|
||||
bool iequals(std::string_view a, std::string_view b);
|
||||
|
||||
/// \brief compare string @a is to string @a b ignoring changes in character case
|
||||
int icompare(std::string_view a, std::string_view b);
|
||||
|
||||
/// \brief return whether string @a is equal to string @a b ignoring changes in character case
|
||||
bool iequals(const char *a, const char *b);
|
||||
|
||||
/// \brief compare string @a is to string @a b ignoring changes in character case
|
||||
int icompare(const char *a, const char *b);
|
||||
|
||||
/// \brief convert the string @a s to lower case in situ
|
||||
void to_lower(std::string &s);
|
||||
|
||||
/// \brief return a lower case copy of string @a s
|
||||
std::string to_lower_copy(std::string_view s);
|
||||
|
||||
/// \brief convert the string @a s to upper case in situ
|
||||
void to_upper(std::string &s);
|
||||
|
||||
/**
|
||||
* @brief Join the strings in the range [ @a a, @a e ) using
|
||||
* @a sep as separator
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* std::vector<std::string> v{ "aap", "noot", "mies" };
|
||||
*
|
||||
* assert(cif::join(v.begin(), v.end(), ", ") == "aap, noot, mies");
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
template <typename IterType>
|
||||
std::string join(IterType b, IterType e, std::string_view sep)
|
||||
{
|
||||
std::ostringstream s;
|
||||
|
||||
if (b != e)
|
||||
{
|
||||
auto ai = b;
|
||||
auto ni = std::next(ai);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
s << *ai;
|
||||
|
||||
if (ni == e)
|
||||
break;
|
||||
|
||||
ai = ni;
|
||||
ni = std::next(ai);
|
||||
|
||||
s << sep;
|
||||
}
|
||||
}
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Join the strings in the array @a arr using @a sep as separator
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* std::list<std::string> v{ "aap", "noot", "mies" };
|
||||
*
|
||||
* assert(cif::join(v, ", ") == "aap, noot, mies");
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
template <typename V>
|
||||
std::string join(const V &arr, std::string_view sep)
|
||||
{
|
||||
return join(arr.begin(), arr.end(), sep);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Split the string in @a s based on the characters in @a separators
|
||||
*
|
||||
* Each of the characters in @a separators induces a split.
|
||||
*
|
||||
* When suppress_empty is true, empty strings are not produced in the
|
||||
* resulting array.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* auto v = cif::split("aap:noot,,mies", ":,", true);
|
||||
*
|
||||
* assert(v == std::vector{"aap", "noot", "mies"});
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
template <typename StringType = std::string_view>
|
||||
std::vector<StringType> split(std::string_view s, std::string_view separators, bool suppress_empty = false)
|
||||
{
|
||||
std::vector<StringType> result;
|
||||
|
||||
auto b = s.data();
|
||||
auto e = b;
|
||||
|
||||
while (e != s.data() + s.length())
|
||||
{
|
||||
if (separators.find(*e) != std::string_view::npos)
|
||||
{
|
||||
if (e > b or not suppress_empty)
|
||||
result.emplace_back(b, e - b);
|
||||
b = e = e + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
++e;
|
||||
}
|
||||
|
||||
if (e > b or not suppress_empty)
|
||||
result.emplace_back(b, e - b);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Replace all occurrences of @a what in string @a s with the string @a with
|
||||
*
|
||||
* The string @a with may be empty in which case each occurrence of @a what is simply
|
||||
* deleted.
|
||||
*/
|
||||
void replace_all(std::string &s, std::string_view what, std::string_view with = {});
|
||||
|
||||
#if defined(__cpp_lib_starts_ends_with)
|
||||
|
||||
/// \brief return whether string @a s starts with @a with
|
||||
inline bool starts_with(std::string s, std::string_view with)
|
||||
{
|
||||
return s.starts_with(with);
|
||||
}
|
||||
|
||||
/// \brief return whether string @a s ends with @a with
|
||||
inline bool ends_with(std::string_view s, std::string_view with)
|
||||
{
|
||||
return s.ends_with(with);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/// \brief return whether string @a s starts with @a with
|
||||
inline bool starts_with(std::string s, std::string_view with)
|
||||
{
|
||||
return s.compare(0, with.length(), with) == 0;
|
||||
}
|
||||
|
||||
/// \brief return whether string @a s ends with @a with
|
||||
inline bool ends_with(std::string_view s, std::string_view with)
|
||||
{
|
||||
return s.length() >= with.length() and s.compare(s.length() - with.length(), with.length(), with) == 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(__cpp_lib_string_contains)
|
||||
|
||||
/// \brief return whether string @a s contains @a q
|
||||
inline bool contains(std::string_view s, std::string_view q)
|
||||
{
|
||||
return s.contains(q);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/// \brief return whether string @a s contains @a q
|
||||
inline bool contains(std::string_view s, std::string_view q)
|
||||
{
|
||||
return s.find(q) != std::string_view::npos;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// \brief return whether string @a s contains @a q ignoring character case
|
||||
bool icontains(std::string_view s, std::string_view q);
|
||||
|
||||
/// \brief trim white space at the start of string @a s in situ
|
||||
void trim_left(std::string &s);
|
||||
|
||||
/// \brief trim white space at the end of string @a s in situ
|
||||
void trim_right(std::string &s);
|
||||
|
||||
/// \brief trim white space at both the start and the end of string @a s in situ
|
||||
void trim(std::string &s);
|
||||
|
||||
/// \brief return a string trimmed of white space at the start of string @a s
|
||||
std::string trim_left_copy(std::string_view s);
|
||||
|
||||
/// \brief return a string trimmed of white space at the end of string @a s
|
||||
std::string trim_right_copy(std::string_view s);
|
||||
|
||||
/// \brief return a string trimmed of white space at both the start and the end of string @a s
|
||||
std::string trim_copy(std::string_view s);
|
||||
|
||||
// To make life easier, we also define iless and iset using iequals
|
||||
|
||||
/// \brief an operator object you can use to compare strings ignoring their character case
|
||||
struct iless
|
||||
{
|
||||
/// \brief return the result of icompare for @a a and @a b
|
||||
bool operator()(const std::string &a, const std::string &b) const
|
||||
{
|
||||
return icompare(a, b) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
/// iset is a std::set of std::string but with a comparator that
|
||||
/// ignores character case.
|
||||
using iset = std::set<std::string, iless>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// This really makes a difference, having our own tolower routines
|
||||
|
||||
/// \brief global list containing the lower case version of each ASCII character
|
||||
extern CIFPP_EXPORT const uint8_t kCharToLowerMap[256];
|
||||
|
||||
/// \brief a very fast tolower implementation
|
||||
inline char tolower(int ch)
|
||||
{
|
||||
return static_cast<char>(kCharToLowerMap[static_cast<uint8_t>(ch)]);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** \brief return a tuple consisting of the category and item name for @a item_name
|
||||
*
|
||||
* The category name is stripped of its leading underscore character.
|
||||
*
|
||||
* If no dot character was found, the category name is empty. That's for
|
||||
* cif 1.0 formatted data.
|
||||
*/
|
||||
|
||||
[[deprecated("use split_item_name instead")]]
|
||||
std::tuple<std::string, std::string> split_tag_name(std::string_view item_name);
|
||||
|
||||
|
||||
/** \brief return a tuple consisting of the category and item name for @a item_name
|
||||
*
|
||||
* The category name is stripped of its leading underscore character.
|
||||
*
|
||||
* If no dot character was found, the category name is empty. That's for
|
||||
* cif 1.0 formatted data.
|
||||
*/
|
||||
|
||||
std::tuple<std::string, std::string> split_item_name(std::string_view item_name);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief generate a cif name, used e.g. to generate asym_id's
|
||||
std::string cif_id_for_number(int number);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** \brief custom word wrapping routine.
|
||||
*
|
||||
* Wrap the text in @a text based on a maximum line width @a width using
|
||||
* a dynamic programming approach to get the most efficient filling of
|
||||
* the space.
|
||||
*/
|
||||
std::vector<std::string> word_wrap(const std::string &text, std::size_t width);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/// \brief std::from_chars for floating point types.
|
||||
///
|
||||
/// These are optional, there's a selected_charconv class below that selects
|
||||
/// the best option to use based on support by the stl library.
|
||||
///
|
||||
/// I.e. that in case of GNU < 12 (or something) the cif implementation will
|
||||
/// be used, all other cases will use the stl version.
|
||||
|
||||
template <typename FloatType, std::enable_if_t<std::is_floating_point_v<FloatType>, int> = 0>
|
||||
std::from_chars_result from_chars(const char *first, const char *last, FloatType &value)
|
||||
{
|
||||
std::from_chars_result result{ first, {} };
|
||||
|
||||
enum State
|
||||
{
|
||||
IntegerSign,
|
||||
Integer,
|
||||
Fraction,
|
||||
ExponentSign,
|
||||
Exponent
|
||||
} state = IntegerSign;
|
||||
int sign = 1;
|
||||
unsigned long long vi = 0;
|
||||
int fl = 0, tz = 0;
|
||||
int exponent_sign = 1;
|
||||
int exponent = 0;
|
||||
bool done = false;
|
||||
|
||||
while (not done and not (bool)result.ec)
|
||||
{
|
||||
char ch = result.ptr != last ? *result.ptr : 0;
|
||||
++result.ptr;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case IntegerSign:
|
||||
if (ch == '-')
|
||||
{
|
||||
sign = -1;
|
||||
state = Integer;
|
||||
}
|
||||
else if (ch == '+')
|
||||
state = Integer;
|
||||
else if (ch >= '0' and ch <= '9')
|
||||
{
|
||||
vi = ch - '0';
|
||||
state = Integer;
|
||||
}
|
||||
else if (ch == '.')
|
||||
state = Fraction;
|
||||
else
|
||||
result.ec = std::errc::invalid_argument;
|
||||
break;
|
||||
|
||||
case Integer:
|
||||
if (ch >= '0' and ch <= '9')
|
||||
vi = 10 * vi + (ch - '0');
|
||||
else if (ch == 'e' or ch == 'E')
|
||||
state = ExponentSign;
|
||||
else if (ch == '.')
|
||||
state = Fraction;
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
--result.ptr;
|
||||
}
|
||||
break;
|
||||
|
||||
case Fraction:
|
||||
if (ch >= '0' and ch <= '9')
|
||||
{
|
||||
vi = 10 * vi + (ch - '0');
|
||||
|
||||
if (ch == '0')
|
||||
tz += 1;
|
||||
else
|
||||
{
|
||||
fl += tz + 1;
|
||||
tz = 0;
|
||||
}
|
||||
}
|
||||
else if (ch == 'e' or ch == 'E')
|
||||
state = ExponentSign;
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
--result.ptr;
|
||||
}
|
||||
break;
|
||||
|
||||
case ExponentSign:
|
||||
if (ch == '-')
|
||||
{
|
||||
exponent_sign = -1;
|
||||
state = Exponent;
|
||||
}
|
||||
else if (ch == '+')
|
||||
state = Exponent;
|
||||
else if (ch >= '0' and ch <= '9')
|
||||
{
|
||||
exponent = ch - '0';
|
||||
state = Exponent;
|
||||
}
|
||||
else
|
||||
result.ec = std::errc::invalid_argument;
|
||||
break;
|
||||
|
||||
case Exponent:
|
||||
if (ch >= '0' and ch <= '9')
|
||||
exponent = 10 * exponent + (ch - '0');
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
--result.ptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (not (bool)result.ec)
|
||||
{
|
||||
while (tz-- > 0)
|
||||
vi /= 10;
|
||||
|
||||
long double v = std::pow(10, -fl) * vi * sign;
|
||||
if (exponent != 0)
|
||||
v *= std::pow(10, exponent * exponent_sign);
|
||||
|
||||
if (std::isnan(v))
|
||||
result.ec = std::errc::invalid_argument;
|
||||
else if (std::abs(v) > std::numeric_limits<FloatType>::max())
|
||||
result.ec = std::errc::result_out_of_range;
|
||||
|
||||
value = static_cast<FloatType>(v);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief duplication of std::chars_format for deficient STL implementations
|
||||
enum class chars_format
|
||||
{
|
||||
scientific = 1,
|
||||
fixed = 2,
|
||||
// hex,
|
||||
general = fixed | scientific
|
||||
};
|
||||
|
||||
/// \brief a simplistic implementation of std::to_chars for old STL implementations
|
||||
template <typename FloatType, std::enable_if_t<std::is_floating_point_v<FloatType>, int> = 0>
|
||||
std::to_chars_result to_chars(char *first, char *last, FloatType &value, chars_format fmt)
|
||||
{
|
||||
int size = static_cast<int>(last - first);
|
||||
int r = 0;
|
||||
|
||||
switch (fmt)
|
||||
{
|
||||
case chars_format::scientific:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%le", value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%e", value);
|
||||
break;
|
||||
|
||||
case chars_format::fixed:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%lf", value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%f", value);
|
||||
break;
|
||||
|
||||
case chars_format::general:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%lg", value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%g", value);
|
||||
break;
|
||||
}
|
||||
|
||||
std::to_chars_result result;
|
||||
if (r < 0 or r >= size)
|
||||
result = { first, std::errc::value_too_large };
|
||||
else
|
||||
result = { first + r, std::errc() };
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief a simplistic implementation of std::to_chars for old STL implementations
|
||||
template <typename FloatType, std::enable_if_t<std::is_floating_point_v<FloatType>, int> = 0>
|
||||
std::to_chars_result to_chars(char *first, char *last, FloatType &value, chars_format fmt, int precision)
|
||||
{
|
||||
int size = static_cast<int>(last - first);
|
||||
int r = 0;
|
||||
|
||||
switch (fmt)
|
||||
{
|
||||
case chars_format::scientific:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%.*le", precision, value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%.*e", precision, value);
|
||||
break;
|
||||
|
||||
case chars_format::fixed:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%.*lf", precision, value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%.*f", precision, value);
|
||||
break;
|
||||
|
||||
case chars_format::general:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%.*lg", precision, value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%.*g", precision, value);
|
||||
break;
|
||||
}
|
||||
|
||||
std::to_chars_result result;
|
||||
if (r < 0 or r >= size)
|
||||
result = { first, std::errc::value_too_large };
|
||||
else
|
||||
result = { first + r, std::errc() };
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief class that uses our implementation of std::from_chars and std::to_chars
|
||||
template <typename T>
|
||||
struct my_charconv
|
||||
{
|
||||
/// @brief Simply call our version of std::from_chars
|
||||
static std::from_chars_result from_chars(const char *a, const char *b, T &d)
|
||||
{
|
||||
return cif::from_chars(a, b, d);
|
||||
}
|
||||
|
||||
/// @brief Simply call our version of std::to_chars
|
||||
static std::to_chars_result to_chars(char *first, char *last, T &value, chars_format fmt)
|
||||
{
|
||||
return cif::to_chars(first, last, value, fmt);
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief class that uses the STL implementation of std::from_chars and std::to_chars
|
||||
template <typename T>
|
||||
struct std_charconv
|
||||
{
|
||||
/// @brief Simply call std::from_chars
|
||||
static std::from_chars_result from_chars(const char *a, const char *b, T &d)
|
||||
{
|
||||
return std::from_chars(a, b, d);
|
||||
}
|
||||
|
||||
/// @brief Simply call std::to_chars
|
||||
static std::to_chars_result to_chars(char *first, char *last, T &value, chars_format fmt)
|
||||
{
|
||||
return std::to_chars(first, last, value, fmt);
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief helper to find a from_chars function
|
||||
template <typename T>
|
||||
using from_chars_function = decltype(std::from_chars(std::declval<const char *>(), std::declval<const char *>(), std::declval<T &>()));
|
||||
|
||||
/**
|
||||
* @brief Helper to select the best implementation of charconv based on availability of the
|
||||
* function in the std:: namespace
|
||||
*
|
||||
* @tparam T The type for which we want to find a from_chars/to_chars function
|
||||
*/
|
||||
template <typename T>
|
||||
using selected_charconv = typename std::conditional_t<std_experimental::is_detected_v<from_chars_function, T>, std_charconv<T>, my_charconv<T>>;
|
||||
|
||||
} // namespace cif
|
||||
370
include/cif++/utilities.hpp
Normal file
370
include/cif++/utilities.hpp
Normal file
@@ -0,0 +1,370 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/exports.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
#ifndef STDOUT_FILENO
|
||||
/// @brief For systems that lack this value
|
||||
#define STDOUT_FILENO 1
|
||||
#endif
|
||||
|
||||
#ifndef STDERR_FILENO
|
||||
/// @brief For systems that lack this value
|
||||
#define STDERR_FILENO 2
|
||||
#endif
|
||||
|
||||
#if _WIN32
|
||||
#include <io.h>
|
||||
#define isatty _isatty
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if _MSC_VER
|
||||
#pragma warning(disable : 4996) // unsafe function or variable (strcpy e.g.)
|
||||
#pragma warning(disable : 4068) // unknown pragma
|
||||
#pragma warning(disable : 4100) // unreferenced formal parameter
|
||||
#pragma warning(disable : 4101) // unreferenced local variable
|
||||
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING 1
|
||||
#endif
|
||||
|
||||
/** \file utilities.hpp
|
||||
*
|
||||
* This file contains code that is very generic in nature like a progress_bar
|
||||
* and classes you can use to colourise output text.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief The global variable VERBOSE contains the level of verbosity
|
||||
* requested. A value of 0 is normal, with some output on error conditions.
|
||||
* A value > 0 will result in more output, the higher the value, the more
|
||||
* output. A value < 0 will make the library silent, even in error
|
||||
* conditions.
|
||||
*/
|
||||
extern CIFPP_EXPORT int VERBOSE;
|
||||
|
||||
/// return the git 'build' number
|
||||
std::string get_version_nr();
|
||||
|
||||
/// return the width of the current output terminal, or 80 if it cannot be determined
|
||||
uint32_t get_terminal_width();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
namespace colour
|
||||
{
|
||||
/// @brief The defined colours
|
||||
enum colour_type
|
||||
{
|
||||
black = 0,
|
||||
red,
|
||||
green,
|
||||
yellow,
|
||||
blue,
|
||||
magenta,
|
||||
cyan,
|
||||
white,
|
||||
none = 9
|
||||
};
|
||||
|
||||
/// @brief The defined styles
|
||||
enum style_type
|
||||
{
|
||||
bold = 1,
|
||||
underlined = 4,
|
||||
blink = 5,
|
||||
inverse = 7,
|
||||
regular = 22,
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
/**
|
||||
* @brief Struct for delimited strings.
|
||||
*/
|
||||
struct coloured_string_t
|
||||
{
|
||||
/**
|
||||
* @brief Construct a new coloured string t object
|
||||
*/
|
||||
coloured_string_t(std::string_view s, colour_type fc, colour_type bc, style_type st)
|
||||
: m_str(s)
|
||||
, m_fore_colour(static_cast<int>(fc) + 30)
|
||||
, m_back_colour(static_cast<int>(bc) + 40)
|
||||
, m_style(static_cast<int>(st))
|
||||
{
|
||||
}
|
||||
|
||||
coloured_string_t &operator=(coloured_string_t &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Write out the string, either coloured or not
|
||||
*/
|
||||
template <typename char_type, typename traits_type>
|
||||
friend std::basic_ostream<char_type, traits_type> &operator<<(
|
||||
std::basic_ostream<char_type, traits_type> &os, const coloured_string_t &cs)
|
||||
{
|
||||
bool use_colour = false;
|
||||
|
||||
if (os.rdbuf() == std::cout.rdbuf() and isatty(STDOUT_FILENO))
|
||||
use_colour = true;
|
||||
else if (os.rdbuf() == std::cerr.rdbuf() and isatty(STDERR_FILENO))
|
||||
use_colour = true;
|
||||
|
||||
if (use_colour)
|
||||
{
|
||||
os << "\033[" << cs.m_fore_colour << ';' << cs.m_style << ';' << cs.m_back_colour << 'm'
|
||||
<< cs.m_str
|
||||
<< "\033[0m";
|
||||
}
|
||||
else
|
||||
os << cs.m_str;
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
/// @cond
|
||||
std::string_view m_str;
|
||||
int m_fore_colour, m_back_colour;
|
||||
int m_style;
|
||||
/// @endcond
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace colour
|
||||
|
||||
/**
|
||||
* @brief Manipulator for coloured strings.
|
||||
*
|
||||
* When writing out text to the terminal it is often useful to have
|
||||
* some of the text colourised. But only if the output is really a
|
||||
* terminal since colouring text is done using escape sequences
|
||||
* an if output is redirected to a file, these escape sequences end up
|
||||
* in the file making the real text less easy to read.
|
||||
*
|
||||
* The code presented here is rather basic. It mimics the std::quoted
|
||||
* manipulator in that it will colour a string with optionally
|
||||
* requested colours and text style.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* using namespace cif::colour;
|
||||
* std::cout << cif::coloured("Hello, world!", white, red, bold) << '\n';
|
||||
* @endcode
|
||||
* @param str String to quote.
|
||||
* @param fg Foreground (=text) colour to use
|
||||
* @param bg Background colour to use
|
||||
* @param st Text style to use
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
requires std::is_assignable_v<std::string_view, T>
|
||||
inline auto coloured(T str,
|
||||
colour::colour_type fg, colour::colour_type bg = colour::colour_type::none,
|
||||
colour::style_type st = colour::style_type::regular)
|
||||
{
|
||||
return colour::detail::coloured_string_t(str, fg, bg, st);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// A progress bar
|
||||
|
||||
/**
|
||||
* @brief A simple progress bar class for terminal based output
|
||||
*
|
||||
* Using a progress bar is very convenient for the end user when
|
||||
* you have long running code. It gives feed back on how fast an
|
||||
* operation is performed and may give an indication how long it
|
||||
* will take before it is finished.
|
||||
*
|
||||
* Using this cif::progress_bar implementation is straightforward:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* using namespace std::chrono_literals;
|
||||
*
|
||||
* cif::progress_bar pb(10, "counting to ten");
|
||||
*
|
||||
* for (int i = 1; i <= 10; ++i)
|
||||
* {
|
||||
* pb.consumed(1);
|
||||
* std::this_thread::sleep_for(1s);
|
||||
* }
|
||||
*
|
||||
* @endcode
|
||||
*
|
||||
* When the progress_bar is created, it first checks
|
||||
* to see if stdout is to a real TTY and if the VERBOSE
|
||||
* flag is not less than zero (quiet mode). If this passes
|
||||
* a thread is started that waits for updates.
|
||||
*
|
||||
* The first two seconds, nothing is written to the screen
|
||||
* so if the work is finished within those two seconds
|
||||
* the screen stays clean.
|
||||
*
|
||||
* After this time, a progress bar is printed that may look
|
||||
* like this:
|
||||
*
|
||||
* @code
|
||||
* step 3 ========================-------------------------------- 40% ⢁
|
||||
* @endcode
|
||||
*
|
||||
* The first characters contain the initial action name or
|
||||
* the message text if it was used afterwards.
|
||||
*
|
||||
* The thermometer is made up with '=' and '-' characters.
|
||||
*
|
||||
* A percentage is also shown and at the end there is a spinner
|
||||
* that gives feedback that the program is really still working.
|
||||
*
|
||||
* The progress bar is removed if the max has been reached
|
||||
* or if the progress bar is destructed. If any output has
|
||||
* been generated, the initial action is printed out along
|
||||
* with the total time spent.
|
||||
*/
|
||||
|
||||
class progress_bar
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new progress bar object
|
||||
*
|
||||
* Progress ranges from 0 (zero) to @a inMax
|
||||
*
|
||||
* The action in @a inAction is used for display
|
||||
*
|
||||
* @param inMax The maximum value
|
||||
* @param inAction The description of what is
|
||||
* going on
|
||||
*/
|
||||
|
||||
progress_bar(int64_t inMax, const std::string &inAction);
|
||||
|
||||
/**
|
||||
* @brief Destroy the progress bar object
|
||||
*
|
||||
*/
|
||||
~progress_bar();
|
||||
|
||||
/**
|
||||
* @brief Notify the progress bar that @a inConsumed
|
||||
* should be added to the internal progress counter
|
||||
*/
|
||||
void consumed(int64_t inConsumed); // consumed is relative
|
||||
|
||||
/**
|
||||
* @brief Notify the progress bar that the internal
|
||||
* progress counter should be updated to @a inProgress
|
||||
*/
|
||||
void progress(int64_t inProgress); // progress is absolute
|
||||
|
||||
/**
|
||||
* @brief Replace the action string in the progress bar
|
||||
* with @a inMessage
|
||||
*/
|
||||
void message(const std::string &inMessage);
|
||||
|
||||
private:
|
||||
progress_bar(const progress_bar &) = delete;
|
||||
progress_bar &operator=(const progress_bar &) = delete;
|
||||
|
||||
struct progress_bar_impl *m_impl;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Resources
|
||||
|
||||
/**
|
||||
* @brief Load a resource from disk or the compiled in resources
|
||||
*
|
||||
* @verbatim embed:rst
|
||||
.. note::
|
||||
|
||||
See the :doc:`documentation on resources </resources>` for more information.
|
||||
|
||||
@endverbatim
|
||||
*
|
||||
* @param name The named resource to load
|
||||
* @return std::unique_ptr<std::istream> A pointer to the std::istream or empty if not found
|
||||
*/
|
||||
|
||||
std::unique_ptr<std::istream> load_resource(std::filesystem::path name);
|
||||
|
||||
/**
|
||||
* @brief Add a file specified by @a dataFile as the data for resource @a name
|
||||
*
|
||||
* @verbatim embed:rst
|
||||
.. note::
|
||||
|
||||
See the :doc:`documentation on resources </resources>` for more information.
|
||||
|
||||
@endverbatim
|
||||
*
|
||||
* @param name The name of the resource to specify
|
||||
* @param dataFile Path to a file containing the data
|
||||
*/
|
||||
|
||||
void add_file_resource(const std::string &name, std::filesystem::path dataFile);
|
||||
|
||||
/**
|
||||
* @brief List all the file resources added with cif::add_file_resource.
|
||||
*
|
||||
* @param os The std::ostream to write the directories to
|
||||
*/
|
||||
|
||||
void list_file_resources(std::ostream &os);
|
||||
|
||||
/**
|
||||
* @brief Add a directory to the list of search directories. This list is
|
||||
* searched in a last-in-first-out order.
|
||||
*
|
||||
* @verbatim embed:rst
|
||||
.. note::
|
||||
|
||||
See the :doc:`documentation on resources </resources>` for more information.
|
||||
|
||||
@endverbatim
|
||||
*/
|
||||
|
||||
void add_data_directory(std::filesystem::path dataDir);
|
||||
|
||||
/**
|
||||
* @brief List all the data directories, for error reporting on missing resources.
|
||||
*
|
||||
* @param os The std::ostream to write the directories to
|
||||
*/
|
||||
|
||||
void list_data_directories(std::ostream &os);
|
||||
|
||||
} // namespace cif
|
||||
548
include/cif++/validate.hpp
Normal file
548
include/cif++/validate.hpp
Normal file
@@ -0,0 +1,548 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/category.hpp"
|
||||
#include "cif++/text.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
|
||||
/**
|
||||
* @file validate.hpp
|
||||
*
|
||||
* Support for validating mmCIF files based on a dictionary. These dictionaries
|
||||
* contain information about the categories and items therein, what they may
|
||||
* contain and how this should be formatted. There's also information on links
|
||||
* between parent and child categories.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
class category;
|
||||
struct category_validator;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// New: error_code
|
||||
|
||||
/**
|
||||
* @enum validation_error
|
||||
*
|
||||
* @brief A stronly typed class containing the error codes reported by @ref cif::validator and friends
|
||||
*/
|
||||
enum class validation_error
|
||||
{
|
||||
value_does_not_match_rx = 1, /**< The value of an item does not conform to the regular expression specified for it */
|
||||
value_is_not_in_enumeration_list, /**< The value of an item is not in the list of values allowed */
|
||||
not_a_known_primitive_type, /**< The type is not a known primitive type */
|
||||
undefined_category, /**< Category has no definition in the dictionary */
|
||||
unknown_item, /**< The item is not defined to be part of the category */
|
||||
incorrect_item_validator, /**< Incorrectly specified validator for item */
|
||||
missing_mandatory_items, /**< Missing mandatory items */
|
||||
missing_key_items, /**< An index could not be constructed due to missing key items */
|
||||
item_not_allowed_in_category, /**< Requested item allowed in category according to dictionary */
|
||||
empty_file, /**< The file contains no datablocks */
|
||||
empty_datablock, /**< The datablock contains no categories */
|
||||
empty_category, /**< The category is empty */
|
||||
not_valid_pdbx, /**< The file is not a valid PDBx file */
|
||||
};
|
||||
/**
|
||||
* @brief The implementation for @ref validation_category error messages
|
||||
*
|
||||
*/
|
||||
class validation_category_impl : public std::error_category
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief User friendly name
|
||||
*
|
||||
* @return const char*
|
||||
*/
|
||||
|
||||
const char *name() const noexcept override
|
||||
{
|
||||
return "cif::validation";
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Provide the error message as a string for the error code @a ev
|
||||
*
|
||||
* @param ev The error code
|
||||
* @return std::string
|
||||
*/
|
||||
|
||||
std::string message(int ev) const override
|
||||
{
|
||||
switch (static_cast<validation_error>(ev))
|
||||
{
|
||||
case validation_error::value_does_not_match_rx:
|
||||
return "Value in item does not match regular expression";
|
||||
case validation_error::value_is_not_in_enumeration_list:
|
||||
return "Value is not in the enumerated list of valid values";
|
||||
case validation_error::not_a_known_primitive_type:
|
||||
return "The type is not a known primitive type";
|
||||
case validation_error::undefined_category:
|
||||
return "Category has no definition in the dictionary";
|
||||
case validation_error::unknown_item:
|
||||
return "Item is not defined to be part of the category";
|
||||
case validation_error::incorrect_item_validator:
|
||||
return "Incorrectly specified validator for item";
|
||||
case validation_error::missing_mandatory_items:
|
||||
return "Missing mandatory items";
|
||||
case validation_error::missing_key_items:
|
||||
return "An index could not be constructed due to missing key items";
|
||||
case validation_error::item_not_allowed_in_category:
|
||||
return "Requested item not allowed in category according to dictionary";
|
||||
case validation_error::empty_file:
|
||||
return "The file contains no datablocks";
|
||||
case validation_error::empty_datablock:
|
||||
return "The datablock contains no categories";
|
||||
case validation_error::empty_category:
|
||||
return "The category is empty";
|
||||
case validation_error::not_valid_pdbx:
|
||||
return "The file is not a valid PDBx file";
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
return "unknown error code";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return whether two error codes are equivalent, always false in this case
|
||||
*
|
||||
*/
|
||||
|
||||
bool equivalent(const std::error_code & /*code*/, int /*condition*/) const noexcept override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Return the implementation for the validation_category
|
||||
*
|
||||
* @return std::error_category&
|
||||
*/
|
||||
inline std::error_category &validation_category()
|
||||
{
|
||||
static validation_category_impl instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
inline std::error_code make_error_code(validation_error e)
|
||||
{
|
||||
return std::error_code(static_cast<int>(e), validation_category());
|
||||
}
|
||||
|
||||
inline std::error_condition make_error_condition(validation_error e)
|
||||
{
|
||||
return std::error_condition(static_cast<int>(e), validation_category());
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class validation_exception : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
validation_exception(validation_error err)
|
||||
: validation_exception(make_error_code(err))
|
||||
{
|
||||
}
|
||||
|
||||
validation_exception(validation_error err, std::string_view category)
|
||||
: validation_exception(make_error_code(err), category)
|
||||
{
|
||||
}
|
||||
|
||||
validation_exception(validation_error err, std::string_view category, std::string_view item)
|
||||
: validation_exception(make_error_code(err), category, item)
|
||||
{
|
||||
}
|
||||
|
||||
validation_exception(std::error_code ec);
|
||||
|
||||
validation_exception(std::error_code ec, std::string_view category);
|
||||
|
||||
validation_exception(std::error_code ec, std::string_view category, std::string_view item);
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** @brief the primitive types known */
|
||||
enum class DDL_PrimitiveType
|
||||
{
|
||||
Char, ///< Text
|
||||
UChar, ///< Text that is compared ignoring the character case
|
||||
Numb ///< Nummeric values
|
||||
};
|
||||
|
||||
/// @brief Return the DDL_PrimitiveType encoded in @a s
|
||||
DDL_PrimitiveType map_to_primitive_type(std::string_view s);
|
||||
|
||||
/// @brief Return the DDL_PrimitiveType encoded in @a s, error reporting variant
|
||||
DDL_PrimitiveType map_to_primitive_type(std::string_view s, std::error_code &ec) noexcept;
|
||||
|
||||
struct regex_impl;
|
||||
|
||||
/**
|
||||
* @brief For each defined type in a dictionary a type_validator is created
|
||||
*
|
||||
* A type validator can check if the contents of an item are conforming the
|
||||
* specification. The check is done using regular expressions.
|
||||
*
|
||||
* A type_validator can also be used to compare two values that conform to
|
||||
* this type. Comparison is of course based on the primitive type.
|
||||
*
|
||||
*/
|
||||
struct type_validator
|
||||
{
|
||||
std::string m_name; ///< The name of the type
|
||||
DDL_PrimitiveType m_primitive_type; ///< The primitive_type of the type
|
||||
std::shared_ptr<regex_impl> m_rx; ///< The regular expression for the type
|
||||
|
||||
type_validator() = delete;
|
||||
|
||||
/// @brief Constructor
|
||||
type_validator(std::string_view name, DDL_PrimitiveType type, std::string_view rx);
|
||||
|
||||
/// @brief Copy constructor
|
||||
type_validator(const type_validator &tv);
|
||||
|
||||
/// @brief Move constructor
|
||||
type_validator(type_validator &&rhs)
|
||||
{
|
||||
swap(*this, rhs);
|
||||
}
|
||||
|
||||
/// @brief Move constructor
|
||||
type_validator &operator=(type_validator rhs)
|
||||
{
|
||||
swap(*this, rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// @brief Destructor
|
||||
~type_validator();
|
||||
|
||||
friend void swap(type_validator &a, type_validator &b)
|
||||
{
|
||||
std::swap(a.m_name, b.m_name);
|
||||
std::swap(a.m_primitive_type, b.m_primitive_type);
|
||||
std::swap(a.m_rx, b.m_rx);
|
||||
}
|
||||
|
||||
/// @brief Return the sorting order
|
||||
bool operator<(const type_validator &rhs) const
|
||||
{
|
||||
return icompare(m_name, rhs.m_name) < 0;
|
||||
}
|
||||
|
||||
/// @brief Compare the contents of @a a and @a b based on the
|
||||
/// primitive type of this type. A value of zero indicates the
|
||||
/// values are equal. Less than zero means @a a sorts before @a b
|
||||
/// and a value larger than zero likewise means the opposite
|
||||
int compare(std::string_view a, std::string_view b) const;
|
||||
};
|
||||
|
||||
/** @brief Item alias, items can be renamed over time
|
||||
*/
|
||||
|
||||
struct item_alias
|
||||
{
|
||||
item_alias(const std::string &alias_name, const std::string &dictionary, const std::string &version)
|
||||
: m_name(alias_name)
|
||||
, m_dict(dictionary)
|
||||
, m_vers(version)
|
||||
{
|
||||
}
|
||||
|
||||
item_alias(const item_alias &) = default;
|
||||
item_alias &operator=(const item_alias &) = default;
|
||||
|
||||
std::string m_name; ///< The alias_name
|
||||
std::string m_dict; ///< The dictionary in which it was known
|
||||
std::string m_vers; ///< The version of the dictionary
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief An item_validator binds a type_validator to an item in
|
||||
* a category along with other information found in the dictionary.
|
||||
*
|
||||
* mmCIF dictionaries may indicate an item is e.g. mandatory or
|
||||
* consists of a certain list of allowed values. Even default
|
||||
* values can be provided.
|
||||
*
|
||||
*/
|
||||
struct item_validator
|
||||
{
|
||||
std::string m_item_name; ///< The item name
|
||||
bool m_mandatory; ///< Flag indicating this item is mandatory
|
||||
const type_validator *m_type; ///< The type for this item
|
||||
cif::iset m_enums; ///< If filled, the set of allowed values
|
||||
std::string m_default; ///< If filled, a default value for this item
|
||||
std::string m_category; ///< The category this item_validator belongs to
|
||||
std::vector<item_alias> m_aliases; ///< The aliases for this item
|
||||
|
||||
/// @brief Compare based on the name
|
||||
bool operator<(const item_validator &rhs) const
|
||||
{
|
||||
return icompare(m_item_name, rhs.m_item_name) < 0;
|
||||
}
|
||||
|
||||
/// @brief Compare based on the name
|
||||
bool operator==(const item_validator &rhs) const
|
||||
{
|
||||
return iequals(m_item_name, rhs.m_item_name);
|
||||
}
|
||||
|
||||
/// @brief Validate the value in @a value for this item
|
||||
/// Will throw a std::system_error exception if it fails
|
||||
void operator()(std::string_view value) const;
|
||||
|
||||
/// @brief A more gentle version of value validation
|
||||
bool validate_value(std::string_view value, std::error_code &ec) const noexcept;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A validator for categories
|
||||
*
|
||||
* Categories can have a key, a set of items that in combination
|
||||
* should be unique.
|
||||
*/
|
||||
struct category_validator
|
||||
{
|
||||
std::string m_name; ///< The name of the category
|
||||
std::vector<std::string> m_keys; ///< The list of items that make up the key
|
||||
cif::iset m_groups; ///< The category groups this category belongs to
|
||||
cif::iset m_mandatory_items; ///< The mandatory items for this category
|
||||
std::set<item_validator> m_item_validators; ///< The item validators for the items in this category
|
||||
|
||||
/// @brief return true if this category sorts before @a rhs
|
||||
bool operator<(const category_validator &rhs) const
|
||||
{
|
||||
return icompare(m_name, rhs.m_name) < 0;
|
||||
}
|
||||
|
||||
/// @brief Add item_validator @a v to the list of item validators
|
||||
void add_item_validator(item_validator &&v);
|
||||
|
||||
/// @brief Return the item_validator for item @a item_name, may return nullptr
|
||||
const item_validator *get_validator_for_item(std::string_view item_name) const;
|
||||
|
||||
/// @brief Return the item_validator for an item that has as alias name @a item_name, may return nullptr
|
||||
const item_validator *get_validator_for_aliased_item(std::string_view item_name) const;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A validator for links between categories
|
||||
*
|
||||
* Links are defined as a set of pairs of item names in a
|
||||
* parent category and a corresponding item in a child
|
||||
* category. This means that the size of m_parent_keys
|
||||
* is always equal to the size of m_child_keys.
|
||||
*
|
||||
* Multiple links may be defined between two categories.
|
||||
*
|
||||
*/
|
||||
struct link_validator
|
||||
{
|
||||
int m_link_group_id; ///< The link group ID
|
||||
std::string m_parent_category; ///< The name of the parent category
|
||||
std::vector<std::string> m_parent_keys; ///< The items in the parent category making up the set of linked items
|
||||
std::string m_child_category; ///< The name of the child category
|
||||
std::vector<std::string> m_child_keys; ///< The items in the child category making up the set of linked items
|
||||
std::string m_link_group_label; ///< The group label assigned to this link
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The validator class combines all the link, category and item validator classes
|
||||
*
|
||||
*/
|
||||
class validator
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new validator object
|
||||
*
|
||||
* @param name The name of the underlying dictionary
|
||||
*/
|
||||
validator()
|
||||
: m_audit_conform("audit_conform")
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new validator object
|
||||
*
|
||||
* @param name The name of the underlying dictionary
|
||||
* @param is The data to parse
|
||||
*/
|
||||
validator(std::istream &is)
|
||||
: m_audit_conform("audit_conform")
|
||||
{
|
||||
parse(is);
|
||||
}
|
||||
|
||||
/// @brief destructor
|
||||
~validator() = default;
|
||||
|
||||
validator(const validator &rhs);
|
||||
|
||||
/// @brief move constructor
|
||||
validator(validator &&rhs)
|
||||
{
|
||||
swap(*this, rhs);
|
||||
}
|
||||
|
||||
validator &operator=(validator rhs)
|
||||
{
|
||||
swap(*this, rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend void swap(validator &a, validator &b) noexcept;
|
||||
|
||||
friend class dictionary_parser;
|
||||
friend class validator_factory;
|
||||
|
||||
/// @brief Parse dictionary in @a is and put content in this validator, optionally extending it
|
||||
/// @param is The stream containing a valid cif dictionary
|
||||
void parse(std::istream &is);
|
||||
|
||||
/// @brief Add type_validator @a v to the list of type validators
|
||||
void add_type_validator(type_validator &&v);
|
||||
|
||||
/// @brief Return the type validator for @a type_code, may return nullptr
|
||||
const type_validator *get_validator_for_type(std::string_view type_code) const;
|
||||
|
||||
/// @brief Add category_validator @a v to the list of category validators
|
||||
void add_category_validator(category_validator &&v);
|
||||
|
||||
/// @brief Return the category validator for @a category, may return nullptr
|
||||
const category_validator *get_validator_for_category(std::string_view category) const;
|
||||
|
||||
/// @brief Add link_validator @a v to the list of link validators
|
||||
void add_link_validator(link_validator &&v);
|
||||
|
||||
/// @brief Return the list of link validators for which the parent is @a category
|
||||
std::vector<const link_validator *> get_links_for_parent(std::string_view category) const;
|
||||
|
||||
/// @brief Return the list of link validators for which the child is @a category
|
||||
std::vector<const link_validator *> get_links_for_child(std::string_view category) const;
|
||||
|
||||
/// @brief Bottleneck function to report an error in validation
|
||||
void report_error(validation_error err, bool fatal = true) const
|
||||
{
|
||||
report_error(make_error_code(err), fatal);
|
||||
}
|
||||
|
||||
/// @brief Bottleneck function to report an error in validation
|
||||
void report_error(std::error_code ec, bool fatal = true) const;
|
||||
|
||||
/// @brief Bottleneck function to report an error in validation
|
||||
void report_error(validation_error err, std::string_view category,
|
||||
std::string_view item, bool fatal = true) const
|
||||
{
|
||||
report_error(make_error_code(err), category, item, fatal);
|
||||
}
|
||||
|
||||
/// @brief Bottleneck function to report an error in validation
|
||||
void report_error(std::error_code ec, std::string_view category,
|
||||
std::string_view item, bool fatal = true) const;
|
||||
|
||||
/// @brief Write out the audit_conform data for this validator
|
||||
/// @param audit_conform
|
||||
void fill_audit_conform(category &audit_conform) const;
|
||||
|
||||
/// @brief Return true if this validator matches @a audit_conform
|
||||
bool matches_audit_conform(const category &audit_conform) const;
|
||||
|
||||
/// @brief Add info
|
||||
void append_audit_conform(const std::string &name, const std::optional<std::string> &version);
|
||||
|
||||
private:
|
||||
// name is fully qualified here:
|
||||
item_validator *get_validator_for_item(std::string_view name) const;
|
||||
|
||||
category m_audit_conform;
|
||||
|
||||
bool m_strict = false;
|
||||
std::set<type_validator> m_type_validators;
|
||||
std::set<category_validator> m_category_validators;
|
||||
std::vector<link_validator> m_link_validators;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Validators are globally unique objects, use the validator_factory
|
||||
* class to construct them. This class is a singleton.
|
||||
*/
|
||||
|
||||
class validator_factory
|
||||
{
|
||||
public:
|
||||
/// @brief Return the singleton instance
|
||||
static validator_factory &instance();
|
||||
|
||||
/// @brief Return validator with info recorded in @a audit_conform
|
||||
const validator &get(const category &audit_conform);
|
||||
|
||||
/// @brief Return the single-file validator with name @a dictionary_name
|
||||
const validator &get(std::string_view dictionary_name);
|
||||
|
||||
/// @brief Return true if the version @a found is equal or higher than @a expected for dictionary @a name
|
||||
static bool check_version(std::string_view name, std::string_view expected, std::string_view found);
|
||||
|
||||
/// @brief Add validator @a v to the list of known validators
|
||||
const validator &add(validator &&v)
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
return m_validators.emplace_back(std::move(v));
|
||||
}
|
||||
|
||||
private:
|
||||
validator_factory() = default;
|
||||
|
||||
/// @brief Construct a new validator with name @a name from the data in @a is with at least version @a version if specified
|
||||
validator construct_validator(std::string_view name, std::optional<std::string> version);
|
||||
|
||||
std::mutex m_mutex;
|
||||
std::list<validator> m_validators;
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
@@ -1,12 +0,0 @@
|
||||
prefix=@prefix@
|
||||
exec_prefix=@exec_prefix@
|
||||
libdir=@libdir@
|
||||
includedir=@includedir@
|
||||
datalibdir=@datarootdir@/libcifpp
|
||||
|
||||
Name: libcifpp
|
||||
Description: C++ library for the manipulation of mmCIF files.
|
||||
Version: @PACKAGE_VERSION@
|
||||
|
||||
Libs: -L${libdir} -lcifpp @PRIVATE_LIBS@
|
||||
Cflags: -I${includedir} @PRIVATE_INC_DIRS@
|
||||
2807
rsrc/ccd-subset.cif
Normal file
2807
rsrc/ccd-subset.cif
Normal file
File diff suppressed because it is too large
Load Diff
150867
rsrc/mmcif_ma.dic
Normal file
150867
rsrc/mmcif_ma.dic
Normal file
File diff suppressed because it is too large
Load Diff
174427
rsrc/mmcif_pdbx.dic
Normal file
174427
rsrc/mmcif_pdbx.dic
Normal file
File diff suppressed because it is too large
Load Diff
147200
rsrc/mmcif_pdbx_v50.dic
147200
rsrc/mmcif_pdbx_v50.dic
File diff suppressed because it is too large
Load Diff
1318
src/AtomType.cpp
1318
src/AtomType.cpp
File diff suppressed because it is too large
Load Diff
627
src/BondMap.cpp
627
src/BondMap.cpp
@@ -1,627 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <mutex>
|
||||
|
||||
#include "cif++/Cif++.hpp"
|
||||
#include "cif++/Compound.hpp"
|
||||
#include "cif++/CifUtils.hpp"
|
||||
#include "cif++/BondMap.hpp"
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
union IDType
|
||||
{
|
||||
IDType() : id_n(0){}
|
||||
IDType(const IDType& rhs) : id_n(rhs.id_n) {}
|
||||
IDType(const std::string& s)
|
||||
: IDType()
|
||||
{
|
||||
assert(s.length() <= 4);
|
||||
if (s.length() > 4)
|
||||
throw BondMapException("Atom ID '" + s + "' is too long");
|
||||
std::copy(s.begin(), s.end(), id_s);
|
||||
}
|
||||
|
||||
IDType& operator=(const IDType& rhs)
|
||||
{
|
||||
id_n = rhs.id_n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
IDType& operator=(const std::string& s)
|
||||
{
|
||||
id_n = 0;
|
||||
assert(s.length() <= 4);
|
||||
if (s.length() > 4)
|
||||
throw BondMapException("Atom ID '" + s + "' is too long");
|
||||
std::copy(s.begin(), s.end(), id_s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator<(const IDType& rhs) const
|
||||
{
|
||||
return id_n < rhs.id_n;
|
||||
}
|
||||
|
||||
bool operator<=(const IDType& rhs) const
|
||||
{
|
||||
return id_n <= rhs.id_n;
|
||||
}
|
||||
|
||||
bool operator==(const IDType& rhs) const
|
||||
{
|
||||
return id_n == rhs.id_n;
|
||||
}
|
||||
|
||||
bool operator!=(const IDType& rhs) const
|
||||
{
|
||||
return id_n != rhs.id_n;
|
||||
}
|
||||
|
||||
char id_s[4];
|
||||
uint32_t id_n;
|
||||
};
|
||||
|
||||
static_assert(sizeof(IDType) == 4, "atom_id_type should be 4 bytes");
|
||||
}
|
||||
|
||||
// // --------------------------------------------------------------------
|
||||
|
||||
// void createBondInfoFile(const fs::path& components, const fs::path& infofile)
|
||||
// {
|
||||
// std::ofstream outfile(infofile.string() + ".tmp", std::ios::binary);
|
||||
// if (not outfile.is_open())
|
||||
// throw BondMapException("Could not create bond info file " + infofile.string() + ".tmp");
|
||||
|
||||
// cif::File infile(components);
|
||||
|
||||
// std::set<atom_id_type> atomIDs;
|
||||
// std::vector<atom_id_type> compoundIDs;
|
||||
|
||||
// for (auto& db: infile)
|
||||
// {
|
||||
// auto chem_comp_bond = db.get("chem_comp_bond");
|
||||
// if (not chem_comp_bond)
|
||||
// {
|
||||
// if (cif::VERBOSE > 1)
|
||||
// std::cerr << "Missing chem_comp_bond category in data block " << db.getName() << std::endl;
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// for (const auto& [atom_id_1, atom_id_2]: chem_comp_bond->rows<std::string,std::string>({"atom_id_1", "atom_id_2"}))
|
||||
// {
|
||||
// atomIDs.insert(atom_id_1);
|
||||
// atomIDs.insert(atom_id_2);
|
||||
// }
|
||||
|
||||
// compoundIDs.push_back({ db.getName() });
|
||||
// }
|
||||
|
||||
// if (cif::VERBOSE)
|
||||
// std::cout << "Number of unique atom names is " << atomIDs.size() << std::endl
|
||||
// << "Number of unique residue names is " << compoundIDs.size() << std::endl;
|
||||
|
||||
// CompoundBondInfoFileHeader header = {};
|
||||
// header.indexEntries = compoundIDs.size();
|
||||
// header.atomEntries = atomIDs.size();
|
||||
|
||||
// outfile << header;
|
||||
|
||||
// for (auto atomID: atomIDs)
|
||||
// outfile << atomID;
|
||||
|
||||
// auto dataOffset = outfile.tellp();
|
||||
|
||||
// std::vector<CompoundBondInfo> entries;
|
||||
// entries.reserve(compoundIDs.size());
|
||||
|
||||
// std::map<atom_id_type, uint16_t> atomIDMap;
|
||||
// for (auto& atomID: atomIDs)
|
||||
// atomIDMap[atomID] = atomIDMap.size();
|
||||
|
||||
// for (auto& db: infile)
|
||||
// {
|
||||
// auto chem_comp_bond = db.get("chem_comp_bond");
|
||||
// if (not chem_comp_bond)
|
||||
// continue;
|
||||
|
||||
// std::set<uint16_t> bondedAtoms;
|
||||
|
||||
// for (const auto& [atom_id_1, atom_id_2]: chem_comp_bond->rows<std::string,std::string>({"atom_id_1", "atom_id_2"}))
|
||||
// {
|
||||
// bondedAtoms.insert(atomIDMap[atom_id_1]);
|
||||
// bondedAtoms.insert(atomIDMap[atom_id_2]);
|
||||
// }
|
||||
|
||||
// std::map<uint16_t, int32_t> bondedAtomMap;
|
||||
// for (auto id: bondedAtoms)
|
||||
// bondedAtomMap[id] = static_cast<int32_t>(bondedAtomMap.size());
|
||||
|
||||
// CompoundBondInfo info = {
|
||||
// db.getName(),
|
||||
// static_cast<uint32_t>(bondedAtomMap.size()),
|
||||
// outfile.tellp() - dataOffset
|
||||
// };
|
||||
|
||||
// entries.push_back(info);
|
||||
|
||||
// // An now first write the array of atom ID's in this compound
|
||||
// for (uint16_t id: bondedAtoms)
|
||||
// write(outfile, id);
|
||||
|
||||
// // And then the symmetric matrix with bonds
|
||||
// size_t N = bondedAtoms.size();
|
||||
// size_t M = (N * (N - 1)) / 2;
|
||||
|
||||
// size_t K = M / 8;
|
||||
// if (M % 8)
|
||||
// K += 1;
|
||||
|
||||
// std::vector<uint8_t> m(K);
|
||||
|
||||
// for (const auto& [atom_id_1, atom_id_2]: chem_comp_bond->rows<std::string,std::string>({"atom_id_1", "atom_id_2"}))
|
||||
// {
|
||||
// auto a = bondedAtomMap[atomIDMap[atom_id_1]];
|
||||
// auto b = bondedAtomMap[atomIDMap[atom_id_2]];
|
||||
|
||||
// assert(a != b);
|
||||
// assert((int)b < (int)N);
|
||||
|
||||
// if (a > b)
|
||||
// std::swap(a, b);
|
||||
|
||||
// size_t ix = ((b - 1) * b) / 2 + a;
|
||||
// assert(ix < M);
|
||||
|
||||
// auto Bix = ix / 8;
|
||||
// auto bix = ix % 8;
|
||||
|
||||
// m[Bix] |= 1 << bix;
|
||||
// }
|
||||
|
||||
// outfile.write(reinterpret_cast<char*>(m.data()), m.size());
|
||||
// }
|
||||
|
||||
// header.dataSize = outfile.tellp() - dataOffset;
|
||||
|
||||
// std::sort(entries.begin(), entries.end(), [](CompoundBondInfo& a, CompoundBondInfo& b)
|
||||
// {
|
||||
// return a.id < b.id;
|
||||
// });
|
||||
|
||||
// for (auto& info: entries)
|
||||
// outfile << info;
|
||||
|
||||
// outfile.seekp(0);
|
||||
// outfile << header;
|
||||
|
||||
// // compress
|
||||
// outfile.close();
|
||||
|
||||
// std::ifstream in(infofile.string() + ".tmp", std::ios::binary);
|
||||
// std::ofstream out(infofile, std::ios::binary);
|
||||
|
||||
// {
|
||||
// io::filtering_stream<io::output> os;
|
||||
// os.push(io::gzip_compressor());
|
||||
// os.push(out);
|
||||
// io::copy(in, os);
|
||||
// }
|
||||
|
||||
// in.close();
|
||||
// out.close();
|
||||
|
||||
// fs::remove(infofile.string() + ".tmp");
|
||||
// }
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
struct CompoundBondInfo
|
||||
{
|
||||
IDType mID;
|
||||
std::set<std::tuple<uint32_t,uint32_t>> mBonded;
|
||||
|
||||
bool bonded(uint32_t a1, uint32_t a2) const
|
||||
{
|
||||
return mBonded.count({ a1, a2 }) > 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class CompoundBondMap
|
||||
{
|
||||
public:
|
||||
|
||||
static CompoundBondMap &instance()
|
||||
{
|
||||
static std::unique_ptr<CompoundBondMap> s_instance(new CompoundBondMap);
|
||||
return *s_instance;
|
||||
}
|
||||
|
||||
bool bonded(const std::string& compoundID, const std::string& atomID1, const std::string& atomID2);
|
||||
|
||||
private:
|
||||
|
||||
CompoundBondMap() {}
|
||||
|
||||
uint32_t getAtomID(const std::string& atomID)
|
||||
{
|
||||
IDType id(atomID);
|
||||
|
||||
uint32_t result;
|
||||
|
||||
auto i = mAtomIDIndex.find(id);
|
||||
if (i == mAtomIDIndex.end())
|
||||
{
|
||||
result = uint32_t(mAtomIDIndex.size());
|
||||
mAtomIDIndex[id] = result;
|
||||
}
|
||||
else
|
||||
result = i->second;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::map<IDType,uint32_t> mAtomIDIndex;
|
||||
std::vector<CompoundBondInfo> mCompounds;
|
||||
std::mutex mMutex;
|
||||
};
|
||||
|
||||
bool CompoundBondMap::bonded(const std::string &compoundID, const std::string& atomID1, const std::string& atomID2)
|
||||
{
|
||||
std::lock_guard lock(mMutex);
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
IDType id(compoundID);
|
||||
uint32_t a1 = getAtomID(atomID1);
|
||||
uint32_t a2 = getAtomID(atomID2);
|
||||
if (a1 > a2)
|
||||
std::swap(a1, a2);
|
||||
|
||||
for (auto &bi: mCompounds)
|
||||
{
|
||||
if (bi.mID != id)
|
||||
continue;
|
||||
|
||||
return bi.bonded(a1, a2);
|
||||
}
|
||||
|
||||
bool result = false;
|
||||
|
||||
// not found in our cache, calculate
|
||||
CompoundBondInfo bondInfo{ id };
|
||||
|
||||
auto compound = mmcif::CompoundFactory::instance().create(compoundID);
|
||||
if (not compound)
|
||||
std::cerr << "Missing compound bond info for " << compoundID << std::endl;
|
||||
else
|
||||
{
|
||||
for (auto &atom: compound->bonds())
|
||||
{
|
||||
uint32_t ca1 = getAtomID(atom.atomID[0]);
|
||||
uint32_t ca2 = getAtomID(atom.atomID[1]);
|
||||
if (ca1 > ca2)
|
||||
std::swap(ca1, ca2);
|
||||
|
||||
bondInfo.mBonded.insert({ca1, ca2});
|
||||
result = result or (a1 == ca1 and a2 == ca2);
|
||||
}
|
||||
}
|
||||
|
||||
mCompounds.push_back(bondInfo);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
BondMap::BondMap(const Structure& p)
|
||||
{
|
||||
auto& compoundBondInfo = CompoundBondMap::instance();
|
||||
|
||||
auto atoms = p.atoms();
|
||||
dim = uint32_t(atoms.size());
|
||||
|
||||
// bond = std::vector<bool>(dim * (dim - 1), false);
|
||||
|
||||
for (auto& atom: atoms)
|
||||
index[atom.id()] = uint32_t(index.size());
|
||||
|
||||
auto bindAtoms = [this](const std::string& a, const std::string& b)
|
||||
{
|
||||
uint32_t ixa = index[a];
|
||||
uint32_t ixb = index[b];
|
||||
|
||||
bond.insert(key(ixa, ixb));
|
||||
};
|
||||
|
||||
auto linkAtoms = [this,&bindAtoms](const std::string& a, const std::string& b)
|
||||
{
|
||||
bindAtoms(a, b);
|
||||
|
||||
link[a].insert(b);
|
||||
link[b].insert(a);
|
||||
};
|
||||
|
||||
cif::Datablock& db = p.getFile().data();
|
||||
|
||||
// collect all compounds first
|
||||
std::set<std::string> compounds;
|
||||
for (auto c: db["chem_comp"])
|
||||
compounds.insert(c["id"].as<std::string>());
|
||||
|
||||
// make sure we also have all residues in the polyseq
|
||||
for (auto m: db["entity_poly_seq"])
|
||||
{
|
||||
std::string c = m["mon_id"].as<std::string>();
|
||||
if (compounds.count(c))
|
||||
continue;
|
||||
|
||||
if (cif::VERBOSE > 1)
|
||||
std::cerr << "Warning: mon_id " << c << " is missing in the chem_comp category" << std::endl;
|
||||
compounds.insert(c);
|
||||
}
|
||||
|
||||
cif::Progress progress(compounds.size(), "Creating bond map");
|
||||
|
||||
// some helper indices to speed things up a bit
|
||||
std::map<std::tuple<std::string,int,std::string>,std::string> atomMapByAsymSeqAndAtom;
|
||||
for (auto& a: p.atoms())
|
||||
{
|
||||
auto key = make_tuple(a.labelAsymID(), a.labelSeqID(), a.labelAtomID());
|
||||
atomMapByAsymSeqAndAtom[key] = a.id();
|
||||
}
|
||||
|
||||
// first link all residues in a polyseq
|
||||
|
||||
std::string lastAsymID;
|
||||
int lastSeqID = 0;
|
||||
for (auto r: db["pdbx_poly_seq_scheme"])
|
||||
{
|
||||
std::string asymID;
|
||||
int seqID;
|
||||
|
||||
cif::tie(asymID, seqID) = r.get("asym_id", "seq_id");
|
||||
|
||||
if (asymID != lastAsymID) // first in a new sequece
|
||||
{
|
||||
lastAsymID = asymID;
|
||||
lastSeqID = seqID;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto c = atomMapByAsymSeqAndAtom[make_tuple(asymID, lastSeqID, "C")];
|
||||
auto n = atomMapByAsymSeqAndAtom[make_tuple(asymID, seqID, "N")];
|
||||
|
||||
if (not (c.empty() or n.empty()))
|
||||
bindAtoms(c, n);
|
||||
|
||||
lastSeqID = seqID;
|
||||
}
|
||||
|
||||
for (auto l: db["struct_conn"])
|
||||
{
|
||||
std::string asym1, asym2, atomId1, atomId2;
|
||||
int seqId1 = 0, seqId2 = 0;
|
||||
cif::tie(asym1, asym2, atomId1, atomId2, seqId1, seqId2) =
|
||||
l.get("ptnr1_label_asym_id", "ptnr2_label_asym_id",
|
||||
"ptnr1_label_atom_id", "ptnr2_label_atom_id",
|
||||
"ptnr1_label_seq_id", "ptnr2_label_seq_id");
|
||||
|
||||
std::string a = atomMapByAsymSeqAndAtom[make_tuple(asym1, seqId1, atomId1)];
|
||||
std::string b = atomMapByAsymSeqAndAtom[make_tuple(asym2, seqId2, atomId2)];
|
||||
|
||||
if (not (a.empty() or b.empty()))
|
||||
linkAtoms(a, b);
|
||||
}
|
||||
|
||||
// then link all atoms in the compounds
|
||||
|
||||
for (auto c: compounds)
|
||||
{
|
||||
if (c == "HOH" or c == "H2O" or c == "WAT")
|
||||
{
|
||||
if (cif::VERBOSE)
|
||||
std::cerr << "skipping water in bond map calculation" << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto bonded = [c, &compoundBondInfo](const Atom& a, const Atom& b)
|
||||
{
|
||||
auto label_a = a.labelAtomID();
|
||||
auto label_b = b.labelAtomID();
|
||||
|
||||
return compoundBondInfo.bonded(c, label_a, label_b);
|
||||
};
|
||||
|
||||
// loop over poly_seq_scheme
|
||||
for (auto r: db["pdbx_poly_seq_scheme"].find(cif::Key("mon_id") == c))
|
||||
{
|
||||
std::string asymID;
|
||||
int seqID;
|
||||
cif::tie(asymID, seqID) = r.get("asym_id", "seq_id");
|
||||
|
||||
std::vector<Atom> rAtoms;
|
||||
copy_if(atoms.begin(), atoms.end(), back_inserter(rAtoms),
|
||||
[&](auto& a) { return a.labelAsymID() == asymID and a.labelSeqID() == seqID; });
|
||||
|
||||
for (uint32_t i = 0; i + 1 < rAtoms.size(); ++i)
|
||||
{
|
||||
for (uint32_t j = i + 1; j < rAtoms.size(); ++j)
|
||||
{
|
||||
if (bonded(rAtoms[i], rAtoms[j]))
|
||||
bindAtoms(rAtoms[i].id(), rAtoms[j].id());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// loop over pdbx_nonpoly_scheme
|
||||
for (auto r: db["pdbx_nonpoly_scheme"].find(cif::Key("mon_id") == c))
|
||||
{
|
||||
std::string asymID;
|
||||
cif::tie(asymID) = r.get("asym_id");
|
||||
|
||||
std::vector<Atom> rAtoms;
|
||||
copy_if(atoms.begin(), atoms.end(), back_inserter(rAtoms),
|
||||
[&](auto& a) { return a.labelAsymID() == asymID; });
|
||||
|
||||
for (uint32_t i = 0; i + 1 < rAtoms.size(); ++i)
|
||||
{
|
||||
for (uint32_t j = i + 1; j < rAtoms.size(); ++j)
|
||||
{
|
||||
if (bonded(rAtoms[i], rAtoms[j]))
|
||||
{
|
||||
uint32_t ixa = index[rAtoms[i].id()];
|
||||
uint32_t ixb = index[rAtoms[j].id()];
|
||||
|
||||
bond.insert(key(ixa, ixb));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// loop over pdbx_branch_scheme
|
||||
for (auto r: db["pdbx_branch_scheme"].find(cif::Key("mon_id") == c))
|
||||
{
|
||||
std::string asymID;
|
||||
cif::tie(asymID) = r.get("asym_id");
|
||||
|
||||
std::vector<Atom> rAtoms;
|
||||
copy_if(atoms.begin(), atoms.end(), back_inserter(rAtoms),
|
||||
[&](auto& a) { return a.labelAsymID() == asymID; });
|
||||
|
||||
for (uint32_t i = 0; i + 1 < rAtoms.size(); ++i)
|
||||
{
|
||||
for (uint32_t j = i + 1; j < rAtoms.size(); ++j)
|
||||
{
|
||||
if (bonded(rAtoms[i], rAtoms[j]))
|
||||
{
|
||||
uint32_t ixa = index[rAtoms[i].id()];
|
||||
uint32_t ixb = index[rAtoms[j].id()];
|
||||
|
||||
bond.insert(key(ixa, ixb));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// start by creating an index for single bonds
|
||||
|
||||
std::multimap<uint32_t,uint32_t> b1_2;
|
||||
for (auto& bk: bond)
|
||||
{
|
||||
uint32_t a, b;
|
||||
std::tie(a, b) = dekey(bk);
|
||||
|
||||
b1_2.insert({ a, b });
|
||||
b1_2.insert({ b, a });
|
||||
}
|
||||
|
||||
std::multimap<uint32_t,uint32_t> b1_3;
|
||||
for (uint32_t i = 0; i < dim; ++i)
|
||||
{
|
||||
auto a = b1_2.equal_range(i);
|
||||
|
||||
std::vector<uint32_t> s;
|
||||
for (auto j = a.first; j != a.second; ++j)
|
||||
s.push_back(j->second);
|
||||
|
||||
for (size_t si1 = 0; si1 + 1 < s.size(); ++si1)
|
||||
{
|
||||
for (size_t si2 = si1 + 1; si2 < s.size(); ++si2)
|
||||
{
|
||||
uint32_t x = s[si1];
|
||||
uint32_t y = s[si2];
|
||||
|
||||
if (isBonded(x, y))
|
||||
continue;
|
||||
|
||||
b1_3.insert({ x, y });
|
||||
b1_3.insert({ y, x });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < dim; ++i)
|
||||
{
|
||||
auto a1 = b1_2.equal_range(i);
|
||||
auto a2 = b1_3.equal_range(i);
|
||||
|
||||
for (auto ai1 = a1.first; ai1 != a1.second; ++ai1)
|
||||
{
|
||||
for (auto ai2 = a2.first; ai2 != a2.second; ++ai2)
|
||||
{
|
||||
uint32_t b1 = ai1->second;
|
||||
uint32_t b2 = ai2->second;
|
||||
|
||||
if (isBonded(b1, b2))
|
||||
continue;
|
||||
|
||||
bond_1_4.insert(key(b1, b2));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> BondMap::linked(const Atom& a) const
|
||||
{
|
||||
auto i = link.find(a.id());
|
||||
|
||||
std::vector<std::string> result;
|
||||
|
||||
if (i != link.end())
|
||||
result = std::vector<std::string>(i->second.begin(), i->second.end());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> BondMap::atomIDsForCompound(const std::string& compoundID)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
|
||||
auto* compound = mmcif::CompoundFactory::instance().create(compoundID);
|
||||
|
||||
if (compound == nullptr)
|
||||
throw BondMapException("Missing bond information for compound " + compoundID);
|
||||
|
||||
for (auto& compAtom: compound->atoms())
|
||||
result.push_back(compAtom.id);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
3696
src/Cif++.cpp
3696
src/Cif++.cpp
File diff suppressed because it is too large
Load Diff
3981
src/Cif2PDB.cpp
3981
src/Cif2PDB.cpp
File diff suppressed because it is too large
Load Diff
1326
src/CifParser.cpp
1326
src/CifParser.cpp
File diff suppressed because it is too large
Load Diff
1300
src/CifUtils.cpp
1300
src/CifUtils.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,351 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#include "cif++/Cif++.hpp"
|
||||
#include "cif++/CifParser.hpp"
|
||||
#include "cif++/CifValidator.hpp"
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
extern int VERBOSE;
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
ValidationError::ValidationError(const std::string& msg)
|
||||
: mMsg(msg)
|
||||
{
|
||||
}
|
||||
|
||||
ValidationError::ValidationError(const std::string& cat, const std::string& item, const std::string& msg)
|
||||
: mMsg("When validating _" + cat + '.' + item + ": " + msg)
|
||||
{
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
DDL_PrimitiveType mapToPrimitiveType(const std::string& s)
|
||||
{
|
||||
DDL_PrimitiveType result;
|
||||
if (iequals(s, "char"))
|
||||
result = DDL_PrimitiveType::Char;
|
||||
else if (iequals(s, "uchar"))
|
||||
result = DDL_PrimitiveType::UChar;
|
||||
else if (iequals(s, "numb"))
|
||||
result = DDL_PrimitiveType::Numb;
|
||||
else
|
||||
throw ValidationError("Not a known primitive type");
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
int ValidateType::compare(const char* a, const char* b) const
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (*a == 0)
|
||||
result = *b == 0 ? 0 : -1;
|
||||
else if (*b == 0)
|
||||
result = *a == 0 ? 0 : +1;
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (mPrimitiveType)
|
||||
{
|
||||
case DDL_PrimitiveType::Numb:
|
||||
{
|
||||
double da = strtod(a, nullptr);
|
||||
double db = strtod(b, nullptr);
|
||||
|
||||
auto d = da - db;
|
||||
if (std::abs(d) > std::numeric_limits<double>::epsilon())
|
||||
{
|
||||
if (d > 0)
|
||||
result = 1;
|
||||
else if (d < 0)
|
||||
result = -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case DDL_PrimitiveType::UChar:
|
||||
case DDL_PrimitiveType::Char:
|
||||
{
|
||||
// CIF is guaranteed to have ascii only, therefore this primitive code will do
|
||||
// also, we're collapsing spaces
|
||||
|
||||
auto ai = a, bi = b;
|
||||
for (;;)
|
||||
{
|
||||
if (*ai == 0)
|
||||
{
|
||||
if (*bi != 0)
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
else if (*bi == 0)
|
||||
{
|
||||
result = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
char ca = *ai;
|
||||
char cb = *bi;
|
||||
|
||||
if (mPrimitiveType == DDL_PrimitiveType::UChar)
|
||||
{
|
||||
ca = tolower(ca);
|
||||
cb = tolower(cb);
|
||||
}
|
||||
|
||||
result = ca - cb;
|
||||
|
||||
if (result != 0)
|
||||
break;
|
||||
|
||||
if (ca == ' ')
|
||||
{
|
||||
while (ai[1] == ' ')
|
||||
++ai;
|
||||
while (bi[1] == ' ')
|
||||
++bi;
|
||||
}
|
||||
|
||||
++ai;
|
||||
++bi;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::invalid_argument& ex)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
//void ValidateItem::addLinked(ValidateItem* parent, const std::string& parentItem, const std::string& childItem)
|
||||
//{
|
||||
//// if (mParent != nullptr and VERBOSE)
|
||||
//// cerr << "replacing parent in " << mCategory->mName << " from " << mParent->mCategory->mName << " to " << parent->mCategory->mName << endl;
|
||||
//// mParent = parent;
|
||||
//
|
||||
// if (mType == nullptr and parent != nullptr)
|
||||
// mType = parent->mType;
|
||||
//
|
||||
// if (parent != nullptr)
|
||||
// {
|
||||
// mLinked.push_back({parent, parentItem, childItem});
|
||||
//
|
||||
// parent->mChildren.insert(this);
|
||||
////
|
||||
//// if (mCategory->mKeys == std::vector<std::string>{mTag})
|
||||
//// parent->mForeignKeys.insert(this);
|
||||
// }
|
||||
//}
|
||||
|
||||
void ValidateItem::operator()(std::string value) const
|
||||
{
|
||||
if (not value.empty() and value != "?" and value != ".")
|
||||
{
|
||||
if (mType != nullptr and not regex_match(value, mType->mRx))
|
||||
throw ValidationError(mCategory->mName, mTag, "Value '" + value + "' does not match type expression for type " + mType->mName);
|
||||
|
||||
if (not mEnums.empty())
|
||||
{
|
||||
if (mEnums.count(value) == 0)
|
||||
throw ValidationError(mCategory->mName, mTag, "Value '" + value + "' is not in the list of allowed values");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
void ValidateCategory::addItemValidator(ValidateItem&& v)
|
||||
{
|
||||
if (v.mMandatory)
|
||||
mMandatoryFields.insert(v.mTag);
|
||||
|
||||
v.mCategory = this;
|
||||
|
||||
auto r = mItemValidators.insert(std::move(v));
|
||||
if (not r.second and VERBOSE >= 4)
|
||||
std::cout << "Could not add validator for item " << v.mTag << " to category " << mName << std::endl;
|
||||
}
|
||||
|
||||
const ValidateItem* ValidateCategory::getValidatorForItem(std::string tag) const
|
||||
{
|
||||
const ValidateItem* result = nullptr;
|
||||
auto i = mItemValidators.find(ValidateItem{tag});
|
||||
if (i != mItemValidators.end())
|
||||
result = &*i;
|
||||
else if (VERBOSE > 4)
|
||||
std::cout << "No validator for tag " << tag << std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
Validator::Validator()
|
||||
{
|
||||
}
|
||||
|
||||
Validator::~Validator()
|
||||
{
|
||||
}
|
||||
|
||||
void Validator::addTypeValidator(ValidateType&& v)
|
||||
{
|
||||
auto r = mTypeValidators.insert(std::move(v));
|
||||
if (not r.second and VERBOSE > 4)
|
||||
std::cout << "Could not add validator for type " << v.mName << std::endl;
|
||||
}
|
||||
|
||||
const ValidateType* Validator::getValidatorForType(std::string typeCode) const
|
||||
{
|
||||
const ValidateType* result = nullptr;
|
||||
|
||||
auto i = mTypeValidators.find(ValidateType{ typeCode, DDL_PrimitiveType::Char, boost::regex() });
|
||||
if (i != mTypeValidators.end())
|
||||
result = &*i;
|
||||
else if (VERBOSE > 4)
|
||||
std::cout << "No validator for type " << typeCode << std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
void Validator::addCategoryValidator(ValidateCategory&& v)
|
||||
{
|
||||
auto r = mCategoryValidators.insert(std::move(v));
|
||||
if (not r.second and VERBOSE > 4)
|
||||
std::cout << "Could not add validator for category " << v.mName << std::endl;
|
||||
}
|
||||
|
||||
const ValidateCategory* Validator::getValidatorForCategory(std::string category) const
|
||||
{
|
||||
const ValidateCategory* result = nullptr;
|
||||
auto i = mCategoryValidators.find(ValidateCategory{category});
|
||||
if (i != mCategoryValidators.end())
|
||||
result = &*i;
|
||||
else if (VERBOSE > 4)
|
||||
std::cout << "No validator for category " << category << std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
ValidateItem* Validator::getValidatorForItem(std::string tag) const
|
||||
{
|
||||
ValidateItem* result = nullptr;
|
||||
|
||||
std::string cat, item;
|
||||
std::tie(cat, item) = splitTagName(tag);
|
||||
|
||||
auto* cv = getValidatorForCategory(cat);
|
||||
if (cv != nullptr)
|
||||
result = const_cast<ValidateItem*>(cv->getValidatorForItem(item));
|
||||
|
||||
if (result == nullptr and VERBOSE > 4)
|
||||
std::cout << "No validator for item " << tag << std::endl;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Validator::addLinkValidator(ValidateLink&& v)
|
||||
{
|
||||
assert(v.mParentKeys.size() == v.mChildKeys.size());
|
||||
if (v.mParentKeys.size() != v.mChildKeys.size())
|
||||
throw std::runtime_error("unequal number of keys for parent and child in link");
|
||||
|
||||
auto pcv = getValidatorForCategory(v.mParentCategory);
|
||||
auto ccv = getValidatorForCategory(v.mChildCategory);
|
||||
|
||||
if (pcv == nullptr)
|
||||
throw std::runtime_error("unknown parent category " + v.mParentCategory);
|
||||
|
||||
if (ccv == nullptr)
|
||||
throw std::runtime_error("unknown child category " + v.mChildCategory);
|
||||
|
||||
for (size_t i = 0; i < v.mParentKeys.size(); ++i)
|
||||
{
|
||||
auto piv = pcv->getValidatorForItem(v.mParentKeys[i]);
|
||||
|
||||
if (piv == nullptr)
|
||||
throw std::runtime_error("unknown parent tag _" + v.mParentCategory + '.' + v.mParentKeys[i]);
|
||||
|
||||
auto civ = ccv->getValidatorForItem(v.mChildKeys[i]);
|
||||
if (civ == nullptr)
|
||||
throw std::runtime_error("unknown child tag _" + v.mChildCategory + '.' + v.mChildKeys[i]);
|
||||
|
||||
if (civ->mType == nullptr and piv->mType != nullptr)
|
||||
const_cast<ValidateItem*>(civ)->mType = piv->mType;
|
||||
}
|
||||
|
||||
mLinkValidators.emplace_back(std::move(v));
|
||||
}
|
||||
|
||||
std::vector<const ValidateLink*> Validator::getLinksForParent(const std::string& category) const
|
||||
{
|
||||
std::vector<const ValidateLink*> result;
|
||||
|
||||
for (auto& l: mLinkValidators)
|
||||
{
|
||||
if (l.mParentCategory == category)
|
||||
result.push_back(&l);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<const ValidateLink*> Validator::getLinksForChild(const std::string& category) const
|
||||
{
|
||||
std::vector<const ValidateLink*> result;
|
||||
|
||||
for (auto& l: mLinkValidators)
|
||||
{
|
||||
if (l.mChildCategory == category)
|
||||
result.push_back(&l);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Validator::reportError(const std::string& msg, bool fatal)
|
||||
{
|
||||
if (mStrict or fatal)
|
||||
throw ValidationError(msg);
|
||||
else if (VERBOSE)
|
||||
std::cerr << msg << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
745
src/Compound.cpp
745
src/Compound.cpp
@@ -1,745 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <numeric>
|
||||
#include <shared_mutex>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include "cif++/Cif++.hpp"
|
||||
#include "cif++/CifParser.hpp"
|
||||
#include "cif++/CifUtils.hpp"
|
||||
#include "cif++/Compound.hpp"
|
||||
#include "cif++/Point.hpp"
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
std::string to_string(BondType bondType)
|
||||
{
|
||||
switch (bondType)
|
||||
{
|
||||
case BondType::sing: return "sing";
|
||||
case BondType::doub: return "doub";
|
||||
case BondType::trip: return "trip";
|
||||
case BondType::quad: return "quad";
|
||||
case BondType::arom: return "arom";
|
||||
case BondType::poly: return "poly";
|
||||
case BondType::delo: return "delo";
|
||||
case BondType::pi: return "pi";
|
||||
}
|
||||
throw std::invalid_argument("Invalid bondType");
|
||||
}
|
||||
|
||||
BondType from_string(const std::string &bondType)
|
||||
{
|
||||
if (cif::iequals(bondType, "sing"))
|
||||
return BondType::sing;
|
||||
if (cif::iequals(bondType, "doub"))
|
||||
return BondType::doub;
|
||||
if (cif::iequals(bondType, "trip"))
|
||||
return BondType::trip;
|
||||
if (cif::iequals(bondType, "quad"))
|
||||
return BondType::quad;
|
||||
if (cif::iequals(bondType, "arom"))
|
||||
return BondType::arom;
|
||||
if (cif::iequals(bondType, "poly"))
|
||||
return BondType::poly;
|
||||
if (cif::iequals(bondType, "delo"))
|
||||
return BondType::delo;
|
||||
if (cif::iequals(bondType, "pi"))
|
||||
return BondType::pi;
|
||||
throw std::invalid_argument("Invalid bondType: " + bondType);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Compound helper classes
|
||||
|
||||
struct CompoundAtomLess
|
||||
{
|
||||
bool operator()(const CompoundAtom &a, const CompoundAtom &b) const
|
||||
{
|
||||
int d = a.id.compare(b.id);
|
||||
if (d == 0)
|
||||
d = a.typeSymbol - b.typeSymbol;
|
||||
return d < 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct CompoundBondLess
|
||||
{
|
||||
bool operator()(const CompoundBond &a, const CompoundBond &b) const
|
||||
{
|
||||
int d = a.atomID[0].compare(b.atomID[0]);
|
||||
if (d == 0)
|
||||
d = a.atomID[1].compare(b.atomID[1]);
|
||||
if (d == 0)
|
||||
d = static_cast<int>(a.type) - static_cast<int>(b.type);
|
||||
return d < 0;
|
||||
}
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Compound
|
||||
|
||||
Compound::Compound(cif::Datablock &db)
|
||||
{
|
||||
auto &chemComp = db["chem_comp"];
|
||||
|
||||
if (chemComp.size() != 1)
|
||||
throw std::runtime_error("Invalid compound file, chem_comp should contain a single row");
|
||||
|
||||
cif::tie(mID, mName, mType, mFormula, mFormulaWeight, mFormalCharge) =
|
||||
chemComp.front().get("id", "name", "type", "formula", "formula_weight", "pdbx_formal_charge");
|
||||
|
||||
auto &chemCompAtom = db["chem_comp_atom"];
|
||||
for (auto row : chemCompAtom)
|
||||
{
|
||||
CompoundAtom atom;
|
||||
std::string typeSymbol;
|
||||
cif::tie(atom.id, typeSymbol, atom.charge, atom.aromatic, atom.leavingAtom, atom.stereoConfig, atom.x, atom.y, atom.z) =
|
||||
row.get("atom_id", "type_symbol", "charge", "pdbx_aromatic_flag", "pdbx_leaving_atom_flag", "pdbx_stereo_config",
|
||||
"model_Cartn_x", "model_Cartn_y", "model_Cartn_z");
|
||||
atom.typeSymbol = AtomTypeTraits(typeSymbol).type();
|
||||
mAtoms.push_back(std::move(atom));
|
||||
}
|
||||
|
||||
auto &chemCompBond = db["chem_comp_bond"];
|
||||
for (auto row : chemCompBond)
|
||||
{
|
||||
CompoundBond bond;
|
||||
std::string valueOrder;
|
||||
cif::tie(bond.atomID[0], bond.atomID[1], valueOrder, bond.aromatic, bond.stereoConfig) = row.get("atom_id_1", "atom_id_2", "value_order", "pdbx_aromatic_flag", "pdbx_stereo_config");
|
||||
bond.type = from_string(valueOrder);
|
||||
mBonds.push_back(std::move(bond));
|
||||
}
|
||||
}
|
||||
|
||||
Compound::Compound(cif::Datablock &db, const std::string &id, const std::string &name, const std::string &type)
|
||||
: mID(id)
|
||||
, mName(name)
|
||||
, mType(type)
|
||||
{
|
||||
auto &chemCompAtom = db["chem_comp_atom"];
|
||||
for (auto row : chemCompAtom)
|
||||
{
|
||||
CompoundAtom atom;
|
||||
std::string typeSymbol;
|
||||
cif::tie(atom.id, typeSymbol, atom.charge, atom.x, atom.y, atom.z) =
|
||||
row.get("atom_id", "type_symbol", "charge", "x", "y", "z");
|
||||
atom.typeSymbol = AtomTypeTraits(typeSymbol).type();
|
||||
|
||||
mFormalCharge += atom.charge;
|
||||
mFormulaWeight += AtomTypeTraits(atom.typeSymbol).weight();
|
||||
|
||||
mAtoms.push_back(std::move(atom));
|
||||
}
|
||||
|
||||
auto &chemCompBond = db["chem_comp_bond"];
|
||||
for (auto row : chemCompBond)
|
||||
{
|
||||
CompoundBond bond;
|
||||
std::string btype;
|
||||
cif::tie(bond.atomID[0], bond.atomID[1], btype, bond.aromatic) = row.get("atom_id_1", "atom_id_2", "type", "aromatic");
|
||||
|
||||
using cif::iequals;
|
||||
|
||||
if (iequals(btype, "single"))
|
||||
bond.type = BondType::sing;
|
||||
else if (iequals(btype, "double"))
|
||||
bond.type = BondType::doub;
|
||||
else if (iequals(btype, "triple"))
|
||||
bond.type = BondType::trip;
|
||||
else if (iequals(btype, "deloc") or iequals(btype, "aromat") or iequals(btype, "aromatic"))
|
||||
bond.type = BondType::delo;
|
||||
else
|
||||
{
|
||||
if (cif::VERBOSE)
|
||||
std::cerr << "Unimplemented chem_comp_bond.type " << btype << " in " << id << std::endl;
|
||||
bond.type = BondType::sing;
|
||||
}
|
||||
mBonds.push_back(std::move(bond));
|
||||
}
|
||||
}
|
||||
|
||||
CompoundAtom Compound::getAtomByID(const std::string &atomID) const
|
||||
{
|
||||
CompoundAtom result = {};
|
||||
for (auto &a : mAtoms)
|
||||
{
|
||||
if (a.id == atomID)
|
||||
{
|
||||
result = a;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (result.id != atomID)
|
||||
throw std::out_of_range("No atom " + atomID + " in Compound " + mID);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Compound::atomsBonded(const std::string &atomId_1, const std::string &atomId_2) const
|
||||
{
|
||||
auto i = find_if(mBonds.begin(), mBonds.end(),
|
||||
[&](const CompoundBond &b) {
|
||||
return (b.atomID[0] == atomId_1 and b.atomID[1] == atomId_2) or (b.atomID[0] == atomId_2 and b.atomID[1] == atomId_1);
|
||||
});
|
||||
|
||||
return i != mBonds.end();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// a factory class to generate compounds
|
||||
|
||||
CIFPP_EXPORT const std::map<std::string, char> kAAMap{
|
||||
{"ALA", 'A'},
|
||||
{"ARG", 'R'},
|
||||
{"ASN", 'N'},
|
||||
{"ASP", 'D'},
|
||||
{"CYS", 'C'},
|
||||
{"GLN", 'Q'},
|
||||
{"GLU", 'E'},
|
||||
{"GLY", 'G'},
|
||||
{"HIS", 'H'},
|
||||
{"ILE", 'I'},
|
||||
{"LEU", 'L'},
|
||||
{"LYS", 'K'},
|
||||
{"MET", 'M'},
|
||||
{"PHE", 'F'},
|
||||
{"PRO", 'P'},
|
||||
{"SER", 'S'},
|
||||
{"THR", 'T'},
|
||||
{"TRP", 'W'},
|
||||
{"TYR", 'Y'},
|
||||
{"VAL", 'V'},
|
||||
{"GLX", 'Z'},
|
||||
{"ASX", 'B'}};
|
||||
|
||||
CIFPP_EXPORT const std::map<std::string, char> kBaseMap{
|
||||
{"A", 'A'},
|
||||
{"C", 'C'},
|
||||
{"G", 'G'},
|
||||
{"T", 'T'},
|
||||
{"U", 'U'},
|
||||
{"DA", 'A'},
|
||||
{"DC", 'C'},
|
||||
{"DG", 'G'},
|
||||
{"DT", 'T'}};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class CompoundFactoryImpl : public std::enable_shared_from_this<CompoundFactoryImpl>
|
||||
{
|
||||
public:
|
||||
CompoundFactoryImpl(std::shared_ptr<CompoundFactoryImpl> next);
|
||||
|
||||
CompoundFactoryImpl(const std::filesystem::path &file, std::shared_ptr<CompoundFactoryImpl> next);
|
||||
|
||||
virtual ~CompoundFactoryImpl()
|
||||
{
|
||||
for (auto c: mCompounds)
|
||||
delete c;
|
||||
}
|
||||
|
||||
Compound *get(std::string id)
|
||||
{
|
||||
std::shared_lock lock(mMutex);
|
||||
|
||||
ba::to_upper(id);
|
||||
|
||||
Compound *result = nullptr;
|
||||
|
||||
// walk the list, see if any of us has the compound already
|
||||
for (auto impl = shared_from_this(); impl; impl = impl->mNext)
|
||||
{
|
||||
for (auto cmp : impl->mCompounds)
|
||||
{
|
||||
if (cmp->id() == id)
|
||||
{
|
||||
result = cmp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (result)
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == nullptr and mMissing.count(id) == 0)
|
||||
{
|
||||
for (auto impl = shared_from_this(); impl; impl = impl->mNext)
|
||||
{
|
||||
result = impl->create(id);
|
||||
if (result != nullptr)
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == nullptr)
|
||||
mMissing.insert(id);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::shared_ptr<CompoundFactoryImpl> next() const
|
||||
{
|
||||
return mNext;
|
||||
}
|
||||
|
||||
bool isKnownPeptide(const std::string &resName)
|
||||
{
|
||||
return mKnownPeptides.count(resName) or
|
||||
(mNext and mNext->isKnownPeptide(resName));
|
||||
}
|
||||
|
||||
bool isKnownBase(const std::string &resName)
|
||||
{
|
||||
return mKnownBases.count(resName) or
|
||||
(mNext and mNext->isKnownBase(resName));
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual Compound *create(const std::string &id)
|
||||
{
|
||||
// For the base class we assume every compound is preloaded
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_timed_mutex mMutex;
|
||||
|
||||
std::vector<Compound *> mCompounds;
|
||||
std::set<std::string> mKnownPeptides;
|
||||
std::set<std::string> mKnownBases;
|
||||
std::set<std::string> mMissing;
|
||||
std::shared_ptr<CompoundFactoryImpl> mNext;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
CompoundFactoryImpl::CompoundFactoryImpl(std::shared_ptr<CompoundFactoryImpl> next)
|
||||
: mNext(next)
|
||||
{
|
||||
for (const auto &[key, value] : kAAMap)
|
||||
mKnownPeptides.insert(key);
|
||||
|
||||
for (const auto &[key, value] : kBaseMap)
|
||||
mKnownBases.insert(key);
|
||||
}
|
||||
|
||||
CompoundFactoryImpl::CompoundFactoryImpl(const std::filesystem::path &file, std::shared_ptr<CompoundFactoryImpl> next)
|
||||
: mNext(next)
|
||||
{
|
||||
cif::File cifFile(file);
|
||||
|
||||
auto compList = cifFile.get("comp_list");
|
||||
if (compList) // So this is a CCP4 restraints file, special handling
|
||||
{
|
||||
auto &chemComp = (*compList)["chem_comp"];
|
||||
|
||||
for (const auto &[id, name, group] : chemComp.rows<std::string, std::string, std::string>("id", "name", "group"))
|
||||
{
|
||||
std::string type;
|
||||
|
||||
// known groups are (counted from ccp4 monomer dictionary)
|
||||
|
||||
// D-pyranose
|
||||
// DNA
|
||||
// L-PEPTIDE LINKING
|
||||
// L-SACCHARIDE
|
||||
// L-peptide
|
||||
// L-pyranose
|
||||
// M-peptide
|
||||
// NON-POLYMER
|
||||
// P-peptide
|
||||
// RNA
|
||||
// furanose
|
||||
// non-polymer
|
||||
// non_polymer
|
||||
// peptide
|
||||
// pyranose
|
||||
// saccharide
|
||||
|
||||
if (cif::iequals(id, "gly"))
|
||||
type = "peptide linking";
|
||||
else if (cif::iequals(group, "l-peptide") or cif::iequals(group, "L-peptide linking") or cif::iequals(group, "peptide"))
|
||||
type = "L-peptide linking";
|
||||
else if (cif::iequals(group, "DNA"))
|
||||
type = "DNA linking";
|
||||
else if (cif::iequals(group, "RNA"))
|
||||
type = "RNA linking";
|
||||
else
|
||||
type = "non-polymer";
|
||||
|
||||
auto &db = cifFile["comp_" + id];
|
||||
|
||||
mCompounds.push_back(new Compound(db, id, name, type));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// A CCD components file, validate it first
|
||||
cifFile.loadDictionary("mmcif_pdbx_v50");
|
||||
|
||||
if (not cifFile.isValid())
|
||||
throw std::runtime_error("Invalid compound file");
|
||||
|
||||
for (auto &db : cifFile)
|
||||
mCompounds.push_back(new Compound(db));
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Version for the default compounds, based on the cached components.cif file from CCD
|
||||
|
||||
class CCDCompoundFactoryImpl : public CompoundFactoryImpl
|
||||
{
|
||||
public:
|
||||
CCDCompoundFactoryImpl(std::shared_ptr<CompoundFactoryImpl> next, const fs::path& file)
|
||||
: CompoundFactoryImpl(next)
|
||||
, mCompoundsFile(file)
|
||||
{
|
||||
}
|
||||
|
||||
CCDCompoundFactoryImpl(std::shared_ptr<CompoundFactoryImpl> next)
|
||||
: CompoundFactoryImpl(next)
|
||||
{
|
||||
}
|
||||
|
||||
Compound *create(const std::string &id) override;
|
||||
|
||||
cif::DatablockIndex mIndex;
|
||||
fs::path mCompoundsFile;
|
||||
};
|
||||
|
||||
Compound *CCDCompoundFactoryImpl::create(const std::string &id)
|
||||
{
|
||||
Compound *result = nullptr;
|
||||
|
||||
std::unique_ptr<std::istream> ccd;
|
||||
|
||||
if (mCompoundsFile.empty())
|
||||
{
|
||||
ccd = cif::loadResource("components.cif");
|
||||
if (not ccd)
|
||||
throw std::runtime_error("Could not locate the CCD components.cif file, please make sure the software is installed properly and/or use the update-dictionary-script to fetch the data.");
|
||||
}
|
||||
else
|
||||
ccd.reset(new std::ifstream(mCompoundsFile));
|
||||
|
||||
cif::File file;
|
||||
|
||||
if (mIndex.empty())
|
||||
{
|
||||
if (cif::VERBOSE > 1)
|
||||
{
|
||||
std::cout << "Creating component index "
|
||||
<< "...";
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
cif::Parser parser(*ccd, file, false);
|
||||
mIndex = parser.indexDatablocks();
|
||||
|
||||
if (cif::VERBOSE > 1)
|
||||
std::cout << " done" << std::endl;
|
||||
|
||||
// reload the resource, perhaps this should be improved...
|
||||
if (mCompoundsFile.empty())
|
||||
{
|
||||
ccd = cif::loadResource("components.cif");
|
||||
if (not ccd)
|
||||
throw std::runtime_error("Could not locate the CCD components.cif file, please make sure the software is installed properly and/or use the update-dictionary-script to fetch the data.");
|
||||
}
|
||||
else
|
||||
ccd.reset(new std::ifstream(mCompoundsFile));
|
||||
}
|
||||
|
||||
if (cif::VERBOSE > 1)
|
||||
{
|
||||
std::cout << "Loading component " << id << "...";
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
cif::Parser parser(*ccd, file, false);
|
||||
parser.parseSingleDatablock(id, mIndex);
|
||||
|
||||
if (cif::VERBOSE > 1)
|
||||
std::cout << " done" << std::endl;
|
||||
|
||||
if (not file.empty())
|
||||
{
|
||||
auto &db = file.firstDatablock();
|
||||
if (db.getName() == id)
|
||||
{
|
||||
result = new Compound(db);
|
||||
|
||||
std::shared_lock lock(mMutex);
|
||||
mCompounds.push_back(result);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == nullptr and cif::VERBOSE)
|
||||
std::cerr << "Could not locate compound " << id << " in the CCD components file" << std::endl;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Version for the default compounds, based on the data found in CCP4's monomers lib
|
||||
|
||||
class CCP4CompoundFactoryImpl : public CompoundFactoryImpl
|
||||
{
|
||||
public:
|
||||
CCP4CompoundFactoryImpl(const fs::path &clibd_mon, std::shared_ptr<CompoundFactoryImpl> next = nullptr);
|
||||
|
||||
Compound *create(const std::string &id) override;
|
||||
|
||||
private:
|
||||
cif::File mFile;
|
||||
fs::path mCLIBD_MON;
|
||||
};
|
||||
|
||||
CCP4CompoundFactoryImpl::CCP4CompoundFactoryImpl(const fs::path &clibd_mon, std::shared_ptr<CompoundFactoryImpl> next)
|
||||
: CompoundFactoryImpl(next)
|
||||
, mFile((clibd_mon / "list" / "mon_lib_list.cif").string())
|
||||
, mCLIBD_MON(clibd_mon)
|
||||
{
|
||||
const std::regex peptideRx("(?:[lmp]-)?peptide", std::regex::icase);
|
||||
|
||||
auto &chemComps = mFile["comp_list"]["chem_comp"];
|
||||
|
||||
for (const auto &[group, threeLetterCode] : chemComps.rows<std::string, std::string>("group", "three_letter_code"))
|
||||
{
|
||||
if (std::regex_match(group, peptideRx))
|
||||
mKnownPeptides.insert(threeLetterCode);
|
||||
else if (ba::iequals(group, "DNA") or ba::iequals(group, "RNA"))
|
||||
mKnownBases.insert(threeLetterCode);
|
||||
}
|
||||
}
|
||||
|
||||
Compound *CCP4CompoundFactoryImpl::create(const std::string &id)
|
||||
{
|
||||
Compound *result = nullptr;
|
||||
|
||||
auto &cat = mFile["comp_list"]["chem_comp"];
|
||||
|
||||
auto rs = cat.find(cif::Key("three_letter_code") == id);
|
||||
|
||||
if (rs.size() == 1)
|
||||
{
|
||||
auto row = rs.front();
|
||||
|
||||
std::string name, group;
|
||||
uint32_t numberAtomsAll, numberAtomsNh;
|
||||
cif::tie(name, group, numberAtomsAll, numberAtomsNh) =
|
||||
row.get("name", "group", "number_atoms_all", "number_atoms_nh");
|
||||
|
||||
fs::path resFile = mCLIBD_MON / ba::to_lower_copy(id.substr(0, 1)) / (id + ".cif");
|
||||
|
||||
if (not fs::exists(resFile) and (id == "COM" or id == "CON" or "PRN")) // seriously...
|
||||
resFile = mCLIBD_MON / ba::to_lower_copy(id.substr(0, 1)) / (id + '_' + id + ".cif");
|
||||
|
||||
if (fs::exists(resFile))
|
||||
{
|
||||
cif::File cf(resFile.string());
|
||||
|
||||
// locate the datablock
|
||||
auto &db = cf["comp_" + id];
|
||||
|
||||
std::string type;
|
||||
|
||||
// known groups are (counted from ccp4 monomer dictionary)
|
||||
|
||||
// D-pyranose
|
||||
// DNA
|
||||
// L-PEPTIDE LINKING
|
||||
// L-SACCHARIDE
|
||||
// L-peptide
|
||||
// L-pyranose
|
||||
// M-peptide
|
||||
// NON-POLYMER
|
||||
// P-peptide
|
||||
// RNA
|
||||
// furanose
|
||||
// non-polymer
|
||||
// non_polymer
|
||||
// peptide
|
||||
// pyranose
|
||||
// saccharide
|
||||
|
||||
if (cif::iequals(id, "gly"))
|
||||
type = "peptide linking";
|
||||
else if (cif::iequals(group, "l-peptide") or cif::iequals(group, "L-peptide linking") or cif::iequals(group, "peptide"))
|
||||
type = "L-peptide linking";
|
||||
else if (cif::iequals(group, "DNA"))
|
||||
type = "DNA linking";
|
||||
else if (cif::iequals(group, "RNA"))
|
||||
type = "RNA linking";
|
||||
else
|
||||
type = "non-polymer";
|
||||
|
||||
mCompounds.push_back(new Compound(db, id, name, type));
|
||||
result = mCompounds.back();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
std::unique_ptr<CompoundFactory> CompoundFactory::sInstance;
|
||||
thread_local std::unique_ptr<CompoundFactory> CompoundFactory::tlInstance;
|
||||
bool CompoundFactory::sUseThreadLocalInstance;
|
||||
|
||||
void CompoundFactory::init(bool useThreadLocalInstanceOnly)
|
||||
{
|
||||
sUseThreadLocalInstance = useThreadLocalInstanceOnly;
|
||||
}
|
||||
|
||||
CompoundFactory::CompoundFactory()
|
||||
: mImpl(nullptr)
|
||||
{
|
||||
const char *clibd_mon = getenv("CLIBD_MON");
|
||||
if (clibd_mon != nullptr and fs::is_directory(clibd_mon))
|
||||
mImpl.reset(new CCP4CompoundFactoryImpl(clibd_mon));
|
||||
else if (cif::VERBOSE)
|
||||
std::cerr << "CCP4 monomers library not found, CLIBD_MON is not defined" << std::endl;
|
||||
|
||||
auto ccd = cif::loadResource("components.cif");
|
||||
if (ccd)
|
||||
mImpl.reset(new CCDCompoundFactoryImpl(mImpl));
|
||||
else if (cif::VERBOSE)
|
||||
std::cerr << "CCD components.cif file was not found" << std::endl;
|
||||
}
|
||||
|
||||
CompoundFactory::~CompoundFactory()
|
||||
{
|
||||
}
|
||||
|
||||
CompoundFactory &CompoundFactory::instance()
|
||||
{
|
||||
if (sUseThreadLocalInstance)
|
||||
{
|
||||
if (not tlInstance)
|
||||
tlInstance.reset(new CompoundFactory());
|
||||
return *tlInstance;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (not sInstance)
|
||||
sInstance.reset(new CompoundFactory());
|
||||
return *sInstance;
|
||||
}
|
||||
}
|
||||
|
||||
void CompoundFactory::clear()
|
||||
{
|
||||
if (sUseThreadLocalInstance)
|
||||
tlInstance.reset(nullptr);
|
||||
else
|
||||
sInstance.reset();
|
||||
}
|
||||
|
||||
void CompoundFactory::setDefaultDictionary(const std::filesystem::path &inDictFile)
|
||||
{
|
||||
if (not fs::exists(inDictFile))
|
||||
throw std::runtime_error("file not found: " + inDictFile.string());
|
||||
|
||||
try
|
||||
{
|
||||
mImpl.reset(new CCDCompoundFactoryImpl(mImpl, inDictFile));
|
||||
}
|
||||
catch (const std::exception &)
|
||||
{
|
||||
std::cerr << "Error loading dictionary " << inDictFile << std::endl;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void CompoundFactory::pushDictionary(const std::filesystem::path &inDictFile)
|
||||
{
|
||||
if (not fs::exists(inDictFile))
|
||||
throw std::runtime_error("file not found: " + inDictFile.string());
|
||||
|
||||
// ifstream file(inDictFile);
|
||||
// if (not file.is_open())
|
||||
// throw std::runtime_error("Could not open peptide list " + inDictFile);
|
||||
|
||||
try
|
||||
{
|
||||
mImpl.reset(new CompoundFactoryImpl(inDictFile, mImpl));
|
||||
}
|
||||
catch (const std::exception &)
|
||||
{
|
||||
std::cerr << "Error loading dictionary " << inDictFile << std::endl;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void CompoundFactory::popDictionary()
|
||||
{
|
||||
if (mImpl)
|
||||
mImpl = mImpl->next();
|
||||
}
|
||||
|
||||
const Compound *CompoundFactory::create(std::string id)
|
||||
{
|
||||
// static bool warned = false;
|
||||
|
||||
// if (mImpl and warned == false)
|
||||
// {
|
||||
// std::cerr << "Warning: no compound information library was found, resulting data may be incorrect or incomplete" << std::endl;
|
||||
// warned = true;
|
||||
// }
|
||||
|
||||
return mImpl ? mImpl->get(id) : nullptr;
|
||||
}
|
||||
|
||||
bool CompoundFactory::isKnownPeptide(const std::string &resName) const
|
||||
{
|
||||
return mImpl ? mImpl->isKnownPeptide(resName) : kAAMap.count(resName) > 0;
|
||||
}
|
||||
|
||||
bool CompoundFactory::isKnownBase(const std::string &resName) const
|
||||
{
|
||||
return mImpl ? mImpl->isKnownBase(resName) : kBaseMap.count(resName) > 0;
|
||||
}
|
||||
|
||||
} // namespace mmcif
|
||||
@@ -1,11 +0,0 @@
|
||||
/* Define to the name of this package. */
|
||||
#cmakedefine PACKAGE_NAME "@PACKAGE_NAME@"
|
||||
|
||||
/* Define to the version of this package. */
|
||||
#cmakedefine PACKAGE_VERSION "@PACKAGE_VERSION@"
|
||||
|
||||
/* Define the complete package string */
|
||||
#cmakedefine PACKAGE_STRING "@PACKAGE_STRING@"
|
||||
|
||||
/* Using resources? */
|
||||
#cmakedefine USE_RSRC @USE_RSRC@
|
||||
@@ -1,113 +0,0 @@
|
||||
/* src/Config.hpp.in. Generated from configure.ac by autoheader. */
|
||||
|
||||
/* define if the Boost library is available */
|
||||
#undef HAVE_BOOST
|
||||
|
||||
/* define if the Boost::Date_Time library is available */
|
||||
#undef HAVE_BOOST_DATE_TIME
|
||||
|
||||
/* define if the Boost::IOStreams library is available */
|
||||
#undef HAVE_BOOST_IOSTREAMS
|
||||
|
||||
/* define if the Boost::Regex library is available */
|
||||
#undef HAVE_BOOST_REGEX
|
||||
|
||||
/* define if the compiler supports basic C++17 syntax */
|
||||
#undef HAVE_CXX17
|
||||
|
||||
/* Define to 1 if you have the <dlfcn.h> header file. */
|
||||
#undef HAVE_DLFCN_H
|
||||
|
||||
/* Define to 1 if you have the `floor' function. */
|
||||
#undef HAVE_FLOOR
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#undef HAVE_INTTYPES_H
|
||||
|
||||
/* Define to 1 if you have the <memory.h> header file. */
|
||||
#undef HAVE_MEMORY_H
|
||||
|
||||
/* Define to 1 if you have the `pow' function. */
|
||||
#undef HAVE_POW
|
||||
|
||||
/* Define if you have POSIX threads libraries and header files. */
|
||||
#undef HAVE_PTHREAD
|
||||
|
||||
/* Have PTHREAD_PRIO_INHERIT. */
|
||||
#undef HAVE_PTHREAD_PRIO_INHERIT
|
||||
|
||||
/* Define to 1 if the system has the type `ptrdiff_t'. */
|
||||
#undef HAVE_PTRDIFF_T
|
||||
|
||||
/* Define to 1 if you have the `rint' function. */
|
||||
#undef HAVE_RINT
|
||||
|
||||
/* Define to 1 if you have the `sqrt' function. */
|
||||
#undef HAVE_SQRT
|
||||
|
||||
/* Define to 1 if you have the <stdint.h> header file. */
|
||||
#undef HAVE_STDINT_H
|
||||
|
||||
/* Define to 1 if you have the <stdlib.h> header file. */
|
||||
#undef HAVE_STDLIB_H
|
||||
|
||||
/* Define to 1 if you have the `strchr' function. */
|
||||
#undef HAVE_STRCHR
|
||||
|
||||
/* Define to 1 if you have the `strerror' function. */
|
||||
#undef HAVE_STRERROR
|
||||
|
||||
/* Define to 1 if you have the <strings.h> header file. */
|
||||
#undef HAVE_STRINGS_H
|
||||
|
||||
/* Define to 1 if you have the <string.h> header file. */
|
||||
#undef HAVE_STRING_H
|
||||
|
||||
/* Define to 1 if you have the <sys/ioctl.h> header file. */
|
||||
#undef HAVE_SYS_IOCTL_H
|
||||
|
||||
/* Define to 1 if you have the <sys/stat.h> header file. */
|
||||
#undef HAVE_SYS_STAT_H
|
||||
|
||||
/* Define to 1 if you have the <sys/types.h> header file. */
|
||||
#undef HAVE_SYS_TYPES_H
|
||||
|
||||
/* Define to 1 if you have the <termios.h> header file. */
|
||||
#undef HAVE_TERMIOS_H
|
||||
|
||||
/* Define to 1 if you have the <unistd.h> header file. */
|
||||
#undef HAVE_UNISTD_H
|
||||
|
||||
/* Define to 1 if the system has the type `_Bool'. */
|
||||
#undef HAVE__BOOL
|
||||
|
||||
/* Define to the sub-directory where libtool stores uninstalled libraries. */
|
||||
#undef LT_OBJDIR
|
||||
|
||||
/* Define to the address where bug reports for this package should be sent. */
|
||||
#undef PACKAGE_BUGREPORT
|
||||
|
||||
/* Define to the full name of this package. */
|
||||
#undef PACKAGE_NAME
|
||||
|
||||
/* Define to the full name and version of this package. */
|
||||
#undef PACKAGE_STRING
|
||||
|
||||
/* Define to the one symbol short name of this package. */
|
||||
#undef PACKAGE_TARNAME
|
||||
|
||||
/* Define to the home page for this package. */
|
||||
#undef PACKAGE_URL
|
||||
|
||||
/* Define to the version of this package. */
|
||||
#undef PACKAGE_VERSION
|
||||
|
||||
/* Define to necessary symbol if this constant uses a non-standard name on
|
||||
your system. */
|
||||
#undef PTHREAD_CREATE_JOINABLE
|
||||
|
||||
/* Define to 1 if you have the ANSI C header files. */
|
||||
#undef STDC_HEADERS
|
||||
|
||||
/* Use mrc to store resources */
|
||||
#undef USE_RSRC
|
||||
File diff suppressed because it is too large
Load Diff
306
src/Point.cpp
306
src/Point.cpp
@@ -1,306 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <random>
|
||||
#include <valarray>
|
||||
|
||||
#include "cif++/Point.hpp"
|
||||
#include "cif++/Matrix.hpp"
|
||||
|
||||
namespace mmcif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
Quaternion Normalize(Quaternion q)
|
||||
{
|
||||
std::valarray<double> t(4);
|
||||
|
||||
t[0] = q.R_component_1();
|
||||
t[1] = q.R_component_2();
|
||||
t[2] = q.R_component_3();
|
||||
t[3] = q.R_component_4();
|
||||
|
||||
t *= t;
|
||||
|
||||
double length = std::sqrt(t.sum());
|
||||
|
||||
if (length > 0.001)
|
||||
q /= static_cast<Quaternion::value_type>(length);
|
||||
else
|
||||
q = Quaternion(1, 0, 0, 0);
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
std::tuple<double,Point> QuaternionToAngleAxis(Quaternion q)
|
||||
{
|
||||
if (q.R_component_1() > 1)
|
||||
q = Normalize(q);
|
||||
|
||||
// angle:
|
||||
double angle = 2 * acos(q.R_component_1());
|
||||
angle = angle * 180 / kPI;
|
||||
|
||||
// axis:
|
||||
float s = std::sqrt(1 - q.R_component_1() * q.R_component_1());
|
||||
if (s < 0.001)
|
||||
s = 1;
|
||||
|
||||
Point axis(q.R_component_2() / s, q.R_component_3() / s, q.R_component_4() / s);
|
||||
|
||||
return std::make_tuple(angle, axis);
|
||||
}
|
||||
|
||||
Point CenterPoints(std::vector<Point>& Points)
|
||||
{
|
||||
Point t;
|
||||
|
||||
for (Point& pt : Points)
|
||||
{
|
||||
t.mX += pt.mX;
|
||||
t.mY += pt.mY;
|
||||
t.mZ += pt.mZ;
|
||||
}
|
||||
|
||||
t.mX /= Points.size();
|
||||
t.mY /= Points.size();
|
||||
t.mZ /= Points.size();
|
||||
|
||||
for (Point& pt : Points)
|
||||
{
|
||||
pt.mX -= t.mX;
|
||||
pt.mY -= t.mY;
|
||||
pt.mZ -= t.mZ;
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
Point Centroid(std::vector<Point>& Points)
|
||||
{
|
||||
Point result;
|
||||
|
||||
for (Point& pt : Points)
|
||||
result += pt;
|
||||
|
||||
result /= static_cast<float>(Points.size());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
double RMSd(const std::vector<Point>& a, const std::vector<Point>& b)
|
||||
{
|
||||
double sum = 0;
|
||||
for (uint32_t i = 0; i < a.size(); ++i)
|
||||
{
|
||||
std::valarray<double> d(3);
|
||||
|
||||
d[0] = b[i].mX - a[i].mX;
|
||||
d[1] = b[i].mY - a[i].mY;
|
||||
d[2] = b[i].mZ - a[i].mZ;
|
||||
|
||||
d *= d;
|
||||
|
||||
sum += d.sum();
|
||||
}
|
||||
|
||||
return std::sqrt(sum / a.size());
|
||||
}
|
||||
|
||||
// The next function returns the largest solution for a quartic equation
|
||||
// based on Ferrari's algorithm.
|
||||
// A depressed quartic is of the form:
|
||||
//
|
||||
// x^4 + ax^2 + bx + c = 0
|
||||
//
|
||||
// (since I'm too lazy to find out a better way, I've implemented the
|
||||
// routine using complex values to avoid nan's as a result of taking
|
||||
// sqrt of a negative number)
|
||||
double LargestDepressedQuarticSolution(double a, double b, double c)
|
||||
{
|
||||
std::complex<double> P = - (a * a) / 12 - c;
|
||||
std::complex<double> Q = - (a * a * a) / 108 + (a * c) / 3 - (b * b) / 8;
|
||||
std::complex<double> R = - Q / 2.0 + std::sqrt((Q * Q) / 4.0 + (P * P * P) / 27.0);
|
||||
|
||||
std::complex<double> U = std::pow(R, 1 / 3.0);
|
||||
|
||||
std::complex<double> y;
|
||||
if (U == 0.0)
|
||||
y = -5.0 * a / 6.0 + U - std::pow(Q, 1.0 / 3.0);
|
||||
else
|
||||
y = -5.0 * a / 6.0 + U - P / (3.0 * U);
|
||||
|
||||
std::complex<double> W = std::sqrt(a + 2.0 * y);
|
||||
|
||||
// And to get the final result:
|
||||
// result = (±W + std::sqrt(-(3 * alpha + 2 * y ± 2 * beta / W))) / 2;
|
||||
// We want the largest result, so:
|
||||
|
||||
std::valarray<double> t(4);
|
||||
|
||||
t[0] = (( W + std::sqrt(-(3.0 * a + 2.0 * y + 2.0 * b / W))) / 2.0).real();
|
||||
t[1] = (( W + std::sqrt(-(3.0 * a + 2.0 * y - 2.0 * b / W))) / 2.0).real();
|
||||
t[2] = ((-W + std::sqrt(-(3.0 * a + 2.0 * y + 2.0 * b / W))) / 2.0).real();
|
||||
t[3] = ((-W + std::sqrt(-(3.0 * a + 2.0 * y - 2.0 * b / W))) / 2.0).real();
|
||||
|
||||
return t.max();
|
||||
}
|
||||
|
||||
Quaternion AlignPoints(const std::vector<Point>& pa, const std::vector<Point>& pb)
|
||||
{
|
||||
// First calculate M, a 3x3 Matrix containing the sums of products of the coordinates of A and B
|
||||
Matrix<double> M(3, 3, 0);
|
||||
|
||||
for (uint32_t i = 0; i < pa.size(); ++i)
|
||||
{
|
||||
const Point& a = pa[i];
|
||||
const Point& b = pb[i];
|
||||
|
||||
M(0, 0) += a.mX * b.mX; M(0, 1) += a.mX * b.mY; M(0, 2) += a.mX * b.mZ;
|
||||
M(1, 0) += a.mY * b.mX; M(1, 1) += a.mY * b.mY; M(1, 2) += a.mY * b.mZ;
|
||||
M(2, 0) += a.mZ * b.mX; M(2, 1) += a.mZ * b.mY; M(2, 2) += a.mZ * b.mZ;
|
||||
}
|
||||
|
||||
// Now calculate N, a symmetric 4x4 Matrix
|
||||
SymmetricMatrix<double> N(4);
|
||||
|
||||
N(0, 0) = M(0, 0) + M(1, 1) + M(2, 2);
|
||||
N(0, 1) = M(1, 2) - M(2, 1);
|
||||
N(0, 2) = M(2, 0) - M(0, 2);
|
||||
N(0, 3) = M(0, 1) - M(1, 0);
|
||||
|
||||
N(1, 1) = M(0, 0) - M(1, 1) - M(2, 2);
|
||||
N(1, 2) = M(0, 1) + M(1, 0);
|
||||
N(1, 3) = M(0, 2) + M(2, 0);
|
||||
|
||||
N(2, 2) = -M(0, 0) + M(1, 1) - M(2, 2);
|
||||
N(2, 3) = M(1, 2) + M(2, 1);
|
||||
|
||||
N(3, 3) = -M(0, 0) - M(1, 1) + M(2, 2);
|
||||
|
||||
// det(N - λI) = 0
|
||||
// find the largest λ (λm)
|
||||
//
|
||||
// Aλ4 + Bλ3 + Cλ2 + Dλ + E = 0
|
||||
// A = 1
|
||||
// B = 0
|
||||
// and so this is a so-called depressed quartic
|
||||
// solve it using Ferrari's algorithm
|
||||
|
||||
double C = -2 * (
|
||||
M(0, 0) * M(0, 0) + M(0, 1) * M(0, 1) + M(0, 2) * M(0, 2) +
|
||||
M(1, 0) * M(1, 0) + M(1, 1) * M(1, 1) + M(1, 2) * M(1, 2) +
|
||||
M(2, 0) * M(2, 0) + M(2, 1) * M(2, 1) + M(2, 2) * M(2, 2));
|
||||
|
||||
double D = 8 * (M(0, 0) * M(1, 2) * M(2, 1) +
|
||||
M(1, 1) * M(2, 0) * M(0, 2) +
|
||||
M(2, 2) * M(0, 1) * M(1, 0)) -
|
||||
8 * (M(0, 0) * M(1, 1) * M(2, 2) +
|
||||
M(1, 2) * M(2, 0) * M(0, 1) +
|
||||
M(2, 1) * M(1, 0) * M(0, 2));
|
||||
|
||||
double E =
|
||||
(N(0,0) * N(1,1) - N(0,1) * N(0,1)) * (N(2,2) * N(3,3) - N(2,3) * N(2,3)) +
|
||||
(N(0,1) * N(0,2) - N(0,0) * N(2,1)) * (N(2,1) * N(3,3) - N(2,3) * N(1,3)) +
|
||||
(N(0,0) * N(1,3) - N(0,1) * N(0,3)) * (N(2,1) * N(2,3) - N(2,2) * N(1,3)) +
|
||||
(N(0,1) * N(2,1) - N(1,1) * N(0,2)) * (N(0,2) * N(3,3) - N(2,3) * N(0,3)) +
|
||||
(N(1,1) * N(0,3) - N(0,1) * N(1,3)) * (N(0,2) * N(2,3) - N(2,2) * N(0,3)) +
|
||||
(N(0,2) * N(1,3) - N(2,1) * N(0,3)) * (N(0,2) * N(1,3) - N(2,1) * N(0,3));
|
||||
|
||||
// solve quartic
|
||||
double lm = LargestDepressedQuarticSolution(C, D, E);
|
||||
|
||||
// calculate t = (N - λI)
|
||||
Matrix<double> li = IdentityMatrix<double>(4) * lm;
|
||||
Matrix<double> t = N - li;
|
||||
|
||||
// calculate a Matrix of cofactors for t
|
||||
Matrix<double> cf(4, 4);
|
||||
|
||||
const uint32_t ixs[4][3] =
|
||||
{
|
||||
{ 1, 2, 3 },
|
||||
{ 0, 2, 3 },
|
||||
{ 0, 1, 3 },
|
||||
{ 0, 1, 2 }
|
||||
};
|
||||
|
||||
uint32_t maxR = 0;
|
||||
for (uint32_t r = 0; r < 4; ++r)
|
||||
{
|
||||
const uint32_t* ir = ixs[r];
|
||||
|
||||
for (uint32_t c = 0; c < 4; ++c)
|
||||
{
|
||||
const uint32_t* ic = ixs[c];
|
||||
|
||||
cf(r, c) =
|
||||
t(ir[0], ic[0]) * t(ir[1], ic[1]) * t(ir[2], ic[2]) +
|
||||
t(ir[0], ic[1]) * t(ir[1], ic[2]) * t(ir[2], ic[0]) +
|
||||
t(ir[0], ic[2]) * t(ir[1], ic[0]) * t(ir[2], ic[1]) -
|
||||
t(ir[0], ic[2]) * t(ir[1], ic[1]) * t(ir[2], ic[0]) -
|
||||
t(ir[0], ic[1]) * t(ir[1], ic[0]) * t(ir[2], ic[2]) -
|
||||
t(ir[0], ic[0]) * t(ir[1], ic[2]) * t(ir[2], ic[1]);
|
||||
}
|
||||
|
||||
if (r > maxR and cf(r, 0) > cf(maxR, 0))
|
||||
maxR = r;
|
||||
}
|
||||
|
||||
// NOTE the negation of the y here, why? Maybe I swapped r/c above?
|
||||
Quaternion q(cf(maxR, 0), cf(maxR, 1), -cf(maxR, 2), cf(maxR, 3));
|
||||
q = Normalize(q);
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
Point Nudge(Point p, float offset)
|
||||
{
|
||||
static std::random_device rd;
|
||||
static std::mt19937_64 rng(rd());
|
||||
|
||||
std::uniform_real_distribution<> randomAngle(0, 2 * kPI);
|
||||
std::normal_distribution<> randomOffset(0, offset);
|
||||
|
||||
float theta = static_cast<float>(randomAngle(rng));
|
||||
float phi1 = static_cast<float>(randomAngle(rng) - kPI);
|
||||
float phi2 = static_cast<float>(randomAngle(rng) - kPI);
|
||||
|
||||
Quaternion q = boost::math::spherical(1.0f, theta, phi1, phi2);
|
||||
|
||||
Point r{ 0, 0, 1 };
|
||||
r.rotate(q);
|
||||
r *= static_cast<float>(randomOffset(rng));
|
||||
|
||||
return p + r;
|
||||
}
|
||||
|
||||
}
|
||||
1528
src/Secondary.cpp
1528
src/Secondary.cpp
File diff suppressed because it is too large
Load Diff
2580
src/Structure.cpp
2580
src/Structure.cpp
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
1892
src/TlsParser.cpp
1892
src/TlsParser.cpp
File diff suppressed because it is too large
Load Diff
1182
src/atom_type.cpp
Normal file
1182
src/atom_type.cpp
Normal file
File diff suppressed because it is too large
Load Diff
2238
src/category.cpp
Normal file
2238
src/category.cpp
Normal file
File diff suppressed because it is too large
Load Diff
806
src/compound.cpp
Normal file
806
src/compound.cpp
Normal file
@@ -0,0 +1,806 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020-2022 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "cif++.hpp"
|
||||
|
||||
#if HAVE_CURL
|
||||
# include <curl/curl.h>
|
||||
#endif
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <numeric>
|
||||
#include <shared_mutex>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
std::string to_string(bond_type bondType)
|
||||
{
|
||||
switch (bondType)
|
||||
{
|
||||
case bond_type::sing: return "sing";
|
||||
case bond_type::doub: return "doub";
|
||||
case bond_type::trip: return "trip";
|
||||
case bond_type::quad: return "quad";
|
||||
case bond_type::arom: return "arom";
|
||||
case bond_type::poly: return "poly";
|
||||
case bond_type::delo: return "delo";
|
||||
case bond_type::pi: return "pi";
|
||||
}
|
||||
throw std::invalid_argument("Invalid bondType");
|
||||
}
|
||||
|
||||
bond_type parse_bond_type_from_string(const std::string &bondType)
|
||||
{
|
||||
if (cif::iequals(bondType, "sing"))
|
||||
return bond_type::sing;
|
||||
if (cif::iequals(bondType, "doub"))
|
||||
return bond_type::doub;
|
||||
if (cif::iequals(bondType, "trip"))
|
||||
return bond_type::trip;
|
||||
if (cif::iequals(bondType, "quad"))
|
||||
return bond_type::quad;
|
||||
if (cif::iequals(bondType, "arom"))
|
||||
return bond_type::arom;
|
||||
if (cif::iequals(bondType, "poly"))
|
||||
return bond_type::poly;
|
||||
if (cif::iequals(bondType, "delo"))
|
||||
return bond_type::delo;
|
||||
if (cif::iequals(bondType, "pi"))
|
||||
return bond_type::pi;
|
||||
throw std::invalid_argument("Invalid bondType: " + bondType);
|
||||
}
|
||||
|
||||
std::string to_string(stereo_config_type stereoConfig)
|
||||
{
|
||||
switch (stereoConfig)
|
||||
{
|
||||
case stereo_config_type::N: return "N";
|
||||
case stereo_config_type::R: return "R";
|
||||
case stereo_config_type::S: return "S";
|
||||
}
|
||||
throw std::invalid_argument("Invalid stereoConfig");
|
||||
}
|
||||
|
||||
stereo_config_type parse_stereo_config_from_string(const std::string &stereoConfig)
|
||||
{
|
||||
if (cif::iequals(stereoConfig, "N"))
|
||||
return stereo_config_type::N;
|
||||
if (cif::iequals(stereoConfig, "R"))
|
||||
return stereo_config_type::R;
|
||||
if (cif::iequals(stereoConfig, "S"))
|
||||
return stereo_config_type::S;
|
||||
throw std::invalid_argument("Invalid stereoConfig: " + stereoConfig);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// compound helper classes
|
||||
|
||||
struct compound_atom_less
|
||||
{
|
||||
bool operator()(const compound_atom &a, const compound_atom &b) const
|
||||
{
|
||||
int d = a.id.compare(b.id);
|
||||
if (d == 0)
|
||||
d = a.type_symbol - b.type_symbol;
|
||||
return d < 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct compound_bond_less
|
||||
{
|
||||
bool operator()(const compound_bond &a, const compound_bond &b) const
|
||||
{
|
||||
int d = a.atom_id[0].compare(b.atom_id[0]);
|
||||
if (d == 0)
|
||||
d = a.atom_id[1].compare(b.atom_id[1]);
|
||||
if (d == 0)
|
||||
d = static_cast<int>(a.type) - static_cast<int>(b.type);
|
||||
return d < 0;
|
||||
}
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// compound
|
||||
|
||||
compound::compound(cif::datablock &db)
|
||||
{
|
||||
auto &chemComp = db["chem_comp"];
|
||||
|
||||
if (chemComp.size() != 1)
|
||||
throw std::runtime_error("Invalid compound file, chem_comp should contain a single row");
|
||||
|
||||
std::string one_letter_code;
|
||||
|
||||
cif::tie(m_id, m_name, m_type, m_formula, m_formula_weight, m_formal_charge, one_letter_code, m_parent_id) =
|
||||
chemComp.front().get("id", "name", "type", "formula", "formula_weight", "pdbx_formal_charge", "one_letter_code", "mon_nstd_parent_comp_id");
|
||||
|
||||
if (one_letter_code.length() == 1)
|
||||
m_one_letter_code = one_letter_code.front();
|
||||
|
||||
// The name should not contain newline characters since that triggers validation errors later on
|
||||
cif::replace_all(m_name, "\n", "");
|
||||
|
||||
auto &chemCompAtom = db["chem_comp_atom"];
|
||||
for (auto row : chemCompAtom)
|
||||
{
|
||||
compound_atom atom;
|
||||
std::string type_symbol, stereo_config;
|
||||
cif::tie(atom.id, type_symbol, atom.charge, atom.aromatic, atom.leaving_atom, stereo_config, atom.x, atom.y, atom.z) =
|
||||
row.get("atom_id", "type_symbol", "charge", "pdbx_aromatic_flag", "pdbx_leaving_atom_flag", "pdbx_stereo_config",
|
||||
"model_Cartn_x", "model_Cartn_y", "model_Cartn_z");
|
||||
atom.type_symbol = atom_type_traits(type_symbol).type();
|
||||
if (stereo_config.empty())
|
||||
atom.stereo_config = stereo_config_type::N;
|
||||
else
|
||||
atom.stereo_config = parse_stereo_config_from_string(stereo_config);
|
||||
m_atoms.push_back(std::move(atom));
|
||||
}
|
||||
|
||||
auto &chemCompBond = db["chem_comp_bond"];
|
||||
for (auto row : chemCompBond)
|
||||
{
|
||||
compound_bond bond;
|
||||
std::string valueOrder;
|
||||
cif::tie(bond.atom_id[0], bond.atom_id[1], valueOrder, bond.aromatic, bond.stereo_config) = row.get("atom_id_1", "atom_id_2", "value_order", "pdbx_aromatic_flag", "pdbx_stereo_config");
|
||||
if (valueOrder.empty())
|
||||
bond.type = bond_type::sing;
|
||||
else
|
||||
bond.type = parse_bond_type_from_string(valueOrder);
|
||||
m_bonds.push_back(std::move(bond));
|
||||
}
|
||||
}
|
||||
|
||||
compound_atom compound::get_atom_by_atom_id(const std::string &atom_id) const
|
||||
{
|
||||
compound_atom result = {};
|
||||
for (auto &a : m_atoms)
|
||||
{
|
||||
if (a.id == atom_id)
|
||||
{
|
||||
result = a;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (result.id != atom_id)
|
||||
throw std::out_of_range("No atom " + atom_id + " in compound " + m_id);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool compound::atoms_bonded(const std::string &atomId_1, const std::string &atomId_2) const
|
||||
{
|
||||
auto i = find_if(m_bonds.begin(), m_bonds.end(),
|
||||
[&](const compound_bond &b)
|
||||
{
|
||||
return (b.atom_id[0] == atomId_1 and b.atom_id[1] == atomId_2) or (b.atom_id[0] == atomId_2 and b.atom_id[1] == atomId_1);
|
||||
});
|
||||
|
||||
return i != m_bonds.end();
|
||||
}
|
||||
|
||||
float compound::bond_length(const std::string &atomId_1, const std::string &atomId_2) const
|
||||
{
|
||||
auto i = find_if(m_bonds.begin(), m_bonds.end(),
|
||||
[&](const compound_bond &b)
|
||||
{
|
||||
return (b.atom_id[0] == atomId_1 and b.atom_id[1] == atomId_2) or (b.atom_id[0] == atomId_2 and b.atom_id[1] == atomId_1);
|
||||
});
|
||||
|
||||
float result = std::numeric_limits<float>::max();
|
||||
|
||||
if (i != m_bonds.end())
|
||||
{
|
||||
auto a = get_atom_by_atom_id(atomId_1);
|
||||
auto b = get_atom_by_atom_id(atomId_2);
|
||||
|
||||
result = distance(point{ a.x, a.y, a.z }, point{ b.x, b.y, b.z });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
bool compound::is_peptide() const
|
||||
{
|
||||
return iequals(m_type, "l-peptide linking") or iequals(m_type, "peptide linking");
|
||||
}
|
||||
|
||||
bool compound::is_base() const
|
||||
{
|
||||
return iequals(m_type, "dna linking") or iequals(m_type, "rna linking");
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// known amino acids and bases
|
||||
|
||||
const std::map<std::string, char> compound_factory::kAAMap{
|
||||
{ "ALA", 'A' },
|
||||
{ "ARG", 'R' },
|
||||
{ "ASN", 'N' },
|
||||
{ "ASP", 'D' },
|
||||
{ "CYS", 'C' },
|
||||
{ "GLN", 'Q' },
|
||||
{ "GLU", 'E' },
|
||||
{ "GLY", 'G' },
|
||||
{ "HIS", 'H' },
|
||||
{ "ILE", 'I' },
|
||||
{ "LEU", 'L' },
|
||||
{ "LYS", 'K' },
|
||||
{ "MET", 'M' },
|
||||
{ "PHE", 'F' },
|
||||
{ "PRO", 'P' },
|
||||
{ "SER", 'S' },
|
||||
{ "THR", 'T' },
|
||||
{ "TRP", 'W' },
|
||||
{ "TYR", 'Y' },
|
||||
{ "VAL", 'V' },
|
||||
{ "GLX", 'Z' },
|
||||
{ "ASX", 'B' }
|
||||
};
|
||||
|
||||
const std::map<std::string, char> compound_factory::kBaseMap{
|
||||
{ "A", 'A' },
|
||||
{ "C", 'C' },
|
||||
{ "G", 'G' },
|
||||
{ "T", 'T' },
|
||||
{ "U", 'U' },
|
||||
{ "DA", 'A' },
|
||||
{ "DC", 'C' },
|
||||
{ "DG", 'G' },
|
||||
{ "DT", 'T' }
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// a factory class to generate compounds
|
||||
|
||||
class compound_factory_impl : public std::enable_shared_from_this<compound_factory_impl>
|
||||
{
|
||||
public:
|
||||
compound_factory_impl();
|
||||
compound_factory_impl(const fs::path &file, std::shared_ptr<compound_factory_impl> next);
|
||||
|
||||
virtual ~compound_factory_impl()
|
||||
{
|
||||
for (auto c : m_compounds)
|
||||
delete c;
|
||||
}
|
||||
|
||||
compound *get(std::string id)
|
||||
{
|
||||
std::shared_lock lock(mMutex);
|
||||
|
||||
compound *result = nullptr;
|
||||
|
||||
for (auto impl = shared_from_this(); impl; impl = impl->m_next)
|
||||
{
|
||||
result = impl->create(id);
|
||||
if (result != nullptr)
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::shared_ptr<compound_factory_impl> next()
|
||||
{
|
||||
return m_next;
|
||||
}
|
||||
|
||||
void describe(std::ostream &os)
|
||||
{
|
||||
if (m_file.empty())
|
||||
os << "CCD components.cif resource\n";
|
||||
else
|
||||
os << "CCD components file: " << std::quoted(m_file.string()) << '\n';
|
||||
|
||||
if (m_next)
|
||||
m_next->describe(os);
|
||||
}
|
||||
|
||||
protected:
|
||||
compound_factory_impl(std::shared_ptr<compound_factory_impl> next);
|
||||
|
||||
virtual compound *create(const std::string &id);
|
||||
|
||||
std::shared_timed_mutex mMutex;
|
||||
|
||||
fs::path m_file;
|
||||
cif::parser::datablock_index m_index;
|
||||
|
||||
std::vector<compound *> m_compounds;
|
||||
cif::iset m_missing;
|
||||
std::shared_ptr<compound_factory_impl> m_next;
|
||||
};
|
||||
|
||||
compound_factory_impl::compound_factory_impl()
|
||||
{
|
||||
}
|
||||
|
||||
compound_factory_impl::compound_factory_impl(std::shared_ptr<compound_factory_impl> next)
|
||||
: m_next(next)
|
||||
{
|
||||
}
|
||||
|
||||
compound_factory_impl::compound_factory_impl(const fs::path &file, std::shared_ptr<compound_factory_impl> next)
|
||||
: compound_factory_impl(next)
|
||||
{
|
||||
m_file = file;
|
||||
}
|
||||
|
||||
compound *compound_factory_impl::create(const std::string &id)
|
||||
{
|
||||
// shortcut
|
||||
|
||||
if (m_missing.contains(id))
|
||||
return nullptr;
|
||||
|
||||
if (auto i = find_if(m_compounds.begin(), m_compounds.end(), [id](compound *c)
|
||||
{ return c->id() == id; });
|
||||
i != m_compounds.end())
|
||||
return *i;
|
||||
|
||||
compound *result = nullptr;
|
||||
|
||||
std::unique_ptr<std::istream> ccd;
|
||||
|
||||
if (m_file.empty())
|
||||
{
|
||||
ccd = cif::load_resource("components.cif");
|
||||
if (not ccd)
|
||||
{
|
||||
std::cerr << "Could not locate the CCD components.cif file, please make sure the software is installed properly and/or use the update-libcifpp-data to fetch the data.\n";
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
ccd.reset(new std::ifstream(m_file));
|
||||
|
||||
cif::file file;
|
||||
|
||||
if (m_index.empty())
|
||||
{
|
||||
if (cif::VERBOSE > 1)
|
||||
{
|
||||
std::cout << "Creating component index "
|
||||
<< "...";
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
cif::parser parser(*ccd, file);
|
||||
m_index = parser.index_datablocks();
|
||||
|
||||
if (cif::VERBOSE > 1)
|
||||
std::cout << " done\n";
|
||||
|
||||
// reload the resource, perhaps this should be improved...
|
||||
if (m_file.empty())
|
||||
{
|
||||
ccd = cif::load_resource("components.cif");
|
||||
if (not ccd)
|
||||
throw std::runtime_error("Could not locate the CCD components.cif file, please make sure the software is installed properly and/or use the update-libcifpp-data to fetch the data.");
|
||||
}
|
||||
else
|
||||
ccd.reset(new std::ifstream(m_file));
|
||||
}
|
||||
|
||||
if (cif::VERBOSE > 1)
|
||||
{
|
||||
std::cout << "Loading component " << id << "...";
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
cif::parser parser(*ccd, file);
|
||||
parser.parse_single_datablock(id, m_index);
|
||||
|
||||
if (cif::VERBOSE > 1)
|
||||
std::cout << " done\n";
|
||||
|
||||
if (not file.empty())
|
||||
{
|
||||
auto &db = file.front();
|
||||
if (db.name() == id)
|
||||
{
|
||||
result = new compound(db);
|
||||
|
||||
std::shared_lock lock(mMutex);
|
||||
m_compounds.push_back(result);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == nullptr)
|
||||
m_missing.insert(id);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class local_compound_factory_impl : public compound_factory_impl
|
||||
{
|
||||
public:
|
||||
local_compound_factory_impl(const cif::file &file, std::shared_ptr<compound_factory_impl> next)
|
||||
: compound_factory_impl(next)
|
||||
, m_local_file(file)
|
||||
{
|
||||
}
|
||||
|
||||
compound *create(const std::string &id) override;
|
||||
|
||||
private:
|
||||
compound *construct_compound(const datablock &db, const std::string &id, const std::string &name, const std::string &three_letter_code, const std::string &group);
|
||||
|
||||
cif::file m_local_file;
|
||||
};
|
||||
|
||||
compound *local_compound_factory_impl::create(const std::string &id)
|
||||
{
|
||||
if (m_missing.contains(id))
|
||||
return nullptr;
|
||||
|
||||
if (auto i = find_if(m_compounds.begin(), m_compounds.end(), [id](compound *c)
|
||||
{ return c->id() == id; });
|
||||
i != m_compounds.end())
|
||||
return *i;
|
||||
|
||||
compound *result = nullptr;
|
||||
|
||||
for (auto &db : m_local_file)
|
||||
{
|
||||
if (db.name() == id)
|
||||
{
|
||||
auto chem_comp = db.get("chem_comp");
|
||||
if (not chem_comp)
|
||||
break;
|
||||
|
||||
try
|
||||
{
|
||||
const auto &[id, name, threeLetterCode, group] =
|
||||
chem_comp->front().get<std::string, std::string, std::string, std::string>("id", "name", "three_letter_code", "group");
|
||||
|
||||
result = construct_compound(db, id, name, threeLetterCode, group);
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
std::throw_with_nested(std::runtime_error("Error loading compound " + id));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (result == nullptr)
|
||||
m_missing.insert(id);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
compound *local_compound_factory_impl::construct_compound(const datablock &rdb, const std::string &id,
|
||||
const std::string &name, const std::string &three_letter_code, const std::string &group)
|
||||
{
|
||||
cif::datablock db(id);
|
||||
|
||||
float formula_weight = 0;
|
||||
int formal_charge = 0;
|
||||
std::map<std::string, std::size_t> formula_data;
|
||||
|
||||
for (std::size_t ord = 1; const auto &[atom_id, type_symbol, type, charge, x, y, z, xi, yi, zi] :
|
||||
rdb["chem_comp_atom"].rows<std::string, std::string, std::string, int, std::optional<float>, std::optional<float>, std::optional<float>, std::optional<float>, std::optional<float>, std::optional<float>>(
|
||||
"atom_id", "type_symbol", "type", "charge",
|
||||
"model_Cartn_x", "model_Cartn_y", "model_Cartn_z",
|
||||
"pdbx_model_Cartn_x_ideal", "pdbx_model_Cartn_y_ideal", "pdbx_model_Cartn_z_ideal"))
|
||||
{
|
||||
auto atom = cif::atom_type_traits(type_symbol);
|
||||
formula_weight += atom.weight();
|
||||
|
||||
formula_data[type_symbol] += 1;
|
||||
|
||||
db["chem_comp_atom"].emplace({ { "comp_id", id },
|
||||
{ "atom_id", atom_id },
|
||||
{ "type_symbol", type_symbol },
|
||||
{ "charge", charge },
|
||||
{ "model_Cartn_x", x.has_value() ? x : xi, 3 },
|
||||
{ "model_Cartn_y", y.has_value() ? y : yi, 3 },
|
||||
{ "model_Cartn_z", z.has_value() ? z : zi, 3 },
|
||||
{ "pdbx_ordinal", ord++ } });
|
||||
|
||||
formal_charge += charge;
|
||||
}
|
||||
|
||||
for (std::size_t ord = 1; const auto &[atom_id_1, atom_id_2, type, aromatic] :
|
||||
rdb["chem_comp_bond"].rows<std::string, std::string, std::string, bool>("atom_id_1", "atom_id_2", "type", "aromatic"))
|
||||
{
|
||||
std::string value_order("SING");
|
||||
|
||||
if (cif::iequals(type, "single") or cif::iequals(type, "sing"))
|
||||
value_order = "SING";
|
||||
else if (cif::iequals(type, "double") or cif::iequals(type, "doub"))
|
||||
value_order = "DOUB";
|
||||
else if (cif::iequals(type, "triple") or cif::iequals(type, "trip"))
|
||||
value_order = "TRIP";
|
||||
|
||||
db["chem_comp_bond"].emplace({ { "comp_id", id },
|
||||
{ "atom_id_1", atom_id_1 },
|
||||
{ "atom_id_2", atom_id_2 },
|
||||
{ "value_order", value_order },
|
||||
{ "pdbx_aromatic_flag", aromatic },
|
||||
// TODO: fetch stereo_config info from chem_comp_chir
|
||||
{ "pdbx_ordinal", ord++ } });
|
||||
}
|
||||
|
||||
db.emplace_back(rdb["pdbx_chem_comp_descriptor"]);
|
||||
|
||||
std::string formula;
|
||||
for (bool first = true; const auto &[symbol, count] : formula_data)
|
||||
{
|
||||
if (std::exchange(first, false))
|
||||
formula += ' ';
|
||||
formula += symbol;
|
||||
if (count > 1)
|
||||
formula += std::to_string(count);
|
||||
}
|
||||
|
||||
std::string type;
|
||||
if (cif::iequals(group, "peptide") or cif::iequals(group, "l-peptide") or cif::iequals(group, "l-peptide linking"))
|
||||
type = "L-PEPTIDE LINKING";
|
||||
else if (cif::iequals(group, "dna"))
|
||||
type = "DNA LINKING";
|
||||
else if (cif::iequals(group, "rna"))
|
||||
type = "RNA LINKING";
|
||||
else
|
||||
type = "NON-POLYMER";
|
||||
|
||||
db["chem_comp"].emplace({ { "id", id },
|
||||
{ "name", name },
|
||||
{ "type", type },
|
||||
{ "formula", formula },
|
||||
{ "pdbx_formal_charge", formal_charge },
|
||||
{ "formula_weight", formula_weight },
|
||||
{ "three_letter_code", three_letter_code } });
|
||||
|
||||
std::shared_lock lock(mMutex);
|
||||
|
||||
auto result = new compound(db);
|
||||
m_compounds.push_back(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
std::unique_ptr<compound_factory> compound_factory::s_instance;
|
||||
thread_local std::unique_ptr<compound_factory> compound_factory::tl_instance;
|
||||
compound_factory_options compound_factory::s_options;
|
||||
|
||||
void compound_factory::init(bool useThreadLocalInstanceOnly)
|
||||
{
|
||||
init({ .use_thread_local_instance_only = useThreadLocalInstanceOnly });
|
||||
}
|
||||
|
||||
void compound_factory::init(compound_factory_options options)
|
||||
{
|
||||
s_options = options;
|
||||
}
|
||||
|
||||
compound_factory::compound_factory()
|
||||
: m_impl(nullptr)
|
||||
{
|
||||
auto ccd = cif::load_resource("components.cif");
|
||||
if (ccd)
|
||||
m_impl = std::make_shared<compound_factory_impl>();
|
||||
else if (cif::VERBOSE > 0)
|
||||
std::cerr << "CCD components.cif resource was not found\n";
|
||||
}
|
||||
|
||||
compound_factory::~compound_factory()
|
||||
{
|
||||
}
|
||||
|
||||
compound_factory &compound_factory::instance()
|
||||
{
|
||||
if (s_options.use_thread_local_instance_only)
|
||||
{
|
||||
if (not tl_instance)
|
||||
tl_instance.reset(new compound_factory());
|
||||
return *tl_instance;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (not s_instance)
|
||||
s_instance.reset(new compound_factory());
|
||||
return *s_instance;
|
||||
}
|
||||
}
|
||||
|
||||
void compound_factory::clear()
|
||||
{
|
||||
if (s_options.use_thread_local_instance_only)
|
||||
tl_instance.reset(nullptr);
|
||||
else
|
||||
s_instance.reset();
|
||||
}
|
||||
|
||||
void compound_factory::set_default_dictionary(const fs::path &inDictFile)
|
||||
{
|
||||
if (not fs::exists(inDictFile))
|
||||
throw std::runtime_error("file not found: " + inDictFile.string());
|
||||
|
||||
try
|
||||
{
|
||||
m_impl.reset(new compound_factory_impl(inDictFile, m_impl));
|
||||
}
|
||||
catch (const std::exception &)
|
||||
{
|
||||
std::throw_with_nested(std::runtime_error("Error loading dictionary " + inDictFile.string()));
|
||||
}
|
||||
}
|
||||
|
||||
void compound_factory::push_dictionary(const fs::path &inDictFile)
|
||||
{
|
||||
if (not fs::exists(inDictFile))
|
||||
throw std::runtime_error("file not found: " + inDictFile.string());
|
||||
|
||||
try
|
||||
{
|
||||
m_impl.reset(new compound_factory_impl(inDictFile, m_impl));
|
||||
}
|
||||
catch (const std::exception &)
|
||||
{
|
||||
std::throw_with_nested(std::runtime_error("Error loading dictionary " + inDictFile.string()));
|
||||
}
|
||||
}
|
||||
|
||||
void compound_factory::push_dictionary(const cif::file &inDictFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
m_impl.reset(new local_compound_factory_impl(inDictFile, m_impl));
|
||||
}
|
||||
catch (const std::exception &)
|
||||
{
|
||||
std::throw_with_nested(std::runtime_error("Error loading dictionary from local mmCIF file"));
|
||||
}
|
||||
}
|
||||
|
||||
void compound_factory::pop_dictionary()
|
||||
{
|
||||
if (m_impl)
|
||||
m_impl = m_impl->next();
|
||||
}
|
||||
|
||||
const compound *compound_factory::create(std::string_view id)
|
||||
{
|
||||
auto result = m_impl ? m_impl->get(std::string{ id }) : nullptr;
|
||||
if (not result)
|
||||
report_missing_compound(id);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool compound_factory::is_known_peptide(const std::string &res_name) const
|
||||
{
|
||||
return kAAMap.count(res_name) > 0;
|
||||
}
|
||||
|
||||
bool compound_factory::is_known_base(const std::string &res_name) const
|
||||
{
|
||||
return kBaseMap.count(res_name) > 0;
|
||||
}
|
||||
|
||||
/// Return whether @a res_name is a peptide
|
||||
bool compound_factory::is_peptide(std::string_view res_name) const
|
||||
{
|
||||
bool result = is_std_peptide(res_name);
|
||||
if (not result and m_impl)
|
||||
{
|
||||
auto compound = const_cast<compound_factory &>(*this).create(res_name);
|
||||
result = compound != nullptr and compound->is_peptide();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Return whether @a res_name is a base
|
||||
bool compound_factory::is_base(std::string_view res_name) const
|
||||
{
|
||||
bool result = is_std_base(res_name);
|
||||
if (not result and m_impl)
|
||||
{
|
||||
auto compound = const_cast<compound_factory &>(*this).create(res_name);
|
||||
result = compound != nullptr and compound->is_base();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Return whether @a res_name is one of the standard peptides
|
||||
bool compound_factory::is_std_peptide(std::string_view res_name) const
|
||||
{
|
||||
return kAAMap.count(std::string{ res_name }) > 0;
|
||||
}
|
||||
|
||||
/// Return whether @a res_name is one of the standard bases
|
||||
bool compound_factory::is_std_base(std::string_view res_name) const
|
||||
{
|
||||
return kBaseMap.count(std::string{ res_name }) > 0;
|
||||
}
|
||||
|
||||
/// Return whether @a res_name is a monomer (either base or peptide)
|
||||
bool compound_factory::is_monomer(std::string_view res_name) const
|
||||
{
|
||||
return is_peptide(res_name) or is_base(res_name);
|
||||
}
|
||||
|
||||
void compound_factory::report_missing_compound(std::string_view compound_id)
|
||||
{
|
||||
static bool s_reported = false;
|
||||
if (std::exchange(s_reported, true) == false)
|
||||
{
|
||||
using namespace cif::colour;
|
||||
|
||||
std::clog << "\n"
|
||||
<< cif::coloured("Configuration error:", white, red) << "\n\n"
|
||||
<< "The attempt to retrieve compound information for " << std::quoted(compound_id) << " failed.\n\n"
|
||||
<< "This information is searched for in a CCD file called components.cif or\n"
|
||||
<< "components.cif.gz which should be located in one of the following directories:\n\n";
|
||||
|
||||
cif::list_data_directories(std::clog);
|
||||
|
||||
std::clog << "\n(Note that you can add a directory to the search paths by setting the \n"
|
||||
<< "LIBCIFPP_DATA_DIR environmental variable)\n\n";
|
||||
|
||||
#if defined(CACHE_DIR)
|
||||
std::clog << "On Linux an optional cron script might have been installed that automatically updates\n"
|
||||
<< "components.cif and mmCIF dictionary files. This script only works when the file\n"
|
||||
<< "libcifpp.conf contains an uncommented line with the text:\n\n"
|
||||
<< "update=true\n\n"
|
||||
<< "If you do not have a working cron script, you can manually update the files\n"
|
||||
<< "in /var/cache/libcifpp using the following commands:\n\n"
|
||||
<< "curl -o " << CACHE_DIR << "/components.cif https://files.wwpdb.org/pub/pdb/data/monomers/components.cif\n"
|
||||
<< "curl -o " << CACHE_DIR << "/mmcif_pdbx.dic https://mmcif.wwpdb.org/dictionaries/ascii/mmcif_pdbx_v50.dic\n"
|
||||
<< "curl -o " << CACHE_DIR << "/mmcif_ma.dic https://github.com/ihmwg/ModelCIF/raw/master/dist/mmcif_ma.dic\n\n";
|
||||
#endif
|
||||
|
||||
if (m_impl)
|
||||
{
|
||||
std::clog << "The current order of compound factory objects is:\n\n";
|
||||
m_impl->describe(std::clog);
|
||||
}
|
||||
else
|
||||
std::clog << "No compound factory objects are created since none of the data sources is found.\n";
|
||||
|
||||
cif::list_file_resources(std::clog);
|
||||
|
||||
std::clog.flush();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace cif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user