Gemmi C++ API
Loading...
Searching...
No Matches
pdb_id.hpp
Go to the documentation of this file.
1// Copyright 2018 Global Phasing Ltd.
2//
3// handling PDB ID and $PDB_DIR: is_pdb_code(), expand_pdb_code_to_path()
4
5#ifndef GEMMI_PDB_ID_HPP_
6#define GEMMI_PDB_ID_HPP_
7
8#include <cctype> // for isdigit, isalnum
9#include <cstdlib> // getenv
10#include <string>
11#include "fail.hpp" // for fail
12#include "util.hpp" // for to_lower
13
14namespace gemmi {
15
16inline bool is_pdb_code(const std::string& str) {
17 return (str.length() == 4 && std::isdigit(str[0]) && std::isalnum(str[1])
18 && std::isalnum(str[2]) && std::isalnum(str[3]))
19 || (str.length() == 12 && str.compare(0, 4, "pdb_") == 0
20 && std::isdigit(str[4]));
21}
22
26inline std::string expand_pdb_code_to_path(const std::string& code, char type,
27 bool throw_if_unset=false) {
28 std::string path;
29 if (const char* pdb_dir = std::getenv("PDB_DIR")) {
30 if (code.size() == 12)
31 fail("extended PDB codes are not supported yet: " + code);
32 int n = 0;
33 if (type == 'M')
34 n = 1;
35 else if (type == 'S')
36 n = 2;
37 std::string lc = to_lower(code);
38 path = pdb_dir;
39 path += "/structures/divided/";
40 const char* dir[] = {"pdb/", "mmCIF/", "structure_factors/"};
41 path += dir[n];
42 path += lc.substr(1, 2);
43 const char* prefix[] = {"/pdb", "/", "/r"};
44 path += prefix[n];
45 path += lc;
46 const char* suffix[] = {".ent.gz", ".cif.gz", "sf.ent.gz"};
47 path += suffix[n];
48 } else if (throw_if_unset) {
49 fail(code + " is a PDB code, but $PDB_DIR is not set.");
50 }
51 return path;
52}
53
55inline std::string expand_if_pdb_code(const std::string& input, char type='M') {
56 if (is_pdb_code(input))
57 return expand_pdb_code_to_path(input, type, true);
58 return input;
59}
60
61} // namespace gemmi
62#endif
bool is_pdb_code(const std::string &str)
Definition pdb_id.hpp:16
std::string expand_pdb_code_to_path(const std::string &code, char type, bool throw_if_unset=false)
Call it after checking the code with gemmi::is_pdb_code(code).
Definition pdb_id.hpp:26
std::string to_lower(std::string str)
Definition util.hpp:62
void fail(const std::string &msg)
Definition fail.hpp:59
std::string expand_if_pdb_code(const std::string &input, char type='M')
Definition pdb_id.hpp:55