Commit 19584163 authored by joegomes's avatar joegomes
Browse files

SDF input file reader functional

parent c5b049a0
Loading
Loading
Loading
Loading

datasets/gdb1k.sdf

0 → 100644
+33910 −0

File added.

Preview size limit exceeded, changes collapsed.

datasets/gdb1k.sdf.csv

0 → 100644
+992 −0
Original line number Diff line number Diff line
atomization_energy
-596.35
-959.62
-2063.19
-1241.88
-1838.28
-1062.9
-1525.86
-1521.04
-1453.58
-1571.76
-994.68
-1105.98
-1116.89
-1579.2
-1774.79
-2061.07
-1123.24
-1405.32
-1554.03
-1255.97
-2190.06
-2049.55
-1477.16
-1718.87
-1260.78
-1531.53
-1370.22
-1803.45
-1545.31
-1512.8
-2051.87
-1331.28
-1608.25
-1600.78
-861.62
-816.7
-1751.2
-1544.61
-1459.54
-1203.34
-1788.78
-1323.8
-1847.46
-1131.64
-1215.13
-2064.75
-1173.81
-1342.31
-1223.35
-980.76
-1758.43
-1316.15
-1781.77
-1006.46
-1388.27
-1063.44
-1174.42
-1119.26
-1773.29
-1600.27
-1911.31
-1757.3
-861.73
-1711.1
-998.84
-1058.82
-2055.09
-1217.33
-1624.43
-1920.45
-1234.05
-1689.49
-1892.91
-1672.1
-1293.47
-1440.44
-1667.71
-2047.58
-1132.01
-1655.54
-1117.96
-1367.
-1210.47
-1765.27
-1646.43
-1779.9
-1600.11
-1726.38
-1395.15
-1315.13
-1101.34
-1465.22
-1416.05
-1018.6
-1713.92
-1900.69
-1770.53
-1326.99
-1059.31
-1168.84
-1425.6
-1446.98
-1563.08
-1291.64
-1549.15
-963.04
-1679.43
-1325.94
-1174.94
-1569.44
-1125.81
-1270.06
-1844.68
-1557.43
-1097.08
-1455.85
-1454.38
-1698.66
-1132.96
-1551.72
-1167.69
-1688.27
-1720.81
-1527.51
-1474.1
-1901.45
-1785.27
-1471.27
-1516.64
-1084.82
-1466.12
-1413.82
-1393.45
-1729.65
-803.46
-926.22
-1104.19
-1131.83
-1160.48
-1262.44
-1734.73
-1152.05
-980.82
-1305.81
-1418.76
-1003.69
-1540.62
-1906.84
-1617.81
-1928.68
-1162.03
-1123.32
-1332.39
-1633.76
-1396.44
-1070.27
-1915.38
-1501.14
-1669.35
-1236.4
-1574.11
-1226.73
-1989.7
-1319.45
-1466.33
-1177.01
-1212.42
-1585.4
-1938.48
-993.7
-1121.33
-1018.41
-1242.45
-1528.92
-1013.4
-1610.81
-1254.21
-1088.85
-1242.47
-1763.12
-1732.12
-1158.53
-1436.69
-1364.02
-1062.97
-1705.49
-1446.21
-1092.62
-1521.71
-1902.7
-1229.27
-1662.1
-1210.27
-1169.19
-1189.09
-1479.3
-1442.18
-1392.3
-1250.37
-1797.33
-1095.1
-1318.51
-1313.49
-1527.09
-1258.46
-1404.09
-1615.95
-1170.42
-1154.32
-1284.37
-1249.45
-1328.89
-1338.18
-1936.69
-1448.78
-1160.57
-1163.14
-831.06
-1253.44
-871.19
-1553.4
-1120.22
-1105.67
-1450.54
-1942.37
-1910.33
-1464.45
-1220.93
-1105.28
-1763.56
-1208.65
-2057.64
-1931.62
-1727.44
-1293.13
-1508.99
-1270.08
-1906.02
-850.55
-1541.73
-1602.07
-1455.97
-1515.17
-1659.61
-1770.03
-1407.43
-1585.07
-1755.19
-1450.11
-1836.94
-956.51
-1318.45
-1457.1
-1453.52
-1783.46
-2191.74
-1400.29
-1113.95
-1277.37
-955.38
-972.49
-1081.42
-1096.2
-1362.56
-944.51
-1986.42
-1469.08
-725.93
-1865.45
-1240.08
-1642.18
-1926.84
-1594.95
-947.54
-1286.29
-1873.28
-1683.09
-1483.76
-1472.6
-1720.2
-1066.53
-1868.72
-1118.53
-1323.38
-1794.58
-1717.96
-1814.1
-953.49
-1890.32
-1988.56
-1745.43
-1802.28
-1834.45
-1200.23
-1365.93
-2069.4
-1254.7
-1759.32
-1050.2
-1231.52
-2047.11
-1749.39
-1285.28
-1033.09
-1144.85
-1263.4
-1157.45
-1547.55
-1360.84
-1009.94
-1356.74
-958.05
-1632.72
-1473.27
-1488.23
-1109.98
-1664.11
-1409.33
-1395.52
-1259.44
-1120.22
-1428.79
-1403.68
-1040.33
-1693.64
-1697.39
-1303.56
-1371.71
-1161.23
-1399.24
-1692.04
-1195.4
-1792.73
-1322.92
-1142.8
-1146.75
-1309.94
-1317.5
-1333.46
-1143.79
-1614.03
-1157.45
-1729.98
-1507.23
-1531.9
-902.44
-1991.71
-1704.8
-1230.86
-1686.99
-1656.48
-1199.56
-1582.33
-981.05
-1739.78
-1166.9
-1406.47
-1922.87
-1391.77
-1329.81
-810.17
-1251.77
-1545.88
-1299.08
-1560.54
-1573.35
-1131.72
-1335.4
-1073.94
-1257.13
-1571.36
-1008.49
-1854.7
-1622.33
-1049.76
-1760.86
-1579.68
-1749.78
-1420.72
-1026.2
-1315.05
-1029.75
-1776.14
-1652.25
-1454.72
-1183.71
-1841.79
-1207.59
-1425.91
-1457.72
-1387.21
-1664.72
-1498.16
-1018.13
-1916.08
-1065.62
-1383.1
-1576.15
-1592.41
-1076.42
-1006.35
-1599.57
-1762.25
-1543.15
-1319.61
-914.2
-1423.73
-1355.35
-1130.81
-1845.02
-1300.91
-1555.23
-1855.61
-1249.87
-1216.27
-1153.75
-1311.6
-1703.61
-1289.94
-1193.68
-677.16
-1638.72
-1402.97
-1849.75
-1377.86
-1358.14
-1019.74
-1648.85
-1891.76
-1324.08
-1747.8
-1112.08
-1867.98
-2052.19
-1861.52
-1145.6
-1315.9
-1676.62
-1279.51
-1899.42
-1255.76
-1077.68
-1118.32
-1179.84
-1461.74
-1904.2
-1170.86
-1014.79
-1253.18
-1846.69
-1485.76
-1894.91
-1805.48
-1306.67
-1842.83
-1754.1
-1987.81
-1182.59
-1276.17
-1619.05
-1626.77
-667.28
-1267.12
-973.32
-1580.88
-1858.76
-1766.88
-1353.07
-1791.28
-1159.28
-1421.56
-1184.76
-1930.18
-1346.56
-1752.81
-1397.5
-1127.5
-966.71
-1559.65
-1468.38
-1160.91
-860.33
-1148.1
-1458.12
-1288.68
-1933.2
-1533.55
-1010.86
-1738.28
-1909.06
-925.91
-1192.89
-1740.7
-1924.09
-1324.02
-1104.57
-1452.21
-1382.22
-1707.83
-1467.11
-1267.25
-1602.41
-1331.79
-1637.73
-1359.16
-1042.24
-1131.38
-1325.75
-1321.07
-796.98
-1482.23
-1808.45
-1481.4
-1275.06
-1250.33
-1862.52
-1898.16
-1360.43
-1277.77
-1122.73
-1415.29
-1485.21
-1301.46
-1453.11
-1108.37
-1227.18
-1604.99
-1152.68
-1710.69
-1937.43
-1782.84
-1639.14
-1321.23
-1247.8
-1327.3
-1914.48
-1438.72
-2045.98
-1538.53
-1851.53
-1635.13
-1459.47
-1161.48
-1640.49
-1107.23
-1248.65
-1253.73
-1568.34
-1496.1
-1018.44
-1003.91
-1375.68
-1725.05
-1660.1
-708.37
-1244.2
-1507.79
-1492.5
-1185.51
-1549.64
-1673.89
-1735.96
-1675.41
-1530.23
-1607.36
-1811.94
-1393.65
-1245.18
-1684.45
-1665.99
-1859.41
-1620.76
-1723.02
-861.3
-1437.87
-1561.49
-996.47
-1513.69
-799.52
-1857.18
-1256.77
-1635.94
-988.51
-1254.34
-1275.79
-1835.68
-1367.87
-1157.46
-1799.1
-1338.98
-1156.77
-1245.71
-1804.84
-1389.7
-1149.68
-1649.39
-1025.19
-1139.22
-1690.79
-1771.85
-1653.63
-1385.72
-1339.
-952.57
-1237.38
-1497.58
-967.43
-1273.24
-1586.7
-1741.42
-1091.7
-1630.09
-1908.95
-1113.93
-1755.5
-1089.61
-2044.34
-1789.99
-1809.36
-1103.89
-1352.29
-1558.58
-1061.53
-1088.23
-1317.7
-1472.02
-884.77
-1870.07
-1623.4
-893.41
-1365.11
-1270.7
-1017.37
-1566.95
-1419.2
-1297.64
-1228.25
-1036.5
-1150.53
-1455.34
-1611.5
-1503.72
-1255.27
-1137.66
-1721.82
-1075.22
-1746.63
-1256.19
-1344.81
-1494.99
-953.53
-1177.16
-977.67
-1017.04
-1776.51
-1124.15
-1489.8
-852.42
-1323.74
-1187.58
-1476.28
-1099.93
-1153.43
-1589.03
-1239.16
-1210.26
-1982.75
-1068.2
-1597.85
-1410.52
-1983.95
-1736.93
-1102.46
-1599.06
-1919.22
-1025.03
-1627.89
-1398.32
-1110.11
-1620.26
-1522.92
-1683.49
-1442.33
-1934.67
-1186.31
-1443.74
-1346.82
-862.34
-1708.82
-1369.28
-1335.03
-1115.15
-879.38
-1431.25
-1792.26
-1539.91
-1380.61
-1274.15
-1306.46
-1029.4
-1591.64
-1211.2
-1801.31
-1614.89
-1018.01
-1197.11
-1282.55
-1435.52
-1287.53
-1318.73
-1551.53
-693.01
-1458.29
-992.26
-1155.53
-1583.2
-653.44
-1068.82
-1490.98
-1702.87
-974.5
-1510.44
-1428.22
-1300.05
-1328.7
-1631.9
-1535.9
-1379.69
-1590.5
-972.64
-1305.32
-1028.25
-1177.97
-1849.44
-1000.01
-1654.94
-1176.14
-1254.11
-1180.34
-1475.84
-1416.59
-1169.33
-1474.9
-1072.55
-1786.15
-1053.94
-927.43
-1341.07
-1125.26
-1984.66
-1990.67
-1839.37
-1455.68
-1100.59
-1643.94
-1164.86
-1577.76
-1265.8
-1096.09
-1434.79
-1645.29
-1433.7
-1282.77
-1163.19
-1714.71
-1588.
-1067.19
-1259.83
-2059.13
-1206.6
-1375.99
-1033.83
-2050.15
-1768.2
-1047.64
-1866.88
-1596.37
-1810.76
-1128.5
-1268.15
-1316.14
-2189.38
-1462.48
-1093.99
-1159.41
-1463.15
-1264.86
-1248.98
-808.87
-949.91
-1221.82
-860.26
-1657.96
-1921.19
-1701.34
-1628.6
-1325.98
-871.86
-1152.69
-1472.35
-1278.26
-1518.14
-1165.21
-1326.29
-857.15
-1852.22
-1276.16
-1536.97
-1470.79
-1401.12
-1271.12
-1625.88
-1204.42
-1680.84
-1924.91
-1312.57
-1917.92
-1294.93
-1744.57
-1406.28
-884.17
-1070.7
-1161.87
-1351.03
-1225.26
-1280.75
-1155.4
-1767.82
-1197.04
-1062.67
-1460.22
-1860.2
-2066.04
-1799.65
-1065.95
-1254.62
-1233.53
-1605.31
-1271.54
-1807.35
-1224.22
-1322.48
-1466.4
-1269.63
-1840.74
-768.02
-825.76
-1505.4
-1778.83
-1575.44
-785.04
-1609.75
-1673.08
-1511.77
-1412.54
-1087.96
-988.34
-1743.65
-1853.99
-1174.66
-1339.81
-1018.95
-1218.16
-1994.55
-1926.
-1295.84
-1099.75
-2055.96
-1298.51
-1871.12
-1348.1
-1680.8
-1136.23
-1491.64
-993.41
-1354.04
-1401.67
-1534.19
-1052.57
-1304.71
-1244.44
-1778.55
-1566.5
-1716.29
-1486.56
-2192.
-1430.45
-1517.02
-1308.78
-1494.09
-1306.23
-1158.13
-1471.15
-1668.51
-1647.92
-1163.8
-1124.79
-1693.
-1519.27
-1904.48
-1174.99
-1032.07
-1444.63
-1695.85
-1307.99
-1995.12
-1051.63
-1196.17
-1480.18
-1349.82
-1251.26
-1262.45
-1422.49
-1020.97
-1129.26
-1325.85
-1086.92
-1384.38
-1400.63
-1098.8
-1993.9
-1677.9
-1113.96
-1787.65
-1321.12
-1724.24
-1864.42
-1891.5
-1459.29
-1391.46
-618.72
-1205.42
-1258.28
-1390.24
-860.01
-1593.91
-927.78
-1336.97
-945.61
-1310.13
-872.88
-1167.84
-1499.73
-1194.16
-1686.62
-1524.28
-2058.97
-1219.26
-1002.62
-1378.2
-971.15
-1564.18
-1896.03
-1300.09
-1198.32
-1374.26
-1099.62
-1189.89
-1502.54
-1643.55
-1796.65
-1929.11
-1191.44
-1321.3
-1996.76
-903.23
-2062.
-1304.31
-1696.08
-1023.89
-1343.55
-1651.31
-1707.52
-1713.09
-1057.63
-1314.1
-1565.05
-1934.27
-1733.32
-1912.68
-1504.27
-1086.74
-1700.36
-1107.82
-1251.24
-1026.67
-1794.95
-1731.09
-1119.24
-1236.15
-1373.04

datasets/gdb7k.pkl.gz

deleted100644 → 0
−919 KiB

File deleted.

+3 −1
Original line number Diff line number Diff line
@@ -142,7 +142,9 @@ class Featurizer(object):
        if verbosity is not None and i % log_every_n == 0:
          log("Featurizing %d / %d" % (i, len(mols)))
        if mol is not None:
          features.append(self._featurize(mol))
          myfeatures = self._featurize(mol)
          features.append(myfeatures)
          #features.append(self._featurize(mol))
        else:
          features.append(np.array([]))

+77 −4
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ def _process_field(val):
      return float(val)
    except ValueError:
      return val
  elif isinstance(val, Chem.Mol):
    return val
  else:
    raise ValueError("Field of unrecognized type: %s" % str(val))

@@ -53,6 +55,8 @@ def _get_input_type(input_file):
    return "pandas-pickle"
  elif file_extension == ".joblib":
    return "pandas-joblib"
  elif file_extension == ".sdf":
    return "sdf"
  else:
    raise ValueError("Unrecognized extension %s" % file_extension)

@@ -69,6 +73,12 @@ def _get_fields(input_file):
  elif input_type == "pandas-pickle":
    df = load_pickle_from_disk(input_file)
    return df.keys()
  # If SDF input, assume that .sdf.csv file contains labels 
  elif input_type == "sdf":
    label_file = input_file + ".csv"
    print("Reading labels from %s" % label_file)
    with open(label_file, "rb") as inp_file_obj:
      return inp_file_obj.readline()
  else:
    raise ValueError("Unrecognized extension for %s" % input_file)

@@ -83,7 +93,7 @@ class DataFeaturizer(object):
  def __init__(self, tasks, smiles_field, split_field=None,
               id_field=None, threshold=None, user_specified_features=None,
               protein_pdb_field=None, ligand_pdb_field=None,
               ligand_mol2_field=None, 
               ligand_mol2_field=None, mol_field=None,
               compound_featurizers=[], complex_featurizers=[],
               verbosity=None, log_every_n=1000):
    """Extracts data from input as Pandas data frame"""
@@ -102,6 +112,7 @@ class DataFeaturizer(object):
    self.protein_pdb_field = protein_pdb_field
    self.ligand_pdb_field = ligand_pdb_field
    self.ligand_mol2_field = ligand_mol2_field
    self.mol_field = mol_field
    self.user_specified_features = user_specified_features
    self.compound_featurizers = compound_featurizers
    self.complex_featurizers = complex_featurizers
@@ -116,13 +127,31 @@ class DataFeaturizer(object):
                             or not self._shard_files_exist(feature_dir)
                             or not reload)

    input_type = _get_input_type(input_file)
    read_sdf = (input_type == "sdf")

    if perform_featurization:
      if not os.path.exists(feature_dir):
        os.makedirs(feature_dir)
      input_type = _get_input_type(input_file)

      log("Loading raw samples now.", self.verbosity)

      if read_sdf:
        # Tasks are stored in .sdf.csv file
        raw_df = load_pandas_from_disk(input_file+".csv")
        # Structures are stored in .sdf file
        print("Reading structures from %s." % input_file)
        suppl = Chem.SDMolSupplier(str(input_file), removeHs=False)
        df_rows = []
        for ind, mol in enumerate(suppl):
          if mol is not None:
            smiles = Chem.MolToSmiles(mol)
            df_rows.append([ind,smiles,mol])
        mol_df = pd.DataFrame(df_rows, columns=('mol_id', 'smiles', 'mol'))
        raw_df = pd.concat([mol_df, raw_df], axis=1, join='inner')
      else:
        raw_df = load_pandas_from_disk(input_file)

      fields = raw_df.keys()
      log("Loaded raw data frame from file.", self.verbosity)
      log("About to preprocess samples.", self.verbosity)
@@ -146,6 +175,13 @@ class DataFeaturizer(object):
        
        df = self._standardize_df(raw_df_shard) 

        if read_sdf:
          # SDF reader compatible with compound_featurizers for now
          for compound_featurizer in self.compound_featurizers:
            log("Currently featurizing feature_type: %s"
                % compound_featurizer.__class__.__name__, self.verbosity)
            self._featurize_mol(df, compound_featurizer, worker_pool=worker_pool)
        else:
          for compound_featurizer in self.compound_featurizers:
            log("Currently featurizing feature_type: %s"
                % compound_featurizer.__class__.__name__, self.verbosity)
@@ -183,7 +219,7 @@ class DataFeaturizer(object):
    if input_type == "csv":
      for ind, field in enumerate(fields):
        data[field] = _process_field(row[ind])
    elif input_type in ["pandas-pickle", "pandas-joblib"]:
    elif input_type in ["pandas-pickle", "pandas-joblib", "sdf"]:
      for field in fields:
        data[field] = _process_field(row[field])
    else:
@@ -206,6 +242,8 @@ class DataFeaturizer(object):
    if self.user_specified_features is not None:
      for feature in self.user_specified_features:
        df[feature] = ori_df[[feature]]
    if self.mol_field is not None:
      df["mol"] = ori_df[[self.mol_field]]
    if self.split_field is not None:
      df["split"] = ori_df[[self.split_field]]
    if self.protein_pdb_field is not None:
@@ -241,6 +279,41 @@ class DataFeaturizer(object):
      #features = featurize_wrapper(zip(ligand_pdbs, protein_pdbs))
    df[featurizer.__class__.__name__] = list(features)

  def _featurize_mol(self, df, featurizer, parallel=True,
                           worker_pool=None):    
    """Featurize individual compounds.

       Given a featurizer that operates on individual chemical compounds 
       or macromolecules, compute & add features for that compound to the 
       features dataframe

       When featurizing a .sdf file, the 3-D structure should be preserved
       so we use the rdkit "mol" object created from .sdf instead of smiles
       string. Some featurizers such as CoulombMatrix also require a 3-D
       structure.  Featurizing from .sdf is currently the only way to
       perform CM feautization.

    """
    sample_mols = df["mol"].tolist()

    if worker_pool is None:
      features = []
      for ind, mol in enumerate(sample_mols):
        if ind % self.log_every_n == 0:
          log("Featurizing sample %d" % ind, self.verbosity)
        features.append(featurizer.featurize([mol], verbosity=self.verbosity))
    else:
      def featurize_wrapper(mol, dilled_featurizer):
        print("Featurizing %s" % mol)
        featurizer = dill.loads(dilled_featurizer)
        feature = featurizer.featurize([mol], verbosity=self.verbosity)
        return feature

      features = worker_pool.map_sync(featurize_wrapper, 
                                      sample_mols)

    df[featurizer.__class__.__name__] = features

  def _featurize_compounds(self, df, featurizer, parallel=True,
                           worker_pool=None):    
    """Featurize individual compounds.
@@ -303,7 +376,7 @@ class FeaturizedSamples(object):
  """
  # The standard columns for featurized data.
  colnames = ["mol_id", "smiles", "split"]
  optional_colnames = ["protein_pdb", "ligand_pdb", "ligand_mol2"]
  optional_colnames = ["protein_pdb", "ligand_pdb", "ligand_mol2", "mol"]

  def __init__(self, samples_dir, featurizers, dataset_files=None, 
               reload=False, verbosity=None):
Loading