Index: kernel/pyext/IMP.i =================================================================== --- kernel/pyext/IMP.i (revision 400) +++ kernel/pyext/IMP.i (working copy) @@ -7,6 +7,7 @@ %include "std_vector.i" %include "std_map.i" %include "std_string.i" +%include "std_pair.i" %include "IMP_macros.i" %include "IMP_exceptions.i" @@ -52,6 +53,9 @@ %pythonprepend ListRestraint::ListRestraint %{ args[1].thisown=0 %} + %pythonprepend PairListRestraint::PairListRestraint %{ + args[1].thisown=0 + %} %pythonprepend ChainTripletRestraint::ChainTripletRestraint %{ args[0].thisown=0 %} @@ -143,6 +147,7 @@ %include "IMP/restraints/BondDecoratorRestraint.h" %include "IMP/restraints/ListRestraint.h" %include "IMP/restraints/RestraintSet.h" +%include "IMP/restraints/PairListRestraint.h" %include "IMP/score_states/BondedListScoreState.h" %include "IMP/score_states/MaxChangeScoreState.h" %include "IMP/score_states/NonbondedListScoreState.h" @@ -160,7 +165,7 @@ %template(ScoreStateIndex) Index; %template(OptimizerStateIndex) Index; %template(MoverIndex) Index; - %template(BondeListIndex) Index; + %template(BondedListIndex) Index; %template(FloatKey) Key; %template(IntKey) Key; %template(StringKey) Key; @@ -168,11 +173,13 @@ %template(ResidueType) Key; %template(show_named_hierarchy) show; %template(show_molecular_hierarchy) show; - %template(Particles) ::std::vector; - %template(Restraints) ::std::vector; - %template(ScoreStates) ::std::vector; - %template(OptimizerStates) ::std::vector; - %template(ParticleIndexes) ::std::vector; + %template(Particles) ::std::vector; + %template(ParticlePair) ::std::pair; + %template(ParticlePairs) ::std::vector; + %template(Restraints) ::std::vector; + %template(ScoreStates) ::std::vector; + %template(OptimizerStates) ::std::vector; + %template(ParticleIndexes) ::std::vector; %template(FloatKeys) ::std::vector; %template(StringKeys) ::std::vector; %template(IntKeys) ::std::vector; Index: kernel/src/restraints/PairListRestraint.cpp =================================================================== --- kernel/src/restraints/PairListRestraint.cpp (revision 0) +++ kernel/src/restraints/PairListRestraint.cpp (revision 0) @@ -0,0 +1,65 @@ +/** + * \file PairListRestraint.cpp + * \brief Apply a score function to a list of pairs ofparticles. + * + * Copyright 2007-8 Sali Lab. All rights reserved. + * + */ + +#include "IMP/restraints/PairListRestraint.h" +#include "IMP/PairScore.h" +#include "IMP/log.h" + +#include + +namespace IMP +{ + +PairListRestraint::PairListRestraint(const ParticlePairs &ps, + PairScore *s) : ss_(s) +{ + add_particle_pairs(ps); +} + +PairListRestraint::~PairListRestraint(){} + +Float PairListRestraint::evaluate(DerivativeAccumulator *accum) +{ + + IMP_CHECK_OBJECT(ss_.get()); + IMP_assert(number_of_particles()%2 == 0, "There should be an even number" + << " of particles"); + Float score=0; + + for (unsigned int i=0; i< number_of_particles(); i+=2) { + score += ss_->evaluate(get_particle(i), get_particle(i+1), accum); + } + + return score; +} + + +void PairListRestraint::add_particle_pair(ParticlePair p) { + Restraint::add_particle(p.first); + Restraint::add_particle(p.second); +} + +void PairListRestraint::clear_particle_pairs() { + Restraint::clear_particles(); +} + +void PairListRestraint::add_particle_pairs(const ParticlePairs &ps) { + for (unsigned int i=0; i< ps.size(); ++i) { + add_particle_pair(ps[i]); + } +} + + +void PairListRestraint::show(std::ostream& out) const +{ + out << "Pair list restraint with score function "; + ss_->show(out); + out << std::endl; +} + +} // namespace IMP Index: kernel/src/restraints/ListRestraint.cpp =================================================================== --- kernel/src/restraints/ListRestraint.cpp (revision 400) +++ kernel/src/restraints/ListRestraint.cpp (working copy) @@ -1,17 +1,17 @@ /** * \file ListRestraint.cpp - * \brief Apply a score fgunction toa list of particles. + * \brief Apply a score function to a list of particles. * * Copyright 2007-8 Sali Lab. All rights reserved. * */ -#include - +#include "IMP/restraints/ListRestraint.h" #include "IMP/SingletonScore.h" #include "IMP/log.h" -#include "IMP/restraints/ListRestraint.h" +#include + namespace IMP { Index: kernel/include/IMP/restraints/PairListRestraint.h =================================================================== --- kernel/include/IMP/restraints/PairListRestraint.h (revision 0) +++ kernel/include/IMP/restraints/PairListRestraint.h (revision 0) @@ -0,0 +1,48 @@ +/** + * \file PairListRestraint.h + * \brief Apply a SingletonScore to each particle pair in a list. + * + * Copyright 2007-8 Sali Lab. All rights reserved. + * + */ + +#ifndef __IMP_PAIRLIST_RESTRAINT_H +#define __IMP_PAIRLIST_RESTRAINT_H + +#include "../IMP_config.h" +#include "../Restraint.h" +#include "../Particle.h" + +#include + +namespace IMP +{ + +class PairScore; + +//! Applies a PairScore to each Particle in a list. +/** \ingroup restraint + */ +class IMPDLLEXPORT PairListRestraint : public Restraint +{ +public: + //! Create the list restraint. + /** \param[in] ps The list of particle pairs to use in the restraint. + \param[in] ss The function to apply to each particle. + */ + PairListRestraint(const ParticlePairs &ps, PairScore *ss); + virtual ~PairListRestraint(); + + IMP_RESTRAINT("0.5", "Daniel Russel"); + + void add_particle_pair(ParticlePair p); + void clear_particle_pairs(); + void add_particle_pairs(const ParticlePairs &ps); + +protected: + std::auto_ptr ss_; +}; + +} // namespace IMP + +#endif /* __IMP_LIST_RESTRAINT_H */ Index: kernel/include/IMP/restraints/ListRestraint.h =================================================================== --- kernel/include/IMP/restraints/ListRestraint.h (revision 400) +++ kernel/include/IMP/restraints/ListRestraint.h (working copy) @@ -9,11 +9,11 @@ #ifndef __IMP_LIST_RESTRAINT_H #define __IMP_LIST_RESTRAINT_H -#include - #include "../IMP_config.h" #include "../Restraint.h" +#include + namespace IMP { @@ -30,7 +30,6 @@ \param[in] ss The function to apply to each particle. */ ListRestraint(const Particles &ps, SingletonScore *ss); - virtual ~ListRestraint(){} IMP_RESTRAINT("0.5", "Daniel Russel"); Index: kernel/test/restraints/test_pairlist.py =================================================================== --- kernel/test/restraints/test_pairlist.py (revision 0) +++ kernel/test/restraints/test_pairlist.py (revision 0) @@ -0,0 +1,51 @@ +import unittest +import IMP, IMP.test + +class IndexDiff(IMP.PairScore): + def __init__(self): + IMP.PairScore.__init__(self) + def evaluate(self, pa, pb, da): + d= (pa.get_index().get_index()- pb.get_index().get_index()) + print d + return abs(d) + def last_modified_by(self): + return "Me" + def version(self): + return "0.5" + def show(self, t): + print "One Singleton" + +class Linear(IMP.UnaryFunction): + def __init__(self): + IMP.UnaryFunction.__init__(self) + def __call__(self, *args): + return args[0] + def show(self, *args): + print "identity" + +class TestPairList(IMP.test.TestCase): + def setUp(self): + IMP.set_log_level(IMP.VERBOSE) + + def test_it(self): + """Test the pair list restraint""" + m= IMP.Model() + ps= IMP.ParticlePairs() + for i in range(0,10): + p= IMP.Particle() + m.add_particle(p) + if (i %2 == 1): + ps.append(IMP.ParticlePair(p, last)) + last= p + os= IndexDiff() + s= IMP.PairListRestraint(ps, os) + m.add_restraint(s) + score= m.evaluate(False) + print str(score) + self.assertEqual(score, 5, "Wrong score") + s.clear_particle_pairs() + self.assertEqual(m.evaluate(False), 0, "Should be no terms") + + +if __name__ == '__main__': + unittest.main() Index: kernel/test/restraints/test_list.py =================================================================== --- kernel/test/restraints/test_list.py (revision 400) +++ kernel/test/restraints/test_list.py (working copy) @@ -26,7 +26,7 @@ IMP.set_log_level(IMP.VERBOSE) def test_it(self): - """Test the singleton restraint""" + """Test the list restraint""" m= IMP.Model() for i in range(0,10): p= IMP.Particle()