Liu-Hy's picture
Add files using upload-large-folder tool
b5650f0 verified
raw
history blame
6.16 kB
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Crohns_Disease"
cohort = "GSE186963"
# Input paths
in_trait_dir = "../DATA/GEO/Crohns_Disease"
in_cohort_dir = "../DATA/GEO/Crohns_Disease/GSE186963"
# Output paths
out_data_file = "./output/preprocess/1/Crohns_Disease/GSE186963.csv"
out_gene_data_file = "./output/preprocess/1/Crohns_Disease/gene_data/GSE186963.csv"
out_clinical_data_file = "./output/preprocess/1/Crohns_Disease/clinical_data/GSE186963.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 gene expression data availability
# Based on the series title ("Whole blood gene expression..."), we conclude it contains gene expression data.
is_gene_available = True
# 2. Variable availability:
# - The dictionary shows "disease: Crohn's disease" in row 0, but it's a constant single value ("Crohn's disease"),
# so there's no variation. Hence the trait is not really available for association analysis.
# - No mention of actual age or gender data in the dictionary, so both are unavailable.
trait_row = None
age_row = None
gender_row = None
# 2.2 Data Type Conversion Functions
# Although we found everything is unavailable, these functions must be defined.
def convert_trait(x: str) -> int:
"""
Example converter for a binary or numeric trait.
Since trait is not available, this function won't be used.
But we define the function to satisfy the requirement.
"""
# Typically we would parse something after the colon, but we won't use it here.
return None
def convert_age(x: str) -> float:
"""
Converts a string to a float representing age, or None if parsing fails.
Not used since age is unavailable.
"""
return None
def convert_gender(x: str) -> int:
"""
Converts gender to 0,1 or None if unknown.
Not used since gender is unavailable.
"""
return None
# 3. Initial filtering and metadata saving
# Trait data is considered unavailable because trait_row is 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
# Since trait_row is None, we 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])
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))
# 1. Identify the columns in gene_annotation that match the gene_data index and gene symbols
# - 'ID' contains values like "TC0100006437.hg.1" matching gene_data's index
# - 'SPOT_ID.1' contains text from which gene symbols can be parsed
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='SPOT_ID.1')
# 2. Convert probe-level measurements to gene expression data
gene_data = apply_gene_mapping(gene_data, mapping_df)
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) Check if a clinical data file actually exists
if os.path.exists(out_clinical_data_file):
# Trait data is considered available if the clinical file is present
is_trait_available = True
else:
is_trait_available = False
linked_data = None
trait_biased = None
# 3) If trait data is available, link and finalize
if is_trait_available:
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)
# Handle missing values
linked_data = handle_missing_values(linked_data, trait)
# Check for biased features
trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)
# Final validation with is_final=True
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="Trait data was declared unavailable earlier, but a clinical file was found."
)
# If usable, save linked data
if is_usable:
linked_data.to_csv(out_data_file)
# 4) Otherwise, if trait data is not available, do partial validation with is_final=False
else:
validate_and_save_cohort_info(
is_final=False,
cohort=cohort,
info_path=json_path,
is_gene_available=True,
is_trait_available=False,
note="Trait data file does not exist, so final validation is skipped."
)