Algebraic criteria for the existence of analytic first integrals