Liu-Hy's picture
Add files using upload-large-folder tool
e6817b9 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Height"
cohort = "GSE97475"
# Input paths
in_trait_dir = "../DATA/GEO/Height"
in_cohort_dir = "../DATA/GEO/Height/GSE97475"
# Output paths
out_data_file = "./output/preprocess/3/Height/GSE97475.csv"
out_gene_data_file = "./output/preprocess/3/Height/gene_data/GSE97475.csv"
out_clinical_data_file = "./output/preprocess/3/Height/clinical_data/GSE97475.csv"
json_path = "./output/preprocess/3/Height/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)
# Get unique values for each clinical feature
unique_values_dict = get_unique_values_by_row(clinical_data)
# Print background information
print("Background Information:")
print(background_info)
print("\nSample Characteristics:")
print(json.dumps(unique_values_dict, indent=2))
# 1. Gene Expression Data Availability
# From title and background information, this appears to be a microarray study of blood PBMCs
# Not pure miRNA or methylation data
is_gene_available = True
# 2. Variable Availability and Data Type Conversion
# Height data is explicitly recorded in row 5
trait_row = 5
# Age data is recorded in row 81
age_row = 81
# Gender data can be inferred from row 118
gender_row = 118
def convert_trait(x):
# Extract height value after colon
if pd.isna(x) or ':' not in x:
return None
height = x.split(': ')[1]
try:
# Convert to float for continuous data
return float(height)
except:
if height == 'NA':
return None
return None
def convert_age(x):
if pd.isna(x) or ':' not in x:
return None
age = x.split(': ')[1]
try:
# Convert to float for continuous data
return float(age)
except:
if age == 'NA':
return None
return None
def convert_gender(x):
if pd.isna(x) or ':' not in x:
return None
gender = x.split(': ')[1]
# Convert to binary (0=female, 1=male)
if gender.lower() == 'female':
return 0
elif gender.lower() == 'male':
return 1
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=(trait_row is not None))
# 4. Clinical feature extraction
if trait_row is not None:
clinical_features_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 extracted data
print("Preview of extracted clinical features:")
print(preview_df(clinical_features_df))
# Save to CSV
clinical_features_df.to_csv(out_clinical_data_file)
# Extract gene expression data from the matrix file
genetic_data = get_genetic_data(matrix_file_path)
# Print information about the data structure
print("First few rows of the genetic data:")
print(genetic_data.head())
print("\nShape of genetic data:", genetic_data.shape)
print("\nColumn names:", genetic_data.columns.tolist())
# Looking at the gene identifiers in the index (A1BG, A26C3, A2LD1, etc.)
# These appear to be standard HGNC gene symbols, so no mapping is needed
requires_gene_mapping = False
# Get gene expression data from matrix file
gene_data = get_genetic_data(matrix_file_path)
is_gene_available = len(gene_data.columns) > 1
# Load clinical data
clinical_data = pd.read_csv(out_clinical_data_file, index_col=0)
# Check if there are any non-NaN height values
is_trait_available = not clinical_data.loc[trait].isna().all()
# 1. Normalize gene symbols
gene_data = normalize_gene_symbols_in_index(gene_data)
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
gene_data.to_csv(out_gene_data_file)
# 2-4. Skip data linking and processing if trait is not available
linked_data = pd.DataFrame()
is_biased = True
if is_trait_available:
linked_data = geo_link_clinical_genetic_data(clinical_data, gene_data)
linked_data = handle_missing_values(linked_data, trait)
is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)
# 5. Final validation and save metadata
note = "This dataset contains valid gene expression data and demographic information (age and gender), but all height measurements are missing."
is_usable = validate_and_save_cohort_info(
is_final=True,
cohort=cohort,
info_path=json_path,
is_gene_available=is_gene_available,
is_trait_available=is_trait_available,
is_biased=is_biased,
df=linked_data,
note=note
)
# 6. Save the linked data only if it's usable
if is_usable:
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
linked_data.to_csv(out_data_file)