Source code for schrodinger.application.bioluminate.residue_scanning.properties
import csv
from typing import Dict
from typing import List
from typing import Union
from schrodinger import structure
from schrodinger.utils import csv_unicode
try:
from schrodinger.application.prime.packages import properties as psp_props
except ImportError:
psp_props = None
BIOLUM_NONE_STR = 'NONE'
PropertyDict = Dict[str, Union[str, float]]
[docs]def write_props_to_csv(sts: structure.Structure, file_name: str) -> None:
"""
Write the mutation property and all delta properties for each provided
structure to CSV.
"""
csv_rows = _assemble_csv_rows(sts)
if len(csv_rows) == 0:
return
sample_fieldnames = csv_rows[0].keys()
with csv_unicode.writer_open(file_name) as csv_handle:
writer = csv.DictWriter(csv_handle, fieldnames=sample_fieldnames)
writer.writeheader()
for csv_row in csv_rows:
writer.writerow(csv_row)
def _assemble_csv_rows(sts: List[structure.Structure]) -> List[PropertyDict]:
"""
Return a list of dicts that each represent one row of data in a CSV. Each
input structure will be made into exactly one row assuming it contains
mutations denoted by its structure-level properties.
"""
csv_rows = []
for st in sts:
if not _mutations_are_present(st):
continue
delta_vals_by_prop = get_delta_property_values(st)
raw_row = _prepend_mutations_prop(st, delta_vals_by_prop)
csv_row = _make_display_row(raw_row)
csv_rows.append(csv_row)
return csv_rows
def _mutations_are_present(st: structure.Structure) -> bool:
"""
Return whether the supplied structure contains a mutation performed by
residue scanning.
"""
mutations_val = st.property.get(psp_props.BL_MUTS)
return mutations_val not in (None, BIOLUM_NONE_STR)
[docs]def get_delta_property_values(st: structure.Structure) -> Dict[str, float]:
"""
Return a dictionary mapping Bioluminate delta structure properties to their
values in the given structure.
:param st: The structure to inspect.
"""
return {
prop: st.property[prop]
for prop in list(st.property)
if prop.startswith(psp_props.DELTA_PROP_BASE)
}
def _prepend_mutations_prop(
st: structure.Structure,
delta_vals_by_prop: Dict[str, float]) -> PropertyDict:
"""
Return a new dict that has the mutation key/value pair as the first entry in
the dict.
"""
vals_by_prop = {psp_props.BL_MUTS: st.property[psp_props.BL_MUTS]}
vals_by_prop.update(delta_vals_by_prop)
return vals_by_prop
def _make_display_row(csv_row: PropertyDict) -> PropertyDict:
"""
Return a property dict where internal property names have been converted
into appropriate display names.
:param csv_row: A dictionary of structure-level properties mapped to that
property's value for a particular structure.
"""
display_row = dict()
for prop, val in csv_row.items():
display_name = structure.PropertyName(prop).userName()
display_row[display_name] = val
return display_row