Subject: [bug#58261] [PATCH v3 08/15] gnu: Add gemmi.

From: David Elsing <[email-masked]>
Date: Thu, 13 Oct 2022 21:04:17 +0000

* gnu/packages/chemistry.scm (gemmi): New variable.
---
 gnu/packages/chemistry.scm                    | 96 +++++++++++++++++++
 .../patches/gemmi-fix-pegtl-usage.patch       | 31 ++++++
 .../patches/gemmi-fix-sajson-types.patch      | 11 +++
 3 files changed, 138 insertions(+)
 create mode 100644 gnu/packages/patches/gemmi-fix-pegtl-usage.patch
 create mode 100644 gnu/packages/patches/gemmi-fix-sajson-types.patch

diff --git a/gnu/packages/chemistry.scm b/gnu/packages/chemistry.scm
index 723b7ee8db..c6b8376248 100644
--- a/gnu/packages/chemistry.scm
+++ b/gnu/packages/chemistry.scm
@@ -589,3 +589,99 @@ (define-public sajson-for-gemmi
        ((#:phases phases)
         #~(modify-phases #$phases
             (delete 'build)))))))
+
+(define-public gemmi
+  (package
+    (name "gemmi")
+    (version "0.5.7")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/project-gemmi/gemmi")
+                    (commit (string-append "v" version))))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "00km5q726bslrw7xbfwb3f3mrsk19qbimfnl3hvr4wi1y3z8i18a"))
+              (patches
+               (search-patches "gemmi-fix-sajson-types.patch"
+                               "gemmi-fix-pegtl-usage.patch"))
+              (modules '((guix build utils)))
+              (snippet
+               '(begin
+                  (delete-file-recursively "include/gemmi/third_party")
+                  (delete-file-recursively "third_party")))))
+    (outputs '("out" "bin" "python"))
+    (build-system cmake-build-system)
+    (native-inputs
+     (list fast-float
+           optionparser
+           pegtl
+           pocketfft-cpp
+           pybind11
+           sajson-for-gemmi
+           stb-sprintf
+           tinydir))
+    (inputs (list python zlib))
+    (arguments
+     (list
+      #:modules '((guix build cmake-build-system)
+                  (guix build utils)
+                  ((guix build python-build-system)
+                   #:select (site-packages)))
+      #:imported-modules (append %cmake-build-system-modules
+                                 '((guix build python-build-system)))
+      #:configure-flags
+      #~(list "-DUSE_PYTHON=ON"
+              (string-append "-DPYTHON_INSTALL_DIR="
+                             (site-packages %build-inputs %outputs)))
+      #:phases
+      #~(modify-phases %standard-phases
+          (add-after 'unpack 'patch-includes
+            (lambda _
+              (substitute*
+                  (list
+                   "include/gemmi/sprintf.hpp"
+                   "include/gemmi/dirwalk.hpp"
+                   "include/gemmi/cif.hpp"
+                   "include/gemmi/json.hpp"
+                   "python/gemmi.cpp"
+                   "include/gemmi/atof.hpp"
+                   "include/gemmi/numb.hpp"
+                   "include/gemmi/fourier.hpp")
+                (("<stb/stb_sprintf.h>") "<stb_sprintf.h>")
+                (("\"third_party/tinydir.h\"") "<tinydir.h>")
+                (("\"third_party/tao/pegtl.hpp\"") "<tao/pegtl.hpp>")
+                (("\"third_party/sajson.h\"") "<sajson.h>")
+                (("\"gemmi/third_party/tao/pegtl/parse_error.hpp\"")
+                 "<tao/pegtl/parse_error.hpp>")
+                (("\"third_party/fast_float.h\"")
+                 "<fast_float/fast_float.h>")
+                (("\"third_party/pocketfft_hdronly.h\"")
+                 "<pocketfft_hdronly.h>"))))
+          (add-after 'unpack 'change-bin-prefix
+            (lambda _
+              (substitute* "CMakeLists.txt"
+                (("install\\(TARGETS program DESTINATION bin\\)")
+                 (string-append
+                  "install(TARGETS program DESTINATION "
+                  #$output:bin "/bin)")))))
+          (replace 'check
+            (lambda* (#:key tests? #:allow-other-keys)
+              (when tests?
+                (with-directory-excursion "../source"
+                  (setenv "PYTHONPATH" "../build")
+                  (invoke "python3" "-m" "unittest" "discover" "-v"
+                          "-s" "tests"))))))))
+    (home-page "https://gemmi.readthedocs.io/en/latest/")
+    (synopsis "Macromolecular crystallography library and utilities")
+    (description "GEMMI is a C++ library for macromolecular crystallography.
+It can be used for working with
+@enumerate
+@item macromolecular models (content of PDB, PDBx/mmCIF and mmJSON files),
+@item refinement restraints (CIF files),
+@item reflection data (MTZ and mmCIF formats),
+@item data on a 3D grid (electron density maps, masks, MRC/CCP4 format)
+@item crystallographic symmetry.
+@end enumerate")
+    (license license:mpl2.0)))
diff --git a/gnu/packages/patches/gemmi-fix-pegtl-usage.patch b/gnu/packages/patches/gemmi-fix-pegtl-usage.patch
new file mode 100644
index 0000000000..3667474847
--- /dev/null
+++ b/gnu/packages/patches/gemmi-fix-pegtl-usage.patch
@@ -0,0 +1,31 @@
+Use the definitions from (newer) upstream PEGTL.
+
+diff --git a/include/gemmi/cif.hpp b/include/gemmi/cif.hpp
+index c7ffdb44..35d24210 100644
+--- a/include/gemmi/cif.hpp
++++ b/include/gemmi/cif.hpp
+@@ -37,7 +37,6 @@ namespace pegtl = tao::pegtl;
+ namespace rules {
+ 
+   template<int TableVal> struct lookup_char {
+-    using analyze_t = pegtl::analysis::generic<pegtl::analysis::rule_type::ANY>;
+     template<typename Input> static bool match(Input& in) {
+       if (!in.empty() && cif::char_table(in.peek_char()) == TableVal) {
+         if (TableVal == 2)  // this set includes new-line
+@@ -71,11 +70,11 @@ namespace rules {
+   struct ws_or_eof : pegtl::sor<whitespace, pegtl::eof> {};
+ 
+   // (b) Reserved words.
+-  struct str_data : TAOCPP_PEGTL_ISTRING("data_") {};
+-  struct str_loop : TAOCPP_PEGTL_ISTRING("loop_") {};
+-  struct str_global : TAOCPP_PEGTL_ISTRING("global_") {};
+-  struct str_save : TAOCPP_PEGTL_ISTRING("save_") {};
+-  struct str_stop : TAOCPP_PEGTL_ISTRING("stop_") {};
++  struct str_data : TAO_PEGTL_ISTRING("data_") {};
++  struct str_loop : TAO_PEGTL_ISTRING("loop_") {};
++  struct str_global : TAO_PEGTL_ISTRING("global_") {};
++  struct str_save : TAO_PEGTL_ISTRING("save_") {};
++  struct str_stop : TAO_PEGTL_ISTRING("stop_") {};
+   struct keyword : pegtl::sor<str_data, str_loop, str_global,
+                               str_save, str_stop> {};
+ 
diff --git a/gnu/packages/patches/gemmi-fix-sajson-types.patch b/gnu/packages/patches/gemmi-fix-sajson-types.patch
new file mode 100644
index 0000000000..9633ddac8b
--- /dev/null
+++ b/gnu/packages/patches/gemmi-fix-sajson-types.patch
@@ -0,0 +1,11 @@
+diff -ur a/include/gemmi/json.hpp b/include/gemmi/json.hpp
+--- a/include/gemmi/json.hpp
++++ b/include/gemmi/json.hpp
+@@ -38,6 +38,7 @@
+ 
+ inline std::string as_cif_value(const sajson::value& val) {
+   switch (val.get_type()) {
++    case sajson::TYPE_INTEGER:
+     case sajson::TYPE_DOUBLE:
+       return val.as_string();
+     case sajson::TYPE_NULL:
-- 
2.37.0