File size: 2,148 Bytes
56598a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Path Configuration
from tools.preprocess import *

# Processing context
trait = "Bipolar_disorder"

# Input paths
tcga_root_dir = "../DATA/TCGA"

# Output paths
out_data_file = "./output/z1/preprocess/Bipolar_disorder/TCGA.csv"
out_gene_data_file = "./output/z1/preprocess/Bipolar_disorder/gene_data/TCGA.csv"
out_clinical_data_file = "./output/z1/preprocess/Bipolar_disorder/clinical_data/TCGA.csv"
json_path = "./output/z1/preprocess/Bipolar_disorder/cohort_info.json"


# Step 1: Initial Data Loading
import os
import pandas as pd

# Initialize placeholders for downstream steps
selected_subdir = None
clinical_df = None
genetic_df = None

# Discover subdirectories
try:
    subdirs = [d for d in os.listdir(tcga_root_dir) if os.path.isdir(os.path.join(tcga_root_dir, d))]
except Exception as e:
    subdirs = []
    print(f"ERROR: Unable to list TCGA root directory '{tcga_root_dir}': {e}")

# Attempt to find a TCGA cohort relevant to Bipolar disorder (unlikely within TCGA cancer cohorts)
trait_keywords = {
    "bipolar", "bipolar_disorder", "bipolar disorder", "mania", "manic"
}
candidates = []
for d in subdirs:
    name_l = d.lower()
    if any(k in name_l for k in trait_keywords):
        candidates.append(d)

# Select the most specific match if any (longest name heuristic)
if candidates:
    selected_subdir = sorted(candidates, key=len, reverse=True)[0]

if selected_subdir is None:
    print(f"No suitable TCGA cohort found for trait '{trait}'. Skipping this trait.")
    # Record that this trait is not applicable for TCGA
    validate_and_save_cohort_info(
        is_final=False,
        cohort="TCGA",
        info_path=json_path,
        is_gene_available=False,
        is_trait_available=False
    )
else:
    cohort_dir = os.path.join(tcga_root_dir, selected_subdir)
    clinical_path, genetic_path = tcga_get_relevant_filepaths(cohort_dir)

    # Load dataframes
    clinical_df = pd.read_csv(clinical_path, sep='\t', index_col=0, low_memory=False)
    genetic_df = pd.read_csv(genetic_path, sep='\t', index_col=0, low_memory=False)

    # Print clinical column names for inspection
    print(list(clinical_df.columns))