From c96834cccfd3c6193cb45179a323353c6218c058 Mon Sep 17 00:00:00 2001 From: ansgar <ansgar@5a81b35b-ba03-0410-adc8-b2c5c5119f08> Date: Wed, 28 Apr 2010 02:35:36 +0000 Subject: [PATCH] more functionality for coord group and coord source (pending a considerable refactoring, however) some debug and fixes for mae io handler added residue chemclass to python git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2125 5a81b35b-ba03-0410-adc8-b2c5c5119f08 --- modules/io/src/mol/entity_io_mae_handler.cc | 74 ++++++++++++-------- modules/mol/base/pymod/export_coord_group.cc | 2 + modules/mol/base/pymod/export_residue.cc | 5 ++ modules/mol/base/src/coord_group.cc | 12 +++- modules/mol/base/src/coord_group.hh | 6 +- modules/mol/base/src/coord_source.cc | 20 ++++-- modules/mol/base/src/coord_source.hh | 4 +- modules/mol/base/src/in_mem_coord_source.cc | 9 ++- modules/mol/base/src/in_mem_coord_source.hh | 1 + 9 files changed, 92 insertions(+), 41 deletions(-) diff --git a/modules/io/src/mol/entity_io_mae_handler.cc b/modules/io/src/mol/entity_io_mae_handler.cc index b8647c00a..03700f62a 100644 --- a/modules/io/src/mol/entity_io_mae_handler.cc +++ b/modules/io/src/mol/entity_io_mae_handler.cc @@ -30,7 +30,6 @@ #include <boost/iostreams/filter/gzip.hpp> #include <boost/filesystem/convenience.hpp> #include <boost/algorithm/string.hpp> -#include <boost/lexical_cast.hpp> #include <boost/format.hpp> #include <boost/regex.hpp> @@ -128,11 +127,16 @@ void MAEReader::Import(mol::EntityHandle& ent) if(in_atom_block) { if(parsing_atoms) { if(boost::regex_match(line,r_delim)) { - std::cerr << "stopping atom parsing" << std::endl; + LOGN_DUMP( "stopping atom parsing" ); parsing_atoms=false; + prop_list.clear(); } else { // parsing atom line std::vector<std::string> tokens=tokenize(line); + for(int i=0;i<tokens.size();++i) { + LOG_DUMP( "[" << tokens[i] << "] "); + } + LOGN_DUMP(""); add_atom(ent,editor, tokens[i_atom_name], tokens[i_atom_xpos], @@ -153,37 +157,39 @@ void MAEReader::Import(mol::EntityHandle& ent) i_chain_name==-1) { throw IOException("missing atom prop"); } - std::cerr << "starting atom parsing" << std::endl; + LOGN_DUMP( "starting atom parsing" ); parsing_atoms=true; } else if(line[0]=='}') { - std::cerr << "exiting atom block" << std::endl; + LOGN_DUMP( "exiting atom block" ); in_atom_block=false; } else { // parsing property line - int pid=prop_list.size(); - prop_list.push_back(line); - std::cerr << "found property '" << prop_list.back() << "'" << std::endl; - if(line=="s_m_pdb_atom_name") i_atom_name=pid; - else if(line=="r_m_x_coord") i_atom_xpos=pid; - else if(line=="r_m_y_coord") i_atom_ypos=pid; - else if(line=="r_m_z_coord") i_atom_zpos=pid; - else if(line=="s_m_pdb_residue_name") i_res_name=pid; - else if(line=="i_m_residue_number") i_res_num=pid; - else if(line=="s_m_pdb_segment_name") i_chain_name=pid; + if(line[0]!='#') { + int pid=prop_list.size()+1; + prop_list.push_back(line); + LOGN_DUMP( "found property '" << prop_list.back() << "' id=" << pid ); + if(line=="s_m_pdb_atom_name") i_atom_name=pid; + else if(line=="r_m_x_coord") i_atom_xpos=pid; + else if(line=="r_m_y_coord") i_atom_ypos=pid; + else if(line=="r_m_z_coord") i_atom_zpos=pid; + else if(line=="s_m_pdb_residue_name") i_res_name=pid; + else if(line=="i_m_residue_number") i_res_num=pid; + else if(line=="s_m_pdb_segment_name") i_chain_name=pid; + } } } } else { // not in atom block if(boost::regex_match(line,r_atom_block)) { - std::cerr << "entering atom block" << std::endl; + LOGN_DUMP( "entering atom block" ); in_atom_block=true; } else if(line[0]=='}') { - std::cerr << "exiting ct block" << std::endl; + LOGN_DUMP( "exiting ct block" ); in_ct_block=false; } } } else { // not in ct block if(boost::regex_match(line,r_ct_block)) { - std::cerr << "entering ct block" << std::endl; + LOGN_DUMP( "entering ct block" ); in_ct_block=true; } } @@ -196,25 +202,27 @@ void MAEReader::Import(mol::EntityHandle& ent) void MAEReader::add_atom(mol::EntityHandle ent, mol::XCSEditor& editor, - const std::string& aname, + const std::string& aname2, const std::string& s_axpos, const std::string& s_aypos, const std::string& s_azpos, - const std::string& rname, + const std::string& rname2, const std::string& s_rnum, - const std::string& cname) + const std::string& cname2) { - std::string ele = aname.substr(0,1); - int irnum = boost::lexical_cast<int>(s_rnum); - geom::Vec3 apos(boost::lexical_cast<Real>(s_axpos), - boost::lexical_cast<Real>(s_aypos), - boost::lexical_cast<Real>(s_azpos)); + std::string aname=boost::trim_copy(aname2); + std::string rname=boost::trim_copy(rname2); + std::string cname=boost::trim_copy(cname2); + std::string ele=aname.substr(0,1); + if(isdigit(ele[0])) ele="H"; + + int irnum = atoi(s_rnum.c_str()); + geom::Vec3 apos(atof(s_axpos.c_str()), + atof(s_aypos.c_str()), + atof(s_azpos.c_str())); mol::ResidueKey rkey(rname); - // some postprocessing - LOGN_TRACE( "cname: [" << cname << "]" ); - mol::ResNum rnum(irnum); // determine chain and residue update @@ -236,23 +244,27 @@ void MAEReader::add_atom(mol::EntityHandle ent, if(update_chain) { if (!(curr_chain_=ent.FindChain(cname))) { - LOGN_DUMP("new chain " << cname); curr_chain_=editor.InsertChain(cname); + LOGN_DUMP("new chain " << curr_chain_); ++chain_count_; + } else { + LOGN_DUMP("old chain " << curr_chain_); } } if(update_residue) { if (!(curr_residue_=curr_chain_.FindResidue(rnum))) { - LOGN_DUMP("new residue " << rkey << " " << rnum); curr_residue_=editor.AppendResidue(curr_chain_, rkey, rnum); assert(curr_residue_.IsValid()); + LOGN_DUMP(" new residue " << curr_residue_); ++residue_count_; + } else { + LOGN_DUMP(" old residue " << curr_residue_); } } // finally add atom - LOGN_DUMP("adding atom " << aname << " (" << ele << ") @" << apos); + LOGN_DUMP(" atom " << aname << " (" << ele << ") @" << apos); mol::AtomProp aprop; aprop.element=ele; aprop.radius=conop::Conopology::Instance().GetDefaultAtomRadius(ele); diff --git a/modules/mol/base/pymod/export_coord_group.cc b/modules/mol/base/pymod/export_coord_group.cc index 28059f82b..90efa24de 100644 --- a/modules/mol/base/pymod/export_coord_group.cc +++ b/modules/mol/base/pymod/export_coord_group.cc @@ -54,6 +54,8 @@ void export_CoordGroup() .def("IsValid", &CoordGroupHandle::IsValid) .def("Capture", capture1) .def("Capture", capture2) + .def("CaptureInto",&CoordGroupHandle::CaptureInto) + .def("GetAtomList",&CoordGroupHandle::GetAtomList) .def("__getitem__",cg_getitem) .def("__setitem__",cg_setitem) ; diff --git a/modules/mol/base/pymod/export_residue.cc b/modules/mol/base/pymod/export_residue.cc index 66214c7d4..688f81be8 100644 --- a/modules/mol/base/pymod/export_residue.cc +++ b/modules/mol/base/pymod/export_residue.cc @@ -36,6 +36,8 @@ namespace { void set_sec_struct1(ResidueBase* b, const SecStructure& s) {b->SetSecStructure(s);} void set_sec_struct2(ResidueBase* b, char c) {b->SetSecStructure(SecStructure(c));} + void set_chemclass1(ResidueBase* b, const ChemClass& cc) {b->SetChemClass(cc);} + void set_chemclass2(ResidueBase* b, char c) {b->SetChemClass(ChemClass(c));} } @@ -104,6 +106,9 @@ void export_Residue() return_value_policy<copy_const_reference>()) .def("GetNumber", &ResidueBase::GetNumber, return_value_policy<copy_const_reference>()) + .def("GetChemClass", &ResidueBase::GetChemClass) + .def("SetChemClass", set_chemclass1) + .def("SetChemClass", set_chemclass2) .add_property("number", make_function(&ResidueBase::GetNumber, return_value_policy<copy_const_reference>())) diff --git a/modules/mol/base/src/coord_group.cc b/modules/mol/base/src/coord_group.cc index 6675b5499..df0b1a5de 100644 --- a/modules/mol/base/src/coord_group.cc +++ b/modules/mol/base/src/coord_group.cc @@ -118,7 +118,7 @@ CoordFramePtr CoordGroupHandle::GetFrame(uint frame) const return source_->GetFrame(frame); } -const AtomHandleList& CoordGroupHandle::GetAtomList() const +AtomHandleList CoordGroupHandle::GetAtomList() const { this->CheckValidity(); return source_->GetAtomList(); @@ -134,6 +134,16 @@ void CoordGroupHandle::Capture() } } +void CoordGroupHandle::CaptureInto(int pos) +{ + this->CheckValidity(); + if (source_->IsMutable()) { + source_->CaptureInto(pos); + } else { + throw IntegrityError("Can't capture. CoordGroup is immutable"); + } +} + void CoordGroupHandle::Capture(uint frame) { this->CheckValidity(); diff --git a/modules/mol/base/src/coord_group.hh b/modules/mol/base/src/coord_group.hh index 3b038e37b..38fa81d2e 100644 --- a/modules/mol/base/src/coord_group.hh +++ b/modules/mol/base/src/coord_group.hh @@ -62,6 +62,8 @@ public: /// \brief store current atom positions in new frame void Capture(); + void CaptureInto(int pos); + /// \brief store current atom positions in given frame (must exist) void Capture(uint frame); @@ -80,7 +82,7 @@ public: /// \brief whether the handle is valid operator bool() const; - const AtomHandleList& GetAtomList() const; + AtomHandleList GetAtomList() const; CoordFramePtr GetFrame(uint frame) const; private: void CheckValidity() const; @@ -91,7 +93,7 @@ private: }; // factory method -// create with a reference set of atoms and the number of frames +// create with a reference set of atoms and zero frames DLLEXPORT_OST_MOL CoordGroupHandle CreateCoordGroup(const AtomHandleList& atoms); }} // ns diff --git a/modules/mol/base/src/coord_source.cc b/modules/mol/base/src/coord_source.cc index b25a91755..9b9f853e3 100644 --- a/modules/mol/base/src/coord_source.cc +++ b/modules/mol/base/src/coord_source.cc @@ -87,6 +87,21 @@ void CoordSource::Capture() this->AddFrame(coords); } +void CoordSource::CaptureInto(int pos) +{ + std::vector<geom::Vec3> coords; + coords.reserve(atoms_.size()); + for (AtomHandleList::const_iterator i=atoms_.begin(), + e=atoms_.end(); i!=e; ++i) { + coords.push_back(i->GetPos()); + } + if(pos<0 || pos>=GetFrameCount()) { + this->AddFrame(coords); + } else { + this->InsertFrame(pos,coords); + } +} + void CoordSource::Capture(uint f) { CoordFrame& fp=*(GetFrame(f)); @@ -124,11 +139,6 @@ bool CoordSource::IsMutable() const return mutable_; } -void CoordSource::AddFrame(const std::vector<geom::Vec3>& coords) -{ - assert(0 && "implement me"); -} - void CoordSource::SetMutable(bool flag) { mutable_=flag; diff --git a/modules/mol/base/src/coord_source.hh b/modules/mol/base/src/coord_source.hh index bc54b88a7..590ba8073 100644 --- a/modules/mol/base/src/coord_source.hh +++ b/modules/mol/base/src/coord_source.hh @@ -66,9 +66,11 @@ public: bool IsMutable() const; void Capture(); + void CaptureInto(int pos); void Capture(uint f); - virtual void AddFrame(const std::vector<geom::Vec3>& coords); + virtual void AddFrame(const std::vector<geom::Vec3>& coords) = 0; + virtual void InsertFrame(int pos, const std::vector<geom::Vec3>& coords) = 0; protected: void SetMutable(bool flag); private: diff --git a/modules/mol/base/src/in_mem_coord_source.cc b/modules/mol/base/src/in_mem_coord_source.cc index 089c667bd..1690c1104 100644 --- a/modules/mol/base/src/in_mem_coord_source.cc +++ b/modules/mol/base/src/in_mem_coord_source.cc @@ -16,7 +16,6 @@ uint InMemCoordSource::GetFrameCount() CoordFramePtr InMemCoordSource::GetFrame(uint frame_id) const { - return frame_id>=frames_.size() ? CoordFramePtr() : frames_[frame_id]; } @@ -31,4 +30,12 @@ void InMemCoordSource::AddFrame(const std::vector<geom::Vec3>& coords) frames_.push_back(fp); } +void InMemCoordSource::InsertFrame(int pos, const std::vector<geom::Vec3>& coords) +{ + CoordFrameList::iterator it = frames_.begin(); + for(int i=0;i<pos;++i) ++it; + CoordFramePtr fp(new CoordFrame(coords)); + frames_.insert(it,fp); +} + }} diff --git a/modules/mol/base/src/in_mem_coord_source.hh b/modules/mol/base/src/in_mem_coord_source.hh index 08388f313..67b31e95f 100644 --- a/modules/mol/base/src/in_mem_coord_source.hh +++ b/modules/mol/base/src/in_mem_coord_source.hh @@ -44,6 +44,7 @@ public: void AddFrame(const CoordFramePtr& frame); virtual void AddFrame(const std::vector<geom::Vec3>& coords); + virtual void InsertFrame(int pos, const std::vector<geom::Vec3>& coords); private: CoordFrameList frames_; -- GitLab