File size: 2,401 Bytes
933cd71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
# Path Configuration
from tools.preprocess import *

# Processing context
trait = "Arrhythmia"

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

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


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

# Discover 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))]

# Attempt to find a cohort relevant to Arrhythmia (cardiac rhythm disorders)
keywords_specific = [
    'arrhythmia', 'atrial_fibrillation', 'brugada', 'long_qt', 'ventricular_tachycardia',
    'supraventricular', 'cardiac_conduction', 'torsades', 'wolff', 'wolff-parkinson-white'
]
keywords_general = ['cardiac', 'cardio', 'heart', 'myocard']

def find_best_cohort(subdirs, specific_kw, general_kw):
    scored = []
    for sd in subdirs:
        sdl = sd.lower()
        score = 0
        if any(k in sdl for k in specific_kw):
            score += 2
        if any(k in sdl for k in general_kw):
            score += 1
        if score > 0:
            scored.append((score, sd))
    if not scored:
        return None
    scored.sort(reverse=True)  # highest score first
    return scored[0][1]

selected_subdir = find_best_cohort(available_subdirs, keywords_specific, keywords_general)

if selected_subdir is None:
    print(f"No suitable TCGA cohort directory found for trait '{trait}'. Skipping this trait.")
    # Record metadata for skipping
    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 files
    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(f"Selected cohort directory: {selected_subdir}")
    print("Clinical data columns:")
    print(list(clinical_df.columns))