From cf6facd153f25dfcb8d72c92df0d8de19012dcd3 Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@unibas.ch>
Date: Fri, 5 Jan 2024 17:46:42 +0100
Subject: [PATCH] mmcif writer: introduce EntityToMMCifString

---
 modules/io/pymod/export_mmcif_io.cc | 20 ++++++++++++++++++++
 modules/io/src/mol/mmcif_str.cc     | 19 +++++++++++++++++++
 modules/io/src/mol/mmcif_str.hh     |  8 ++++++++
 3 files changed, 47 insertions(+)

diff --git a/modules/io/pymod/export_mmcif_io.cc b/modules/io/pymod/export_mmcif_io.cc
index 15c0beebe..3665c351b 100644
--- a/modules/io/pymod/export_mmcif_io.cc
+++ b/modules/io/pymod/export_mmcif_io.cc
@@ -57,6 +57,18 @@ boost::python::tuple WrapMMCifStringToEntity(const String& mmcif,
                                    std::get<2>(res));
 }
 
+String WrapEntityToMMCifStringEnt(const ost::mol::EntityHandle& ent,
+                                 const String& data_name,
+                                 bool mmcif_conform) {
+  return EntityToMMCifString(ent, data_name, mmcif_conform);
+}
+
+String WrapEntityToMMCifStringView(const ost::mol::EntityView& ent,
+                                   const String& data_name,
+                                   bool mmcif_conform) {
+  return EntityToMMCifString(ent, data_name, mmcif_conform);
+}
+
 void WrapStarLoopAddData(StarWriterLoop& sl, const boost::python::list& l) {
   std::vector<StarWriterValue> v;
   for (int i = 0; i < boost::python::len(l); ++i){
@@ -516,4 +528,12 @@ void export_mmcif_io()
   def("MMCifStrToEntity", &WrapMMCifStringToEntity, (arg("pdb_string"),
                                                      arg("profile")=IOProfile(),
                                                      arg("process")=false));
+
+  def("EntityToMMCifString",  &WrapEntityToMMCifStringEnt, (arg("ent"),
+                                                            arg("data_name")="OST_structure",
+                                                            arg("mmcif_conform")=true));
+
+  def("EntityToMMCifString",  &WrapEntityToMMCifStringView, (arg("ent"),
+                                                             arg("data_name")="OST_structure",
+                                                             arg("mmcif_conform")=true));
 }
diff --git a/modules/io/src/mol/mmcif_str.cc b/modules/io/src/mol/mmcif_str.cc
index e24f9d791..a127ab7fa 100644
--- a/modules/io/src/mol/mmcif_str.cc
+++ b/modules/io/src/mol/mmcif_str.cc
@@ -19,9 +19,28 @@
 #include <sstream>
 #include <ost/io/mol/mmcif_str.hh>
 #include <ost/io/mol/mmcif_reader.hh>
+#include <ost/io/mmcif_writer.hh>
 
 namespace ost { namespace io {
 
+String EntityToMMCifString(const ost::mol::EntityHandle& ent,
+                           const String& data_name, bool mmcif_conform) {
+  std::stringstream ss;
+  MMCifWriter writer;
+  writer.SetStructure(ent, mmcif_conform);
+  writer.Write(data_name, ss);
+  return ss.str();
+}
+
+String EntityToMMCifString(const ost::mol::EntityView& ent,
+                           const String& data_name, bool mmcif_conform) {
+  std::stringstream ss;
+  MMCifWriter writer;
+  writer.SetStructure(ent, mmcif_conform);
+  writer.Write(data_name, ss);
+  return ss.str();
+}
+
 std::tuple<mol::EntityHandle, MMCifInfo, ost::seq::SequenceList>
 MMCifStringToEntity(const String& mmcif, const IOProfile& profile, bool process) {
   std::stringstream stream(mmcif);
diff --git a/modules/io/src/mol/mmcif_str.hh b/modules/io/src/mol/mmcif_str.hh
index 7e0ec2a11..ec0c92df4 100644
--- a/modules/io/src/mol/mmcif_str.hh
+++ b/modules/io/src/mol/mmcif_str.hh
@@ -27,6 +27,14 @@
 
 namespace ost { namespace io {
 
+String DLLEXPORT_OST_IO
+EntityToMMCifString(const ost::mol::EntityHandle& ent, const String& data_name,
+                    bool mmcif_conform);
+
+String DLLEXPORT_OST_IO
+EntityToMMCifString(const ost::mol::EntityView& ent, const String& data_name,
+                    bool mmcif_conform);
+
 std::tuple<mol::EntityHandle, MMCifInfo, ost::seq::SequenceList> DLLEXPORT_OST_IO
 MMCifStringToEntity(const String& mmcif, const IOProfile& profile, bool process);
 
-- 
GitLab