File size: 5,924 Bytes
94b0695 | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | import os
import json
import random
import numpy as np
import pandas as pd
from scipy import stats
RESULTS_DIR = "results"
SAMPLE_SIZE = 10000
SEED = 42
NN_PARAMS = {
"AA": {"dH": -7.9, "dS": -22.2},
"TT": {"dH": -7.9, "dS": -22.2},
"AT": {"dH": -7.2, "dS": -20.4},
"TA": {"dH": -7.2, "dS": -21.3},
"CA": {"dH": -8.5, "dS": -22.7},
"AC": {"dH": -8.5, "dS": -22.7},
"GT": {"dH": -8.4, "dS": -22.4},
"TG": {"dH": -8.4, "dS": -22.4},
"CT": {"dH": -7.8, "dS": -21.0},
"TC": {"dH": -7.8, "dS": -21.0},
"GA": {"dH": -8.2, "dS": -22.2},
"AG": {"dH": -8.2, "dS": -22.2},
"CG": {"dH": -10.6, "dS": -27.2},
"GC": {"dH": -9.8, "dS": -24.4},
"GG": {"dH": -8.0, "dS": -19.9},
"CC": {"dH": -8.0, "dS": -19.9},
}
INIT_DH = 0.2
INIT_DS = -5.7
R = 1.987e-3
T_KELVIN = 310.15
def calc_melting_temp(seq):
seq = seq.upper()
dH = INIT_DH
dS = INIT_DS
gc = seq.count("G") + seq.count("C")
if gc > 0:
dH += 0.1
dS -= 2.8
else:
dH += 2.2
dS += 6.9
for i in range(len(seq) - 1):
dinuc = seq[i:i + 2]
if dinuc in NN_PARAMS:
dH += NN_PARAMS[dinuc]["dH"]
dS += NN_PARAMS[dinuc]["dS"]
dS_total = dS / 1000.0
if abs(dS_total) < 1e-10:
return None
return dH / dS_total - 273.15
def calc_gibbs(seq, T=T_KELVIN):
seq = seq.upper()
dH = INIT_DH
dS = INIT_DS
for i in range(len(seq) - 1):
dinuc = seq[i:i + 2]
if dinuc in NN_PARAMS:
dH += NN_PARAMS[dinuc]["dH"]
dS += NN_PARAMS[dinuc]["dS"]
return dH - T * (dS / 1000.0)
def random_sequence(length, rng):
return "".join(rng.choice(["A", "C", "G", "T"]) for _ in range(length))
def secondary_structure_flags(seq):
seq = seq.upper()
hairpin = any(seq[i] == seq[-(i + 1)] for i in range(len(seq) // 2))
return {"hairpin": hairpin, "g4": "GGGG" in seq, "imotif": "CCCC" in seq}
def main():
nullomers_path = os.path.join(RESULTS_DIR, "nullomers_k11.txt")
if not os.path.exists(nullomers_path):
raise FileNotFoundError(f"{nullomers_path} not found. Run 01_nullomer_identification.py first.")
with open(nullomers_path) as f:
all_nullomers = [line.strip() for line in f if line.strip()]
rng_np = np.random.default_rng(SEED)
rng_py = random.Random(SEED)
sample_size = min(SAMPLE_SIZE, len(all_nullomers))
nullomer_sample = rng_np.choice(all_nullomers, size=sample_size, replace=False).tolist()
k = len(nullomer_sample[0])
random_seqs = [random_sequence(k, rng_py) for _ in range(sample_size)]
records = []
for seq in nullomer_sample:
flags = secondary_structure_flags(seq)
records.append({"sequence": seq, "group": "nullomer",
"Tm": calc_melting_temp(seq), "dG": calc_gibbs(seq),
"GC": (seq.count("G") + seq.count("C")) / len(seq), **flags})
for seq in random_seqs:
flags = secondary_structure_flags(seq)
records.append({"sequence": seq, "group": "random",
"Tm": calc_melting_temp(seq), "dG": calc_gibbs(seq),
"GC": (seq.count("G") + seq.count("C")) / len(seq), **flags})
df = pd.DataFrame(records).dropna(subset=["Tm", "dG"])
df.to_csv(os.path.join(RESULTS_DIR, "nullomer_thermodynamics.csv"), index=False)
null_df = df[df["group"] == "nullomer"]
rand_df = df[df["group"] == "random"]
t_tm, p_tm = stats.ttest_ind(null_df["Tm"], rand_df["Tm"])
t_dg, p_dg = stats.ttest_ind(null_df["dG"], rand_df["dG"])
r_gc_tm, p_gc_tm = stats.pearsonr(null_df["GC"], null_df["Tm"])
delta_dg = null_df["dG"].mean() - rand_df["dG"].mean()
boltzmann_fold = np.exp(-delta_dg / (R * T_KELVIN))
summary = {
"n_nullomers": int(len(null_df)),
"n_random": int(len(rand_df)),
"nullomer_Tm_mean": round(float(null_df["Tm"].mean()), 2),
"nullomer_Tm_std": round(float(null_df["Tm"].std()), 2),
"random_Tm_mean": round(float(rand_df["Tm"].mean()), 2),
"random_Tm_std": round(float(rand_df["Tm"].std()), 2),
"Tm_difference": round(float(null_df["Tm"].mean() - rand_df["Tm"].mean()), 2),
"Tm_t_statistic": round(float(t_tm), 3),
"Tm_p_value": float(p_tm),
"nullomer_dG_mean": round(float(null_df["dG"].mean()), 2),
"nullomer_dG_std": round(float(null_df["dG"].std()), 2),
"random_dG_mean": round(float(rand_df["dG"].mean()), 2),
"random_dG_std": round(float(rand_df["dG"].std()), 2),
"delta_dG_kcal_mol": round(float(delta_dg), 2),
"boltzmann_fold_disadvantage": round(float(boltzmann_fold), 1),
"dG_t_statistic": round(float(t_dg), 3),
"dG_p_value": float(p_dg),
"GC_Tm_pearson_r": round(float(r_gc_tm), 3),
"GC_Tm_p_value": float(p_gc_tm),
"pct_very_stable_nullomers": round(float((null_df["dG"] < -10).mean() * 100), 1),
"pct_very_stable_random": round(float((rand_df["dG"] < -10).mean() * 100), 1),
"pct_hairpin": round(float(null_df["hairpin"].mean() * 100), 1),
"pct_g4": round(float(null_df["g4"].mean() * 100), 1),
"pct_imotif": round(float(null_df["imotif"].mean() * 100), 1),
}
with open(os.path.join(RESULTS_DIR, "thermodynamic_summary.json"), "w") as f:
json.dump(summary, f, indent=2)
print(f"Tm: {summary['nullomer_Tm_mean']} vs {summary['random_Tm_mean']} °C "
f"t={summary['Tm_t_statistic']} p={summary['Tm_p_value']:.2e}\n"
f"dG: {summary['nullomer_dG_mean']} vs {summary['random_dG_mean']} kcal/mol "
f"ΔΔG={summary['delta_dG_kcal_mol']} {summary['boltzmann_fold_disadvantage']}-fold\n"
f"GC-Tm r={summary['GC_Tm_pearson_r']} "
f"very stable: {summary['pct_very_stable_nullomers']}%")
if __name__ == "__main__":
main()
|