Mercurial > repos > bgruening > flexynesis_cbioportal_import
annotate flexynesis_utils.py @ 8:461b8e2d79d5 draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit bcee55468e70cd025cc5359bef5e9991e893eca9
| author | bgruening | 
|---|---|
| date | Thu, 16 Oct 2025 20:15:31 +0000 | 
| parents | 693011647a67 | 
| children | 
| rev | line source | 
|---|---|
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 1 #!/usr/bin/env python | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 2 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 3 import argparse | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 4 import os | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 5 import sys | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 6 from pathlib import Path | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 7 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 8 import pandas as pd | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 9 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 10 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 11 def read_data(data_input, index=False): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 12 """Load CSV or TSV data file.""" | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 13 try: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 14 file_ext = Path(data_input).suffix.lower() | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 15 sep = ',' if file_ext == '.csv' else '\t' | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 16 index_col = 0 if index else None | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 17 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 18 if file_ext in ['.csv', '.tsv', '.txt', '.tab', '.tabular']: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 19 return pd.read_csv(data_input, sep=sep, index_col=index_col) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 20 else: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 21 raise ValueError(f"Unsupported file extension: {file_ext}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 22 except Exception as e: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 23 raise ValueError(f"Error loading data from {data_input}: {e}") from e | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 24 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 25 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 26 def binarize_mutations(df, gene_idx=1, sample_idx=2): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 27 """ | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 28 Binarize mutation data by creating a matrix of gene x sample with 1/0 values. | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 29 """ | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 30 # galaxy index is 1-based, convert to zero-based | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 31 gene_idx -= 1 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 32 sample_idx -= 1 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 33 # check idx | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 34 if gene_idx >= len(df.columns) or sample_idx >= len(df.columns): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 35 raise ValueError(f"Column indices out of bounds. DataFrame has {len(df.columns)} columns, " | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 36 f"but requested indices are {gene_idx} and {sample_idx}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 37 if gene_idx == sample_idx: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 38 raise ValueError("Gene and sample column indices must be different") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 39 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 40 # Get column names by index | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 41 gene_col = df.columns[gene_idx] | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 42 print(f"Using gene column: {gene_col} (index {gene_idx})") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 43 sample_col = df.columns[sample_idx] | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 44 print(f"Using sample column: {sample_col} (index {sample_idx})") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 45 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 46 # Check if columns contain data | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 47 if df[gene_col].isna().all(): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 48 raise ValueError(f"Gene column (index {gene_idx}) contains only NaN values.") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 49 if df[sample_col].isna().all(): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 50 raise ValueError(f"Sample column (index {sample_idx}) contains only NaN values.") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 51 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 52 # Group by gene and sample, count mutations | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 53 mutation_counts = df.groupby([gene_col, sample_col]).size().reset_index(name='count') | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 54 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 55 # Create pivot table | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 56 mutation_matrix = mutation_counts.pivot(index=gene_col, columns=sample_col, values='count').fillna(0) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 57 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 58 # Binarize: convert any count > 0 to 1 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 59 mutation_matrix[mutation_matrix > 0] = 1 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 60 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 61 return mutation_matrix | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 62 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 63 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 64 def make_data_dict(clin_path, omics_paths): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 65 """Read clinical and omics data files into a dictionary.""" | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 66 data = {} | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 67 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 68 # Read clinical data | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 69 print(f"Reading clinical data from {clin_path}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 70 try: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 71 clin = read_data(clin_path, index=True) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 72 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 73 if clin.empty: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 74 raise ValueError(f"Clinical file {clin_path} is empty") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 75 data['clin'] = clin | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 76 print(f"Loaded clinical data: {clin.shape[0]} samples, {clin.shape[1]} features") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 77 except Exception as e: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 78 raise ValueError(f"Error reading clinical file {clin_path}: {e}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 79 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 80 # Read omics data | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 81 print(f"Reading omics data from {', '.join(omics_paths)}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 82 for path in omics_paths: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 83 try: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 84 name = os.path.splitext(os.path.basename(path))[0] | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 85 df = read_data(path, index=True) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 86 if df.empty: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 87 print(f"Warning: Omics file {path} is empty, skipping") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 88 continue | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 89 data[name] = df | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 90 print(f"Loaded {name}: {df.shape[0]} features, {df.shape[1]} samples") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 91 except Exception as e: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 92 print(f"Warning: Error reading omics file {path}: {e}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 93 continue | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 94 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 95 if len(data) == 1: # Only clinical data loaded | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 96 raise ValueError("No omics data was successfully loaded") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 97 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 98 return data | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 99 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 100 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 101 def validate_data_consistency(data): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 102 """Validate that clinical and omics data have consistent samples.""" | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 103 clin_samples = set(data['clin'].index) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 104 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 105 for name, df in data.items(): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 106 if name == 'clin': | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 107 continue | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 108 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 109 omics_samples = set(df.columns) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 110 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 111 # Check for sample overlap | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 112 common_samples = clin_samples.intersection(omics_samples) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 113 if len(common_samples) == 0: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 114 raise ValueError(f"No common samples between clinical data and {name}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 115 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 116 missing_in_omics = clin_samples - omics_samples | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 117 missing_in_clin = omics_samples - clin_samples | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 118 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 119 if missing_in_omics: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 120 print(f"Warning: {len(missing_in_omics)} clinical samples not found in {name}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 121 if missing_in_clin: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 122 print(f"Warning: {len(missing_in_clin)} samples in {name} not found in clinical data") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 123 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 124 return True | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 125 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 126 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 127 def split_and_save_data(data, ratio=0.7, output_dir='.'): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 128 """Split data into train/test sets and save to files.""" | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 129 # Validate data consistency first | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 130 validate_data_consistency(data) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 131 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 132 samples = data['clin'].index.tolist() | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 133 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 134 train_samples = list(pd.Series(samples).sample(frac=ratio, random_state=42)) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 135 test_samples = list(set(samples) - set(train_samples)) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 136 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 137 train_data = {} | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 138 test_data = {} | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 139 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 140 for key, df in data.items(): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 141 try: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 142 if key == 'clin': | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 143 train_data[key] = df.loc[df.index.intersection(train_samples)] | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 144 test_data[key] = df.loc[df.index.intersection(test_samples)] | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 145 else: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 146 train_data[key] = df.loc[:, df.columns.intersection(train_samples)] | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 147 test_data[key] = df.loc[:, df.columns.intersection(test_samples)] | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 148 except Exception as e: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 149 print(f"Error splitting data {key}: {e}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 150 continue | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 151 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 152 # Create output directories | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 153 os.makedirs(os.path.join(output_dir, 'train'), exist_ok=True) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 154 os.makedirs(os.path.join(output_dir, 'test'), exist_ok=True) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 155 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 156 # Save train and test data | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 157 for key in data.keys(): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 158 try: | 
| 6 
3f7481deaff1
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
 bgruening parents: 
2diff
changeset | 159 train_data[key].to_csv(os.path.join(output_dir, 'train', f'{key}.tabular'), sep='\t') | 
| 
3f7481deaff1
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
 bgruening parents: 
2diff
changeset | 160 test_data[key].to_csv(os.path.join(output_dir, 'test', f'{key}.tabular'), sep='\t') | 
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 161 except Exception as e: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 162 print(f"Error saving {key}: {e}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 163 continue | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 164 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 165 | 
| 7 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 166 def validate_numeric_column(df, column_names, require_integer=False): | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 167 """ Validate that a column(s) in the DataFrame contains numeric values. """ | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 168 if isinstance(column_names, str): | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 169 # Handle comma-separated string: "col1,col2,col3" | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 170 if ',' in column_names: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 171 column_names = [col.strip() for col in column_names.split(',')] | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 172 else: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 173 # Single column name | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 174 column_names = [column_names] | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 175 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 176 # Validate each column | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 177 for column_name in column_names: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 178 if column_name not in df.columns: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 179 raise ValueError(f"Column '{column_name}' not found in DataFrame.") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 180 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 181 try: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 182 numeric_col = pd.to_numeric(df[column_name], errors='raise') | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 183 except Exception as e: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 184 raise ValueError(f"Non-numeric values found in column '{column_name}': {e}") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 185 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 186 if require_integer: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 187 # Check if all non-null values are equivalent to integers | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 188 non_null_values = numeric_col.dropna() | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 189 if not (non_null_values == non_null_values.round()).all(): | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 190 raise ValueError(f"Column '{column_name}' contains non-integer numeric values.") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 191 print(f"Column '{column_name}': All values are integers or integer-equivalent floats.") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 192 else: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 193 print(f"Column '{column_name}': All values are numeric (integers and floats accepted).") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 194 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 195 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 196 def validate_survival(df, column_names): | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 197 """Validate survival column(s) (integer).""" | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 198 validate_numeric_column(df, column_names, require_integer=True) | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 199 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 200 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 201 def validate_covariate(df, column_names): | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 202 """Validate covariate column(s) (numeric).""" | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 203 validate_numeric_column(df, column_names, require_integer=False) | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 204 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 205 | 
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 206 def main(): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 207 parser = argparse.ArgumentParser(description='Flexynesis extra utilities') | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 208 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 209 parser.add_argument("--util", type=str, required=True, | 
| 7 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 210 choices=['split', 'binarize', 'validate_survival', 'validate_covariate'], | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 211 help="Utility function: 'split' for spiting data to train and test, 'binarize' for creating a binarized matrix from a mutation data, 'validate_survival' for validating survival data.") | 
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 212 | 
| 7 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 213 # Arguments for split (clin also for validate_survival and validate_covariate) | 
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 214 parser.add_argument('--clin', required=False, | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 215 help='Path to clinical data CSV file (samples in rows)') | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 216 parser.add_argument('--omics', required=False, | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 217 help='Comma-separated list of omics CSV files (samples in columns)') | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 218 parser.add_argument('--split', type=float, default=0.7, | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 219 help='Train split ratio (default: 0.7)') | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 220 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 221 # Arguments for binarize | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 222 parser.add_argument('--mutations', type=str, required=False, | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 223 help='Path to mutation data CSV file (samples in rows, genes in columns)') | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 224 parser.add_argument('--gene_idx', type=int, default=0, | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 225 help='Column index for genes in mutation data (default: 0)') | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 226 parser.add_argument('--sample_idx', type=int, default=1, | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 227 help='Column index for samples in mutation data (default: 1)') | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 228 | 
| 7 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 229 # Arguments for validate_survival and validate_covariate | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 230 parser.add_argument('--clin_variable', type=str, required=False, | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 231 help='Column name for clinical variable (e.g., death, SEX, ...)') | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 232 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 233 # common arguments (binarize and split) | 
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 234 parser.add_argument('--out', default='.', | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 235 help='Output directory (default: current directory)') | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 236 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 237 args = parser.parse_args() | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 238 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 239 try: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 240 # validate utility function | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 241 if not args.util: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 242 raise ValueError("Utility function must be specified") | 
| 7 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 243 if args.util not in ['split', 'binarize', 'validate_survival', 'validate_covariate']: | 
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 244 raise ValueError(f"Invalid utility function: {args.util}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 245 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 246 if args.util == 'split': | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 247 # Validate inputs | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 248 if not args.clin: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 249 raise ValueError("Clinical data file must be provided") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 250 if not args.omics: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 251 raise ValueError("At least one omics file must be provided") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 252 if not os.path.isfile(args.clin): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 253 raise FileNotFoundError(f"Clinical file not found: {args.clin}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 254 # Validate split ratio | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 255 if not 0 < args.split < 1: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 256 raise ValueError(f"Split ratio must be between 0 and 1, got {args.split}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 257 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 258 elif args.util == 'binarize': | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 259 # Validate mutation data file | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 260 if not args.mutations: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 261 raise ValueError("Mutation data file must be provided") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 262 if not os.path.isfile(args.mutations): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 263 raise FileNotFoundError(f"Mutation data file not found: {args.mutations}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 264 # Validate gene and sample indices | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 265 if args.gene_idx < 0 or args.sample_idx < 0: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 266 raise ValueError("Gene and sample indices must be non-negative integers") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 267 | 
| 7 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 268 elif args.util == 'validate_survival' or args.util == 'validate_covariate': | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 269 # Validate clinical data file | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 270 if not args.clin: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 271 raise ValueError("Clinical data file must be provided") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 272 if not os.path.isfile(args.clin): | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 273 raise FileNotFoundError(f"Clinical file not found: {args.clin}") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 274 # Validate survival event variable | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 275 if not args.clin_variable: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 276 raise ValueError("Survival event variable must be specified") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 277 | 
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 278 # Create output directory if it doesn't exist | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 279 if not os.path.exists(args.out): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 280 os.makedirs(args.out) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 281 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 282 if args.util == 'split': | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 283 # Parse omics files | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 284 omics_files = [f.strip() for f in args.omics.split(',') if f.strip()] | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 285 if not omics_files: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 286 raise ValueError("At least one omics file must be provided") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 287 # Check omics files exist | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 288 for f in omics_files: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 289 if not os.path.isfile(f): | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 290 raise FileNotFoundError(f"Omics file not found: {f}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 291 data = make_data_dict(args.clin, omics_files) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 292 split_and_save_data(data, ratio=args.split, output_dir=args.out) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 293 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 294 elif args.util == 'binarize': | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 295 mutations_df = read_data(args.mutations, index=False) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 296 if mutations_df.empty: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 297 raise ValueError("Mutation data file is empty") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 298 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 299 binarized_matrix = binarize_mutations(mutations_df, gene_idx=args.gene_idx, sample_idx=args.sample_idx) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 300 # Save binarized matrix | 
| 6 
3f7481deaff1
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
 bgruening parents: 
2diff
changeset | 301 output_file = os.path.join(args.out, 'binarized_mutations.tabular') | 
| 
3f7481deaff1
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
 bgruening parents: 
2diff
changeset | 302 binarized_matrix.to_csv(output_file, sep='\t') | 
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 303 print(f"Binarized mutation matrix saved to {output_file}") | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 304 | 
| 7 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 305 elif args.util == 'validate_survival': | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 306 clin_df = read_data(args.clin, index=False) | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 307 if clin_df.empty: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 308 raise ValueError("Clinical data file is empty") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 309 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 310 # Validate survival event variable | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 311 validate_survival(clin_df, args.clin_variable) | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 312 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 313 elif args.util == 'validate_covariate': | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 314 clin_df = read_data(args.clin, index=False) | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 315 if clin_df.empty: | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 316 raise ValueError("Clinical data file is empty") | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 317 | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 318 # Validate clinical variable | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 319 validate_covariate(clin_df, args.clin_variable) | 
| 
693011647a67
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
 bgruening parents: 
6diff
changeset | 320 | 
| 2 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 321 except Exception as e: | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 322 print(f"Error: {e}", file=sys.stderr) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 323 sys.exit(1) | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 324 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 325 | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 326 if __name__ == "__main__": | 
| 
ee7e61ab554d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit b2463fb68d0ae54864d87718ee72f5e063aa4587
 bgruening parents: diff
changeset | 327 main() | 
