Message Boards Message Boards

Vitamins: chemical data, visualization, foods and nutritional info

Summary

In this post I will show how to create a function called VitaminData[ ] using information available in the Wolfram Knowledgebase. This custom function will provide chemical and physical data, molecule visualizations, lists of food sources rich on a specific vitamin and other nutritional data.

Function, data, uses:

  • List

By way of comparison, the vitamins in the list of entities:

EntityList@EntityClass["Chemical", "Vitamins"]

i1

The function I created has an extended list of vitamins, of course some are questionable and/or old definitions, but I found it interesting to add to the function for didactic reasons and to make the study more complete.

VitaminData["List"]

i2

  • Show

Using the function more simply we get the vitamin in the form of entity:

VitaminData["A"]

i3

With the optional “Show” we have several options, one of them is the 2D model:

VitaminData["B12", "Show" -> "Model2D"]

i4

Using “Show” for the 3D model:

VitaminData["B12", "Show" -> "Model3D"]

i5

We can visualize each bond in the molecule with "Show" and "Bonds":

VitaminData["B5", "Show" -> "Bonds"]

i6

We can count atoms and show their respective charges with "Show" and "Atoms":

VitaminData["B12", "Show" -> "Atoms"]

i7

To have the complete list of properties available in the optional “Show”, use “Properties”:

VitaminData["A", "Show" -> "Properties"]

i8

For example, the molecular mass:

VitaminData["H", "Show" -> "MolecularMass"]

i9

Another example of one of the properties from the list above is NFPALabel:

VitaminData["K1", "Show" -> "NFPALabel"]

i10

With the optional “Show” and “Data”, all properties of the above list are shown at once, interactively:

VitaminData["C", "Show" -> "Data"]

i11

  • Food

Another function option is “Food”, where the relative amount of each vitamin in a specific food is determined:

VitaminData["C", "Food" -> "Banana"]

i12

If we use the function only with “Food”, without naming the vitamin, the full list of vitamins for a specific food is shown:

VitaminData["Food" -> "Apple"]

i13

  • Nutritional Information

Another option for the function is “Nutri”, which is the nutritional information of foods in label form. Below is the example for four types of foods:

Table[VitaminData[
  "Nutri" -> n], {n, {"Banana", "Carrot", "Shrimp", "Salmon"}}]

i14

Code

  • Code of the function VitaminData[]:

    VitaminData["List"] := VitaminData["A", "Show" -> "List"];
    VitaminData[vita_, OptionsPattern[]] := 
     Module[{Vitamin, vitamins, ss, cc, a0, a1, a2, a3, a4, a5, a6, a7, 
       ai}, Options[VitaminData] = {"Show" -> "Entity"}; 
      ss = {"A", "B1", "B2", "B3", "B4", "B5", "B6", "B12", "C", "D2", 
        "D3", "H", "H1", "K1", "K3", "K5", "M", "P", "AlphaTocopherol", 
        "DAlphaTocopherol", "Niacin", "Choline"}; 
      Vitamin[vit_] := (vitamins = 
         Join[Table[
           Entity["Chemical", 
            StringJoin["Vitamin", i]], {i, {"A", "B1", "B2", "B3", "B4", 
             "B5", "B6", "B12", "C", "D2", "D3", "H", "H1", "K1", "K3", 
             "K5", "M", "P"}}], 
          Table[Entity["Chemical", 
            j], {j, {"AlphaTocopherol", "DAlphaTocopherol", "Niacin", 
             "Choline"}}]]; cc = vit /. AssociationThread[ss, Range@22]; 
        vitamins[[cc]]); a0 = Thread[{ss, Vitamin[All]}]; 
      a1 = Vitamin[vita]; 
      a2 = Dataset[
        EntityValue[Vitamin[vita], 
         EntityValue[Vitamin[vita], "Properties"], 
         "PropertyAssociation"]]; 
      a3 = MoleculePlot[Vitamin[vita], ImageSize -> Large]; 
      a4 = MoleculePlot3D[Vitamin[vita], ImageSize -> Large];
      a5 = BondList@Vitamin[vita]; a6 = Counts@AtomList@Vitamin[vita];
      a7 = Thread[{Map[Text[Style[#, Small]] &, 
          DeleteCases[
           StringReplace[
             StringDelete[
              EntityValue[Entity["Chemical", "VitaminA"], "Properties"] //
                 InputForm // ToString, {"EntityProperty[", "Chemical", 
               "]"}], {FromCharacterCode[32] -> ""}] // ToExpression, 
           ""]], EntityValue[Entity["Chemical", "VitaminA"], 
          "Properties"]}]; 
      ai = EntityValue[Vitamin[vita], OptionValue["Show"]]; 
      If[MemberQ[{"List", "Entity", "Data", "Model2D", "Model3D", "Bonds",
           "Atoms", "Mass", "Properties"}, OptionValue["Show"]] == True, 
       OptionValue["Show"] /. {"List" -> a0, "Entity" -> a1, "Data" -> a2,
          "Model2D" -> a3, "Model3D" -> a4, "Bonds" -> a5, "Atoms" -> a6, 
         "Properties" -> a7}, ai]]
    VitaminData["Nutri" -> al_] := {Entity["FoodType", al], 
       Entity[
         "Food", {EntityProperty["Food", "FoodType"] -> 
           ContainsExactly[{Entity["FoodType", al]}], 
          EntityProperty["Food", "AddedFoodTypes"] -> 
           ContainsExactly[{}]}][
        EntityProperty["Food", "NutritionLabel"]]};
    VitaminData[x_, "Food" -> y_] := 
     Module[{bb}, 
      bb = Entity[
         "Food", {EntityProperty["Food", "FoodType"] -> 
           ContainsExactly[{Entity["FoodType", y]}], 
          EntityProperty["Food", "AddedFoodTypes"] -> 
           ContainsExactly[{}]}][
        EntityProperty["Food", 
         x /. AssociationThread[{"A", "B1", "B2", "B3", "B4", "B5", "B6", 
            "B12", "C", "D2", "D3", "H", "H1", "K1", "K3", "K5", "M", "P",
             "AlphaTocopherol", "DAlphaTocopherol", "Niacin", 
            "Choline"}, {"RelativeVitaminAContent", 
            "RelativeThiaminContent", "RelativeRiboflavinContent", 
            "NoData", "NoData", "RelativePantothenicAcidContent", 
            "RelativeVitaminB6Content", "RelativeVitaminB12Content", 
            "RelativeVitaminCContent", "RelativeVitaminD2Content", 
            "RelativeVitaminD3Content", "RelativeBiotinContent", "NoData",
             "RelativeVitaminKContent", "RelativeVitaminKContent", 
            "RelativeVitaminKContent", "RelativeFolicAcidContent", 
            "NoData", "RelativeVitaminEContent", 
            "RelativeVitaminEContent", "RelativeNiacinContent", 
            "RelativeCholineContent"}]]]; 
      If[QuantityQ[bb] == True, {x, bb}, {x, "NoData"}]]; 
    VitaminData["Food" -> an_] := 
     Map[VitaminData[#, "Food" -> an] &, {"A", "B1", "B2", "B3", "B4", 
       "B5", "B6", "B12", "C", "D2", "D3", "H", "H1", "K1", "K3", "K5", 
       "M", "P", "AlphaTocopherol", "DAlphaTocopherol", "Niacin", 
       "Choline"}]
    

Food Ranking

Below we can see that there are 1816 foods in the Mathematica database as “FoodType” entities. I decided to leave this option out of the VitaminData[] function because each evaluation takes a long time to perform.

SparseArray@EntityList@Entity["FoodType"]

i15

Below are some examples of vitamin ranking lists (top 10 foods) for all foods in the database. In this case I could specify some very specific types as well, such as Beta-Tocopherol (instead of Alpha-Tocopherol, for example) and vitamin D3:

  • Vitamin C

    MaximalBy[
     Thread[{EntityList@Entity["FoodType"], 
       Map[Entity[
           "Food", {EntityProperty["Food", "FoodType"] -> 
             ContainsExactly[{#}], 
            EntityProperty["Food", "AddedFoodTypes"] -> 
             ContainsExactly[{}]}][
          EntityProperty["Food", "RelativeVitaminCContent"]] &, 
        EntityList@Entity["FoodType"]]}], Last, UpTo[10]]
    

i16

  • Vitamin A

    MaximalBy[
     Thread[{EntityList@Entity["FoodType"], 
       Map[Entity[
           "Food", {EntityProperty["Food", "FoodType"] -> 
             ContainsExactly[{#}], 
            EntityProperty["Food", "AddedFoodTypes"] -> 
             ContainsExactly[{}]}][
          EntityProperty["Food", "RelativeVitaminAContent"]] &, 
        EntityList@Entity["FoodType"]]}], Last, UpTo[10]]
    

i17

  • Vitamin B6

    MaximalBy[
     Thread[{EntityList@Entity["FoodType"], 
       Map[Entity[
           "Food", {EntityProperty["Food", "FoodType"] -> 
             ContainsExactly[{#}], 
            EntityProperty["Food", "AddedFoodTypes"] -> 
             ContainsExactly[{}]}][
          EntityProperty["Food", "RelativeVitaminB6Content"]] &, 
        EntityList@Entity["FoodType"]]}], Last, UpTo[10]]
    

i18

  • Beta-Tocopherol

    MaximalBy[
     Thread[{EntityList@Entity["FoodType"], 
       Map[Entity[
           "Food", {EntityProperty["Food", "FoodType"] -> 
             ContainsExactly[{#}], 
            EntityProperty["Food", "AddedFoodTypes"] -> 
             ContainsExactly[{}]}][
          EntityProperty["Food", "RelativeBetaTocopherolContent"]] &, 
        EntityList@Entity["FoodType"]]}], Last, UpTo[10]]
    

i19

  • Vitamin D3

    MaximalBy[
     Thread[{EntityList@Entity["FoodType"], 
       Map[Entity[
           "Food", {EntityProperty["Food", "FoodType"] -> 
             ContainsExactly[{#}], 
            EntityProperty["Food", "AddedFoodTypes"] -> 
             ContainsExactly[{}]}][
          EntityProperty["Food", "RelativeVitaminD3Content"]] &, 
        EntityList@Entity["FoodType"]]}], Last, UpTo[10]]
    

i20

These lists above were just a few examples of vitamin ranking using all foods in the database and could be extended to various other substances.

Thanks.

POSTED BY: Claudio Chaib
4 Replies

Thanks Daniel Lichtblau for the suggestion to submit the function to the W. F. R.!

Thanks J. M. for the suggestion to expand the list of vitamins available for this function.

Thanks Wolfram Function Repository team for approving the VitaminData function.

The VitaminData function has been improved and expanded to be used more fully:

“In this function there are 50 vitamins/vitamers organized into 20 vitamer groups. The function can provide the chemical and physical data of each molecule, as well as its 2D and 3D visualization, information on its chemical bonds and provide the atom counts with their respective charges. There are also 27 relative vitamin content properties among the 15 relative groups available to provide values for a specific food. The function is available to work with all 1816 food types from the Wolfram knowledgebase. This function can also generate nutritional information in the form of food labels for each of the foods. Finally, as a special option, you can also create food rankings for many types of vitamins (for all the relative vitamin content available).”

New arguments and options have been added to the function.

The function can be used with the ResourceFunction["VitaminData"] command.

List of options for the function:

ResourceFunction["VitaminData"]["OptionalsList"]

im1

Below is the option that shows the list of all vitamins/vitamers usable in the function (organized in their groups):

ResourceFunction["VitaminData"]["VitamersList"]

im2

There are many other options and uses for the function, and anyone interested can take a look at the VitaminData function page:

https://resources.wolframcloud.com/FunctionRepository/resources/VitaminData

Thanks.

POSTED BY: Claudio Chaib
Posted 5 years ago

Haven't had the time to try this out in detail, but: seeing that you separate out niacin and niacinamide, as well as the various vitamins K, it is good for your VitaminData[] function to treat vitamers separately. You might consider including more of them (e.g. hydroxocobalamin and mecobalamin for B12, and the various vitamers of B6.

POSTED BY: J. M.

Very nice. This might make for a good WFR item.

POSTED BY: Daniel Lichtblau

enter image description here - Congratulations! This post is now featured in our Staff Pick column as distinguished by a badge on your profile of a Featured Contributor! Thank you, keep it coming, and consider contributing your work to the The Notebook Archive!

POSTED BY: EDITORIAL BOARD
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract