Liu-Hy's picture
Add files using upload-large-folder tool
72233eb verified
Raw
History Blame Contribute Delete
5.5 kB
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Adrenocortical_Cancer"
cohort = "GSE68950"
# Input paths
in_trait_dir = "../DATA/GEO/Adrenocortical_Cancer"
in_cohort_dir = "../DATA/GEO/Adrenocortical_Cancer/GSE68950"
# Output paths
out_data_file = "./output/z1/preprocess/Adrenocortical_Cancer/GSE68950.csv"
out_gene_data_file = "./output/z1/preprocess/Adrenocortical_Cancer/gene_data/GSE68950.csv"
out_clinical_data_file = "./output/z1/preprocess/Adrenocortical_Cancer/clinical_data/GSE68950.csv"
json_path = "./output/z1/preprocess/Adrenocortical_Cancer/cohort_info.json"
# Step 1: Initial Data Loading
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)
# Step 2: Dataset Analysis and Clinical Feature Extraction
import os
import math
import pandas as pd
# 1) Determine gene expression availability (Affymetrix HT_HG-U133A, gene expression)
is_gene_available = True
# Candidate rows from sample characteristics dictionary
# 0: cosmic id
# 1: disease state -> candidate for trait (binary: Adrenocortical Cancer vs others)
# 2: disease location
# 3: organism part
# 4: sample
# 5: cell line code
# 6: supplier
# 7: affy_batch
# 8: crna plate
trait_row = 1
age_row = None # Cell line compendium; no human subject age
gender_row = None # Cell line compendium; no human subject gender
# Conversion helpers
def _post_colon(value):
if value is None:
return None
s = str(value)
if ':' in s:
s = s.split(':', 1)[1]
s = s.strip()
if s == '':
return None
return s
def convert_trait(v):
s = _post_colon(v)
if s is None:
return None
t = s.lower()
if t in {'na', 'n/a', '#n/a', 'unknown'}:
return None
# Positive mapping for Adrenocortical_Cancer
# Capture common phrasings
if ('adrenocortical' in t and 'carcin' in t) \
or (('adrenal' in t) and ('cortical' in t) and ('carcin' in t)) \
or (('adrenal' in t) and ('cortex' in t) and ('carcin' in t)) \
or ('adrenal cortical carcinoma' in t) \
or ('adrenocortical carcinoma' in t) \
or ('adrenal cortex carcinoma' in t):
return 1
return 0
def convert_age(v):
s = _post_colon(v)
if s is None:
return None
t = s.lower().replace('years', '').replace('year', '').replace('yrs', '').replace('yr', '').strip()
try:
val = float(t)
if math.isnan(val):
return None
return val
except Exception:
return None
def convert_gender(v):
s = _post_colon(v)
if s is None:
return None
t = s.strip().lower()
if t in {'female', 'f', 'woman', 'women', 'girl'}:
return 0
if t in {'male', 'm', 'man', 'men', 'boy'}:
return 1
return None
# 2) Determine if the trait is actually available (non-constant) in this cohort
is_trait_available = False
if trait_row is not None:
# Map the candidate trait row values to 0/1/None and check variability
try:
mapped = clinical_data.loc[trait_row].apply(convert_trait)
unique_vals = set([x for x in mapped if x is not None])
# Trait must have at least two classes to be usable
if unique_vals == {0, 1}:
is_trait_available = True
else:
# All 0s (no ACC) or all 1s or only None -> treat as unavailable
trait_row = None
except Exception:
# If anything goes wrong accessing the row, treat as unavailable
trait_row = None
# 3) Save metadata (initial filtering)
# Note: This is a cancer cell line compendium; age and gender are not provided.
_ = 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 is available and non-constant)
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)
print("Preview of selected clinical features:", preview)
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
selected_clinical_df.to_csv(out_clinical_data_file)
else:
print("INFO: Trait is not available for association analysis in this cohort (constant or absent). Skipping clinical feature extraction. This is a cell line compendium without age or gender.")