Skip to content
Snippets Groups Projects
Commit 8375f3bf authored by Marco Biasini's avatar Marco Biasini Committed by Bienchen
Browse files

added gzip support to PDBWriter

parent 380279e8
No related branches found
No related tags found
No related merge requests found
...@@ -20,9 +20,14 @@ ...@@ -20,9 +20,14 @@
Author: Marco Biasini Author: Marco Biasini
*/ */
#include <locale> #include <locale>
#include <boost/format.hpp>
#include <string.h> #include <string.h>
#include <boost/format.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/algorithm/string.hpp>
#include <ost/io/io_exception.hh> #include <ost/io/io_exception.hh>
#include <ost/mol/atom_handle.hh> #include <ost/mol/atom_handle.hh>
#include <ost/mol/residue_handle.hh> #include <ost/mol/residue_handle.hh>
...@@ -332,7 +337,7 @@ PDBWriter::PDBWriter(std::ostream& stream, const IOProfile& profile): ...@@ -332,7 +337,7 @@ PDBWriter::PDBWriter(std::ostream& stream, const IOProfile& profile):
multi_model_(false), charmm_style_(profile.dialect=="CHARMM"), is_pqr_(false), multi_model_(false), charmm_style_(profile.dialect=="CHARMM"), is_pqr_(false),
profile_(profile) profile_(profile)
{ {
out_.push(outstream_);
} }
PDBWriter::PDBWriter(const boost::filesystem::path& filename, PDBWriter::PDBWriter(const boost::filesystem::path& filename,
...@@ -346,19 +351,28 @@ PDBWriter::PDBWriter(const boost::filesystem::path& filename, ...@@ -346,19 +351,28 @@ PDBWriter::PDBWriter(const boost::filesystem::path& filename,
charmm_style_(profile.dialect=="CHARMM"), is_pqr_(false), charmm_style_(profile.dialect=="CHARMM"), is_pqr_(false),
profile_(profile), filename_("") profile_(profile), filename_("")
{ {
if (boost::iequals(".gz", boost::filesystem::extension(filename))) {
out_.push(boost::iostreams::gzip_compressor());
}
out_.push(outstream_);
} }
PDBWriter::PDBWriter(const String& filename, const IOProfile& profile): PDBWriter::PDBWriter(const String& filename, const IOProfile& profile):
outfile_(filename.c_str()), outstream_(outfile_), mol_count_(0), line_(80), outfile_(filename.c_str()), outstream_(outfile_), mol_count_(0), line_(80),
multi_model_(false), charmm_style_(profile.dialect=="CHARMM"), multi_model_(false), charmm_style_(profile.dialect=="CHARMM"),
is_pqr_(false), profile_(profile), filename_(filename) is_pqr_(false), profile_(profile), filename_(filename)
{} {
if (boost::iequals(".gz", boost::filesystem::extension(filename))) {
out_.push(boost::iostreams::gzip_compressor());
}
out_.push(outstream_);
}
void PDBWriter::WriteModelLeader() void PDBWriter::WriteModelLeader()
{ {
++mol_count_; ++mol_count_;
if (multi_model_) { if (multi_model_) {
outstream_ << "MODEL " << mol_count_ << std::endl; out_ << "MODEL " << mol_count_ << std::endl;
} else if (mol_count_>1) { } else if (mol_count_>1) {
throw IOException("Trying to write several models into one file with "); throw IOException("Trying to write several models into one file with ");
} }
...@@ -367,14 +381,14 @@ void PDBWriter::WriteModelLeader() ...@@ -367,14 +381,14 @@ void PDBWriter::WriteModelLeader()
void PDBWriter::WriteModelTrailer() void PDBWriter::WriteModelTrailer()
{ {
if (multi_model_) { if (multi_model_) {
outstream_ << "ENDMDL" << std::endl; out_ << "ENDMDL" << std::endl;
} }
} }
template <typename H> template <typename H>
void PDBWriter::WriteModel(H ent) void PDBWriter::WriteModel(H ent)
{ {
if (!outstream_) { if (!out_) {
if (!filename_.empty()) { if (!filename_.empty()) {
std::stringstream ss; std::stringstream ss;
ss << "Can't write PDB to file '" << filename_ << "'"; ss << "Can't write PDB to file '" << filename_ << "'";
...@@ -384,10 +398,10 @@ void PDBWriter::WriteModel(H ent) ...@@ -384,10 +398,10 @@ void PDBWriter::WriteModel(H ent)
} }
ForcePOSIX posix; ForcePOSIX posix;
this->WriteModelLeader(); this->WriteModelLeader();
PDBWriterImpl writer(outstream_,line_, atom_indices_, charmm_style_); PDBWriterImpl writer(out_, line_, atom_indices_, charmm_style_);
writer.SetIsPQR(is_pqr_); writer.SetIsPQR(is_pqr_);
ent.Apply(writer); ent.Apply(writer);
PDBConectWriterImpl con_writer(outstream_,atom_indices_); PDBConectWriterImpl con_writer(out_, atom_indices_);
ent.Apply(con_writer); ent.Apply(con_writer);
this->WriteModelTrailer(); this->WriteModelTrailer();
} }
...@@ -410,7 +424,7 @@ void PDBWriter::Write(const mol::AtomHandleList& atoms) ...@@ -410,7 +424,7 @@ void PDBWriter::Write(const mol::AtomHandleList& atoms)
mol::ChainHandle last_chain; mol::ChainHandle last_chain;
for (mol::AtomHandleList::const_iterator i=atoms.begin(), for (mol::AtomHandleList::const_iterator i=atoms.begin(),
e=atoms.end(); i!=e; ++i, ++counter) { e=atoms.end(); i!=e; ++i, ++counter) {
write_atom(outstream_, line_, *i, counter, is_pqr_, charmm_style_); write_atom(out_, line_, *i, counter, is_pqr_, charmm_style_);
} }
this->WriteModelTrailer(); this->WriteModelTrailer();
} }
...@@ -418,7 +432,7 @@ void PDBWriter::Write(const mol::AtomHandleList& atoms) ...@@ -418,7 +432,7 @@ void PDBWriter::Write(const mol::AtomHandleList& atoms)
PDBWriter::~PDBWriter() PDBWriter::~PDBWriter()
{ {
outstream_ << "END "; out_ << "END ";
} }
}} }}
...@@ -43,7 +43,9 @@ class EntityHandle; ...@@ -43,7 +43,9 @@ class EntityHandle;
namespace io { namespace io {
class DLLEXPORT_OST_IO PDBWriter { class DLLEXPORT_OST_IO PDBWriter {
typedef boost::iostreams::filtering_stream<boost::iostreams::output> OutStream;
public: public:
PDBWriter(const String& filename, PDBWriter(const String& filename,
const IOProfile& profile); const IOProfile& profile);
...@@ -76,6 +78,7 @@ private: ...@@ -76,6 +78,7 @@ private:
bool is_pqr_; bool is_pqr_;
IOProfile profile_; IOProfile profile_;
String filename_; String filename_;
OutStream out_;
}; };
}} }}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment