MMTF-C++
The C++ language MMTF libraries
encoder.hpp
Go to the documentation of this file.
1// *************************************************************************
2//
3// Licensed under the MIT License (see accompanying LICENSE file).
4//
5// The author of this code is: Daniel Farrell
6//
7// Based on mmtf_python, adapted to c++ standards 2018
8//
9// *************************************************************************
10
11
12#ifndef MMTF_ENCODER_H
13#define MMTF_ENCODER_H
14
15#include "structure_data.hpp"
16#include "errors.hpp"
17#include "object_encoders.hpp"
18#include "binary_encoder.hpp"
19#include <string>
20
21namespace mmtf {
22
35inline void encodeToFile(const StructureData& data,
36 const std::string& filename, int32_t coord_divider = 1000,
37 int32_t occupancy_b_factor_divider = 100,
38 int32_t chain_name_max_length = 4);
39
48template <typename Stream>
49inline void encodeToStream(const StructureData& data, Stream& stream,
50 int32_t coord_divider = 1000, int32_t occupancy_b_factor_divider = 100,
51 int32_t chain_name_max_length = 4);
52
62inline std::map<std::string, msgpack::object>
63encodeToMap(const StructureData& data, msgpack::zone& m_zone,
64 int32_t coord_divider = 1000, int32_t occupancy_b_factor_divider = 100,
65 int32_t chain_name_max_length = 4);
66
67// *************************************************************************
68// IMPLEMENTATION
69// *************************************************************************
70inline void encodeToFile(const StructureData& data,
71 const std::string& filename, int32_t coord_divider,
72 int32_t occupancy_b_factor_divider, int32_t chain_name_max_length) {
73 // encode to a file
74 std::ofstream ofs(filename.c_str(), std::ios::binary | std::ios::out );
75 if ( !ofs ) {
76 throw EncodeError("Could not open >" + filename + "< for writing, exiting.");
77 }
78 encodeToStream(data, ofs, coord_divider,
79 occupancy_b_factor_divider, chain_name_max_length);
80}
81
82template <typename Stream>
83inline void encodeToStream(const StructureData& data, Stream& stream,
84 int32_t coord_divider, int32_t occupancy_b_factor_divider,
85 int32_t chain_name_max_length) {
86 msgpack::zone _zone;
87 msgpack::pack(stream, encodeToMap(data, _zone, coord_divider,
88 occupancy_b_factor_divider, chain_name_max_length));
89}
90
91inline std::map<std::string, msgpack::object>
92encodeToMap(const StructureData& data, msgpack::zone& m_zone,
93 int32_t coord_divider, int32_t occupancy_b_factor_divider,
94 int32_t chain_name_max_length) {
95 if (!data.hasConsistentData(true, chain_name_max_length)) {
96 throw mmtf::EncodeError("mmtf EncoderError, StructureData does not have Consistent data... exiting!");
97 }
98
99
100 std::map<std::string, msgpack::object> data_map;
101 // std::string
102 data_map["mmtfVersion"] = msgpack::object(data.mmtfVersion, m_zone);
103 data_map["mmtfProducer"] = msgpack::object(data.mmtfProducer, m_zone);
104 if (!mmtf::isDefaultValue(data.spaceGroup)) {
105 data_map["spaceGroup"] = msgpack::object(data.spaceGroup, m_zone);
106 }
108 data_map["structureId"] = msgpack::object(data.structureId, m_zone);
109 }
110 if (!mmtf::isDefaultValue(data.title)) {
111 data_map["title"] = msgpack::object(data.title, m_zone);
112 }
114 data_map["depositionDate"] = msgpack::object(data.depositionDate, m_zone);
115 }
117 data_map["releaseDate"] = msgpack::object(data.releaseDate, m_zone);
118 }
119 // std::vector<std::string>
120 data_map["chainIdList"] = msgpack::object(mmtf::encodeStringVector(data.chainIdList, chain_name_max_length), m_zone);
122 data_map["chainNameList"] = msgpack::object(mmtf::encodeStringVector(data.chainNameList, chain_name_max_length), m_zone);
123 }
125 data_map["experimentalMethods"] =
126 msgpack::object(data.experimentalMethods, m_zone);
127 }
128 // std::vector<char>
129 if (!mmtf::isDefaultValue(data.altLocList)) {
130 data_map["altLocList"] =
131 msgpack::object(mmtf::encodeRunLengthChar(data.altLocList), m_zone);
132 }
134 data_map["insCodeList"] = msgpack::object(mmtf::encodeRunLengthChar(data.insCodeList), m_zone);
135 }
136 // std::vector<int8_t>
138 data_map["bondOrderList"] =
139 msgpack::object(mmtf::encodeInt8ToByte(data.bondOrderList), m_zone);
140 }
142 data_map["secStructList"] =
143 msgpack::object(mmtf::encodeInt8ToByte(data.secStructList), m_zone);
144 }
145 // int32_t
146 data_map["numBonds"] = msgpack::object(data.numBonds, m_zone);
147 data_map["numAtoms"] = msgpack::object(data.numAtoms, m_zone);
148 data_map["numGroups"] = msgpack::object(data.numGroups, m_zone);
149 data_map["numChains"] = msgpack::object(data.numChains, m_zone);
150 data_map["numModels"] = msgpack::object(data.numModels, m_zone);
151 // std::vector<int32_t>
152 data_map["groupTypeList"] = msgpack::object(mmtf::encodeFourByteInt(data.groupTypeList), m_zone);
153 data_map["groupIdList"] = msgpack::object(mmtf::encodeRunLengthDeltaInt(data.groupIdList), m_zone);
154 data_map["groupsPerChain"] = msgpack::object(data.groupsPerChain, m_zone);
155 data_map["chainsPerModel"] = msgpack::object(data.chainsPerModel, m_zone);
157 data_map["bondAtomList"] = msgpack::object(mmtf::encodeFourByteInt(data.bondAtomList), m_zone);
158 }
159 if (!mmtf::isDefaultValue(data.atomIdList)) {
160 data_map["atomIdList"] = msgpack::object(mmtf::encodeRunLengthDeltaInt(data.atomIdList), m_zone);
161 }
163 data_map["sequenceIndexList"] = msgpack::object(mmtf::encodeRunLengthDeltaInt(data.sequenceIndexList), m_zone);
164 }
165 // float
166 if (!mmtf::isDefaultValue(data.resolution)) {
167 data_map["resolution"] = msgpack::object(data.resolution, m_zone);
168 }
169 if (!mmtf::isDefaultValue(data.rFree)) {
170 data_map["rFree"] = msgpack::object(data.rFree, m_zone);
171 }
172 if (!mmtf::isDefaultValue(data.rWork)) {
173 data_map["rWork"] = msgpack::object(data.rWork, m_zone);
174 }
175 // std::vector<float>
176 data_map["xCoordList"] = msgpack::object(mmtf::encodeDeltaRecursiveFloat(data.xCoordList, coord_divider), m_zone);
177 data_map["yCoordList"] = msgpack::object(mmtf::encodeDeltaRecursiveFloat(data.yCoordList, coord_divider), m_zone);
178 data_map["zCoordList"] = msgpack::object(mmtf::encodeDeltaRecursiveFloat(data.zCoordList, coord_divider), m_zone);
180 data_map["bFactorList"] = msgpack::object(mmtf::encodeDeltaRecursiveFloat(data.bFactorList, occupancy_b_factor_divider), m_zone);
181 }
183 data_map["occupancyList"] = msgpack::object(mmtf::encodeRunLengthFloat(data.occupancyList, occupancy_b_factor_divider), m_zone);
184 }
185 if (!mmtf::isDefaultValue(data.unitCell)) {
186 data_map["unitCell"] = msgpack::object(data.unitCell, m_zone);
187 }
188 // std::vector<GroupType>
189 data_map["groupList"] = msgpack::object(data.groupList, m_zone);
190 // std::vector<BioAssembly>
192 data_map["bioAssemblyList"] = msgpack::object(data.bioAssemblyList, m_zone);
193 }
194 // std::vector<Entity>
195 if (!mmtf::isDefaultValue(data.entityList)) {
196 data_map["entityList"] = msgpack::object(data.entityList, m_zone);
197 }
198 // std::vector<std::vector<float>>
200 data_map["ncsOperatorList"] = msgpack::object(data.ncsOperatorList, m_zone);
201 }
202 return data_map;
203}
204
205} // mmtf namespace
206
207#endif
Exception thrown when failing during encoding.
Definition: errors.hpp:31
Definition: binary_decoder.hpp:24
std::vector< char > encodeFourByteInt(std::vector< int32_t > vec_in)
Definition: binary_encoder.hpp:250
void encodeToFile(const StructureData &data, const std::string &filename, int32_t coord_divider=1000, int32_t occupancy_b_factor_divider=100, int32_t chain_name_max_length=4)
Encode an MMTF data structure into a file.
Definition: encoder.hpp:70
std::vector< char > encodeInt8ToByte(std::vector< int8_t > vec_in)
Definition: binary_encoder.hpp:240
std::vector< char > encodeDeltaRecursiveFloat(std::vector< float > floats_in, int32_t multiplier)
Definition: binary_encoder.hpp:319
std::vector< char > encodeStringVector(std::vector< std::string > in_sv, int32_t CHAIN_LEN)
Definition: binary_encoder.hpp:261
std::map< std::string, msgpack::object > encodeToMap(const StructureData &data, msgpack::zone &m_zone, int32_t coord_divider=1000, int32_t occupancy_b_factor_divider=100, int32_t chain_name_max_length=4)
Encode an MMTF data structure into a map of msgpack objects.
Definition: encoder.hpp:92
std::vector< char > encodeRunLengthFloat(std::vector< float > floats_in, int32_t multiplier)
Definition: binary_encoder.hpp:305
std::vector< char > encodeRunLengthChar(std::vector< char > in_cv)
Definition: binary_encoder.hpp:279
void encodeToStream(const StructureData &data, Stream &stream, int32_t coord_divider=1000, int32_t occupancy_b_factor_divider=100, int32_t chain_name_max_length=4)
Encode an MMTF data structure into a stream.
Definition: encoder.hpp:83
bool isDefaultValue(const T &value)
Definition: structure_data.hpp:373
std::vector< char > encodeRunLengthDeltaInt(std::vector< int32_t > int_vec)
Definition: binary_encoder.hpp:293
Top level MMTF data container.
Definition: structure_data.hpp:151
int32_t numBonds
Definition: structure_data.hpp:167
std::string mmtfVersion
Definition: structure_data.hpp:152
float rFree
Definition: structure_data.hpp:165
std::vector< int32_t > sequenceIndexList
Definition: structure_data.hpp:186
std::vector< char > altLocList
Definition: structure_data.hpp:180
std::vector< int8_t > bondOrderList
Definition: structure_data.hpp:174
std::string structureId
Definition: structure_data.hpp:156
int32_t numGroups
Definition: structure_data.hpp:169
std::vector< float > zCoordList
Definition: structure_data.hpp:177
int32_t numModels
Definition: structure_data.hpp:171
std::vector< float > xCoordList
Definition: structure_data.hpp:175
std::vector< int32_t > groupsPerChain
Definition: structure_data.hpp:189
std::vector< float > bFactorList
Definition: structure_data.hpp:178
std::vector< int32_t > atomIdList
Definition: structure_data.hpp:179
int32_t numChains
Definition: structure_data.hpp:170
std::vector< std::string > chainIdList
Definition: structure_data.hpp:187
std::string spaceGroup
Definition: structure_data.hpp:155
std::vector< int32_t > groupIdList
Definition: structure_data.hpp:182
std::string depositionDate
Definition: structure_data.hpp:158
std::vector< int32_t > bondAtomList
Definition: structure_data.hpp:173
std::vector< GroupType > groupList
Definition: structure_data.hpp:172
std::vector< char > insCodeList
Definition: structure_data.hpp:185
bool hasConsistentData(bool verbose=false, uint32_t chain_name_max_length=4) const
Check consistency of structural data.
Definition: structure_data.hpp:458
float rWork
Definition: structure_data.hpp:166
float resolution
Definition: structure_data.hpp:164
std::string mmtfProducer
Definition: structure_data.hpp:153
std::vector< std::vector< float > > ncsOperatorList
Definition: structure_data.hpp:160
std::vector< int32_t > chainsPerModel
Definition: structure_data.hpp:190
std::vector< int32_t > groupTypeList
Definition: structure_data.hpp:183
std::vector< BioAssembly > bioAssemblyList
Definition: structure_data.hpp:161
std::vector< std::string > chainNameList
Definition: structure_data.hpp:188
std::string releaseDate
Definition: structure_data.hpp:159
std::vector< float > unitCell
Definition: structure_data.hpp:154
std::vector< int8_t > secStructList
Definition: structure_data.hpp:184
std::vector< Entity > entityList
Definition: structure_data.hpp:162
int32_t numAtoms
Definition: structure_data.hpp:168
std::vector< float > occupancyList
Definition: structure_data.hpp:181
std::vector< float > yCoordList
Definition: structure_data.hpp:176
std::vector< std::string > experimentalMethods
Definition: structure_data.hpp:163
std::string title
Definition: structure_data.hpp:157