def test_get_physicochemical_or_spatial_features_dict( self, structure_klifs_id): """ Test if physicochemical an spatial features dictionary has correct keys. """ pocket_bp = PocketBioPython.from_structure_klifs_id( structure_klifs_id, LOCAL) pocket_df = PocketDataFrame.from_structure_klifs_id( structure_klifs_id, LOCAL) fingerprint = Fingerprint() # Physicochemical features physicochemical_dict = fingerprint._get_physicochemical_features_dict( pocket_bp) assert isinstance(physicochemical_dict, dict) assert list( physicochemical_dict.keys()) == FEATURE_NAMES_PHYSICOCHEMICAL_DICT # Spatial features spatial_dict = fingerprint._get_spatial_features_dict(pocket_df) assert isinstance(spatial_dict, dict) assert list(spatial_dict.keys()) == FEATURE_NAMES_SPATIAL_DICT assert list(spatial_dict["distances"].keys() ) == FEATURE_NAMES_DISTANCES_AND_MOMENTS assert list(spatial_dict["moments"].keys() ) == FEATURE_NAMES_DISTANCES_AND_MOMENTS
def test_values(self, structure_klifs_id, klifs_session): """ Test class property: values. """ pocket = PocketDataFrame.from_structure_klifs_id( structure_klifs_id, klifs_session=klifs_session ) feature = SubpocketsFeature.from_pocket(pocket) assert isinstance(feature.values, dict) assert feature.values == feature._moments
def from_text(cls, text, extension, residue_ids, residue_ixs, structure_name, kinase_name): """ Calculate fingerprint for a KLIFS structure (by complex data as text and pocket residue IDs and indices). Parameters ---------- text : str Structural complex data as string (file content). extension : str Structural complex data format (file extension). residue_ids : list of int Pocket residue IDs. residue_ixs : list of int Pocket residue indices. structure_name : str # TODO or structure_klifs_id? Structure name. kinase_name : str Kinase name. Returns ------- kissim.encoding.Fingerprint Fingerprint. """ # BioPython-based and DataFrame-based pocket are both necessary for fingerprint features pocket_bp = PocketBioPython.from_text( text, extension, residue_ids, residue_ixs, structure_name ) pocket_df = PocketDataFrame.from_text( text, extension, residue_ids, residue_ixs, structure_name ) if pocket_bp is None or pocket_df is None: logger.warning(f"{structure_name}: Empty fingerprint (pocket unaccessible).") fingerprint = None else: fingerprint = cls() fingerprint.structure_klifs_id = structure_name fingerprint.kinase_name = kinase_name fingerprint.residue_ids = pocket_bp._residue_ids fingerprint.residue_ixs = pocket_bp._residue_ixs values_dict = {} values_dict["physicochemical"] = fingerprint._get_physicochemical_features_dict( pocket_bp ) values_dict["spatial"] = fingerprint._get_spatial_features_dict(pocket_df) fingerprint.values_dict = values_dict return fingerprint
def test_from_pocket(self, structure_klifs_id, klifs_session): """ Test if SubpocketsFeature can be set from a Pocket object. Test object attribues. """ pocket = PocketDataFrame.from_structure_klifs_id( structure_klifs_id, klifs_session=klifs_session ) feature = SubpocketsFeature.from_pocket(pocket) assert isinstance(feature, SubpocketsFeature) # Test class attributes # Note: _distances and _moments are tested separately below assert feature.name == structure_klifs_id for residue_id, residue_ix in zip(feature._residue_ids, feature._residue_ixs): if residue_id is not None: assert isinstance(residue_id, int) assert isinstance(residue_ix, int)
def test_calculate_distance_to_center( self, structure_klifs_id, klifs_session, residue_id, subpocket_center, distance ): """ Test calculation of distances between a subpocket center and a pocket residues. """ pocket = PocketDataFrame.from_structure_klifs_id( structure_klifs_id, klifs_session=klifs_session ) feature = SubpocketsFeature.from_pocket(pocket) ca_atoms = pocket.ca_atoms distance_calculated = feature._calculate_distance_to_center( ca_atoms, residue_id, subpocket_center ) if np.isnan(distance): assert np.isnan(distance_calculated) else: assert pytest.approx(distance_calculated, abs=1e-6) == distance
def test_calculate_distances_to_center( self, structure_klifs_id, klifs_session, subpocket_center, mean_distance ): """ Test calculation of distances between a subpocket center and all pocket residues. Test also the case that there is no subpocket center. """ pocket = PocketDataFrame.from_structure_klifs_id( structure_klifs_id, klifs_session=klifs_session ) feature = SubpocketsFeature.from_pocket(pocket) distances_calculated = feature._calculate_distances_to_center(pocket, subpocket_center) if subpocket_center is None: assert len(distances_calculated) == len(pocket._residue_ids) assert all(np.isnan(distances_calculated)) else: mean_distance_calculated = np.nanmean(np.array(distances_calculated)) assert pytest.approx(mean_distance_calculated, abs=1e-6) == mean_distance
def test_calculate_distances_and_moments( self, structure_klifs_id, klifs_session, distances_mean, moments_mean, subpocket_centers_mean, ): """ Test calculation of distances and moments for all subpockets. We are testing here the class attributes _distances and _moments, whose values are the return values from the class methods calculate_distance and calculate_moments. """ pocket = PocketDataFrame.from_structure_klifs_id( structure_klifs_id, klifs_session=klifs_session ) feature = SubpocketsFeature.from_pocket(pocket) # Test distances for _, distances in feature._distances.items(): assert isinstance(distances, list) distances_mean_calculated = { name: np.nanmean(distances) for name, distances in feature._distances.items() } assert pytest.approx(distances_mean_calculated, abs=1e-6) == distances_mean # Test moments for _, moments in feature._moments.items(): assert isinstance(moments, list) moments_mean_calculated = { name: np.nanmean(moments) for name, moments in feature._moments.items() } assert pytest.approx(moments_mean_calculated, abs=1e-6) == moments_mean # Test subpocket centers for _, subpocket_center in feature._subpocket_centers.items(): assert isinstance(subpocket_center, list) subpocket_centers_mean_calculated = { name: (np.nanmean(coordinates) if coordinates is not None else None) for name, coordinates in feature._subpocket_centers.items() } assert pytest.approx(subpocket_centers_mean_calculated, abs=1e-6) == subpocket_centers_mean
def test_add_subpockets(self, structure_klifs_id, klifs_session, subpockets, subpocket_center): """ Test if subpockets are added correctly. """ pocket = PocketDataFrame.from_structure_klifs_id( structure_klifs_id, klifs_session=klifs_session ) feature = SubpocketsFeature() pocket = feature._add_subpockets(pocket, subpockets) assert pocket.subpockets.columns.to_list() == [ "subpocket.name", "subpocket.color", "subpocket.center", ] assert pocket.subpockets["subpocket.name"].to_list() == subpockets["subpocket.name"] assert pocket.subpockets["subpocket.color"].to_list() == subpockets["subpocket.color"] subpocket_center_calculated = pocket.subpockets["subpocket.center"][0] for i, j in zip(subpocket_center_calculated, subpocket_center): assert pytest.approx(i, abs=1e-4) == j
def test_details(self, structure_klifs_id, klifs_session): """ Test class property: details. """ pocket = PocketDataFrame.from_structure_klifs_id( structure_klifs_id, klifs_session=klifs_session ) feature = SubpocketsFeature.from_pocket(pocket) assert isinstance(feature.details, dict) # Distances assert isinstance(feature.details["distances"], pd.DataFrame) assert feature.details["distances"].index.to_list() == feature._residue_ixs # Test first column name and dtypes (the other columns depend on the input subpocket names) assert feature.details["distances"].columns.to_list()[0] == "residue.id" assert feature.details["distances"].dtypes[0] == "Int32" # Moments assert isinstance(feature.details["moments"], pd.DataFrame) assert feature.details["moments"].index.to_list() == [1, 2, 3]