Reusing Commands as Python Scripts in SAMSON

One common challenge for molecular modelers using interactive platforms is the repetitive nature of certain tasks: adding hydrogens, applying color schemes, changing visual presets, and more. These actions, often done through graphical menus, can be tedious when applied regularly to multiple molecular systems or during batch processing.

Wouldn’t it be helpful to turn those clicks into code that you can automate?

SAMSON, the molecular design platform, offers a simple way to convert user actions into reusable Python code. This feature not only boosts productivity but also helps standardize workflows and ensure reproducibility.

How to Extract Python Code from Commands

Let’s say you’ve applied a command like Add Hydrogens to a molecule. Instead of repeating this via the interface each time, you can get the equivalent Python code in just a few clicks.

  1. Open the Find everything search bar in SAMSON using Shift + E.
  2. Search for the command you previously executed, such as Add hydrogens.
  3. A message will appear: “This command is available as Python code”.
  4. Click the adjacent Copy icon to copy the Python code.

That’s it. You now have the exact Python code used by that SAMSON command ready for reuse.

Copy Python code for a command

What You Can Convert to Python

This feature extends to several types of SAMSON functionalities:

  • Commands: Like adding hydrogens, computing partial charges, generating surfaces, and more.
  • Color Palettes: Assign a color scheme to atoms and get the corresponding script to reapply it.
  • Visual Presets: Instantly retrieve the Python code underlying a visual style you’ve applied.

Copy Python code for a color palette

Copy Python code for a visual preset

Where to Use These Scripts

Once you’ve copied the Python code, you can:

  • Paste it directly into SAMSON’s integrated Python Console.
  • Build automated workflows in the Code Editor.
  • Embed the script within documents for distribution or future reuse.

This approach not only automates repetitive steps, but also creates reproducible research workflows—overcoming a key pain point in molecular modeling, especially in collaborative settings.

Example: Automating Hydrogen Addition

Imagine you routinely prepare structures by adding missing hydrogens. Instead of clicking through menus every time, extract and reuse the code:

Now, you can integrate that into a larger pipeline or apply it across multiple structures in a loop.

Conclusion

This small, often overlooked feature provides great value in transitioning from interactive use to programmatic control—without needing to write Python from scratch. For molecular modelers looking to streamline their workflow, convert your clicks to scripts and take control of your automation.

To learn more, explore the SAMSON Scripting documentation.

SAMSON and all SAMSON Extensions are free for non-commercial use. Get SAMSON at https://www.samson-connect.net.

Comments are closed.