Biowebtronics Biotech, startups, web development and internet of things.

Molecular-weightJS

My web application for the chemistry department is really focused on delivering an enjoyable user experience to the researcher, rather than the usual barely functional, ugly as hell software that we get to use.

With this in mind I wanted the app to calculate the molecular weight of a compound for the user, I think it would be a nice thing to see after you just enter the chemical formula, the app just does a bit of magic and does the sum for you.

Due to the app being javascript all the way through the stack, the code for calculating molecular weight based on a given properly formated chemical formula string looks like so:

mass = {
  "H":  1.00794,
  "He": 4.002602,
  "Li": 6.941,
  "Be": 9.012182,
  "B":  10.811,
  "C":  12.011,
  "N":  14.00674,
  "O":  15.9994,
  "F":  18.9984032,
  "Ne": 20.1797,
  "Na": 22.989768,
  "Mg": 24.3050,
  "Al": 26.981539,
  "Si": 28.0855,
  "P":  30.973762,
  "S":  32.066,
  "Cl": 35.4527,
  "Ar": 39.948,
  "K":  39.0983,
  "Ca": 40.078,
  "Sc": 44.955910,
  "Ti": 47.88,
  "V":  50.9415,
  "Cr": 51.9961,
  "Mn": 54.93805,
  "Fe": 55.847,
  "Co": 58.93320,
  "Ni": 58.6934,
  "Cu": 63.546,
  "Zn": 65.39,
  "Ga": 69.723,
  "Ge": 72.61,
  "As": 74.92159,
  "Se": 78.96,
  "Br": 79.904,
  "Kr": 83.80,
  "Rb": 85.4678,
  "Sr": 87.62,
  "Y":  88.90585,
  "Zr": 91.224,
  "Nb": 92.90638,
  "Mo": 95.94,
  "Tc": 98,
  "Ru": 101.07,
  "Rh": 102.90550,
  "Pd": 106.42,
  "Ag": 107.8682,
  "Cd": 112.411,
  "In": 114.82,
  "Sn": 118.710,
  "Sb": 121.757,
  "Te": 127.60,
  "I":  126.90447,
  "Xe": 131.29,
  "Cs": 132.90543,
  "Ba": 137.327,
  "La": 138.9055,
  "Ce": 140.115,
  "Pr": 140.90765,
  "Nd": 144.24,
  "Pm": 145,
  "Sm": 150.36,
  "Eu": 151.965,
  "Gd": 157.25,
  "Tb": 158.92534,
  "Dy": 162.50,
  "Ho": 164.93032,
  "Er": 167.26,
  "Tm": 168.93421,
  "Yb": 173.04,
  "Lu": 174.967,
  "Hf": 178.49,
  "Ta": 180.9479,
  "W":  183.85,
  "Re": 186.207,
  "Os": 190.2,
  "Ir": 192.22,
  "Pt": 195.08,
  "Au": 196.96654,
  "Hg": 200.59,
  "Tl": 204.3833,
  "Pb": 207.2,
  "Bi": 208.98037,
  "Po": 209,
  "At": 210,
  "Rn": 222,
  "Fr": 223,
  "Ra": 226.0254,
  "Ac": 227,
  "Th": 232.0381,
  "Pa": 213.0359,
  "U":  238.0289,
  "Np": 237.0482,
  "Pu": 244,
  "Am": 243,
  "Cm": 247,
  "Bk": 247,
  "Cf": 251,
  "Es": 252,
  "Fm": 257,
  "Md": 258,
  "No": 259,
  "Lr": 260,
  "Rf": 261,
  "Db": 262,
  "Sg": 263,
  "Bh": 262,
  "Hs": 265,
  "Mt": 266,
};

var mW = function (chem) {
    var s = chem.match(/([A-Z][a-z]?)(\d*)/g, chem);
    var compoundWeight = 0;
    for (var i = 0; i < s.length; i++) {
        var element = s[i].match(/([A-Z][a-z]?)/g);
        var count = s[i].match(/([0-9]*)\d/g) || 1;
        compoundWeight += mass[element] * count;
        }
    return compoundWeight
};

It first creates an object of all the atomic masses. The mW function splits up each element and quanity into different strings in an array. Then a loop iterates over each string which extracts the letters to do the look up in the atomic mass object and then it looks for the number to do the multiplication, finally the compundWeight variable is incremented.

You can find the repo on Github if you would like to contribute any changes or improvements.

I like doing code for chemistry and biology. Lets do more!

-B

comments powered by Disqus