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 all_alnums(const char* p) {
17 for (;;++p)
18 if (!std::isalnum(*p))
19 return *p == '\0';
21}
22
23inline bool is_pdb_code(const std::string& str) {
24 return (str.length() == 4 && std::isdigit(str[0]) && all_alnums(&str[1]))
25 || (str.length() == 12 && str.compare(0, 4, "pdb_") == 0
26 && std::isdigit(str[4]) && all_alnums(&str[5]));
27}
28inline std::string path_in_pdb_dir(const std::string& code, char type) {
29 if (code.size() == 12)
30 fail("extended PDB codes are not supported yet: " + code);
31 std::string lc = to_lower(code);
32 int n = 0;
33 if (type == 'M')
34 n = 1;
35 else if (type == 'S')
36 n = 2;
37 std::string path = "/structures/divided/";
38 const char* dir[] = {"pdb/", "mmCIF/", "structure_factors/"};
39 path += dir[n];
40 path += lc.substr(1, 2);
41 const char* prefix[] = {"/pdb", "/", "/r"};
42 path += prefix[n];
43 path += lc;
44 const char* suffix[] = {".ent.gz", ".cif.gz", "sf.ent.gz"};
45 path += suffix[n];
46 return path;
47}
48
52inline std::string expand_pdb_code_to_path(const std::string& code, char type,
53 bool throw_if_unset=false) {
54 std::string path;
55 if (const char* pdb_dir = std::getenv("PDB_DIR")) {
56 path = pdb_dir + path_in_pdb_dir(code, type);
57 } else if (throw_if_unset) {
58 fail(code + " is a PDB code, but $PDB_DIR is not set.");
59 }
60 return path;
61}
62
64inline std::string expand_if_pdb_code(const std::string& input, char type='M') {
65 if (is_pdb_code(input))
66 return expand_pdb_code_to_path(input, type, true);
67 return input;
68}
69
70} // namespace gemmi
71#endif
fail(), unreachable() and __declspec/__attribute__ macros
bool is_pdb_code(const std::string &str)
Definition pdb_id.hpp:23
bool all_alnums(const char *p)
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:52
std::string to_lower(std::string str)
Definition util.hpp:63
std::string path_in_pdb_dir(const std::string &code, char type)
Definition pdb_id.hpp:28
void fail(const std::string &msg)
Definition fail.hpp:59
void unreachable()
Definition fail.hpp:80
std::string expand_if_pdb_code(const std::string &input, char type='M')
Definition pdb_id.hpp:64
Utilities. Mostly for working with strings and vectors.