Ab initio molecular calculations including spin-orbit coupling. I. Method and atomic tests