contact_map.ContactCount

class contact_map.ContactCount(counter, object_f, n_x=None, n_y=None, max_size=None)[source]

Return object when dealing with contacts (residue or atom).

This contains all the information about the contacts of a given type. This information can be represented several ways. One is as a list of contact pairs, each associated with the fraction of time the contact occurs. Another is as a matrix, where the rows and columns label the pair number, and the value is the fraction of time. This class provides several methods to get different representations of this data for further analysis.

In general, instances of this class shouldn’t be created by a user using __init__; instead, they will be returned by other methods. So users will often need to use this object for analysis.

Parameters:
  • counter (collections.Counter) – the counter describing the count of how often the contact occurred; key is a frozenset of a pair of numbers (identifying the atoms/residues); value is the raw count of the number of times it occurred
  • object_f (callable) – method to obtain the object associated with the number used in counter; typically mdtraj.Topology.residue() or mdtraj.Topology.atom().
  • n_x (int, tuple(start, end), optional) – range of objects in the x direction (used in plotting) Default tries to plot the least amount of symetric points.
  • n_y (int, tuple(start, end), optional) – range of objects in the y direction (used in plotting) Default tries to show the least amount of symetric points.
  • max_size (int, optional) – maximum size of the count (used to determine the shape of output matrices and dataframes)
__init__(counter, object_f, n_x=None, n_y=None, max_size=None)[source]

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

Methods

__init__(counter, object_f[, n_x, n_y, max_size]) Initialize self.
filter(idx) New ContactCount filtered to idx.
most_common([obj]) Most common values (ordered) with object as keys.
most_common_idx() Most common values (ordered) with indices as keys.
plot([cmap, diverging_cmap, with_colorbar]) Plot contact matrix (requires matplotlib)
plot_axes(ax[, cmap, diverging_cmap, …]) Plot contact matrix on a matplotlib.axes
to_networkx([weighted, as_index, graph]) Graph representation of contacts (requires networkx)

Attributes

counter keys use index number; count is contact occurrences
df DataFrame representation of the contact matrix
sparse_matrix sparse matrix representation of contacts
counter

keys use index number; count is contact occurrences

Type:collections.Counter
df

DataFrame representation of the contact matrix

Rows/columns correspond to indices and the values correspond to the count

Type:pandas.SparseDataFrame
filter(idx)[source]

New ContactCount filtered to idx.

Returns a new ContactCount with the only the counter keys/values where both the keys are in idx

most_common(obj=None)[source]

Most common values (ordered) with object as keys.

This uses the objects for the contact pair (typically MDTraj Atom or Residue objects), instead of numeric indices. This is more readable and can be easily used for further manipulation.

Parameters:obj (MDTraj Atom or Residue) – if given, the return value only has entries including this object (allowing one to, for example, get the most common contacts with a specific residue)
Returns:the most common contacts in order. If the list is l, then each element l[e] is a tuple with two parts: l[e][0] is the key, which is a pair of Atom or Residue objects, and l[e][1] is the count of how often that contact occurred.
Return type:list

See also

most_common_idx()
same thing, using index numbers as key
most_common_idx()[source]

Most common values (ordered) with indices as keys.

Returns:the most common contacts in order. The if the list is l, then each element l[e] consists of two parts: l[e][0] is a pair of integers, representing the indices of the objects associated with the contact, and l[e][1] is the count of how often that contact occurred
Return type:list

See also

most_common()
same thing, using objects as key
plot(cmap='seismic', diverging_cmap=None, with_colorbar=True, **kwargs)[source]

Plot contact matrix (requires matplotlib)

Parameters:
  • cmap (str) – color map name, default ‘seismic’
  • diverging_cmap (bool) – Whether the given color map is treated as diverging (if True) or sequential (if False). If a color map is diverging and all data is positive, only the upper half of the color map is used. Default (None) will give correct results if cmap is the string name of a known sequential or diverging matplotlib color map and will treat as sequential if unknown.
  • with_colorbar (bool) – Whether to include a color bar legend.
  • **kwargs – All additional keyword arguments to be passed to the matplotlib.pyplot.subplots() call
Returns:

  • fig (matplotlib.Figure) – matplotlib figure object for this plot
  • ax (matplotlib.Axes) – matplotlib axes object for this plot

plot_axes(ax, cmap='seismic', diverging_cmap=None, with_colorbar=True)[source]

Plot contact matrix on a matplotlib.axes

Parameters:
  • ax (matplotlib.axes) – axes to plot the contact matrix on
  • cmap (str) – color map name, default ‘seismic’
  • diverging_cmap (bool) – If True, color map interpolation is from -1.0 to 1.0; allowing diverging color maps to be used for contact maps and contact differences. If false, the range is from 0 to 1.0. Default value of None selects a value based on the value of cmap, treating as False for unknown color maps.
  • with_colorbar (bool) – If a colorbar is added to the axes
sparse_matrix

sparse matrix representation of contacts

Rows/columns correspond to indices and the values correspond to the count

Type:scipy.sparse.dok.dok_matrix
to_networkx(weighted=True, as_index=False, graph=None)[source]

Graph representation of contacts (requires networkx)

Parameters:
  • weighted (bool) – whether to use the frequencies as edge weights in the graph, default True
  • as_index (bool) – if True, the nodes in the graph are integer indices; if False (default), the nodes are mdtraj.topology objects (Atom/Residue)
  • graph (networkx.Graph or None) – if provided, edges are added to an existing graph
Returns:

graph representation of the contact matrix

Return type:

networkx.Graph