Having visualised a coal-char molecular model, which script discribes the no. of atoms and their molecular weight?

My model consists of carbon, hydrogen, oxygen, nitrogen and sulphur. How do write a script that counts the number of different atom groups and their corresponding molecular weights?