Liu-Hy's picture
Add files using upload-large-folder tool
933cd71 verified
Raw
History Blame Contribute Delete
2.15 kB
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Anorexia_Nervosa"
# Input paths
tcga_root_dir = "../DATA/TCGA"
# Output paths
out_data_file = "./output/z1/preprocess/Anorexia_Nervosa/TCGA.csv"
out_gene_data_file = "./output/z1/preprocess/Anorexia_Nervosa/gene_data/TCGA.csv"
out_clinical_data_file = "./output/z1/preprocess/Anorexia_Nervosa/clinical_data/TCGA.csv"
json_path = "./output/z1/preprocess/Anorexia_Nervosa/cohort_info.json"
# Step 1: Initial Data Loading
import os
import pandas as pd
# Discover available TCGA cohort directories
all_entries = os.listdir(tcga_root_dir)
cohort_dirs = [d for d in all_entries if os.path.isdir(os.path.join(tcga_root_dir, d))]
# Define keywords related to the trait to find a relevant cohort (none expected for Anorexia Nervosa in TCGA)
trait_keywords = {
"anorexia", "nervosa", "eating", "appetite", "weight", "body_mass", "bmi", "cachexia"
}
# Score directories by presence of any keyword
def score_dir(name: str) -> int:
lname = name.lower()
return sum(1 for kw in trait_keywords if kw in lname)
scored = [(d, score_dir(d)) for d in cohort_dirs]
# Select the best match if any positive score
scored.sort(key=lambda x: x[1], reverse=True)
selected_dir = scored[0][0] if scored and scored[0][1] > 0 else None
if selected_dir is None:
# No suitable TCGA cohort for Anorexia Nervosa; mark as unavailable and complete this task
_ = validate_and_save_cohort_info(
is_final=False,
cohort="TCGA",
info_path=json_path,
is_gene_available=False,
is_trait_available=False
)
clinical_df = None
genetic_df = None
print("No suitable TCGA cohort found for the trait; skipping TCGA for this trait.")
else:
cohort_path = os.path.join(tcga_root_dir, selected_dir)
clinical_fp, genetic_fp = tcga_get_relevant_filepaths(cohort_path)
# Load dataframes
clinical_df = pd.read_csv(clinical_fp, sep='\t', index_col=0, low_memory=False)
genetic_df = pd.read_csv(genetic_fp, sep='\t', index_col=0, low_memory=False)
# Print clinical column names
print(clinical_df.columns.tolist())