Gemmi C++ API
Loading...
Searching...
No Matches
mmread.hpp
Go to the documentation of this file.
1// Copyright 2017 Global Phasing Ltd.
2//
3// Read any supported coordinate file. Usually, mmread_gz.hpp is preferred.
4
5#ifndef GEMMI_MMREAD_HPP_
6#define GEMMI_MMREAD_HPP_
7
8#include "cif.hpp" // for cif::read
9#include "fail.hpp" // for fail
10#include "input.hpp" // for BasicInput
11#include "json.hpp" // for read_mmjson
12#include "mmcif.hpp" // for make_structure_from_block, ...
13#include "model.hpp" // for Structure
14#include "pdb.hpp" // for read_pdb
15#include "util.hpp" // for iends_with
16
17namespace gemmi {
18
19inline CoorFormat coor_format_from_ext(const std::string& path) {
20 if (iends_with(path, ".pdb") || iends_with(path, ".ent"))
21 return CoorFormat::Pdb;
22 if (iends_with(path, ".cif") || iends_with(path, ".mmcif"))
23 return CoorFormat::Mmcif;
24 if (iends_with(path, ".json"))
25 return CoorFormat::Mmjson;
27}
28
29// If it's neither CIF nor JSON nor almost empty - we assume PDB.
30inline CoorFormat coor_format_from_content(const char* buf, const char* end) {
31 while (buf < end - 8) {
32 if (std::isspace(*buf)) {
33 ++buf;
34 } else if (*buf == '#') {
35 while (buf < end - 8 && *buf != '\n')
36 ++buf;
37 } else if (*buf == '{') {
38 return CoorFormat::Mmjson;
39 } else if (ialpha4_id(buf) == ialpha4_id("data") && buf[4] == '_') {
40 return CoorFormat::Mmcif;
41 } else {
42 return CoorFormat::Pdb;
43 }
44 }
46}
47
49 cif::Document* save_doc=nullptr) {
51 // check for special case - refmac dictionary or CCD file
53 if (n != -1)
55 }
56 return make_structure(std::move(doc), save_doc);
57}
58
59// when reading JSON, the input buffer is changed (as an optimization)
60inline Structure read_structure_from_memory(char* data, size_t size,
61 const std::string& path,
63 cif::Document* save_doc=nullptr) {
64 if (save_doc)
65 save_doc->clear();
67 format = coor_format_from_content(data, data + size);
69 return read_pdb_from_memory(data, size, path);
71 return make_structure_from_doc(cif::read_memory(data, size, path.c_str()),
72 true, save_doc);
74 return make_structure(cif::read_mmjson_insitu(data, size, path), save_doc);
75 fail("wrong format of coordinate file " + path);
76}
77
78// deprecated
79inline Structure read_structure_from_char_array(char* data, size_t size,
80 const std::string& path,
81 cif::Document* save_doc=nullptr) {
83}
84
85template<typename T>
87 cif::Document* save_doc=nullptr) {
90 return read_structure_from_memory(mem.data(), mem.size(), input.path(), format, save_doc);
91 }
92 if (save_doc)
93 save_doc->clear();
95 format = coor_format_from_ext(input.basepath());
96 switch (format) {
97 case CoorFormat::Pdb:
98 return read_pdb(input);
101 case CoorFormat::Mmjson: {
103 st.input_format = CoorFormat::Mmjson;
104 return st;
105 }
110 fail("Unknown format of " +
111 (input.path().empty() ? "coordinate file" : input.path()) + ".");
112 }
113 unreachable();
114}
115
116inline Structure read_structure_file(const std::string& path,
118 return read_structure(BasicInput(path), format);
119}
120
121} // namespace gemmi
122#endif
CIF parser (based on PEGTL) with pluggable actions, and a set of actions that prepare Document....
fail(), unreachable() and __declspec/__attribute__ macros
Input abstraction. Used to decouple file reading and decompression.
Reading CIF-JSON (COMCIFS) and mmJSON (PDBj) formats into cif::Document.
Read mmCIF (PDBx/mmCIF) file into a Structure from model. hpp.
Data structures to store macromolecular structure models.
Document read(T &&input, int check_level=1)
Definition cif.hpp:348
Document read_mmjson(T &&input)
Definition json.hpp:25
GEMMI_DLL Document read_mmjson_insitu(char *buffer, std::size_t size, const std::string &name="mmJSON")
Document read_memory(const char *data, size_t size, const char *name, int check_level=1)
Definition cif.hpp:310
CharArray read_into_buffer(T &&input)
Definition fileutil.hpp:164
Structure make_structure_from_chemcomp_block(const cif::Block &block, int which=7)
Definition mmcif.hpp:55
Structure read_structure_from_char_array(char *data, size_t size, const std::string &path, cif::Document *save_doc=nullptr)
Definition mmread.hpp:79
Structure read_structure(T &&input, CoorFormat format=CoorFormat::Unknown, cif::Document *save_doc=nullptr)
Definition mmread.hpp:86
CoorFormat coor_format_from_content(const char *buf, const char *end)
Definition mmread.hpp:30
Structure make_structure_from_doc(cif::Document &&doc, bool possible_chemcomp, cif::Document *save_doc=nullptr)
Definition mmread.hpp:48
Structure read_pdb(T &&input, PdbReadOptions options={})
Definition pdb.hpp:58
Structure make_structure(cif::Document &&doc, cif::Document *save_doc=nullptr)
structure from a coordinate mmCIF document
Definition mmcif.hpp:19
int check_chemcomp_block_number(const cif::Document &doc)
Definition mmcif.hpp:75
CoorFormat coor_format_from_ext(const std::string &path)
Definition mmread.hpp:19
CoorFormat
File format of a macromolecular model.
Definition model.hpp:88
bool iends_with(const std::string &str, const std::string &suffix)
Definition util.hpp:99
Structure read_pdb_from_memory(const char *data, size_t size, const std::string &name, PdbReadOptions options={})
Definition pdb.hpp:44
Structure read_structure_file(const std::string &path, CoorFormat format=CoorFormat::Unknown)
Definition mmread.hpp:116
Structure read_structure_from_memory(char *data, size_t size, const std::string &path, CoorFormat format=CoorFormat::Unknown, cif::Document *save_doc=nullptr)
Definition mmread.hpp:60
void fail(const std::string &msg)
Definition fail.hpp:59
void unreachable()
Definition fail.hpp:80
constexpr int ialpha4_id(const char *s)
Definition util.hpp:306
Structure make_structure_from_chemcomp_doc(const cif::Document &doc, cif::Document *save_doc=nullptr, int which=7)
Definition mmcif.hpp:92
Read the PDB file format and store it in Structure.
Utilities. Mostly for working with strings and vectors.