This repository provides an easy-to-use graphical user interface (GUI) for fitting a Henderson-Hasselbalch curve to experimental data and calculating pKa values. The GUI is implemented in Python using tkinter
and generates high-quality interactive plots with Plotly
. The repository contains the main script (pKa_GUI_finall_2.pyw
) and a sample input file (input.csv
) to demonstrate functionality.
The Henderson-Hasselbalch equation is a key tool in chemistry and biochemistry for understanding acid-base equilibria. It is expressed as:
This equation relates the pH of a solution to the protonation state of an ionizable group, often measured as a chemical shift in NMR spectroscopy. This GUI allows users to:
- Fit a Henderson-Hasselbalch curve to experimental data.
- Calculate the pKa value, representing the pH at which the group is 50% protonated.
- Visualize the results interactively.
- File Input: Load a two-column CSV file with headers
pH
andshift
. The sample fileinput.csv
demonstrates the required format. - Interactive Selection: Use SHIFT or CTRL to select specific data points for the fit. This flexibility ensures that users can exclude irrelevant or outlier points.
- Real-Time Feedback: A "DRAW" button initiates the fitting process and generates an interactive plot.
- Error Bars: Experimental variability is visualized with error bars on the plot.
- Fitting Parameters:
Shift_min
andShift_max
represent the minimal and maximal chemical shift values observed during protonation/deprotonation.pKa
is calculated with high precision using advanced curve-fitting techniques.
- Fine-Tuned Axis Representation:
- Numbers on the axes are displayed with two decimal places for clarity.
- Tick marks and spacing are adjusted for better readability.
- Axis lines are prominently displayed without distracting grid lines.
- Interactive plots generated with Plotly include:
- Annotations of fitted parameters (
pKa
,Shift_min
, andShift_max
). - A dynamically updated title showing the name of the loaded CSV file.
- Exported plots open directly in your default browser with a tab title matching the input file.
- Annotations of fitted parameters (
- Informative instructions are displayed at the top of the GUI:
"Load the appropriate two-column CSV file with headers 'pH' and 'shift'. Use SHIFT or CTRL to select the points you want to include for fitting the Henderson-Hasselbalch curve and calculating pKa. Press DRAW."
- The interface adjusts dynamically to fit content, and scrollbars are provided for datasets exceeding the visible area.
The input file must be a two-column CSV file with the following format:
- Header Row:
pH
andshift
. - Columns:
pH
: Numerical values representing the pH of the solution.shift
: Numerical values representing the observed chemical shift.
Example (input.csv
):
pH,shift
2.0,8.10
3.0,8.05
4.0,7.90
5.0,7.40
6.0,6.80
7.0,6.50
The script generates:
- Interactive Plotly Visualizations:
- Scatterplot of experimental data with error bars.
- Fitted Henderson-Hasselbalch curve in red.
- Annotated parameters:
pKa
,Shift_min
, andShift_max
. - Saved as an HTML file with the name matching the input file (e.g.,
input.html
).
- High-Precision Calculations:
pKa
value.- R-squared value to evaluate the goodness of fit.
- File Loading: The user selects a two-column CSV file containing experimental data. The GUI automatically displays the contents in a scrollable table, sorted by
pH
. - Data Selection: Users can highlight data points using SHIFT or CTRL to include only relevant points in the fitting process.
- Curve Fitting: Upon pressing "DRAW":
- The selected data is passed to a curve-fitting algorithm (
scipy.optimize.curve_fit
). - Initial guesses are intelligently derived from the dataset (e.g.,
pKa
is estimated from the steepest slope). - Fitted parameters are calculated with constraints to ensure realistic values.
- The selected data is passed to a curve-fitting algorithm (
- Interactive Plot: A Plotly visualization is generated, saved as an HTML file, and opened in the user's default browser.
To use this script, install the following Python libraries:
-
Required Libraries:
pandas
: For handling CSV data.numpy
: For numerical computations.scipy
: For curve fitting.plotly
: For generating interactive plots.tkinter
: Built-in with Python, used for the GUI.webbrowser
: Built-in with Python, used for opening plots in the default browser.
-
Installation Command: Use
pip
to install all dependencies:pip install pandas numpy scipy plotly
- Clone this repository or download the files:
git clone https://github.com/your-username/henderson-hasselbalch-gui.git cd henderson-hasselbalch-gui
- Run the GUI script:
python pKa_GUI_finall_2.pyw
- Follow the on-screen instructions:
- Load your dataset (
input.csv
). - Select the data points for fitting.
- Click "DRAW" to generate the curve and calculate parameters.
- Load your dataset (
The script ensures accurate parameter estimation by:
- Automatically identifying appropriate bounds for fitting parameters.
- Allowing manual selection of data points to exclude outliers.
Interactive plots provide immediate feedback and make it easy to interpret results, especially for datasets with varying precision or noise.
Even users with minimal coding experience can easily:
- Load data.
- Select points for analysis.
- Generate publication-quality plots.
pKa_GUI_finall_2.pyw
: The main Python script with the GUI implementation.input.csv
: A sample dataset for testing.
Contributions to improve the script or add new features are welcome! Please fork the repository and submit a pull request with your changes.
This project is licensed under the MIT License.