Liu-Hy's picture
Add files using upload-large-folder tool
c78bff9 verified
Raw
History Blame Contribute Delete
2.61 kB
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Allergies"
# Input paths
tcga_root_dir = "../DATA/TCGA"
# Output paths
out_data_file = "./output/z1/preprocess/Allergies/TCGA.csv"
out_gene_data_file = "./output/z1/preprocess/Allergies/gene_data/TCGA.csv"
out_clinical_data_file = "./output/z1/preprocess/Allergies/clinical_data/TCGA.csv"
json_path = "./output/z1/preprocess/Allergies/cohort_info.json"
# Step 1: Initial Data Loading
import os
import pandas as pd
# Step 1: Identify the most appropriate TCGA subdirectory for the trait "Allergies"
# Since TCGA cohorts are cancer types and none relate to allergies, we attempt a keyword search.
keywords = [
"allerg", "hypersens", "atopy", "atopic", "asthma", "urticaria", "rhinitis", "eczema", "hayfever", "hay_fever"
]
# List available TCGA subdirectories
available_subdirs = [d for d in os.listdir(tcga_root_dir) if os.path.isdir(os.path.join(tcga_root_dir, d))]
# Find candidates whose names contain any allergy-related keyword
candidates = [d for d in available_subdirs if any(k in d.lower() for k in keywords)]
selected_dir = None
if len(candidates) > 0:
# If multiple matches, choose the one with the longest keyword overlap (more specific)
def score_dir(name: str) -> int:
lname = name.lower()
return sum(lname.count(k) for k in keywords)
candidates.sort(key=score_dir, reverse=True)
selected_dir = candidates[0]
# If no suitable directory found, mark as skipped and stop here
if selected_dir is None:
print("No TCGA cohort directory matches the target trait 'Allergies'. Skipping this trait.")
# Record as unavailable for this trait
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
else:
print(f"Selected TCGA cohort directory: {selected_dir}")
cohort_dir = os.path.join(tcga_root_dir, selected_dir)
# Step 2: Identify clinical and genetic file paths
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)
print(f"Clinical file: {clinical_file_path}")
print(f"Genetic file: {genetic_file_path}")
# Step 3: Load both files
clinical_df = pd.read_csv(clinical_file_path, sep="\t", index_col=0, low_memory=False)
genetic_df = pd.read_csv(genetic_file_path, sep="\t", index_col=0, low_memory=False)
# Step 4: Print column names of the clinical data
print("Clinical data columns:")
print(list(clinical_df.columns))