Liu-Hy's picture
Add files using upload-large-folder tool
3088323 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Cervical_Cancer"
cohort = "GSE138079"
# Input paths
in_trait_dir = "../DATA/GEO/Cervical_Cancer"
in_cohort_dir = "../DATA/GEO/Cervical_Cancer/GSE138079"
# Output paths
out_data_file = "./output/preprocess/1/Cervical_Cancer/GSE138079.csv"
out_gene_data_file = "./output/preprocess/1/Cervical_Cancer/gene_data/GSE138079.csv"
out_clinical_data_file = "./output/preprocess/1/Cervical_Cancer/clinical_data/GSE138079.csv"
json_path = "./output/preprocess/1/Cervical_Cancer/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)
import pandas as pd
from typing import Optional, Callable, Any, Dict
# 1. Gene expression data availability
is_gene_available = True # This dataset is labeled as mRNA expression data.
# 2. Variable Availability and Data Type Conversion
# After examining the sample characteristics, no rows match the human Cervical_Cancer trait,
# and there is no age or gender info. Hence, all three rows are None.
trait_row = None
age_row = None
gender_row = None
# Define conversion functions. Even though data is unavailable, we still need these
# as placeholders. A typical approach is to parse the string after “:” if present,
# but we return None to indicate no valid data.
def convert_trait(value: str) -> Optional[float]:
return None
def convert_age(value: str) -> Optional[float]:
return None
def convert_gender(value: str) -> Optional[int]:
return None
# 3. Save Metadata (Initial Filtering)
# Trait is not available, so is_trait_available is False.
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 clinical feature extraction.
if trait_row is not None:
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 = preview_df(selected_clinical_df)
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])
# Based on the index values shown (e.g., '12', '13', '14'), these are not recognized human gene symbols.
# They appear more like numeric or probe identifiers. Therefore, gene symbol mapping is needed.
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))
# STEP6: Gene Identifier Mapping
# 1. Decide which columns store the probe identifiers and the gene symbols.
# From the annotation preview, "ID" appears to match the probe identifiers in gene_data,
# and "GENE_SYMBOL" is likely the column for gene symbols.
probe_col = "ID"
gene_symbol_col = "GENE_SYMBOL"
# 2. Get the gene mapping dataframe by extracting the relevant columns.
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=gene_symbol_col)
# 3. Convert probe-level measurements to gene-level expression data.
gene_data = apply_gene_mapping(gene_data, mapping_df)
# STEP 7
# Before proceeding, check if trait data is actually available from previous steps.
# If not, we cannot link clinical and genetic data, so we skip those steps.
# We will still normalize gene symbols, then record the dataset status appropriately.
# 1. Normalize gene symbols in the gene_data, then save to CSV.
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
normalized_gene_data.to_csv(out_gene_data_file)
# 2. If trait data is not available, perform initial (non-final) validation, then skip linking & QC steps.
if not is_trait_available:
is_usable = validate_and_save_cohort_info(
is_final=False,
cohort=cohort,
info_path=json_path,
is_gene_available=True, # We do have gene data
is_trait_available=False, # No trait data was found
note="No trait data; skipping linking and QC steps."
)
# Since the dataset isn't usable without trait data, do not proceed further.
else:
# If trait data is available, proceed with linking and QC steps.
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
linked_data = handle_missing_values(linked_data, trait)
trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)
# 3. Final validation (since trait data is present).
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 is available. Completed linking and QC steps."
)
# 4. If the dataset is usable, save the final linked data.
if is_usable:
linked_data.to_csv(out_data_file)