# Path Configuration from tools.preprocess import * # Processing context trait = "Allergies" cohort = "GSE184382" # Input paths in_trait_dir = "../DATA/GEO/Allergies" in_cohort_dir = "../DATA/GEO/Allergies/GSE184382" # Output paths out_data_file = "./output/z1/preprocess/Allergies/GSE184382.csv" out_gene_data_file = "./output/z1/preprocess/Allergies/gene_data/GSE184382.csv" out_clinical_data_file = "./output/z1/preprocess/Allergies/clinical_data/GSE184382.csv" json_path = "./output/z1/preprocess/Allergies/cohort_info.json" # Step 1: Initial Data Loading import os from tools.preprocess import * # Try library helper first, then fall back to a robust recursive search def find_geo_files_recursive(root_dir: str): matrix_candidates = [] soft_candidates = [] for dirpath, _, filenames in os.walk(root_dir): for fname in filenames: lf = fname.lower() full_path = os.path.join(dirpath, fname) # Prefer GEO series_matrix files if ('series_matrix' in lf or 'matrix' in lf) and lf.endswith('.gz'): matrix_candidates.append(full_path) # SOFT files if 'soft' in lf and (lf.endswith('.gz') or lf.endswith('.soft') or lf.endswith('.txt')): soft_candidates.append(full_path) matrix_candidates.sort() soft_candidates.sort() return matrix_candidates[0] if matrix_candidates else None, soft_candidates[0] if soft_candidates else None soft_file = None matrix_file = None # Attempt 1: use library helper try: soft_guess, matrix_guess = geo_get_relevant_filepaths(in_cohort_dir) # Note: geo_get_relevant_filepaths returns (soft, matrix) soft_file = soft_guess matrix_file = matrix_guess except Exception: pass # Attempt 2: recursive search if needed if matrix_file is None or not os.path.exists(matrix_file): rec_matrix, rec_soft = find_geo_files_recursive(in_cohort_dir) matrix_file = matrix_file if (matrix_file and os.path.exists(matrix_file)) else rec_matrix soft_file = soft_file if (soft_file and os.path.exists(soft_file)) else rec_soft # Handle missing matrix file gracefully (no hard failure) if matrix_file is None or not os.path.exists(matrix_file): print(f"WARNING: No series matrix file found under {in_cohort_dir}. Skipping data extraction for Step 1.") # Record dataset availability status validate_and_save_cohort_info( is_final=False, cohort=cohort, info_path=json_path, is_gene_available=False, is_trait_available=False ) # Fallback outputs for required prints background_info = "" sample_characteristics_dict = {} print("Background Information:") print(background_info) print("Sample Characteristics Dictionary:") print(sample_characteristics_dict) else: # Informative prints on selected files print(f"Matrix file selected: {matrix_file}") if soft_file is not None: print(f"SOFT file selected: {soft_file}") else: print("WARNING: No SOFT file found. Proceeding with matrix file only for Step 1.") # 2. Read the matrix file to obtain background information and sample characteristics data background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design'] clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1'] background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes) # 3. Obtain the sample characteristics dictionary from the clinical dataframe (limit unique values per feature) sample_characteristics_dict = get_unique_values_by_row(clinical_data, max_len=20) # 4. Explicitly print out all the background information and the sample characteristics dictionary print("Background Information:") print(background_info) print("Sample Characteristics Dictionary:") print(sample_characteristics_dict)