Python script to build Figure 7

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

# Dictionary with parameters for the Toxicity Endpoints profile
toxicity_endpoints_data = {
    # Ames test (Genotoxicity)
    "Ames": {"bounds": [0, 0.3, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Carcinogenicity - Categorical (Prob)
    "Mouse_carcinogenicity_c": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Rat_carcinogenicity_c": {"bounds": [0, 0.5, 0.8, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Rodents_carcinogenicity": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Carcinogenicity - Continuous (-log TD50)
    # Visual bounds extended symmetrically for plotting
    "Mouse_carcinogenicity": {"bounds": [0, 1, 3, 5], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Rat_carcinogenicity": {"bounds": [0, 1, 3, 5], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Systemic and Subcellular Toxicities (Prob)
    "Micronucleus": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Reproductive_toxicity": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Mitochondrial_toxicity": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Hemolytic_toxicity": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Repeated_dose_toxicity": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Acute Oral Toxicity (AOT)
    "AOT_c": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "AOT": {"bounds": [-3.7, -2.7, -1.7, -0.7], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # FDA Maximum Recommended Daily Dose (FDAMDD)
    "FDAMDD_c": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "FDAMDD": {"bounds": [-1, 1, 3, 5], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
}

def create_tox_endpoints_bars():
    """
    Generates and saves the proportional bar charts for the Toxicity Endpoints parameters.
    Handles specific text overflow issue for Mouse_carcinogenicity.
    """
    # Dimensions: 8 cm x 1.3 cm (converted to inches)
    fig_width = 8 / 2.54
    fig_height = 1.3 / 2.54

    # Output directory
    output_dir = "ADMET_ToxEndpoints_Bars_Corrected"
    os.makedirs(output_dir, exist_ok=True)
    
    # Paper style background color
    bg_color = '#FFFFFF'

    for param, info in toxicity_endpoints_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 and format bottom spine
        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)

        # Format parameter label and add strict units
        display_param = param.replace("_", " ")
        if param in ["Mouse_carcinogenicity", "Rat_carcinogenicity"]:
            display_param += " (-log TD50)"
        elif param == "AOT":
            display_param += " (-log LD50)"
        elif param == "FDAMDD":
            display_param += " (-log nmol/Kg/d)"
        else:
            display_param += " (Prob)"
            
        # SPECIFIC FIX FOR MOUSE_CARCINOGENICITY:
        if param == "Mouse_carcinogenicity":
            current_fontsize = 9 # Smaller font for this specific plot only
        else:
            current_fontsize = 11 # Standard font size for all other plots
            
        ax.set_xlabel(display_param, fontsize=current_fontsize, 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 Toxicity Endpoints profile bars...")
    create_tox_endpoints_bars()
    print("Process completed successfully! Check the 'ADMET_ToxEndpoints_Bars' folder.")