Water and molecular oxygen in the ISM and the solar system