Chemistry of molecular ions in planetary atmospheres and in laboratory plasmas