Optimal estimation and rank detection for sparse spiked covariance matrices