Anisotropic magnetic couplings and structure-driven canted to collinear transitions in Sr2IrO4 by magnetically constrained noncollinear DFT