Skip to main content

graphs and networks - Connectivity in a molecule and permutations


What am I trying to do is to make a script that calculates all chemically reasonable torsion angles in any molecule.


For one torsional angle I need 4 CONNECTED atoms, 1-2-3-4, 1-2-3 defining one plane, and 2-3-4 defining the other.


I was thinking of doing something of a form: "IF (distance between 1 and 2 is between 0,9 and 1,4 Angstroms), AND (distance between 2 and 3 is between 1 and 1,4 Angstroms), AND (distance between 3 and 4 is between 0,9 and 1,4 Angstroms), calculate torsion angle between planes defined by atoms 1-2-3 and 2-3-4".


Since I just got my hands on Mathematica, I have no idea how to do that.


Main problem is how to write a loop which would go through all permutations of 4 atoms in set of, for example, 10 atoms.


Stucture is given in XYZ format.


I would appreciate any suggestions.



Answer





You have only a set of coordinates and atom types in an XYZ file. When you import it in Mathematica you can import 3 elements: the 3D plot, the coordinates, and atom types


{plot, coords, atoms} = 
Import["https://raw.githubusercontent.com/nutjunkie/IQmol/master/share/fragments/Molecules/Amino_Acids/L-Cysteine.xyz"
, {"XYZ", {"Graphics3D", "VertexCoordinates", "VertexTypes"}}];

This is our small molecule, cysteine,


enter image description here


To work with the chemical graph, you also need the bonds. Here is an easy way we can get them manually; first we grab every single pair of atoms, using Subsets, and then use Select along with EuclideanDistance to choose only the bonds in a certain size range. Notice that the coordinates are in picometers even though the XYZ file is written in angstroms.


vertexlist = Range@Length@atoms;
bonds = Select[Subsets[vertexlist, {2}],

90 < EuclideanDistance @@ coords[[#]] < 140 &]
(* {{1, 2}, {3, 4}, {3, 5}, {6, 7}, {6, 13}, {8, 9}, {8,
10}, {10, 11}, {12, 14}} *)

Let's look at the molecule as a Graph3D,


Graph3D[vertexlist, UndirectedEdge @@@ bonds, 
VertexCoordinates -> coords/100]

enter image description here


You can see we didn't catch all the bonds that the Import function did (1.4 angstroms is shorter than many carbon-carbon or carbon-sulfur bonds). So let's use the same undocumented function that it used to infer the bonds,



bonds = UndirectedEdge @@@
Graphics`MoleculePlotDump`InferBonds[atoms, coords, 40, 25]

enter image description here


Now we have a connected molecule


chemicalGraph = 
Graph3D[vertexlist, UndirectedEdge @@@ bonds,
VertexCoordinates -> coords/200]

enter image description here




Here's a brute force method to find all the sets of 4 atoms that make a connected subgraph,


subgraphs = 
Select[Subgraph[chemicalGraph, #] & /@ Subsets[vertexlist, {4}],
ConnectedGraphQ];

Now we just need the dihedral angle for each fragment. We can use the method described here to get the dihedral angle from the three vectors. Here are some helper functions,


vectorFromEdge[edge_] := coords[[First@edge]] - coords[[Last@edge]];
vectorsFromSubgraph[sg_] := vectorFromEdge /@ EdgeList[sg];
dihedralFromVectors[{b1_, b2_, b3_}] := Module[{n1, n2},

n1 = Normalize@Cross[b1, b2];
n2 = Normalize@Cross[b2, b3];
ArcTan[n1.n2, Cross[n1, Normalize@b2].n2]
];
dihedralsFromSubgraph[sg_] := dihedralFromVectors /@
Permutations[vectorsFromSubgraph[sg]];

Now that we have the tools to get the angle, let's visualize one set of points and the planes that make the dihedral angle,


plot2 = plot /. {Sphere[a__] :> Sequence[], 
Cylinder[a__] :> Tube[a]};

Show[
plot2,
With[{points = coords[[ VertexList[subgraphs[[2]]]]]},
Graphics3D[
{Red, Sphere[#, 40] & /@ points,
Green,
InfinitePlane[points[[1]],
Most@vectorsFromSubgraph@subgraphs[[2]]],
Blue,
InfinitePlane[points[[-1]],

Rest@vectorsFromSubgraph@subgraphs[[2]]]}
]
]
]

enter image description here


and the dihedral ange shown is


dihedralFromVectors @vectorsFromSubgraph@subgraphs[[2]]
%/Degree
(* 0.973156 *)

(* 55.7577 *)

You can get all the dihedral angels from the subgraph,


dihedralsFromSubgraph@subgraphs[[2]]
(* {0.973156, -2.08642, -2.1166, 2.08642, 2.1166, -0.973156} *)

3 unique dihedrals, and then repeated but negative.


You can get all the dihedral angles and do a histogram


dihedralsFromSubgraph /@ subgraphs // Flatten // Histogram


enter image description here


If you wanted to do the same thing using a molecule from the Wolfram Knowledgebase, you can just use EntityValue to grab the graph components. Here is the distribution of dihedral angles in buckminsterfullerene


{plot, coords, atoms, bonds} = EntityValue[
Entity["Chemical", "FullereneC60"], {"MoleculePlot",
"AtomPositions", "VertexTypes", "EdgeRules"}
];
vertexlist = Range@Length@atoms;
chemicalGraph =
Graph3D[vertexlist, UndirectedEdge @@@ bonds,
VertexCoordinates -> coords/200]

subgraphs =
Select[Subgraph[chemicalGraph, #] & /@ Subsets[vertexlist, {4}],
ConnectedGraphQ];

Finding the subgraphs took a bit more time in this case. You could probably find a better way to search for them.


I made an interactive tool for querying bond angles, lengths, and dihedral angles in molecules. It is posted here on the community site


enter image description here enter image description here


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...