An algorithm to build mock galaxy catalogues using MICE simulations