Python script to build Figure 6

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

# Dictionary with parameters for the Organ Toxicity profile

organ_toxicity_data = {
    # Neurotoxicity (log)
    # Visual bounds set at -3 and 0 for proportionality around -2 and -1.5
    "Neurotoxicity": {"bounds": [-3, -2, -1.5, 0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Drug-Induced Liver Injury (DILI)
    "DILI": {"bounds": [0, 0.3, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # hERG Liability (Cardiac toxicity / QT prolongation)
    "hERG_1uM": {"bounds": [0, 0.15, 0.4, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "hERG_10uM": {"bounds": [0, 0.25, 0.75, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "hERG_30uM": {"bounds": [0, 0.25, 0.8, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "hERG_1-10uM": {"bounds": [0, 0.3, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "hERG_10-30uM": {"bounds": [0, 0.3, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Respiratory Toxicity (Custom color palette extracted from HTML)
    "Respiratory_toxicity": {"bounds": [0, 0.4, 0.8, 1.0], "colors": ["#00FF7F", "#F0E68C", "#CD853F"]},
    
    # Nephrotoxicity
    "Nephrotoxicity": {"bounds": [0, 0.3, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Eye Irritation / Corrosion
    "Eye_corrosion": {"bounds": [0, 0.2, 0.8, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Eye_irritation": {"bounds": [0, 0.3, 0.8, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Skin Toxicity
    "Skin_corrosion": {"bounds": [0, 0.3, 0.8, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Skin_irritation": {"bounds": [0, 0.3, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    "Skin_sensitisation": {"bounds": [0, 0.3, 0.8, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
    
    # Acute Delayed Toxicity (ADT)
    "ADT": {"bounds": [0, 0.4, 0.7, 1.0], "colors": ["#63C28D", "#FFDF33", "#C14E4E"]},
}

def create_organtox_bars():
    """
    Generates and saves the proportional bar charts for the Organ Toxicity parameters.
    """
    # Dimensions requested: 8 cm x 1.3 cm (converted to inches for matplotlib)
    fig_width = 8 / 2.54
    fig_height = 1.3 / 2.54

    # Create a new directory specific for Figure 5 (Organ Toxicity)
    output_dir = "ADMET_OrganToxicity_Bars"
    os.makedirs(output_dir, exist_ok=True)
    
    # Background color matching the established paper style
    bg_color = '#FFFFFF'

    for param, info in organ_toxicity_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 based on value distance
        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 (borders)
        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 for better readability
        # Replace underscores with spaces and add units where appropriate
        display_param = param.replace("_", " ")
        if param == "Neurotoxicity":
            display_param += " (log)"
        else:
            display_param += " (Prob)"
            
        ax.set_xlabel(display_param, fontsize=11, labelpad=5, weight='bold')

        # Adjust layout manually to prevent UserWarning about margins
        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 Organ Toxicity profile bars...")
    create_organtox_bars()
    print("Process completed successfully! Check the 'ADMET_OrganToxicity_Bars' folder.")