Liu-Hy's picture
Add files using upload-large-folder tool
24fe0da verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Endometriosis"
cohort = "GSE37837"
# Input paths
in_trait_dir = "../DATA/GEO/Endometriosis"
in_cohort_dir = "../DATA/GEO/Endometriosis/GSE37837"
# Output paths
out_data_file = "./output/preprocess/1/Endometriosis/GSE37837.csv"
out_gene_data_file = "./output/preprocess/1/Endometriosis/gene_data/GSE37837.csv"
out_clinical_data_file = "./output/preprocess/1/Endometriosis/clinical_data/GSE37837.csv"
json_path = "./output/preprocess/1/Endometriosis/cohort_info.json"
# STEP1
from tools.preprocess import *
# 1. Identify the paths to the SOFT file and the matrix file
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
# 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
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
# 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)
# 1. Gene Expression Data Availability
is_gene_available = True # Based on the series description indicating it is a "whole genome expression" dataset.
# 2. Variable Availability and Data Type Conversion
# 2.1 Identify keys and check uniqueness
# The entire cohort is diagnosed with endometriosis (no separate controls),
# so the trait does not vary within this dataset. Therefore, trait_row = None.
trait_row = None
# Age data is found in key 0 with multiple distinct values.
age_row = 0
# Gender data is found in key 1, but it has only one unique value ("female").
# Hence, it does not vary, so gender_row = None.
gender_row = None
# 2.2 Define conversion functions for these variables.
def convert_trait(value: str):
"""
Since trait_row is None (no variation), this function is unused.
Provide a placeholder for completeness.
"""
return None
def convert_age(value: str):
"""
Extracts the numeric part after the colon for age data and converts to float.
Returns None if parsing fails.
Example input: "age (y): 29"
"""
try:
after_colon = value.split(':', 1)[1].strip()
return float(after_colon)
except:
return None
def convert_gender(value: str):
"""
Since gender is not varying, this function is unused. Provide a placeholder.
"""
return None
# 3. Save Metadata (initial filtering)
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
# Since trait_row is None, we skip this step.
# STEP3
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
gene_data = get_genetic_data(matrix_file)
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
print(gene_data.index[:20])
# Based on observation, these are not standard human gene symbols.
# Therefore, gene mapping is required.
requires_gene_mapping = True
# STEP5
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
gene_annotation = get_gene_annotation(soft_file)
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
print("Gene annotation preview:")
print(preview_df(gene_annotation))
# STEP: Gene Identifier Mapping
# 1. Identify the appropriate columns for probe ID and gene symbol:
# From the annotation preview, the "ID" column matches the gene expression data index,
# and "GENE_SYMBOL" holds the actual gene symbols.
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')
# 2. Convert probe-level measurements to gene-level expressions
gene_data = apply_gene_mapping(gene_data, mapping_df)
# (Optional) Inspect the resulting gene_data
print("Mapped gene_data shape:", gene_data.shape)
print(gene_data.head())
import os
import pandas as pd
# STEP 7
# 1. Normalize the gene expression data to standard gene symbols.
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
normalized_gene_data.to_csv(out_gene_data_file)
print("Normalized gene expression data saved to:", out_gene_data_file)
# Because trait_row was None in a previous step, there's no trait variation (everyone has the same trait value).
# We'll finalize the dataset as unusable due to lack of trait variation,
# but still must provide df and is_biased to comply with the final validation requirements.
empty_df = pd.DataFrame() # Minimal placeholder DataFrame
is_biased = True # Arbitrarily set to True; actual value won't matter if trait is unavailable
# Perform final validation with no trait data
is_usable = validate_and_save_cohort_info(
is_final=True,
cohort=cohort,
info_path=json_path,
is_gene_available=True, # We do have gene expression
is_trait_available=False, # But there's no valid trait variation
is_biased=is_biased,
df=empty_df,
note="No trait variation => dataset not usable for trait-based analysis."
)
print("Dataset finalized but is not usable due to lack of trait data; no final data saved.")