# Path Configuration from tools.preprocess import * # Processing context trait = "Liver_cirrhosis" cohort = "GSE185529" # Input paths in_trait_dir = "../DATA/GEO/Liver_cirrhosis" in_cohort_dir = "../DATA/GEO/Liver_cirrhosis/GSE185529" # Output paths out_data_file = "./output/preprocess/3/Liver_cirrhosis/GSE185529.csv" out_gene_data_file = "./output/preprocess/3/Liver_cirrhosis/gene_data/GSE185529.csv" out_clinical_data_file = "./output/preprocess/3/Liver_cirrhosis/clinical_data/GSE185529.csv" json_path = "./output/preprocess/3/Liver_cirrhosis/cohort_info.json" # Step 1: Get file paths soft_file_path, matrix_file_path = geo_get_relevant_filepaths(in_cohort_dir) # First examine SOFT file contents to identify subseries with gzip.open(soft_file_path, 'rt') as f: soft_content = f.read() # Look for subseries IDs subseries_match = re.search(r'!Series_relation = SuperSeries of: (GSE\d+)', soft_content) if subseries_match: subseries_id = subseries_match.group(1) subseries_files = [f for f in os.listdir(in_cohort_dir) if subseries_id in f] if subseries_files: subseries_soft = [f for f in subseries_files if 'soft' in f.lower()][0] subseries_matrix = [f for f in subseries_files if 'matrix' in f.lower()][0] soft_file_path = os.path.join(in_cohort_dir, subseries_soft) matrix_file_path = os.path.join(in_cohort_dir, subseries_matrix) # Extract background info and clinical data from the appropriate files background_info, clinical_data = get_background_and_clinical_data(soft_file_path) if len(clinical_data.columns) <= 2: # If SOFT file didn't yield enough info, try matrix file background_info, clinical_data = get_background_and_clinical_data(matrix_file_path) # Get dictionary of unique values for each clinical feature unique_values_dict = get_unique_values_by_row(clinical_data) # Print background info and sample characteristics print("Dataset Background Information:") print("-" * 80) print(background_info) print("\nSample Characteristics:") print("-" * 80) print(json.dumps(unique_values_dict, indent=2)) # 1. Gene Expression Data Availability is_gene_available = True # Based on series title which implies gene expression study # 2.1 Data Availability trait_row = None # No disease/control info in characteristics age_row = None # No age info in characteristics gender_row = None # No gender info in characteristics # 2.2 Data Type Conversion # Only define convert_trait since other data not available def convert_trait(x): if x is None: return None value = x.split(': ')[1].lower() if ': ' in x else x.lower() # Return None since we don't have trait data return None # 3. Save metadata validate_and_save_cohort_info( is_final=False, cohort=cohort, info_path=json_path, is_gene_available=is_gene_available, is_trait_available=False # trait_row is None ) # 4. Skip clinical feature extraction since trait_row is None # 1. Extract gene expression data from matrix file genetic_data = get_genetic_data(matrix_file_path) # 2. Print first 20 row IDs print("First 20 gene/probe identifiers:") print(genetic_data.index[:20]) # Based on the gene IDs shown ('2824546_st', '2824549_st', etc.), these are # not standard human gene symbols but rather probe identifiers from an Affymetrix microarray platform. # They need to be mapped to proper gene symbols for downstream analysis. requires_gene_mapping = True # 1. Extract gene annotation data from SOFT file gene_annotation = get_gene_annotation(soft_file_path) # 2. Preview annotation data print("Column names and first few values in gene annotation data:") print(preview_df(gene_annotation)) # 2. Extract mapping dataframe with probe IDs and gene symbols mapping_data = gene_annotation[['probeset_id', 'gene_assignment']].copy() mapping_data = mapping_data.rename(columns={'probeset_id': 'ID', 'gene_assignment': 'Gene'}) mapping_data = mapping_data.astype({'ID': 'str'}) # Parse the gene_assignment field to extract valid gene symbols mapping_data['Gene'] = mapping_data['Gene'].apply(lambda x: re.search(r'//\s*(\w+)\s*//', str(x)).group(1) if pd.notnull(x) and '//' in str(x) else None) mapping_data = mapping_data.dropna() # 3. Convert probe-level data to gene-level expression data gene_data = apply_gene_mapping(genetic_data, mapping_data) # Preview transformed data print("\nFirst few gene identifiers after mapping:") print(gene_data.index[:20]) # 2. Extract mapping dataframe with probe IDs and gene symbols mapping_data = gene_annotation[['ID', 'gene_assignment']].copy() mapping_data['ID'] = mapping_data['ID'].astype(str) + '_st' # Add '_st' suffix to match expression data format def extract_gene(text): if pd.isna(text) or '//' not in str(text): return None matches = re.findall(r'//\s*(\w+)\s*//', str(text)) if matches: # Convert mouse gene symbols to human by making uppercase return matches[0].upper() return None mapping_data['Gene'] = mapping_data['gene_assignment'].apply(extract_gene) mapping_data = mapping_data[['ID', 'Gene']].dropna() # 3. Convert probe-level data to gene-level expression data gene_data = apply_gene_mapping(genetic_data, mapping_data) # Normalize gene symbols using NCBI database info gene_data = normalize_gene_symbols_in_index(gene_data) # Preview transformed data print("\nFirst few gene identifiers after mapping:") print(gene_data.index[:20])