mirror of
https://github.com/PDB-REDO/libcifpp.git
synced 2026-06-05 14:34:21 +08:00
Compare commits
59 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
288b2bb720 | ||
|
|
fb3b7bda68 | ||
|
|
6d5efe1cbd | ||
|
|
1ceec22184 | ||
|
|
951ff9b953 | ||
|
|
641f06a7e7 | ||
|
|
915ba4ac21 | ||
|
|
824637d83f | ||
|
|
0871406fe3 | ||
|
|
1ad7e47b2e | ||
|
|
f72a2c69d0 | ||
|
|
84d9275cb8 | ||
|
|
1b7c387c8b | ||
|
|
2f1adbd22c | ||
|
|
65031523a6 | ||
|
|
02cc0fa0f6 | ||
|
|
1e1afa023f | ||
|
|
41f343c2cd | ||
|
|
a73c4deaca | ||
|
|
01a21aebc4 | ||
|
|
49434043f2 | ||
|
|
2e23877912 | ||
|
|
b737dd7df4 | ||
|
|
137ffaf768 | ||
|
|
747c6d30d2 | ||
|
|
4585968b11 | ||
|
|
84af564aee | ||
|
|
86d957675e | ||
|
|
bc33e608db | ||
|
|
0b5d28338e | ||
|
|
bdbf22e705 | ||
|
|
160f6016ee | ||
|
|
0855965edf | ||
|
|
fe3cbdab78 | ||
|
|
a8c25f910d | ||
|
|
cb82ec9b01 | ||
|
|
e84282cb9a | ||
|
|
8b2e02e1b0 | ||
|
|
1addd2be89 | ||
|
|
2aebfc29ac | ||
|
|
26a5410b38 | ||
|
|
f44e6d0948 | ||
|
|
d496ebf6dd | ||
|
|
1719ed6979 | ||
|
|
821895bb1b | ||
|
|
3f437277d1 | ||
|
|
39fc56084a | ||
|
|
e2fca07fad | ||
|
|
ec0d75ce95 | ||
|
|
877a64adaa | ||
|
|
0fcf9ed5ad | ||
|
|
c99de817fa | ||
|
|
600c86a185 | ||
|
|
1ae755b0a5 | ||
|
|
7186057dd3 | ||
|
|
768fec9c58 | ||
|
|
7197dd877b | ||
|
|
b7aa7eac9f | ||
|
|
818dc2f952 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -8,3 +8,6 @@ msvc/
|
||||
src/revision.hpp
|
||||
test/test-create_sugar_?.cif
|
||||
Testing/
|
||||
include/cif++/exports.hpp
|
||||
docs/api
|
||||
docs/conf.py
|
||||
22
.readthedocs.yaml
Normal file
22
.readthedocs.yaml
Normal file
@@ -0,0 +1,22 @@
|
||||
version: 2
|
||||
|
||||
build:
|
||||
os: ubuntu-22.04
|
||||
tools:
|
||||
python: "3.11"
|
||||
apt_packages:
|
||||
- doxygen
|
||||
- cmake
|
||||
jobs:
|
||||
pre_build:
|
||||
- cmake -S . -B build -DBUILD_DOCUMENTATION=ON
|
||||
- cmake --build build --target Doxygen
|
||||
|
||||
# Build from the docs/ directory with Sphinx
|
||||
sphinx:
|
||||
configuration: docs/conf.py
|
||||
|
||||
# Explicitly set the version of Python and its requirements
|
||||
python:
|
||||
install:
|
||||
- requirements: docs/requirements.txt
|
||||
@@ -25,7 +25,7 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
# set the project name
|
||||
project(libcifpp VERSION 5.1.2 LANGUAGES CXX)
|
||||
project(libcifpp VERSION 5.2.2 LANGUAGES CXX)
|
||||
|
||||
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||
|
||||
@@ -35,8 +35,9 @@ include(CheckIncludeFiles)
|
||||
include(CheckLibraryExists)
|
||||
include(CMakePackageConfigHelpers)
|
||||
include(CheckCXXSourceCompiles)
|
||||
include(Dart)
|
||||
include(GenerateExportHeader)
|
||||
include(CTest)
|
||||
include(CMakeDependentOption)
|
||||
|
||||
set(CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
@@ -50,8 +51,8 @@ elseif(MSVC)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
||||
endif()
|
||||
|
||||
# Building shared libraries?
|
||||
option(BUILD_SHARED_LIBS "Build a shared library instead of a static one" OFF)
|
||||
# Build documentation?
|
||||
option(BUILD_DOCUMENTATION "Build the documentation" OFF)
|
||||
|
||||
# We do not want to write an export file for all our symbols...
|
||||
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
@@ -59,6 +60,10 @@ set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
# Optionally build a version to be installed inside CCP4
|
||||
option(BUILD_FOR_CCP4 "Build a version to be installed in CCP4" OFF)
|
||||
|
||||
# Building shared libraries?
|
||||
cmake_policy(SET CMP0127 NEW)
|
||||
cmake_dependent_option(BUILD_SHARED_LIBS "Build a shared library instead of a static one" OFF "NOT (BUILD_FOR_CCP4 AND WIN32)" ON)
|
||||
|
||||
# Lots of code depend on the availability of the components.cif file
|
||||
option(CIFPP_DOWNLOAD_CCD "Download the CCD file components.cif during installation" ON)
|
||||
|
||||
@@ -81,8 +86,6 @@ else()
|
||||
endif()
|
||||
|
||||
# Unit tests
|
||||
option(ENABLE_TESTING "Build test exectuables" OFF)
|
||||
|
||||
if(BUILD_FOR_CCP4)
|
||||
if("$ENV{CCP4}" STREQUAL "" OR NOT EXISTS $ENV{CCP4})
|
||||
message(FATAL_ERROR "A CCP4 built was requested but CCP4 was not sourced")
|
||||
@@ -238,8 +241,9 @@ set(project_sources
|
||||
|
||||
${PROJECT_SOURCE_DIR}/src/pdb/cif2pdb.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/pdb/pdb2cif.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/pdb/pdb_record.hpp
|
||||
${PROJECT_SOURCE_DIR}/src/pdb/pdb2cif_remark_3.hpp
|
||||
${PROJECT_SOURCE_DIR}/src/pdb/pdb2cif_remark_3.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/pdb/tls.cpp
|
||||
)
|
||||
|
||||
set(project_headers
|
||||
@@ -264,16 +268,17 @@ set(project_headers
|
||||
|
||||
${PROJECT_SOURCE_DIR}/include/cif++/model.hpp
|
||||
|
||||
${PROJECT_SOURCE_DIR}/include/cif++/pdb.hpp
|
||||
|
||||
${PROJECT_SOURCE_DIR}/include/cif++/pdb/cif2pdb.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/cif++/pdb/io.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/cif++/pdb/pdb2cif.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/cif++/pdb/pdb2cif_remark_3.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/cif++/pdb/tls.hpp
|
||||
)
|
||||
|
||||
add_library(cifpp ${project_sources} ${project_headers} ${PROJECT_SOURCE_DIR}/src/symop_table_data.hpp)
|
||||
add_library(cifpp::cifpp ALIAS cifpp)
|
||||
generate_export_header(cifpp EXPORT_FILE_NAME cif++/exports.hpp)
|
||||
generate_export_header(cifpp EXPORT_FILE_NAME ${PROJECT_SOURCE_DIR}/include/cif++/exports.hpp)
|
||||
|
||||
if(BOOST_REGEX)
|
||||
target_compile_definitions(cifpp PRIVATE USE_BOOST_REGEX=1 BOOST_REGEX_STANDALONE=1)
|
||||
@@ -292,7 +297,7 @@ target_include_directories(cifpp
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
|
||||
target_link_libraries(cifpp PUBLIC Threads::Threads ZLIB::ZLIB Eigen3::Eigen ${CIFPP_REQUIRED_LIBRARIES})
|
||||
target_link_libraries(cifpp PUBLIC Threads::Threads ZLIB::ZLIB ${CIFPP_REQUIRED_LIBRARIES})
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
|
||||
target_link_options(cifpp PRIVATE -undefined dynamic_lookup)
|
||||
@@ -314,11 +319,12 @@ if(CIFPP_DOWNLOAD_CCD)
|
||||
endif()
|
||||
|
||||
# Installation directories
|
||||
set(CIFPP_DATA_DIR "${CMAKE_INSTALL_FULL_DATADIR}/libcifpp")
|
||||
set(CIFPP_DATA_DIR "${CMAKE_INSTALL_FULL_DATADIR}/libcifpp" CACHE PATH "The directory where dictionary files are stored")
|
||||
target_compile_definitions(cifpp PUBLIC DATA_DIR="${CIFPP_DATA_DIR}")
|
||||
|
||||
if(UNIX)
|
||||
set(CIFPP_CACHE_DIR "${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/cache/libcifpp")
|
||||
set(CIFPP_CACHE_DIR "${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/cache/libcifpp"
|
||||
CACHE PATH "The directory where the update script stores new dictionary files")
|
||||
target_compile_definitions(cifpp PUBLIC CACHE_DIR="${CIFPP_CACHE_DIR}")
|
||||
|
||||
set(CIFPP_ETC_DIR "${CMAKE_INSTALL_FULL_SYSCONFDIR}")
|
||||
@@ -357,12 +363,6 @@ install(
|
||||
COMPONENT Devel
|
||||
)
|
||||
|
||||
install(
|
||||
FILES ${PROJECT_BINARY_DIR}/cif++/exports.hpp
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cif++
|
||||
COMPONENT Devel
|
||||
)
|
||||
|
||||
install(FILES
|
||||
${PROJECT_SOURCE_DIR}/rsrc/mmcif_ddl.dic
|
||||
${PROJECT_SOURCE_DIR}/rsrc/mmcif_pdbx.dic
|
||||
@@ -397,11 +397,10 @@ install(FILES
|
||||
COMPONENT Devel
|
||||
)
|
||||
|
||||
set(cifpp_MAJOR_VERSION ${CMAKE_PROJECT_VERSION_MAJOR})
|
||||
set_target_properties(cifpp PROPERTIES
|
||||
VERSION ${PROJECT_VERSION}
|
||||
SOVERSION ${cifpp_MAJOR_VERSION}
|
||||
INTERFACE_cifpp_MAJOR_VERSION ${cifpp_MAJOR_VERSION})
|
||||
SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
|
||||
INTERFACE_cifpp_MAJOR_VERSION ${PROJECT_VERSION_MAJOR})
|
||||
|
||||
set_property(TARGET cifpp APPEND PROPERTY
|
||||
COMPATIBLE_INTERFACE_STRING cifpp_MAJOR_VERSION
|
||||
@@ -413,21 +412,7 @@ write_basic_package_version_file(
|
||||
COMPATIBILITY AnyNewerVersion
|
||||
)
|
||||
|
||||
# pkgconfig support
|
||||
set(prefix ${CMAKE_INSTALL_PREFIX})
|
||||
set(exec_prefix ${CMAKE_INSTALL_PREFIX})
|
||||
set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
|
||||
set(includedir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
|
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libcifpp.pc.in
|
||||
${CMAKE_CURRENT_BINARY_DIR}/libcifpp.pc.in @ONLY)
|
||||
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/libcifpp.pc
|
||||
INPUT ${CMAKE_CURRENT_BINARY_DIR}/libcifpp.pc.in)
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libcifpp.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
|
||||
|
||||
if(ENABLE_TESTING)
|
||||
enable_testing()
|
||||
|
||||
if(BUILD_TESTING)
|
||||
find_package(Boost REQUIRED)
|
||||
|
||||
list(APPEND CIFPP_tests unit-v2 unit-3d format model rename-compound sugar spinner)
|
||||
@@ -488,6 +473,10 @@ if(CIFPP_INSTALL_UPDATE_SCRIPT)
|
||||
target_compile_definitions(cifpp PUBLIC CACHE_DIR="${CIFPP_CACHE_DIR}")
|
||||
endif()
|
||||
|
||||
if(BUILD_DOCUMENTATION)
|
||||
add_subdirectory(docs)
|
||||
endif()
|
||||
|
||||
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
|
||||
set(CPACK_SOURCE_TGZ ON)
|
||||
set(CPACK_SOURCE_TBZ2 OFF)
|
||||
|
||||
50
README.md
50
README.md
@@ -1,10 +1,12 @@
|
||||
libcifpp
|
||||
========
|
||||
# libcifpp
|
||||
|
||||
This library contains code to work with mmCIF and PDB files.
|
||||
This library contains code to work with mmCIF and legacy PDB files.
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
## Documentation
|
||||
|
||||
The documentation can be found at https://www.hekkelman.com/libcifpp-doc/
|
||||
|
||||
## Synopsis
|
||||
|
||||
```c++
|
||||
// A simple program counting residues with an OXT atom
|
||||
@@ -55,11 +57,16 @@ int main(int argc, char *argv[])
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Requirements
|
||||
------------
|
||||
## Installation
|
||||
|
||||
You might be able to use libcifpp from a package manager used by your
|
||||
OS distribution. But most likely this package will be out-of-date.
|
||||
Therefore it is recommended to build *libcifpp* from code. It is not
|
||||
hard to do.
|
||||
|
||||
### Requirements
|
||||
|
||||
The code for this library was written in C++17. You therefore need a
|
||||
recent compiler to build it. For the development gcc 9.4 and clang 9.0
|
||||
@@ -67,6 +74,7 @@ have been used as well as MSVC version 2019.
|
||||
|
||||
Other requirements are:
|
||||
|
||||
- [cmake](https://cmake.org) A build tool.
|
||||
- [mrc](https://github.com/mhekkel/mrc), a resource compiler that
|
||||
allows including data files into the executable making them easier to
|
||||
install. Strictly speaking this is optional, but at the expense of
|
||||
@@ -77,22 +85,22 @@ Other requirements are:
|
||||
`libeigen3-dev`
|
||||
- zlib, the development version of this library. On Debian/Ubuntu this
|
||||
is the package `zlib1g-dev`.
|
||||
- [boost](https://www.boost.org). The boost libraries are only needed if
|
||||
you want to build the testing code.
|
||||
- [boost](https://www.boost.org).
|
||||
|
||||
When building using MS Visual Studio, you will also need [libzeep](https://github.com/mhekkel/libzeep)
|
||||
since MSVC does not yet provide a C++ template required by libcifpp.
|
||||
|
||||
Building
|
||||
--------
|
||||
The Boost libraries are only needed in case you want to build the test
|
||||
code or if you are using GCC. That last condition is due to a long
|
||||
standing bug in the implementation of std::regex. It simply crashes
|
||||
on the regular expressions used in the mmcif_pdbx dictionary and so
|
||||
we use the boost regex implementation instead.
|
||||
|
||||
This library uses [cmake](https://cmake.org). The usual way of building
|
||||
and installing is to create a `build` directory and run cmake there.
|
||||
### Building
|
||||
|
||||
On linux e.g. you would issue the following commands to build and install
|
||||
libcifpp in your `$HOME/.local` folder:
|
||||
Building the code is as simple as typing:
|
||||
|
||||
```bash
|
||||
```console
|
||||
git clone https://github.com/PDB-REDO/libcifpp.git --recurse-submodules
|
||||
cd libcifpp
|
||||
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=$HOME/.local -DCMAKE_BUILD_TYPE=Release
|
||||
@@ -104,5 +112,9 @@ This checks out the source code from github, creates a new directory
|
||||
where cmake stores its files. Run a configure, build the code and then
|
||||
it installs the library and auxiliary files.
|
||||
|
||||
If you want to run the tests before installing, you should add `-DENABLE_TESTING=ON`
|
||||
to the first cmake command.
|
||||
Tests are created by default, and to test the code you can run:
|
||||
|
||||
```console
|
||||
cmake --build build
|
||||
ctest --test-dir build
|
||||
```
|
||||
|
||||
25
changelog
25
changelog
@@ -1,3 +1,24 @@
|
||||
Version 5.2.2
|
||||
- Remove dependency on Eigen3 for users of libcifpp
|
||||
- Fix typos in documentation
|
||||
- Do not build latex files in documentation
|
||||
- Fixed conversion from string to integer, would fail on +2 e.g.
|
||||
- sqrt is not constexpr, thus kGoldenRatio should be const, not constexpr
|
||||
|
||||
Version 5.2.1
|
||||
- New versionstring module
|
||||
- small fixes for generating documentation
|
||||
- correctly setting SONAME
|
||||
|
||||
Version 5.2.0
|
||||
- With lots of documentation
|
||||
- Refactored coloured text output
|
||||
- Removed the subdirectory cif++/pdb, there now is a single
|
||||
header file pdb.hpp for I/O of legacy PDB files.
|
||||
|
||||
Version 5.1.3
|
||||
- Dropped pkgconfig support
|
||||
|
||||
Version 5.1.2
|
||||
- New version string code
|
||||
- Added check for Eigen3 in cifppConfig.cmake
|
||||
@@ -5,8 +26,8 @@ Version 5.1.2
|
||||
Version 5.1.1
|
||||
- Added missing include <compare> in symmetry.hpp
|
||||
- Added empty() to matrix
|
||||
- Fix for parsing PDB files with a last line that does not end with
|
||||
a new line character.
|
||||
- Fix for parsing legacy PDB files with a last line that does
|
||||
not end with a new line character.
|
||||
|
||||
Version 5.1
|
||||
- New parser, optimised for speed
|
||||
|
||||
11
cmake/FindSphinx.cmake
Normal file
11
cmake/FindSphinx.cmake
Normal file
@@ -0,0 +1,11 @@
|
||||
#Look for an executable called sphinx-build
|
||||
find_program(SPHINX_EXECUTABLE
|
||||
NAMES sphinx-build
|
||||
DOC "Path to sphinx-build executable")
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
|
||||
#Handle standard arguments to find_package like REQUIRED and QUIET
|
||||
find_package_handle_standard_args(Sphinx
|
||||
"Failed to find sphinx-build executable"
|
||||
SPHINX_EXECUTABLE)
|
||||
@@ -54,6 +54,10 @@ cmake_minimum_required(VERSION 3.15)
|
||||
variables contained in the revision file.
|
||||
#]=======================================================================]
|
||||
|
||||
# Record the location of this module now, not at the time the CMakeLists.txt
|
||||
# is being processed
|
||||
get_filename_component(_current_cmake_module_dir ${CMAKE_CURRENT_LIST_FILE} PATH)
|
||||
|
||||
# First locate a .git file or directory.
|
||||
function(_get_git_dir _start_dir _variable)
|
||||
|
||||
@@ -255,141 +259,13 @@ function(write_version_header dir)
|
||||
if(VERSION_STRING_OPTION_LIB_NAME)
|
||||
set(VAR_PREFIX "${VERSION_STRING_OPTION_LIB_NAME}")
|
||||
set(IDENT_PREFIX "${VERSION_STRING_OPTION_LIB_NAME}_")
|
||||
set(BOOL_IS_MAIN "false")
|
||||
else()
|
||||
set(VAR_PREFIX "")
|
||||
set(IDENT_PREFIX "")
|
||||
set(BOOL_IS_MAIN "true")
|
||||
endif()
|
||||
|
||||
# And finally, write out the header file
|
||||
file(WRITE "${VERSION_STRING_DATA}/${file_name}.in" [[// This file was generated by VersionString.cmake
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
|
||||
constexpr const char k@VAR_PREFIX@ProjectName[] = "@PROJECT_NAME@";
|
||||
constexpr const char k@VAR_PREFIX@VersionNumber[] = "@PROJECT_VERSION@";
|
||||
constexpr int k@VAR_PREFIX@BuildNumber = @BUILD_NUMBER@;
|
||||
constexpr const char k@VAR_PREFIX@RevisionGitTag[] = "@REVISION_GIT_TAGREF@";
|
||||
constexpr const char k@VAR_PREFIX@RevisionDate[] = "@REVISION_DATE_TIME@";
|
||||
|
||||
#ifndef VERSION_INFO_DEFINED
|
||||
#define VERSION_INFO_DEFINED 1
|
||||
|
||||
namespace version_info_v1
|
||||
{
|
||||
|
||||
class version_info_base
|
||||
{
|
||||
public:
|
||||
|
||||
static void write(std::ostream &os, bool verbose)
|
||||
{
|
||||
auto &s_head = head();
|
||||
if (s_head != nullptr)
|
||||
write(s_head, os, verbose);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
version_info_base(const char *name, const char *version, int build_number, const char *git_tag, const char *revision_date)
|
||||
: m_name(name)
|
||||
, m_version(version)
|
||||
, m_build_number(build_number)
|
||||
, m_git_tag(git_tag)
|
||||
, m_revision_date(revision_date)
|
||||
{
|
||||
auto &s_head = head();
|
||||
m_next = s_head;
|
||||
s_head = this;
|
||||
}
|
||||
|
||||
static void write(const version_info_base *inst, std::ostream &os, bool verbose)
|
||||
{
|
||||
if (inst->m_next)
|
||||
{
|
||||
write(inst->m_next, os, verbose);
|
||||
|
||||
if (not verbose)
|
||||
return;
|
||||
|
||||
os << '-' << std::endl;
|
||||
}
|
||||
|
||||
os << inst->m_name << " version " << inst->m_version << std::endl;
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
if (inst->m_build_number != 0)
|
||||
{
|
||||
os << "build: " << inst->m_build_number << ' ' << inst->m_revision_date << std::endl;
|
||||
if (inst->m_git_tag[0] != 0)
|
||||
os << "git tag: " << inst->m_git_tag << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using version_info_ptr = version_info_base *;
|
||||
|
||||
static version_info_ptr &head()
|
||||
{
|
||||
static version_info_ptr s_head = nullptr;
|
||||
return s_head;
|
||||
}
|
||||
|
||||
const char *m_name;
|
||||
const char *m_version;
|
||||
int m_build_number;
|
||||
const char *m_git_tag;
|
||||
const char *m_revision_date;
|
||||
version_info_base *m_next = nullptr;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class version_info : public version_info_base
|
||||
{
|
||||
public:
|
||||
using implementation_type = T;
|
||||
|
||||
version_info(const char *name, const char *version, int build_number, const char *git_tag, const char *revision_date)
|
||||
: version_info_base(name, version, build_number, git_tag, revision_date)
|
||||
{
|
||||
}
|
||||
|
||||
struct register_object
|
||||
{
|
||||
register_object()
|
||||
{
|
||||
static implementation_type s_instance;
|
||||
}
|
||||
};
|
||||
|
||||
template<register_object&> struct reference_object;
|
||||
|
||||
static register_object s_registered_object;
|
||||
static reference_object<s_registered_object> s_referenced_object;
|
||||
};
|
||||
|
||||
template<typename T> typename version_info<T>::register_object version_info<T>::s_registered_object;
|
||||
|
||||
}
|
||||
|
||||
inline void write_version_string(std::ostream &os, bool verbose)
|
||||
{
|
||||
version_info_v1::version_info_base::write(os, verbose);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
class version_info_@IDENT_PREFIX@impl : public version_info_v1::version_info<version_info_@IDENT_PREFIX@impl>
|
||||
{
|
||||
public:
|
||||
version_info_@IDENT_PREFIX@impl()
|
||||
: version_info(k@VAR_PREFIX@ProjectName, k@VAR_PREFIX@VersionNumber, k@VAR_PREFIX@BuildNumber, k@VAR_PREFIX@RevisionGitTag, k@VAR_PREFIX@RevisionDate)
|
||||
{
|
||||
}
|
||||
};
|
||||
]])
|
||||
configure_file("${VERSION_STRING_DATA}/${file_name}.in" "${dir}/${file_name}" @ONLY)
|
||||
configure_file("${_current_cmake_module_dir}/revision.hpp.in" "${dir}/${file_name}" @ONLY)
|
||||
endfunction()
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ include(CMakeFindDependencyMacro)
|
||||
find_dependency(Threads)
|
||||
|
||||
find_dependency(ZLIB REQUIRED)
|
||||
find_dependency(Eigen3 REQUIRED)
|
||||
|
||||
if(MSVC)
|
||||
find_dependency(zeep REQUIRED)
|
||||
|
||||
138
cmake/revision.hpp.in
Normal file
138
cmake/revision.hpp.in
Normal file
@@ -0,0 +1,138 @@
|
||||
// This file was generated by VersionString.cmake
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
|
||||
constexpr const char k@VAR_PREFIX@ProjectName[] = "@PROJECT_NAME@";
|
||||
constexpr const char k@VAR_PREFIX@VersionNumber[] = "@PROJECT_VERSION@";
|
||||
constexpr int k@VAR_PREFIX@BuildNumber = @BUILD_NUMBER@;
|
||||
constexpr const char k@VAR_PREFIX@RevisionGitTag[] = "@REVISION_GIT_TAGREF@";
|
||||
constexpr const char k@VAR_PREFIX@RevisionDate[] = "@REVISION_DATE_TIME@";
|
||||
|
||||
#ifndef VERSION_INFO_DEFINED
|
||||
#define VERSION_INFO_DEFINED 1
|
||||
|
||||
namespace version_info_v1_1
|
||||
{
|
||||
|
||||
class version_info_base
|
||||
{
|
||||
public:
|
||||
static void write_version_string(std::ostream &os, bool verbose)
|
||||
{
|
||||
auto s_main = registered_main();
|
||||
if (s_main != nullptr)
|
||||
s_main->write(os, verbose);
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
for (auto lib = registered_libraries(); lib != nullptr; lib = lib->m_next)
|
||||
{
|
||||
os << '-' << std::endl;
|
||||
lib->write(os, verbose);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
version_info_base(const char *name, const char *version, int build_number, const char *git_tag, const char *revision_date, bool is_main)
|
||||
: m_name(name)
|
||||
, m_version(version)
|
||||
, m_build_number(build_number)
|
||||
, m_git_tag(git_tag)
|
||||
, m_revision_date(revision_date)
|
||||
{
|
||||
if (is_main)
|
||||
registered_main() = this;
|
||||
else
|
||||
{
|
||||
auto &s_head = registered_libraries();
|
||||
m_next = s_head;
|
||||
s_head = this;
|
||||
}
|
||||
}
|
||||
|
||||
void write(std::ostream &os, bool verbose)
|
||||
{
|
||||
os << m_name << " version " << m_version << std::endl;
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
if (m_build_number != 0)
|
||||
{
|
||||
os << "build: " << m_build_number << ' ' << m_revision_date << std::endl;
|
||||
if (m_git_tag[0] != 0)
|
||||
os << "git tag: " << m_git_tag << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using version_info_ptr = version_info_base *;
|
||||
|
||||
static version_info_ptr ®istered_main()
|
||||
{
|
||||
static version_info_ptr s_main = nullptr;
|
||||
return s_main;
|
||||
}
|
||||
|
||||
static version_info_ptr ®istered_libraries()
|
||||
{
|
||||
static version_info_ptr s_head = nullptr;
|
||||
return s_head;
|
||||
}
|
||||
|
||||
const char *m_name;
|
||||
const char *m_version;
|
||||
int m_build_number;
|
||||
const char *m_git_tag;
|
||||
const char *m_revision_date;
|
||||
version_info_base *m_next = nullptr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class version_info : public version_info_base
|
||||
{
|
||||
public:
|
||||
using implementation_type = T;
|
||||
|
||||
version_info(const char *name, const char *version, int build_number, const char *git_tag, const char *revision_date, bool is_main)
|
||||
: version_info_base(name, version, build_number, git_tag, revision_date, is_main)
|
||||
{
|
||||
}
|
||||
|
||||
struct register_object
|
||||
{
|
||||
register_object()
|
||||
{
|
||||
static implementation_type s_instance;
|
||||
}
|
||||
};
|
||||
|
||||
template <register_object &>
|
||||
struct reference_object;
|
||||
|
||||
static register_object s_registered_object;
|
||||
static reference_object<s_registered_object> s_referenced_object;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
typename version_info<T>::register_object version_info<T>::s_registered_object;
|
||||
|
||||
} // namespace version_info_v1_1
|
||||
|
||||
inline void write_version_string(std::ostream &os, bool verbose)
|
||||
{
|
||||
version_info_v1_1::version_info_base::write_version_string(os, verbose);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
class version_info_@IDENT_PREFIX@impl : public version_info_v1_1::version_info<version_info_@IDENT_PREFIX@impl>
|
||||
{
|
||||
public:
|
||||
version_info_@IDENT_PREFIX@impl()
|
||||
: version_info(k@VAR_PREFIX@ProjectName, k@VAR_PREFIX@VersionNumber, k@VAR_PREFIX@BuildNumber, k@VAR_PREFIX@RevisionGitTag, k@VAR_PREFIX@RevisionDate, @BOOL_IS_MAIN@)
|
||||
{
|
||||
}
|
||||
};
|
||||
@@ -11,8 +11,8 @@ int main()
|
||||
|
||||
std::regex_search(s, m, r);
|
||||
|
||||
std::cout << s.substr(0, 10) << std::endl;
|
||||
std::cout << m.str(1).substr(0, 10) << std::endl;
|
||||
std::cout << s.substr(0, 10) << '\n';
|
||||
std::cout << m.str(1).substr(0, 10) << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
48
docs/CMakeLists.txt
Normal file
48
docs/CMakeLists.txt
Normal file
@@ -0,0 +1,48 @@
|
||||
find_package(Doxygen REQUIRED)
|
||||
find_package(Sphinx REQUIRED)
|
||||
|
||||
# Find all the public headers
|
||||
# get_target_property(CIFPP_PUBLIC_HEADER_DIR libCIFPP INTERFACE_INCLUDE_DIRECTORIES)
|
||||
set(CIFPP_PUBLIC_HEADER_DIR ${PROJECT_SOURCE_DIR}/include)
|
||||
file(GLOB_RECURSE CIFPP_PUBLIC_HEADERS ${CIFPP_PUBLIC_HEADER_DIR}/*.hpp)
|
||||
|
||||
set(DOXYGEN_INPUT_DIR ${CIFPP_PUBLIC_HEADER_DIR})
|
||||
set(DOXYGEN_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/xml)
|
||||
set(DOXYGEN_INDEX_FILE ${DOXYGEN_OUTPUT_DIR}/index.xml)
|
||||
set(DOXYFILE_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
|
||||
set(DOXYFILE_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
|
||||
|
||||
# Replace variables inside @@ with the current values
|
||||
configure_file(${DOXYFILE_IN} ${DOXYFILE_OUT} @ONLY)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${DOXYGEN_OUTPUT_DIR}
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory ${DOXYGEN_OUTPUT_DIR})
|
||||
|
||||
add_custom_command(OUTPUT ${DOXYGEN_INDEX_FILE}
|
||||
BYPRODUCTS ${DOXYGEN_OUTPUT_DIR}
|
||||
DEPENDS ${DOXYGEN_OUTPUT_DIR} ${CIFPP_PUBLIC_HEADERS} ${DOXYFILE_OUT}
|
||||
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYFILE_OUT}
|
||||
MAIN_DEPENDENCY ${DOXYFILE_OUT} ${DOXYFILE_IN}
|
||||
COMMENT "Generating docs")
|
||||
|
||||
add_custom_target("Doxygen-${PROJECT_NAME}" ALL DEPENDS ${DOXYGEN_INDEX_FILE})
|
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/conf.py.in ${CMAKE_CURRENT_SOURCE_DIR}/conf.py @ONLY)
|
||||
|
||||
set(SPHINX_SOURCE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
set(SPHINX_BUILD ${CMAKE_CURRENT_BINARY_DIR}/sphinx)
|
||||
|
||||
add_custom_target("Sphinx-${PROJECT_NAME}" ALL
|
||||
COMMAND ${SPHINX_EXECUTABLE} -b html
|
||||
-Dbreathe_projects.${PROJECT_NAME}=${DOXYGEN_OUTPUT_DIR}
|
||||
${SPHINX_SOURCE} ${SPHINX_BUILD}
|
||||
DEPENDS ${DOXYGEN_INDEX_FILE}
|
||||
BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/api
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
COMMENT "Generating documentation with Sphinx")
|
||||
|
||||
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/sphinx/
|
||||
DESTINATION ${CMAKE_INSTALL_DOCDIR}
|
||||
PATTERN .doctrees EXCLUDE
|
||||
PATTERN .buildinfo EXCLUDE)
|
||||
10
docs/Doxyfile.in
Normal file
10
docs/Doxyfile.in
Normal file
@@ -0,0 +1,10 @@
|
||||
EXCLUDE_SYMBOLS = cif::detail::*, std*
|
||||
FILE_PATTERNS = *.hpp
|
||||
STRIP_FROM_PATH = @DOXYGEN_INPUT_DIR@
|
||||
RECURSIVE = YES
|
||||
GENERATE_XML = YES
|
||||
GENERATE_LATEX = NO
|
||||
PREDEFINED += and=&& or=|| not=! CIFPP_EXPORT= HAVE_LIBCLIPPER=1
|
||||
GENERATE_HTML = NO
|
||||
GENERATE_TODOLIST = NO
|
||||
INPUT = @DOXYGEN_INPUT_DIR@
|
||||
4
docs/_static/.gitignore
vendored
Normal file
4
docs/_static/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# Ignore everything in this directory
|
||||
*
|
||||
# Except this file
|
||||
!.gitignore
|
||||
400
docs/basics.rst
Normal file
400
docs/basics.rst
Normal file
@@ -0,0 +1,400 @@
|
||||
Basic usage
|
||||
===========
|
||||
|
||||
This library, *libcifpp*, is a generic *CIF* library with some specific additions to work with *mmCIF* files. The main focus of this library is to make sure that files read or written are valid. That is, they are syntactically valid *and* their content is valid with respect to a CIF dictionary, if such a dictionary is available and specified.
|
||||
|
||||
Reading a file is as simple as:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cif++.hpp>
|
||||
|
||||
cif::file f("/path/to/file.cif");
|
||||
|
||||
The file may also be compressed using *gzip* which is detected automatically.
|
||||
|
||||
Writing out the file again is also simple, to write out the terminal you can do:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
std::cout << f;
|
||||
|
||||
// or
|
||||
f.save(std::cout);
|
||||
|
||||
// or write a compressed file using gzip compression:
|
||||
f.save("/tmp/f.cif.gz");
|
||||
|
||||
CIF files contain one or more datablocks. To print out the names of all datablocks in our file:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
for (auto &db : f)
|
||||
std::cout << db.name() << '\n';
|
||||
|
||||
Most often *libcifpp* is used to read in structure files in mmCIF format. These files only contain one datablock and so you can safely use code like this:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// get a reference to the first datablock in f
|
||||
auto &db = f.front();
|
||||
|
||||
But if you know the name of the datablock, this also works:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// get a reference to the datablock name '1CBS'
|
||||
auto &db = f["1CBS"];
|
||||
|
||||
Now, each datablock contains categories. To print out all their names:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
for (auto &cat : db)
|
||||
std::cout << cat.name() << '\n';
|
||||
|
||||
But you probably know what category you need to use, so lets fetch it by name:
|
||||
|
||||
.. _atom_site-label:
|
||||
.. code-block:: cpp
|
||||
|
||||
// get a reference to the atom_site category in db
|
||||
auto &atom_site = db["atom_site"];
|
||||
|
||||
// and make sure there's some data in it:
|
||||
assert(not atom_site.empty());
|
||||
|
||||
.. note::
|
||||
|
||||
Note that we omit the leading underscore in the name of the category here.
|
||||
|
||||
Categories contain rows of data and each row has fields or items. Referencing a row in a category results in a :cpp:class:`cif::row_handle` object which you can use to request or manipulate item data.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// Get the first row in atom_site
|
||||
auto rh = atom_site.front();
|
||||
|
||||
// Get the label_atom_id value from this row handle as a std::string
|
||||
std::string atom_id = rh["label_atom_id"].as<std::string>();
|
||||
|
||||
// Get the x, y and z coordinates using structered binding
|
||||
const auto &[x, y, z] = rh.get<float,float,float>("Cartn_x", "Cartn_y", "Cartn_z");
|
||||
|
||||
// Assign a new value to the x coordinate or our atom
|
||||
rh["Cartn_x"] = x + 1;
|
||||
|
||||
Querying
|
||||
--------
|
||||
|
||||
Walking over the rows in a category is often not very useful. More often you are interested in specific rows in a category. The function :cpp:func:`cif::category::find` and friends are here to help.
|
||||
|
||||
What these functions have in common is that they return data based on a query implemented by :cpp:class:`cif::condition`. These condition objects are built in code using regular C++ syntax. The most basic example of a query is:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = cif::key("id") == 1;
|
||||
|
||||
Here the condition is that all rows returned should have a value of 1 in there item named *id*. Likewise you can use other data types and even combine those. Oh, and I said we use regular C++ syntax for conditions, so you may as well use other operators to compare values:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// condition for C-alpha atoms having an occupancy less than 1.0
|
||||
cif::condition c = cif::key("occupancy") < 1.0f and cif::key("label_atom_id") == "CA";
|
||||
|
||||
Using the namespace *cif::literals* that code becomes a little less verbose:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
using namespace cif::literals;
|
||||
cif::condition c = "occupancy"_key < 1.0f and "label_atom_id"_key == "CA";
|
||||
|
||||
Conditions can also be combined:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = "occupancy"_key < 1.0f and "label_atom_id"_key == "CA";
|
||||
|
||||
// extend the condition by requiring the compound ID to be unequal to PRO
|
||||
c = std::move(c) and "label_comp_id"_key != "PRO";
|
||||
|
||||
.. note::
|
||||
|
||||
Note the use of std::move here.
|
||||
|
||||
Using queries constructed in this way is simple:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = ...
|
||||
auto result = atom_site.find(std::move(c));
|
||||
|
||||
// or construct a condition inline:
|
||||
auto result = atom_site.find("label_atom_id"_key == "CA");
|
||||
|
||||
In the example above the result is a range of :cpp:class:`cif::row_handle` objects. Often, using individual field values is more useful:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// Requesting a single item:
|
||||
for (auto id : atom_site.find<std::string>("label_atom_id"_key == "CA", "id"))
|
||||
std::cout << "ID for CA: " << id << '\n';
|
||||
|
||||
// Requesting multiple items:
|
||||
for (const auto &[id, x, y, z] : atom_site.find<std::string,float,float,float>("label_atom_id"_key == "CA",
|
||||
"id", "Cartn_x", "Cartn_y", "Cartn_z"))
|
||||
{
|
||||
std::cout << "Atom " << id << " is at [" << x << ", " << y << ", " z << "]\n";
|
||||
}
|
||||
|
||||
Returning a complete set if often not required, if you only want to have the first you can use :cpp:func:`cif::category::find_first` as shown here:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// return the ID item for the first C-alpha atom
|
||||
std::string v1 = atom_site.find_first<std::string>("label_atom_id"_key == "CA", "id");
|
||||
|
||||
// If you're not sure the row exists, use std::optional
|
||||
auto v2 = atom_site.find_first<std::optional<std::string>>("label_atom_id"_key == "CA", "id");
|
||||
if (v2.has_value())
|
||||
...
|
||||
|
||||
There are cases when you really need exactly one result. The :cpp:func:`cif::category::find1` can be used in that case, it will throw an exception if the query does not result in exactly one row.
|
||||
|
||||
NULL and ANY
|
||||
------------
|
||||
|
||||
Sometimes items may be empty. The trouble is a bit that empty comes in two flavors: unknown and null. Null in *CIF* parlance means the item should not contain a value since it makes no sense in this case, the value stored in the file is a single dot character: ``'.'``. E.g. *atom_site* records may have a NULL value for label_seq_id for atoms that are part of a *non-polymer*.
|
||||
|
||||
The other empty value is indicated by a question mark character: ``'?'``. This means the value is simply unknown.
|
||||
|
||||
Both these are NULL in *libcifpp* conditions and can be searched for using :cpp:var:`cif::null`.
|
||||
|
||||
So you can search for:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = "label_seq_id"_key == cif::null;
|
||||
|
||||
You might also want to look for a certain value and don't care in which item it is stored, in that case you can use :cpp:var:`cif::any`.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::condition c = cif::any == "foo";
|
||||
|
||||
And in linked record you might have the items that have a value in both parent and child or both should be NULL. For that, you can request the value to return by find to be of type std::optional and then use that value to build the query. An example to explain this, let's find the location of the atom that is referenced as the first atom in a struct_conn record:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// Take references to the two categories we need
|
||||
auto struct_conn = db["struct_conn"];
|
||||
auto atom_site = db["atom_site"];
|
||||
|
||||
// Loop over all rows in struct_conn taking only the values we need
|
||||
// Note that the label_seq_id is returned as a std::optional<int>
|
||||
// That means it may contain an integer or may be empty
|
||||
for (const auto &[asym1, seqid1, authseqid1, atomid1] :
|
||||
struct_conn.rows<std::string,std::optional<int>,std::string,std::string,std::string>(
|
||||
"ptnr1_label_asym_id", "ptnr1_label_seq_id", "ptnr1_auth_seq_id", "ptnr1_label_atom_id"
|
||||
))
|
||||
{
|
||||
// Find the location of the first atom
|
||||
cif::point p1 = atom_site.find1<float,float,float>(
|
||||
"label_asym_id"_key == asym1 and "label_seq_id"_key == seqid1 and "auth_seq_id"_key == authseqid1 and "label_atom_id"_key == atomid1,
|
||||
"cartn_x", "cartn_y", "cartn_z");
|
||||
}
|
||||
|
||||
|
||||
Validation
|
||||
----------
|
||||
|
||||
CIF files can have a dictionary attached. And based on such a dictionary a :cpp:class:`cif::validator` object can be constructed which in turn can be used to validate the content of the file.
|
||||
|
||||
A simple case:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cif++.hpp>
|
||||
|
||||
cif::file f("1cbs.cif.gz");
|
||||
f.load_dictionary("mmcif_pdbx");
|
||||
|
||||
if (not f.is_valid())
|
||||
std::cout << "This file is not valid\n";
|
||||
|
||||
If you want to know why it is not valid, you should set the global variable :cpp:var:`cif::VERBOSE` to something higer than zero. Depending on the value more or less diagnostic output is sent to std::cerr.
|
||||
|
||||
In the case above we load a dictionary based on its name. You can of course also load dictionaries based on a specific file, that's a bit more work:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
std::filesystem::ifstream dictFile("/tmp/my-dictionary.dic");
|
||||
auto &validator = cif::parse_dictionary("my-dictionary", dictFile);
|
||||
|
||||
cif::file f("1cbs.cif.gz");
|
||||
|
||||
// assign the validator
|
||||
f.set_validator(&validator);
|
||||
|
||||
// alternatively, load it by name
|
||||
f.load_dictionary("my-dictionary");
|
||||
|
||||
if (not f.is_valid())
|
||||
std::cout << "This file is not valid\n";
|
||||
|
||||
Creating your own dictionary is a lot of work, especially if you are only extending an existing dictionary with a couple of new categories or items. So, what you can do is extend a loaded validator like this (code taken from DSSP):
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// db is a cif::datablock reference containing an mmCIF file with DSSP annotations
|
||||
auto &validator = const_cast<cif::validator &>(*db.get_validator());
|
||||
if (validator.get_validator_for_category("dssp_struct_summary") == nullptr)
|
||||
{
|
||||
auto dssp_extension = cif::load_resource("dssp-extension.dic");
|
||||
if (dssp_extension)
|
||||
cif::extend_dictionary(validator, *dssp_extension);
|
||||
}
|
||||
|
||||
.. note::
|
||||
|
||||
In the example above we're loading the data using :doc:`/resources`. See the documentation on that for more information.
|
||||
|
||||
If a validator has been assigned to a file, assignments to items are checked for valid data. So the following code will throw an exception (see: :ref:`_atom_site-label`):
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
auto rh = atom_site.front();
|
||||
rh["Cartn_x"] = "foo";
|
||||
|
||||
Linking
|
||||
-------
|
||||
|
||||
Based on information recorded in dictionary files (see :ref:`Validation`) you can locate linked records in parent or child categories.
|
||||
|
||||
To make this example not too complex, lets assume the following example file:
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
data_test
|
||||
loop_
|
||||
_cat_1.id
|
||||
_cat_1.name
|
||||
_cat_1.desc
|
||||
1 aap Aap
|
||||
2 noot Noot
|
||||
3 mies Mies
|
||||
|
||||
loop_
|
||||
_cat_2.id
|
||||
_cat_2.name
|
||||
_cat_2.num
|
||||
_cat_2.desc
|
||||
1 aap 1 'Een dier'
|
||||
2 aap 2 'Een andere aap'
|
||||
3 noot 1 'walnoot bijvoorbeeld'
|
||||
|
||||
And we have a dictionary containing the following link definition:
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
loop_
|
||||
_pdbx_item_linked_group_list.parent_category_id
|
||||
_pdbx_item_linked_group_list.link_group_id
|
||||
_pdbx_item_linked_group_list.parent_name
|
||||
_pdbx_item_linked_group_list.child_name
|
||||
_pdbx_item_linked_group_list.child_category_id
|
||||
cat_1 1 '_cat_1.name' '_cat_2.name' cat_2
|
||||
|
||||
So, there are links between *cat_1* and *cat_2* based on the value in items named *name*. Using this information, we can now locate children and parents:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// Assuming the file was loaded in f:
|
||||
auto &cat1 = f.front()["cat_1"];
|
||||
auto &cat2 = f.front()["cat_2"];
|
||||
auto &cat3 = f.front()["cat_3"];
|
||||
|
||||
// Loop over all ape's in cat2
|
||||
for (auto r : cat1.get_children(cat1.find1("name"_key == "aap"), cat2))
|
||||
std::cout << r.get<std::string>("desc") << '\n';
|
||||
|
||||
Updating a value in an item in a parent category will update the corresponding value in all related children:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
auto r1 = cat1.find1("id"_key == 1);
|
||||
r1["name"] = "aapje";
|
||||
|
||||
auto rs1 = cat2.find("name"_key == "aapje");
|
||||
assert(rs1.size() == 2);
|
||||
|
||||
However, changing a value in a child record will not update the parent. This may result in an invalid file since you may then have a child that has no parent:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
auto r2 = cat2.find1("id"_key == 3);
|
||||
r2["name"] = "wim";
|
||||
|
||||
assert(f.is_valid() == false);
|
||||
|
||||
So you have to fix this yourself by inserting a new item in cat1 with the new value.
|
||||
|
||||
.. _splitting-rows:
|
||||
Another situation is when you change a value in a parent and updating children might introduce a situation where you need to split a child. To give an example, consider this:
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
data_test
|
||||
loop_
|
||||
_cat_1.id
|
||||
_cat_1.name
|
||||
_cat_1.desc
|
||||
1 aap Aap
|
||||
2 noot Noot
|
||||
3 mies Mies
|
||||
|
||||
loop_
|
||||
_cat_2.id
|
||||
_cat_2.name
|
||||
_cat_2.num
|
||||
_cat_2.desc
|
||||
1 aap 1 'Een dier'
|
||||
2 aap 2 'Een andere aap'
|
||||
3 noot 1 'walnoot bijvoorbeeld'
|
||||
|
||||
loop_
|
||||
_cat_3.id
|
||||
_cat_3.name
|
||||
_cat_3.num
|
||||
1 aap 1
|
||||
2 aap 2
|
||||
|
||||
And we have a dictionary containing the following link definition (reversed compared to the previous example):
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
loop_
|
||||
_pdbx_item_linked_group_list.parent_category_id
|
||||
_pdbx_item_linked_group_list.link_group_id
|
||||
_pdbx_item_linked_group_list.parent_name
|
||||
_pdbx_item_linked_group_list.child_name
|
||||
_pdbx_item_linked_group_list.child_category_id
|
||||
cat_2 1 '_cat_2.name' '_cat_1.name' cat_1
|
||||
cat_3 1 '_cat_3.name' '_cat_2.name' cat_2
|
||||
cat_3 1 '_cat_3.num' '_cat_2.num' cat_2
|
||||
|
||||
So *cat3* is a parent of *cat2* and *cat2* is a parent of *cat1*. Now, if you change the *name* value of the first row of *cat3* to 'aapje', the corresponding row in *cat2* is updated as well. But when you update *cat2* you have to update *cat1* too. And simply changing the name field in row 1 of *cat1* is wrong. The default behaviour in libcifpp is to split the record in *cat1* and have a new child with the new name whereas the other remains as is.
|
||||
|
||||
The new *cat1* will thus be like:
|
||||
|
||||
.. code-block:: cif
|
||||
|
||||
loop_
|
||||
_cat_1.id
|
||||
_cat_1.name
|
||||
_cat_1.desc
|
||||
1 aapje Aap
|
||||
2 noot Noot
|
||||
3 mies Mies
|
||||
5 aap Aap
|
||||
|
||||
49
docs/bitsandpieces.rst
Normal file
49
docs/bitsandpieces.rst
Normal file
@@ -0,0 +1,49 @@
|
||||
Bits & Pieces
|
||||
=============
|
||||
|
||||
The *libcifpp* library offers some extra code that makes the life of developers a bit easier.
|
||||
|
||||
gzio
|
||||
----
|
||||
|
||||
To work with compressed data files a *std::streambuf* implemenation was added based on the code in `gxrio <https://github.com/mhekkel/gxrio>`_. This allows you to read and write compressed data streams transparently.
|
||||
|
||||
When working with files you can use :cpp:class:`cif::gzio::ifstream` and :cpp:class:`cif::gzio::ofstream`. The selection of whether to use compression or not is based on the file extension. If it is ``.gz`` gzip compression is used:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::gzio::ifstream file("my-file.txt.gz");
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
std::cout << line << '\n';
|
||||
|
||||
Writing is equally easy:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cif::gzio::ofstream file("/tmp/output.txt.gz");
|
||||
file << "Hello, world!";
|
||||
file.close();
|
||||
|
||||
You can also use the :cpp:class:`cif::gzio::istream` and feed it a *std::streambuf* object that may or may not contain compressed data. In that case the first bytes of the input are sniffed and if it is gzip compressed data, decompression will be done.
|
||||
|
||||
A progress bar
|
||||
--------------
|
||||
|
||||
Applications based on *libcifpp* may have a longer run time. To give some feedback to the user running your application in a terminal you can use the :cpp:class:`cif::progress_bar`. This class will display an ASCII progress bar along with optional status messages, but only if output is to a real TTY (terminal).
|
||||
|
||||
A progress bar is also shown only if the duration is more than two seconds. To avoid having flashing progress bars for short actions.
|
||||
|
||||
The progress bar uses an internal progress counter that starts at zero and ends when the max value has been reached after which it will be removed from the screen. Updating this internal progress counter can be done by adding a number of steps calling :cpp:func:`cif::progress_bar::consumed` or by setting the exact value for the counter by calling :cpp:func:`cif::progress_bar::progress`.
|
||||
|
||||
Colouring output
|
||||
----------------
|
||||
|
||||
It is also nice to emphasise some output in the terminal by using colours. For this you can create output manipulators using :cpp:func:`cif::coloured`. To write a string in white, and bold letters on a red background you can do:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
using namespace cif::colour;
|
||||
std::cout << cif::coloured("Hello, world!", white, red, bold) << '\n';
|
||||
|
||||
33
docs/compound.rst
Normal file
33
docs/compound.rst
Normal file
@@ -0,0 +1,33 @@
|
||||
Chemical Compounds
|
||||
==================
|
||||
|
||||
The data in *CIF* and *mmCIF* files often describes the structure of some chemical compounds. The structure is recorded in the categories *atom_site* and friends. Records in these categories refer to chemical compounds using a compound ID. This compound ID is the ID field of the *chem_comp* category. For all of the known compounds in the PDB there is an entry in the Chemical Compounds Dictionary or `CCD <https://www.wwpdb.org/data/ccd>`_. If *libcifpp* was properly installed you have a copy of this file somewhere on your disk. And if you have installed the update scripts, a fresh version of this file will be retrieved weekly.
|
||||
|
||||
As an alternative to CCD there are the monomer library files from `CCP4 <https://www.ccp4.ac.uk/>`_. These contain somewhat different data but the overlap is good enough for usage in *libcifpp*.
|
||||
|
||||
Information about compounds is captured in the :cpp:class:`cif::compound`. An instance of a compound object for a certain compound ID can be obtained by using the singleton :cpp:class:`cif::compound_factory`.
|
||||
|
||||
If the compound you want to use is not available in the CCD or in CCP4, you can add that information yourself. For this you can use the method :cpp:func:`cif::compound_factory::push_dictionary`.
|
||||
|
||||
So, given that we have CCD, CCP4 monomer library and used defined compound definitions, what will you get when you try to retrieve such a compound by ID? The answer is, the factory has a stack of compound generators. The first thrown on the stack is the one for a CCD file (*components.cif*) if it can be found. Then, if the *CLIBD_MON* environmental variable is defined, a generator for monomer library files is added to the stack. And then all generators for files you added using *push_dictionary* are added in order. The generators are searched in the reverse order in which they were added to see if it creates a compound object for the ID. If no compound was created at all, nullptr is returned.
|
||||
|
||||
Updating CCD
|
||||
------------
|
||||
|
||||
The CCD data is stored in a single file called *components.cif* and can be downloaded from `CCD <https://www.wwpdb.org/data/ccd>`_.
|
||||
|
||||
As can be read in the section on resources (:doc:`/resources`) files in libcifpp are loaded in a specific order. If the CCD datafile was downloaded during installation, a copy can be found in the directory */usr/share/libcifpp/* (if you installed in */usr*). This is a static file and will not be updated until the next installation of libcifpp.
|
||||
|
||||
When configuring libcifpp, you can specify the *CIFPP_INSTALL_UPDATE_SCRIPT* option, as in:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
cmake -S . -B build -DCIFPP_INSTALL_UPDATE_SCRIPT=ON # ... more options?
|
||||
|
||||
This will install a script named *update-libcifpp-data* in */etc/cron.weekly* or */etc/periodic/weekly*. This file uses a config file named */etc/libcifpp.conf* which you then need to edit. In this config file the following line needs to be uncommented:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
# update=true
|
||||
|
||||
After that, the update script will weekly download the latest components.cif file to */var/cache/libcifpp*.
|
||||
66
docs/conf.py.in
Normal file
66
docs/conf.py.in
Normal file
@@ -0,0 +1,66 @@
|
||||
project = '@PROJECT_NAME@'
|
||||
copyright = '2023, Maarten L. Hekkelman'
|
||||
author = 'Maarten L. Hekkelman'
|
||||
release = '@PROJECT_VERSION@'
|
||||
|
||||
# -- General configuration ---------------------------------------------------
|
||||
|
||||
extensions = [
|
||||
"breathe",
|
||||
"exhale",
|
||||
"myst_parser"
|
||||
]
|
||||
|
||||
breathe_projects = {
|
||||
"@PROJECT_NAME@": "../build/docs/xml"
|
||||
}
|
||||
|
||||
myst_enable_extensions = [ "colon_fence" ]
|
||||
breathe_default_project = "@PROJECT_NAME@"
|
||||
|
||||
# Setup the exhale extension
|
||||
exhale_args = {
|
||||
# These arguments are required
|
||||
"containmentFolder": "./api",
|
||||
"rootFileName": "library_root.rst",
|
||||
"doxygenStripFromPath": "../include/",
|
||||
# Heavily encouraged optional argument (see docs)
|
||||
"rootFileTitle": "API Reference",
|
||||
# Suggested optional arguments
|
||||
# "createTreeView": True,
|
||||
# TIP: if using the sphinx-bootstrap-theme, you need
|
||||
# "treeViewIsBootstrap": True,
|
||||
"exhaleExecutesDoxygen": False,
|
||||
"contentsDirectives" : False,
|
||||
|
||||
"verboseBuild": False
|
||||
}
|
||||
|
||||
# Tell sphinx what the primary language being documented is.
|
||||
primary_domain = 'cpp'
|
||||
|
||||
# Tell sphinx what the pygments highlight language should be.
|
||||
highlight_language = 'cpp'
|
||||
|
||||
templates_path = ['_templates']
|
||||
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
|
||||
|
||||
# -- Options for HTML output -------------------------------------------------
|
||||
|
||||
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||
# a list of builtin themes.
|
||||
#
|
||||
html_theme = 'sphinx_rtd_theme'
|
||||
|
||||
# Add any paths that contain custom static files (such as style sheets) here,
|
||||
# relative to this directory. They are copied after the builtin static files,
|
||||
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||
html_static_path = ['_static']
|
||||
|
||||
html_theme_options = {
|
||||
}
|
||||
|
||||
cpp_index_common_prefix = [
|
||||
'cif::'
|
||||
]
|
||||
|
||||
2
docs/genindex.rst
Normal file
2
docs/genindex.rst
Normal file
@@ -0,0 +1,2 @@
|
||||
Index
|
||||
=====
|
||||
46
docs/index.rst
Normal file
46
docs/index.rst
Normal file
@@ -0,0 +1,46 @@
|
||||
Introduction
|
||||
============
|
||||
|
||||
Information on 3D structures of proteins originally came formatted in `PDB <http://www.wwpdb.org/documentation/file-format-content/format33/v3.3.html>`_ files. Although the specification for this format had some real restrictions like a mandatory HEADER and CRYST line, many programs implemented this very poorly often writing out only ATOM records. And users became used to this.
|
||||
|
||||
The legacy PDB format has some severe limitations rendering it useless for all but very small protein structures. A new format called `mmCIF <https://mmcif.wwpdb.org/>`_ has been around for decades and now is the default format for the Protein Data Bank.
|
||||
|
||||
The software developed in the `PDB-REDO <https://pdb-redo.eu/>`_ project aims at improving 3D models based on original experimental data. For this, the tools need to be able to work with both legacy PDB and mmCIF files. A decision was made to make mmCIF leading internally in all programs and convert legacy PDB directly into mmCIF before processing the data. A robust conversion had to be developed to make this possible since, as noted above, files can come with more or less information making it sometimes needed to do a sequence alignment to find out the exact residue numbers.
|
||||
|
||||
And so libcif++ came to life, a library to work with mmCIF files. Work on this library started early 2017 and has developed quite a bit since then. To reduce dependency on other libraries, some functionality was added that is not strictly related to reading and writing mmCIF files but may be useful nonetheless. This is mostly code that is used in 3D calculations and symmetry operations.
|
||||
|
||||
Design
|
||||
------
|
||||
|
||||
The main part of the library is a set of classes that work with mmCIF files. They are:
|
||||
|
||||
* :cpp:class:`cif::file`
|
||||
* :cpp:class:`cif::datablock`
|
||||
* :cpp:class:`cif::category`
|
||||
|
||||
The :cpp:class:`cif::file` class encapsulates the contents of a mmCIF file. In such a file there are one or more :cpp:class:`cif::datablock` objects and each datablock contains one or more :cpp:class:`cif::category` objects.
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
|
||||
Using *libcifpp* is easy, if you are familiar with modern C++:
|
||||
|
||||
.. literalinclude:: ../README.md
|
||||
:language: c++
|
||||
:start-after: ```c++
|
||||
:end-before: ```
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
:caption: Contents
|
||||
|
||||
self
|
||||
basics.rst
|
||||
compound.rst
|
||||
model.rst
|
||||
resources.rst
|
||||
symmetry.rst
|
||||
bitsandpieces.rst
|
||||
api/library_root.rst
|
||||
genindex.rst
|
||||
|
||||
36
docs/model.rst
Normal file
36
docs/model.rst
Normal file
@@ -0,0 +1,36 @@
|
||||
Molecular Model
|
||||
===============
|
||||
|
||||
Theoretically it is possible to get along with only the classes *cif::file*, *cif::datablock* and *cif::category*. But to keep your data complete and valid you then have to update lots of categories for all but the simplest manipulations. For this *libcifpp* comes with a higher level API modelling atoms, residues, monomers, polymers and complete structures in their respective classes.
|
||||
|
||||
Note that these classes only work properly if you are using *mmCIF* files and have an mmcif_pdbx dictionary available, either compiled in using `mrc <https://github.com/mhekkel/mrc.git>`_ or installed in the proper location.
|
||||
|
||||
.. note::
|
||||
|
||||
This part of *libcifpp* is the least developed part. What is available should work but functionality should eventually be extended.
|
||||
|
||||
Atom
|
||||
----
|
||||
|
||||
The :cpp:class:`cif::mm::atom` is a lightweight proxy class giving access to the data stored in *atom_site* and *atom_site_anisotrop*. It only caches the most often used item data and every modification is directly written back into the *mmCIF* categories.
|
||||
|
||||
Atoms can be copied by value with low cost. The atom class only contains a pointer to an implementation that is reference counted.
|
||||
|
||||
Residue, Monomer and Polymer
|
||||
----------------------------
|
||||
|
||||
The :cpp:class:`cif::mm::residue`, :cpp:class:`cif::mm::monomer` and :cpp:class:`cif::mm::polymer` implement what you'd expect. A monomer is a residue that is part of a polymer and thus has a sequence number and siblings.
|
||||
|
||||
Sugars & Branches
|
||||
-----------------
|
||||
|
||||
There are also classes for modelling sugars and sugar branches. You can create sugar branches
|
||||
|
||||
Structure
|
||||
---------
|
||||
|
||||
The :cpp:class:`cif::mm::structure` can be used to load one of the models from an *mmCIF* file. By default the first model is loaded. (Multiple models are often only available files containing structures defined using NMR).
|
||||
|
||||
A structure holds a reference to a *cif::datablock* and retrieves its data from this datablock and writes any modification back into that datablock.
|
||||
|
||||
One of the most useful parts of the structure class is the ability to create and modify residues. This updates related *chem_comp* and *entity* categories as well.
|
||||
5
docs/requirements.in
Normal file
5
docs/requirements.in
Normal file
@@ -0,0 +1,5 @@
|
||||
sphinx<5
|
||||
exhale==0.3.6
|
||||
myst-parser
|
||||
breathe
|
||||
sphinx_rtd_theme==1.3.0
|
||||
93
docs/requirements.txt
Normal file
93
docs/requirements.txt
Normal file
@@ -0,0 +1,93 @@
|
||||
#
|
||||
# This file is autogenerated by pip-compile with Python 3.10
|
||||
# by the following command:
|
||||
#
|
||||
# pip-compile --output-file=requirements.txt requirements.in
|
||||
#
|
||||
alabaster==0.7.13
|
||||
# via sphinx
|
||||
babel==2.12.1
|
||||
# via sphinx
|
||||
beautifulsoup4==4.12.2
|
||||
# via exhale
|
||||
breathe==4.35.0
|
||||
# via
|
||||
# -r requirements.in
|
||||
# exhale
|
||||
certifi==2023.7.22
|
||||
# via requests
|
||||
charset-normalizer==3.2.0
|
||||
# via requests
|
||||
docutils==0.17.1
|
||||
# via
|
||||
# breathe
|
||||
# exhale
|
||||
# myst-parser
|
||||
# sphinx
|
||||
# sphinx-rtd-theme
|
||||
exhale==0.3.6
|
||||
# via -r requirements.in
|
||||
idna==3.4
|
||||
# via requests
|
||||
imagesize==1.4.1
|
||||
# via sphinx
|
||||
jinja2==3.1.2
|
||||
# via
|
||||
# myst-parser
|
||||
# sphinx
|
||||
lxml==4.9.3
|
||||
# via exhale
|
||||
markdown-it-py==2.2.0
|
||||
# via
|
||||
# mdit-py-plugins
|
||||
# myst-parser
|
||||
markupsafe==2.1.3
|
||||
# via jinja2
|
||||
mdit-py-plugins==0.3.5
|
||||
# via myst-parser
|
||||
mdurl==0.1.2
|
||||
# via markdown-it-py
|
||||
myst-parser==0.18.1
|
||||
# via -r requirements.in
|
||||
packaging==23.1
|
||||
# via sphinx
|
||||
pygments==2.16.1
|
||||
# via sphinx
|
||||
pyyaml==6.0.1
|
||||
# via myst-parser
|
||||
requests==2.31.0
|
||||
# via sphinx
|
||||
six==1.16.0
|
||||
# via exhale
|
||||
snowballstemmer==2.2.0
|
||||
# via sphinx
|
||||
soupsieve==2.4.1
|
||||
# via beautifulsoup4
|
||||
sphinx==4.5.0
|
||||
# via
|
||||
# -r requirements.in
|
||||
# breathe
|
||||
# exhale
|
||||
# myst-parser
|
||||
# sphinx-rtd-theme
|
||||
# sphinxcontrib-jquery
|
||||
sphinx-rtd-theme==1.3.0
|
||||
# via -r requirements.in
|
||||
sphinxcontrib-applehelp==1.0.4
|
||||
# via sphinx
|
||||
sphinxcontrib-devhelp==1.0.2
|
||||
# via sphinx
|
||||
sphinxcontrib-htmlhelp==2.0.1
|
||||
# via sphinx
|
||||
sphinxcontrib-jquery==4.1
|
||||
# via sphinx-rtd-theme
|
||||
sphinxcontrib-jsmath==1.0.1
|
||||
# via sphinx
|
||||
sphinxcontrib-qthelp==1.0.3
|
||||
# via sphinx
|
||||
sphinxcontrib-serializinghtml==1.1.5
|
||||
# via sphinx
|
||||
typing-extensions==4.7.1
|
||||
# via myst-parser
|
||||
urllib3==2.0.4
|
||||
# via requests
|
||||
47
docs/resources.rst
Normal file
47
docs/resources.rst
Normal file
@@ -0,0 +1,47 @@
|
||||
Resources
|
||||
=========
|
||||
|
||||
Programs using libcifpp often need access to common data files. E.g. CIF dictionary files, CCP4 monomer restraints files or the CCD data file. In libcifpp these files are called resources. These files are often also based on external sources that are updated on a regular basis.
|
||||
|
||||
Resources can be compiled into the executable so that the resulting
|
||||
application can be made portable to other machines. For this you
|
||||
need to use `mrc <https://github.com/mhekkel/mrc.git>`_ which only works
|
||||
on Un*x like systems using the ELF executable format or on MS Windows
|
||||
|
||||
But resources may also be located as files on the filesytem at
|
||||
specific locations. And you can specify your own location for
|
||||
files (a directory) or even override named resources with your
|
||||
own data.
|
||||
|
||||
Loading Resources
|
||||
-----------------
|
||||
|
||||
No matter where the resource is located, you should always use the single libcifpp API call :cpp:func:`cif::load_resource` to load them. This function returns a *std::istream* wrapped inside a *std::unique_ptr*.
|
||||
|
||||
The order in which resources are search for is:
|
||||
|
||||
* Use the resource that was defined by calling :cpp:func:`cif::add_file_resource`
|
||||
for this name.
|
||||
|
||||
* Search the paths specified by :cpp:func:`cif::add_data_directory`, last one
|
||||
added is searched first
|
||||
|
||||
* Search the so-called *CACHE_DIR*. This location is defined
|
||||
at compile time and based on the installation directory of
|
||||
libcifpp. Usually it is */var/cache/libcifpp*.
|
||||
It is in this directory where the cron job for libcifpp will
|
||||
put the updated files weekly.
|
||||
|
||||
* If the *CCP4* environment is available, the
|
||||
*$ENV{CCP4}/share/libcifpp* is searched.
|
||||
|
||||
* If the environment variable *LIBCIFPP_DATA_DIR* is set it
|
||||
is searched
|
||||
|
||||
* The *DATA_DIR* is searched, this is also a variable defined
|
||||
at compile time, also based on the installation directory
|
||||
of libcifpp. It usually is */usr/share/libcifpp*
|
||||
|
||||
* As a last resort an attempt is made to load the data from
|
||||
resources compiled by `mrc <https://github.com/mhekkel/mrc.git>`_.
|
||||
|
||||
108
docs/symmetry.rst
Normal file
108
docs/symmetry.rst
Normal file
@@ -0,0 +1,108 @@
|
||||
Symmetry & Geometry
|
||||
===================
|
||||
|
||||
Although not really a core *CIF* functionality, when working with *mmCIF* files you often need to work with symmetry information. And symmetry works on points in a certain space and thus geometry calculations are also something you need often. Former versions of *libcifpp* used to use `clipper <http://www.ysbl.york.ac.uk/~cowtan/clipper/doc/index.html>`_ to do many of these calculations, but that introduces a dependency and besides, the way clipper numbers symmetry operations is not completely compatible with the way this is done in the PDB.
|
||||
|
||||
Points
|
||||
------
|
||||
|
||||
The most basic type in use is :cpp:type:`cif::point`. It can be thought of as a point in space with three coordinates, but it is also often used as a vector in 3d space. To keep the interface simple there's no separate vector type.
|
||||
|
||||
Many functions are available in :ref:`file_cif++_point.hpp` that work on points. There are functions to calculate the :cpp:func:`cif::distance` between two points and also function to calculate dot products, cross products and dihedral angles between sets of points.
|
||||
|
||||
Quaternions
|
||||
-----------
|
||||
|
||||
All operations inside *libcifpp* that perform some kind of rotation use :cpp:type:`cif::quaternion`. The reason to use Quaternions is not only that they are cool, they are faster than multiplying with a matrix and the results also suffer less from numerical instability.
|
||||
|
||||
Matrix
|
||||
------
|
||||
|
||||
Although Quaternions are the preferred way of doing rotations, not every manipulation is a rotation and thus we need a matrix class as well. Matrices and their operations are encoded as matrix_expressions in *libcifpp* allowing the compiler to generate very fast code. See the :ref:`file_cif++_matrix.hpp` for what is on offer.
|
||||
|
||||
Crystals
|
||||
--------
|
||||
|
||||
The *CIF* and *mmCIF* were initially developed to store crystallographic information on structures. Apart from coordinates and the chemical information the crystallographic information is important. This information can be split into two parts, a unit cell and a set of :ref:`symmetry-ops` making up a spacegroup. The spacegroup number and name are stored in the *symmetry* category. The corresponding symmetry operations can be obtained in *libcifpp* by using the :cpp:class:`cif::spacegroup`. The cell is stored in the category *cell* and likewise can be loaded using the :cpp:class:`cif::cell`. Together these two classes make up a crystal and so we have a :cpp:class:`cif::crystal` which contains both. You can easily create such a crystal object by passing the datablock containing the data to the constructor. As in:
|
||||
|
||||
.. code:: cpp
|
||||
|
||||
// Load the file
|
||||
cif::file f("1cbs.cif.gz");
|
||||
|
||||
auto &db = f.front();
|
||||
cif::crystal c(db);
|
||||
|
||||
.. _symmetry-ops:
|
||||
Symmetry operations
|
||||
-------------------
|
||||
|
||||
Each basic symmetry operation in the crystallographic world consists of a matrix multiplication followed by a translation. To apply such an operation on a carthesian coordinate you first have to convert the point into a fractional coordinate with respect to the unit cell of the crystal, then apply the matrix and translation operations and then convert the result back into carthesian coordinates. This is all done by the proper routines in *libcifpp*.
|
||||
|
||||
Symmetry operations are encoded as a string in *mmCIF* PDBx files. The format is a string with the rotational number followed by an underscore and then the encoded translation in each direction where 5 means no translation. So, the identity operator is ``1_555`` meaning that we have rotational number 1 (which is always the identity rotation, point multiplied with the identity matrix) and a translation of zero in each direction.
|
||||
|
||||
To give an idea how this works, here's a piece of code copied from one of the unit tests in *libcifpp*. It takes the *struct_conn* records in a certain PDB file and checks wether the distances in each row correspond to what we can calculate.
|
||||
|
||||
.. code:: cpp
|
||||
|
||||
using namespace cif::literals;
|
||||
|
||||
// Load the file
|
||||
cif::file f(gTestDir / "2bi3.cif.gz");
|
||||
|
||||
// Simply assume we can use the first datablock
|
||||
auto &db = f.front();
|
||||
|
||||
// Load the crystal information
|
||||
cif::crystal c(db);
|
||||
|
||||
// Take references to the two categories we need
|
||||
auto struct_conn = db["struct_conn"];
|
||||
auto atom_site = db["atom_site"];
|
||||
|
||||
// Loop over all rows in struct_conn taking only the values we need
|
||||
for (const auto &[
|
||||
asym1, seqid1, authseqid1, atomid1, symm1,
|
||||
asym2, seqid2, authseqid2, atomid2, symm2,
|
||||
dist] : struct_conn.find<
|
||||
std::string,std::optional<int>,std::string,std::string,std::string,
|
||||
std::string,std::optional<int>,std::string,std::string,std::string,
|
||||
float>(
|
||||
cif::key("ptnr1_symmetry") != "1_555" or cif::key("ptnr2_symmetry") != "1_555",
|
||||
"ptnr1_label_asym_id", "ptnr1_label_seq_id", "ptnr1_auth_seq_id", "ptnr1_label_atom_id", "ptnr1_symmetry",
|
||||
"ptnr2_label_asym_id", "ptnr2_label_seq_id", "ptnr2_auth_seq_id", "ptnr2_label_atom_id", "ptnr2_symmetry",
|
||||
"pdbx_dist_value"
|
||||
))
|
||||
{
|
||||
// Find the location of the first atom
|
||||
cif::point p1 = atom_site.find1<float,float,float>(
|
||||
"label_asym_id"_key == asym1 and "label_seq_id"_key == seqid1 and "auth_seq_id"_key == authseqid1 and "label_atom_id"_key == atomid1,
|
||||
"cartn_x", "cartn_y", "cartn_z");
|
||||
|
||||
// Find the location of the second atom
|
||||
cif::point p2 = atom_site.find1<float,float,float>(
|
||||
"label_asym_id"_key == asym2 and "label_seq_id"_key == seqid2 and "auth_seq_id"_key == authseqid2 and "label_atom_id"_key == atomid2,
|
||||
"cartn_x", "cartn_y", "cartn_z");
|
||||
|
||||
// Calculate the position of the first atom using the symmetry operator defined in struct_conn
|
||||
auto sa1 = c.symmetry_copy(p1, cif::sym_op(symm1));
|
||||
|
||||
// Calculate the position of the second atom using the symmetry operator defined in struct_conn
|
||||
auto sa2 = c.symmetry_copy(p2, cif::sym_op(symm2));
|
||||
|
||||
// The distance between these symmetry atoms should be equal to the distance in the struct_conn record
|
||||
assert(cif::distance(sa1, sa2) == dist);
|
||||
|
||||
// And to show how you can obtain the closest symmetry copy of an atom near another one:
|
||||
// here we request the symmetry copy of p2 that lies closest to p1
|
||||
const auto &[d, p, so] = c.closest_symmetry_copy(p1, p2);
|
||||
|
||||
// And that should of course be equal to the location in struct_conn for p2
|
||||
assert(p.m_x == sa2.m_x);
|
||||
assert(p.m_y == sa2.m_y);
|
||||
assert(p.m_z == sa2.m_z);
|
||||
|
||||
// Distance and symmetry operator string should also be the same
|
||||
assert(d == dist);
|
||||
assert(so.string() == symm2);
|
||||
}
|
||||
7
examples/CMakeLists.txt
Normal file
7
examples/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
project(cifpp_example LANGUAGES CXX)
|
||||
|
||||
find_package(cifpp REQUIRED)
|
||||
|
||||
add_executable(example example.cpp)
|
||||
target_link_libraries(example cifpp::cifpp)
|
||||
@@ -8,13 +8,16 @@ namespace fs = std::filesystem;
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
std::cerr << "Usage: example <inputfile>\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
cif::file file = cif::pdb::read(argv[1]);
|
||||
|
||||
if (file.empty())
|
||||
{
|
||||
std::cerr << "Empty file" << std::endl;
|
||||
std::cerr << "Empty file\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -22,13 +25,13 @@ int main(int argc, char *argv[])
|
||||
auto &atom_site = db["atom_site"];
|
||||
auto n = atom_site.find(cif::key("label_atom_id") == "OXT").size();
|
||||
|
||||
std::cout << "File contains " << atom_site.size() << " atoms of which " << n << (n == 1 ? " is" : " are") << " OXT" << std::endl
|
||||
<< "residues with an OXT are:" << std::endl;
|
||||
std::cout << "File contains " << atom_site.size() << " atoms of which " << n << (n == 1 ? " is" : " are") << " OXT\n"
|
||||
<< "residues with an OXT are:\n";
|
||||
|
||||
for (const auto &[asym, comp, seqnr] : atom_site.find<std::string, std::string, int>(
|
||||
cif::key("label_atom_id") == "OXT", "label_asym_id", "label_comp_id", "label_seq_id"))
|
||||
{
|
||||
std::cout << asym << ' ' << comp << ' ' << seqnr << std::endl;
|
||||
std::cout << asym << ' ' << comp << ' ' << seqnr << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
CXX = c++ -std=c++17
|
||||
CXXFLAGS = $(shell pkg-config --cflags libcifpp)
|
||||
LIBS = $(shell pkg-config --libs libcifpp)
|
||||
|
||||
all: example
|
||||
|
||||
example: example.cpp
|
||||
$(CXX) -o $@ $? $(CXXFLAGS) $(LIBS)
|
||||
@@ -37,5 +37,5 @@
|
||||
|
||||
#include "cif++/model.hpp"
|
||||
|
||||
#include "cif++/pdb/io.hpp"
|
||||
#include "cif++/pdb.hpp"
|
||||
#include "cif++/gzio.hpp"
|
||||
@@ -24,207 +24,254 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
// Lib for working with structures as contained in mmCIF and PDB files
|
||||
/** \file atom_type.hpp
|
||||
*
|
||||
* This file contains information about all known elements
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/exports.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
/** Atom type as an integer. All known elements are available as a constant. */
|
||||
|
||||
enum atom_type : uint8_t
|
||||
{
|
||||
Nn = 0, // Unknown
|
||||
Nn = 0, ///< Unknown
|
||||
|
||||
H = 1, // Hydrogen
|
||||
He = 2, // Helium
|
||||
H = 1, ///< Hydrogen
|
||||
He = 2, ///< Helium
|
||||
|
||||
Li = 3, // Lithium
|
||||
Be = 4, // Beryllium
|
||||
B = 5, // Boron
|
||||
C = 6, // Carbon
|
||||
N = 7, // Nitrogen
|
||||
O = 8, // Oxygen
|
||||
F = 9, // Fluorine
|
||||
Ne = 10, // Neon
|
||||
Li = 3, ///< Lithium
|
||||
Be = 4, ///< Beryllium
|
||||
B = 5, ///< Boron
|
||||
C = 6, ///< Carbon
|
||||
N = 7, ///< Nitrogen
|
||||
O = 8, ///< Oxygen
|
||||
F = 9, ///< Fluorine
|
||||
Ne = 10, ///< Neon
|
||||
|
||||
Na = 11, // Sodium
|
||||
Mg = 12, // Magnesium
|
||||
Al = 13, // Aluminium
|
||||
Si = 14, // Silicon
|
||||
P = 15, // Phosphorus
|
||||
S = 16, // Sulfur
|
||||
Cl = 17, // Chlorine
|
||||
Ar = 18, // Argon
|
||||
Na = 11, ///< Sodium
|
||||
Mg = 12, ///< Magnesium
|
||||
Al = 13, ///< Aluminium
|
||||
Si = 14, ///< Silicon
|
||||
P = 15, ///< Phosphorus
|
||||
S = 16, ///< Sulfur
|
||||
Cl = 17, ///< Chlorine
|
||||
Ar = 18, ///< Argon
|
||||
|
||||
K = 19, // Potassium
|
||||
Ca = 20, // Calcium
|
||||
Sc = 21, // Scandium
|
||||
Ti = 22, // Titanium
|
||||
V = 23, // Vanadium
|
||||
Cr = 24, // Chromium
|
||||
Mn = 25, // Manganese
|
||||
Fe = 26, // Iron
|
||||
Co = 27, // Cobalt
|
||||
Ni = 28, // Nickel
|
||||
Cu = 29, // Copper
|
||||
Zn = 30, // Zinc
|
||||
Ga = 31, // Gallium
|
||||
Ge = 32, // Germanium
|
||||
As = 33, // Arsenic
|
||||
Se = 34, // Selenium
|
||||
Br = 35, // Bromine
|
||||
Kr = 36, // Krypton
|
||||
K = 19, ///< Potassium
|
||||
Ca = 20, ///< Calcium
|
||||
Sc = 21, ///< Scandium
|
||||
Ti = 22, ///< Titanium
|
||||
V = 23, ///< Vanadium
|
||||
Cr = 24, ///< Chromium
|
||||
Mn = 25, ///< Manganese
|
||||
Fe = 26, ///< Iron
|
||||
Co = 27, ///< Cobalt
|
||||
Ni = 28, ///< Nickel
|
||||
Cu = 29, ///< Copper
|
||||
Zn = 30, ///< Zinc
|
||||
Ga = 31, ///< Gallium
|
||||
Ge = 32, ///< Germanium
|
||||
As = 33, ///< Arsenic
|
||||
Se = 34, ///< Selenium
|
||||
Br = 35, ///< Bromine
|
||||
Kr = 36, ///< Krypton
|
||||
|
||||
Rb = 37, // Rubidium
|
||||
Sr = 38, // Strontium
|
||||
Y = 39, // Yttrium
|
||||
Zr = 40, // Zirconium
|
||||
Nb = 41, // Niobium
|
||||
Mo = 42, // Molybdenum
|
||||
Tc = 43, // Technetium
|
||||
Ru = 44, // Ruthenium
|
||||
Rh = 45, // Rhodium
|
||||
Pd = 46, // Palladium
|
||||
Ag = 47, // Silver
|
||||
Cd = 48, // Cadmium
|
||||
In = 49, // Indium
|
||||
Sn = 50, // Tin
|
||||
Sb = 51, // Antimony
|
||||
Te = 52, // Tellurium
|
||||
I = 53, // Iodine
|
||||
Xe = 54, // Xenon
|
||||
Cs = 55, // Caesium
|
||||
Ba = 56, // Barium
|
||||
La = 57, // Lanthanum
|
||||
Rb = 37, ///< Rubidium
|
||||
Sr = 38, ///< Strontium
|
||||
Y = 39, ///< Yttrium
|
||||
Zr = 40, ///< Zirconium
|
||||
Nb = 41, ///< Niobium
|
||||
Mo = 42, ///< Molybdenum
|
||||
Tc = 43, ///< Technetium
|
||||
Ru = 44, ///< Ruthenium
|
||||
Rh = 45, ///< Rhodium
|
||||
Pd = 46, ///< Palladium
|
||||
Ag = 47, ///< Silver
|
||||
Cd = 48, ///< Cadmium
|
||||
In = 49, ///< Indium
|
||||
Sn = 50, ///< Tin
|
||||
Sb = 51, ///< Antimony
|
||||
Te = 52, ///< Tellurium
|
||||
I = 53, ///< Iodine
|
||||
Xe = 54, ///< Xenon
|
||||
Cs = 55, ///< Caesium
|
||||
Ba = 56, ///< Barium
|
||||
La = 57, ///< Lanthanum
|
||||
|
||||
Hf = 72, // Hafnium
|
||||
Ta = 73, // Tantalum
|
||||
W = 74, // Tungsten
|
||||
Re = 75, // Rhenium
|
||||
Os = 76, // Osmium
|
||||
Ir = 77, // Iridium
|
||||
Pt = 78, // Platinum
|
||||
Au = 79, // Gold
|
||||
Hg = 80, // Mercury
|
||||
Tl = 81, // Thallium
|
||||
Pb = 82, // Lead
|
||||
Bi = 83, // Bismuth
|
||||
Po = 84, // Polonium
|
||||
At = 85, // Astatine
|
||||
Rn = 86, // Radon
|
||||
Fr = 87, // Francium
|
||||
Ra = 88, // Radium
|
||||
Ac = 89, // Actinium
|
||||
Hf = 72, ///< Hafnium
|
||||
Ta = 73, ///< Tantalum
|
||||
W = 74, ///< Tungsten
|
||||
Re = 75, ///< Rhenium
|
||||
Os = 76, ///< Osmium
|
||||
Ir = 77, ///< Iridium
|
||||
Pt = 78, ///< Platinum
|
||||
Au = 79, ///< Gold
|
||||
Hg = 80, ///< Mercury
|
||||
Tl = 81, ///< Thallium
|
||||
Pb = 82, ///< Lead
|
||||
Bi = 83, ///< Bismuth
|
||||
Po = 84, ///< Polonium
|
||||
At = 85, ///< Astatine
|
||||
Rn = 86, ///< Radon
|
||||
Fr = 87, ///< Francium
|
||||
Ra = 88, ///< Radium
|
||||
Ac = 89, ///< Actinium
|
||||
|
||||
Rf = 104, // Rutherfordium
|
||||
Db = 105, // Dubnium
|
||||
Sg = 106, // Seaborgium
|
||||
Bh = 107, // Bohrium
|
||||
Hs = 108, // Hassium
|
||||
Mt = 109, // Meitnerium
|
||||
Ds = 110, // Darmstadtium
|
||||
Rg = 111, // Roentgenium
|
||||
Cn = 112, // Copernicium
|
||||
Nh = 113, // Nihonium
|
||||
Fl = 114, // Flerovium
|
||||
Mc = 115, // Moscovium
|
||||
Lv = 116, // Livermorium
|
||||
Ts = 117, // Tennessine
|
||||
Og = 118, // Oganesson
|
||||
Rf = 104, ///< Rutherfordium
|
||||
Db = 105, ///< Dubnium
|
||||
Sg = 106, ///< Seaborgium
|
||||
Bh = 107, ///< Bohrium
|
||||
Hs = 108, ///< Hassium
|
||||
Mt = 109, ///< Meitnerium
|
||||
Ds = 110, ///< Darmstadtium
|
||||
Rg = 111, ///< Roentgenium
|
||||
Cn = 112, ///< Copernicium
|
||||
Nh = 113, ///< Nihonium
|
||||
Fl = 114, ///< Flerovium
|
||||
Mc = 115, ///< Moscovium
|
||||
Lv = 116, ///< Livermorium
|
||||
Ts = 117, ///< Tennessine
|
||||
Og = 118, ///< Oganesson
|
||||
|
||||
Ce = 58, // Cerium
|
||||
Pr = 59, // Praseodymium
|
||||
Nd = 60, // Neodymium
|
||||
Pm = 61, // Promethium
|
||||
Sm = 62, // Samarium
|
||||
Eu = 63, // Europium
|
||||
Gd = 64, // Gadolinium
|
||||
Tb = 65, // Terbium
|
||||
Dy = 66, // Dysprosium
|
||||
Ho = 67, // Holmium
|
||||
Er = 68, // Erbium
|
||||
Tm = 69, // Thulium
|
||||
Yb = 70, // Ytterbium
|
||||
Lu = 71, // Lutetium
|
||||
Ce = 58, ///< Cerium
|
||||
Pr = 59, ///< Praseodymium
|
||||
Nd = 60, ///< Neodymium
|
||||
Pm = 61, ///< Promethium
|
||||
Sm = 62, ///< Samarium
|
||||
Eu = 63, ///< Europium
|
||||
Gd = 64, ///< Gadolinium
|
||||
Tb = 65, ///< Terbium
|
||||
Dy = 66, ///< Dysprosium
|
||||
Ho = 67, ///< Holmium
|
||||
Er = 68, ///< Erbium
|
||||
Tm = 69, ///< Thulium
|
||||
Yb = 70, ///< Ytterbium
|
||||
Lu = 71, ///< Lutetium
|
||||
|
||||
Th = 90, // Thorium
|
||||
Pa = 91, // Protactinium
|
||||
U = 92, // Uranium
|
||||
Np = 93, // Neptunium
|
||||
Pu = 94, // Plutonium
|
||||
Am = 95, // Americium
|
||||
Cm = 96, // Curium
|
||||
Bk = 97, // Berkelium
|
||||
Cf = 98, // Californium
|
||||
Es = 99, // Einsteinium
|
||||
Fm = 100, // Fermium
|
||||
Md = 101, // Mendelevium
|
||||
No = 102, // Nobelium
|
||||
Lr = 103, // Lawrencium
|
||||
Th = 90, ///< Thorium
|
||||
Pa = 91, ///< Protactinium
|
||||
U = 92, ///< Uranium
|
||||
Np = 93, ///< Neptunium
|
||||
Pu = 94, ///< Plutonium
|
||||
Am = 95, ///< Americium
|
||||
Cm = 96, ///< Curium
|
||||
Bk = 97, ///< Berkelium
|
||||
Cf = 98, ///< Californium
|
||||
Es = 99, ///< Einsteinium
|
||||
Fm = 100, ///< Fermium
|
||||
Md = 101, ///< Mendelevium
|
||||
No = 102, ///< Nobelium
|
||||
Lr = 103, ///< Lawrencium
|
||||
|
||||
D = 119, // Deuterium
|
||||
D = 119, ///< Deuterium
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// atom_type_info
|
||||
|
||||
/// An enum used to select the desired radius for an atom.
|
||||
/// All values are collected from the wikipedia pages on atom radii
|
||||
|
||||
enum class radius_type
|
||||
{
|
||||
calculated,
|
||||
empirical,
|
||||
calculated, ///< Calculated radius from theoretical models
|
||||
empirical, ///< Empirically measured covalent radii
|
||||
|
||||
/// @deprecated It is a bit unclear where these values came from. So, better not use them
|
||||
covalent_empirical,
|
||||
|
||||
single_bond,
|
||||
double_bond,
|
||||
triple_bond,
|
||||
single_bond, ///< Bond length for a single covalent bond calculated using statistically analysis
|
||||
double_bond, ///< Bond length for a double covalent bond calculated using statistically analysis
|
||||
triple_bond, ///< Bond length for a triple covalent bond calculated using statistically analysis
|
||||
|
||||
van_der_waals,
|
||||
van_der_waals, ///< Radius of an imaginary hard sphere representing the distance of closest approach for another atom
|
||||
|
||||
type_count
|
||||
type_count ///< Number of radii
|
||||
};
|
||||
|
||||
/// @brief The number of radii per element which can be requested from atom_type_info
|
||||
constexpr size_t kRadiusTypeCount = static_cast<size_t>(radius_type::type_count);
|
||||
|
||||
/// An enum used to select either the effective or the crystal radius of an ion.
|
||||
/// See explanation on Wikipedia: https://en.wikipedia.org/wiki/Ionic_radius
|
||||
|
||||
enum class ionic_radius_type
|
||||
{
|
||||
effective, crystal
|
||||
effective, ///< Based on distance between ions in a crystal structure as determined by X-ray crystallography
|
||||
crystal ///< Calculated ion radius based on a function of ionic charge and spin
|
||||
};
|
||||
|
||||
/// Requests for an unknown radius value return kNA
|
||||
constexpr float kNA = std::numeric_limits<float>::quiet_NaN();
|
||||
|
||||
/// A struct holding the known information for all elements defined in atom_type
|
||||
|
||||
struct atom_type_info
|
||||
{
|
||||
/// The type as an atom_type
|
||||
atom_type type;
|
||||
|
||||
/// The official name for this element
|
||||
std::string name;
|
||||
|
||||
/// The official symbol for this element
|
||||
std::string symbol;
|
||||
|
||||
/// The weight of this element
|
||||
float weight;
|
||||
|
||||
/// A flag indicating whether the element is a metal
|
||||
bool metal;
|
||||
|
||||
/// Array containing all known radii for this element. A value of kNA is
|
||||
/// stored for unknown values
|
||||
float radii[kRadiusTypeCount];
|
||||
};
|
||||
|
||||
/// Array of atom_type_info struct for each of the defined elements in atom_type
|
||||
|
||||
extern CIFPP_EXPORT const atom_type_info kKnownAtoms[];
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// AtomTypeTraits
|
||||
|
||||
/// A traits class to access information for known elements
|
||||
|
||||
class atom_type_traits
|
||||
{
|
||||
public:
|
||||
/// Constructor taking an atom_type \a a
|
||||
atom_type_traits(atom_type a);
|
||||
|
||||
/// Constructor based on the element as a string in \a symbol
|
||||
atom_type_traits(const std::string &symbol);
|
||||
|
||||
atom_type type() const { return m_info->type; }
|
||||
std::string name() const { return m_info->name; }
|
||||
std::string symbol() const { return m_info->symbol; }
|
||||
float weight() const { return m_info->weight; }
|
||||
atom_type type() const { return m_info->type; } ///< Returns the atom_type
|
||||
std::string name() const { return m_info->name; } ///< Returns the name of the element
|
||||
std::string symbol() const { return m_info->symbol; } ///< Returns the symbol of the element
|
||||
float weight() const { return m_info->weight; } ///< Returns the average weight of the element
|
||||
|
||||
bool is_metal() const { return m_info->metal; }
|
||||
bool is_metal() const { return m_info->metal; } ///< Returns true if the element is a metal
|
||||
|
||||
/// Return true if the symbol in \a symbol actually exists in the list of known elements in atom_type
|
||||
static bool is_element(const std::string &symbol);
|
||||
|
||||
/// Return true if the symbol in \a symbol exists and is a metal
|
||||
static bool is_metal(const std::string &symbol);
|
||||
|
||||
/// @brief Return the radius for the element, use \a type to select which radius to return
|
||||
/// @param type The selector for which radius to return
|
||||
/// @return The requested radius or kNA if not known (or applicable)
|
||||
float radius(radius_type type = radius_type::single_bond) const
|
||||
{
|
||||
if (type >= radius_type::type_count)
|
||||
@@ -247,35 +294,46 @@ class atom_type_traits
|
||||
/// \brief Return the radius for a charged version of this atom, returns the effective radius by default
|
||||
///
|
||||
/// \param charge The charge of the ion
|
||||
/// \param type The requested ion radius type
|
||||
/// \return The radius of the ion
|
||||
float ionic_radius(int charge, ionic_radius_type type = ionic_radius_type::effective) const
|
||||
{
|
||||
return type == ionic_radius_type::effective ? effective_ionic_radius(charge) : crystal_ionic_radius(charge);
|
||||
}
|
||||
|
||||
// data type encapsulating the Waasmaier & Kirfel scattering factors
|
||||
// in a simplified form (only a and b).
|
||||
// Added the electrion scattering factors as well
|
||||
/**
|
||||
* @brief data type encapsulating the scattering factors
|
||||
* in a simplified form (only a and b).
|
||||
*/
|
||||
struct SFData
|
||||
{
|
||||
/** @cond */
|
||||
double a[6], b[6];
|
||||
/** @endcond */
|
||||
};
|
||||
|
||||
// to get the Cval and Siva values, use this constant as charge:
|
||||
enum
|
||||
{
|
||||
kWKSFVal = -99
|
||||
};
|
||||
/// @brief to get the Cval and Siva scattering factor values, use this constant as charge:
|
||||
static constexpr int kWKSFVal = -99;
|
||||
|
||||
/// @brief Return the Waasmaier & Kirfel scattering factor values for the element
|
||||
///
|
||||
/// The coefficients from Waasmaier & Kirfel (1995), Acta Cryst. A51, 416-431.
|
||||
///
|
||||
/// @param charge The charge for which the structure values should be returned, use kWSKFVal to return the *Cval* and *Siva* values
|
||||
/// @return The scattering factors as a SFData struct
|
||||
const SFData &wksf(int charge = 0) const;
|
||||
|
||||
/// @brief Return the electron scattering factor values for the element
|
||||
///
|
||||
/// @return The scattering factors as a SFData struct
|
||||
const SFData &elsf() const;
|
||||
|
||||
// Clipper doesn't like atoms with charges that do not have a scattering factor. And
|
||||
// rightly so, but we need to know in advance if this is the case
|
||||
/// Clipper doesn't like atoms with charges that do not have a scattering factor. And
|
||||
/// rightly so, but we need to know in advance if this is the case
|
||||
bool has_sf(int charge) const;
|
||||
|
||||
private:
|
||||
const struct atom_type_info *m_info;
|
||||
};
|
||||
|
||||
} // namespace pdbx
|
||||
} // namespace cif
|
||||
|
||||
@@ -32,31 +32,52 @@
|
||||
#include "cif++/iterator.hpp"
|
||||
#include "cif++/row.hpp"
|
||||
#include "cif++/validate.hpp"
|
||||
#include "cif++/text.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
// TODO: implement all of:
|
||||
// https://en.cppreference.com/w/cpp/named_req/Container
|
||||
// https://en.cppreference.com/w/cpp/named_req/SequenceContainer
|
||||
// and more?
|
||||
/** \file category.hpp
|
||||
* Documentation for the cif::category class
|
||||
*
|
||||
* The category class should meet the requirements of Container and
|
||||
* SequenceContainer.
|
||||
*
|
||||
* TODO: implement all of:
|
||||
* https://en.cppreference.com/w/cpp/named_req/Container
|
||||
* https://en.cppreference.com/w/cpp/named_req/SequenceContainer
|
||||
* and more?
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// special exception
|
||||
// special exceptions
|
||||
|
||||
/// @brief A duplicate_key_error is thrown when an attempt is made
|
||||
/// to insert a row with values that would introduce a duplicate key
|
||||
/// in the index. Of course, this can only happen if a @ref category_validator
|
||||
/// has been defined for this category.
|
||||
class duplicate_key_error : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new duplicate key error object
|
||||
*/
|
||||
duplicate_key_error(const std::string &msg)
|
||||
: std::runtime_error(msg)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
/// @brief A multiple_results_error is throw when you request a single
|
||||
/// row using a query but the query contains more than exactly one row.
|
||||
class multiple_results_error : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new multiple results error object
|
||||
*/
|
||||
multiple_results_error()
|
||||
: std::runtime_error("query should have returned exactly one row")
|
||||
{
|
||||
@@ -66,14 +87,27 @@ class multiple_results_error : public std::runtime_error
|
||||
// --------------------------------------------------------------------
|
||||
// These should be moved elsewhere, one day.
|
||||
|
||||
template<typename _Tp> inline constexpr bool is_optional_v = false;
|
||||
template<typename _Tp> inline constexpr bool is_optional_v<std::optional<_Tp>> = true;
|
||||
/// \cond
|
||||
template <typename _Tp>
|
||||
inline constexpr bool is_optional_v = false;
|
||||
template <typename _Tp>
|
||||
inline constexpr bool is_optional_v<std::optional<_Tp>> = true;
|
||||
/// \endcond
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// The class category is a sequence container for rows of data values.
|
||||
/// You could think of it as a std::vector<cif::row_handle> like class.
|
||||
///
|
||||
/// A @ref category_validator can be assigned to an object of category
|
||||
/// after which this class can validate contained data and use an
|
||||
/// index to keep key values unique.
|
||||
|
||||
class category
|
||||
{
|
||||
public:
|
||||
/// \cond
|
||||
|
||||
friend class row_handle;
|
||||
|
||||
template <typename, typename...>
|
||||
@@ -85,38 +119,68 @@ class category
|
||||
using iterator = iterator_impl<category>;
|
||||
using const_iterator = iterator_impl<const category>;
|
||||
|
||||
category() = default;
|
||||
/// \endcond
|
||||
|
||||
category(std::string_view name);
|
||||
|
||||
category(const category &rhs);
|
||||
|
||||
category(category &&rhs);
|
||||
|
||||
category &operator=(const category &rhs);
|
||||
|
||||
category &operator=(category &&rhs);
|
||||
category() = default; ///< Default constructor
|
||||
category(std::string_view name); ///< Constructor taking a \a name
|
||||
category(const category &rhs); ///< Copy constructor
|
||||
category(category &&rhs); ///< Move constructor
|
||||
category &operator=(const category &rhs); ///< Copy assignement operator
|
||||
category &operator=(category &&rhs); ///< Move assignement operator
|
||||
|
||||
/// @brief Destructor
|
||||
/// @note Please note that the destructor is not virtual. It is assumed that
|
||||
/// you will not derive from this class.
|
||||
~category();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
const std::string &name() const { return m_name; }
|
||||
|
||||
iset key_fields() const;
|
||||
|
||||
std::set<uint16_t> key_field_indices() const;
|
||||
const std::string &name() const { return m_name; } ///< Returns the name of the category
|
||||
iset key_fields() const; ///< Returns the cif::iset of key field names. Retrieved from the @ref category_validator for this category
|
||||
std::set<uint16_t> key_field_indices() const; ///< Returns a set of indices for the key fields.
|
||||
|
||||
/// @brief Set the validator for this category to @a v
|
||||
/// @param v The category_validator to assign. A nullptr value is allowed.
|
||||
/// @param db The enclosing @ref datablock
|
||||
void set_validator(const validator *v, datablock &db);
|
||||
|
||||
/// @brief Update the links in this category
|
||||
/// @param db The enclosing @ref datablock
|
||||
void update_links(datablock &db);
|
||||
|
||||
/// @brief Return the global @ref validator for the data
|
||||
/// @return The @ref validator or nullptr if not assigned
|
||||
const validator *get_validator() const { return m_validator; }
|
||||
|
||||
/// @brief Return the category validator for this category
|
||||
/// @return The @ref category_validator or nullptr if not assigned
|
||||
const category_validator *get_cat_validator() const { return m_cat_validator; }
|
||||
|
||||
/// @brief Validate the data stored using the assigned @ref category_validator
|
||||
/// @return Returns true is all validations pass
|
||||
bool is_valid() const;
|
||||
|
||||
/// @brief Validate links, that means, values in this category should have an
|
||||
/// accompanying value in parent categories.
|
||||
///
|
||||
/// @note
|
||||
/// The code makes one exception when validating missing links and that's between
|
||||
/// *atom_site* and a parent *pdbx_poly_seq_scheme* or *entity_poly_seq*.
|
||||
/// This particular case should be skipped because it is wrong:
|
||||
/// there are atoms that are not part of a polymer, and thus will have no
|
||||
/// parent in those categories.
|
||||
///
|
||||
/// @return Returns true is all validations pass
|
||||
bool validate_links() const;
|
||||
|
||||
/// @brief Equality operator, returns true if @a rhs is equal to this
|
||||
/// @param rhs The object to compare with
|
||||
/// @return True if the data contained is equal
|
||||
bool operator==(const category &rhs) const;
|
||||
|
||||
/// @brief Unequality operator, returns true if @a rhs is not equal to this
|
||||
/// @param rhs The object to compare with
|
||||
/// @return True if the data contained is not equal
|
||||
bool operator!=(const category &rhs) const
|
||||
{
|
||||
return not operator==(rhs);
|
||||
@@ -124,61 +188,87 @@ class category
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// @brief Return a reference to the first row in this category.
|
||||
/// @return Reference to the first row in this category. The result is undefined if
|
||||
/// the category is empty.
|
||||
reference front()
|
||||
{
|
||||
return { *this, *m_head };
|
||||
}
|
||||
|
||||
/// @brief Return a const reference to the first row in this category.
|
||||
/// @return const reference to the first row in this category. The result is undefined if
|
||||
/// the category is empty.
|
||||
const_reference front() const
|
||||
{
|
||||
return { const_cast<category &>(*this), const_cast<row &>(*m_head) };
|
||||
}
|
||||
|
||||
/// @brief Return a reference to the last row in this category.
|
||||
/// @return Reference to the last row in this category. The result is undefined if
|
||||
/// the category is empty.
|
||||
reference back()
|
||||
{
|
||||
return { *this, *m_tail };
|
||||
}
|
||||
|
||||
/// @brief Return a const reference to the last row in this category.
|
||||
/// @return const reference to the last row in this category. The result is undefined if
|
||||
/// the category is empty.
|
||||
const_reference back() const
|
||||
{
|
||||
return { const_cast<category &>(*this), const_cast<row &>(*m_tail) };
|
||||
}
|
||||
|
||||
/// Return an iterator to the first row
|
||||
iterator begin()
|
||||
{
|
||||
return { *this, m_head };
|
||||
}
|
||||
|
||||
/// Return an iterator pointing past the last row
|
||||
iterator end()
|
||||
{
|
||||
return { *this, nullptr };
|
||||
}
|
||||
|
||||
/// Return a const iterator to the first row
|
||||
const_iterator begin() const
|
||||
{
|
||||
return { *this, m_head };
|
||||
}
|
||||
|
||||
/// Return a const iterator pointing past the last row
|
||||
const_iterator end() const
|
||||
{
|
||||
return { *this, nullptr };
|
||||
}
|
||||
|
||||
/// Return a const iterator to the first row
|
||||
const_iterator cbegin() const
|
||||
{
|
||||
return { *this, m_head };
|
||||
}
|
||||
|
||||
/// Return an iterator pointing past the last row
|
||||
const_iterator cend() const
|
||||
{
|
||||
return { *this, nullptr };
|
||||
}
|
||||
|
||||
/// Return a count of the rows in this container
|
||||
size_t size() const
|
||||
{
|
||||
return std::distance(cbegin(), cend());
|
||||
}
|
||||
|
||||
/// Return the theoretical maximum number or rows that can be stored
|
||||
size_t max_size() const
|
||||
{
|
||||
return std::numeric_limits<size_t>::max(); // this is a bit optimistic, I guess
|
||||
}
|
||||
|
||||
/// Return true if the category is empty
|
||||
bool empty() const
|
||||
{
|
||||
return m_head == nullptr;
|
||||
@@ -195,6 +285,9 @@ class category
|
||||
/// @return The row found in the index, or an undefined row_handle
|
||||
row_handle operator[](const key_type &key);
|
||||
|
||||
/// @brief Return a const row_handle for the row specified by \a key
|
||||
/// @param key The value for the key, fields specified in the dictionary should have a value
|
||||
/// @return The row found in the index, or an undefined row_handle
|
||||
const row_handle operator[](const key_type &key) const
|
||||
{
|
||||
return const_cast<category *>(this)->operator[](key);
|
||||
@@ -202,6 +295,17 @@ class category
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// @brief Return a special const iterator for all rows in this category.
|
||||
/// This iterator can be used in a structured binding context. E.g.:
|
||||
///
|
||||
/// @code{.cpp}
|
||||
/// for (const auto &[name, value] : cat.rows<std::string,int>("item_name", "item_value"))
|
||||
/// std::cout << name << ": " << value << '\n';
|
||||
/// @endcode
|
||||
///
|
||||
/// @tparam Ts The types for the columns requested
|
||||
/// @param names The names for the columns requested
|
||||
|
||||
template <typename... Ts, typename... Ns>
|
||||
iterator_proxy<const category, Ts...> rows(Ns... names) const
|
||||
{
|
||||
@@ -209,6 +313,22 @@ class category
|
||||
return iterator_proxy<const category, Ts...>(*this, begin(), { names... });
|
||||
}
|
||||
|
||||
/// @brief Return a special iterator for all rows in this category.
|
||||
/// This iterator can be used in a structured binding context. E.g.:
|
||||
///
|
||||
/// @code{.cpp}
|
||||
/// for (const auto &[name, value] : cat.rows<std::string,int>("item_name", "item_value"))
|
||||
/// std::cout << name << ": " << value << '\n';
|
||||
///
|
||||
/// // or in case we only need one column:
|
||||
///
|
||||
/// for (int id : cat.rows<int>("id"))
|
||||
/// std::cout << id << '\n';
|
||||
/// @endcode
|
||||
///
|
||||
/// @tparam Ts The types for the columns requested
|
||||
/// @param names The names for the columns requested
|
||||
|
||||
template <typename... Ts, typename... Ns>
|
||||
iterator_proxy<category, Ts...> rows(Ns... names)
|
||||
{
|
||||
@@ -218,26 +338,72 @@ class category
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// @brief Return a special iterator to loop over all rows that conform to @a cond
|
||||
///
|
||||
/// @code{.cpp}
|
||||
/// for (row_handle rh : cat.find(cif::key("first_name") == "John" and cif::key("last_name") == "Doe"))
|
||||
/// .. // do something with rh
|
||||
/// @endcode
|
||||
///
|
||||
/// @param cond The condition for the query
|
||||
/// @return A special iterator that loops over all elements that match. The iterator can be dereferenced
|
||||
/// to a @ref row_handle
|
||||
|
||||
conditional_iterator_proxy<category> find(condition &&cond)
|
||||
{
|
||||
return find(begin(), std::move(cond));
|
||||
}
|
||||
|
||||
/// @brief Return a special iterator to loop over all rows that conform to @a cond
|
||||
/// starting at @a pos
|
||||
///
|
||||
/// @param pos Where to start searching
|
||||
/// @param cond The condition for the query
|
||||
/// @return A special iterator that loops over all elements that match. The iterator can be dereferenced
|
||||
/// to a @ref row_handle
|
||||
|
||||
conditional_iterator_proxy<category> find(iterator pos, condition &&cond)
|
||||
{
|
||||
return { *this, pos, std::move(cond) };
|
||||
}
|
||||
|
||||
/// @brief Return a special const iterator to loop over all rows that conform to @a cond
|
||||
///
|
||||
/// @param cond The condition for the query
|
||||
/// @return A special iterator that loops over all elements that match. The iterator can be dereferenced
|
||||
/// to a const @ref row_handle
|
||||
|
||||
conditional_iterator_proxy<const category> find(condition &&cond) const
|
||||
{
|
||||
return find(cbegin(), std::move(cond));
|
||||
}
|
||||
|
||||
/// @brief Return a special const iterator to loop over all rows that conform to @a cond
|
||||
/// starting at @a pos
|
||||
///
|
||||
/// @param pos Where to start searching
|
||||
/// @param cond The condition for the query
|
||||
/// @return A special iterator that loops over all elements that match. The iterator can be dereferenced
|
||||
/// to a const @ref row_handle
|
||||
|
||||
conditional_iterator_proxy<const category> find(const_iterator pos, condition &&cond) const
|
||||
{
|
||||
return conditional_iterator_proxy<const category>{ *this, pos, std::move(cond) };
|
||||
}
|
||||
|
||||
/// @brief Return a special iterator to loop over all rows that conform to @a cond. The resulting
|
||||
/// iterator can be used in a structured binding context.
|
||||
///
|
||||
/// @code{.cpp}
|
||||
/// for (const auto &[name, value] : cat.find<std::string,int>(cif::key("item_value") > 10, "item_name", "item_value"))
|
||||
/// std::cout << name << ": " << value << '\n';
|
||||
/// @endcode
|
||||
///
|
||||
/// @param cond The condition for the query
|
||||
/// @tparam Ts The types for the columns requested
|
||||
/// @param names The names for the columns requested
|
||||
/// @return A special iterator that loops over all elements that match.
|
||||
|
||||
template <typename... Ts, typename... Ns>
|
||||
conditional_iterator_proxy<category, Ts...> find(condition &&cond, Ns... names)
|
||||
{
|
||||
@@ -245,6 +411,14 @@ class category
|
||||
return find<Ts...>(cbegin(), std::move(cond), std::forward<Ns>(names)...);
|
||||
}
|
||||
|
||||
/// @brief Return a special const iterator to loop over all rows that conform to @a cond. The resulting
|
||||
/// iterator can be used in a structured binding context.
|
||||
///
|
||||
/// @param cond The condition for the query
|
||||
/// @tparam Ts The types for the columns requested
|
||||
/// @param names The names for the columns requested
|
||||
/// @return A special iterator that loops over all elements that match.
|
||||
|
||||
template <typename... Ts, typename... Ns>
|
||||
conditional_iterator_proxy<const category, Ts...> find(condition &&cond, Ns... names) const
|
||||
{
|
||||
@@ -252,6 +426,15 @@ class category
|
||||
return find<Ts...>(cbegin(), std::move(cond), std::forward<Ns>(names)...);
|
||||
}
|
||||
|
||||
/// @brief Return a special iterator to loop over all rows that conform to @a cond starting at @a pos.
|
||||
/// The resulting iterator can be used in a structured binding context.
|
||||
///
|
||||
/// @param pos Iterator pointing to the location where to start
|
||||
/// @param cond The condition for the query
|
||||
/// @tparam Ts The types for the columns requested
|
||||
/// @param names The names for the columns requested
|
||||
/// @return A special iterator that loops over all elements that match.
|
||||
|
||||
template <typename... Ts, typename... Ns>
|
||||
conditional_iterator_proxy<category, Ts...> find(const_iterator pos, condition &&cond, Ns... names)
|
||||
{
|
||||
@@ -259,6 +442,15 @@ class category
|
||||
return { *this, pos, std::move(cond), std::forward<Ns>(names)... };
|
||||
}
|
||||
|
||||
/// @brief Return a special const iterator to loop over all rows that conform to @a cond starting at @a pos.
|
||||
/// The resulting iterator can be used in a structured binding context.
|
||||
///
|
||||
/// @param pos Iterator pointing to the location where to start
|
||||
/// @param cond The condition for the query
|
||||
/// @tparam Ts The types for the columns requested
|
||||
/// @param names The names for the columns requested
|
||||
/// @return A special iterator that loops over all elements that match.
|
||||
|
||||
template <typename... Ts, typename... Ns>
|
||||
conditional_iterator_proxy<const category, Ts...> find(const_iterator pos, condition &&cond, Ns... names) const
|
||||
{
|
||||
@@ -269,11 +461,20 @@ class category
|
||||
// --------------------------------------------------------------------
|
||||
// if you only expect a single row
|
||||
|
||||
/// @brief Return the row handle for the row that matches @a cond Throws @a multiple_results_error if
|
||||
/// there are is not exactly one row matching @a cond
|
||||
/// @param cond The condition to search for
|
||||
/// @return Row handle to the row found
|
||||
row_handle find1(condition &&cond)
|
||||
{
|
||||
return find1(begin(), std::move(cond));
|
||||
}
|
||||
|
||||
/// @brief Return the row handle for the row that matches @a cond starting at @a pos
|
||||
/// Throws @a multiple_results_error if there are is not exactly one row matching @a cond
|
||||
/// @param pos The position to start the search
|
||||
/// @param cond The condition to search for
|
||||
/// @return Row handle to the row found
|
||||
row_handle find1(iterator pos, condition &&cond)
|
||||
{
|
||||
auto h = find(pos, std::move(cond));
|
||||
@@ -284,11 +485,20 @@ class category
|
||||
return *h.begin();
|
||||
}
|
||||
|
||||
/// @brief Return the const row handle for the row that matches @a cond Throws @a multiple_results_error if
|
||||
/// there are is not exactly one row matching @a cond
|
||||
/// @param cond The condition to search for
|
||||
/// @return Row handle to the row found
|
||||
const row_handle find1(condition &&cond) const
|
||||
{
|
||||
return find1(cbegin(), std::move(cond));
|
||||
}
|
||||
|
||||
/// @brief Return const the row handle for the row that matches @a cond starting at @a pos
|
||||
/// Throws @a multiple_results_error if there are is not exactly one row matching @a cond
|
||||
/// @param pos The position to start the search
|
||||
/// @param cond The condition to search for
|
||||
/// @return Row handle to the row found
|
||||
const row_handle find1(const_iterator pos, condition &&cond) const
|
||||
{
|
||||
auto h = find(pos, std::move(cond));
|
||||
@@ -299,12 +509,26 @@ class category
|
||||
return *h.begin();
|
||||
}
|
||||
|
||||
/// @brief Return value for the column named @a column for the single row that
|
||||
/// matches @a cond. Throws @a multiple_results_error if there are is not exactly one row
|
||||
/// @tparam The type to use for the result
|
||||
/// @param cond The condition to search for
|
||||
/// @param column The name of the column to return the value for
|
||||
/// @return The value found
|
||||
template <typename T>
|
||||
T find1(condition &&cond, const char *column) const
|
||||
{
|
||||
return find1<T>(cbegin(), std::move(cond), column);
|
||||
}
|
||||
|
||||
/// @brief Return value for the column named @a column for the single row that
|
||||
/// matches @a cond when starting to search at @a pos.
|
||||
/// Throws @a multiple_results_error if there are is not exactly one row
|
||||
/// @tparam The type to use for the result
|
||||
/// @param pos The location to start the search
|
||||
/// @param cond The condition to search for
|
||||
/// @param column The name of the column to return the value for
|
||||
/// @return The value found
|
||||
template <typename T, std::enable_if_t<not is_optional_v<T>, int> = 0>
|
||||
T find1(const_iterator pos, condition &&cond, const char *column) const
|
||||
{
|
||||
@@ -316,6 +540,14 @@ class category
|
||||
return *h.begin();
|
||||
}
|
||||
|
||||
/// @brief Return a value of type std::optional<T> for the column named @a column for the single row that
|
||||
/// matches @a cond when starting to search at @a pos.
|
||||
/// If the row was not found, an empty value is returned.
|
||||
/// @tparam The type to use for the result
|
||||
/// @param pos The location to start the search
|
||||
/// @param cond The condition to search for
|
||||
/// @param column The name of the column to return the value for
|
||||
/// @return The value found, can be empty if no row matches the condition
|
||||
template <typename T, std::enable_if_t<is_optional_v<T>, int> = 0>
|
||||
T find1(const_iterator pos, condition &&cond, const char *column) const
|
||||
{
|
||||
@@ -330,6 +562,13 @@ class category
|
||||
return *h.begin();
|
||||
}
|
||||
|
||||
/// @brief Return a std::tuple for the values for the columns named in @a columns
|
||||
/// for the single row that matches @a cond
|
||||
/// Throws @a multiple_results_error if there are is not exactly one row
|
||||
/// @tparam The types to use for the resulting tuple
|
||||
/// @param cond The condition to search for
|
||||
/// @param columns The names of the columns to return the value for
|
||||
/// @return The values found as a single tuple of type std::tuple<Ts...>
|
||||
template <typename... Ts, typename... Cs, typename U = std::enable_if_t<sizeof...(Ts) != 1>>
|
||||
std::tuple<Ts...> find1(condition &&cond, Cs... columns) const
|
||||
{
|
||||
@@ -338,6 +577,14 @@ class category
|
||||
return find1<Ts...>(cbegin(), std::move(cond), std::forward<Cs>(columns)...);
|
||||
}
|
||||
|
||||
/// @brief Return a std::tuple for the values for the columns named in @a columns
|
||||
/// for the single row that matches @a cond when starting to search at @a pos
|
||||
/// Throws @a multiple_results_error if there are is not exactly one row
|
||||
/// @tparam The types to use for the resulting tuple
|
||||
/// @param pos The location to start the search
|
||||
/// @param cond The condition to search for
|
||||
/// @param columns The names of the columns to return the value for
|
||||
/// @return The values found as a single tuple of type std::tuple<Ts...>
|
||||
template <typename... Ts, typename... Cs, typename U = std::enable_if_t<sizeof...(Ts) != 1>>
|
||||
std::tuple<Ts...> find1(const_iterator pos, condition &&cond, Cs... columns) const
|
||||
{
|
||||
@@ -353,11 +600,18 @@ class category
|
||||
// --------------------------------------------------------------------
|
||||
// if you want only a first hit
|
||||
|
||||
/// @brief Return a row handle to the first row that matches @a cond
|
||||
/// @param cond The condition to search for
|
||||
/// @return The handle to the row that matches or an empty row_handle
|
||||
row_handle find_first(condition &&cond)
|
||||
{
|
||||
return find_first(begin(), std::move(cond));
|
||||
}
|
||||
|
||||
/// @brief Return a row handle to the first row that matches @a cond starting at @a pos
|
||||
/// @param pos The location to start searching
|
||||
/// @param cond The condition to search for
|
||||
/// @return The handle to the row that matches or an empty row_handle
|
||||
row_handle find_first(iterator pos, condition &&cond)
|
||||
{
|
||||
auto h = find(pos, std::move(cond));
|
||||
@@ -365,11 +619,18 @@ class category
|
||||
return h.empty() ? row_handle{} : *h.begin();
|
||||
}
|
||||
|
||||
/// @brief Return a const row handle to the first row that matches @a cond
|
||||
/// @param cond The condition to search for
|
||||
/// @return The const handle to the row that matches or an empty row_handle
|
||||
const row_handle find_first(condition &&cond) const
|
||||
{
|
||||
return find_first(cbegin(), std::move(cond));
|
||||
}
|
||||
|
||||
/// @brief Return a const row handle to the first row that matches @a cond starting at @a pos
|
||||
/// @param pos The location to start searching
|
||||
/// @param cond The condition to search for
|
||||
/// @return The const handle to the row that matches or an empty row_handle
|
||||
const row_handle find_first(const_iterator pos, condition &&cond) const
|
||||
{
|
||||
auto h = find(pos, std::move(cond));
|
||||
@@ -377,12 +638,24 @@ class category
|
||||
return h.empty() ? row_handle{} : *h.begin();
|
||||
}
|
||||
|
||||
/// @brief Return the value for column @a column for the first row that matches condition @a cond
|
||||
/// @tparam The type of the value to return
|
||||
/// @param cond The condition to search for
|
||||
/// @param column The column for which the value should be returned
|
||||
/// @return The value found or a default constructed value if not found
|
||||
template <typename T>
|
||||
T find_first(condition &&cond, const char *column) const
|
||||
{
|
||||
return find_first<T>(cbegin(), std::move(cond), column);
|
||||
}
|
||||
|
||||
/// @brief Return the value for column @a column for the first row that matches condition @a cond
|
||||
/// when starting the search at @a pos
|
||||
/// @tparam The type of the value to return
|
||||
/// @param pos The location to start searching
|
||||
/// @param cond The condition to search for
|
||||
/// @param column The column for which the value should be returned
|
||||
/// @return The value found or a default constructed value if not found
|
||||
template <typename T>
|
||||
T find_first(const_iterator pos, condition &&cond, const char *column) const
|
||||
{
|
||||
@@ -391,6 +664,11 @@ class category
|
||||
return h.empty() ? T{} : *h.begin();
|
||||
}
|
||||
|
||||
/// @brief Return a tuple containing the values for the columns @a columns for the first row that matches condition @a cond
|
||||
/// @tparam The types of the values to return
|
||||
/// @param cond The condition to search for
|
||||
/// @param columns The columns for which the values should be returned
|
||||
/// @return The values found or default constructed values if not found
|
||||
template <typename... Ts, typename... Cs, typename U = std::enable_if_t<sizeof...(Ts) != 1>>
|
||||
std::tuple<Ts...> find_first(condition &&cond, Cs... columns) const
|
||||
{
|
||||
@@ -399,6 +677,13 @@ class category
|
||||
return find_first<Ts...>(cbegin(), std::move(cond), std::forward<Cs>(columns)...);
|
||||
}
|
||||
|
||||
/// @brief Return a tuple containing the values for the columns @a columns for the first row that matches condition @a cond
|
||||
/// when starting the search at @a pos
|
||||
/// @tparam The types of the values to return
|
||||
/// @param pos The location to start searching
|
||||
/// @param cond The condition to search for
|
||||
/// @param columns The columns for which the values should be returned
|
||||
/// @return The values found or default constructed values if not found
|
||||
template <typename... Ts, typename... Cs, typename U = std::enable_if_t<sizeof...(Ts) != 1>>
|
||||
std::tuple<Ts...> find_first(const_iterator pos, condition &&cond, Cs... columns) const
|
||||
{
|
||||
@@ -410,6 +695,11 @@ class category
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// @brief Return the maximum value for column @a column for all rows that match condition @a cond
|
||||
/// @tparam The type of the value to return
|
||||
/// @param column The column to use for the value
|
||||
/// @param cond The condition to search for
|
||||
/// @return The value found or the minimal value for the type
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
|
||||
T find_max(const char *column, condition &&cond) const
|
||||
{
|
||||
@@ -424,12 +714,21 @@ class category
|
||||
return result;
|
||||
}
|
||||
|
||||
/// @brief Return the maximum value for column @a column for all rows
|
||||
/// @tparam The type of the value to return
|
||||
/// @param column The column to use for the value
|
||||
/// @return The value found or the minimal value for the type
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
|
||||
T find_max(const char *column) const
|
||||
{
|
||||
return find_max<T>(column, all());
|
||||
}
|
||||
|
||||
/// @brief Return the minimum value for column @a column for all rows that match condition @a cond
|
||||
/// @tparam The type of the value to return
|
||||
/// @param column The column to use for the value
|
||||
/// @param cond The condition to search for
|
||||
/// @return The value found or the maximum value for the type
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
|
||||
T find_min(const char *column, condition &&cond) const
|
||||
{
|
||||
@@ -444,12 +743,19 @@ class category
|
||||
return result;
|
||||
}
|
||||
|
||||
/// @brief Return the maximum value for column @a column for all rows
|
||||
/// @tparam The type of the value to return
|
||||
/// @param column The column to use for the value
|
||||
/// @return The value found or the maximum value for the type
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
|
||||
T find_min(const char *column) const
|
||||
{
|
||||
return find_min<T>(column, all());
|
||||
}
|
||||
|
||||
/// @brief Return whether a row exists that matches condition @a cond
|
||||
/// @param cond The condition to match
|
||||
/// @return True if a row exists
|
||||
bool exists(condition &&cond) const
|
||||
{
|
||||
bool result = false;
|
||||
@@ -478,6 +784,9 @@ class category
|
||||
return result;
|
||||
}
|
||||
|
||||
/// @brief Return the total number of rows that match condition @a cond
|
||||
/// @param cond The condition to match
|
||||
/// @return The count
|
||||
size_t count(condition &&cond) const
|
||||
{
|
||||
size_t result = 0;
|
||||
@@ -505,11 +814,24 @@ class category
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// Using the relations defined in the validator, return whether the row
|
||||
/// in @a r has any children in other categories
|
||||
bool has_children(row_handle r) const;
|
||||
|
||||
/// Using the relations defined in the validator, return whether the row
|
||||
/// in @a r has any parents in other categories
|
||||
bool has_parents(row_handle r) const;
|
||||
|
||||
/// Using the relations defined in the validator, return the row handles
|
||||
/// for all rows in @a childCat that are linked to row @a r
|
||||
std::vector<row_handle> get_children(row_handle r, const category &childCat) const;
|
||||
|
||||
/// Using the relations defined in the validator, return the row handles
|
||||
/// for all rows in @a parentCat that are linked to row @a r
|
||||
std::vector<row_handle> get_parents(row_handle r, const category &parentCat) const;
|
||||
|
||||
/// Using the relations defined in the validator, return the row handles
|
||||
/// for all rows in @a cat that are in any way linked to row @a r
|
||||
std::vector<row_handle> get_linked(row_handle r, const category &cat) const;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -524,20 +846,40 @@ class category
|
||||
// insert_impl(pos, std::move(row));
|
||||
// }
|
||||
|
||||
/// Erase the row pointed to by @a pos and return the iterator to the
|
||||
/// row following pos.
|
||||
iterator erase(iterator pos);
|
||||
|
||||
/// Erase row @a rh
|
||||
void erase(row_handle rh)
|
||||
{
|
||||
erase(iterator(*this, rh.m_row));
|
||||
}
|
||||
|
||||
/// @brief Erase all rows that match condition @a cond
|
||||
/// @param cond The condition
|
||||
/// @return The number of rows that have been erased
|
||||
size_t erase(condition &&cond);
|
||||
|
||||
/// @brief Erase all rows that match condition @a cond calling
|
||||
/// the visitor function @a visit for each before actually erasing it.
|
||||
/// @param cond The condition
|
||||
/// @param visit The visitor function
|
||||
/// @return The number of rows that have been erased
|
||||
size_t erase(condition &&cond, std::function<void(row_handle)> &&visit);
|
||||
|
||||
/// @brief Emplace the values in @a ri in a new row
|
||||
/// @param ri An object containing the values to insert
|
||||
/// @return iterator to the newly created row
|
||||
iterator emplace(row_initializer &&ri)
|
||||
{
|
||||
return this->emplace(ri.begin(), ri.end());
|
||||
}
|
||||
|
||||
/// @brief Create a new row and emplace the values in the range @a b to @a e in it
|
||||
/// @param b Iterator to the beginning of the range of @ref item_value
|
||||
/// @param e Iterator to the end of the range of @ref item_value
|
||||
/// @return iterator to the newly created row
|
||||
template <typename ItemIter>
|
||||
iterator emplace(ItemIter b, ItemIter e)
|
||||
{
|
||||
@@ -561,6 +903,7 @@ class category
|
||||
return insert_impl(cend(), r);
|
||||
}
|
||||
|
||||
/// @brief Completely erase all rows contained in this category
|
||||
void clear();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -568,6 +911,11 @@ class category
|
||||
/// based on a sequence number. This function will be called until the
|
||||
/// result is unique in the context of this category
|
||||
std::string get_unique_id(std::function<std::string(int)> generator = cif::cif_id_for_number);
|
||||
|
||||
|
||||
/// @brief Generate a new, unique ID based on a string prefix followed by a number
|
||||
/// @param prefix The string prefix
|
||||
/// @return a new unique ID
|
||||
std::string get_unique_id(const std::string &prefix)
|
||||
{
|
||||
return get_unique_id([prefix](int nr)
|
||||
@@ -576,7 +924,7 @@ class category
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Rename a single column in the rows that match \a cond to value \a value
|
||||
/// \brief Update a single column named @a tag in the rows that match \a cond to value \a value
|
||||
/// making sure the linked categories are updated according to the link.
|
||||
/// That means, child categories are updated if the links are absolute
|
||||
/// and unique. If they are not, the child category rows are split.
|
||||
@@ -589,6 +937,11 @@ class category
|
||||
update_value(rows, tag, value);
|
||||
}
|
||||
|
||||
/// \brief Update a single column named @a tag in @a rows to value \a value
|
||||
/// making sure the linked categories are updated according to the link.
|
||||
/// That means, child categories are updated if the links are absolute
|
||||
/// and unique. If they are not, the child category rows are split.
|
||||
|
||||
void update_value(const std::vector<row_handle> &rows, std::string_view tag, std::string_view value);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -608,12 +961,15 @@ class category
|
||||
{
|
||||
auto iv = m_cat_validator->get_validator_for_item(column_name);
|
||||
if (iv == nullptr)
|
||||
std::cerr << "Invalid name used '" << column_name << "' is not a known column in " + m_name << std::endl;
|
||||
std::cerr << "Invalid name used '" << column_name << "' is not a known column in " + m_name << '\n';
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// @brief Return the name for column with index @a ix
|
||||
/// @param ix The index number
|
||||
/// @return The name of the column
|
||||
std::string_view get_column_name(uint16_t ix) const
|
||||
{
|
||||
if (ix >= m_columns.size())
|
||||
@@ -622,6 +978,9 @@ class category
|
||||
return m_columns[ix].m_name;
|
||||
}
|
||||
|
||||
/// @brief Make sure a column with name @a column_name is known and return its index number
|
||||
/// @param column_name The name of the column
|
||||
/// @return The index number of the column
|
||||
uint16_t add_column(std::string_view column_name)
|
||||
{
|
||||
using namespace std::literals;
|
||||
@@ -645,29 +1004,55 @@ class category
|
||||
return result;
|
||||
}
|
||||
|
||||
/// @brief Return whether a column with name @a name exists in this category
|
||||
/// @param name The name of the column
|
||||
/// @return True if the column exists
|
||||
bool has_column(std::string_view name) const
|
||||
{
|
||||
return get_column_ix(name) < m_columns.size();
|
||||
}
|
||||
|
||||
/// @brief Return the cif::iset of columns in this category
|
||||
iset get_columns() const;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// @brief Sort the rows using comparator function @a f
|
||||
/// @param f The comparator function taking two row_handles and returning
|
||||
/// an int indicating whether the first is smaller, equal or larger than
|
||||
/// the second. ( respectively a value <0, 0, or >0 )
|
||||
void sort(std::function<int(row_handle, row_handle)> f);
|
||||
|
||||
/// @brief Reorder the rows in the category using the index defined by
|
||||
/// the @ref category_validator
|
||||
void reorder_by_index();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// This function returns effectively the list of fully qualified column
|
||||
/// names, that is category_name + '.' + column_name for each column
|
||||
std::vector<std::string> get_tag_order() const;
|
||||
|
||||
/// Write the contents of the category to the std::ostream @a os
|
||||
void write(std::ostream &os) const;
|
||||
|
||||
/// @brief Write the contents of the category to the std::ostream @a os and
|
||||
/// use @a order as the order of the columns. If @a addMissingColumns is
|
||||
/// false, columns that do not contain any value will be suppressed
|
||||
/// @param os The std::ostream to write to
|
||||
/// @param order The order in which the columns should appear
|
||||
/// @param addMissingColumns When false, empty columns are suppressed from the output
|
||||
void write(std::ostream &os, const std::vector<std::string> &order, bool addMissingColumns = true);
|
||||
|
||||
private:
|
||||
void write(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyColumns) const;
|
||||
|
||||
public:
|
||||
|
||||
/// friend function to make it possible to do:
|
||||
/// @code {.cpp}
|
||||
/// std::cout << my_category;
|
||||
/// @endcode
|
||||
friend std::ostream &operator<<(std::ostream &os, const category &cat)
|
||||
{
|
||||
cat.write(os);
|
||||
@@ -677,7 +1062,6 @@ class category
|
||||
private:
|
||||
void update_value(row *row, uint16_t column, std::string_view value, bool updateLinked, bool validate = true);
|
||||
|
||||
private:
|
||||
void erase_orphans(condition &&cond, category &parent);
|
||||
|
||||
using allocator_type = std::allocator<void>;
|
||||
|
||||
@@ -26,17 +26,32 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
/// \file This file contains the definition for the class compound, encapsulating
|
||||
/// the information found for compounds in the CCD.
|
||||
|
||||
#include "cif++/atom_type.hpp"
|
||||
#include "cif++/datablock.hpp"
|
||||
#include "cif++/exports.hpp"
|
||||
#include "cif++/point.hpp"
|
||||
#include "cif++/utilities.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
/// \file compound.hpp
|
||||
/// This file contains the definition for the class compound, encapsulating
|
||||
/// the information found for compounds in the CCD.
|
||||
///
|
||||
/// The data is loaded by default from a file called `components.cif`. This file
|
||||
/// is located using load_resource. (See documentation on cif::load_resource for more information)
|
||||
///
|
||||
/// But if the CCP4 environment is available at runtime, the compound information
|
||||
/// may also be generated from the CCP4 monomer library.
|
||||
///
|
||||
/// Note that the information in CCP4 and CCD is not equal.
|
||||
///
|
||||
|
||||
/// See also :doc:`/compound` for more information.
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
@@ -46,21 +61,47 @@ class compound;
|
||||
struct compound_atom;
|
||||
class compound_factory_impl;
|
||||
|
||||
/// \brief The bond type as defined in the CCD, possible values taken from the mmcif_pdbx file
|
||||
/// \brief The bond type or bond order as defined in the CCD, possible values taken from the mmcif_pdbx file
|
||||
enum class bond_type
|
||||
{
|
||||
sing, // 'single bond'
|
||||
doub, // 'double bond'
|
||||
trip, // 'triple bond'
|
||||
quad, // 'quadruple bond'
|
||||
arom, // 'aromatic bond'
|
||||
poly, // 'polymeric bond'
|
||||
delo, // 'delocalized double bond'
|
||||
pi, // 'pi bond'
|
||||
sing, ///< single bond
|
||||
doub, ///< double bond
|
||||
trip, ///< triple bond
|
||||
quad, ///< quadruple bond
|
||||
arom, ///< aromatic bond
|
||||
poly, ///< polymeric bond
|
||||
delo, ///< delocalized double bond
|
||||
pi, ///< pi bond
|
||||
};
|
||||
|
||||
std::string to_string(bond_type bondType);
|
||||
bond_type from_string(const std::string &bondType);
|
||||
/// @brief return the string representation of @a bondType
|
||||
std::string bond_type_to_string(bond_type bondType);
|
||||
|
||||
/// @brief return the cif::bond_type for the string representation @a bondType
|
||||
bond_type parse_bond_type_from_string(const std::string &bondType);
|
||||
|
||||
/// \brief The possible stereo config values for a compound_atom.
|
||||
///
|
||||
/// As the site https://psiberg.com/r-s-nomenclature/ states:
|
||||
///
|
||||
/// > RS nomenclature is currently the preferred system for assigning absolute
|
||||
/// > configuration to chiral molecules. The letters R and S come from the Latin
|
||||
/// > words ‘Rectus‘ and ‘Sinister‘ meaning ‘right’ and ‘left’. Molecules that
|
||||
/// > rotate the plane of polarized light to right are referred to as ‘R isomers’
|
||||
/// > and the molecules that rotate the plane of polarized light to left are
|
||||
/// > referred to ‘S isomers’.
|
||||
enum class stereo_config_type : uint8_t
|
||||
{
|
||||
N = 'N', ///< Not polarizing
|
||||
R = 'R', ///< Rectus
|
||||
S = 'S' ///< Sinister
|
||||
};
|
||||
|
||||
/// @brief return the string representation of @a stereo_config
|
||||
std::string to_string(stereo_config_type stereo_config);
|
||||
|
||||
/// @brief return the cif::stereo_config_type for the string representation @a stereo_config
|
||||
stereo_config_type parse_stereo_config_from_string(const std::string &stereo_config);
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
/// \brief struct containing information about an atom in a chemical compound.
|
||||
@@ -68,14 +109,17 @@ bond_type from_string(const std::string &bondType);
|
||||
|
||||
struct compound_atom
|
||||
{
|
||||
std::string id;
|
||||
atom_type type_symbol;
|
||||
int charge = 0;
|
||||
bool aromatic = false;
|
||||
bool leaving_atom = false;
|
||||
bool stereo_config = false;
|
||||
float x, y, z;
|
||||
std::string id; ///< Identifier for each atom in the chemical component
|
||||
atom_type type_symbol; ///< The element type for each atom in the chemical component.
|
||||
int charge = 0; ///< The formal charge assigned to each atom in the chemical component.
|
||||
bool aromatic = false; ///< Defines atoms in an aromatic moiety
|
||||
bool leaving_atom = false; ///< Flags atoms with "leaving" capability
|
||||
stereo_config_type stereo_config = stereo_config_type::N; ///< Defines the stereochemical configuration of the chiral center atom.
|
||||
float x, ///< The x component of the coordinates for each atom specified as orthogonal angstroms.
|
||||
y, ///< The y component of the coordinates for each atom specified as orthogonal angstroms.
|
||||
z; ///< The z component of the coordinates for each atom specified as orthogonal angstroms.
|
||||
|
||||
/// Return the location of the atom as a point
|
||||
point get_location() const
|
||||
{
|
||||
return { x, y, z };
|
||||
@@ -87,9 +131,10 @@ struct compound_atom
|
||||
|
||||
struct compound_bond
|
||||
{
|
||||
std::string atom_id[2];
|
||||
bond_type type;
|
||||
bool aromatic = false, stereo_config = false;
|
||||
std::string atom_id[2]; ///< The ID's of the two atoms that define the bond.
|
||||
bond_type type; ///< The bond order of the chemical bond associated with the specified atoms.
|
||||
bool aromatic = false, ///< Defines aromatic bonds.
|
||||
stereo_config = false; ///< Defines stereochemical bonds.
|
||||
};
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
@@ -105,23 +150,26 @@ class compound
|
||||
public:
|
||||
// accessors
|
||||
|
||||
std::string id() const { return m_id; }
|
||||
std::string name() const { return m_name; }
|
||||
std::string type() const { return m_type; }
|
||||
std::string id() const { return m_id; } ///< Return the alphanumeric code for the chemical component.
|
||||
std::string name() const { return m_name; } ///< Return the name of the chemical component.
|
||||
std::string type() const { return m_type; } ///< Return the type of monomer.
|
||||
std::string formula() const { return m_formula; } ///< Return the chemical formula of the chemical component.
|
||||
float formula_weight() const { return m_formula_weight; } ///< Return the formula mass of the chemical component in Daltons.
|
||||
int formal_charge() const { return m_formal_charge; } ///< Return the formal charge on the chemical component.
|
||||
|
||||
/// The group record is only available in CCP4 monomer library files.
|
||||
/// For CCD entries this value will always contain 'non-polymer'
|
||||
std::string group() const { return m_group; }
|
||||
std::string formula() const { return m_formula; }
|
||||
float formula_weight() const { return m_formula_weight; }
|
||||
int formal_charge() const { return m_formal_charge; }
|
||||
|
||||
const std::vector<compound_atom> &atoms() const { return m_atoms; }
|
||||
const std::vector<compound_bond> &bonds() const { return m_bonds; }
|
||||
const std::vector<compound_atom> &atoms() const { return m_atoms; } ///< Return the list of atoms for this compound
|
||||
const std::vector<compound_bond> &bonds() const { return m_bonds; } ///< Return the list of bonds for this compound
|
||||
|
||||
compound_atom get_atom_by_atom_id(const std::string &atom_id) const;
|
||||
compound_atom get_atom_by_atom_id(const std::string &atom_id) const; ///< Return the atom with id @a atom_id
|
||||
|
||||
bool atoms_bonded(const std::string &atomId_1, const std::string &atomId_2) const;
|
||||
float bond_length(const std::string &atomId_1, const std::string &atomId_2) const;
|
||||
bool atoms_bonded(const std::string &atomId_1, const std::string &atomId_2) const; ///< Return true if @a atomId_1 is bonded to @a atomId_2
|
||||
float bond_length(const std::string &atomId_1, const std::string &atomId_2) const; ///< Return the bond length between @a atomId_1 and @a atomId_2
|
||||
|
||||
bool is_water() const
|
||||
bool is_water() const ///< Return if the compound is actually a water
|
||||
{
|
||||
return m_id == "HOH" or m_id == "H2O" or m_id == "WAT";
|
||||
}
|
||||
@@ -148,6 +196,8 @@ class compound
|
||||
// --------------------------------------------------------------------
|
||||
// Factory class for compound and Link objects
|
||||
|
||||
/// Use the compound_factory singleton instance to create compound objects
|
||||
|
||||
class compound_factory
|
||||
{
|
||||
public:
|
||||
@@ -159,14 +209,28 @@ class compound_factory
|
||||
/// flag to true.
|
||||
|
||||
static void init(bool useThreadLocalInstanceOnly);
|
||||
|
||||
/// Return the singleton instance. If initialized with local threads, this is the
|
||||
/// instance for the current thread.
|
||||
static compound_factory &instance();
|
||||
|
||||
/// Delete and reset the singleton instance. If initialized with local threads, this is the
|
||||
/// instance for the current thread.
|
||||
static void clear();
|
||||
|
||||
/// Set the default dictionary file to @a inDictFile
|
||||
void set_default_dictionary(const std::filesystem::path &inDictFile);
|
||||
|
||||
/// Override any previously loaded dictionary with @a inDictFile
|
||||
void push_dictionary(const std::filesystem::path &inDictFile);
|
||||
|
||||
/// Remove the last pushed dictionary
|
||||
void pop_dictionary();
|
||||
|
||||
/// Return whether @a res_name is a valid and known peptide
|
||||
bool is_known_peptide(const std::string &res_name) const;
|
||||
|
||||
/// Return whether @a res_name is a valid and known base
|
||||
bool is_known_base(const std::string &res_name) const;
|
||||
|
||||
/// \brief Create the compound object for \a id
|
||||
@@ -179,7 +243,8 @@ class compound_factory
|
||||
|
||||
~compound_factory();
|
||||
|
||||
static CIFPP_EXPORT const std::map<std::string, char> kAAMap, kBaseMap;
|
||||
CIFPP_EXPORT static const std::map<std::string, char> kAAMap, ///< Globally accessible static list of the default amino acids
|
||||
kBaseMap; ///< Globally accessible static list of the default bases
|
||||
|
||||
private:
|
||||
compound_factory();
|
||||
|
||||
@@ -34,14 +34,100 @@
|
||||
#include <regex>
|
||||
#include <utility>
|
||||
|
||||
/** \file condition.hpp
|
||||
* This file contains code to create conditions: object encapsulating a
|
||||
* query you can use to find rows in a @ref cif::category
|
||||
*
|
||||
* Conditions are created as standard C++ expressions. That means
|
||||
* you can use the standard comparison operators to compare field
|
||||
* contents with a value and boolean operators to chain everything
|
||||
* together.
|
||||
*
|
||||
* To create a query that simply compares one field with one value:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* cif::condition c = cif::key("id") == 1;
|
||||
* @endcode
|
||||
*
|
||||
* That will find rows where the ID field contains the number 1. If
|
||||
* using cif::key is a bit too much typing, you can also write:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* using namespace cif::literals;
|
||||
*
|
||||
* cif::condition c2 = "id"_key == 1;
|
||||
* @endcode
|
||||
*
|
||||
* Now if you want both ID = 1 and ID = 2 in the result:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* auto c3 = "id"_key == 1 or "id"_key == 2;
|
||||
* @endcode
|
||||
*
|
||||
* There are some special values you can use. To find rows with field that
|
||||
* do not have a value:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* auto c4 = "type"_key == cif::null;
|
||||
* @endcode
|
||||
*
|
||||
* Of if it should not be NULL:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* auto c5 = "type"_key != cif::null;
|
||||
* @endcode
|
||||
*
|
||||
* There's even a way to find all records:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* auto c6 = cif::all;
|
||||
* @endcode
|
||||
*
|
||||
* And when you want to search for any column containing the value 'foo':
|
||||
*
|
||||
* @code{.cpp}
|
||||
* auto c7 = cif::any == "foo";
|
||||
* @endcode
|
||||
*
|
||||
* All these conditions can be chained together again:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* auto c8 = std::move(c3) and std::move(c5);
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// let's make life easier
|
||||
/// let's make life easier, since @ref cif::category is not known yet,
|
||||
/// we declare a function to access its contents
|
||||
|
||||
/**
|
||||
* @brief Get the fields that can be used as key in conditions for a category
|
||||
*
|
||||
* @param cat The category whose fields to return
|
||||
* @return iset The set of key field names
|
||||
*/
|
||||
iset get_category_fields(const category &cat);
|
||||
|
||||
/**
|
||||
* @brief Get the column index for column @a col in category @a cat
|
||||
*
|
||||
* @param cat The category
|
||||
* @param col The name of the column
|
||||
* @return uint16_t The index
|
||||
*/
|
||||
uint16_t get_column_ix(const category &cat, std::string_view col);
|
||||
|
||||
/**
|
||||
* @brief Return whether the column @a col in category @a cat has a primitive type of *uchar*
|
||||
*
|
||||
* @param cat The category
|
||||
* @param col The column name
|
||||
* @return true If the primitive type is of type *uchar*
|
||||
* @return false If the primitive type is not of type *uchar*
|
||||
*/
|
||||
bool is_column_type_uchar(const category &cat, std::string_view col);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -72,16 +158,32 @@ namespace detail
|
||||
struct not_condition_impl;
|
||||
} // namespace detail
|
||||
|
||||
/**
|
||||
* @brief The interface class for conditions. This uses the bridge pattern,
|
||||
* which means the implementation is in the member m_impl
|
||||
*/
|
||||
class condition
|
||||
{
|
||||
public:
|
||||
using condition_impl = detail::condition_impl;
|
||||
|
||||
/** @cond */
|
||||
using condition_impl = detail::condition_impl;
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief Construct a new, empty condition object
|
||||
*
|
||||
*/
|
||||
condition()
|
||||
: m_impl(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new condition object with implementation @a impl
|
||||
*
|
||||
* @param impl The implementation to use
|
||||
*/
|
||||
explicit condition(condition_impl *impl)
|
||||
: m_impl(impl)
|
||||
{
|
||||
@@ -89,6 +191,9 @@ class condition
|
||||
|
||||
condition(const condition &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Construct a new condition object moving the data from @a rhs
|
||||
*/
|
||||
condition(condition &&rhs) noexcept
|
||||
: m_impl(nullptr)
|
||||
{
|
||||
@@ -97,6 +202,9 @@ class condition
|
||||
|
||||
condition &operator=(const condition &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Assignment operator moving the data from @a rhs
|
||||
*/
|
||||
condition &operator=(condition &&rhs) noexcept
|
||||
{
|
||||
std::swap(m_impl, rhs.m_impl);
|
||||
@@ -109,8 +217,22 @@ class condition
|
||||
m_impl = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Prepare the condition to be used on category @a c. This will
|
||||
* take care of setting the correct indices for fields e.g.
|
||||
*
|
||||
* @param c The category this query should act upon
|
||||
*/
|
||||
void prepare(const category &c);
|
||||
|
||||
/**
|
||||
* @brief This operator returns true if the row referenced by @a r is
|
||||
* a match for this condition.
|
||||
*
|
||||
* @param r The reference to a row.
|
||||
* @return true If there is a match
|
||||
* @return false If there is no match
|
||||
*/
|
||||
bool operator()(row_handle r) const
|
||||
{
|
||||
assert(this->m_impl != nullptr);
|
||||
@@ -118,27 +240,53 @@ class condition
|
||||
return m_impl ? m_impl->test(r) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return true if the condition is not empty
|
||||
*/
|
||||
explicit operator bool() { return not empty(); }
|
||||
|
||||
/**
|
||||
* @brief Return true if the condition is empty, has no condition
|
||||
*/
|
||||
bool empty() const { return m_impl == nullptr; }
|
||||
|
||||
/**
|
||||
* @brief If the prepare step found out there is only one hit
|
||||
* this single hit can be returned by this method.
|
||||
*
|
||||
* @return std::optional<row_handle> The result will contain
|
||||
* a row reference if there is a single hit, it will be empty otherwise
|
||||
*/
|
||||
std::optional<row_handle> single() const
|
||||
{
|
||||
return m_impl ? m_impl->single() : std::optional<row_handle>();
|
||||
}
|
||||
|
||||
friend condition operator||(condition &&a, condition &&b);
|
||||
friend condition operator&&(condition &&a, condition &&b);
|
||||
friend condition operator||(condition &&a, condition &&b); /**< Return a condition which is the logical OR or condition @a and @b */
|
||||
friend condition operator&&(condition &&a, condition &&b); /**< Return a condition which is the logical AND or condition @a and @b */
|
||||
|
||||
/// @cond
|
||||
friend struct detail::or_condition_impl;
|
||||
friend struct detail::and_condition_impl;
|
||||
friend struct detail::not_condition_impl;
|
||||
/// @endcond
|
||||
|
||||
/**
|
||||
* @brief Swap two conditions
|
||||
*/
|
||||
void swap(condition &rhs)
|
||||
{
|
||||
std::swap(m_impl, rhs.m_impl);
|
||||
std::swap(m_prepared, rhs.m_prepared);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to use to write out a condition to @a os, for debugging purposes
|
||||
*
|
||||
* @param os The std::ostream to write to
|
||||
* @param cond The condition to write
|
||||
* @return std::ostream& The same as @a os
|
||||
*/
|
||||
friend std::ostream &operator<<(std::ostream &os, const condition &cond)
|
||||
{
|
||||
if (cond.m_impl)
|
||||
@@ -691,6 +839,9 @@ namespace detail
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/**
|
||||
* @brief Create a condition containing the logical AND of conditions @a a and @a b
|
||||
*/
|
||||
inline condition operator and(condition &&a, condition &&b)
|
||||
{
|
||||
if (a.m_impl and b.m_impl)
|
||||
@@ -700,6 +851,9 @@ inline condition operator and(condition &&a, condition &&b)
|
||||
return condition(std::move(b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create a condition containing the logical OR of conditions @a a and @a b
|
||||
*/
|
||||
inline condition operator or(condition &&a, condition &&b)
|
||||
{
|
||||
if (a.m_impl and b.m_impl)
|
||||
@@ -732,21 +886,49 @@ inline condition operator or(condition &&a, condition &&b)
|
||||
return condition(std::move(b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief A helper class to make it possible to search for empty fields (NULL)
|
||||
*
|
||||
* @code{.cpp}
|
||||
* "id"_key == cif::empty_type();
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
struct empty_type
|
||||
{
|
||||
};
|
||||
|
||||
/// \brief A helper to make it possible to have conditions like ("id"_key == cif::null)
|
||||
/**
|
||||
* @brief A helper to make it possible to have conditions like
|
||||
*
|
||||
* @code{.cpp}
|
||||
* "id"_key == cif::null;
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
inline constexpr empty_type null = empty_type();
|
||||
|
||||
/**
|
||||
* @brief Class to use in creating conditions, creates a reference to a field or column
|
||||
*
|
||||
*/
|
||||
struct key
|
||||
{
|
||||
/**
|
||||
* @brief Construct a new key object using @a itemTag as name
|
||||
*
|
||||
* @param itemTag
|
||||
*/
|
||||
explicit key(const std::string &itemTag)
|
||||
: m_item_tag(itemTag)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new key object using @a itemTag as name
|
||||
*
|
||||
* @param itemTag
|
||||
*/
|
||||
explicit key(const char *itemTag)
|
||||
: m_item_tag(itemTag)
|
||||
{
|
||||
@@ -755,44 +937,49 @@ struct key
|
||||
key(const key &) = delete;
|
||||
key &operator=(const key &) = delete;
|
||||
|
||||
std::string m_item_tag;
|
||||
std::string m_item_tag; ///< The column name
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Operator to create an equals condition based on a key @a key and a value @a v
|
||||
*/
|
||||
template <typename T>
|
||||
condition operator==(const key &key, const T &v)
|
||||
{
|
||||
return condition(new detail::key_equals_condition_impl({ key.m_item_tag, v }));
|
||||
}
|
||||
|
||||
inline condition operator==(const key &key, const char *value)
|
||||
/**
|
||||
* @brief Operator to create an equals condition based on a key @a key and a value @a value
|
||||
*/
|
||||
inline condition operator==(const key &key, std::string_view value)
|
||||
{
|
||||
if (value != nullptr and *value != 0)
|
||||
if (not value.empty())
|
||||
return condition(new detail::key_equals_condition_impl({ key.m_item_tag, value }));
|
||||
else
|
||||
return condition(new detail::key_is_empty_condition_impl(key.m_item_tag));
|
||||
}
|
||||
|
||||
// inline condition_t operator==(const key& key, const detail::ItemReference& v)
|
||||
// {
|
||||
// if (v.empty())
|
||||
// return condition_t(new detail::key_is_empty_condition_impl(key.m_item_tag));
|
||||
// else
|
||||
// return condition_t(new detail::key_compare_condition_impl(key.m_item_tag, [tag = key.m_item_tag, v](const category& c, const row& r, bool icase)
|
||||
// { return r[tag].template compare<(v, icase) == 0; }));
|
||||
// }
|
||||
|
||||
/**
|
||||
* @brief Operator to create a not equals condition based on a key @a key and a value @a v
|
||||
*/
|
||||
template <typename T>
|
||||
condition operator!=(const key &key, const T &v)
|
||||
{
|
||||
return condition(new detail::not_condition_impl(operator==(key, v)));
|
||||
}
|
||||
|
||||
inline condition operator!=(const key &key, const char *v)
|
||||
/**
|
||||
* @brief Operator to create a not equals condition based on a key @a key and a value @a value
|
||||
*/
|
||||
inline condition operator!=(const key &key, std::string_view value)
|
||||
{
|
||||
std::string value(v ? v : "");
|
||||
return condition(new detail::not_condition_impl(operator==(key, value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create a greater than condition based on a key @a key and a value @a v
|
||||
*/
|
||||
template <typename T>
|
||||
condition operator>(const key &key, const T &v)
|
||||
{
|
||||
@@ -805,6 +992,9 @@ condition operator>(const key &key, const T &v)
|
||||
s.str()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create a greater than or equals condition based on a key @a key and a value @a v
|
||||
*/
|
||||
template <typename T>
|
||||
condition operator>=(const key &key, const T &v)
|
||||
{
|
||||
@@ -817,6 +1007,9 @@ condition operator>=(const key &key, const T &v)
|
||||
s.str()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create a less than condition based on a key @a key and a value @a v
|
||||
*/
|
||||
template <typename T>
|
||||
condition operator<(const key &key, const T &v)
|
||||
{
|
||||
@@ -829,6 +1022,9 @@ condition operator<(const key &key, const T &v)
|
||||
s.str()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create a less than or equals condition based on a key @a key and a value @a v
|
||||
*/
|
||||
template <typename T>
|
||||
condition operator<=(const key &key, const T &v)
|
||||
{
|
||||
@@ -841,43 +1037,82 @@ condition operator<=(const key &key, const T &v)
|
||||
s.str()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create a condition based on a key @a key and a regular expression @a rx
|
||||
*/
|
||||
inline condition operator==(const key &key, const std::regex &rx)
|
||||
{
|
||||
return condition(new detail::key_matches_condition_impl(key.m_item_tag, rx));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create a condition based on a key @a key which should be empty/null
|
||||
*/
|
||||
inline condition operator==(const key &key, const empty_type &)
|
||||
{
|
||||
return condition(new detail::key_is_empty_condition_impl(key.m_item_tag));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create a condition based on a key @a key which should be not empty/null
|
||||
*/
|
||||
inline condition operator!=(const key &key, const empty_type &)
|
||||
{
|
||||
return condition(new detail::key_is_not_empty_condition_impl(key.m_item_tag));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create a condition to search any column for a value @a v if @a v contains a value
|
||||
* compare to null if not.
|
||||
*/
|
||||
template <typename T>
|
||||
condition operator==(const key &key, const std::optional<T> &v)
|
||||
{
|
||||
if (v.has_value())
|
||||
return condition(new detail::key_equals_condition_impl({ key.m_item_tag, *v }));
|
||||
else
|
||||
return condition(new detail::key_is_empty_condition_impl(key.m_item_tag));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create a boolean opposite of the condition in @a rhs
|
||||
*/
|
||||
inline condition operator not(condition &&rhs)
|
||||
{
|
||||
return condition(new detail::not_condition_impl(std::move(rhs)));
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
struct any_type
|
||||
{
|
||||
};
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief A helper for any field constructs
|
||||
*/
|
||||
inline constexpr any_type any = any_type{};
|
||||
|
||||
/**
|
||||
* @brief Create a condition to search any column for a value @a v
|
||||
*/
|
||||
template <typename T>
|
||||
condition operator==(const any_type &, const T &v)
|
||||
{
|
||||
return condition(new detail::any_is_condition_impl<T>(v));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create a condition to search any column for a regular expression @a rx
|
||||
*/
|
||||
inline condition operator==(const any_type &, const std::regex &rx)
|
||||
{
|
||||
return condition(new detail::any_matches_condition_impl(rx));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create a condition to return all rows
|
||||
*/
|
||||
inline condition all()
|
||||
{
|
||||
return condition(new detail::all_condition_impl());
|
||||
@@ -885,6 +1120,13 @@ inline condition all()
|
||||
|
||||
namespace literals
|
||||
{
|
||||
/**
|
||||
* @brief Return a cif::key for the column name @a text
|
||||
*
|
||||
* @param text The name of the column
|
||||
* @param length The length of @a text
|
||||
* @return key The cif::key created
|
||||
*/
|
||||
inline key operator""_key(const char *text, size_t length)
|
||||
{
|
||||
return key(std::string(text, length));
|
||||
|
||||
@@ -29,57 +29,161 @@
|
||||
#include "cif++/category.hpp"
|
||||
#include "cif++/forward_decl.hpp"
|
||||
|
||||
/** \file datablock.hpp
|
||||
* Each valid mmCIF file contains at least one @ref cif::datablock.
|
||||
* A datablock has a name and can contain one or more @ref cif::category "categories"
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief A datablock is a list of category objects with some additional features
|
||||
*
|
||||
*/
|
||||
|
||||
class datablock : public std::list<category>
|
||||
{
|
||||
public:
|
||||
datablock() = default;
|
||||
|
||||
/**
|
||||
* @brief Construct a new datablock object with name @a name
|
||||
*
|
||||
* @param name The name for the new datablock
|
||||
*/
|
||||
datablock(std::string_view name)
|
||||
: m_name(name)
|
||||
{
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
datablock(const datablock &);
|
||||
datablock(datablock &&) = default;
|
||||
|
||||
datablock &operator=(const datablock &);
|
||||
datablock &operator=(datablock &&) = default;
|
||||
/** @endcond */
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Return the name of this datablock
|
||||
*/
|
||||
const std::string &name() const { return m_name; }
|
||||
|
||||
/**
|
||||
* @brief Set the name of this datablock to @a name
|
||||
*
|
||||
* @param name The new name
|
||||
*/
|
||||
void set_name(std::string_view name)
|
||||
{
|
||||
m_name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the validator object to @a v
|
||||
*
|
||||
* @param v The new validator object, may be null
|
||||
*/
|
||||
void set_validator(const validator *v);
|
||||
|
||||
/**
|
||||
* @brief Get the validator object
|
||||
*
|
||||
* @return const validator* The validator or nullptr if there is none
|
||||
*/
|
||||
const validator *get_validator() const;
|
||||
|
||||
/**
|
||||
* @brief Validates the content of this datablock and all its content
|
||||
*
|
||||
* @return true If the content is valid
|
||||
* @return false If the content is not valid
|
||||
*/
|
||||
bool is_valid() const;
|
||||
|
||||
/**
|
||||
* @brief Validates all contained data for valid links between parents and children
|
||||
* as defined in the validator
|
||||
*
|
||||
* @return true If all links are valid
|
||||
* @return false If all links are not valid
|
||||
*/
|
||||
bool validate_links() const;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Return the category named @a name, will create a new and empty
|
||||
* category named @a name if it does not exist.
|
||||
*
|
||||
* @param name The name of the category to return
|
||||
* @return category& Reference to the named category
|
||||
*/
|
||||
category &operator[](std::string_view name);
|
||||
|
||||
/**
|
||||
* @brief Return the const category named @a name, will return a reference
|
||||
* to a static empty category if it was not found.
|
||||
*
|
||||
* @param name The name of the category to return
|
||||
* @return category& Reference to the named category
|
||||
*/
|
||||
const category &operator[](std::string_view name) const;
|
||||
|
||||
/**
|
||||
* @brief Return a pointer to the category named @a name or nullptr if
|
||||
* it does not exist.
|
||||
*
|
||||
* @param name The name of the category
|
||||
* @return category* Pointer to the category found or nullptr
|
||||
*/
|
||||
category *get(std::string_view name);
|
||||
|
||||
/**
|
||||
* @brief Return a pointer to the category named @a name or nullptr if
|
||||
* it does not exist.
|
||||
*
|
||||
* @param name The name of the category
|
||||
* @return category* Pointer to the category found or nullptr
|
||||
*/
|
||||
const category *get(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* @brief Tries to find a category with name @a name and will create a
|
||||
* new one if it is not found. The result is a tuple of an iterator
|
||||
* pointing to the category and a boolean indicating whether the category
|
||||
* was created or not.
|
||||
*
|
||||
* @param name The name for the category
|
||||
* @return std::tuple<iterator, bool> A tuple containing an iterator pointing
|
||||
* at the category and a boolean indicating whether the category was newly
|
||||
* created.
|
||||
*/
|
||||
std::tuple<iterator, bool> emplace(std::string_view name);
|
||||
|
||||
/**
|
||||
* @brief Get the preferred order of the categories when writing them
|
||||
*/
|
||||
std::vector<std::string> get_tag_order() const;
|
||||
|
||||
/**
|
||||
* @brief Write out the contents to @a os
|
||||
*/
|
||||
void write(std::ostream &os) const;
|
||||
|
||||
/**
|
||||
* @brief Write out the contents to @a os using the order defined in @a tag_order
|
||||
*/
|
||||
void write(std::ostream &os, const std::vector<std::string> &tag_order);
|
||||
|
||||
/**
|
||||
* @brief Friend operator<< to write datablock @a db to std::ostream @a os
|
||||
*/
|
||||
friend std::ostream &operator<<(std::ostream &os, const datablock &db)
|
||||
{
|
||||
db.write(os);
|
||||
@@ -88,6 +192,9 @@ class datablock : public std::list<category>
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Comparison operator to compare two datablock for equal content
|
||||
*/
|
||||
bool operator==(const datablock &rhs) const;
|
||||
|
||||
private:
|
||||
|
||||
@@ -28,10 +28,23 @@
|
||||
|
||||
#include "cif++/validate.hpp"
|
||||
|
||||
/**
|
||||
* @file validate.hpp
|
||||
*
|
||||
* Functions to create and manipulate validator objects
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief Parse the contents of @a is and create a new validator object with name @a name
|
||||
*/
|
||||
validator parse_dictionary(std::string_view name, std::istream &is);
|
||||
|
||||
/**
|
||||
* @brief Extend the definitions in validator @a v with the contents of stream @a is
|
||||
*/
|
||||
void extend_dictionary(validator &v, std::istream &is);
|
||||
|
||||
} // namespace cif
|
||||
|
||||
@@ -32,26 +32,60 @@
|
||||
#include "cif++/datablock.hpp"
|
||||
#include "cif++/parser.hpp"
|
||||
|
||||
/** \file file.hpp
|
||||
*
|
||||
* The file class defined here encapsulates the contents of an mmCIF file
|
||||
* It is mainly a list of @ref cif::datablock objects
|
||||
*
|
||||
* The class file has methods to load dictionaries. These dictionaries are
|
||||
* loaded from resources (if available) or from disk from several locations.
|
||||
*
|
||||
* See the documentation on load_resource() in file: utilities.hpp for more
|
||||
* information on how data is loaded.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The class file is actually a list of datablock objects
|
||||
*
|
||||
*/
|
||||
|
||||
class file : public std::list<datablock>
|
||||
{
|
||||
public:
|
||||
file() = default;
|
||||
|
||||
/**
|
||||
* @brief Construct a new file object using the data in the file @a p as content
|
||||
*
|
||||
* @param p Path to a file containing the data to load
|
||||
*/
|
||||
explicit file(const std::filesystem::path &p)
|
||||
{
|
||||
load(p);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new file object using the data in the std::istream @a is
|
||||
*
|
||||
* @param is The istream containing the data to load
|
||||
*/
|
||||
explicit file(std::istream &is)
|
||||
{
|
||||
load(is);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new file object with data in the constant string defined
|
||||
* by @a data and @a length
|
||||
*
|
||||
* @param data The pointer to the character string with data to load
|
||||
* @param length The length of the data
|
||||
*/
|
||||
explicit file(const char *data, size_t length)
|
||||
{
|
||||
struct membuf : public std::streambuf
|
||||
@@ -66,50 +100,136 @@ class file : public std::list<datablock>
|
||||
load(is);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
file(const file &) = default;
|
||||
file(file &&) = default;
|
||||
file &operator=(const file &) = default;
|
||||
file &operator=(file &&) = default;
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief Set the validator object to @a v
|
||||
*/
|
||||
void set_validator(const validator *v);
|
||||
|
||||
/**
|
||||
* @brief Get the validator object
|
||||
*/
|
||||
const validator *get_validator() const
|
||||
{
|
||||
return m_validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validate the content and return true if everything was valid.
|
||||
*
|
||||
* Will throw an exception if there is no validator defined.
|
||||
*
|
||||
* If each category was valid, validate_links will also be called.
|
||||
*
|
||||
* @return true If the content is valid
|
||||
* @return false If the content is not valid
|
||||
*/
|
||||
bool is_valid() const;
|
||||
|
||||
/**
|
||||
* @brief Validate the content and return true if everything was valid.
|
||||
*
|
||||
* Will attempt to load the referenced dictionary if none was specified.
|
||||
*
|
||||
* If each category was valid, validate_links will also be called.
|
||||
*
|
||||
* @return true If the content is valid
|
||||
* @return false If the content is not valid
|
||||
*/
|
||||
bool is_valid();
|
||||
|
||||
/**
|
||||
* @brief Validate the links for all datablocks contained.
|
||||
*
|
||||
* Will throw an exception if no validator was specified.
|
||||
*
|
||||
* @return true If all links were valid
|
||||
* @return false If all links were not valid
|
||||
*/
|
||||
bool validate_links() const;
|
||||
|
||||
/**
|
||||
* @brief Attempt to load a dictionary (validator) based on
|
||||
* the contents of the *audit_conform* category, if available.
|
||||
*/
|
||||
void load_dictionary();
|
||||
|
||||
|
||||
/**
|
||||
* @brief Attempt to load the named dictionary @a name and
|
||||
* create a validator based on it.
|
||||
*
|
||||
* @param name The name of the dictionary to load
|
||||
*/
|
||||
void load_dictionary(std::string_view name);
|
||||
|
||||
/**
|
||||
* @brief Return true if a datablock with the name @a name is part of this file
|
||||
*/
|
||||
bool contains(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* @brief return a reference to the first datablock in the file
|
||||
*/
|
||||
datablock &front()
|
||||
{
|
||||
assert(not empty());
|
||||
return std::list<datablock>::front();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief return a const reference to the first datablock in the file
|
||||
*/
|
||||
const datablock &front() const
|
||||
{
|
||||
assert(not empty());
|
||||
return std::list<datablock>::front();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief return a reference to the datablock named @a name
|
||||
*/
|
||||
datablock &operator[](std::string_view name);
|
||||
|
||||
/**
|
||||
* @brief return a const reference to the datablock named @a name
|
||||
*/
|
||||
const datablock &operator[](std::string_view name) const;
|
||||
|
||||
/**
|
||||
* @brief Tries to find a datablock with name @a name and will create a
|
||||
* new one if it is not found. The result is a tuple of an iterator
|
||||
* pointing to the datablock and a boolean indicating whether the datablock
|
||||
* was created or not.
|
||||
*
|
||||
* @param name The name for the datablock
|
||||
* @return std::tuple<iterator, bool> A tuple containing an iterator pointing
|
||||
* at the datablock and a boolean indicating whether the datablock was newly
|
||||
* created.
|
||||
*/
|
||||
std::tuple<iterator, bool> emplace(std::string_view name);
|
||||
|
||||
/** Load the data from the file specified by @a p */
|
||||
void load(const std::filesystem::path &p);
|
||||
|
||||
/** Load the data from @a is */
|
||||
void load(std::istream &is);
|
||||
|
||||
/** Save the data to the file specified by @a p */
|
||||
void save(const std::filesystem::path &p) const;
|
||||
|
||||
/** Save the data to @a is */
|
||||
void save(std::ostream &os) const;
|
||||
|
||||
/**
|
||||
* @brief Friend operator<< to write file @a f to std::ostream @a os
|
||||
*/
|
||||
friend std::ostream &operator<<(std::ostream &os, const file &f)
|
||||
{
|
||||
f.save(os);
|
||||
|
||||
@@ -28,10 +28,12 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
/// \file format.hpp
|
||||
/// File containing a basic reimplementation of boost::format
|
||||
/// but then a bit more simplistic. Still this allowed me to move my code
|
||||
/// from using boost::format to something without external dependency easily.
|
||||
/** \file format.hpp
|
||||
*
|
||||
* File containing a basic reimplementation of boost::format
|
||||
* but then a bit more simplistic. Still this allowed me to move my code
|
||||
* from using boost::format to something without external dependency easily.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
@@ -85,6 +87,8 @@ namespace detail
|
||||
|
||||
} // namespace
|
||||
|
||||
/** @cond */
|
||||
|
||||
template <typename... Args>
|
||||
class format_plus_arg
|
||||
{
|
||||
@@ -132,6 +136,23 @@ class format_plus_arg
|
||||
vargs_vector_type m_vargs;
|
||||
};
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief A simplistic reimplementation of boost::format, in fact it is
|
||||
* actually a way to call the C function snprintf to format the arguments
|
||||
* in @a args into the format string @a fmt
|
||||
*
|
||||
* The string in @a fmt should thus be a C style format string.
|
||||
*
|
||||
* TODO: Move to C++23 style of printing.
|
||||
*
|
||||
* @tparam Args The types of the arguments
|
||||
* @param fmt The format string
|
||||
* @param args The arguments
|
||||
* @return An object that can be written out to a std::ostream using operator<<
|
||||
*/
|
||||
|
||||
template <typename... Args>
|
||||
constexpr auto format(std::string_view fmt, Args... args)
|
||||
{
|
||||
@@ -144,11 +165,20 @@ constexpr auto format(std::string_view fmt, Args... args)
|
||||
class fill_out_streambuf : public std::streambuf
|
||||
{
|
||||
public:
|
||||
|
||||
/** @cond */
|
||||
|
||||
using base_type = std::streambuf;
|
||||
using int_type = base_type::int_type;
|
||||
using char_type = base_type::char_type;
|
||||
using traits_type = base_type::traits_type;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief Construct a new fill out streambuf object based on ostream @a os and a
|
||||
* width to fill out to of @a width
|
||||
*/
|
||||
fill_out_streambuf(std::ostream &os, int width = 80)
|
||||
: m_os(os)
|
||||
, m_upstream(os.rdbuf())
|
||||
@@ -156,11 +186,21 @@ class fill_out_streambuf : public std::streambuf
|
||||
{
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
|
||||
~fill_out_streambuf()
|
||||
{
|
||||
m_os.rdbuf(m_upstream);
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief The magic happens here. Write out a couple of spaces when
|
||||
* the last character to write is a newline to make the line as
|
||||
* wide as the requested width.
|
||||
*/
|
||||
|
||||
virtual int_type
|
||||
overflow(int_type ic = traits_type::eof())
|
||||
{
|
||||
@@ -191,8 +231,10 @@ class fill_out_streambuf : public std::streambuf
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Return the upstream streambuf */
|
||||
std::streambuf *get_upstream() const { return m_upstream; }
|
||||
|
||||
/** Return how many lines have been written */
|
||||
int get_line_count() const { return m_line_count; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -31,6 +31,13 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @file forward_decl.hpp
|
||||
*
|
||||
* File containing only forward declarations
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
|
||||
@@ -13,27 +13,29 @@
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
/// \file gzio.hpp
|
||||
///
|
||||
/// Single header file for the implementation of stream classes
|
||||
/// that can transparently read and write compressed files.
|
||||
///
|
||||
/// The gzio::istream_buf class sniffs the input and decides whether to use
|
||||
/// a decompressor if a signature was recognized.
|
||||
///
|
||||
/// There's also an ifstream and ofstream class here that can
|
||||
/// read and write compressed files. In this case the decission
|
||||
/// whether to use a compressions/decompression algorithm is
|
||||
/// based on the extension of the \a filename argument.
|
||||
|
||||
// This is a stripped down version of the gxrio library from
|
||||
// https://github.com/mhekkel/gxrio.git
|
||||
// Most notably, the lzma support has been removed since getting
|
||||
// that to work in Windows proved to be too much work.
|
||||
/** \file gzio.hpp
|
||||
*
|
||||
* Single header file for the implementation of stream classes
|
||||
* that can transparently read and write compressed files.
|
||||
*
|
||||
* The gzio::istream_buf class sniffs the input and decides whether to use
|
||||
* a decompressor if a signature was recognized.
|
||||
*
|
||||
* There's also an ifstream and ofstream class here that can
|
||||
* read and write compressed files. In this case the decission
|
||||
* whether to use a compressions/decompression algorithm is
|
||||
* based on the extension of the \a filename argument.
|
||||
*
|
||||
* This is a stripped down version of the gxrio library from
|
||||
* https://github.com/mhekkel/gxrio.git
|
||||
* Most notably, the lzma support has been removed since getting
|
||||
* that to work in Windows proved to be too much work.
|
||||
*/
|
||||
|
||||
namespace cif::gzio
|
||||
{
|
||||
|
||||
/** The default buffer size to use */
|
||||
const size_t kDefaultBufferSize = 256;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -50,6 +52,8 @@ template <typename CharT, typename Traits>
|
||||
class basic_streambuf : public std::basic_streambuf<CharT, Traits>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
|
||||
using char_type = CharT;
|
||||
using traits_type = Traits;
|
||||
|
||||
@@ -77,14 +81,21 @@ class basic_streambuf : public std::basic_streambuf<CharT, Traits>
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/** Set the upstream streambuf to @a upstream */
|
||||
void set_upstream(streambuf_type *upstream)
|
||||
{
|
||||
m_upstream = upstream;
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
|
||||
virtual basic_streambuf *init(streambuf_type *sb) = 0;
|
||||
virtual basic_streambuf *close() = 0;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
protected:
|
||||
/// \brief The upstream streambuf object, usually this is a basic_filebuf
|
||||
streambuf_type *m_upstream = nullptr;
|
||||
@@ -105,6 +116,8 @@ template <typename CharT, typename Traits, size_t BufferSize = kDefaultBufferSiz
|
||||
class basic_igzip_streambuf : public basic_streambuf<CharT, Traits>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
|
||||
static_assert(sizeof(CharT) == 1, "Unfortunately, support for wide characters is not implemented yet.");
|
||||
|
||||
using char_type = CharT;
|
||||
@@ -121,6 +134,8 @@ class basic_igzip_streambuf : public basic_streambuf<CharT, Traits>
|
||||
|
||||
basic_igzip_streambuf(const basic_igzip_streambuf &) = delete;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief Move constructor
|
||||
basic_igzip_streambuf(basic_igzip_streambuf &&rhs)
|
||||
: base_type(std::move(rhs))
|
||||
@@ -141,6 +156,8 @@ class basic_igzip_streambuf : public basic_streambuf<CharT, Traits>
|
||||
}
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
|
||||
basic_igzip_streambuf &operator=(const basic_igzip_streambuf &) = delete;
|
||||
|
||||
/// \brief Move operator= implementation
|
||||
@@ -171,6 +188,8 @@ class basic_igzip_streambuf : public basic_streambuf<CharT, Traits>
|
||||
close();
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief This closes the zlib stream and sets the get pointers to null.
|
||||
base_type *close() override
|
||||
{
|
||||
@@ -302,6 +321,8 @@ template <typename CharT, typename Traits, size_t BufferSize = kDefaultBufferSiz
|
||||
class basic_ogzip_streambuf : public basic_streambuf<CharT, Traits>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
|
||||
static_assert(sizeof(CharT) == 1, "Unfortunately, support for wide characters is not implemented yet.");
|
||||
|
||||
using char_type = CharT;
|
||||
@@ -332,6 +353,8 @@ class basic_ogzip_streambuf : public basic_streambuf<CharT, Traits>
|
||||
|
||||
basic_ogzip_streambuf &operator=(const basic_ogzip_streambuf &) = delete;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief Move operator=
|
||||
basic_ogzip_streambuf &operator=(basic_ogzip_streambuf &&rhs)
|
||||
{
|
||||
@@ -487,6 +510,8 @@ template <typename CharT, typename Traits>
|
||||
class basic_istream : public std::basic_istream<CharT, Traits>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
|
||||
using base_type = std::basic_istream<CharT, Traits>;
|
||||
|
||||
using traits_type = Traits;
|
||||
@@ -498,6 +523,8 @@ class basic_istream : public std::basic_istream<CharT, Traits>
|
||||
|
||||
using gzip_streambuf_type = basic_igzip_streambuf<char_type, traits_type>;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief Regular move constructor
|
||||
basic_istream(basic_istream &&rhs)
|
||||
: base_type(std::move(rhs))
|
||||
@@ -591,6 +618,8 @@ template <typename CharT, typename Traits>
|
||||
class basic_ifstream : public basic_istream<CharT, Traits>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
|
||||
using base_type = basic_istream<CharT, Traits>;
|
||||
|
||||
using char_type = CharT;
|
||||
@@ -608,6 +637,8 @@ class basic_ifstream : public basic_istream<CharT, Traits>
|
||||
close();
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief Construct an ifstream
|
||||
/// \param filename Null terminated string specifying the file to open
|
||||
/// \param mode The mode in which to open the file
|
||||
@@ -647,10 +678,13 @@ class basic_ifstream : public basic_istream<CharT, Traits>
|
||||
this->rdbuf(&m_filebuf);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
basic_ifstream(const basic_ifstream &) = delete;
|
||||
|
||||
basic_ifstream &operator=(const basic_ifstream &) = delete;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief Move version of operator=
|
||||
basic_ifstream &operator=(basic_ifstream &&rhs)
|
||||
{
|
||||
@@ -777,6 +811,8 @@ template <typename CharT, typename Traits>
|
||||
class basic_ostream : public std::basic_ostream<CharT, Traits>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
|
||||
using base_type = std::basic_ostream<CharT, Traits>;
|
||||
|
||||
using char_type = CharT;
|
||||
@@ -785,6 +821,8 @@ class basic_ostream : public std::basic_ostream<CharT, Traits>
|
||||
using z_streambuf_type = basic_streambuf<char_type, traits_type>;
|
||||
using upstreambuf_type = std::basic_streambuf<char_type, traits_type>;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief Regular move constructor
|
||||
basic_ostream(basic_ostream &&rhs)
|
||||
: base_type(std::move(rhs))
|
||||
@@ -846,6 +884,8 @@ template <typename CharT, typename Traits>
|
||||
class basic_ofstream : public basic_ostream<CharT, Traits>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
|
||||
using base_type = basic_ostream<CharT, Traits>;
|
||||
|
||||
using char_type = CharT;
|
||||
@@ -861,6 +901,8 @@ class basic_ofstream : public basic_ostream<CharT, Traits>
|
||||
close();
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief Construct an ofstream
|
||||
/// \param filename Null terminated string specifying the file to open
|
||||
/// \param mode The mode in which to open the file
|
||||
@@ -899,10 +941,14 @@ class basic_ofstream : public basic_ostream<CharT, Traits>
|
||||
this->rdbuf(&m_filebuf);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
|
||||
basic_ofstream(const basic_ofstream &) = delete;
|
||||
|
||||
basic_ofstream &operator=(const basic_ofstream &) = delete;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief Move operator=
|
||||
basic_ofstream &operator=(basic_ofstream &&rhs)
|
||||
{
|
||||
@@ -1032,11 +1078,13 @@ class basic_ofstream : public basic_ostream<CharT, Traits>
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Convenience typedefs
|
||||
/// \brief Convenience typedef for a regular istream
|
||||
using istream = basic_istream<char, std::char_traits<char>>;
|
||||
|
||||
/// \brief Convenience typedef for a file ifstream
|
||||
using ifstream = basic_ifstream<char, std::char_traits<char>>;
|
||||
|
||||
// using ostream = basic_ostream<char, std::char_traits<char>>;
|
||||
/// \brief Convenience typedef for a file ofstream
|
||||
using ofstream = basic_ofstream<char, std::char_traits<char>>;
|
||||
|
||||
} // namespace gzio
|
||||
|
||||
@@ -41,16 +41,18 @@
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
/// \file item.hpp
|
||||
/// This file contains the declaration of item but also the item_value and item_handle
|
||||
/// These handle the storage of and access to the data for a single data field.
|
||||
/** \file item.hpp
|
||||
*
|
||||
* This file contains the declaration of item but also the item_value and item_handle
|
||||
* These handle the storage of and access to the data for a single data field.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/// \brief item is a transient class that is used to pass data into rows
|
||||
/// but it also takes care of formatting data.
|
||||
/// but it also takes care of formatting data.
|
||||
class item
|
||||
{
|
||||
public:
|
||||
@@ -109,7 +111,7 @@ class item
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content a the formatted integral value \a value
|
||||
template <typename T, std::enable_if_t<std::is_integral_v<T> and not std::is_same_v<T,bool>, int> = 0>
|
||||
template <typename T, std::enable_if_t<std::is_integral_v<T> and not std::is_same_v<T, bool>, int> = 0>
|
||||
item(const std::string_view name, const T &value)
|
||||
: m_name(name)
|
||||
{
|
||||
@@ -126,7 +128,7 @@ class item
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content a the formatted boolean value \a value
|
||||
template <typename T, std::enable_if_t<std::is_same_v<T,bool>, int> = 0>
|
||||
template <typename T, std::enable_if_t<std::is_same_v<T, bool>, int> = 0>
|
||||
item(const std::string_view name, const T &value)
|
||||
: m_name(name)
|
||||
{
|
||||
@@ -141,16 +143,15 @@ class item
|
||||
{
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
item(const item &rhs) = default;
|
||||
|
||||
item(item &&rhs) noexcept = default;
|
||||
|
||||
item &operator=(const item &rhs) = default;
|
||||
|
||||
item &operator=(item &&rhs) noexcept = default;
|
||||
/** @endcond */
|
||||
|
||||
std::string_view name() const { return m_name; }
|
||||
std::string_view value() const { return m_value; }
|
||||
std::string_view name() const { return m_name; } ///< Return the name of the item
|
||||
std::string_view value() const { return m_value; } ///< Return the value of the item
|
||||
|
||||
/// \brief replace the content of the stored value with \a v
|
||||
void value(std::string_view v) { m_value = v; }
|
||||
@@ -168,11 +169,13 @@ class item
|
||||
size_t length() const { return m_value.length(); }
|
||||
|
||||
/// \brief support for structured binding
|
||||
template<size_t N>
|
||||
template <size_t N>
|
||||
decltype(auto) get() const
|
||||
{
|
||||
if constexpr (N == 0) return name();
|
||||
else if constexpr (N == 1) return value();
|
||||
if constexpr (N == 0)
|
||||
return name();
|
||||
else if constexpr (N == 1)
|
||||
return value();
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -190,7 +193,9 @@ class item
|
||||
|
||||
struct item_value
|
||||
{
|
||||
/** @cond */
|
||||
item_value() = default;
|
||||
/** @endcond */
|
||||
|
||||
/// \brief constructor
|
||||
item_value(std::string_view text)
|
||||
@@ -210,6 +215,7 @@ struct item_value
|
||||
}
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
item_value(item_value &&rhs)
|
||||
: m_length(std::exchange(rhs.m_length, 0))
|
||||
, m_storage(std::exchange(rhs.m_storage, 0))
|
||||
@@ -236,24 +242,29 @@ struct item_value
|
||||
|
||||
item_value(const item_value &) = delete;
|
||||
item_value &operator=(const item_value &) = delete;
|
||||
/** @endcond */
|
||||
|
||||
/** operator bool, allows easy checking for empty items */
|
||||
explicit operator bool() const
|
||||
{
|
||||
return m_length != 0;
|
||||
}
|
||||
|
||||
size_t m_length = 0;
|
||||
size_t m_length = 0; ///< Length of the data
|
||||
union
|
||||
{
|
||||
char m_local_data[8];
|
||||
char *m_data;
|
||||
uint64_t m_storage;
|
||||
char m_local_data[8]; ///< Storage area for small strings (strings smaller than kBufferSize)
|
||||
char *m_data; ///< Pointer to a string stored in the heap
|
||||
uint64_t m_storage; ///< Alternative storage of the data, used in move operations
|
||||
};
|
||||
|
||||
/** The maximum length of locally stored strings */
|
||||
static constexpr size_t kBufferSize = sizeof(m_local_data);
|
||||
|
||||
// By using std::string_view instead of c_str we obain a
|
||||
// nice performance gain since we avoid many calls to strlen.
|
||||
|
||||
/** Return the content of the item as a std::string_view */
|
||||
constexpr inline std::string_view text() const
|
||||
{
|
||||
return { m_length >= kBufferSize ? m_data : m_local_data, m_length };
|
||||
@@ -268,10 +279,19 @@ struct item_value
|
||||
struct item_handle
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
// conversion helper class
|
||||
template <typename T, typename = void>
|
||||
struct item_value_as;
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* @brief Assign value @a value to the item referenced
|
||||
*
|
||||
* @tparam T Type of the value
|
||||
* @param value The value
|
||||
* @return reference to this item_handle
|
||||
*/
|
||||
template <typename T>
|
||||
item_handle &operator=(const T &value)
|
||||
{
|
||||
@@ -280,6 +300,20 @@ struct item_handle
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief A method with a variable number of arguments that will be concatenated and
|
||||
* assigned as a string. Use it like this:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* cif::item_handle ih;
|
||||
* is.os("The result of ", 1, " * ", 42, " is of course ", 42);
|
||||
* @endcode
|
||||
*
|
||||
* And the content will then be `The result of 1 * 42 is of course 42`.
|
||||
*
|
||||
* @tparam Ts Types of the parameters
|
||||
* @param v The parameters to concatenate
|
||||
*/
|
||||
template <typename... Ts>
|
||||
void os(const Ts &...v)
|
||||
{
|
||||
@@ -288,8 +322,10 @@ struct item_handle
|
||||
this->operator=(ss.str());
|
||||
}
|
||||
|
||||
/** Swap contents of this and @a b */
|
||||
void swap(item_handle &b);
|
||||
|
||||
/** Return the contents of this item as type @tparam T */
|
||||
template <typename T = std::string>
|
||||
auto as() const -> T
|
||||
{
|
||||
@@ -297,18 +333,36 @@ struct item_handle
|
||||
return item_value_as<value_type>::convert(*this);
|
||||
}
|
||||
|
||||
/** Return the contents of this item as type @tparam T or, if not
|
||||
* set, use @a dv as the default value.
|
||||
*/
|
||||
template <typename T>
|
||||
auto value_or(const T &dv) const
|
||||
{
|
||||
return empty() ? dv : this->as<T>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compare the contents of this item with value @a value
|
||||
* optionally ignoring character case, if @a icase is true.
|
||||
* Returns 0 if both are equal, -1 if this sorts before @a value
|
||||
* and 1 if this sorts after @a value
|
||||
*
|
||||
* @tparam T Type of the value @a value
|
||||
* @param value The value to compare with
|
||||
* @param icase Flag indicating if we should compare character case sensitive
|
||||
* @return -1, 0 or 1
|
||||
*/
|
||||
template <typename T>
|
||||
int compare(const T &value, bool icase = true) const
|
||||
{
|
||||
return item_value_as<T>::compare(*this, value, icase);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compare the value contained with the value @a value and
|
||||
* return true if both are equal.
|
||||
*/
|
||||
template <typename T>
|
||||
bool operator==(const T &value) const
|
||||
{
|
||||
@@ -317,45 +371,64 @@ struct item_handle
|
||||
}
|
||||
|
||||
// We may not have C++20 yet...
|
||||
|
||||
/**
|
||||
* @brief Compare the value contained with the value @a value and
|
||||
* return true if both are not equal.
|
||||
*/
|
||||
template <typename T>
|
||||
bool operator!=(const T &value) const
|
||||
{
|
||||
return not operator==(value);
|
||||
}
|
||||
|
||||
// empty means either null or unknown
|
||||
/**
|
||||
* @brief Returns true if the content string is empty or
|
||||
* only contains '.' meaning null or '?' meaning unknown
|
||||
* in a mmCIF context
|
||||
*/
|
||||
bool empty() const
|
||||
{
|
||||
auto txt = text();
|
||||
return txt.empty() or (txt.length() == 1 and (txt.front() == '.' or txt.front() == '?'));
|
||||
}
|
||||
|
||||
/** Easy way to test for an empty item */
|
||||
explicit operator bool() const { return not empty(); }
|
||||
|
||||
// is_null means the field contains '.'
|
||||
/// is_null return true if the field contains '.'
|
||||
bool is_null() const
|
||||
{
|
||||
auto txt = text();
|
||||
return txt.length() == 1 and txt.front() == '.';
|
||||
}
|
||||
|
||||
// is_unknown means the field contains '?'
|
||||
/// is_unknown returns true if the field contains '?'
|
||||
bool is_unknown() const
|
||||
{
|
||||
auto txt = text();
|
||||
return txt.length() == 1 and txt.front() == '?';
|
||||
}
|
||||
|
||||
/** Return a std::string_view for the contents */
|
||||
std::string_view text() const;
|
||||
|
||||
/**
|
||||
* @brief Construct a new item handle object
|
||||
*
|
||||
* @param column Column index
|
||||
* @param row Reference to the row
|
||||
*/
|
||||
item_handle(uint16_t column, row_handle &row)
|
||||
: m_column(column)
|
||||
, m_row_handle(row)
|
||||
{
|
||||
}
|
||||
|
||||
static CIFPP_EXPORT const item_handle s_null_item;
|
||||
/** A variable holding an empty item */
|
||||
CIFPP_EXPORT static const item_handle s_null_item;
|
||||
|
||||
/** friend to swap two item handles */
|
||||
friend void swap(item_handle a, item_handle b)
|
||||
{
|
||||
a.swap(b);
|
||||
@@ -372,6 +445,7 @@ struct item_handle
|
||||
|
||||
// So sad that older gcc implementations of from_chars did not support floats yet...
|
||||
|
||||
/** @cond */
|
||||
template <typename T>
|
||||
struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> and not std::is_same_v<T, bool>>>
|
||||
{
|
||||
@@ -385,17 +459,24 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> an
|
||||
{
|
||||
auto txt = ref.text();
|
||||
|
||||
std::from_chars_result r = selected_charconv<value_type>::from_chars(txt.data(), txt.data() + txt.size(), result);
|
||||
auto b = txt.data();
|
||||
auto e = txt.data() + txt.size();
|
||||
|
||||
if (r.ec != std::errc())
|
||||
std::from_chars_result r = (b + 1 < e and *b == '+' and std::isdigit(b[1])) ?
|
||||
selected_charconv<value_type>::from_chars(b + 1, e, result) :
|
||||
selected_charconv<value_type>::from_chars(b, e, result);
|
||||
|
||||
if (r.ec != std::errc() or r.ptr != e)
|
||||
{
|
||||
result = {};
|
||||
if (cif::VERBOSE)
|
||||
{
|
||||
if (r.ec == std::errc::invalid_argument)
|
||||
std::cerr << "Attempt to convert " << std::quoted(txt) << " into a number" << std::endl;
|
||||
std::cerr << "Attempt to convert " << std::quoted(txt) << " into a number\n";
|
||||
else if (r.ec == std::errc::result_out_of_range)
|
||||
std::cerr << "Conversion of " << std::quoted(txt) << " into a type that is too small" << std::endl;
|
||||
std::cerr << "Conversion of " << std::quoted(txt) << " into a type that is too small\n";
|
||||
else
|
||||
std::cerr << "Not a valid number " << std::quoted(txt) << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -415,16 +496,23 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> an
|
||||
{
|
||||
value_type v = {};
|
||||
|
||||
std::from_chars_result r = selected_charconv<value_type>::from_chars(txt.data(), txt.data() + txt.size(), v);
|
||||
auto b = txt.data();
|
||||
auto e = txt.data() + txt.size();
|
||||
|
||||
if (r.ec != std::errc())
|
||||
std::from_chars_result r = (b + 1 < e and *b == '+' and std::isdigit(b[1])) ?
|
||||
selected_charconv<value_type>::from_chars(b + 1, e, v) :
|
||||
selected_charconv<value_type>::from_chars(b, e, v);
|
||||
|
||||
if (r.ec != std::errc() or r.ptr != e)
|
||||
{
|
||||
if (cif::VERBOSE)
|
||||
{
|
||||
if (r.ec == std::errc::invalid_argument)
|
||||
std::cerr << "Attempt to convert " << std::quoted(txt) << " into a number" << std::endl;
|
||||
std::cerr << "Attempt to convert " << std::quoted(txt) << " into a number\n";
|
||||
else if (r.ec == std::errc::result_out_of_range)
|
||||
std::cerr << "Conversion of " << std::quoted(txt) << " into a type that is too small" << std::endl;
|
||||
std::cerr << "Conversion of " << std::quoted(txt) << " into a type that is too small\n";
|
||||
else
|
||||
std::cerr << "Not a valid number " << std::quoted(txt) << '\n';
|
||||
}
|
||||
result = 1;
|
||||
}
|
||||
@@ -546,22 +634,33 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_same_v<T, std::str
|
||||
}
|
||||
};
|
||||
|
||||
/** @endcond */
|
||||
|
||||
} // namespace cif
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
||||
template<> struct tuple_size<::cif::item>
|
||||
: public std::integral_constant<std::size_t, 2> {};
|
||||
/** @cond */
|
||||
|
||||
template<> struct tuple_element<0, ::cif::item>
|
||||
template <>
|
||||
struct tuple_size<::cif::item>
|
||||
: public std::integral_constant<std::size_t, 2>
|
||||
{
|
||||
};
|
||||
|
||||
template <>
|
||||
struct tuple_element<0, ::cif::item>
|
||||
{
|
||||
using type = decltype(std::declval<::cif::item>().name());
|
||||
};
|
||||
|
||||
template<> struct tuple_element<1, ::cif::item>
|
||||
template <>
|
||||
struct tuple_element<1, ::cif::item>
|
||||
{
|
||||
using type = decltype(std::declval<::cif::item>().value());
|
||||
};
|
||||
|
||||
}
|
||||
/** @endcond */
|
||||
|
||||
} // namespace std
|
||||
@@ -30,22 +30,46 @@
|
||||
|
||||
#include <array>
|
||||
|
||||
/**
|
||||
* @file iterator.hpp
|
||||
*
|
||||
* This file contains several implementations of generic iterators.
|
||||
*
|
||||
* Using partial specialization we can have implementation for
|
||||
* iterators that return row_handles, a single value or tuples of
|
||||
* multiple values.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Implementation of an iterator that can return
|
||||
* multiple values in a tuple. Of course, that tuple can
|
||||
* then used in structured binding to receive the values
|
||||
* in a for loop e.g.
|
||||
*
|
||||
* @tparam Category The category for this iterator
|
||||
* @tparam Ts The types this iterator can be dereferenced to
|
||||
*/
|
||||
template <typename Category, typename... Ts>
|
||||
class iterator_impl
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
template <typename, typename...>
|
||||
friend class iterator_impl;
|
||||
|
||||
friend class category;
|
||||
/** @endcond */
|
||||
|
||||
/** variable that contains the number of elements in the tuple */
|
||||
static constexpr size_t N = sizeof...(Ts);
|
||||
|
||||
/** @cond */
|
||||
using category_type = std::remove_cv_t<Category>;
|
||||
using row_type = std::conditional_t<std::is_const_v<Category>, const row, row>;
|
||||
|
||||
@@ -152,14 +176,16 @@ class iterator_impl
|
||||
return m_current != rhs.m_current;
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
private:
|
||||
template <size_t... Is>
|
||||
tuple_type get(std::index_sequence<Is...>) const
|
||||
{
|
||||
if (m_current != nullptr)
|
||||
{
|
||||
row_handle rh{*m_category, *m_current};
|
||||
return tuple_type{rh[m_column_ix[Is]].template as<Ts>()...};
|
||||
row_handle rh{ *m_category, *m_current };
|
||||
return tuple_type{ rh[m_column_ix[Is]].template as<Ts>()... };
|
||||
}
|
||||
|
||||
return {};
|
||||
@@ -171,10 +197,18 @@ class iterator_impl
|
||||
std::array<uint16_t, N> m_column_ix;
|
||||
};
|
||||
|
||||
template<typename Category>
|
||||
/**
|
||||
* @brief Implementation of an iterator that returns
|
||||
* only row_handles
|
||||
*
|
||||
* @tparam Category The category for this iterator
|
||||
*/
|
||||
template <typename Category>
|
||||
class iterator_impl<Category>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
|
||||
template <typename, typename...>
|
||||
friend class iterator_impl;
|
||||
|
||||
@@ -195,7 +229,7 @@ class iterator_impl<Category>
|
||||
template <typename C2>
|
||||
iterator_impl(const iterator_impl<C2> &rhs)
|
||||
: m_category(rhs.m_category)
|
||||
, m_current(const_cast<row_type*>(rhs.m_current))
|
||||
, m_current(const_cast<row_type *>(rhs.m_current))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -223,7 +257,7 @@ class iterator_impl<Category>
|
||||
|
||||
reference operator*()
|
||||
{
|
||||
return {*m_category, *m_current};
|
||||
return { *m_category, *m_current };
|
||||
}
|
||||
|
||||
pointer operator->()
|
||||
@@ -271,16 +305,26 @@ class iterator_impl<Category>
|
||||
return m_current != rhs.m_current;
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
private:
|
||||
category_type *m_category = nullptr;
|
||||
row_type *m_current = nullptr;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Implementation of an iterator that can return
|
||||
* a single value.
|
||||
*
|
||||
* @tparam Category The category for this iterator
|
||||
* @tparam T The type this iterator can be dereferenced to
|
||||
*/
|
||||
|
||||
template<typename Category, typename T>
|
||||
template <typename Category, typename T>
|
||||
class iterator_impl<Category, T>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
template <typename, typename...>
|
||||
friend class iterator_impl;
|
||||
|
||||
@@ -390,12 +434,14 @@ class iterator_impl<Category, T>
|
||||
return m_current != rhs.m_current;
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
private:
|
||||
value_type get() const
|
||||
{
|
||||
if (m_current != nullptr)
|
||||
{
|
||||
row_handle rh{*m_category, *m_current};
|
||||
row_handle rh{ *m_category, *m_current };
|
||||
return rh[m_column_ix].template as<T>();
|
||||
}
|
||||
|
||||
@@ -411,10 +457,23 @@ class iterator_impl<Category, T>
|
||||
// --------------------------------------------------------------------
|
||||
// iterator proxy
|
||||
|
||||
/**
|
||||
* @brief An iterator_proxy is used as a result type for methods that
|
||||
* return a range of values you want to iterate over.
|
||||
*
|
||||
* E.g. the class cif::category contains the method cif::category::rows()
|
||||
* that returns an iterator_proxy that allows you to iterate over
|
||||
* all the rows in the category.
|
||||
*
|
||||
* @tparam Category The category for the iterators
|
||||
* @tparam Ts The types the iterators return. See class: iterator
|
||||
*/
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
class iterator_proxy
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
static constexpr const size_t N = sizeof...(Ts);
|
||||
|
||||
using category_type = Category;
|
||||
@@ -431,21 +490,21 @@ class iterator_proxy
|
||||
|
||||
iterator_proxy(const iterator_proxy &) = delete;
|
||||
iterator_proxy &operator=(const iterator_proxy &) = delete;
|
||||
/** @endcond */
|
||||
|
||||
iterator begin() const { return iterator(m_begin, m_column_ix); }
|
||||
iterator end() const { return iterator(m_end, m_column_ix); }
|
||||
iterator begin() const { return iterator(m_begin, m_column_ix); } ///< Return the iterator pointing to the first row
|
||||
iterator end() const { return iterator(m_end, m_column_ix); } ///< Return the iterator pointing past the last row
|
||||
|
||||
bool empty() const { return m_begin == m_end; }
|
||||
|
||||
explicit operator bool() const { return not empty(); }
|
||||
|
||||
size_t size() const { return std::distance(begin(), end()); }
|
||||
bool empty() const { return m_begin == m_end; } ///< Return true if the range is empty
|
||||
explicit operator bool() const { return not empty(); } ///< Easy way to detect if the range is empty
|
||||
size_t size() const { return std::distance(begin(), end()); } ///< Return size of the range
|
||||
|
||||
// row front() { return *begin(); }
|
||||
// row back() { return *(std::prev(end())); }
|
||||
|
||||
category_type &category() const { return *m_category; }
|
||||
category_type &category() const { return *m_category; } ///< Return the category the iterator belong to
|
||||
|
||||
/** swap */
|
||||
void swap(iterator_proxy &rhs)
|
||||
{
|
||||
std::swap(m_category, rhs.m_category);
|
||||
@@ -463,10 +522,20 @@ class iterator_proxy
|
||||
// --------------------------------------------------------------------
|
||||
// conditional iterator proxy
|
||||
|
||||
/**
|
||||
* @brief A conditional iterator proxy is similar to an iterator_proxy
|
||||
* in that it can be used to return a range of rows you can iterate over.
|
||||
* In the case of an conditional_iterator_proxy a cif::condition is used
|
||||
* to filter out only those rows that match the condition.
|
||||
*
|
||||
* @tparam CategoryType The category the iterators belong to
|
||||
* @tparam Ts The types to which the iterators can be dereferenced
|
||||
*/
|
||||
template <typename CategoryType, typename... Ts>
|
||||
class conditional_iterator_proxy
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
static constexpr const size_t N = sizeof...(Ts);
|
||||
|
||||
using category_type = std::remove_cv_t<CategoryType>;
|
||||
@@ -507,7 +576,7 @@ class conditional_iterator_proxy
|
||||
{
|
||||
if (++mBegin == mEnd)
|
||||
break;
|
||||
|
||||
|
||||
if (m_condition->operator()(mBegin))
|
||||
break;
|
||||
}
|
||||
@@ -549,20 +618,21 @@ class conditional_iterator_proxy
|
||||
conditional_iterator_proxy(const conditional_iterator_proxy &) = delete;
|
||||
conditional_iterator_proxy &operator=(const conditional_iterator_proxy &) = delete;
|
||||
|
||||
iterator begin() const;
|
||||
iterator end() const;
|
||||
/** @endcond */
|
||||
|
||||
bool empty() const;
|
||||
iterator begin() const; ///< Return the iterator pointing to the first row
|
||||
iterator end() const; ///< Return the iterator pointing past the last row
|
||||
|
||||
explicit operator bool() const { return not empty(); }
|
||||
bool empty() const; ///< Return true if the range is empty
|
||||
explicit operator bool() const { return not empty(); } ///< Easy way to detect if the range is empty
|
||||
size_t size() const { return std::distance(begin(), end()); } ///< Return size of the range
|
||||
|
||||
size_t size() const { return std::distance(begin(), end()); }
|
||||
|
||||
row_handle front() { return *begin(); }
|
||||
row_handle front() { return *begin(); } ///< Return reference to the first row
|
||||
// row_handle back() { return *begin(); }
|
||||
|
||||
CategoryType &category() const { return *m_cat; }
|
||||
CategoryType &category() const { return *m_cat; } ///< Category the iterators belong to
|
||||
|
||||
/** swap */
|
||||
void swap(conditional_iterator_proxy &rhs);
|
||||
|
||||
private:
|
||||
@@ -574,6 +644,7 @@ class conditional_iterator_proxy
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** @cond */
|
||||
template <typename Category, typename... Ts>
|
||||
iterator_proxy<Category, Ts...>::iterator_proxy(Category &cat, row_iterator pos, char const *const columns[N])
|
||||
: m_category(&cat)
|
||||
@@ -607,6 +678,8 @@ conditional_iterator_proxy<Category, Ts...>::conditional_iterator_impl::conditio
|
||||
, mEnd(cat.end(), cix)
|
||||
, m_condition(&cond)
|
||||
{
|
||||
if (m_condition == nullptr or m_condition->empty())
|
||||
mBegin = mEnd;
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
@@ -631,10 +704,15 @@ conditional_iterator_proxy<Category, Ts...>::conditional_iterator_proxy(Category
|
||||
{
|
||||
static_assert(sizeof...(Ts) == sizeof...(Ns), "Number of column names should be equal to number of requested value types");
|
||||
|
||||
m_condition.prepare(cat);
|
||||
if (m_condition)
|
||||
{
|
||||
m_condition.prepare(cat);
|
||||
|
||||
while (mCBegin != mCEnd and not m_condition(*mCBegin))
|
||||
++mCBegin;
|
||||
while (mCBegin != mCEnd and not m_condition(*mCBegin))
|
||||
++mCBegin;
|
||||
}
|
||||
else
|
||||
mCBegin == mCEnd;
|
||||
|
||||
uint16_t i = 0;
|
||||
((mCix[i++] = m_cat->get_column_ix(names)), ...);
|
||||
@@ -675,4 +753,6 @@ void conditional_iterator_proxy<Category, Ts...>::swap(conditional_iterator_prox
|
||||
std::swap(mCix, rhs.mCix);
|
||||
}
|
||||
|
||||
/** @endcond */
|
||||
|
||||
} // namespace cif
|
||||
@@ -35,30 +35,48 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @file matrix.hpp
|
||||
*
|
||||
* Some basic matrix operations and classes to hold matrices.
|
||||
*
|
||||
* We're using expression templates for optimal performance.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
// --------------------------------------------------------------------
|
||||
// We're using expression templates here
|
||||
|
||||
/**
|
||||
* @brief Base for the matrix expression templates
|
||||
* This all uses the Curiously recurring template pattern
|
||||
*
|
||||
* @tparam M The type of the derived class
|
||||
*/
|
||||
template <typename M>
|
||||
class matrix_expression
|
||||
{
|
||||
public:
|
||||
constexpr size_t dim_m() const { return static_cast<const M &>(*this).dim_m(); }
|
||||
constexpr size_t dim_n() const { return static_cast<const M &>(*this).dim_n(); }
|
||||
constexpr size_t dim_m() const { return static_cast<const M &>(*this).dim_m(); } ///< Return the size (dimension) in direction m
|
||||
constexpr size_t dim_n() const { return static_cast<const M &>(*this).dim_n(); } ///< Return the size (dimension) in direction n
|
||||
|
||||
constexpr bool empty() const { return dim_m() == 0 or dim_n() == 0; }
|
||||
constexpr bool empty() const { return dim_m() == 0 or dim_n() == 0; } ///< Convenient way to test for empty matrices
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr auto &operator()(size_t i, size_t j)
|
||||
{
|
||||
return static_cast<M &>(*this).operator()(i, j);
|
||||
}
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(size_t i, size_t j) const
|
||||
{
|
||||
return static_cast<const M &>(*this).operator()(i, j);
|
||||
}
|
||||
|
||||
/** Swap the contents of rows @a r1 and @a r2 */
|
||||
void swap_row(size_t r1, size_t r2)
|
||||
{
|
||||
for (size_t c = 0; c < dim_m(); ++c)
|
||||
@@ -69,6 +87,7 @@ class matrix_expression
|
||||
}
|
||||
}
|
||||
|
||||
/** Swap the contents of columns @a c1 and @a c2 */
|
||||
void swap_col(size_t c1, size_t c2)
|
||||
{
|
||||
for (size_t r = 0; r < dim_n(); ++r)
|
||||
@@ -79,6 +98,7 @@ class matrix_expression
|
||||
}
|
||||
}
|
||||
|
||||
/** write the matrix @a m to std::ostream @a os */
|
||||
friend std::ostream &operator<<(std::ostream &os, const matrix_expression &m)
|
||||
{
|
||||
os << '[';
|
||||
@@ -107,15 +127,29 @@ class matrix_expression
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
// element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
|
||||
/**
|
||||
* @brief Storage class implementation of matrix_expression.
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
|
||||
template <typename F = float>
|
||||
class matrix : public matrix_expression<matrix<F>>
|
||||
{
|
||||
public:
|
||||
/** The value type */
|
||||
using value_type = F;
|
||||
|
||||
/**
|
||||
* @brief Copy construct a new matrix object using @a m
|
||||
*
|
||||
* @tparam M2 Type of @a m
|
||||
* @param m The matrix expression to copy values from
|
||||
*/
|
||||
template <typename M2>
|
||||
matrix(const matrix_expression<M2> &m)
|
||||
: m_m(m.dim_m())
|
||||
@@ -129,6 +163,14 @@ class matrix : public matrix_expression<matrix<F>>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new matrix object with dimension @a m and @a n
|
||||
* setting the values to @a v
|
||||
*
|
||||
* @param m Requested dimension M
|
||||
* @param n Requested dimension N
|
||||
* @param v Value to store in each element
|
||||
*/
|
||||
matrix(size_t m, size_t n, value_type v = 0)
|
||||
: m_m(m)
|
||||
, m_n(n)
|
||||
@@ -137,15 +179,18 @@ class matrix : public matrix_expression<matrix<F>>
|
||||
std::fill(m_data.begin(), m_data.end(), v);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
matrix() = default;
|
||||
matrix(matrix &&m) = default;
|
||||
matrix(const matrix &m) = default;
|
||||
matrix &operator=(matrix &&m) = default;
|
||||
matrix &operator=(const matrix &m) = default;
|
||||
/** @endcond */
|
||||
|
||||
constexpr size_t dim_m() const { return m_m; }
|
||||
constexpr size_t dim_n() const { return m_n; }
|
||||
constexpr size_t dim_m() const { return m_m; } ///< Return dimension m
|
||||
constexpr size_t dim_n() const { return m_n; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(size_t i, size_t j) const
|
||||
{
|
||||
assert(i < m_m);
|
||||
@@ -153,6 +198,7 @@ class matrix : public matrix_expression<matrix<F>>
|
||||
return m_data[i * m_n + j];
|
||||
}
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr value_type &operator()(size_t i, size_t j)
|
||||
{
|
||||
assert(i < m_m);
|
||||
@@ -168,14 +214,27 @@ class matrix : public matrix_expression<matrix<F>>
|
||||
// --------------------------------------------------------------------
|
||||
// special case, 3x3 matrix
|
||||
|
||||
/**
|
||||
* @brief Storage class implementation of matrix_expression
|
||||
* with compile time fixed size.
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
|
||||
template <typename F, size_t M, size_t N>
|
||||
class matrix_fixed : public matrix_expression<matrix_fixed<F, M, N>>
|
||||
{
|
||||
public:
|
||||
/** The value type */
|
||||
using value_type = F;
|
||||
|
||||
/** The storage size */
|
||||
static constexpr size_t kSize = M * N;
|
||||
|
||||
/** Copy constructor */
|
||||
template <typename M2>
|
||||
matrix_fixed(const M2 &m)
|
||||
{
|
||||
@@ -187,21 +246,26 @@ class matrix_fixed : public matrix_expression<matrix_fixed<F, M, N>>
|
||||
}
|
||||
}
|
||||
|
||||
/** default constructor */
|
||||
matrix_fixed(value_type v = 0)
|
||||
{
|
||||
m_data.fill(v);
|
||||
}
|
||||
|
||||
/** Alternate constructor taking an array of values to store */
|
||||
matrix_fixed(const F (&v)[kSize])
|
||||
{
|
||||
fill(v, std::make_index_sequence<kSize>{});
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
matrix_fixed(matrix_fixed &&m) = default;
|
||||
matrix_fixed(const matrix_fixed &m) = default;
|
||||
matrix_fixed &operator=(matrix_fixed &&m) = default;
|
||||
matrix_fixed &operator=(const matrix_fixed &m) = default;
|
||||
/** @endcond */
|
||||
|
||||
/** Store the values in @a a in the matrix */
|
||||
template<size_t... Ixs>
|
||||
matrix_fixed& fill(const F (&a)[kSize], std::index_sequence<Ixs...>)
|
||||
{
|
||||
@@ -209,9 +273,10 @@ class matrix_fixed : public matrix_expression<matrix_fixed<F, M, N>>
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr size_t dim_m() const { return M; }
|
||||
constexpr size_t dim_n() const { return N; }
|
||||
constexpr size_t dim_m() const { return M; } ///< Return dimension m
|
||||
constexpr size_t dim_n() const { return N; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(size_t i, size_t j) const
|
||||
{
|
||||
assert(i < M);
|
||||
@@ -219,6 +284,7 @@ class matrix_fixed : public matrix_expression<matrix_fixed<F, M, N>>
|
||||
return m_data[i * N + j];
|
||||
}
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr value_type &operator()(size_t i, size_t j)
|
||||
{
|
||||
assert(i < M);
|
||||
@@ -230,20 +296,32 @@ class matrix_fixed : public matrix_expression<matrix_fixed<F, M, N>>
|
||||
std::array<value_type, M * N> m_data;
|
||||
};
|
||||
|
||||
/** typedef of a fixed matrix of size 3x3 */
|
||||
template <typename F>
|
||||
using matrix3x3 = matrix_fixed<F, 3, 3>;
|
||||
|
||||
/** typedef of a fixed matrix of size 4x4 */
|
||||
template <typename F>
|
||||
using matrix4x4 = matrix_fixed<F, 4, 4>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Storage class implementation of symmetric matrix_expression
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
template <typename F = float>
|
||||
class symmetric_matrix : public matrix_expression<symmetric_matrix<F>>
|
||||
{
|
||||
public:
|
||||
/** The value type */
|
||||
using value_type = F;
|
||||
|
||||
/** constructor for a matrix of size @a n x @a n elements with value @a v */
|
||||
symmetric_matrix(size_t n, value_type v = 0)
|
||||
: m_n(n)
|
||||
, m_data((m_n * (m_n + 1)) / 2)
|
||||
@@ -251,15 +329,18 @@ class symmetric_matrix : public matrix_expression<symmetric_matrix<F>>
|
||||
std::fill(m_data.begin(), m_data.end(), v);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
symmetric_matrix() = default;
|
||||
symmetric_matrix(symmetric_matrix &&m) = default;
|
||||
symmetric_matrix(const symmetric_matrix &m) = default;
|
||||
symmetric_matrix &operator=(symmetric_matrix &&m) = default;
|
||||
symmetric_matrix &operator=(const symmetric_matrix &m) = default;
|
||||
/** @endcond */
|
||||
|
||||
constexpr size_t dim_m() const { return m_n; }
|
||||
constexpr size_t dim_n() const { return m_n; }
|
||||
constexpr size_t dim_m() const { return m_n; } ///< Return dimension m
|
||||
constexpr size_t dim_n() const { return m_n; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(size_t i, size_t j) const
|
||||
{
|
||||
return i < j
|
||||
@@ -267,6 +348,7 @@ class symmetric_matrix : public matrix_expression<symmetric_matrix<F>>
|
||||
: m_data[(i * (i + 1)) / 2 + j];
|
||||
}
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr value_type &operator()(size_t i, size_t j)
|
||||
{
|
||||
if (i > j)
|
||||
@@ -282,25 +364,39 @@ class symmetric_matrix : public matrix_expression<symmetric_matrix<F>>
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Storage class implementation of symmetric matrix_expression
|
||||
* with compile time fixed size.
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
template <typename F, size_t M>
|
||||
class symmetric_matrix_fixed : public matrix_expression<symmetric_matrix_fixed<F, M>>
|
||||
{
|
||||
public:
|
||||
/** The value type */
|
||||
using value_type = F;
|
||||
|
||||
/** constructor with all elements set to value @a v */
|
||||
symmetric_matrix_fixed(value_type v = 0)
|
||||
{
|
||||
std::fill(m_data.begin(), m_data.end(), v);
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
symmetric_matrix_fixed(symmetric_matrix_fixed &&m) = default;
|
||||
symmetric_matrix_fixed(const symmetric_matrix_fixed &m) = default;
|
||||
symmetric_matrix_fixed &operator=(symmetric_matrix_fixed &&m) = default;
|
||||
symmetric_matrix_fixed &operator=(const symmetric_matrix_fixed &m) = default;
|
||||
/** @endcond */
|
||||
|
||||
constexpr size_t dim_m() const { return M; }
|
||||
constexpr size_t dim_n() const { return M; }
|
||||
constexpr size_t dim_m() const { return M; } ///< Return dimension m
|
||||
constexpr size_t dim_n() const { return M; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(size_t i, size_t j) const
|
||||
{
|
||||
return i < j
|
||||
@@ -308,6 +404,7 @@ class symmetric_matrix_fixed : public matrix_expression<symmetric_matrix_fixed<F
|
||||
: m_data[(i * (i + 1)) / 2 + j];
|
||||
}
|
||||
|
||||
/** Return a reference to element [ @a i, @a j ] */
|
||||
constexpr value_type &operator()(size_t i, size_t j)
|
||||
{
|
||||
if (i > j)
|
||||
@@ -320,28 +417,42 @@ class symmetric_matrix_fixed : public matrix_expression<symmetric_matrix_fixed<F
|
||||
std::array<value_type, (M * (M + 1)) / 2> m_data;
|
||||
};
|
||||
|
||||
/** typedef of a fixed symmetric matrix of size 3x3 */
|
||||
template <typename F>
|
||||
using symmetric_matrix3x3 = symmetric_matrix_fixed<F, 3>;
|
||||
|
||||
/** typedef of a fixed symmetric matrix of size 4x4 */
|
||||
template <typename F>
|
||||
using symmetric_matrix4x4 = symmetric_matrix_fixed<F, 4>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief implementation of symmetric matrix_expression with a value
|
||||
* of 1 for the diagonal values and 0 for all the others.
|
||||
*
|
||||
* @tparam F The type of the stored values
|
||||
*
|
||||
* matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
|
||||
* element m i,j is mapped to [i * n + j] and thus storage is row major
|
||||
*/
|
||||
template <typename F = float>
|
||||
class identity_matrix : public matrix_expression<identity_matrix<F>>
|
||||
{
|
||||
public:
|
||||
/** the value type */
|
||||
using value_type = F;
|
||||
|
||||
/** constructor taking a dimension @a n */
|
||||
identity_matrix(size_t n)
|
||||
: m_n(n)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr size_t dim_m() const { return m_n; }
|
||||
constexpr size_t dim_n() const { return m_n; }
|
||||
constexpr size_t dim_m() const { return m_n; } ///< Return dimension m
|
||||
constexpr size_t dim_n() const { return m_n; } ///< Return dimension n
|
||||
|
||||
/** Return the value of element [ @a i, @a j ] */
|
||||
constexpr value_type operator()(size_t i, size_t j) const
|
||||
{
|
||||
return static_cast<value_type>(i == j ? 1 : 0);
|
||||
@@ -354,10 +465,17 @@ class identity_matrix : public matrix_expression<identity_matrix<F>>
|
||||
// --------------------------------------------------------------------
|
||||
// matrix functions, implemented as expression templates
|
||||
|
||||
/**
|
||||
* @brief Implementation of a substraction operation as a matrix expression
|
||||
*
|
||||
* @tparam M1 Type of matrix 1
|
||||
* @tparam M2 Type of matrix 2
|
||||
*/
|
||||
template <typename M1, typename M2>
|
||||
class matrix_subtraction : public matrix_expression<matrix_subtraction<M1, M2>>
|
||||
{
|
||||
public:
|
||||
/** constructor */
|
||||
matrix_subtraction(const M1 &m1, const M2 &m2)
|
||||
: m_m1(m1)
|
||||
, m_m2(m2)
|
||||
@@ -366,9 +484,10 @@ class matrix_subtraction : public matrix_expression<matrix_subtraction<M1, M2>>
|
||||
assert(m_m1.dim_n() == m_m2.dim_n());
|
||||
}
|
||||
|
||||
constexpr size_t dim_m() const { return m_m1.dim_m(); }
|
||||
constexpr size_t dim_n() const { return m_m1.dim_n(); }
|
||||
constexpr size_t dim_m() const { return m_m1.dim_m(); } ///< Return dimension m
|
||||
constexpr size_t dim_n() const { return m_m1.dim_n(); } ///< Return dimension n
|
||||
|
||||
/** Access to the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(size_t i, size_t j) const
|
||||
{
|
||||
return m_m1(i, j) - m_m2(i, j);
|
||||
@@ -379,16 +498,24 @@ class matrix_subtraction : public matrix_expression<matrix_subtraction<M1, M2>>
|
||||
const M2 &m_m2;
|
||||
};
|
||||
|
||||
/** operator to subtract two matrices and return a matrix expression */
|
||||
template <typename M1, typename M2>
|
||||
auto operator-(const matrix_expression<M1> &m1, const matrix_expression<M2> &m2)
|
||||
{
|
||||
return matrix_subtraction(m1, m2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Implementation of a multiplication operation as a matrix expression
|
||||
*
|
||||
* @tparam M1 Type of matrix 1
|
||||
* @tparam M2 Type of matrix 2
|
||||
*/
|
||||
template <typename M1, typename M2>
|
||||
class matrix_matrix_multiplication : public matrix_expression<matrix_matrix_multiplication<M1, M2>>
|
||||
{
|
||||
public:
|
||||
/** constructor */
|
||||
matrix_matrix_multiplication(const M1 &m1, const M2 &m2)
|
||||
: m_m1(m1)
|
||||
, m_m2(m2)
|
||||
@@ -396,9 +523,10 @@ class matrix_matrix_multiplication : public matrix_expression<matrix_matrix_mult
|
||||
assert(m1.dim_m() == m2.dim_n());
|
||||
}
|
||||
|
||||
constexpr size_t dim_m() const { return m_m1.dim_m(); }
|
||||
constexpr size_t dim_n() const { return m_m1.dim_n(); }
|
||||
constexpr size_t dim_m() const { return m_m1.dim_m(); } ///< Return dimension m
|
||||
constexpr size_t dim_n() const { return m_m1.dim_n(); } ///< Return dimension n
|
||||
|
||||
/** Access to the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(size_t i, size_t j) const
|
||||
{
|
||||
using value_type = decltype(m_m1(0, 0));
|
||||
@@ -416,21 +544,30 @@ class matrix_matrix_multiplication : public matrix_expression<matrix_matrix_mult
|
||||
const M2 &m_m2;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Implementation of a multiplication operation of a matrix and a scalar value as a matrix expression
|
||||
*
|
||||
* @tparam M1 Type of matrix
|
||||
* @tparam M2 Type of scalar value
|
||||
*/
|
||||
template <typename M, typename T>
|
||||
class matrix_scalar_multiplication : public matrix_expression<matrix_scalar_multiplication<M, T>>
|
||||
{
|
||||
public:
|
||||
/** value type */
|
||||
using value_type = T;
|
||||
|
||||
/** constructor */
|
||||
matrix_scalar_multiplication(const M &m, value_type v)
|
||||
: m_m(m)
|
||||
, m_v(v)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr size_t dim_m() const { return m_m.dim_m(); }
|
||||
constexpr size_t dim_n() const { return m_m.dim_n(); }
|
||||
constexpr size_t dim_m() const { return m_m.dim_m(); } ///< Return dimension m
|
||||
constexpr size_t dim_n() const { return m_m.dim_n(); } ///< Return dimension n
|
||||
|
||||
/** Access to the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(size_t i, size_t j) const
|
||||
{
|
||||
return m_m(i, j) * m_v;
|
||||
@@ -441,12 +578,14 @@ class matrix_scalar_multiplication : public matrix_expression<matrix_scalar_mult
|
||||
value_type m_v;
|
||||
};
|
||||
|
||||
/** First implementation of operator*, enabled if the second parameter is a scalar */
|
||||
template <typename M1, typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
||||
auto operator*(const matrix_expression<M1> &m, T v)
|
||||
{
|
||||
return matrix_scalar_multiplication(m, v);
|
||||
}
|
||||
|
||||
/** First implementation of operator*, enabled if the second parameter is not a scalar and thus must be a matrix, right? */
|
||||
template <typename M1, typename M2, std::enable_if_t<not std::is_floating_point_v<M2>, int> = 0>
|
||||
auto operator*(const matrix_expression<M1> &m1, const matrix_expression<M2> &m2)
|
||||
{
|
||||
@@ -455,9 +594,14 @@ auto operator*(const matrix_expression<M1> &m1, const matrix_expression<M2> &m2)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** Generic routine to calculate the determinant of a matrix
|
||||
*
|
||||
* @note This is currently only implemented for fixed matrices of size 3x3
|
||||
*/
|
||||
template <typename M>
|
||||
auto determinant(const M &m);
|
||||
|
||||
/** Implementation of the determinant function for fixed size matrices of size 3x3 */
|
||||
template <typename F = float>
|
||||
auto determinant(const matrix3x3<F> &m)
|
||||
{
|
||||
@@ -466,9 +610,14 @@ auto determinant(const matrix3x3<F> &m)
|
||||
m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0)));
|
||||
}
|
||||
|
||||
/** Generic routine to calculate the inverse of a matrix
|
||||
*
|
||||
* @note This is currently only implemented for fixed matrices of size 3x3
|
||||
*/
|
||||
template <typename M>
|
||||
M inverse(const M &m);
|
||||
|
||||
/** Implementation of the inverse function for fixed size matrices of size 3x3 */
|
||||
template <typename F = float>
|
||||
matrix3x3<F> inverse(const matrix3x3<F> &m)
|
||||
{
|
||||
@@ -491,18 +640,25 @@ matrix3x3<F> inverse(const matrix3x3<F> &m)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Implementation of a cofactor calculation as a matrix expression
|
||||
*
|
||||
* @tparam M Type of matrix
|
||||
*/
|
||||
template <typename M>
|
||||
class matrix_cofactors : public matrix_expression<matrix_cofactors<M>>
|
||||
{
|
||||
public:
|
||||
/** constructor */
|
||||
matrix_cofactors(const M &m)
|
||||
: m_m(m)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr size_t dim_m() const { return m_m.dim_m(); }
|
||||
constexpr size_t dim_n() const { return m_m.dim_n(); }
|
||||
constexpr size_t dim_m() const { return m_m.dim_m(); } ///< Return dimension m
|
||||
constexpr size_t dim_n() const { return m_m.dim_n(); } ///< Return dimension n
|
||||
|
||||
/** Access to the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(size_t i, size_t j) const
|
||||
{
|
||||
const size_t ixs[4][3] = {
|
||||
|
||||
@@ -27,13 +27,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "cif++/atom_type.hpp"
|
||||
#include "cif++/datablock.hpp"
|
||||
#include "cif++/point.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
|
||||
#if __cpp_lib_format
|
||||
#include <format>
|
||||
#endif
|
||||
|
||||
/** @file model.hpp
|
||||
*
|
||||
* This file contains code to work with models of molecules.
|
||||
*
|
||||
* The classes available encapsulate the real world concepts of
|
||||
* atoms, residues, monomers, polymers and everything is then
|
||||
* bound together in a structure.
|
||||
*
|
||||
* This code is not finished yet, ideally it would be a high
|
||||
* level interface to manipulate macro molecular structures
|
||||
* and an attempt has been made to start work on this. But
|
||||
* there's still a lot that needs to be implemented.
|
||||
*
|
||||
* However, the code that is here is still useful in
|
||||
* manipulating the underlying mmCIF data model.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif::mm
|
||||
{
|
||||
|
||||
@@ -45,9 +66,24 @@ class structure;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The class atom encapsulates the data in _atom_site and
|
||||
* _atom_site_anisotrop
|
||||
*
|
||||
* The class atom is a kind of flyweight class. It can be copied
|
||||
* with low overhead. All data is stored in the underlying mmCIF
|
||||
* categories but some very often used fields are cached in the
|
||||
* impl.
|
||||
*
|
||||
* It is also possible to have symmetry copies of atoms. They
|
||||
* share the same data in the cif::category but their location
|
||||
* differs by using a symmetry operator.
|
||||
*/
|
||||
|
||||
class atom
|
||||
{
|
||||
private:
|
||||
/** @cond */
|
||||
struct atom_impl : public std::enable_shared_from_this<atom_impl>
|
||||
{
|
||||
atom_impl(const datablock &db, std::string_view id)
|
||||
@@ -90,24 +126,24 @@ class atom
|
||||
|
||||
row_handle row()
|
||||
{
|
||||
return m_cat[{{"id", m_id}}];
|
||||
return m_cat[{ { "id", m_id } }];
|
||||
}
|
||||
|
||||
const row_handle row() const
|
||||
{
|
||||
return m_cat[{{"id", m_id}}];
|
||||
return m_cat[{ { "id", m_id } }];
|
||||
}
|
||||
|
||||
row_handle row_aniso()
|
||||
{
|
||||
auto cat = m_db.get("atom_site_anisotrop");
|
||||
return cat ? cat->operator[]({ {"id", m_id} }) : row_handle{};
|
||||
return cat ? cat->operator[]({ { "id", m_id } }) : row_handle{};
|
||||
}
|
||||
|
||||
const row_handle row_aniso() const
|
||||
{
|
||||
auto cat = m_db.get("atom_site_anisotrop");
|
||||
return cat ? cat->operator[]({ {"id", m_id} }) : row_handle{};
|
||||
return cat ? cat->operator[]({ { "id", m_id } }) : row_handle{};
|
||||
}
|
||||
|
||||
const datablock &m_db;
|
||||
@@ -116,46 +152,62 @@ class atom
|
||||
point m_location;
|
||||
std::string m_symop = "1_555";
|
||||
};
|
||||
/** @endcond */
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new, empty atom object
|
||||
*/
|
||||
atom() {}
|
||||
|
||||
/**
|
||||
* @brief Construct a new atom object using @a impl as impl
|
||||
*
|
||||
* @param impl The implementation objectt
|
||||
*/
|
||||
atom(std::shared_ptr<atom_impl> impl)
|
||||
: m_impl(impl)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Copy construct a new atom object
|
||||
*/
|
||||
atom(const atom &rhs)
|
||||
: m_impl(rhs.m_impl)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a new atom object based on a cif::row
|
||||
*
|
||||
* @param db The datablock where the _atom_site category resides
|
||||
* @param row The row containing the data for this atom
|
||||
*/
|
||||
atom(const datablock &db, const row_handle &row)
|
||||
: atom(std::make_shared<atom_impl>(db, row["id"].as<std::string>()))
|
||||
{
|
||||
}
|
||||
|
||||
// a special constructor to create symmetry copies
|
||||
/**
|
||||
* @brief A special constructor to create symmetry copies
|
||||
*
|
||||
* @param rhs The original atom to copy
|
||||
* @param symmmetry_location The symmetry location
|
||||
* @param symmetry_operation The symmetry operator used
|
||||
*/
|
||||
atom(const atom &rhs, const point &symmmetry_location, const std::string &symmetry_operation)
|
||||
: atom(std::make_shared<atom_impl>(*rhs.m_impl, symmmetry_location, symmetry_operation))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief To quickly test if the atom has data
|
||||
explicit operator bool() const { return (bool)m_impl; }
|
||||
|
||||
// // return a copy of this atom, with data copied instead of referenced
|
||||
// atom clone() const
|
||||
// {
|
||||
// auto copy = std::make_shared<atom_impl>(*m_impl);
|
||||
// copy->mClone = true;
|
||||
// return atom(copy);
|
||||
// }
|
||||
|
||||
/// \brief Copy assignement operator
|
||||
atom &operator=(const atom &rhs) = default;
|
||||
|
||||
// template <typename T>
|
||||
// T get_property(const std::string_view name) const;
|
||||
|
||||
/// \brief Return the field named @a name in the _atom_site category for this atom
|
||||
std::string get_property(std::string_view name) const
|
||||
{
|
||||
if (not m_impl)
|
||||
@@ -163,6 +215,7 @@ class atom
|
||||
return m_impl->get_property(name);
|
||||
}
|
||||
|
||||
/// \brief Return the field named @a name in the _atom_site category for this atom cast to an int
|
||||
int get_property_int(std::string_view name) const
|
||||
{
|
||||
if (not m_impl)
|
||||
@@ -170,6 +223,7 @@ class atom
|
||||
return m_impl->get_property_int(name);
|
||||
}
|
||||
|
||||
/// \brief Return the field named @a name in the _atom_site category for this atom cast to a float
|
||||
float get_property_float(std::string_view name) const
|
||||
{
|
||||
if (not m_impl)
|
||||
@@ -177,6 +231,7 @@ class atom
|
||||
return m_impl->get_property_float(name);
|
||||
}
|
||||
|
||||
/// \brief Set value for the field named @a name in the _atom_site category to @a value
|
||||
void set_property(const std::string_view name, const std::string &value)
|
||||
{
|
||||
if (not m_impl)
|
||||
@@ -184,17 +239,27 @@ class atom
|
||||
m_impl->set_property(name, value);
|
||||
}
|
||||
|
||||
/// \brief Set value for the field named @a name in the _atom_site category to @a value
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
|
||||
void set_property(const std::string_view name, const T &value)
|
||||
{
|
||||
set_property(name, std::to_string(value));
|
||||
}
|
||||
|
||||
/** Return the ID of the _atom_site record.
|
||||
*
|
||||
* @note Although I've never seen anything other than integers,
|
||||
* the standard says this should be a string and so we use that.
|
||||
*/
|
||||
const std::string &id() const { return impl().m_id; }
|
||||
|
||||
/// \brief Return the type of the atom
|
||||
cif::atom_type get_type() const { return atom_type_traits(get_property("type_symbol")).type(); }
|
||||
|
||||
/// \brief Return the cached location of this atom
|
||||
point get_location() const { return impl().m_location; }
|
||||
|
||||
/// \brief Set the location of this atom, will set both the cached data as well as the data in the underlying _atom_site category
|
||||
void set_location(point p)
|
||||
{
|
||||
if (not m_impl)
|
||||
@@ -243,53 +308,57 @@ class atom
|
||||
set_location(loc);
|
||||
}
|
||||
|
||||
// for direct access to underlying data, be careful!
|
||||
/// for direct access to underlying data, be careful!
|
||||
const row_handle get_row() const { return impl().row(); }
|
||||
|
||||
/// for direct access to underlying data, be careful!
|
||||
const row_handle get_row_aniso() const { return impl().row_aniso(); }
|
||||
|
||||
/// Return if the atom is actually a symmetry copy or the original one
|
||||
bool is_symmetry_copy() const { return impl().m_symop != "1_555"; }
|
||||
|
||||
/// Return the symmetry operator used
|
||||
std::string symmetry() const { return impl().m_symop; }
|
||||
|
||||
// const compound &compound() const;
|
||||
|
||||
/// Return true if this atom is part of a water molecule
|
||||
bool is_water() const
|
||||
{
|
||||
auto comp_id = get_label_comp_id();
|
||||
return comp_id == "HOH" or comp_id == "H2O" or comp_id == "WAT";
|
||||
}
|
||||
|
||||
/// Return the charge
|
||||
int get_charge() const { return impl().get_charge(); }
|
||||
|
||||
// float uIso() const;
|
||||
// bool getAnisoU(float anisou[6]) const { return impl().getAnisoU(anisou); }
|
||||
|
||||
/// Return the occupancy
|
||||
float get_occupancy() const { return get_property_float("occupancy"); }
|
||||
|
||||
// specifications
|
||||
|
||||
std::string get_label_asym_id() const { return get_property("label_asym_id"); }
|
||||
int get_label_seq_id() const { return get_property_int("label_seq_id"); }
|
||||
std::string get_label_atom_id() const { return get_property("label_atom_id"); }
|
||||
std::string get_label_alt_id() const { return get_property("label_alt_id"); }
|
||||
std::string get_label_comp_id() const { return get_property("label_comp_id"); }
|
||||
std::string get_label_entity_id() const { return get_property("label_entity_id"); }
|
||||
std::string get_label_asym_id() const { return get_property("label_asym_id"); } ///< Return the label_asym_id property
|
||||
int get_label_seq_id() const { return get_property_int("label_seq_id"); } ///< Return the label_seq_id property
|
||||
std::string get_label_atom_id() const { return get_property("label_atom_id"); } ///< Return the label_atom_id property
|
||||
std::string get_label_alt_id() const { return get_property("label_alt_id"); } ///< Return the label_alt_id property
|
||||
std::string get_label_comp_id() const { return get_property("label_comp_id"); } ///< Return the label_comp_id property
|
||||
std::string get_label_entity_id() const { return get_property("label_entity_id"); } ///< Return the label_entity_id property
|
||||
|
||||
std::string get_auth_asym_id() const { return get_property("auth_asym_id"); }
|
||||
std::string get_auth_seq_id() const { return get_property("auth_seq_id"); }
|
||||
std::string get_auth_atom_id() const { return get_property("auth_atom_id"); }
|
||||
std::string get_auth_alt_id() const { return get_property("auth_alt_id"); }
|
||||
std::string get_auth_comp_id() const { return get_property("auth_comp_id"); }
|
||||
std::string get_pdb_ins_code() const { return get_property("pdbx_PDB_ins_code"); }
|
||||
std::string get_auth_asym_id() const { return get_property("auth_asym_id"); } ///< Return the auth_asym_id property
|
||||
std::string get_auth_seq_id() const { return get_property("auth_seq_id"); } ///< Return the auth_seq_id property
|
||||
std::string get_auth_atom_id() const { return get_property("auth_atom_id"); } ///< Return the auth_atom_id property
|
||||
std::string get_auth_alt_id() const { return get_property("auth_alt_id"); } ///< Return the auth_alt_id property
|
||||
std::string get_auth_comp_id() const { return get_property("auth_comp_id"); } ///< Return the auth_comp_id property
|
||||
std::string get_pdb_ins_code() const { return get_property("pdbx_PDB_ins_code"); } ///< Return the pdb_ins_code property
|
||||
|
||||
/// Return true if this atom is an alternate
|
||||
bool is_alternate() const { return not get_label_alt_id().empty(); }
|
||||
|
||||
// std::string labelID() const; // label_comp_id + '_' + label_asym_id + '_' + label_seq_id
|
||||
|
||||
/// Convenience method to return a string that might be ID in PDB space
|
||||
std::string pdb_id() const
|
||||
{
|
||||
return get_label_comp_id() + '_' + get_auth_asym_id() + '_' + get_auth_seq_id() + get_pdb_ins_code();
|
||||
}
|
||||
|
||||
/// Compare two atoms
|
||||
bool operator==(const atom &rhs) const
|
||||
{
|
||||
if (m_impl == rhs.m_impl)
|
||||
@@ -301,32 +370,35 @@ class atom
|
||||
return &m_impl->m_db == &rhs.m_impl->m_db and m_impl->m_id == rhs.m_impl->m_id;
|
||||
}
|
||||
|
||||
/// Compare two atoms
|
||||
bool operator!=(const atom &rhs) const
|
||||
{
|
||||
return not operator==(rhs);
|
||||
}
|
||||
|
||||
// // access data in compound for this atom
|
||||
|
||||
// convenience routine
|
||||
/// Is this atom a backbone atom
|
||||
bool is_back_bone() const
|
||||
{
|
||||
auto atomID = get_label_atom_id();
|
||||
return atomID == "N" or atomID == "O" or atomID == "C" or atomID == "CA";
|
||||
}
|
||||
|
||||
/// swap
|
||||
void swap(atom &b)
|
||||
{
|
||||
std::swap(m_impl, b.m_impl);
|
||||
}
|
||||
|
||||
/// Compare this atom with @a b
|
||||
int compare(const atom &b) const { return impl().compare(*b.m_impl); }
|
||||
|
||||
/// Should this atom sort before @a rhs
|
||||
bool operator<(const atom &rhs) const
|
||||
{
|
||||
return compare(rhs) < 0;
|
||||
}
|
||||
|
||||
/// Write the atom to std::ostream @a os
|
||||
friend std::ostream &operator<<(std::ostream &os, const atom &atom);
|
||||
|
||||
private:
|
||||
@@ -342,16 +414,23 @@ class atom
|
||||
std::shared_ptr<atom_impl> m_impl;
|
||||
};
|
||||
|
||||
/** swap */
|
||||
inline void swap(atom &a, atom &b)
|
||||
{
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
/** Calculate the distance between atoms @a and @a b in ångström */
|
||||
inline float distance(const atom &a, const atom &b)
|
||||
{
|
||||
return distance(a.get_location(), b.get_location());
|
||||
}
|
||||
|
||||
/** Calculate the square of the distance between atoms @a and @a b in ångström
|
||||
*
|
||||
* @note Use this whenever possible instead of simply using distance since
|
||||
* this function does not have to calculate a square root which is expensive.
|
||||
*/
|
||||
inline float distance_squared(const atom &a, const atom &b)
|
||||
{
|
||||
return distance_squared(a.get_location(), b.get_location());
|
||||
@@ -359,23 +438,36 @@ inline float distance_squared(const atom &a, const atom &b)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The entity types that can be found in a mmCIF file
|
||||
*
|
||||
*/
|
||||
enum class EntityType
|
||||
{
|
||||
polymer,
|
||||
NonPolymer,
|
||||
Macrolide,
|
||||
Water,
|
||||
Branched
|
||||
Polymer, ///< entity is a polymer
|
||||
NonPolymer, ///< entity is not a polymer
|
||||
Macrolide, ///< entity is a macrolide
|
||||
Water, ///< water in the solvent model
|
||||
Branched ///< entity is branched
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The class residue is a collection of atoms forming a molecule
|
||||
*
|
||||
* This class is used to store ligand e.g. Derived classes are monomer
|
||||
* and sugar.
|
||||
*/
|
||||
|
||||
class residue
|
||||
{
|
||||
public:
|
||||
friend class structure;
|
||||
|
||||
// constructor
|
||||
/**
|
||||
* @brief Construct a new residue object based on key items
|
||||
*/
|
||||
residue(structure &structure, const std::string &compoundID,
|
||||
const std::string &asymID, int seqID,
|
||||
const std::string &authAsymID, const std::string &authSeqID,
|
||||
@@ -390,8 +482,10 @@ class residue
|
||||
{
|
||||
}
|
||||
|
||||
/** Construct a new residue in structure with the atoms in @a atoms */
|
||||
residue(structure &structure, const std::vector<atom> &atoms);
|
||||
|
||||
/** @cond */
|
||||
residue(const residue &rhs) = delete;
|
||||
residue &operator=(const residue &rhs) = delete;
|
||||
|
||||
@@ -399,50 +493,61 @@ class residue
|
||||
residue &operator=(residue &&rhs) = default;
|
||||
|
||||
virtual ~residue() = default;
|
||||
/** @endcond */
|
||||
|
||||
/** Return the entity_id of this residue */
|
||||
std::string get_entity_id() const;
|
||||
|
||||
/** Return the entity type of this residue */
|
||||
EntityType entity_type() const;
|
||||
|
||||
const std::string &get_asym_id() const { return m_asym_id; }
|
||||
int get_seq_id() const { return m_seq_id; }
|
||||
const std::string &get_asym_id() const { return m_asym_id; } ///< Return the asym_id
|
||||
int get_seq_id() const { return m_seq_id; } ///< Return the seq_id
|
||||
|
||||
const std::string get_auth_asym_id() const { return m_auth_asym_id; }
|
||||
const std::string get_auth_seq_id() const { return m_auth_seq_id; }
|
||||
std::string get_pdb_ins_code() const { return m_pdb_ins_code; }
|
||||
const std::string get_auth_asym_id() const { return m_auth_asym_id; } ///< Return the auth_asym_id
|
||||
const std::string get_auth_seq_id() const { return m_auth_seq_id; } ///< Return the auth_seq_id
|
||||
std::string get_pdb_ins_code() const { return m_pdb_ins_code; } ///< Return the pdb_ins_code
|
||||
|
||||
const std::string &get_compound_id() const { return m_compound_id; }
|
||||
void set_compound_id(const std::string &id) { m_compound_id = id; }
|
||||
const std::string &get_compound_id() const { return m_compound_id; } ///< Return the compound_id
|
||||
void set_compound_id(const std::string &id) { m_compound_id = id; } ///< Set the compound_id to @a id
|
||||
|
||||
/** Return the structure this residue belongs to */
|
||||
structure *get_structure() const { return m_structure; }
|
||||
|
||||
// const compound &compound() const;
|
||||
|
||||
/** Return a list of the atoms for this residue */
|
||||
std::vector<atom> &atoms()
|
||||
{
|
||||
return m_atoms;
|
||||
}
|
||||
|
||||
/** Return a const list of the atoms for this residue */
|
||||
const std::vector<atom> &atoms() const
|
||||
{
|
||||
return m_atoms;
|
||||
}
|
||||
|
||||
/** Add atom @a atom to the atoms in this residue */
|
||||
void add_atom(atom &atom);
|
||||
|
||||
/// \brief Unique atoms returns only the atoms without alternates and the first of each alternate atom id.
|
||||
std::vector<atom> unique_atoms() const;
|
||||
|
||||
/// \brief The alt ID used for the unique atoms
|
||||
std::string unique_alt_id() const;
|
||||
|
||||
/// \brief Return the atom with atom_id @a atomID
|
||||
atom get_atom_by_atom_id(const std::string &atomID) const;
|
||||
|
||||
// Is this residue a single entity?
|
||||
bool is_entity() const;
|
||||
bool is_water() const { return m_compound_id == "HOH"; }
|
||||
// bool empty() const { return m_structure == nullptr; }
|
||||
/// \brief Return the list of atoms having ID \a atomID
|
||||
///
|
||||
/// This includes all alternate atoms with this ID
|
||||
/// whereas get_atom_by_atom_id only returns the first unique atom
|
||||
std::vector<atom> get_atoms_by_id(const std::string &atomID) const;
|
||||
|
||||
/// \brief Is this residue a single entity?
|
||||
bool is_entity() const;
|
||||
|
||||
/// \brief Is this residue a water molecule?
|
||||
bool is_water() const { return m_compound_id == "HOH"; }
|
||||
|
||||
/// \brief Return true if this residue has alternate atoms
|
||||
bool has_alternate_atoms() const;
|
||||
|
||||
/// \brief Return the list of unique alt ID's present in this residue
|
||||
@@ -451,14 +556,13 @@ class residue
|
||||
/// \brief Return the list of unique atom ID's
|
||||
std::set<std::string> get_atom_ids() const;
|
||||
|
||||
/// \brief Return the list of atoms having ID \a atomID
|
||||
std::vector<atom> get_atoms_by_id(const std::string &atomID) const;
|
||||
|
||||
// some routines for 3d work
|
||||
/// \brief Return a tuple containing the center location and the radius for the atoms of this residue
|
||||
std::tuple<point, float> center_and_radius() const;
|
||||
|
||||
/// \brief Write the residue @a res to the std::ostream @a os
|
||||
friend std::ostream &operator<<(std::ostream &os, const residue &res);
|
||||
|
||||
/// \brief Return true if this residue is equal to @a rhs
|
||||
bool operator==(const residue &rhs) const
|
||||
{
|
||||
return this == &rhs or (m_structure == rhs.m_structure and
|
||||
@@ -469,6 +573,7 @@ class residue
|
||||
}
|
||||
|
||||
protected:
|
||||
/** @cond */
|
||||
residue() {}
|
||||
|
||||
structure *m_structure = nullptr;
|
||||
@@ -476,45 +581,53 @@ class residue
|
||||
int m_seq_id = 0;
|
||||
std::string m_auth_asym_id, m_auth_seq_id, m_pdb_ins_code;
|
||||
std::vector<atom> m_atoms;
|
||||
/** @endcond */
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// a monomer models a single residue in a protein chain
|
||||
|
||||
/**
|
||||
* @brief a monomer models a single residue in a protein chain
|
||||
*
|
||||
*/
|
||||
|
||||
class monomer : public residue
|
||||
{
|
||||
public:
|
||||
// monomer();
|
||||
monomer(const monomer &rhs) = delete;
|
||||
monomer &operator=(const monomer &rhs) = delete;
|
||||
|
||||
/// \brief Move constructor
|
||||
monomer(monomer &&rhs);
|
||||
|
||||
/// \brief Move assignment operator
|
||||
monomer &operator=(monomer &&rhs);
|
||||
|
||||
/// \brief constructor with actual values
|
||||
monomer(const polymer &polymer, size_t index, int seqID, const std::string &authSeqID,
|
||||
const std::string &pdbInsCode, const std::string &compoundID);
|
||||
|
||||
bool is_first_in_chain() const;
|
||||
bool is_last_in_chain() const;
|
||||
bool is_first_in_chain() const; ///< Return if this residue is the first residue in the chain
|
||||
bool is_last_in_chain() const; ///< Return if this residue is the last residue in the chain
|
||||
|
||||
// convenience
|
||||
bool has_alpha() const;
|
||||
bool has_kappa() const;
|
||||
bool has_alpha() const; ///< Return if a alpha value can be calculated (depends on location in chain)
|
||||
bool has_kappa() const; ///< Return if a kappa value can be calculated (depends on location in chain)
|
||||
|
||||
// Assuming this is really an amino acid...
|
||||
|
||||
float phi() const;
|
||||
float psi() const;
|
||||
float alpha() const;
|
||||
float kappa() const;
|
||||
float tco() const;
|
||||
float omega() const;
|
||||
float phi() const; ///< Return the phi value for this residue
|
||||
float psi() const; ///< Return the psi value for this residue
|
||||
float alpha() const; ///< Return the alpha value for this residue
|
||||
float kappa() const; ///< Return the kappa value for this residue
|
||||
float tco() const; ///< Return the tco value for this residue
|
||||
float omega() const; ///< Return the omega value for this residue
|
||||
|
||||
// torsion angles
|
||||
size_t nr_of_chis() const;
|
||||
float chi(size_t i) const;
|
||||
size_t nr_of_chis() const; ///< Return how many torsion angles can be calculated
|
||||
float chi(size_t i) const; ///< Return torsion angle @a i
|
||||
|
||||
bool is_cis() const;
|
||||
bool is_cis() const; ///< Return true if this residue is in a cis conformation
|
||||
|
||||
/// \brief Returns true if the four atoms C, CA, N and O are present
|
||||
bool is_complete() const;
|
||||
@@ -522,24 +635,38 @@ class monomer : public residue
|
||||
/// \brief Returns true if any of the backbone atoms has an alternate
|
||||
bool has_alternate_backbone_atoms() const;
|
||||
|
||||
atom CAlpha() const { return get_atom_by_atom_id("CA"); }
|
||||
atom C() const { return get_atom_by_atom_id("C"); }
|
||||
atom N() const { return get_atom_by_atom_id("N"); }
|
||||
atom O() const { return get_atom_by_atom_id("O"); }
|
||||
atom H() const { return get_atom_by_atom_id("H"); }
|
||||
atom CAlpha() const { return get_atom_by_atom_id("CA"); } ///< Return the CAlpha atom
|
||||
atom C() const { return get_atom_by_atom_id("C"); } ///< Return the C atom
|
||||
atom N() const { return get_atom_by_atom_id("N"); } ///< Return the N atom
|
||||
atom O() const { return get_atom_by_atom_id("O"); } ///< Return the O atom
|
||||
atom H() const { return get_atom_by_atom_id("H"); } ///< Return the H atom
|
||||
|
||||
/// \brief Return true if this monomer is bonded to monomer @a rhs
|
||||
bool is_bonded_to(const monomer &rhs) const
|
||||
{
|
||||
return this != &rhs and are_bonded(*this, rhs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return true if the distance between the CA atoms of the
|
||||
* two monomers @a a and @a b are within the expected range with
|
||||
* an error margin of @a errorMargin.
|
||||
*
|
||||
* The expected distance is 3.0 ångström for a cis conformation
|
||||
* and 3.8 ångström for trans.
|
||||
*/
|
||||
static bool are_bonded(const monomer &a, const monomer &b, float errorMargin = 0.5f);
|
||||
|
||||
/// \brief Return true if the bond between @a a and @a b is cis
|
||||
static bool is_cis(const monomer &a, const monomer &b);
|
||||
|
||||
/// \brief Return the omega angle between @a a and @a b
|
||||
static float omega(const monomer &a, const monomer &b);
|
||||
|
||||
// for LEU and VAL
|
||||
/// \brief Return the chiral volume, only for LEU and VAL
|
||||
float chiral_volume() const;
|
||||
|
||||
/// \brief Compare this monomer with \a rhs
|
||||
bool operator==(const monomer &rhs) const
|
||||
{
|
||||
return m_polymer == rhs.m_polymer and m_index == rhs.m_index;
|
||||
@@ -552,24 +679,24 @@ class monomer : public residue
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief A polymer is simply a list of monomers
|
||||
*
|
||||
*/
|
||||
class polymer : public std::vector<monomer>
|
||||
{
|
||||
public:
|
||||
/// \brief Constructor
|
||||
polymer(structure &s, const std::string &entityID, const std::string &asymID, const std::string &auth_asym_id);
|
||||
|
||||
polymer(const polymer &) = delete;
|
||||
polymer &operator=(const polymer &) = delete;
|
||||
|
||||
// monomer &getBySeqID(int seqID);
|
||||
// const monomer &getBySeqID(int seqID) const;
|
||||
structure *get_structure() const { return m_structure; } ///< Return the structure
|
||||
|
||||
structure *get_structure() const { return m_structure; }
|
||||
|
||||
std::string get_asym_id() const { return m_asym_id; }
|
||||
std::string get_auth_asym_id() const { return m_auth_asym_id; } // The PDB chain ID, actually
|
||||
std::string get_entity_id() const { return m_entity_id; }
|
||||
|
||||
// int Distance(const monomer &a, const monomer &b) const;
|
||||
std::string get_asym_id() const { return m_asym_id; } ///< Return the asym_id
|
||||
std::string get_auth_asym_id() const { return m_auth_asym_id; } ///< Return the PDB chain ID, actually
|
||||
std::string get_entity_id() const { return m_entity_id; } ///< Return the entity_id
|
||||
|
||||
private:
|
||||
structure *m_structure;
|
||||
@@ -583,28 +710,52 @@ class polymer : public std::vector<monomer>
|
||||
|
||||
class branch;
|
||||
|
||||
/**
|
||||
* @brief A sugar is a residue that is part of a glycosylation site
|
||||
*
|
||||
*/
|
||||
|
||||
class sugar : public residue
|
||||
{
|
||||
public:
|
||||
/// \brief constructor
|
||||
sugar(branch &branch, const std::string &compoundID,
|
||||
const std::string &asymID, int authSeqID);
|
||||
|
||||
/** @cond */
|
||||
sugar(sugar &&rhs);
|
||||
sugar &operator=(sugar &&rhs);
|
||||
/** @endcond */
|
||||
|
||||
int num() const {
|
||||
/**
|
||||
* @brief Return the sugar number in the glycosylation tree
|
||||
*
|
||||
* To store the sugar number, the auth_seq_id field has been overloaded
|
||||
* in the specification. But since a sugar number should be, ehm, a number
|
||||
* and auth_seq_id is specified to contain a string, we do a check here
|
||||
* to see if it really is a number.
|
||||
*
|
||||
* @return The sugar number
|
||||
*/
|
||||
int num() const
|
||||
{
|
||||
int result;
|
||||
auto r = std::from_chars(m_auth_seq_id.data(), m_auth_seq_id.data() + m_auth_seq_id.length(), result);
|
||||
if (r.ec != std::errc())
|
||||
throw std::runtime_error("The auth_seq_id should be a number for a sugar");
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief Return the name of this sugar
|
||||
std::string name() const;
|
||||
|
||||
/// \brief Return the atom the C1 is linked to
|
||||
atom get_link() const { return m_link; }
|
||||
|
||||
/// \brief Set the link atom C1 is linked to to @a link
|
||||
void set_link(atom link) { m_link = link; }
|
||||
|
||||
/// \brief Return the sugar number of the sugar linked to C1
|
||||
size_t get_link_nr() const
|
||||
{
|
||||
size_t result = 0;
|
||||
@@ -613,42 +764,69 @@ class sugar : public residue
|
||||
return result;
|
||||
}
|
||||
|
||||
cif::mm::atom add_atom(row_initializer atom_info);
|
||||
/// \brief Construct an atom based on the info in @a atom_info and add it to this sugar
|
||||
atom add_atom(row_initializer atom_info);
|
||||
|
||||
private:
|
||||
branch *m_branch;
|
||||
atom m_link;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A branch is a list of sugars
|
||||
*
|
||||
* A list is how it is stored, but a branch is like a branch in a tree,
|
||||
* with potentially lots of sub branches. Each sugar is linked to a sugar
|
||||
* up in the branch with its (almost always) C1 atom.
|
||||
*
|
||||
*/
|
||||
class branch : public std::vector<sugar>
|
||||
{
|
||||
public:
|
||||
/// \brief constructor
|
||||
branch(structure &structure, const std::string &asym_id, const std::string &entity_id);
|
||||
|
||||
branch(const branch &) = delete;
|
||||
branch &operator=(const branch &) = delete;
|
||||
|
||||
/** @cond */
|
||||
branch(branch &&) = default;
|
||||
branch &operator=(branch &&) = default;
|
||||
/** @endcond */
|
||||
|
||||
/// \brief Update the link atoms in all sugars in this branch
|
||||
void link_atoms();
|
||||
|
||||
/// \brief Return the name of the branch
|
||||
std::string name() const;
|
||||
|
||||
/// \brief Return the weight of the branch based on the formulae of the sugars
|
||||
float weight() const;
|
||||
std::string get_asym_id() const { return m_asym_id; }
|
||||
std::string get_entity_id() const { return m_entity_id; }
|
||||
|
||||
structure &get_structure() { return *m_structure; }
|
||||
structure &get_structure() const { return *m_structure; }
|
||||
std::string get_asym_id() const { return m_asym_id; } ///< Return the asym_id
|
||||
std::string get_entity_id() const { return m_entity_id; } ///< Return the entity_id
|
||||
|
||||
structure &get_structure() { return *m_structure; } ///< Return the structure
|
||||
structure &get_structure() const { return *m_structure; } ///< Return the structure
|
||||
|
||||
/// \brief Return a reference to the sugar with number @a num
|
||||
sugar &get_sugar_by_num(int nr);
|
||||
|
||||
/// \brief Return a const reference to the sugar with number @a num
|
||||
const sugar &get_sugar_by_num(int nr) const
|
||||
{
|
||||
return const_cast<branch *>(this)->get_sugar_by_num(nr);
|
||||
}
|
||||
|
||||
/// \brief Construct a new sugar with compound ID @a compound_id in this branch
|
||||
/// and return a reference to the newly created sugar. Use this to create a first
|
||||
/// sugar in a branch.
|
||||
sugar &construct_sugar(const std::string &compound_id);
|
||||
|
||||
/// \brief Construct a new sugar with compound ID @a compound_id in this branch
|
||||
/// and return a reference to the newly created sugar. The newly created sugar
|
||||
/// will be connected to an already created sugar in the branch using the
|
||||
/// information in @a atom_id, @a linked_sugar_nr and @a linked_atom_id
|
||||
sugar &construct_sugar(const std::string &compound_id, const std::string &atom_id,
|
||||
int linked_sugar_nr, const std::string &linked_atom_id);
|
||||
|
||||
@@ -663,11 +841,13 @@ class branch : public std::vector<sugar>
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief A still very limited set of options for reading structures
|
||||
enum class StructureOpenOptions
|
||||
{
|
||||
SkipHydrogen = 1 << 0
|
||||
SkipHydrogen = 1 << 0 ///< Do not include hydrogen atoms in the structure object
|
||||
};
|
||||
|
||||
/// \brief A way to combine two options. Not very useful as there is only one...
|
||||
constexpr inline bool operator&(StructureOpenOptions a, StructureOpenOptions b)
|
||||
{
|
||||
return static_cast<int>(a) bitand static_cast<int>(b);
|
||||
@@ -675,60 +855,65 @@ constexpr inline bool operator&(StructureOpenOptions a, StructureOpenOptions b)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief A structure is the combination of polymers, ligand and sugar branches found
|
||||
* in the mmCIF file. This will always contain one model, the first model is taken
|
||||
* if not otherwise specified.
|
||||
*
|
||||
*/
|
||||
class structure
|
||||
{
|
||||
public:
|
||||
/// \brief Read the structure from cif::file @a p
|
||||
structure(file &p, size_t modelNr = 1, StructureOpenOptions options = {});
|
||||
|
||||
/// \brief Load the structure from already parsed mmCIF data in @a db
|
||||
structure(datablock &db, size_t modelNr = 1, StructureOpenOptions options = {});
|
||||
|
||||
/** @cond */
|
||||
structure(structure &&s) = default;
|
||||
/** @endcond */
|
||||
|
||||
// structures cannot be copied.
|
||||
|
||||
// Create a read-only clone of the current structure (for multithreaded calculations that move atoms)
|
||||
// NOTE: removed, simply create a new structure for each thread
|
||||
structure(const structure &) = delete;
|
||||
|
||||
structure &operator=(const structure &) = delete;
|
||||
// Structure &operator=(Structure &&s) = default;
|
||||
|
||||
~structure() = default;
|
||||
|
||||
/// \brief Return the model number
|
||||
size_t get_model_nr() const { return m_model_nr; }
|
||||
|
||||
/// \brief Return a list of all the atoms in this structure
|
||||
const std::vector<atom> &atoms() const { return m_atoms; }
|
||||
// std::vector<atom> &atoms() { return m_atoms; }
|
||||
|
||||
EntityType get_entity_type_for_entity_id(const std::string entityID) const;
|
||||
EntityType get_entity_type_for_asym_id(const std::string asymID) const;
|
||||
EntityType get_entity_type_for_entity_id(const std::string entityID) const; ///< Return the entity type for the entity with id @a entity_id
|
||||
EntityType get_entity_type_for_asym_id(const std::string asymID) const; ///< Return the entity type for the asym with id @a asym_id
|
||||
|
||||
// std::vector<atom> waters() const;
|
||||
const std::list<polymer> &polymers() const { return m_polymers; } ///< Return the list of polymers
|
||||
std::list<polymer> &polymers() { return m_polymers; } ///< Return the list of polymers
|
||||
|
||||
const std::list<polymer> &polymers() const { return m_polymers; }
|
||||
std::list<polymer> &polymers() { return m_polymers; }
|
||||
|
||||
polymer &get_polymer_by_asym_id(const std::string &asymID);
|
||||
|
||||
const polymer &get_polymer_by_asym_id(const std::string &asymID) const
|
||||
polymer &get_polymer_by_asym_id(const std::string &asymID); ///< Return the polymer having asym ID @a asymID
|
||||
const polymer &get_polymer_by_asym_id(const std::string &asymID) const ///< Return the polymer having asym ID @a asymID
|
||||
{
|
||||
return const_cast<structure *>(this)->get_polymer_by_asym_id(asymID);
|
||||
}
|
||||
|
||||
const std::list<branch> &branches() const { return m_branches; }
|
||||
std::list<branch> &branches() { return m_branches; }
|
||||
const std::list<branch> &branches() const { return m_branches; } ///< Return the list of all branches
|
||||
std::list<branch> &branches() { return m_branches; } ///< Return the list of all branches
|
||||
|
||||
branch &get_branch_by_asym_id(const std::string &asymID);
|
||||
const branch &get_branch_by_asym_id(const std::string &asymID) const;
|
||||
branch &get_branch_by_asym_id(const std::string &asymID); ///< Return the branch having asym ID @a asymID
|
||||
const branch &get_branch_by_asym_id(const std::string &asymID) const; ///< Return the branch having asym ID @a asymID
|
||||
|
||||
const std::vector<residue> &non_polymers() const { return m_non_polymers; }
|
||||
const std::vector<residue> &non_polymers() const { return m_non_polymers; } ///< Return the list of non-polymers, actually the list of ligands
|
||||
|
||||
bool has_atom_id(const std::string &id) const;
|
||||
atom get_atom_by_id(const std::string &id) const;
|
||||
// atom getAtomByLocation(point pt, float maxDistance) const;
|
||||
bool has_atom_id(const std::string &id) const; ///< Return true if an atom with ID @a id exists in this structure
|
||||
atom get_atom_by_id(const std::string &id) const; ///< Return the atom with ID @a id
|
||||
|
||||
/// \brief Return the atom identified by the label_ values specified
|
||||
atom get_atom_by_label(const std::string &atomID, const std::string &asymID,
|
||||
const std::string &compID, int seqID, const std::string &altID = "");
|
||||
|
||||
// /// \brief Return the atom closest to point \a p
|
||||
/// \brief Return the atom closest to point \a p
|
||||
atom get_atom_by_position(point p) const;
|
||||
|
||||
/// \brief Return the atom closest to point \a p with atom type \a type in a residue of type \a res_type
|
||||
@@ -779,14 +964,33 @@ class structure
|
||||
return get_residue(atom.get_label_asym_id(), atom.get_label_comp_id(), atom.get_label_seq_id(), atom.get_auth_seq_id());
|
||||
}
|
||||
|
||||
// Actions
|
||||
// Actions. Originally a lot more actions were expected here
|
||||
|
||||
/// \brief Remove atom @a a
|
||||
void remove_atom(atom &a)
|
||||
{
|
||||
remove_atom(a, true);
|
||||
}
|
||||
|
||||
void swap_atoms(atom a1, atom a2); // swap the labels for these atoms
|
||||
void move_atom(atom a, point p); // move atom to a new location
|
||||
void swap_atoms(atom a1, atom a2); ///< swap the labels for these atoms
|
||||
void move_atom(atom a, point p); ///< move atom to a new location
|
||||
|
||||
/**
|
||||
* @brief Change residue @a res to a new compound ID optionally
|
||||
* remapping atoms.
|
||||
*
|
||||
* A new chem_comp entry as well as an entity is created if needed and
|
||||
* if the list of @a remappedAtoms is not empty it is used to remap.
|
||||
*
|
||||
* The array in @a remappedAtoms contains tuples of strings, both
|
||||
* strings contain an atom_id. The first is the one in the current
|
||||
* residue and the second is the atom_id that should be used instead.
|
||||
* If the second string is empty, the atom is removed from the residue.
|
||||
*
|
||||
* @param res
|
||||
* @param newcompound
|
||||
* @param remappedAtoms
|
||||
*/
|
||||
void change_residue(residue &res, const std::string &newcompound,
|
||||
const std::vector<std::tuple<std::string, std::string>> &remappedAtoms);
|
||||
|
||||
@@ -794,6 +998,7 @@ class structure
|
||||
///
|
||||
/// \param asym_id The asym ID
|
||||
/// \param seq_id The sequence ID
|
||||
/// \param auth_seq_id The auth sequence ID
|
||||
void remove_residue(const std::string &asym_id, int seq_id, const std::string &auth_seq_id);
|
||||
|
||||
/// \brief Create a new non-polymer entity, returns new ID
|
||||
@@ -826,17 +1031,17 @@ class structure
|
||||
/// \brief Create a new and empty (sugar) branch
|
||||
branch &create_branch();
|
||||
|
||||
/// \brief Create a new (sugar) branch with one first NAG containing atoms constructed from \a atoms
|
||||
branch &create_branch(std::vector<row_initializer> atoms);
|
||||
// /// \brief Create a new (sugar) branch with one first NAG containing atoms constructed from \a atoms
|
||||
// branch &create_branch(std::vector<row_initializer> atoms);
|
||||
|
||||
/// \brief Extend an existing (sugar) branch identified by \a asymID with one sugar containing atoms constructed from \a atom_info
|
||||
///
|
||||
/// \param asym_id The asym id of the branch to extend
|
||||
/// \param atom_info Array containing the info for the atoms to construct for the new sugar
|
||||
/// \param link_sugar The sugar to link to, note: this is the sugar number (1 based)
|
||||
/// \param link_atom The atom id of the atom linked in the sugar
|
||||
branch &extend_branch(const std::string &asym_id, std::vector<row_initializer> atom_info,
|
||||
int link_sugar, const std::string &link_atom);
|
||||
// /// \brief Extend an existing (sugar) branch identified by \a asymID with one sugar containing atoms constructed from \a atom_info
|
||||
// ///
|
||||
// /// \param asym_id The asym id of the branch to extend
|
||||
// /// \param atom_info Array containing the info for the atoms to construct for the new sugar
|
||||
// /// \param link_sugar The sugar to link to, note: this is the sugar number (1 based)
|
||||
// /// \param link_atom The atom id of the atom linked in the sugar
|
||||
// branch &extend_branch(const std::string &asym_id, std::vector<row_initializer> atom_info,
|
||||
// int link_sugar, const std::string &link_atom);
|
||||
|
||||
/// \brief Remove \a branch
|
||||
void remove_branch(branch &branch);
|
||||
@@ -858,6 +1063,7 @@ class structure
|
||||
/// \brief Translate, rotate and translate again the coordinates of all atoms in the structure by \a t1 , \a q and \a t2
|
||||
void translate_rotate_and_translate(point t1, quaternion q, point t2);
|
||||
|
||||
/// \brief Remove all categories that have no rows left
|
||||
void cleanup_empty_categories();
|
||||
|
||||
/// \brief Direct access to underlying data
|
||||
@@ -866,29 +1072,31 @@ class structure
|
||||
return m_db[name];
|
||||
}
|
||||
|
||||
/// \brief Direct access to underlying data
|
||||
datablock &get_datablock() const
|
||||
{
|
||||
return m_db;
|
||||
}
|
||||
|
||||
/// \brief Check if all atoms are part of either a polymer, a branch or one of the non-polymer residues
|
||||
void validate_atoms() const;
|
||||
|
||||
// TODO: make this protected?
|
||||
|
||||
void load_atoms_for_model(StructureOpenOptions options);
|
||||
|
||||
/// \brief emplace a newly created atom using @a args
|
||||
template <typename... Args>
|
||||
atom &emplace_atom(Args&... args)
|
||||
atom &emplace_atom(Args &...args)
|
||||
{
|
||||
return emplace_atom(atom{ std::forward<Args>(args)... });
|
||||
}
|
||||
|
||||
/// \brief emplace the moved atom @a atom
|
||||
atom &emplace_atom(atom &&atom);
|
||||
|
||||
private:
|
||||
friend polymer;
|
||||
friend residue;
|
||||
|
||||
void load_atoms_for_model(StructureOpenOptions options);
|
||||
|
||||
std::string insert_compound(const std::string &compoundID, bool is_entity);
|
||||
|
||||
std::string create_entity_for_branch(branch &branch);
|
||||
|
||||
@@ -30,14 +30,22 @@
|
||||
|
||||
#include <map>
|
||||
|
||||
/**
|
||||
* @file parser.hpp
|
||||
*
|
||||
* This file contains the declaration of an mmCIF parser
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** Exception that is thrown when the mmCIF file contains a parsing error */
|
||||
class parse_error : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
/// \brief constructor
|
||||
parse_error(uint32_t line_nr, const std::string &message)
|
||||
: std::runtime_error("parse error at line " + std::to_string(line_nr) + ": " + message)
|
||||
{
|
||||
@@ -46,57 +54,83 @@ class parse_error : public std::runtime_error
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The sac_parser is a similar to SAX parsers (Simple API for XML,
|
||||
* in our case it is Simple API for CIF)
|
||||
*
|
||||
* This is a hand crafted, optimised parser for reading cif files,
|
||||
* both cif 1.0 and cif 1.1 is supported. But version 2.0 is not.
|
||||
* That means that the content of files strictly contains only
|
||||
* ASCII characters. Anything else will generate an error.
|
||||
*
|
||||
* This class is an abstract base class. Derived classes should
|
||||
* implement the produce_ methods.
|
||||
*/
|
||||
|
||||
// TODO: Need to implement support for transformed long lines
|
||||
|
||||
class sac_parser
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
using datablock_index = std::map<std::string, std::size_t>;
|
||||
|
||||
virtual ~sac_parser() = default;
|
||||
/** @endcond */
|
||||
|
||||
/// \brief The parser only supports ASCII so we can
|
||||
/// create a table with character properties.
|
||||
enum CharTraitsMask : uint8_t
|
||||
{
|
||||
kOrdinaryMask = 1 << 0,
|
||||
kNonBlankMask = 1 << 1,
|
||||
kTextLeadMask = 1 << 2,
|
||||
kAnyPrintMask = 1 << 3
|
||||
kOrdinaryMask = 1 << 0, ///< The character is in the Ordinary class
|
||||
kNonBlankMask = 1 << 1, ///< The character is in the NonBlank class
|
||||
kTextLeadMask = 1 << 2, ///< The character is in the TextLead class
|
||||
kAnyPrintMask = 1 << 3 ///< The character is in the AnyPrint class
|
||||
};
|
||||
|
||||
/// \brief Return true if the character @a ch is a *space* character
|
||||
static constexpr bool is_space(int ch)
|
||||
{
|
||||
return ch == ' ' or ch == '\t' or ch == '\r' or ch == '\n';
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *white* character
|
||||
static constexpr bool is_white(int ch)
|
||||
{
|
||||
return is_space(ch) or ch == '#';
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *ordinary* character
|
||||
static constexpr bool is_ordinary(int ch)
|
||||
{
|
||||
return ch >= 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kOrdinaryMask) != 0;
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *non_blank* character
|
||||
static constexpr bool is_non_blank(int ch)
|
||||
{
|
||||
return ch > 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kNonBlankMask) != 0;
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *text_lead* character
|
||||
static constexpr bool is_text_lead(int ch)
|
||||
{
|
||||
return ch >= 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kTextLeadMask) != 0;
|
||||
}
|
||||
|
||||
/// \brief Return true if the character @a ch is a *any_print* character
|
||||
static constexpr bool is_any_print(int ch)
|
||||
{
|
||||
return ch == '\t' or
|
||||
(ch >= 0x20 and ch <= 0x7f and (kCharTraitsTable[ch - 0x20] & kAnyPrintMask) != 0);
|
||||
}
|
||||
|
||||
/// \brief Return true if the string in @a text can safely be written without quotation
|
||||
static bool is_unquoted_string(std::string_view text);
|
||||
|
||||
protected:
|
||||
/** @cond */
|
||||
|
||||
static constexpr uint8_t kCharTraitsTable[128] = {
|
||||
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
|
||||
14, 15, 14, 14, 14, 15, 15, 14, 15, 15, 15, 15, 15, 15, 15, 15, // 2
|
||||
@@ -152,17 +186,45 @@ class sac_parser
|
||||
|
||||
void match(CIFToken token);
|
||||
|
||||
/** @endcond */
|
||||
|
||||
public:
|
||||
|
||||
/** \brief Parse only a single datablock in the string @a datablock
|
||||
* The start of the datablock is first located and then data
|
||||
* is parsed up until the next start of a datablock or the end of
|
||||
* the data.
|
||||
* */
|
||||
bool parse_single_datablock(const std::string &datablock);
|
||||
|
||||
/** \brief Return an index for all the datablocks found, that is
|
||||
* the index will contain the names and offsets for each.
|
||||
*/
|
||||
datablock_index index_datablocks();
|
||||
|
||||
/**
|
||||
* @brief Parse the datablock named @a datablock
|
||||
*
|
||||
* This will first lookup the datablock's offset in the index @a index
|
||||
* and then start parsing from that location until the next datablock.
|
||||
*
|
||||
* @param datablock Name of the datablock to parse
|
||||
* @param index The index created using index_datablocks
|
||||
* @return true If the datablock was found
|
||||
* @return false If the datablock was not found
|
||||
*/
|
||||
bool parse_single_datablock(const std::string &datablock, const datablock_index &index);
|
||||
|
||||
/**
|
||||
* @brief Parse the file
|
||||
*
|
||||
*/
|
||||
void parse_file();
|
||||
|
||||
protected:
|
||||
|
||||
/** @cond */
|
||||
|
||||
sac_parser(std::istream &is, bool init = true);
|
||||
|
||||
void parse_global();
|
||||
@@ -174,7 +236,7 @@ class sac_parser
|
||||
void error(const std::string &msg)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Error parsing mmCIF: " << msg << std::endl;
|
||||
std::cerr << "Error parsing mmCIF: " << msg << '\n';
|
||||
|
||||
throw parse_error(m_line_nr, msg);
|
||||
}
|
||||
@@ -182,7 +244,7 @@ class sac_parser
|
||||
void warning(const std::string &msg)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "parser warning at line " << m_line_nr << ": " << msg << std::endl;
|
||||
std::cerr << "parser warning at line " << m_line_nr << ": " << msg << '\n';
|
||||
}
|
||||
|
||||
// production methods, these are pure virtual here
|
||||
@@ -222,19 +284,29 @@ class sac_parser
|
||||
// token buffer
|
||||
std::vector<char> m_token_buffer;
|
||||
std::string_view m_token_value;
|
||||
|
||||
/** @endcond */
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief An actual implementation of a sac_parser generating data in a file
|
||||
*
|
||||
* This parser will create the cif::file, cif::datablock and cif::category
|
||||
* objects required to contain all data
|
||||
*/
|
||||
class parser : public sac_parser
|
||||
{
|
||||
public:
|
||||
/// \brief constructor, generates data into @a file from @a is
|
||||
parser(std::istream &is, file &file)
|
||||
: sac_parser(is)
|
||||
, m_file(file)
|
||||
{
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
void produce_datablock(std::string_view name) override;
|
||||
|
||||
void produce_category(std::string_view name) override;
|
||||
@@ -248,6 +320,8 @@ class parser : public sac_parser
|
||||
datablock *m_datablock = nullptr;
|
||||
category *m_category = nullptr;
|
||||
row_handle m_row;
|
||||
|
||||
/** @endcond */
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
|
||||
147
include/cif++/pdb.hpp
Normal file
147
include/cif++/pdb.hpp
Normal file
@@ -0,0 +1,147 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2023 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/file.hpp"
|
||||
|
||||
/**
|
||||
* @file pdb.hpp
|
||||
*
|
||||
* This file presents the API to read and write files in the
|
||||
* legacy and ancient PDB format.
|
||||
*
|
||||
* The code works on the basis of best effort since it is
|
||||
* impossible to have correct round trip fidelity.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif::pdb
|
||||
{
|
||||
|
||||
/// --------------------------------------------------------------------
|
||||
// PDB to mmCIF
|
||||
|
||||
/** @brief Read a file in either mmCIF or PDB format from file @a file,
|
||||
* compressed or not, depending on the content.
|
||||
*/
|
||||
|
||||
file read(const std::filesystem::path &file);
|
||||
|
||||
/** @brief Read a file in either mmCIF or PDB format from std::istream @a is,
|
||||
* compressed or not, depending on the content.
|
||||
*/
|
||||
|
||||
file read(std::istream &is);
|
||||
|
||||
/**
|
||||
* @brief Read a file in legacy PDB format from std::istream @a is and
|
||||
* put the data into @a cifFile
|
||||
*/
|
||||
file read_pdb_file(std::istream &pdbFile);
|
||||
|
||||
// mmCIF to PDB
|
||||
|
||||
/** @brief Write out the data in @a db in legacy PDB format
|
||||
* to std::ostream @a os
|
||||
*/
|
||||
void write(std::ostream &os, const datablock &db);
|
||||
|
||||
/** @brief Write out the data in @a f in legacy PDB format
|
||||
* to std::ostream @a os
|
||||
*/
|
||||
inline void write(std::ostream &os, const file &f)
|
||||
{
|
||||
write(os, f.front());
|
||||
}
|
||||
|
||||
/** @brief Write out the data in @a db to file @a file
|
||||
* in legacy PDB format or mmCIF format, depending on the
|
||||
* filename extension.
|
||||
*
|
||||
* If extension of @a file is *.gz* the resulting file will
|
||||
* be written in gzip compressed format.
|
||||
*/
|
||||
void write(const std::filesystem::path &file, const datablock &db);
|
||||
|
||||
/** @brief Write out the data in @a f to file @a file
|
||||
* in legacy PDB format or mmCIF format, depending on the
|
||||
* filename extension.
|
||||
*
|
||||
* If extension of @a file is *.gz* the resulting file will
|
||||
* be written in gzip compressed format.
|
||||
*/
|
||||
inline void write(const std::filesystem::path &p, const file &f)
|
||||
{
|
||||
write(p, f.front());
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Other I/O related routines
|
||||
|
||||
/** @brief Return the HEADER line for the data in @a data
|
||||
*
|
||||
* The line returned should be compatible with the legacy PDB
|
||||
* format and is e.g. used in the DSSP program.
|
||||
*
|
||||
* @param data The datablock to use as source for the requested data
|
||||
* @param truncate_at The maximum length of the line returned
|
||||
*/
|
||||
|
||||
std::string get_HEADER_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
/** @brief Return the COMPND line for the data in @a data
|
||||
*
|
||||
* The line returned should be compatible with the legacy PDB
|
||||
* format and is e.g. used in the DSSP program.
|
||||
*
|
||||
* @param data The datablock to use as source for the requested data
|
||||
* @param truncate_at The maximum length of the line returned
|
||||
*/
|
||||
|
||||
std::string get_COMPND_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
/** @brief Return the SOURCE line for the data in @a data
|
||||
*
|
||||
* The line returned should be compatible with the legacy PDB
|
||||
* format and is e.g. used in the DSSP program.
|
||||
*
|
||||
* @param data The datablock to use as source for the requested data
|
||||
* @param truncate_at The maximum length of the line returned
|
||||
*/
|
||||
|
||||
std::string get_SOURCE_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
/** @brief Return the AUTHOR line for the data in @a data
|
||||
*
|
||||
* The line returned should be compatible with the legacy PDB
|
||||
* format and is e.g. used in the DSSP program.
|
||||
*
|
||||
* @param data The datablock to use as source for the requested data
|
||||
* @param truncate_at The maximum length of the line returned
|
||||
*/
|
||||
|
||||
std::string get_AUTHOR_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
|
||||
} // namespace pdbx
|
||||
|
||||
@@ -26,17 +26,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/datablock.hpp"
|
||||
/// \file cif2pdb.hpp
|
||||
/// \deprecated This file is no longer used. Please use "cif++/pdb.hpp" instead
|
||||
|
||||
namespace cif::pdb
|
||||
{
|
||||
#warning "Use of this file is deprecated, please use "cif++/pdb.hpp"
|
||||
|
||||
/// \brief Just the HEADER, COMPND, SOURCE and AUTHOR lines
|
||||
void write_header_lines(std::ostream &os, const datablock &data);
|
||||
|
||||
std::string get_HEADER_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
std::string get_COMPND_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
std::string get_SOURCE_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
std::string get_AUTHOR_line(const datablock &data, std::string::size_type truncate_at = 127);
|
||||
|
||||
} // namespace pdbx
|
||||
|
||||
@@ -26,35 +26,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/datablock.hpp"
|
||||
/// \file io.hpp
|
||||
/// \deprecated This file is no longer used. Please use "cif++/pdb.hpp" instead
|
||||
|
||||
namespace cif::pdb
|
||||
{
|
||||
|
||||
/// \brief Read a file in either mmCIF or PDB format, compressed or not,
|
||||
/// depending on the content.
|
||||
file read(const std::filesystem::path &file);
|
||||
|
||||
/// \brief Read a file in either mmCIF or PDB format, compressed or not,
|
||||
/// depending on the content.
|
||||
file read(std::istream &is);
|
||||
|
||||
/// \brief Write out a file in PDB format
|
||||
void write(std::ostream &os, const datablock &db);
|
||||
|
||||
/// \brief Write out a file in PDB format
|
||||
inline void write(std::ostream &os, const file &f)
|
||||
{
|
||||
write(os, f.front());
|
||||
}
|
||||
|
||||
/// \brief Write out a file in PDB format or mmCIF format, depending on the filename extension
|
||||
void write(const std::filesystem::path &file, const datablock &db);
|
||||
|
||||
/// \brief Write out a file in PDB format or mmCIF format, depending on the filename extension
|
||||
inline void write(const std::filesystem::path &p, const file &f)
|
||||
{
|
||||
write(p, f.front());
|
||||
}
|
||||
|
||||
}
|
||||
#warning "Use of this file is deprecated, please use "cif++/pdb.hpp"
|
||||
|
||||
@@ -26,39 +26,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/file.hpp"
|
||||
namespace cif::pdb
|
||||
{
|
||||
/// \file pdb2cif.hpp
|
||||
/// \deprecated This file is no longer used. Please use "cif++/pdb.hpp" instead
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
struct PDBRecord
|
||||
{
|
||||
PDBRecord *mNext;
|
||||
uint32_t mLineNr;
|
||||
char mName[11];
|
||||
size_t mVlen;
|
||||
char mValue[1];
|
||||
|
||||
PDBRecord(uint32_t lineNr, const std::string &name, const std::string &value);
|
||||
~PDBRecord();
|
||||
|
||||
void *operator new(size_t);
|
||||
void *operator new(size_t size, size_t vLen);
|
||||
|
||||
void operator delete(void *p);
|
||||
void operator delete(void *p, size_t vLen);
|
||||
|
||||
bool is(const char *name) const;
|
||||
|
||||
char vC(size_t column);
|
||||
std::string vS(size_t columnFirst, size_t columnLast = std::numeric_limits<size_t>::max());
|
||||
int vI(int columnFirst, int columnLast);
|
||||
std::string vF(size_t columnFirst, size_t columnLast);
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
void ReadPDBFile(std::istream &pdbFile, file &cifFile);
|
||||
|
||||
} // namespace pdbx
|
||||
#warning "Use of this file is deprecated, please use "cif++/pdb.hpp"
|
||||
@@ -26,26 +26,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/datablock.hpp"
|
||||
/// \file tls.hpp
|
||||
/// \deprecated This code has been moved to libpdb-redo
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
struct tls_selection;
|
||||
struct tls_residue;
|
||||
|
||||
struct tls_selection
|
||||
{
|
||||
virtual ~tls_selection() {}
|
||||
virtual void collect_residues(cif::datablock &db, std::vector<tls_residue> &residues, std::size_t indentLevel = 0) const = 0;
|
||||
std::vector<std::tuple<std::string, int, int>> get_ranges(cif::datablock &db, bool pdbNamespace) const;
|
||||
};
|
||||
|
||||
// Low level: get the selections
|
||||
std::unique_ptr<tls_selection> parse_tls_selection_details(const std::string &program, const std::string &selection);
|
||||
|
||||
} // namespace cif
|
||||
#warning "This code has been moved to libpdb-redo"
|
||||
|
||||
@@ -40,24 +40,49 @@
|
||||
#include <clipper/core/coords.h>
|
||||
#endif
|
||||
|
||||
/** \file point.hpp
|
||||
*
|
||||
* This file contains the definition for *cif::point* as well as
|
||||
* lots of routines and classes that can manipulate points.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Our value for Pi
|
||||
const double
|
||||
kPI = 3.141592653589793238462643383279502884;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// A stripped down quaternion implementation, based on boost::math::quaternion
|
||||
// We use quaternions to do rotations in 3d space
|
||||
/**
|
||||
* @brief A stripped down quaternion implementation, based on boost::math::quaternion
|
||||
*
|
||||
* We use quaternions to do rotations in 3d space. Quaternions are faster than
|
||||
* matrix calculations and they also suffer less from drift caused by rounding
|
||||
* errors.
|
||||
*
|
||||
* Like complex number, quaternions do have a meaningful notion of "real part",
|
||||
* but unlike them there is no meaningful notion of "imaginary part".
|
||||
* Instead there is an "unreal part" which itself is a quaternion, and usually
|
||||
* nothing simpler (as opposed to the complex number case).
|
||||
* However, for practicality, there are accessors for the other components
|
||||
* (these are necessary for the templated copy constructor, for instance).
|
||||
*
|
||||
* @note Quaternion multiplication is *NOT* commutative;
|
||||
* symbolically, "q *= rhs;" means "q = q * rhs;"
|
||||
* and "q /= rhs;" means "q = q * inverse_of(rhs);"
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
class quaternion_type
|
||||
{
|
||||
public:
|
||||
/// \brief the value type of the elements, usually this is float
|
||||
using value_type = T;
|
||||
|
||||
/// \brief constructor with the four members
|
||||
constexpr explicit quaternion_type(value_type const &value_a = {}, value_type const &value_b = {}, value_type const &value_c = {}, value_type const &value_d = {})
|
||||
: a(value_a)
|
||||
, b(value_b)
|
||||
@@ -66,6 +91,7 @@ class quaternion_type
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking two complex values as input
|
||||
constexpr explicit quaternion_type(std::complex<value_type> const &z0, std::complex<value_type> const &z1 = std::complex<value_type>())
|
||||
: a(z0.real())
|
||||
, b(z0.imag())
|
||||
@@ -74,9 +100,10 @@ class quaternion_type
|
||||
{
|
||||
}
|
||||
|
||||
constexpr quaternion_type(quaternion_type const &) = default;
|
||||
constexpr quaternion_type(quaternion_type &&) = default;
|
||||
constexpr quaternion_type(quaternion_type const &) = default; ///< Copy constructor
|
||||
constexpr quaternion_type(quaternion_type &&) = default; ///< Copy constructor
|
||||
|
||||
/// \brief Copy constructor accepting a quaternion with a different value_type
|
||||
template <typename X>
|
||||
constexpr explicit quaternion_type(quaternion_type<X> const &rhs)
|
||||
: a(static_cast<value_type>(rhs.a))
|
||||
@@ -87,24 +114,20 @@ class quaternion_type
|
||||
}
|
||||
|
||||
// accessors
|
||||
//
|
||||
// Note: Like complex number, quaternions do have a meaningful notion of "real part",
|
||||
// but unlike them there is no meaningful notion of "imaginary part".
|
||||
// Instead there is an "unreal part" which itself is a quaternion, and usually
|
||||
// nothing simpler (as opposed to the complex number case).
|
||||
// However, for practicality, there are accessors for the other components
|
||||
// (these are necessary for the templated copy constructor, for instance).
|
||||
|
||||
/// \brief See class description, return the *real* part of the quaternion
|
||||
constexpr value_type real() const
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
/// \brief See class description, return the *unreal* part of the quaternion
|
||||
constexpr quaternion_type unreal() const
|
||||
{
|
||||
return { 0, b, c, d };
|
||||
}
|
||||
|
||||
/// \brief swap
|
||||
constexpr void swap(quaternion_type &o)
|
||||
{
|
||||
std::swap(a, o.a);
|
||||
@@ -115,6 +138,7 @@ class quaternion_type
|
||||
|
||||
// assignment operators
|
||||
|
||||
/// \brief Assignment operator accepting a quaternion with optionally another value_type
|
||||
template <typename X>
|
||||
constexpr quaternion_type &operator=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
@@ -126,6 +150,7 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Assignment operator
|
||||
constexpr quaternion_type &operator=(quaternion_type const &rhs)
|
||||
{
|
||||
a = rhs.a;
|
||||
@@ -136,6 +161,7 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Assignment operator that sets the *real* part to @a rhs and the *unreal* parts to zero
|
||||
constexpr quaternion_type &operator=(value_type const &rhs)
|
||||
{
|
||||
a = rhs;
|
||||
@@ -145,6 +171,9 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Assignment operator that sets the *real* part to the real part of @a rhs
|
||||
/// and the first *unreal* part to the imaginary part of of @a rhs. The other *unreal*
|
||||
// parts are set to zero.
|
||||
constexpr quaternion_type &operator=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
a = rhs.real();
|
||||
@@ -156,17 +185,16 @@ class quaternion_type
|
||||
}
|
||||
|
||||
// other assignment-related operators
|
||||
//
|
||||
// NOTE: Quaternion multiplication is *NOT* commutative;
|
||||
// symbolically, "q *= rhs;" means "q = q * rhs;"
|
||||
// and "q /= rhs;" means "q = q * inverse_of(rhs);"
|
||||
|
||||
/// \brief operator += adding value @a rhs to the *real* part
|
||||
constexpr quaternion_type &operator+=(value_type const &rhs)
|
||||
{
|
||||
a += rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator += adding the real part of @a rhs to the *real* part
|
||||
/// and the imaginary part of @a rhs to the first *unreal* part
|
||||
constexpr quaternion_type &operator+=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
a += std::real(rhs);
|
||||
@@ -174,6 +202,7 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator += adding the parts of @a rhs to the equivalent part of this
|
||||
template <class X>
|
||||
constexpr quaternion_type &operator+=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
@@ -184,12 +213,15 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator -= subtracting value @a rhs from the *real* part
|
||||
constexpr quaternion_type &operator-=(value_type const &rhs)
|
||||
{
|
||||
a -= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator -= subtracting the real part of @a rhs from the *real* part
|
||||
/// and the imaginary part of @a rhs from the first *unreal* part
|
||||
constexpr quaternion_type &operator-=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
a -= std::real(rhs);
|
||||
@@ -197,6 +229,7 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief operator -= subtracting the parts of @a rhs from the equivalent part of this
|
||||
template <class X>
|
||||
constexpr quaternion_type &operator-=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
@@ -207,6 +240,7 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief multiply all parts with value @a rhs
|
||||
constexpr quaternion_type &operator*=(value_type const &rhs)
|
||||
{
|
||||
a *= rhs;
|
||||
@@ -216,6 +250,7 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief multiply with complex number @a rhs
|
||||
constexpr quaternion_type &operator*=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
value_type ar = rhs.real();
|
||||
@@ -225,13 +260,15 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr friend quaternion_type operator*(const quaternion_type &a, const quaternion_type &b)
|
||||
/// \brief multiply @a a with @a b and return the result
|
||||
friend constexpr quaternion_type operator*(const quaternion_type &a, const quaternion_type &b)
|
||||
{
|
||||
auto result = a;
|
||||
result *= b;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief multiply with quaternion @a rhs
|
||||
template <typename X>
|
||||
constexpr quaternion_type &operator*=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
@@ -245,6 +282,7 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief divide all parts by @a rhs
|
||||
constexpr quaternion_type &operator/=(value_type const &rhs)
|
||||
{
|
||||
a /= rhs;
|
||||
@@ -254,6 +292,7 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief divide by complex number @a rhs
|
||||
constexpr quaternion_type &operator/=(std::complex<value_type> const &rhs)
|
||||
{
|
||||
value_type ar = rhs.real();
|
||||
@@ -264,6 +303,7 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief divide by quaternion @a rhs
|
||||
template <typename X>
|
||||
constexpr quaternion_type &operator/=(quaternion_type<X> const &rhs)
|
||||
{
|
||||
@@ -278,7 +318,8 @@ class quaternion_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr friend quaternion_type normalize(quaternion_type q)
|
||||
/// \brief normalise the values so that the length of the result is exactly 1
|
||||
friend constexpr quaternion_type normalize(quaternion_type q)
|
||||
{
|
||||
std::valarray<value_type> t(4);
|
||||
|
||||
@@ -299,26 +340,30 @@ class quaternion_type
|
||||
return q;
|
||||
}
|
||||
|
||||
constexpr friend quaternion_type conj(quaternion_type q)
|
||||
/// \brief return the conjugate of this
|
||||
friend constexpr quaternion_type conj(quaternion_type q)
|
||||
{
|
||||
return quaternion_type{ +q.a, -q.b, -q.c, -q.d };
|
||||
}
|
||||
|
||||
constexpr value_type get_a() const { return a; }
|
||||
constexpr value_type get_b() const { return b; }
|
||||
constexpr value_type get_c() const { return c; }
|
||||
constexpr value_type get_d() const { return d; }
|
||||
constexpr value_type get_a() const { return a; } ///< Return part a
|
||||
constexpr value_type get_b() const { return b; } ///< Return part b
|
||||
constexpr value_type get_c() const { return c; } ///< Return part c
|
||||
constexpr value_type get_d() const { return d; } ///< Return part d
|
||||
|
||||
/// \brief compare with @a rhs
|
||||
constexpr bool operator==(const quaternion_type &rhs) const
|
||||
{
|
||||
return a == rhs.a and b == rhs.b and c == rhs.c and d == rhs.d;
|
||||
}
|
||||
|
||||
/// \brief compare with @a rhs
|
||||
constexpr bool operator!=(const quaternion_type &rhs) const
|
||||
{
|
||||
return a != rhs.a or b != rhs.b or c != rhs.c or d != rhs.d;
|
||||
}
|
||||
|
||||
/// \brief test for all zero values
|
||||
constexpr operator bool() const
|
||||
{
|
||||
return a != 0 or b != 0 or c != 0 or d != 0;
|
||||
@@ -328,6 +373,19 @@ class quaternion_type
|
||||
value_type a, b, c, d;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief This code is similar to the code in boost so I copy the documentation as well:
|
||||
*
|
||||
* > spherical is a simple transposition of polar, it takes as inputs a (positive)
|
||||
* > magnitude and a point on the hypersphere, given by three angles. The first of
|
||||
* > these, theta has a natural range of -pi to +pi, and the other two have natural
|
||||
* > ranges of -pi/2 to +pi/2 (as is the case with the usual spherical coordinates in
|
||||
* > **R**<sup>3</sup>). Due to the many symmetries and periodicities, nothing untoward happens if
|
||||
* > the magnitude is negative or the angles are outside their natural ranges. The
|
||||
* > expected degeneracies (a magnitude of zero ignores the angles settings...) do
|
||||
* > happen however.
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
inline quaternion_type<T> spherical(T const &rho, T const &theta, T const &phi1, T const &phi2)
|
||||
{
|
||||
@@ -345,24 +403,34 @@ inline quaternion_type<T> spherical(T const &rho, T const &theta, T const &phi1,
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief By default we use the float version of a quaternion
|
||||
using quaternion = quaternion_type<float>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
// point, a location with x, y and z coordinates as floating point.
|
||||
// This one is derived from a tuple<float,float,float> so
|
||||
// you can do things like:
|
||||
//
|
||||
// float x, y, z;
|
||||
// tie(x, y, z) = atom.loc();
|
||||
/**
|
||||
* @brief 3D point: a location with x, y and z coordinates as floating point.
|
||||
*
|
||||
* Note that you can simply use structured binding to get access to the
|
||||
* individual parts like so:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* float x, y, z;
|
||||
* tie(x, y, z) = atom.get_location();
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
template <typename F>
|
||||
struct point_type
|
||||
{
|
||||
/// \brief the value type of the x, y and z members
|
||||
using value_type = F;
|
||||
|
||||
value_type m_x, m_y, m_z;
|
||||
value_type m_x, ///< The x part of the location
|
||||
m_y, ///< The y part of the location
|
||||
m_z; ///< The z part of the location
|
||||
|
||||
/// \brief default constructor, initialises the values to zero
|
||||
constexpr point_type()
|
||||
: m_x(0)
|
||||
, m_y(0)
|
||||
@@ -370,6 +438,7 @@ struct point_type
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking three values
|
||||
constexpr point_type(value_type x, value_type y, value_type z)
|
||||
: m_x(x)
|
||||
, m_y(y)
|
||||
@@ -377,6 +446,7 @@ struct point_type
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief Copy constructor
|
||||
template <typename PF>
|
||||
constexpr point_type(const point_type<PF> &pt)
|
||||
: m_x(static_cast<F>(pt.m_x))
|
||||
@@ -385,12 +455,14 @@ struct point_type
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking a tuple of three values
|
||||
constexpr point_type(const std::tuple<value_type, value_type, value_type> &pt)
|
||||
: point_type(std::get<0>(pt), std::get<1>(pt), std::get<2>(pt))
|
||||
{
|
||||
}
|
||||
|
||||
#if HAVE_LIBCLIPPER
|
||||
/// \brief Construct a point using the values in clipper coordinate @a pt
|
||||
constexpr point_type(const clipper::Coord_orth &pt)
|
||||
: m_x(pt[0])
|
||||
, m_y(pt[1])
|
||||
@@ -398,6 +470,7 @@ struct point_type
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief Assign a point using the values in clipper coordinate @a rhs
|
||||
constexpr point_type &operator=(const clipper::Coord_orth &rhs)
|
||||
{
|
||||
m_x = rhs[0];
|
||||
@@ -407,6 +480,7 @@ struct point_type
|
||||
}
|
||||
#endif
|
||||
|
||||
/// \brief Assignment operator
|
||||
template <typename PF>
|
||||
constexpr point_type &operator=(const point_type<PF> &rhs)
|
||||
{
|
||||
@@ -416,18 +490,19 @@ struct point_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr value_type &get_x() { return m_x; }
|
||||
constexpr value_type get_x() const { return m_x; }
|
||||
constexpr void set_x(value_type x) { m_x = x; }
|
||||
constexpr value_type &get_x() { return m_x; } ///< Get a reference to x
|
||||
constexpr value_type get_x() const { return m_x; } ///< Get the value of x
|
||||
constexpr void set_x(value_type x) { m_x = x; } ///< Set the value of x to @a x
|
||||
|
||||
constexpr value_type &get_y() { return m_y; }
|
||||
constexpr value_type get_y() const { return m_y; }
|
||||
constexpr void set_y(value_type y) { m_y = y; }
|
||||
constexpr value_type &get_y() { return m_y; } ///< Get a reference to y
|
||||
constexpr value_type get_y() const { return m_y; } ///< Get the value of y
|
||||
constexpr void set_y(value_type y) { m_y = y; } ///< Set the value of y to @a y
|
||||
|
||||
constexpr value_type &get_z() { return m_z; }
|
||||
constexpr value_type get_z() const { return m_z; }
|
||||
constexpr void set_z(value_type z) { m_z = z; }
|
||||
constexpr value_type &get_z() { return m_z; } ///< Get a reference to z
|
||||
constexpr value_type get_z() const { return m_z; } ///< Get the value of z
|
||||
constexpr void set_z(value_type z) { m_z = z; } ///< Set the value of z to @a z
|
||||
|
||||
/// \brief add @a rhs
|
||||
constexpr point_type &operator+=(const point_type &rhs)
|
||||
{
|
||||
m_x += rhs.m_x;
|
||||
@@ -437,6 +512,7 @@ struct point_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief add @a d to all members
|
||||
constexpr point_type &operator+=(value_type d)
|
||||
{
|
||||
m_x += d;
|
||||
@@ -446,6 +522,14 @@ struct point_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Add the points @a lhs and @a rhs and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator+(const point_type &lhs, const point_type<F2> &rhs)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(lhs.m_x + rhs.m_x, lhs.m_y + rhs.m_y, lhs.m_z + rhs.m_z);
|
||||
}
|
||||
|
||||
/// \brief subtract @a rhs
|
||||
constexpr point_type &operator-=(const point_type &rhs)
|
||||
{
|
||||
m_x -= rhs.m_x;
|
||||
@@ -455,6 +539,7 @@ struct point_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief subtract @a d from all members
|
||||
constexpr point_type &operator-=(value_type d)
|
||||
{
|
||||
m_x -= d;
|
||||
@@ -464,6 +549,20 @@ struct point_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief Subtract the points @a lhs and @a rhs and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator-(const point_type &lhs, const point_type<F2> &rhs)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(lhs.m_x - rhs.m_x, lhs.m_y - rhs.m_y, lhs.m_z - rhs.m_z);
|
||||
}
|
||||
|
||||
/// \brief Return the negative copy of @a pt
|
||||
friend constexpr point_type operator-(const point_type &pt)
|
||||
{
|
||||
return point_type(-pt.m_x, -pt.m_y, -pt.m_z);
|
||||
}
|
||||
|
||||
/// \brief multiply all members with @a rhs
|
||||
constexpr point_type &operator*=(value_type rhs)
|
||||
{
|
||||
m_x *= rhs;
|
||||
@@ -472,6 +571,21 @@ struct point_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief multiply point @a pt with value @a f and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator*(const point_type &pt, F2 f)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(pt.m_x * f, pt.m_y * f, pt.m_z * f);
|
||||
}
|
||||
|
||||
/// \brief multiply point @a pt with value @a f and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator*(F2 f, const point_type &pt)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(pt.m_x * f, pt.m_y * f, pt.m_z * f);
|
||||
}
|
||||
|
||||
/// \brief divide all members by @a rhs
|
||||
constexpr point_type &operator/=(value_type rhs)
|
||||
{
|
||||
m_x /= rhs;
|
||||
@@ -480,6 +594,20 @@ struct point_type
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \brief divide point @a pt by value @a f and return the result
|
||||
template <typename F2>
|
||||
friend constexpr auto operator/(const point_type &pt, F2 f)
|
||||
{
|
||||
return point_type<std::common_type_t<value_type, F2>>(pt.m_x / f, pt.m_y / f, pt.m_z / f);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief looking at this point as a vector, normalise it which
|
||||
* means dividing all members by the length making the length
|
||||
* effectively 1.
|
||||
*
|
||||
* @return The previous length of this vector
|
||||
*/
|
||||
constexpr value_type normalize()
|
||||
{
|
||||
auto length = m_x * m_x + m_y * m_y + m_z * m_z;
|
||||
@@ -491,6 +619,7 @@ struct point_type
|
||||
return length;
|
||||
}
|
||||
|
||||
/// \brief Rotate this point using the quaterion @a q
|
||||
constexpr void rotate(const quaternion &q)
|
||||
{
|
||||
quaternion_type<value_type> p(0, m_x, m_y, m_z);
|
||||
@@ -502,6 +631,9 @@ struct point_type
|
||||
m_z = p.get_d();
|
||||
}
|
||||
|
||||
/// \brief Rotate this point using the quaterion @a q by first
|
||||
/// moving the point to @a pivot and after rotating moving it
|
||||
/// back
|
||||
constexpr void rotate(const quaternion &q, point_type pivot)
|
||||
{
|
||||
operator-=(pivot);
|
||||
@@ -510,97 +642,71 @@ struct point_type
|
||||
}
|
||||
|
||||
#if HAVE_LIBCLIPPER
|
||||
/// \brief Make it possible to pass a point to clipper functions expecting a clipper coordinate
|
||||
operator clipper::Coord_orth() const
|
||||
{
|
||||
return clipper::Coord_orth(m_x, m_y, m_z);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// \brief Allow access to this point as if it is a tuple of three const value_type's
|
||||
constexpr operator std::tuple<const value_type &, const value_type &, const value_type &>() const
|
||||
{
|
||||
return std::make_tuple(std::ref(m_x), std::ref(m_y), std::ref(m_z));
|
||||
}
|
||||
|
||||
/// \brief Allow access to this point as if it is a tuple of three value_type's
|
||||
constexpr operator std::tuple<value_type &, value_type &, value_type &>()
|
||||
{
|
||||
return std::make_tuple(std::ref(m_x), std::ref(m_y), std::ref(m_z));
|
||||
}
|
||||
|
||||
/// \brief Compare with @a rhs
|
||||
constexpr bool operator==(const point_type &rhs) const
|
||||
{
|
||||
return m_x == rhs.m_x and m_y == rhs.m_y and m_z == rhs.m_z;
|
||||
}
|
||||
|
||||
// consider point as a vector... perhaps I should rename point?
|
||||
|
||||
/// \brief looking at the point as if it is a vector, return the squared length
|
||||
constexpr value_type length_sq() const
|
||||
{
|
||||
return m_x * m_x + m_y * m_y + m_z * m_z;
|
||||
}
|
||||
|
||||
/// \brief looking at the point as if it is a vector, return the length
|
||||
constexpr value_type length() const
|
||||
{
|
||||
return std::sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
|
||||
return std::sqrt(length_sq());
|
||||
}
|
||||
|
||||
/// \brief Print out the point @a pt to @a os
|
||||
friend std::ostream &operator<<(std::ostream &os, const point_type &pt)
|
||||
{
|
||||
os << '(' << pt.m_x << ',' << pt.m_y << ',' << pt.m_z << ')';
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief By default we use points with float value_type
|
||||
using point = point_type<float>;
|
||||
|
||||
template <typename F>
|
||||
inline constexpr std::ostream &operator<<(std::ostream &os, const point_type<F> &pt)
|
||||
{
|
||||
os << '(' << pt.m_x << ',' << pt.m_y << ',' << pt.m_z << ')';
|
||||
return os;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline constexpr point_type<F> operator+(const point_type<F> &lhs, const point_type<F> &rhs)
|
||||
{
|
||||
return point_type<F>(lhs.m_x + rhs.m_x, lhs.m_y + rhs.m_y, lhs.m_z + rhs.m_z);
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline constexpr point_type<F> operator-(const point_type<F> &lhs, const point_type<F> &rhs)
|
||||
{
|
||||
return point_type<F>(lhs.m_x - rhs.m_x, lhs.m_y - rhs.m_y, lhs.m_z - rhs.m_z);
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline constexpr point_type<F> operator-(const point_type<F> &pt)
|
||||
{
|
||||
return point_type<F>(-pt.m_x, -pt.m_y, -pt.m_z);
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline constexpr point_type<F> operator*(const point_type<F> &pt, F f)
|
||||
{
|
||||
return point_type<F>(pt.m_x * f, pt.m_y * f, pt.m_z * f);
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline constexpr point_type<F> operator*(F f, const point_type<F> &pt)
|
||||
{
|
||||
return point_type<F>(pt.m_x * f, pt.m_y * f, pt.m_z * f);
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline constexpr point_type<F> operator/(const point_type<F> &pt, F f)
|
||||
{
|
||||
return point_type<F>(pt.m_x / f, pt.m_y / f, pt.m_z / f);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// several standard 3d operations
|
||||
|
||||
template <typename F>
|
||||
inline constexpr auto distance_squared(const point_type<F> &a, const point_type<F> &b)
|
||||
/// \brief return the squared distance between points @a a and @a b
|
||||
template <typename F1, typename F2>
|
||||
constexpr auto distance_squared(const point_type<F1> &a, const point_type<F2> &b)
|
||||
{
|
||||
return (a.m_x - b.m_x) * (a.m_x - b.m_x) +
|
||||
(a.m_y - b.m_y) * (a.m_y - b.m_y) +
|
||||
(a.m_z - b.m_z) * (a.m_z - b.m_z);
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline constexpr auto distance(const point_type<F> &a, const point_type<F> &b)
|
||||
/// \brief return the distance between points @a a and @a b
|
||||
template <typename F1, typename F2>
|
||||
constexpr auto distance(const point_type<F1> &a, const point_type<F2> &b)
|
||||
{
|
||||
return std::sqrt(
|
||||
(a.m_x - b.m_x) * (a.m_x - b.m_x) +
|
||||
@@ -608,20 +714,24 @@ inline constexpr auto distance(const point_type<F> &a, const point_type<F> &b)
|
||||
(a.m_z - b.m_z) * (a.m_z - b.m_z));
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline constexpr auto dot_product(const point_type<F> &a, const point_type<F> &b)
|
||||
/// \brief return the dot product between the vectors @a a and @a b
|
||||
template <typename F1, typename F2>
|
||||
inline constexpr auto dot_product(const point_type<F1> &a, const point_type<F2> &b)
|
||||
{
|
||||
return a.m_x * b.m_x + a.m_y * b.m_y + a.m_z * b.m_z;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline constexpr point_type<F> cross_product(const point_type<F> &a, const point_type<F> &b)
|
||||
/// \brief return the cross product between the vectors @a a and @a b
|
||||
template <typename F1, typename F2>
|
||||
inline constexpr auto cross_product(const point_type<F1> &a, const point_type<F2> &b)
|
||||
{
|
||||
return point_type<F>(a.m_y * b.m_z - b.m_y * a.m_z,
|
||||
return point_type<std::common_type_t<F1, F2>>(
|
||||
a.m_y * b.m_z - b.m_y * a.m_z,
|
||||
a.m_z * b.m_x - b.m_z * a.m_x,
|
||||
a.m_x * b.m_y - b.m_x * a.m_y);
|
||||
}
|
||||
|
||||
/// \brief return the angle in degrees between the vectors from point @a p2 to @a p1 and @a p2 to @a p3
|
||||
template <typename F>
|
||||
constexpr auto angle(const point_type<F> &p1, const point_type<F> &p2, const point_type<F> &p3)
|
||||
{
|
||||
@@ -631,6 +741,9 @@ constexpr auto angle(const point_type<F> &p1, const point_type<F> &p2, const poi
|
||||
return std::acos(dot_product(v1, v2) / (v1.length() * v2.length())) * 180 / kPI;
|
||||
}
|
||||
|
||||
/// \brief return the dihedral angle in degrees for the four points @a p1, @a p2, @a p3 and @a p4
|
||||
///
|
||||
/// See https://en.wikipedia.org/wiki/Dihedral_angle for an explanation of what a dihedral angle is
|
||||
template <typename F>
|
||||
constexpr auto dihedral_angle(const point_type<F> &p1, const point_type<F> &p2, const point_type<F> &p3, const point_type<F> &p4)
|
||||
{
|
||||
@@ -658,6 +771,7 @@ constexpr auto dihedral_angle(const point_type<F> &p1, const point_type<F> &p2,
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief return the cosinus angle for the four points @a p1, @a p2, @a p3 and @a p4
|
||||
template <typename F>
|
||||
constexpr auto cosinus_angle(const point_type<F> &p1, const point_type<F> &p2, const point_type<F> &p3, const point_type<F> &p4)
|
||||
{
|
||||
@@ -669,6 +783,7 @@ constexpr auto cosinus_angle(const point_type<F> &p1, const point_type<F> &p2, c
|
||||
return x > 0 ? dot_product(v12, v34) / std::sqrt(x) : 0;
|
||||
}
|
||||
|
||||
/// \brief return the distance from point @a p to the line from @a l1 to @a l2
|
||||
template <typename F>
|
||||
constexpr auto distance_point_to_line(const point_type<F> &l1, const point_type<F> &l2, const point_type<F> &p)
|
||||
{
|
||||
@@ -680,15 +795,19 @@ constexpr auto distance_point_to_line(const point_type<F> &l1, const point_type<
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// For e.g. simulated annealing, returns a new point that is moved in
|
||||
// a random direction with a distance randomly chosen from a normal
|
||||
// distribution with a stddev of offset.
|
||||
|
||||
/**
|
||||
* @brief For e.g. simulated annealing, returns a new point that is moved in
|
||||
* a random direction with a distance randomly chosen from a normal
|
||||
* distribution with a stddev of offset.
|
||||
*/
|
||||
point nudge(point p, float offset);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Return a quaternion created from angle @a angle and axis @a axis
|
||||
quaternion construct_from_angle_axis(float angle, point axis);
|
||||
|
||||
/// \brief Return a tuple of an angle and an axis for quaternion @a q
|
||||
std::tuple<double, point> quaternion_to_angle_axis(quaternion q);
|
||||
|
||||
/// @brief Given four points and an angle, return the quaternion required to rotate
|
||||
@@ -697,8 +816,12 @@ std::tuple<double, point> quaternion_to_angle_axis(quaternion q);
|
||||
quaternion construct_for_dihedral_angle(point p1, point p2, point p3, point p4,
|
||||
float angle, float esd);
|
||||
|
||||
point centroid(const std::vector<point> &Points);
|
||||
point center_points(std::vector<point> &Points);
|
||||
/// \brief Return the point that is the centroid of all the points in @a pts
|
||||
point centroid(const std::vector<point> &pts);
|
||||
|
||||
/// \brief Move all the points in @a pts so that their centroid is at the origin
|
||||
/// (0, 0, 0) and return the offset used (the former centroid)
|
||||
point center_points(std::vector<point> &pts);
|
||||
|
||||
/// \brief Returns how the two sets of points \a a and \b b can be aligned
|
||||
///
|
||||
@@ -712,38 +835,56 @@ quaternion align_points(const std::vector<point> &a, const std::vector<point> &b
|
||||
double RMSd(const std::vector<point> &a, const std::vector<point> &b);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Helper class to generate evenly divided points on a sphere
|
||||
// we use a fibonacci sphere to calculate even distribution of the dots
|
||||
|
||||
/**
|
||||
* @brief Helper class to generate evenly divided points on a sphere
|
||||
*
|
||||
* We use a fibonacci sphere to calculate even distribution of the dots
|
||||
*
|
||||
* @tparam N The number of points on the sphere is 2 * N + 1
|
||||
*/
|
||||
template <int N>
|
||||
class spherical_dots
|
||||
{
|
||||
public:
|
||||
/// \brief the number of points
|
||||
constexpr static int P = 2 * N * 1;
|
||||
|
||||
/// \brief the *weight* of the fibonacci sphere
|
||||
constexpr static double W = (4 * kPI) / P;
|
||||
|
||||
/// \brief the internal storage type
|
||||
using array_type = typename std::array<point, P>;
|
||||
|
||||
/// \brief iterator type
|
||||
using iterator = typename array_type::const_iterator;
|
||||
|
||||
/// \brief singleton instance
|
||||
static spherical_dots &instance()
|
||||
{
|
||||
static spherical_dots sInstance;
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
size_t size() const { return m_points.size(); }
|
||||
/// \brief The number of points
|
||||
size_t size() const { return P; }
|
||||
|
||||
/// \brief Access a point by index
|
||||
const point operator[](uint32_t inIx) const { return m_points[inIx]; }
|
||||
|
||||
/// \brief iterator pointing to the first point
|
||||
iterator begin() const { return m_points.begin(); }
|
||||
|
||||
/// \brief iterator pointing past the last point
|
||||
iterator end() const { return m_points.end(); }
|
||||
|
||||
double weight() const { return m_weight; }
|
||||
/// \brief return the *weight*,
|
||||
double weight() const { return W; }
|
||||
|
||||
spherical_dots()
|
||||
{
|
||||
const double
|
||||
kGoldenRatio = (1 + std::sqrt(5.0)) / 2;
|
||||
|
||||
m_weight = (4 * kPI) / P;
|
||||
|
||||
auto p = m_points.begin();
|
||||
|
||||
for (int32_t i = -N; i <= N; ++i)
|
||||
@@ -761,7 +902,6 @@ class spherical_dots
|
||||
|
||||
private:
|
||||
array_type m_points;
|
||||
double m_weight;
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
|
||||
@@ -30,6 +30,51 @@
|
||||
|
||||
#include <array>
|
||||
|
||||
/**
|
||||
* @file row.hpp
|
||||
*
|
||||
* The class cif::row should be an opaque type. It is used to store the
|
||||
* internal data per row in a category. You should use cif::row_handle
|
||||
* to get access to the contents in a row.
|
||||
*
|
||||
* One could think of rows as vectors of cif::item. But internally
|
||||
* that's not the case.
|
||||
*
|
||||
* You can access the values of stored items by name or index.
|
||||
* The return value of operator[] is an cif::item_handle object.
|
||||
*
|
||||
* @code {.cpp}
|
||||
* cif::category &atom_site = my_db["atom_site"];
|
||||
* cif::row_handle rh = atom_site.front();
|
||||
*
|
||||
* // by name:
|
||||
* std::string name = rh["label_atom_id"].as<std::string>();
|
||||
*
|
||||
* // by index:
|
||||
* uint16_t ix = atom_site.get_column_ix("label_atom_id");
|
||||
* assert(rh[ix].as<std::string() == name);
|
||||
* @endcode
|
||||
*
|
||||
* There some template magic here to allow easy extracting of data
|
||||
* from rows. This can be done using cif::tie e.g.:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* std::string name;
|
||||
* float x, y, z;
|
||||
*
|
||||
* cif::tie(name, x, y, z) = rh.get("label_atom_id", "cartn_x", "cartn_y", "cartn_z");
|
||||
* @endcode
|
||||
*
|
||||
* However, a more modern way uses structured binding:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* const auto &[name, x, y, z] = rh.get<std::string,float,float,float>("label_atom_id", "cartn_x", "cartn_y", "cartn_z");
|
||||
* @endcode
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
@@ -96,6 +141,8 @@ namespace detail
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/// \brief similar to std::tie, assign values to each element in @a v from the
|
||||
/// result of a get on a row_handle.
|
||||
template <typename... Ts>
|
||||
auto tie(Ts &...v)
|
||||
{
|
||||
@@ -110,11 +157,17 @@ class row : public std::vector<item_value>
|
||||
public:
|
||||
row() = default;
|
||||
|
||||
/**
|
||||
* @brief Return the item_value pointer for item at index @a ix
|
||||
*/
|
||||
item_value* get(uint16_t ix)
|
||||
{
|
||||
return ix < size() ? &data()[ix] : nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the const item_value pointer for item at index @a ix
|
||||
*/
|
||||
const item_value* get(uint16_t ix) const
|
||||
{
|
||||
return ix < size() ? &data()[ix] : nullptr;
|
||||
@@ -150,6 +203,7 @@ class row : public std::vector<item_value>
|
||||
class row_handle
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
friend struct item_handle;
|
||||
friend class category;
|
||||
friend class category_index;
|
||||
@@ -163,79 +217,119 @@ class row_handle
|
||||
row_handle &operator=(const row_handle &) = default;
|
||||
row_handle &operator=(row_handle &&) = default;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief constructor taking a category @a cat and a row @a r
|
||||
row_handle(const category &cat, const row &r)
|
||||
: m_category(const_cast<category *>(&cat))
|
||||
, m_row(const_cast<row *>(&r))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief return the category this row belongs to
|
||||
const category &get_category() const
|
||||
{
|
||||
return *m_category;
|
||||
}
|
||||
|
||||
/// \brief Return true if the row is empty or uninitialised
|
||||
bool empty() const
|
||||
{
|
||||
return m_category == nullptr or m_row == nullptr;
|
||||
}
|
||||
|
||||
/// \brief convenience method to test for empty()
|
||||
explicit operator bool() const
|
||||
{
|
||||
return not empty();
|
||||
}
|
||||
|
||||
/// \brief return a cif::item_handle to the item in column @a column_ix
|
||||
item_handle operator[](uint16_t column_ix)
|
||||
{
|
||||
return empty() ? item_handle::s_null_item : item_handle(column_ix, *this);
|
||||
}
|
||||
|
||||
/// \brief return a const cif::item_handle to the item in column @a column_ix
|
||||
const item_handle operator[](uint16_t column_ix) const
|
||||
{
|
||||
return empty() ? item_handle::s_null_item : item_handle(column_ix, const_cast<row_handle &>(*this));
|
||||
}
|
||||
|
||||
/// \brief return a cif::item_handle to the item in the column named @a column_name
|
||||
item_handle operator[](std::string_view column_name)
|
||||
{
|
||||
return empty() ? item_handle::s_null_item : item_handle(add_column(column_name), *this);
|
||||
}
|
||||
|
||||
/// \brief return a const cif::item_handle to the item in the column named @a column_name
|
||||
const item_handle operator[](std::string_view column_name) const
|
||||
{
|
||||
return empty() ? item_handle::s_null_item : item_handle(get_column_ix(column_name), const_cast<row_handle &>(*this));
|
||||
}
|
||||
|
||||
/// \brief Return an object that can be used in combination with cif::tie
|
||||
/// to assign the values for the columns @a columns
|
||||
template <typename... C>
|
||||
auto get(C... columns) const
|
||||
{
|
||||
return detail::get_row_result<C...>(*this, { get_column_ix(columns)... });
|
||||
}
|
||||
|
||||
/// \brief Return a tuple of values of types @a Ts for the columns @a columns
|
||||
template <typename... Ts, typename... C, std::enable_if_t<sizeof...(Ts) == sizeof...(C) and sizeof...(C) != 1, int> = 0>
|
||||
std::tuple<Ts...> get(C... columns) const
|
||||
{
|
||||
return detail::get_row_result<Ts...>(*this, { get_column_ix(columns)... });
|
||||
}
|
||||
|
||||
/// \brief Get the value of column @a column cast to type @a T
|
||||
template <typename T>
|
||||
T get(const char *column) const
|
||||
{
|
||||
return operator[](get_column_ix(column)).template as<T>();
|
||||
}
|
||||
|
||||
/// \brief assign each of the columns named in @a values to their respective value
|
||||
void assign(const std::vector<item> &values)
|
||||
{
|
||||
for (auto &value : values)
|
||||
assign(value, true);
|
||||
}
|
||||
|
||||
/** \brief assign the value @a value to the column named @a name
|
||||
*
|
||||
* If updateLinked it true, linked records are updated as well.
|
||||
* That means that if column @a name is part of the link definition
|
||||
* and the link results in a linked record in another category
|
||||
* this record in the linked category is updated as well.
|
||||
*
|
||||
* If validate is true, which is default, the assigned value is
|
||||
* checked to see if it conforms to the rules defined in the dictionary
|
||||
*/
|
||||
|
||||
void assign(std::string_view name, std::string_view value, bool updateLinked, bool validate = true)
|
||||
{
|
||||
assign(add_column(name), value, updateLinked, validate);
|
||||
}
|
||||
|
||||
/** \brief assign the value @a value to column at index @a column
|
||||
*
|
||||
* If updateLinked it true, linked records are updated as well.
|
||||
* That means that if column @a column is part of the link definition
|
||||
* and the link results in a linked record in another category
|
||||
* this record in the linked category is updated as well.
|
||||
*
|
||||
* If validate is true, which is default, the assigned value is
|
||||
* checked to see if it conforms to the rules defined in the dictionary
|
||||
*/
|
||||
|
||||
void assign(uint16_t column, std::string_view value, bool updateLinked, bool validate = true);
|
||||
|
||||
/// \brief compare two rows
|
||||
bool operator==(const row_handle &rhs) const { return m_category == rhs.m_category and m_row == rhs.m_row; }
|
||||
|
||||
/// \brief compare two rows
|
||||
bool operator!=(const row_handle &rhs) const { return m_category != rhs.m_category or m_row != rhs.m_row; }
|
||||
|
||||
private:
|
||||
@@ -267,9 +361,17 @@ class row_handle
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The class row_initializer is a list of cif::item's.
|
||||
*
|
||||
* This class is used to construct new rows, it allows to
|
||||
* group a list of item name and value pairs and pass it
|
||||
* in one go to the constructing function.
|
||||
*/
|
||||
class row_initializer : public std::vector<item>
|
||||
{
|
||||
public:
|
||||
/** @cond */
|
||||
friend class category;
|
||||
|
||||
row_initializer() = default;
|
||||
@@ -278,26 +380,38 @@ class row_initializer : public std::vector<item>
|
||||
row_initializer &operator=(const row_initializer &) = default;
|
||||
row_initializer &operator=(row_initializer &&) = default;
|
||||
|
||||
/** @endcond */
|
||||
|
||||
/// \brief constructor taking a std::initializer_list of items
|
||||
row_initializer(std::initializer_list<item> items)
|
||||
: std::vector<item>(items)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking a range of items
|
||||
template <typename ItemIter, std::enable_if_t<std::is_same_v<typename ItemIter::value_type, item>, int> = 0>
|
||||
row_initializer(ItemIter b, ItemIter e)
|
||||
: std::vector<item>(b, e)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor taking the values of an existing row
|
||||
row_initializer(row_handle rh);
|
||||
|
||||
|
||||
/// \brief set the value for item name @a name to @a value
|
||||
void set_value(std::string_view name, std::string_view value);
|
||||
|
||||
/// \brief set the value for item based on @a i
|
||||
void set_value(const item &i)
|
||||
{
|
||||
set_value(i.name(), i.value());
|
||||
}
|
||||
|
||||
/// \brief set the value for item name @a name to @a value, but only if the item did not have a value already
|
||||
void set_value_if_empty(std::string_view name, std::string_view value);
|
||||
|
||||
/// \brief set the value for item @a i, but only if the item did not have a value already
|
||||
void set_value_if_empty(const item &i)
|
||||
{
|
||||
set_value_if_empty(i.name(), i.value());
|
||||
|
||||
@@ -38,15 +38,18 @@
|
||||
#include <compare>
|
||||
#endif
|
||||
|
||||
/// \file cif++/symmetry.hpp
|
||||
/// This file contains code to do symmetry operations based on the
|
||||
/// operations as specified in the International Tables.
|
||||
/** \file cif++/symmetry.hpp
|
||||
*
|
||||
* This file contains code to do symmetry operations based on the
|
||||
* operations as specified in the International Tables.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Apply matrix transformation @a m on point @a pt and return the result
|
||||
inline point operator*(const matrix3x3<float> &m, const point &pt)
|
||||
{
|
||||
return {
|
||||
@@ -58,28 +61,40 @@ inline point operator*(const matrix3x3<float> &m, const point &pt)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief the space groups we know
|
||||
enum class space_group_name
|
||||
{
|
||||
full,
|
||||
xHM,
|
||||
Hall
|
||||
full, ///< The *full* spacegroup
|
||||
xHM, ///< The *xHM* spacegroup
|
||||
Hall ///< The *Hall* spacegroup
|
||||
};
|
||||
|
||||
/// \brief For each known spacegroup we define a structure like this
|
||||
struct space_group
|
||||
{
|
||||
const char *name;
|
||||
const char *xHM;
|
||||
const char *Hall;
|
||||
int nr;
|
||||
const char *name; ///< The name according to *full*
|
||||
const char *xHM; ///< The name according to *xHM*
|
||||
const char *Hall; ///< The name according to *Hall*
|
||||
int nr; ///< The number for this spacegroup
|
||||
};
|
||||
|
||||
/// \brief Global list of spacegroups
|
||||
extern CIFPP_EXPORT const space_group kSpaceGroups[];
|
||||
|
||||
/// \brief Global for the size of the list of spacegroups
|
||||
extern CIFPP_EXPORT const std::size_t kNrOfSpaceGroups;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Helper class to efficiently pack the data that
|
||||
* makes up a symmetry operation
|
||||
*
|
||||
*/
|
||||
|
||||
struct symop_data
|
||||
{
|
||||
/// \brief constructor
|
||||
constexpr symop_data(const std::array<int, 15> &data)
|
||||
: m_packed((data[0] bitand 0x03ULL) << 34 bitor
|
||||
(data[1] bitand 0x03ULL) << 32 bitor
|
||||
@@ -99,27 +114,32 @@ struct symop_data
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief compare
|
||||
bool operator==(const symop_data &rhs) const
|
||||
{
|
||||
return m_packed == rhs.m_packed;
|
||||
}
|
||||
|
||||
/// \brief sorting order
|
||||
bool operator<(const symop_data &rhs) const
|
||||
{
|
||||
return m_packed < rhs.m_packed;
|
||||
}
|
||||
|
||||
/// \brief return an int representing the value stored in the two bits at offset @a offset
|
||||
inline constexpr int unpack3(int offset) const
|
||||
{
|
||||
int result = (m_packed >> offset) bitand 0x03;
|
||||
return result == 3 ? -1 : result;
|
||||
}
|
||||
|
||||
/// \brief return an int representing the value stored in the three bits at offset @a offset
|
||||
inline constexpr int unpack7(int offset) const
|
||||
{
|
||||
return (m_packed >> offset) bitand 0x07;
|
||||
}
|
||||
|
||||
/// \brief return an array of 15 ints representing the values stored
|
||||
constexpr std::array<int, 15> data() const
|
||||
{
|
||||
return {
|
||||
@@ -154,8 +174,14 @@ struct symop_data
|
||||
uint64_t m_packed;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief For each symmetry operator defined in the international tables
|
||||
* we have an entry in this struct type. It contains the spacegroup
|
||||
* number, the symmetry operations and the rotational number.
|
||||
*/
|
||||
struct symop_datablock
|
||||
{
|
||||
/// \brief constructor
|
||||
constexpr symop_datablock(int spacegroup, int rotational_number, const std::array<int, 15> &rt_data)
|
||||
: m_v((spacegroup bitand 0xffffULL) << 48 bitor
|
||||
(rotational_number bitand 0xffULL) << 40 bitor
|
||||
@@ -163,9 +189,9 @@ struct symop_datablock
|
||||
{
|
||||
}
|
||||
|
||||
uint16_t spacegroup() const { return m_v >> 48; }
|
||||
symop_data symop() const { return symop_data(m_v); }
|
||||
uint8_t rotational_number() const { return (m_v >> 40) bitand 0xff; }
|
||||
uint16_t spacegroup() const { return m_v >> 48; } ///< Return the spacegroup
|
||||
symop_data symop() const { return symop_data(m_v); } ///< Return the symmetry operation
|
||||
uint8_t rotational_number() const { return (m_v >> 40) bitand 0xff; } ///< Return the rotational_number
|
||||
|
||||
private:
|
||||
uint64_t m_v;
|
||||
@@ -173,7 +199,10 @@ struct symop_datablock
|
||||
|
||||
static_assert(sizeof(symop_datablock) == sizeof(uint64_t), "Size of symop_data is wrong");
|
||||
|
||||
/// \brief Global containing the list of known symmetry operations
|
||||
extern CIFPP_EXPORT const symop_datablock kSymopNrTable[];
|
||||
|
||||
/// \brief Size of the list of known symmetry operations
|
||||
extern CIFPP_EXPORT const std::size_t kSymopNrTableSize;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -186,13 +215,21 @@ class spacegroup;
|
||||
class rtop;
|
||||
struct sym_op;
|
||||
|
||||
/** @brief A class that encapsulates the symmetry operations as used in PDB files,
|
||||
* i.e. a rotational number and a translation vector.
|
||||
*
|
||||
* The syntax in string format follows the syntax as used in mmCIF files, i.e.
|
||||
* rotational number followed by underscore and the three translations where 5 is
|
||||
* no movement.
|
||||
*
|
||||
* So the string 1_555 means no symmetry movement at all since the rotational number
|
||||
* 1 always corresponds to the symmetry operation [x, y, z].
|
||||
*/
|
||||
|
||||
/// @brief A class that encapsulates the symmetry operations as used in PDB files, i.e. a rotational number and a translation vector
|
||||
/// The syntax in string format follows the syntax as used in mmCIF files, i.e. rotational number followed by underscore and the
|
||||
/// three translations where 5 is no movement.
|
||||
struct sym_op
|
||||
{
|
||||
public:
|
||||
/// \brief constructor
|
||||
sym_op(uint8_t nr = 1, uint8_t ta = 5, uint8_t tb = 5, uint8_t tc = 5)
|
||||
: m_nr(nr)
|
||||
, m_ta(ta)
|
||||
@@ -201,47 +238,68 @@ struct sym_op
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief construct a sym_op based on the contents encoded in string @a s
|
||||
explicit sym_op(std::string_view s);
|
||||
|
||||
/** @cond */
|
||||
sym_op(const sym_op &) = default;
|
||||
sym_op(sym_op &&) = default;
|
||||
sym_op &operator=(const sym_op &) = default;
|
||||
sym_op &operator=(sym_op &&) = default;
|
||||
/** @endcond */
|
||||
|
||||
/// \brief return true if this sym_op is the identity operator
|
||||
constexpr bool is_identity() const
|
||||
{
|
||||
return m_nr == 1 and m_ta == 5 and m_tb == 5 and m_tc == 5;
|
||||
}
|
||||
|
||||
/// \brief quick test for unequal to identity
|
||||
explicit constexpr operator bool() const
|
||||
{
|
||||
return not is_identity();
|
||||
}
|
||||
|
||||
/// \brief return the content encoded in a string
|
||||
std::string string() const;
|
||||
|
||||
#if defined(__cpp_impl_three_way_comparison)
|
||||
/// \brief a default spaceship operator
|
||||
constexpr auto operator<=>(const sym_op &rhs) const = default;
|
||||
#else
|
||||
/// \brief a default equals operator
|
||||
constexpr bool operator==(const sym_op &rhs) const
|
||||
{
|
||||
return m_nr == rhs.m_nr and m_ta == rhs.m_ta and m_tb == rhs.m_tb and m_tc == rhs.m_tc;
|
||||
}
|
||||
|
||||
/// \brief a default not-equals operator
|
||||
constexpr bool operator!=(const sym_op &rhs) const
|
||||
{
|
||||
return not operator==(rhs);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// @cond
|
||||
uint8_t m_nr;
|
||||
uint8_t m_ta, m_tb, m_tc;
|
||||
/// @endcond
|
||||
};
|
||||
|
||||
static_assert(sizeof(sym_op) == 4, "Sym_op should be four bytes");
|
||||
|
||||
namespace literals
|
||||
{
|
||||
/**
|
||||
* @brief This operator allows you to write code like this:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* using namespace cif::literals;
|
||||
*
|
||||
* cif::sym_op so = "1_555"_symop;
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
inline sym_op operator""_symop(const char *text, size_t length)
|
||||
{
|
||||
return sym_op({ text, length });
|
||||
@@ -251,17 +309,33 @@ namespace literals
|
||||
// --------------------------------------------------------------------
|
||||
// The transformation class
|
||||
|
||||
/**
|
||||
* @brief A class you can use to apply symmetry transformations on points
|
||||
*
|
||||
* Transformations consist of two operations, a matrix transformation which
|
||||
* is often a rotation followed by a translation.
|
||||
*
|
||||
* In case the matrix transformation is a pure rotation a quaternion
|
||||
* is created to do the actual calculations. That's faster and more
|
||||
* precise.
|
||||
*/
|
||||
class transformation
|
||||
{
|
||||
public:
|
||||
/// \brief constructor taking a symop_data object @a data
|
||||
transformation(const symop_data &data);
|
||||
|
||||
/// \brief constructor taking a rotation matrix @a r and a translation vector @a t
|
||||
transformation(const matrix3x3<float> &r, const cif::point &t);
|
||||
|
||||
/** @cond */
|
||||
transformation(const transformation &) = default;
|
||||
transformation(transformation &&) = default;
|
||||
transformation &operator=(const transformation &) = default;
|
||||
transformation &operator=(transformation &&) = default;
|
||||
/** @endcond */
|
||||
|
||||
/// \brief operator() to perform the transformation on point @a pt and return the result
|
||||
point operator()(point pt) const
|
||||
{
|
||||
if (m_q)
|
||||
@@ -272,9 +346,13 @@ class transformation
|
||||
return pt + m_translation;
|
||||
}
|
||||
|
||||
/// \brief return a transformation object that is the result of applying @a rhs after @a lhs
|
||||
friend transformation operator*(const transformation &lhs, const transformation &rhs);
|
||||
|
||||
/// \brief return the inverse transformation for @a t
|
||||
friend transformation inverse(const transformation &t);
|
||||
|
||||
/// \brief return the inverse tranformation for this
|
||||
transformation operator-() const
|
||||
{
|
||||
return inverse(*this);
|
||||
@@ -283,7 +361,6 @@ class transformation
|
||||
friend class spacegroup;
|
||||
|
||||
private:
|
||||
|
||||
// Most rotation matrices provided by the International Tables
|
||||
// are really rotation matrices, in those cases we can construct
|
||||
// a quaternion. Unfortunately, that doesn't work for all of them
|
||||
@@ -298,22 +375,30 @@ class transformation
|
||||
// --------------------------------------------------------------------
|
||||
// class cell
|
||||
|
||||
/**
|
||||
* @brief The cell class describes the dimensions and angles of a unit cell
|
||||
* in a crystal
|
||||
*/
|
||||
|
||||
class cell
|
||||
{
|
||||
public:
|
||||
/// \brief constructor
|
||||
cell(float a, float b, float c, float alpha = 90.f, float beta = 90.f, float gamma = 90.f);
|
||||
|
||||
/// \brief constructor that takes the appropriate values from the *cell* category in datablock @a db
|
||||
cell(const datablock &db);
|
||||
|
||||
float get_a() const { return m_a; }
|
||||
float get_b() const { return m_b; }
|
||||
float get_c() const { return m_c; }
|
||||
float get_a() const { return m_a; } ///< return dimension a
|
||||
float get_b() const { return m_b; } ///< return dimension b
|
||||
float get_c() const { return m_c; } ///< return dimension c
|
||||
|
||||
float get_alpha() const { return m_alpha; }
|
||||
float get_beta() const { return m_beta; }
|
||||
float get_gamma() const { return m_gamma; }
|
||||
float get_alpha() const { return m_alpha; } ///< return angle alpha
|
||||
float get_beta() const { return m_beta; } ///< return angle beta
|
||||
float get_gamma() const { return m_gamma; } ///< return angle gamma
|
||||
|
||||
matrix3x3<float> get_orthogonal_matrix() const { return m_orthogonal; }
|
||||
matrix3x3<float> get_fractional_matrix() const { return m_fractional; }
|
||||
matrix3x3<float> get_orthogonal_matrix() const { return m_orthogonal; } ///< return the matrix to use to transform coordinates from fractional to orthogonal
|
||||
matrix3x3<float> get_fractional_matrix() const { return m_fractional; } ///< return the matrix to use to transform coordinates from orthogonal to fractional
|
||||
|
||||
private:
|
||||
void init();
|
||||
@@ -324,36 +409,55 @@ class cell
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/// \brief Return the spacegroup number from the *symmetry* category in datablock @a db
|
||||
int get_space_group_number(const datablock &db);
|
||||
|
||||
/// \brief Return the spacegroup number for spacegroup named @a spacegroup
|
||||
int get_space_group_number(std::string_view spacegroup);
|
||||
|
||||
/// \brief Return the spacegroup number for spacegroup named @a spacegroup assuming space_group_name @a type
|
||||
int get_space_group_number(std::string_view spacegroup, space_group_name type);
|
||||
|
||||
/**
|
||||
* @brief class to encapsulate the list of transformations making up a spacegroup
|
||||
*
|
||||
*/
|
||||
class spacegroup : public std::vector<transformation>
|
||||
{
|
||||
public:
|
||||
/// \brief constructor using the information in the *symmetry* category in datablock @a db
|
||||
spacegroup(const datablock &db)
|
||||
: spacegroup(get_space_group_number(db))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// \brief constructor using the spacegroup named @a name
|
||||
spacegroup(std::string_view name)
|
||||
: spacegroup(get_space_group_number(name))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor using the spacegroup named @a name assuming space_group_name @a type
|
||||
spacegroup(std::string_view name, space_group_name type)
|
||||
: spacegroup(get_space_group_number(name, type))
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor using the spacegroup number @a nr
|
||||
spacegroup(int nr);
|
||||
|
||||
int get_nr() const { return m_nr; }
|
||||
std::string get_name() const;
|
||||
int get_nr() const { return m_nr; } ///< Return the nr
|
||||
std::string get_name() const; ///< Return the name
|
||||
|
||||
/** \brief perform a spacegroup operation on point @a pt using
|
||||
* cell @a c and sym_op @a symop
|
||||
*/
|
||||
|
||||
point operator()(const point &pt, const cell &c, sym_op symop) const;
|
||||
|
||||
/** \brief perform an inverse spacegroup operation on point @a pt using
|
||||
* cell @a c and sym_op @a symop
|
||||
*/
|
||||
point inverse(const point &pt, const cell &c, sym_op symop) const;
|
||||
|
||||
private:
|
||||
@@ -362,42 +466,55 @@ class spacegroup : public std::vector<transformation>
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// A crystal combines a cell and a spacegroup.
|
||||
/**
|
||||
* @brief A crystal combines a cell and a spacegroup.
|
||||
*
|
||||
* The information in cell and spacegroup together make up all
|
||||
* information you need to do symmetry calculations in a crystal
|
||||
*/
|
||||
|
||||
class crystal
|
||||
{
|
||||
public:
|
||||
/// \brief constructor using the information found in datablock @a db
|
||||
crystal(const datablock &db)
|
||||
: m_cell(db)
|
||||
, m_spacegroup(db)
|
||||
{
|
||||
}
|
||||
|
||||
/// \brief constructor using cell @a c and spacegroup @a sg
|
||||
crystal(const cell &c, const spacegroup &sg)
|
||||
: m_cell(c)
|
||||
, m_spacegroup(sg)
|
||||
{
|
||||
}
|
||||
|
||||
/** @cond */
|
||||
crystal(const crystal &) = default;
|
||||
crystal(crystal &&) = default;
|
||||
crystal &operator=(const crystal &) = default;
|
||||
crystal &operator=(crystal &&) = default;
|
||||
/** @endcond */
|
||||
|
||||
const cell &get_cell() const { return m_cell; }
|
||||
const spacegroup &get_spacegroup() const { return m_spacegroup; }
|
||||
const cell &get_cell() const { return m_cell; } ///< Return the cell
|
||||
const spacegroup &get_spacegroup() const { return m_spacegroup; } ///< Return the spacegroup
|
||||
|
||||
/// \brief Return the symmetry copy of point @a pt using symmetry operation @a symop
|
||||
point symmetry_copy(const point &pt, sym_op symop) const
|
||||
{
|
||||
return m_spacegroup(pt, m_cell, symop);
|
||||
}
|
||||
|
||||
/// \brief Return the symmetry copy of point @a pt using the inverse of symmetry operation @a symop
|
||||
point inverse_symmetry_copy(const point &pt, sym_op symop) const
|
||||
{
|
||||
return m_spacegroup.inverse(pt, m_cell, symop);
|
||||
}
|
||||
|
||||
std::tuple<float,point,sym_op> closest_symmetry_copy(point a, point b) const;
|
||||
|
||||
/// \brief Return a tuple consisting of distance, new location and symmetry operation
|
||||
/// for the point @a b with respect to point @a a.
|
||||
std::tuple<float, point, sym_op> closest_symmetry_copy(point a, point b) const;
|
||||
|
||||
private:
|
||||
cell m_cell;
|
||||
@@ -407,11 +524,13 @@ class crystal
|
||||
// --------------------------------------------------------------------
|
||||
// Symmetry operations on points
|
||||
|
||||
/// \brief convenience function returning the fractional point @a pt in orthogonal coordinates for cell @a c
|
||||
inline point orthogonal(const point &pt, const cell &c)
|
||||
{
|
||||
return c.get_orthogonal_matrix() * pt;
|
||||
}
|
||||
|
||||
/// \brief convenience function returning the orthogonal point @a pt in fractional coordinates for cell @a c
|
||||
inline point fractional(const point &pt, const cell &c)
|
||||
{
|
||||
return c.get_fractional_matrix() * pt;
|
||||
|
||||
@@ -44,6 +44,12 @@
|
||||
#include <zeep/type-traits.hpp>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \file text.hpp
|
||||
*
|
||||
* Various text manipulating routines
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
@@ -52,18 +58,40 @@ namespace cif
|
||||
// some basic utilities: Since we're using ASCII input only, we define for optimisation
|
||||
// our own case conversion routines.
|
||||
|
||||
/// \brief return whether string @a is equal to string @a b ignoring changes in character case
|
||||
bool iequals(std::string_view a, std::string_view b);
|
||||
|
||||
/// \brief compare string @a is to string @a b ignoring changes in character case
|
||||
int icompare(std::string_view a, std::string_view b);
|
||||
|
||||
/// \brief return whether string @a is equal to string @a b ignoring changes in character case
|
||||
bool iequals(const char *a, const char *b);
|
||||
|
||||
/// \brief compare string @a is to string @a b ignoring changes in character case
|
||||
int icompare(const char *a, const char *b);
|
||||
|
||||
/// \brief convert the string @a s to lower case in situ
|
||||
void to_lower(std::string &s);
|
||||
|
||||
/// \brief return a lower case copy of string @a s
|
||||
std::string to_lower_copy(std::string_view s);
|
||||
|
||||
/// \brief convert the string @a s to upper case in situ
|
||||
void to_upper(std::string &s);
|
||||
// std::string toUpperCopy(const std::string &s);
|
||||
|
||||
/**
|
||||
* @brief Join the strings in the range [ @a a, @a e ) using
|
||||
* @a sep as separator
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* std::vector<std::string> v{ "aap", "noot", "mies" };
|
||||
*
|
||||
* assert(cif::join(v.begin(), v.end(), ", ") == "aap, noot, mies");
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
template <typename IterType>
|
||||
std::string join(IterType b, IterType e, std::string_view sep)
|
||||
{
|
||||
@@ -91,12 +119,41 @@ std::string join(IterType b, IterType e, std::string_view sep)
|
||||
return s.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Join the strings in the array @a arr using @a sep as separator
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* std::list<std::string> v{ "aap", "noot", "mies" };
|
||||
*
|
||||
* assert(cif::join(v, ", ") == "aap, noot, mies");
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
template <typename V>
|
||||
std::string join(const V &arr, std::string_view sep)
|
||||
{
|
||||
return join(arr.begin(), arr.end(), sep);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Split the string in @a s based on the characters in @a separators
|
||||
*
|
||||
* Each of the characters in @a separators induces a split.
|
||||
*
|
||||
* When suppress_empty is true, empty strings are not produced in the
|
||||
* resulting array.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* auto v = cif::split("aap:noot,,mies", ":,", true);
|
||||
*
|
||||
* assert(v == std::vector{"aap", "noot", "mies"});
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
template <typename StringType = std::string_view>
|
||||
std::vector<StringType> split(std::string_view s, std::string_view separators, bool suppress_empty = false)
|
||||
{
|
||||
@@ -124,15 +181,23 @@ std::vector<StringType> split(std::string_view s, std::string_view separators, b
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Replace all occurrences of @a what in string @a s with the string @a with
|
||||
*
|
||||
* The string @a with may be empty in which case each occurrence of @a what is simply
|
||||
* deleted.
|
||||
*/
|
||||
void replace_all(std::string &s, std::string_view what, std::string_view with = {});
|
||||
|
||||
#if defined(__cpp_lib_starts_ends_with)
|
||||
|
||||
/// \brief return whether string @a s starts with @a with
|
||||
inline bool starts_with(std::string s, std::string_view with)
|
||||
{
|
||||
return s.starts_with(with);
|
||||
}
|
||||
|
||||
/// \brief return whether string @a s ends with @a with
|
||||
inline bool ends_with(std::string_view s, std::string_view with)
|
||||
{
|
||||
return s.ends_with(with);
|
||||
@@ -140,11 +205,13 @@ inline bool ends_with(std::string_view s, std::string_view with)
|
||||
|
||||
#else
|
||||
|
||||
/// \brief return whether string @a s starts with @a with
|
||||
inline bool starts_with(std::string s, std::string_view with)
|
||||
{
|
||||
return s.compare(0, with.length(), with) == 0;
|
||||
}
|
||||
|
||||
/// \brief return whether string @a s ends with @a with
|
||||
inline bool ends_with(std::string_view s, std::string_view with)
|
||||
{
|
||||
return s.length() >= with.length() and s.compare(s.length() - with.length(), with.length(), with) == 0;
|
||||
@@ -154,6 +221,7 @@ inline bool ends_with(std::string_view s, std::string_view with)
|
||||
|
||||
#if defined(__cpp_lib_string_contains)
|
||||
|
||||
/// \brief return whether string @a s contains @a q
|
||||
inline bool contains(std::string_view s, std::string_view q)
|
||||
{
|
||||
return s.contains(q);
|
||||
@@ -161,6 +229,7 @@ inline bool contains(std::string_view s, std::string_view q)
|
||||
|
||||
#else
|
||||
|
||||
/// \brief return whether string @a s contains @a q
|
||||
inline bool contains(std::string_view s, std::string_view q)
|
||||
{
|
||||
return s.find(q) != std::string_view::npos;
|
||||
@@ -168,33 +237,51 @@ inline bool contains(std::string_view s, std::string_view q)
|
||||
|
||||
#endif
|
||||
|
||||
/// \brief return whether string @a s contains @a q ignoring character case
|
||||
bool icontains(std::string_view s, std::string_view q);
|
||||
|
||||
/// \brief trim white space at the start of string @a s in situ
|
||||
void trim_left(std::string &s);
|
||||
|
||||
/// \brief trim white space at the end of string @a s in situ
|
||||
void trim_right(std::string &s);
|
||||
|
||||
/// \brief trim white space at both the start and the end of string @a s in situ
|
||||
void trim(std::string &s);
|
||||
|
||||
/// \brief return a string trimmed of white space at the start of string @a s
|
||||
std::string trim_left_copy(std::string_view s);
|
||||
|
||||
/// \brief return a string trimmed of white space at the end of string @a s
|
||||
std::string trim_right_copy(std::string_view s);
|
||||
|
||||
/// \brief return a string trimmed of white space at both the start and the end of string @a s
|
||||
std::string trim_copy(std::string_view s);
|
||||
|
||||
// To make life easier, we also define iless and iset using iequals
|
||||
|
||||
/// \brief an operator object you can use to compare strings ignoring their character case
|
||||
struct iless
|
||||
{
|
||||
/// \brief return the result of icompare for @a a and @a b
|
||||
bool operator()(const std::string &a, const std::string &b) const
|
||||
{
|
||||
return icompare(a, b) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::set<std::string, iless> iset;
|
||||
|
||||
/// iset is a std::set of std::string but with a comparator that
|
||||
/// ignores character case.
|
||||
using iset = std::set<std::string, iless>;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// This really makes a difference, having our own tolower routines
|
||||
|
||||
/// \brief global list containing the lower case version of each ASCII character
|
||||
extern CIFPP_EXPORT const uint8_t kCharToLowerMap[256];
|
||||
|
||||
/// \brief a very fast tolower implementation
|
||||
inline char tolower(int ch)
|
||||
{
|
||||
return static_cast<char>(kCharToLowerMap[static_cast<uint8_t>(ch)]);
|
||||
@@ -202,22 +289,37 @@ inline char tolower(int ch)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** \brief return a tuple consisting of the category and item name for @a tag
|
||||
*
|
||||
* The category name is stripped of its leading underscore character.
|
||||
*
|
||||
* If no dot character was found, the category name is empty. That's for
|
||||
* cif 1.0 formatted data.
|
||||
*/
|
||||
|
||||
std::tuple<std::string, std::string> split_tag_name(std::string_view tag);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// generate a cif name, mainly used to generate asym_id's
|
||||
|
||||
/// \brief generate a cif name, used e.g. to generate asym_id's
|
||||
std::string cif_id_for_number(int number);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// custom wordwrapping routine
|
||||
|
||||
/** \brief custom word wrapping routine.
|
||||
*
|
||||
* Wrap the text in @a text based on a maximum line width @a width using
|
||||
* a dynamic programming approach to get the most efficient filling of
|
||||
* the space.
|
||||
*/
|
||||
std::vector<std::string> word_wrap(const std::string &text, size_t width);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/// std::from_chars for floating point types.
|
||||
/// \brief std::from_chars for floating point types.
|
||||
///
|
||||
/// These are optional, there's a selected_charconv class below that selects
|
||||
/// the best option to used based on support by the stl library
|
||||
/// the best option to use based on support by the stl library.
|
||||
///
|
||||
/// I.e. that in case of GNU < 12 (or something) the cif implementation will
|
||||
/// be used, all other cases will use the stl version.
|
||||
|
||||
@@ -342,6 +444,7 @@ std::from_chars_result from_chars(const char *first, const char *last, FloatType
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief duplication of std::chars_format for deficient STL implementations
|
||||
enum class chars_format
|
||||
{
|
||||
scientific = 1,
|
||||
@@ -350,6 +453,7 @@ enum class chars_format
|
||||
general = fixed | scientific
|
||||
};
|
||||
|
||||
/// \brief a simplistic implementation of std::to_chars for old STL implementations
|
||||
template <typename FloatType, std::enable_if_t<std::is_floating_point_v<FloatType>, int> = 0>
|
||||
std::to_chars_result to_chars(char *first, char *last, FloatType &value, chars_format fmt)
|
||||
{
|
||||
@@ -389,6 +493,7 @@ std::to_chars_result to_chars(char *first, char *last, FloatType &value, chars_f
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief a simplistic implementation of std::to_chars for old STL implementations
|
||||
template <typename FloatType, std::enable_if_t<std::is_floating_point_v<FloatType>, int> = 0>
|
||||
std::to_chars_result to_chars(char *first, char *last, FloatType &value, chars_format fmt, int precision)
|
||||
{
|
||||
@@ -428,37 +533,50 @@ std::to_chars_result to_chars(char *first, char *last, FloatType &value, chars_f
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief class that uses our implementation of std::from_chars and std::to_chars
|
||||
template <typename T>
|
||||
struct my_charconv
|
||||
{
|
||||
/// @brief Simply call our version of std::from_chars
|
||||
static std::from_chars_result from_chars(const char *a, const char *b, T &d)
|
||||
{
|
||||
return cif::from_chars(a, b, d);
|
||||
}
|
||||
|
||||
/// @brief Simply call our version of std::to_chars
|
||||
static std::to_chars_result to_chars(char *first, char *last, T &value, chars_format fmt)
|
||||
{
|
||||
return cif::to_chars(first, last, value, fmt);
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief class that uses the STL implementation of std::from_chars and std::to_chars
|
||||
template <typename T>
|
||||
struct std_charconv
|
||||
{
|
||||
/// @brief Simply call std::from_chars
|
||||
static std::from_chars_result from_chars(const char *a, const char *b, T &d)
|
||||
{
|
||||
return std::from_chars(a, b, d);
|
||||
}
|
||||
|
||||
/// @brief Simply call std::to_chars
|
||||
static std::to_chars_result to_chars(char *first, char *last, T &value, chars_format fmt)
|
||||
{
|
||||
return std::to_chars(first, last, value, fmt);
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief helper to find a from_chars function
|
||||
template <typename T>
|
||||
using from_chars_function = decltype(std::from_chars(std::declval<const char *>(), std::declval<const char *>(), std::declval<T &>()));
|
||||
|
||||
/**
|
||||
* @brief Helper to select the best implementation of charconv based on availability of the
|
||||
* function in the std:: namespace
|
||||
*
|
||||
* @tparam T The type for which we want to find a from_chars/to_chars function
|
||||
*/
|
||||
template <typename T>
|
||||
using selected_charconv = typename std::conditional_t<std::experimental::is_detected_v<from_chars_function, T>, std_charconv<T>, my_charconv<T>>;
|
||||
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
@@ -29,11 +29,18 @@
|
||||
#include "cif++/exports.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
#ifndef STDOUT_FILENO
|
||||
/// @brief For systems that lack this value
|
||||
#define STDOUT_FILENO 1
|
||||
#endif
|
||||
|
||||
#ifndef STDERR_FILENO
|
||||
/// @brief For systems that lack this value
|
||||
#define STDERR_FILENO 2
|
||||
#endif
|
||||
|
||||
#if _WIN32
|
||||
#include <io.h>
|
||||
#define isatty _isatty
|
||||
@@ -49,122 +56,271 @@
|
||||
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING 1
|
||||
#endif
|
||||
|
||||
/** \file utilities.hpp
|
||||
*
|
||||
* This file contains code that is very generic in nature like a progress_bar
|
||||
* and classes you can use to colourise output text.
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief The global variable VERBOSE contains the level of verbosity
|
||||
* requested. A value of 0 is normal, with some output on error conditions.
|
||||
* A value > 0 will result in more output, the higher the value, the more
|
||||
* output. A value < 0 will make the library silent, even in error
|
||||
* conditions.
|
||||
*/
|
||||
extern CIFPP_EXPORT int VERBOSE;
|
||||
|
||||
// the git 'build' number
|
||||
/// return the git 'build' number
|
||||
std::string get_version_nr();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Code helping with terminal i/o
|
||||
|
||||
/// return the width of the current output terminal, or 80 if it cannot be determined
|
||||
uint32_t get_terminal_width();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Path of the current executable
|
||||
|
||||
std::string get_executable_path();
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// some manipulators to write coloured text to terminals
|
||||
|
||||
enum StringColour
|
||||
namespace colour
|
||||
{
|
||||
scBLACK = 0,
|
||||
scRED,
|
||||
scGREEN,
|
||||
scYELLOW,
|
||||
scBLUE,
|
||||
scMAGENTA,
|
||||
scCYAN,
|
||||
scWHITE,
|
||||
scNONE = 9
|
||||
};
|
||||
|
||||
template <typename String, typename CharT>
|
||||
struct ColouredString
|
||||
{
|
||||
static_assert(std::is_reference<String>::value or std::is_pointer<String>::value, "String type must be pointer or reference");
|
||||
|
||||
ColouredString(String s, StringColour fore, StringColour back, bool bold = true)
|
||||
: m_s(s)
|
||||
, m_fore(fore)
|
||||
, m_back(back)
|
||||
, m_bold(bold)
|
||||
/// @brief The defined colours
|
||||
enum colour_type
|
||||
{
|
||||
}
|
||||
black = 0,
|
||||
red,
|
||||
green,
|
||||
yellow,
|
||||
blue,
|
||||
magenta,
|
||||
cyan,
|
||||
white,
|
||||
none = 9
|
||||
};
|
||||
|
||||
ColouredString &operator=(const ColouredString &) = delete;
|
||||
|
||||
String m_s;
|
||||
StringColour m_fore, m_back;
|
||||
bool m_bold;
|
||||
};
|
||||
|
||||
template <typename CharT, typename Traits>
|
||||
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const ColouredString<const CharT *, CharT> &s)
|
||||
{
|
||||
if (isatty(STDOUT_FILENO))
|
||||
/// @brief The defined styles
|
||||
enum style_type
|
||||
{
|
||||
std::basic_ostringstream<CharT, Traits> ostr;
|
||||
ostr << "\033[" << (30 + s.m_fore) << ';' << (s.m_bold ? "1" : "22") << ';' << (40 + s.m_back) << 'm'
|
||||
<< s.m_s
|
||||
<< "\033[0m";
|
||||
bold = 1,
|
||||
underlined = 4,
|
||||
blink = 5,
|
||||
inverse = 7,
|
||||
regular = 22,
|
||||
};
|
||||
|
||||
return os << ostr.str();
|
||||
}
|
||||
else
|
||||
return os << s.m_s;
|
||||
}
|
||||
|
||||
template <typename CharT, typename Traits, typename String>
|
||||
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const ColouredString<String, CharT> &s)
|
||||
{
|
||||
if (isatty(STDOUT_FILENO))
|
||||
namespace detail
|
||||
{
|
||||
std::basic_ostringstream<CharT, Traits> ostr;
|
||||
ostr << "\033[" << (30 + s.m_fore) << ';' << (s.m_bold ? "1" : "22") << ';' << (40 + s.m_back) << 'm'
|
||||
<< s.m_s
|
||||
<< "\033[0m";
|
||||
/**
|
||||
* @brief Struct for delimited strings.
|
||||
*/
|
||||
template <typename StringType>
|
||||
struct coloured_string_t
|
||||
{
|
||||
static_assert(std::is_reference_v<StringType> or std::is_pointer_v<StringType>,
|
||||
"String type must be pointer or reference");
|
||||
|
||||
return os << ostr.str();
|
||||
}
|
||||
else
|
||||
return os << s.m_s;
|
||||
/**
|
||||
* @brief Construct a new coloured string t object
|
||||
*/
|
||||
coloured_string_t(StringType s, colour_type fc, colour_type bc, style_type st)
|
||||
: m_str(s)
|
||||
, m_fore_colour(static_cast<int>(fc) + 30)
|
||||
, m_back_colour(static_cast<int>(bc) + 40)
|
||||
, m_style(static_cast<int>(st))
|
||||
{
|
||||
}
|
||||
|
||||
coloured_string_t &operator=(coloured_string_t &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Write out the string, either coloured or not
|
||||
*/
|
||||
template <typename char_type, typename traits_type>
|
||||
friend std::basic_ostream<char_type, traits_type> &operator<<(
|
||||
std::basic_ostream<char_type, traits_type> &os, const coloured_string_t &cs)
|
||||
{
|
||||
bool use_colour = false;
|
||||
|
||||
if (os.rdbuf() == std::cout.rdbuf() and isatty(STDOUT_FILENO))
|
||||
use_colour = true;
|
||||
else if (os.rdbuf() == std::cerr.rdbuf() and isatty(STDERR_FILENO))
|
||||
use_colour = true;
|
||||
|
||||
if (use_colour)
|
||||
{
|
||||
os << "\033[" << cs.m_fore_colour << ';' << cs.m_style << ';' << cs.m_back_colour << 'm'
|
||||
<< cs.m_str
|
||||
<< "\033[0m";
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
/// @cond
|
||||
StringType m_str;
|
||||
int m_fore_colour, m_back_colour;
|
||||
int m_style;
|
||||
/// @endcond
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace colour
|
||||
|
||||
/**
|
||||
* @brief Manipulator for coloured strings.
|
||||
*
|
||||
* When writing out text to the terminal it is often useful to have
|
||||
* some of the text colourised. But only if the output is really a
|
||||
* terminal since colouring text is done using escape sequences
|
||||
* an if output is redirected to a file, these escape sequences end up
|
||||
* in the file making the real text less easy to read.
|
||||
*
|
||||
* The code presented here is rather basic. It mimics the std::quoted
|
||||
* manipulator in that it will colour a string with optionally
|
||||
* requested colours and text style.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* using namespace cif::colour;
|
||||
* std::cout << cif::coloured("Hello, world!", white, red, bold) << '\n';
|
||||
* @endcode
|
||||
* @param str String to quote.
|
||||
* @param fg Foreground (=text) colour to use
|
||||
* @param bg Background colour to use
|
||||
* @param st Text style to use
|
||||
*/
|
||||
|
||||
template <typename char_type>
|
||||
inline auto coloured(const char_type *str,
|
||||
colour::colour_type fg, colour::colour_type bg = colour::colour_type::none,
|
||||
colour::style_type st = colour::style_type::regular)
|
||||
{
|
||||
return colour::detail::coloured_string_t<const char_type *>(str, fg, bg, st);
|
||||
}
|
||||
|
||||
template <typename CharT>
|
||||
inline auto coloured(const CharT *s, StringColour fore = scWHITE, StringColour back = scRED, bool bold = true)
|
||||
/// @brief Manipulator for coloured strings.
|
||||
template <typename char_type, typename traits_type, typename allocator_type>
|
||||
inline auto coloured(const std::basic_string<char_type, traits_type, allocator_type> &str,
|
||||
colour::colour_type fg, colour::colour_type bg = colour::colour_type::none,
|
||||
colour::style_type st = colour::style_type::regular)
|
||||
{
|
||||
return ColouredString<const CharT *, CharT>(s, fore, back, bold);
|
||||
return colour::detail::coloured_string_t<const std::basic_string<char_type, traits_type, allocator_type> &>(str, fg, bg, st);
|
||||
}
|
||||
|
||||
template <typename CharT, typename Traits, typename Alloc>
|
||||
inline auto coloured(const std::basic_string<CharT, Traits, Alloc> &s, StringColour fore = scWHITE, StringColour back = scRED, bool bold = true)
|
||||
/// @brief Manipulator for coloured strings.
|
||||
template <typename char_type, typename traits_type, typename allocator_type>
|
||||
inline auto coloured(std::basic_string<char_type, traits_type, allocator_type> &str,
|
||||
colour::colour_type fg, colour::colour_type bg = colour::colour_type::none,
|
||||
colour::style_type st = colour::style_type::regular)
|
||||
{
|
||||
return ColouredString<const std::basic_string<CharT, Traits, Alloc>, CharT>(s, fore, back, bold);
|
||||
return colour::detail::coloured_string_t<std::basic_string<char_type, traits_type, allocator_type> &>(str, fg, bg, st);
|
||||
}
|
||||
|
||||
template <typename CharT, typename Traits, typename Alloc>
|
||||
inline auto coloured(std::basic_string<CharT, Traits, Alloc> &s, StringColour fore = scWHITE, StringColour back = scRED, bool bold = true)
|
||||
/// @brief Manipulator for coloured strings.
|
||||
template <typename char_type, typename traits_type>
|
||||
inline auto coloured(std::basic_string_view<char_type, traits_type> &str,
|
||||
colour::colour_type fg, colour::colour_type bg = colour::colour_type::none,
|
||||
colour::style_type st = colour::style_type::regular)
|
||||
{
|
||||
return ColouredString<std::basic_string<CharT, Traits, Alloc>, CharT>(s, fore, back, bold);
|
||||
return colour::detail::coloured_string_t<std::basic_string_view<char_type, traits_type> &>(str, fg, bg, st);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// A progress bar
|
||||
|
||||
/**
|
||||
* @brief A simple progress bar class for terminal based output
|
||||
*
|
||||
* Using a progress bar is very convenient for the end user when
|
||||
* you have long running code. It gives feed back on how fast an
|
||||
* operation is performed and may give an indication how long it
|
||||
* will take before it is finished.
|
||||
*
|
||||
* Using this cif::progress_bar implementation is straightforward:
|
||||
*
|
||||
* @code {.cpp}
|
||||
* using namespace std::chrono_literals;
|
||||
*
|
||||
* cif::progress_bar pb(10, "counting to ten");
|
||||
*
|
||||
* for (int i = 1; i <= 10; ++i)
|
||||
* {
|
||||
* pb.consumed(1);
|
||||
* std::this_thread::sleep_for(1s);
|
||||
* }
|
||||
*
|
||||
* @endcode
|
||||
*
|
||||
* When the progress_bar is created, it first checks
|
||||
* to see if stdout is to a real TTY and if the VERBOSE
|
||||
* flag is not less than zero (quiet mode). If this passes
|
||||
* a thread is started that waits for updates.
|
||||
*
|
||||
* The first two seconds, nothing is written to the screen
|
||||
* so if the work is finished within those two seconds
|
||||
* the screen stays clean.
|
||||
*
|
||||
* After this time, a progress bar is printed that may look
|
||||
* like this:
|
||||
*
|
||||
* @code
|
||||
* step 3 ========================-------------------------------- 40% ⢁
|
||||
* @endcode
|
||||
*
|
||||
* The first characters contain the initial action name or
|
||||
* the message text if it was used afterwards.
|
||||
*
|
||||
* The thermometer is made up with '=' and '-' characters.
|
||||
*
|
||||
* A percentage is also shown and at the end there is a spinner
|
||||
* that gives feedback that the program is really still working.
|
||||
*
|
||||
* The progress bar is removed if the max has been reached
|
||||
* or if the progress bar is destructed. If any output has
|
||||
* been generated, the initial action is printed out along
|
||||
* with the total time spent.
|
||||
*/
|
||||
|
||||
class progress_bar
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new progress bar object
|
||||
*
|
||||
* Progress ranges from 0 (zero) to @a inMax
|
||||
*
|
||||
* The action in @a inAction is used for display
|
||||
*
|
||||
* @param inMax The maximum value
|
||||
* @param inAction The description of what is
|
||||
* going on
|
||||
*/
|
||||
|
||||
progress_bar(int64_t inMax, const std::string &inAction);
|
||||
|
||||
/**
|
||||
* @brief Destroy the progress bar object
|
||||
*
|
||||
*/
|
||||
~progress_bar();
|
||||
|
||||
/**
|
||||
* @brief Notify the progress bar that @a inConsumed
|
||||
* should be added to the internal progress counter
|
||||
*/
|
||||
void consumed(int64_t inConsumed); // consumed is relative
|
||||
|
||||
/**
|
||||
* @brief Notify the progress bar that the internal
|
||||
* progress counter should be updated to @a inProgress
|
||||
*/
|
||||
void progress(int64_t inProgress); // progress is absolute
|
||||
|
||||
/**
|
||||
* @brief Replace the action string in the progress bar
|
||||
* with @a inMessage
|
||||
*/
|
||||
void message(const std::string &inMessage);
|
||||
|
||||
private:
|
||||
@@ -177,8 +333,50 @@ class progress_bar
|
||||
// --------------------------------------------------------------------
|
||||
// Resources
|
||||
|
||||
/**
|
||||
* @brief Load a resource from disk or the compiled in resources
|
||||
*
|
||||
* @verbatim embed:rst
|
||||
.. note::
|
||||
|
||||
See the :doc:`documentation on resources </resources>` for more information.
|
||||
|
||||
@endverbatim
|
||||
*
|
||||
* @param name The named resource to load
|
||||
* @return std::unique_ptr<std::istream> A pointer to the std::istream or empty if not found
|
||||
*/
|
||||
|
||||
std::unique_ptr<std::istream> load_resource(std::filesystem::path name);
|
||||
|
||||
/**
|
||||
* @brief Add a file specified by @a dataFile as the data for resource @a name
|
||||
*
|
||||
* @verbatim embed:rst
|
||||
.. note::
|
||||
|
||||
See the :doc:`documentation on resources </resources>` for more information.
|
||||
|
||||
@endverbatim
|
||||
*
|
||||
* @param name The name of the resource to specify
|
||||
* @param dataFile Path to a file containing the data
|
||||
*/
|
||||
|
||||
void add_file_resource(const std::string &name, std::filesystem::path dataFile);
|
||||
|
||||
/**
|
||||
* @brief Add a directory to the list of search directories. This list is
|
||||
* searched in a last-in-first-out order.
|
||||
*
|
||||
* @verbatim embed:rst
|
||||
.. note::
|
||||
|
||||
See the :doc:`documentation on resources </resources>` for more information.
|
||||
|
||||
@endverbatim
|
||||
*/
|
||||
|
||||
void add_data_directory(std::filesystem::path dataDir);
|
||||
|
||||
} // namespace cif
|
||||
|
||||
@@ -33,6 +33,16 @@
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
|
||||
/**
|
||||
* @file validate.hpp
|
||||
*
|
||||
* Support for validating mmCIF files based on a dictionary. These dictionaries
|
||||
* contain information about the categories and items therein, what they may
|
||||
* contain and how this should be formatted. There's also information on links
|
||||
* between parent and child categories.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
@@ -40,39 +50,67 @@ struct category_validator;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The exception thrown when a validation error occurs
|
||||
*
|
||||
*/
|
||||
class validation_error : public std::exception
|
||||
{
|
||||
public:
|
||||
/// @brief Constructor
|
||||
validation_error(const std::string &msg);
|
||||
|
||||
/// @brief Constructor
|
||||
validation_error(const std::string &cat, const std::string &item,
|
||||
const std::string &msg);
|
||||
|
||||
/// @brief The description of the error
|
||||
const char *what() const noexcept { return m_msg.c_str(); }
|
||||
|
||||
/// @cond
|
||||
std::string m_msg;
|
||||
/// @endcond
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** @brief the primitive types known */
|
||||
enum class DDL_PrimitiveType
|
||||
{
|
||||
Char,
|
||||
UChar,
|
||||
Numb
|
||||
Char, ///< Text
|
||||
UChar, ///< Text that is compared ignoring the character case
|
||||
Numb ///< Nummeric values
|
||||
};
|
||||
|
||||
/// @brief Return the DDL_PrimitiveType encoded in @a s
|
||||
DDL_PrimitiveType map_to_primitive_type(std::string_view s);
|
||||
|
||||
struct regex_impl;
|
||||
|
||||
/**
|
||||
* @brief For each defined type in a dictionary a type_validator is created
|
||||
*
|
||||
* A type validator can check if the contents of an item are conforming the
|
||||
* specification. The check is done using regular expressions.
|
||||
*
|
||||
* A type_validator can also be used to compare two values that conform to
|
||||
* this type. Comparison is of course based on the primitive type.
|
||||
*
|
||||
*/
|
||||
struct type_validator
|
||||
{
|
||||
std::string m_name;
|
||||
DDL_PrimitiveType m_primitive_type;
|
||||
regex_impl *m_rx;
|
||||
std::string m_name; ///< The name of the type
|
||||
DDL_PrimitiveType m_primitive_type; ///< The primitive_type of the type
|
||||
regex_impl *m_rx; ///< The regular expression for the type
|
||||
|
||||
type_validator() = delete;
|
||||
|
||||
/// @brief Constructor
|
||||
type_validator(std::string_view name, DDL_PrimitiveType type, std::string_view rx);
|
||||
|
||||
type_validator(const type_validator &) = delete;
|
||||
|
||||
/// @brief Copy constructor
|
||||
type_validator(type_validator &&rhs)
|
||||
: m_name(std::move(rhs.m_name))
|
||||
, m_primitive_type(rhs.m_primitive_type)
|
||||
@@ -81,6 +119,8 @@ struct type_validator
|
||||
}
|
||||
|
||||
type_validator &operator=(const type_validator &) = delete;
|
||||
|
||||
/// @brief Move constructor
|
||||
type_validator &operator=(type_validator &&rhs)
|
||||
{
|
||||
m_name = std::move(rhs.m_name);
|
||||
@@ -90,119 +130,167 @@ struct type_validator
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// @brief Destructor
|
||||
~type_validator();
|
||||
|
||||
/// @brief Return the sorting order
|
||||
bool operator<(const type_validator &rhs) const
|
||||
{
|
||||
return icompare(m_name, rhs.m_name) < 0;
|
||||
}
|
||||
|
||||
/// @brief Compare the contents of @a a and @a b based on the
|
||||
/// primitive type of this type. A value of zero indicates the
|
||||
/// values are equal. Less than zero means @a a sorts before @a b
|
||||
/// and a value larger than zero likewise means the opposite
|
||||
int compare(std::string_view a, std::string_view b) const;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief An item_validator binds a type_validator to an item in
|
||||
* a category along with other information found in the dictionary.
|
||||
*
|
||||
* mmCIF dictionaries may indicate an item is e.g. mandatory or
|
||||
* consists of a certain list of allowed values. Even default
|
||||
* values can be provided.
|
||||
*
|
||||
*/
|
||||
struct item_validator
|
||||
{
|
||||
std::string m_tag;
|
||||
bool m_mandatory;
|
||||
const type_validator *m_type;
|
||||
cif::iset m_enums;
|
||||
std::string m_default;
|
||||
bool m_default_is_null;
|
||||
category_validator *m_category = nullptr;
|
||||
|
||||
// ItemLinked is used for non-key links
|
||||
struct item_link
|
||||
{
|
||||
item_validator *m_parent;
|
||||
std::string m_parent_item;
|
||||
std::string m_child_item;
|
||||
};
|
||||
|
||||
std::vector<item_link> mLinked;
|
||||
std::string m_tag; ///< The item name
|
||||
bool m_mandatory; ///< Flag indicating this item is mandatory
|
||||
const type_validator *m_type; ///< The type for this item
|
||||
cif::iset m_enums; ///< If filled, the set of allowed values
|
||||
std::string m_default; ///< If filled, a default value for this item
|
||||
category_validator *m_category = nullptr; ///< The category_validator this item_validator belongs to
|
||||
|
||||
/// @brief Compare based on the name
|
||||
bool operator<(const item_validator &rhs) const
|
||||
{
|
||||
return icompare(m_tag, rhs.m_tag) < 0;
|
||||
}
|
||||
|
||||
/// @brief Compare based on the name
|
||||
bool operator==(const item_validator &rhs) const
|
||||
{
|
||||
return iequals(m_tag, rhs.m_tag);
|
||||
}
|
||||
|
||||
/// @brief Validate the value in @a value for this item
|
||||
/// Will throw a validation_error exception if it fails
|
||||
void operator()(std::string_view value) const;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A validator for categories
|
||||
*
|
||||
* Categories can have a key, a set of items that in combination
|
||||
* should be unique.
|
||||
*/
|
||||
struct category_validator
|
||||
{
|
||||
std::string m_name;
|
||||
std::vector<std::string> m_keys;
|
||||
cif::iset m_groups;
|
||||
cif::iset m_mandatory_fields;
|
||||
std::set<item_validator> m_item_validators;
|
||||
std::string m_name; ///< The name of the category
|
||||
std::vector<std::string> m_keys; ///< The list of items that make up the key
|
||||
cif::iset m_groups; ///< The category groups this category belongs to
|
||||
cif::iset m_mandatory_fields; ///< The mandatory fields for this category
|
||||
std::set<item_validator> m_item_validators; ///< The item validators for the items in this category
|
||||
|
||||
/// @brief return true if this category sorts before @a rhs
|
||||
bool operator<(const category_validator &rhs) const
|
||||
{
|
||||
return icompare(m_name, rhs.m_name) < 0;
|
||||
}
|
||||
|
||||
/// @brief Add item_validator @a v to the list of item validators
|
||||
void addItemValidator(item_validator &&v);
|
||||
|
||||
/// @brief Return the item_validator for item @a tag, may return nullptr
|
||||
const item_validator *get_validator_for_item(std::string_view tag) const;
|
||||
|
||||
const std::set<item_validator> &item_validators() const
|
||||
{
|
||||
return m_item_validators;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A validator for links between categories
|
||||
*
|
||||
* Links are defined as a set of pairs of item names in a
|
||||
* parent category and a corresponding item in a child
|
||||
* category. This means that the size of m_parent_keys
|
||||
* is always equal to the size of m_child_keys.
|
||||
*
|
||||
* Multiple links may be defined between two categories.
|
||||
*
|
||||
*/
|
||||
struct link_validator
|
||||
{
|
||||
int m_link_group_id;
|
||||
std::string m_parent_category;
|
||||
std::vector<std::string> m_parent_keys;
|
||||
std::string m_child_category;
|
||||
std::vector<std::string> m_child_keys;
|
||||
std::string m_link_group_label;
|
||||
int m_link_group_id; ///< The link group ID
|
||||
std::string m_parent_category; ///< The name of the parent category
|
||||
std::vector<std::string> m_parent_keys; ///< The items in the parent category making up the set of linked items
|
||||
std::string m_child_category; ///< The name of the child category
|
||||
std::vector<std::string> m_child_keys; ///< The items in the child category making up the set of linked items
|
||||
std::string m_link_group_label; ///< The group label assigned to this link
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief The validator class combines all the link, category and item validator classes
|
||||
*
|
||||
*/
|
||||
class validator
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new validator object
|
||||
*
|
||||
* @param name The name of the underlying dictionary
|
||||
*/
|
||||
validator(std::string_view name)
|
||||
: m_name(name)
|
||||
{
|
||||
}
|
||||
|
||||
/// @brief destructor
|
||||
~validator() = default;
|
||||
|
||||
validator(const validator &rhs) = delete;
|
||||
validator &operator=(const validator &rhs) = delete;
|
||||
|
||||
/// @brief move constructor
|
||||
validator(validator &&rhs) = default;
|
||||
|
||||
/// @brief move assignment operator
|
||||
validator &operator=(validator &&rhs) = default;
|
||||
|
||||
friend class dictionary_parser;
|
||||
|
||||
/// @brief Add type_validator @a v to the list of type validators
|
||||
void add_type_validator(type_validator &&v);
|
||||
|
||||
/// @brief Return the type validator for @a type_code, may return nullptr
|
||||
const type_validator *get_validator_for_type(std::string_view type_code) const;
|
||||
|
||||
/// @brief Add category_validator @a v to the list of category validators
|
||||
void add_category_validator(category_validator &&v);
|
||||
|
||||
/// @brief Return the category validator for @a category, may return nullptr
|
||||
const category_validator *get_validator_for_category(std::string_view category) const;
|
||||
|
||||
/// @brief Add link_validator @a v to the list of link validators
|
||||
void add_link_validator(link_validator &&v);
|
||||
|
||||
/// @brief Return the list of link validators for which the parent is @a category
|
||||
std::vector<const link_validator *> get_links_for_parent(std::string_view category) const;
|
||||
|
||||
/// @brief Return the list of link validators for which the child is @a category
|
||||
std::vector<const link_validator *> get_links_for_child(std::string_view category) const;
|
||||
|
||||
/// @brief Bottleneck function to report an error in validation
|
||||
void report_error(const std::string &msg, bool fatal) const;
|
||||
|
||||
const std::string &name() const { return m_name; }
|
||||
void set_name(const std::string &name) { m_name = name; }
|
||||
const std::string &name() const { return m_name; } ///< Get the name of this validator
|
||||
void set_name(const std::string &name) { m_name = name; } ///< Set the name of this validator
|
||||
|
||||
const std::string &version() const { return m_version; }
|
||||
void version(const std::string &version) { m_version = version; }
|
||||
const std::string &version() const { return m_version; } ///< Get the version of this validator
|
||||
void set_version(const std::string &version) { m_version = version; } ///< Set the version of this validator
|
||||
|
||||
private:
|
||||
// name is fully qualified here:
|
||||
@@ -217,21 +305,29 @@ class validator
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Validators are globally unique objects, use the validator_factory
|
||||
* class to construct them. This class is a singleton.
|
||||
*/
|
||||
|
||||
class validator_factory
|
||||
{
|
||||
public:
|
||||
/// @brief Return the singleton instance
|
||||
static validator_factory &instance()
|
||||
{
|
||||
static validator_factory s_instance;
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
/// @brief Return the validator with name @a dictionary_name
|
||||
const validator &operator[](std::string_view dictionary_name);
|
||||
|
||||
/// @brief Construct a new validator with name @a name from the data in @a is
|
||||
const validator &construct_validator(std::string_view name, std::istream &is);
|
||||
|
||||
private:
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
validator_factory() = default;
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
prefix=@prefix@
|
||||
exec_prefix=@exec_prefix@
|
||||
libdir=@libdir@
|
||||
includedir=@includedir@
|
||||
datalibdir=@datarootdir@/libcifpp
|
||||
|
||||
Name: libcifpp
|
||||
Description: C++ library for the manipulation of mmCIF files.
|
||||
Version: @PACKAGE_VERSION@
|
||||
|
||||
Requires: zlib
|
||||
Libs: -L${libdir} -lcifpp
|
||||
Cflags: -I${includedir} -pthread
|
||||
@@ -34,8 +34,6 @@ namespace cif
|
||||
namespace data
|
||||
{
|
||||
|
||||
const float kNA = std::nanf("1");
|
||||
|
||||
const atom_type_info kKnownAtoms[] =
|
||||
{
|
||||
{ Nn, "Unknown", "Nn", 0, false, { kNA, kNA, kNA, kNA, kNA, kNA, kNA } }, // 0 Nn Unknown
|
||||
@@ -1114,7 +1112,7 @@ auto atom_type_traits::wksf(int charge) const -> const SFData&
|
||||
// Oops, not found. Fall back to zero charge and see if we can use that
|
||||
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "No scattering factor found for " << name() << " with charge " << charge << " will try to fall back to zero charge..." << std::endl;
|
||||
std::cerr << "No scattering factor found for " << name() << " with charge " << charge << " will try to fall back to zero charge...\n";
|
||||
|
||||
for (auto& sf: data::kWKSFData)
|
||||
{
|
||||
@@ -1145,7 +1143,7 @@ auto atom_type_traits::elsf() const -> const SFData&
|
||||
|
||||
float atom_type_traits::crystal_ionic_radius(int charge) const
|
||||
{
|
||||
float result = data::kNA;
|
||||
float result = kNA;
|
||||
|
||||
if (charge >= -3 and charge <= 8)
|
||||
{
|
||||
@@ -1164,7 +1162,7 @@ float atom_type_traits::crystal_ionic_radius(int charge) const
|
||||
|
||||
float atom_type_traits::effective_ionic_radius(int charge) const
|
||||
{
|
||||
float result = data::kNA;
|
||||
float result = kNA;
|
||||
|
||||
if (charge >= -3 and charge <= 8)
|
||||
{
|
||||
|
||||
106
src/category.cpp
106
src/category.cpp
@@ -674,7 +674,7 @@ void category::set_validator(const validator *v, datablock &db)
|
||||
{
|
||||
std::ostringstream msg;
|
||||
msg << "Cannot construct index since the key field" << (missing.size() > 1 ? "s" : "") << " "
|
||||
<< cif::join(missing, ", ") << " in " << m_name << " " << (missing.size() == 1 ? "is" : "are") << " missing" << std::endl;
|
||||
<< cif::join(missing, ", ") << " in " << m_name << " " << (missing.size() == 1 ? "is" : "are") << " missing\n";
|
||||
throw std::runtime_error(msg.str());
|
||||
}
|
||||
}
|
||||
@@ -723,7 +723,7 @@ bool category::is_valid() const
|
||||
if (empty())
|
||||
{
|
||||
if (VERBOSE > 2)
|
||||
std::cerr << "Skipping validation of empty category " << m_name << std::endl;
|
||||
std::cerr << "Skipping validation of empty category " << m_name << '\n';
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -875,17 +875,17 @@ bool category::validate_links() const
|
||||
{
|
||||
result = false;
|
||||
|
||||
std::cerr << "Links for " << link.v->m_link_group_label << " are incomplete" << std::endl
|
||||
<< " There are " << missing << " items in " << m_name << " that don't have matching parent items in " << parent->m_name << std::endl;
|
||||
std::cerr << "Links for " << link.v->m_link_group_label << " are incomplete\n"
|
||||
<< " There are " << missing << " items in " << m_name << " that don't have matching parent items in " << parent->m_name << '\n';
|
||||
|
||||
if (VERBOSE)
|
||||
{
|
||||
std::cerr << "showing first " << first_missing_rows.size() << " rows" << std::endl
|
||||
<< std::endl;
|
||||
std::cerr << "showing first " << first_missing_rows.size() << " rows\n"
|
||||
<< '\n';
|
||||
|
||||
first_missing_rows.write(std::cerr, link.v->m_child_keys, false);
|
||||
|
||||
std::cerr << std::endl;
|
||||
std::cerr << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -921,25 +921,30 @@ condition category::get_parents_condition(row_handle rh, const category &parentC
|
||||
|
||||
condition result;
|
||||
|
||||
for (auto &link : m_validator->get_links_for_child(m_name))
|
||||
auto links = m_validator->get_links_for_child(m_name);
|
||||
links.erase(remove_if(links.begin(), links.end(), [n=parentCat.m_name](auto &l) { return l->m_parent_category != n; }), links.end());
|
||||
|
||||
if (not links.empty())
|
||||
{
|
||||
if (link->m_parent_category != parentCat.m_name)
|
||||
continue;
|
||||
|
||||
condition cond;
|
||||
|
||||
for (size_t ix = 0; ix < link->m_child_keys.size(); ++ix)
|
||||
for (auto &link : links)
|
||||
{
|
||||
auto childValue = rh[link->m_child_keys[ix]];
|
||||
condition cond;
|
||||
|
||||
if (childValue.empty())
|
||||
continue;
|
||||
for (size_t ix = 0; ix < link->m_child_keys.size(); ++ix)
|
||||
{
|
||||
auto childValue = rh[link->m_child_keys[ix]];
|
||||
|
||||
cond = std::move(cond) and key(link->m_parent_keys[ix]) == childValue.text();
|
||||
if (childValue.empty())
|
||||
continue;
|
||||
|
||||
cond = std::move(cond) and key(link->m_parent_keys[ix]) == childValue.text();
|
||||
}
|
||||
|
||||
result = std::move(result) or std::move(cond);
|
||||
}
|
||||
|
||||
result = std::move(result) or std::move(cond);
|
||||
}
|
||||
else if (cif::VERBOSE > 0)
|
||||
std::cerr << "warning: no child to parent links were found for child " << parentCat.name() << " and parent " << name() << '\n';
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -956,30 +961,35 @@ condition category::get_children_condition(row_handle rh, const category &childC
|
||||
if (childCatValidator != nullptr)
|
||||
mandatoryChildFields = childCatValidator->m_mandatory_fields;
|
||||
|
||||
for (auto &link : m_validator->get_links_for_parent(m_name))
|
||||
auto links = m_validator->get_links_for_parent(m_name);
|
||||
links.erase(remove_if(links.begin(), links.end(), [n=childCat.m_name](auto &l) { return l->m_child_category != n; }), links.end());
|
||||
|
||||
if (not links.empty())
|
||||
{
|
||||
if (link->m_child_category != childCat.m_name)
|
||||
continue;
|
||||
|
||||
condition cond;
|
||||
|
||||
for (size_t ix = 0; ix < link->m_parent_keys.size(); ++ix)
|
||||
for (auto &link : links)
|
||||
{
|
||||
auto childKey = link->m_child_keys[ix];
|
||||
auto parentKey = link->m_parent_keys[ix];
|
||||
condition cond;
|
||||
|
||||
auto parentValue = rh[parentKey];
|
||||
for (size_t ix = 0; ix < link->m_parent_keys.size(); ++ix)
|
||||
{
|
||||
auto childKey = link->m_child_keys[ix];
|
||||
auto parentKey = link->m_parent_keys[ix];
|
||||
|
||||
if (parentValue.empty())
|
||||
cond = std::move(cond) and key(childKey) == null;
|
||||
else if (link->m_parent_keys.size() > 1 and not mandatoryChildFields.contains(childKey))
|
||||
cond = std::move(cond) and (key(childKey) == parentValue.text() or key(childKey) == null);
|
||||
else
|
||||
cond = std::move(cond) and key(childKey) == parentValue.text();
|
||||
auto parentValue = rh[parentKey];
|
||||
|
||||
if (parentValue.empty())
|
||||
cond = std::move(cond) and key(childKey) == null;
|
||||
else if (link->m_parent_keys.size() > 1 and not mandatoryChildFields.contains(childKey))
|
||||
cond = std::move(cond) and (key(childKey) == parentValue.text() or key(childKey) == null);
|
||||
else
|
||||
cond = std::move(cond) and key(childKey) == parentValue.text();
|
||||
}
|
||||
|
||||
result = std::move(result) or std::move(cond);
|
||||
}
|
||||
|
||||
result = std::move(result) or std::move(cond);
|
||||
}
|
||||
else if (cif::VERBOSE > 0)
|
||||
std::cerr << "warning: no parent to child links were found for parent " << name() << " and child " << childCat.name() << '\n';
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1214,9 +1224,9 @@ void category::erase_orphans(condition &&cond, category &parent)
|
||||
{
|
||||
category c(m_name);
|
||||
c.emplace(r);
|
||||
std::cerr << "Removing orphaned record: " << std::endl
|
||||
<< c << std::endl
|
||||
<< std::endl;
|
||||
std::cerr << "Removing orphaned record: \n"
|
||||
<< c << '\n'
|
||||
<< '\n';
|
||||
|
||||
}
|
||||
|
||||
@@ -1383,7 +1393,7 @@ void category::update_value(const std::vector<row_handle> &rows, std::string_vie
|
||||
|
||||
// cannot update this...
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Cannot update child " << childCat->m_name << "." << childTag << " with value " << value << std::endl;
|
||||
std::cerr << "Cannot update child " << childCat->m_name << "." << childTag << " with value " << value << '\n';
|
||||
}
|
||||
|
||||
// finally, update the children
|
||||
@@ -1480,8 +1490,8 @@ void category::update_value(row *row, uint16_t column, std::string_view value, b
|
||||
|
||||
// if (cif::VERBOSE > 2)
|
||||
// {
|
||||
// std::cerr << "Parent: " << linked->mParentcategory << " Child: " << linked->m_child_category << std::endl
|
||||
// << cond << std::endl;
|
||||
// std::cerr << "Parent: " << linked->mParentcategory << " Child: " << linked->m_child_category << '\n'
|
||||
// << cond << '\n';
|
||||
// }
|
||||
|
||||
// Now, suppose there are already rows in child that conform to the new value,
|
||||
@@ -1510,7 +1520,7 @@ void category::update_value(row *row, uint16_t column, std::string_view value, b
|
||||
if (not rows_n.empty())
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Will not rename in child category since there are already rows that link to the parent" << std::endl;
|
||||
std::cerr << "Will not rename in child category since there are already rows that link to the parent\n";
|
||||
|
||||
continue;
|
||||
}
|
||||
@@ -1871,7 +1881,7 @@ void category::write(std::ostream &os, const std::vector<uint16_t> &order, bool
|
||||
|
||||
if (needLoop)
|
||||
{
|
||||
os << "loop_" << '\n';
|
||||
os << "loop_\n";
|
||||
|
||||
std::vector<size_t> columnWidths(m_columns.size());
|
||||
|
||||
@@ -2018,7 +2028,7 @@ void category::write(std::ostream &os, const std::vector<uint16_t> &order, bool
|
||||
}
|
||||
}
|
||||
|
||||
os << "# " << '\n';
|
||||
os << "# \n";
|
||||
}
|
||||
|
||||
bool category::operator==(const category &rhs) const
|
||||
@@ -2031,7 +2041,7 @@ bool category::operator==(const category &rhs) const
|
||||
// set<std::string> tagsA(a.fields()), tagsB(b.fields());
|
||||
//
|
||||
// if (tagsA != tagsB)
|
||||
// std::cout << "Unequal number of fields" << std::endl;
|
||||
// std::cout << "Unequal number of fields\n";
|
||||
|
||||
const category_validator *catValidator = nullptr;
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ std::string to_string(bond_type bondType)
|
||||
throw std::invalid_argument("Invalid bondType");
|
||||
}
|
||||
|
||||
bond_type from_string(const std::string &bondType)
|
||||
bond_type parse_bond_type_from_string(const std::string &bondType)
|
||||
{
|
||||
if (cif::iequals(bondType, "sing"))
|
||||
return bond_type::sing;
|
||||
@@ -77,6 +77,28 @@ bond_type from_string(const std::string &bondType)
|
||||
throw std::invalid_argument("Invalid bondType: " + bondType);
|
||||
}
|
||||
|
||||
std::string to_string(stereo_config_type stereoConfig)
|
||||
{
|
||||
switch (stereoConfig)
|
||||
{
|
||||
case stereo_config_type::N: return "N";
|
||||
case stereo_config_type::R: return "R";
|
||||
case stereo_config_type::S: return "S";
|
||||
}
|
||||
throw std::invalid_argument("Invalid stereoConfig");
|
||||
}
|
||||
|
||||
stereo_config_type parse_stereo_config_from_string(const std::string &stereoConfig)
|
||||
{
|
||||
if (cif::iequals(stereoConfig, "N"))
|
||||
return stereo_config_type::N;
|
||||
if (cif::iequals(stereoConfig, "R"))
|
||||
return stereo_config_type::R;
|
||||
if (cif::iequals(stereoConfig, "S"))
|
||||
return stereo_config_type::S;
|
||||
throw std::invalid_argument("Invalid stereoConfig: " + stereoConfig);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// compound helper classes
|
||||
|
||||
@@ -126,11 +148,12 @@ compound::compound(cif::datablock &db)
|
||||
for (auto row : chemCompAtom)
|
||||
{
|
||||
compound_atom atom;
|
||||
std::string type_symbol;
|
||||
cif::tie(atom.id, type_symbol, atom.charge, atom.aromatic, atom.leaving_atom, atom.stereo_config, atom.x, atom.y, atom.z) =
|
||||
std::string type_symbol, stereo_config;
|
||||
cif::tie(atom.id, type_symbol, atom.charge, atom.aromatic, atom.leaving_atom, stereo_config, atom.x, atom.y, atom.z) =
|
||||
row.get("atom_id", "type_symbol", "charge", "pdbx_aromatic_flag", "pdbx_leaving_atom_flag", "pdbx_stereo_config",
|
||||
"model_Cartn_x", "model_Cartn_y", "model_Cartn_z");
|
||||
atom.type_symbol = atom_type_traits(type_symbol).type();
|
||||
atom.stereo_config = parse_stereo_config_from_string(stereo_config);
|
||||
m_atoms.push_back(std::move(atom));
|
||||
}
|
||||
|
||||
@@ -140,7 +163,7 @@ compound::compound(cif::datablock &db)
|
||||
compound_bond bond;
|
||||
std::string valueOrder;
|
||||
cif::tie(bond.atom_id[0], bond.atom_id[1], valueOrder, bond.aromatic, bond.stereo_config) = row.get("atom_id_1", "atom_id_2", "value_order", "pdbx_aromatic_flag", "pdbx_stereo_config");
|
||||
bond.type = from_string(valueOrder);
|
||||
bond.type = parse_bond_type_from_string(valueOrder);
|
||||
m_bonds.push_back(std::move(bond));
|
||||
}
|
||||
}
|
||||
@@ -186,7 +209,7 @@ compound::compound(cif::datablock &db, const std::string &id, const std::string
|
||||
else
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Unimplemented chem_comp_bond.type " << btype << " in " << id << std::endl;
|
||||
std::cerr << "Unimplemented chem_comp_bond.type " << btype << " in " << id << '\n';
|
||||
bond.type = bond_type::sing;
|
||||
}
|
||||
m_bonds.push_back(std::move(bond));
|
||||
@@ -443,15 +466,15 @@ compound_factory_impl::compound_factory_impl(const fs::path &file, std::shared_p
|
||||
|
||||
if (not cifFile.is_valid())
|
||||
{
|
||||
std::cerr << "The components file " << file << " is not valid" << std::endl;
|
||||
std::cerr << "The components file " << file << " is not valid\n";
|
||||
if (cif::VERBOSE < 1)
|
||||
std::cerr << "(use --verbose to see why)" << std::endl;
|
||||
std::cerr << "(use --verbose to see why)\n";
|
||||
}
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << "When trying to load the components file " << file << " there was an exception:" << std::endl
|
||||
<< e.what() << std::endl;
|
||||
std::cerr << "When trying to load the components file " << file << " there was an exception:\n"
|
||||
<< e.what() << '\n';
|
||||
}
|
||||
|
||||
for (auto &db : cifFile)
|
||||
@@ -493,7 +516,7 @@ compound *CCD_compound_factory_impl::create(const std::string &id)
|
||||
ccd = cif::load_resource("components.cif");
|
||||
if (not ccd)
|
||||
{
|
||||
std::cerr << "Could not locate the CCD components.cif file, please make sure the software is installed properly and/or use the update-libcifpp-data to fetch the data." << std::endl;
|
||||
std::cerr << "Could not locate the CCD components.cif file, please make sure the software is installed properly and/or use the update-libcifpp-data to fetch the data.\n";
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
@@ -553,7 +576,7 @@ compound *CCD_compound_factory_impl::create(const std::string &id)
|
||||
}
|
||||
|
||||
if (result == nullptr and cif::VERBOSE > 0)
|
||||
std::cerr << "Could not locate compound " << id << " in the CCD components file" << std::endl;
|
||||
std::cerr << "Could not locate compound " << id << " in the CCD components file\n";
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -678,13 +701,13 @@ compound_factory::compound_factory()
|
||||
if (ccd)
|
||||
m_impl = std::make_shared<CCD_compound_factory_impl>(m_impl);
|
||||
else if (cif::VERBOSE > 0)
|
||||
std::cerr << "CCD components.cif file was not found" << std::endl;
|
||||
std::cerr << "CCD components.cif file was not found\n";
|
||||
|
||||
const char *clibd_mon = getenv("CLIBD_MON");
|
||||
if (clibd_mon != nullptr and fs::is_directory(clibd_mon))
|
||||
m_impl = std::make_shared<CCP4_compound_factory_impl>(clibd_mon, m_impl);
|
||||
else if (cif::VERBOSE > 0)
|
||||
std::cerr << "CCP4 monomers library not found, CLIBD_MON is not defined" << std::endl;
|
||||
std::cerr << "CCP4 monomers library not found, CLIBD_MON is not defined\n";
|
||||
}
|
||||
|
||||
compound_factory::~compound_factory()
|
||||
|
||||
@@ -198,8 +198,8 @@ std::vector<std::string> datablock::get_tag_order() const
|
||||
|
||||
void datablock::write(std::ostream &os) const
|
||||
{
|
||||
os << "data_" << m_name << std::endl
|
||||
<< "# " << std::endl;
|
||||
os << "data_" << m_name << '\n'
|
||||
<< "# \n";
|
||||
|
||||
// mmcif support, sort of. First write the 'entry' Category
|
||||
// and if it exists, _AND_ we have a Validator, write out the
|
||||
@@ -237,8 +237,8 @@ void datablock::write(std::ostream &os) const
|
||||
|
||||
void datablock::write(std::ostream &os, const std::vector<std::string> &tag_order)
|
||||
{
|
||||
os << "data_" << m_name << std::endl
|
||||
<< "# " << std::endl;
|
||||
os << "data_" << m_name << '\n'
|
||||
<< "# \n";
|
||||
|
||||
std::vector<std::string> cat_order;
|
||||
for (auto &o : tag_order)
|
||||
|
||||
@@ -103,7 +103,7 @@ class dictionary_parser : public parser
|
||||
{
|
||||
auto r = info->front();
|
||||
m_validator.set_name(r["title"].as<std::string>());
|
||||
m_validator.version(r["version"].as<std::string>());
|
||||
m_validator.set_version(r["version"].as<std::string>());
|
||||
}
|
||||
|
||||
m_datablock = savedDatablock;
|
||||
@@ -213,16 +213,16 @@ class dictionary_parser : public parser
|
||||
ess.insert(e["value"].as<std::string>());
|
||||
|
||||
std::string defaultValue = dict["item_default"].front().get<std::string>("value");
|
||||
bool defaultIsNull = false;
|
||||
if (defaultValue.empty())
|
||||
{
|
||||
// TODO: Is this correct???
|
||||
for (auto r : dict["_item_default"])
|
||||
{
|
||||
defaultIsNull = r["value"].is_null();
|
||||
break;
|
||||
}
|
||||
}
|
||||
// bool defaultIsNull = false;
|
||||
// if (defaultValue.empty())
|
||||
// {
|
||||
// // TODO: Is this correct???
|
||||
// for (auto r : dict["_item_default"])
|
||||
// {
|
||||
// defaultIsNull = r["value"].is_null();
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// collect the dict from our dataBlock and construct validators
|
||||
for (auto i : dict["item"])
|
||||
@@ -245,7 +245,7 @@ class dictionary_parser : public parser
|
||||
|
||||
auto vi = find(ivs.begin(), ivs.end(), item_validator{ item_name });
|
||||
if (vi == ivs.end())
|
||||
ivs.push_back(item_validator{ item_name, iequals(mandatory, "yes"), tv, ess, defaultValue, defaultIsNull });
|
||||
ivs.push_back(item_validator{ item_name, iequals(mandatory, "yes"), tv, ess, defaultValue /*, defaultIsNull*/ });
|
||||
else
|
||||
{
|
||||
// need to update the itemValidator?
|
||||
@@ -253,12 +253,12 @@ class dictionary_parser : public parser
|
||||
{
|
||||
if (VERBOSE > 2)
|
||||
{
|
||||
std::cerr << "inconsistent mandatory value for " << tagName << " in dictionary" << std::endl;
|
||||
std::cerr << "inconsistent mandatory value for " << tagName << " in dictionary\n";
|
||||
|
||||
if (iequals(tagName, saveFrameName))
|
||||
std::cerr << "choosing " << mandatory << std::endl;
|
||||
std::cerr << "choosing " << mandatory << '\n';
|
||||
else
|
||||
std::cerr << "choosing " << (vi->m_mandatory ? "Y" : "N") << std::endl;
|
||||
std::cerr << "choosing " << (vi->m_mandatory ? "Y" : "N") << '\n';
|
||||
}
|
||||
|
||||
if (iequals(tagName, saveFrameName))
|
||||
@@ -268,7 +268,7 @@ class dictionary_parser : public parser
|
||||
if (vi->m_type != nullptr and tv != nullptr and vi->m_type != tv)
|
||||
{
|
||||
if (VERBOSE > 1)
|
||||
std::cerr << "inconsistent type for " << tagName << " in dictionary" << std::endl;
|
||||
std::cerr << "inconsistent type for " << tagName << " in dictionary\n";
|
||||
}
|
||||
|
||||
// vi->mMandatory = (iequals(mandatory, "yes"));
|
||||
@@ -410,7 +410,7 @@ class dictionary_parser : public parser
|
||||
for (auto &iv : cv.m_item_validators)
|
||||
{
|
||||
if (iv.m_type == nullptr and cif::VERBOSE >= 0)
|
||||
std::cerr << "Missing item_type for " << iv.m_tag << std::endl;
|
||||
std::cerr << "Missing item_type for " << iv.m_tag << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -452,7 +452,7 @@ class dictionary_parser : public parser
|
||||
// mFileImpl.mTypeValidators.erase(v);
|
||||
|
||||
if (VERBOSE >= 5)
|
||||
std::cerr << "Added type " << code << " (" << primitiveCode << ") => " << construct << std::endl;
|
||||
std::cerr << "Added type " << code << " (" << primitiveCode << ") => " << construct << '\n';
|
||||
|
||||
result = true;
|
||||
}
|
||||
|
||||
16
src/file.cpp
16
src/file.cpp
@@ -58,7 +58,7 @@ bool file::is_valid()
|
||||
if (m_validator == nullptr)
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << "No dictionary loaded explicitly, loading default" << std::endl;
|
||||
std::cerr << "No dictionary loaded explicitly, loading default\n";
|
||||
|
||||
load_dictionary();
|
||||
}
|
||||
@@ -108,7 +108,7 @@ void file::load_dictionary()
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (VERBOSE)
|
||||
std::cerr << "Failed to load dictionary " << std::quoted(name) << ": " << ex.what() << std::endl;
|
||||
std::cerr << "Failed to load dictionary " << std::quoted(name) << ": " << ex.what() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -182,17 +182,17 @@ std::tuple<file::iterator, bool> file::emplace(std::string_view name)
|
||||
|
||||
void file::load(const std::filesystem::path &p)
|
||||
{
|
||||
gzio::ifstream in(p);
|
||||
if (not in.is_open())
|
||||
throw std::runtime_error("Could not open file '" + p.string() + '\'');
|
||||
|
||||
try
|
||||
{
|
||||
gzio::ifstream in(p);
|
||||
if (not in.is_open())
|
||||
throw std::runtime_error("Could not open file " + p.string());
|
||||
|
||||
load(in);
|
||||
}
|
||||
catch (const std::exception &)
|
||||
{
|
||||
throw_with_nested(std::runtime_error("Error reading file " + p.string()));
|
||||
throw_with_nested(std::runtime_error("Error reading file '" + p.string() + '\''));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ void file::save(const std::filesystem::path &p) const
|
||||
void file::save(std::ostream &os) const
|
||||
{
|
||||
// if (not is_valid())
|
||||
// std::cout << "File is not valid!" << std::endl;
|
||||
// std::cout << "File is not valid!\n";
|
||||
|
||||
for (auto &db : *this)
|
||||
db.write(os);
|
||||
|
||||
127
src/model.cpp
127
src/model.cpp
@@ -75,7 +75,7 @@ int atom::atom_impl::get_property_int(std::string_view name) const
|
||||
|
||||
std::from_chars_result r = std::from_chars(s.data(), s.data() + s.length(), result);
|
||||
if (r.ec != std::errc() and VERBOSE > 0)
|
||||
std::cerr << "Error converting " << s << " to number for property " << name << std::endl;
|
||||
std::cerr << "Error converting " << s << " to number for property " << name << '\n';
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -89,7 +89,7 @@ float atom::atom_impl::get_property_float(std::string_view name) const
|
||||
|
||||
std::from_chars_result r = cif::from_chars(s.data(), s.data() + s.length(), result);
|
||||
if (r.ec != std::errc() and VERBOSE > 0)
|
||||
std::cerr << "Error converting " << s << " to number for property " << name << std::endl;
|
||||
std::cerr << "Error converting " << s << " to number for property " << name << '\n';
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -219,7 +219,7 @@ int atom::atom_impl::get_charge() const
|
||||
// if (result == nullptr)
|
||||
// {
|
||||
// if (VERBOSE > 0)
|
||||
// std::cerr << "Compound not found: '" << get_property<std::string>("label_comp_id") << '\'' << std::endl;
|
||||
// std::cerr << "Compound not found: '" << get_property<std::string>("label_comp_id") << '\'' << '\n';
|
||||
|
||||
// throw std::runtime_error("no compound");
|
||||
// }
|
||||
@@ -327,37 +327,6 @@ residue::residue(structure &structure, const std::vector<atom> &atoms)
|
||||
m_atoms.push_back(atom);
|
||||
}
|
||||
|
||||
// residue::residue(residue &&rhs)
|
||||
// : m_structure(rhs.m_structure)
|
||||
// , m_compound_id(std::move(rhs.m_compound_id))
|
||||
// , m_asym_id(std::move(rhs.m_asym_id))
|
||||
// , m_seq_id(rhs.m_seq_id)
|
||||
// , m_auth_seq_id(rhs.m_auth_seq_id)
|
||||
// , m_atoms(std::move(rhs.m_atoms))
|
||||
// {
|
||||
// // std::cerr << "move constructor residue" << std::endl;
|
||||
// rhs.m_structure = nullptr;
|
||||
// }
|
||||
|
||||
// residue &residue::operator=(residue &&rhs)
|
||||
// {
|
||||
// // std::cerr << "move assignment residue" << std::endl;
|
||||
// m_structure = rhs.m_structure;
|
||||
// rhs.m_structure = nullptr;
|
||||
// m_compound_id = std::move(rhs.m_compound_id);
|
||||
// m_asym_id = std::move(rhs.m_asym_id);
|
||||
// m_seq_id = rhs.m_seq_id;
|
||||
// m_auth_seq_id = rhs.m_auth_seq_id;
|
||||
// m_atoms = std::move(rhs.m_atoms);
|
||||
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
// residue::~residue()
|
||||
// {
|
||||
// // std::cerr << "~residue" << std::endl;
|
||||
// }
|
||||
|
||||
std::string residue::get_entity_id() const
|
||||
{
|
||||
std::string result;
|
||||
@@ -381,68 +350,13 @@ EntityType residue::entity_type() const
|
||||
return m_structure->get_entity_type_for_entity_id(get_entity_id());
|
||||
}
|
||||
|
||||
// std::string residue::authInsCode() const
|
||||
// {
|
||||
// assert(m_structure);
|
||||
|
||||
// std::string result;
|
||||
// if (not m_atoms.empty())
|
||||
// result = m_atoms.front().get_property("pdbx_PDB_ins_code");
|
||||
|
||||
// return result;
|
||||
// }
|
||||
|
||||
// std::string residue::get_auth_asym_id() const
|
||||
// {
|
||||
// assert(m_structure);
|
||||
|
||||
// std::string result;
|
||||
// if (not m_atoms.empty())
|
||||
// result = m_atoms.front().get_property("auth_asym_id");
|
||||
|
||||
// return result;
|
||||
// }
|
||||
|
||||
// std::string residue::authSeqID() const
|
||||
// {
|
||||
// return m_auth_seq_id;
|
||||
// }
|
||||
|
||||
// const Compound &residue::compound() const
|
||||
// {
|
||||
// auto result = compound_factory::instance().create(m_compound_id);
|
||||
// if (result == nullptr)
|
||||
// throw std::runtime_error("Failed to create compound " + m_compound_id);
|
||||
// return *result;
|
||||
// }
|
||||
|
||||
// std::string residue::unique_alt_id() const
|
||||
// {
|
||||
// if (m_structure == nullptr)
|
||||
// throw std::runtime_error("Invalid residue object");
|
||||
|
||||
// auto firstAlt = std::find_if(m_atoms.begin(), m_atoms.end(), [](auto &a)
|
||||
// { return not a.get_label_alt_id().empty(); });
|
||||
|
||||
// return firstAlt != m_atoms.end() ? firstAlt->get_label_alt_id() : "";
|
||||
// }
|
||||
|
||||
void residue::add_atom(atom &atom)
|
||||
{
|
||||
// atom.set_property("label_comp_id", m_compound_id);
|
||||
// atom.set_property("label_asym_id", m_asym_id);
|
||||
// if (m_seq_id != 0)
|
||||
// atom.set_property("label_seq_id", std::to_string(m_seq_id));
|
||||
// atom.set_property("auth_seq_id", m_auth_seq_id);
|
||||
|
||||
m_atoms.push_back(atom);
|
||||
}
|
||||
|
||||
std::vector<atom> residue::unique_atoms() const
|
||||
{
|
||||
// if (m_structure == nullptr)
|
||||
// throw std::runtime_error("Invalid residue object");
|
||||
|
||||
std::vector<atom> result;
|
||||
std::string firstAlt;
|
||||
|
||||
@@ -460,7 +374,7 @@ std::vector<atom> residue::unique_atoms() const
|
||||
else if (alt != firstAlt)
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << "skipping alternate atom " << atom << std::endl;
|
||||
std::cerr << "skipping alternate atom " << atom << '\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -498,7 +412,7 @@ atom residue::get_atom_by_atom_id(const std::string &atom_id) const
|
||||
}
|
||||
|
||||
if (not result and VERBOSE > 1)
|
||||
std::cerr << "atom with atom_id " << atom_id << " not found in residue " << m_asym_id << ':' << m_seq_id << std::endl;
|
||||
std::cerr << "atom with atom_id " << atom_id << " not found in residue " << m_asym_id << ':' << m_seq_id << '\n';
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -681,7 +595,7 @@ float monomer::alpha() const
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << ex.what() << std::endl;
|
||||
std::cerr << ex.what() << '\n';
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -710,7 +624,7 @@ float monomer::kappa() const
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << "When trying to calculate kappa for " << m_asym_id << ':' << m_seq_id << ": "
|
||||
<< ex.what() << std::endl;
|
||||
<< ex.what() << '\n';
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -733,7 +647,7 @@ float monomer::tco() const
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << "When trying to calculate tco for " << get_asym_id() << ':' << get_seq_id() << ": "
|
||||
<< ex.what() << std::endl;
|
||||
<< ex.what() << '\n';
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -752,7 +666,7 @@ float monomer::omega() const
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << "When trying to calculate omega for " << get_asym_id() << ':' << get_seq_id() << ": "
|
||||
<< ex.what() << std::endl;
|
||||
<< ex.what() << '\n';
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -828,7 +742,7 @@ float monomer::chi(size_t nr) const
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << e.what() << std::endl;
|
||||
std::cerr << e.what() << '\n';
|
||||
result = 0;
|
||||
}
|
||||
|
||||
@@ -1003,7 +917,7 @@ polymer::polymer(structure &s, const std::string &entityID, const std::string &a
|
||||
else if (VERBOSE > 0)
|
||||
{
|
||||
monomer m{*this, index, seqID, authSeqID, pdbInsCode, compoundID};
|
||||
std::cerr << "Dropping alternate residue " << m << std::endl;
|
||||
std::cerr << "Dropping alternate residue " << m << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1359,7 +1273,7 @@ structure::structure(datablock &db, size_t modelNr, StructureOpenOptions options
|
||||
if (model_nr and *model_nr != m_model_nr)
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << "No atoms loaded for model 1, trying model " << *model_nr << std::endl;
|
||||
std::cerr << "No atoms loaded for model 1, trying model " << *model_nr << '\n';
|
||||
m_model_nr = *model_nr;
|
||||
load_atoms_for_model(options);
|
||||
}
|
||||
@@ -1368,7 +1282,7 @@ structure::structure(datablock &db, size_t modelNr, StructureOpenOptions options
|
||||
if (m_atoms.empty())
|
||||
{
|
||||
if (VERBOSE >= 0)
|
||||
std::cerr << "Warning: no atoms loaded" << std::endl;
|
||||
std::cerr << "Warning: no atoms loaded\n";
|
||||
}
|
||||
else
|
||||
load_data();
|
||||
@@ -1459,7 +1373,7 @@ void structure::load_data()
|
||||
if (ri == resMap.end())
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << "Missing residue for atom " << atom << std::endl;
|
||||
std::cerr << "Missing residue for atom " << atom << '\n';
|
||||
|
||||
// see if it might match a non poly
|
||||
for (auto &res : m_non_polymers)
|
||||
@@ -1494,7 +1408,7 @@ EntityType structure::get_entity_type_for_entity_id(const std::string entityID)
|
||||
EntityType result;
|
||||
|
||||
if (iequals(entity_type, "polymer"))
|
||||
result = EntityType::polymer;
|
||||
result = EntityType::Polymer;
|
||||
else if (iequals(entity_type, "non-polymer"))
|
||||
result = EntityType::NonPolymer;
|
||||
else if (iequals(entity_type, "macrolide"))
|
||||
@@ -1901,7 +1815,7 @@ void structure::remove_atom(atom &a, bool removeFromResidue)
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (VERBOSE > 0)
|
||||
std::cerr << "Error removing atom from residue: " << ex.what() << std::endl;
|
||||
std::cerr << "Error removing atom from residue: " << ex.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2082,7 +1996,7 @@ void structure::change_residue(residue &res, const std::string &newCompound,
|
||||
if (i == atoms.end())
|
||||
{
|
||||
if (VERBOSE >= 0)
|
||||
std::cerr << "Missing atom for atom ID " << a1 << std::endl;
|
||||
std::cerr << "Missing atom for atom ID " << a1 << '\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -2162,7 +2076,7 @@ void structure::remove_residue(residue &res)
|
||||
|
||||
switch (res.entity_type())
|
||||
{
|
||||
case EntityType::polymer:
|
||||
case EntityType::Polymer:
|
||||
{
|
||||
auto &m = dynamic_cast<monomer &>(res);
|
||||
|
||||
@@ -2722,7 +2636,7 @@ std::string structure::create_entity_for_branch(branch &branch)
|
||||
entityID = entity.get_unique_id("");
|
||||
|
||||
if (VERBOSE)
|
||||
std::cout << "Creating new entity " << entityID << " for branched sugar " << entityName << std::endl;
|
||||
std::cout << "Creating new entity " << entityID << " for branched sugar " << entityName << '\n';
|
||||
|
||||
entity.emplace({
|
||||
{"id", entityID},
|
||||
@@ -2926,7 +2840,8 @@ void reconstruct_pdbx(datablock &db)
|
||||
if (db.get("atom_site") == nullptr)
|
||||
throw std::runtime_error("Cannot reconstruct PDBx file, atom data missing");
|
||||
|
||||
|
||||
assert(false);
|
||||
throw std::runtime_error("not implemented yet");
|
||||
}
|
||||
|
||||
} // namespace pdbx
|
||||
|
||||
@@ -485,7 +485,7 @@ sac_parser::CIFToken sac_parser::get_next_token()
|
||||
std::cerr << get_token_name(result);
|
||||
if (result != CIFToken::Eof)
|
||||
std::cerr << " " << std::quoted(m_token_value);
|
||||
std::cerr << std::endl;
|
||||
std::cerr << '\n';
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -830,7 +830,7 @@ void sac_parser::parse_save_frame()
|
||||
void parser::produce_datablock(std::string_view name)
|
||||
{
|
||||
if (VERBOSE >= 4)
|
||||
std::cerr << "producing data_" << name << std::endl;
|
||||
std::cerr << "producing data_" << name << '\n';
|
||||
|
||||
const auto &[iter, ignore] = m_file.emplace(name);
|
||||
m_datablock = &(*iter);
|
||||
@@ -839,7 +839,7 @@ void parser::produce_datablock(std::string_view name)
|
||||
void parser::produce_category(std::string_view name)
|
||||
{
|
||||
if (VERBOSE >= 4)
|
||||
std::cerr << "producing category " << name << std::endl;
|
||||
std::cerr << "producing category " << name << '\n';
|
||||
|
||||
const auto &[cat, ignore] = m_datablock->emplace(name);
|
||||
m_category = &*cat;
|
||||
@@ -848,7 +848,7 @@ void parser::produce_category(std::string_view name)
|
||||
void parser::produce_row()
|
||||
{
|
||||
if (VERBOSE >= 4 and m_category != nullptr)
|
||||
std::cerr << "producing row for category " << m_category->name() << std::endl;
|
||||
std::cerr << "producing row for category " << m_category->name() << '\n';
|
||||
|
||||
if (m_category == nullptr)
|
||||
error("inconsistent categories in loop_");
|
||||
@@ -861,7 +861,7 @@ void parser::produce_row()
|
||||
void parser::produce_item(std::string_view category, std::string_view item, std::string_view value)
|
||||
{
|
||||
if (VERBOSE >= 4)
|
||||
std::cerr << "producing _" << category << '.' << item << " -> " << value << std::endl;
|
||||
std::cerr << "producing _" << category << '.' << item << " -> " << value << '\n';
|
||||
|
||||
if (m_category == nullptr or not iequals(category, m_category->name()))
|
||||
error("inconsistent categories in loop_");
|
||||
|
||||
1770
src/pdb/cif2pdb.cpp
1770
src/pdb/cif2pdb.cpp
File diff suppressed because it is too large
Load Diff
@@ -24,10 +24,9 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <cif++.hpp>
|
||||
#include <cif++/pdb/pdb2cif.hpp>
|
||||
#include <cif++/pdb/pdb2cif_remark_3.hpp>
|
||||
#include <cif++/gzio.hpp>
|
||||
#include "pdb2cif_remark_3.hpp"
|
||||
|
||||
#include "cif++.hpp"
|
||||
|
||||
#include <iomanip>
|
||||
#include <map>
|
||||
@@ -38,10 +37,8 @@ using cif::category;
|
||||
using cif::datablock;
|
||||
using cif::iequals;
|
||||
using cif::key;
|
||||
// using cif::row;
|
||||
using cif::to_lower;
|
||||
using cif::to_lower_copy;
|
||||
// using cif::compound_factory;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// attempt to come up with better error handling
|
||||
@@ -265,7 +262,7 @@ int PDBRecord::vI(int columnFirst, int columnLast)
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (cif::VERBOSE >= 0)
|
||||
std::cerr << "Trying to parse '" << std::string(mValue + columnFirst - 7, mValue + columnLast - 7) << '\'' << std::endl;
|
||||
std::cerr << "Trying to parse '" << std::string(mValue + columnFirst - 7, mValue + columnLast - 7) << '\'' << '\n';
|
||||
throw;
|
||||
}
|
||||
|
||||
@@ -335,7 +332,7 @@ std::tuple<std::string, std::string> SpecificationListParser::GetNextSpecificati
|
||||
else if (not isspace(ch))
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "skipping invalid character in SOURCE ID: " << ch << std::endl;
|
||||
std::cerr << "skipping invalid character in SOURCE ID: " << ch << '\n';
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -352,7 +349,7 @@ std::tuple<std::string, std::string> SpecificationListParser::GetNextSpecificati
|
||||
if (ch == ';')
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Empty value for SOURCE: " << id << std::endl;
|
||||
std::cerr << "Empty value for SOURCE: " << id << '\n';
|
||||
state = eStart;
|
||||
}
|
||||
else if (not isspace(ch))
|
||||
@@ -416,7 +413,7 @@ std::tuple<std::string, std::string> SpecificationListParser::GetNextSpecificati
|
||||
if (ch == ';')
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Skipping invalid header line: '" << std::string(start, mP) << std::endl;
|
||||
std::cerr << "Skipping invalid header line: '" << std::string(start, mP) << '\n';
|
||||
state = eStart;
|
||||
}
|
||||
break;
|
||||
@@ -830,7 +827,7 @@ class PDBFileParser
|
||||
{
|
||||
ec = error::make_error_code(error::pdbErrors::residueNotFound);
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Residue " << chainID << resSeq << iCode << " could not be mapped" << std::endl;
|
||||
std::cerr << "Residue " << chainID << resSeq << iCode << " could not be mapped\n";
|
||||
}
|
||||
else
|
||||
result = mChainSeq2AsymSeq.at(key);
|
||||
@@ -925,7 +922,7 @@ class PDBFileParser
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << ex.what() << std::endl;
|
||||
std::cerr << ex.what() << '\n';
|
||||
ec = error::make_error_code(error::pdbErrors::invalidDate);
|
||||
}
|
||||
|
||||
@@ -937,7 +934,7 @@ class PDBFileParser
|
||||
std::error_code ec;
|
||||
auto result = pdb2cifDate(s, ec);
|
||||
if (ec and cif::VERBOSE > 0)
|
||||
std::cerr << "Invalid date(" << s << "): " << ec.message() << std::endl;
|
||||
std::cerr << "Invalid date(" << s << "): " << ec.message() << '\n';
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1158,7 +1155,7 @@ void PDBFileParser::PreParseInput(std::istream &is)
|
||||
break;
|
||||
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Line number " << lineNr << " is empty!" << std::endl;
|
||||
std::cerr << "Line number " << lineNr << " is empty!\n";
|
||||
|
||||
getline(is, lookahead);
|
||||
++lineNr;
|
||||
@@ -1279,7 +1276,7 @@ void PDBFileParser::PreParseInput(std::istream &is)
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (cif::VERBOSE >= 0)
|
||||
std::cerr << "Dropping FORMUL line (" << (lineNr - 1) << ") with invalid component number '" << value.substr(1, 3) << '\'' << std::endl;
|
||||
std::cerr << "Dropping FORMUL line (" << (lineNr - 1) << ") with invalid component number '" << value.substr(1, 3) << '\'' << '\n';
|
||||
continue;
|
||||
// throw_with_nested(std::runtime_error("Invalid component number '" + value.substr(1, 3) + '\''));
|
||||
}
|
||||
@@ -1307,7 +1304,7 @@ void PDBFileParser::PreParseInput(std::istream &is)
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (cif::VERBOSE >= 0)
|
||||
std::cerr << "Error parsing FORMUL at line " << lineNr << std::endl;
|
||||
std::cerr << "Error parsing FORMUL at line " << lineNr << '\n';
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -1406,7 +1403,7 @@ void PDBFileParser::PreParseInput(std::istream &is)
|
||||
auto f = cur->vF(74, 78);
|
||||
auto r = cif::from_chars(f.data(), f.data() + f.length(), link.distance);
|
||||
if (r.ec != std::errc() and cif::VERBOSE > 0)
|
||||
std::cerr << "Error parsing link distance at line " << cur->mLineNr << std::endl;
|
||||
std::cerr << "Error parsing link distance at line " << cur->mLineNr << '\n';
|
||||
}
|
||||
// 74 – 78 Real(5.2) Length Link distance
|
||||
|
||||
@@ -1420,7 +1417,7 @@ void PDBFileParser::PreParseInput(std::istream &is)
|
||||
if (not dropped.empty())
|
||||
{
|
||||
if (cif::VERBOSE >= 0)
|
||||
std::cerr << "Dropped unsupported records: " << cif::join(dropped, ", ") << std::endl;
|
||||
std::cerr << "Dropped unsupported records: " << cif::join(dropped, ", ") << '\n';
|
||||
}
|
||||
|
||||
if (mData == nullptr)
|
||||
@@ -1449,7 +1446,7 @@ void PDBFileParser::Match(const std::string &expected, bool throwIfMissing)
|
||||
if (throwIfMissing)
|
||||
throw std::runtime_error("Expected record " + expected + " but found " + mRec->mName);
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Expected record " + expected + " but found " + mRec->mName << std::endl;
|
||||
std::cerr << "Expected record " + expected + " but found " + mRec->mName << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1585,7 +1582,7 @@ void PDBFileParser::ParseTitle()
|
||||
if (not iequals(key, "MOL_ID") and mCompounds.empty())
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Ignoring invalid COMPND record" << std::endl;
|
||||
std::cerr << "Ignoring invalid COMPND record\n";
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1633,7 +1630,7 @@ void PDBFileParser::ParseTitle()
|
||||
// if (colon == std::string::npos)
|
||||
// {
|
||||
// if (cif::VERBOSE > 0)
|
||||
// std::cerr << "invalid source field, missing colon (" << s << ')' << std::endl;
|
||||
// std::cerr << "invalid source field, missing colon (" << s << ')' << '\n';
|
||||
// continue;
|
||||
// }
|
||||
SpecificationListParser p(vS(11));
|
||||
@@ -1721,7 +1718,7 @@ void PDBFileParser::ParseTitle()
|
||||
if (mRec->is("NUMMDL"))
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "skipping unimplemented NUMMDL record" << std::endl;
|
||||
std::cerr << "skipping unimplemented NUMMDL record\n";
|
||||
GetNextRecord();
|
||||
}
|
||||
|
||||
@@ -1826,7 +1823,7 @@ void PDBFileParser::ParseTitle()
|
||||
if (mRec->is("SPRSDE"))
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "skipping unimplemented SPRSDE record" << std::endl;
|
||||
std::cerr << "skipping unimplemented SPRSDE record\n";
|
||||
GetNextRecord();
|
||||
}
|
||||
|
||||
@@ -2114,7 +2111,7 @@ void PDBFileParser::ParseRemarks()
|
||||
|
||||
while (mRec->is("REMARK 400"))
|
||||
{
|
||||
s << vS(12) << std::endl;
|
||||
s << vS(12) << '\n';
|
||||
GetNextRecord();
|
||||
}
|
||||
|
||||
@@ -2131,7 +2128,7 @@ void PDBFileParser::ParseRemarks()
|
||||
|
||||
while (mRec->is("REMARK 450"))
|
||||
{
|
||||
s << vS(12) << std::endl;
|
||||
s << vS(12) << '\n';
|
||||
GetNextRecord();
|
||||
}
|
||||
|
||||
@@ -2351,7 +2348,7 @@ void PDBFileParser::ParseRemarks()
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Dropping REMARK 500 at line " << mRec->mLineNr << " due to invalid symmetry operation" << std::endl;
|
||||
std::cerr << "Dropping REMARK 500 at line " << mRec->mLineNr << " due to invalid symmetry operation\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -2757,7 +2754,7 @@ void PDBFileParser::ParseRemarks()
|
||||
|
||||
while (mRec->is("REMARK 999"))
|
||||
{
|
||||
s << vS(12) << std::endl;
|
||||
s << vS(12) << '\n';
|
||||
GetNextRecord();
|
||||
}
|
||||
|
||||
@@ -2781,12 +2778,12 @@ void PDBFileParser::ParseRemarks()
|
||||
std::stringstream s;
|
||||
|
||||
if (not mRec->vS(11).empty())
|
||||
s << mRec->vS(11) << std::endl;
|
||||
s << mRec->vS(11) << '\n';
|
||||
GetNextRecord();
|
||||
|
||||
while (mRec->is(skipped.c_str()))
|
||||
{
|
||||
s << mRec->vS(11) << std::endl;
|
||||
s << mRec->vS(11) << '\n';
|
||||
GetNextRecord();
|
||||
}
|
||||
|
||||
@@ -2923,7 +2920,7 @@ void PDBFileParser::ParseRemark200()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << ec.message() << " for pdbx_collection_date" << std::endl;
|
||||
std::cerr << ec.message() << " for pdbx_collection_date\n";
|
||||
|
||||
// The date field can become truncated when multiple values are available
|
||||
if (diffrnNr != 1)
|
||||
@@ -3044,7 +3041,7 @@ void PDBFileParser::ParseRemark200()
|
||||
"R MERGE FOR SHELL (I)", "R SYM FOR SHELL (I)", "<I/SIGMA(I)> FOR SHELL", "DATA REDUNDANCY IN SHELL" }))
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Not writing reflns_shell record since d_res_high is missing" << std::endl;
|
||||
std::cerr << "Not writing reflns_shell record since d_res_high is missing\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3624,8 +3621,8 @@ void PDBFileParser::ConstructEntities()
|
||||
|
||||
if (cif::VERBOSE > 0)
|
||||
{
|
||||
std::cerr << "Detected residues that cannot be aligned to SEQRES" << std::endl
|
||||
<< "First residue is " << chain.mDbref.chainID << ':' << r.mSeqNum << r.mIcode << std::endl;
|
||||
std::cerr << "Detected residues that cannot be aligned to SEQRES\n"
|
||||
<< "First residue is " << chain.mDbref.chainID << ':' << r.mSeqNum << r.mIcode << '\n';
|
||||
}
|
||||
|
||||
chain.mTerIndex = lastResidueIndex + 1;
|
||||
@@ -4055,7 +4052,7 @@ void PDBFileParser::ConstructEntities()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "dropping unmatched SEQADV record" << std::endl;
|
||||
std::cerr << "dropping unmatched SEQADV record\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -4377,7 +4374,7 @@ void PDBFileParser::ConstructEntities()
|
||||
if (ec) // no need to write a modres if it could not be found
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "dropping unmapped MODRES record" << std::endl;
|
||||
std::cerr << "dropping unmapped MODRES record\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -4474,7 +4471,7 @@ void PDBFileParser::ConstructEntities()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "error mapping unobserved residue" << std::endl;
|
||||
std::cerr << "error mapping unobserved residue\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -4740,7 +4737,7 @@ void PDBFileParser::ParseSecondaryStructure()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Could not map residue for HELIX " << vI(8, 10) << std::endl;
|
||||
std::cerr << "Could not map residue for HELIX " << vI(8, 10) << '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -4858,7 +4855,7 @@ void PDBFileParser::ParseSecondaryStructure()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Dropping SHEET record " << vI(8, 10) << std::endl;
|
||||
std::cerr << "Dropping SHEET record " << vI(8, 10) << '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -4894,7 +4891,7 @@ void PDBFileParser::ParseSecondaryStructure()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "skipping unmatched pdbx_struct_sheet_hbond record" << std::endl;
|
||||
std::cerr << "skipping unmatched pdbx_struct_sheet_hbond record\n";
|
||||
}
|
||||
else
|
||||
getCategory("pdbx_struct_sheet_hbond")->emplace({
|
||||
@@ -4993,7 +4990,7 @@ void PDBFileParser::ParseConnectivtyAnnotation()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Dropping SSBOND " << vI(8, 10) << std::endl;
|
||||
std::cerr << "Dropping SSBOND " << vI(8, 10) << '\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -5014,7 +5011,7 @@ void PDBFileParser::ParseConnectivtyAnnotation()
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Dropping SSBOND " << vI(8, 10) << " due to invalid symmetry operation" << std::endl;
|
||||
std::cerr << "Dropping SSBOND " << vI(8, 10) << " due to invalid symmetry operation\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -5059,7 +5056,7 @@ void PDBFileParser::ParseConnectivtyAnnotation()
|
||||
if (mRec->is("LINK ") or mRec->is("LINKR "))
|
||||
{
|
||||
if (cif::VERBOSE > 0 and mRec->is("LINKR "))
|
||||
std::cerr << "Accepting non-standard LINKR record, but ignoring extra information" << std::endl;
|
||||
std::cerr << "Accepting non-standard LINKR record, but ignoring extra information\n";
|
||||
|
||||
// 1 - 6 Record name "LINK "
|
||||
std::string name1 = vS(13, 16); // 13 - 16 Atom name1 Atom name.
|
||||
@@ -5112,7 +5109,7 @@ void PDBFileParser::ParseConnectivtyAnnotation()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Dropping LINK record at line " << mRec->mLineNr << std::endl;
|
||||
std::cerr << "Dropping LINK record at line " << mRec->mLineNr << '\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -5127,7 +5124,7 @@ void PDBFileParser::ParseConnectivtyAnnotation()
|
||||
if (r.ec != std::errc())
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Distance value '" << distance << "' is not a valid float in LINK record" << std::endl;
|
||||
std::cerr << "Distance value '" << distance << "' is not a valid float in LINK record\n";
|
||||
swap(ccp4LinkID, distance); // assume this is a ccp4_link_id... oh really?
|
||||
}
|
||||
}
|
||||
@@ -5143,7 +5140,7 @@ void PDBFileParser::ParseConnectivtyAnnotation()
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Dropping LINK record at line " << mRec->mLineNr << " due to invalid symmetry operation" << std::endl;
|
||||
std::cerr << "Dropping LINK record at line " << mRec->mLineNr << " due to invalid symmetry operation\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -5215,7 +5212,7 @@ void PDBFileParser::ParseConnectivtyAnnotation()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Dropping CISPEP record at line " << mRec->mLineNr << std::endl;
|
||||
std::cerr << "Dropping CISPEP record at line " << mRec->mLineNr << '\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -5282,7 +5279,7 @@ void PDBFileParser::ParseMiscellaneousFeatures()
|
||||
if (ec)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "skipping struct_site_gen record" << std::endl;
|
||||
std::cerr << "skipping struct_site_gen record\n";
|
||||
}
|
||||
else
|
||||
cat->emplace({
|
||||
@@ -5612,7 +5609,7 @@ void PDBFileParser::ParseCoordinate(int modelNr)
|
||||
if (groupPDB == "HETATM")
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Changing atom from HETATM to ATOM at line " << mRec->mLineNr << std::endl;
|
||||
std::cerr << "Changing atom from HETATM to ATOM at line " << mRec->mLineNr << '\n';
|
||||
groupPDB = "ATOM";
|
||||
}
|
||||
}
|
||||
@@ -5621,7 +5618,7 @@ void PDBFileParser::ParseCoordinate(int modelNr)
|
||||
if (groupPDB == "ATOM")
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Changing atom from ATOM to HETATM at line " << mRec->mLineNr << std::endl;
|
||||
std::cerr << "Changing atom from ATOM to HETATM at line " << mRec->mLineNr << '\n';
|
||||
groupPDB = "HETATM";
|
||||
}
|
||||
}
|
||||
@@ -5805,7 +5802,7 @@ void PDBFileParser::Parse(std::istream &is, cif::file &result)
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
if (cif::VERBOSE >= 0)
|
||||
std::cerr << "Error parsing REMARK 3" << std::endl;
|
||||
std::cerr << "Error parsing REMARK 3\n";
|
||||
throw;
|
||||
}
|
||||
//
|
||||
@@ -5865,12 +5862,12 @@ void PDBFileParser::Parse(std::istream &is, cif::file &result)
|
||||
(z1 - z2) * (z1 - z2)
|
||||
);
|
||||
else if (cif::VERBOSE > 0)
|
||||
std::cerr << "Cannot calculate distance for link since one of the atoms is in another dimension" << std::endl;
|
||||
std::cerr << "Cannot calculate distance for link since one of the atoms is in another dimension\n";
|
||||
}
|
||||
catch (std::exception &ex)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Error finding atom for LINK distance calculation: " << ex.what() << std::endl;
|
||||
std::cerr << "Error finding atom for LINK distance calculation: " << ex.what() << '\n';
|
||||
}
|
||||
|
||||
r["pdbx_dist_value"] = distance;
|
||||
@@ -5885,7 +5882,7 @@ void PDBFileParser::Parse(std::istream &is, cif::file &result)
|
||||
std::cerr << "Error parsing PDB";
|
||||
if (mRec != nullptr)
|
||||
std::cerr << " at line " << mRec->mLineNr;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << '\n';
|
||||
}
|
||||
throw;
|
||||
}
|
||||
@@ -6053,9 +6050,9 @@ int PDBFileParser::PDBChain::AlignResToSeqRes()
|
||||
// C++ is getting closer to Pascal :-)
|
||||
auto printAlignment = [&tb, highX, highY, &rx, &ry, this]()
|
||||
{
|
||||
std::cerr << std::string(cif::get_terminal_width(), '-') << std::endl
|
||||
<< "Alignment for chain " << mDbref.chainID << std::endl
|
||||
<< std::endl;
|
||||
std::cerr << std::string(22, '-') << '\n'
|
||||
<< "Alignment for chain " << mDbref.chainID << '\n'
|
||||
<< '\n';
|
||||
std::vector<std::pair<std::string, std::string>> alignment;
|
||||
|
||||
int x = highX;
|
||||
@@ -6097,9 +6094,9 @@ int PDBFileParser::PDBChain::AlignResToSeqRes()
|
||||
|
||||
reverse(alignment.begin(), alignment.end());
|
||||
for (auto a : alignment)
|
||||
std::cerr << " " << a.first << " -- " << a.second << std::endl;
|
||||
std::cerr << " " << a.first << " -- " << a.second << '\n';
|
||||
|
||||
std::cerr << std::endl;
|
||||
std::cerr << '\n';
|
||||
};
|
||||
|
||||
if (cif::VERBOSE > 1)
|
||||
@@ -6120,7 +6117,7 @@ int PDBFileParser::PDBChain::AlignResToSeqRes()
|
||||
|
||||
case 1:
|
||||
if (cif::VERBOSE > 3)
|
||||
std::cerr << "Missing residue in ATOM records: " << rx[x].mMonID << " at " << rx[x].mSeqNum << std::endl;
|
||||
std::cerr << "Missing residue in ATOM records: " << rx[x].mMonID << " at " << rx[x].mSeqNum << '\n';
|
||||
|
||||
--x;
|
||||
break;
|
||||
@@ -6128,7 +6125,7 @@ int PDBFileParser::PDBChain::AlignResToSeqRes()
|
||||
case 0:
|
||||
if (rx[x].mMonID != ry[y].mMonID)
|
||||
{
|
||||
std::cerr << "Warning, unaligned residues at " << x << "/" << y << "(" << rx[x].mMonID << '/' << ry[y].mMonID << ") SEQRES does not agree with ATOM records" << std::endl;
|
||||
std::cerr << "Warning, unaligned residues at " << x << "/" << y << "(" << rx[x].mMonID << '/' << ry[y].mMonID << ") SEQRES does not agree with ATOM records\n";
|
||||
rx[x].mMonID = ry[y].mMonID;
|
||||
}
|
||||
|
||||
@@ -6185,7 +6182,7 @@ bool PDBFileParser::PDBChain::SameSequence(const PDBChain &rhs) const
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
void ReadPDBFile(std::istream &pdbFile, cif::file &cifFile)
|
||||
void read_pdb_file(std::istream &pdbFile, cif::file &cifFile)
|
||||
{
|
||||
PDBFileParser p;
|
||||
|
||||
@@ -6194,7 +6191,7 @@ void ReadPDBFile(std::istream &pdbFile, cif::file &cifFile)
|
||||
p.Parse(pdbFile, cifFile);
|
||||
|
||||
if (not cifFile.is_valid() and cif::VERBOSE >= 0)
|
||||
std::cerr << "Resulting mmCIF file is not valid!" << std::endl;
|
||||
std::cerr << "Resulting mmCIF file is not valid!\n";
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -6213,7 +6210,7 @@ file read(std::istream &is)
|
||||
// is 'H'. It is as simple as that.
|
||||
|
||||
if (ch == 'h' or ch == 'H')
|
||||
ReadPDBFile(is, result);
|
||||
read_pdb_file(is, result);
|
||||
else
|
||||
{
|
||||
try
|
||||
|
||||
@@ -24,8 +24,9 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "cif++.hpp"
|
||||
#include "cif++/pdb/pdb2cif_remark_3.hpp"
|
||||
#include "pdb2cif_remark_3.hpp"
|
||||
|
||||
#include <cif++.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
@@ -1042,7 +1043,7 @@ std::string Remark3Parser::nextLine()
|
||||
}
|
||||
|
||||
if (cif::VERBOSE >= 2)
|
||||
std::cerr << "RM3: " << mLine << std::endl;
|
||||
std::cerr << "RM3: " << mLine << '\n';
|
||||
|
||||
return mLine;
|
||||
}
|
||||
@@ -1056,7 +1057,11 @@ bool Remark3Parser::match(const char *expr, int nextState)
|
||||
if (result)
|
||||
mState = nextState;
|
||||
else if (cif::VERBOSE >= 3)
|
||||
std::cerr << cif::coloured("No match:", cif::scWHITE, cif::scRED) << " '" << expr << '\'' << std::endl;
|
||||
{
|
||||
using namespace colour;
|
||||
|
||||
std::cerr << coloured("No match:", white, red, bold) << " '" << expr << '\'' << '\n';
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1116,7 +1121,11 @@ float Remark3Parser::parse()
|
||||
}
|
||||
|
||||
if (cif::VERBOSE >= 2)
|
||||
std::cerr << cif::coloured("Dropping line:", cif::scWHITE, cif::scRED) << " '" << mLine << '\'' << std::endl;
|
||||
{
|
||||
using namespace colour;
|
||||
|
||||
std::cerr << coloured("Dropping line:", white, red, bold) << " '" << mLine << '\'' << '\n';
|
||||
}
|
||||
|
||||
++dropped;
|
||||
}
|
||||
@@ -1168,7 +1177,7 @@ void Remark3Parser::storeCapture(const char *category, std::initializer_list<con
|
||||
continue;
|
||||
|
||||
if (cif::VERBOSE >= 3)
|
||||
std::cerr << "storing: '" << value << "' in _" << category << '.' << item << std::endl;
|
||||
std::cerr << "storing: '" << value << "' in _" << category << '.' << item << '\n';
|
||||
|
||||
auto &cat = mDb[category];
|
||||
if (cat.empty() or createNew)
|
||||
@@ -1329,7 +1338,7 @@ bool Remark3Parser::parse(const std::string &expMethod, PDBRecord *r, cif::datab
|
||||
if (line != "REFINEMENT.")
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Unexpected data in REMARK 3" << std::endl;
|
||||
std::cerr << "Unexpected data in REMARK 3\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1341,7 +1350,7 @@ bool Remark3Parser::parse(const std::string &expMethod, PDBRecord *r, cif::datab
|
||||
if (not std::regex_match(line, m, rxp))
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Expected valid PROGRAM line in REMARK 3" << std::endl;
|
||||
std::cerr << "Expected valid PROGRAM line in REMARK 3\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1380,13 +1389,13 @@ bool Remark3Parser::parse(const std::string &expMethod, PDBRecord *r, cif::datab
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
if (cif::VERBOSE >= 0)
|
||||
std::cerr << "Error parsing REMARK 3 with " << parser->program() << std::endl
|
||||
std::cerr << "Error parsing REMARK 3 with " << parser->program() << '\n'
|
||||
<< e.what() << '\n';
|
||||
score = 0;
|
||||
}
|
||||
|
||||
if (cif::VERBOSE >= 2)
|
||||
std::cerr << "Score for " << parser->program() << ": " << score << std::endl;
|
||||
std::cerr << "Score for " << parser->program() << ": " << score << '\n';
|
||||
|
||||
if (score > 0)
|
||||
{
|
||||
@@ -1422,7 +1431,7 @@ bool Remark3Parser::parse(const std::string &expMethod, PDBRecord *r, cif::datab
|
||||
else if (cif::starts_with(program, "X-PLOR"))
|
||||
tryParser(new XPLOR_Remark3Parser(program, expMethod, r, db));
|
||||
else if (cif::VERBOSE > 0)
|
||||
std::cerr << "Skipping unknown program (" << program << ") in REMARK 3" << std::endl;
|
||||
std::cerr << "Skipping unknown program (" << program << ") in REMARK 3\n";
|
||||
}
|
||||
|
||||
sort(scores.begin(), scores.end());
|
||||
@@ -1431,7 +1440,7 @@ bool Remark3Parser::parse(const std::string &expMethod, PDBRecord *r, cif::datab
|
||||
if (guessProgram)
|
||||
{
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Unknown or untrusted program in REMARK 3, trying all parsers to see if there is a match" << std::endl;
|
||||
std::cerr << "Unknown or untrusted program in REMARK 3, trying all parsers to see if there is a match\n";
|
||||
|
||||
tryParser(new BUSTER_TNT_Remark3Parser("BUSTER-TNT", expMethod, r, db));
|
||||
tryParser(new CNS_Remark3Parser("CNS", expMethod, r, db));
|
||||
@@ -1456,7 +1465,7 @@ bool Remark3Parser::parse(const std::string &expMethod, PDBRecord *r, cif::datab
|
||||
auto &best = scores.front();
|
||||
|
||||
if (cif::VERBOSE > 0)
|
||||
std::cerr << "Choosing " << best.parser->program() << " version '" << best.parser->version() << "' as refinement program. Score = " << best.score << std::endl;
|
||||
std::cerr << "Choosing " << best.parser->program() << " version '" << best.parser->version() << "' as refinement program. Score = " << best.score << '\n';
|
||||
|
||||
auto &software = db["software"];
|
||||
std::string program = best.parser->program();
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "pdb2cif.hpp"
|
||||
#include "pdb_record.hpp"
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
63
src/pdb/pdb_record.hpp
Normal file
63
src/pdb/pdb_record.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 NKI/AVL, Netherlands Cancer Institute
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/file.hpp"
|
||||
|
||||
/// \file pdb_record.hpp
|
||||
|
||||
namespace cif::pdb
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
struct PDBRecord
|
||||
{
|
||||
PDBRecord *mNext;
|
||||
uint32_t mLineNr;
|
||||
char mName[11];
|
||||
size_t mVlen;
|
||||
char mValue[1];
|
||||
|
||||
PDBRecord(uint32_t lineNr, const std::string &name, const std::string &value);
|
||||
~PDBRecord();
|
||||
|
||||
void *operator new(size_t);
|
||||
void *operator new(size_t size, size_t vLen);
|
||||
|
||||
void operator delete(void *p);
|
||||
void operator delete(void *p, size_t vLen);
|
||||
|
||||
bool is(const char *name) const;
|
||||
|
||||
char vC(size_t column);
|
||||
std::string vS(size_t columnFirst, size_t columnLast = std::numeric_limits<size_t>::max());
|
||||
int vI(int columnFirst, int columnLast);
|
||||
std::string vF(size_t columnFirst, size_t columnLast);
|
||||
};
|
||||
|
||||
} // namespace pdbx
|
||||
2016
src/pdb/tls.cpp
2016
src/pdb/tls.cpp
File diff suppressed because it is too large
Load Diff
@@ -233,7 +233,7 @@ int main(int argc, char* const argv[])
|
||||
{
|
||||
if (argc != 4)
|
||||
{
|
||||
std::cerr << "Usage symop-map-generator <syminfo.lib-file> <symop.lib-file> < <output-file>" << std::endl;
|
||||
std::cerr << "Usage symop-map-generator <syminfo.lib-file> <symop.lib-file> < <output-file>\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -420,7 +420,7 @@ const space_group kSpaceGroups[] =
|
||||
|
||||
Hall = '"' + Hall + '"' + std::string(40 - Hall.length(), ' ');
|
||||
|
||||
out << "\t{ " << old << ", " << xHM << ", " << Hall << ", " << nr << " }," << std::endl;
|
||||
out << "\t{ " << old << ", " << xHM << ", " << Hall << ", " << nr << " },\n";
|
||||
}
|
||||
|
||||
out << R"(
|
||||
@@ -429,7 +429,7 @@ out << R"(
|
||||
const size_t kNrOfSpaceGroups = sizeof(kSpaceGroups) / sizeof(space_group);
|
||||
|
||||
const symop_datablock kSymopNrTable[] = {
|
||||
)" << std::endl;
|
||||
)";
|
||||
|
||||
int spacegroupNr = 0;
|
||||
for (auto& sd: data)
|
||||
@@ -438,14 +438,14 @@ const symop_datablock kSymopNrTable[] = {
|
||||
std::tie(sp, o, std::ignore) = sd;
|
||||
|
||||
if (sp > spacegroupNr)
|
||||
out << " // " << symInfo[sp].xHM << std::endl;
|
||||
out << " // " << symInfo[sp].xHM << '\n';
|
||||
spacegroupNr = sp;
|
||||
|
||||
out << " { " << std::setw(3) << sp
|
||||
<< ", " << std::setw(3) << o << ", { ";
|
||||
for (auto& i: std::get<2>(sd))
|
||||
out << std::setw(2) << i << ',';
|
||||
out << " } }," << std::endl;
|
||||
out << " } },\n";
|
||||
}
|
||||
|
||||
out << R"(};
|
||||
@@ -453,16 +453,16 @@ const symop_datablock kSymopNrTable[] = {
|
||||
const size_t kSymopNrTableSize = sizeof(kSymopNrTable) / sizeof(symop_datablock);
|
||||
|
||||
} // namespace mmcif
|
||||
)" << std::endl;
|
||||
)";
|
||||
|
||||
out.close();
|
||||
fs::rename(tmpFile, output);
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "Program terminated due to error:" << std::endl
|
||||
<< ex.what() << std::endl;
|
||||
std::cerr << '\n'
|
||||
<< "Program terminated due to error:\n"
|
||||
<< ex.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -364,7 +364,6 @@ const space_group kSpaceGroups[] =
|
||||
const size_t kNrOfSpaceGroups = sizeof(kSpaceGroups) / sizeof(space_group);
|
||||
|
||||
const symop_datablock kSymopNrTable[] = {
|
||||
|
||||
// P 1
|
||||
{ 1, 1, { 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, } },
|
||||
// P -1
|
||||
@@ -5290,4 +5289,3 @@ const symop_datablock kSymopNrTable[] = {
|
||||
const size_t kSymopNrTableSize = sizeof(kSymopNrTable) / sizeof(symop_datablock);
|
||||
|
||||
} // namespace mmcif
|
||||
|
||||
|
||||
@@ -43,11 +43,6 @@
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
|
||||
#if not defined(_WIN32)
|
||||
#include <sys/ioctl.h>
|
||||
#include <termios.h>
|
||||
#endif
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -86,25 +81,10 @@ uint32_t get_terminal_width()
|
||||
return csbi.srWindow.Right - csbi.srWindow.Left + 1;
|
||||
}
|
||||
|
||||
std::string GetExecutablePath()
|
||||
{
|
||||
WCHAR buffer[4096];
|
||||
|
||||
DWORD n = ::GetModuleFileNameW(nullptr, buffer, sizeof(buffer) / sizeof(WCHAR));
|
||||
if (n == 0)
|
||||
throw std::runtime_error("could not get exe path");
|
||||
|
||||
std::wstring ws(buffer);
|
||||
|
||||
// convert from utf16 to utf8
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv1;
|
||||
std::string u8str = conv1.to_bytes(ws);
|
||||
|
||||
return u8str;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <termios.h>
|
||||
#include <limits.h>
|
||||
|
||||
uint32_t get_terminal_width()
|
||||
@@ -120,17 +100,6 @@ uint32_t get_terminal_width()
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string get_executable_path()
|
||||
{
|
||||
using namespace std::literals;
|
||||
|
||||
// This used to be PATH_MAX, but lets simply assume 1024 is enough...
|
||||
char path[1024] = "";
|
||||
if (readlink("/proc/self/exe", path, sizeof(path)) == -1)
|
||||
throw std::runtime_error("could not get exe path "s + strerror(errno));
|
||||
return {path};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -241,10 +210,15 @@ void progress_bar_impl::message(const std::string &msg)
|
||||
}
|
||||
|
||||
const char* kSpinner[] = {
|
||||
// "▉", "▊", "▋", "▌", "▍", "▎", "▏", "▎", "▍", "▌", "▋", "▊", "▉"
|
||||
".", "o", "O", "0", "O", "o", ".", " "
|
||||
// ".", "o", "O", "0", "O", "o", ".", " "
|
||||
// "⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"
|
||||
".", "o", "O", "0", "@", "*", " "
|
||||
};
|
||||
|
||||
const size_t kSpinnerCount = sizeof(kSpinner) / sizeof(char*);
|
||||
|
||||
const int kSpinnerTimeInterval = 100;
|
||||
|
||||
const uint32_t kMinBarWidth = 40, kMinMsgWidth = 12;
|
||||
|
||||
void progress_bar_impl::print_progress()
|
||||
@@ -297,7 +271,7 @@ void progress_bar_impl::print_progress()
|
||||
msg << std::setw(3) << static_cast<int>(std::ceil(progress * 100)) << "% ";
|
||||
|
||||
auto now = std::chrono::system_clock::now();
|
||||
m_spinner_index = (std::chrono::duration_cast<std::chrono::milliseconds>(now - m_start).count() / 200) % kSpinnerCount;
|
||||
m_spinner_index = (std::chrono::duration_cast<std::chrono::milliseconds>(now - m_start).count() / kSpinnerTimeInterval) % kSpinnerCount;
|
||||
|
||||
msg << kSpinner[m_spinner_index];
|
||||
|
||||
|
||||
@@ -97,24 +97,6 @@ type_validator::~type_validator()
|
||||
delete m_rx;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct my_from_chars
|
||||
{
|
||||
static std::from_chars_result from_chars(const char *a, const char *b, T &d)
|
||||
{
|
||||
return cif::from_chars(a, b, d);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct std_from_chars
|
||||
{
|
||||
static std::from_chars_result from_chars(const char *a, const char *b, T &d)
|
||||
{
|
||||
return std::from_chars(a, b, d);
|
||||
}
|
||||
};
|
||||
|
||||
int type_validator::compare(std::string_view a, std::string_view b) const
|
||||
{
|
||||
int result = 0;
|
||||
@@ -260,7 +242,7 @@ void category_validator::addItemValidator(item_validator &&v)
|
||||
|
||||
auto r = m_item_validators.insert(std::move(v));
|
||||
if (not r.second and VERBOSE >= 4)
|
||||
std::cout << "Could not add validator for item " << v.m_tag << " to category " << m_name << std::endl;
|
||||
std::cout << "Could not add validator for item " << v.m_tag << " to category " << m_name << '\n';
|
||||
}
|
||||
|
||||
const item_validator *category_validator::get_validator_for_item(std::string_view tag) const
|
||||
@@ -270,7 +252,7 @@ const item_validator *category_validator::get_validator_for_item(std::string_vie
|
||||
if (i != m_item_validators.end())
|
||||
result = &*i;
|
||||
else if (VERBOSE > 4)
|
||||
std::cout << "No validator for tag " << tag << std::endl;
|
||||
std::cout << "No validator for tag " << tag << '\n';
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -280,7 +262,7 @@ void validator::add_type_validator(type_validator &&v)
|
||||
{
|
||||
auto r = m_type_validators.insert(std::move(v));
|
||||
if (not r.second and VERBOSE > 4)
|
||||
std::cout << "Could not add validator for type " << v.m_name << std::endl;
|
||||
std::cout << "Could not add validator for type " << v.m_name << '\n';
|
||||
}
|
||||
|
||||
const type_validator *validator::get_validator_for_type(std::string_view typeCode) const
|
||||
@@ -291,7 +273,7 @@ const type_validator *validator::get_validator_for_type(std::string_view typeCod
|
||||
if (i != m_type_validators.end())
|
||||
result = &*i;
|
||||
else if (VERBOSE > 4)
|
||||
std::cout << "No validator for type " << typeCode << std::endl;
|
||||
std::cout << "No validator for type " << typeCode << '\n';
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -299,7 +281,7 @@ void validator::add_category_validator(category_validator &&v)
|
||||
{
|
||||
auto r = m_category_validators.insert(std::move(v));
|
||||
if (not r.second and VERBOSE > 4)
|
||||
std::cout << "Could not add validator for category " << v.m_name << std::endl;
|
||||
std::cout << "Could not add validator for category " << v.m_name << '\n';
|
||||
}
|
||||
|
||||
const category_validator *validator::get_validator_for_category(std::string_view category) const
|
||||
@@ -309,7 +291,7 @@ const category_validator *validator::get_validator_for_category(std::string_view
|
||||
if (i != m_category_validators.end())
|
||||
result = &*i;
|
||||
else if (VERBOSE > 4)
|
||||
std::cout << "No validator for category " << category << std::endl;
|
||||
std::cout << "No validator for category " << category << '\n';
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -325,7 +307,7 @@ item_validator *validator::get_validator_for_item(std::string_view tag) const
|
||||
result = const_cast<item_validator *>(cv->get_validator_for_item(item));
|
||||
|
||||
if (result == nullptr and VERBOSE > 4)
|
||||
std::cout << "No validator for item " << tag << std::endl;
|
||||
std::cout << "No validator for item " << tag << '\n';
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -394,7 +376,7 @@ void validator::report_error(const std::string &msg, bool fatal) const
|
||||
if (m_strict or fatal)
|
||||
throw validation_error(msg);
|
||||
else if (VERBOSE > 0)
|
||||
std::cerr << msg << std::endl;
|
||||
std::cerr << msg << '\n';
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
@@ -181,7 +181,7 @@ _pdbx_chem_comp_audit.comp_id
|
||||
_pdbx_chem_comp_audit.action_type
|
||||
_pdbx_chem_comp_audit.date
|
||||
_pdbx_chem_comp_audit.processing_site
|
||||
REA_v2 "CREA_v2te component" 1999-07-08 RCSB
|
||||
REA_v2 "Create component" 1999-07-08 RCSB
|
||||
REA_v2 "Modify descriptor" 2011-06-04 RCSB
|
||||
REA_v2 "Other modification" 2016-10-18 RCSB
|
||||
#
|
||||
|
||||
@@ -82,4 +82,19 @@ BOOST_AUTO_TEST_CASE(fmt_1)
|
||||
|
||||
BOOST_CHECK_EQUAL(cif::format("Hello, %-10.10s, the magic number is %d and pi is %g", world, 42, cif::kPI).str(),
|
||||
"Hello, world , the magic number is 42 and pi is 3.14159");
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
BOOST_AUTO_TEST_CASE(clr_1)
|
||||
{
|
||||
using namespace cif::colour;
|
||||
|
||||
std::cout << "Hello, " << cif::coloured("world!", white, red, regular) << '\n'
|
||||
<< "Hello, " << cif::coloured("world!", white, red, bold) << '\n'
|
||||
<< "Hello, " << cif::coloured("world!", black, red) << '\n'
|
||||
<< "Hello, " << cif::coloured("world!", white, green) << '\n'
|
||||
<< "Hello, " << cif::coloured("world!", white, blue) << '\n'
|
||||
<< "Hello, " << cif::coloured("world!", blue, white) << '\n'
|
||||
<< "Hello, " << cif::coloured("world!", red, white, bold) << '\n';
|
||||
}
|
||||
@@ -194,9 +194,9 @@ _atom_type.symbol C
|
||||
if (not (expected.front() == structure.get_datablock()))
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
std::cout << expected.front() << std::endl
|
||||
<< std::endl
|
||||
<< structure.get_datablock() << std::endl;
|
||||
std::cout << expected.front() << '\n'
|
||||
<< '\n'
|
||||
<< structure.get_datablock() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,9 +306,9 @@ _atom_type.symbol C
|
||||
if (not (expected.front() == structure.get_datablock()))
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
std::cout << expected.front() << std::endl
|
||||
<< std::endl
|
||||
<< structure.get_datablock() << std::endl;
|
||||
std::cout << expected.front() << '\n'
|
||||
<< '\n'
|
||||
<< structure.get_datablock() << '\n';
|
||||
|
||||
|
||||
expected.save("/tmp/a");
|
||||
|
||||
@@ -72,7 +72,7 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
std::cerr << e.what() << '\n';
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <cif++/utilities.hpp>
|
||||
#include "cif++/utilities.hpp"
|
||||
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
@@ -124,7 +124,7 @@ BOOST_AUTO_TEST_CASE(t1)
|
||||
|
||||
BOOST_TEST(rmsd < 1e-5);
|
||||
|
||||
// std::cout << "rmsd: " << RMSd(p1, p2) << std::endl;
|
||||
// std::cout << "rmsd: " << RMSd(p1, p2) << '\n';
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(t2)
|
||||
@@ -161,7 +161,7 @@ BOOST_AUTO_TEST_CASE(t3)
|
||||
v.rotate(q);
|
||||
v += p[0];
|
||||
|
||||
std::cout << v << std::endl;
|
||||
std::cout << v << '\n';
|
||||
|
||||
double a = cif::angle(v, p[0], p[1]);
|
||||
|
||||
@@ -517,6 +517,55 @@ BOOST_AUTO_TEST_CASE(symm_2bi3_1, *utf::tolerance(0.1f))
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(symm_2bi3_1a, *utf::tolerance(0.1f))
|
||||
{
|
||||
using namespace cif::literals;
|
||||
|
||||
cif::file f(gTestDir / "2bi3.cif.gz");
|
||||
|
||||
auto &db = f.front();
|
||||
|
||||
cif::crystal c(db);
|
||||
auto struct_conn = db["struct_conn"];
|
||||
auto atom_site = db["atom_site"];
|
||||
|
||||
for (const auto &[
|
||||
asym1, seqid1, authseqid1, atomid1, symm1,
|
||||
asym2, seqid2, authseqid2, atomid2, symm2,
|
||||
dist] : struct_conn.find<
|
||||
std::string,std::optional<int>,std::string,std::string,std::string,
|
||||
std::string,std::optional<int>,std::string,std::string,std::string,
|
||||
float>(
|
||||
cif::key("ptnr1_symmetry") != "1_555" or cif::key("ptnr2_symmetry") != "1_555",
|
||||
"ptnr1_label_asym_id", "ptnr1_label_seq_id", "ptnr1_auth_seq_id", "ptnr1_label_atom_id", "ptnr1_symmetry",
|
||||
"ptnr2_label_asym_id", "ptnr2_label_seq_id", "ptnr2_auth_seq_id", "ptnr2_label_atom_id", "ptnr2_symmetry",
|
||||
"pdbx_dist_value"
|
||||
))
|
||||
{
|
||||
cif::point p1 = atom_site.find1<float,float,float>(
|
||||
"label_asym_id"_key == asym1 and "label_seq_id"_key == seqid1 and "auth_seq_id"_key == authseqid1 and "label_atom_id"_key == atomid1,
|
||||
"cartn_x", "cartn_y", "cartn_z");
|
||||
cif::point p2 = atom_site.find1<float,float,float>(
|
||||
"label_asym_id"_key == asym2 and "label_seq_id"_key == seqid2 and "auth_seq_id"_key == authseqid2 and "label_atom_id"_key == atomid2,
|
||||
"cartn_x", "cartn_y", "cartn_z");
|
||||
|
||||
auto sa1 = c.symmetry_copy(p1, cif::sym_op(symm1));
|
||||
auto sa2 = c.symmetry_copy(p2, cif::sym_op(symm2));
|
||||
|
||||
BOOST_TEST(cif::distance(sa1, sa2) == dist);
|
||||
|
||||
const auto &[d, p, so] = c.closest_symmetry_copy(p1, p2);
|
||||
|
||||
BOOST_TEST(p.m_x == sa2.m_x);
|
||||
BOOST_TEST(p.m_y == sa2.m_y);
|
||||
BOOST_TEST(p.m_z == sa2.m_z);
|
||||
|
||||
BOOST_TEST(d == dist);
|
||||
BOOST_TEST(so.string() == symm2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(symm_3bwh_1, *utf::tolerance(0.1f))
|
||||
{
|
||||
|
||||
@@ -150,6 +150,31 @@ BOOST_AUTO_TEST_CASE(cc_2)
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cc_3)
|
||||
{
|
||||
cif::category c("foo");
|
||||
c.emplace({
|
||||
{ "f-1", 1 },
|
||||
{ "f-2", "-1" },
|
||||
{ "f-3", "+1" },
|
||||
{ "f-4", " 1" },
|
||||
{ "f-5", " +1" },
|
||||
{ "f-6", "1 " },
|
||||
});
|
||||
|
||||
auto row = c.front();
|
||||
BOOST_CHECK_EQUAL(row["f-1"].as<int>(), 1);
|
||||
BOOST_CHECK_EQUAL(row["f-2"].as<int>(), -1);
|
||||
BOOST_CHECK_EQUAL(row["f-3"].as<int>(), 1);
|
||||
|
||||
// BOOST_CHECK_THROW(row["f-4"].as<int>(), std::exception);
|
||||
// BOOST_CHECK_THROW(row["f-5"].as<int>(), std::exception);
|
||||
// BOOST_CHECK_THROW(row["f-6"].as<int>(), std::exception);
|
||||
BOOST_CHECK_EQUAL(row["f-4"].as<int>(), 0);
|
||||
BOOST_CHECK_EQUAL(row["f-5"].as<int>(), 0);
|
||||
BOOST_CHECK_EQUAL(row["f-6"].as<int>(), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(item_1)
|
||||
{
|
||||
using namespace cif;
|
||||
@@ -1865,6 +1890,15 @@ _test.name
|
||||
BOOST_TEST(db["test"].find_first<int>(cif::key("id") == 1, "id") == 1);
|
||||
BOOST_TEST(db["test"].find_first<int>(cif::all(), "id") == 1);
|
||||
|
||||
std::optional<int> v;
|
||||
|
||||
v = db["test"].find_first<std::optional<int>>(cif::key("id") == 1, "id");
|
||||
BOOST_TEST(v.has_value());
|
||||
BOOST_TEST(*v == 1);
|
||||
|
||||
v = db["test"].find_first<std::optional<int>>(cif::key("id") == 6, "id");
|
||||
BOOST_TEST(not v.has_value());
|
||||
|
||||
// find1 tests
|
||||
BOOST_TEST(db["test"].find1<int>(cif::key("id") == 1, "id") == 1);
|
||||
BOOST_CHECK_THROW(db["test"].find1<int>(cif::all(), "id"), cif::multiple_results_error);
|
||||
@@ -1882,7 +1916,7 @@ BOOST_AUTO_TEST_CASE(r1)
|
||||
of pdbx_nonpoly_scheme which itself is a parent of pdbx_entity_nonpoly. If I want to rename a residue
|
||||
I cannot update pdbx_nonpoly_scheme since changing a parent changes children, but not vice versa.
|
||||
|
||||
But if I change the comp_id in atom_site, the pdbx_nonpoly_scheme is update, that's good, and then
|
||||
But if I change the comp_id in atom_site, the pdbx_nonpoly_scheme is updated, that's good, and then
|
||||
pdbx_entity_nonpoly is updated and that's bad.
|
||||
|
||||
The idea is now that if we update a parent and a child that must change as well, we first check
|
||||
@@ -2168,6 +2202,228 @@ _cat_3.num
|
||||
// f.save(std::cout);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(pc_1)
|
||||
{
|
||||
/*
|
||||
Parent/child tests
|
||||
|
||||
Note that the dictionary is different than the one in test r1
|
||||
*/
|
||||
|
||||
const char dict[] = R"(
|
||||
data_test_dict.dic
|
||||
_datablock.id test_dict.dic
|
||||
_datablock.description
|
||||
;
|
||||
A test dictionary
|
||||
;
|
||||
_dictionary.title test_dict.dic
|
||||
_dictionary.datablock_id test_dict.dic
|
||||
_dictionary.version 1.0
|
||||
|
||||
loop_
|
||||
_item_type_list.code
|
||||
_item_type_list.primitive_code
|
||||
_item_type_list.construct
|
||||
code char
|
||||
'[][_,.;:"&<>()/\{}'`~!@#$%A-Za-z0-9*|+-]*'
|
||||
|
||||
text char
|
||||
'[][ \n\t()_,.;:"&<>/\{}'`~!@#$%?+=*A-Za-z0-9|^-]*'
|
||||
|
||||
int numb
|
||||
'[+-]?[0-9]+'
|
||||
|
||||
save_cat_1
|
||||
_category.description 'A simple test category'
|
||||
_category.id cat_1
|
||||
_category.mandatory_code no
|
||||
_category_key.name '_cat_1.id'
|
||||
save_
|
||||
|
||||
save__cat_1.id
|
||||
_item.name '_cat_1.id'
|
||||
_item.category_id cat_1
|
||||
_item.mandatory_code yes
|
||||
_item_linked.child_name '_cat_2.parent_id'
|
||||
_item_linked.parent_name '_cat_1.id'
|
||||
_item_type.code int
|
||||
save_
|
||||
|
||||
save__cat_1.name
|
||||
_item.name '_cat_1.name'
|
||||
_item.category_id cat_1
|
||||
_item.mandatory_code yes
|
||||
_item_type.code code
|
||||
save_
|
||||
|
||||
save__cat_1.desc
|
||||
_item.name '_cat_1.desc'
|
||||
_item.category_id cat_1
|
||||
_item.mandatory_code yes
|
||||
_item_type.code text
|
||||
save_
|
||||
|
||||
save_cat_2
|
||||
_category.description 'A second simple test category'
|
||||
_category.id cat_2
|
||||
_category.mandatory_code no
|
||||
_category_key.name '_cat_2.id'
|
||||
save_
|
||||
|
||||
save__cat_2.id
|
||||
_item.name '_cat_2.id'
|
||||
_item.category_id cat_2
|
||||
_item.mandatory_code yes
|
||||
_item_type.code int
|
||||
save_
|
||||
|
||||
save__cat_2.name
|
||||
_item.name '_cat_2.name'
|
||||
_item.category_id cat_2
|
||||
_item.mandatory_code yes
|
||||
_item_type.code code
|
||||
save_
|
||||
|
||||
save__cat_2.num
|
||||
_item.name '_cat_2.num'
|
||||
_item.category_id cat_2
|
||||
_item.mandatory_code yes
|
||||
_item_type.code int
|
||||
save_
|
||||
|
||||
save__cat_2.desc
|
||||
_item.name '_cat_2.desc'
|
||||
_item.category_id cat_2
|
||||
_item.mandatory_code yes
|
||||
_item_type.code text
|
||||
save_
|
||||
|
||||
save_cat_3
|
||||
_category.description 'A third simple test category'
|
||||
_category.id cat_3
|
||||
_category.mandatory_code no
|
||||
_category_key.name '_cat_3.id'
|
||||
save_
|
||||
|
||||
save__cat_3.id
|
||||
_item.name '_cat_3.id'
|
||||
_item.category_id cat_3
|
||||
_item.mandatory_code yes
|
||||
_item_type.code int
|
||||
save_
|
||||
|
||||
save__cat_3.name
|
||||
_item.name '_cat_3.name'
|
||||
_item.category_id cat_3
|
||||
_item.mandatory_code yes
|
||||
_item_type.code code
|
||||
save_
|
||||
|
||||
save__cat_3.num
|
||||
_item.name '_cat_3.num'
|
||||
_item.category_id cat_3
|
||||
_item.mandatory_code yes
|
||||
_item_type.code int
|
||||
save_
|
||||
|
||||
loop_
|
||||
_pdbx_item_linked_group_list.parent_category_id
|
||||
_pdbx_item_linked_group_list.link_group_id
|
||||
_pdbx_item_linked_group_list.parent_name
|
||||
_pdbx_item_linked_group_list.child_name
|
||||
_pdbx_item_linked_group_list.child_category_id
|
||||
cat_1 1 '_cat_1.name' '_cat_2.name' cat_2
|
||||
cat_2 1 '_cat_2.name' '_cat_3.name' cat_3
|
||||
cat_2 1 '_cat_2.num' '_cat_3.num' cat_3
|
||||
|
||||
)";
|
||||
|
||||
struct membuf : public std::streambuf
|
||||
{
|
||||
membuf(char *text, size_t length)
|
||||
{
|
||||
this->setg(text, text, text + length);
|
||||
}
|
||||
} buffer(const_cast<char *>(dict), sizeof(dict) - 1);
|
||||
|
||||
std::istream is_dict(&buffer);
|
||||
|
||||
auto validator = cif::parse_dictionary("test", is_dict);
|
||||
|
||||
cif::file f;
|
||||
f.set_validator(&validator);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
const char data[] = R"(
|
||||
data_test
|
||||
loop_
|
||||
_cat_1.id
|
||||
_cat_1.name
|
||||
_cat_1.desc
|
||||
1 aap Aap
|
||||
2 noot Noot
|
||||
3 mies Mies
|
||||
|
||||
loop_
|
||||
_cat_2.id
|
||||
_cat_2.name
|
||||
_cat_2.num
|
||||
_cat_2.desc
|
||||
1 aap 1 'Een dier'
|
||||
2 aap 2 'Een andere aap'
|
||||
3 noot 1 'walnoot bijvoorbeeld'
|
||||
|
||||
loop_
|
||||
_cat_3.id
|
||||
_cat_3.name
|
||||
_cat_3.num
|
||||
1 aap 1
|
||||
2 aap 2
|
||||
)";
|
||||
|
||||
using namespace cif::literals;
|
||||
|
||||
struct data_membuf : public std::streambuf
|
||||
{
|
||||
data_membuf(char *text, size_t length)
|
||||
{
|
||||
this->setg(text, text, text + length);
|
||||
}
|
||||
} data_buffer(const_cast<char *>(data), sizeof(data) - 1);
|
||||
|
||||
std::istream is_data(&data_buffer);
|
||||
f.load(is_data);
|
||||
|
||||
auto &cat1 = f.front()["cat_1"];
|
||||
auto &cat2 = f.front()["cat_2"];
|
||||
auto &cat3 = f.front()["cat_3"];
|
||||
|
||||
// some parent/child tests
|
||||
|
||||
// find all children in cat2 for the row with id == 1 in cat1
|
||||
auto rs1 = cat1.get_children(cat1.find1("id"_key == 1), cat2);
|
||||
BOOST_TEST(rs1.size() == 2);
|
||||
|
||||
auto rs2 = cat1.get_children(cat1.find1("id"_key == 2), cat2);
|
||||
BOOST_TEST(rs2.size() == 1);
|
||||
|
||||
auto rs3 = cat1.get_children(cat1.find1("id"_key == 3), cat2);
|
||||
BOOST_TEST(rs3.size() == 0);
|
||||
|
||||
// finding parents
|
||||
auto rs4 = cat2.get_parents(cat2.find1("id"_key == 1), cat1);
|
||||
BOOST_TEST(rs4.size() == 1);
|
||||
|
||||
auto rs5 = cat3.get_parents(cat3.find1("id"_key == 1), cat2);
|
||||
BOOST_TEST(rs5.size() == 1);
|
||||
|
||||
// This link is not defined:
|
||||
auto rs6 = cat3.get_parents(cat3.find1("id"_key == 1), cat1);
|
||||
BOOST_TEST(rs6.size() == 0);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
// BOOST_AUTO_TEST_CASE(bondmap_1)
|
||||
|
||||
Reference in New Issue
Block a user