Path A - Module 51: Writing Your Own Form-Agnostic Functions#

You have used MolSysMT functions for 46 modules. Now, it’s time to write your own.

The secret of MolSysMT’s power is its Digestion System. In this module, you will learn to use the @arg_digest decorator to make your custom scripts accept any molecular form automatically. You will write code that is as powerful as the framework itself.

import molsysmt as msm
from molsysmt._private.argdigest import arg_digest

1. The Problem: Manual Conversion#

Imagine you write a function to calculate the length of your KLVFF peptide. Usually, you would have to check if the input is a file, a string, or an object, and convert it yourself. That’s a lot of boring code.

2. The Solution: @arg_digest#

By adding this decorator, you tell MolSysMT: “Make sure the molecular_system argument is converted to a specific form before it enters my function.”

@arg_digest()
def calculate_peptide_span(molecular_system):
    """
    Custom function to calculate the distance between the N and C terminals.
    Thanks to arg_digest, inside this function, 'molecular_system' is ALWAYS 
    already processed and recognized.
    """
    # We pick the first and last C-alpha atoms
    ca_atoms = msm.select(molecular_system, selection='atom_name=="CA"')
    first_ca = ca_atoms[0]
    last_ca = ca_atoms[-1]
    
    distance = msm.get_distances(molecular_system, selection=first_ca, selection_2=last_ca)
    return distance

print("Custom form-agnostic function defined.")

3. Agnosticism in Action#

Now look how our simple function can handle anything!

# 1. Using a PDB ID string
span_1 = calculate_peptide_span('pdb:1VII')
print(f"Span from PDB ID: {span_1}")

# 2. Using a native MolSys object
molsys = msm.build.build_peptide('KLVFF')
span_2 = calculate_peptide_span(molsys)
print(f"Span from Native Object: {span_2}")

# 3. Using a dictionary (MolSysDict)
molsys_dict = msm.convert(molsys, to_form='molsysmt.MolSysDict')
span_3 = calculate_peptide_span(molsys_dict)
print(f"Span from Dictionary: {span_3}")

🏆 Path A Challenge: The Tool Creator#

  1. Write a custom function called audit_my_complex.

  2. Decorate it with @arg_digest().

  3. Inside the function, use msm.info() and msm.physchem.get_mass() to return a summary of the system.

  4. Test your function by passing it a list of items: ['pdb:2BEG', {}] (The Piped Model from Module 8).

You are no longer just a user; you are a MolSysMT Developer. In Module 52, we will learn how to handle errors and debug your scripts using SMonitor.