Commit 4d8e9e71 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Starting refactor of complex support.

parent 92d4aa76
Loading
Loading
Loading
Loading
+81 −4
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ def load_pdbbind_labels(labels_file):
               "ignore-this-field", "reference", "ligand name"))
  return contents_df

def compute_pdbbind_feature(compound_featurizers, complex_featurizers,
def compute_pdbbind_grid_feature(compound_featurizers, complex_featurizers,
                                 pdb_subdir, pdb_code):
  """Compute features for a given complex"""
  protein_file = os.path.join(pdb_subdir, "%s_protein.pdb" % pdb_code)
@@ -54,7 +54,83 @@ def compute_pdbbind_feature(compound_featurizers, complex_featurizers,
  features = np.concatenate(all_features)
  return features

def load_pdbbind(pdbbind_dir, base_dir, reload=True):
def compute_pdbbind_atomic_coordinates(compound_featurizers, complex_featurizers,
                                       pdb_subdir, pdb_code):
  """Compute features for a given complex"""
  protein_file = os.path.join(pdb_subdir, "%s_protein.pdb" % pdb_code)
  ligand_file = os.path.join(pdb_subdir, "%s_ligand.sdf" % pdb_code)
  #rdkit_mol = Chem.MolFromMol2File(str(ligand_file))
  rdkit_mol = Chem.SDMolSupplier(str(ligand_file)).next()

  all_features = []
  for complex_featurizer in complex_featurizers:
    features = complex_featurizer.featurize_complexes(
      [ligand_file], [protein_file])
    all_features.append(features)
  
  for compound_featurizer in compound_featurizers:
    features = np.squeeze(compound_featurizer.featurize([rdkit_mol]))
    all_features.append(features)

  features = np.concatenate(all_features)
  return features
    
def load_core_pdbbind_grid(pdbbind_dir, base_dir, reload=True):
  """Load PDBBind datasets. Does not do train/test split"""
  # Set some global variables up top
  reload = True
  verbosity = "high"
  model = "logistic"
  regen = False

  # Create some directories for analysis
  # The base_dir holds the results of all analysis
  if not reload:
    if os.path.exists(base_dir):
      shutil.rmtree(base_dir)
  if not os.path.exists(base_dir):
    os.makedirs(base_dir)
  current_dir = os.path.dirname(os.path.realpath(__file__))
  #Make directories to store the raw and featurized datasets.
  data_dir = os.path.join(base_dir, "dataset")

  # Load PDBBind dataset
  labels_file = os.path.join(pdbbind_dir, "INDEX_core_data.2013")
  pdb_subdirs = os.path.join(pdbbind_dir, "website-core-set")
  tasks = ["-logKd/Ki"]
  print("About to load contents.")
  contents_df = load_pdbbind_labels(labels_file)
  ids = contents_df["PDB code"].values
  y = np.array([float(val) for val in contents_df["-logKd/Ki"].values])

  # Define featurizers
  grid_featurizer = GridFeaturizer(
      voxel_width=16.0, feature_types="voxel_combined",
      voxel_feature_types=["ecfp", "splif", "hbond", "pi_stack", "cation_pi",
      "salt_bridge"], ecfp_power=9, splif_power=9,
      parallel=True, flatten=True)
  compound_featurizers = [CircularFingerprint(size=1024)]
  complex_featurizers = [grid_featurizer]
  
  # Featurize Dataset
  features = []
  for pdb_code in ids:
    print("Processing %s" % str(pdb_code))
    pdb_subdir = os.path.join(pdb_subdirs, pdb_code)
    computed_feature = compute_pdbbind_grid_feature(
        compound_featurizers, complex_featurizers, pdb_subdir, pdb_code)
    if len(computed_feature) == 0:
      computed_feature = np.zeros(1024)
    features.append(computed_feature)
  X = np.vstack(features)
  w = np.ones_like(y)
   
  dataset = Dataset.from_numpy(data_dir, X, y, w, ids)
  transformers = []
  
  return tasks, dataset, transformers

def load_core_pdbbind_atomic_coordinates(pdbbind_dir, base_dir, reload=True):
  """Load PDBBind datasets. Does not do train/test split"""
  # Set some global variables up top
  reload = True
@@ -94,8 +170,9 @@ def load_pdbbind(pdbbind_dir, base_dir, reload=True):
  # Featurize Dataset
  features = []
  for pdb_code in ids:
    print("Processing %s" % str(pdb_code))
    pdb_subdir = os.path.join(pdb_subdirs, pdb_code)
    computed_feature = compute_pdbbind_feature(
    computed_feature = compute_pdbbind_atomic_coordinates(
        compound_featurizers, complex_featurizers, pdb_subdir, pdb_code)
    if len(computed_feature) == 0:
      computed_feature = np.zeros(1024)
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

__author__ = "Joseph Gomes"
__author__ = "Joseph Gomes and Bharath Ramsundar"
__copyright__ = "Copyright 2016, Stanford University"
__license__ = "LGPL v2.1+"

+2 −0
Original line number Diff line number Diff line
@@ -252,6 +252,8 @@ class DataLoader(object):
        return True
    return False

  # TODO(rbharath): Should this function be modified to accept filenames for
  # ligand/protein files instead of loaded strings?
  def _featurize_complexes(self, df, featurizer, parallel=True,
                           worker_pool=None):
    """Generates circular fingerprints for dataset."""
+259 −170

File changed.

Preview size limit exceeded, changes collapsed.

+2 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ from sklearn.metrics import accuracy_score
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import precision_score
from scipy.stats import pearsonr

def to_one_hot(y):
@@ -126,7 +127,7 @@ class Metric(object):
    self.threshold = threshold
    if mode is None:
      if self.name in ["roc_auc_score", "matthews_corrcoef", "recall_score",
                       "accuracy_score", "kappa_score"]:
                       "accuracy_score", "kappa_score", "precision_score"]:
        mode = "classification"
      elif self.name in ["pearson_r2_score", "r2_score", "mean_squared_error",
                         "mean_absolute_error", "rms_score",