Liu-Hy's picture
Add files using upload-large-folder tool
24fe0da verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Endometriosis"
cohort = "GSE111974"
# Input paths
in_trait_dir = "../DATA/GEO/Endometriosis"
in_cohort_dir = "../DATA/GEO/Endometriosis/GSE111974"
# Output paths
out_data_file = "./output/preprocess/1/Endometriosis/GSE111974.csv"
out_gene_data_file = "./output/preprocess/1/Endometriosis/gene_data/GSE111974.csv"
out_clinical_data_file = "./output/preprocess/1/Endometriosis/clinical_data/GSE111974.csv"
json_path = "./output/preprocess/1/Endometriosis/cohort_info.json"
# STEP 1: Initial Data Loading
# 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,
prefixes_a=background_prefixes,
prefixes_b=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("\nSample Characteristics Dictionary:")
print(sample_characteristics_dict)
import pandas as pd
import os
import json
from typing import Optional, Callable, Dict, Any
# 1. Check gene expression data availability
# Based on the series summary and overall design, this appears to be an RNA expression dataset.
# So we set:
is_gene_available = True
# 2. Identify row indices for trait, age, and gender from the sample characteristics dictionary
# The sample characteristics dictionary only has key 0 with "tissue: Endometrial tissue",
# which is a constant value. There is no mention of "endometriosis" or any variation indicating
# presence/absence of endometriosis. Therefore, trait data is unavailable.
trait_row = None
# Similarly, there is no mention of "age" or "gender" in the dictionary, so:
age_row = None
gender_row = None
# 2.2 Define the conversion functions. Even though they won't be used (due to None rows),
# we still define them to adhere to instructions.
def convert_trait(x: str) -> Optional[int]:
"""
Convert the string to indicate presence/absence of endometriosis in binary form.
Since the dataset does not contain trait info, return None.
"""
return None
def convert_age(x: str) -> Optional[float]:
"""
Convert the string to a float representing age.
Since the dataset does not contain this info, return None.
"""
return None
def convert_gender(x: str) -> Optional[int]:
"""
Convert gender to binary (female=0, male=1).
Since the dataset does not contain this info, return None.
"""
return None
# 3. Initial filtering and saving metadata
# Trait availability is determined by whether trait_row is None:
is_trait_available = (trait_row is not None)
# We call validate_and_save_cohort_info with is_final=False for initial filtering.
is_usable = 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,
note="Initial filtering for GSE111974"
)
# 4. Clinical Feature Extraction
# Since trait_row is None, we skip extracting clinical features.
# 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])
# Observing the gene identifiers, they appear to be microarray probe IDs (likely from Agilent arrays),
# not standard human gene symbols, hence they require mapping.
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. From the annotation preview, the probe identifiers are in the 'ID' column,
# and the gene symbols are in the 'GENE_SYMBOL' column.
# 2. Create the gene mapping dataframe
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')
# 3. Convert probe-level measurements to gene-level expression
gene_data = apply_gene_mapping(gene_data, mapping_df)
# STEP 7
import pandas as pd
# 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)
# Since no trait data is available (trait_row=None in previous steps),
# we must skip linking, missing-value handling, and bias checks that require trait data.
# 2. Record that we have gene data but not trait data. We cannot perform final validation
# for a trait-based study, so we do another "initial" metadata save (is_final=False).
is_usable = validate_and_save_cohort_info(
is_final=False,
cohort=cohort,
info_path=json_path,
is_gene_available=True, # We do have gene expression
is_trait_available=False, # No trait data
note="No trait data; only gene data processed and saved."
)
# 3. Since the dataset is not usable for trait-based analysis, do not produce a fully linked CSV.
if is_usable:
print("Unexpectedly marked usable, despite lacking trait data.")
else:
print("No trait data available. Dataset is not usable for trait-based analysis. No final data saved.")