mirror of
https://github.com/PDB-REDO/libcifpp.git
synced 2026-06-04 22:14:24 +08:00
Compare commits
71 Commits
v9.0.3
...
with-sqlit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56aab89176 | ||
|
|
e3e7648c5c | ||
|
|
83ee1984d8 | ||
|
|
f9741a27cd | ||
|
|
0b002afb9f | ||
|
|
a58e5a1bfc | ||
|
|
1229652444 | ||
|
|
05197a85c6 | ||
|
|
71cd4958bd | ||
|
|
61965c2391 | ||
|
|
0f60f42f9e | ||
|
|
2e61b330c4 | ||
|
|
964e7620eb | ||
|
|
8a329e7c2d | ||
|
|
8485747377 | ||
|
|
34af410d5e | ||
|
|
8f5b9eb631 | ||
|
|
388eae211e | ||
|
|
73f18a4da2 | ||
|
|
8e2494532e | ||
|
|
2f7f62bdce | ||
|
|
bf9551a994 | ||
|
|
30ff5bea36 | ||
|
|
b6568664ea | ||
|
|
863f010a7c | ||
|
|
da76bbae7c | ||
|
|
e2454a2e79 | ||
|
|
9503a7e9b4 | ||
|
|
20784bdaf5 | ||
|
|
9558085105 | ||
|
|
429c31ae42 | ||
|
|
58ac1ce033 | ||
|
|
23e575858c | ||
|
|
4a31878975 | ||
|
|
9f6e1e245b | ||
|
|
8a8ca9599d | ||
|
|
00b0473438 | ||
|
|
7a9d94bc57 | ||
|
|
a3ba760ab5 | ||
|
|
510e336306 | ||
|
|
f15a76e29b | ||
|
|
915a147449 | ||
|
|
edf24ca9ff | ||
|
|
ffff2479d2 | ||
|
|
46a9318aa5 | ||
|
|
4a7f48eed8 | ||
|
|
42e66afd92 | ||
|
|
b550e9b027 | ||
|
|
452bb83ce7 | ||
|
|
6eda9aaf36 | ||
|
|
251fb55d6a | ||
|
|
f94e9aece9 | ||
|
|
c565bb96be | ||
|
|
e51f31dc4c | ||
|
|
4e128885d6 | ||
|
|
b37054228d | ||
|
|
815b33fee0 | ||
|
|
97f55c1639 | ||
|
|
89de73eb6f | ||
|
|
75f2ec3792 | ||
|
|
f4d29e8da9 | ||
|
|
b97b2638b8 | ||
|
|
ea8dea8cbd | ||
|
|
bc0222dc0e | ||
|
|
10a6b5649b | ||
|
|
ff2a233156 | ||
|
|
743e2800f8 | ||
|
|
32ac884127 | ||
|
|
bec69f7d07 | ||
|
|
a99215ad6a | ||
|
|
e3d2cbd044 |
658
CMakeLists.txt
658
CMakeLists.txt
@@ -24,16 +24,18 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.23)
|
||||
|
||||
cmake_policy(SET CMP0135 NEW)
|
||||
|
||||
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
||||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
|
||||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
|
||||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
|
||||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
|
||||
endif()
|
||||
|
||||
# set the project name
|
||||
project(
|
||||
libcifpp
|
||||
VERSION 9.0.3
|
||||
LANGUAGES CXX C)
|
||||
libcifpp
|
||||
VERSION 10.0.0
|
||||
LANGUAGES CXX C)
|
||||
|
||||
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||
|
||||
@@ -43,19 +45,20 @@ include(GenerateExportHeader)
|
||||
include(CTest)
|
||||
include(ExternalProject)
|
||||
include(FetchContent)
|
||||
include(VersionString)
|
||||
|
||||
# When building with ninja-multiconfig, build both debug and release by default
|
||||
if(CMAKE_GENERATOR STREQUAL "Ninja Multi-Config")
|
||||
set(CMAKE_CROSS_CONFIGS "Debug;Release")
|
||||
set(CMAKE_DEFAULT_CONFIGS "Debug;Release")
|
||||
set(CMAKE_CROSS_CONFIGS "Debug;Release")
|
||||
set(CMAKE_DEFAULT_CONFIGS "Debug;Release")
|
||||
endif()
|
||||
|
||||
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
|
||||
set(CMAKE_CXX_FLAGS
|
||||
"${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers"
|
||||
)
|
||||
set(CMAKE_CXX_FLAGS
|
||||
"${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers"
|
||||
)
|
||||
elseif(MSVC)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
||||
endif()
|
||||
|
||||
# Build documentation?
|
||||
@@ -64,117 +67,129 @@ set(BUILD_DOCUMENTATION OFF CACHE BOOL "Build the documentation")
|
||||
# Optionally build a version to be installed inside CCP4
|
||||
set(BUILD_FOR_CCP4 OFF CACHE BOOL "Build a version to be installed in CCP4")
|
||||
|
||||
# Create the cql/sqlite interface
|
||||
set(BUILD_SQLITE_INTERFACE ON CACHE BOOL "Build the sqlite interface")
|
||||
|
||||
# Building shared libraries?
|
||||
if(NOT(BUILD_FOR_CCP4 AND WIN32))
|
||||
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build a shared library instead of a static one")
|
||||
if(NOT (BUILD_FOR_CCP4 AND WIN32))
|
||||
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build a shared library instead of a static one")
|
||||
endif()
|
||||
|
||||
if(PROJECT_IS_TOP_LEVEL AND NOT BUILD_FOR_CCP4)
|
||||
# Lots of code depend on the availability of the components.cif file
|
||||
set(CIFPP_DOWNLOAD_CCD ON CACHE BOOL "Download the CCD file components.cif during installation")
|
||||
# Lots of code depend on the availability of the components.cif file
|
||||
set(CIFPP_DOWNLOAD_CCD ON CACHE BOOL "Download the CCD file components.cif during installation")
|
||||
|
||||
# An optional cron script can be installed to keep the data files up-to-date
|
||||
if(UNIX AND NOT APPLE)
|
||||
set(CIFPP_INSTALL_UPDATE_SCRIPT ON CACHE BOOL "Install the script to update CCD and dictionary files")
|
||||
endif()
|
||||
# An optional cron script can be installed to keep the data files up-to-date
|
||||
if(UNIX AND NOT APPLE)
|
||||
set(CIFPP_INSTALL_UPDATE_SCRIPT ON CACHE BOOL "Install the script to update CCD and dictionary files")
|
||||
endif()
|
||||
else()
|
||||
unset(CIFPP_DOWNLOAD_CCD)
|
||||
unset(CIFPP_INSTALL_UPDATE_SCRIPT)
|
||||
unset(CIFPP_DOWNLOAD_CCD)
|
||||
unset(CIFPP_INSTALL_UPDATE_SCRIPT)
|
||||
endif()
|
||||
|
||||
# When CCP4 is sourced in the environment, we can recreate the symmetry
|
||||
# operations table
|
||||
if(EXISTS "$ENV{CCP4}/lib/data/syminfo.lib")
|
||||
set(CIFPP_RECREATE_SYMOP_DATA ON CACHE BOOL "Recreate SymOp data table in case it is out of date")
|
||||
set(CIFPP_RECREATE_SYMOP_DATA ON CACHE BOOL "Recreate SymOp data table in case it is out of date")
|
||||
endif()
|
||||
|
||||
# CCP4 build
|
||||
if(BUILD_FOR_CCP4)
|
||||
if("$ENV{CCP4}" STREQUAL "" OR NOT EXISTS $ENV{CCP4})
|
||||
message(FATAL_ERROR "cifpp: A CCP4 built was requested but CCP4 was not sourced")
|
||||
else()
|
||||
list(PREPEND CMAKE_MODULE_PATH "$ENV{CCP4}")
|
||||
list(PREPEND CMAKE_PREFIX_PATH "$ENV{CCP4}")
|
||||
set(CMAKE_INSTALL_PREFIX "$ENV{CCP4}")
|
||||
if("$ENV{CCP4}" STREQUAL "" OR NOT EXISTS $ENV{CCP4})
|
||||
message(FATAL_ERROR "cifpp: A CCP4 built was requested but CCP4 was not sourced")
|
||||
else()
|
||||
list(PREPEND CMAKE_MODULE_PATH "$ENV{CCP4}")
|
||||
list(PREPEND CMAKE_PREFIX_PATH "$ENV{CCP4}")
|
||||
set(CMAKE_INSTALL_PREFIX "$ENV{CCP4}")
|
||||
|
||||
if(WIN32)
|
||||
set(BUILD_SHARED_LIBS ON)
|
||||
endif()
|
||||
endif()
|
||||
if(WIN32)
|
||||
set(BUILD_SHARED_LIBS ON)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Now include the GNUInstallDirs module
|
||||
include(GNUInstallDirs)
|
||||
|
||||
if(WIN32)
|
||||
if(${CMAKE_SYSTEM_VERSION} GREATER_EQUAL 10) # Windows 10
|
||||
add_definitions(-D _WIN32_WINNT=0x0A00)
|
||||
elseif(${CMAKE_SYSTEM_VERSION} EQUAL 6.3) # Windows 8.1
|
||||
add_definitions(-D _WIN32_WINNT=0x0603)
|
||||
elseif(${CMAKE_SYSTEM_VERSION} EQUAL 6.2) # Windows 8
|
||||
add_definitions(-D _WIN32_WINNT=0x0602)
|
||||
elseif(${CMAKE_SYSTEM_VERSION} EQUAL 6.1) # Windows 7
|
||||
add_definitions(-D _WIN32_WINNT=0x0601)
|
||||
elseif(${CMAKE_SYSTEM_VERSION} EQUAL 6.0) # Windows Vista
|
||||
add_definitions(-D _WIN32_WINNT=0x0600)
|
||||
else() # Windows XP (5.1)
|
||||
add_definitions(-D _WIN32_WINNT=0x0501)
|
||||
endif()
|
||||
if(${CMAKE_SYSTEM_VERSION} GREATER_EQUAL 10) # Windows 10
|
||||
add_definitions(-D _WIN32_WINNT=0x0A00)
|
||||
elseif(${CMAKE_SYSTEM_VERSION} EQUAL 6.3) # Windows 8.1
|
||||
add_definitions(-D _WIN32_WINNT=0x0603)
|
||||
elseif(${CMAKE_SYSTEM_VERSION} EQUAL 6.2) # Windows 8
|
||||
add_definitions(-D _WIN32_WINNT=0x0602)
|
||||
elseif(${CMAKE_SYSTEM_VERSION} EQUAL 6.1) # Windows 7
|
||||
add_definitions(-D _WIN32_WINNT=0x0601)
|
||||
elseif(${CMAKE_SYSTEM_VERSION} EQUAL 6.0) # Windows Vista
|
||||
add_definitions(-D _WIN32_WINNT=0x0600)
|
||||
else() # Windows XP (5.1)
|
||||
add_definitions(-D _WIN32_WINNT=0x0501)
|
||||
endif()
|
||||
|
||||
# We do not want to write an export file for all our symbols...
|
||||
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
# We do not want to write an export file for all our symbols...
|
||||
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
# make msvc standards compliant...
|
||||
add_compile_options(/permissive- /bigobj)
|
||||
add_link_options(/NODEFAULTLIB:library)
|
||||
# make msvc standards compliant...
|
||||
add_compile_options(/permissive- /bigobj)
|
||||
add_link_options(/NODEFAULTLIB:library)
|
||||
|
||||
# This is dubious...
|
||||
if(BUILD_SHARED_LIBS)
|
||||
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
|
||||
else()
|
||||
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
|
||||
endif()
|
||||
# This is dubious...
|
||||
if(BUILD_SHARED_LIBS)
|
||||
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
|
||||
else()
|
||||
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Libraries
|
||||
|
||||
if(MSVC)
|
||||
# Avoid linking the shared library of zlib. Search ZLIB_ROOT first if it is
|
||||
# set.
|
||||
if(ZLIB_ROOT)
|
||||
set(_ZLIB_SEARCH_ROOT PATHS ${ZLIB_ROOT} NO_DEFAULT_PATH)
|
||||
list(APPEND _ZLIB_SEARCHES _ZLIB_SEARCH_ROOT)
|
||||
endif()
|
||||
# Avoid linking the shared library of zlib. Search ZLIB_ROOT first if it is
|
||||
# set.
|
||||
if(ZLIB_ROOT)
|
||||
set(_ZLIB_SEARCH_ROOT PATHS ${ZLIB_ROOT} NO_DEFAULT_PATH)
|
||||
list(APPEND _ZLIB_SEARCHES _ZLIB_SEARCH_ROOT)
|
||||
endif()
|
||||
|
||||
# Normal search.
|
||||
set(_ZLIB_x86 "(x86)")
|
||||
set(_ZLIB_SEARCH_NORMAL
|
||||
PATHS "[HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\Zlib;InstallPath]"
|
||||
"$ENV{ProgramFiles}/zlib" "$ENV{ProgramFiles${_ZLIB_x86}}/zlib")
|
||||
unset(_ZLIB_x86)
|
||||
list(APPEND _ZLIB_SEARCHES _ZLIB_SEARCH_NORMAL)
|
||||
# Normal search.
|
||||
set(_ZLIB_x86 "(x86)")
|
||||
set(_ZLIB_SEARCH_NORMAL
|
||||
PATHS "[HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\Zlib;InstallPath]"
|
||||
"$ENV{ProgramFiles}/zlib" "$ENV{ProgramFiles${_ZLIB_x86}}/zlib")
|
||||
unset(_ZLIB_x86)
|
||||
list(APPEND _ZLIB_SEARCHES _ZLIB_SEARCH_NORMAL)
|
||||
|
||||
if(BUILD_FOR_CCP4)
|
||||
list(PREPEND _ZLIB_SEARCHES "$ENV{CCP4}/lib")
|
||||
endif()
|
||||
if(BUILD_FOR_CCP4)
|
||||
list(PREPEND _ZLIB_SEARCHES "$ENV{CCP4}/lib")
|
||||
endif()
|
||||
|
||||
foreach(search ${_ZLIB_SEARCHES})
|
||||
find_library(
|
||||
ZLIB_LIBRARY
|
||||
NAMES zlibstatic NAMES_PER_DIR ${${search}}
|
||||
PATH_SUFFIXES lib)
|
||||
endforeach()
|
||||
foreach(search ${_ZLIB_SEARCHES})
|
||||
find_library(
|
||||
ZLIB_LIBRARY
|
||||
NAMES zlibstatic NAMES_PER_DIR ${${search}}
|
||||
PATH_SUFFIXES lib)
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
# First check if <format> is available
|
||||
find_file(FMT NAME format)
|
||||
# Using fast_float for float parsing, but only if needed
|
||||
try_compile(STD_CHARCONV_COMPILING
|
||||
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-charconv.cpp
|
||||
CXX_STANDARD 20
|
||||
CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
if(FMT EQUAL "FMT-NOTFOUND")
|
||||
if(NOT (fmt_FOUND OR TARGET fmt))
|
||||
find_package(fmt REQUIRED)
|
||||
message(FATAL_ERROR "cifpp: <format> not found and neither was libfmt, compiler too old, you're out of luck")
|
||||
if(NOT STD_CHARCONV_COMPILING)
|
||||
message(NOTICE "libcifpp: Using fast_float for std::from_chars")
|
||||
find_package(FastFloat 8.0 QUIET CONFIG)
|
||||
if(NOT FastFloat_FOUND)
|
||||
message(STATUS "FastFloat not found in system, fetching from GitHub")
|
||||
FetchContent_Declare(fastfloat
|
||||
GIT_REPOSITORY "https://github.com/fastfloat/fast_float"
|
||||
GIT_TAG v8.0.2
|
||||
EXCLUDE_FROM_ALL)
|
||||
FetchContent_MakeAvailable(fastfloat)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@@ -182,17 +197,17 @@ find_package(Threads)
|
||||
find_package(ZLIB QUIET)
|
||||
|
||||
if(NOT ZLIB_FOUND)
|
||||
message(FATAL_ERROR "cifpp: The zlib development files were not found you this system, please install them and try again (hint: on debian/ubuntu use apt-get install zlib1g-dev)")
|
||||
message(FATAL_ERROR "cifpp: The zlib development files were not found you this system, please install them and try again (hint: on debian/ubuntu use apt-get install zlib1g-dev)")
|
||||
endif()
|
||||
|
||||
include(FindPkgConfig)
|
||||
|
||||
if(PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(PCRE2 IMPORTED_TARGET libpcre2-8)
|
||||
pkg_check_modules(PCRE2 IMPORTED_TARGET libpcre2-8)
|
||||
endif()
|
||||
|
||||
if(NOT PCRE2_FOUND)
|
||||
add_subdirectory(pcre2-simple)
|
||||
add_subdirectory(pcre2-simple)
|
||||
endif()
|
||||
|
||||
# Using Eigen3 is a bit of a thing. We don't want to build it completely since
|
||||
@@ -201,357 +216,362 @@ endif()
|
||||
find_package(Eigen3 3.4 QUIET)
|
||||
|
||||
if(Eigen3_FOUND AND TARGET Eigen3::Eigen)
|
||||
get_target_property(EIGEN_INCLUDE_DIR Eigen3::Eigen
|
||||
INTERFACE_INCLUDE_DIRECTORIES)
|
||||
get_target_property(EIGEN_INCLUDE_DIR Eigen3::Eigen
|
||||
INTERFACE_INCLUDE_DIRECTORIES)
|
||||
else()
|
||||
# Use ExternalProject since FetchContent always tries to install the result...
|
||||
ExternalProject_Add(my-eigen3
|
||||
URL https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.zip
|
||||
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND "")
|
||||
# Use ExternalProject since FetchContent always tries to install the result...
|
||||
ExternalProject_Add(my-eigen3
|
||||
URL https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.zip
|
||||
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND "")
|
||||
|
||||
ExternalProject_Get_Property(my-eigen3 SOURCE_DIR)
|
||||
set(EIGEN_INCLUDE_DIR ${SOURCE_DIR})
|
||||
ExternalProject_Get_Property(my-eigen3 SOURCE_DIR)
|
||||
set(EIGEN_INCLUDE_DIR ${SOURCE_DIR})
|
||||
endif()
|
||||
|
||||
# Create a revision file, containing the current git version info
|
||||
include(VersionString)
|
||||
write_version_header(${CMAKE_CURRENT_SOURCE_DIR}/src/ LIB_NAME "LibCIFPP")
|
||||
|
||||
# SymOp data table
|
||||
if(CIFPP_RECREATE_SYMOP_DATA)
|
||||
# The tool to create the table
|
||||
add_executable(symop-map-generator
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/symop-map-generator.cpp")
|
||||
# The tool to create the table
|
||||
add_executable(symop-map-generator
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/symop-map-generator.cpp")
|
||||
|
||||
target_compile_features(symop-map-generator PUBLIC cxx_std_20)
|
||||
target_compile_features(symop-map-generator PUBLIC cxx_std_20)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/symop_table_data.hpp
|
||||
COMMAND
|
||||
$<TARGET_FILE:symop-map-generator> $ENV{CLIBD}/syminfo.lib
|
||||
$ENV{CLIBD}/symop.lib ${CMAKE_CURRENT_SOURCE_DIR}/src/symop_table_data.hpp)
|
||||
add_custom_command(
|
||||
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/symop_table_data.hpp
|
||||
COMMAND
|
||||
$<TARGET_FILE:symop-map-generator> $ENV{CLIBD}/syminfo.lib
|
||||
$ENV{CLIBD}/symop.lib ${CMAKE_CURRENT_SOURCE_DIR}/src/symop_table_data.hpp)
|
||||
|
||||
add_custom_target(
|
||||
OUTPUT
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/symop_table_data.hpp
|
||||
DEPENDS symop-map-generator "$ENV{CLIBD}/syminfo.lib"
|
||||
"$ENV{CLIBD}/symop.lib")
|
||||
add_custom_target(
|
||||
OUTPUT
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/symop_table_data.hpp
|
||||
DEPENDS symop-map-generator "$ENV{CLIBD}/syminfo.lib"
|
||||
"$ENV{CLIBD}/symop.lib")
|
||||
endif()
|
||||
|
||||
# Create a revision file, containing the current git version info
|
||||
write_version_header("${CMAKE_CURRENT_SOURCE_DIR}/src/" LIB_NAME "LibCIFPP")
|
||||
|
||||
# Sources
|
||||
set(project_sources
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/category.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/condition.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/datablock.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/dictionary_parser.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/file.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/item.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/parser.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/row.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/validate.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/text.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/utilities.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/atom_type.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/compound.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/point.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/symmetry.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/model.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/cif2pdb.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/pdb2cif.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/pdb_record.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/pdb2cif_remark_3.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/pdb2cif_remark_3.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/reconstruct.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/validate-pdbx.cpp
|
||||
list(APPEND project_sources
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/category.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/condition.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/datablock.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/dictionary_parser.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/file.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/item.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/parser.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/row.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/validate.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/text.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/utilities.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/atom_type.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/compound.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/point.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/symmetry.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/model.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/cql.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/cif2pdb.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/pdb2cif.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/pdb_record.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/pdb2cif_remark_3.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/pdb2cif_remark_3.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/reconstruct.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/pdb/validate-pdbx.cpp
|
||||
)
|
||||
|
||||
set(project_headers
|
||||
include/cif++.hpp
|
||||
include/cif++/atom_type.hpp
|
||||
include/cif++/category.hpp
|
||||
include/cif++/compound.hpp
|
||||
include/cif++/condition.hpp
|
||||
include/cif++/datablock.hpp
|
||||
include/cif++/dictionary_parser.hpp
|
||||
include/cif++/exports.hpp
|
||||
include/cif++/file.hpp
|
||||
include/cif++/format.hpp
|
||||
include/cif++/forward_decl.hpp
|
||||
include/cif++/gzio.hpp
|
||||
include/cif++/item.hpp
|
||||
include/cif++/iterator.hpp
|
||||
include/cif++/matrix.hpp
|
||||
include/cif++/model.hpp
|
||||
include/cif++/parser.hpp
|
||||
include/cif++/pdb/cif2pdb.hpp
|
||||
include/cif++/pdb.hpp
|
||||
include/cif++/pdb/io.hpp
|
||||
include/cif++/pdb/pdb2cif.hpp
|
||||
include/cif++/pdb/tls.hpp
|
||||
include/cif++/point.hpp
|
||||
include/cif++/row.hpp
|
||||
include/cif++/symmetry.hpp
|
||||
include/cif++/text.hpp
|
||||
include/cif++/utilities.hpp
|
||||
include/cif++/validate.hpp
|
||||
if(BUILD_SQLITE_INTERFACE)
|
||||
list(APPEND project_sources
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/sqlite3/sqlite3.c)
|
||||
endif()
|
||||
|
||||
list(APPEND project_headers
|
||||
include/cif++.hpp
|
||||
include/cif++/atom_type.hpp
|
||||
include/cif++/category.hpp
|
||||
include/cif++/compound.hpp
|
||||
include/cif++/condition.hpp
|
||||
include/cif++/datablock.hpp
|
||||
include/cif++/dictionary_parser.hpp
|
||||
include/cif++/exports.hpp
|
||||
include/cif++/file.hpp
|
||||
include/cif++/format.hpp
|
||||
include/cif++/forward_decl.hpp
|
||||
include/cif++/gzio.hpp
|
||||
include/cif++/item.hpp
|
||||
include/cif++/iterator.hpp
|
||||
include/cif++/matrix.hpp
|
||||
include/cif++/model.hpp
|
||||
include/cif++/parser.hpp
|
||||
include/cif++/pdb/cif2pdb.hpp
|
||||
include/cif++/pdb.hpp
|
||||
include/cif++/pdb/io.hpp
|
||||
include/cif++/pdb/pdb2cif.hpp
|
||||
include/cif++/pdb/tls.hpp
|
||||
include/cif++/point.hpp
|
||||
include/cif++/row.hpp
|
||||
include/cif++/symmetry.hpp
|
||||
include/cif++/text.hpp
|
||||
include/cif++/cql.hpp
|
||||
include/cif++/utilities.hpp
|
||||
include/cif++/validate.hpp
|
||||
)
|
||||
|
||||
add_library(cifpp)
|
||||
add_library(cifpp::cifpp ALIAS cifpp)
|
||||
|
||||
if(TARGET my-eigen3)
|
||||
add_dependencies(cifpp my-eigen3)
|
||||
add_dependencies(cifpp my-eigen3)
|
||||
endif()
|
||||
|
||||
target_sources(cifpp
|
||||
PRIVATE ${project_sources}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/symop_table_data.hpp
|
||||
PUBLIC
|
||||
FILE_SET cifpp_headers TYPE HEADERS
|
||||
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
FILES ${project_headers}
|
||||
PRIVATE ${project_sources}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/symop_table_data.hpp
|
||||
PUBLIC
|
||||
FILE_SET cifpp_headers TYPE HEADERS
|
||||
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
FILES ${project_headers}
|
||||
)
|
||||
|
||||
# The code now really requires C++20
|
||||
target_compile_features(cifpp PUBLIC cxx_std_20)
|
||||
|
||||
generate_export_header(cifpp EXPORT_FILE_NAME
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/cif++/exports.hpp)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/cif++/exports.hpp)
|
||||
|
||||
if(MSVC)
|
||||
target_compile_definitions(cifpp PUBLIC NOMINMAX=1)
|
||||
target_compile_definitions(cifpp PUBLIC NOMINMAX=1)
|
||||
endif()
|
||||
|
||||
set_target_properties(cifpp PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
target_include_directories(
|
||||
cifpp
|
||||
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
PRIVATE "${EIGEN_INCLUDE_DIR}")
|
||||
cifpp
|
||||
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
PRIVATE "${EIGEN_INCLUDE_DIR}")
|
||||
|
||||
target_link_libraries(cifpp
|
||||
PUBLIC Threads::Threads ZLIB::ZLIB $<$<TARGET_EXISTS:std::atomic>:std::atomic>)
|
||||
PUBLIC Threads::Threads ZLIB::ZLIB $<$<TARGET_EXISTS:std::atomic>:std::atomic>)
|
||||
|
||||
if(PCRE2_FOUND)
|
||||
target_include_directories(cifpp PRIVATE ${PCRE2_INCLUDE_DIRS})
|
||||
target_link_libraries(cifpp PRIVATE ${PCRE2_LINK_LIBRARIES})
|
||||
target_include_directories(cifpp PRIVATE ${PCRE2_INCLUDE_DIRS})
|
||||
target_link_libraries(cifpp PRIVATE ${PCRE2_LINK_LIBRARIES})
|
||||
else()
|
||||
target_link_libraries(cifpp PRIVATE $<BUILD_INTERFACE:pcre2s>)
|
||||
target_link_libraries(cifpp PRIVATE $<BUILD_INTERFACE:pcre2s>)
|
||||
endif()
|
||||
|
||||
if(fmt_FOUND)
|
||||
target_link_libraries(cifpp PUBLIC fmt)
|
||||
if(NOT STD_CHARCONV_COMPILING)
|
||||
target_link_libraries(cifpp PRIVATE FastFloat::fast_float)
|
||||
endif()
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
|
||||
target_link_options(cifpp PRIVATE -undefined dynamic_lookup)
|
||||
target_link_options(cifpp PRIVATE -undefined dynamic_lookup)
|
||||
endif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
|
||||
|
||||
if(CIFPP_DOWNLOAD_CCD)
|
||||
# download the components.cif file from CCD
|
||||
set(COMPONENTS_CIF ${CMAKE_CURRENT_SOURCE_DIR}/rsrc/components.cif)
|
||||
# download the components.cif file from CCD
|
||||
set(COMPONENTS_CIF ${CMAKE_CURRENT_SOURCE_DIR}/rsrc/components.cif)
|
||||
|
||||
if(EXISTS ${COMPONENTS_CIF})
|
||||
file(SIZE ${COMPONENTS_CIF} CCD_FILE_SIZE)
|
||||
if(EXISTS ${COMPONENTS_CIF})
|
||||
file(SIZE ${COMPONENTS_CIF} CCD_FILE_SIZE)
|
||||
|
||||
if(CCD_FILE_SIZE EQUAL 0)
|
||||
message(STATUS "cifpp: Removing empty ${COMPONENTS_CIF} file")
|
||||
file(REMOVE "${COMPONENTS_CIF}")
|
||||
endif()
|
||||
endif()
|
||||
if(CCD_FILE_SIZE EQUAL 0)
|
||||
message(STATUS "cifpp: Removing empty ${COMPONENTS_CIF} file")
|
||||
file(REMOVE "${COMPONENTS_CIF}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(NOT EXISTS ${COMPONENTS_CIF})
|
||||
# Since the file(DOWNLOAD) command in cmake does not use compression, we try
|
||||
# to download the gzipped version and decompress it ourselves.
|
||||
find_program(GUNZIP gunzip)
|
||||
if(NOT EXISTS ${COMPONENTS_CIF})
|
||||
# Since the file(DOWNLOAD) command in cmake does not use compression, we try
|
||||
# to download the gzipped version and decompress it ourselves.
|
||||
find_program(GUNZIP gunzip)
|
||||
|
||||
if(WIN32 OR GUNZIP STREQUAL "GUNZIP-NOTFOUND")
|
||||
file(
|
||||
DOWNLOAD https://files.wwpdb.org/pub/pdb/data/monomers/components.cif
|
||||
${COMPONENTS_CIF}
|
||||
SHOW_PROGRESS
|
||||
STATUS CCD_FETCH_STATUS)
|
||||
else()
|
||||
if(NOT EXISTS "${COMPONENTS_CIF}.gz")
|
||||
file(
|
||||
DOWNLOAD
|
||||
https://files.wwpdb.org/pub/pdb/data/monomers/components.cif.gz
|
||||
${COMPONENTS_CIF}.gz
|
||||
SHOW_PROGRESS
|
||||
STATUS CCD_FETCH_STATUS)
|
||||
endif()
|
||||
if(WIN32 OR GUNZIP STREQUAL "GUNZIP-NOTFOUND")
|
||||
file(
|
||||
DOWNLOAD https://files.wwpdb.org/pub/pdb/data/monomers/components.cif
|
||||
${COMPONENTS_CIF}
|
||||
SHOW_PROGRESS
|
||||
STATUS CCD_FETCH_STATUS)
|
||||
else()
|
||||
if(NOT EXISTS "${COMPONENTS_CIF}.gz")
|
||||
file(
|
||||
DOWNLOAD
|
||||
https://files.wwpdb.org/pub/pdb/data/monomers/components.cif.gz
|
||||
${COMPONENTS_CIF}.gz
|
||||
SHOW_PROGRESS
|
||||
STATUS CCD_FETCH_STATUS)
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${COMPONENTS_CIF}
|
||||
COMMAND "${GUNZIP}" ${COMPONENTS_CIF}.gz
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rsrc/)
|
||||
add_custom_command(
|
||||
OUTPUT ${COMPONENTS_CIF}
|
||||
COMMAND "${GUNZIP}" ${COMPONENTS_CIF}.gz
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rsrc/)
|
||||
|
||||
add_custom_target(COMPONENTS ALL DEPENDS ${COMPONENTS_CIF})
|
||||
endif()
|
||||
add_custom_target(COMPONENTS ALL DEPENDS ${COMPONENTS_CIF})
|
||||
endif()
|
||||
|
||||
# Do not continue if downloading went wrong
|
||||
list(POP_FRONT CCD_FETCH_STATUS CCD_FETCH_STATUS_CODE)
|
||||
# Do not continue if downloading went wrong
|
||||
list(POP_FRONT CCD_FETCH_STATUS CCD_FETCH_STATUS_CODE)
|
||||
|
||||
if(CCD_FETCH_STATUS_CODE)
|
||||
message(
|
||||
FATAL_ERROR "cifpp: Error trying to download CCD file: ${CCD_FETCH_STATUS}")
|
||||
endif()
|
||||
endif()
|
||||
if(CCD_FETCH_STATUS_CODE)
|
||||
message(
|
||||
FATAL_ERROR "cifpp: Error trying to download CCD file: ${CCD_FETCH_STATUS}")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Installation directories
|
||||
if(BUILD_FOR_CCP4)
|
||||
set(CIFPP_DATA_DIR
|
||||
"$ENV{CCP4}/share/libcifpp"
|
||||
CACHE PATH "Directory where dictionary and other static data is stored")
|
||||
set(CIFPP_DATA_DIR
|
||||
"$ENV{CCP4}/share/libcifpp"
|
||||
CACHE PATH "Directory where dictionary and other static data is stored")
|
||||
else()
|
||||
set(CIFPP_DATA_DIR
|
||||
"${CMAKE_INSTALL_FULL_DATADIR}/libcifpp"
|
||||
CACHE PATH "Directory where dictionary and other static data is stored")
|
||||
set(CIFPP_DATA_DIR
|
||||
"${CMAKE_INSTALL_FULL_DATADIR}/libcifpp"
|
||||
CACHE PATH "Directory where dictionary and other static data is stored")
|
||||
endif()
|
||||
|
||||
if(CIFPP_DATA_DIR)
|
||||
target_compile_definitions(cifpp PUBLIC DATA_DIR="${CIFPP_DATA_DIR}")
|
||||
set_target_properties(cifpp PROPERTIES CIFPP_DATA_DIR ${CIFPP_DATA_DIR})
|
||||
target_compile_definitions(cifpp PUBLIC DATA_DIR="${CIFPP_DATA_DIR}")
|
||||
set_target_properties(cifpp PROPERTIES CIFPP_DATA_DIR ${CIFPP_DATA_DIR})
|
||||
endif()
|
||||
|
||||
if(NOT PROJECT_IS_TOP_LEVEL)
|
||||
set(CIFPP_SHARE_DIR ${CIFPP_DATA_DIR} PARENT_SCOPE)
|
||||
set(CIFPP_SHARE_DIR ${CIFPP_DATA_DIR} PARENT_SCOPE)
|
||||
endif()
|
||||
|
||||
if(UNIX AND NOT BUILD_FOR_CCP4)
|
||||
if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr/local")
|
||||
set(CIFPP_CACHE_DIR
|
||||
"/var/cache/libcifpp"
|
||||
CACHE PATH "The directory where downloaded data files are stored")
|
||||
else()
|
||||
set(CIFPP_CACHE_DIR
|
||||
"${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/cache/libcifpp"
|
||||
CACHE PATH "The directory where downloaded data files are stored")
|
||||
endif()
|
||||
if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr/local")
|
||||
set(CIFPP_CACHE_DIR
|
||||
"/var/cache/libcifpp"
|
||||
CACHE PATH "The directory where downloaded data files are stored")
|
||||
else()
|
||||
set(CIFPP_CACHE_DIR
|
||||
"${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/cache/libcifpp"
|
||||
CACHE PATH "The directory where downloaded data files are stored")
|
||||
endif()
|
||||
|
||||
target_compile_definitions(cifpp PUBLIC CACHE_DIR="${CIFPP_CACHE_DIR}")
|
||||
target_compile_definitions(cifpp PUBLIC CACHE_DIR="${CIFPP_CACHE_DIR}")
|
||||
|
||||
set(CIFPP_ETC_DIR
|
||||
"${CMAKE_INSTALL_FULL_SYSCONFDIR}"
|
||||
CACHE PATH "The directory where the update configuration file is stored")
|
||||
set(CIFPP_ETC_DIR
|
||||
"${CMAKE_INSTALL_FULL_SYSCONFDIR}"
|
||||
CACHE PATH "The directory where the update configuration file is stored")
|
||||
else()
|
||||
unset(CIFPP_CACHE_DIR)
|
||||
unset(CIFPP_CACHE_DIR)
|
||||
endif()
|
||||
|
||||
# Install rules
|
||||
install(TARGETS cifpp
|
||||
EXPORT cifpp
|
||||
FILE_SET cifpp_headers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
EXPORT cifpp
|
||||
FILE_SET cifpp_headers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
|
||||
if(MSVC AND BUILD_SHARED_LIBS)
|
||||
install(
|
||||
FILES $<TARGET_PDB_FILE:cifpp>
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
OPTIONAL)
|
||||
install(
|
||||
FILES $<TARGET_PDB_FILE:cifpp>
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
OPTIONAL)
|
||||
endif()
|
||||
|
||||
# Clean up old config files (with old names)
|
||||
file(GLOB OLD_CONFIG_FILES
|
||||
${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cifpp/cifppConfig*.cmake
|
||||
${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cifpp/cifppTargets*.cmake)
|
||||
${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cifpp/cifppConfig*.cmake
|
||||
${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cifpp/cifppTargets*.cmake)
|
||||
|
||||
if(OLD_CONFIG_FILES)
|
||||
message(
|
||||
STATUS "cifpp: Installation will remove old config files: ${OLD_CONFIG_FILES}")
|
||||
install(CODE "file(REMOVE ${OLD_CONFIG_FILES})")
|
||||
message(
|
||||
STATUS "cifpp: Installation will remove old config files: ${OLD_CONFIG_FILES}")
|
||||
install(CODE "file(REMOVE ${OLD_CONFIG_FILES})")
|
||||
endif()
|
||||
|
||||
install(EXPORT cifpp
|
||||
NAMESPACE cifpp::
|
||||
FILE "cifpp-targets.cmake"
|
||||
DESTINATION lib/cmake/cifpp)
|
||||
NAMESPACE cifpp::
|
||||
FILE "cifpp-targets.cmake"
|
||||
DESTINATION lib/cmake/cifpp)
|
||||
|
||||
install(
|
||||
FILES ${CMAKE_CURRENT_SOURCE_DIR}/rsrc/mmcif_ddl.dic
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rsrc/mmcif_pdbx.dic
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rsrc/mmcif_ma.dic
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/libcifpp)
|
||||
FILES ${CMAKE_CURRENT_SOURCE_DIR}/rsrc/mmcif_ddl.dic
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rsrc/mmcif_pdbx.dic
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rsrc/mmcif_ma.dic
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/libcifpp)
|
||||
|
||||
if(CIFPP_DATA_DIR AND CIFPP_DOWNLOAD_CCD)
|
||||
install(FILES ${COMPONENTS_CIF}
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/libcifpp)
|
||||
install(FILES ${COMPONENTS_CIF}
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/libcifpp)
|
||||
endif()
|
||||
|
||||
set(CONFIG_TEMPLATE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cifpp-config.cmake.in)
|
||||
set(REQUIRE_FMT ${fmt_FOUND})
|
||||
|
||||
configure_package_config_file(
|
||||
${CONFIG_TEMPLATE_FILE} ${CMAKE_CURRENT_BINARY_DIR}/cifpp/cifpp-config.cmake
|
||||
INSTALL_DESTINATION lib/cmake/cifpp
|
||||
PATH_VARS CIFPP_DATA_DIR)
|
||||
${CONFIG_TEMPLATE_FILE} ${CMAKE_CURRENT_BINARY_DIR}/cifpp/cifpp-config.cmake
|
||||
INSTALL_DESTINATION lib/cmake/cifpp
|
||||
PATH_VARS CIFPP_DATA_DIR)
|
||||
|
||||
install(
|
||||
FILES "${CMAKE_CURRENT_BINARY_DIR}/cifpp/cifpp-config.cmake"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/cifpp/cifpp-config-version.cmake"
|
||||
DESTINATION lib/cmake/cifpp)
|
||||
FILES "${CMAKE_CURRENT_BINARY_DIR}/cifpp/cifpp-config.cmake"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/cifpp/cifpp-config-version.cmake"
|
||||
DESTINATION lib/cmake/cifpp)
|
||||
|
||||
set_target_properties(
|
||||
cifpp
|
||||
PROPERTIES VERSION ${PROJECT_VERSION}
|
||||
SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
|
||||
INTERFACE_cifpp_MAJOR_VERSION ${PROJECT_VERSION_MAJOR})
|
||||
cifpp
|
||||
PROPERTIES VERSION ${PROJECT_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)
|
||||
TARGET cifpp
|
||||
APPEND
|
||||
PROPERTY COMPATIBLE_INTERFACE_STRING cifpp_MAJOR_VERSION)
|
||||
|
||||
write_basic_package_version_file(
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/cifpp/cifpp-config-version.cmake"
|
||||
VERSION ${PROJECT_VERSION}
|
||||
COMPATIBILITY AnyNewerVersion)
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/cifpp/cifpp-config-version.cmake"
|
||||
VERSION ${PROJECT_VERSION}
|
||||
COMPATIBILITY AnyNewerVersion)
|
||||
|
||||
if(BUILD_TESTING AND PROJECT_IS_TOP_LEVEL)
|
||||
add_subdirectory(test)
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
|
||||
# Optionally install the update scripts for CCD and dictionary files
|
||||
if(CIFPP_INSTALL_UPDATE_SCRIPT)
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tools/update-libcifpp-data.in
|
||||
update-libcifpp-data @ONLY)
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tools/update-libcifpp-data.in
|
||||
update-libcifpp-data @ONLY)
|
||||
|
||||
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR
|
||||
${CMAKE_SYSTEM_NAME} STREQUAL "GNU" OR
|
||||
${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
|
||||
install(
|
||||
FILES ${CMAKE_CURRENT_BINARY_DIR}/update-libcifpp-data
|
||||
DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/cron.weekly
|
||||
PERMISSIONS OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE
|
||||
WORLD_READ)
|
||||
else()
|
||||
message(FATAL_ERROR "cifpp: Don't know where to install the update script")
|
||||
endif()
|
||||
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR
|
||||
${CMAKE_SYSTEM_NAME} STREQUAL "GNU" OR
|
||||
${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
|
||||
install(
|
||||
FILES ${CMAKE_CURRENT_BINARY_DIR}/update-libcifpp-data
|
||||
DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/cron.weekly
|
||||
PERMISSIONS OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE
|
||||
WORLD_READ)
|
||||
else()
|
||||
message(FATAL_ERROR "cifpp: Don't know where to install the update script")
|
||||
endif()
|
||||
|
||||
# a config file, to make it complete
|
||||
# install(DIRECTORY DESTINATION "${CMAKE_INSTALL_LOCALSTATEDIR}/libcifpp")
|
||||
if(NOT EXISTS "${CMAKE_INSTALL_SYSCONFDIR}/libcifpp.conf")
|
||||
file(
|
||||
WRITE ${CMAKE_CURRENT_BINARY_DIR}/libcifpp.conf
|
||||
[[# Uncomment the next line to enable automatic updates
|
||||
# a config file, to make it complete
|
||||
# install(DIRECTORY DESTINATION "${CMAKE_INSTALL_LOCALSTATEDIR}/libcifpp")
|
||||
if(NOT EXISTS "${CMAKE_INSTALL_SYSCONFDIR}/libcifpp.conf")
|
||||
file(
|
||||
WRITE ${CMAKE_CURRENT_BINARY_DIR}/libcifpp.conf
|
||||
[[# Uncomment the next line to enable automatic updates
|
||||
# update=true
|
||||
]])
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libcifpp.conf
|
||||
DESTINATION ${CMAKE_INSTALL_SYSCONFDIR})
|
||||
install(
|
||||
CODE "message(\"cifpp: A configuration file has been written to ${CIFPP_ETC_DIR}/libcifpp.conf, please edit this file to enable automatic updates\")"
|
||||
)
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libcifpp.conf
|
||||
DESTINATION ${CMAKE_INSTALL_SYSCONFDIR})
|
||||
install(
|
||||
CODE "message(\"cifpp: A configuration file has been written to ${CIFPP_ETC_DIR}/libcifpp.conf, please edit this file to enable automatic updates\")"
|
||||
)
|
||||
|
||||
install(DIRECTORY DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/libcifpp/cache-update.d)
|
||||
endif()
|
||||
install(DIRECTORY DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/libcifpp/cache-update.d)
|
||||
endif()
|
||||
|
||||
target_compile_definitions(cifpp PUBLIC CACHE_DIR="${CIFPP_CACHE_DIR}")
|
||||
target_compile_definitions(cifpp PUBLIC CACHE_DIR="${CIFPP_CACHE_DIR}")
|
||||
endif()
|
||||
|
||||
if(BUILD_DOCUMENTATION)
|
||||
add_subdirectory(docs)
|
||||
add_subdirectory(docs)
|
||||
endif()
|
||||
|
||||
12
changelog
12
changelog
@@ -1,3 +1,15 @@
|
||||
Version 10.0.0
|
||||
- Added a SQLite interface.
|
||||
|
||||
Version 9.0.5
|
||||
- Added exists to compound_factory
|
||||
- Added sub_matrix, fix and extend determinant calculation
|
||||
- Added yet another structure::create_non_poly
|
||||
- Remove revision.hpp file in make clean (new VersionString.cmake)
|
||||
|
||||
Version 9.0.4
|
||||
- Fix various stopping and reconstruction errors
|
||||
|
||||
Version 9.0.3
|
||||
- Reconstruction fixed when some entity ids are missing
|
||||
|
||||
|
||||
@@ -238,7 +238,7 @@ function(write_version_header dir)
|
||||
if(res EQUAL 0)
|
||||
set(REVISION_STRING "${out}")
|
||||
else()
|
||||
message(STATUS "Git hash not found, does this project has a 'build' tag?")
|
||||
message(STATUS "Git hash not found, does this project have a 'build' tag?")
|
||||
endif()
|
||||
else()
|
||||
message(STATUS "Git hash not found")
|
||||
|
||||
@@ -8,8 +8,5 @@ include(CMakeFindDependencyMacro)
|
||||
|
||||
find_dependency(Threads)
|
||||
find_dependency(ZLIB REQUIRED)
|
||||
if(@REQUIRE_FMT@)
|
||||
find_dependency(fmt REQUIRED)
|
||||
endif()
|
||||
|
||||
check_required_components(cifpp)
|
||||
|
||||
17
cmake/test-charconv.cpp
Normal file
17
cmake/test-charconv.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <charconv>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
int main()
|
||||
{
|
||||
float v;
|
||||
char s[] = "1.0";
|
||||
|
||||
auto r = std::from_chars(s, s + strlen(s), v);
|
||||
|
||||
assert(r.ec == std::errc{});
|
||||
assert(r.ptr = s + strlen(s));
|
||||
assert(v == 1.0f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -26,15 +26,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/forward_decl.hpp"
|
||||
|
||||
#include "cif++/condition.hpp"
|
||||
#include "cif++/forward_decl.hpp"
|
||||
#include "cif++/iterator.hpp"
|
||||
#include "cif++/row.hpp"
|
||||
#include "cif++/text.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
/** \file category.hpp
|
||||
* Documentation for the cif::category class
|
||||
*
|
||||
@@ -181,6 +178,27 @@ class category
|
||||
|
||||
const std::string &name() const { return m_name; } ///< Returns the name of the category
|
||||
|
||||
/// \brief Rename category to @a new_name
|
||||
void name(std::string_view new_name)
|
||||
{
|
||||
m_name = new_name;
|
||||
m_dirty = true;
|
||||
}
|
||||
|
||||
/// \brief Return true if the category has been modified since last open/save
|
||||
constexpr bool is_dirty() const
|
||||
{
|
||||
return m_dirty;
|
||||
}
|
||||
|
||||
/// \brief Mark the category as modified according to @a dirty
|
||||
void set_dirty(bool dirty)
|
||||
{
|
||||
m_dirty = dirty;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
[[deprecated("use key_items instead")]] iset key_fields() const; ///< Returns the cif::iset of key item names. Retrieved from the @ref category_validator for this category
|
||||
|
||||
iset key_items() const; ///< Returns the cif::iset of key item names. Retrieved from the @ref category_validator for this category
|
||||
@@ -1055,60 +1073,6 @@ class category
|
||||
{ return value; });
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Naming used to be very inconsistent. For backward compatibility,
|
||||
// the old function names are here as deprecated variants.
|
||||
|
||||
/// \brief Return the index number for \a column_name
|
||||
[[deprecated("Use get_item_ix instead")]] uint16_t get_column_ix(std::string_view column_name) const
|
||||
{
|
||||
return get_item_ix(column_name);
|
||||
}
|
||||
|
||||
/// @brief Return the name for column with index @a ix
|
||||
/// @param ix The index number
|
||||
/// @return The name of the column
|
||||
[[deprecated("use get_item_name instead")]] std::string_view get_column_name(uint16_t ix) const
|
||||
{
|
||||
return get_item_name(ix);
|
||||
}
|
||||
|
||||
/// @brief Make sure a item with name @a item_name is known and return its index number
|
||||
/// @param item_name The name of the item
|
||||
/// @return The index number of the item
|
||||
[[deprecated("use add_item instead")]] uint16_t add_column(std::string_view item_name)
|
||||
{
|
||||
return add_item(item_name);
|
||||
}
|
||||
|
||||
/** @brief Remove column name @a colum_name
|
||||
* @param column_name The column to be removed
|
||||
*/
|
||||
[[deprecated("use remove_item instead")]] void remove_column(std::string_view column_name)
|
||||
{
|
||||
remove_item(column_name);
|
||||
}
|
||||
|
||||
/** @brief Rename column @a from_name to @a to_name */
|
||||
[[deprecated("use rename_item instead")]] void rename_column(std::string_view from_name, std::string_view to_name)
|
||||
{
|
||||
rename_item(from_name, to_name);
|
||||
}
|
||||
|
||||
/// @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
|
||||
[[deprecated("use has_item instead")]] bool has_column(std::string_view name) const
|
||||
{
|
||||
return has_item(name);
|
||||
}
|
||||
|
||||
/// @brief Return the cif::iset of columns in this category
|
||||
[[deprecated("use get_items instead")]] iset get_columns() const
|
||||
{
|
||||
return get_items();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/// \brief Return the index number for \a item_name
|
||||
|
||||
@@ -1135,6 +1099,9 @@ class category
|
||||
*/
|
||||
void remove_item(std::string_view item_name);
|
||||
|
||||
/// \brief Drop items in this category that contain empty values in all rows.
|
||||
void drop_empty_items();
|
||||
|
||||
/** @brief Rename item @a from_name to @a to_name */
|
||||
void rename_item(std::string_view from_name, std::string_view to_name);
|
||||
|
||||
@@ -1146,8 +1113,14 @@ class category
|
||||
return get_item_ix(name) < m_items.size();
|
||||
}
|
||||
|
||||
/// @brief Return the cif::iset of items in this category
|
||||
iset get_items() const;
|
||||
/// @brief Return the items in this category
|
||||
std::vector<std::string> get_items() const;
|
||||
|
||||
/// @brief Return the number of items (colums) in this category
|
||||
size_t get_item_count() const noexcept
|
||||
{
|
||||
return m_items.size();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
@@ -1177,16 +1150,48 @@ class category
|
||||
/// Write the contents of the category to the std::ostream @a os
|
||||
void write(std::ostream &os) const;
|
||||
|
||||
/// \brief Various supported output formats
|
||||
enum class output_format
|
||||
{
|
||||
cif, // Output in mmCIF format
|
||||
csv, // comma separated values
|
||||
tsv, // tab separated values
|
||||
list, // values delimited by a '|' character
|
||||
column, // output in columns
|
||||
markdown, //
|
||||
table, // ascii art table
|
||||
box, // table with unicode line characters
|
||||
};
|
||||
|
||||
/// @brief
|
||||
/// @brief Write the contents of the category to the std::ostream @a os and
|
||||
/// use @a order as the order of the items. If @a addMissingItems is
|
||||
/// false, items that do not contain any value will be suppressed. Use this version
|
||||
/// to write out
|
||||
/// @param os The std::ostream to write to
|
||||
/// @param fmt The format to use
|
||||
/// @param order The order in which the items should appear
|
||||
/// @param addMissingItems When false, empty items are suppressed from the output
|
||||
void write(std::ostream &os, output_format fmt,
|
||||
const std::vector<std::string> &order, bool addMissingItems = true);
|
||||
|
||||
/// @brief Write the contents of the category to the std::ostream @a os and
|
||||
/// use @a order as the order of the items. If @a addMissingItems is
|
||||
/// false, items that do not contain any value will be suppressed
|
||||
/// @param os The std::ostream to write to
|
||||
/// @param order The order in which the items should appear
|
||||
/// @param addMissingItems When false, empty items are suppressed from the output
|
||||
void write(std::ostream &os, const std::vector<std::string> &order, bool addMissingItems = true);
|
||||
void write(std::ostream &os, const std::vector<std::string> &order, bool addMissingItems = true)
|
||||
{
|
||||
write(os, output_format::cif, order, addMissingItems);
|
||||
}
|
||||
|
||||
private:
|
||||
void write(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems) const;
|
||||
void write_cif(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems) const;
|
||||
void write_delimited(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems,
|
||||
std::string_view delimiter, bool aligned, bool header) const;
|
||||
void write_markdown(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems) const;
|
||||
void write_table(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems, bool ascii) const;
|
||||
|
||||
public:
|
||||
/// friend function to make it possible to do:
|
||||
@@ -1286,6 +1291,8 @@ class category
|
||||
uint32_t m_last_unique_num = 0;
|
||||
class category_index *m_index = nullptr;
|
||||
row *m_head = nullptr, *m_tail = nullptr;
|
||||
|
||||
bool m_dirty = false; // Keep track of modifications
|
||||
};
|
||||
|
||||
} // namespace cif
|
||||
|
||||
@@ -179,7 +179,7 @@ class compound
|
||||
friend class compound_factory_impl;
|
||||
friend class local_compound_factory_impl;
|
||||
|
||||
compound(cif::datablock &db);
|
||||
compound(datablock &db);
|
||||
|
||||
std::string m_id;
|
||||
std::string m_name;
|
||||
@@ -270,11 +270,15 @@ class compound_factory
|
||||
return is_std_base(res_name) or is_std_peptide(res_name);
|
||||
}
|
||||
|
||||
/// Return whether @a res_name is water
|
||||
bool is_water(std::string_view res_name) const
|
||||
{
|
||||
return res_name == "HOH" or res_name == "H2O" or res_name == "WAT";
|
||||
}
|
||||
|
||||
/// Return whether @a res_name already exists, without creating it.
|
||||
bool exists(std::string_view res_name) const;
|
||||
|
||||
/// \brief Create the compound object for \a id
|
||||
///
|
||||
/// This will create the compound instance for \a id if it doesn't exist already.
|
||||
@@ -331,14 +335,14 @@ class compound_factory
|
||||
class compound_source
|
||||
{
|
||||
public:
|
||||
compound_source(const cif::file &file)
|
||||
compound_source(const file &file)
|
||||
{
|
||||
cif::compound_factory::instance().push_dictionary(file);
|
||||
compound_factory::instance().push_dictionary(file);
|
||||
}
|
||||
|
||||
~compound_source()
|
||||
{
|
||||
cif::compound_factory::instance().pop_dictionary();
|
||||
compound_factory::instance().pop_dictionary();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/text.hpp"
|
||||
#include "cif++/row.hpp"
|
||||
#include "cif++/format.hpp"
|
||||
|
||||
@@ -1076,6 +1077,16 @@ condition operator==(const key &key, const T &v)
|
||||
return condition(new detail::key_equals_number_condition_impl(key.m_item_name, static_cast<double>(v)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create an not-equals condition based on a key @a key and a numeric value @a v
|
||||
*/
|
||||
template <Numeric T>
|
||||
condition operator!=(const key &key, const T &v)
|
||||
{
|
||||
// TODO: change key_equals_etc... to use std::variant<double,int64_t> or something
|
||||
return condition(new detail::not_condition_impl(key == v));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Operator to create an equals condition based on a key @a key and a value @a value
|
||||
*/
|
||||
|
||||
444
include/cif++/cql.hpp
Normal file
444
include/cif++/cql.hpp
Normal file
@@ -0,0 +1,444 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2025 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++/category.hpp"
|
||||
#include "cif++/datablock.hpp"
|
||||
#include "cif++/item.hpp"
|
||||
#include "cif++/iterator.hpp"
|
||||
#include "cif++/row.hpp"
|
||||
#include "cif++/validate.hpp"
|
||||
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
namespace cif::cql
|
||||
{
|
||||
|
||||
class result;
|
||||
class row;
|
||||
class transaction;
|
||||
class connection;
|
||||
|
||||
struct result_impl;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class field_ref final
|
||||
{
|
||||
public:
|
||||
std::string_view name() const &
|
||||
{
|
||||
return m_row.get_category().get_item_name(m_index);
|
||||
}
|
||||
|
||||
constexpr size_t num() const noexcept
|
||||
{
|
||||
return m_index;
|
||||
}
|
||||
|
||||
std::string_view text() const &
|
||||
{
|
||||
return m_row[m_index].text();
|
||||
}
|
||||
|
||||
/** Return the contents of this item as type @tparam T */
|
||||
template <typename T = std::string>
|
||||
auto as() const -> T
|
||||
{
|
||||
return m_row[m_index].as<T>();
|
||||
}
|
||||
|
||||
/** 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 m_row[m_index].value_or(dv);
|
||||
}
|
||||
|
||||
field_ref(row_handle rh, int col, std::shared_ptr<result_impl> result_impl)
|
||||
: m_row(rh)
|
||||
, m_index(col)
|
||||
, m_result_impl(result_impl)
|
||||
{
|
||||
}
|
||||
|
||||
field_ref(const field_ref &) = default;
|
||||
field_ref(field_ref &&) = default;
|
||||
|
||||
field_ref &operator=(const field_ref &) = default;
|
||||
field_ref &operator=(field_ref &&) = default;
|
||||
|
||||
private:
|
||||
row_handle m_row;
|
||||
int m_index;
|
||||
|
||||
std::shared_ptr<result_impl> m_result_impl;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class row_ref final
|
||||
{
|
||||
public:
|
||||
class const_field_iterator
|
||||
{
|
||||
public:
|
||||
friend class result;
|
||||
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = const field_ref;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = value_type *;
|
||||
using reference = value_type &;
|
||||
|
||||
const_field_iterator(const const_field_iterator &) = default;
|
||||
const_field_iterator(const_field_iterator &&) = default;
|
||||
|
||||
const_field_iterator &operator=(const const_field_iterator &) = default;
|
||||
const_field_iterator &operator=(const_field_iterator &&) = default;
|
||||
|
||||
reference operator*()
|
||||
{
|
||||
return m_current;
|
||||
}
|
||||
|
||||
pointer operator->()
|
||||
{
|
||||
return &m_current;
|
||||
}
|
||||
|
||||
const_field_iterator &operator++()
|
||||
{
|
||||
if (m_row)
|
||||
{
|
||||
++m_col;
|
||||
m_current = field_ref(m_row, m_col, m_result_impl);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_field_iterator operator++(int)
|
||||
{
|
||||
const_field_iterator result(*this);
|
||||
this->operator++();
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(const const_field_iterator &rhs) const
|
||||
{
|
||||
return m_row == rhs.m_row and m_col == rhs.m_col;
|
||||
}
|
||||
|
||||
bool operator!=(const const_field_iterator &rhs) const
|
||||
{
|
||||
return m_row != rhs.m_row or m_col != rhs.m_col;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class row_ref;
|
||||
|
||||
const_field_iterator(row_handle row, int column, std::shared_ptr<result_impl> result_impl)
|
||||
: m_row(row)
|
||||
, m_col(column)
|
||||
, m_current(m_row, m_col, result_impl)
|
||||
, m_result_impl(result_impl)
|
||||
{
|
||||
}
|
||||
|
||||
row_handle m_row;
|
||||
int m_col;
|
||||
field_ref m_current;
|
||||
|
||||
std::shared_ptr<result_impl> m_result_impl;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
row_ref() = default;
|
||||
|
||||
row_ref(row_handle rh, std::shared_ptr<result_impl> result_impl)
|
||||
: m_row(rh)
|
||||
, m_result_impl(result_impl)
|
||||
{
|
||||
}
|
||||
|
||||
row_ref(const row_ref &) = default;
|
||||
row_ref &operator=(const row_ref &) = default;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
const_field_iterator cbegin() const noexcept { return const_field_iterator(m_row, 0, m_result_impl); }
|
||||
const_field_iterator begin() const noexcept { return const_field_iterator(m_row, 0, m_result_impl); }
|
||||
const_field_iterator cend() const noexcept { return const_field_iterator(m_row, size(), m_result_impl); }
|
||||
const_field_iterator end() const noexcept { return const_field_iterator(m_row, size(), m_result_impl); }
|
||||
|
||||
field_ref front() const noexcept { return field_ref(m_row, 0, m_result_impl); }
|
||||
field_ref back() const noexcept { return field_ref(m_row, size() - 1, m_result_impl); }
|
||||
|
||||
size_t size() const noexcept;
|
||||
bool empty() const noexcept { return size() == 0; }
|
||||
|
||||
field_ref operator[](size_t index) const noexcept { return field_ref(m_row, index, m_result_impl); }
|
||||
field_ref operator[](std::string_view name) const;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
bool operator==(const row_ref &rhs) const { return m_row == rhs.m_row; }
|
||||
bool operator!=(const row_ref &rhs) const { return m_row != rhs.m_row; }
|
||||
|
||||
private:
|
||||
row_handle m_row;
|
||||
std::shared_ptr<result_impl> m_result_impl;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class result
|
||||
{
|
||||
public:
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class iterator
|
||||
{
|
||||
public:
|
||||
friend class view;
|
||||
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = const row_ref;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = value_type *;
|
||||
using reference = value_type &;
|
||||
|
||||
// const_row_iterator() = default;
|
||||
|
||||
iterator(std::shared_ptr<result_impl> result_impl, category::iterator cat_iter)
|
||||
: m_iter(cat_iter)
|
||||
, m_current(*m_iter, result_impl)
|
||||
, m_result_impl(result_impl)
|
||||
{
|
||||
}
|
||||
|
||||
iterator(const iterator &) = default;
|
||||
iterator(iterator &&) = default;
|
||||
|
||||
// const_row_iterator &operator=(const const_row_iterator &) = default;
|
||||
// const_row_iterator &operator=(const_row_iterator &&) = default;
|
||||
|
||||
reference operator*()
|
||||
{
|
||||
return m_current;
|
||||
}
|
||||
|
||||
pointer operator->()
|
||||
{
|
||||
return &m_current;
|
||||
}
|
||||
|
||||
iterator &operator++()
|
||||
{
|
||||
++m_iter;
|
||||
m_current = { *m_iter, m_result_impl };
|
||||
return *this;
|
||||
}
|
||||
|
||||
iterator operator++(int)
|
||||
{
|
||||
iterator result(*this);
|
||||
this->operator++();
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(const iterator &rhs) const
|
||||
{
|
||||
return m_result_impl == rhs.m_result_impl and m_iter == rhs.m_iter;
|
||||
}
|
||||
|
||||
bool operator!=(const iterator &rhs) const
|
||||
{
|
||||
return m_result_impl != rhs.m_result_impl or m_iter != rhs.m_iter;
|
||||
}
|
||||
|
||||
private:
|
||||
category::iterator m_iter;
|
||||
row_ref m_current;
|
||||
std::shared_ptr<result_impl> m_result_impl;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
result() = delete;
|
||||
result(result const &rhs) noexcept = default;
|
||||
result(result &&rhs) noexcept = default;
|
||||
result &operator=(result const &rhs) noexcept = default;
|
||||
result &operator=(result &&rhs) noexcept = default;
|
||||
|
||||
result(category &&data, const std::string &query = "");
|
||||
|
||||
~result() = default;
|
||||
|
||||
row_ref one_row() const
|
||||
{
|
||||
if (size() != 1)
|
||||
throw std::runtime_error("Expected one row");
|
||||
return front();
|
||||
}
|
||||
|
||||
field_ref one_field() const
|
||||
{
|
||||
expect_columns(1);
|
||||
|
||||
if (size() != 1)
|
||||
throw std::runtime_error("Expected one row");
|
||||
|
||||
return one_row().front();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
iterator begin() const noexcept;
|
||||
iterator cbegin() const noexcept;
|
||||
|
||||
iterator end() const noexcept;
|
||||
iterator cend() const noexcept;
|
||||
|
||||
row_ref front() const;
|
||||
row_ref back() const;
|
||||
|
||||
size_t size() const noexcept;
|
||||
bool empty() const noexcept { return size() == 0; }
|
||||
|
||||
size_t column_count() const;
|
||||
|
||||
category &get_category() const;
|
||||
|
||||
result expect_columns(size_t cols) const
|
||||
{
|
||||
if (auto actual = column_count(); cols != actual)
|
||||
throw std::runtime_error("Unexpected number of columns");
|
||||
return *this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const result &r)
|
||||
{
|
||||
os << r.get_category();
|
||||
return os;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class transaction;
|
||||
friend class SelectStatement;
|
||||
|
||||
std::shared_ptr<result_impl> m_impl;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
template <typename... Ts>
|
||||
class cql_iterator_proxy : public cif::iterator_proxy<category, Ts...>
|
||||
{
|
||||
public:
|
||||
cql_iterator_proxy(result &&res)
|
||||
: cif::iterator_proxy<category, Ts...>(res.get_category())
|
||||
, m_result(std::forward<result>(res))
|
||||
{
|
||||
m_result.expect_columns(cif::iterator_proxy<category, Ts...>::N);
|
||||
}
|
||||
|
||||
private:
|
||||
result m_result;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class transaction final
|
||||
{
|
||||
public:
|
||||
transaction(connection &conn);
|
||||
~transaction();
|
||||
|
||||
transaction(const transaction &) = delete;
|
||||
transaction &operator=(const transaction &) = delete;
|
||||
|
||||
/// \brief Execute the sql in @a query returning an iterable result
|
||||
result exec(std::string query);
|
||||
|
||||
/// \brief Execute the sql in @a query returning an iterable result.
|
||||
/// Updates @a tail with what remains after the first statement in @a query
|
||||
result exec(std::string query, std::string &tail);
|
||||
|
||||
template<typename... Ts>
|
||||
cql_iterator_proxy<Ts...> stream(const std::string &sql)
|
||||
{
|
||||
return cql_iterator_proxy<Ts...>{ exec(sql) };
|
||||
}
|
||||
|
||||
void commit();
|
||||
void rollback();
|
||||
|
||||
private:
|
||||
connection &m_conn;
|
||||
bool m_transaction_active = false;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class connection final
|
||||
{
|
||||
public:
|
||||
connection(datablock &db);
|
||||
~connection();
|
||||
|
||||
friend class transaction;
|
||||
|
||||
/// \brief Return true if the string @a sql contains a complete statement.
|
||||
bool is_complete_statement(const std::string &sql) const;
|
||||
|
||||
/// \brief Execute the sql in @a query returning an iterable result
|
||||
result exec(std::string query);
|
||||
|
||||
/// \brief Execute the sql in @a query returning an iterable result.
|
||||
/// Updates @a tail with what remains after the first statement in @a query
|
||||
result exec(std::string query, std::string &tail);
|
||||
|
||||
/// \brief Return true if the underlying data was modified by any query.
|
||||
bool is_modified() const;
|
||||
|
||||
private:
|
||||
struct connection_impl *m_impl;
|
||||
};
|
||||
|
||||
} // namespace cif::cql
|
||||
@@ -43,7 +43,7 @@ namespace cif
|
||||
|
||||
/**
|
||||
* @brief A datablock is a list of category objects with some additional features
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
class datablock : public std::list<category>
|
||||
@@ -53,7 +53,7 @@ class datablock : public std::list<category>
|
||||
|
||||
/**
|
||||
* @brief Construct a new datablock object with name @a name
|
||||
*
|
||||
*
|
||||
* @param name The name for the new datablock
|
||||
*/
|
||||
datablock(std::string_view name)
|
||||
@@ -80,7 +80,7 @@ class datablock : public std::list<category>
|
||||
{
|
||||
std::swap(a.m_name, b.m_name);
|
||||
std::swap(a.m_validator, b.m_validator);
|
||||
std::swap(static_cast<std::list<category>&>(a), static_cast<std::list<category>&>(b));
|
||||
std::swap(static_cast<std::list<category> &>(a), static_cast<std::list<category> &>(b));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -92,7 +92,7 @@ class datablock : public std::list<category>
|
||||
|
||||
/**
|
||||
* @brief Set the name of this datablock to @a name
|
||||
*
|
||||
*
|
||||
* @param name The new name
|
||||
*/
|
||||
void set_name(std::string_view name)
|
||||
@@ -102,27 +102,33 @@ class datablock : public std::list<category>
|
||||
|
||||
/**
|
||||
* @brief Attempt to load the dictionary specified in audit_conform category
|
||||
*
|
||||
*
|
||||
*/
|
||||
void load_dictionary();
|
||||
|
||||
/**
|
||||
* @brief Attempt to load the dictionary @a dict
|
||||
*
|
||||
*/
|
||||
void load_dictionary(std::string_view dict);
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
@@ -131,7 +137,7 @@ class datablock : public std::list<category>
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
@@ -140,7 +146,7 @@ class datablock : public std::list<category>
|
||||
/**
|
||||
* @brief Strip removes all categories and items that are invalid according
|
||||
* to the assigned validator. Will also add a valid audit_conform block.
|
||||
*
|
||||
*
|
||||
* @return true if the remaining datablock is valid
|
||||
*/
|
||||
bool strip();
|
||||
@@ -150,7 +156,7 @@ class datablock : public std::list<category>
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
@@ -159,7 +165,7 @@ class datablock : public std::list<category>
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
@@ -168,7 +174,7 @@ class datablock : public std::list<category>
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
@@ -177,13 +183,12 @@ class datablock : public std::list<category>
|
||||
/**
|
||||
* @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 Return true if this datablock contains a non-empty category
|
||||
*/
|
||||
@@ -197,7 +202,7 @@ class datablock : public std::list<category>
|
||||
* 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
|
||||
|
||||
@@ -37,7 +37,6 @@
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
@@ -53,12 +52,12 @@ namespace cif
|
||||
// --------------------------------------------------------------------
|
||||
/** @brief item is a transient class that is used to pass data into rows
|
||||
* but it also takes care of formatting data.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* The class cif::item is often used implicitly when creating a row in a category
|
||||
* using the emplace function.
|
||||
*
|
||||
*
|
||||
* @code{.cpp}
|
||||
* cif::category cat("my-cat");
|
||||
* cat.emplace({
|
||||
@@ -68,12 +67,12 @@ namespace cif
|
||||
* { "item-4", std::make_optional<int>(42) }, // <- stores an item with value 42
|
||||
* { "item-5" } // <- stores an item with value .
|
||||
* });
|
||||
*
|
||||
*
|
||||
* std::cout << cat << '\n';
|
||||
* @endcode
|
||||
*
|
||||
*
|
||||
* Will result in:
|
||||
*
|
||||
*
|
||||
* @code{.txt}
|
||||
* _my-cat.item-1 1
|
||||
* _my-cat.item-2 1.00
|
||||
@@ -176,7 +175,7 @@ class item
|
||||
|
||||
/// \brief constructor for an item with name \a name and as
|
||||
/// content value \a value
|
||||
template<typename T, std::enable_if_t<std::is_same_v<T, std::string>, int> = 0>
|
||||
template <typename T, std::enable_if_t<std::is_same_v<T, std::string>, int> = 0>
|
||||
item(const std::string_view name, T &&value)
|
||||
: m_name(name)
|
||||
, m_value(std::move(value))
|
||||
@@ -221,15 +220,15 @@ class item
|
||||
item &operator=(item &&rhs) noexcept = default;
|
||||
/** @endcond */
|
||||
|
||||
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
|
||||
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
|
||||
std::string value() const && { return std::move(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; }
|
||||
|
||||
/// \brief empty means either null or unknown
|
||||
bool empty() const { return m_value.empty(); }
|
||||
bool empty() const { return is_null() or is_unknown() or m_value.empty(); }
|
||||
|
||||
/// \brief returns true if the item contains '.'
|
||||
bool is_null() const { return m_value == "."; }
|
||||
@@ -260,8 +259,8 @@ class item
|
||||
// --------------------------------------------------------------------
|
||||
/// \brief the internal storage for items in a category
|
||||
///
|
||||
/// Internal storage, strictly forward linked list with minimal space
|
||||
/// requirements. Strings of size 7 or shorter are stored internally.
|
||||
/// Internal storage, with minimal space requirements. Strings of
|
||||
/// size 7 or shorter are stored internally.
|
||||
/// Typically, more than 99% of the strings in an mmCIF file are less
|
||||
/// than 8 bytes in length.
|
||||
|
||||
@@ -338,7 +337,8 @@ struct item_value
|
||||
/** 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 };
|
||||
const char *ptr = m_length >= kBufferSize ? m_data : m_local_data;
|
||||
return (m_length == 1 and *ptr == '?') ? std::string_view{} : std::string_view{ ptr, m_length };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -560,7 +560,9 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> an
|
||||
auto b = txt.data();
|
||||
auto e = txt.data() + txt.size();
|
||||
|
||||
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);
|
||||
std::from_chars_result r = (b + 1 < e and *b == '+' and std::isdigit(b[1])) //
|
||||
? from_chars(b + 1, e, result)
|
||||
: from_chars(b, e, result);
|
||||
|
||||
if ((bool)r.ec or r.ptr != e)
|
||||
{
|
||||
@@ -595,7 +597,9 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> an
|
||||
auto b = txt.data();
|
||||
auto e = txt.data() + txt.size();
|
||||
|
||||
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);
|
||||
std::from_chars_result r = (b + 1 < e and *b == '+' and std::isdigit(b[1]))
|
||||
? from_chars(b + 1, e, v)
|
||||
: from_chars(b, e, v);
|
||||
|
||||
if ((bool)r.ec or r.ptr != e)
|
||||
{
|
||||
|
||||
@@ -26,9 +26,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cif++/condition.hpp"
|
||||
#include "cif++/row.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <numeric>
|
||||
|
||||
/**
|
||||
* @file iterator.hpp
|
||||
@@ -262,6 +265,11 @@ class iterator_impl<Category>
|
||||
return m_current;
|
||||
}
|
||||
|
||||
int64_t row_id() const
|
||||
{
|
||||
return reinterpret_cast<int64_t>(m_current.m_row);
|
||||
}
|
||||
|
||||
iterator_impl &operator++()
|
||||
{
|
||||
if (m_current)
|
||||
@@ -489,6 +497,9 @@ class iterator_proxy
|
||||
std::swap(m_item_ix, rhs.m_item_ix);
|
||||
}
|
||||
|
||||
protected:
|
||||
iterator_proxy(category_type &cat);
|
||||
|
||||
private:
|
||||
category_type *m_category;
|
||||
row_iterator m_begin, m_end;
|
||||
@@ -530,6 +541,7 @@ class conditional_iterator_proxy
|
||||
using pointer = value_type *;
|
||||
using reference = value_type;
|
||||
|
||||
conditional_iterator_impl() = default;
|
||||
conditional_iterator_impl(CategoryType &cat, row_iterator pos, const condition &cond, const std::array<uint16_t, N> &cix);
|
||||
conditional_iterator_impl(const conditional_iterator_impl &i) = default;
|
||||
conditional_iterator_impl &operator=(const conditional_iterator_impl &i) = default;
|
||||
@@ -649,6 +661,15 @@ iterator_proxy<Category, Ts...>::iterator_proxy(Category &cat, row_iterator pos,
|
||||
m_item_ix[i++] = m_category->get_item_ix(item);
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
iterator_proxy<Category, Ts...>::iterator_proxy(Category &cat)
|
||||
: m_category(&cat)
|
||||
, m_begin(cat.begin())
|
||||
, m_end(cat.end())
|
||||
{
|
||||
std::iota(m_item_ix.begin(), m_item_ix.end(), 0);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
@@ -661,6 +682,8 @@ conditional_iterator_proxy<Category, Ts...>::conditional_iterator_impl::conditio
|
||||
{
|
||||
if (m_condition == nullptr or m_condition->empty())
|
||||
m_begin = m_end;
|
||||
else
|
||||
m_current = *m_begin;
|
||||
}
|
||||
|
||||
template <typename Category, typename... Ts>
|
||||
|
||||
@@ -124,6 +124,23 @@ class matrix_expression
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
template <typename M2>
|
||||
constexpr bool operator==(const matrix_expression<M2> &m) const
|
||||
{
|
||||
bool same = false;
|
||||
if (dim_m() == m.dim_m() and dim_n() == m.dim_n())
|
||||
{
|
||||
same = true;
|
||||
for (std::size_t i = 0; same and i < m.dim_m(); ++i)
|
||||
{
|
||||
for (std::size_t j = 0; same and j < m.dim_n(); ++j)
|
||||
same = operator()(i, j) == m(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
return same;
|
||||
}
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -594,6 +611,35 @@ auto operator*(const matrix_expression<M1> &m1, const matrix_expression<M2> &m2)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
template <typename M2>
|
||||
class sub_matrix : public matrix_expression<sub_matrix<M2>>
|
||||
{
|
||||
public:
|
||||
sub_matrix(const M2 &m, int i, int j)
|
||||
: m_m(m)
|
||||
, m_i(i)
|
||||
, m_j(j)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr std::size_t dim_m() const { return m_m.dim_m() - 1; } ///< Return dimension m
|
||||
constexpr std::size_t dim_n() const { return m_m.dim_n() - 1; } ///< Return dimension n
|
||||
|
||||
/** Access to the value of element [ @a i, @a j ] */
|
||||
constexpr auto operator()(std::size_t i, std::size_t j) const
|
||||
{
|
||||
return m_m(
|
||||
i >= m_i ? i + 1 : i,
|
||||
j >= m_j ? j + 1 : j);
|
||||
}
|
||||
|
||||
private:
|
||||
const M2 &m_m;
|
||||
std::size_t m_i, m_j;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** Generic routine to calculate the determinant of a matrix
|
||||
*
|
||||
* @note This is currently only implemented for fixed matrices of size 3x3
|
||||
@@ -605,11 +651,23 @@ auto determinant(const M &m);
|
||||
template <typename F = float>
|
||||
auto determinant(const matrix3x3<F> &m)
|
||||
{
|
||||
return (m(0, 0) * (m(1, 1) * m(2, 2) - m(1, 2) * m(2, 1)) +
|
||||
m(0, 1) * (m(1, 2) * m(2, 0) - m(1, 0) * m(2, 2)) +
|
||||
m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0)));
|
||||
return (m(0, 0) * ((m(1, 1) * m(2, 2) - m(1, 2) * m(2, 1))) +
|
||||
m(0, 1) * ((m(1, 2) * m(2, 0) - m(1, 0) * m(2, 2))) +
|
||||
m(0, 2) * ((m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0))));
|
||||
}
|
||||
|
||||
/** Implementation of the determinant function for fixed size matrices of size 4x4 */
|
||||
template <typename F = float>
|
||||
F determinant(const matrix4x4<F> &m)
|
||||
{
|
||||
return m(0, 0) * determinant(matrix3x3<F>(sub_matrix<decltype(m)>(m, 0, 0))) -
|
||||
m(0, 1) * determinant(matrix3x3<F>(sub_matrix<decltype(m)>(m, 0, 1))) +
|
||||
m(0, 2) * determinant(matrix3x3<F>(sub_matrix<decltype(m)>(m, 0, 2))) -
|
||||
m(0, 3) * determinant(matrix3x3<F>(sub_matrix<decltype(m)>(m, 0, 3)));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/** Generic routine to calculate the inverse of a matrix
|
||||
*
|
||||
* @note This is currently only implemented for fixed matrices of size 3x3
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "cif++/atom_type.hpp"
|
||||
#include "cif++/datablock.hpp"
|
||||
#include "cif++/point.hpp"
|
||||
#include "cif++/row.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
@@ -134,14 +135,20 @@ class atom
|
||||
|
||||
row_handle row_aniso()
|
||||
{
|
||||
row_handle result{};
|
||||
auto cat = m_db.get("atom_site_anisotrop");
|
||||
return cat ? cat->operator[]({ { "id", m_id } }) : row_handle{};
|
||||
if (cat)
|
||||
result = cat->operator[]({ { "id", m_id } });
|
||||
return result;
|
||||
}
|
||||
|
||||
const row_handle row_aniso() const
|
||||
{
|
||||
row_handle result{};
|
||||
auto cat = m_db.get("atom_site_anisotrop");
|
||||
return cat ? cat->operator[]({ { "id", m_id } }) : row_handle{};
|
||||
if (cat)
|
||||
result = cat->operator[]({ { "id", m_id } });
|
||||
return result;
|
||||
}
|
||||
|
||||
const datablock &m_db;
|
||||
@@ -1059,6 +1066,14 @@ class structure
|
||||
/// \return The newly create asym ID
|
||||
std::string create_non_poly(const std::string &entity_id, std::vector<row_initializer> atoms);
|
||||
|
||||
/// \brief Create a new NonPolymer struct_asym for a compound of type \a compound_id, returns asym_id.
|
||||
/// This method creates new atom records filled with info from the CCD compound info.
|
||||
///
|
||||
/// \param compound_id The compound ID of the new nonpoly
|
||||
/// \param skip_hydrogen Do not create hydrogen atoms when true
|
||||
/// \return The newly create asym ID
|
||||
std::string create_non_poly(const std::string &compound_id, bool skip_hydrogen);
|
||||
|
||||
/// \brief Create a new water with atom constructed from info in \a atom_info
|
||||
/// This method creates a new atom record filled with info from the info.
|
||||
///
|
||||
|
||||
@@ -149,6 +149,23 @@ void fixup_pdbx(file &pdbx_file, const validator &v);
|
||||
|
||||
bool reconstruct_pdbx(file &pdbx_file, const validator &v);
|
||||
|
||||
/** \brief This is an extension to cif::validator, use the logic in common
|
||||
* PDBx files to see if the file is internally consistent.
|
||||
*
|
||||
* This function for now checks if the following categories are consistent:
|
||||
*
|
||||
* atom_site -> pdbx_poly_seq_scheme -> entity_poly_seq -> entity_poly -> entity
|
||||
*
|
||||
* Use the common \ref cif::VERBOSE flag to turn on diagnostic messages.
|
||||
*
|
||||
* This function throws a std::system_error in case of an error
|
||||
*
|
||||
* \param pdbx_file The input file
|
||||
* \result Returns true if the file was valid and consistent
|
||||
*/
|
||||
|
||||
bool is_valid_pdbx_file(const file &pdbx_file);
|
||||
|
||||
/** \brief This is an extension to cif::validator, use the logic in common
|
||||
* PDBx files to see if the file is internally consistent.
|
||||
*
|
||||
@@ -165,8 +182,7 @@ bool reconstruct_pdbx(file &pdbx_file, const validator &v);
|
||||
* \result Returns true if the file was valid and consistent
|
||||
*/
|
||||
|
||||
bool is_valid_pdbx_file(const file &pdbx_file,
|
||||
const validator &v = validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
bool is_valid_pdbx_file(const file &pdbx_file, const validator &v);
|
||||
|
||||
/** \brief This is an extension to cif::validator, use the logic in common
|
||||
* PDBx files to see if the file is internally consistent.
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/*-
|
||||
* 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
|
||||
|
||||
/// \file cif2pdb.hpp
|
||||
/// \deprecated This file is no longer used. Please use "cif++/pdb.hpp" instead
|
||||
|
||||
#warning "Use of this file is deprecated, please use "cif++/pdb.hpp"
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2022 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
|
||||
|
||||
/// \file io.hpp
|
||||
/// \deprecated This file is no longer used. Please use "cif++/pdb.hpp" instead
|
||||
|
||||
#warning "Use of this file is deprecated, please use "cif++/pdb.hpp"
|
||||
@@ -1,32 +0,0 @@
|
||||
/*-
|
||||
* 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
|
||||
|
||||
/// \file pdb2cif.hpp
|
||||
/// \deprecated This file is no longer used. Please use "cif++/pdb.hpp" instead
|
||||
|
||||
#warning "Use of this file is deprecated, please use "cif++/pdb.hpp"
|
||||
@@ -1,32 +0,0 @@
|
||||
/*-
|
||||
* 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
|
||||
|
||||
/// \file tls.hpp
|
||||
/// \deprecated This code has been moved to libpdb-redo
|
||||
|
||||
#warning "This code has been moved to libpdb-redo"
|
||||
@@ -30,7 +30,9 @@
|
||||
#include <cmath>
|
||||
#include <complex>
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <valarray>
|
||||
|
||||
#if __has_include(<clipper/core/coords.h>)
|
||||
@@ -365,11 +367,18 @@ class quaternion_type
|
||||
}
|
||||
|
||||
/// \brief test for all zero values
|
||||
constexpr operator bool() const
|
||||
constexpr explicit operator bool() const
|
||||
{
|
||||
return a != 0 or b != 0 or c != 0 or d != 0;
|
||||
}
|
||||
|
||||
/// \brief for debugging e.g.
|
||||
friend std::ostream &operator<<(std::ostream &os, const quaternion_type &rhs)
|
||||
{
|
||||
os << std::format("{{ a: {}, b: {}, c: {}, d: {} }}", rhs.a, rhs.b, rhs.c, rhs.d);
|
||||
return os;
|
||||
}
|
||||
|
||||
private:
|
||||
value_type a, b, c, d;
|
||||
};
|
||||
@@ -743,6 +752,55 @@ inline constexpr auto cross_product(const point_type<F1> &a, const point_type<F2
|
||||
a.m_x * b.m_y - b.m_x * a.m_y);
|
||||
}
|
||||
|
||||
/// \brief return the squared norm of point @a p
|
||||
template <typename F>
|
||||
constexpr F norm_squared(const point_type<F> &p)
|
||||
{
|
||||
return p.m_x * p.m_x + p.m_y * p.m_y + p.m_z * p.m_z;
|
||||
}
|
||||
|
||||
/// \brief return the norm of point @a p
|
||||
template <typename F>
|
||||
constexpr point_type<F> norm(const point_type<F> &p)
|
||||
{
|
||||
return std::sqrt(norm_squared(p));
|
||||
}
|
||||
|
||||
/// \brief return the point where two lines intersect, or an empty value if they don't intersect at all
|
||||
template <typename F>
|
||||
std::optional<cif::point> line_line_intersection(const point_type<F> &p1,
|
||||
const point_type<F> &p2, const point_type<F> &p3, const point_type<F> &p4)
|
||||
{
|
||||
auto p13 = p1 - p3;
|
||||
auto p43 = p4 - p3;
|
||||
if (std::abs(p43.m_x) < std::numeric_limits<F>::epsilon() and std::abs(p43.m_y) < std::numeric_limits<F>::epsilon() and std::abs(p43.m_z) < std::numeric_limits<F>::epsilon())
|
||||
return {};
|
||||
|
||||
auto p21 = p2 - p1;
|
||||
if (std::abs(p21.m_x) < std::numeric_limits<F>::epsilon() and std::abs(p21.m_y) < std::numeric_limits<F>::epsilon() and std::abs(p21.m_z) < std::numeric_limits<F>::epsilon())
|
||||
return {};
|
||||
|
||||
auto d1343 = cif::dot_product(p43, p13);
|
||||
auto d4321 = cif::dot_product(p43, p21);
|
||||
auto d1321 = cif::dot_product(p13, p21);
|
||||
auto d4343 = cif::dot_product(p43, p43);
|
||||
auto d2121 = cif::dot_product(p21, p21);
|
||||
|
||||
auto denom = d2121 * d4343 - d4321 * d4321;
|
||||
if (std::abs(denom) < std::numeric_limits<F>::epsilon())
|
||||
return {};
|
||||
|
||||
auto numer = d1343 * d4321 - d1321 * d4343;
|
||||
|
||||
auto mua = numer / denom;
|
||||
auto mub = (d1343 + d4321 * mua) / d4343;
|
||||
|
||||
auto pa = p1 + mua * p21;
|
||||
auto pb = p3 + mub * p43;
|
||||
|
||||
return { (pa + pb) / 2 };
|
||||
}
|
||||
|
||||
/// \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)
|
||||
@@ -806,6 +864,9 @@ constexpr auto distance_point_to_line(const point_type<F> &l1, const point_type<
|
||||
return cross.length() / line.length();
|
||||
}
|
||||
|
||||
/// \brief return the smallest sphere around the points in @a pts
|
||||
std::tuple<point, float> smallest_sphere_around_points(std::vector<point> pts);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/**
|
||||
* @brief For e.g. simulated annealing, returns a new point that is moved in
|
||||
|
||||
@@ -29,55 +29,61 @@
|
||||
#include "cif++/item.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
/**
|
||||
* @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_item_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
|
||||
{
|
||||
|
||||
namespace cql
|
||||
{
|
||||
struct connection_impl;
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
@@ -141,7 +147,7 @@ namespace detail
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/// \brief similar to std::tie, assign values to each element in @a v from the
|
||||
/// \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)
|
||||
@@ -160,7 +166,7 @@ class row : public std::vector<item_value>
|
||||
/**
|
||||
* @brief Return the item_value pointer for item at index @a ix
|
||||
*/
|
||||
item_value* get(uint16_t ix)
|
||||
item_value *get(uint16_t ix)
|
||||
{
|
||||
return ix < size() ? &data()[ix] : nullptr;
|
||||
}
|
||||
@@ -168,7 +174,7 @@ class row : public std::vector<item_value>
|
||||
/**
|
||||
* @brief Return the const item_value pointer for item at index @a ix
|
||||
*/
|
||||
const item_value* get(uint16_t ix) const
|
||||
const item_value *get(uint16_t ix) const
|
||||
{
|
||||
return ix < size() ? &data()[ix] : nullptr;
|
||||
}
|
||||
@@ -184,7 +190,7 @@ class row : public std::vector<item_value>
|
||||
{
|
||||
if (ix >= size())
|
||||
resize(ix + 1);
|
||||
|
||||
|
||||
at(ix) = std::move(iv);
|
||||
}
|
||||
|
||||
@@ -208,7 +214,8 @@ class row_handle
|
||||
friend class category;
|
||||
friend class category_index;
|
||||
friend class row_initializer;
|
||||
template <typename, typename...> friend class iterator_impl;
|
||||
template <typename, typename...>
|
||||
friend class iterator_impl;
|
||||
|
||||
row_handle() = default;
|
||||
|
||||
@@ -233,6 +240,12 @@ class row_handle
|
||||
return *m_category;
|
||||
}
|
||||
|
||||
/// \brief return the row ID
|
||||
int64_t row_id() const
|
||||
{
|
||||
return reinterpret_cast<int64_t>(m_row);
|
||||
}
|
||||
|
||||
/// \brief Return true if the row is empty or uninitialised
|
||||
bool empty() const
|
||||
{
|
||||
@@ -299,19 +312,19 @@ class row_handle
|
||||
}
|
||||
|
||||
/// \brief assign each of the items named in @a values to their respective value
|
||||
void assign(const std::vector<item> &values)
|
||||
void assign(const std::vector<item> &values, bool updateLinked = true)
|
||||
{
|
||||
for (auto &value : values)
|
||||
assign(value, true);
|
||||
assign(value, updateLinked);
|
||||
}
|
||||
|
||||
/** \brief assign the value @a value to the item named @a name
|
||||
*
|
||||
/** \brief assign the value @a value to the item named @a name
|
||||
*
|
||||
* If updateLinked it true, linked records are updated as well.
|
||||
* That means that if item @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
|
||||
*/
|
||||
@@ -322,12 +335,12 @@ class row_handle
|
||||
}
|
||||
|
||||
/** \brief assign the value @a value to item at index @a item
|
||||
*
|
||||
*
|
||||
* If updateLinked it true, linked records are updated as well.
|
||||
* That means that if item @a item 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
|
||||
*/
|
||||
@@ -346,6 +359,8 @@ class row_handle
|
||||
|
||||
uint16_t add_item(std::string_view name);
|
||||
|
||||
friend cql::connection_impl;
|
||||
|
||||
row *get_row()
|
||||
{
|
||||
return m_row;
|
||||
@@ -371,7 +386,7 @@ 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.
|
||||
@@ -406,7 +421,6 @@ class row_initializer : public std::vector<item>
|
||||
/// \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);
|
||||
|
||||
|
||||
@@ -355,279 +355,35 @@ std::string cif_id_for_number(int number);
|
||||
std::vector<std::string> word_wrap(const std::string &text, std::size_t width);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
/// \brief std::from_chars for floating point types.
|
||||
///
|
||||
/// These are optional, there's a selected_charconv class below that selects
|
||||
/// 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.
|
||||
|
||||
template <typename FloatType, std::enable_if_t<std::is_floating_point_v<FloatType>, int> = 0>
|
||||
std::from_chars_result from_chars(const char *first, const char *last, FloatType &value)
|
||||
{
|
||||
std::from_chars_result result{ first, {} };
|
||||
|
||||
enum State
|
||||
{
|
||||
IntegerSign,
|
||||
Integer,
|
||||
Fraction,
|
||||
ExponentSign,
|
||||
Exponent
|
||||
} state = IntegerSign;
|
||||
int sign = 1;
|
||||
unsigned long long vi = 0;
|
||||
int fl = 0, tz = 0;
|
||||
int exponent_sign = 1;
|
||||
int exponent = 0;
|
||||
bool done = false;
|
||||
|
||||
while (not done and not (bool)result.ec)
|
||||
{
|
||||
char ch = result.ptr != last ? *result.ptr : 0;
|
||||
++result.ptr;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case IntegerSign:
|
||||
if (ch == '-')
|
||||
{
|
||||
sign = -1;
|
||||
state = Integer;
|
||||
}
|
||||
else if (ch == '+')
|
||||
state = Integer;
|
||||
else if (ch >= '0' and ch <= '9')
|
||||
{
|
||||
vi = ch - '0';
|
||||
state = Integer;
|
||||
}
|
||||
else if (ch == '.')
|
||||
state = Fraction;
|
||||
else
|
||||
result.ec = std::errc::invalid_argument;
|
||||
break;
|
||||
|
||||
case Integer:
|
||||
if (ch >= '0' and ch <= '9')
|
||||
vi = 10 * vi + (ch - '0');
|
||||
else if (ch == 'e' or ch == 'E')
|
||||
state = ExponentSign;
|
||||
else if (ch == '.')
|
||||
state = Fraction;
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
--result.ptr;
|
||||
}
|
||||
break;
|
||||
|
||||
case Fraction:
|
||||
if (ch >= '0' and ch <= '9')
|
||||
{
|
||||
vi = 10 * vi + (ch - '0');
|
||||
|
||||
if (ch == '0')
|
||||
tz += 1;
|
||||
else
|
||||
{
|
||||
fl += tz + 1;
|
||||
tz = 0;
|
||||
}
|
||||
}
|
||||
else if (ch == 'e' or ch == 'E')
|
||||
state = ExponentSign;
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
--result.ptr;
|
||||
}
|
||||
break;
|
||||
|
||||
case ExponentSign:
|
||||
if (ch == '-')
|
||||
{
|
||||
exponent_sign = -1;
|
||||
state = Exponent;
|
||||
}
|
||||
else if (ch == '+')
|
||||
state = Exponent;
|
||||
else if (ch >= '0' and ch <= '9')
|
||||
{
|
||||
exponent = ch - '0';
|
||||
state = Exponent;
|
||||
}
|
||||
else
|
||||
result.ec = std::errc::invalid_argument;
|
||||
break;
|
||||
|
||||
case Exponent:
|
||||
if (ch >= '0' and ch <= '9')
|
||||
exponent = 10 * exponent + (ch - '0');
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
--result.ptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (not (bool)result.ec)
|
||||
{
|
||||
while (tz-- > 0)
|
||||
vi /= 10;
|
||||
|
||||
long double v = std::pow(10, -fl) * vi * sign;
|
||||
if (exponent != 0)
|
||||
v *= std::pow(10, exponent * exponent_sign);
|
||||
|
||||
if (std::isnan(v))
|
||||
result.ec = std::errc::invalid_argument;
|
||||
else if (std::abs(v) > std::numeric_limits<FloatType>::max())
|
||||
result.ec = std::errc::result_out_of_range;
|
||||
|
||||
value = static_cast<FloatType>(v);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \brief duplication of std::chars_format for deficient STL implementations
|
||||
enum class chars_format
|
||||
{
|
||||
scientific = 1,
|
||||
fixed = 2,
|
||||
// hex,
|
||||
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)
|
||||
{
|
||||
int size = static_cast<int>(last - first);
|
||||
int r = 0;
|
||||
|
||||
switch (fmt)
|
||||
{
|
||||
case chars_format::scientific:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%le", value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%e", value);
|
||||
break;
|
||||
|
||||
case chars_format::fixed:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%lf", value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%f", value);
|
||||
break;
|
||||
|
||||
case chars_format::general:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%lg", value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%g", value);
|
||||
break;
|
||||
}
|
||||
|
||||
std::to_chars_result result;
|
||||
if (r < 0 or r >= size)
|
||||
result = { first, std::errc::value_too_large };
|
||||
else
|
||||
result = { first + r, std::errc() };
|
||||
|
||||
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)
|
||||
{
|
||||
int size = static_cast<int>(last - first);
|
||||
int r = 0;
|
||||
|
||||
switch (fmt)
|
||||
{
|
||||
case chars_format::scientific:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%.*le", precision, value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%.*e", precision, value);
|
||||
break;
|
||||
|
||||
case chars_format::fixed:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%.*lf", precision, value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%.*f", precision, value);
|
||||
break;
|
||||
|
||||
case chars_format::general:
|
||||
if constexpr (std::is_same_v<FloatType, long double>)
|
||||
r = snprintf(first, last - first, "%.*lg", precision, value);
|
||||
else
|
||||
r = snprintf(first, last - first, "%.*g", precision, value);
|
||||
break;
|
||||
}
|
||||
|
||||
std::to_chars_result result;
|
||||
if (r < 0 or r >= size)
|
||||
result = { first, std::errc::value_too_large };
|
||||
else
|
||||
result = { first + r, std::errc() };
|
||||
|
||||
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);
|
||||
}
|
||||
using from_chars_function = decltype(std::from_chars(std::declval<const char *>(), std::declval<const char *>(), std::declval<T &>()));
|
||||
|
||||
/// @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 &>()));
|
||||
template <typename T, typename = void>
|
||||
struct ff_charconv;
|
||||
|
||||
/**
|
||||
* @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>>;
|
||||
struct ff_charconv<T, typename std::enable_if_t<std::is_floating_point_v<T>>>
|
||||
{
|
||||
static std::from_chars_result from_chars(const char *a, const char *b, T &v);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using charconv = typename std::conditional_t<std_experimental::is_detected_v<from_chars_function, T>, std_charconv<T>, ff_charconv<T>>;
|
||||
|
||||
template <typename T>
|
||||
constexpr auto from_chars(const char *s, const char *e, T &v)
|
||||
{
|
||||
return charconv<T>::from_chars(s, e, v);
|
||||
}
|
||||
|
||||
} // namespace cif
|
||||
@@ -30,7 +30,6 @@
|
||||
#include "cif++/text.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
@@ -343,11 +342,11 @@ struct item_validator
|
||||
*/
|
||||
struct category_validator
|
||||
{
|
||||
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_items; ///< The mandatory items for this category
|
||||
std::set<item_validator> m_item_validators; ///< The item validators for the items in this category
|
||||
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_items; ///< The mandatory items for this category
|
||||
std::vector<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
|
||||
@@ -520,10 +519,18 @@ class validator_factory
|
||||
static validator_factory &instance();
|
||||
|
||||
/// @brief Return validator with info recorded in @a audit_conform
|
||||
const validator &get(const category &audit_conform);
|
||||
const validator *get(const category &audit_conform);
|
||||
|
||||
/// @brief Return the single-file validator with name @a dictionary_name
|
||||
const validator &get(std::string_view dictionary_name);
|
||||
/// and the dictionary name may be a set of dictionaries separated by comma
|
||||
const validator *get(std::string_view dictionary_name);
|
||||
|
||||
/// @brief Return validator with info recorded in @a audit_conform
|
||||
const validator &operator[](const category &audit_conform);
|
||||
|
||||
/// @brief Return the single-file validator with name @a dictionary_name
|
||||
/// and the dictionary name may be a set of dictionaries separated by comma
|
||||
const validator &operator[](std::string_view dictionary_name);
|
||||
|
||||
/// @brief Return true if the version @a found is equal or higher than @a expected for dictionary @a name
|
||||
static bool check_version(std::string_view name, std::string_view expected, std::string_view found);
|
||||
@@ -535,6 +542,21 @@ class validator_factory
|
||||
return m_validators.emplace_back(std::move(v));
|
||||
}
|
||||
|
||||
#if __cplusplus >= 202302L
|
||||
/// @brief Return validator with info recorded in @a audit_conform
|
||||
static validator &operator[](const category &audit_conform)
|
||||
{
|
||||
return instance()[audit_conform];
|
||||
}
|
||||
|
||||
/// @brief Return the single-file validator with name @a dictionary_name
|
||||
/// and the dictionary name may be a set of dictionaries separated by comma
|
||||
static validator &operator[](std::string_view dict)
|
||||
{
|
||||
return instance()[dict];
|
||||
}
|
||||
#endif
|
||||
|
||||
private:
|
||||
validator_factory() = default;
|
||||
|
||||
|
||||
2080
sql-92.bnf
Normal file
2080
sql-92.bnf
Normal file
File diff suppressed because it is too large
Load Diff
505
src/category.cpp
505
src/category.cpp
@@ -25,6 +25,7 @@
|
||||
*/
|
||||
|
||||
#include "cif++/category.hpp"
|
||||
|
||||
#include "cif++/datablock.hpp"
|
||||
#include "cif++/parser.hpp"
|
||||
#include "cif++/utilities.hpp"
|
||||
@@ -32,6 +33,8 @@
|
||||
|
||||
#include <numeric>
|
||||
#include <stack>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// TODO: Find out what the rules are exactly for linked items, the current implementation
|
||||
// is inconsistent. It all depends whether a link is satified if a item taking part in the
|
||||
@@ -108,7 +111,7 @@ class row_comparator
|
||||
std::string_view ka = ai->value;
|
||||
std::string_view kb = rhb[k].text();
|
||||
|
||||
if (not (ai->may_be_null and rhb[k].empty()))
|
||||
if (not(ai->may_be_null and rhb[k].empty()))
|
||||
d = f(ka, kb);
|
||||
|
||||
if (d != 0)
|
||||
@@ -535,6 +538,7 @@ void swap(category &a, category &b) noexcept
|
||||
std::swap(a.m_index, b.m_index);
|
||||
std::swap(a.m_head, b.m_head);
|
||||
std::swap(a.m_tail, b.m_tail);
|
||||
std::swap(a.m_dirty, b.m_dirty);
|
||||
}
|
||||
|
||||
category::~category()
|
||||
@@ -606,6 +610,30 @@ void category::remove_item(std::string_view item_name)
|
||||
}
|
||||
}
|
||||
|
||||
void category::drop_empty_items()
|
||||
{
|
||||
std::vector<bool> is_empty(m_items.size(), true);
|
||||
|
||||
for (auto &row : *this)
|
||||
{
|
||||
for (size_t ix = 0; ix < m_items.size(); ++ix)
|
||||
{
|
||||
if (is_empty[ix] and not row[ix].empty())
|
||||
is_empty[ix] = false;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> items;
|
||||
for (size_t ix = 0; ix < m_items.size(); ++ix)
|
||||
{
|
||||
if (is_empty[ix])
|
||||
items.push_back(m_items[ix].m_name);
|
||||
}
|
||||
|
||||
for (auto &item : items)
|
||||
remove_item(item);
|
||||
}
|
||||
|
||||
void category::rename_item(std::string_view from_name, std::string_view to_name)
|
||||
{
|
||||
for (std::size_t ix = 0; ix < m_items.size(); ++ix)
|
||||
@@ -620,12 +648,12 @@ void category::rename_item(std::string_view from_name, std::string_view to_name)
|
||||
}
|
||||
}
|
||||
|
||||
iset category::get_items() const
|
||||
std::vector<std::string> category::get_items() const
|
||||
{
|
||||
iset result;
|
||||
std::vector<std::string> result;
|
||||
|
||||
for (auto &col : m_items)
|
||||
result.insert(col.m_name);
|
||||
result.emplace_back(col.m_name);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1248,6 +1276,7 @@ void category::clear()
|
||||
|
||||
delete m_index;
|
||||
m_index = nullptr;
|
||||
m_dirty = true;
|
||||
}
|
||||
|
||||
void category::erase_orphans(condition &&cond, category &parent)
|
||||
@@ -1496,6 +1525,8 @@ void category::update_value(row *row, uint16_t item, std::string_view value, boo
|
||||
if (value == oldValue) // no need to update
|
||||
return;
|
||||
|
||||
m_dirty = true;
|
||||
|
||||
std::string oldStrValue{ oldValue };
|
||||
|
||||
// check the value
|
||||
@@ -1637,6 +1668,8 @@ void category::delete_row(row *r)
|
||||
row_allocator_type ra(get_allocator());
|
||||
row_allocator_traits::destroy(ra, r);
|
||||
row_allocator_traits::deallocate(ra, r, 1);
|
||||
|
||||
m_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1737,6 +1770,8 @@ category::iterator category::insert_impl(const_iterator pos, row *n)
|
||||
n = n->m_next = m_head->m_next;
|
||||
}
|
||||
|
||||
m_dirty = true;
|
||||
|
||||
return iterator(*this, n);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
@@ -1759,6 +1794,8 @@ void category::swap_item(uint16_t item_ix, row_handle &a, row_handle &b)
|
||||
auto &ra = *a.m_row;
|
||||
auto &rb = *b.m_row;
|
||||
|
||||
m_dirty = true;
|
||||
|
||||
while (ra.size() <= item_ix)
|
||||
ra.emplace_back("");
|
||||
|
||||
@@ -1773,6 +1810,8 @@ void category::sort(std::function<int(row_handle, row_handle)> f)
|
||||
if (m_head == nullptr)
|
||||
return;
|
||||
|
||||
m_dirty = true;
|
||||
|
||||
std::vector<row_handle> rows;
|
||||
for (auto itemRow = m_head; itemRow != nullptr; itemRow = itemRow->m_next)
|
||||
rows.emplace_back(*this, *itemRow);
|
||||
@@ -1912,10 +1951,10 @@ void category::write(std::ostream &os) const
|
||||
{
|
||||
std::vector<uint16_t> order(m_items.size());
|
||||
iota(order.begin(), order.end(), static_cast<uint16_t>(0));
|
||||
write(os, order, false);
|
||||
write_cif(os, order, false);
|
||||
}
|
||||
|
||||
void category::write(std::ostream &os, const std::vector<std::string> &items, bool addMissingItems)
|
||||
void category::write(std::ostream &os, output_format fmt, const std::vector<std::string> &items, bool addMissingItems)
|
||||
{
|
||||
// make sure all items are present
|
||||
for (auto &c : items)
|
||||
@@ -1936,10 +1975,43 @@ void category::write(std::ostream &os, const std::vector<std::string> &items, bo
|
||||
}
|
||||
}
|
||||
|
||||
write(os, order, true);
|
||||
switch (fmt)
|
||||
{
|
||||
case output_format::cif:
|
||||
write_cif(os, order, addMissingItems);
|
||||
break;
|
||||
|
||||
case output_format::csv:
|
||||
write_delimited(os, order, addMissingItems, ",", false, true);
|
||||
break;
|
||||
|
||||
case output_format::tsv:
|
||||
write_delimited(os, order, addMissingItems, "\t", false, true);
|
||||
break;
|
||||
|
||||
case output_format::list:
|
||||
write_delimited(os, order, addMissingItems, "|", false, false);
|
||||
break;
|
||||
|
||||
case output_format::column:
|
||||
write_delimited(os, order, addMissingItems, " ", true, true);
|
||||
break;
|
||||
|
||||
case output_format::markdown:
|
||||
write_markdown(os, order, addMissingItems);
|
||||
break;
|
||||
|
||||
case output_format::table:
|
||||
write_table(os, order, addMissingItems, true);
|
||||
break;
|
||||
|
||||
case output_format::box:
|
||||
write_table(os, order, addMissingItems, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void category::write(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems) const
|
||||
void category::write_cif(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems) const
|
||||
{
|
||||
if (empty())
|
||||
return;
|
||||
@@ -2112,6 +2184,423 @@ void category::write(std::ostream &os, const std::vector<uint16_t> &order, bool
|
||||
os << "# \n";
|
||||
}
|
||||
|
||||
void category::write_delimited(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems,
|
||||
std::string_view delimiter, bool aligned, bool header) const
|
||||
{
|
||||
if (empty())
|
||||
return;
|
||||
|
||||
std::vector<bool> right_aligned(m_items.size(), false);
|
||||
|
||||
if (aligned and m_cat_validator != nullptr)
|
||||
{
|
||||
for (auto cix : order)
|
||||
{
|
||||
auto &col = m_items[cix];
|
||||
right_aligned[cix] = col.m_validator != nullptr and
|
||||
col.m_validator->m_type != nullptr and
|
||||
col.m_validator->m_type->m_primitive_type == cif::DDL_PrimitiveType::Numb;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::size_t> itemWidths(m_items.size());
|
||||
auto get_line = [delimiter](std::string_view s) -> std::string
|
||||
{
|
||||
if (delimiter == ",")
|
||||
{
|
||||
if (s.find_first_of("\",") == std::string::npos)
|
||||
return std::string{ s };
|
||||
|
||||
std::string r{ '"' };
|
||||
r.reserve(s.length() + 2);
|
||||
for (auto ch : s)
|
||||
{
|
||||
if (ch == '"')
|
||||
r.append("\"\"");
|
||||
else
|
||||
r.push_back(ch);
|
||||
}
|
||||
r.push_back('"');
|
||||
return r;
|
||||
}
|
||||
else if (delimiter == "\t")
|
||||
{
|
||||
std::string r;
|
||||
r.reserve(s.length());
|
||||
for (auto ch : s)
|
||||
{
|
||||
if (ch == '\r' or ch == '\n' or ch == '\t' or ch == '\\')
|
||||
r.push_back('\\');
|
||||
r.push_back(ch);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
else if (delimiter == "|" or delimiter == " ")
|
||||
return std::string{ s };
|
||||
else
|
||||
{
|
||||
assert(false);
|
||||
return std::string{ s };
|
||||
}
|
||||
};
|
||||
|
||||
if (aligned)
|
||||
{
|
||||
if (header)
|
||||
{
|
||||
for (auto cix : order)
|
||||
{
|
||||
auto &col = m_items[cix];
|
||||
itemWidths[cix] = col.m_name.length();
|
||||
}
|
||||
}
|
||||
|
||||
for (auto r = m_head; r != nullptr; r = r->m_next)
|
||||
{
|
||||
for (uint16_t ix = 0; ix < r->size(); ++ix)
|
||||
{
|
||||
auto v = r->get(ix);
|
||||
if (v == nullptr)
|
||||
continue;
|
||||
|
||||
size_t l = get_line(v->text()).length();
|
||||
if (itemWidths[ix] < l)
|
||||
itemWidths[ix] = l;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (header)
|
||||
{
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << delimiter;
|
||||
|
||||
std::size_t w = itemWidths[cix];
|
||||
std::string_view s = m_items[cix].m_name;
|
||||
|
||||
if (s.length() < w)
|
||||
{
|
||||
if (delimiter == " ")
|
||||
{
|
||||
int l = (w - s.length()) / 2;
|
||||
int r = w - s.length() - l;
|
||||
os << std::string(l, ' ') << s << std::string(r, ' ');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (right_aligned[cix])
|
||||
os << std::string(w - s.length(), ' ');
|
||||
os << s;
|
||||
if (not right_aligned[cix])
|
||||
os << std::string(w - s.length(), ' ');
|
||||
}
|
||||
}
|
||||
else
|
||||
os << s;
|
||||
}
|
||||
|
||||
os << '\n';
|
||||
|
||||
if (delimiter == " ")
|
||||
{
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << delimiter;
|
||||
os << std::string(itemWidths[cix], '-');
|
||||
}
|
||||
|
||||
os << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
for (auto r = m_head; r != nullptr; r = r->m_next) // loop over rows
|
||||
{
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << delimiter;
|
||||
|
||||
std::size_t w = itemWidths[cix];
|
||||
|
||||
std::string_view s;
|
||||
auto iv = r->get(cix);
|
||||
|
||||
if (iv != nullptr)
|
||||
s = iv->text();
|
||||
|
||||
if (s == "?" or s == ".")
|
||||
s = "";
|
||||
|
||||
if (s.length() < w)
|
||||
{
|
||||
if (right_aligned[cix])
|
||||
os << std::string(w - s.length(), ' ');
|
||||
os << s;
|
||||
if (not right_aligned[cix])
|
||||
os << std::string(w - s.length(), ' ');
|
||||
}
|
||||
else
|
||||
os << s;
|
||||
}
|
||||
|
||||
os << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
void category::write_markdown(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems) const
|
||||
{
|
||||
if (empty())
|
||||
return;
|
||||
|
||||
std::vector<bool> right_aligned(m_items.size(), false);
|
||||
|
||||
if (m_cat_validator != nullptr)
|
||||
{
|
||||
for (auto cix : order)
|
||||
{
|
||||
auto &col = m_items[cix];
|
||||
right_aligned[cix] = col.m_validator != nullptr and
|
||||
col.m_validator->m_type != nullptr and
|
||||
col.m_validator->m_type->m_primitive_type == cif::DDL_PrimitiveType::Numb;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::size_t> itemWidths(m_items.size());
|
||||
|
||||
for (auto cix : order)
|
||||
{
|
||||
auto &col = m_items[cix];
|
||||
itemWidths[cix] = col.m_name.length();
|
||||
}
|
||||
|
||||
for (auto r = m_head; r != nullptr; r = r->m_next)
|
||||
{
|
||||
for (uint16_t ix = 0; ix < r->size(); ++ix)
|
||||
{
|
||||
auto v = r->get(ix);
|
||||
if (v == nullptr)
|
||||
continue;
|
||||
|
||||
size_t l = v->text().length();
|
||||
if (itemWidths[ix] < l)
|
||||
itemWidths[ix] = l;
|
||||
}
|
||||
}
|
||||
|
||||
os << "| ";
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << " | ";
|
||||
|
||||
std::size_t w = itemWidths[cix];
|
||||
std::string_view s = m_items[cix].m_name;
|
||||
|
||||
if (s.length() < w)
|
||||
{
|
||||
int l = (w - s.length()) / 2;
|
||||
int r = w - s.length() - l;
|
||||
os << std::string(l, ' ') << s << std::string(r, ' ');
|
||||
}
|
||||
else
|
||||
os << s;
|
||||
}
|
||||
os << " |\n";
|
||||
|
||||
os << "| ";
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << " | ";
|
||||
if (not right_aligned[cix])
|
||||
os << ':';
|
||||
os << std::string(itemWidths[cix] - 1, '-');
|
||||
if (right_aligned[cix])
|
||||
os << ':';
|
||||
}
|
||||
os << " |\n";
|
||||
|
||||
for (auto r = m_head; r != nullptr; r = r->m_next) // loop over rows
|
||||
{
|
||||
os << "| ";
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << " | ";
|
||||
|
||||
std::size_t w = itemWidths[cix];
|
||||
|
||||
std::string_view s;
|
||||
auto iv = r->get(cix);
|
||||
|
||||
if (iv != nullptr)
|
||||
s = iv->text();
|
||||
|
||||
if (s == "?" or s == ".")
|
||||
s = "";
|
||||
|
||||
if (s.length() < w)
|
||||
{
|
||||
if (right_aligned[cix])
|
||||
os << std::string(w - s.length(), ' ');
|
||||
os << s;
|
||||
if (not right_aligned[cix])
|
||||
os << std::string(w - s.length(), ' ');
|
||||
}
|
||||
else
|
||||
os << s;
|
||||
}
|
||||
|
||||
os << " |\n";
|
||||
}
|
||||
}
|
||||
|
||||
void category::write_table(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems, bool ascii) const
|
||||
{
|
||||
static constexpr const std::string_view
|
||||
kUnicodeBox[13] = {
|
||||
"┌─", "─┬─", "─┐\n",
|
||||
"├─", "─┼─", "─┤\n",
|
||||
"└─", "─┴─", "─┘\n",
|
||||
"│ ", " │ ", " │\n",
|
||||
"─"
|
||||
},
|
||||
kAsciiBox[13] = { //
|
||||
"+-", "-+-", "-+\n", //
|
||||
"+-", "-+-", "-+\n", //
|
||||
"+-", "-+-", "-+\n", //
|
||||
"| ", " | ", " |\n", //
|
||||
"-"
|
||||
};
|
||||
|
||||
if (empty())
|
||||
return;
|
||||
|
||||
auto box = ascii ? kAsciiBox : kUnicodeBox;
|
||||
|
||||
std::vector<bool> right_aligned(m_items.size(), false);
|
||||
|
||||
if (m_cat_validator != nullptr)
|
||||
{
|
||||
for (auto cix : order)
|
||||
{
|
||||
auto &col = m_items[cix];
|
||||
right_aligned[cix] = col.m_validator != nullptr and
|
||||
col.m_validator->m_type != nullptr and
|
||||
col.m_validator->m_type->m_primitive_type == cif::DDL_PrimitiveType::Numb;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::size_t> itemWidths(m_items.size());
|
||||
|
||||
for (auto cix : order)
|
||||
{
|
||||
auto &col = m_items[cix];
|
||||
itemWidths[cix] = col.m_name.length();
|
||||
}
|
||||
|
||||
for (auto r = m_head; r != nullptr; r = r->m_next)
|
||||
{
|
||||
for (uint16_t ix = 0; ix < r->size(); ++ix)
|
||||
{
|
||||
auto v = r->get(ix);
|
||||
if (v == nullptr)
|
||||
continue;
|
||||
|
||||
size_t l = v->text().length();
|
||||
if (itemWidths[ix] < l)
|
||||
itemWidths[ix] = l;
|
||||
}
|
||||
}
|
||||
|
||||
os << box[0];
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << box[1];
|
||||
for (size_t i = 0; i < itemWidths[cix]; ++i)
|
||||
os << box[12];
|
||||
}
|
||||
os << box[2];
|
||||
|
||||
os << box[9];
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << box[10];
|
||||
|
||||
std::size_t w = itemWidths[cix];
|
||||
std::string_view s = m_items[cix].m_name;
|
||||
|
||||
if (s.length() < w)
|
||||
{
|
||||
int l = (w - s.length()) / 2;
|
||||
int r = w - s.length() - l;
|
||||
os << std::string(l, ' ') << s << std::string(r, ' ');
|
||||
}
|
||||
else
|
||||
os << s;
|
||||
}
|
||||
os << box[11];
|
||||
|
||||
os << box[3];
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << box[4];
|
||||
for (size_t i = 0; i < itemWidths[cix]; ++i)
|
||||
os << box[12];
|
||||
}
|
||||
os << box[5];
|
||||
|
||||
for (auto r = m_head; r != nullptr; r = r->m_next) // loop over rows
|
||||
{
|
||||
os << box[9];
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << box[10];
|
||||
|
||||
std::size_t w = itemWidths[cix];
|
||||
|
||||
std::string_view s;
|
||||
auto iv = r->get(cix);
|
||||
|
||||
if (iv != nullptr)
|
||||
s = iv->text();
|
||||
|
||||
if (s == "?" or s == ".")
|
||||
s = "";
|
||||
|
||||
if (s.length() < w)
|
||||
{
|
||||
if (right_aligned[cix])
|
||||
os << std::string(w - s.length(), ' ');
|
||||
os << s;
|
||||
if (not right_aligned[cix])
|
||||
os << std::string(w - s.length(), ' ');
|
||||
}
|
||||
else
|
||||
os << s;
|
||||
}
|
||||
|
||||
os << box[11];
|
||||
}
|
||||
|
||||
os << box[6];
|
||||
for (bool first = true; uint16_t cix : order)
|
||||
{
|
||||
if (not std::exchange(first, false))
|
||||
os << box[7];
|
||||
for (size_t i = 0; i < itemWidths[cix]; ++i)
|
||||
os << box[12];
|
||||
}
|
||||
os << box[8];
|
||||
}
|
||||
|
||||
bool category::operator==(const category &rhs) const
|
||||
{
|
||||
// shortcut
|
||||
|
||||
119
src/compound.cpp
119
src/compound.cpp
@@ -24,14 +24,38 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "cif++.hpp"
|
||||
#include "cif++/compound.hpp" // for compound_atom, compound_bond, compoun...
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <numeric>
|
||||
#include <shared_mutex>
|
||||
#include "cif++/atom_type.hpp" // for atom_type_traits
|
||||
#include "cif++/category.hpp" // for category
|
||||
#include "cif++/datablock.hpp" // for datablock
|
||||
#include "cif++/file.hpp" // for file
|
||||
#include "cif++/item.hpp" // for item
|
||||
#include "cif++/iterator.hpp" // for iterator_proxy
|
||||
#include "cif++/parser.hpp" // for parser
|
||||
#include "cif++/point.hpp" // for distance, point
|
||||
#include "cif++/row.hpp" // for tie, row_initializer, tie_wrap
|
||||
#include "cif++/text.hpp" // for iequals, replace_all, iset
|
||||
#include "cif++/utilities.hpp" // for load_resource, VERBOSE, colour_type
|
||||
|
||||
#include <algorithm> // for find_if
|
||||
#include <cstddef> // for size_t
|
||||
#include <exception> // for exception, throw_with_nested
|
||||
#include <filesystem> // for path, exists
|
||||
#include <fstream> // for char_traits, basic_ostream, operator<<
|
||||
#include <iomanip> // for operator<<, quoted
|
||||
#include <iostream> // for clog, cout, cerr
|
||||
#include <limits> // for numeric_limits
|
||||
#include <list> // for _List_iterator
|
||||
#include <map> // for allocator, map, _Rb_tree_iterator
|
||||
#include <memory> // for shared_ptr, unique_ptr, __shared_ptr_...
|
||||
#include <optional> // for optional
|
||||
#include <shared_mutex> // for shared_lock, shared_timed_mutex
|
||||
#include <stdexcept> // for runtime_error, invalid_argument, out_...
|
||||
#include <string> // for basic_string, string, operator==, ope...
|
||||
#include <string_view> // for string_view, basic_string_view
|
||||
#include <utility> // for pair, exchange, move
|
||||
#include <vector> // for vector
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@@ -140,7 +164,7 @@ compound::compound(cif::datablock &db)
|
||||
|
||||
cif::tie(m_id, m_name, m_type, m_formula, m_formula_weight, m_formal_charge, one_letter_code, m_parent_id) =
|
||||
chemComp.front().get("id", "name", "type", "formula", "formula_weight", "pdbx_formal_charge", "one_letter_code", "mon_nstd_parent_comp_id");
|
||||
|
||||
|
||||
if (one_letter_code.length() == 1)
|
||||
m_one_letter_code = one_letter_code.front();
|
||||
|
||||
@@ -159,7 +183,7 @@ compound::compound(cif::datablock &db)
|
||||
if (stereo_config.empty())
|
||||
atom.stereo_config = stereo_config_type::N;
|
||||
else
|
||||
atom.stereo_config = parse_stereo_config_from_string(stereo_config);
|
||||
atom.stereo_config = parse_stereo_config_from_string(stereo_config);
|
||||
m_atoms.push_back(std::move(atom));
|
||||
}
|
||||
|
||||
@@ -172,7 +196,7 @@ compound::compound(cif::datablock &db)
|
||||
if (valueOrder.empty())
|
||||
bond.type = bond_type::sing;
|
||||
else
|
||||
bond.type = parse_bond_type_from_string(valueOrder);
|
||||
bond.type = parse_bond_type_from_string(valueOrder);
|
||||
m_bonds.push_back(std::move(bond));
|
||||
}
|
||||
}
|
||||
@@ -231,12 +255,12 @@ float compound::bond_length(const std::string &atomId_1, const std::string &atom
|
||||
|
||||
bool compound::is_peptide() const
|
||||
{
|
||||
return iequals(m_type, "l-peptide linking") or iequals(m_type, "peptide linking");
|
||||
return iequals(m_type, "l-peptide linking") or iequals(m_type, "peptide linking");
|
||||
}
|
||||
|
||||
bool compound::is_base() const
|
||||
{
|
||||
return iequals(m_type, "dna linking") or iequals(m_type, "rna linking");
|
||||
return iequals(m_type, "dna linking") or iequals(m_type, "rna linking");
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
@@ -294,12 +318,31 @@ class compound_factory_impl : public std::enable_shared_from_this<compound_facto
|
||||
delete c;
|
||||
}
|
||||
|
||||
virtual bool exists_self(const std::string &id) const
|
||||
{
|
||||
if (m_missing.contains(id))
|
||||
return false;
|
||||
|
||||
if (std::find_if(m_compounds.begin(), m_compounds.end(), [id](compound *c)
|
||||
{ return c->id() == id; }) != m_compounds.end())
|
||||
return true;
|
||||
|
||||
return m_next and m_next->exists_self(id);
|
||||
}
|
||||
|
||||
bool exists(std::string_view id)
|
||||
{
|
||||
std::shared_lock lock(mMutex);
|
||||
|
||||
return exists_self(std::string{ id });
|
||||
}
|
||||
|
||||
compound *get(std::string id)
|
||||
{
|
||||
std::shared_lock lock(mMutex);
|
||||
|
||||
compound *result = nullptr;
|
||||
|
||||
|
||||
for (auto impl = shared_from_this(); impl; impl = impl->m_next)
|
||||
{
|
||||
result = impl->create(id);
|
||||
@@ -363,7 +406,9 @@ compound *compound_factory_impl::create(const std::string &id)
|
||||
if (m_missing.contains(id))
|
||||
return nullptr;
|
||||
|
||||
if (auto i = find_if(m_compounds.begin(), m_compounds.end(), [id](compound *c) { return c->id() == id; }); i != m_compounds.end())
|
||||
if (auto i = find_if(m_compounds.begin(), m_compounds.end(), [id](compound *c)
|
||||
{ return c->id() == id; });
|
||||
i != m_compounds.end())
|
||||
return *i;
|
||||
|
||||
compound *result = nullptr;
|
||||
@@ -454,7 +499,6 @@ class local_compound_factory_impl : public compound_factory_impl
|
||||
compound *create(const std::string &id) override;
|
||||
|
||||
private:
|
||||
|
||||
compound *construct_compound(const datablock &db, const std::string &id, const std::string &name, const std::string &three_letter_code, const std::string &group);
|
||||
|
||||
cif::file m_local_file;
|
||||
@@ -465,7 +509,9 @@ compound *local_compound_factory_impl::create(const std::string &id)
|
||||
if (m_missing.contains(id))
|
||||
return nullptr;
|
||||
|
||||
if (auto i = find_if(m_compounds.begin(), m_compounds.end(), [id](compound *c) { return c->id() == id; }); i != m_compounds.end())
|
||||
if (auto i = find_if(m_compounds.begin(), m_compounds.end(), [id](compound *c)
|
||||
{ return c->id() == id; });
|
||||
i != m_compounds.end())
|
||||
return *i;
|
||||
|
||||
compound *result = nullptr;
|
||||
@@ -510,12 +556,10 @@ compound *local_compound_factory_impl::construct_compound(const datablock &rdb,
|
||||
|
||||
float formula_weight = 0;
|
||||
int formal_charge = 0;
|
||||
std::map<std::string,std::size_t> formula_data;
|
||||
std::map<std::string, std::size_t> formula_data;
|
||||
|
||||
for (std::size_t ord = 1; const auto &[atom_id, type_symbol, type, charge, x, y, z, xi, yi, zi] :
|
||||
rdb["chem_comp_atom"].rows<std::string, std::string, std::string, int,
|
||||
std::optional<float>, std::optional<float>, std::optional<float>,
|
||||
std::optional<float>, std::optional<float>, std::optional<float>>(
|
||||
rdb["chem_comp_atom"].rows<std::string, std::string, std::string, int, std::optional<float>, std::optional<float>, std::optional<float>, std::optional<float>, std::optional<float>, std::optional<float>>(
|
||||
"atom_id", "type_symbol", "type", "charge",
|
||||
"model_Cartn_x", "model_Cartn_y", "model_Cartn_z",
|
||||
"pdbx_model_Cartn_x_ideal", "pdbx_model_Cartn_y_ideal", "pdbx_model_Cartn_z_ideal"))
|
||||
@@ -525,16 +569,14 @@ compound *local_compound_factory_impl::construct_compound(const datablock &rdb,
|
||||
|
||||
formula_data[type_symbol] += 1;
|
||||
|
||||
db["chem_comp_atom"].emplace({
|
||||
{ "comp_id", id },
|
||||
db["chem_comp_atom"].emplace({ { "comp_id", id },
|
||||
{ "atom_id", atom_id },
|
||||
{ "type_symbol", type_symbol },
|
||||
{ "charge", charge },
|
||||
{ "model_Cartn_x", x.has_value() ? x : xi, 3 },
|
||||
{ "model_Cartn_y", y.has_value() ? y : yi, 3 },
|
||||
{ "model_Cartn_z", z.has_value() ? z : zi, 3 },
|
||||
{ "pdbx_ordinal", ord++ }
|
||||
});
|
||||
{ "model_Cartn_x", x.has_value() ? x : xi, 3 },
|
||||
{ "model_Cartn_y", y.has_value() ? y : yi, 3 },
|
||||
{ "model_Cartn_z", z.has_value() ? z : zi, 3 },
|
||||
{ "pdbx_ordinal", ord++ } });
|
||||
|
||||
formal_charge += charge;
|
||||
}
|
||||
@@ -551,21 +593,19 @@ compound *local_compound_factory_impl::construct_compound(const datablock &rdb,
|
||||
else if (cif::iequals(type, "triple") or cif::iequals(type, "trip"))
|
||||
value_order = "TRIP";
|
||||
|
||||
db["chem_comp_bond"].emplace({
|
||||
{ "comp_id", id },
|
||||
db["chem_comp_bond"].emplace({ { "comp_id", id },
|
||||
{ "atom_id_1", atom_id_1 },
|
||||
{ "atom_id_2", atom_id_2 },
|
||||
{ "value_order", value_order },
|
||||
{ "pdbx_aromatic_flag", aromatic },
|
||||
// TODO: fetch stereo_config info from chem_comp_chir
|
||||
{ "pdbx_ordinal", ord++ }
|
||||
});
|
||||
{ "pdbx_ordinal", ord++ } });
|
||||
}
|
||||
|
||||
db.emplace_back(rdb["pdbx_chem_comp_descriptor"]);
|
||||
|
||||
std::string formula;
|
||||
for (bool first = true; const auto &[symbol, count]: formula_data)
|
||||
for (bool first = true; const auto &[symbol, count] : formula_data)
|
||||
{
|
||||
if (std::exchange(first, false))
|
||||
formula += ' ';
|
||||
@@ -584,15 +624,13 @@ compound *local_compound_factory_impl::construct_compound(const datablock &rdb,
|
||||
else
|
||||
type = "NON-POLYMER";
|
||||
|
||||
db["chem_comp"].emplace({
|
||||
{ "id", id },
|
||||
db["chem_comp"].emplace({ { "id", id },
|
||||
{ "name", name },
|
||||
{ "type", type },
|
||||
{ "formula", formula },
|
||||
{ "pdbx_formal_charge", formal_charge },
|
||||
{ "formula_weight", formula_weight },
|
||||
{ "three_letter_code", three_letter_code }
|
||||
});
|
||||
{ "three_letter_code", three_letter_code } });
|
||||
|
||||
std::shared_lock lock(mMutex);
|
||||
|
||||
@@ -698,6 +736,11 @@ void compound_factory::pop_dictionary()
|
||||
m_impl = m_impl->next();
|
||||
}
|
||||
|
||||
bool compound_factory::exists(std::string_view id) const
|
||||
{
|
||||
return m_impl and m_impl->exists(id);
|
||||
}
|
||||
|
||||
const compound *compound_factory::create(std::string_view id)
|
||||
{
|
||||
auto result = m_impl ? m_impl->get(std::string{ id }) : nullptr;
|
||||
@@ -722,7 +765,7 @@ bool compound_factory::is_peptide(std::string_view res_name) const
|
||||
bool result = is_std_peptide(res_name);
|
||||
if (not result and m_impl)
|
||||
{
|
||||
auto compound = const_cast<compound_factory&>(*this).create(res_name);
|
||||
auto compound = const_cast<compound_factory &>(*this).create(res_name);
|
||||
result = compound != nullptr and compound->is_peptide();
|
||||
}
|
||||
return result;
|
||||
@@ -734,7 +777,7 @@ bool compound_factory::is_base(std::string_view res_name) const
|
||||
bool result = is_std_base(res_name);
|
||||
if (not result and m_impl)
|
||||
{
|
||||
auto compound = const_cast<compound_factory&>(*this).create(res_name);
|
||||
auto compound = const_cast<compound_factory &>(*this).create(res_name);
|
||||
result = compound != nullptr and compound->is_base();
|
||||
}
|
||||
return result;
|
||||
|
||||
1102
src/cql.cpp
Normal file
1102
src/cql.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -25,8 +25,11 @@
|
||||
*/
|
||||
|
||||
#include "cif++/datablock.hpp"
|
||||
|
||||
#include "cif++/validate.hpp"
|
||||
|
||||
#include <exception>
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
@@ -42,7 +45,28 @@ datablock::datablock(const datablock &db)
|
||||
void datablock::load_dictionary()
|
||||
{
|
||||
if (auto *audit_conform = get("audit_conform"); audit_conform and not audit_conform->empty())
|
||||
set_validator(&validator_factory::instance().get(*audit_conform));
|
||||
{
|
||||
try
|
||||
{
|
||||
set_validator(validator_factory::instance().get(*audit_conform));
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
std::clog << ex.what() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void datablock::load_dictionary(std::string_view dict)
|
||||
{
|
||||
try
|
||||
{
|
||||
set_validator(validator_factory::instance().get(dict));
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
std::clog << ex.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
void datablock::set_validator(const validator *v)
|
||||
@@ -96,7 +120,8 @@ bool datablock::strip()
|
||||
bool result = true;
|
||||
|
||||
// remove all categories that have no validator
|
||||
erase(std::remove_if(begin(), end(), [](category &c) {
|
||||
erase(std::remove_if(begin(), end(), [](category &c)
|
||||
{
|
||||
bool result = false;
|
||||
if (c.get_cat_validator() == nullptr)
|
||||
{
|
||||
@@ -104,8 +129,8 @@ bool datablock::strip()
|
||||
std::clog << "Dropping category " << c.name() << '\n';
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}), end());
|
||||
return result; }),
|
||||
end());
|
||||
|
||||
// then strip the remaining categories
|
||||
for (auto &cat : *this)
|
||||
|
||||
@@ -24,7 +24,9 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "cif++/model.hpp"
|
||||
#include "cif++.hpp"
|
||||
#include "cif++/point.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
@@ -32,6 +34,7 @@
|
||||
#include <iomanip>
|
||||
#include <numeric>
|
||||
#include <stack>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@@ -349,17 +352,7 @@ std::tuple<point, float> residue::center_and_radius() const
|
||||
for (auto &a : m_atoms)
|
||||
pts.push_back(a.get_location());
|
||||
|
||||
auto center = centroid(pts);
|
||||
float radius = 0;
|
||||
|
||||
for (auto &pt : pts)
|
||||
{
|
||||
float d = static_cast<float>(distance(pt, center));
|
||||
if (radius < d)
|
||||
radius = d;
|
||||
}
|
||||
|
||||
return std::make_tuple(center, radius);
|
||||
return smallest_sphere_around_points(pts);
|
||||
}
|
||||
|
||||
bool residue::has_alternate_atoms() const
|
||||
@@ -2346,6 +2339,36 @@ std::string structure::create_non_poly(const std::string &entity_id, std::vector
|
||||
return asym_id;
|
||||
}
|
||||
|
||||
std::string structure::create_non_poly(const std::string &compound_id, bool skip_hydrogen)
|
||||
{
|
||||
auto compound = cif::compound_factory::instance().create(compound_id);
|
||||
if (compound == nullptr)
|
||||
throw std::runtime_error(std::format("{} is not a known compound", compound_id));
|
||||
|
||||
std::vector<cif::row_initializer> atoms;
|
||||
for (auto a : compound->atoms())
|
||||
{
|
||||
// We skip H-atoms, as fitting without H-atoms works better and we avoid conflicts in protonation states between CCD and MONLIB
|
||||
if (skip_hydrogen and cif::atom_type_traits(a.type_symbol).symbol() == "H")
|
||||
continue;
|
||||
|
||||
auto ax = a.get_location().get_x();
|
||||
auto ay = a.get_location().get_y();
|
||||
auto az = a.get_location().get_z();
|
||||
|
||||
atoms.emplace_back(cif::row_initializer{
|
||||
{ "type_symbol", cif::atom_type_traits(a.type_symbol).symbol() },
|
||||
{ "label_atom_id", a.id },
|
||||
{ "auth_atom_id", a.id },
|
||||
{ "Cartn_x", ax },
|
||||
{ "Cartn_y", ay },
|
||||
{ "Cartn_z", az },
|
||||
{ "B_iso_or_equiv", 30.00 } });
|
||||
}
|
||||
|
||||
return create_non_poly(create_non_poly_entity(compound_id), atoms);
|
||||
}
|
||||
|
||||
void structure::create_water(row_initializer atom)
|
||||
{
|
||||
using namespace literals;
|
||||
@@ -2898,8 +2921,8 @@ static int compare_numbers(std::string_view a, std::string_view b)
|
||||
|
||||
std::from_chars_result ra, rb;
|
||||
|
||||
ra = selected_charconv<double>::from_chars(a.data(), a.data() + a.length(), da);
|
||||
rb = selected_charconv<double>::from_chars(b.data(), b.data() + b.length(), db);
|
||||
ra = from_chars(a.data(), a.data() + a.length(), da);
|
||||
rb = from_chars(b.data(), b.data() + b.length(), db);
|
||||
|
||||
if (not(bool) ra.ec and not(bool) rb.ec)
|
||||
{
|
||||
|
||||
@@ -345,6 +345,7 @@ sac_parser::CIFToken sac_parser::get_next_token()
|
||||
{
|
||||
retract();
|
||||
result = CIFToken::VALUE;
|
||||
m_token_value = "?";
|
||||
}
|
||||
else
|
||||
state = State::Value;
|
||||
|
||||
@@ -26,21 +26,17 @@
|
||||
|
||||
#include "pdb2cif_remark_3.hpp"
|
||||
|
||||
#include "cif++.hpp"
|
||||
#include <cif++/compound.hpp>
|
||||
#include <cif++/gzio.hpp>
|
||||
#include <cif++/model.hpp>
|
||||
#include <cif++/pdb.hpp>
|
||||
#include <cif++/symmetry.hpp>
|
||||
|
||||
#include <iomanip>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
#include <stdexcept>
|
||||
|
||||
using cif::category;
|
||||
using cif::datablock;
|
||||
using cif::iequals;
|
||||
using cif::key;
|
||||
using cif::to_lower;
|
||||
using cif::to_lower_copy;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// attempt to come up with better error handling
|
||||
|
||||
@@ -3142,7 +3138,6 @@ void PDBFileParser::ParseRemark350()
|
||||
std::map<std::string, std::string> values;
|
||||
std::vector<std::string> asymIdList;
|
||||
std::smatch m;
|
||||
cif::row_handle genR;
|
||||
|
||||
std::vector<double> mat, vec;
|
||||
|
||||
@@ -6375,7 +6370,7 @@ void read_pdb_file(std::istream &pdbFile, cif::file &cifFile)
|
||||
{
|
||||
cifFile.front().load_dictionary();
|
||||
if (cifFile.front().get_validator() == nullptr)
|
||||
cifFile.front().set_validator(&validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
cifFile.front().set_validator(validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
|
||||
if (not cifFile.is_valid() and cif::VERBOSE >= 0)
|
||||
std::cerr << "Resulting mmCIF file is not valid!\n";
|
||||
@@ -6445,7 +6440,7 @@ file read(std::istream &is)
|
||||
{
|
||||
auto &db = result.front();
|
||||
if (db.get_validator() == nullptr)
|
||||
db.set_validator(&validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
db.set_validator(validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
if (db.is_valid())
|
||||
db.get_validator()->fill_audit_conform(db["audit_conform"]);
|
||||
}
|
||||
|
||||
@@ -1478,7 +1478,7 @@ bool Remark3Parser::parse(const std::string &expMethod, PDBRecord *r, cif::datab
|
||||
|
||||
best.parser->fixup();
|
||||
|
||||
auto &validator = cif::validator_factory::instance().get("mmcif_pdbx.dic");
|
||||
auto &validator = cif::validator_factory::instance()["mmcif_pdbx.dic"];
|
||||
|
||||
for (auto &cat1 : best.parser->mDb)
|
||||
{
|
||||
|
||||
@@ -295,11 +295,13 @@ void createEntityIDs(datablock &db)
|
||||
{ "id", entity_id },
|
||||
{ "type", "water" } });
|
||||
else
|
||||
{
|
||||
entity.emplace({ //
|
||||
{ "id", entity_id },
|
||||
{ "type", "non-polymer" } });
|
||||
|
||||
newEntitiesForCompound[comp_id] = entity_id;
|
||||
newEntitiesForCompound[comp_id] = entity_id;
|
||||
}
|
||||
}
|
||||
|
||||
entity_ids[i] = entity_id;
|
||||
@@ -648,7 +650,7 @@ void checkAtomRecords(datablock &db)
|
||||
{
|
||||
char b[12];
|
||||
|
||||
if (auto [ptr, ec] = cif::to_chars(b, b + sizeof(b), v, cif::chars_format::fixed, prec); ec == std::errc{})
|
||||
if (auto [ptr, ec] = std::to_chars(b, b + sizeof(b), v, std::chars_format::fixed, prec); ec == std::errc{})
|
||||
row.assign(item_name, { b, static_cast<std::string::size_type>(ptr - b) }, false, false);
|
||||
}
|
||||
}
|
||||
@@ -1462,9 +1464,9 @@ bool reconstruct_pdbx(file &file)
|
||||
auto &db = file.front();
|
||||
|
||||
if (auto ac = db.get("audit_conform"); ac != nullptr)
|
||||
return reconstruct_pdbx(file, validator_factory::instance().get(*ac));
|
||||
return reconstruct_pdbx(file, validator_factory::instance()[*ac]);
|
||||
else
|
||||
return reconstruct_pdbx(file, validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
return reconstruct_pdbx(file, validator_factory::instance()["mmcif_pdbx.dic"]);
|
||||
}
|
||||
|
||||
bool reconstruct_pdbx(file &file, const validator &validator)
|
||||
@@ -1687,9 +1689,9 @@ void fixup_pdbx(file &file)
|
||||
auto &db = file.front();
|
||||
|
||||
if (auto ac = db.get("audit_conform"); ac != nullptr)
|
||||
fixup_pdbx(file, validator_factory::instance().get(*ac));
|
||||
fixup_pdbx(file, validator_factory::instance()[*ac]);
|
||||
else
|
||||
fixup_pdbx(file, validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
fixup_pdbx(file, validator_factory::instance()["mmcif_pdbx.dic"]);
|
||||
}
|
||||
|
||||
void fixup_pdbx(file &file, const validator &validator)
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
*/
|
||||
|
||||
#include "cif++.hpp"
|
||||
#include "cif++/validate.hpp"
|
||||
|
||||
namespace cif::pdb
|
||||
{
|
||||
@@ -65,6 +66,13 @@ condition get_parents_condition(const validator &validator, row_handle rh, const
|
||||
return result;
|
||||
}
|
||||
|
||||
bool is_valid_pdbx_file(const file &file)
|
||||
{
|
||||
std::error_code ec;
|
||||
bool result = is_valid_pdbx_file(file, validator_factory::instance()["mmcif_pdbx.dic"], ec);
|
||||
return result and not(bool) ec;
|
||||
}
|
||||
|
||||
bool is_valid_pdbx_file(const file &file, const validator &v)
|
||||
{
|
||||
std::error_code ec;
|
||||
@@ -79,9 +87,9 @@ bool is_valid_pdbx_file(const file &file, std::error_code &ec)
|
||||
if (file.empty())
|
||||
ec = make_error_code(validation_error::empty_file);
|
||||
else if (auto ac = file.front().get("audit_conform"); ac != nullptr)
|
||||
result = is_valid_pdbx_file(file, validator_factory::instance().get(*ac), ec);
|
||||
result = is_valid_pdbx_file(file, validator_factory::instance()[*ac], ec);
|
||||
else
|
||||
result = is_valid_pdbx_file(file, validator_factory::instance().get("mmcif_pdbx.dic"), ec);
|
||||
result = is_valid_pdbx_file(file, validator_factory::instance()["mmcif_pdbx.dic"], ec);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
266
src/point.cpp
266
src/point.cpp
@@ -25,17 +25,19 @@
|
||||
*/
|
||||
|
||||
#include "cif++/point.hpp"
|
||||
#include "cif++/matrix.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <random>
|
||||
#include "cif++/matrix.hpp" // for matrix_subtraction, matrix_cofactors
|
||||
|
||||
#include <initializer_list>
|
||||
#include <random> // for uniform_real_distribution, normal_distri...
|
||||
#include <stdexcept>
|
||||
|
||||
namespace cif
|
||||
{
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
quaternion_type<T> normalize(quaternion_type<T> q)
|
||||
{
|
||||
std::valarray<double> t(4);
|
||||
@@ -126,10 +128,9 @@ quaternion construct_for_dihedral_angle(point p1, point p2, point p3, point p4,
|
||||
p4 -= p3;
|
||||
p3 -= p3;
|
||||
|
||||
quaternion q;
|
||||
auto axis = -p2;
|
||||
|
||||
float dh = dihedral_angle(p1, p2, p3, p4);
|
||||
|
||||
return construct_from_angle_axis(angle - dh, axis);
|
||||
}
|
||||
|
||||
@@ -293,9 +294,9 @@ quaternion align_points(const std::vector<point> &pa, const std::vector<point> &
|
||||
}
|
||||
|
||||
quaternion q(
|
||||
static_cast<float>(cf(maxR, 0)),
|
||||
static_cast<float>(cf(maxR, 1)),
|
||||
static_cast<float>(cf(maxR, 2)),
|
||||
static_cast<float>(cf(maxR, 0)),
|
||||
static_cast<float>(cf(maxR, 1)),
|
||||
static_cast<float>(cf(maxR, 2)),
|
||||
static_cast<float>(cf(maxR, 3)));
|
||||
q = normalize(q);
|
||||
|
||||
@@ -327,4 +328,251 @@ point nudge(point p, float offset)
|
||||
return p + r;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
std::tuple<point, float> smallest_sphere_around_2_points(std::array<cif::point, 2> pts)
|
||||
{
|
||||
return { (pts[0] + pts[1]) / 2, distance(pts[0], pts[1]) / 2 };
|
||||
}
|
||||
|
||||
std::tuple<point, float> smallest_sphere_around_3_points(std::array<cif::point, 3> pts)
|
||||
{
|
||||
// Find two bisectors
|
||||
auto vz = cross_product(pts[1] - pts[0], pts[2] - pts[0]);
|
||||
|
||||
auto bs1 = cross_product(vz, pts[1] - pts[0]);
|
||||
bs1.normalize();
|
||||
|
||||
auto v1 = (pts[1] - pts[0]);
|
||||
v1.normalize();
|
||||
|
||||
auto s1 = pts[0] + (distance(pts[1], pts[0]) / 2) * v1;
|
||||
|
||||
auto bs2 = cross_product(vz, pts[2] - pts[0]);
|
||||
bs2.normalize();
|
||||
|
||||
auto v2 = (pts[2] - pts[0]);
|
||||
v2.normalize();
|
||||
|
||||
auto s2 = pts[0] + (distance(pts[2], pts[0]) / 2) * v2;
|
||||
|
||||
auto c = line_line_intersection(s1, s1 + bs1, s2, s2 + bs2);
|
||||
if (c)
|
||||
return { *c, distance(*c, pts[0]) };
|
||||
|
||||
// Colinear points I guess, try something else
|
||||
auto l1 = distance_squared(pts[0], pts[1]);
|
||||
auto l2 = distance_squared(pts[0], pts[2]);
|
||||
auto l3 = distance_squared(pts[1], pts[2]);
|
||||
|
||||
if (l1 > l2 and l1 > l3)
|
||||
return smallest_sphere_around_2_points({ pts[0], pts[1] });
|
||||
else if (l2 > l1 and l2 > l3)
|
||||
return smallest_sphere_around_2_points({ pts[0], pts[2] });
|
||||
else
|
||||
return smallest_sphere_around_2_points({ pts[1], pts[2] });
|
||||
}
|
||||
|
||||
std::tuple<point, float> smallest_sphere_around_4_points(std::array<cif::point, 4> pts)
|
||||
{
|
||||
auto t0 = -norm_squared(pts[0]);
|
||||
auto t1 = -norm_squared(pts[1]);
|
||||
auto t2 = -norm_squared(pts[2]);
|
||||
auto t3 = -norm_squared(pts[3]);
|
||||
|
||||
// clang-format off
|
||||
matrix4x4<float> Tm({
|
||||
pts[0].m_x, pts[0].m_y, pts[0].m_z, 1,
|
||||
pts[1].m_x, pts[1].m_y, pts[1].m_z, 1,
|
||||
pts[2].m_x, pts[2].m_y, pts[2].m_z, 1,
|
||||
pts[3].m_x, pts[3].m_y, pts[3].m_z, 1
|
||||
});
|
||||
auto T = determinant(Tm);
|
||||
|
||||
if (T != 0)
|
||||
{
|
||||
matrix4x4<float> Dm({
|
||||
t0, pts[0].m_y, pts[0].m_z, 1,
|
||||
t1, pts[1].m_y, pts[1].m_z, 1,
|
||||
t2, pts[2].m_y, pts[2].m_z, 1,
|
||||
t3, pts[3].m_y, pts[3].m_z, 1
|
||||
});
|
||||
auto D = determinant(Dm) / T;
|
||||
|
||||
matrix4x4<float> Em({
|
||||
pts[0].m_x, t0, pts[0].m_z, 1,
|
||||
pts[1].m_x, t1, pts[1].m_z, 1,
|
||||
pts[2].m_x, t2, pts[2].m_z, 1,
|
||||
pts[3].m_x, t3, pts[3].m_z, 1
|
||||
});
|
||||
auto E = determinant(Em) / T;
|
||||
|
||||
matrix4x4<float> Fm({
|
||||
pts[0].m_x, pts[0].m_y, t0, 1,
|
||||
pts[1].m_x, pts[1].m_y, t1, 1,
|
||||
pts[2].m_x, pts[2].m_y, t2, 1,
|
||||
pts[3].m_x, pts[3].m_y, t3, 1
|
||||
});
|
||||
|
||||
auto F = determinant(Fm) / T;
|
||||
|
||||
matrix4x4<float> Gm({
|
||||
pts[0].m_x, pts[0].m_y, pts[0].m_z, t0,
|
||||
pts[1].m_x, pts[1].m_y, pts[1].m_z, t1,
|
||||
pts[2].m_x, pts[2].m_y, pts[2].m_z, t2,
|
||||
pts[3].m_x, pts[3].m_y, pts[3].m_z, t3
|
||||
});
|
||||
auto G = determinant(Gm) / T;
|
||||
|
||||
point center{ -D / 2, -E / 2, -F / 2 };
|
||||
float radius = std::sqrt(D * D + E * E + F * F - 4 * G) / 2;
|
||||
|
||||
// clang-format on
|
||||
|
||||
return { center, radius };
|
||||
}
|
||||
|
||||
// Perhaps some colinear points, try something else:
|
||||
|
||||
for (auto ix : std::initializer_list<std::array<size_t, 4>>{
|
||||
{ 1, 2, 3, 0 },
|
||||
{ 0, 2, 3, 1 },
|
||||
{ 0, 1, 3, 2 },
|
||||
{ 0, 1, 2, 3 },
|
||||
})
|
||||
{
|
||||
auto [center, radius] =
|
||||
smallest_sphere_around_3_points({ pts[ix[0]], pts[ix[1]], pts[ix[2]] });
|
||||
|
||||
if (distance(pts[ix[3]], center) <= radius)
|
||||
return { center, radius };
|
||||
}
|
||||
|
||||
assert(false);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
std::tuple<point, float> smallest_sphere_around_all_points(std::vector<point> P, std::vector<point> R)
|
||||
{
|
||||
if (P.empty() or R.size() == 4)
|
||||
{
|
||||
switch (R.size())
|
||||
{
|
||||
case 1:
|
||||
return { R[0], 0 };
|
||||
|
||||
case 2:
|
||||
return smallest_sphere_around_2_points({ R[0], R[1] });
|
||||
|
||||
case 3:
|
||||
return smallest_sphere_around_3_points({ R[0], R[1], R[2] });
|
||||
|
||||
case 4:
|
||||
return smallest_sphere_around_4_points({ R[0], R[1], R[2], R[3] });
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
auto p = P.back();
|
||||
P.pop_back();
|
||||
|
||||
auto [c, r] = smallest_sphere_around_all_points(P, R);
|
||||
assert(not std::isnan(r));
|
||||
if (distance(c, p) <= r)
|
||||
return { c, r };
|
||||
|
||||
R.emplace_back(p);
|
||||
return smallest_sphere_around_all_points(P, R);
|
||||
}
|
||||
|
||||
bool point_in_circle(point p, std::vector<point> c)
|
||||
{
|
||||
switch (c.size())
|
||||
{
|
||||
case 0:
|
||||
return false;
|
||||
|
||||
case 1:
|
||||
return p == c.front();
|
||||
|
||||
case 2:
|
||||
{
|
||||
auto [center, radius] = smallest_sphere_around_2_points({ c[0], c[1] });
|
||||
return cif::distance_squared(p, center) <= radius * radius;
|
||||
}
|
||||
|
||||
case 3:
|
||||
{
|
||||
auto [center, radius] = smallest_sphere_around_3_points({ c[0], c[1], c[2] });
|
||||
return cif::distance_squared(p, center) <= radius * radius;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
auto [center, radius] = smallest_sphere_around_4_points({ c[0], c[1], c[2], c[3] });
|
||||
return cif::distance_squared(p, center) <= radius * radius;
|
||||
}
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
throw std::runtime_error("Error finding smallest sphere");
|
||||
}
|
||||
}
|
||||
|
||||
std::tuple<point, float> smallest_sphere_around_points(std::vector<point> pts)
|
||||
{
|
||||
std::random_device rd;
|
||||
std::mt19937 g(rd());
|
||||
|
||||
std::shuffle(pts.begin(), pts.end(), g);
|
||||
|
||||
std::vector<size_t> cix;
|
||||
|
||||
auto cirle_points = [&]()
|
||||
{
|
||||
std::vector<point> result;
|
||||
for (auto ix : cix)
|
||||
result.emplace_back(pts[ix]);
|
||||
return result;
|
||||
};
|
||||
|
||||
size_t i = 0;
|
||||
while (i < pts.size())
|
||||
{
|
||||
if (std::find(cix.begin(), cix.end(), i) != cix.end() or
|
||||
point_in_circle(pts[i], cirle_points()))
|
||||
{
|
||||
++i;
|
||||
}
|
||||
else
|
||||
{
|
||||
cix.erase(std::remove_if(cix.begin(), cix.end(), [i](size_t j)
|
||||
{ return j < i; }),
|
||||
cix.end());
|
||||
cix.push_back(i);
|
||||
if (cix.size() < 4)
|
||||
i = 0;
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
switch (cix.size())
|
||||
{
|
||||
case 1:
|
||||
return { pts[cix[0]], 0 };
|
||||
case 2:
|
||||
return smallest_sphere_around_2_points({ pts[cix[0]], pts[cix[1]] });
|
||||
case 3:
|
||||
return smallest_sphere_around_3_points({ pts[cix[0]], pts[cix[1]], pts[cix[2]] });
|
||||
case 4:
|
||||
return smallest_sphere_around_4_points({ pts[cix[0]], pts[cix[1]], pts[cix[2]], pts[cix[3]] });
|
||||
default:
|
||||
assert(false);
|
||||
throw std::runtime_error("Error finding smallest sphere");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace cif
|
||||
|
||||
265928
src/sqlite3/sqlite3.c
Normal file
265928
src/sqlite3/sqlite3.c
Normal file
File diff suppressed because it is too large
Load Diff
13972
src/sqlite3/sqlite3.h
Normal file
13972
src/sqlite3/sqlite3.h
Normal file
File diff suppressed because it is too large
Load Diff
33
src/text.cpp
33
src/text.cpp
@@ -28,6 +28,11 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <charconv>
|
||||
|
||||
#if __has_include("fast_float/fast_float.h")
|
||||
#include "fast_float/fast_float.h"
|
||||
#endif
|
||||
|
||||
namespace cif
|
||||
{
|
||||
@@ -512,4 +517,32 @@ std::vector<std::string> word_wrap(const std::string &text, std::size_t width)
|
||||
return result;
|
||||
}
|
||||
|
||||
#if __has_include("fast_float/fast_float.h")
|
||||
|
||||
template <typename T>
|
||||
std::from_chars_result ff_charconv<T, typename std::enable_if_t<std::is_floating_point_v<T>>>::from_chars(const char *a, const char *b, T &v)
|
||||
{
|
||||
auto r = fast_float::from_chars(a, b, v);
|
||||
return { r.ptr, r.ec };
|
||||
}
|
||||
|
||||
template struct ff_charconv<float>;
|
||||
template struct ff_charconv<double>;
|
||||
// template struct ff_charconv<long double>;
|
||||
|
||||
#ifdef __STDCPP_FLOAT64_T__
|
||||
template struct ff_charconv<std::float64_t>;
|
||||
#endif
|
||||
#ifdef __STDCPP_FLOAT32_T__
|
||||
template struct ff_charconv<std::float32_t>;
|
||||
#endif
|
||||
#ifdef __STDCPP_FLOAT16_T__
|
||||
template struct ff_charconv<std::float16_t>;
|
||||
#endif
|
||||
#ifdef __STDCPP_BFLOAT16_T__
|
||||
template struct ff_charconv<std::bfloat16_t>;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace cif
|
||||
@@ -25,14 +25,17 @@
|
||||
*/
|
||||
|
||||
#include "cif++/validate.hpp"
|
||||
|
||||
#include "cif++/category.hpp"
|
||||
#include "cif++/dictionary_parser.hpp"
|
||||
#include "cif++/gzio.hpp"
|
||||
#include "cif++/utilities.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <format>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
|
||||
// The validator depends on regular expressions. Unfortunately,
|
||||
// the implementation of std::regex in g++ is buggy and crashes
|
||||
@@ -75,6 +78,7 @@ struct regex_impl
|
||||
private:
|
||||
pcre2_code *m_rx = nullptr;
|
||||
pcre2_match_data *m_data = nullptr;
|
||||
mutable std::mutex m_mutex;
|
||||
};
|
||||
|
||||
regex_impl::regex_impl(std::string_view rx)
|
||||
@@ -95,6 +99,8 @@ regex_impl::regex_impl(std::string_view rx)
|
||||
|
||||
regex_impl::~regex_impl()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
|
||||
if (m_data)
|
||||
pcre2_match_data_free(m_data);
|
||||
|
||||
@@ -104,6 +110,8 @@ regex_impl::~regex_impl()
|
||||
|
||||
bool regex_impl::match(std::string_view v) const
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
|
||||
bool result = false;
|
||||
|
||||
if (int rc = pcre2_match(m_rx, (PCRE2_SPTR)v.data(), v.length(), 0, 0, m_data, nullptr); rc >= 0)
|
||||
@@ -181,8 +189,8 @@ int type_validator::compare(std::string_view a, std::string_view b) const
|
||||
|
||||
std::from_chars_result ra, rb;
|
||||
|
||||
ra = selected_charconv<double>::from_chars(a.data(), a.data() + a.length(), da);
|
||||
rb = selected_charconv<double>::from_chars(b.data(), b.data() + b.length(), db);
|
||||
ra = from_chars(a.data(), a.data() + a.length(), da);
|
||||
rb = from_chars(b.data(), b.data() + b.length(), db);
|
||||
|
||||
if (not(bool) ra.ec and not(bool) rb.ec)
|
||||
{
|
||||
@@ -263,7 +271,7 @@ void item_validator::operator()(std::string_view value) const
|
||||
{
|
||||
std::error_code ec;
|
||||
if (not validate_value(value, ec))
|
||||
throw std::system_error(ec, std::string{ value } + " does not match rx for " + m_item_name);
|
||||
throw std::system_error(ec, std::format("'{}' is not a valid value for {}", value, m_item_name));
|
||||
}
|
||||
|
||||
bool item_validator::validate_value(std::string_view value, std::error_code &ec) const noexcept
|
||||
@@ -290,15 +298,20 @@ void category_validator::add_item_validator(item_validator &&v)
|
||||
|
||||
v.m_category = m_name;
|
||||
|
||||
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_item_name << " to category " << m_name << '\n';
|
||||
auto i = std::find(m_item_validators.begin(), m_item_validators.end(), v);
|
||||
if (i != m_item_validators.end())
|
||||
{
|
||||
if (VERBOSE >= 4)
|
||||
std::cout << "Could not add validator for item " << v.m_item_name << " to category " << m_name << '\n';
|
||||
}
|
||||
else
|
||||
m_item_validators.emplace_back(std::move(v));
|
||||
}
|
||||
|
||||
const item_validator *category_validator::get_validator_for_item(std::string_view item_name) const
|
||||
{
|
||||
const item_validator *result = nullptr;
|
||||
auto i = m_item_validators.find(item_validator{ std::string(item_name) });
|
||||
auto i = std::find(m_item_validators.begin(), m_item_validators.end(), item_validator{ std::string(item_name) });
|
||||
if (i != m_item_validators.end())
|
||||
result = &*i;
|
||||
else if (VERBOSE > 4)
|
||||
@@ -535,19 +548,18 @@ validator_factory &validator_factory::instance()
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
const validator &validator_factory::get(std::string_view dictionary_name)
|
||||
const validator *validator_factory::get(std::string_view dictionary_name)
|
||||
{
|
||||
category audit_conform("audit_conform");
|
||||
for (auto part : cif::split(dictionary_name, ";", true))
|
||||
for (auto part : cif::split(dictionary_name, ";,", true))
|
||||
audit_conform.emplace({ { "dict_name", part } });
|
||||
|
||||
return get(audit_conform);
|
||||
}
|
||||
|
||||
const validator &validator_factory::get(const category &audit_conform)
|
||||
const validator *validator_factory::get(const category &audit_conform)
|
||||
{
|
||||
if (audit_conform.empty())
|
||||
throw std::runtime_error("Empty audit_conform category, cannot create a validator");
|
||||
const validator *result = nullptr;
|
||||
|
||||
std::lock_guard lock(m_mutex);
|
||||
|
||||
@@ -555,37 +567,60 @@ const validator &validator_factory::get(const category &audit_conform)
|
||||
for (auto &v : m_validators)
|
||||
{
|
||||
if (v.matches_audit_conform(audit_conform))
|
||||
return v;
|
||||
result = &v;
|
||||
}
|
||||
|
||||
// If the audit conform contains only one record, this is easy
|
||||
if (audit_conform.size() == 1)
|
||||
if (result == nullptr and audit_conform.size() == 1)
|
||||
{
|
||||
const auto &[name, version] = audit_conform.front().get<std::string, std::optional<std::string>>("dict_name", "dict_version");
|
||||
return m_validators.emplace_back(construct_validator(name, version));
|
||||
const auto &[name, version] =
|
||||
audit_conform.front().get<std::string, std::optional<std::string>>("dict_name", "dict_version");
|
||||
if (not name.empty())
|
||||
result = &m_validators.emplace_back(construct_validator(name, version));
|
||||
}
|
||||
|
||||
// A new, merged dictionary
|
||||
|
||||
std::optional<validator> v;
|
||||
for (const auto &[name, version] : audit_conform.rows<std::string, std::optional<std::string>>("dict_name", "dict_version"))
|
||||
if (result == nullptr)
|
||||
{
|
||||
if (not v)
|
||||
v = construct_validator(name, version);
|
||||
else
|
||||
// A new, merged dictionary
|
||||
std::optional<validator> v;
|
||||
for (const auto &[name, version] : audit_conform.rows<std::string, std::optional<std::string>>("dict_name", "dict_version"))
|
||||
{
|
||||
auto data = load_resource(name);
|
||||
if (not data)
|
||||
throw std::runtime_error("Could not load dictionary " + std::string{ name });
|
||||
if (name.empty())
|
||||
continue;
|
||||
|
||||
v->parse(*data);
|
||||
if (not v) // first dict
|
||||
v = construct_validator(name, version);
|
||||
else // additional/extending dict
|
||||
{
|
||||
auto data = load_resource(name);
|
||||
if (not data)
|
||||
throw std::runtime_error("Could not load dictionary " + std::string{ name });
|
||||
|
||||
v->parse(*data);
|
||||
}
|
||||
}
|
||||
|
||||
if (v)
|
||||
result = &m_validators.emplace_back(std::move(*v));
|
||||
}
|
||||
|
||||
if (not v)
|
||||
throw std::runtime_error("Missing dictionary information?");
|
||||
return result;
|
||||
}
|
||||
|
||||
return m_validators.emplace_back(std::move(*v));
|
||||
const validator &validator_factory::operator[](const category &audit_conform)
|
||||
{
|
||||
auto v = get(audit_conform);
|
||||
if (v == nullptr)
|
||||
throw std::runtime_error("Could not load dictionary for audit_conform");
|
||||
return *v;
|
||||
}
|
||||
|
||||
const validator &validator_factory::operator[](std::string_view dictionary_name)
|
||||
{
|
||||
auto v = get(dictionary_name);
|
||||
if (v == nullptr)
|
||||
throw std::runtime_error("Could not load dictionary for " + std::string{ dictionary_name });
|
||||
return *v;
|
||||
}
|
||||
|
||||
validator validator_factory::construct_validator(std::string_view name, std::optional<std::string> version)
|
||||
|
||||
@@ -26,12 +26,10 @@ if(NOT (Catch2_FOUND OR TARGET Catch2))
|
||||
find_package(Catch2 3 QUIET)
|
||||
|
||||
if(NOT Catch2_FOUND)
|
||||
include(FetchContent)
|
||||
|
||||
FetchContent_Declare(
|
||||
Catch2
|
||||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||
GIT_TAG v3.8.0)
|
||||
GIT_TAG v3.4.0)
|
||||
|
||||
FetchContent_MakeAvailable(Catch2)
|
||||
|
||||
@@ -51,6 +49,8 @@ list(
|
||||
spinner
|
||||
reconstruction
|
||||
validate-pdbx
|
||||
cql
|
||||
matrix
|
||||
)
|
||||
|
||||
add_library(test-main OBJECT "${CMAKE_CURRENT_SOURCE_DIR}/test-main.cpp")
|
||||
@@ -72,6 +72,8 @@ foreach(CIFPP_TEST IN LISTS CIFPP_tests)
|
||||
target_compile_options(${CIFPP_TEST} PRIVATE /EHsc)
|
||||
endif()
|
||||
|
||||
add_test(NAME ${CIFPP_TEST}
|
||||
COMMAND $<TARGET_FILE:${CIFPP_TEST}> --data-dir ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
if(NOT (CIFPP_TEST STREQUAL "spinner-test"))
|
||||
add_test(NAME ${CIFPP_TEST}
|
||||
COMMAND $<TARGET_FILE:${CIFPP_TEST}> --data-dir ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
537
test/cql-test.cpp
Normal file
537
test/cql-test.cpp
Normal file
@@ -0,0 +1,537 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*/
|
||||
|
||||
#include "test-main.hpp"
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <cif++.hpp>
|
||||
#include <cif++/cql.hpp>
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
cif::file operator""_cf(const char *text, std::size_t length)
|
||||
{
|
||||
struct membuf : public std::streambuf
|
||||
{
|
||||
membuf(char *text, std::size_t length)
|
||||
{
|
||||
this->setg(text, text, text + length);
|
||||
}
|
||||
} buffer(const_cast<char *>(text), length);
|
||||
|
||||
std::istream is(&buffer);
|
||||
return cif::file(is);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
const char *kAuthors[] = {
|
||||
"Kleywegt, G.J.",
|
||||
"Bergfors, T.",
|
||||
"Senn, H.",
|
||||
"Le Motte, P.",
|
||||
"Gsell, B.",
|
||||
"Shudo, K.",
|
||||
"Jones, T.A.",
|
||||
|
||||
"Banaszak, L.",
|
||||
"Winter, N.",
|
||||
"Xu, Z.",
|
||||
"Bernlohr, D.A.",
|
||||
"Cowan, S.W.",
|
||||
"Jones, T.A.",
|
||||
"Bergfors, T.",
|
||||
"Kleywegt, G.J.",
|
||||
"Jones, T.A.",
|
||||
"Cowan, S.W.",
|
||||
"Newcomer, M.E.",
|
||||
"Jones, T.A.",
|
||||
"Jones, T.A.",
|
||||
"Bergfors, T.",
|
||||
"Sedzik, J.",
|
||||
"Unge, T."
|
||||
};
|
||||
|
||||
// Test simple SELECT
|
||||
TEST_CASE("cql-1")
|
||||
{
|
||||
cif::file f(gTestDir / ".." / "examples" / "1cbs.cif.gz");
|
||||
auto &db = f.front();
|
||||
db.load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
auto r = tx.exec("SELECT name, ordinal FROM citation_author WHERE citation_id = 'primary';");
|
||||
CHECK(r.size() == 7);
|
||||
|
||||
for (size_t ix = 0; auto row : r)
|
||||
{
|
||||
REQUIRE(ix < (sizeof(kAuthors) / sizeof(char *)));
|
||||
|
||||
CHECK(row[0].as<std::string>() == kAuthors[ix]);
|
||||
CHECK(row[1].as<size_t>() == ix + 1);
|
||||
|
||||
CHECK(row["name"].as<std::string>() == kAuthors[ix]);
|
||||
CHECK(row["ordinal"].as<size_t>() == ix + 1);
|
||||
|
||||
++ix;
|
||||
}
|
||||
|
||||
r = tx.exec("SELECT ordinal, name FROM citation_author WHERE citation_id = 'primary';");
|
||||
CHECK(r.size() == 7);
|
||||
|
||||
for (size_t ix = 0; auto row : r)
|
||||
{
|
||||
REQUIRE(ix < (sizeof(kAuthors) / sizeof(char *)));
|
||||
|
||||
CHECK(row[1].as<std::string>() == kAuthors[ix]);
|
||||
CHECK(row[0].as<size_t>() == ix + 1);
|
||||
|
||||
CHECK(row["name"].as<std::string>() == kAuthors[ix]);
|
||||
CHECK(row["ordinal"].as<size_t>() == ix + 1);
|
||||
|
||||
++ix;
|
||||
}
|
||||
|
||||
r = tx.exec("SELECT * FROM citation_author WHERE citation_id = 'primary';");
|
||||
CHECK(r.size() == 7);
|
||||
|
||||
for (int ix = 0; auto row : r)
|
||||
{
|
||||
REQUIRE(static_cast<size_t>(ix) < (sizeof(kAuthors) / sizeof(char *)));
|
||||
|
||||
for (auto fld : row)
|
||||
{
|
||||
switch (fld.num())
|
||||
{
|
||||
case 0:
|
||||
CHECK(fld.name() == "citation_id");
|
||||
CHECK(fld.as<std::string>() == "primary");
|
||||
break;
|
||||
case 1:
|
||||
CHECK(fld.name() == "name");
|
||||
CHECK(fld.as<std::string>() == kAuthors[ix]);
|
||||
break;
|
||||
case 2:
|
||||
CHECK(fld.name() == "ordinal");
|
||||
CHECK(fld.as<int>() == ix + 1);
|
||||
break;
|
||||
default:
|
||||
REQUIRE(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CHECK(row["name"].as<std::string>() == kAuthors[ix]);
|
||||
CHECK(row["ordinal"].as<int>() == ix + 1);
|
||||
CHECK(row["citation_id"].as<std::string>() == "primary");
|
||||
|
||||
++ix;
|
||||
}
|
||||
}
|
||||
|
||||
// Test SELECT AS
|
||||
TEST_CASE("cql-2")
|
||||
{
|
||||
cif::file f(gTestDir / ".." / "examples" / "1cbs.cif.gz");
|
||||
auto &db = f.front();
|
||||
db.load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
auto r = tx.exec("SELECT name AS v1, ordinal AS v2 FROM citation_author WHERE citation_id = 'primary';");
|
||||
CHECK(r.size() == 7);
|
||||
|
||||
for (size_t ix = 0; auto row : r)
|
||||
{
|
||||
REQUIRE(ix < (sizeof(kAuthors) / sizeof(char *)));
|
||||
|
||||
CHECK(row[0].as<std::string>() == kAuthors[ix]);
|
||||
CHECK(row[1].as<size_t>() == ix + 1);
|
||||
|
||||
CHECK(row["v1"].as<std::string>() == kAuthors[ix]);
|
||||
CHECK(row["v2"].as<size_t>() == ix + 1);
|
||||
|
||||
++ix;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("cql-3")
|
||||
{
|
||||
cif::file f(gTestDir / ".." / "examples" / "1cbs.cif.gz");
|
||||
auto &db = f.front();
|
||||
db.load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
auto r = tx.exec("SELECT name FROM citation_author WHERE ordinal = 10").one_field();
|
||||
CHECK(r.as<std::string>() == kAuthors[9]);
|
||||
}
|
||||
|
||||
TEST_CASE("cql-4")
|
||||
{
|
||||
cif::file f(gTestDir / ".." / "examples" / "1cbs.cif.gz");
|
||||
auto &db = f.front();
|
||||
db.load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
auto r = tx.exec("SELECT name FROM citation_author WHERE ordinal BETWEEN 10 AND 15");
|
||||
REQUIRE(r.size() == 6);
|
||||
}
|
||||
|
||||
TEST_CASE("cql-5")
|
||||
{
|
||||
cif::file f(gTestDir / ".." / "examples" / "1cbs.cif.gz");
|
||||
auto &db = f.front();
|
||||
db.load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
auto r = tx.exec("SELECT (SELECT year FROM citation WHERE id = citation_id) AS jaar FROM citation_author WHERE ordinal IS 23").one_field();
|
||||
CHECK(r.name() == "jaar");
|
||||
CHECK(r.as<int>() == 1988);
|
||||
}
|
||||
|
||||
TEST_CASE("cql-6")
|
||||
{
|
||||
cif::file f(gTestDir / ".." / "examples" / "1cbs.cif.gz");
|
||||
auto &db = f.front();
|
||||
db.load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
auto r = tx.exec("SELECT COUNT(*) FROM citation WHERE page_last IS NULL").one_field();
|
||||
CHECK(r.as<int>() == 4);
|
||||
|
||||
r = tx.exec("SELECT COUNT(*) FROM citation WHERE page_last IS NOT NULL").one_field();
|
||||
CHECK(r.as<int>() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("cql-stream-1")
|
||||
{
|
||||
cif::file f(gTestDir / ".." / "examples" / "1cbs.cif.gz");
|
||||
auto &db = f.front();
|
||||
db.load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
for (size_t ix = 0;
|
||||
const auto &[name, ordinal] : tx.stream<std::string, size_t>(
|
||||
"SELECT name, ordinal FROM citation_author WHERE citation_id = 'primary';"))
|
||||
{
|
||||
REQUIRE(ix < (sizeof(kAuthors) / sizeof(char *)));
|
||||
|
||||
CHECK(name == kAuthors[ix]);
|
||||
CHECK(ordinal == (ix + 1));
|
||||
|
||||
++ix;
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("cql-insert-1")
|
||||
{
|
||||
auto f1 = R"(
|
||||
data_T1
|
||||
loop_
|
||||
_table1.id
|
||||
_table1.name
|
||||
1 aap
|
||||
2 noot)"_cf;
|
||||
auto f0 = f1;
|
||||
|
||||
auto &db = f1.front();
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
auto count = tx.exec("SELECT COUNT(*) FROM table1;").one_field().as<int>();
|
||||
CHECK(count == 2);
|
||||
|
||||
auto r = tx.exec("INSERT INTO table1 (id, name) VALUES (3, 'mies')");
|
||||
|
||||
count = tx.exec("SELECT COUNT(*) FROM table1").one_field().as<int>();
|
||||
CHECK(count == 3);
|
||||
|
||||
(void)tx.exec("DELETE FROM table1 WHERE CAST(id AS INTEGER) = 1;");
|
||||
|
||||
count = tx.exec("SELECT COUNT(*) FROM table1;").one_field().as<int>();
|
||||
CHECK(count == 2);
|
||||
|
||||
(void)tx.exec("UPDATE table1 SET name = 'amandel' WHERE CAST(id AS INTEGER) = 2");
|
||||
|
||||
auto f2 = R"(
|
||||
data_T1
|
||||
loop_
|
||||
_table1.id
|
||||
_table1.name
|
||||
2 amandel
|
||||
3 mies)"_cf;
|
||||
|
||||
CHECK(f1 == f2);
|
||||
|
||||
tx.rollback();
|
||||
|
||||
CHECK(f1 == f0);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("cql-rename")
|
||||
{
|
||||
auto f1 = R"(
|
||||
data_T1
|
||||
loop_
|
||||
_table1.id
|
||||
_table1.name
|
||||
1 aap
|
||||
2 noot)"_cf;
|
||||
|
||||
auto &db = f1.front();
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
(void)tx.exec("ALTER TABLE table1 RENAME TO 'table2'");
|
||||
|
||||
auto f2 = R"(
|
||||
data_T1
|
||||
loop_
|
||||
_table2.id
|
||||
_table2.name
|
||||
1 aap
|
||||
2 noot)"_cf;
|
||||
|
||||
CHECK(f1 == f2);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("cql-foreign-keys-1")
|
||||
{
|
||||
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
|
||||
_item_type_list.detail
|
||||
code char
|
||||
'[][_,.;:"&<>()/\{}'`~!@#$%A-Za-z0-9*|+-]*'
|
||||
; code item types/single words ...
|
||||
;
|
||||
text char
|
||||
'[][ \n\t()_,.;:"&<>/\{}'`~!@#$%?+=*A-Za-z0-9|^-]*'
|
||||
; text item types / multi-line text ...
|
||||
;
|
||||
int numb
|
||||
'[+-]?[0-9]+'
|
||||
; int item types are the subset of numbers that are the negative
|
||||
or positive integers.
|
||||
;
|
||||
|
||||
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_aliases.dictionary cif_core.dic
|
||||
_item_aliases.version 2.0.1
|
||||
_item_linked.child_name '_cat_2.parent_id'
|
||||
_item_linked.parent_name '_cat_1.id'
|
||||
_item_type.code code
|
||||
save_
|
||||
|
||||
save__cat_1.name
|
||||
_item.name '_cat_1.name'
|
||||
_item.category_id cat_1
|
||||
_item.mandatory_code yes
|
||||
_item_aliases.dictionary cif_core.dic
|
||||
_item_aliases.version 2.0.1
|
||||
_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_aliases.dictionary cif_core.dic
|
||||
_item_aliases.version 2.0.1
|
||||
_item_type.code int
|
||||
save_
|
||||
|
||||
save__cat_2.parent_id
|
||||
_item.name '_cat_2.parent_id'
|
||||
_item.category_id cat_2
|
||||
_item.mandatory_code yes
|
||||
_item_aliases.dictionary cif_core.dic
|
||||
_item_aliases.version 2.0.1
|
||||
_item_type.code code
|
||||
save_
|
||||
|
||||
save__cat_2.desc
|
||||
_item.name '_cat_2.desc'
|
||||
_item.category_id cat_2
|
||||
_item.mandatory_code yes
|
||||
_item_aliases.dictionary cif_core.dic
|
||||
_item_aliases.version 2.0.1
|
||||
_item_type.code text
|
||||
save_
|
||||
)";
|
||||
|
||||
struct membuf : public std::streambuf
|
||||
{
|
||||
membuf(char *text, std::size_t length)
|
||||
{
|
||||
this->setg(text, text, text + length);
|
||||
}
|
||||
} buffer(const_cast<char *>(dict), sizeof(dict) - 1);
|
||||
|
||||
std::istream is_dict(&buffer);
|
||||
|
||||
cif::validator validator(is_dict);
|
||||
|
||||
cif::file f;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
const char data[] = R"(
|
||||
data_test
|
||||
loop_
|
||||
_cat_1.id
|
||||
_cat_1.name
|
||||
1 Aap
|
||||
2 Noot
|
||||
3 Mies
|
||||
|
||||
loop_
|
||||
_cat_2.id
|
||||
_cat_2.parent_id
|
||||
_cat_2.desc
|
||||
1 1 'Een dier'
|
||||
2 1 'Een andere aap'
|
||||
3 2 'walnoot bijvoorbeeld'
|
||||
)";
|
||||
|
||||
struct data_membuf : public std::streambuf
|
||||
{
|
||||
data_membuf(char *text, std::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, validator);
|
||||
|
||||
auto &db = f.front();
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
for (const auto &desc : tx.stream<std::string>(R"(SELECT b.desc FROM cat_1 a, cat_2 b WHERE a.id = b.parent_id AND a.name = 'Noot')"))
|
||||
{
|
||||
CHECK(desc == "walnoot bijvoorbeeld");
|
||||
}
|
||||
|
||||
// Check cascading delete
|
||||
tx.exec("DELETE FROM cat_1 WHERE id = 1");
|
||||
CHECK(db["cat_1"].size() == 2);
|
||||
CHECK(db["cat_2"].size() == 1);
|
||||
|
||||
tx.rollback();
|
||||
CHECK(db["cat_1"].size() == 3);
|
||||
CHECK(db["cat_2"].size() == 3);
|
||||
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("drop-table")
|
||||
{
|
||||
auto f1 = R"(
|
||||
data_T1
|
||||
loop_
|
||||
_table1.id
|
||||
_table1.name
|
||||
1 aap
|
||||
2 noot)"_cf;
|
||||
|
||||
auto &db = f1.front();
|
||||
|
||||
cif::cql::connection connection(db);
|
||||
cif::cql::transaction tx(connection);
|
||||
|
||||
SECTION("commit")
|
||||
{
|
||||
(void)tx.exec("DROP TABLE table1;");
|
||||
tx.commit();
|
||||
|
||||
CHECK(db.empty());
|
||||
}
|
||||
|
||||
// Ah, too bad: this doesn't work
|
||||
// SECTION("rollback")
|
||||
// {
|
||||
// (void)tx.exec("DROP TABLE table1;");
|
||||
// tx.rollback();
|
||||
|
||||
// CHECK(not db.empty());
|
||||
// CHECK(db["table1"].size() == 2);
|
||||
// }
|
||||
}
|
||||
103
test/matrix-test.cpp
Normal file
103
test/matrix-test.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*/
|
||||
|
||||
#include "cif++/matrix.hpp"
|
||||
#include "test-main.hpp"
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <cif++.hpp>
|
||||
|
||||
TEST_CASE("m1")
|
||||
{
|
||||
cif::matrix3x3<int> m = cif::identity_matrix<int>(3);
|
||||
|
||||
CHECK(cif::determinant(m) == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("m2")
|
||||
{
|
||||
cif::matrix4x4<int> m = cif::identity_matrix<int>(4);
|
||||
|
||||
cif::sub_matrix<cif::matrix4x4<int>> ms(m, 1, 1);
|
||||
CHECK(ms == cif::identity_matrix<int>(3));
|
||||
}
|
||||
|
||||
TEST_CASE("m3")
|
||||
{
|
||||
cif::matrix4x4<int> m{
|
||||
{ 1, 2, 3, 4, //
|
||||
5, 6, 7, 8, //
|
||||
9, 10, 11, 12, //
|
||||
13, 14, 15, 16 }
|
||||
};
|
||||
cif::sub_matrix<cif::matrix4x4<int>> ms(m, 1, 1);
|
||||
|
||||
cif::matrix3x3<int> t{
|
||||
{ 1, 3, 4, 9, 11, 12, 13, 15, 16 }
|
||||
};
|
||||
|
||||
CHECK(ms == t);
|
||||
}
|
||||
|
||||
TEST_CASE("m4")
|
||||
{
|
||||
cif::matrix4x4<int> m{
|
||||
{
|
||||
-2,
|
||||
3,
|
||||
1,
|
||||
0,
|
||||
4,
|
||||
1,
|
||||
-3,
|
||||
2,
|
||||
0,
|
||||
-1,
|
||||
2,
|
||||
5,
|
||||
3,
|
||||
2,
|
||||
0,
|
||||
-4,
|
||||
}
|
||||
};
|
||||
|
||||
// std::cout << m << "\n\n";
|
||||
|
||||
// std::cout << cif::matrix3x3<int>(cif::sub_matrix<decltype(m)>(m, 0, 0)) << "\n\n";
|
||||
// std::cout << cif::matrix3x3<int>(cif::sub_matrix<decltype(m)>(m, 0, 1)) << "\n\n";
|
||||
// std::cout << cif::matrix3x3<int>(cif::sub_matrix<decltype(m)>(m, 0, 2)) << "\n\n";
|
||||
// std::cout << cif::matrix3x3<int>(cif::sub_matrix<decltype(m)>(m, 0, 3)) << "\n\n";
|
||||
|
||||
// std::cout << cif::determinant(cif::matrix3x3<int>(cif::sub_matrix<decltype(m)>(m, 0, 0))) << "\n\n";
|
||||
// std::cout << cif::determinant(cif::matrix3x3<int>(cif::sub_matrix<decltype(m)>(m, 0, 1))) << "\n\n";
|
||||
// std::cout << cif::determinant(cif::matrix3x3<int>(cif::sub_matrix<decltype(m)>(m, 0, 2))) << "\n\n";
|
||||
// std::cout << cif::determinant(cif::matrix3x3<int>(cif::sub_matrix<decltype(m)>(m, 0, 3))) << "\n\n";
|
||||
|
||||
CHECK(cif::determinant(m) == 332);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ TEST_CASE("create_nonpoly_1")
|
||||
|
||||
cif::file file;
|
||||
auto &&[dbi, ignore] = file.emplace("TEST"); // create a datablock
|
||||
dbi->set_validator(&cif::validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
dbi->load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::mm::structure structure(file);
|
||||
|
||||
@@ -82,7 +82,7 @@ _atom_site.pdbx_formal_charge
|
||||
# that's enough to test with
|
||||
)"_cf;
|
||||
|
||||
atoms.front().set_validator(&cif::validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
atoms.front().load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
auto &hem_data = atoms["HEM"];
|
||||
auto &atom_site = hem_data["atom_site"];
|
||||
@@ -159,7 +159,7 @@ _struct_asym.details ?
|
||||
_atom_type.symbol C
|
||||
)"_cf;
|
||||
|
||||
expected.front().set_validator(&cif::validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
expected.front().load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
if (not(expected.front() == structure.get_datablock()))
|
||||
{
|
||||
@@ -178,7 +178,7 @@ TEST_CASE("create_nonpoly_2")
|
||||
|
||||
cif::file file;
|
||||
auto &&[dbi, ignore] = file.emplace("TEST"); // create a datablock
|
||||
dbi->set_validator(&cif::validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
dbi->load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::mm::structure structure(file);
|
||||
|
||||
@@ -270,7 +270,7 @@ _struct_asym.details ?
|
||||
_atom_type.symbol C
|
||||
)"_cf;
|
||||
|
||||
expected.front().set_validator(&cif::validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
expected.front().load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
REQUIRE(expected.front() == structure.get_datablock());
|
||||
|
||||
@@ -354,7 +354,7 @@ _struct_asym.details ?
|
||||
#
|
||||
)"_cf;
|
||||
|
||||
data.front().set_validator(&cif::validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
data.front().load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
cif::mm::structure s(data);
|
||||
|
||||
@@ -566,7 +566,7 @@ _struct_asym.details ?
|
||||
#
|
||||
)"_cf;
|
||||
|
||||
data.front().set_validator(&cif::validator_factory::instance().get("mmcif_pdbx.dic"));
|
||||
data.front().load_dictionary("mmcif_pdbx.dic");
|
||||
|
||||
SECTION("max")
|
||||
{
|
||||
|
||||
@@ -49,6 +49,4 @@ TEST_CASE("q-1")
|
||||
|
||||
CHECK(pdbx_poly_seq_scheme.count("asym_id"_key == "A" and "entity_id"_key == 1 and "seq_id"_key == 1 and "mon_id"_key == "PRO" and "hetero"_key == false) == 1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -24,15 +24,17 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "cif++/point.hpp"
|
||||
#include "test-main.hpp"
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/matchers/catch_matchers_floating_point.hpp>
|
||||
#include <cif++.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <cif++.hpp>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning (disable : 5054) // warning C5054: operator '&': deprecated between enumerations of different types
|
||||
#pragma warning (disable : 4127) // conditional expression is constant
|
||||
# pragma warning(disable : 5054) // warning C5054: operator '&': deprecated between enumerations of different types
|
||||
# pragma warning(disable : 4127) // conditional expression is constant
|
||||
#endif
|
||||
|
||||
#include <Eigen/Eigenvalues>
|
||||
@@ -315,8 +317,7 @@ TEST_CASE("m2q_0a")
|
||||
cif::point p2 = p1;
|
||||
p2.rotate(q);
|
||||
|
||||
cif::matrix3x3<float> rot_c({
|
||||
static_cast<float>(d[0]),
|
||||
cif::matrix3x3<float> rot_c({ static_cast<float>(d[0]),
|
||||
static_cast<float>(d[1]),
|
||||
static_cast<float>(d[2]),
|
||||
static_cast<float>(d[3]),
|
||||
@@ -324,8 +325,7 @@ TEST_CASE("m2q_0a")
|
||||
static_cast<float>(d[5]),
|
||||
static_cast<float>(d[6]),
|
||||
static_cast<float>(d[7]),
|
||||
static_cast<float>(d[8])
|
||||
});
|
||||
static_cast<float>(d[8]) });
|
||||
|
||||
cif::point p3 = rot_c * p1;
|
||||
|
||||
@@ -610,3 +610,40 @@ TEST_CASE("volume_3bwh_1")
|
||||
|
||||
CHECK_THAT(c.get_cell().get_volume(), Catch::Matchers::WithinRel(741009.625f, 0.01f));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("smallest_sphere-1")
|
||||
{
|
||||
std::vector<cif::point> pts{
|
||||
{ 0.9295, 4.9006, 46.9706 },
|
||||
{ -0.1215, 5.5936, 46.0726 },
|
||||
{ -0.7975, 4.7046, 45.0796 },
|
||||
{ -1.4875, 3.5486, 45.7196 },
|
||||
{ -0.6535, 2.8816, 46.8186 },
|
||||
{ 0.3825, 3.5156, 47.4496 },
|
||||
{ 1.1995, 2.9206, 48.5286 },
|
||||
{ 0.8255, 2.0466, 49.4716 },
|
||||
{ 1.6625, 1.5036, 50.5176 },
|
||||
{ 1.1165, 0.6056, 51.3626 },
|
||||
{ 1.8325, -0.0064, 52.4656 },
|
||||
{ 1.1945, -0.9044, 53.2216 },
|
||||
{ 1.8135, -1.5534, 54.3566 },
|
||||
{ 1.0925, -2.4574, 55.0656 },
|
||||
{ 1.5205, -3.2204, 56.2476 },
|
||||
{ 1.1955, 5.8066, 48.1796 },
|
||||
{ 2.2495, 4.6896, 46.1796 },
|
||||
{ -1.2515, 1.5186, 47.1786 },
|
||||
{ 3.1385, 1.9106, 50.6166 },
|
||||
{ 3.2605, -1.1834, 54.7206 },
|
||||
{ 2.5975, -3.8554, 56.2096 },
|
||||
{ 0.7975, -3.2184, 57.2686 }
|
||||
};
|
||||
|
||||
for (int i = 0; i < 1000; ++i)
|
||||
{
|
||||
auto [c, r] = cif::smallest_sphere_around_points(pts);
|
||||
CHECK_THAT(cif::distance(c, cif::point{ 0, 0.743099928, 51.1741028 }), Catch::Matchers::WithinAbs(0.f, 0.01f));
|
||||
CHECK_THAT(r, Catch::Matchers::WithinAbs(7.31248331f, 0.01f));
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user