MMTF-C++
The C++ language MMTF libraries
Loading...
Searching...
No Matches
export_helpers.hpp
Go to the documentation of this file.
1// *************************************************************************
2//
3// Licensed under the MIT License (see accompanying LICENSE file).
4//
5// The authors of this code are: Thomas Holder
6//
7// *************************************************************************
8//
9// Helper functions and classes for exporting MMTF data.
10// See "examples/tableexport.cpp" for example usage.
11//
12// *************************************************************************
13
14#ifndef MMTF_EXPORT_HELPERS_H
15#define MMTF_EXPORT_HELPERS_H
16
17#include "errors.hpp"
18#include "structure_data.hpp"
19
20#include <vector>
21
22namespace mmtf
23{
24
33class BondAdder {
34 StructureData* m_data;
35 std::vector<int32_t> m_atom2groupType;
36 std::vector<int32_t> m_atomOffsets;
37
38public:
45 : m_data(&data), m_atomOffsets(data.groupTypeList.size(), -1)
46 {
47 m_atom2groupType.reserve(data.numAtoms);
48
49 for (size_t i = 0; i < data.groupTypeList.size(); ++i) {
50 int32_t groupType = data.groupTypeList[i];
51
52 // sanity check
53 if (m_atomOffsets[groupType] != -1) {
54 throw EncodeError("groupTypeList has duplicates");
55 }
56
57 size_t atomOffset = m_atom2groupType.size();
58 size_t groupSize = data.groupList[groupType].atomNameList.size();
59
60 m_atomOffsets[groupType] = atomOffset;
61 m_atom2groupType.resize(atomOffset + groupSize, groupType);
62 }
63 }
64
74 bool operator()(int32_t atom1, int32_t atom2, int8_t order)
75 {
76 if (atom1 >= m_atom2groupType.size() ||
77 atom2 >= m_atom2groupType.size())
78 return false;
79
80 if (m_atom2groupType[atom1] == m_atom2groupType[atom2]) {
81 int32_t groupType = m_atom2groupType[atom1];
82 GroupType& group = m_data->groupList[groupType];
83 group.bondAtomList.push_back(atom1 - m_atomOffsets[groupType]);
84 group.bondAtomList.push_back(atom2 - m_atomOffsets[groupType]);
85 group.bondOrderList.push_back(order);
86 } else {
87 m_data->bondAtomList.push_back(atom1);
88 m_data->bondAtomList.push_back(atom2);
89 m_data->bondOrderList.push_back(order);
90 }
91
92 ++m_data->numBonds;
93 return true;
94 }
95};
96
105{
106 size_t n_old = data.groupList.size();
107 size_t i_free = 0;
108 std::vector<size_t> idremap(n_old, 0);
109
110 for (size_t i = 1; i < n_old; ++i) {
111 size_t i_found = 0;
112
113 while (i_found < i && !(data.groupList[i] == data.groupList[i_found])) {
114 ++i_found;
115 }
116
117 if (i_found == i) {
118 if (i_free != 0) {
119 data.groupList[i_free] = data.groupList[i]; // std::move possible with C++11
120 i_found = i_free;
121 ++i_free;
122 }
123 } else if (i_free == 0) {
124 i_free = i;
125 }
126
127 idremap[i] = i_found;
128 }
129
130 if (i_free != 0) {
131 data.groupList.resize(i_free);
132
133 for (size_t i = 0; i < data.groupTypeList.size(); ++i) {
134 data.groupTypeList[i] = idremap[data.groupTypeList[i]];
135 }
136 }
137}
138
139} // namespace mmtf
140
141#endif
142
143// vi:sw=2:expandtab
BondAdder(StructureData &data)
Definition export_helpers.hpp:44
bool operator()(int32_t atom1, int32_t atom2, int8_t order)
Add one bond.
Definition export_helpers.hpp:74
Exception thrown when failing during encoding.
Definition errors.hpp:31
Definition binary_decoder.hpp:25
void compressGroupList(StructureData &data)
Eliminate redundant groups from groupList.
Definition export_helpers.hpp:104
Group (residue) level data store.
Definition structure_data.hpp:53
std::vector< int8_t > bondOrderList
Definition structure_data.hpp:58
std::vector< int32_t > bondAtomList
Definition structure_data.hpp:57
Top level MMTF data container.
Definition structure_data.hpp:157
std::vector< GroupType > groupList
Definition structure_data.hpp:178
std::vector< int32_t > groupTypeList
Definition structure_data.hpp:190
int32_t numAtoms
Definition structure_data.hpp:174