Search code examples
pythonpymol

Writing Pymol internal console commands and output in txt in PymolPy3


I have been trying to get (through the pymolpy3 package happy to change) the commands and output from the internal console saved in a txt file. So far, I can see the output generated in the console when I am running the script but no output is saved.

For reference

Uimport pymolPy3
import sys



# Redirect stdout to a log file
log_file_path = r'C:\path\logs.log'
sys.stdout = open(log_file_path, 'w')

# Launch PyMOL without the GUI
pm = pymolPy3.pymolPy3(0)

# Load structure
pdb = '4PXF'


# Fetch the PDB file for your protein
pm(f"fetch {pdb}, async=0")

# Select and show only existing residues (no missing residues)
pm("select existing_residues, resi first_existing_residue-last_existing_residue")
pm("show sticks, existing_residues")

# Get the amino acid sequence of existing residues

pm("iterate_state 1, existing_residues and name CA, resn")

#Save the session

# Quit PyMOL
pm("quit")

# Close the log file
sys.stdout.close()

It returns an empty log file, whereas the console output is

 PyMOL(TM) 2.5.5 - Incentive Product
 Copyright (C) Schrodinger, LLC
 
 This Executable Build integrates and extends Open-Source PyMOL.
 Detected 12 CPU cores.  Enabled multithreaded rendering.
PyMOL>fetch 4PXF, async=0
TITLE     Crystal structure of the active G-protein-coupled receptor opsin in complex with the finger-loop peptide derived from the full-length arrestin-1
 ExecutiveLoad-Detail: Detected mmCIF
 CmdLoad: ".\4pxf.cif" loaded as "4PXF".
PyMOL>select existing_residues, resi first_existing_residue-last_existing_residue
 Selector: selection "existing_residues" defined with 2844 atoms.
PyMOL>show sticks, existing_residues
PyMOL>iterate_state 1, existing_residues and name CA, resn
'ASN'
'VAL'
'GLY'
'TRP'
'SER'
'ARG'
...
 IterateState: iterated over 334 atom coordinate states.

As you can guess I am more interested in the 3 letter combinations.

Any help is welcome, as I have been stuck in this stage for a while now. Thank you in advance!

PS. The previous stackoverflow post on something similar does not work in displaying isnide the txt the 3 letter amino acid residues

I have tried different system logs, running the automation scripts through cmd and straight into a txt file through bash commands. The previous stackoverflow answer does not work in my case


Solution

  • without launching the GUI I can save the list of aas with:

    from pymol import (
                        
                        cmd ,
                        stored,
                        )
    
    
    print('########## PYMOL VERSION ##########################################')
    print('         ',  cmd.get_version() )
    print('###################################################################')
    
    
    
    cmd.log_open('log_file.txt')
    
    
    # Load structure
    pdb = '4PXF'
    
    
    
    # Fetch the PDB file for your protein
    cmd.fetch(pdb , 'async=0')
    
    # print(a)
    
    # Select and show only existing residues (no missing residues)
    cmd.select('existing_residues', 'resi first_existing_residue-last_existing_residue')
    
               
    cmd.show('sticks', 'existing_residues')
    
    # Get the amino acid sequence of existing residues
    
    stored.a ='\n'
    
    cmd.iterate_state( 1, 'existing_residues and name CA' , 'cmd.log(resn), cmd.log(stored.a)')
    
    
    # #Save the session
    cmd.save('thisSession.pse')
    
    cmd.log_close()
    
    

    saves log_file.txt file:

    ....
    
    ASN
    ASP
    ILE
    ASP
    VAL
    MET
    GLY
    LEU
    PLM
    
    
    

    not able to save all the console output (and there is no console here !!) using How can I save command outputs in PyMOL to a txt file accepted answer

    Same result using :

    from pymol import (
                        
                        cmd ,
                        stored,
                        )
    
    
    print('########## PYMOL VERSION ##########################################')
    print('         ',  cmd.get_version() )
    print('###################################################################')
    
    
    
    import sys
    sys.stderr = open('log.txt', 'w')
    sys.stdout = open('log.txt', 'w')
    
    
    
    # Load structure
    pdb = '4PXF'
    
    # Fetch the PDB file for your protein
    cmd.fetch(pdb , 'async=0')
    
    
    
    # Select and show only existing residues (no missing residues)
    cmd.select('existing_residues', 'resi first_existing_residue-last_existing_residue')
    
    
               
    cmd.show('sticks', 'existing_residues')
    
    # Get the amino acid sequence of existing residues
    
    cmd.iterate_state( 1, 'existing_residues and name CA' , "resn")
    
    # #Save the session
    cmd.save('thisSession.pse')
    

    with output file log.txt:

    PyMOL not running, entering library mode (experimental)
    'ASN'
    'GLY'
    'THR'
    'GLU'
    'GLY'
    ....
    ....
    ....
    MET'
    'GLY'
    'LEU'
    'PLM'
    
    

    Again not able to save all the console output (and there is no console here !!) using How can I save command outputs in PyMOL to a txt file accepted answer