Python script to build Figure 8

"script-number-127": Python script for Figure 8.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import os

# Dictionary with parameters for the Endocrine Disruption (Tox21) profile

endocrine_data = {
    # Nuclear Receptors
    "AR": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "ER": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "AR_LBD": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "ER_LBD": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Aromatase": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "AhR": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "PPAR": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "TR": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "GR": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Stress Response Pathways
    "ARE": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "ATAD5": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # EXCEPTION IDENTIFIED IN HTML: HSE cuts at 0.3, not 0.4
    "HSE": {"bounds": [0, 0.3, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    "p53": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "MMP": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
}

def create_endocrine_bars():
    """
    Generates and saves the proportional bar charts for the Endocrine Disruption parameters.
    """
    # Dimensions: 8 cm x 1.3 cm (converted to inches)
    fig_width = 8 / 2.54
    fig_height = 1.3 / 2.54

    # Output directory for Figure 7
    output_dir = "ADMET_Endocrine_Bars"
    os.makedirs(output_dir, exist_ok=True)
    
    # Background color matching the established paper style
    bg_color = '#FFFFFF'

    for param, info in endocrine_data.items():
        bounds = info["bounds"]
        colors = info["colors"]
        
        labels = [str(b) for b in bounds]

        # Initialize figure
        fig, ax = plt.subplots(figsize=(fig_width, fig_height))
        fig.patch.set_facecolor(bg_color)
        ax.set_facecolor(bg_color)

        min_val = bounds[0]
        max_val = bounds[-1]

        # Draw each colored segment proportionally
        for i in range(len(colors)):
            start = bounds[i]
            width = bounds[i+1] - bounds[i]
            rect = patches.Rectangle(
                (start, 0), width, 1, 
                facecolor=colors[i], edgecolor='none'
            )
            ax.add_patch(rect)

        # Axes scaling
        ax.set_xlim(min_val, max_val)
        ax.set_ylim(0, 1)

        # X-axis setup (ticks and labels)
        ax.set_xticks(bounds)
        ax.set_xticklabels(labels, rotation=45, ha='right', rotation_mode='anchor', fontsize=9)
        
        # Hide top, left and right spines
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['left'].set_visible(False)
        
        # Offset the bottom spine slightly downwards
        ax.spines['bottom'].set_position(('outward', 5))
        ax.spines['bottom'].set_linewidth(1)
        
        # Tick styling
        ax.tick_params(axis='x', direction='out', length=4, width=1, colors='black')
        
        # Completely hide the Y axis
        ax.get_yaxis().set_visible(False)

        # Format parameter label
        display_param = param.replace("_", " ") + " (Prob)"
            
        ax.set_xlabel(display_param, fontsize=11, labelpad=5, weight='bold')

        # Adjust layout manually
        plt.subplots_adjust(bottom=0.45)
        
        # Save the figure
        filename = os.path.join(output_dir, f"{param}_bar.png")
        plt.savefig(filename, dpi=300, bbox_inches='tight', facecolor=fig.get_facecolor())
        plt.close()

if __name__ == "__main__":
    print("Starting generation of Endocrine Disruption profile bars with exact HTML bounds...")
    create_endocrine_bars()
    print("Process completed successfully! Check the 'ADMET_Endocrine_Bars' folder.")