Download code/20_structure_validation.py from HarriziSaad/BULMA: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/20_structure_validation.py
- Command line
-
hf download hf://datasets/HarriziSaad/BULMA/code/20_structure_validation.py
-
curl -L -o 20_structure_validation.py https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/20_structure_validation.py
3.67 kB
| import itertools | |
| import numpy as np | |
| import pandas as pd | |
| import biotite.structure as struc | |
| import biotite.structure.io.pdb as pdb | |
| from scipy import stats | |
| from common import RES, EXT, SEED, read_matrix, abc | |
| from structure_utils import alphafold_model | |
| AF = EXT / "alphafold" | |
| MAX_ITER = 20 | |
| N_PERM = 10_000 | |
| A = abc().set_index("gene") | |
| rng = np.random.default_rng(SEED) | |
| def protein(gene): | |
| s = pdb.PDBFile.read(str(alphafold_model(A.uniprot[gene], AF))).get_structure(model=1) | |
| return s[struc.filter_amino_acids(s)] | |
| chains = {g: protein(g) for g in A.index} | |
| S = pd.read_csv(RES / "abc_struct_sim.csv") | |
| rows = [] | |
| for g1, g2 in zip(S.gene1, S.gene2): | |
| fitted, _, fi, mi = struc.superimpose_structural_homologs(chains[g1], chains[g2], max_iterations=MAX_ITER, | |
| reference_length="shorter") | |
| tm = struc.tm_score(chains[g1], fitted, fi, mi, reference_length="shorter") | |
| rows.append((g1, g2, tm, len(fi))) | |
| V = pd.DataFrame(rows, columns=["gene1", "gene2", "tmscore_biotite", "superposed_pairs_biotite"]).round(4) | |
| V = S[["gene1", "gene2", "same_subfamily", "tmscore", "paralog"]].merge(V, on=["gene1", "gene2"]) | |
| V.to_csv(RES / "abc_struct_validation.csv", index=False) | |
| def matrix(genes, col): | |
| M = pd.DataFrame(np.nan, index=genes, columns=genes) | |
| for g1, g2, v in V[["gene1", "gene2", col]].itertuples(index=False): | |
| if g1 in genes and g2 in genes: | |
| M.loc[g1, g2] = M.loc[g2, g1] = v | |
| return M.values | |
| def mantel(X, Y): | |
| n = len(X) | |
| iu = np.triu_indices(n, 1) | |
| obs = stats.spearmanr(X[iu], Y[iu]).statistic | |
| null = np.array([stats.spearmanr(X[iu], Y[np.ix_(p, p)][iu]).statistic for p in (rng.permutation(n) for _ in range(N_PERM))]) | |
| return obs, (np.sum(np.abs(null) >= abs(obs)) + 1) / (N_PERM + 1) | |
| genes = list(A.index) | |
| I = pd.read_csv(RES / "abc_identity.csv", index_col=0).loc[genes, genes].values | |
| r_id, p_id = mantel(I, matrix(genes, "tmscore_biotite")) | |
| Z = read_matrix("hom.z_result_nm.pub") | |
| pg = [g for g in genes if A.systematic[g] in Z.index and Z.loc[A.systematic[g]].notna().sum() >= 50] | |
| X = Z.loc[[A.systematic[g] for g in pg]] | |
| R = np.full((len(pg), len(pg)), np.nan) | |
| for i, j in itertools.combinations(range(len(pg)), 2): | |
| m = X.iloc[i].notna() & X.iloc[j].notna() | |
| R[i, j] = R[j, i] = stats.pearsonr(X.iloc[i][m], X.iloc[j][m]).statistic | |
| r_pf, p_pf = mantel(matrix(pg, "tmscore_biotite"), np.arctanh(R)) | |
| par = V[V.paralog] | |
| summ = pd.DataFrame([ | |
| ("pairs", len(V), np.nan), | |
| ("Spearman rho, this study vs Biotite", stats.spearmanr(V.tmscore, V.tmscore_biotite).statistic, np.nan), | |
| ("median absolute difference, paralogous pairs", float(np.median(np.abs(par.tmscore - par.tmscore_biotite))), np.nan), | |
| ("median absolute difference, all pairs", float(np.median(np.abs(V.tmscore - V.tmscore_biotite))), np.nan), | |
| ("median TM-score, same subfamily, this study", float(V[V.same_subfamily].tmscore.median()), np.nan), | |
| ("median TM-score, same subfamily, Biotite", float(V[V.same_subfamily].tmscore_biotite.median()), np.nan), | |
| ("median TM-score, different subfamilies, this study", float(V[~V.same_subfamily].tmscore.median()), np.nan), | |
| ("median TM-score, different subfamilies, Biotite", float(V[~V.same_subfamily].tmscore_biotite.median()), np.nan), | |
| ("Mantel rho, sequence identity vs Biotite TM-score", r_id, p_id), | |
| ("Mantel rho, Biotite TM-score vs fitness-profile similarity (Fisher z)", r_pf, p_pf), | |
| ], columns=["quantity", "value", "p_gene_label_permutation"]) | |
| summ.to_csv(RES / "abc_struct_validation_summary.csv", index=False) | |