Mercurial > repos > mbernt > baseline_toxicity_calculator
comparison qsar1.py @ 0:ce46f2008024 draft default tip
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 008f820fb9b8ec547e00205f809f982b8f4b8318
| author | mbernt |
|---|---|
| date | Tue, 09 Apr 2024 07:51:18 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:ce46f2008024 |
|---|---|
| 1 import argparse | |
| 2 import re | |
| 3 | |
| 4 import pandas as pd | |
| 5 | |
| 6 parser = argparse.ArgumentParser(description='Calculate baseline toxicity for different aquatic species') | |
| 7 parser.add_argument('--function', type=str, choices=['calculate_baseline', 'apply_linear_functions'], | |
| 8 help='Function to execute') | |
| 9 parser.add_argument('--csv_input', type=argparse.FileType('r'), help='Path to the input csv file') | |
| 10 parser.add_argument('--functions_csv', type=argparse.FileType('r'), default=None, | |
| 11 help='Path to the csv file containing functions (only for apply_linear_functions)') | |
| 12 parser.add_argument('--output', type=argparse.FileType('w'), help='Path for the output csv file') | |
| 13 args = parser.parse_args() | |
| 14 | |
| 15 if args.function == 'calculate_baseline': | |
| 16 df = pd.read_csv(args.csv_input) | |
| 17 df.iloc[:, 0] = df.iloc[:, 0].astype(int) | |
| 18 df['Caenorhabditis elegans [mol/L]'] = 10 ** (-(0.81 * df.iloc[:, 0] + 1.15)) | |
| 19 df['Daphia magna [mol/L]'] = 10 ** (-(0.82 * df.iloc[:, 0] + 1.48)) | |
| 20 df['Danio rerio [mol/L]'] = 10 ** (-(0.99 * df.iloc[:, 0] + 0.78)) | |
| 21 df['Generic Human Cells [mol/L]'] = 0.026 / (10 ** df.iloc[:, 0]) * (1 + 10 ** (0.7 * df.iloc[:, 0] + 0.34) * 3 * 0.001 + 10 ** 3 * 0.07 * 0.001) | |
| 22 df.to_csv(args.output, index=False) | |
| 23 | |
| 24 elif args.function == 'apply_linear_functions': | |
| 25 df = pd.read_csv(args.csv_input) | |
| 26 functions_df = pd.read_csv(args.functions_csv) | |
| 27 | |
| 28 def parse_and_apply_equation(equation, x_values): | |
| 29 # Extract 'a' and 'b' from the equation (assuming the format 'ax+b' or 'ax-b') | |
| 30 pattern = re.compile(r'([+-]?\d*\.?\d*)x([+-]\d+)?') | |
| 31 match = pattern.search(equation) | |
| 32 a = float(match.group(1)) if match.group(1) not in ('', '+', '-') else 1.0 | |
| 33 b = float(match.group(2)) if match.group(2) else 0 | |
| 34 return a * x_values + b | |
| 35 | |
| 36 for i, row in functions_df.iterrows(): | |
| 37 func = row['function'] | |
| 38 df[f'result_{i}'] = parse_and_apply_equation(func, df['logD']) | |
| 39 df.to_csv(args.output, index=False) |
