import matplotlib.pyplot as plt
import numpy as np

filename = "rmsd.xvg"

time = []
rmsd = []

with open(filename, "r") as f:
    for line in f:
        if line.startswith("#") or line.startswith("@"):
            continue

        parts = line.split()
        if len(parts) >= 2:
            time.append(float(parts[0]))
            rmsd.append(float(parts[1]))

time = np.array(time)
rmsd = np.array(rmsd)

window = 20
smoothed = np.convolve(rmsd, np.ones(window) / window, mode="valid")
time_smooth = time[:len(smoothed)]

plt.figure(figsize=(10, 6))
plt.plot(time, rmsd, color="thistle", linewidth=1.0, alpha=0.8, label="Backbone RMSD")
plt.plot(time_smooth, smoothed, color="purple", linewidth=2.0,
         label=f"Moving average ({window} points)")

plt.xlabel("Time (ns)", fontsize=14)
plt.ylabel("Backbone RMSD (nm)", fontsize=14)

plt.xticks(fontsize=14)
plt.yticks(fontsize=14)

plt.grid(True, linestyle="--", alpha=0.5)
plt.legend(fontsize=12)

plt.tight_layout()
plt.savefig("rmsd_profile.png", dpi=300, bbox_inches="tight")
plt.show()
