Liu-Hy's picture
Add files using upload-large-folder tool
a5a8278 verified
raw
history blame contribute delete
5.79 kB
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Bipolar_disorder"
cohort = "GSE45484"
# Input paths
in_trait_dir = "../DATA/GEO/Bipolar_disorder"
in_cohort_dir = "../DATA/GEO/Bipolar_disorder/GSE45484"
# Output paths
out_data_file = "./output/preprocess/1/Bipolar_disorder/GSE45484.csv"
out_gene_data_file = "./output/preprocess/1/Bipolar_disorder/gene_data/GSE45484.csv"
out_clinical_data_file = "./output/preprocess/1/Bipolar_disorder/clinical_data/GSE45484.csv"
json_path = "./output/preprocess/1/Bipolar_disorder/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 "Analysis of gene-expression changes..." we have gene expression data.
# 2. Variable Availability and Data Type Conversion
# From the sample characteristics, it appears all subjects have bipolar disorder (the trait),
# so there's no variation for "Bipolar_disorder". Hence trait_row = None.
trait_row = None
# For age, key=4 has many distinct values, so age is available.
age_row = 4
# For gender, key=3 has two distinct values: "sex: M" and "sex: F", so gender is available.
gender_row = 3
# Define conversion functions. They parse the string after the colon (':'), then convert.
def convert_trait(value: str):
# No variation in trait. This won't be used because trait_row=None, but we still define it.
return None
def convert_age(value: str):
# Example: "age: 46"
# Split on colon and strip spaces
parts = value.split(':')
if len(parts) < 2:
return None
try:
return float(parts[1].strip())
except ValueError:
return None
def convert_gender(value: str):
# Example: "sex: M" or "sex: F"
# Split on colon and strip spaces
parts = value.split(':')
if len(parts) < 2:
return None
gender_str = parts[1].strip().upper()
if gender_str == 'M':
return 1
elif gender_str == 'F':
return 0
return None
# 3. Save Metadata. Trait data is not available since trait_row=None.
is_trait_available = (trait_row is not None)
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
)
# 4. Clinical Feature Extraction
# We only proceed if trait_row is not None. Here, trait_row=None, so 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 the gene identifiers (e.g., ILMN_1651199), these are Illumina probe identifiers.
# Therefore, we require gene symbol mapping.
print("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 columns for probe IDs ("ID") and gene symbols ("Symbol") from the annotation data.
# 2. Get the gene mapping dataframe.
gene_mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')
# 3. Convert probe-level measurements to gene-level measurements.
gene_data = apply_gene_mapping(gene_data, gene_mapping_df)
# STEP7
import pandas as pd
# 1. Normalize gene symbols in the obtained gene data
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
normalized_gene_data.to_csv(out_gene_data_file)
# Since in Step 2 we determined that 'trait_row' is None, the trait is not actually available (all samples had the same trait).
# Therefore, there's no valid 'selected_clinical_df' to link. We must skip the processing steps that rely on trait information.
if trait_row is None:
# Final validation with trait unavailable. We still need to pass df and is_biased for is_final=True.
# Here, we consider the dataset "biased" for trait analysis, as it has no variation in the trait.
is_usable = validate_and_save_cohort_info(
is_final=True,
cohort=cohort,
info_path=json_path,
is_gene_available=True,
is_trait_available=False,
is_biased=True,
df=pd.DataFrame(), # Provide an empty DataFrame
note="No trait variation found; skipping clinical linking."
)
else:
# If trait were available, we would have proceeded with linking, missing value handling, bias checks, etc.
pass
# If the dataset were usable, we would save the final linked data.
# With no trait available, 'is_usable' should be False, so nothing further is done.