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 
21 namespace mmtf {
22 
35 inline 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 
48 template <typename Stream>
49 inline 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 
62 inline std::map<std::string, msgpack::object>
63 encodeToMap(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 // *************************************************************************
70 inline 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 
82 template <typename Stream>
83 inline 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 
91 inline std::map<std::string, msgpack::object>
92 encodeToMap(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  }
107  if (!mmtf::isDefaultValue(data.structureId)) {
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  }
116  if (!mmtf::isDefaultValue(data.releaseDate)) {
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);
121  if (!mmtf::isDefaultValue(data.chainNameList)) {
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  }
133  if (!mmtf::isDefaultValue(data.insCodeList)) {
134  data_map["insCodeList"] = msgpack::object(mmtf::encodeRunLengthChar(data.insCodeList), m_zone);
135  }
136  // std::vector<int8_t>
137  if (!mmtf::isDefaultValue(data.bondOrderList)) {
138  data_map["bondOrderList"] =
139  msgpack::object(mmtf::encodeInt8ToByte(data.bondOrderList), m_zone);
140  }
141  if (!mmtf::isDefaultValue(data.secStructList)) {
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);
156  if (!mmtf::isDefaultValue(data.bondAtomList)) {
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);
179  if (!mmtf::isDefaultValue(data.bFactorList)) {
180  data_map["bFactorList"] = msgpack::object(mmtf::encodeDeltaRecursiveFloat(data.bFactorList, occupancy_b_factor_divider), m_zone);
181  }
182  if (!mmtf::isDefaultValue(data.occupancyList)) {
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
mmtf::encodeToStream
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
mmtf::StructureData::sequenceIndexList
std::vector< int32_t > sequenceIndexList
Definition: structure_data.hpp:186
mmtf::StructureData::unitCell
std::vector< float > unitCell
Definition: structure_data.hpp:154
mmtf::encodeInt8ToByte
std::vector< char > encodeInt8ToByte(std::vector< int8_t > vec_in)
Definition: binary_encoder.hpp:240
mmtf::StructureData::bFactorList
std::vector< float > bFactorList
Definition: structure_data.hpp:178
mmtf::StructureData::occupancyList
std::vector< float > occupancyList
Definition: structure_data.hpp:181
mmtf::StructureData::rWork
float rWork
Definition: structure_data.hpp:166
mmtf::StructureData::numChains
int32_t numChains
Definition: structure_data.hpp:170
mmtf::StructureData
Top level MMTF data container.
Definition: structure_data.hpp:151
mmtf::encodeToFile
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
mmtf::StructureData::resolution
float resolution
Definition: structure_data.hpp:164
errors.hpp
mmtf::StructureData::numBonds
int32_t numBonds
Definition: structure_data.hpp:167
mmtf::StructureData::xCoordList
std::vector< float > xCoordList
Definition: structure_data.hpp:175
mmtf::StructureData::yCoordList
std::vector< float > yCoordList
Definition: structure_data.hpp:176
mmtf::StructureData::groupsPerChain
std::vector< int32_t > groupsPerChain
Definition: structure_data.hpp:189
mmtf::StructureData::chainsPerModel
std::vector< int32_t > chainsPerModel
Definition: structure_data.hpp:190
mmtf::StructureData::groupList
std::vector< GroupType > groupList
Definition: structure_data.hpp:172
mmtf::StructureData::ncsOperatorList
std::vector< std::vector< float > > ncsOperatorList
Definition: structure_data.hpp:160
mmtf::encodeToMap
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
mmtf::StructureData::spaceGroup
std::string spaceGroup
Definition: structure_data.hpp:155
mmtf::StructureData::groupTypeList
std::vector< int32_t > groupTypeList
Definition: structure_data.hpp:183
mmtf::StructureData::rFree
float rFree
Definition: structure_data.hpp:165
mmtf::StructureData::structureId
std::string structureId
Definition: structure_data.hpp:156
mmtf::StructureData::bondAtomList
std::vector< int32_t > bondAtomList
Definition: structure_data.hpp:173
mmtf::encodeRunLengthChar
std::vector< char > encodeRunLengthChar(std::vector< char > in_cv)
Definition: binary_encoder.hpp:279
structure_data.hpp
object_encoders.hpp
mmtf::StructureData::secStructList
std::vector< int8_t > secStructList
Definition: structure_data.hpp:184
mmtf::StructureData::entityList
std::vector< Entity > entityList
Definition: structure_data.hpp:162
mmtf::isDefaultValue
bool isDefaultValue(const T &value)
Definition: structure_data.hpp:373
mmtf::StructureData::insCodeList
std::vector< char > insCodeList
Definition: structure_data.hpp:185
mmtf::StructureData::atomIdList
std::vector< int32_t > atomIdList
Definition: structure_data.hpp:179
mmtf::StructureData::numAtoms
int32_t numAtoms
Definition: structure_data.hpp:168
mmtf::StructureData::altLocList
std::vector< char > altLocList
Definition: structure_data.hpp:180
mmtf::encodeRunLengthFloat
std::vector< char > encodeRunLengthFloat(std::vector< float > floats_in, int32_t multiplier)
Definition: binary_encoder.hpp:305
mmtf::StructureData::groupIdList
std::vector< int32_t > groupIdList
Definition: structure_data.hpp:182
mmtf::StructureData::hasConsistentData
bool hasConsistentData(bool verbose=false, uint32_t chain_name_max_length=4) const
Check consistency of structural data.
Definition: structure_data.hpp:458
mmtf::encodeRunLengthDeltaInt
std::vector< char > encodeRunLengthDeltaInt(std::vector< int32_t > int_vec)
Definition: binary_encoder.hpp:293
mmtf::StructureData::bioAssemblyList
std::vector< BioAssembly > bioAssemblyList
Definition: structure_data.hpp:161
mmtf::encodeFourByteInt
std::vector< char > encodeFourByteInt(std::vector< int32_t > vec_in)
Definition: binary_encoder.hpp:250
mmtf::StructureData::mmtfProducer
std::string mmtfProducer
Definition: structure_data.hpp:153
mmtf::StructureData::experimentalMethods
std::vector< std::string > experimentalMethods
Definition: structure_data.hpp:163
mmtf::StructureData::numGroups
int32_t numGroups
Definition: structure_data.hpp:169
mmtf::StructureData::zCoordList
std::vector< float > zCoordList
Definition: structure_data.hpp:177
mmtf::EncodeError
Exception thrown when failing during encoding.
Definition: errors.hpp:31
mmtf::StructureData::chainNameList
std::vector< std::string > chainNameList
Definition: structure_data.hpp:188
mmtf::encodeStringVector
std::vector< char > encodeStringVector(std::vector< std::string > in_sv, int32_t CHAIN_LEN)
Definition: binary_encoder.hpp:261
mmtf::StructureData::numModels
int32_t numModels
Definition: structure_data.hpp:171
mmtf::StructureData::depositionDate
std::string depositionDate
Definition: structure_data.hpp:158
mmtf::StructureData::bondOrderList
std::vector< int8_t > bondOrderList
Definition: structure_data.hpp:174
mmtf
Definition: binary_decoder.hpp:24
binary_encoder.hpp
mmtf::StructureData::chainIdList
std::vector< std::string > chainIdList
Definition: structure_data.hpp:187
mmtf::StructureData::title
std::string title
Definition: structure_data.hpp:157
mmtf::encodeDeltaRecursiveFloat
std::vector< char > encodeDeltaRecursiveFloat(std::vector< float > floats_in, int32_t multiplier)
Definition: binary_encoder.hpp:319
mmtf::StructureData::releaseDate
std::string releaseDate
Definition: structure_data.hpp:159
mmtf::StructureData::mmtfVersion
std::string mmtfVersion
Definition: structure_data.hpp:152