Commit cf1b17c8 authored by Bharath Ramsundar's avatar Bharath Ramsundar Committed by Bharath Ramsundar
Browse files

Cleanup

parent 782afc02
Loading
Loading
Loading
Loading
+34 −38
Original line number Diff line number Diff line
@@ -70,6 +70,24 @@ def hash_ecfp_pair(ecfp_pair, power):
  return (ecfp_hash)


def _vectorize(hash_function,
               feature_dict=None,
               feature_list=None,
               channel_power=10):
  """Helper function to vectorize a spatial description."""
  feature_vector = np.zeros(2**channel_power)
  if feature_dict is not None:
    on_channels = [
        hash_function(feature, channel_power)
        for key, feature in feature_dict.items()
    ]
    feature_vector[on_channels] += 1
  elif feature_list is not None:
    feature_vector[0] += len(feature_list)

  return feature_vector


def compute_all_ecfp(mol, indices=None, degree=2):
  """Obtain molecular fragment for all atoms emanating outward to given degree.
  For each fragment, compute SMILES string (for now) and hash to an int.
@@ -358,7 +376,7 @@ class RdkitGridFeaturizer(ComplexFeaturizer):
               box_width=16.0,
               voxel_width=1.0,
               flatten=False,
               sanitize=False,
               sanitize=True,
               **kwargs):
    """
    Parameters
@@ -389,9 +407,10 @@ class RdkitGridFeaturizer(ComplexFeaturizer):
    flatten: bool, optional (defaul False)
      Indicate whether calculated features should be flattened. Output is always
      flattened if flat features are specified in feature_types.
    sanitize: bool, optional (defaul False)
      If set to True molecules will be sanitized. Note that calculating some
      features (e.g. aromatic interactions) require sanitized molecules.
    sanitize: bool, optional (default True)
      If set to True molecules will be sanitized. Note that
      calculating some features (e.g. aromatic interactions)
      require sanitized molecules.
    **kwargs: dict, optional
      Keyword arguments can be usaed to specify custom cutoffs and bins (see
      default values below).
@@ -410,11 +429,11 @@ class RdkitGridFeaturizer(ComplexFeaturizer):
    cation_pi_angle_cutoff: 30.0
    """

    # check if user tries to set removed arguments
    deprecated_args = [
        'box_x', 'box_y', 'box_z', 'save_intermediates', 'voxelize_features',
        'parallel', 'voxel_feature_types'
    ]
    ## check if user tries to set removed arguments
    #deprecated_args = [
    #    'box_x', 'box_y', 'box_z', 'save_intermediates', 'voxelize_features',
    #    'parallel', 'voxel_feature_types'
    #]

    # list of features that require sanitized molecules
    require_sanitized = ['pi_stack', 'cation_pi', 'ecfp_ligand']
@@ -422,12 +441,6 @@ class RdkitGridFeaturizer(ComplexFeaturizer):
    # not implemented featurization types
    not_implemented = ['sybyl']

    for arg in deprecated_args:
      if arg in kwargs:
        #TODO(rbharath): 1.4 is long gone... Figure out what to do here
        logger.warning('%s argument was removed and it is ignored,'
                       ' using it will result in error in version 1.4' % arg)

    self.sanitize = sanitize
    self.flatten = flatten

@@ -474,8 +487,9 @@ class RdkitGridFeaturizer(ComplexFeaturizer):
    # each entry is a tuple (is_flat, feature_name)
    self.feature_types = []

    # list of features that cannot be calculated with specified parameters
    # this list is used to define <flat/voxel/all>_combined subset
    # list of features that cannot be calculated with specified
    # parameters this list is used to define
    # <flat/voxel/all>_combined subset
    ignored_features = []
    if self.sanitize is False:
      ignored_features += require_sanitized
@@ -533,7 +547,7 @@ class RdkitGridFeaturizer(ComplexFeaturizer):
      return [compute_ecfp_features(lig_rdk, self.ecfp_degree, self.ecfp_power)]
    if feature_name == 'ecfp_hashed':
      return [
          self._vectorize(
          _vectorize(
              hash_ecfp, feature_dict=ecfp_dict, channel_power=self.ecfp_power)
          for ecfp_dict in featurize_binding_pocket_ecfp(
              prot_xyz,
@@ -546,7 +560,7 @@ class RdkitGridFeaturizer(ComplexFeaturizer):
      ]
    if feature_name == 'splif_hashed':
      return [
          self._vectorize(
          _vectorize(
              hash_ecfp_pair,
              feature_dict=splif_dict,
              channel_power=self.splif_power) for splif_dict in featurize_splif(
@@ -555,8 +569,7 @@ class RdkitGridFeaturizer(ComplexFeaturizer):
      ]
    if feature_name == 'hbond_count':
      return [
          self._vectorize(
              hash_ecfp_pair, feature_list=hbond_list, channel_power=0)
          _vectorize(hash_ecfp_pair, feature_list=hbond_list, channel_power=0)
          for hbond_list in compute_hydrogen_bonds(
              prot_xyz, prot_rdk, lig_xyz, lig_rdk, distances, self.cutoffs[
                  'hbond_dist_bins'], self.cutoffs['hbond_angle_cutoffs'])
@@ -843,20 +856,3 @@ class RdkitGridFeaturizer(ComplexFeaturizer):
        feature_dict=ligand_pi_t,
        nb_channel=1)
    return [pi_parallel_tensor, pi_t_tensor]

  def _vectorize(self,
                 hash_function,
                 feature_dict=None,
                 feature_list=None,
                 channel_power=10):
    feature_vector = np.zeros(2**channel_power)
    if feature_dict is not None:
      on_channels = [
          hash_function(feature, channel_power)
          for key, feature in feature_dict.items()
      ]
      feature_vector[on_channels] += 1
    elif feature_list is not None:
      feature_vector[0] += len(feature_list)

    return feature_vector