# Path Configuration from tools.preprocess import * # Processing context trait = "Parkinsons_Disease" cohort = "GSE101534" # Input paths in_trait_dir = "../DATA/GEO/Parkinsons_Disease" in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE101534" # Output paths out_data_file = "./output/preprocess/3/Parkinsons_Disease/GSE101534.csv" out_gene_data_file = "./output/preprocess/3/Parkinsons_Disease/gene_data/GSE101534.csv" out_clinical_data_file = "./output/preprocess/3/Parkinsons_Disease/clinical_data/GSE101534.csv" json_path = "./output/preprocess/3/Parkinsons_Disease/cohort_info.json" # Get file paths soft_file_path, matrix_file_path = geo_get_relevant_filepaths(in_cohort_dir) # Get background info and clinical data background_info, clinical_data = get_background_and_clinical_data(matrix_file_path) print("Background Information:") print(background_info) print("\nSample Characteristics:") # Get dictionary of unique values per row unique_values_dict = get_unique_values_by_row(clinical_data) for row, values in unique_values_dict.items(): print(f"\n{row}:") print(values) # 1. Gene Expression Data is_gene_available = True # Based on background info mentioning "Genome-wide expression profiling" # 2.1 Data Row Identification trait_row = 0 # Based on mutation status in characteristic dictionary age_row = None # Age not available gender_row = None # Gender not available # 2.2 Data Type Conversion Functions def convert_trait(value): if not isinstance(value, str): return None value = value.lower().split(': ')[-1] if value == 'patient': return 1 elif value == 'healthy': return 0 return None def convert_age(value): return None # Not used since age data unavailable def convert_gender(value): return None # Not used since gender data unavailable # 3. Save Metadata is_trait_available = trait_row is not None validate_and_save_cohort_info( is_final=False, cohort=cohort, info_path=json_path, is_gene_available=is_gene_available, is_trait_available=is_trait_available ) # 4. Clinical Feature Extraction if trait_row is not None: selected_clinical = geo_select_clinical_features( clinical_df=clinical_data, trait=trait, trait_row=trait_row, convert_trait=convert_trait ) # Preview data preview = preview_df(selected_clinical) print("Preview of clinical data:", preview) # Save to CSV selected_clinical.to_csv(out_clinical_data_file) # Get gene expression data from matrix file genetic_data = get_genetic_data(matrix_file_path) # Examine data structure print("Data structure and head:") print(genetic_data.head()) print("\nShape:", genetic_data.shape) print("\nFirst 20 row IDs (gene/probe identifiers):") print(list(genetic_data.index)[:20]) # Get a few column names to verify sample IDs print("\nFirst 5 column names:") print(list(genetic_data.columns)[:5]) # Looking at the identifiers "16650001", etc - these appear to be microarray probe IDs # rather than standard human gene symbols (which would be like "SNCA", "PINK1", etc) # Therefore we need to map these probe IDs to gene symbols requires_gene_mapping = True # First examine a portion of the SOFT file to locate gene annotation table with gzip.open(soft_file_path, 'rt') as f: in_table = False table_lines = [] for i, line in enumerate(f): if '!Platform_table_begin' in line: in_table = True table_lines.append(line) continue if in_table and '!Platform_table_end' in line: break if in_table: table_lines.append(line) if i < 5: # Print first few lines for context print(line.strip()) # Parse table content into dataframe table_content = ''.join(table_lines) gene_annotation = pd.read_csv(io.StringIO(table_content), sep='\t', comment='!') # Display column names and preview data print("\nColumn names:") print(gene_annotation.columns) print("\nPreview of gene annotation data:") print(preview_df(gene_annotation)) # Extract gene annotation data using the library function gene_annotation = get_gene_annotation(soft_file_path) # Load mapping between RefSeq accessions and gene symbols # Since we see NM_ and NR_ accessions, this data likely needs mapping through external resources # For now let's examine what columns and data we have print("Column names in gene annotation data:") print(gene_annotation.columns.tolist()) print("\nSample of gene annotation data:") print(gene_annotation.head().to_dict('records')) # Look at the distribution of GB_ACC patterns to understand what types of IDs we have gb_acc_patterns = gene_annotation['GB_ACC'].dropna().str.extract(r'(^[A-Z]{2}_)')[0].value_counts() print("\nDistribution of GB_ACC identifier types:") print(gb_acc_patterns) # Check total number of entries with GB_ACC total = len(gene_annotation) with_gb = gene_annotation['GB_ACC'].notna().sum() print(f"\nTotal entries: {total}") print(f"Entries with GB_ACC: {with_gb} ({(with_gb/total)*100:.1f}%)") # Use the working annotation extraction from Step 6 gene_annotation = get_gene_annotation(soft_file_path) # Create mapping dataframe focusing on ID and RefSeq accession mapping_df = gene_annotation[['ID', 'GB_ACC']].dropna() # Clean the RefSeq IDs and extract gene symbols def get_gene_from_refseq(acc): if pd.isna(acc): return None # Keep only NM (mRNA) and NR (RNA) entries, strip version numbers acc = acc.split('.')[0] if acc.startswith(('NM_', 'NR_')): return acc return None mapping_df['Gene'] = mapping_df['GB_ACC'].apply(get_gene_from_refseq) mapping_df = mapping_df[['ID', 'Gene']].dropna() # Apply mapping to convert probe data to gene expression data gene_data = apply_gene_mapping(genetic_data, mapping_df) print("Gene expression data shape after mapping:", gene_data.shape) if len(gene_data) > 0: print("\nFirst few gene symbols:", list(gene_data.index)[:10]) print("\nPreview of gene expression values:") print(gene_data.iloc[:5, :5]) # Reload clinical data that was processed earlier selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0) # First evaluate bias using the clinical data we have trait_biased, clinical_df_processed = judge_and_remove_biased_features(selected_clinical_df, trait) # Exit with appropriate metadata since gene mapping failed in previous step note = "Gene mapping failed - no valid gene expression data obtained." validate_and_save_cohort_info( is_final=True, cohort=cohort, info_path=json_path, is_gene_available=False, # Set to False since we couldn't get valid gene data is_trait_available=True, is_biased=trait_biased, df=clinical_df_processed, note=note ) # Do not save any output files since data processing failed