Python script to build Figure 10

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

# Dictionary with parameters for the Cosmetic/Dermal Risk Assessment profile

cosmetic_data = {
    "Eye_corrosion": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Eye_irritation": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Skin_corrosion": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Skin_irritation": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Skin_sensitisation": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Acute_dermal_toxicity": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Photoinduced_toxicity": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Phototoxicity_Photoirritation": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Photoallergy": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
}

def create_cosmetic_bars():
    """
    Generates and saves the proportional bar charts for the Cosmetic Risk Assessment parameters.
    Handles multiline titles for long parameter names and standardizes layout.
    """
    fig_width = 8 / 2.54
    fig_height = 1.3 / 2.54

    output_dir = "ADMET_Cosmetic_Bars"
    os.makedirs(output_dir, exist_ok=True)
    
    bg_color = '#FFFFFF'

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

        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 proportional rectangles
        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)

        ax.set_xlim(min_val, max_val)
        ax.set_ylim(0, 1)

        ax.set_xticks(bounds)
        ax.set_xticklabels(labels, rotation=45, ha='right', rotation_mode='anchor', fontsize=9)
        
        # Format axes
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['left'].set_visible(False)
        
        ax.spines['bottom'].set_position(('outward', 5))
        ax.spines['bottom'].set_linewidth(1)
        ax.tick_params(axis='x', direction='out', length=4, width=1, colors='black')
        ax.get_yaxis().set_visible(False)

        # Dynamic label formatting
        raw_display = param.replace("_", " ")
        
        # Forced line breaks for specific long labels to maintain clean layout
        if "Phototoxicity Photoirritation" in raw_display:
            raw_display = raw_display.replace(" Photoirritation", "\nPhotoirritation")
        elif "Skin sensitisation" in raw_display:
            raw_display = raw_display.replace(" sensitisation", "\nsensitisation")
        elif "Acute dermal toxicity" in raw_display:
            raw_display = raw_display.replace(" toxicity", "\ntoxicity")
        elif "Photoinduced toxicity" in raw_display:
            raw_display = raw_display.replace(" toxicity", "\ntoxicity")

        display_param = f"{raw_display} (Prob)"
            
        current_fontsize = 11
        bottom_margin = 0.45

        # Check for newline to adjust margin and font size accordingly
        if "\n" in display_param:
            bottom_margin = 0.55
            current_fontsize = 9 

        ax.set_xlabel(display_param, fontsize=current_fontsize, labelpad=5, weight='bold')

        plt.subplots_adjust(bottom=bottom_margin)
        
        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 Cosmetic Risk Assessment profile bars...")
    create_cosmetic_bars()
    print("Process completed successfully! Check the 'ADMET_Cosmetic_Bars' folder.")