Quick Chemical Formulas

ChemEqn app.

While teaching chemistry this year, I’ve needed a quick way to write chemical formulas. The fastest thing to do is just write it out flat–sans sub and superscripts–like Na+, or CO2-. But that’s not pretty, and introduces another potential element that could confuse.

I found that you can use Lingojam’s sub and superscript generators to copy and paste the official UTF-8 characters for pretty much all the sub and superscripts I need for chemistry, but that takes a while to do.

So instead, I put together, just in time to write my finals, a little ChemEqn app that uses keyboard shortcuts to quickly substitute in the sub and superscripts as you type.

  • [Ctrl][number]: give you the number subscripted:
    • e.g. [Ctrl][2]: gives ₂
  • [Ctrl][Shift][number]: gives superscripts:
    • e.g. Ctrl][Shift][2]: gives ²
  • [Ctrl][Shift][+]: gives a positive charge (⁺)
  • [Ctrl][Shift][-]: gives a negative charge (⁻)
  • [Ctrl][>]: gives a forward arrow (→)
  • [Ctrl][<]: gives a backward arrow (←)
  • [Ctrl][/]: gives the double arrow (⇌)

The method is not perfect, since you still have to decide which comes first when you have both a sub and superscript after an element (O₃²⁻ for example). Another issue is that when I do [Ctrl][Shift][+] and [Ctrl][Shift][-] it zooms into and out of the browser window since we’re using the default zoom shortcuts, but that’s, at least for me, a minor inconvenience.

I do like the app since it still makes for quite readable formulas that can be easily copied and pasted almost anywhere without messing up since it only uses UTF-8 characters that are pretty standard across the web (and most computer programs).

Playing with Electron Configurations

I upgraded the table part of the Electron Configuration Interactive I used in the app I made for Practicing Writing out Electron Configurations. It’s now more interactive and embeddable.

Click on the green cell (in the 3d subshell) to start adding electrons. Clicking on the previous cell will remove electrons.

The full documentation is here.

Limiting Chemical Reactions

Figuring out the limiting reactant in a chemical reaction integrates many of the basic chemistry concepts including: unit conversions from moles to mass and vice versa; the meaning of chemical formulas; and understanding the stoichiometry of chemical reactions. So, since we’ll need a number of these, I wrote a python program to help me design the questions (and figure out the answers).

Program examples come zipped because they require the program file and the elements_database.py library:

Baking Powder and Vinegar (Common Molecules)

Limiting_component-Common.py: This has the baking powder and vinegar reaction limited by 5 g of baking soda. It’s nice because it uses a few pre-defined “common molecules” (which are defined in the elements_database.py library.

You enter the reactants and products and the program checks if the reaction is balanced, then calculates the moles and masses based on the limiting component, and finally double checks to make sure the reaction is mass balanced.

Limiting_component-Common.py

from elements_database import *
import sys

print "LIMITING REACTANT PROGRAM"
print 
print "  Determines the needed mass and moles of reactants and products if reaction is limited by one of the components"

c = common_molecules()

'''Create Reaction'''
rxn = reaction()
# Add reactants (and products)
#   Use rxn.add_reactant(molecule[, stoichiometry])
#       molecule: from molecule class in elements_database
#       stoichiometry: integer number
rxn.add_reactant(c.baking_soda,1)
rxn.add_reactant(c.hydrochloric_acid,1)
rxn.add_product(c.carbon_dioxide)
rxn.add_product(c.water, 1)
rxn.add_product(c.salt)

'''Print out the reaction'''
print 
print "Chemical Formula"
print "  " + rxn.print_reaction()
print 

'''Check if reaction is balanced'''
balanced = rxn.check_for_balance(printout=True)

'''Calculate limits of reaction'''
if balanced:
    rxn.limited_by_mass(c.baking_soda, 5, printout=True)

Outputs results in the Results table (using scientific notation):

LIMITING REACTANT PROGRAM

  Determines the needed mass and moles of reactants and products if reaction is limited by one of the components

Chemical Formula
  NaHCO3 + HCl  --> CO2 + H2O + NaCl 

Check for balance
 --------------------------------------------- 
| Element | Reactants | Products | Difference |
 --------------------------------------------- 
|   Na    |    1      |    -1    |     0      |
|   H     |    2      |    -2    |     0      |
|   C     |    1      |    -1    |     0      |
|   O     |    3      |    -3    |     0      |
|   Cl    |    1      |    -1    |     0      |
 --------------------------------------------- 
Balance is:  True

Given: Limiting component is 5 g of NaHCO3.
  Molar mass = 84.00676928
  Moles of NaHCO3 = 0.0595190130849

 Results
   ------------------------------------------------------------------ 
  | Molecule | Stoich.*| Molar Mass (g) | Moles req. |    Mass (g)   |
   ------------------------------------------------------------------ 
  |NaHCO3    |    1    |    84.0068     |  5.952e-02 |   5.000e+00   |
  |HCl       |    1    |    36.4602     |  5.952e-02 |   2.170e+00   |
  |CO2       |    -1   |    44.0096     |  5.952e-02 |   2.619e+00   |
  |H2O       |    -1   |    18.0156     |  5.952e-02 |   1.072e+00   |
  |NaCl      |    -1   |    58.4418     |  5.952e-02 |   3.478e+00   |
   ------------------------------------------------------------------ 
 * negative stoichiometry means the component is a product

  Final Check: Confirm Mass balance: 
     Reactants:    7.1701 g 
     Products:    -7.1701 g 
     --------------------------
          =      0.0000 g 
     --------------------------

General Example

If not using the common molecules database, you need to define the components in the reaction as molecules yourself. This example reacts magnesium sulfate and sodium hydroxide, and limits the reaction with 20 g of magnesium sulfate.

Limiting_component-General.zip

The main file is:

Chem_Exam-limiting.py

from elements_database import *
import sys

print "LIMITING REACTANT PROGRAM"
print 
print "  Determines the needed mass and moles of reactants and products if reaction is limited by one of the components"

# create reaction

rxn2 = reaction()
# Add reactants (and products)
#   Use rxn.add_reactant(molecule[, stoichiometry])
#       molecule: from molecule class in elements_database
#       stoichiometry: integer number
rxn2.add_reactant(molecule("Mg:1,S:1,O:4"))
rxn2.add_reactant(molecule("Na:1,O:1,H:1"), 2)
rxn2.add_product(molecule("Mg:1,O:2,H:2"))
rxn2.add_product(molecule("Na:2,S:1,O:4"))

'''Print out the reaction'''
print 
print "Chemical Formula"
print "  " + rxn2.print_reaction()
print 

'''Check if reaction is balanced'''
balanced = rxn2.check_for_balance(printout=True)

'''Calculate limits of reaction'''
if balanced:
    rxn2.limited_by_mass(molecule("Mg:1,S:1,O:4"), 20, True)

# print out masses
print "Masses Involved in Reaction"
print "  Reactants:"
for i in rxn2.reactants:
    #print "rxn", i
    print "    {m}: {g} g".format(m=i.molecule.print_formula().ljust(10), g=i.mass)
print "  Products:"
for i in rxn2.products:
    #print "rxn", i
    print "    {m}: {g} g".format(m=i.molecule.print_formula().ljust(10), g=-i.mass)

This program is slightly different from the common molecules example in that, at the end, it prints out masses calculated in a more easily readable format in addition to the other data.

Masses Involved in Reaction
  Reactants:
    MgSO4     : 20.0 g
    NaOH      : 13.2919931774 g
  Products:
    MgO2H2    : 9.69066512026 g
    Na2SO4    : 23.6013280571 g

When I have some time I’ll convert this to JavaScript like the molecular mass example.

Molar Mass of Molecules

Calculate the molar mass of a molecule:

The notation for the chemical formula is a little funky: you put the element symbol and then the number of atoms separated by a colon; each element/number of atoms pair are separated by commas, so sodium chloride (NaCl) would be “Na:1,Cl:1“.

This will have to do until I can write something to parse the regular chemical formula notation.

On the plus side, you can link to a specific molecular mass calculation by adding the formula to the url. So magnesium chloride (MgCl2) can be found with this url:

https://soriki.com/js/chem/chem_db/molecular_mass.html?formula=Mg:1,Cl:2

Updated Atom Builder

A couple of my students asked for worksheets to practice drawing atoms and electron shells. I updated the Atom Builder app to make sure it works and to make the app embedable.

So now I can ask a student to draw 23Na+ then show the what they should get:

Worksheet

Draw diagrams of the following atoms, showing the number of neutrons, protons, and electrons in shells. See the example above.

  1. 14C: answer.
  2. 32K+: answer.
  3. 18O2-: answer.
  4. 4He2+: answer.
  5. 32P: answer.

I guess the next step is to adapt the app so you can hide the element symbol so student have to figure what element based on the diagram.

DNA Writer: Storing Information in DNA Exercise

DNA Writer: Translate text into a DNA code (and back again) using a simple lookup table.

I created this little DNA Writer webpage after seeing the article on scientists recording one of Shakespeare’s sonnets on DNA, I was inspired to put together something similar as an assignment for my middle-school science class to demonstrate how DNA records information. With the website to do quick translations for me, I’ll give each student the translation table and a simple message in DNA code and have them figure out the message.

Update: I’ve adapted the code to add a two to five letter sequence of non-coding DNA to the beginning and end of the message code. There’s also start and stop code as well.

The DNA sequence (or RNA in this figure) can be broken down into groups of three nucleotides called codons. Each codon codes for a specific amino acid, so the order of codons gives the sequence of amino acids in the proteins created by the DNA strand. Image by TransControl via Wikipedia.

The DNA Writer code uses a simple look-up table where each letter in the English alphabet is assigned a unique three letter nucleotide code. The three letters are chosen from the letters of the DNA bases – AGCT – similar to the way codons are organized in mRNA. Any unknown characters or punctuation are ignored.

Also, with a little tweaking, I think I can adapt this assignment to show how random mutation can be introduced into DNA sequences during transcription. Maybe break the class into groups of 4, give the first student a message as a nucleotide sequence have them copy and pass it on to the next student and so on. If I structure this as a race between the groups, then someone’s bound to introduce some errors, so when they translate the final code back into English they should see how the random mutation affected their code.

UPDATE: Non-Coding (junk) DNA: I’ve updated the code so that you have the option of adding a short (2-5 character) string of non-coding DNA to the beginning and end of each sequence.

A more personalized and printer friendly format for output.

UPDATE 2: Personalized and Printable output: Since I’m using the DNA writer to give each student a personalized message, I’ve created a button that gives “Printer Friendly Output” which will produce an individualized page with the code, the translation table, and some information on how it works, so I can print off individualized assignments more easily.

UPDATE 3: You can now get a color coded version of the sequence.

Ravenclaw’s DNA sequence color coded, and translated back to English.

Update 4: Now you can embed the nucleobase color patterns into other websites. Like so:

Update 5: Closer to the standard lettering

DNA Writer A: https://earthsciweb.org/js/bio/dna-writerA/

In constructing the codon-to-english conversion table I had to decide if I wanted to go with the standard coding (e.g. letting GTC which codes for alanine represent A) or make up a random encoding.

I opted for the random approach for a number of reasons, but the primary one was that multiple codons can code for the same amino acid. GCT, GCC, GCA, and GCG all code for alanine. This would not necessarily be a problem, except that if we respect all of the multiple encodings, we run out of codons to represent things like numbers and punctuation. A secondary reason is that U is used to represent the 21st amino acid, selenocysteine, but its codon is the same as the stop codon (Croat, 2012) and its addition to the protein chain depends on not just a single codon in the sequence.

I’ve created a hybrid option: dnaWriterA which respects the standard lettering as much as possible (based off of the inverse DNA codon table on Wikipedia). In the table below, the bolded sequences are the ones that have been reassigned.

Letter/codeAmino acidCodon
startATG
stopTAA
space (” “)GCA
.GGA
AAlaGCTGCCGCAGCG
BAsn or AspAAC
CCysTGTTGC
DAspGATGAC
EGluGAAGAG
FPheTTTTTC
GGlyGGTGGCGGAGGG
HHisCATCAC
IIleATTATCATA
JTTG
KLysAAA
LLeuCTTCTCCTACTGTTATTG
MMetATG
NAsnAATAAC
OAGG
PProCCTCCCCCACCG
QGlnCAACAG
RArgCGTCGCCGACGGAGAAGG
SSerTCTTCCTCATCGAGTAGC
TThrACTACCACAACG
UAGA
VValGTTGTCGTAGTG
WTrpTGG
XAGC
YTyrTATTAC
ZGln or GluCAACAGGAAGAG
0AGT
1GCG
2GGG
3CTG
4CCG
5CGG
6TCG
7ACG
8GTG
9GAG
Codons mapping to letters/codes used in the dnaWriterA version. The bolded sequences are the ones that have been reassigned.

I’ve also posted the code to GitHub: https://github.com/lurbano/dnaWriterA with instructions on how to adapt the sequence.

Atom Builder

This app lets you drag and drop electrons, protons, and neutrons to create atoms with different charges, elements, and atomic masses. You can also enter the element symbol, charge and atomic mass and it will build the atom for you.

Note, however, it only does the first 20 elements.