### Launch local HTTP server for PyMOL examples Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/content/running.html Commands to start the local web server for serving PyMOL example files. ```bash cd ./modules/web/examples python justhttpd.py ``` ```bash ../../../../pymol -qc justhttpd.py ``` -------------------------------- ### Launch PyMOL HTTP server via command line Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/content/server.html Start the PyMOL web server as the main script during PyMOL launch. ```bash pymol $PYMOL_PATH/modules/web/pymolhttpd.py ``` -------------------------------- ### Launch PyMOL HTTP server from Python Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/content/server.html Import and start the PyMOL HTTP server within a custom Python script. ```python from pymol import pymolhttpd httpd = pymolhttpd.PymolHttpd(8080, "htdocs") httpd.start() ``` -------------------------------- ### PyMOL Command Execution Examples Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample11/index.html Examples of invoking the cmd function to perform various PyMOL operations. ```javascript cmd('set?name=suspend_updates', 'delete?name=all', 'reset', 'load?filename=$PYMOL_PATH/test/dat/1tii.pdb', 'show?representation=cartoon', 'show_as?representation=sticks&selection=chain A', 'orient?selection=chain A&animate=5', 'unset?name=suspend_updates') ``` ```javascript cmd('count_atoms') ``` ```javascript cmd('reinitialize') ``` -------------------------------- ### Initialize PyMOL and Load PDB in JavaScript Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample06/htdocs/index.html Creates a PyMOL object instance and assigns the command API to a global symbol. Use this to start controlling PyMOL from JavaScript. ```javascript var pymol = new PyMOL(); // create PyMOL object instance for server of this page ``` ```javascript var cmd = pymol.cmd; // assign a global symbol for the PyMOL cmd API ``` -------------------------------- ### Integrate PyMOL Commands with jQuery Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample16/index.html Examples showing how to use jQuery to trigger PyMOL commands via direct URLs or buffered command URLs. ```javascript $(document).ready(function() { $("a[name=load]").click(function() { try { $.getJSON(host+"/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb&_callback=?", callback1); } catch (e) { alert(e); } return false; // causes the HREF to not load a new page }); }); ``` ```javascript $(document).ready(function() { // this function assumes that a (global) pymol instance has already been created // and that there are already some commands in the command buffer. $("a[name=load]").click(function() { try { $.getJSON(host+pymol.getBufferURL()+"&_callback=?", callback1); } catch (e) { alert(e); } return false; // causes the HREF to not load a new page }); }); ``` -------------------------------- ### Load PDB, Show Cartoon, and Get Chains Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample14/index.html This function reinitializes PyMOL, loads a PDB file, sets the display style to cartoon, and then retrieves all polymer chains, passing them to the 'part2' function for coloring. This demonstrates a typical workflow for initial molecular visualization. ```javascript function part1() { cmd.reinitialize(); cmd.load("$PYMOL_PATH/test/dat/1tii.pdb"); cmd.show_as("cartoon"); cmd.get_chains(part2, "polymer"); } ``` -------------------------------- ### Load PDB File into PyMOL Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample05/htdocs/index.html Loads a PDB file into PyMOL using the 'load' command. Assumes PDB files are located in a specific example directory. ```javascript function loadPDB(code) { return cmd('load?filename=$PYMOL_PATH/modules/web/examples/data/'+code+'.pdb'); } ``` -------------------------------- ### Reinitializing PyMOL and Starting Command Cascade Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample12/index.html This function reinitializes PyMOL and then initiates a sequence of commands by calling another function. This serves as the entry point for the command cascade. ```javascript function part1() { cmd.reinitialize(part2); } ``` -------------------------------- ### PyMOL Startup Configuration (PWG) Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample15/index.html Defines PyMOL startup parameters using a simple key-value format. Options control aspects like window size, position, and quality. ```text port 8085 options -qxie -F 0 -X 10 -Y 50 -W 250 -H 250 ``` ```text port 8086 options -qxi -F 0 -X 10 -Y 350 -W 250 -H 250 ``` -------------------------------- ### Initialize jQuery PyMOL interaction Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample13/index.html Sets up the host URL and a callback function to display results in the DOM. ```javascript var pymolhost = "http://localhost:8083"; function callback1(result) { document.getElementById('result').innerHTML = "
"+result+"
"; } $(document).ready(function() { $("a[name=load]").click(function() { try { $.getJSON(pymolhost+"/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb&_callback=?", callback1); } catch (e) { alert(e); } return false; // causes the HREF to not load a new page }); $("a[name=delete]").click(function() { try { $.getJSON(pymolhost+"/apply/pymol.cmd.delete?name=pept&_callback=?", callback1); } catch (e) { alert(e); } return false; // causes the HREF to not load a new page }); $("a[name=get_names]").click(function() { try { $.getJSON(pymolhost+"/apply/pymol.cmd.get_names?type=objects&_callback=?", callback1); } catch (e) { alert(e); } return false; // causes the HREF to not load a new page }); $("a[name=badload]").click(function() { try { $.getJSON(pymolhost+"/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb"); } catch (e) { if (e.description == undefined) { alert(e); //Firefox } else { alert(e.description); //Explorer } } return false; // causes the HREF to not load a new page }); }); a.info { text-decoration:underline; background-color: transparent; } a{ text-decoration:none; padding: 1px; background-color: #dddddd } ``` -------------------------------- ### Rotate Molecule Around Axis Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample01/htdocs/api.html Rotates the molecule around a specified axis by a given angle. This example rotates around the X-axis by 45 degrees. ```python /apply/pymol.cmd.turn?axis=x&angle=45 ``` -------------------------------- ### Control Command Buffer and Execute Commands Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample16/index.html Demonstrates enabling the buffer, queuing commands, retrieving JSON/URL representations, and sending them to PyMOL. ```javascript function part1() { pymol.setBufferMode('on'); cmd.reinitialize(); cmd.load("$PYMOL_PATH/test/dat/1tii.pdb", "atestname"); cmd.show_as("cartoon"); cmd.get_chains("polymer"); jcmd = pymol.getBufferJSON(); show_message(jcmd); jurl = pymol.getBufferURL(); show_message(jurl); pymol.sendJSON(process_chains, jcmd); pymol.setBufferMode('off'); } ``` -------------------------------- ### PyMOL Basic Commands Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample01/htdocs/api.html This section covers fundamental PyMOL commands for initialization, loading structures, selecting atoms, and displaying representations. ```APIDOC ## POST /apply/pymol.cmd.reinitialize ### Description Reinitializes PyMOL to its default state. ### Method POST ### Endpoint /apply/pymol.cmd.reinitialize ### Parameters None ### Request Example (No request body needed) ### Response #### Success Response (200) (No specific response body defined, typically indicates success) #### Response Example (No specific response example provided) ``` ```APIDOC ## POST /apply/pymol.cmd.load ### Description Loads a molecular structure file into PyMOL. ### Method POST ### Endpoint /apply/pymol.cmd.load ### Query Parameters - **filename** (string) - Required - The path to the molecular structure file. ### Request Example (Query parameters are part of the URL) ### Response #### Success Response (200) (No specific response body defined, typically indicates success) #### Response Example (No specific response example provided) ``` ```APIDOC ## POST /apply/pymol.cmd.select ### Description Selects atoms based on a given selection criterion. ### Method POST ### Endpoint /apply/pymol.cmd.select ### Query Parameters - **name** (string) - Required - The name to assign to the selection. - **selection** (string) - Required - The selection criterion (e.g., atom indices, residue ranges). - **enable** (integer) - Optional - Whether to enable the selection (1 for enabled, 0 for disabled). - **quiet** (integer) - Optional - Controls the verbosity of the output. - **merge** (integer) - Optional - Whether to merge with existing selections. - **state** (integer) - Optional - The state to apply the selection to. - **domain** (string) - Optional - The domain to select. ### Request Example (Query parameters are part of the URL) ### Response #### Success Response (200) (No specific response body defined, typically indicates success) #### Response Example (No specific response example provided) ``` ```APIDOC ## POST /apply/pymol.cmd.show ### Description Shows a specific representation for selected atoms. ### Method POST ### Endpoint /apply/pymol.cmd.show ### Query Parameters - **representation** (string) - Required - The representation to show (e.g., spheres, cartoon). - **selection** (string) - Required - The selection of atoms to apply the representation to. ### Request Example (Query parameters are part of the URL) ### Response #### Success Response (200) (No specific response body defined, typically indicates success) #### Response Example (No specific response example provided) ``` ```APIDOC ## POST /apply/pymol.cmd.origin ### Description Sets the origin for a selection. ### Method POST ### Endpoint /apply/pymol.cmd.origin ### Query Parameters - **selection** (string) - Required - The selection of atoms to set the origin for. - **object** (string) - Optional - The object to use as the origin. - **position** (string) - Optional - The position to set the origin to. - **state** (integer) - Optional - The state to apply this to. ### Request Example (Query parameters are part of the URL) ### Response #### Success Response (200) (No specific response body defined, typically indicates success) #### Response Example (No specific response example provided) ``` ```APIDOC ## POST /apply/pymol.cmd.turn ### Description Rotates a selection around a specified axis. ### Method POST ### Endpoint /apply/pymol.cmd.turn ### Query Parameters - **axis** (string) - Required - The axis of rotation (x, y, or z). - **angle** (number) - Required - The angle of rotation in degrees. ### Request Example (Query parameters are part of the URL) ### Response #### Success Response (200) (No specific response body defined, typically indicates success) #### Response Example (No specific response example provided) ``` -------------------------------- ### Execute PyMOL Command Synchronously Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample05/htdocs/index.html Sends a synchronous GET request to PyMOL to execute a command and returns the JSON result. Handles potential PyMOL exceptions. ```javascript function cmd(suffix) { // apply synchronously and return JSON result req = newRequest(); req.open('get', '/apply/pymol.cmd.' + suffix + ( suffix.indexOf('?') < 0 ? '?' : '&') + "_ts=" + new Date().getTime(), false); // synchronous req.setRequestHeader("Accept", "application/json"); req.send(""); if (req.status == 500) { alert("PyMOL Exception:\n" + eval('(' + req.responseText + ')').join("\n")); return null; } return eval('(' + req.responseText + ')'); } ``` -------------------------------- ### Initialize PyMOL JavaScript API with Buffering Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample08/htdocs/index.html Enable the buffering system by passing 'on' as the third argument to the PyMOL constructor. ```javascript var pymol = new PyMOL(null, null, 'on'); // create PyMOL object with buffering enabled var cmd = pymol.cmd; // assign a global symbol for the PyMOL cmd API ``` -------------------------------- ### Initialize PyMOL Request Queue Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample03/htdocs/index.html Sets up the queue array and the active status flag for managing sequential command execution. ```javascript var pymol_queue = []; var pymol_active = false; ``` -------------------------------- ### Displaying and Getting Chains in PyMOL via JavaScript Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample12/index.html This function sets the display style to 'cartoon' and then retrieves all polymer chains, passing them to a callback function for further processing (e.g., coloring). ```javascript function part3(list) { cmd.show_as("cartoon"); cmd.get_chains(part4, "polymer"); } ``` -------------------------------- ### Specify PyMOL Launch Options Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/content/pwg.html The 'options' command allows you to pass specific launch options to PyMOL, such as quiet mode (-q) or window size (-W). ```plaintext options -q -W 250 ``` -------------------------------- ### Launch Python Module Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/content/pwg.html Use the 'launch' command to import a Python module and execute its __launch__(cmd) function, allowing for programmatic control of PyMOL. ```plaintext launch ``` -------------------------------- ### Configure PyMOL Web Server Document Root and Launch Browser Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/content/pwg.html The 'root' command sets the document root for the PyMOL web server, and the 'browser' command opens a local web browser instance pointing to the server. This is ideal for scenarios where the web page itself will manage requests to the PyMOL server. ```plaintext root $PYMOL_PATH/modules/web/examples/sample01/htdocs browser ``` -------------------------------- ### Initialize and Control PyMOL Instances Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample15/index.html Initializes PyMOL instances on specified ports and provides functions to launch, use, and quit them. Requires PyMOL JavaScript API. ```javascript var pymol8085 = new PyMOL('localhost', 8085, 'on'); var pymol8086 = new PyMOL('localhost', 8086, 'on'); ``` ```javascript function launchPymol(port) { var iframe = document.getElementById("iframe"+port); iframe.src = "start"+port+".pwg"; } ``` ```javascript function quitPymol(port) { var iframe = document.getElementById("iframe"+port); iframe.src = "http://localhost:"+port+"/apply/_quit"; } ``` ```javascript function usePymol8085() { pymol8085.cmd.reinitialize(); pymol8085.cmd.load("$PYMOL_PATH/test/dat/1tii.pdb"); pymol8085.cmd.show_as("cartoon"); pymol8085.flush(); } ``` ```javascript function usePymol8086() { pymol8086.cmd.reinitialize(); pymol8086.cmd.load("$PYMOL_PATH/test/dat/il2.pdb"); pymol8086.cmd.show_as("cartoon"); pymol8086.flush(); } ``` -------------------------------- ### Initialize PyMOL Connection with JavaScript Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample12/index.html Instantiate the PyMOL object by specifying the host and port for the PyMOL server. This is essential for cross-domain communication. ```javascript var pymol = new PyMOL('localhost',8082); // create PyMOL object instance for server of this page ``` ```javascript var cmd = pymol.cmd; // assign a global symbol for the PyMOL cmd API ``` -------------------------------- ### load Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/content/reference.html Loads a file into PyMOL. ```APIDOC ## load ### Description Loads a file into PyMOL. ### Parameters #### Query Parameters - **filename** (string) - Required - **object** (string) - Optional - Default: '' - **state** (int) - Optional - Default: 0 - **format** (string) - Optional - Default: '' - **finish** (int) - Optional - Default: 1 - **discrete** (int) - Optional - Default: -1 - **quiet** (int) - Optional - Default: 1 - **multiplex** (None/int) - Optional - Default: None - **zoom** (int) - Optional - Default: -1 - **partial** (int) - Optional - Default: 0 - **mimic** (int) - Optional - Default: 1 ``` -------------------------------- ### Initialize PyMOL JavaScript API Source: https://github.com/schrodinger/pymol-open-source/blob/master/modules/web/examples/sample16/index.html Create a PyMOL object instance and assign the command API to a global variable. ```javascript var pymol = new PyMOL('localhost',8086); // create PyMOL object instance for server of this page var cmd = pymol.cmd; // assign a global symbol for the PyMOL cmd API ``` -------------------------------- ### Molecular Surfaces and Maps Source: https://context7.com/schrodinger/pymol-open-source/llms.txt Methods to generate, color, and visualize molecular surfaces, electron density maps, and volumes. ```python from pymol import cmd # Generate surfaces cmd.show("surface", "polymer.protein") cmd.set("surface_quality", 1) cmd.set("surface_type", 0) # solid surface # Surface by property cmd.set("surface_color", "white") cmd.spectrum("b", "blue_white_red", "polymer.protein") # Cavity detection surface cmd.set("surface_cavity_mode", 1) cmd.set("surface_cavity_radius", 5) # Map generation cmd.map_new("emap", "gaussian", 1.0, "polymer.protein", 5) cmd.isomesh("mesh1", "emap", 1.0) cmd.isosurface("surf1", "emap", 1.5) # Electrostatic surface cmd.set("surface_color", "white") cmd.ramp_new("eramp", "emap", [-10, 0, 10], ["red", "white", "blue"]) cmd.set("surface_color", "eramp", "polymer.protein") # Volume rendering cmd.volume("vol1", "emap") cmd.volume_color("vol1", "rainbow") # Load crystallographic maps cmd.load("2fofc.ccp4", "map_2fofc") cmd.isomesh("mesh_2fofc", "map_2fofc", 1.0, "all", carve=2.0) ``` -------------------------------- ### Real-time Sculpting and Minimization Source: https://context7.com/schrodinger/pymol-open-source/llms.txt Interactive structure refinement tools for energy minimization and coordinate smoothing. ```python from pymol import cmd # Activate sculpting for an object cmd.sculpt_activate("molecule") # Perform sculpting iterations for i in range(100): energy = cmd.sculpt_iterate("molecule", cycles=10) print(f"Iteration {i}: Energy = {energy:.2f}") # Configure sculpting parameters cmd.set("sculpt_vdw_vis_mode", 1) cmd.set("sculpt_field_mask", 0x1FF) # all terms # Smooth trajectory coordinates cmd.smooth("all", passes=3, window=5, first=1, last=0) ``` -------------------------------- ### Visualization Settings and Configuration Source: https://context7.com/schrodinger/pymol-open-source/llms.txt Adjust global, object-specific, and rendering settings to customize the visual output. ```python from pymol import cmd # Set global settings cmd.set("cartoon_fancy_helices", 1) cmd.set("cartoon_side_chain_helper", 1) cmd.set("ray_shadows", 0) cmd.set("antialias", 2) cmd.set("depth_cue", 0) cmd.set("specular", 0.5) # Set per-object settings cmd.set("cartoon_color", "red", "chain A") cmd.set("surface_color", "blue", "chain B") # Ray tracing settings cmd.set("ray_trace_mode", 1) # normal color cmd.set("ray_trace_gain", 0.1) cmd.set("ray_opaque_background", 1) # Ambient occlusion settings cmd.set("ambient_occlusion_mode", 1) cmd.set("ambient_occlusion_scale", 15) # Display settings cmd.set("bg_color", "white") cmd.set("line_width", 2.0) cmd.set("sphere_scale", 0.3) cmd.set("stick_radius", 0.2) # Get current setting values value = cmd.get_setting_float("cartoon_transparency") print(f"Cartoon transparency: {value}") # Bond-level settings cmd.set_bond("stick_transparency", 0.5, "resn LIG") cmd.set_bond("line_color", "yellow", "chain A", "chain B") # Label settings cmd.set("label_size", 20) cmd.set("label_color", "black") cmd.set("label_font_id", 7) ``` -------------------------------- ### Run PyMOL tests from command line Source: https://github.com/schrodinger/pymol-open-source/blob/master/testing/README.md Execute the test suite using the PyMOL command-line interface in console mode. ```bash pymol -cq /path/to/pymol-testing/runall.pml ``` -------------------------------- ### Scripting and Automation Source: https://context7.com/schrodinger/pymol-open-source/llms.txt Techniques for extending PyMOL with custom commands, batch processing, and remote control via XML-RPC. ```python from pymol import cmd import os # Define custom command with @cmd.extend decorator @cmd.extend def highlight_binding_site(selection="organic", radius=5.0): """Highlight residues near a ligand""" cmd.select("binding_site", f"byres (all within {radius} of {selection})") cmd.show("sticks", "binding_site") cmd.color("yellow", "binding_site and elem C") cmd.zoom("binding_site", buffer=3) return cmd.count_atoms("binding_site") # Use the custom command n_atoms = highlight_binding_site("resn LIG", radius=4.0) print(f"Binding site has {n_atoms} atoms") # Batch processing multiple structures pdb_files = ["1abc.pdb", "2def.pdb", "3ghi.pdb"] for pdb in pdb_files: name = os.path.basename(pdb).replace(".pdb", "") cmd.load(pdb, name) cmd.align(name, "reference") cmd.png(f"{name}_aligned.png", ray=1) cmd.delete(name) # Group objects for organization cmd.group("proteins", "prot*") cmd.group("ligands", "lig*") cmd.group("proteins", "open") # expand group # Iterate with callbacks for complex analysis results = [] cmd.iterate("name CA", "results.append((model, chain, resi, resn))", space={'results': results}) for model, chain, resi, resn in results: print(f"{model}/{chain}/{resn}{resi}") # Remote control via XML-RPC (start PyMOL with -R flag) # In a separate Python process: import xmlrpc.client server = xmlrpc.client.Server('http://localhost:9123') server.do('fetch 1hpv') server.do('show cartoon') ```