From 37d97daf9474361d23ea6e3e91adc38a2aea3121 Mon Sep 17 00:00:00 2001
From: tobias <tobias@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Thu, 22 Apr 2010 11:32:47 +0000
Subject: [PATCH] allow export to CHARMM CRD file format

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2105 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/io/src/mol/entity_io_crd_handler.cc | 62 +++++++++++++++++++--
 modules/io/src/mol/entity_io_crd_handler.hh | 17 ++++++
 2 files changed, 73 insertions(+), 6 deletions(-)

diff --git a/modules/io/src/mol/entity_io_crd_handler.cc b/modules/io/src/mol/entity_io_crd_handler.cc
index cfbb7efcc..e89b294f9 100644
--- a/modules/io/src/mol/entity_io_crd_handler.cc
+++ b/modules/io/src/mol/entity_io_crd_handler.cc
@@ -154,7 +154,48 @@ void CRDReader::ParseAndAddAtom(const String& line, mol::EntityHandle& ent)
   ++atom_count_;
 }
 
+CRDWriter::CRDWriter(std::ostream& ostream) :
+     outfile_(), outstream_(ostream), atom_count_(0)
+{}
 
+CRDWriter::CRDWriter(const boost::filesystem::path& filename) :
+  outfile_(filename.file_string().c_str()), outstream_(outfile_),
+  atom_count_(0)
+{}
+
+CRDWriter::CRDWriter(const String& filename) :
+  outfile_(filename.c_str()), outstream_(outfile_), atom_count_(0)
+{}
+
+void CRDWriter::WriteHeader(const mol::EntityView& ent)
+{
+  outstream_  << "* COOR FILE CREATED BY OPENSTRUCTURE" << std::endl;
+  outstream_  << "*" << std::endl;
+  outstream_  << ent.GetAtomCount() << std::endl;
+}
+
+bool CRDWriter::VisitAtom(const mol::AtomHandle& atom)
+{
+  atom_count_++;
+  String e_name=atom.GetEntity().GetName();
+  if (e_name=="") {
+    e_name="MOL";
+  }
+  mol::ResidueHandle res=atom.GetResidue();
+  outstream_  << format("%5i") % atom_count_
+              << format("%5i") % res.GetNumber() << " "
+              << format("%4s") % res.GetKey() << " "
+              << format("%-4s") % atom.GetName()
+              << format("%10.5f") % atom.GetPos().GetX()
+              << format("%10.5f") % atom.GetPos().GetY()
+              << format("%10.5f") % atom.GetPos().GetZ() << " "
+              << format("%-4s") % e_name << " "
+              << format("%-5i") % res.GetNumber() << " "
+              << format("%8.5f") % atom.GetProp().b_factor
+              << std::endl;
+
+  return true;
+}
 
 bool EntityIOCRDHandler::RequiresBuilder() const
 {
@@ -168,9 +209,22 @@ void EntityIOCRDHandler::Import(mol::EntityHandle& ent,
   reader.Import(ent);
 }
 
+void EntityIOCRDHandler::Export(const mol::EntityView& ent,
+                                std::ostream& stream) const
+{
+  CRDWriter writer(stream);
+  writer.WriteHeader(ent);
+  mol::EntityView non_const_view = ent;
+  non_const_view.Apply(writer);
+}
+
 void EntityIOCRDHandler::Export(const mol::EntityView& ent,
                                 const boost::filesystem::path& loc) const
 {
+  CRDWriter writer(loc);
+  writer.WriteHeader(ent);
+  mol::EntityView non_const_view = ent;
+  non_const_view.Apply(writer);
 }
 
 namespace {
@@ -201,7 +255,7 @@ bool EntityIOCRDHandler::ProvidesImport(const boost::filesystem::path& loc,
 bool EntityIOCRDHandler::ProvidesExport(const boost::filesystem::path& loc,
                                         const String& type)
 {
-  return false;
+  return crd_handler_is_responsible_for(loc, type);
 }
 
 mol::EntityHandle LoadCRD(const String& file_name) 
@@ -224,11 +278,7 @@ void EntityIOCRDHandler::Import(mol::EntityHandle& ent,
 }
 
 
-void EntityIOCRDHandler::Export(const mol::EntityView& ent, 
-                                std::ostream& stream) const
-{
-  throw IOException("CRD format does not support export to stream");
-}
+
 
 }} // ns
 
diff --git a/modules/io/src/mol/entity_io_crd_handler.hh b/modules/io/src/mol/entity_io_crd_handler.hh
index ceb635f81..29ffa6705 100644
--- a/modules/io/src/mol/entity_io_crd_handler.hh
+++ b/modules/io/src/mol/entity_io_crd_handler.hh
@@ -52,6 +52,23 @@ private:
   boost::iostreams::filtering_stream<boost::iostreams::input>  in_;
 };
 
+
+class DLLEXPORT_OST_IO CRDWriter : public mol::EntityVisitor {
+public:
+  CRDWriter(const String& filename);
+  CRDWriter(const boost::filesystem::path& filename);
+  CRDWriter(std::ostream& outstream);
+
+  virtual bool VisitAtom(const mol::AtomHandle& atom);
+
+  void WriteHeader(const mol::EntityView& ent);
+
+private:
+  std::ofstream   outfile_;
+  std::ostream&   outstream_;
+  int atom_count_;
+};
+
 class DLLEXPORT_OST_IO EntityIOCRDHandler: public EntityIOHandler {
 public:
   virtual void Import(mol::EntityHandle& ent, const boost::filesystem::path& loc);
-- 
GitLab