Skip to content

Commit

Permalink
replace cif::read_file with read_cif_gz in python bindings
Browse files Browse the repository at this point in the history
and in two examples.

In Python, cif.read_file() and gemmi.read_small_structure() can now read
gzipped files.
  • Loading branch information
wojdyr committed Feb 13, 2025
1 parent 8c41feb commit 4579695
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
4 changes: 2 additions & 2 deletions examples/torsion_in_ring.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// prints torsion restraints that are part of a ring

#include <gemmi/cif.hpp>
#include <gemmi/read_cif.hpp>
#include <gemmi/chemcomp.hpp>
//#include <gemmi/to_cif.hpp> // for write_cif_to_stream
#include <stdio.h>
Expand All @@ -9,7 +9,7 @@ namespace cif = gemmi::cif;
using AtomId = gemmi::Restraints::AtomId;

void check_torsions(const char* file_path) {
cif::Document doc = cif::read_file(file_path);
cif::Document doc = gemmi::read_cif_gz(file_path);
for (cif::Block& block : doc.blocks) {
if (block.name.empty() && block.name == "comp_list")
continue;
Expand Down
8 changes: 4 additions & 4 deletions examples/with_bgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/vf2_sub_graph_iso.hpp>
#include <boost/graph/mcgregor_common_subgraphs.hpp>
#include <gemmi/cif.hpp> // for cif::read_file
#include <gemmi/read_cif.hpp> // for read_cif_gz
#include <gemmi/chemcomp.hpp> // for ChemComp, make_chemcomp_from_block

struct AtomVertex {
Expand Down Expand Up @@ -41,7 +41,7 @@ Graph make_graph(const gemmi::ChemComp& cc) {
}

gemmi::ChemComp make_chemcomp(const char* path) {
gemmi::cif::Document doc = gemmi::cif::read_file(path);
gemmi::cif::Document doc = gemmi::read_cif_gz(path);
// assuming the component description is in the last block of the file
return gemmi::make_chemcomp_from_block(doc.blocks.back());
}
Expand Down Expand Up @@ -83,7 +83,7 @@ void count_automorphisms_of_SO3() {
//
// Example output:
//
// $ ./with_bgl ccd/M10.cif monomers/m/M10.cif
// $ ./with_bgl ccd/M10.cif monomers/m/M10.cif
// isomorphic!
// O4 -> O9
// O9 -> O4
Expand Down Expand Up @@ -162,7 +162,7 @@ void check_subgraph_isomorphism(const char* cif1, const char* cif2) {
// CAE -> C7
// OAI -> O8
// CAD -> C1
//
//
// real 0m0.012s
// user 0m0.008s
// sys 0m0.004s
Expand Down
11 changes: 11 additions & 0 deletions python/cif.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2017 Global Phasing Ltd.

#include <sstream>
#include "gemmi/cif.hpp" // for parse_input
#include "gemmi/cifdoc.hpp"
#include "gemmi/to_cif.hpp"
#include "gemmi/to_json.hpp"
Expand Down Expand Up @@ -59,6 +60,16 @@ T& add_to_vector(std::vector<T>& vec, const T& new_item, int pos) {
return vec[pos];
}

// cif parsing without checking for missing values and duplicates
void cif_parse_string(Document& doc, const std::string& data) {
tao::pegtl::memory_input<> in(data, "string");
parse_input(doc, in);
}
void cif_parse_file(Document& doc, const std::string& filename) {
GEMMI_CIF_FILE_INPUT(in, filename);
parse_input(doc, in);
}

} // anonymous namespace

// for delitem_slice
Expand Down
4 changes: 0 additions & 4 deletions python/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ void add_write(nb::module_& m, nb::class_<gemmi::Structure>& structure);
// defined in align.cpp
void add_assign_label_seq_id(nb::class_<gemmi::Structure>& structure);

// defined in read.cpp
void cif_parse_string(gemmi::cif::Document& doc, const std::string& data);
void cif_parse_file(gemmi::cif::Document& doc, const std::string& filename);

// convert pythonic index to C++ index
inline int c_index(int index, size_t size) {
if (index < 0)
Expand Down
14 changes: 2 additions & 12 deletions python/read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "gemmi/numb.hpp"
#include "gemmi/cifdoc.hpp"
#include "gemmi/cif.hpp"
#include "gemmi/mmcif.hpp" // for make_structure_from_block, ...
#include "gemmi/pdb.hpp" // for read_pdb_string
#include "gemmi/gz.hpp" // for estimate_uncompressed_size
Expand All @@ -24,7 +23,7 @@ using namespace gemmi;
NB_MAKE_OPAQUE(std::vector<SmallStructure::Site>)

void add_cif_read(nb::module_& cif) {
cif.def("read_file", &cif::read_file, nb::arg("filename"),
cif.def("read_file", &read_cif_gz, nb::arg("filename"),
"Reads a CIF file copying data into Document.");
cif.def("read", &read_cif_or_mmjson_gz,
nb::arg("filename"), "Reads normal or gzipped CIF file.");
Expand Down Expand Up @@ -101,7 +100,7 @@ void add_read_structure(nb::module_& m) {

// from smcif.hpp
m.def("read_small_structure", [](const std::string& path) {
cif::Block block = cif::read_file(path).sole_block();
cif::Block block = read_cif_gz(path).sole_block();
return new SmallStructure(make_small_structure_from_block(block));
}, nb::arg("path"), "Reads a small molecule CIF file.");
m.def("make_small_structure_from_block", &make_small_structure_from_block,
Expand Down Expand Up @@ -180,12 +179,3 @@ void add_small(nb::module_& m) {
nb::arg("st"), nb::arg("n")=0);
}

// used in cif.cpp
void cif_parse_string(cif::Document& doc, const std::string& data) {
tao::pegtl::memory_input<> in(data, "string");
cif::parse_input(doc, in);
}
void cif_parse_file(cif::Document& doc, const std::string& filename) {
GEMMI_CIF_FILE_INPUT(in, filename);
cif::parse_input(doc, in);
}

0 comments on commit 4579695

Please sign in to comment.