nRing Distribution Dashboard

DrugBank
MolPort
Python script number 5 to build the frequency distribution graph of the nRing parameter on DrugBank molecules.
import matplotlib.pyplot as plt
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
import numpy as np

# 1. nRing Data (Number of Rings)
bin_centers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
frequencies = [15.35, 15.08, 19.21, 18.74, 17.74, 7.60, 3.39, 1.12, 1.08, 0.50, 0.19]

# 2. Gaussian Fit Parameters
amplitude = 19.98
mean = 2.147
sd = 2.285

# Generate smooth X data for the curve
x_smooth = np.linspace(-1, 11, 300)

# Calculate Y using the Gaussian equation
y_smooth = amplitude * np.exp(-0.5 * ((x_smooth - mean) / sd)**2)

# 3. Define colors (nRing Traffic Light)
colors = []
for x in bin_centers:
    # Optimal Range: 1 to 4 rings (The vast majority of drugs)
    if 1 <= x <= 4:
        colors.append('green')
    # Caution Range: 0 (Acyclic) or 5-6 (Complex)
    elif (x == 0) or (5 <= x <= 6):
        colors.append('gold')
    # Risk Rank: > 6 (Too complex/rigid)
    else:
        colors.append('firebrick')

# 4. Create the chart
plt.figure(figsize=(7, 6))

# A. Draw Bars
plt.bar(bin_centers, frequencies, width=0.8, color=colors, edgecolor='black', alpha=0.7, label='Data Frequency')

# B. Draw Trend Line
plt.plot(x_smooth, y_smooth, color='darkorange', linewidth=2.5, label='Gaussian Fit')

# 5. Tags and Titles
plt.xlabel('Number of Rings (nRing)', fontsize=12)
plt.ylabel('% Frequency', fontsize=12)
plt.title('Number of Rings Distribution', fontsize=14)

# Adjust X axis
plt.xticks(bin_centers)
plt.xlim(-1, 11)

# 6. Custom Legend
legend_elements = [
    Line2D([0], [0], color='darkorange', lw=2, label=f'Fit (Mean={mean}, SD={sd})'),
    Patch(facecolor='green', edgecolor='black', alpha=0.7, label='Optimal (1 - 4 Rings)'),
    Patch(facecolor='gold', edgecolor='black', alpha=0.7, label='Flexible/Complex (0, 5-6 Rings)'),
    Patch(facecolor='firebrick', edgecolor='black', alpha=0.7, label='High Rigidity risk (> 6 Rings)')
]

plt.legend(handles=legend_elements, loc='upper right')
plt.grid(axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()

plt.show()