plotting molecule in R with plotly

Viewed 93

I have two dataframes, shown below, which represent a molecule. I want to produce a plot of this molecule with plot_ly.

One dataframe, df, contains coordinates and element type. The other dataframe, conn, contains connection structure between the atoms, and the number of bonds, (1, 2, or 3), which connect two atoms. For example, from row two of conn, atom 55 connects to atom 54 with a double bond.

head(df)
  atomId element     xCord     yCord     zCord
1     54       C -16.47474 -14.96508  8.332247
2     55       C -15.43503 -14.37500  7.599300
3     56       C -15.03843 -13.23943  8.285489
4     57       C -15.82452 -13.17686  9.413819
5     58       N -16.68804 -14.21907  9.439404
6     59       H -17.41647 -14.37430 10.166242

head(conn)
    atom1 atom2 order
135    58    54     1
136    55    54     2
137    71    55     1
138    70    56     1
139    55    56     1
140    58    57     1

df <- structure(list(atomId = c("54", "55", "56", "57", "58", "59", 
"69", "70", "71"), element = c("C", "C", "C", "C", "N", "H", 
"H", "H", "H"), xCord = c(-16.474736, -15.43503, -15.038428, 
-15.82452, -16.68804, -17.416475, -15.788173, -14.29002, -15.038094
), yCord = c(-14.965081, -14.375, -13.239429, -13.176858, -14.219065, 
-14.374302, -12.380838, -12.509718, -14.671816), zCord = c(8.332247, 
7.5993, 8.285489, 9.413819, 9.439404, 10.166242, 10.151304, 7.979794, 
6.636516)), class = "data.frame", row.names = c(NA, -9L))

conn <- structure(list(atom1 = c(58, 55, 71, 70, 55, 58, 56, 69, 59), 
    atom2 = c(54, 54, 55, 56, 56, 57, 57, 57, 58), order = c(1, 
    2, 1, 1, 1, 1, 2, 1, 1)), row.names = 135:143, class = "data.frame")

I want to plot this molecule in R. I was able to make a 3D scatter plot, but I am having a hard time plotting the bonds.

plot_ly(x = df$xCord, y = df$yCord, z = df$zCord, type="scatter3d", 
        mode="markers", color = df$element, hoverinfo = "text",
        text = paste('AtomId: ', df$atomId, "\nElement: ", df$element)

enter image description here

I really like this plot because it is interactive, and I can annotate the points. However, I would like to draw lines between the connections. I would like to show bond lines, such as in this image, however, I actually prefer the dots in plotly instead of giant ugly 3D spheres, as in the second picture.

enter image description here

Can anyone help me represent the bond lines in Plotly, or some other comparable package? Worst case I understand if they have to be color coded, or encoded with dashed/solid lines, ect. Id prefer to see double lines for a double bond, but will take what I can get.

Thanks!

0 Answers
Related