1

I would like to have new molecules imported in pymol to be preset as 'ball and stick'. I cannot find a way to make this a default setting, preferably in the pymol.rc.

In the command line,

preset.ball_and_stick(selection='all', mode=1)

is what gets the job done.

However, since the command relies on a selection, it has no effect in the pymol.rc, because it is loaded before any imports.

Also

set stick_ball, on

has no effect.

1 Answers1

0

wasnt able to find the setting either, only ways I could figure out are at

script or plugin level.

For the script option:

from pymol import cmd

def show_lines():

    cmd.hide('all')

    cmd.set('stick_ball' , 'on')
                    
    cmd.set('stick_ball_ratio' , '1.5')
    
    cmd.show('sticks' , 'all')

cmd.extend("show_lines", show_lines)

you need to run the script from the File Menu (maybe run it at startup ?? from pymol.rc or as arg ). Problem is that what it does is just adds a new pymol command show_lines that hides all the object loaded as cartoons and show them as sticks.

For the plugin one , you need to add a folder in your plugin_installation_folder that contains the below __init__.py file, and enable the script at startup from the Plugin/Plugin Manager Menu :

'''
PyMOL .............................. Plugin

'''
from pymol import cmd

from pymol.Qt import QtWidgets 


def getLoadFileNameWithExt(*args, **kwargs):
    """
    Return a file name, append extension from filter if no extension provided.
    """

    fname, filter = QtWidgets.QFileDialog.getOpenFileName(*args, **kwargs)

    if not fname:
        return ''

    return fname

def __init_plugin__(app=None):
    '''
    Add an entry to the PyMOL "Plugin" menu
    '''
    from pymol.plugins import addmenuitemqt
    
    addmenuitemqt('Load_Lines', run_plugin_gui)


#### ATTENZIONE A GLOBAL DIALOG SUPER NECESSARIO ############
# global reference to avoid garbage collection of our dialog
dialog = None

def run_plugin_gui():
    '''
    Open our custom dialog
    '''
    global dialog

    if dialog is None:
        dialog = make_dialog()

    dialog.show()

class Window(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        
        self.resize(400,400)
    
        self.Winlayout = QtWidgets.QVBoxLayout()
        
        self.setLayout(self.Winlayout)
        
        self.button_browse = QtWidgets.QPushButton('Load pdb as Sticks')
        
        self.Winlayout.addWidget(self.button_browse)
    
        self.button_browse.clicked.connect(self.browse_filename)
        
    def browse_filename(self):
        
        self.loaded =  cmd.get_names(type = 'objects', enabled_only = 0)
        
        print('loaded objects : \n', self.loaded)
        
        self.filename = getLoadFileNameWithExt(
            self, 'Load ...', filter='PDB File (*.pdb)')
        
        if self.filename:
            print(self.filename)
            
            cmd.load(self.filename)
            
            self.loaded_after = cmd.get_names(type = 'objects', enabled_only = 0)
            
            for i in self.loaded_after:
                
                if i not in self.loaded:

                    cmd.set('stick_ball' , 'on')
                    
                    cmd.set('stick_ball_ratio' , '1.5')
                    
                    cmd.hide('cartoon' , i)
                    
                    cmd.show('sticks' , i )

def make_dialog():

    dialog = Window()
    
    return dialog


Running the Plugin will open a window:

enter image description here

pressing the button will open a load_dialog (for just.pdb , but that could be altered) , before loading the new object the plugin-script keeps track of the already loaded objects, and then after loading the new one , it only hide the cartoons and shows sticks for this one, need to figure out how to select multiple pdb for loading as in the pymol File/Open Menu , but thats another question.

EDITED

code to be changed to load multiple .pdbs at once:

fname, filter = QtWidgets.QFileDialog.getOpenFileName(*args, **kwargs)

changes in : fname, filter = QtWidgets.QFileDialog.getOpenFileNames(*args, **kwargs) just one s more;

and :

...

if self.filename:
            print(self.filename)
            
            cmd.load(self.filename)
            
            self.loaded_after = cmd.get_names(type = 'objects', enabled_only = 0)
            
            for i in self.loaded_after:
                
                if i not in self.loaded:
                    

.....

changed in :

...
for filez in self.filename:

                print(filez)
                
                cmd.load(filez)
                
        self.loaded_after = cmd.get_names(type = 'objects', enabled_only = 0)
                
        for i in self.loaded_after:
                    
            if i not in self.loaded:
                        

...

You can play with:

cmd.set('stick_ball' , 'on')
                    
cmd.set('stick_ball_ratio' , '1.5')

for the representation that suits you better see: https://pymol.org/dokuwiki/doku.php?id=setting:stick and other googling resources

pippo1980
  • 2,181
  • 3
  • 14
  • 30