Pymol-Scripts / Pymol-script-repo

Collected scripts for Pymol
http://www.pymolwiki.org/index.php/Git_intro
435 stars 257 forks source link

Why Can't I Mutate The Specific Amino? #137

Closed JonasLi-19 closed 2 months ago

JonasLi-19 commented 1 year ago

Dear developer, I want to mutate all GLN&ASN to LEU of protein files to do some further research, but EVEN THOUGH the python scripts runs well without error, the output file still have all the GLN&ASN left.


import os
from pymol import cmd

source_folder = '/home/lrj/test_dataset'

for pdbid in os.listdir(source_folder):
    folder_path = os.path.join(source_folder, pdbid)
    if os.path.isdir(folder_path):
        for file in os.listdir(folder_path):
            if file.endswith('protein.pdb'):
                # reinitialize the pymol cmd
                cmd.reinitialize()
                # load file
                protein_file = os.path.join(folder_path, file)
                base_name = file.split('.')[0]
                cmd.load(protein_file, base_name)
                cmd.wizard("mutagenesis")
                # define the dict
                mut_dict = {"ASN": "LEU", "GLN": "LEU"}

                for res in cmd.get_model(base_name).atom:
                    if res.resn in mut_dict:
                        res_chain = res.chain
                        resi = res.resi
                        cmd.do(f'select res, {base_name} and chain {res_chain} and resi {resi}')
                        cmd.do(f"cmd.get_wizard().set_mode('{mut_dict[res.resn]}')")
                        cmd.do('cmd.get_wizard().apply()')
                cmd.set_wizard()
                object_list = cmd.get_object_list()
                target_obj = object_list[-1]  # Assuming the target object is the last in the list
                # update
                cmd.update(target_obj, base_name)
                # Save the output
                cmd.save(os.path.join(folder_path, f'{pdbid}_mutate0_protein.pdb'), base_name)
                cmd.delete(base_name)

AND the command terminal code output is like:

Selector: selection "res" defined with 14 atoms. PyMOL>cmd.get_wizard().set_mode('LEU') PyMOL>cmd.get_wizard().apply() PyMOL>select res, 1aaq_protein and chain B and resi 98 Selector: selection "res" defined with 14 atoms. PyMOL>cmd.get_wizard().set_mode('LEU') PyMOL>cmd.get_wizard().apply() PyMOL>select res, 1aaq_protein and chain B and resi 98 Selector: selection "res" defined with 14 atoms. PyMOL>cmd.get_wizard().set_mode('LEU') PyMOL>cmd.get_wizard().apply() PyMOL>select res, 1aaq_protein and chain B and resi 98 Selector: selection "res" defined with 14 atoms. PyMOL>cmd.get_wizard().set_mode('LEU') PyMOL>cmd.get_wizard().apply()

pslacerda commented 3 months ago

Hi @JonasLi-19, Are you still on this issue?

I'm not expert on this but I may try to help.

                for res in cmd.get_model(base_name).atom:
                    if res.resn in mut_dict:
                        res_chain = res.chain
                        resi = res.resi
                        cmd.do(f'select res, {base_name} and chain {res_chain} and resi {resi}')
                        cmd.do(f"cmd.get_wizard().set_mode('{mut_dict[res.resn]}')")
                        cmd.do('cmd.get_wizard().apply()')

Maybe the error is because you're looping by atom instead of residues (but using variable name res); you can try to skip residues if (res_chain, resi) is already on the visited (use a set).

You can also use the API's functions cmd.select and cmd.get_wizard instead of cmd.do.


Disregarding the above, I couldn't reproduce your cmd.do commands on 1E92 to mutate the first:

fetch 1e92
select res, 1e92 and chain A and resi 1
cmd.wizard("mutagenesis")
cmd.get_wizard().set_mode('PRO')
cmd.get_wizard().apply()

It keep displaying "Keep a residue...' and a rotamer should also be choosen.