diff --git a/modules/io/src/mol/entity_io_crd_handler.cc b/modules/io/src/mol/entity_io_crd_handler.cc index cfbb7efccbc7986a61b903409c90755b4fe17a1f..e89b294f93f3f7d1f8d28240c1727766513bbd10 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 ceb635f81a2723900b70a0e6215e468c8fbe356d..29ffa6705e78e7b3b0fcd768bba00a13435843cd 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);