A generic algorithm to fold proteins