File size: 4,453 Bytes
ee94703
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Path Configuration
from tools.preprocess import *

# Processing context
trait = "Adrenocortical_Cancer"

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

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

import os
import pandas as pd

# 1. Identify the relevant subdirectory for the trait "Obesity"
subdirectories = [
    'CrawlData.ipynb', '.DS_Store', 'TCGA_lower_grade_glioma_and_glioblastoma_(GBMLGG)',
    'TCGA_Uterine_Carcinosarcoma_(UCS)', 'TCGA_Thyroid_Cancer_(THCA)', 'TCGA_Thymoma_(THYM)',
    'TCGA_Testicular_Cancer_(TGCT)', 'TCGA_Stomach_Cancer_(STAD)', 'TCGA_Sarcoma_(SARC)',
    'TCGA_Rectal_Cancer_(READ)', 'TCGA_Prostate_Cancer_(PRAD)', 'TCGA_Pheochromocytoma_Paraganglioma_(PCPG)',
    'TCGA_Pancreatic_Cancer_(PAAD)', 'TCGA_Ovarian_Cancer_(OV)', 'TCGA_Ocular_melanomas_(UVM)',
    'TCGA_Mesothelioma_(MESO)', 'TCGA_Melanoma_(SKCM)', 'TCGA_Lung_Squamous_Cell_Carcinoma_(LUSC)',
    'TCGA_Lung_Cancer_(LUNG)', 'TCGA_Lung_Adenocarcinoma_(LUAD)', 'TCGA_Lower_Grade_Glioma_(LGG)',
    'TCGA_Liver_Cancer_(LIHC)', 'TCGA_Large_Bcell_Lymphoma_(DLBC)', 'TCGA_Kidney_Papillary_Cell_Carcinoma_(KIRP)',
    'TCGA_Kidney_Clear_Cell_Carcinoma_(KIRC)', 'TCGA_Kidney_Chromophobe_(KICH)', 'TCGA_Head_and_Neck_Cancer_(HNSC)',
    'TCGA_Glioblastoma_(GBM)', 'TCGA_Esophageal_Cancer_(ESCA)', 'TCGA_Endometrioid_Cancer_(UCEC)',
    'TCGA_Colon_and_Rectal_Cancer_(COADREAD)', 'TCGA_Colon_Cancer_(COAD)', 'TCGA_Cervical_Cancer_(CESC)',
    'TCGA_Breast_Cancer_(BRCA)', 'TCGA_Bladder_Cancer_(BLCA)', 'TCGA_Bile_Duct_Cancer_(CHOL)',
    'TCGA_Adrenocortical_Cancer_(ACC)', 'TCGA_Acute_Myeloid_Leukemia_(LAML)'
]

trait_keyword = trait
target_subdir = None

for sd in subdirectories:
    if trait_keyword.lower() in sd.lower():
        target_subdir = sd
        break

if target_subdir is None:
    # No suitable data found for this trait; mark as completed
    print("No TCGA subdirectory found for the trait. Skipping.")
else:
    # 2. Locate clinical and genetic data files
    cohort_dir = os.path.join(tcga_root_dir, target_subdir)
    clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)

    # 3. Load the data
    clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep='\t')
    genetic_df = pd.read_csv(genetic_file_path, index_col=0, sep='\t')

    # 4. Print column names of clinical data
    print(clinical_df.columns)
candidate_age_cols = ["age_at_initial_pathologic_diagnosis"]
candidate_gender_cols = []

candidate_demo_cols = candidate_age_cols + candidate_gender_cols
if candidate_demo_cols:
    extracted_df = clinical_df[candidate_demo_cols]
    preview_data = preview_df(extracted_df)
    print(preview_data)
# Based on the inspection of the provided dictionaries for age and gender:
age_col = "age_at_initial_pathologic_diagnosis"
gender_col = None

print("Chosen age_col:", age_col)
print("Chosen gender_col:", gender_col)
# 1. Extract and standardize the clinical features
selected_clinical_df = tcga_select_clinical_features(
    clinical_df=clinical_df,
    trait=trait,
    age_col=age_col,
    gender_col=gender_col
)

# (Optional) Save the selected clinical data
selected_clinical_df.to_csv(out_clinical_data_file)

# 2. Normalize gene symbols in the genetic data
normalized_gene_df = normalize_gene_symbols_in_index(genetic_df)
normalized_gene_df.to_csv(out_gene_data_file)

# 3. Link the clinical and genetic data on sample IDs
linked_data = selected_clinical_df.join(normalized_gene_df.T, how="inner")

# 4. Handle missing values
cleaned_df = handle_missing_values(linked_data, trait)

# 5. Determine if the trait or demographic features are biased
is_biased, final_df = judge_and_remove_biased_features(cleaned_df, trait)

# 6. Final quality validation
is_gene_available = not normalized_gene_df.empty
is_trait_available = trait in final_df.columns
is_usable = validate_and_save_cohort_info(
    is_final=True,
    cohort="TCGA",
    info_path=json_path,
    is_gene_available=is_gene_available,
    is_trait_available=is_trait_available,
    is_biased=is_biased,
    df=final_df,
    note=""
)

# 7. If the dataset is usable, save the final dataframe
if is_usable:
    final_df.to_csv(out_data_file)