Liu-Hy's picture
Add files using upload-large-folder tool
6f366b0 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Substance_Use_Disorder"
cohort = "GSE148375"
# Input paths
in_trait_dir = "../DATA/GEO/Substance_Use_Disorder"
in_cohort_dir = "../DATA/GEO/Substance_Use_Disorder/GSE148375"
# Output paths
out_data_file = "./output/preprocess/3/Substance_Use_Disorder/GSE148375.csv"
out_gene_data_file = "./output/preprocess/3/Substance_Use_Disorder/gene_data/GSE148375.csv"
out_clinical_data_file = "./output/preprocess/3/Substance_Use_Disorder/clinical_data/GSE148375.csv"
json_path = "./output/preprocess/3/Substance_Use_Disorder/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("Background Information:")
print(background_info)
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
# Yes, this is a Blood sample based GWAS study, so gene expression data should be available
is_gene_available = True
# 2.1 Data Availability
# smoking_status at row 6 can indicate Substance Use Disorder status
trait_row = 6
# age at row 1
age_row = 1
# gender at row 2
gender_row = 2
# 2.2 Data Type Conversion Functions
def convert_trait(x):
if pd.isna(x) or ':' not in x:
return None
val = x.split(':')[1].strip().lower()
# Convert smoking status to binary (1 for current smoker, 0 for non/ex-smoker)
if 'smoker' in val:
if val == 'smoker':
return 1
else: # 'non-smoker' or 'ex-smoker'
return 0
return None
def convert_age(x):
if pd.isna(x) or ':' not in x:
return None
try:
return float(x.split(':')[1].strip())
except:
return None
def convert_gender(x):
if pd.isna(x) or ':' not in x:
return None
val = x.split(':')[1].strip().lower()
if val == 'female':
return 0
elif val == 'male':
return 1
return None
# 3. Save metadata (initial filtering)
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
)
print("Preview of extracted clinical features:")
print(preview_df(selected_clinical_df))
# Save clinical data
selected_clinical_df.to_csv(out_clinical_data_file)
# Get gene expression data from matrix file - revised to handle line skipping properly
row_count = 0
with gzip.open(matrix_file_path, 'rt') as f:
for line in f:
if '!series_matrix_table_begin' in line:
break
row_count += 1
genetic_data = pd.read_csv(matrix_file_path, compression='gzip', skiprows=row_count+1, sep='\t',
index_col=0, comment='!')
# Remove the end marker row if present
genetic_data = genetic_data[~genetic_data.index.str.contains('!series_matrix_table_end', na=False)]
# Examine data structure
print("Data structure and head:")
print(genetic_data.head())
print("\nShape:", genetic_data.shape)
print("\nFirst 20 row IDs (gene/probe identifiers):")
print(list(genetic_data.index)[:20])
print("\nFirst 5 column names:")
print(list(genetic_data.columns)[:5])
# Revise gene availability after discovering this is an exome sequencing dataset, not gene expression
is_gene_available = False
# Save metadata with corrected gene availability status
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)
)