New Developments in Wolfram Chemistry

Jason Biggs from Wolfram Research walks through new chemistry tools in Wolfram Language, including molecule representations, formula tools, and visualization functions. The Chemistry Functions packlet adds utilities for SMILES visualization, random molecule generation, and molecular editing. Coming soon: crystal structures, protein symbolics, and computational chemistry importers.
Chapters
My name is Jason Biggs. I'm a developer for kernel chemistry functionality here at Wolfram Research. I work on both WolframAlpha and the Core product, Mathematica, as well as putting things in the function repository and the packet repository. Yeah, I think we've got a good attendance here. Right, so I'm going to give a talk with the familiar title here, new developments in Wolfram Chemistry. Right, we have here the nice, the nice word cloud of new functions I'm going to share with you today. Right, I like to start with just a general brief overview of the Wolfram language chemistry. So to start with, you've got the continually growing Wolfram knowledge base. It's a repository of curated data about types of chemicals, elements, isotopes. We've got minerals, we've got lots of different entity types. And for any given entity type, you've got various entity properties. Element pops through. Right, so you've got all of this curated information about all the elements, not all of the chemicals. Right. Because that's an uncountable set. But then we've got all of basically all the known isotopes. We have a lot of information about those as well. Starting in version 12, we also have some built in data structures in the language to help represent arbitrary chemical objects. So we've got molecule, and here's the word arbitrary again, meaning it doesn't have to belong in some database. You could construct it from scratch from your imagination. You can create a molecule object and then you can query it for its properties and use it, pass it into all different functions to visualize it or use it in computations. We've also got a molecule pattern for representing arbitrary substructures or functional groups. Right. We have a representation for a bio sequence. So this would be some kind of DNA, RNA or peptide sequence. Right. So here, I mean, let me just jump down here. So you can imagine a biosequence peptide with just a single letter here, right? So this comes out T. We could then convert that into a molecule. At this point we could look that up in the, the entity, the Wolfram knowledge base and create an entity out of that. And when we've got that, we've got, we can take that and we can ask what are your properties? And this is going to be a mixture of curated and computed properties belonging to the L3 entity in the, in the knowledge base. So you can see based so far, these have been three fairly equivalent representations of the same thing. This is a single molecule of L threonine. If you wanted to make A larger bio sequence, right. I mean it's always nice to type Gattaca, right? You can take that and you can create a molecule out of that and it's going to be much larger. And then when we look for an entity of that, we won't find it because it is not in and of itself molecule in our chemical database. Right. So if you. So those have been fairly the same, three different representations of the same type of object. But then you've got, you can have a lossier conversion where you take the. So let's see, let's start here, back up here. Peptide, threonine, turn it into a molecule and then we can take that molecule and we can ask what its chemical formula is. And so this is a much simpler object. This just represents a some type of chemical formula with a given coefficients four carbons, nine hydrogens, a nitrogen and three oxygens. From that we can ask find isomers of that. Right? And we can look for smile strings. And we'll query an external database here and we'll actually call pubchem and we'll download the list of isomers that they have for it, which can force for some formulas be a very large list. Right. So let's go ahead and collapse that and see how many. We found 665 isomers of L3N in there. And so we've got functions for comparing these different types of data structures. You can say, does this biosequence match that chemical formula? Right. And so that's the general overview of the system functionality. We've been working on some new system functionality to extend this these different types of data structures. And one that we're pretty excited about but is not quite ready and was not ready for inclusion in the upcoming wolfram language version 14 because we, because it doesn't fit in perfectly with everything else yet it really needs to be able to. We need to define, okay, so the, the data structure is a crystal structure. And so in this case you would import some CIF file which contains a structure for Andalusite in this case, and it would give you some kind of a symbolic representation of that crystal structure together with its unit cell, its space group. And from that information you can grab the atom coordinates, the labels, you can transform between fractional and cartesian coordinates, find all the symmetry operations. But again, this is just a preview. And along those same lines we're looking towards symbolic representation of the protein structure. And so that is again not quite ready for version 14. So what I'm going to talk about today are Some Wolfram language packlets, right? So these go in the Packlet repository, you can install them. Right. So these packlets I talked about last year and so they work with a packlet install Wolfram Chemistry Molecule Fingerprints, you call this. And then you have access to all of the functions there in the Molecule Fingerprints packlet. We've also got one for generating isomers, not finding isomers, which is what we saw before, but generating them from scratching just from the graph selfies is also very interesting, but I should keep moving. Protein Visualization is a packlet that was put out in the last year. It was written by Sutek Saha in our group and this Friday at 1:30 he's giving a talk on that backlet. And then finally we've got the Chemistry functions, which is just general interest chemistry functions. And that's what I'm going to be talking about today. In this we've got a few simple utility functions that are useful for when you're programming with these objects in the Wolfram language. We've got some interesting and unique visualization functions, some that are geared towards education. Overall, we've got a lot of niche functions that might not exactly make it into. Might not be right for the. The system context. Right. You wouldn't include them in the standard library of the language. One quote here is from Stephen Will we still want a molecule fingerprint as a symbol in 100 years? And that's not a question I really have an answer to, but since I don't think it's really a problem to have that as a symbol if it lives in a package. So that's what's great about the Packlet repository is the ability to expand the language in any kind of direction the users want to take it. And also we like to think of it as a testing ground for future system functionality. Basically kind of feeling out, putting in stuff that doesn't quite work perfectly yet doesn't fit in with the rest of the language as much as everything else should. And as a way to gauge user interest, if some of these you find interesting, there's lots of ways to provide feedback and let us know. Okay, so with that I'm going to kind of go through these. Not all of the functions in the Chemistry Functions packlet, but quite a few. I'm just going to show some of them are. Some of them are pretty straightforward, like this one here. So Empirical Formula was released in the first version of the Chemistry Function Packlet, which means that it's available right now. So all we need to do is we Call needs on the package context and suddenly everything is in our context path. And so what this does is it takes a returns a chemical formula object with the simplest whole number ratio of atoms for a given chemical compound, right? So this is a common problem that you might find in a general chemistry class, right? So you have, you have this dye with a composition of 75% carbon, 17% nitrogen and 6% hydrogen by mass. And so we just enter these things as an association and immediately it tells us, well, that has an empirical formula of C5 and H5. But then the homework problem goes a little bit farther and says that it has a molar mass of about 240 grams per mole. And we want to determine the molecular formula of the dye. And so we give empirical formula, two arguments, and in the second one we say 240 grams per mole and we find that it is in fact C15, H3 or N3 H15. So pretty straightforward function, but it's fairly neat. This function, format molecules as graphics is available also in the first version of the packlet. This one. Yeah, it's, it's just kind of neat, right? So we call format molecules as graphics and suddenly rather than any a summary box, when we call a molecule water, we just get the, the graphics here and it formats with a, like a blue, let's see, it's got a blue border. When you, when you highlight it, we can use, you can copy those and you can use them as input and functions. Right? Molecule plot 3d this and suddenly it's going to make that into 3d, generate the 3d coordinates, same as this. You can copy and paste these into another input cell and ask for its atom count. And there it is. If we want to turn that off, we just come down, we would say format as molecules as graphics and say false and suddenly we have, we are back to the summary box. And so this is something, yeah, I've thought it is pretty neat. But we generally like the summary box because it gives you some extra information, gives you the atom count, bond count. You can copy and paste these smiles or enchi key strings. And so they're very useful. But there's also a lot to be said for displaying things as graphic objects. Another fairly straightforward function, but very useful. Like when you're writing functions, when you're writing code, when you're writing code that takes molecules as inputs to have an assortment of random molecules to generate, to use as tests. And so here this random molecule just takes a predefined list of 100,000 smile strings from publicly Available data sets. This would be, you know, pubchem Kimball. I think that's mainly it. They're reasonably sized between 1 and 70 atoms, no ions or salts. And so here's a. We do a feature space plot to show that the distribution is relatively uniform. Keep in mind I have in no way defined distribution there. So. But yeah, in this particular embedding space, it's a relatively uniform distribution. Right. This function is kind of neat. It customize a molecule plot including undocumented options. Right. So here we could take, take some again, we'll call random molecule and we'll call this molecule Plot Options. The name is not quite. I'd like a better name for this function. But anyway, I wrote this utility for my own usage and figured it'd be neat to put it in the, in the packlet, right. So here we've, we've got an interactive widget we can or manipulate, right. We can modify how, how close your, your double bond lines are to the original bonds. You can change the thickness of the bonds. Let's see, we don't have any wedges in this particular molecule, so I won't worry about that. You can change the padding around the atom labels. You can change how small the double bonds are, right. You can choose a different coordinate generation engine. Right. So these are publicly available packages that are used to generate 2D coordinates for a molecule, right. And so they all, they're all fairly equivalent, but whenever you draw these things in 2D, there are choices to be made. And you prefer 120 degree angles, but which do you turn, right or left? And so these all kind of end up in different spots and they're all equivalent or they're not. Let's see, they're all reasonable representations of the molecule, right? So then we can come down here and combine that with the previous function format molecules as graphics. And so suddenly we set all our options the way we wanted them. We hit copy the molecule plot options and then suddenly we've got this set of options that can be then passed to molecule Plot. And in this case I actually should call set options on it so that all molecule plots will format exactly the way that you wanted them to. And then you can call, yeah, you can call set options. And then just when it, when you create a molecule, it formats as graphics using your own preferences. You can ask for its atom count. And again we're going to turn that off for the rest of the slideshow here. So these are functions, predicate functions that are useful when programming. So you write a function that works on a chemical formula. And you could write, you know, my function cf_ chemicalformula equals, you know, da da da da. In this case, you don't know, for instance that this is a valid chemical formula. You just, you're just testing that it has a chemical formula head. What you'd really like to be able to do is to put a question mark here and type chemical formula Q. And that means that anything that happens over here is going to work with a valid chemical formula. So in this case note, we can write two chemical formulas, one both with string inputs, one that's not valid, one that is valid. And we can see that the valid one formats properly, the invalid one does not. And we can test that one of them is chemical formula Q and the other is not. Another interesting useful function, empty molecule Q. Let's say you start with molecular oxygen, delete its two atoms, that leaves you with an empty molecule and it comes back as true. Embedded molecule Q is an interesting useful function. So when you create a molecule object, a symbolic molecule in the Wolfram language, and you ask to plot it in 3D, then our philosophy is to make it just work. So what happens when you ask for the 3D coordinates of a molecule? We generate them de novo, as Bob Nakbar on our team likes to say. And sometimes you don't want that. Sometimes you only want to use the coordinates that you brought in from some external program, coordinates that you have gone through a multi step optimization to find. And you don't want them to be just thrown away. So that's where the function embedded molecule Q comes in. So if I import this XYZ file, it comes back with 3D coordinates, whereas this particular mole file only has 2D coordinates. And so it comes back as false. Right. Ring molecule Q. Pretty straightforward. You can see here by selecting 200 random molecules and asking for those that are not ring molecules, we only get back about 10. So that means that the set of molecules used for random molecule is biased towards those with rings, which seems in keeping with the publicly available data sets, we can check whether two molecules are stereoisomers. Yes, they are. We can ask if they're enantiomers and then we can ask if they're diastereomers. And they can't be both enantiomers and diastereomers. So that comes back as false. This is a. Let's see. Sorry, I'm kind of hammering at these like, it's like a Robin Williams stand up. Anyway, so this is Bohr model plot. This is another this is one of the education themed functions to help you kind of look at the inside the composition of an atom. Let's see, let's make that image size a little bit bigger here. Right, so when you look here, this is a technetium. It's got, when you hover over any of the little circles that represent electrons, it'll tell you the different quantum numbers for that. The principle, the angular, angular momentum, L spin quantum numbers. That's right. Okay. And, and then it'll show the S orbitals in pink. Yeah, they're color coded. And it's fairly neat that it's fast enough that you can run it as a manipulate and go all the way up to the end of the periodic table. You can. So that one's a fairly neat function. See also periodic table plot, which is also very straightforward. So we're not going to cover it right now. This function is actually one of my. I really like this function. If you work in chem informatics or if you, if you work in a system where you're, you're grabbing molecules by the, by the thousands from external data sets, this is how they're represented by smile strings. And smile strings are fairly readable. I mean, if you just look at this, you see that's four carbon atoms in a chain. A parenthesis represents a branch, then another carbon. I mean, so it is readable, but it can become a little bit tricky to read. And that's where this function comes in handy. Right, so we plot the smile string on the top and the molecule on the bottom. And as you hover over any of the atoms in the smile string, the corresponding atom is highlighted in the molecule. And when you hover over the parentheses, the group that is highlighted is, that is the parenthetical group is highlighted. So here we've got two parentheses. We can see the main chain goes along here to here to here. Also commonly found in these smile strings are ring closures. So here we've got the first carbon atom of the smile string and then it's attached to a one. And when you come back around, you find that one again. So that one represents this ring. And so since it's essentially a linear string, you need some kind of code for wrapping back in and on yourself. So that's what these, these numbers represent. So again we've got the parentheses for the ring closure that makes this kind of an exotic smile string. It didn't have to have been written that way, but it was. And then we've got the carbon atom, the double bond, the large Parenthetical group here. Right. So you can see the main smile string, the tree goes the two rings, and then this whole section is just a parenthetical group. And then you can have. You can have multiple ring closures. So these can be pretty neat. One, two. You can see I've still got a bug to deal with here. The middle bond shouldn't be highlighted because this two really only refers to the outer envelope. Yeah. And these. And so just reading these smile strings, it can be confusing because these ring closure numbers can be reused. One, one. And so then now we. One stands for a different ring. Okay, So I like that function. Still checking. Okay, so molecule join is a very useful utility. Not, not particularly exciting, but you can take any set. So here I'm taking two molecules and I'm just joining them into one molecule with no bonds in between them. So you take ammonium, take hydroxide, and you got ammonium hydroxide. Of course, you can call. You can also get. You can also add bonds between them. So here I'm going to create a list of five tyrosine molecules. Right. I'm going to undo that. But that's. Yeah. So you start with five tyrosine molecules. You create a bond from molecule 1, atom 8, to molecule 2, atom 1, and then do the same thing going from 8 to 1, and you can end up with this linear chain of tyrosines. And then you can also just generate something kind of a little silly, like take 10 naphthalene molecules and join them in a ring. And then once you've done that, generate 3D coordinates for it. All right, let's see. So this function is pretty neat, I think. So molecule manipulate 3D. So we take the molecule, we, as soon as you call it, you generate 3D coordinates for it. And now you can modify those coordinates. I'm gonna. In all the. When I do these examples, I kind of make silly modifications, but I imagine there might be some cases where you would really need to modify a particular bond. Right. So this carbon, carbon bond is very reasonably set at 1.54 Angstroms. But if we wanted to, we could in real time here, bump it all the way out to four angstroms. Just because we like to be a little silly. I'm going to clear the selection, and then I'm going to, now that I've made it larger, I'm going to modify this dihedral angle and just swoop that around. And then at the end, once we've made the particular modifications we like, we can copy it and molecule plot 3D at the copied molecule, and it retains those silly coordinates that we just made. Okay, Molecule manipulate this. This is coming out in the new version of the Chemistry Functions packlet, which hasn't been pushed yet, but it will be within the next week, I think. It's a version of Molecule Draw, basically, that lives in your notebook and can bind dynamically to a variable. It's not as responsive as Molecule Draw, so therefore it's not in the system context. It needs a little bit of work, but it's usable. And so it's here in the packlet. And so, let's see. So we've got this Molecule Manipulate. It's here living in our notebook. Molecule Draw is modal. It pops up a dialog and that's where you do the drawing. Whereas here it just sits in your notebook. So then you can copy that or you can just paste it and it comes down here. And this is the molecule you just drawn. You can, like we said before, you can bind dynamically to a molecule, M or to a variable. So in this case, M is now an empty molecule. And so. But I will start drawing. And now we've got the 3D plot. I have 3D and then I can attach a phenyl group to the end. Yeah, there we go. This demonstration here needs a little bit more work to. For drawing in 3D, I think in real time would be pretty neat, but it is not quite there yet. Okay, so we're moving right along. Molecule grid plot. This is an interesting utility, not particularly exciting at the moment, but what's useful here is you can plot out molecules in a grid where they are drawn to the same scale and they return as one single graphics object. Molecule scaffold. Find that portion of a molecule. It's also known as a Mergo scaffold or a molecule framework. So essentially, you take off all the terminal atoms and leaving only the ring systems and then. And the bonds between them. And then you can go further by converting all the molecule, the atoms to carbon, or you can convert them all to just an asterisk, meaning it returns a molecule pattern that would match any given molecule. So if I say here, I look for the scaffold of this particular molecule. It's this connection, these two rings, a six membered and a five membered ring. And then we select 200 random molecules and find those that contain the same scaffold, then you can visualize that scaffold on the molecule. Oh, yeah. And notice I called set options previously in the notebook. So that's why these kind of Look a little funny. And then this is the function I want to end on. This is coming in the new version of the packlet import Computational chemistry data. And this one is new enough that I don't. I have not made out really cool examples for it yet, but when this packlet gets published I will have some better examples. But this will read Computational Chemistry output files from a host of different software packages that will compute electronic structure. This is done by using a Python library to read in the data. Now this should just work for macOS and Linux and on Windows it might require the user to install Python. We are trying to. The advances in the external evaluate functions in in Mathematica are advancing enough that where it should also just work on Windows it should download the appropriate Python and environment. Yeah, it's available when you install. When you load the chemistry functions packlet, it becomes available through import using the format name Computational Chemistry output. If there's any catchier name, we could come up with that. But here it is. Let's look at this. We've got an output file from nwchem run on a particular molecule and this one is on GitHub. So we're just going to import it, we're going to specify the format and we're going to say we want all the elements. This is all the information that we're able to grab from that file. Instead of just elements, we'll just. Will just grab everything. This will return an association with all the data available from that. Atomic orbital, names, partial charges, the basis set. This is some very useful information. I think a whole lot of functionality can be built on top of this. Now I think everybody wants to go to their next talk. I just want to point out that right now, yeah, we've got a new talk right now. We've got a chemistry office hour tomorrow and then Friday we've got protein visualization. Okay, so thank you everybody for coming.
More from Guy

Cheryl Strauss Einhorn | The Human Edge: Smarter Decisions in the Age of AI | Talks at Google

Rachel Glennerster | Innovation and the Center for Global Development | Talks at Google

Rebecca Winthrop | Rethinking the Purpose of Education in the Age of AI | Talks at Google

Dr. Robert Wachter | A Giant Leap: How AI Is Transforming Healthcare... | Talks at Google






