File size: 3,673 Bytes
638ea38 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 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)
|