-
Notifications
You must be signed in to change notification settings - Fork 2
Formats
ChemTools generally holds all data in a MolTable type format (unless working with the ChemObject
interface in which case all components are proper objects). This means converters to and from this format are necessary. There are a few of these built-in and then OpenBabel provides the rest of the support.
The core function for this is ChemFormatsConvert
which handles all delegation and format inference where possible. We can load it like so:
<<ChemTools`Formats`
The primary converters provided go to and from the ZMatrix format. Here's an example of how we can do this.
First we'll load our molecule of choice:
mox=ImportString["methyl oxirane", {"ChemEntity", "AtomsBonds"}];
Then we can simply directly convert it to a ZMatrix:
ChemFormatsConvert[mox, "ZMatrix"]
(*Out:*)
{
{"O"},
{"C", 1, 1.42099},
{"C", 2, 1.48559, 1, 1.03069},
{"C", 3, 2.59875, 2, 0.526841, 1, 1.7614},
{"H", 4, 2.18813, 3, 0.954265, 2, 0.254474},
{"H", 5, 2.5339, 4, 1.68216, 3, 0.120839},
{"H", 6, 1.8229, 5, 1.57004, 4, 0.0152169},
{"H", 7, 3.18874, 6, 1.71571, 5, 0.334627},
{"H", 8, 1.7756, 7, 1.66034, 6, -1.4759},
{"H", 9, 1.77597, 8, 1.04748, 7, -0.738643},
{1, 2, 1},
{1, 3, 1},
{2, 3, 1},
{2, 4, 1},
{2, 5, 1},
{3, 6, 1},
{3, 7, 1},
{4, 8, 1},
{4, 9, 1},
{4, 10, 1}
}
Or if we want a different ordering of our atoms and to use degrees instead of radians:
ChemFormatsConvert[mox, "ZMatrix",
"ZMatrixOrder"->
RandomSample[Range[10]],
"UseRadians"->False
]
(*Out:*)
{
{"C"},
{"H", 1, 2.22835},
{"H", 2, 2.5339, 1, 25.2407},
{"C", 3, 2.21374, 2, 25.1543, 1, -0.789906},
{"O", 4, 1.42099, 3, 68.9011, 2, -119.287},
{"H", 5, 2.9071, 4, 44.7472, 3, -168.54},
{"H", 6, 1.77597, 5, 62.2634, 4, 91.7887},
{"C", 7, 1.0948, 6, 35.76, 5, -68.1372},
{"H", 8, 1.09404, 7, 108.483, 6, -117.634},
{"H", 9, 3.18874, 8, 59.5512, 7, -68.8411},
{1, 3, 1},
{1, 4, 1},
{1, 5, 1},
{1, 10, 1},
{2, 4, 1},
{4, 5, 1},
{4, 8, 1},
{6, 8, 1},
{7, 8, 1},
{8, 9, 1}
}
We can also specify how which things our ZMatrix should be structured in terms of which atoms come where and which atoms they reference:
ChemFormatsConvert[mox, "ZMatrix",
"ZMatrixOrder"->
{
{5},
{2, 5},
{4, 5, 2},
{3, 4, 5, 2},
{7, 5, 3, 2},
{8, 5, 3, 2},
{9, 5, 3, 2},
{10, 9, 8, 7}
},
"UseRadians"->False
]
(*Out:*)
{
{"H"},
{"C", 1, 1.08001},
{"C", 1, 2.18813, 2, 39.0942},
{"C", 3, 2.59875, 1, 54.6753, 2, -16.0275},
{"H", 1, 3.12036, 4, 13.3422, 2, -47.0752},
{"H", 1, 2.54523, 4, 80.9097, 2, -41.9419},
{"H", 1, 2.51498, 4, 93.201, 2, -2.61458},
{"H", 7, 1.77597, 6, 60.0162, 5, -42.3211},
{1, 2, 1},
{1, 2, 1},
{1, 4, 1},
{2, 3, 1},
{2, 4, 1},
{3, 6, 1},
{3, 7, 1},
{3, 8, 1},
{4, 5, 1},
{4, 6, 1}
}
Along with the ZMatrix output the directly supported formats can all be exported to string. Here's the same molecule as before, but exported to a string:
ChemFormatsConvert[mox, "String"]
-----------Out-----------
10 10 0 0 0 999 V2000
0.7791 -0.7557 0.1088 O 0 0 0 0 0 0 0 0 0
-0.2134 0.0628 -0.4947 C 0 0 0 0 0 0 0 0 0
0.9769 0.6632 0.1608 C 0 0 0 0 0 0 0 0 0
-1.5426 0.0296 0.2252 C 0 0 0 0 0 0 0 0 0
-0.2946 0.0736 -1.5716 H 0 0 0 0 0 0 0 0 0
1.7518 1.0816 -0.4685 H 0 0 0 0 0 0 0 0 0
0.8599 1.1507 1.1198 H 0 0 0 0 0 0 0 0 0
-2.1133 0.9324 -0.0118 H 0 0 0 0 0 0 0 0 0
-2.1191 -0.8409 -0.1019 H 0 0 0 0 0 0 0 0 0
-1.4167 -0.0263 1.3113 H 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0
1 3 1 0 0 0
2 3 1 0 0 0
2 4 1 0 0 0
2 5 1 0 0 0
3 6 1 0 0 0
3 7 1 0 0 0
4 8 1 0 0 0
4 9 1 0 0 0
4 10 1 0 0 0
M END
We can also go from a ZMatrix to a string:
ChemFormatsConvert[mox, "ZMatrix"]//ChemFormatsConvert["String"]
-----------Out-----------
O
C 1 1.4209893560473985
C 2 1.4855889404542564 1 1.0306884050347445
C 3 2.5987451914337427 2 0.5268407789072533 1 1.761401893737616
H 4 2.1881339629922114 3 0.9542646834783888 2 0.2544737327201317
H 5 2.53389947906384 4 1.6821649783296049 3 0.12083859302191426
H 6 1.8228980525525829 5 1.570043436582211 4 0.01521690162557631
H 7 3.1887445319435677 6 1.7157081810253894 5 0.3346270130686004
H 8 1.7755969531399853 7 1.6603401387968588 6 -1.4758993686487982
H 9 1.775971047061297 8 1.0474798384052741 7 -0.7386426855032722
1 2 1
1 3 1
2 3 1
2 4 1
2 5 1
3 6 1
3 7 1
4 8 1
4 9 1
4 10 1
Other directly supported string types include "XYZ"
, "SDF"
, and "MOL2"
:
We can also convert to the Rule
-based representation Mathematica currently uses:
ChemFormatsConvert[mox, "Rules"]
(*Out:*)
{"VertexTypes" -> {"O", "C", "C", "C", "H", "H", "H", "H", "H", "H"},
"VertexCoordinates" -> {{77.91, -75.57, 10.88}, {-21.34,
6.28, -49.47}, {97.69, 66.32, 16.08}, {-154.26, 2.96,
22.52}, {-29.46, 7.36, -157.16}, {175.18, 108.16, -46.85}, {85.99,
115.07, 111.98}, {-211.33,
93.24, -1.18}, {-211.91, -84.09, -10.19}, {-141.67, -2.63,
131.13}},
"EdgeRules" -> {1 -> 2, 1 -> 3, 2 -> 3, 2 -> 4, 2 -> 5, 3 -> 6,
3 -> 7, 4 -> 8, 4 -> 9, 4 -> 10},
"EdgeTypes" -> {"Single", "Single", "Single", "Single", "Single",
"Single", "Single", "Single", "Single", "Single"},
"FormalCharges" -> {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
It can also detect when the SDF format is appropriate:
ChemFormatsConvert[{mox, mox}, "Rules"]
(*Out:*)
{"VertexTypes" -> {{"O", "C", "C", "C", "H", "H", "H", "H", "H",
"H"}, {"O", "C", "C", "C", "H", "H", "H", "H", "H", "H"}},
"VertexCoordinates" -> {{{77.91, -75.57, 10.88}, {-21.34,
6.28, -49.47}, {97.69, 66.32, 16.08}, {-154.26, 2.96,
22.52}, {-29.46, 7.36, -157.16}, {175.18,
108.16, -46.85}, {85.99, 115.07, 111.98}, {-211.33,
93.24, -1.18}, {-211.91, -84.09, -10.19}, {-141.67, -2.63,
131.13}}, {{77.91, -75.57, 10.88}, {-21.34,
6.28, -49.47}, {97.69, 66.32, 16.08}, {-154.26, 2.96,
22.52}, {-29.46, 7.36, -157.16}, {175.18,
108.16, -46.85}, {85.99, 115.07, 111.98}, {-211.33,
93.24, -1.18}, {-211.91, -84.09, -10.19}, {-141.67, -2.63,
131.13}}},
"EdgeTypes" -> {{"Single", "Single", "Single", "Single", "Single",
"Single", "Single", "Single", "Single", "Single"}, {"Single",
"Single", "Single", "Single", "Single", "Single", "Single",
"Single", "Single", "Single"}},
"EdgeRules" -> {{1 -> 2, 1 -> 3, 2 -> 3, 2 -> 4, 2 -> 5, 3 -> 6,
3 -> 7, 4 -> 8, 4 -> 9, 4 -> 10}, {1 -> 2, 1 -> 3, 2 -> 3, 2 -> 4,
2 -> 5, 3 -> 6, 3 -> 7, 4 -> 8, 4 -> 9, 4 -> 10}},
"FormalCharges" -> {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0,
0, 0, 0, 0, 0}}}
For most other format conversions OpenBabel is used. For instance, here's a conversion to the XED format:
ChemFormatsConvert[mox, "XED"]
-----------Out-----------
0.000 10 10
File conversion by Open Babel
1 2 1 3 2 3 2 4 2 5
3 6 3 7 4 8 4 9 4 10
8 0.779100 -0.755700 0.108800 10 0.0000
6 -0.213400 0.062800 -0.494700 1 0.0000
6 0.976900 0.663200 0.160800 1 0.0000
6 -1.542600 0.029600 0.225200 1 0.0000
1 -0.294600 0.073600 -1.571600 15 0.0000
1 1.751800 1.081600 -0.468500 15 0.0000
1 0.859900 1.150700 1.119800 15 0.0000
1 -2.113300 0.932400 -0.011800 15 0.0000
1 -2.119100 -0.840900 -0.101900 15 0.0000
1 -1.416700 -0.026300 1.311300 15 0.0000
1 0.0000 0 0.0000