HMM-based estimator for selection coefficients in both single and lattice populations