-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathflat.cpp
More file actions
90 lines (84 loc) · 2.89 KB
/
flat.cpp
File metadata and controls
90 lines (84 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright Global Phasing Ltd.
#include "gemmi/flat.hpp"
#include "gemmi/calculate.hpp" // for count_atom_sites
#include <cstring> // for memcpy, memset
namespace gemmi {
namespace {
template<size_t N>
void copy_padded(char (&dest)[N], const std::string& src) {
std::memcpy(dest, src.c_str(), src.size());
std::memset(dest + src.size(), 0, N - src.size());
}
} // anonymous namespace
FlatStructure::FlatStructure(const Structure& st) {
empty_st = st.empty_copy();
size_t n = count_atom_sites(st);
table.reserve(n);
FlatAtom fa;
for (const Model& model : st.models) {
fa.model_num = model.num;
for (const Chain& chain : model.chains) {
if (chain.name.size() > 7)
fail("FlatStructure doesn't support 8+ char subchain names: ", chain.name);
copy_padded(fa.chain_id, chain.name);
for (const Residue& res : chain.residues) {
if (res.name.size() > 7)
fail("FlatStructure doesn't support 8+ char residue names: ", res.name);
copy_padded(fa.residue_name, res.name);
if (res.subchain.size() > 7)
fail("FlatStructure doesn't support 8+ char subchain names: ", res.subchain);
copy_padded(fa.subchain, res.subchain);
if (res.entity_id.size() > 7)
fail("FlatStructure doesn't support 8+ char entity IDs: ", res.entity_id);
copy_padded(fa.entity_id, res.entity_id);
fa.seq_id = res.seqid;
fa.het_flag = res.het_flag;
fa.entity_type = res.entity_type;
for (const Atom& atom : res.atoms) {
if (atom.name.size() > 7)
fail("FlatStructure doesn't support 8+ char atom names: ", atom.name);
copy_padded(fa.atom_name, atom.name);
fa.pos = atom.pos;
fa.occ = atom.occ;
fa.b_iso = atom.b_iso;
fa.altloc = atom.altloc;
fa.element = atom.element;
fa.charge = atom.charge;
fa.aniso = atom.aniso;
fa.serial = atom.serial;
table.push_back(fa);
}
}
}
}
}
Structure FlatStructure::generate_structure() {
Structure st(empty_st);
for (const FlatAtom& fa : table) {
Model& model = st.find_or_add_model(fa.model_num);
Chain* chain = model.find_chain(fa.chain_id);
if (!chain) {
model.chains.emplace_back(fa.chain_id);
chain = &model.chains.back();
}
ResidueId rid{fa.seq_id, {}, fa.residue_name};
Residue* residue = chain->find_or_add_residue(rid);
residue->het_flag = fa.het_flag;
residue->subchain = fa.subchain;
residue->entity_id = fa.entity_id;
residue->entity_type = fa.entity_type;
Atom atom;
atom.name = fa.atom_name;
atom.pos = fa.pos;
atom.occ = fa.occ;
atom.b_iso = fa.b_iso;
atom.altloc = fa.altloc;
atom.element = fa.element;
atom.charge = fa.charge;
atom.aniso = fa.aniso;
atom.serial = fa.serial;
residue->atoms.emplace_back(atom);
}
return st;
}
} // namespace gemmi