Liu-Hy's picture
Add files using upload-large-folder tool
dd19378 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "X-Linked_Lymphoproliferative_Syndrome"
cohort = "GSE180395"
# Input paths
in_trait_dir = "../DATA/GEO/X-Linked_Lymphoproliferative_Syndrome"
in_cohort_dir = "../DATA/GEO/X-Linked_Lymphoproliferative_Syndrome/GSE180395"
# Output paths
out_data_file = "./output/preprocess/3/X-Linked_Lymphoproliferative_Syndrome/GSE180395.csv"
out_gene_data_file = "./output/preprocess/3/X-Linked_Lymphoproliferative_Syndrome/gene_data/GSE180395.csv"
out_clinical_data_file = "./output/preprocess/3/X-Linked_Lymphoproliferative_Syndrome/clinical_data/GSE180395.csv"
json_path = "./output/preprocess/3/X-Linked_Lymphoproliferative_Syndrome/cohort_info.json"
# Get file paths
soft_file_path, matrix_file_path = geo_get_relevant_filepaths(in_cohort_dir)
# Get background info and clinical data
background_info, clinical_data = get_background_and_clinical_data(matrix_file_path)
# Print shape and first few rows to verify data
print("Background Information:")
print(background_info)
print("\nClinical Data Shape:", clinical_data.shape)
print("\nFirst few rows of Clinical Data:")
print(clinical_data.head())
print("\nSample Characteristics:")
# Get dictionary of unique values per row
unique_values_dict = get_unique_values_by_row(clinical_data)
for row, values in unique_values_dict.items():
print(f"\n{row}:")
print(values)
# 1. Gene Expression Data Availability
# This dataset contains kidney biopsy data, likely containing gene expression data
is_gene_available = True
# 2.1. Data Availability
# Looking at sample_group values under key 0, we can classify XLP status
trait_row = 0
# No age data available
age_row = None
# No gender data available
gender_row = None
# 2.2 Data Type Conversion Functions
def convert_trait(value):
if value is None or ':' not in value:
return None
group = value.split(': ')[1].lower()
# Since XLP often presents with lymphoproliferative conditions
if any(x in group for x in ['gn', 'glomerul', 'infiltration', 'lymph']):
return 1 # Disease manifestation
return 0 # Control/healthy
def convert_age(value):
# Not needed since age data unavailable
return None
def convert_gender(value):
# Not needed since gender data unavailable
return None
# 3. Save Initial Metadata
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=trait_row is not None
)
# 4. 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 the data
preview = preview_df(selected_clinical_df)
print("Preview of selected clinical features:", preview)
# Save to CSV
selected_clinical_df.to_csv(out_clinical_data_file)
# Extract gene expression data from matrix file
genetic_data = get_genetic_data(matrix_file_path)
# Print first 20 row IDs and shape of data
print("Shape of genetic data:", genetic_data.shape)
print("\nFirst 5 rows with sample columns:")
print(genetic_data.head())
print("\nFirst 20 gene/probe IDs:")
print(list(genetic_data.index[:20]))
# Print first few lines of raw matrix file to inspect format
print("\nFirst few lines of raw matrix file:")
with gzip.open(matrix_file_path, 'rt') as f:
for i, line in enumerate(f):
if i < 10: # Print first 10 lines
print(line.strip())
elif "!series_matrix_table_begin" in line:
print("\nFound table marker at line", i)
# Print next 3 lines after marker
for _ in range(3):
print(next(f).strip())
break
# Based on the "_at" suffix in the gene IDs and the fact these appear to be mouse array probes (e.g. "100009613_at"),
# these identifiers need to be mapped to standard gene symbols
requires_gene_mapping = True
# Extract gene annotation from SOFT file
gene_annotation = get_gene_annotation(soft_file_path)
# Print shape and column names for inspection
print("Gene annotation shape:", gene_annotation.shape)
print("\nColumn names:", list(gene_annotation.columns))
# Preview annotation structure
preview = preview_df(gene_annotation)
print("\nGene annotation preview:")
print(preview)
# The annotation contains Entrez IDs which we'll use as interim gene identifiers
gene_annotation.columns = ['ID', 'Gene']
# Create mapping dataframe - get_gene_mapping handles the column renaming and filtering
mapping_df = get_gene_mapping(gene_annotation, 'ID', 'Gene')
# Apply gene mapping using Entrez IDs as interim gene identifiers
gene_data = apply_gene_mapping(genetic_data, mapping_df)
# Print shape and preview mapped gene data
print("Gene expression data shape after mapping:", gene_data.shape)
print("\nPreview of mapped gene expression data:")
print(preview_df(gene_data))
# Create empty dataframe since gene mapping failed
gene_data = pd.DataFrame()
# 1. Save empty gene expression data
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
gene_data.to_csv(out_gene_data_file)
# 2. Save metadata about dataset being unusable
note = "Gene mapping failed as SOFT file only contains Entrez IDs without gene symbols. Additionally, this dataset appears to be a kidney biopsy expression study not focused on XLP."
is_usable = validate_and_save_cohort_info(
is_final=True,
cohort=cohort,
info_path=json_path,
is_gene_available=True, # Raw gene data exists but mapping failed
is_trait_available=False, # No XLP-specific data available
is_biased=False, # Set explicit value as required
df=gene_data, # Provide empty DataFrame
note=note
)
# Do not save linked data as it is not usable