Liu-Hy's picture
Add files using upload-large-folder tool
ee5a411 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Head_and_Neck_Cancer"
cohort = "GSE244580"
# Input paths
in_trait_dir = "../DATA/GEO/Head_and_Neck_Cancer"
in_cohort_dir = "../DATA/GEO/Head_and_Neck_Cancer/GSE244580"
# Output paths
out_data_file = "./output/preprocess/3/Head_and_Neck_Cancer/GSE244580.csv"
out_gene_data_file = "./output/preprocess/3/Head_and_Neck_Cancer/gene_data/GSE244580.csv"
out_clinical_data_file = "./output/preprocess/3/Head_and_Neck_Cancer/clinical_data/GSE244580.csv"
json_path = "./output/preprocess/3/Head_and_Neck_Cancer/cohort_info.json"
# Get relevant file paths
soft_file_path, matrix_file_path = geo_get_relevant_filepaths(in_cohort_dir)
# Extract background info and clinical data from the matrix file
background_info, clinical_data = get_background_and_clinical_data(matrix_file_path)
# Get dictionary of unique values per row in clinical data
unique_values_dict = get_unique_values_by_row(clinical_data)
# Print background info
print("Background Information:")
print("-" * 50)
print(background_info)
print("\n")
# Print clinical data unique values
print("Sample Characteristics:")
print("-" * 50)
for row, values in unique_values_dict.items():
print(f"{row}:")
print(f" {values}")
print()
# 1. Gene Expression Data Availability
# Based on background information, this dataset contains gene expression data from microarray analysis
is_gene_available = True
# 2.1 Data Availability
# From sample characteristics, we can find trait information (disease state) in row 0
# Age and gender information are not available
trait_row = 0
age_row = None
gender_row = None
# 2.2 Data Type Conversion Functions
def convert_trait(value: str) -> int:
"""Convert disease state to binary:
0 for chronic tonsillitis (control)
1 for peritumoral tissue/lymph node (cancer-related)"""
if not value or ':' not in value:
return None
value = value.split(':')[1].strip().lower()
if 'chronic tonsillitis' in value:
return 0
elif 'peritumoral' in value or 'lymph node' in value:
return 1
return None
# Age conversion function not needed since age data unavailable
convert_age = None
# Gender conversion function not needed since gender data unavailable
convert_gender = None
# 3. Save initial metadata
initial_validation = validate_and_save_cohort_info(
is_final=False,
cohort=cohort,
info_path=json_path,
is_gene_available=is_gene_available,
is_trait_available=(trait_row is not None)
)
# 4. Extract clinical features since trait_row is not None
clinical_df = geo_select_clinical_features(
clinical_df=clinical_data,
trait=trait,
trait_row=trait_row,
convert_trait=convert_trait,
age_row=age_row,
convert_age=convert_age,
gender_row=gender_row,
convert_gender=convert_gender
)
# Preview the processed clinical data
print("Preview of processed clinical data:")
print(preview_df(clinical_df))
# Save clinical data
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
clinical_df.to_csv(out_clinical_data_file)
# Extract gene expression data
genetic_data = get_genetic_data(matrix_file_path)
# Print first 20 probe IDs
print("First 20 probe IDs:")
print(genetic_data.index[:20])
# The identifiers appear to be probe IDs from a microarray platform rather than human gene symbols
# They are numeric identifiers in a specific format (8 digits starting with 1665)
# These will need to be mapped to standard gene symbols for analysis
requires_gene_mapping = True
# Extract gene annotation from SOFT file
gene_annotation = get_gene_annotation(soft_file_path)
# Preview column names and first few values
preview_dict = preview_df(gene_annotation)
print("Column names and preview values:")
for col, values in preview_dict.items():
print(f"\n{col}:")
print(values)
# From examining Step 5's output, we see probe IDs in 'ID' column
# and gene identifiers in 'GB_ACC' column from gene_annotation
gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GB_ACC')
# Apply gene mapping to convert probe-level data to gene expression data
gene_data = apply_gene_mapping(genetic_data, gene_mapping)
# Print row and column counts to verify data dimensions
print("\nGene expression data shape after mapping:")
print(f"Number of genes: {len(gene_data.index)}")
print(f"Number of samples: {len(gene_data.columns)}")
# Preview the mapped gene data
print("\nPreview of mapped gene expression data:")
print(gene_data.head())
# Save gene expression data
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
gene_data.to_csv(out_gene_data_file)
# Since gene mapping failed, we need to go back and check the gene annotation columns
print("\nAvailable columns in gene_annotation:")
for col in gene_annotation.columns:
print(f"{col}:")
print(gene_annotation[col].head())
print()
# Get gene mapping using SPOT_ID which appears to contain gene location info
gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='SPOT_ID')
# Apply gene mapping to convert probe-level data to gene expression data
gene_data = apply_gene_mapping(genetic_data, gene_mapping)
# Normalize gene symbols and save normalized gene data
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
normalized_gene_data.to_csv(out_gene_data_file)
# Read the processed clinical data file
clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)
# Since gene mapping failed (no genes extracted), we should not proceed
is_usable = validate_and_save_cohort_info(
is_final=False, # Changed to False since processing failed
cohort=cohort,
info_path=json_path,
is_gene_available=False, # Changed to False since mapping failed
is_trait_available=True,
note="Gene mapping failed - proper gene symbols not available in annotation data"
)
print(f"Dataset {cohort} preprocessing failed due to inadequate gene annotation data.")