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

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

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[ ...