Is it possible to specify a custom constraint function in PyRosetta?
Here's a list of all the functions that we can use: https://www.rosettacommons.org/docs/latest/rosetta_basics/file_types/constraint-file
However, none of those functions match my use case.
Category:
Post Situation:
Hello,
if you would like to specify a custom constraint function in pyrosetta you will need to do it on the c++ side, and then recompile your pyrosetta installation. I reccommend looking at how the harmonic function is implemented for direction!
also -- do not forget that you can chain many functions together (via sumfunc) which might help you avoid writing c++ code
~Dan
Just in case anyone else was curious about why it is impossible —or politicians have made one distrust everything read—, here is a wee experiment I did naïvely —I have not used pybind11 or boost— that proves why it is impossible.
Basically, Func is a pybind11 class with a method called func, which accepts a float and returns another based on the values it got from instantiation. It has other methods, such as dfunc gradient one.
If you pass `pyrosetta.rosetta.core.scoring.constraints.AtomPairConstraint` a python function (def or lambda) it does not like its type.
Masking its type (code snippet below, but without inheriting), ``pyrosetta.rosetta.core.scoring.constraints.AtomPairConstraint`` still dislikes its type by saying `<pyrosetta.rosetta.core.scoring.func.Func object at 0x........>` is not `pyrosetta.rosetta.core.scoring.func.Func`.
Making the mock function inherit `pyrosetta.rosetta.core.scoring.func.Func` makes the constraint accept it, but when calling a scorefun (or `print(con)`) with the constraint set it raises a RuntimeError as tries to call pure virtual function "Func::func", ie. we did not actually affect the C++ level it as this happens with `pyrosetta.rosetta.core.scoring.func.Func().func(2.3)`.
However, it might be possible to write a pybind11-wrapped C++ object that accepts a python function and calls it as discussed in https://pybind11.readthedocs.io/en/stable/advanced/cast/functional.html —but even if doable would require compiling pyrosetta or a class with the same functionality as core::scoring::func::Func.