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.
4
5#ifndef GEMMI_MMREAD_HPP_
6#define GEMMI_MMREAD_HPP_
7
8#include "chemcomp_xyz.hpp" // for make_structure_from_chemcomp_block
9#include "cif.hpp" // for cif::read
10#include "fail.hpp" // for fail
11#include "input.hpp" // for BasicInput
12#include "json.hpp" // for read_mmjson
13#include "mmcif.hpp" // for make_structure_from_block
14#include "model.hpp" // for Structure
15#include "pdb.hpp" // for read_pdb
16#include "util.hpp" // for iends_with
17
18namespace gemmi {
19
20inline CoorFormat coor_format_from_ext(const std::string& path) {
21 if (iends_with(path, ".pdb") || iends_with(path, ".ent"))
22 return CoorFormat::Pdb;
23 if (iends_with(path, ".cif") || iends_with(path, ".mmcif"))
24 return CoorFormat::Mmcif;
25 if (iends_with(path, ".json"))
26 return CoorFormat::Mmjson;
28}
29
30// If it's neither CIF nor JSON nor almost empty - we assume PDB.
31inline CoorFormat coor_format_from_content(const char* buf, const char* end) {
32 while (buf < end - 8) {
33 if (std::isspace(*buf)) {
34 ++buf;
35 } else if (*buf == '#') {
36 while (buf < end - 8 && *buf != '\n')
37 ++buf;
38 } else if (*buf == '{') {
39 return CoorFormat::Mmjson;
40 } else if (ialpha4_id(buf) == ialpha4_id("data") && buf[4] == '_') {
41 return CoorFormat::Mmcif;
42 } else {
43 return CoorFormat::Pdb;
44 }
45 }
47}
48
50 cif::Document* save_doc=nullptr) {
52 // check for special case - refmac dictionary or CCD file
54 if (n != -1)
56 }
57 return make_structure(std::move(doc), save_doc);
58}
59
60inline Structure read_structure_from_char_array(char* data, size_t size,
61 const std::string& path,
62 cif::Document* save_doc=nullptr) {
63 if (save_doc)
64 save_doc->clear();
65 CoorFormat format = coor_format_from_content(data, data + size);
67 return read_pdb_from_memory(data, size, path);
69 return make_structure_from_doc(cif::read_memory(data, size, path.c_str()),
70 true, save_doc);
72 return make_structure(cif::read_mmjson_insitu(data, size, path), save_doc);
73 fail("wrong format of coordinate file " + path);
74}
75
76template<typename T>
78 cif::Document* save_doc=nullptr) {
81 return read_structure_from_char_array(mem.data(), mem.size(), input.path(), save_doc);
82 }
83 if (save_doc)
84 save_doc->clear();
86 format = coor_format_from_ext(input.basepath());
87 switch (format) {
88 case CoorFormat::Pdb:
89 return read_pdb(input);
98 fail("Unknown format of " +
99 (input.path().empty() ? "coordinate file" : input.path()) + ".");
100 }
101 unreachable();
102}
103
104inline Structure read_structure_file(const std::string& path,
106 return read_structure(BasicInput(path), format);
107}
108
109} // namespace gemmi
110#endif
Document read_mmjson_insitu(char *buffer, size_t size, const std::string &name="mmJSON")
Definition json.hpp:105
Document read_mmjson(T &&input)
Definition json.hpp:124
Document read(T &&input)
Definition cif.hpp:346
Document read_memory(const char *data, size_t size, const char *name)
Definition cif.hpp:308
CoorFormat
File format of a macromolecular model.
Definition model.hpp:80
CharArray read_into_buffer(T &&input)
Definition fileutil.hpp:122
Structure read_structure_from_char_array(char *data, size_t size, const std::string &path, cif::Document *save_doc=nullptr)
Definition mmread.hpp:60
Structure read_structure(T &&input, CoorFormat format=CoorFormat::Unknown, cif::Document *save_doc=nullptr)
Definition mmread.hpp:77
CoorFormat coor_format_from_content(const char *buf, const char *end)
Definition mmread.hpp:31
Structure make_structure_from_doc(cif::Document &&doc, bool possible_chemcomp, cif::Document *save_doc=nullptr)
Definition mmread.hpp:49
Structure make_structure(cif::Document &&doc, cif::Document *save_doc=nullptr)
Definition mmcif.hpp:17
Structure make_structure_from_chemcomp_block(const cif::Block &block)
int check_chemcomp_block_number(const cif::Document &doc)
CoorFormat coor_format_from_ext(const std::string &path)
Definition mmread.hpp:20
Structure make_structure_from_chemcomp_doc(const cif::Document &doc)
bool iends_with(const std::string &str, const std::string &suffix)
Definition util.hpp:98
Structure read_structure_file(const std::string &path, CoorFormat format=CoorFormat::Unknown)
Definition mmread.hpp:104
Structure read_pdb(T &&input, PdbReadOptions options=PdbReadOptions())
Definition pdb.hpp:694
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:305
Structure read_pdb_from_memory(const char *data, size_t size, const std::string &name, PdbReadOptions options=PdbReadOptions())
Definition pdb.hpp:680