BULMA / code /16_structure.py
HarriziSaad's picture
Upload 104 files
638ea38 verified
Raw History Blame Contribute Delete
2.69 kB
import itertools
import numpy as np
import pandas as pd
from Bio.PDB import PDBParser, PPBuilder
from common import RES, EXT, abc
from structure_utils import alphafold_model, correspondence, kabsch, tm_score
AF = EXT / "alphafold"
A = abc().set_index("gene")
def ca_chain(gene):
s = PDBParser(QUIET=True).get_structure(gene, alphafold_model(A.uniprot[gene], AF))
res = [r for r in s[0].get_residues() if "CA" in r]
seq = "".join(str(pp.get_sequence()) for pp in PPBuilder().build_peptides(s[0]))
return seq, np.array([r["CA"].coord for r in res], dtype=float)
chains = {g: ca_chain(g) for g in A.index}
rows = []
for g1, g2 in itertools.combinations(A.index, 2):
(s1, x1), (s2, x2) = chains[g1], chains[g2]
i1, i2 = correspondence(s1, s2)
L = min(len(s1), len(s2))
d0 = 1.24 * (L - 15) ** (1 / 3) - 1.8
R, pc, qc = kabsch(x1[i1], x2[i2])
d = np.linalg.norm((x1[i1] - pc) @ R + qc - x2[i2], axis=1)
tm_single = np.sum(1 / (1 + (d / d0) ** 2)) / L
tm_opt, rmsd = tm_score(x1[i1], x2[i2], L)
rows.append((g1, g2, A.subfamily[g1] == A.subfamily[g2], round(tm_single, 4), round(tm_opt, 4), round(rmsd, 2), len(i1), L))
out = pd.DataFrame(rows, columns=["gene1", "gene2", "same_subfamily", "tmscore_single_superposition", "tmscore", "rmsd_core",
"aligned", "L"])
out.to_csv(RES / "abc_struct_sim.csv", index=False)
par = pd.read_csv(RES / "abc_paralog_pairs.csv") if (RES / "abc_paralog_pairs.csv").exists() else None
pd.set_option("display.width", 200)
if par is not None:
keys = {frozenset(p) for p in par[["gene1", "gene2"]].itertuples(index=False)}
out["paralog"] = [frozenset((a, b)) in keys for a, b in zip(out.gene1, out.gene2)]
out.to_csv(RES / "abc_struct_sim.csv", index=False)
from scipy import stats
I = pd.read_csv(RES / "abc_identity.csv", index_col=0)
genes = list(A.index)
n = len(genes)
T = pd.DataFrame(np.nan, index=genes, columns=genes)
for g1, g2, tm in zip(out.gene1, out.gene2, out.tmscore):
T.loc[g1, g2] = T.loc[g2, g1] = tm
iu = np.triu_indices(n, 1)
Iv, Tv = I.loc[genes, genes].values, T.values
obs = stats.spearmanr(Iv[iu], Tv[iu]).statistic
rng = np.random.default_rng(0)
B = 10000
null = np.array([stats.spearmanr(Iv[iu], Tv[np.ix_(p, p)][iu]).statistic for p in (rng.permutation(n) for _ in range(B))])
p_mantel = (np.sum(np.abs(null) >= abs(obs)) + 1) / (B + 1)
pd.DataFrame([("identity vs TM-score, Mantel", obs, p_mantel, len(iu[0]), B)],
columns=["test", "rho", "p_gene_label_permutation", "pairs", "permutations"]).to_csv(
RES / "abc_identity_tm_mantel.csv", index=False)