schrodinger.application.desmond.util module

Utility functions and classes that may make your coding life easier and cleaner.

The functions and classes fall into the following categories.

Debug and testing:
  • TestSuite - A lightweight unit-testing framework
GUI programming:
  • Counter - A counter that automatically increments the count upon each reading of the count. This class is especially useful in GUI programming to avoid, e.g., hard code in ‘grid’ functions.
  • busy_icon_exec - Executes a command, which is either a Python callable object or a string representing a shell command, and changes the mouse icon to a busy icon during the execution process and changes it back to the previous one when the process is done.
  • parse_geometry - Parses a standard X window geometry string and returns the width, height, and postion of the window in a list of integers.
  • resize_window
  • resize_window2 - Adjusts a given window to its requested size. See for details their own docstrings.
Maestro programming:
  • get_entry_id - Gets the ID of an entry.
File manipulation:
  • remove_file - Tries to remove a file or files and does not throw exceptions when the file or files do not exist.
  • write_n_ct - Writes a list of CTs to a single file.
  • chdir - Changes the current directory to the specified one. If the argument is ‘..’, changes to the parent directory.
  • parent_dir - Returns the parent directory name.
  • relpath - Gets a relative path.
  • symlink - Creates a symbolic link using relative path.
  • append_comment - Appends a string as a comment to a given file.
String manipulation:
  • random_string - Returns a random string.
  • getlogin() - Returns the login name. This was created to overcome the limitation/bug of the standard python function ‘os.getlogin()’.
Misc:
  • print_if - Prints if the condition is true.

Copyright Schrodinger, LLC. All rights reserved.

class schrodinger.application.desmond.util.TestSuite(title)

Bases: object

A small and tight class designed for unit tests.

With this utility, unit test code can be written much faster than with the ‘unittest’ module.

__init__(title)

Sets the title of the suite to ‘title’ and all internal counters to 0.

run()

Performs testing of all test cases accumulated so far and print information indicating a particular case is passed or failed. The user can explicitly call this function as many times as like. Already tested cases will not be tested again. This means that the user can do something like this:

tsuite = TestSuite( 'util' )
...
tsuite << case1
tsuite.run()    # Testing of 'case1' will be performed
tsuite.run()    # Allowed, but meaningless since 'case1' will be tested again.

and so this:

tsuite << case1
tsuite.run()
tsuite << case1
tsuite.run()    # Allowed. The 'tsuite' takes the 2nd 'case1' as a new case and performs the testing.

The user usually does NOT need to call this function explicitly because when the ‘TestSuite’ object is about to be destructed, it will automatically check and test any untested cases.

class schrodinger.application.desmond.util.Counter(val=0)

Bases: object

This class was originally designed for the convenience of gridding widgets. For example, instead of writing a code like:

my_label1.grid( row = 1, sticky = W )
my_label2.grid( row = 2, sticky = W )
my_label3.grid( row = 3, sticky = W )

we can avoid the hardcoding (‘row = 1’, etc., which is generally bad and hard to maintain) using this class. The improved code will look like:

row_index = Counter()
my_label1.grid( row = row_index.val, sticky = W )
my_label2.grid( row = row_index.val, sticky = W )
my_label3.grid( row = row_index.val, sticky = W )

which is equivalent to the above code, but generally easier to write and modify. The trick is that the property ‘val’, when read, will return the current value of the internal counter and then increment the counter (not the returned value) by 1.

If the user just wants to get the current value of the counter but not want to change it, s/he can do either one of the two:

  1. Use the ‘va_’ property,
  2. Explicitly convert the object to ‘int’.
__init__(val=0)

Constructs the object. One can provide a value ‘val’ to initialize the internal variable. For example:

row_index = Counter( 2 )

will let the counter start from 2, instead of 0 (default value).

reset(val=0)

Resets the counter to ‘val’.

val

Readonly. When read, this returns the value of the current count and then increment the count by 1. The incrementation does not affect the returned value.

va_

Readonly. When read, this returns the value of the current count without changing the internal state whatsoever of the object.

class schrodinger.application.desmond.util.InfAutofill

Bases: object

__init__

Initialize self. See help(type(self)) for accurate signature.

class schrodinger.application.desmond.util.EntryRestrictor(excl_char='', incl_char=None, empty_ok=False, check=None)

Bases: object

Similar to EntryFieldRestrictor, but for the ‘Entry’ widget instead of the ‘EntryField’ widget.

__init__(excl_char='', incl_char=None, empty_ok=False, check=None)

Note that ‘excl_char’ has higher priority (means that if a char is in ‘excl_char’, it will NOT be allowed to put into the field even though it is in ‘incl_char’).

Parameters:
  • excl_char – A string containing the chars that will not be allowed to put into the entry field.
  • incl_char – A string containing the chars that will be allowed to put into the entry field. ‘incl_char’ can be None, which means any char is allowed.
schrodinger.application.desmond.util.busy_icon_exec(master, command)

Executes the ‘command’, waiting until it finishes and returning what is returned from the command.

During the execution process, the icon is automatically changed to the ‘watch’ type of icon and recover to the previous icon when the command is finished.

‘command’ can be either of:
1. a string, in this case the string must be executable under a subprocess. The string normally contains the command and all necessary options, and there must be one or more spaces separating the command and options. 2. a python function with no argument.
schrodinger.application.desmond.util.parse_geometry(geometry)

Parses a standard X window geometry string (e.g., ‘35x70+0-0’) to a list of integers (e.g., [35, 70, 0, 0] for the previous string).

The list of integers are in the order: width, height, horizontal offset, vertical offset.

schrodinger.application.desmond.util.resize_window(win)

Resizes the width of a given application window ‘win’ to its requested size. The width of the window is not affected.

schrodinger.application.desmond.util.resize_window2(win, should_reset_minsize=False)

Resizes a given application window ‘win’ to its requested size. Both the height and width may be changed.

schrodinger.application.desmond.util.is_parent(parent, child)

Returns True if ‘parent’ is the parent widget of the ‘child’, or False otherwise.

schrodinger.application.desmond.util.get_entry_id(entry_name)

Returns the ID of an entry whose entry name being ‘entry_name’, or None if there is no such an entry.

Note that if there are more than one entries have the same name of ‘entry_name’, the entry id of the first one will be returned.

schrodinger.application.desmond.util.remove_file(basename, prefix=None, suffix=None)

Deletes an existing file whose name is ‘fname’. No effect if the file does not exist.

schrodinger.application.desmond.util.write_n_ct(fname, struc)

Writes a list of CTs to a file with the name as given by ‘fname’.

The CTs in the output file are in the same order as in the list. The list can contain None elements, which will be ignored. This function has no effect if the ‘struc’ is an empty list or contains only Nones.

schrodinger.application.desmond.util.chdir(dir_name)

Changes the current directory to the one of the name ‘dir_name’. If ‘dir_name’ is ‘..’, then it will change to the parent directory, and this is is done in a portable way.

schrodinger.application.desmond.util.parent_dir(dir_name, up=1)

Returns the parent directory name.

Parameters:up – This should be a non-negative integer value indicating the parent along the path. Default value is 1, indicating the immediate parent. Value 2, for example, indicates the parent of the immediate parent directory.
schrodinger.application.desmond.util.relpath(xpath, refpath=None)

Given two paths (‘xpath’ and ‘refpath’), returns the relative path of ‘xpath’ with respect to ‘refpath’.

Both ‘xpath’ and ‘refpath’ can be relative or absolute paths, and ‘refpath’ defaults to the current directory if it is not provided.

Creates a symbolic link on the current directory to a file as given by ‘src_fname’.

This differs from ‘os.symlink’ in that this function creates a symbolic link using a relative path. Also if there is already a file with the same name as ‘des_fname’, this function will try to delete the file and then create symbolic link. An exception will be raised, if this attemp fails.

Parameters:
  • src_fname – The name of the file to link to.
  • def_fname – The name to use for the symbolic link.
schrodinger.application.desmond.util.is_subdir(xpath, refpath=None)

Given two paths (‘xpath’ and ‘refpath’), returns True if ‘xpath’ is a direct or indirect subdirectory of ‘refpath’. Also returns True if ‘xpath’ and ‘refpath’ are the same.

Both ‘xpath’ and ‘refpath’ can be relative or absolute path, and ‘refpath’ defaults to the current directory if it is not provided.

schrodinger.application.desmond.util.append_comment(fname, comment)

Appends a string ‘comment’ to a file ‘fname’. A char ‘#’ will be automatically added to the head of the string.

Parameters:comment – A string or a list of strings. If it is a list of strings, each string will be appended as a separate comment.
schrodinger.application.desmond.util.random_string(n, char_pool='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')

Returns a random string with ‘n’ chars. The ‘n’ chars will be taken from a pool of chars as given by ‘char_pool’.

schrodinger.application.desmond.util.getlogin()

Returns the login name if it can be found, or otherwise ‘unidentified_user’.

Note: ‘os.getlogin’ seems to have a bad dependency on the terminal in use, and as such exception can be thrown when the terminal does not provide the requested login information. This function is to solve this problem by trying three different ways to get the login name, and if all ways failed it will return a valid string that tells the login name can not be identified.

schrodinger.application.desmond.util.expand_macro(s, macro_dict)

Replaces the macros in the string ‘s’ using the values given by the macro dictionary ‘macro_dict’. The expanded string will be returned.

Macro conventions:
  • All macros should start with a single ‘$’, followed by capital letters, e.g., “$JOBNAME”, “$USERNAME”.
  • Optional macros should be bracketed by ‘[]’, e.g., “myjob[_lambda$LAMBDANO]”, where “$LAMBDANO” is an optional macro.
  • If optional macros are not expanded, the bracketed part of the string will be discarded.
  • Macro values should not contain the following chars: ‘[‘, ‘]’, and ‘$’.
schrodinger.application.desmond.util.print_if(condition, content)

Prints the string ‘content’ if ‘condition’ evalutes True.

schrodinger.application.desmond.util.fatal(msg: str) → None
schrodinger.application.desmond.util.ensure_file_exists(fn: str) → None

Ensure that the file exists and is not empty.

Raises:SystemExit – If file not found or is empty.
schrodinger.application.desmond.util.time_duration(start_time, end_time, scale=1)

Given the start time and the end time, returns a string that says the duration between the two time points in the format of ‘xh y’ z”’, where ‘x’, ‘y’, and ‘z’ are hours, minutes, and seconds, respectively.

Parameters:
  • start_time – A time in seconds since the Epoch (i.e. a value as returned by the ‘time.time()’ function).
  • end_time – A time in seconds since the Epoch (i.e. a value as returned by the ‘time.time()’ function).
schrodinger.application.desmond.util.get_product_info(product_name)
Returns a tuple with the following elements:
  • 0 - the exec dir
  • 1 - the lib dir
  • 2 - the version number
  • 3 - the platform

All elements are strings.

schrodinger.application.desmond.util.is_powerof2(x)

Returns True if ‘x’ is a power of 2, or False otherwise.

schrodinger.application.desmond.util.html_embed_image(url)
schrodinger.application.desmond.util.unique(li)

Returns a list containing unique elements from li and at the same time preserving the order of the elements as in li.

schrodinger.application.desmond.util.get_indices(slice_list, length)

Returns unique indices from a list of slice objects. index that is invalid will be excluded.

schrodinger.application.desmond.util.parse_slice(s, inclusive=False)

Parses slice expression and returns slice object. Raise ValueError if invalid slice expression is passed to this function. By default, the slice expression is identical to python’s which is half open. If inclusive is set to true, the slice expression will become full close one. For instance, “1:3” <==> [1, 2, 3].

schrodinger.application.desmond.util.get_traj_filename(basename: str) → Optional[str]
schrodinger.application.desmond.util.parse_edge_file(fname: str) → List[Tuple[str, str]]
Rules:
  1. An edge is identified by its ID, which is a string of the two node IDs separated by ‘_’.
  2. Each node ID can be either a full ID or a short ID.
  3. Each line in the .edge file should contain at most 1 edge ID.
  4. Lines containing only white spaces are allowed, and they will be ignored by the parser.
  5. A comment is a string that starts with ‘#’ and ends with ‘
‘,
and it will be ignored by the parser.
return:A list of edge IDs from the parsed edge file.
schrodinger.application.desmond.util.parse_ligand_file(fname: str) → List[str]
Parse a ligand file with the following format:
  1. A ligand is identified by the structure title.
  2. Lines containing only white spaces are allowed, and they will be ignored by the parser.
  3. Comments are not allowed.
Returns:A list of structure titles.
schrodinger.application.desmond.util.write_ligand_file(fname: str, cts: List[schrodinger.structure.Structure]) → None

Given a list of structures, write a file containing the ligand titles.

Parameters:
  • fname – Path for output.
  • cts – List of structures.