Liu-Hy's picture
Add files using upload-large-folder tool
b5650f0 verified
raw
history blame
6.09 kB
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Crohns_Disease"
cohort = "GSE207022"
# Input paths
in_trait_dir = "../DATA/GEO/Crohns_Disease"
in_cohort_dir = "../DATA/GEO/Crohns_Disease/GSE207022"
# Output paths
out_data_file = "./output/preprocess/1/Crohns_Disease/GSE207022.csv"
out_gene_data_file = "./output/preprocess/1/Crohns_Disease/gene_data/GSE207022.csv"
out_clinical_data_file = "./output/preprocess/1/Crohns_Disease/clinical_data/GSE207022.csv"
json_path = "./output/preprocess/1/Crohns_Disease/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) Determine if the dataset likely contains gene expression data
is_gene_available = True # Based on background info, it's a gene expression profiling study
# 2) Variable Availability and Data Type Conversion
# 2.1 Identify data availability for trait, age, gender
# Observing the sample characteristics dictionary, row 3 captures "diagnosis: Crohn's disease" or "diagnosis: healthy control".
# So this row includes different values for diagnosis, which can be used to represent our trait.
trait_row = 3
age_row = None # No age-related entry is present
gender_row = None # No gender-related entry is present
# 2.2 Defining data type and conversion functions
# Trait is binary: 1 for Crohn's disease, 0 for healthy control
def convert_trait(x: str) -> Optional[int]:
# Split by colon to isolate the value
parts = x.split(':', 1)
if len(parts) < 2:
return None
val = parts[1].strip().lower()
if 'crohn' in val:
return 1
elif 'healthy' in val:
return 0
return None
# Age and Gender not available, so we'll set those functions to None
convert_age = None
convert_gender = None
# 3) Conduct initial filtering and save metadata
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 (only if trait_row is not None)
if trait_row is not None:
# Assuming 'clinical_data' is the DataFrame we have from a previous step
selected_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 selected clinical data
preview_result = preview_df(selected_clinical_df, n=5)
print("Clinical Data Preview:", preview_result)
# Save the clinical data to CSV
selected_clinical_df.to_csv(out_clinical_data_file)
# 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])
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 which columns in 'gene_annotation' correspond to the gene expression IDs and the gene symbols.
# From the preview, the 'ID' column matches our gene expression data's index, and 'Gene Symbol' holds the gene symbols.
# 2) Obtain the gene mapping dataframe with the relevant columns
mapping_df = get_gene_mapping(gene_annotation, 'ID', 'Gene Symbol')
# 3) Apply the mapping to convert probe-level measurements into gene expression values
gene_data = apply_gene_mapping(gene_data, mapping_df)
# Print the shape of the mapped dataframe and a quick preview
print("Mapped gene expression data shape:", gene_data.shape)
print(gene_data.head())
import os
import pandas as pd
# 1) Normalize gene symbols in the obtained gene expression data
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
normalized_gene_data.to_csv(out_gene_data_file)
# 2) Link the clinical and genetic data (since trait data was indeed available at trait_row = 3)
selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
# 3) Handle missing values in the linked data
linked_data = handle_missing_values(linked_data, trait)
# 4) Check for biased features (including the trait)
trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)
# 5) Final validation and saving metadata
is_usable = validate_and_save_cohort_info(
is_final=True,
cohort=cohort,
info_path=json_path,
is_gene_available=True,
is_trait_available=True,
is_biased=trait_biased,
df=linked_data,
note="Data from GSE207022, trait is Crohn's disease."
)
# 6) If the dataset is usable, save the linked data
if is_usable:
linked_data.to_csv(out_data_file)