|
| 1 | +""" |
| 2 | +Calculates structure distance matrices and kinase distance matrices. |
| 3 | +""" |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | + |
| 9 | +def structure_distance_matrix(structure_distances, coverage_min=0.0): |
| 10 | + """ |
| 11 | + Get fingerprint distances for all structure pairs in the form of a matrix (DataFrame). |
| 12 | +
|
| 13 | + Parameters |
| 14 | + ---------- |
| 15 | + structure_distances : pandas.DataFrame |
| 16 | + Fingerprint distance and bit coverage for each structure pair (kinase pair). |
| 17 | + fill : bool |
| 18 | + Fill or fill not (default) lower triangle of distance matrix. |
| 19 | + coverage_min : float |
| 20 | + Returns only pairs with a user-defined minimum coverage (defaults to 0.0, i.e. no |
| 21 | + coverage restrictions). |
| 22 | +
|
| 23 | + Returns |
| 24 | + ------- |
| 25 | + pandas.DataFrame |
| 26 | + Structure distance matrix. |
| 27 | + """ |
| 28 | + |
| 29 | + data = structure_distances |
| 30 | + |
| 31 | + # Filter by coverage |
| 32 | + data = data[data["bit_coverage"] >= coverage_min] |
| 33 | + # Data for upper half of the matrix |
| 34 | + pairs_upper = data[["structure.1", "structure.2", "distance"]] |
| 35 | + # Data for lower half of the matrix |
| 36 | + pairs_lower = pairs_upper.rename( |
| 37 | + columns={"structure.1": "structure.2", "structure.2": "structure.1"} |
| 38 | + ) |
| 39 | + |
| 40 | + # Concatenate upper and lower matrix data |
| 41 | + pairs = pd.concat([pairs_upper, pairs_lower]).sort_values(["structure.1", "structure.2"]) |
| 42 | + # Convert to matrix |
| 43 | + matrix = pairs.pivot(columns="structure.2", index="structure.1", values="distance") |
| 44 | + # Matrix diagonal is NaN > set to 0.0 |
| 45 | + np.fill_diagonal(matrix.values, 0) |
| 46 | + |
| 47 | + return matrix |
| 48 | + |
| 49 | + |
| 50 | +def kinase_distance_matrix( |
| 51 | + structure_distances, by="minimum", fill_diagonal=True, coverage_min=0.0 |
| 52 | +): |
| 53 | + """ |
| 54 | + Extract per kinase pair one distance value from the set of structure pair distance values |
| 55 | + and return these fingerprint distances for all kinase pairs in the form of a matrix |
| 56 | + (DataFrame). |
| 57 | +
|
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + structure_distances : pandas.DataFrame |
| 61 | + Fingerprint distance and bit coverage for each structure pair (kinase pair). |
| 62 | + by : str |
| 63 | + Condition on which the distance value per kinase pair is extracted from the set of |
| 64 | + distances values per structure pair. Default: Minimum distance value. |
| 65 | + fill_diagonal : bool |
| 66 | + Fill diagonal with 0 (same kinase has distance of 0) by default. If `False`, diagonal |
| 67 | + will be a experimental values calculated based on the structure pairs per kinase pair. |
| 68 | + Is by default set to False, if `by="size"`. |
| 69 | + coverage_min : float |
| 70 | + Returns only pairs with a user-defined minimum coverage (defaults to 0.0, i.e. no |
| 71 | + coverage restrictions). |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + pandas.DataFrame |
| 76 | + Kinase distance matrix. |
| 77 | + """ |
| 78 | + |
| 79 | + if by == "size": |
| 80 | + fill_diagonal = False |
| 81 | + |
| 82 | + # Data for upper half of the matrix |
| 83 | + pairs_upper = kinase_distances(structure_distances, by, coverage_min).reset_index()[ |
| 84 | + ["kinase.1", "kinase.2", "distance"] |
| 85 | + ] |
| 86 | + # Data for lower half of the matrix |
| 87 | + pairs_lower = pairs_upper.rename(columns={"kinase.1": "kinase.2", "kinase.2": "kinase.1"}) |
| 88 | + |
| 89 | + # Concatenate upper and lower matrix data |
| 90 | + pairs = ( |
| 91 | + pd.concat([pairs_upper, pairs_lower]) |
| 92 | + .sort_values(["kinase.1", "kinase.2"]) |
| 93 | + .drop_duplicates() |
| 94 | + .reset_index(drop=True) |
| 95 | + ) |
| 96 | + |
| 97 | + # Convert to matrix |
| 98 | + matrix = pairs.pivot(columns="kinase.2", index="kinase.1", values="distance") |
| 99 | + |
| 100 | + if fill_diagonal: |
| 101 | + np.fill_diagonal(matrix.values, 0) |
| 102 | + |
| 103 | + # If matrix contains number of structure pairs: NaN > 0, cast to int |
| 104 | + if by == "size": |
| 105 | + matrix = matrix.fillna(0) |
| 106 | + matrix = matrix.astype("int64") |
| 107 | + |
| 108 | + return matrix |
| 109 | + |
| 110 | + |
| 111 | +def kinase_distances(structure_distances, by="minimum", coverage_min=0.0): |
| 112 | + """ |
| 113 | + Extract per kinase pair one distance value from the set of structure pair distance values. |
| 114 | +
|
| 115 | + Parameters |
| 116 | + ---------- |
| 117 | + structure_distances : pandas.DataFrame |
| 118 | + Fingerprint distance and bit coverage for each structure pair (kinase pair). |
| 119 | + by : str |
| 120 | + Condition on which the distance value per kinase pair is extracted from the set of |
| 121 | + distances values per structure pair. Default: Minimum distance value. |
| 122 | + coverage_min : float |
| 123 | + Returns only pairs with a user-defined minimum coverage (defaults to 0.0, i.e. no |
| 124 | + coverage restrictions). |
| 125 | +
|
| 126 | + Returns |
| 127 | + ------- |
| 128 | + pandas.DataFrame |
| 129 | + Fingerprint distance and coverage for kinase pairs. |
| 130 | + """ |
| 131 | + |
| 132 | + data = structure_distances |
| 133 | + |
| 134 | + # Filter by coverage |
| 135 | + data = data[data["bit_coverage"] >= coverage_min].reset_index() |
| 136 | + # Group by kinase names |
| 137 | + structure_distances_grouped_by_kinases = data.groupby(by=["kinase.1", "kinase.2"], sort=False) |
| 138 | + |
| 139 | + # Get distance values per kinase pair based on given condition |
| 140 | + # Note: For min/max we'd like to know which structure pairs were selected! |
| 141 | + by_terms = "minimum maximum mean median size std".split() |
| 142 | + |
| 143 | + if by == "minimum": |
| 144 | + kinase_distances = data.iloc[ |
| 145 | + structure_distances_grouped_by_kinases["distance"].idxmin() |
| 146 | + ].set_index(["kinase.1", "kinase.2"]) |
| 147 | + elif by == "maximum": |
| 148 | + kinase_distances = data.iloc[ |
| 149 | + structure_distances_grouped_by_kinases["distance"].idxmax() |
| 150 | + ].set_index(["kinase.1", "kinase.2"]) |
| 151 | + elif by == "mean": |
| 152 | + kinase_distances = structure_distances_grouped_by_kinases.mean()[["distance"]] |
| 153 | + elif by == "median": |
| 154 | + kinase_distances = structure_distances_grouped_by_kinases.median()[["distance"]] |
| 155 | + elif by == "size": |
| 156 | + kinase_distances = structure_distances_grouped_by_kinases.size().to_frame("distance") |
| 157 | + elif by == "std": |
| 158 | + kinase_distances = structure_distances_grouped_by_kinases.std()[["distance"]] |
| 159 | + kinase_distances = round(kinase_distances, 3) |
| 160 | + else: |
| 161 | + raise ValueError(f'Condition "by" unknown. Choose from: {", ".join(by_terms)}') |
| 162 | + |
| 163 | + return kinase_distances |
0 commit comments