Algebraic structure of gravity with torsion