Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- output/preprocess/Ovarian_Cancer/code/GSE130402.py +117 -0
- output/preprocess/Ovarian_Cancer/code/GSE132342.py +113 -0
- output/preprocess/Ovarian_Cancer/code/GSE135820.py +211 -0
- output/preprocess/Ovarian_Cancer/code/GSE146553.py +196 -0
- output/preprocess/Ovarian_Cancer/code/GSE146964.py +215 -0
- output/preprocess/Ovarian_Cancer/code/GSE201525.py +162 -0
- output/preprocess/Ovarian_Cancer/code/TCGA.py +308 -0
- output/preprocess/Ovarian_Cancer/gene_data/GSE201525.csv +0 -0
- output/preprocess/Pancreatic_Cancer/GSE125158.csv +0 -0
- output/preprocess/Pancreatic_Cancer/clinical_data/GSE125158.csv +4 -4
- output/preprocess/Pancreatic_Cancer/clinical_data/GSE130563.csv +1 -1
- output/preprocess/Pancreatic_Cancer/clinical_data/GSE131027.csv +2 -2
- output/preprocess/Pancreatic_Cancer/clinical_data/GSE236951.csv +4 -4
- output/preprocess/Pancreatic_Cancer/code/GSE120127.py +230 -0
- output/preprocess/Pancreatic_Cancer/code/GSE124069.py +171 -0
- output/preprocess/Pancreatic_Cancer/code/GSE125158.py +202 -0
- output/preprocess/Pancreatic_Cancer/code/GSE130563.py +465 -0
- output/preprocess/Pancreatic_Cancer/code/GSE131027.py +200 -0
- output/preprocess/Pancreatic_Cancer/code/GSE157494.py +120 -0
- output/preprocess/Pancreatic_Cancer/code/GSE183795.py +188 -0
- output/preprocess/Pancreatic_Cancer/code/GSE222788.py +140 -0
- output/preprocess/Pancreatic_Cancer/code/GSE223409.py +128 -0
- output/preprocess/Pancreatic_Cancer/code/GSE236951.py +174 -0
- output/preprocess/Pancreatic_Cancer/code/TCGA.py +244 -0
- output/preprocess/Pancreatic_Cancer/cohort_info.json +1 -112
- output/preprocess/Parkinsons_Disease/GSE49126.csv +0 -0
- output/preprocess/Parkinsons_Disease/GSE72267.csv +0 -0
- output/preprocess/Parkinsons_Disease/clinical_data/GSE101534.csv +2 -2
- output/preprocess/Parkinsons_Disease/clinical_data/GSE202667.csv +3 -4
- output/preprocess/Parkinsons_Disease/clinical_data/GSE72267.csv +2 -2
- output/preprocess/Parkinsons_Disease/code/GSE101534.py +440 -0
- output/preprocess/Parkinsons_Disease/code/GSE103099.py +141 -0
- output/preprocess/Parkinsons_Disease/code/GSE202665.py +224 -0
- output/preprocess/Parkinsons_Disease/code/GSE202667.py +194 -0
- output/preprocess/Parkinsons_Disease/code/GSE30335.py +216 -0
- output/preprocess/Parkinsons_Disease/code/GSE49126.py +248 -0
- output/preprocess/Parkinsons_Disease/code/GSE57475.py +203 -0
- output/preprocess/Parkinsons_Disease/code/GSE71220.py +109 -0
- output/preprocess/Parkinsons_Disease/code/GSE72267.py +195 -0
- output/preprocess/Parkinsons_Disease/code/GSE80599.py +222 -0
- output/preprocess/Parkinsons_Disease/code/TCGA.py +64 -0
- output/preprocess/Parkinsons_Disease/cohort_info.json +1 -112
- output/regress/Polycystic_Kidney_Disease/significant_genes_condition_Gender.json +0 -0
- output/regress/Polycystic_Kidney_Disease/significant_genes_condition_Hypertension.json +0 -0
- output/regress/Polycystic_Kidney_Disease/significant_genes_condition_None.json +0 -0
- output/regress/Polycystic_Kidney_Disease/significant_genes_condition_Obesity.json +0 -0
- output/regress/Polycystic_Ovary_Syndrome/significant_genes_condition_Age.json +0 -0
- output/regress/Polycystic_Ovary_Syndrome/significant_genes_condition_Atherosclerosis.json +0 -0
- output/regress/Polycystic_Ovary_Syndrome/significant_genes_condition_Atrial_Fibrillation.json +0 -0
- output/regress/Polycystic_Ovary_Syndrome/significant_genes_condition_Bipolar_disorder.json +403 -220
output/preprocess/Ovarian_Cancer/code/GSE130402.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Ovarian_Cancer"
|
| 6 |
+
cohort = "GSE130402"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Ovarian_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Ovarian_Cancer/GSE130402"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Ovarian_Cancer/GSE130402.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Ovarian_Cancer/gene_data/GSE130402.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Ovarian_Cancer/clinical_data/GSE130402.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Ovarian_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
|
| 41 |
+
# Determine gene expression data availability based on background info:
|
| 42 |
+
# Microarray gene expression analyses were conducted after miRNA transfection.
|
| 43 |
+
is_gene_available = True
|
| 44 |
+
|
| 45 |
+
# Variable availability:
|
| 46 |
+
# This is a cell-line experiment (HEY, SKOV3: ovarian; PC3: prostate) with no human subject info.
|
| 47 |
+
# Therefore, human trait, age, and gender are not available.
|
| 48 |
+
trait_row = None
|
| 49 |
+
age_row = None
|
| 50 |
+
gender_row = None
|
| 51 |
+
|
| 52 |
+
# Conversion functions (defined for completeness; not used since trait_row is None)
|
| 53 |
+
def _after_colon(x: str) -> str:
|
| 54 |
+
if x is None:
|
| 55 |
+
return None
|
| 56 |
+
if isinstance(x, str):
|
| 57 |
+
parts = x.split(":", 1)
|
| 58 |
+
val = parts[1].strip() if len(parts) > 1 else x.strip()
|
| 59 |
+
return val if val != "" else None
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
def convert_trait(x):
|
| 63 |
+
val = _after_colon(x)
|
| 64 |
+
if val is None:
|
| 65 |
+
return None
|
| 66 |
+
s = val.lower()
|
| 67 |
+
# Heuristic: ovarian cancer cell lines (HEY, SKOV3) -> 1; non-ovarian (e.g., PC3) -> 0
|
| 68 |
+
if "hey" in s or "skov3" in s:
|
| 69 |
+
return 1
|
| 70 |
+
if "pc3" in s or "prostate" in s:
|
| 71 |
+
return 0
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
def convert_age(x):
|
| 75 |
+
val = _after_colon(x)
|
| 76 |
+
if val is None:
|
| 77 |
+
return None
|
| 78 |
+
import re
|
| 79 |
+
m = re.search(r"(\d+(\.\d+)?)", val)
|
| 80 |
+
return float(m.group(1)) if m else None
|
| 81 |
+
|
| 82 |
+
def convert_gender(x):
|
| 83 |
+
val = _after_colon(x)
|
| 84 |
+
if val is None:
|
| 85 |
+
return None
|
| 86 |
+
s = val.strip().lower()
|
| 87 |
+
if s in {"f", "female", "woman", "women"}:
|
| 88 |
+
return 0
|
| 89 |
+
if s in {"m", "male", "man", "men"}:
|
| 90 |
+
return 1
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
# Initial filtering and save metadata
|
| 94 |
+
is_trait_available = trait_row is not None
|
| 95 |
+
_ = validate_and_save_cohort_info(
|
| 96 |
+
is_final=False,
|
| 97 |
+
cohort=cohort,
|
| 98 |
+
info_path=json_path,
|
| 99 |
+
is_gene_available=is_gene_available,
|
| 100 |
+
is_trait_available=is_trait_available
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Clinical feature extraction: skipped because human clinical data is not available (trait_row is None)
|
| 104 |
+
if trait_row is not None:
|
| 105 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 106 |
+
clinical_df=clinical_data,
|
| 107 |
+
trait=trait,
|
| 108 |
+
trait_row=trait_row,
|
| 109 |
+
convert_trait=convert_trait,
|
| 110 |
+
age_row=age_row,
|
| 111 |
+
convert_age=convert_age,
|
| 112 |
+
gender_row=gender_row,
|
| 113 |
+
convert_gender=convert_gender
|
| 114 |
+
)
|
| 115 |
+
preview = preview_df(selected_clinical_df)
|
| 116 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 117 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
output/preprocess/Ovarian_Cancer/code/GSE132342.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Ovarian_Cancer"
|
| 6 |
+
cohort = "GSE132342"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Ovarian_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Ovarian_Cancer/GSE132342"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Ovarian_Cancer/GSE132342.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Ovarian_Cancer/gene_data/GSE132342.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Ovarian_Cancer/clinical_data/GSE132342.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Ovarian_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Step 1: Determine gene data availability
|
| 40 |
+
is_gene_available = True # Gene expression profiling reported in series summary/design
|
| 41 |
+
|
| 42 |
+
# Step 2: Variable availability and conversion functions
|
| 43 |
+
|
| 44 |
+
# Keys in the sample characteristics dictionary
|
| 45 |
+
trait_row = None # All samples are HGSOC cases -> trait (Ovarian_Cancer) is constant and thus not usable
|
| 46 |
+
age_row = 8 # 'age: q1/q2/q3/q4' available as quartiles
|
| 47 |
+
gender_row = None # 'Sex: Female' only -> constant and not usable
|
| 48 |
+
|
| 49 |
+
def _after_colon(x):
|
| 50 |
+
if x is None:
|
| 51 |
+
return None
|
| 52 |
+
try:
|
| 53 |
+
return str(x).split(":", 1)[1].strip()
|
| 54 |
+
except Exception:
|
| 55 |
+
return None
|
| 56 |
+
|
| 57 |
+
def convert_trait(x):
|
| 58 |
+
# Map ovarian cancer cases to 1, non-cancer/controls to 0; unknown -> None
|
| 59 |
+
val = _after_colon(x)
|
| 60 |
+
if val is None:
|
| 61 |
+
return None
|
| 62 |
+
v = val.lower()
|
| 63 |
+
if any(k in v for k in ["ovarian", "ovary", "hgsoc", "high-grade serous", "hgsc", "serous ovarian"]):
|
| 64 |
+
return 1
|
| 65 |
+
if any(k in v for k in ["control", "normal", "healthy", "benign", "adjacent normal"]):
|
| 66 |
+
return 0
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
def convert_age(x):
|
| 70 |
+
# Convert age quartiles to ordinal numeric scale (q1=1 ... q4=4)
|
| 71 |
+
val = _after_colon(x)
|
| 72 |
+
if val is None:
|
| 73 |
+
return None
|
| 74 |
+
v = val.strip().lower()
|
| 75 |
+
mapping = {"q1": 1, "q2": 2, "q3": 3, "q4": 4}
|
| 76 |
+
return mapping.get(v, None)
|
| 77 |
+
|
| 78 |
+
def convert_gender(x):
|
| 79 |
+
# Female -> 0, Male -> 1
|
| 80 |
+
val = _after_colon(x)
|
| 81 |
+
if val is None:
|
| 82 |
+
return None
|
| 83 |
+
v = val.strip().lower()
|
| 84 |
+
if v in ["female", "f", "woman", "women"]:
|
| 85 |
+
return 0
|
| 86 |
+
if v in ["male", "m", "man", "men"]:
|
| 87 |
+
return 1
|
| 88 |
+
return None
|
| 89 |
+
|
| 90 |
+
# Step 3: Save metadata using initial filtering
|
| 91 |
+
is_trait_available = trait_row is not None
|
| 92 |
+
_ = validate_and_save_cohort_info(
|
| 93 |
+
is_final=False,
|
| 94 |
+
cohort=cohort,
|
| 95 |
+
info_path=json_path,
|
| 96 |
+
is_gene_available=is_gene_available,
|
| 97 |
+
is_trait_available=is_trait_available
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# Step 4: Clinical feature extraction (skip because trait_row is None)
|
| 101 |
+
# If trait_row were available:
|
| 102 |
+
# selected_clinical_df = geo_select_clinical_features(
|
| 103 |
+
# clinical_df=clinical_data,
|
| 104 |
+
# trait=trait,
|
| 105 |
+
# trait_row=trait_row,
|
| 106 |
+
# convert_trait=convert_trait,
|
| 107 |
+
# age_row=age_row,
|
| 108 |
+
# convert_age=convert_age,
|
| 109 |
+
# gender_row=gender_row,
|
| 110 |
+
# convert_gender=convert_gender
|
| 111 |
+
# )
|
| 112 |
+
# preview = preview_df(selected_clinical_df)
|
| 113 |
+
# selected_clinical_df.to_csv(out_clinical_data_file)
|
output/preprocess/Ovarian_Cancer/code/GSE135820.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Ovarian_Cancer"
|
| 6 |
+
cohort = "GSE135820"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Ovarian_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Ovarian_Cancer/GSE135820"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Ovarian_Cancer/GSE135820.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Ovarian_Cancer/gene_data/GSE135820.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Ovarian_Cancer/clinical_data/GSE135820.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Ovarian_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Determine data availability
|
| 40 |
+
is_gene_available = True # NanoString gene expression panel (mRNA), not miRNA/methylation
|
| 41 |
+
|
| 42 |
+
# Identify rows for variables based on the Sample Characteristics Dictionary
|
| 43 |
+
trait_row = None # Ovarian_Cancer presence is effectively constant here (all ovarian cancer cases)
|
| 44 |
+
age_row = 3
|
| 45 |
+
gender_row = None # No gender/sex field present
|
| 46 |
+
|
| 47 |
+
# Conversion helpers
|
| 48 |
+
def _extract_after_colon(x):
|
| 49 |
+
if x is None:
|
| 50 |
+
return None
|
| 51 |
+
if isinstance(x, (int, float)):
|
| 52 |
+
return x
|
| 53 |
+
s = str(x)
|
| 54 |
+
if ':' in s:
|
| 55 |
+
s = s.split(':', 1)[1]
|
| 56 |
+
s = s.strip()
|
| 57 |
+
if s == '' or s.lower() in {'na', 'n/a', 'nan', 'none', 'unknown'}:
|
| 58 |
+
return None
|
| 59 |
+
return s
|
| 60 |
+
|
| 61 |
+
def convert_trait(x):
|
| 62 |
+
# Map to presence of ovarian cancer (binary). Here, all diagnoses are ovarian cancers,
|
| 63 |
+
# so this would map to 1 for both HGSOC and non-HGSOC if it were used.
|
| 64 |
+
s = _extract_after_colon(x)
|
| 65 |
+
if s is None:
|
| 66 |
+
return None
|
| 67 |
+
sl = str(s).lower()
|
| 68 |
+
if any(k in sl for k in ['hgsoc', 'non-hgsoc', 'ovarian']):
|
| 69 |
+
return 1
|
| 70 |
+
if any(k in sl for k in ['control', 'normal', 'healthy']):
|
| 71 |
+
return 0
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
def convert_age(x):
|
| 75 |
+
s = _extract_after_colon(x)
|
| 76 |
+
if s is None:
|
| 77 |
+
return None
|
| 78 |
+
try:
|
| 79 |
+
v = float(str(s).strip())
|
| 80 |
+
return v
|
| 81 |
+
except Exception:
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
def convert_gender(x):
|
| 85 |
+
s = _extract_after_colon(x)
|
| 86 |
+
if s is None:
|
| 87 |
+
return None
|
| 88 |
+
sl = str(s).strip().lower()
|
| 89 |
+
# Female -> 0, Male -> 1
|
| 90 |
+
if sl in {'female', 'f', 'woman', 'women'}:
|
| 91 |
+
return 0
|
| 92 |
+
if sl in {'male', 'm', 'man', 'men'}:
|
| 93 |
+
return 1
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
# Initial filtering metadata save
|
| 97 |
+
is_trait_available = trait_row is not None
|
| 98 |
+
_ = validate_and_save_cohort_info(
|
| 99 |
+
is_final=False,
|
| 100 |
+
cohort=cohort,
|
| 101 |
+
info_path=json_path,
|
| 102 |
+
is_gene_available=is_gene_available,
|
| 103 |
+
is_trait_available=is_trait_available
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Clinical feature extraction (skip if trait_row is None)
|
| 107 |
+
if trait_row is not None:
|
| 108 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 109 |
+
clinical_df=clinical_data,
|
| 110 |
+
trait=trait,
|
| 111 |
+
trait_row=trait_row,
|
| 112 |
+
convert_trait=convert_trait,
|
| 113 |
+
age_row=age_row,
|
| 114 |
+
convert_age=convert_age,
|
| 115 |
+
gender_row=gender_row,
|
| 116 |
+
convert_gender=convert_gender
|
| 117 |
+
)
|
| 118 |
+
_ = preview_df(selected_clinical_df)
|
| 119 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 120 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 121 |
+
|
| 122 |
+
# Step 3: Gene Data Extraction
|
| 123 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 124 |
+
gene_data = get_genetic_data(matrix_file)
|
| 125 |
+
|
| 126 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 127 |
+
print(gene_data.index[:20])
|
| 128 |
+
|
| 129 |
+
# Step 4: Gene Identifier Review
|
| 130 |
+
print("requires_gene_mapping = True")
|
| 131 |
+
|
| 132 |
+
# Step 5: Gene Annotation
|
| 133 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 134 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 135 |
+
|
| 136 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 137 |
+
print("Gene annotation preview:")
|
| 138 |
+
print(preview_df(gene_annotation))
|
| 139 |
+
|
| 140 |
+
# Step 6: Gene Identifier Mapping
|
| 141 |
+
# Determine which columns in the gene annotation correspond to probe IDs and gene symbols
|
| 142 |
+
id_col = 'ID' # Matches matrix IDs like 'NM_000038.3:6850'
|
| 143 |
+
symbol_col = 'ORF' if 'ORF' in gene_annotation.columns else 'Customer.Identifier'
|
| 144 |
+
|
| 145 |
+
# 2. Build the mapping dataframe
|
| 146 |
+
gene_mapping = get_gene_mapping(gene_annotation, prob_col=id_col, gene_col=symbol_col)
|
| 147 |
+
|
| 148 |
+
# 3. Apply mapping to convert probe-level data to gene-level expression
|
| 149 |
+
# Keep original probe-level data for clarity
|
| 150 |
+
probe_data = gene_data
|
| 151 |
+
gene_data = apply_gene_mapping(expression_df=probe_data, mapping_df=gene_mapping)
|
| 152 |
+
|
| 153 |
+
# Step 7: Data Normalization and Linking
|
| 154 |
+
# 1. Normalize gene symbols and save gene expression data
|
| 155 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 156 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 157 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 158 |
+
|
| 159 |
+
# Determine trait availability from previous steps
|
| 160 |
+
trait_available = False
|
| 161 |
+
try:
|
| 162 |
+
trait_available = bool(is_trait_available)
|
| 163 |
+
except NameError:
|
| 164 |
+
try:
|
| 165 |
+
trait_available = (trait_row is not None)
|
| 166 |
+
except NameError:
|
| 167 |
+
trait_available = False
|
| 168 |
+
|
| 169 |
+
if not trait_available:
|
| 170 |
+
# 5. Final validation and save cohort info when trait data is unavailable; skip linking and downstream steps
|
| 171 |
+
placeholder_df = pd.DataFrame({trait: [None], 'G1': [1.0], 'G2': [1.0], 'G3': [1.0], 'G4': [1.0]})
|
| 172 |
+
note = ("INFO: Trait variable is unavailable for this cohort. Samples are predominantly ovarian cancers by design "
|
| 173 |
+
"with no explicit case/control labeling in clinical annotations; therefore, dataset is not usable for "
|
| 174 |
+
"trait-based association analysis. Gene expression data were normalized and saved separately.")
|
| 175 |
+
is_usable = validate_and_save_cohort_info(
|
| 176 |
+
is_final=True,
|
| 177 |
+
cohort=cohort,
|
| 178 |
+
info_path=json_path,
|
| 179 |
+
is_gene_available=True,
|
| 180 |
+
is_trait_available=False,
|
| 181 |
+
is_biased=False,
|
| 182 |
+
df=placeholder_df,
|
| 183 |
+
note=note
|
| 184 |
+
)
|
| 185 |
+
else:
|
| 186 |
+
# 2. Link clinical and genetic data
|
| 187 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 188 |
+
|
| 189 |
+
# 3. Handle missing values
|
| 190 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 191 |
+
|
| 192 |
+
# 4. Bias evaluation and removal of biased demographics
|
| 193 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 194 |
+
|
| 195 |
+
# 5. Final validation and save cohort info
|
| 196 |
+
note = "INFO: Linked data processed with missing value handling and bias evaluation."
|
| 197 |
+
is_usable = validate_and_save_cohort_info(
|
| 198 |
+
is_final=True,
|
| 199 |
+
cohort=cohort,
|
| 200 |
+
info_path=json_path,
|
| 201 |
+
is_gene_available=True,
|
| 202 |
+
is_trait_available=True,
|
| 203 |
+
is_biased=is_trait_biased,
|
| 204 |
+
df=unbiased_linked_data,
|
| 205 |
+
note=note
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
# 6. Save usable linked data
|
| 209 |
+
if is_usable:
|
| 210 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 211 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Ovarian_Cancer/code/GSE146553.py
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Ovarian_Cancer"
|
| 6 |
+
cohort = "GSE146553"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Ovarian_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Ovarian_Cancer/GSE146553"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Ovarian_Cancer/GSE146553.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Ovarian_Cancer/gene_data/GSE146553.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Ovarian_Cancer/clinical_data/GSE146553.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Ovarian_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# 1) Gene expression availability
|
| 40 |
+
is_gene_available = True # Affymetrix HG 1.0ST arrays and RNA-seq were used for gene expression
|
| 41 |
+
|
| 42 |
+
# 2) Variable availability (from Sample Characteristics Dictionary)
|
| 43 |
+
trait_row = 4 # 'tissue' field: can infer ovarian cancer status (tumor vs normal); exclude cell lines
|
| 44 |
+
age_row = 2 # 'patient age'
|
| 45 |
+
gender_row = None # Only 'gender: female' is present (constant), so not useful
|
| 46 |
+
|
| 47 |
+
# 2.2) Converters
|
| 48 |
+
def _after_colon(x):
|
| 49 |
+
if x is None:
|
| 50 |
+
return None
|
| 51 |
+
if isinstance(x, str):
|
| 52 |
+
parts = x.split(":", 1)
|
| 53 |
+
return parts[1].strip() if len(parts) > 1 else x.strip()
|
| 54 |
+
return x
|
| 55 |
+
|
| 56 |
+
def convert_trait(x):
|
| 57 |
+
val = _after_colon(x)
|
| 58 |
+
if val is None:
|
| 59 |
+
return None
|
| 60 |
+
v = val.strip().lower()
|
| 61 |
+
if v in {"na", ""}:
|
| 62 |
+
return None
|
| 63 |
+
# Exclude non-human samples
|
| 64 |
+
if "cell line" in v:
|
| 65 |
+
return None
|
| 66 |
+
# Map to case/control
|
| 67 |
+
if "normal" in v:
|
| 68 |
+
return 0
|
| 69 |
+
if ("cancer" in v) or ("tumor" in v) or ("carcinoma" in v):
|
| 70 |
+
return 1
|
| 71 |
+
return None
|
| 72 |
+
|
| 73 |
+
def convert_age(x):
|
| 74 |
+
val = _after_colon(x)
|
| 75 |
+
if val is None:
|
| 76 |
+
return None
|
| 77 |
+
v = str(val).strip()
|
| 78 |
+
if v.lower() == "na" or v == "":
|
| 79 |
+
return None
|
| 80 |
+
try:
|
| 81 |
+
return float(v)
|
| 82 |
+
except Exception:
|
| 83 |
+
import re
|
| 84 |
+
m = re.search(r"[-+]?\d*\.?\d+", v)
|
| 85 |
+
return float(m.group()) if m else None
|
| 86 |
+
|
| 87 |
+
def convert_gender(x):
|
| 88 |
+
val = _after_colon(x)
|
| 89 |
+
if val is None:
|
| 90 |
+
return None
|
| 91 |
+
v = val.strip().lower()
|
| 92 |
+
if v in {"na", ""}:
|
| 93 |
+
return None
|
| 94 |
+
if v.startswith("f"):
|
| 95 |
+
return 0
|
| 96 |
+
if v.startswith("m"):
|
| 97 |
+
return 1
|
| 98 |
+
return None
|
| 99 |
+
|
| 100 |
+
# 3) Save initial metadata
|
| 101 |
+
is_trait_available = trait_row is not None
|
| 102 |
+
_ = validate_and_save_cohort_info(
|
| 103 |
+
is_final=False,
|
| 104 |
+
cohort=cohort,
|
| 105 |
+
info_path=json_path,
|
| 106 |
+
is_gene_available=is_gene_available,
|
| 107 |
+
is_trait_available=is_trait_available
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# 4) Clinical feature extraction (only if trait_row is available)
|
| 111 |
+
if trait_row is not None:
|
| 112 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 113 |
+
clinical_df=clinical_data,
|
| 114 |
+
trait=trait,
|
| 115 |
+
trait_row=trait_row,
|
| 116 |
+
convert_trait=convert_trait,
|
| 117 |
+
age_row=age_row,
|
| 118 |
+
convert_age=convert_age,
|
| 119 |
+
gender_row=gender_row,
|
| 120 |
+
convert_gender=convert_gender
|
| 121 |
+
)
|
| 122 |
+
preview = preview_df(selected_clinical_df)
|
| 123 |
+
print(preview)
|
| 124 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 125 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 126 |
+
|
| 127 |
+
# Step 3: Gene Data Extraction
|
| 128 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 129 |
+
gene_data = get_genetic_data(matrix_file)
|
| 130 |
+
|
| 131 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 132 |
+
print(gene_data.index[:20])
|
| 133 |
+
|
| 134 |
+
# Step 4: Gene Identifier Review
|
| 135 |
+
# The identifiers like '7896736' are Affymetrix/array probe or transcript cluster IDs, not human gene symbols.
|
| 136 |
+
requires_gene_mapping = True
|
| 137 |
+
print(f"requires_gene_mapping = {requires_gene_mapping}")
|
| 138 |
+
|
| 139 |
+
# Step 5: Gene Annotation
|
| 140 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 141 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 142 |
+
|
| 143 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 144 |
+
print("Gene annotation preview:")
|
| 145 |
+
print(preview_df(gene_annotation))
|
| 146 |
+
|
| 147 |
+
# Step 6: Gene Identifier Mapping
|
| 148 |
+
# 1-2) Decide columns and build mapping dataframe
|
| 149 |
+
# Probe IDs match 'ID'; gene symbols can be parsed from 'gene_assignment'
|
| 150 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='gene_assignment')
|
| 151 |
+
|
| 152 |
+
# 3) Apply mapping to convert probe-level data to gene-level expression
|
| 153 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 154 |
+
|
| 155 |
+
# Step 7: Data Normalization and Linking
|
| 156 |
+
import os
|
| 157 |
+
import pandas as pd
|
| 158 |
+
|
| 159 |
+
# 1) Normalize gene symbols and save
|
| 160 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 161 |
+
|
| 162 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 163 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 164 |
+
|
| 165 |
+
# 2) Ensure clinical dataframe is available (load if missing), then link
|
| 166 |
+
try:
|
| 167 |
+
selected_clinical_df
|
| 168 |
+
except NameError:
|
| 169 |
+
selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)
|
| 170 |
+
|
| 171 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 172 |
+
|
| 173 |
+
# 3) Handle missing values
|
| 174 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 175 |
+
|
| 176 |
+
# 4) Bias checks and remove biased demographics
|
| 177 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 178 |
+
|
| 179 |
+
# 5) Final validation and metadata saving
|
| 180 |
+
note = ("INFO: Trait inferred from 'tissue' field with cell line samples excluded; "
|
| 181 |
+
"Gender not included due to being constant/unavailable; Age included when available.")
|
| 182 |
+
is_usable = validate_and_save_cohort_info(
|
| 183 |
+
is_final=True,
|
| 184 |
+
cohort=cohort,
|
| 185 |
+
info_path=json_path,
|
| 186 |
+
is_gene_available=True,
|
| 187 |
+
is_trait_available=True,
|
| 188 |
+
is_biased=is_trait_biased,
|
| 189 |
+
df=unbiased_linked_data,
|
| 190 |
+
note=note
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
# 6) Save linked data if usable
|
| 194 |
+
if is_usable:
|
| 195 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 196 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Ovarian_Cancer/code/GSE146964.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Ovarian_Cancer"
|
| 6 |
+
cohort = "GSE146964"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Ovarian_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Ovarian_Cancer/GSE146964"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Ovarian_Cancer/GSE146964.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Ovarian_Cancer/gene_data/GSE146964.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Ovarian_Cancer/clinical_data/GSE146964.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Ovarian_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Determine data availability based on the provided background and sample characteristics
|
| 40 |
+
is_gene_available = True # Affymetrix mRNA microarrays indicate gene expression data
|
| 41 |
+
trait_row = None # Trait (Ovarian_Cancer) is constant across samples (all malignant HGSOC)
|
| 42 |
+
age_row = None # No age information in the sample characteristics
|
| 43 |
+
gender_row = None # Gender is constant (all Female)
|
| 44 |
+
|
| 45 |
+
# Conversion functions
|
| 46 |
+
def _extract_value(x):
|
| 47 |
+
if x is None:
|
| 48 |
+
return None
|
| 49 |
+
if isinstance(x, str):
|
| 50 |
+
parts = x.split(":", 1)
|
| 51 |
+
val = parts[1] if len(parts) == 2 else parts[0]
|
| 52 |
+
return val.strip().strip('"').strip()
|
| 53 |
+
return x
|
| 54 |
+
|
| 55 |
+
def convert_trait(x):
|
| 56 |
+
# Map to binary: 1 = Ovarian cancer case; 0 = non-cancer/control.
|
| 57 |
+
val = _extract_value(x)
|
| 58 |
+
if val is None or val == "":
|
| 59 |
+
return None
|
| 60 |
+
v = val.lower()
|
| 61 |
+
positives = [
|
| 62 |
+
"ovarian cancer", "high grade serous ovarian cancer", "hgsc", "hgsoc",
|
| 63 |
+
"cancer", "tumor", "carcinoma", "malignant", "case", "diseased"
|
| 64 |
+
]
|
| 65 |
+
negatives = ["normal", "control", "benign", "healthy", "non-tumor", "adjacent normal"]
|
| 66 |
+
if any(p in v for p in positives):
|
| 67 |
+
return 1
|
| 68 |
+
if any(n in v for n in negatives):
|
| 69 |
+
return 0
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
def convert_age(x):
|
| 73 |
+
val = _extract_value(x)
|
| 74 |
+
if val is None or val == "":
|
| 75 |
+
return None
|
| 76 |
+
v = val.lower().replace("years", "").replace("year", "").replace("yrs", "").strip()
|
| 77 |
+
for bad in ["na", "n/a", "unknown", "not available", "missing", "--", "null"]:
|
| 78 |
+
if v == bad:
|
| 79 |
+
return None
|
| 80 |
+
try:
|
| 81 |
+
return float(v)
|
| 82 |
+
except Exception:
|
| 83 |
+
# Try to extract leading number if present
|
| 84 |
+
import re
|
| 85 |
+
m = re.search(r"[-+]?\d*\.?\d+", v)
|
| 86 |
+
if m:
|
| 87 |
+
try:
|
| 88 |
+
return float(m.group())
|
| 89 |
+
except Exception:
|
| 90 |
+
return None
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
def convert_gender(x):
|
| 94 |
+
val = _extract_value(x)
|
| 95 |
+
if val is None or val == "":
|
| 96 |
+
return None
|
| 97 |
+
v = val.lower()
|
| 98 |
+
if v in ["f", "female", "woman", "women"]:
|
| 99 |
+
return 0
|
| 100 |
+
if v in ["m", "male", "man", "men"]:
|
| 101 |
+
return 1
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
# Initial filtering and metadata saving
|
| 105 |
+
is_trait_available = trait_row is not None
|
| 106 |
+
_ = validate_and_save_cohort_info(
|
| 107 |
+
is_final=False,
|
| 108 |
+
cohort=cohort,
|
| 109 |
+
info_path=json_path,
|
| 110 |
+
is_gene_available=is_gene_available,
|
| 111 |
+
is_trait_available=is_trait_available
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Clinical feature extraction (skipped because trait_row is None)
|
| 115 |
+
if trait_row is not None:
|
| 116 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 117 |
+
clinical_df=clinical_data,
|
| 118 |
+
trait=trait,
|
| 119 |
+
trait_row=trait_row,
|
| 120 |
+
convert_trait=convert_trait,
|
| 121 |
+
age_row=age_row,
|
| 122 |
+
convert_age=convert_age,
|
| 123 |
+
gender_row=gender_row,
|
| 124 |
+
convert_gender=convert_gender
|
| 125 |
+
)
|
| 126 |
+
_ = preview_df(selected_clinical_df)
|
| 127 |
+
# Save selected clinical data
|
| 128 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 129 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 130 |
+
|
| 131 |
+
# Step 3: Gene Data Extraction
|
| 132 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 133 |
+
gene_data = get_genetic_data(matrix_file)
|
| 134 |
+
|
| 135 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 136 |
+
print(gene_data.index[:20])
|
| 137 |
+
|
| 138 |
+
# Step 4: Gene Identifier Review
|
| 139 |
+
# Based on the observed identifiers such as 'AFFX-BkGr-GC03_st', which are Affymetrix control/probe IDs,
|
| 140 |
+
# these are not human gene symbols and require mapping to gene symbols via platform annotation.
|
| 141 |
+
requires_gene_mapping = True
|
| 142 |
+
print(f"requires_gene_mapping = {requires_gene_mapping}")
|
| 143 |
+
|
| 144 |
+
# Step 5: Gene Annotation
|
| 145 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 146 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 147 |
+
|
| 148 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 149 |
+
print("Gene annotation preview:")
|
| 150 |
+
print(preview_df(gene_annotation))
|
| 151 |
+
|
| 152 |
+
# Step 6: Gene Identifier Mapping
|
| 153 |
+
# Decide appropriate columns from annotation for probe IDs and gene symbols
|
| 154 |
+
prob_col = 'probeset_id' if 'probeset_id' in gene_annotation.columns else 'ID'
|
| 155 |
+
gene_col = 'gene_assignment' if 'gene_assignment' in gene_annotation.columns else 'mrna_assignment'
|
| 156 |
+
|
| 157 |
+
# Build mapping dataframe (ID -> Gene text)
|
| 158 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=prob_col, gene_col=gene_col)
|
| 159 |
+
|
| 160 |
+
# Apply mapping to convert probe-level data to gene-level expression
|
| 161 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 162 |
+
|
| 163 |
+
# Step 7: Data Normalization and Linking
|
| 164 |
+
import os
|
| 165 |
+
|
| 166 |
+
# 1. Normalize gene symbols and save gene data
|
| 167 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 168 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 169 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 170 |
+
|
| 171 |
+
# Prepare clinical features if not already prepared (derive from available characteristics)
|
| 172 |
+
# Use 'condition: Malignant' as trait (row 3) and 'gender: Female' as gender (row 1)
|
| 173 |
+
trait_row = 3
|
| 174 |
+
gender_row = 1
|
| 175 |
+
age_row = None
|
| 176 |
+
|
| 177 |
+
selected_clinical_data = geo_select_clinical_features(
|
| 178 |
+
clinical_df=clinical_data,
|
| 179 |
+
trait=trait,
|
| 180 |
+
trait_row=trait_row,
|
| 181 |
+
convert_trait=convert_trait,
|
| 182 |
+
age_row=age_row,
|
| 183 |
+
convert_age=convert_age,
|
| 184 |
+
gender_row=gender_row,
|
| 185 |
+
convert_gender=convert_gender
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# 2. Link clinical and genetic data
|
| 189 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data)
|
| 190 |
+
|
| 191 |
+
# 3. Handle missing values in the linked data
|
| 192 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 193 |
+
|
| 194 |
+
# 4. Determine bias and remove biased demographic features
|
| 195 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 196 |
+
|
| 197 |
+
# 5. Final quality validation and save metadata
|
| 198 |
+
is_gene_available = True
|
| 199 |
+
is_trait_available = True # trait derived from 'condition' row
|
| 200 |
+
note = "INFO: Trait derived from 'condition: Malignant'; all samples are cases. Expect trait imbalance."
|
| 201 |
+
is_usable = validate_and_save_cohort_info(
|
| 202 |
+
is_final=True,
|
| 203 |
+
cohort=cohort,
|
| 204 |
+
info_path=json_path,
|
| 205 |
+
is_gene_available=is_gene_available,
|
| 206 |
+
is_trait_available=is_trait_available,
|
| 207 |
+
is_biased=is_trait_biased,
|
| 208 |
+
df=unbiased_linked_data,
|
| 209 |
+
note=note
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
# 6. Save linked data only if usable
|
| 213 |
+
if is_usable:
|
| 214 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 215 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Ovarian_Cancer/code/GSE201525.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Ovarian_Cancer"
|
| 6 |
+
cohort = "GSE201525"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Ovarian_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Ovarian_Cancer/GSE201525"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Ovarian_Cancer/GSE201525.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Ovarian_Cancer/gene_data/GSE201525.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Ovarian_Cancer/clinical_data/GSE201525.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Ovarian_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Determine data availability based on provided Sample Characteristics Dictionary
|
| 40 |
+
# Dictionary shows only 'treatment' (row 0) and 'replicate' (row 1); no clinical trait, age, or gender.
|
| 41 |
+
trait_row = None
|
| 42 |
+
age_row = None
|
| 43 |
+
gender_row = None
|
| 44 |
+
|
| 45 |
+
# Gene expression likely available (expression profiling under interferon treatments; not miRNA/methylation)
|
| 46 |
+
is_gene_available = True
|
| 47 |
+
is_trait_available = trait_row is not None
|
| 48 |
+
|
| 49 |
+
# Converters
|
| 50 |
+
def _after_colon(x):
|
| 51 |
+
if x is None:
|
| 52 |
+
return None
|
| 53 |
+
s = str(x)
|
| 54 |
+
if ':' in s:
|
| 55 |
+
s = s.split(':', 1)[1]
|
| 56 |
+
return s.strip().lower()
|
| 57 |
+
|
| 58 |
+
def convert_trait(x):
|
| 59 |
+
v = _after_colon(x)
|
| 60 |
+
if not v:
|
| 61 |
+
return None
|
| 62 |
+
# Heuristics for ovarian cancer presence
|
| 63 |
+
positives = ['cancer', 'tumor', 'tumour', 'carcinoma', 'serous', 'hgsoc', 'ovca', 'ovarian cancer']
|
| 64 |
+
negatives = ['normal', 'healthy', 'control', 'benign', 'adjacent normal', 'non-tumor', 'non tumour', 'noncancer']
|
| 65 |
+
if any(p in v for p in positives):
|
| 66 |
+
return 1
|
| 67 |
+
if any(n in v for n in negatives):
|
| 68 |
+
return 0
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
def convert_age(x):
|
| 72 |
+
v = _after_colon(x)
|
| 73 |
+
if not v or v in {'na', 'n/a', 'none', 'unknown', 'not available'}:
|
| 74 |
+
return None
|
| 75 |
+
# Extract numbers; if range present, take midpoint
|
| 76 |
+
import re
|
| 77 |
+
nums = re.findall(r'\d+\.?\d*', v)
|
| 78 |
+
if not nums:
|
| 79 |
+
return None
|
| 80 |
+
try:
|
| 81 |
+
if '-' in v or 'to' in v:
|
| 82 |
+
vals = [float(n) for n in nums[:2]]
|
| 83 |
+
if len(vals) == 2:
|
| 84 |
+
return sum(vals) / 2.0
|
| 85 |
+
# Otherwise take the first number
|
| 86 |
+
return float(nums[0])
|
| 87 |
+
except:
|
| 88 |
+
return None
|
| 89 |
+
|
| 90 |
+
def convert_gender(x):
|
| 91 |
+
v = _after_colon(x)
|
| 92 |
+
if not v:
|
| 93 |
+
return None
|
| 94 |
+
if v in {'female', 'f', 'woman', 'women'}:
|
| 95 |
+
return 0
|
| 96 |
+
if v in {'male', 'm', 'man', 'men'}:
|
| 97 |
+
return 1
|
| 98 |
+
return None
|
| 99 |
+
|
| 100 |
+
# Save initial metadata
|
| 101 |
+
_ = validate_and_save_cohort_info(
|
| 102 |
+
is_final=False,
|
| 103 |
+
cohort=cohort,
|
| 104 |
+
info_path=json_path,
|
| 105 |
+
is_gene_available=is_gene_available,
|
| 106 |
+
is_trait_available=is_trait_available
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Clinical feature extraction is skipped because trait_row is None (no clinical trait available)
|
| 110 |
+
# If trait_row were available, we would call geo_select_clinical_features and save the output.
|
| 111 |
+
|
| 112 |
+
# Step 3: Gene Data Extraction
|
| 113 |
+
# Extract gene expression data and inspect first 20 row IDs
|
| 114 |
+
gene_data = get_genetic_data(matrix_file)
|
| 115 |
+
print(list(gene_data.index[:20]))
|
| 116 |
+
|
| 117 |
+
# Step 4: Gene Identifier Review
|
| 118 |
+
print("requires_gene_mapping = True")
|
| 119 |
+
|
| 120 |
+
# Step 5: Gene Annotation
|
| 121 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 122 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 123 |
+
|
| 124 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 125 |
+
print("Gene annotation preview:")
|
| 126 |
+
print(preview_df(gene_annotation))
|
| 127 |
+
|
| 128 |
+
# Step 6: Gene Identifier Mapping
|
| 129 |
+
# Build mapping dataframe and normalize gene symbols to uppercase to align with extractor expectations
|
| 130 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')
|
| 131 |
+
mapping_df['Gene'] = mapping_df['Gene'].astype(str).str.upper()
|
| 132 |
+
|
| 133 |
+
# Apply mapping to convert probe-level data to gene-level expression
|
| 134 |
+
gene_data = apply_gene_mapping(gene_data, mapping_df)
|
| 135 |
+
|
| 136 |
+
# Optional sanity check
|
| 137 |
+
print(gene_data.shape)
|
| 138 |
+
|
| 139 |
+
# Step 7: Data Normalization and Linking
|
| 140 |
+
# 1. Normalize the obtained gene data and save to disk
|
| 141 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 142 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 143 |
+
|
| 144 |
+
# 2. No clinical trait available in this cohort; create an empty linked_data placeholder
|
| 145 |
+
linked_data = pd.DataFrame()
|
| 146 |
+
|
| 147 |
+
# 5. Final validation and save cohort info (dataset is unavailable due to missing trait)
|
| 148 |
+
note = "INFO: No usable clinical trait (only treatment/replicate fields). Skipped linking and bias checks."
|
| 149 |
+
is_usable = validate_and_save_cohort_info(
|
| 150 |
+
is_final=True,
|
| 151 |
+
cohort=cohort,
|
| 152 |
+
info_path=json_path,
|
| 153 |
+
is_gene_available=True,
|
| 154 |
+
is_trait_available=False,
|
| 155 |
+
is_biased=False,
|
| 156 |
+
df=linked_data,
|
| 157 |
+
note=note
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
# 6. Save linked data only if usable (it won't be in this case)
|
| 161 |
+
if is_usable:
|
| 162 |
+
linked_data.to_csv(out_data_file)
|
output/preprocess/Ovarian_Cancer/code/TCGA.py
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Ovarian_Cancer"
|
| 6 |
+
|
| 7 |
+
# Input paths
|
| 8 |
+
tcga_root_dir = "../DATA/TCGA"
|
| 9 |
+
|
| 10 |
+
# Output paths
|
| 11 |
+
out_data_file = "./output/z5/preprocess/Ovarian_Cancer/TCGA.csv"
|
| 12 |
+
out_gene_data_file = "./output/z5/preprocess/Ovarian_Cancer/gene_data/TCGA.csv"
|
| 13 |
+
out_clinical_data_file = "./output/z5/preprocess/Ovarian_Cancer/clinical_data/TCGA.csv"
|
| 14 |
+
json_path = "./output/z5/preprocess/Ovarian_Cancer/cohort_info.json"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Step 1: Initial Data Loading
|
| 18 |
+
import os
|
| 19 |
+
import pandas as pd
|
| 20 |
+
|
| 21 |
+
# Step 1: Select the most appropriate TCGA cohort directory for Ovarian Cancer
|
| 22 |
+
subdirs = [d for d in os.listdir(tcga_root_dir) if os.path.isdir(os.path.join(tcga_root_dir, d))]
|
| 23 |
+
|
| 24 |
+
# Define matching terms/synonyms for ovarian cancer
|
| 25 |
+
terms = ['ovarian', 'ovary', '(ov)']
|
| 26 |
+
|
| 27 |
+
def match_score(name: str) -> int:
|
| 28 |
+
name_l = name.lower()
|
| 29 |
+
score = 0
|
| 30 |
+
score += 3 if 'ovarian' in name_l else 0
|
| 31 |
+
score += 2 if 'ovary' in name_l else 0
|
| 32 |
+
score += 1 if '(ov)' in name_l else 0
|
| 33 |
+
return score
|
| 34 |
+
|
| 35 |
+
candidates = [(d, match_score(d)) for d in subdirs if any(t in d.lower() for t in terms)]
|
| 36 |
+
candidates = sorted(candidates, key=lambda x: (-x[1], len(x[0])))
|
| 37 |
+
|
| 38 |
+
if not candidates or candidates[0][1] == 0:
|
| 39 |
+
# No suitable cohort found; mark as skipped
|
| 40 |
+
validate_and_save_cohort_info(
|
| 41 |
+
is_final=False,
|
| 42 |
+
cohort="TCGA",
|
| 43 |
+
info_path=json_path,
|
| 44 |
+
is_gene_available=False,
|
| 45 |
+
is_trait_available=False
|
| 46 |
+
)
|
| 47 |
+
print("No suitable TCGA cohort directory found for trait Ovarian_Cancer. Skipping.")
|
| 48 |
+
clinical_df = pd.DataFrame()
|
| 49 |
+
genetic_df = pd.DataFrame()
|
| 50 |
+
else:
|
| 51 |
+
selected_dir_name = candidates[0][0]
|
| 52 |
+
cohort_dir = os.path.join(tcga_root_dir, selected_dir_name)
|
| 53 |
+
|
| 54 |
+
# Step 2: Identify clinical and genetic file paths
|
| 55 |
+
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)
|
| 56 |
+
|
| 57 |
+
# Step 3: Load both files as DataFrames
|
| 58 |
+
clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0, compression='infer', low_memory=False)
|
| 59 |
+
genetic_df = pd.read_csv(genetic_file_path, sep='\t', index_col=0, compression='infer', low_memory=False)
|
| 60 |
+
|
| 61 |
+
# Step 4: Print clinical column names
|
| 62 |
+
print(clinical_df.columns.tolist())
|
| 63 |
+
|
| 64 |
+
# Step 2: Find Candidate Demographic Features
|
| 65 |
+
# Use the previously obtained list of column names to identify candidate demographic features
|
| 66 |
+
columns_list = ['_INTEGRATION', '_PANCAN_CNA_PANCAN_K8', '_PANCAN_Cluster_Cluster_PANCAN', '_PANCAN_DNAMethyl_PANCAN', '_PANCAN_RPPA_PANCAN_K8', '_PANCAN_UNC_RNAseq_PANCAN_K16', '_PANCAN_miRNA_PANCAN', '_PANCAN_mirna_OV', '_PANCAN_mutation_PANCAN', '_PATIENT', '_cohort', '_primary_disease', '_primary_site', 'additional_pharmaceutical_therapy', 'additional_radiation_therapy', 'age_at_initial_pathologic_diagnosis', 'anatomic_neoplasm_subdivision', 'bcr_followup_barcode', 'bcr_patient_barcode', 'bcr_sample_barcode', 'clinical_stage', 'days_to_birth', 'days_to_collection', 'days_to_death', 'days_to_initial_pathologic_diagnosis', 'days_to_last_followup', 'days_to_new_tumor_event_additional_surgery_procedure', 'days_to_new_tumor_event_after_initial_treatment', 'eastern_cancer_oncology_group', 'followup_case_report_form_submission_reason', 'followup_treatment_success', 'form_completion_date', 'gender', 'histological_type', 'history_of_neoadjuvant_treatment', 'icd_10', 'icd_o_3_histology', 'icd_o_3_site', 'informed_consent_verified', 'initial_pathologic_diagnosis_method', 'initial_weight', 'intermediate_dimension', 'is_ffpe', 'karnofsky_performance_score', 'longest_dimension', 'lost_follow_up', 'lymphatic_invasion', 'neoplasm_histologic_grade', 'new_neoplasm_event_type', 'new_tumor_event_additional_surgery_procedure', 'new_tumor_event_after_initial_treatment', 'oct_embedded', 'other_dx', 'pathology_report_file_name', 'patient_id', 'performance_status_scale_timing', 'person_neoplasm_cancer_status', 'postoperative_rx_tx', 'primary_therapy_outcome_success', 'progression_determined_by', 'radiation_therapy', 'residual_tumor', 'sample_type', 'sample_type_id', 'shortest_dimension', 'tissue_prospective_collection_indicator', 'tissue_retrospective_collection_indicator', 'tissue_source_site', 'tumor_residual_disease', 'tumor_tissue_site', 'venous_invasion', 'vial_number', 'vital_status', 'year_of_initial_pathologic_diagnosis', '_GENOMIC_ID_TCGA_OV_PDMRNAseq', '_GENOMIC_ID_data/public/TCGA/OV/miRNA_HiSeq_gene', '_GENOMIC_ID_TCGA_OV_mutation_bcm_solid_gene', '_GENOMIC_ID_TCGA_OV_exp_u133a', '_GENOMIC_ID_TCGA_OV_hMethyl450', '_GENOMIC_ID_TCGA_OV_miRNA_HiSeq', '_GENOMIC_ID_TCGA_OV_mutation_curated_bcm_solid_gene', '_GENOMIC_ID_TCGA_OV_hMethyl27', '_GENOMIC_ID_TCGA_OV_mutation_wustl_hiseq_gene', '_GENOMIC_ID_TCGA_OV_RPPA_RBN', '_GENOMIC_ID_TCGA_OV_mutation_wustl_gene', '_GENOMIC_ID_TCGA_OV_exp_HiSeq', '_GENOMIC_ID_TCGA_OV_gistic2thd', '_GENOMIC_ID_TCGA_OV_PDMarray', '_GENOMIC_ID_TCGA_OV_RPPA', '_GENOMIC_ID_TCGA_OV_exp_HiSeqV2_PANCAN', '_GENOMIC_ID_TCGA_OV_exp_HiSeq_exon', '_GENOMIC_ID_TCGA_OV_exp_HiSeqV2', '_GENOMIC_ID_TCGA_OV_mutation_broad_gene', '_GENOMIC_ID_TCGA_OV_PDMarrayCNV', '_GENOMIC_ID_TCGA_OV_PDMRNAseqCNV', '_GENOMIC_ID_TCGA_OV_exp_HiSeqV2_exon', '_GENOMIC_ID_TCGA_OV_mutation', '_GENOMIC_ID_TCGA_OV_G4502A_07_3', '_GENOMIC_ID_TCGA_OV_G4502A_07_2']
|
| 67 |
+
|
| 68 |
+
import re
|
| 69 |
+
|
| 70 |
+
def tokenize(name: str):
|
| 71 |
+
return [t for t in re.split(r'[^a-zA-Z]+', name.lower()) if t]
|
| 72 |
+
|
| 73 |
+
candidate_age_cols = []
|
| 74 |
+
candidate_gender_cols = []
|
| 75 |
+
|
| 76 |
+
for col in columns_list:
|
| 77 |
+
tokens = tokenize(col)
|
| 78 |
+
# Age-like: contains token 'age' or token containing 'birth'
|
| 79 |
+
if ('age' in tokens) or any('birth' in t for t in tokens):
|
| 80 |
+
# Exclude false positives like 'clinical_stage'
|
| 81 |
+
if not any(t.endswith('stage') for t in tokens):
|
| 82 |
+
candidate_age_cols.append(col)
|
| 83 |
+
# Gender-like: contains tokens 'gender' or 'sex'
|
| 84 |
+
if ('gender' in tokens) or ('sex' in tokens):
|
| 85 |
+
candidate_gender_cols.append(col)
|
| 86 |
+
|
| 87 |
+
print(f"candidate_age_cols = {candidate_age_cols}")
|
| 88 |
+
print(f"candidate_gender_cols = {candidate_gender_cols}")
|
| 89 |
+
|
| 90 |
+
# Preview extracted data if clinical_df is available in the environment
|
| 91 |
+
if 'clinical_df' in globals():
|
| 92 |
+
try:
|
| 93 |
+
if isinstance(clinical_df, pd.DataFrame):
|
| 94 |
+
if candidate_age_cols:
|
| 95 |
+
available_age_cols = [c for c in candidate_age_cols if c in clinical_df.columns]
|
| 96 |
+
if available_age_cols:
|
| 97 |
+
age_df = clinical_df[available_age_cols]
|
| 98 |
+
print(preview_df(age_df, n=5))
|
| 99 |
+
if candidate_gender_cols:
|
| 100 |
+
available_gender_cols = [c for c in candidate_gender_cols if c in clinical_df.columns]
|
| 101 |
+
if available_gender_cols:
|
| 102 |
+
gender_df = clinical_df[available_gender_cols]
|
| 103 |
+
print(preview_df(gender_df, n=5))
|
| 104 |
+
except Exception:
|
| 105 |
+
pass
|
| 106 |
+
|
| 107 |
+
# Step 3: Select Demographic Features
|
| 108 |
+
# Attempt to use preview dictionaries if they exist; otherwise, treat as empty
|
| 109 |
+
try:
|
| 110 |
+
age_preview_dict # type: ignore
|
| 111 |
+
except NameError:
|
| 112 |
+
age_preview_dict = {}
|
| 113 |
+
try:
|
| 114 |
+
gender_preview_dict # type: ignore
|
| 115 |
+
except NameError:
|
| 116 |
+
gender_preview_dict = {}
|
| 117 |
+
|
| 118 |
+
import math
|
| 119 |
+
import re
|
| 120 |
+
|
| 121 |
+
def is_missing(v):
|
| 122 |
+
if v is None:
|
| 123 |
+
return True
|
| 124 |
+
if isinstance(v, float):
|
| 125 |
+
return math.isnan(v)
|
| 126 |
+
if isinstance(v, str):
|
| 127 |
+
return v.strip().lower() in {"", "na", "nan", "none", "null"}
|
| 128 |
+
return False
|
| 129 |
+
|
| 130 |
+
def extract_age_value(v):
|
| 131 |
+
if is_missing(v):
|
| 132 |
+
return None
|
| 133 |
+
# Accept numeric
|
| 134 |
+
if isinstance(v, (int, float)) and not math.isnan(v):
|
| 135 |
+
return int(v)
|
| 136 |
+
# Extract digits from string
|
| 137 |
+
s = str(v)
|
| 138 |
+
m = re.search(r'-?\d+', s)
|
| 139 |
+
if m:
|
| 140 |
+
try:
|
| 141 |
+
return int(m.group())
|
| 142 |
+
except Exception:
|
| 143 |
+
return None
|
| 144 |
+
return None
|
| 145 |
+
|
| 146 |
+
def is_gender_value(v):
|
| 147 |
+
if is_missing(v):
|
| 148 |
+
return False
|
| 149 |
+
s = str(v).strip().lower()
|
| 150 |
+
return s in {"male", "female", "m", "f"}
|
| 151 |
+
|
| 152 |
+
def select_best_age_col(candidates, preview_dict):
|
| 153 |
+
best_col = None
|
| 154 |
+
best_score = -1
|
| 155 |
+
for col in candidates:
|
| 156 |
+
preview_vals = preview_dict.get(col, [])
|
| 157 |
+
score = sum(1 for x in preview_vals if extract_age_value(x) is not None)
|
| 158 |
+
if score > best_score:
|
| 159 |
+
best_score = score
|
| 160 |
+
best_col = col
|
| 161 |
+
# Require at least one meaningful value in preview to accept
|
| 162 |
+
if best_score <= 0:
|
| 163 |
+
return None
|
| 164 |
+
return best_col
|
| 165 |
+
|
| 166 |
+
def select_best_gender_col(candidates, preview_dict):
|
| 167 |
+
best_col = None
|
| 168 |
+
best_score = -1
|
| 169 |
+
for col in candidates:
|
| 170 |
+
preview_vals = preview_dict.get(col, [])
|
| 171 |
+
score = sum(1 for x in preview_vals if is_gender_value(x))
|
| 172 |
+
if score > best_score:
|
| 173 |
+
best_score = score
|
| 174 |
+
best_col = col
|
| 175 |
+
# Require at least one meaningful value in preview to accept
|
| 176 |
+
if best_score <= 0:
|
| 177 |
+
return None
|
| 178 |
+
return best_col
|
| 179 |
+
|
| 180 |
+
# Provided candidate lists are assumed to exist
|
| 181 |
+
# candidate_age_cols, candidate_gender_cols
|
| 182 |
+
|
| 183 |
+
age_col = select_best_age_col(candidate_age_cols, age_preview_dict) if candidate_age_cols else None
|
| 184 |
+
gender_col = select_best_gender_col(candidate_gender_cols, gender_preview_dict) if candidate_gender_cols else None
|
| 185 |
+
|
| 186 |
+
print("Selected age_col:", age_col)
|
| 187 |
+
print("Selected gender_col:", gender_col)
|
| 188 |
+
|
| 189 |
+
# Explicitly print info for chosen columns (first 5 preview values if available)
|
| 190 |
+
if age_col is not None:
|
| 191 |
+
print("age_col preview (first 5):", age_preview_dict.get(age_col))
|
| 192 |
+
else:
|
| 193 |
+
print("No suitable age column found based on preview.")
|
| 194 |
+
|
| 195 |
+
if gender_col is not None:
|
| 196 |
+
print("gender_col preview (first 5):", gender_preview_dict.get(gender_col))
|
| 197 |
+
else:
|
| 198 |
+
print("No suitable gender column found based on preview.")
|
| 199 |
+
|
| 200 |
+
# Step 4: Feature Engineering and Validation
|
| 201 |
+
import os
|
| 202 |
+
import pandas as pd
|
| 203 |
+
|
| 204 |
+
# 1) Extract and standardize clinical features (trait, optional age and gender)
|
| 205 |
+
def _safe_tcga_convert_trait(idx: str):
|
| 206 |
+
try:
|
| 207 |
+
return tcga_convert_trait(idx)
|
| 208 |
+
except Exception:
|
| 209 |
+
return None
|
| 210 |
+
|
| 211 |
+
# Start with trait from barcode
|
| 212 |
+
trait_from_barcode = clinical_df.index.to_series().map(_safe_tcga_convert_trait)
|
| 213 |
+
|
| 214 |
+
# Fallback from sample_type_id if available
|
| 215 |
+
trait_from_type_id = None
|
| 216 |
+
if 'sample_type_id' in clinical_df.columns:
|
| 217 |
+
def _map_type_id(v):
|
| 218 |
+
try:
|
| 219 |
+
iv = int(v)
|
| 220 |
+
if 1 <= iv <= 9:
|
| 221 |
+
return 1
|
| 222 |
+
if 10 <= iv <= 19:
|
| 223 |
+
return 0
|
| 224 |
+
except Exception:
|
| 225 |
+
pass
|
| 226 |
+
return None
|
| 227 |
+
trait_from_type_id = clinical_df['sample_type_id'].map(_map_type_id)
|
| 228 |
+
|
| 229 |
+
# Fallback from sample_type if available
|
| 230 |
+
trait_from_type = None
|
| 231 |
+
if 'sample_type' in clinical_df.columns:
|
| 232 |
+
def _map_type_text(s):
|
| 233 |
+
if pd.isna(s):
|
| 234 |
+
return None
|
| 235 |
+
t = str(s).strip().lower()
|
| 236 |
+
tumor_like = {'primary tumor', 'recurrent tumor', 'metastatic', 'additional metastatic'}
|
| 237 |
+
normal_like = {'solid tissue normal', 'blood derived normal', 'normal', 'normal tissue'}
|
| 238 |
+
if t in tumor_like:
|
| 239 |
+
return 1
|
| 240 |
+
if t in normal_like:
|
| 241 |
+
return 0
|
| 242 |
+
# handle partial matches
|
| 243 |
+
if 'tumor' in t or 'metast' in t:
|
| 244 |
+
return 1
|
| 245 |
+
if 'normal' in t:
|
| 246 |
+
return 0
|
| 247 |
+
return None
|
| 248 |
+
trait_from_type = clinical_df['sample_type'].map(_map_type_text)
|
| 249 |
+
|
| 250 |
+
# Combine trait sources by priority
|
| 251 |
+
trait_series = trait_from_barcode.copy()
|
| 252 |
+
if trait_from_type_id is not None:
|
| 253 |
+
trait_series = trait_series.fillna(trait_from_type_id)
|
| 254 |
+
if trait_from_type is not None:
|
| 255 |
+
trait_series = trait_series.fillna(trait_from_type)
|
| 256 |
+
trait_series = trait_series.rename(trait)
|
| 257 |
+
|
| 258 |
+
# Assemble clinical DataFrame
|
| 259 |
+
clinical_features = [trait_series]
|
| 260 |
+
# age_col and gender_col are provided from previous step and may be None
|
| 261 |
+
if 'age_col' in globals() and age_col:
|
| 262 |
+
clinical_features.append(clinical_df[age_col].apply(tcga_convert_age).rename('Age'))
|
| 263 |
+
if 'gender_col' in globals() and gender_col:
|
| 264 |
+
clinical_features.append(clinical_df[gender_col].apply(tcga_convert_gender).rename('Gender'))
|
| 265 |
+
selected_clinical_df = pd.concat(clinical_features, axis=1)
|
| 266 |
+
|
| 267 |
+
# 2) Normalize gene symbols and save normalized gene expression data
|
| 268 |
+
gene_df_norm = normalize_gene_symbols_in_index(genetic_df)
|
| 269 |
+
# Ensure numeric
|
| 270 |
+
gene_df_norm = gene_df_norm.apply(pd.to_numeric, errors='coerce')
|
| 271 |
+
|
| 272 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 273 |
+
gene_df_norm.to_csv(out_gene_data_file)
|
| 274 |
+
|
| 275 |
+
# 3) Link clinical and genetic data on sample IDs
|
| 276 |
+
common_ids = selected_clinical_df.index.intersection(gene_df_norm.columns)
|
| 277 |
+
clinical_sub = selected_clinical_df.loc[common_ids]
|
| 278 |
+
genes_sub_T = gene_df_norm.loc[:, common_ids].T # samples x genes
|
| 279 |
+
linked_data = pd.concat([clinical_sub, genes_sub_T], axis=1)
|
| 280 |
+
|
| 281 |
+
# 4) Handle missing values
|
| 282 |
+
processed_df = handle_missing_values(linked_data, trait_col=trait)
|
| 283 |
+
|
| 284 |
+
# 5) Determine bias and remove biased demographic features
|
| 285 |
+
is_biased, processed_df = judge_and_remove_biased_features(processed_df, trait)
|
| 286 |
+
|
| 287 |
+
# 6) Final validation and save cohort info
|
| 288 |
+
covariate_cols = [trait, 'Age', 'Gender']
|
| 289 |
+
gene_cols_after = [c for c in processed_df.columns if c not in covariate_cols]
|
| 290 |
+
is_gene_available = len(gene_cols_after) > 0
|
| 291 |
+
is_trait_available = (trait in processed_df.columns) and (len(processed_df) > 0)
|
| 292 |
+
|
| 293 |
+
note = "INFO: age_col and gender_col were None; trait derived from barcode and/or sample_type_id/sample_type. Gene symbols normalized using NCBI synonym map."
|
| 294 |
+
is_usable = validate_and_save_cohort_info(
|
| 295 |
+
is_final=True,
|
| 296 |
+
cohort="TCGA",
|
| 297 |
+
info_path=json_path,
|
| 298 |
+
is_gene_available=is_gene_available,
|
| 299 |
+
is_trait_available=is_trait_available,
|
| 300 |
+
is_biased=is_biased,
|
| 301 |
+
df=processed_df,
|
| 302 |
+
note=note
|
| 303 |
+
)
|
| 304 |
+
|
| 305 |
+
# 7) Save linked data if usable
|
| 306 |
+
if is_usable:
|
| 307 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 308 |
+
processed_df.to_csv(out_data_file)
|
output/preprocess/Ovarian_Cancer/gene_data/GSE201525.csv
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/preprocess/Pancreatic_Cancer/GSE125158.csv
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/preprocess/Pancreatic_Cancer/clinical_data/GSE125158.csv
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
GSM3564350,GSM3564351,GSM3564352,GSM3564353,GSM3564354,GSM3564355,GSM3564356,GSM3564357,GSM3564358,GSM3564359,GSM3564360,GSM3564361,GSM3564362,GSM3564363,GSM3564364,GSM3564365,GSM3564366,GSM3564367,GSM3564368,GSM3564369,GSM3564370,GSM3564371,GSM3564372,GSM3564373,GSM3564374,GSM3564375,GSM3564376,GSM3564377,GSM3564378,GSM3564379
|
| 2 |
-
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
|
| 3 |
-
64.0,76.0,65.0,69.0,70.0,65.0,73.0,64.0,61.0,67.0,64.0,42.0,52.0,69.0,64.0,55.0,80.0,60.0,60.0,78.0,78.0,67.0,72.0,80.0,58.0,65.0,68.0,74.0,71.0,66.0
|
| 4 |
-
1.0,1.0,0.0,1.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,1.0,1.0,1.0
|
|
|
|
| 1 |
+
,GSM3564350,GSM3564351,GSM3564352,GSM3564353,GSM3564354,GSM3564355,GSM3564356,GSM3564357,GSM3564358,GSM3564359,GSM3564360,GSM3564361,GSM3564362,GSM3564363,GSM3564364,GSM3564365,GSM3564366,GSM3564367,GSM3564368,GSM3564369,GSM3564370,GSM3564371,GSM3564372,GSM3564373,GSM3564374,GSM3564375,GSM3564376,GSM3564377,GSM3564378,GSM3564379
|
| 2 |
+
Pancreatic_Cancer,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
|
| 3 |
+
Age,64.0,76.0,65.0,69.0,70.0,65.0,73.0,64.0,61.0,67.0,64.0,42.0,52.0,69.0,64.0,55.0,80.0,60.0,60.0,78.0,78.0,67.0,72.0,80.0,58.0,65.0,68.0,74.0,71.0,66.0
|
| 4 |
+
Gender,1.0,1.0,0.0,1.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,1.0,1.0,1.0
|
output/preprocess/Pancreatic_Cancer/clinical_data/GSE130563.csv
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
,GSM3743555,GSM3743556,GSM3743557,GSM3743558,GSM3743559,GSM3743560,GSM3743561,GSM3743562,GSM3743563,GSM3743564,GSM3743565,GSM3743566,GSM3743567,GSM3743568,GSM3743569,GSM3743570,GSM3743571,GSM3743572,GSM3743573,GSM3743574,GSM3743575,GSM3743576,GSM3743577,GSM3743578,GSM3743579,GSM3743580,GSM3743581,GSM3743582,GSM3743583,GSM3743584,GSM3743585,GSM3743586,GSM3743587,GSM3743588,GSM3743589,GSM3743590,GSM3743591,GSM3743592,GSM3743593,GSM3743594,GSM3743595,GSM3743596,GSM3743597,GSM3743598,GSM3743599,GSM3743600
|
| 2 |
-
Pancreatic_Cancer,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,,,,,,,,
|
| 3 |
Age,33.0,68.0,73.0,49.0,78.0,57.0,55.0,50.0,47.0,63.0,51.0,50.0,69.0,50.0,60.0,68.0,66.0,54.0,64.0,76.0,68.0,73.0,56.0,80.0,68.0,79.0,72.0,52.0,74.0,74.0,55.0,56.0,77.0,70.0,70.0,63.0,59.0,74.0,30.0,51.0,55.0,55.0,45.0,58.0,50.0,54.0
|
| 4 |
Gender,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0,1.0,1.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0
|
|
|
|
| 1 |
,GSM3743555,GSM3743556,GSM3743557,GSM3743558,GSM3743559,GSM3743560,GSM3743561,GSM3743562,GSM3743563,GSM3743564,GSM3743565,GSM3743566,GSM3743567,GSM3743568,GSM3743569,GSM3743570,GSM3743571,GSM3743572,GSM3743573,GSM3743574,GSM3743575,GSM3743576,GSM3743577,GSM3743578,GSM3743579,GSM3743580,GSM3743581,GSM3743582,GSM3743583,GSM3743584,GSM3743585,GSM3743586,GSM3743587,GSM3743588,GSM3743589,GSM3743590,GSM3743591,GSM3743592,GSM3743593,GSM3743594,GSM3743595,GSM3743596,GSM3743597,GSM3743598,GSM3743599,GSM3743600
|
| 2 |
+
Pancreatic_Cancer,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
| 3 |
Age,33.0,68.0,73.0,49.0,78.0,57.0,55.0,50.0,47.0,63.0,51.0,50.0,69.0,50.0,60.0,68.0,66.0,54.0,64.0,76.0,68.0,73.0,56.0,80.0,68.0,79.0,72.0,52.0,74.0,74.0,55.0,56.0,77.0,70.0,70.0,63.0,59.0,74.0,30.0,51.0,55.0,55.0,45.0,58.0,50.0,54.0
|
| 4 |
Gender,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0,1.0,1.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0
|
output/preprocess/Pancreatic_Cancer/clinical_data/GSE131027.csv
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
,
|
| 2 |
-
Pancreatic_Cancer,,0.0,0.0,0.0,0.0
|
|
|
|
| 1 |
+
,GSM3759992,GSM3759993,GSM3759994,GSM3759995,GSM3759996,GSM3759997,GSM3759998,GSM3759999,GSM3760000,GSM3760001,GSM3760002,GSM3760003,GSM3760004,GSM3760005,GSM3760006,GSM3760007,GSM3760008,GSM3760009,GSM3760010,GSM3760011,GSM3760012,GSM3760013,GSM3760014,GSM3760015,GSM3760016,GSM3760017,GSM3760018,GSM3760019,GSM3760020,GSM3760021,GSM3760022,GSM3760023,GSM3760024,GSM3760025,GSM3760026,GSM3760027,GSM3760028,GSM3760029,GSM3760030,GSM3760031,GSM3760032,GSM3760033,GSM3760034,GSM3760035,GSM3760036,GSM3760037,GSM3760038,GSM3760039,GSM3760040,GSM3760041,GSM3760042,GSM3760043,GSM3760044,GSM3760045,GSM3760046,GSM3760047,GSM3760048,GSM3760049,GSM3760050,GSM3760051,GSM3760052,GSM3760053,GSM3760054,GSM3760055,GSM3760056,GSM3760057,GSM3760058,GSM3760059,GSM3760060,GSM3760061,GSM3760062,GSM3760063,GSM3760064,GSM3760065,GSM3760066,GSM3760067,GSM3760068,GSM3760069,GSM3760070,GSM3760071,GSM3760072,GSM3760073,GSM3760074,GSM3760075,GSM3760076,GSM3760077,GSM3760078,GSM3760079,GSM3760080,GSM3760081,GSM3760082,GSM3760083
|
| 2 |
+
Pancreatic_Cancer,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
output/preprocess/Pancreatic_Cancer/clinical_data/GSE236951.csv
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
GSM7587683,GSM7587684,GSM7587685,GSM7587686,GSM7587687,GSM7587688,GSM7587689,GSM7587690,GSM7587691,GSM7587692,GSM7587693,GSM7587694,GSM7587695,GSM7587696,GSM7587697,GSM7587698,GSM7587699,GSM7587700,GSM7587701,GSM7587702,GSM7587703,GSM7587704,GSM7587705,GSM7587706,GSM7587707,GSM7587708,GSM7587709,GSM7587710,GSM7587711,GSM7587712,GSM7587713,GSM7587714,GSM7587715,GSM7587716
|
| 2 |
-
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
| 3 |
-
83.0,64.0,59.0,64.0,72.0,72.0,89.0,59.0,64.0,82.0,75.0,61.0,59.0,68.0,49.0,71.0,68.0,58.0,76.0,67.0,52.0,57.0,72.0,59.0,53.0,95.0,53.0,55.0,43.0,71.0,48.0,43.0,55.0,63.0
|
| 4 |
-
1.0,1.0,1.0,1.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,0.0,1.0,0.0,0.0,1.0,1.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0
|
|
|
|
| 1 |
+
,GSM7587683,GSM7587684,GSM7587685,GSM7587686,GSM7587687,GSM7587688,GSM7587689,GSM7587690,GSM7587691,GSM7587692,GSM7587693,GSM7587694,GSM7587695,GSM7587696,GSM7587697,GSM7587698,GSM7587699,GSM7587700,GSM7587701,GSM7587702,GSM7587703,GSM7587704,GSM7587705,GSM7587706,GSM7587707,GSM7587708,GSM7587709,GSM7587710,GSM7587711,GSM7587712,GSM7587713,GSM7587714,GSM7587715,GSM7587716
|
| 2 |
+
Pancreatic_Cancer,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
| 3 |
+
Age,83.0,64.0,59.0,64.0,72.0,72.0,89.0,59.0,64.0,82.0,75.0,61.0,59.0,68.0,49.0,71.0,68.0,58.0,76.0,67.0,52.0,57.0,72.0,59.0,53.0,95.0,53.0,55.0,43.0,71.0,48.0,43.0,55.0,63.0
|
| 4 |
+
Gender,1.0,1.0,1.0,1.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,0.0,1.0,0.0,0.0,1.0,1.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0
|
output/preprocess/Pancreatic_Cancer/code/GSE120127.py
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE120127"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE120127"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE120127.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE120127.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE120127.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Determine data availability based on provided background and sample characteristics
|
| 40 |
+
is_gene_available = True # Likely gene expression data; excludes pure miRNA/methylation-only datasets
|
| 41 |
+
# Given all samples are "pancreatic cancer cell line" and no controls, trait is constant -> not available
|
| 42 |
+
trait_row = None
|
| 43 |
+
age_row = None # Cell line dataset -> no human age
|
| 44 |
+
gender_row = None # Cell line dataset -> human gender not applicable for our purpose
|
| 45 |
+
|
| 46 |
+
# Converters
|
| 47 |
+
def _extract_value(x):
|
| 48 |
+
if x is None:
|
| 49 |
+
return None
|
| 50 |
+
s = str(x)
|
| 51 |
+
# take the part after the last colon if present
|
| 52 |
+
parts = s.split(':')
|
| 53 |
+
v = parts[-1].strip() if len(parts) >= 2 else s.strip()
|
| 54 |
+
if v == '' or v.lower() in {'na', 'n/a', 'nan', 'none', 'unknown', 'missing'}:
|
| 55 |
+
return None
|
| 56 |
+
return v
|
| 57 |
+
|
| 58 |
+
def convert_trait(x):
|
| 59 |
+
v = _extract_value(x)
|
| 60 |
+
if v is None:
|
| 61 |
+
return None
|
| 62 |
+
v_low = v.strip().strip('"').lower()
|
| 63 |
+
# Map cancer vs normal/control
|
| 64 |
+
positives = {'pancreatic cancer', 'pancreatic cancer cell line', 'cancer', 'tumor', 'tumour', 'pdac'}
|
| 65 |
+
negatives = {'normal', 'healthy', 'control', 'adjacent normal', 'benign', 'non-cancer', 'noncancer'}
|
| 66 |
+
if any(p in v_low for p in positives):
|
| 67 |
+
return 1
|
| 68 |
+
if any(n in v_low for n in negatives):
|
| 69 |
+
return 0
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
def convert_age(x):
|
| 73 |
+
v = _extract_value(x)
|
| 74 |
+
if v is None:
|
| 75 |
+
return None
|
| 76 |
+
# Extract numeric age if present
|
| 77 |
+
import re
|
| 78 |
+
m = re.search(r'(\d+(\.\d+)?)', v)
|
| 79 |
+
if m:
|
| 80 |
+
try:
|
| 81 |
+
return float(m.group(1))
|
| 82 |
+
except Exception:
|
| 83 |
+
return None
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
def convert_gender(x):
|
| 87 |
+
v = _extract_value(x)
|
| 88 |
+
if v is None:
|
| 89 |
+
return None
|
| 90 |
+
v_low = v.strip().strip('"').lower()
|
| 91 |
+
if v_low in {'m', 'male', 'man'}:
|
| 92 |
+
return 1
|
| 93 |
+
if v_low in {'f', 'female', 'woman'}:
|
| 94 |
+
return 0
|
| 95 |
+
return None
|
| 96 |
+
|
| 97 |
+
# Initial filtering metadata save
|
| 98 |
+
is_trait_available = trait_row is not None
|
| 99 |
+
_ = validate_and_save_cohort_info(
|
| 100 |
+
is_final=False,
|
| 101 |
+
cohort=cohort,
|
| 102 |
+
info_path=json_path,
|
| 103 |
+
is_gene_available=is_gene_available,
|
| 104 |
+
is_trait_available=is_trait_available
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# Clinical feature extraction (skip because trait_row is None)
|
| 108 |
+
if trait_row is not None:
|
| 109 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 110 |
+
clinical_df=clinical_data,
|
| 111 |
+
trait=trait,
|
| 112 |
+
trait_row=trait_row,
|
| 113 |
+
convert_trait=convert_trait,
|
| 114 |
+
age_row=age_row,
|
| 115 |
+
convert_age=convert_age,
|
| 116 |
+
gender_row=gender_row,
|
| 117 |
+
convert_gender=convert_gender
|
| 118 |
+
)
|
| 119 |
+
_ = preview_df(selected_clinical_df)
|
| 120 |
+
# Save clinical data
|
| 121 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 122 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 123 |
+
|
| 124 |
+
# Step 3: Gene Data Extraction
|
| 125 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 126 |
+
gene_data = get_genetic_data(matrix_file)
|
| 127 |
+
|
| 128 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 129 |
+
print(gene_data.index[:20])
|
| 130 |
+
|
| 131 |
+
# Step 4: Gene Identifier Review
|
| 132 |
+
print("requires_gene_mapping = True")
|
| 133 |
+
|
| 134 |
+
# Step 5: Gene Annotation
|
| 135 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 136 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 137 |
+
|
| 138 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 139 |
+
print("Gene annotation preview:")
|
| 140 |
+
print(preview_df(gene_annotation))
|
| 141 |
+
|
| 142 |
+
# Step 6: Gene Identifier Mapping
|
| 143 |
+
import os
|
| 144 |
+
|
| 145 |
+
# Keep the original expression data separate
|
| 146 |
+
expr_df = gene_data.copy()
|
| 147 |
+
expr_ids = set(expr_df.index.astype(str).str.strip())
|
| 148 |
+
|
| 149 |
+
# Helper to evaluate overlap for an annotation DataFrame
|
| 150 |
+
def find_best_id_column(ann: pd.DataFrame, expr_ids: set) -> tuple:
|
| 151 |
+
best_col = None
|
| 152 |
+
best_overlap = -1
|
| 153 |
+
# Evaluate all columns as potential ID columns
|
| 154 |
+
for col in ann.columns:
|
| 155 |
+
try:
|
| 156 |
+
col_vals = ann[col].astype(str).str.strip()
|
| 157 |
+
except Exception:
|
| 158 |
+
continue
|
| 159 |
+
overlap = col_vals[col_vals.isin(expr_ids)].shape[0]
|
| 160 |
+
if overlap > best_overlap:
|
| 161 |
+
best_overlap = overlap
|
| 162 |
+
best_col = col
|
| 163 |
+
return best_col, best_overlap
|
| 164 |
+
|
| 165 |
+
def pick_gene_symbol_column(ann: pd.DataFrame) -> str:
|
| 166 |
+
candidates = [
|
| 167 |
+
'Gene Symbol', 'Gene symbol', 'Symbol', 'Gene', 'gene_symbol',
|
| 168 |
+
'Approved Symbol', 'Approved symbol'
|
| 169 |
+
]
|
| 170 |
+
for c in candidates:
|
| 171 |
+
if c in ann.columns:
|
| 172 |
+
return c
|
| 173 |
+
# Fallback: try a heuristic by looking for a column name containing 'symbol'
|
| 174 |
+
for c in ann.columns:
|
| 175 |
+
if 'symbol' in c.lower():
|
| 176 |
+
return c
|
| 177 |
+
return None
|
| 178 |
+
|
| 179 |
+
# Try all available SOFT files to find the one matching the expression IDs
|
| 180 |
+
soft_files = [os.path.join(in_cohort_dir, f) for f in os.listdir(in_cohort_dir) if 'soft' in f.lower()]
|
| 181 |
+
best_mapping = None
|
| 182 |
+
best_info = None
|
| 183 |
+
|
| 184 |
+
for sf in soft_files:
|
| 185 |
+
try:
|
| 186 |
+
ann_df = get_gene_annotation(sf)
|
| 187 |
+
except Exception as e:
|
| 188 |
+
print(f"WARNING: Failed to read annotation from {os.path.basename(sf)}: {e}")
|
| 189 |
+
continue
|
| 190 |
+
|
| 191 |
+
id_col, overlap = find_best_id_column(ann_df, expr_ids)
|
| 192 |
+
gene_col = pick_gene_symbol_column(ann_df)
|
| 193 |
+
|
| 194 |
+
print(f"Checked annotation: {os.path.basename(sf)} | Candidate ID column: {id_col} | Overlap: {overlap} | Gene symbol column: {gene_col}")
|
| 195 |
+
|
| 196 |
+
if (overlap is not None) and (overlap > 0) and (gene_col is not None):
|
| 197 |
+
# Build mapping and apply to a small subset to validate
|
| 198 |
+
try:
|
| 199 |
+
mapping_df = get_gene_mapping(ann_df, prob_col=id_col, gene_col=gene_col)
|
| 200 |
+
# Keep only mapping entries that appear in the expression data
|
| 201 |
+
mapping_in_expr = mapping_df[mapping_df['ID'].isin(expr_df.index)]
|
| 202 |
+
if len(mapping_in_expr) == 0:
|
| 203 |
+
continue
|
| 204 |
+
mapped_df = apply_gene_mapping(expression_df=expr_df, mapping_df=mapping_in_expr)
|
| 205 |
+
if mapped_df.shape[0] > 0:
|
| 206 |
+
# Track the best (highest overlap) mapping
|
| 207 |
+
if (best_mapping is None) or (overlap > best_info['overlap']):
|
| 208 |
+
best_mapping = mapped_df
|
| 209 |
+
best_info = {
|
| 210 |
+
'soft_file': os.path.basename(sf),
|
| 211 |
+
'id_col': id_col,
|
| 212 |
+
'gene_col': gene_col,
|
| 213 |
+
'overlap': overlap,
|
| 214 |
+
'mapped_shape': mapped_df.shape
|
| 215 |
+
}
|
| 216 |
+
except Exception as e:
|
| 217 |
+
print(f"WARNING: Mapping failed for {os.path.basename(sf)} with columns ({id_col}, {gene_col}): {e}")
|
| 218 |
+
continue
|
| 219 |
+
|
| 220 |
+
# Apply the best mapping found; otherwise, fail fast to avoid empty results
|
| 221 |
+
if best_mapping is not None:
|
| 222 |
+
print(f"Using annotation: {best_info['soft_file']}")
|
| 223 |
+
print(f"Chosen ID column: {best_info['id_col']} | Gene symbol column: {best_info['gene_col']} | Overlap: {best_info['overlap']}")
|
| 224 |
+
print(f"Resulting gene_data shape: {best_info['mapped_shape']}")
|
| 225 |
+
gene_data = best_mapping
|
| 226 |
+
else:
|
| 227 |
+
raise RuntimeError(
|
| 228 |
+
f"No usable annotation found to map {len(expr_ids)} expression IDs to gene symbols. "
|
| 229 |
+
f"Checked {len(soft_files)} SOFT files: {[os.path.basename(s) for s in soft_files]}"
|
| 230 |
+
)
|
output/preprocess/Pancreatic_Cancer/code/GSE124069.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE124069"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE124069"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE124069.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE124069.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE124069.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Determine data availability based on provided background and sample characteristics
|
| 40 |
+
is_gene_available = True # Affymetrix microarray of total RNA indicates gene expression data
|
| 41 |
+
trait_row = None # Only "pancreatic cancer" present -> constant across samples
|
| 42 |
+
age_row = None # Cell line experiment, no human age available
|
| 43 |
+
gender_row = None # Cell line experiment, no human gender available
|
| 44 |
+
|
| 45 |
+
# Conversion functions (defined for completeness; not used since rows are None)
|
| 46 |
+
def _extract_value(x):
|
| 47 |
+
if x is None:
|
| 48 |
+
return None
|
| 49 |
+
s = str(x).strip()
|
| 50 |
+
if ':' in s:
|
| 51 |
+
s = s.split(':', 1)[1].strip()
|
| 52 |
+
return s if s not in {'', 'NA', 'NaN', 'nan', 'none', 'null', 'unknown'} else None
|
| 53 |
+
|
| 54 |
+
def convert_trait(x):
|
| 55 |
+
v = _extract_value(x)
|
| 56 |
+
if v is None:
|
| 57 |
+
return None
|
| 58 |
+
v_low = v.lower()
|
| 59 |
+
# Map pancreatic cancer to 1; other/unknown to 0 (not expected here)
|
| 60 |
+
if 'pancreatic' in v_low and 'cancer' in v_low:
|
| 61 |
+
return 1
|
| 62 |
+
return 0
|
| 63 |
+
|
| 64 |
+
def convert_age(x):
|
| 65 |
+
v = _extract_value(x)
|
| 66 |
+
if v is None:
|
| 67 |
+
return None
|
| 68 |
+
# Try to extract a number (e.g., "65", "65 years")
|
| 69 |
+
import re
|
| 70 |
+
m = re.search(r'[-+]?\d*\.?\d+', v)
|
| 71 |
+
if m:
|
| 72 |
+
try:
|
| 73 |
+
return float(m.group())
|
| 74 |
+
except:
|
| 75 |
+
return None
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
def convert_gender(x):
|
| 79 |
+
v = _extract_value(x)
|
| 80 |
+
if v is None:
|
| 81 |
+
return None
|
| 82 |
+
v_low = v.lower()
|
| 83 |
+
if v_low in {'female', 'f'}:
|
| 84 |
+
return 0
|
| 85 |
+
if v_low in {'male', 'm'}:
|
| 86 |
+
return 1
|
| 87 |
+
return None
|
| 88 |
+
|
| 89 |
+
# Initial filtering and metadata saving
|
| 90 |
+
is_trait_available = trait_row is not None
|
| 91 |
+
_ = validate_and_save_cohort_info(
|
| 92 |
+
is_final=False,
|
| 93 |
+
cohort=cohort,
|
| 94 |
+
info_path=json_path,
|
| 95 |
+
is_gene_available=is_gene_available,
|
| 96 |
+
is_trait_available=is_trait_available
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Clinical feature extraction is skipped because trait_row is None (no usable clinical data)
|
| 100 |
+
|
| 101 |
+
# Step 3: Gene Data Extraction
|
| 102 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 103 |
+
gene_data = get_genetic_data(matrix_file)
|
| 104 |
+
|
| 105 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 106 |
+
print(gene_data.index[:20])
|
| 107 |
+
|
| 108 |
+
# Step 4: Gene Identifier Review
|
| 109 |
+
requires_gene_mapping = True
|
| 110 |
+
print(f"\nrequires_gene_mapping = {requires_gene_mapping}")
|
| 111 |
+
|
| 112 |
+
# Step 5: Gene Annotation
|
| 113 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 114 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 115 |
+
|
| 116 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 117 |
+
print("Gene annotation preview:")
|
| 118 |
+
print(preview_df(gene_annotation))
|
| 119 |
+
|
| 120 |
+
# Step 6: Gene Identifier Mapping
|
| 121 |
+
# 1-2. Identify identifier and gene symbol columns and build mapping dataframe
|
| 122 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene Symbol')
|
| 123 |
+
|
| 124 |
+
# 3. Apply mapping to convert probe-level data to gene-level expression
|
| 125 |
+
gene_data = apply_gene_mapping(gene_data, mapping_df)
|
| 126 |
+
|
| 127 |
+
# Step 7: Data Normalization and Linking
|
| 128 |
+
import os
|
| 129 |
+
|
| 130 |
+
# 1. Normalize gene symbols and save gene-level expression
|
| 131 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 132 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 133 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 134 |
+
|
| 135 |
+
# 2. Link clinical and genetic data only if clinical data exists (it doesn't for this cohort)
|
| 136 |
+
linked_data = None
|
| 137 |
+
has_clinical = ('selected_clinical_data' in locals()) and (selected_clinical_data is not None)
|
| 138 |
+
|
| 139 |
+
if has_clinical:
|
| 140 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data)
|
| 141 |
+
# 3. Handle missing values
|
| 142 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 143 |
+
# 4. Bias checks
|
| 144 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 145 |
+
# 5. Final validation with clinical data available
|
| 146 |
+
is_usable = validate_and_save_cohort_info(
|
| 147 |
+
is_final=True,
|
| 148 |
+
cohort=cohort,
|
| 149 |
+
info_path=json_path,
|
| 150 |
+
is_gene_available=True,
|
| 151 |
+
is_trait_available=True,
|
| 152 |
+
is_biased=is_trait_biased,
|
| 153 |
+
df=unbiased_linked_data,
|
| 154 |
+
note="INFO: Clinical data available and processed."
|
| 155 |
+
)
|
| 156 |
+
# 6. Save linked data only if usable
|
| 157 |
+
if is_usable:
|
| 158 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 159 |
+
unbiased_linked_data.to_csv(out_data_file)
|
| 160 |
+
else:
|
| 161 |
+
# No clinical trait available; perform final validation for gene-only dataset
|
| 162 |
+
is_usable = validate_and_save_cohort_info(
|
| 163 |
+
is_final=True,
|
| 164 |
+
cohort=cohort,
|
| 165 |
+
info_path=json_path,
|
| 166 |
+
is_gene_available=True,
|
| 167 |
+
is_trait_available=False,
|
| 168 |
+
is_biased=False,
|
| 169 |
+
df=normalized_gene_data.T, # Use gene-only matrix to avoid abnormality flag
|
| 170 |
+
note="INFO: No clinical trait; cell line study. Gene-only data saved; linked data not generated."
|
| 171 |
+
)
|
output/preprocess/Pancreatic_Cancer/code/GSE125158.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE125158"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE125158"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE125158.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE125158.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE125158.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
import re
|
| 41 |
+
import pandas as pd
|
| 42 |
+
|
| 43 |
+
# 1) Gene expression availability
|
| 44 |
+
# mRNA expression (not miRNA/methylation) -> available
|
| 45 |
+
is_gene_available = True
|
| 46 |
+
|
| 47 |
+
# 2) Variable availability and converters
|
| 48 |
+
|
| 49 |
+
# From the Sample Characteristics Dictionary:
|
| 50 |
+
# 0: diagnosis: pancreatic cancer / healthy -> trait
|
| 51 |
+
# 2: gender: male/female -> gender
|
| 52 |
+
# 3: age: numbers -> age
|
| 53 |
+
trait_row = 0
|
| 54 |
+
gender_row = 2
|
| 55 |
+
age_row = 3
|
| 56 |
+
|
| 57 |
+
def _extract_value(x):
|
| 58 |
+
if pd.isna(x):
|
| 59 |
+
return None
|
| 60 |
+
s = str(x)
|
| 61 |
+
parts = s.split(":", 1)
|
| 62 |
+
v = parts[1] if len(parts) > 1 else parts[0]
|
| 63 |
+
return v.strip()
|
| 64 |
+
|
| 65 |
+
def convert_trait(x):
|
| 66 |
+
v = _extract_value(x)
|
| 67 |
+
if v is None:
|
| 68 |
+
return None
|
| 69 |
+
v = v.lower().replace("_", " ").replace("-", " ")
|
| 70 |
+
v = re.sub(r"\s+", " ", v).strip()
|
| 71 |
+
# Positive (case)
|
| 72 |
+
if "pdac" in v or ("pancreatic" in v and ("cancer" in v or "adenocarcinoma" in v or "carcinoma" in v)):
|
| 73 |
+
return 1
|
| 74 |
+
# Negative (control)
|
| 75 |
+
negatives = {"healthy", "control", "normal", "volunteer"}
|
| 76 |
+
if v in negatives or "healthy" in v or "volunteer" in v or "control" in v or "non cancer" in v or "non-cancer" in v:
|
| 77 |
+
return 0
|
| 78 |
+
return None
|
| 79 |
+
|
| 80 |
+
def convert_age(x):
|
| 81 |
+
v = _extract_value(x)
|
| 82 |
+
if v is None:
|
| 83 |
+
return None
|
| 84 |
+
m = re.search(r"[-+]?\d+(\.\d+)?", v)
|
| 85 |
+
if m:
|
| 86 |
+
try:
|
| 87 |
+
val = float(m.group(0))
|
| 88 |
+
return int(val) if val.is_integer() else val
|
| 89 |
+
except Exception:
|
| 90 |
+
return None
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
def convert_gender(x):
|
| 94 |
+
v = _extract_value(x)
|
| 95 |
+
if v is None:
|
| 96 |
+
return None
|
| 97 |
+
v = v.lower().strip()
|
| 98 |
+
if v in {"male", "m"}:
|
| 99 |
+
return 1
|
| 100 |
+
if v in {"female", "f"}:
|
| 101 |
+
return 0
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
# 3) Save metadata (initial filtering)
|
| 105 |
+
is_trait_available = trait_row is not None
|
| 106 |
+
_ = validate_and_save_cohort_info(
|
| 107 |
+
is_final=False,
|
| 108 |
+
cohort=cohort,
|
| 109 |
+
info_path=json_path,
|
| 110 |
+
is_gene_available=is_gene_available,
|
| 111 |
+
is_trait_available=is_trait_available
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# 4) Clinical feature extraction (only if trait available)
|
| 115 |
+
if trait_row is not None:
|
| 116 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 117 |
+
clinical_df=clinical_data,
|
| 118 |
+
trait=trait,
|
| 119 |
+
trait_row=trait_row,
|
| 120 |
+
convert_trait=convert_trait,
|
| 121 |
+
age_row=age_row,
|
| 122 |
+
convert_age=convert_age,
|
| 123 |
+
gender_row=gender_row,
|
| 124 |
+
convert_gender=convert_gender
|
| 125 |
+
)
|
| 126 |
+
preview = preview_df(selected_clinical_df)
|
| 127 |
+
print(preview)
|
| 128 |
+
|
| 129 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 130 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 131 |
+
|
| 132 |
+
# Step 3: Gene Data Extraction
|
| 133 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 134 |
+
gene_data = get_genetic_data(matrix_file)
|
| 135 |
+
|
| 136 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 137 |
+
print(gene_data.index[:20])
|
| 138 |
+
|
| 139 |
+
# Step 4: Gene Identifier Review
|
| 140 |
+
print("requires_gene_mapping = True")
|
| 141 |
+
|
| 142 |
+
# Step 5: Gene Annotation
|
| 143 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 144 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 145 |
+
|
| 146 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 147 |
+
print("Gene annotation preview:")
|
| 148 |
+
print(preview_df(gene_annotation))
|
| 149 |
+
|
| 150 |
+
# Step 6: Gene Identifier Mapping
|
| 151 |
+
# Determine the appropriate columns for mapping based on the annotation preview:
|
| 152 |
+
# Probe ID column: 'ID' (e.g., 'A_23_P100001')
|
| 153 |
+
# Gene symbol column: 'GENE_SYMBOL' (e.g., 'FAM174B')
|
| 154 |
+
|
| 155 |
+
# 1-2) Build mapping dataframe from annotation
|
| 156 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')
|
| 157 |
+
|
| 158 |
+
# 3) Apply mapping to convert probe-level data to gene-level expression
|
| 159 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 160 |
+
|
| 161 |
+
# Step 7: Data Normalization and Linking
|
| 162 |
+
import os
|
| 163 |
+
|
| 164 |
+
# 1. Normalize gene symbols and save gene expression data
|
| 165 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 166 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 167 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 168 |
+
|
| 169 |
+
# 2. Link clinical and genetic data
|
| 170 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 171 |
+
|
| 172 |
+
# 3. Handle missing values
|
| 173 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 174 |
+
|
| 175 |
+
# 4. Bias evaluation and removal of biased demographics
|
| 176 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 177 |
+
|
| 178 |
+
# 5. Final validation and save cohort info
|
| 179 |
+
gene_cols_after_qc = [c for c in unbiased_linked_data.columns if c not in [trait, 'Age', 'Gender']]
|
| 180 |
+
is_gene_available_rt = (normalized_gene_data.shape[0] > 0) and (normalized_gene_data.shape[1] > 0) and (len(gene_cols_after_qc) > 0)
|
| 181 |
+
is_trait_available_rt = (trait in unbiased_linked_data.columns)
|
| 182 |
+
|
| 183 |
+
note = (
|
| 184 |
+
f"INFO: Genes(before_norm)={gene_data.shape[0]}, Genes(after_norm)={normalized_gene_data.shape[0]}, "
|
| 185 |
+
f"Samples(after_QC)={len(unbiased_linked_data)}, GeneFeatures(after_QC)={len(gene_cols_after_qc)}"
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
is_usable = validate_and_save_cohort_info(
|
| 189 |
+
is_final=True,
|
| 190 |
+
cohort=cohort,
|
| 191 |
+
info_path=json_path,
|
| 192 |
+
is_gene_available=is_gene_available_rt,
|
| 193 |
+
is_trait_available=is_trait_available_rt,
|
| 194 |
+
is_biased=is_trait_biased,
|
| 195 |
+
df=unbiased_linked_data,
|
| 196 |
+
note=note
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
# 6. Save linked data if usable
|
| 200 |
+
if is_usable:
|
| 201 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 202 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Pancreatic_Cancer/code/GSE130563.py
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE130563"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE130563"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE130563.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE130563.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE130563.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
import pandas as pd
|
| 41 |
+
|
| 42 |
+
# 1) Gene expression data availability (microarray expression profiling described in background)
|
| 43 |
+
is_gene_available = True
|
| 44 |
+
|
| 45 |
+
# 2) Variable availability and converters
|
| 46 |
+
# Keys determined from the provided Sample Characteristics Dictionary:
|
| 47 |
+
# - Trait (Pancreatic_Cancer): diagnosis at key 0
|
| 48 |
+
# - Age: key 4
|
| 49 |
+
# - Gender: key 1
|
| 50 |
+
trait_row = 0
|
| 51 |
+
age_row = 4
|
| 52 |
+
gender_row = 1
|
| 53 |
+
|
| 54 |
+
def _get_value_after_colon(x):
|
| 55 |
+
if x is None:
|
| 56 |
+
return None
|
| 57 |
+
s = str(x)
|
| 58 |
+
parts = s.split(":", 1)
|
| 59 |
+
val = parts[1] if len(parts) > 1 else parts[0]
|
| 60 |
+
return val.strip()
|
| 61 |
+
|
| 62 |
+
def convert_trait(x):
|
| 63 |
+
# Binary: 1 = pancreatic cancer (PDAC), 0 = non-cancer diagnoses (benign, pancreatitis, etc.)
|
| 64 |
+
val = _get_value_after_colon(x)
|
| 65 |
+
if val is None:
|
| 66 |
+
return None
|
| 67 |
+
s = val.strip().lower()
|
| 68 |
+
if s in {"", "na", "n/a", "not determined", "n.d.", "n.d. (not determined)", "unknown"}:
|
| 69 |
+
return None
|
| 70 |
+
if "pdac" in s or "pancreatic ductal adenocarcinoma" in s:
|
| 71 |
+
return 1
|
| 72 |
+
if "adenocarcinoma" in s and ("pancreas" in s or "pancreatic" in s):
|
| 73 |
+
return 1
|
| 74 |
+
# All other listed diagnoses (e.g., chronic pancreatitis, benign biliary conditions) -> non-cancer
|
| 75 |
+
return 0
|
| 76 |
+
|
| 77 |
+
def convert_age(x):
|
| 78 |
+
# Continuous age in years
|
| 79 |
+
val = _get_value_after_colon(x)
|
| 80 |
+
if val is None:
|
| 81 |
+
return None
|
| 82 |
+
val = val.strip()
|
| 83 |
+
try:
|
| 84 |
+
return float(val)
|
| 85 |
+
except Exception:
|
| 86 |
+
return None
|
| 87 |
+
|
| 88 |
+
def convert_gender(x):
|
| 89 |
+
# Binary: female -> 0, male -> 1
|
| 90 |
+
val = _get_value_after_colon(x)
|
| 91 |
+
if val is None:
|
| 92 |
+
return None
|
| 93 |
+
s = val.strip().lower()
|
| 94 |
+
if s in {"f", "female"}:
|
| 95 |
+
return 0
|
| 96 |
+
if s in {"m", "male"}:
|
| 97 |
+
return 1
|
| 98 |
+
return None
|
| 99 |
+
|
| 100 |
+
# 3) Save metadata (initial filtering)
|
| 101 |
+
is_trait_available = trait_row is not None
|
| 102 |
+
_ = validate_and_save_cohort_info(
|
| 103 |
+
is_final=False,
|
| 104 |
+
cohort=cohort,
|
| 105 |
+
info_path=json_path,
|
| 106 |
+
is_gene_available=is_gene_available,
|
| 107 |
+
is_trait_available=is_trait_available
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# 4) Clinical feature extraction (only if clinical data available)
|
| 111 |
+
if trait_row is not None:
|
| 112 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 113 |
+
clinical_df=clinical_data,
|
| 114 |
+
trait=trait,
|
| 115 |
+
trait_row=trait_row,
|
| 116 |
+
convert_trait=convert_trait,
|
| 117 |
+
age_row=age_row,
|
| 118 |
+
convert_age=convert_age,
|
| 119 |
+
gender_row=gender_row,
|
| 120 |
+
convert_gender=convert_gender
|
| 121 |
+
)
|
| 122 |
+
clinical_preview = preview_df(selected_clinical_df)
|
| 123 |
+
print(clinical_preview)
|
| 124 |
+
|
| 125 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 126 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 127 |
+
|
| 128 |
+
# Step 3: Gene Data Extraction
|
| 129 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 130 |
+
gene_data = get_genetic_data(matrix_file)
|
| 131 |
+
|
| 132 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 133 |
+
print(gene_data.index[:20])
|
| 134 |
+
|
| 135 |
+
# Step 4: Gene Identifier Review
|
| 136 |
+
requires_gene_mapping = True
|
| 137 |
+
print("requires_gene_mapping = True")
|
| 138 |
+
|
| 139 |
+
# Step 5: Gene Annotation
|
| 140 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 141 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 142 |
+
|
| 143 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 144 |
+
print("Gene annotation preview:")
|
| 145 |
+
print(preview_df(gene_annotation))
|
| 146 |
+
|
| 147 |
+
# Step 6: Gene Identifier Mapping
|
| 148 |
+
import gzip
|
| 149 |
+
import io
|
| 150 |
+
import pandas as pd
|
| 151 |
+
import re
|
| 152 |
+
|
| 153 |
+
# Preserve original probe-level expression matrix
|
| 154 |
+
probe_level_df = gene_data
|
| 155 |
+
|
| 156 |
+
# Parse the first platform table from the SOFT file between !platform_table_begin and !platform_table_end
|
| 157 |
+
platform_lines = []
|
| 158 |
+
in_table = False
|
| 159 |
+
with gzip.open(soft_file, 'rt') as fh:
|
| 160 |
+
for line in fh:
|
| 161 |
+
line = line.rstrip('\n')
|
| 162 |
+
if line.startswith('!platform_table_begin'):
|
| 163 |
+
in_table = True
|
| 164 |
+
platform_lines = []
|
| 165 |
+
continue
|
| 166 |
+
if line.startswith('!platform_table_end') and in_table:
|
| 167 |
+
in_table = False
|
| 168 |
+
break
|
| 169 |
+
if in_table:
|
| 170 |
+
platform_lines.append(line)
|
| 171 |
+
|
| 172 |
+
if not platform_lines:
|
| 173 |
+
raise RuntimeError("Failed to locate a platform table in the SOFT file.")
|
| 174 |
+
|
| 175 |
+
platform_text = "\n".join(platform_lines)
|
| 176 |
+
platform_df = pd.read_csv(io.StringIO(platform_text), sep='\t', dtype=str, low_memory=False)
|
| 177 |
+
|
| 178 |
+
# Normalize column names to help matching
|
| 179 |
+
def normalize_colname(c):
|
| 180 |
+
return re.sub(r'[\s\-]+', '_', c.strip().lower())
|
| 181 |
+
|
| 182 |
+
colmap = {normalize_colname(c): c for c in platform_df.columns}
|
| 183 |
+
|
| 184 |
+
# Identify probe ID column
|
| 185 |
+
id_col_options = ['id', 'id_ref', 'probe_id', 'probe_set_id', 'probeset_id']
|
| 186 |
+
id_col = None
|
| 187 |
+
for key in id_col_options:
|
| 188 |
+
if key in colmap:
|
| 189 |
+
id_col = colmap[key]
|
| 190 |
+
break
|
| 191 |
+
if id_col is None and 'ID' in platform_df.columns:
|
| 192 |
+
id_col = 'ID'
|
| 193 |
+
if id_col is None:
|
| 194 |
+
# As a last resort, if the gene_annotation from earlier exists and has 'ID', use that
|
| 195 |
+
if 'ID' in gene_annotation.columns:
|
| 196 |
+
id_col = 'ID'
|
| 197 |
+
else:
|
| 198 |
+
raise RuntimeError("Could not determine probe ID column in platform table.")
|
| 199 |
+
|
| 200 |
+
# Identify gene symbol column, prefer "Gene Symbol"-like columns
|
| 201 |
+
symbol_col = None
|
| 202 |
+
symbol_like_options = [
|
| 203 |
+
'gene_symbol', 'symbol', 'genesymbol', 'official_symbol'
|
| 204 |
+
]
|
| 205 |
+
for key in symbol_like_options:
|
| 206 |
+
if key in colmap:
|
| 207 |
+
symbol_col = colmap[key]
|
| 208 |
+
break
|
| 209 |
+
|
| 210 |
+
# If no direct gene symbol column, try gene assignment-like column
|
| 211 |
+
assignment_col = None
|
| 212 |
+
assignment_like_options = ['gene_assignment', 'gene_assignments', 'geneannotation', 'gene_annotation']
|
| 213 |
+
if symbol_col is None:
|
| 214 |
+
for key in assignment_like_options:
|
| 215 |
+
if key in colmap:
|
| 216 |
+
assignment_col = colmap[key]
|
| 217 |
+
break
|
| 218 |
+
|
| 219 |
+
# Fall back to any column that appears to contain symbols by coverage heuristic
|
| 220 |
+
def column_symbol_coverage(series, max_rows=2000):
|
| 221 |
+
sample = series.astype(str).head(max_rows)
|
| 222 |
+
extracted = sample.map(extract_human_gene_symbols)
|
| 223 |
+
non_empty = extracted.map(lambda x: isinstance(x, list) and len(x) > 0)
|
| 224 |
+
coverage = float(non_empty.mean())
|
| 225 |
+
unique_syms = set()
|
| 226 |
+
for lst in extracted[non_empty]:
|
| 227 |
+
unique_syms.update(lst)
|
| 228 |
+
return coverage, len(unique_syms)
|
| 229 |
+
|
| 230 |
+
if symbol_col is None and assignment_col is None:
|
| 231 |
+
best_col = None
|
| 232 |
+
best_score = (-1.0, -1)
|
| 233 |
+
for c in platform_df.columns:
|
| 234 |
+
if c == id_col:
|
| 235 |
+
continue
|
| 236 |
+
try:
|
| 237 |
+
cov, uniq = column_symbol_coverage(platform_df[c])
|
| 238 |
+
except Exception:
|
| 239 |
+
continue
|
| 240 |
+
if (cov > best_score[0]) or (cov == best_score[0] and uniq > best_score[1]):
|
| 241 |
+
best_col = c
|
| 242 |
+
best_score = (cov, uniq)
|
| 243 |
+
# If the best column has reasonable coverage, use it as symbol_col; otherwise keep None
|
| 244 |
+
if best_col is not None and best_score[0] > 0:
|
| 245 |
+
symbol_col = best_col
|
| 246 |
+
|
| 247 |
+
# Build mapping dataframe
|
| 248 |
+
if symbol_col is not None:
|
| 249 |
+
chosen_gene_col = symbol_col
|
| 250 |
+
mapping_df = get_gene_mapping(platform_df, prob_col=id_col, gene_col=chosen_gene_col)
|
| 251 |
+
else:
|
| 252 |
+
# Use assignment_col and rely on extract_human_gene_symbols to parse symbols
|
| 253 |
+
if assignment_col is None:
|
| 254 |
+
raise RuntimeError("Failed to identify a suitable gene symbol column from platform annotation.")
|
| 255 |
+
chosen_gene_col = assignment_col
|
| 256 |
+
mapping_df = get_gene_mapping(platform_df, prob_col=id_col, gene_col=chosen_gene_col)
|
| 257 |
+
|
| 258 |
+
# Apply mapping to convert probe-level data to gene-level expression
|
| 259 |
+
mapped_df = apply_gene_mapping(expression_df=probe_level_df, mapping_df=mapping_df)
|
| 260 |
+
|
| 261 |
+
# Ensure we obtained a non-empty gene-level matrix
|
| 262 |
+
if not isinstance(mapped_df, pd.DataFrame) or mapped_df.shape[0] == 0 or mapped_df.shape[1] == 0:
|
| 263 |
+
raise RuntimeError(f"Gene mapping produced an empty matrix using column '{chosen_gene_col}'. Please inspect platform annotation.")
|
| 264 |
+
|
| 265 |
+
gene_data = mapped_df
|
| 266 |
+
print(f"Gene mapping succeeded. Probe ID column: '{id_col}'. Gene annotation column: '{chosen_gene_col}'.")
|
| 267 |
+
print(f"Mapped genes: {gene_data.shape[0]}, samples: {gene_data.shape[1]}")
|
| 268 |
+
|
| 269 |
+
# Step 7: Gene Annotation
|
| 270 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 271 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 272 |
+
|
| 273 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 274 |
+
print("Gene annotation preview:")
|
| 275 |
+
print(preview_df(gene_annotation))
|
| 276 |
+
|
| 277 |
+
# Step 8: Gene Identifier Mapping
|
| 278 |
+
import gzip
|
| 279 |
+
import io
|
| 280 |
+
import re
|
| 281 |
+
import pandas as pd
|
| 282 |
+
|
| 283 |
+
# Reload the original probe-level expression matrix to avoid prior empty state
|
| 284 |
+
probe_level_df = get_genetic_data(matrix_file)
|
| 285 |
+
|
| 286 |
+
# 1) Parse the platform table from the SOFT file (!platform_table_begin ... !platform_table_end)
|
| 287 |
+
platform_lines = []
|
| 288 |
+
in_table = False
|
| 289 |
+
with gzip.open(soft_file, 'rt') as fh:
|
| 290 |
+
for line in fh:
|
| 291 |
+
line = line.rstrip('\n')
|
| 292 |
+
if line.startswith('!platform_table_begin'):
|
| 293 |
+
in_table = True
|
| 294 |
+
platform_lines = []
|
| 295 |
+
continue
|
| 296 |
+
if line.startswith('!platform_table_end') and in_table:
|
| 297 |
+
in_table = False
|
| 298 |
+
break
|
| 299 |
+
if in_table:
|
| 300 |
+
platform_lines.append(line)
|
| 301 |
+
|
| 302 |
+
platform_df = None
|
| 303 |
+
if platform_lines:
|
| 304 |
+
platform_text = "\n".join(platform_lines)
|
| 305 |
+
platform_df = pd.read_csv(io.StringIO(platform_text), sep='\t', dtype=str, low_memory=False)
|
| 306 |
+
|
| 307 |
+
def _norm(c: str) -> str:
|
| 308 |
+
return re.sub(r'[^a-z0-9]+', '_', c.strip().lower())
|
| 309 |
+
|
| 310 |
+
def _symbol_coverage(series: pd.Series, max_rows: int = 5000):
|
| 311 |
+
sample = series.dropna().astype(str).head(max_rows)
|
| 312 |
+
if sample.empty:
|
| 313 |
+
return 0.0, 0
|
| 314 |
+
extracted = sample.map(extract_human_gene_symbols)
|
| 315 |
+
non_empty = extracted.map(lambda x: isinstance(x, list) and len(x) > 0)
|
| 316 |
+
coverage = float(non_empty.mean()) if len(non_empty) > 0 else 0.0
|
| 317 |
+
unique_syms = set()
|
| 318 |
+
for lst in extracted[non_empty]:
|
| 319 |
+
unique_syms.update(lst)
|
| 320 |
+
return coverage, len(unique_syms)
|
| 321 |
+
|
| 322 |
+
# 2) Identify probe ID column and potential gene symbol column
|
| 323 |
+
id_col = None
|
| 324 |
+
sym_col = None
|
| 325 |
+
if platform_df is not None and platform_df.shape[1] > 0:
|
| 326 |
+
norm_to_orig = {_norm(c): c for c in platform_df.columns}
|
| 327 |
+
# ID column
|
| 328 |
+
for key in ['id', 'id_ref', 'probe_id', 'probeset_id', 'probe_set_id']:
|
| 329 |
+
if key in norm_to_orig:
|
| 330 |
+
id_col = norm_to_orig[key]
|
| 331 |
+
break
|
| 332 |
+
if id_col is None:
|
| 333 |
+
id_col = 'ID' if 'ID' in platform_df.columns else platform_df.columns[0]
|
| 334 |
+
|
| 335 |
+
# Try common symbol-like columns
|
| 336 |
+
for key in ['gene_symbol', 'symbol', 'genesymbol', 'official_symbol', 'hgnc_symbol']:
|
| 337 |
+
if key in norm_to_orig:
|
| 338 |
+
sym_col = norm_to_orig[key]
|
| 339 |
+
break
|
| 340 |
+
# If still none, try assignment-like columns
|
| 341 |
+
if sym_col is None:
|
| 342 |
+
for key in ['gene_assignment', 'gene_assignments', 'gene_annotation', 'gene_annot', 'gene_info']:
|
| 343 |
+
if key in norm_to_orig:
|
| 344 |
+
sym_col = norm_to_orig[key]
|
| 345 |
+
break
|
| 346 |
+
# If still none, try heuristic: any column containing "symbol"
|
| 347 |
+
if sym_col is None:
|
| 348 |
+
for c in platform_df.columns:
|
| 349 |
+
if 'symbol' in c.lower():
|
| 350 |
+
sym_col = c
|
| 351 |
+
break
|
| 352 |
+
|
| 353 |
+
# 3) Build mapping dataframe if a symbol-like column exists and intersects with probes
|
| 354 |
+
mapping_df = None
|
| 355 |
+
mapping_used = False
|
| 356 |
+
if platform_df is not None and sym_col is not None:
|
| 357 |
+
try:
|
| 358 |
+
mapping_df = get_gene_mapping(annotation=platform_df, prob_col=id_col, gene_col=sym_col)
|
| 359 |
+
mapping_df = mapping_df[mapping_df['ID'].isin(probe_level_df.index)]
|
| 360 |
+
if not mapping_df.empty:
|
| 361 |
+
mapped_df = apply_gene_mapping(expression_df=probe_level_df, mapping_df=mapping_df)
|
| 362 |
+
if isinstance(mapped_df, pd.DataFrame) and mapped_df.shape[0] > 0 and mapped_df.shape[1] > 0:
|
| 363 |
+
gene_data = mapped_df
|
| 364 |
+
mapping_used = True
|
| 365 |
+
print(f"Gene mapping succeeded. Probe ID column: '{id_col}'. Gene annotation column: '{sym_col}'.")
|
| 366 |
+
print(f"Mapped genes: {gene_data.shape[0]}, samples: {gene_data.shape[1]}")
|
| 367 |
+
else:
|
| 368 |
+
mapping_df = None
|
| 369 |
+
except Exception as e:
|
| 370 |
+
print(f"Mapping attempt failed with error: {e}")
|
| 371 |
+
mapping_df = None
|
| 372 |
+
|
| 373 |
+
# 4) Optional heuristic: attempt to parse ORF column if present and mapping not used
|
| 374 |
+
orf_coverage = None
|
| 375 |
+
if not mapping_used and platform_df is not None and 'ORF' in platform_df.columns:
|
| 376 |
+
orf_coverage, orf_unique = _symbol_coverage(platform_df['ORF'])
|
| 377 |
+
# Only attempt if coverage suggests there are real gene symbols; otherwise skip
|
| 378 |
+
if orf_coverage and orf_coverage > 0.2:
|
| 379 |
+
try:
|
| 380 |
+
mapping_df = get_gene_mapping(annotation=platform_df, prob_col=id_col, gene_col='ORF')
|
| 381 |
+
mapping_df = mapping_df[mapping_df['ID'].isin(probe_level_df.index)]
|
| 382 |
+
if not mapping_df.empty:
|
| 383 |
+
mapped_df = apply_gene_mapping(expression_df=probe_level_df, mapping_df=mapping_df)
|
| 384 |
+
if isinstance(mapped_df, pd.DataFrame) and mapped_df.shape[0] > 0 and mapped_df.shape[1] > 0:
|
| 385 |
+
gene_data = mapped_df
|
| 386 |
+
mapping_used = True
|
| 387 |
+
print(f"Heuristic mapping using ORF column succeeded. Coverage={orf_coverage:.2%}.")
|
| 388 |
+
print(f"Mapped genes: {gene_data.shape[0]}, samples: {gene_data.shape[1]}")
|
| 389 |
+
except Exception as e:
|
| 390 |
+
print(f"Heuristic ORF mapping attempt failed with error: {e}")
|
| 391 |
+
|
| 392 |
+
# 5) Robust fallback: keep probe-level expression if no valid symbol mapping
|
| 393 |
+
if not mapping_used:
|
| 394 |
+
gene_data = probe_level_df.copy()
|
| 395 |
+
print("No usable gene symbol column found in platform annotation; proceeding with probe-level expression.")
|
| 396 |
+
if platform_df is not None:
|
| 397 |
+
print("Platform columns:", list(platform_df.columns))
|
| 398 |
+
if 'ORF' in platform_df.columns and orf_coverage is None:
|
| 399 |
+
orf_coverage, _ = _symbol_coverage(platform_df['ORF'])
|
| 400 |
+
if 'ORF' in platform_df.columns:
|
| 401 |
+
print(f"ORF column gene-symbol coverage estimate: {orf_coverage:.2%}" if orf_coverage is not None else "ORF coverage not computed.")
|
| 402 |
+
# Show a small sample for diagnostics
|
| 403 |
+
try:
|
| 404 |
+
print("Sample of platform ID/ORF (first 5 rows):", platform_df[[id_col, 'ORF']].head(5).to_dict(orient='list'))
|
| 405 |
+
except Exception:
|
| 406 |
+
pass
|
| 407 |
+
print(f"Retained probe-level matrix. Probes: {gene_data.shape[0]}, samples: {gene_data.shape[1]}")
|
| 408 |
+
|
| 409 |
+
# Step 9: Data Normalization and Linking
|
| 410 |
+
import os
|
| 411 |
+
|
| 412 |
+
# 1. Normalize gene symbols; if normalization drops everything (probe IDs), fall back to probe-level data
|
| 413 |
+
try:
|
| 414 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data.copy())
|
| 415 |
+
except Exception as e:
|
| 416 |
+
# If normalization fails unexpectedly, fall back to probe-level
|
| 417 |
+
normalized_gene_data = gene_data.copy()
|
| 418 |
+
|
| 419 |
+
used_probe_level = False
|
| 420 |
+
gene_matrix_to_use = normalized_gene_data
|
| 421 |
+
if gene_matrix_to_use.shape[0] == 0:
|
| 422 |
+
# Normalization produced empty matrix because IDs are probes; retain probe-level data
|
| 423 |
+
gene_matrix_to_use = gene_data.copy()
|
| 424 |
+
used_probe_level = True
|
| 425 |
+
|
| 426 |
+
# Ensure output directory exists before saving gene data
|
| 427 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 428 |
+
gene_matrix_to_use.to_csv(out_gene_data_file)
|
| 429 |
+
|
| 430 |
+
# 2. Link the clinical and genetic data
|
| 431 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, gene_matrix_to_use)
|
| 432 |
+
|
| 433 |
+
# 3. Handle missing values
|
| 434 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 435 |
+
|
| 436 |
+
# 4. Bias checks with safety for empty/degenerate dataframes
|
| 437 |
+
if linked_data.shape[0] == 0 or linked_data.shape[1] <= 4:
|
| 438 |
+
# Degenerate after filtering/imputation; mark as biased/unusable
|
| 439 |
+
is_trait_biased = True
|
| 440 |
+
unbiased_linked_data = linked_data
|
| 441 |
+
else:
|
| 442 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 443 |
+
|
| 444 |
+
# 5. Final validation and save cohort info
|
| 445 |
+
note_parts = []
|
| 446 |
+
if used_probe_level:
|
| 447 |
+
note_parts.append("WARNING: Gene symbol normalization produced empty matrix; retained probe-level expression (Affymetrix probes).")
|
| 448 |
+
note_parts.append(f"INFO: Gene matrix used has {gene_matrix_to_use.shape[0]} features and {gene_matrix_to_use.shape[1]} samples before linking.")
|
| 449 |
+
note = " ".join(note_parts)
|
| 450 |
+
|
| 451 |
+
is_usable = validate_and_save_cohort_info(
|
| 452 |
+
is_final=True,
|
| 453 |
+
cohort=cohort,
|
| 454 |
+
info_path=json_path,
|
| 455 |
+
is_gene_available=True,
|
| 456 |
+
is_trait_available=True,
|
| 457 |
+
is_biased=is_trait_biased,
|
| 458 |
+
df=unbiased_linked_data,
|
| 459 |
+
note=note
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
# 6. Save linked data if usable
|
| 463 |
+
if is_usable:
|
| 464 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 465 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Pancreatic_Cancer/code/GSE131027.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE131027"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE131027"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE131027.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE131027.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE131027.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
import math
|
| 41 |
+
import pandas as pd
|
| 42 |
+
|
| 43 |
+
# 1) Gene expression data availability (based on series description: expression features investigation)
|
| 44 |
+
is_gene_available = True
|
| 45 |
+
|
| 46 |
+
# 2) Variable availability and converters
|
| 47 |
+
# Trait: Pancreatic Cancer present under key 1 (cancer type), binary
|
| 48 |
+
trait_row = 1
|
| 49 |
+
|
| 50 |
+
def convert_trait(x):
|
| 51 |
+
if x is None or (isinstance(x, float) and math.isnan(x)):
|
| 52 |
+
return None
|
| 53 |
+
s = str(x)
|
| 54 |
+
# Extract value after the last colon to be robust to multiple colons
|
| 55 |
+
val = s.split(':')[-1].strip().lower()
|
| 56 |
+
if val in {'', 'na', 'n/a', 'unknown'}:
|
| 57 |
+
return None
|
| 58 |
+
return 1 if ('pancreatic' in val or 'pancreas' in val) else 0
|
| 59 |
+
|
| 60 |
+
# Age: not available in the provided characteristics
|
| 61 |
+
age_row = None
|
| 62 |
+
|
| 63 |
+
def convert_age(x):
|
| 64 |
+
if x is None or (isinstance(x, float) and math.isnan(x)):
|
| 65 |
+
return None
|
| 66 |
+
s = str(x).split(':')[-1]
|
| 67 |
+
# Extract first numeric token
|
| 68 |
+
import re
|
| 69 |
+
m = re.search(r'[-+]?\d*\.?\d+', s)
|
| 70 |
+
if not m:
|
| 71 |
+
return None
|
| 72 |
+
try:
|
| 73 |
+
return float(m.group(0))
|
| 74 |
+
except Exception:
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
# Gender: not available in the provided characteristics
|
| 78 |
+
gender_row = None
|
| 79 |
+
|
| 80 |
+
def convert_gender(x):
|
| 81 |
+
if x is None or (isinstance(x, float) and math.isnan(x)):
|
| 82 |
+
return None
|
| 83 |
+
val = str(x).split(':')[-1].strip().lower()
|
| 84 |
+
if val in {'', 'na', 'n/a', 'unknown'}:
|
| 85 |
+
return None
|
| 86 |
+
if val in {'female', 'f', 'woman', 'women'}:
|
| 87 |
+
return 0
|
| 88 |
+
if val in {'male', 'm', 'man', 'men'}:
|
| 89 |
+
return 1
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
# 3) Save initial metadata
|
| 93 |
+
is_trait_available = trait_row is not None
|
| 94 |
+
_ = validate_and_save_cohort_info(
|
| 95 |
+
is_final=False,
|
| 96 |
+
cohort=cohort,
|
| 97 |
+
info_path=json_path,
|
| 98 |
+
is_gene_available=is_gene_available,
|
| 99 |
+
is_trait_available=is_trait_available
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# 4) Clinical Feature Extraction (only if trait is available)
|
| 103 |
+
if trait_row is not None:
|
| 104 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 105 |
+
clinical_df=clinical_data,
|
| 106 |
+
trait=trait,
|
| 107 |
+
trait_row=trait_row,
|
| 108 |
+
convert_trait=convert_trait,
|
| 109 |
+
age_row=age_row,
|
| 110 |
+
convert_age=convert_age,
|
| 111 |
+
gender_row=gender_row,
|
| 112 |
+
convert_gender=convert_gender
|
| 113 |
+
)
|
| 114 |
+
# Preview
|
| 115 |
+
preview = preview_df(selected_clinical_df)
|
| 116 |
+
print(preview)
|
| 117 |
+
|
| 118 |
+
# Save clinical data
|
| 119 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 120 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 121 |
+
|
| 122 |
+
# Step 3: Gene Data Extraction
|
| 123 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 124 |
+
gene_data = get_genetic_data(matrix_file)
|
| 125 |
+
|
| 126 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 127 |
+
print(gene_data.index[:20])
|
| 128 |
+
|
| 129 |
+
# Step 4: Gene Identifier Review
|
| 130 |
+
requires_gene_mapping = True
|
| 131 |
+
print(f"requires_gene_mapping = {requires_gene_mapping}")
|
| 132 |
+
|
| 133 |
+
# Step 5: Gene Annotation
|
| 134 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 135 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 136 |
+
|
| 137 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 138 |
+
print("Gene annotation preview:")
|
| 139 |
+
print(preview_df(gene_annotation))
|
| 140 |
+
|
| 141 |
+
# Step 6: Gene Identifier Mapping
|
| 142 |
+
# Ensure required objects are available
|
| 143 |
+
if 'gene_annotation' not in locals():
|
| 144 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 145 |
+
if 'gene_data' not in locals():
|
| 146 |
+
gene_data = get_genetic_data(matrix_file)
|
| 147 |
+
|
| 148 |
+
# 1-2. Decide columns and build the mapping dataframe
|
| 149 |
+
probe_col = 'ID' # matches probe IDs like '1007_s_at'
|
| 150 |
+
gene_symbol_col = 'Gene Symbol' # contains gene symbols
|
| 151 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=gene_symbol_col)
|
| 152 |
+
|
| 153 |
+
# 3. Apply mapping to convert probe-level to gene-level expression
|
| 154 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 155 |
+
|
| 156 |
+
# Step 7: Data Normalization and Linking
|
| 157 |
+
import os
|
| 158 |
+
import pandas as pd
|
| 159 |
+
|
| 160 |
+
# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.
|
| 161 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 162 |
+
|
| 163 |
+
# Ensure output directory exists before saving gene data
|
| 164 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 165 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 166 |
+
|
| 167 |
+
# Ensure clinical data is available in memory; if not, load it
|
| 168 |
+
if 'selected_clinical_df' not in locals():
|
| 169 |
+
selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)
|
| 170 |
+
|
| 171 |
+
# 2. Link the clinical and genetic data with the 'geo_link_clinical_genetic_data' function from the library.
|
| 172 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 173 |
+
|
| 174 |
+
# Compute availability flags for final validation
|
| 175 |
+
is_gene_available_flag = (normalized_gene_data.shape[0] > 0)
|
| 176 |
+
is_trait_available_flag = (trait in linked_data.columns)
|
| 177 |
+
|
| 178 |
+
# 3. Handle missing values in the linked data
|
| 179 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 180 |
+
|
| 181 |
+
# 4. Determine whether the trait and some demographic features are severely biased, and remove biased features.
|
| 182 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 183 |
+
|
| 184 |
+
# 5. Conduct quality check and save the cohort information.
|
| 185 |
+
note = "INFO: Affymetrix probe IDs mapped to gene symbols via platform annotation; symbols normalized using NCBI synonyms."
|
| 186 |
+
is_usable = validate_and_save_cohort_info(
|
| 187 |
+
is_final=True,
|
| 188 |
+
cohort=cohort,
|
| 189 |
+
info_path=json_path,
|
| 190 |
+
is_gene_available=is_gene_available_flag,
|
| 191 |
+
is_trait_available=is_trait_available_flag,
|
| 192 |
+
is_biased=is_trait_biased,
|
| 193 |
+
df=unbiased_linked_data,
|
| 194 |
+
note=note
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
# 6. If the linked data is usable, save it as a CSV file to 'out_data_file'.
|
| 198 |
+
if is_usable:
|
| 199 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 200 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Pancreatic_Cancer/code/GSE157494.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE157494"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE157494"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE157494.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE157494.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE157494.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Determine data availability based on background information
|
| 40 |
+
is_gene_available = True # Affymetrix HG-U133 Plus 2.0 gene expression profiling is present
|
| 41 |
+
|
| 42 |
+
# Variable availability from the Sample Characteristics Dictionary
|
| 43 |
+
# Only 'sample type' is available with values 'xenografted tumor' and 'Cell line', and all samples are pancreatic cancer.
|
| 44 |
+
trait_row = None # Pancreatic_Cancer status is constant (all cases), thus not usable
|
| 45 |
+
age_row = None # No age information available
|
| 46 |
+
gender_row = None # No gender information available
|
| 47 |
+
|
| 48 |
+
# Conversion functions
|
| 49 |
+
def _extract_value(x):
|
| 50 |
+
if x is None:
|
| 51 |
+
return None
|
| 52 |
+
if isinstance(x, str):
|
| 53 |
+
parts = x.split(":", 1)
|
| 54 |
+
return parts[1].strip() if len(parts) == 2 else x.strip()
|
| 55 |
+
return x
|
| 56 |
+
|
| 57 |
+
def convert_trait(x):
|
| 58 |
+
v = _extract_value(x)
|
| 59 |
+
if v is None:
|
| 60 |
+
return None
|
| 61 |
+
# Normalize
|
| 62 |
+
s = str(v).strip().lower()
|
| 63 |
+
# Direct numeric inputs
|
| 64 |
+
if s in {"1", "case", "tumor", "cancer", "pdac"}:
|
| 65 |
+
return 1
|
| 66 |
+
if s in {"0", "control", "normal", "healthy", "adjacent normal"}:
|
| 67 |
+
return 0
|
| 68 |
+
# Heuristic mapping
|
| 69 |
+
if any(k in s for k in ["pancrea", "pdac", "adenocarcinoma", "tumor", "cancer", "xenograft", "pdx", "cdx"]):
|
| 70 |
+
return 1
|
| 71 |
+
if any(k in s for k in ["normal", "healthy", "control"]):
|
| 72 |
+
return 0
|
| 73 |
+
return None
|
| 74 |
+
|
| 75 |
+
def convert_age(x):
|
| 76 |
+
v = _extract_value(x)
|
| 77 |
+
if v is None:
|
| 78 |
+
return None
|
| 79 |
+
import re
|
| 80 |
+
s = str(v).lower()
|
| 81 |
+
m = re.search(r"(-?\d+(\.\d+)?)", s)
|
| 82 |
+
if not m:
|
| 83 |
+
return None
|
| 84 |
+
try:
|
| 85 |
+
age = float(m.group(1))
|
| 86 |
+
# Keep plausible human ages
|
| 87 |
+
if 0 <= age <= 120:
|
| 88 |
+
return age
|
| 89 |
+
except Exception:
|
| 90 |
+
pass
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
def convert_gender(x):
|
| 94 |
+
v = _extract_value(x)
|
| 95 |
+
if v is None:
|
| 96 |
+
return None
|
| 97 |
+
s = str(v).strip().lower()
|
| 98 |
+
if s in {"f", "female", "woman", "women"}:
|
| 99 |
+
return 0
|
| 100 |
+
if s in {"m", "male", "man", "men"}:
|
| 101 |
+
return 1
|
| 102 |
+
# Heuristic: terms like "boy"/"girl" rarely appear, but map if present
|
| 103 |
+
if "female" in s or "girl" in s:
|
| 104 |
+
return 0
|
| 105 |
+
if "male" in s or "boy" in s:
|
| 106 |
+
return 1
|
| 107 |
+
return None
|
| 108 |
+
|
| 109 |
+
# Initial filtering and save metadata
|
| 110 |
+
is_trait_available = trait_row is not None
|
| 111 |
+
_ = validate_and_save_cohort_info(
|
| 112 |
+
is_final=False,
|
| 113 |
+
cohort=cohort,
|
| 114 |
+
info_path=json_path,
|
| 115 |
+
is_gene_available=is_gene_available,
|
| 116 |
+
is_trait_available=is_trait_available
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Clinical feature extraction is skipped because trait_row is None
|
| 120 |
+
# If trait_row becomes available in future steps, use geo_select_clinical_features accordingly.
|
output/preprocess/Pancreatic_Cancer/code/GSE183795.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE183795"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE183795"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE183795.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE183795.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE183795.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
import pandas as pd
|
| 41 |
+
|
| 42 |
+
# 1) Gene expression availability (Affymetrix microarray gene-expression -> True)
|
| 43 |
+
is_gene_available = True
|
| 44 |
+
|
| 45 |
+
# 2) Variable availability and conversion functions
|
| 46 |
+
|
| 47 |
+
# Trait: Use tissue information (row 0): Tumor = 1, adjacent non-tumor/Normal = 0
|
| 48 |
+
trait_row = 0
|
| 49 |
+
age_row = None
|
| 50 |
+
gender_row = None
|
| 51 |
+
|
| 52 |
+
def _after_colon(x):
|
| 53 |
+
if x is None:
|
| 54 |
+
return None
|
| 55 |
+
s = str(x)
|
| 56 |
+
parts = s.split(":", 1)
|
| 57 |
+
return parts[1].strip() if len(parts) > 1 else s.strip()
|
| 58 |
+
|
| 59 |
+
def convert_trait(x):
|
| 60 |
+
v = _after_colon(x)
|
| 61 |
+
if v is None:
|
| 62 |
+
return None
|
| 63 |
+
v_low = v.lower()
|
| 64 |
+
# Map controls first to avoid "non-tumor" being matched by "tumor"
|
| 65 |
+
if ("adjacent" in v_low) or ("non-tumor" in v_low) or ("normal" in v_low):
|
| 66 |
+
return 0
|
| 67 |
+
if "tumor" in v_low or "tumour" in v_low:
|
| 68 |
+
return 1
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
# Placeholders (not used since rows are None)
|
| 72 |
+
def convert_age(x):
|
| 73 |
+
v = _after_colon(x)
|
| 74 |
+
if v is None:
|
| 75 |
+
return None
|
| 76 |
+
try:
|
| 77 |
+
val = float(v)
|
| 78 |
+
except Exception:
|
| 79 |
+
return None
|
| 80 |
+
return val
|
| 81 |
+
|
| 82 |
+
def convert_gender(x):
|
| 83 |
+
v = _after_colon(x)
|
| 84 |
+
if v is None:
|
| 85 |
+
return None
|
| 86 |
+
v_low = v.lower()
|
| 87 |
+
if v_low in ["male", "m", "1"]:
|
| 88 |
+
return 1
|
| 89 |
+
if v_low in ["female", "f", "0"]:
|
| 90 |
+
return 0
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
# 3) Save metadata (initial filtering)
|
| 94 |
+
is_trait_available = trait_row is not None
|
| 95 |
+
_ = validate_and_save_cohort_info(
|
| 96 |
+
is_final=False,
|
| 97 |
+
cohort=cohort,
|
| 98 |
+
info_path=json_path,
|
| 99 |
+
is_gene_available=is_gene_available,
|
| 100 |
+
is_trait_available=is_trait_available
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# 4) Clinical feature extraction (only if trait data is available)
|
| 104 |
+
if trait_row is not None:
|
| 105 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 106 |
+
clinical_df=clinical_data,
|
| 107 |
+
trait=trait,
|
| 108 |
+
trait_row=trait_row,
|
| 109 |
+
convert_trait=convert_trait,
|
| 110 |
+
age_row=age_row,
|
| 111 |
+
convert_age=convert_age if age_row is not None else None,
|
| 112 |
+
gender_row=gender_row,
|
| 113 |
+
convert_gender=convert_gender if gender_row is not None else None
|
| 114 |
+
)
|
| 115 |
+
preview = preview_df(selected_clinical_df)
|
| 116 |
+
print(preview)
|
| 117 |
+
|
| 118 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 119 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 120 |
+
|
| 121 |
+
# Step 3: Gene Data Extraction
|
| 122 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 123 |
+
gene_data = get_genetic_data(matrix_file)
|
| 124 |
+
|
| 125 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 126 |
+
print(gene_data.index[:20])
|
| 127 |
+
|
| 128 |
+
# Step 4: Gene Identifier Review
|
| 129 |
+
print("requires_gene_mapping = True")
|
| 130 |
+
|
| 131 |
+
# Step 5: Gene Annotation
|
| 132 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 133 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 134 |
+
|
| 135 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 136 |
+
print("Gene annotation preview:")
|
| 137 |
+
print(preview_df(gene_annotation))
|
| 138 |
+
|
| 139 |
+
# Step 6: Gene Identifier Mapping
|
| 140 |
+
# Determine appropriate columns for probe IDs and gene symbols from the annotation dataframe
|
| 141 |
+
id_col = 'ID'
|
| 142 |
+
# Prefer 'gene_assignment' as it contains rich annotation strings with symbols
|
| 143 |
+
candidate_gene_cols = ['gene_assignment', 'Gene Symbol', 'GENE_SYMBOL', 'gene_symbol', 'Symbol']
|
| 144 |
+
gene_col = next((c for c in candidate_gene_cols if c in gene_annotation.columns), None)
|
| 145 |
+
|
| 146 |
+
if gene_col is None:
|
| 147 |
+
raise ValueError("No suitable gene symbol column found in gene annotation dataframe.")
|
| 148 |
+
|
| 149 |
+
# 2) Build mapping dataframe (probe ID -> gene symbol text)
|
| 150 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=id_col, gene_col=gene_col)
|
| 151 |
+
|
| 152 |
+
# 3) Apply mapping to convert probe-level data to gene-level expression
|
| 153 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 154 |
+
|
| 155 |
+
# Step 7: Data Normalization and Linking
|
| 156 |
+
import os
|
| 157 |
+
|
| 158 |
+
# 1. Normalize gene symbols and save
|
| 159 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 160 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 161 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 162 |
+
|
| 163 |
+
# 2. Link clinical and genetic data
|
| 164 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 165 |
+
|
| 166 |
+
# 3. Handle missing values
|
| 167 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 168 |
+
|
| 169 |
+
# 4. Assess bias and drop biased demographic features if needed
|
| 170 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 171 |
+
|
| 172 |
+
# 5. Final validation and save cohort info
|
| 173 |
+
note = "INFO: Trait mapping used: tissue Tumor=1, adjacent non-tumor/Normal=0. Age/Gender not available in clinical annotations."
|
| 174 |
+
is_usable = validate_and_save_cohort_info(
|
| 175 |
+
is_final=True,
|
| 176 |
+
cohort=cohort,
|
| 177 |
+
info_path=json_path,
|
| 178 |
+
is_gene_available=True,
|
| 179 |
+
is_trait_available=True,
|
| 180 |
+
is_biased=is_trait_biased,
|
| 181 |
+
df=unbiased_linked_data,
|
| 182 |
+
note=note
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
# 6. Save linked dataset if usable
|
| 186 |
+
if is_usable:
|
| 187 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 188 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Pancreatic_Cancer/code/GSE222788.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE222788"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE222788"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE222788.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE222788.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE222788.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import re
|
| 40 |
+
import os
|
| 41 |
+
|
| 42 |
+
# 1. Gene Expression Data Availability
|
| 43 |
+
# Based on the background info (NanoString PanCancer Immune Profiling Panel on RNA), gene expression data is available.
|
| 44 |
+
is_gene_available = True
|
| 45 |
+
|
| 46 |
+
# 2. Variable Availability and Data Type Conversion
|
| 47 |
+
|
| 48 |
+
# From the sample characteristics, only "treatment group" is provided.
|
| 49 |
+
# There are no explicit or inferable fields for disease status (trait), age, or gender with variability.
|
| 50 |
+
trait_row = None # Pancreatic cancer status is constant (all PDAC); not usable for association.
|
| 51 |
+
age_row = None # Not available
|
| 52 |
+
gender_row = None # Not available
|
| 53 |
+
|
| 54 |
+
def _get_value_after_colon(x):
|
| 55 |
+
if x is None:
|
| 56 |
+
return None
|
| 57 |
+
if isinstance(x, str):
|
| 58 |
+
parts = x.split(":", 1)
|
| 59 |
+
return parts[1].strip() if len(parts) > 1 else x.strip()
|
| 60 |
+
return x
|
| 61 |
+
|
| 62 |
+
def convert_trait(x):
|
| 63 |
+
# Binary: 1 = pancreatic cancer, 0 = non-cancer/normal. Unknown -> None
|
| 64 |
+
val = _get_value_after_colon(x)
|
| 65 |
+
if val is None:
|
| 66 |
+
return None
|
| 67 |
+
s = str(val).strip().lower()
|
| 68 |
+
positives = {
|
| 69 |
+
"pdac", "pancreatic ductal adenocarcinoma", "pancreatic cancer", "cancer",
|
| 70 |
+
"tumor", "malignant", "case", "patient", "lesion", "lapc"
|
| 71 |
+
}
|
| 72 |
+
negatives = {
|
| 73 |
+
"normal", "healthy", "control", "adjacent normal", "benign", "non-tumor",
|
| 74 |
+
"non tumor", "noncancerous", "non-cancer"
|
| 75 |
+
}
|
| 76 |
+
if s in positives:
|
| 77 |
+
return 1
|
| 78 |
+
if s in negatives:
|
| 79 |
+
return 0
|
| 80 |
+
# Heuristics
|
| 81 |
+
if "pancrea" in s or "pdac" in s or "adenocarcinoma" in s or "tumor" in s or "cancer" in s:
|
| 82 |
+
return 1
|
| 83 |
+
if "normal" in s or "control" in s or "benign" in s:
|
| 84 |
+
return 0
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
def convert_age(x):
|
| 88 |
+
# Continuous age in years. Unknown -> None
|
| 89 |
+
val = _get_value_after_colon(x)
|
| 90 |
+
if val is None:
|
| 91 |
+
return None
|
| 92 |
+
s = str(val).lower()
|
| 93 |
+
m = re.search(r'(\d+(?:\.\d+)?)', s)
|
| 94 |
+
if not m:
|
| 95 |
+
return None
|
| 96 |
+
try:
|
| 97 |
+
age = float(m.group(1))
|
| 98 |
+
if 0 < age < 120:
|
| 99 |
+
return age
|
| 100 |
+
except Exception:
|
| 101 |
+
pass
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
def convert_gender(x):
|
| 105 |
+
# Binary: female -> 0, male -> 1. Unknown -> None
|
| 106 |
+
val = _get_value_after_colon(x)
|
| 107 |
+
if val is None:
|
| 108 |
+
return None
|
| 109 |
+
s = str(val).strip().lower()
|
| 110 |
+
if s in {"male", "m"}:
|
| 111 |
+
return 1
|
| 112 |
+
if s in {"female", "f"}:
|
| 113 |
+
return 0
|
| 114 |
+
return None
|
| 115 |
+
|
| 116 |
+
# 3. Save Metadata (initial filtering)
|
| 117 |
+
is_trait_available = trait_row is not None
|
| 118 |
+
_ = validate_and_save_cohort_info(
|
| 119 |
+
is_final=False,
|
| 120 |
+
cohort=cohort,
|
| 121 |
+
info_path=json_path,
|
| 122 |
+
is_gene_available=is_gene_available,
|
| 123 |
+
is_trait_available=is_trait_available
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# 4. Clinical Feature Extraction (skip because trait_row is None)
|
| 127 |
+
if trait_row is not None:
|
| 128 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 129 |
+
clinical_df=clinical_data,
|
| 130 |
+
trait=trait,
|
| 131 |
+
trait_row=trait_row,
|
| 132 |
+
convert_trait=convert_trait,
|
| 133 |
+
age_row=age_row,
|
| 134 |
+
convert_age=convert_age,
|
| 135 |
+
gender_row=gender_row,
|
| 136 |
+
convert_gender=convert_gender
|
| 137 |
+
)
|
| 138 |
+
_ = preview_df(selected_clinical_df)
|
| 139 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 140 |
+
selected_clinical_df.to_csv(out_clinical_data_file, index=True)
|
output/preprocess/Pancreatic_Cancer/code/GSE223409.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE223409"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE223409"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE223409.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE223409.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE223409.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import re
|
| 40 |
+
import pandas as pd
|
| 41 |
+
|
| 42 |
+
# 1) Assess gene expression availability based on background info and characteristics
|
| 43 |
+
# SuperSeries with EVs and treatment-focused, likely small RNA/EV content rather than mRNA expression suitable for our analysis.
|
| 44 |
+
is_gene_available = False
|
| 45 |
+
|
| 46 |
+
# 2) Variable availability from Sample Characteristics Dictionary
|
| 47 |
+
# No human disease status, age, or gender information present.
|
| 48 |
+
trait_row = None
|
| 49 |
+
age_row = None
|
| 50 |
+
gender_row = None
|
| 51 |
+
|
| 52 |
+
# 2.2) Conversion functions
|
| 53 |
+
def _extract_value(cell):
|
| 54 |
+
if cell is None:
|
| 55 |
+
return None
|
| 56 |
+
# Split by colon and take the part after colon if present
|
| 57 |
+
parts = str(cell).split(":", 1)
|
| 58 |
+
val = parts[1].strip() if len(parts) > 1 else parts[0].strip()
|
| 59 |
+
# Normalize empty/unknown
|
| 60 |
+
if val == "" or val.lower() in {"na", "n/a", "nan", "none", "unknown", "not available"}:
|
| 61 |
+
return None
|
| 62 |
+
return val
|
| 63 |
+
|
| 64 |
+
def convert_trait(x):
|
| 65 |
+
val = _extract_value(x)
|
| 66 |
+
if val is None:
|
| 67 |
+
return None
|
| 68 |
+
s = val.lower()
|
| 69 |
+
# Heuristic mapping for pancreatic cancer status
|
| 70 |
+
cancer_pos = {"pancreatic cancer", "pdac", "pancreatic ductal adenocarcinoma", "cancer", "tumor", "tumour", "carcinoma", "case", "malignant"}
|
| 71 |
+
cancer_neg = {"normal", "healthy", "control", "adjacent normal", "benign", "non-cancer", "noncancer"}
|
| 72 |
+
if any(k in s for k in cancer_pos):
|
| 73 |
+
return 1
|
| 74 |
+
if any(k in s for k in cancer_neg):
|
| 75 |
+
return 0
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
def convert_age(x):
|
| 79 |
+
val = _extract_value(x)
|
| 80 |
+
if val is None:
|
| 81 |
+
return None
|
| 82 |
+
# Extract the first integer or float number as age in years
|
| 83 |
+
m = re.search(r"(\d+(\.\d+)?)", val)
|
| 84 |
+
if not m:
|
| 85 |
+
return None
|
| 86 |
+
try:
|
| 87 |
+
age = float(m.group(1))
|
| 88 |
+
return age
|
| 89 |
+
except Exception:
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
def convert_gender(x):
|
| 93 |
+
val = _extract_value(x)
|
| 94 |
+
if val is None:
|
| 95 |
+
return None
|
| 96 |
+
s = val.strip().lower()
|
| 97 |
+
# Map female->0, male->1
|
| 98 |
+
if s in {"female", "f", "woman", "women", "girl"}:
|
| 99 |
+
return 0
|
| 100 |
+
if s in {"male", "m", "man", "men", "boy"}:
|
| 101 |
+
return 1
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
# 3) Initial filtering metadata save
|
| 105 |
+
is_trait_available = trait_row is not None
|
| 106 |
+
_ = validate_and_save_cohort_info(
|
| 107 |
+
is_final=False,
|
| 108 |
+
cohort=cohort,
|
| 109 |
+
info_path=json_path,
|
| 110 |
+
is_gene_available=is_gene_available,
|
| 111 |
+
is_trait_available=is_trait_available
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# 4) Clinical feature extraction (skip because trait_row is None)
|
| 115 |
+
# If clinical data were available, we would run:
|
| 116 |
+
# selected = geo_select_clinical_features(
|
| 117 |
+
# clinical_df=clinical_data,
|
| 118 |
+
# trait=trait,
|
| 119 |
+
# trait_row=trait_row,
|
| 120 |
+
# convert_trait=convert_trait,
|
| 121 |
+
# age_row=age_row,
|
| 122 |
+
# convert_age=convert_age,
|
| 123 |
+
# gender_row=gender_row,
|
| 124 |
+
# convert_gender=convert_gender
|
| 125 |
+
# )
|
| 126 |
+
# preview = preview_df(selected)
|
| 127 |
+
# print(preview)
|
| 128 |
+
# selected.to_csv(out_clinical_data_file, index=True)
|
output/preprocess/Pancreatic_Cancer/code/GSE236951.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
cohort = "GSE236951"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Pancreatic_Cancer"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Pancreatic_Cancer/GSE236951"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/GSE236951.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/GSE236951.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/GSE236951.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
import re
|
| 41 |
+
|
| 42 |
+
# 1) Gene expression data availability (Nanostring immune gene expression panel)
|
| 43 |
+
is_gene_available = True
|
| 44 |
+
|
| 45 |
+
# 2) Variable availability (rows inferred from the provided dictionary)
|
| 46 |
+
trait_row = 0 # disease: Pancreatic ductal adenocarcinoma / Colon adenocarcinoma / Benign colon disease
|
| 47 |
+
age_row = 3 # age: NN years
|
| 48 |
+
gender_row = 2 # Sex: Male / Female
|
| 49 |
+
|
| 50 |
+
# Helper to get the value after colon and normalize
|
| 51 |
+
def _after_colon(x):
|
| 52 |
+
if x is None:
|
| 53 |
+
return None
|
| 54 |
+
parts = str(x).split(":", 1)
|
| 55 |
+
val = parts[1] if len(parts) > 1 else parts[0]
|
| 56 |
+
return val.strip()
|
| 57 |
+
|
| 58 |
+
# 2.2) Converters
|
| 59 |
+
def convert_trait(x):
|
| 60 |
+
v = _after_colon(x)
|
| 61 |
+
if v is None:
|
| 62 |
+
return None
|
| 63 |
+
vl = v.lower()
|
| 64 |
+
# Map pancreatic cancer cases to 1, others (colon cancer/benign colon disease) to 0
|
| 65 |
+
if "pancrea" in vl: # captures 'pancreatic ductal adenocarcinoma'
|
| 66 |
+
return 1
|
| 67 |
+
if ("colon" in vl) or ("benign" in vl):
|
| 68 |
+
return 0
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
def convert_age(x):
|
| 72 |
+
v = _after_colon(x)
|
| 73 |
+
if v is None:
|
| 74 |
+
return None
|
| 75 |
+
# Extract the first number (e.g., "59 years")
|
| 76 |
+
m = re.search(r"(\d+(\.\d+)?)", v)
|
| 77 |
+
if not m:
|
| 78 |
+
return None
|
| 79 |
+
try:
|
| 80 |
+
return float(m.group(1))
|
| 81 |
+
except Exception:
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
def convert_gender(x):
|
| 85 |
+
v = _after_colon(x)
|
| 86 |
+
if v is None:
|
| 87 |
+
return None
|
| 88 |
+
vl = v.lower()
|
| 89 |
+
if vl in {"male", "m"}:
|
| 90 |
+
return 1
|
| 91 |
+
if vl in {"female", "f"}:
|
| 92 |
+
return 0
|
| 93 |
+
return None
|
| 94 |
+
|
| 95 |
+
# 3) Save metadata with initial filtering
|
| 96 |
+
is_trait_available = trait_row is not None
|
| 97 |
+
_ = validate_and_save_cohort_info(
|
| 98 |
+
is_final=False,
|
| 99 |
+
cohort=cohort,
|
| 100 |
+
info_path=json_path,
|
| 101 |
+
is_gene_available=is_gene_available,
|
| 102 |
+
is_trait_available=is_trait_available
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# 4) Clinical feature extraction (only if clinical data is available)
|
| 106 |
+
if is_trait_available:
|
| 107 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 108 |
+
clinical_df=clinical_data,
|
| 109 |
+
trait=trait,
|
| 110 |
+
trait_row=trait_row,
|
| 111 |
+
convert_trait=convert_trait,
|
| 112 |
+
age_row=age_row,
|
| 113 |
+
convert_age=convert_age,
|
| 114 |
+
gender_row=gender_row,
|
| 115 |
+
convert_gender=convert_gender
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
preview = preview_df(selected_clinical_df)
|
| 119 |
+
print(preview)
|
| 120 |
+
|
| 121 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 122 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 123 |
+
|
| 124 |
+
# Step 3: Gene Data Extraction
|
| 125 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 126 |
+
gene_data = get_genetic_data(matrix_file)
|
| 127 |
+
|
| 128 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 129 |
+
print(gene_data.index[:20])
|
| 130 |
+
|
| 131 |
+
# Step 4: Gene Identifier Review
|
| 132 |
+
# Observed identifiers (e.g., A2M, ABCB1, ABL1) are standard human gene symbols (HGNC).
|
| 133 |
+
requires_gene_mapping = False
|
| 134 |
+
print(f"requires_gene_mapping = {requires_gene_mapping}")
|
| 135 |
+
|
| 136 |
+
# Step 5: Data Normalization and Linking
|
| 137 |
+
import os
|
| 138 |
+
|
| 139 |
+
# 1. Normalize gene symbols and save
|
| 140 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 141 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 142 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 143 |
+
|
| 144 |
+
# 2. Link clinical and genetic data
|
| 145 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 146 |
+
|
| 147 |
+
# Determine availability flags based on actual data (ensure native Python bool)
|
| 148 |
+
is_gene_available_final = bool((normalized_gene_data.shape[0] > 0) and (normalized_gene_data.shape[1] > 0))
|
| 149 |
+
is_trait_available_final = bool((trait in linked_data.columns) and bool(linked_data[trait].notna().any()))
|
| 150 |
+
|
| 151 |
+
# 3. Handle missing values
|
| 152 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 153 |
+
|
| 154 |
+
# 4. Assess bias and drop biased demographics
|
| 155 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 156 |
+
|
| 157 |
+
# 5. Final validation and save cohort info
|
| 158 |
+
note = ("INFO: Nanostring PanCancer Immune Panel (~700 immune genes). "
|
| 159 |
+
"Identifiers appear to be HGNC symbols; normalized with NCBI gene synonyms and aggregated.")
|
| 160 |
+
is_usable = validate_and_save_cohort_info(
|
| 161 |
+
is_final=True,
|
| 162 |
+
cohort=cohort,
|
| 163 |
+
info_path=json_path,
|
| 164 |
+
is_gene_available=is_gene_available_final,
|
| 165 |
+
is_trait_available=is_trait_available_final,
|
| 166 |
+
is_biased=bool(is_trait_biased),
|
| 167 |
+
df=unbiased_linked_data,
|
| 168 |
+
note=note
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
# 6. Save linked data if usable
|
| 172 |
+
if bool(is_usable):
|
| 173 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 174 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Pancreatic_Cancer/code/TCGA.py
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Pancreatic_Cancer"
|
| 6 |
+
|
| 7 |
+
# Input paths
|
| 8 |
+
tcga_root_dir = "../DATA/TCGA"
|
| 9 |
+
|
| 10 |
+
# Output paths
|
| 11 |
+
out_data_file = "./output/z5/preprocess/Pancreatic_Cancer/TCGA.csv"
|
| 12 |
+
out_gene_data_file = "./output/z5/preprocess/Pancreatic_Cancer/gene_data/TCGA.csv"
|
| 13 |
+
out_clinical_data_file = "./output/z5/preprocess/Pancreatic_Cancer/clinical_data/TCGA.csv"
|
| 14 |
+
json_path = "./output/z5/preprocess/Pancreatic_Cancer/cohort_info.json"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Step 1: Initial Data Loading
|
| 18 |
+
import os
|
| 19 |
+
import pandas as pd
|
| 20 |
+
|
| 21 |
+
# Step 1: Select the most relevant TCGA cohort directory for Pancreatic Cancer
|
| 22 |
+
subdirs = [d for d in os.listdir(tcga_root_dir) if os.path.isdir(os.path.join(tcga_root_dir, d))]
|
| 23 |
+
search_terms = ['pancre', '(paad)']
|
| 24 |
+
candidates = [d for d in subdirs if any(term in d.lower() for term in search_terms)]
|
| 25 |
+
|
| 26 |
+
if not candidates:
|
| 27 |
+
# No suitable directory found: record and stop further processing for this trait
|
| 28 |
+
validate_and_save_cohort_info(
|
| 29 |
+
is_final=False,
|
| 30 |
+
cohort="TCGA",
|
| 31 |
+
info_path=json_path,
|
| 32 |
+
is_gene_available=False,
|
| 33 |
+
is_trait_available=False
|
| 34 |
+
)
|
| 35 |
+
else:
|
| 36 |
+
# Choose the most specific match (prefer PAAD explicit mention)
|
| 37 |
+
candidates_sorted = sorted(candidates, key=lambda x: (('(PAAD)' in x.upper()) or ('PAAD' in x.upper()), len(x)), reverse=True)
|
| 38 |
+
selected_dir = candidates_sorted[0]
|
| 39 |
+
cohort_dir = os.path.join(tcga_root_dir, selected_dir)
|
| 40 |
+
|
| 41 |
+
# Step 2: Identify file paths for clinical and genetic data
|
| 42 |
+
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)
|
| 43 |
+
|
| 44 |
+
# Step 3: Load both files as DataFrames
|
| 45 |
+
clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0, low_memory=False)
|
| 46 |
+
genetic_df = pd.read_csv(genetic_file_path, sep='\t', index_col=0, low_memory=False)
|
| 47 |
+
|
| 48 |
+
# Step 4: Print clinical column names
|
| 49 |
+
print(list(clinical_df.columns))
|
| 50 |
+
|
| 51 |
+
# Step 2: Find Candidate Demographic Features
|
| 52 |
+
import os
|
| 53 |
+
import pandas as pd
|
| 54 |
+
|
| 55 |
+
# Try to get clinical_df from previous steps; if not available, attempt to load from TCGA PAAD cohort
|
| 56 |
+
clinical_df = globals().get('clinical_df', None)
|
| 57 |
+
if clinical_df is None:
|
| 58 |
+
# Find a PAAD cohort directory under tcga_root_dir
|
| 59 |
+
paad_dir = None
|
| 60 |
+
for root, dirs, files in os.walk(tcga_root_dir):
|
| 61 |
+
for d in dirs:
|
| 62 |
+
if 'PAAD' in d.upper():
|
| 63 |
+
paad_dir = os.path.join(root, d)
|
| 64 |
+
break
|
| 65 |
+
if paad_dir:
|
| 66 |
+
break
|
| 67 |
+
|
| 68 |
+
if paad_dir:
|
| 69 |
+
try:
|
| 70 |
+
clinical_file_path, _ = tcga_get_relevant_filepaths(paad_dir)
|
| 71 |
+
clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0, dtype=str)
|
| 72 |
+
except Exception:
|
| 73 |
+
clinical_df = None
|
| 74 |
+
|
| 75 |
+
# Determine columns to scan
|
| 76 |
+
if clinical_df is not None:
|
| 77 |
+
columns = list(clinical_df.columns)
|
| 78 |
+
else:
|
| 79 |
+
# Fallback to previously defined list of columns if available
|
| 80 |
+
columns = globals().get('clinical_columns', [])
|
| 81 |
+
|
| 82 |
+
# Identify candidate age and gender columns
|
| 83 |
+
candidate_age_cols = []
|
| 84 |
+
candidate_gender_cols = []
|
| 85 |
+
|
| 86 |
+
for col in columns:
|
| 87 |
+
cl = col.lower()
|
| 88 |
+
# Age-related: explicit known fields and common patterns without capturing 'stage'
|
| 89 |
+
if (
|
| 90 |
+
cl == 'age'
|
| 91 |
+
or cl.startswith('age_')
|
| 92 |
+
or cl.endswith('_age')
|
| 93 |
+
or 'age_at' in cl
|
| 94 |
+
or cl in {'days_to_birth', 'age_at_initial_pathologic_diagnosis'}
|
| 95 |
+
):
|
| 96 |
+
candidate_age_cols.append(col)
|
| 97 |
+
# Gender-related
|
| 98 |
+
if cl in {'gender', 'sex'} or cl.startswith('gender') or cl.endswith('gender'):
|
| 99 |
+
candidate_gender_cols.append(col)
|
| 100 |
+
|
| 101 |
+
# Keep only existing columns (safety)
|
| 102 |
+
if clinical_df is not None:
|
| 103 |
+
candidate_age_cols = [c for c in candidate_age_cols if c in clinical_df.columns]
|
| 104 |
+
candidate_gender_cols = [c for c in candidate_gender_cols if c in clinical_df.columns]
|
| 105 |
+
|
| 106 |
+
# Required output format
|
| 107 |
+
print(f"candidate_age_cols = {candidate_age_cols}")
|
| 108 |
+
print(f"candidate_gender_cols = {candidate_gender_cols}")
|
| 109 |
+
|
| 110 |
+
# Preview extracted data as dictionaries (first 5 values)
|
| 111 |
+
if clinical_df is not None and candidate_age_cols:
|
| 112 |
+
age_preview_dict = preview_df(clinical_df[candidate_age_cols], n=5)
|
| 113 |
+
print(age_preview_dict)
|
| 114 |
+
if clinical_df is not None and candidate_gender_cols:
|
| 115 |
+
gender_preview_dict = preview_df(clinical_df[candidate_gender_cols], n=5)
|
| 116 |
+
print(gender_preview_dict)
|
| 117 |
+
|
| 118 |
+
# Step 3: Select Demographic Features
|
| 119 |
+
# Select demographic feature columns based on candidate lists and typical TCGA conventions.
|
| 120 |
+
|
| 121 |
+
# Safely retrieve candidate lists if they exist; otherwise default to empty lists
|
| 122 |
+
try:
|
| 123 |
+
_candidate_age_cols = list(candidate_age_cols)
|
| 124 |
+
except NameError:
|
| 125 |
+
_candidate_age_cols = []
|
| 126 |
+
|
| 127 |
+
try:
|
| 128 |
+
_candidate_gender_cols = list(candidate_gender_cols)
|
| 129 |
+
except NameError:
|
| 130 |
+
_candidate_gender_cols = []
|
| 131 |
+
|
| 132 |
+
age_col = None
|
| 133 |
+
gender_col = None
|
| 134 |
+
|
| 135 |
+
# Prefer explicit age column over days-based representations
|
| 136 |
+
preferred_age_cols = [
|
| 137 |
+
'age_at_initial_pathologic_diagnosis',
|
| 138 |
+
'age_at_diagnosis',
|
| 139 |
+
'age'
|
| 140 |
+
]
|
| 141 |
+
for col in preferred_age_cols:
|
| 142 |
+
if col in _candidate_age_cols:
|
| 143 |
+
age_col = col
|
| 144 |
+
break
|
| 145 |
+
# If none of the preferred are present, fall back to any candidate that likely represents age
|
| 146 |
+
if age_col is None and _candidate_age_cols:
|
| 147 |
+
# Avoid days_to_birth if there's any other option, because it needs conversion
|
| 148 |
+
alt = [c for c in _candidate_age_cols if 'days_to_birth' not in c.lower()]
|
| 149 |
+
age_col = (alt[0] if alt else _candidate_age_cols[0])
|
| 150 |
+
|
| 151 |
+
# Prefer 'gender' or 'sex'
|
| 152 |
+
preferred_gender_cols = ['gender', 'sex']
|
| 153 |
+
for col in preferred_gender_cols:
|
| 154 |
+
if col in _candidate_gender_cols:
|
| 155 |
+
gender_col = col
|
| 156 |
+
break
|
| 157 |
+
# Fallback to first candidate if any
|
| 158 |
+
if gender_col is None and _candidate_gender_cols:
|
| 159 |
+
gender_col = _candidate_gender_cols[0]
|
| 160 |
+
|
| 161 |
+
# Explicitly print the chosen columns
|
| 162 |
+
print(f"Selected age_col: {age_col}")
|
| 163 |
+
print(f"Selected gender_col: {gender_col}")
|
| 164 |
+
|
| 165 |
+
# Additionally, if clinical_df is available, show first 5 non-null values for verification
|
| 166 |
+
try:
|
| 167 |
+
if age_col is not None and 'clinical_df' in globals() and age_col in clinical_df.columns:
|
| 168 |
+
print("age_col preview (first 5 non-null):", clinical_df[age_col].dropna().head(5).tolist())
|
| 169 |
+
if gender_col is not None and 'clinical_df' in globals() and gender_col in clinical_df.columns:
|
| 170 |
+
print("gender_col preview (first 5 non-null):", clinical_df[gender_col].dropna().head(5).tolist())
|
| 171 |
+
except Exception as e:
|
| 172 |
+
# Avoid breaking the pipeline due to preview printing
|
| 173 |
+
print(f"Warning: Could not preview selected columns due to: {e}")
|
| 174 |
+
|
| 175 |
+
# Step 4: Feature Engineering and Validation
|
| 176 |
+
import os
|
| 177 |
+
import pandas as pd
|
| 178 |
+
|
| 179 |
+
# Ensure clinical_df and genetic_df are available (fallback loading if needed)
|
| 180 |
+
if 'clinical_df' not in globals() or clinical_df is None or 'genetic_df' not in globals() or genetic_df is None:
|
| 181 |
+
paad_dir = None
|
| 182 |
+
for root, dirs, files in os.walk(tcga_root_dir):
|
| 183 |
+
for d in dirs:
|
| 184 |
+
if 'PAAD' in d.upper():
|
| 185 |
+
paad_dir = os.path.join(root, d)
|
| 186 |
+
break
|
| 187 |
+
if paad_dir:
|
| 188 |
+
break
|
| 189 |
+
if paad_dir:
|
| 190 |
+
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(paad_dir)
|
| 191 |
+
clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0, low_memory=False)
|
| 192 |
+
genetic_df = pd.read_csv(genetic_file_path, sep='\t', index_col=0, low_memory=False)
|
| 193 |
+
else:
|
| 194 |
+
raise RuntimeError("PAAD cohort directory not found; cannot proceed.")
|
| 195 |
+
|
| 196 |
+
# 1) Extract and standardize clinical features
|
| 197 |
+
selected_clinical_df = tcga_select_clinical_features(
|
| 198 |
+
clinical_df=clinical_df,
|
| 199 |
+
trait=trait,
|
| 200 |
+
age_col=age_col if 'age_col' in globals() else None,
|
| 201 |
+
gender_col=gender_col if 'gender_col' in globals() else None
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
# 2) Normalize gene symbols in expression data and save
|
| 205 |
+
gene_df_norm = normalize_gene_symbols_in_index(genetic_df.copy())
|
| 206 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 207 |
+
gene_df_norm.to_csv(out_gene_data_file)
|
| 208 |
+
|
| 209 |
+
# 3) Link clinical and genetic data on sample IDs
|
| 210 |
+
linked_data = selected_clinical_df.join(gene_df_norm.T, how='inner')
|
| 211 |
+
|
| 212 |
+
# 4) Handle missing values
|
| 213 |
+
processed_df = handle_missing_values(linked_data, trait_col=trait)
|
| 214 |
+
|
| 215 |
+
# 5) Determine bias and remove biased demographic features
|
| 216 |
+
trait_biased, processed_df = judge_and_remove_biased_features(processed_df, trait=trait)
|
| 217 |
+
|
| 218 |
+
# 6) Final validation and metadata saving
|
| 219 |
+
# Cast to native Python bool to avoid numpy.bool_ in JSON
|
| 220 |
+
is_gene_available = bool((gene_df_norm.shape[0] > 0) and (gene_df_norm.shape[1] > 0))
|
| 221 |
+
is_trait_available = bool(selected_clinical_df[trait].notna().any())
|
| 222 |
+
|
| 223 |
+
note = (
|
| 224 |
+
"INFO: Trait derived from TCGA sample type code (01-09 tumor=1, 10-19 normal=0). "
|
| 225 |
+
f"Age source: {age_col if 'age_col' in globals() else None}; "
|
| 226 |
+
f"Gender source: {gender_col if 'gender_col' in globals() else None}. "
|
| 227 |
+
"Gene symbols normalized using NCBI synonyms; unmapped symbols removed and duplicates aggregated."
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
is_usable = validate_and_save_cohort_info(
|
| 231 |
+
is_final=True,
|
| 232 |
+
cohort="TCGA",
|
| 233 |
+
info_path=json_path,
|
| 234 |
+
is_gene_available=is_gene_available,
|
| 235 |
+
is_trait_available=is_trait_available,
|
| 236 |
+
is_biased=trait_biased,
|
| 237 |
+
df=processed_df,
|
| 238 |
+
note=note
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
# 7) Save linked dataset only if usable
|
| 242 |
+
if is_usable:
|
| 243 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 244 |
+
processed_df.to_csv(out_data_file)
|
output/preprocess/Pancreatic_Cancer/cohort_info.json
CHANGED
|
@@ -1,112 +1 @@
|
|
| 1 |
-
{
|
| 2 |
-
"GSE236951": {
|
| 3 |
-
"is_usable": true,
|
| 4 |
-
"is_gene_available": true,
|
| 5 |
-
"is_trait_available": true,
|
| 6 |
-
"is_available": true,
|
| 7 |
-
"is_biased": false,
|
| 8 |
-
"has_age": true,
|
| 9 |
-
"has_gender": true,
|
| 10 |
-
"sample_size": 34
|
| 11 |
-
},
|
| 12 |
-
"GSE223409": {
|
| 13 |
-
"is_usable": false,
|
| 14 |
-
"is_gene_available": true,
|
| 15 |
-
"is_trait_available": false,
|
| 16 |
-
"is_available": false,
|
| 17 |
-
"is_biased": null,
|
| 18 |
-
"has_age": null,
|
| 19 |
-
"has_gender": null,
|
| 20 |
-
"sample_size": null
|
| 21 |
-
},
|
| 22 |
-
"GSE222788": {
|
| 23 |
-
"is_usable": false,
|
| 24 |
-
"is_gene_available": false,
|
| 25 |
-
"is_trait_available": false,
|
| 26 |
-
"is_available": false,
|
| 27 |
-
"is_biased": null,
|
| 28 |
-
"has_age": null,
|
| 29 |
-
"has_gender": null,
|
| 30 |
-
"sample_size": null
|
| 31 |
-
},
|
| 32 |
-
"GSE183795": {
|
| 33 |
-
"is_usable": true,
|
| 34 |
-
"is_gene_available": true,
|
| 35 |
-
"is_trait_available": true,
|
| 36 |
-
"is_available": true,
|
| 37 |
-
"is_biased": false,
|
| 38 |
-
"has_age": false,
|
| 39 |
-
"has_gender": false,
|
| 40 |
-
"sample_size": 244
|
| 41 |
-
},
|
| 42 |
-
"GSE157494": {
|
| 43 |
-
"is_usable": true,
|
| 44 |
-
"is_gene_available": true,
|
| 45 |
-
"is_trait_available": true,
|
| 46 |
-
"is_available": true,
|
| 47 |
-
"is_biased": false,
|
| 48 |
-
"has_age": false,
|
| 49 |
-
"has_gender": false,
|
| 50 |
-
"sample_size": 41
|
| 51 |
-
},
|
| 52 |
-
"GSE131027": {
|
| 53 |
-
"is_usable": false,
|
| 54 |
-
"is_gene_available": false,
|
| 55 |
-
"is_trait_available": false,
|
| 56 |
-
"is_available": false,
|
| 57 |
-
"is_biased": null,
|
| 58 |
-
"has_age": null,
|
| 59 |
-
"has_gender": null,
|
| 60 |
-
"sample_size": null
|
| 61 |
-
},
|
| 62 |
-
"GSE130563": {
|
| 63 |
-
"is_usable": false,
|
| 64 |
-
"is_gene_available": false,
|
| 65 |
-
"is_trait_available": false,
|
| 66 |
-
"is_available": false,
|
| 67 |
-
"is_biased": null,
|
| 68 |
-
"has_age": null,
|
| 69 |
-
"has_gender": null,
|
| 70 |
-
"sample_size": null
|
| 71 |
-
},
|
| 72 |
-
"GSE125158": {
|
| 73 |
-
"is_usable": true,
|
| 74 |
-
"is_gene_available": true,
|
| 75 |
-
"is_trait_available": true,
|
| 76 |
-
"is_available": true,
|
| 77 |
-
"is_biased": false,
|
| 78 |
-
"has_age": false,
|
| 79 |
-
"has_gender": false,
|
| 80 |
-
"sample_size": 30
|
| 81 |
-
},
|
| 82 |
-
"GSE124069": {
|
| 83 |
-
"is_usable": false,
|
| 84 |
-
"is_gene_available": true,
|
| 85 |
-
"is_trait_available": false,
|
| 86 |
-
"is_available": false,
|
| 87 |
-
"is_biased": null,
|
| 88 |
-
"has_age": null,
|
| 89 |
-
"has_gender": null,
|
| 90 |
-
"sample_size": null
|
| 91 |
-
},
|
| 92 |
-
"GSE120127": {
|
| 93 |
-
"is_usable": false,
|
| 94 |
-
"is_gene_available": true,
|
| 95 |
-
"is_trait_available": false,
|
| 96 |
-
"is_available": false,
|
| 97 |
-
"is_biased": null,
|
| 98 |
-
"has_age": null,
|
| 99 |
-
"has_gender": null,
|
| 100 |
-
"sample_size": null
|
| 101 |
-
},
|
| 102 |
-
"TCGA": {
|
| 103 |
-
"is_usable": false,
|
| 104 |
-
"is_gene_available": true,
|
| 105 |
-
"is_trait_available": true,
|
| 106 |
-
"is_available": true,
|
| 107 |
-
"is_biased": true,
|
| 108 |
-
"has_age": true,
|
| 109 |
-
"has_gender": true,
|
| 110 |
-
"sample_size": 183
|
| 111 |
-
}
|
| 112 |
-
}
|
|
|
|
| 1 |
+
{"GSE236951": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": true, "has_gender": true, "sample_size": 34, "note": "INFO: Nanostring PanCancer Immune Panel (~700 immune genes). Identifiers appear to be HGNC symbols; normalized with NCBI gene synonyms and aggregated."}, "GSE223409": {"is_usable": false, "is_gene_available": false, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}, "GSE222788": {"is_usable": false, "is_gene_available": true, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}, "GSE183795": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": false, "has_gender": false, "sample_size": 244, "note": "INFO: Trait mapping used: tissue Tumor=1, adjacent non-tumor/Normal=0. Age/Gender not available in clinical annotations."}, "GSE157494": {"is_usable": false, "is_gene_available": true, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}, "GSE131027": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": false, "has_gender": false, "sample_size": 92, "note": "INFO: Affymetrix probe IDs mapped to gene symbols via platform annotation; symbols normalized using NCBI synonyms."}, "GSE130563": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": true, "has_gender": true, "sample_size": 46, "note": "WARNING: Gene symbol normalization produced empty matrix; retained probe-level expression (Affymetrix probes). INFO: Gene matrix used has 23786 features and 46 samples before linking."}, "GSE125158": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": true, "has_gender": true, "sample_size": 30, "note": "INFO: Genes(before_norm)=18488, Genes(after_norm)=18247, Samples(after_QC)=30, GeneFeatures(after_QC)=18247"}, "GSE124069": {"is_usable": false, "is_gene_available": true, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": "INFO: No clinical trait; cell line study. Gene-only data saved; linked data not generated."}, "GSE120127": {"is_usable": false, "is_gene_available": true, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}, "TCGA": {"is_usable": false, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": true, "has_age": true, "has_gender": true, "sample_size": 183, "note": "INFO: Trait derived from TCGA sample type code (01-09 tumor=1, 10-19 normal=0). Age source: age_at_initial_pathologic_diagnosis; Gender source: gender. Gene symbols normalized using NCBI synonyms; unmapped symbols removed and duplicates aggregated."}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output/preprocess/Parkinsons_Disease/GSE49126.csv
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/preprocess/Parkinsons_Disease/GSE72267.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/preprocess/Parkinsons_Disease/clinical_data/GSE101534.csv
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
GSM2705776,GSM2705777,GSM2705778,GSM2705779,GSM2705780,GSM2705781,GSM2705782,GSM2705783,GSM2705784,GSM2705785,GSM2705786,GSM2705787,GSM2705788,GSM2705789,GSM2705790,GSM2705791,GSM2705792,GSM2705793,GSM2705794,GSM2705795,GSM2705796,GSM2705797,GSM2705798,GSM2705799,GSM2705800,GSM2705801,GSM2705802,GSM2705803,GSM2705804,GSM2705805,GSM2705806,GSM2705807,GSM2705808,GSM2705809,GSM2705810,GSM2705811,GSM2705812,GSM2705813,GSM2705814,GSM2705815,GSM2705816,GSM2705817,GSM2705818,GSM2705819,GSM2705820,GSM2705821,GSM2705822,GSM2705823,GSM2705824,GSM2705825,GSM2705826
|
| 2 |
-
0.0,1.0,1.0,
|
|
|
|
| 1 |
+
,GSM2705776,GSM2705777,GSM2705778,GSM2705779,GSM2705780,GSM2705781,GSM2705782,GSM2705783,GSM2705784,GSM2705785,GSM2705786,GSM2705787,GSM2705788,GSM2705789,GSM2705790,GSM2705791,GSM2705792,GSM2705793,GSM2705794,GSM2705795,GSM2705796,GSM2705797,GSM2705798,GSM2705799,GSM2705800,GSM2705801,GSM2705802,GSM2705803,GSM2705804,GSM2705805,GSM2705806,GSM2705807,GSM2705808,GSM2705809,GSM2705810,GSM2705811,GSM2705812,GSM2705813,GSM2705814,GSM2705815,GSM2705816,GSM2705817,GSM2705818,GSM2705819,GSM2705820,GSM2705821,GSM2705822,GSM2705823,GSM2705824,GSM2705825,GSM2705826
|
| 2 |
+
Parkinsons_Disease,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0
|
output/preprocess/Parkinsons_Disease/clinical_data/GSE202667.csv
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
1.0,0.0,,,,,,
|
| 3 |
-
53.0,57.0,63.0,75.0,85.0,76.0,69.0,66.0
|
| 4 |
-
1.0,,,,,,,
|
|
|
|
| 1 |
+
,GSM6128103,GSM6128104,GSM6128105,GSM6128106,GSM6128107,GSM6128108,GSM6128109,GSM6128110,GSM6128111,GSM6128112,GSM6128113,GSM6128114,GSM6128115,GSM6128116,GSM6128117,GSM6128118,GSM6128119,GSM6128120,GSM6128121,GSM6128122,GSM6128123,GSM6128124,GSM6128125,GSM6128126,GSM6128127,GSM6128128,GSM6128129,GSM6128130,GSM6128131,GSM6128132,GSM6128133,GSM6128134,GSM6128135,GSM6128136,GSM6128137,GSM6128138,GSM6128139,GSM6128140,GSM6128141,GSM6128142,GSM6128143,GSM6128144,GSM6128145,GSM6128146,GSM6128147,GSM6128148,GSM6128149,GSM6128150,GSM6128151,GSM6128152,GSM6128153,GSM6128154,GSM6128155,GSM6128156,GSM6128157,GSM6128158,GSM6128159,GSM6128160,GSM6128161,GSM6128162
|
| 2 |
+
Parkinsons_Disease,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
| 3 |
+
Age,53.0,53.0,53.0,53.0,53.0,53.0,57.0,57.0,57.0,57.0,57.0,57.0,63.0,63.0,63.0,63.0,63.0,63.0,75.0,75.0,75.0,75.0,75.0,75.0,85.0,85.0,85.0,85.0,85.0,85.0,76.0,76.0,76.0,76.0,76.0,76.0,63.0,63.0,63.0,63.0,63.0,63.0,69.0,69.0,69.0,69.0,69.0,69.0,66.0,66.0,66.0,66.0,66.0,66.0,53.0,53.0,53.0,53.0,53.0,53.0
|
|
|
output/preprocess/Parkinsons_Disease/clinical_data/GSE72267.csv
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
GSM1859079,GSM1859080,GSM1859081,GSM1859082,GSM1859083,GSM1859084,GSM1859085,GSM1859086,GSM1859087,GSM1859088,GSM1859089,GSM1859090,GSM1859091,GSM1859092,GSM1859093,GSM1859094,GSM1859095,GSM1859096,GSM1859097,GSM1859098,GSM1859099,GSM1859100,GSM1859101,GSM1859102,GSM1859103,GSM1859104,GSM1859105,GSM1859106,GSM1859107,GSM1859108,GSM1859109,GSM1859110,GSM1859111,GSM1859112,GSM1859113,GSM1859114,GSM1859115,GSM1859116,GSM1859117,GSM1859118,GSM1859119,GSM1859120,GSM1859121,GSM1859122,GSM1859123,GSM1859124,GSM1859125,GSM1859126,GSM1859127,GSM1859128,GSM1859129,GSM1859130,GSM1859131,GSM1859132,GSM1859133,GSM1859134,GSM1859135,GSM1859136,GSM1859137
|
| 2 |
-
0.0,1.0,1.0,0.0,0.0,1.0,0.0,1.0,0.0,1.0,1.0,0.0,0.0,1.0,0.0,0.0,1.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0,0.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
|
|
|
|
| 1 |
+
,GSM1859079,GSM1859080,GSM1859081,GSM1859082,GSM1859083,GSM1859084,GSM1859085,GSM1859086,GSM1859087,GSM1859088,GSM1859089,GSM1859090,GSM1859091,GSM1859092,GSM1859093,GSM1859094,GSM1859095,GSM1859096,GSM1859097,GSM1859098,GSM1859099,GSM1859100,GSM1859101,GSM1859102,GSM1859103,GSM1859104,GSM1859105,GSM1859106,GSM1859107,GSM1859108,GSM1859109,GSM1859110,GSM1859111,GSM1859112,GSM1859113,GSM1859114,GSM1859115,GSM1859116,GSM1859117,GSM1859118,GSM1859119,GSM1859120,GSM1859121,GSM1859122,GSM1859123,GSM1859124,GSM1859125,GSM1859126,GSM1859127,GSM1859128,GSM1859129,GSM1859130,GSM1859131,GSM1859132,GSM1859133,GSM1859134,GSM1859135,GSM1859136,GSM1859137
|
| 2 |
+
Parkinsons_Disease,0.0,1.0,1.0,0.0,0.0,1.0,0.0,1.0,0.0,1.0,1.0,0.0,0.0,1.0,0.0,0.0,1.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0,0.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
|
output/preprocess/Parkinsons_Disease/code/GSE101534.py
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE101534"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE101534"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE101534.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE101534.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE101534.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
|
| 41 |
+
# Step 1: Determine gene expression availability
|
| 42 |
+
is_gene_available = True # "Genome-wide expression profiling" indicates gene expression data is available.
|
| 43 |
+
|
| 44 |
+
# Step 2: Variable availability and conversion functions
|
| 45 |
+
|
| 46 |
+
# Sample Characteristics Dictionary indicates mutation categories at row 0:
|
| 47 |
+
# {0: ['mutation: healthy', 'mutation: patient', 'mutation: gene corrected', 'mutation: inserted G2019S']}
|
| 48 |
+
# Use row 0 to infer Parkinson's Disease status at the donor level.
|
| 49 |
+
trait_row = 0
|
| 50 |
+
|
| 51 |
+
# Age and Gender are not available in the provided characteristics
|
| 52 |
+
age_row = None
|
| 53 |
+
gender_row = None
|
| 54 |
+
|
| 55 |
+
# Conversion functions
|
| 56 |
+
def _extract_value(x):
|
| 57 |
+
if x is None:
|
| 58 |
+
return None
|
| 59 |
+
try:
|
| 60 |
+
parts = str(x).split(":", 1)
|
| 61 |
+
val = parts[1] if len(parts) > 1 else parts[0]
|
| 62 |
+
return val.strip()
|
| 63 |
+
except Exception:
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
def convert_trait(x):
|
| 67 |
+
v = _extract_value(x)
|
| 68 |
+
if v is None:
|
| 69 |
+
return None
|
| 70 |
+
v_low = v.lower()
|
| 71 |
+
# Assumption: label by donor diagnosis rather than cell-line genotype.
|
| 72 |
+
# - 'patient' -> PD donor -> 1
|
| 73 |
+
# - 'healthy' -> healthy donor -> 0
|
| 74 |
+
# - 'gene corrected' -> derived from PD donor -> 1
|
| 75 |
+
# - 'inserted' (G2019S introduced into healthy donor) -> 0
|
| 76 |
+
if "patient" in v_low:
|
| 77 |
+
return 1
|
| 78 |
+
if "healthy" in v_low:
|
| 79 |
+
return 0
|
| 80 |
+
if "gene corrected" in v_low or "corrected" in v_low:
|
| 81 |
+
return 1
|
| 82 |
+
if "inserted" in v_low:
|
| 83 |
+
return 0
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
def convert_age(x):
|
| 87 |
+
v = _extract_value(x)
|
| 88 |
+
if v is None:
|
| 89 |
+
return None
|
| 90 |
+
v = v.replace("years", "").replace("year", "").strip()
|
| 91 |
+
try:
|
| 92 |
+
return float(v)
|
| 93 |
+
except Exception:
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
def convert_gender(x):
|
| 97 |
+
v = _extract_value(x)
|
| 98 |
+
if v is None:
|
| 99 |
+
return None
|
| 100 |
+
v_low = v.lower()
|
| 101 |
+
if v_low in ["female", "f", "woman", "girl"]:
|
| 102 |
+
return 0
|
| 103 |
+
if v_low in ["male", "m", "man", "boy"]:
|
| 104 |
+
return 1
|
| 105 |
+
return None
|
| 106 |
+
|
| 107 |
+
# Step 3: Save metadata with initial filtering
|
| 108 |
+
is_trait_available = trait_row is not None
|
| 109 |
+
_ = validate_and_save_cohort_info(
|
| 110 |
+
is_final=False,
|
| 111 |
+
cohort=cohort,
|
| 112 |
+
info_path=json_path,
|
| 113 |
+
is_gene_available=is_gene_available,
|
| 114 |
+
is_trait_available=is_trait_available
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Step 4: Clinical feature extraction (only if trait_row is not None)
|
| 118 |
+
if trait_row is not None:
|
| 119 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 120 |
+
clinical_df=clinical_data,
|
| 121 |
+
trait=trait,
|
| 122 |
+
trait_row=trait_row,
|
| 123 |
+
convert_trait=convert_trait,
|
| 124 |
+
age_row=age_row,
|
| 125 |
+
convert_age=convert_age,
|
| 126 |
+
gender_row=gender_row,
|
| 127 |
+
convert_gender=convert_gender
|
| 128 |
+
)
|
| 129 |
+
clinical_preview = preview_df(selected_clinical_df)
|
| 130 |
+
print("Clinical data preview:", clinical_preview)
|
| 131 |
+
|
| 132 |
+
# Save clinical data
|
| 133 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 134 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 135 |
+
|
| 136 |
+
# Step 3: Gene Data Extraction
|
| 137 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 138 |
+
gene_data = get_genetic_data(matrix_file)
|
| 139 |
+
|
| 140 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 141 |
+
print(gene_data.index[:20])
|
| 142 |
+
|
| 143 |
+
# Step 4: Gene Identifier Review
|
| 144 |
+
print("requires_gene_mapping = True")
|
| 145 |
+
|
| 146 |
+
# Step 5: Gene Annotation
|
| 147 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 148 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 149 |
+
|
| 150 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 151 |
+
print("Gene annotation preview:")
|
| 152 |
+
print(preview_df(gene_annotation))
|
| 153 |
+
|
| 154 |
+
# Step 6: Gene Identifier Mapping
|
| 155 |
+
import os
|
| 156 |
+
import re
|
| 157 |
+
|
| 158 |
+
# Ensure we use the platform (GPL) SOFT file for annotation, which typically contains gene symbols
|
| 159 |
+
files = os.listdir(in_cohort_dir)
|
| 160 |
+
gpl_softs = [f for f in files if ('soft' in f.lower()) and ('gpl' in f.lower())]
|
| 161 |
+
platform_soft_file = os.path.join(in_cohort_dir, gpl_softs[0]) if gpl_softs else soft_file
|
| 162 |
+
|
| 163 |
+
# Load gene annotation from the platform SOFT
|
| 164 |
+
gene_annotation = get_gene_annotation(platform_soft_file)
|
| 165 |
+
|
| 166 |
+
# Decide identifier and gene symbol columns
|
| 167 |
+
id_col = 'ID'
|
| 168 |
+
non_id_cols = [c for c in gene_annotation.columns if c != id_col]
|
| 169 |
+
|
| 170 |
+
# Priority search for standard gene symbol columns
|
| 171 |
+
priority_names = [
|
| 172 |
+
'Gene Symbol', 'GENE_SYMBOL', 'Symbol', 'SYMBOL', 'Gene symbol',
|
| 173 |
+
'GENE', 'GeneSymbol', 'GENE_SYMBOLS', 'Gene Symbols', 'GENE SYMBOL',
|
| 174 |
+
'gene_assignment', 'GENEASSIGNMENT'
|
| 175 |
+
]
|
| 176 |
+
# Build a case-insensitive map
|
| 177 |
+
lower_to_orig = {str(c).lower(): c for c in gene_annotation.columns}
|
| 178 |
+
gene_col = None
|
| 179 |
+
for name in priority_names:
|
| 180 |
+
key = name.lower()
|
| 181 |
+
if key in lower_to_orig:
|
| 182 |
+
gene_col = lower_to_orig[key]
|
| 183 |
+
break
|
| 184 |
+
|
| 185 |
+
# If no priority column found, pick the non-ID column with best extractable human symbol coverage
|
| 186 |
+
if gene_col is None:
|
| 187 |
+
best_col = None
|
| 188 |
+
best_coverage = -1.0
|
| 189 |
+
for col in non_id_cols:
|
| 190 |
+
try:
|
| 191 |
+
symbols_series = gene_annotation[col].apply(extract_human_gene_symbols)
|
| 192 |
+
coverage = (symbols_series.apply(lambda lst: len(lst) > 0)).mean()
|
| 193 |
+
if coverage > best_coverage:
|
| 194 |
+
best_coverage = coverage
|
| 195 |
+
best_col = col
|
| 196 |
+
except Exception:
|
| 197 |
+
continue
|
| 198 |
+
gene_col = best_col
|
| 199 |
+
|
| 200 |
+
# Build mapping dataframe and apply to convert to gene-level data
|
| 201 |
+
probe_level_df = gene_data.copy()
|
| 202 |
+
|
| 203 |
+
if gene_col is None:
|
| 204 |
+
raise RuntimeError("Failed to identify a gene symbol column in the platform annotation; cannot perform required mapping.")
|
| 205 |
+
|
| 206 |
+
print(f"Selected annotation column for gene symbols: {gene_col}")
|
| 207 |
+
|
| 208 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=id_col, gene_col=gene_col)
|
| 209 |
+
|
| 210 |
+
# Diagnostics before mapping
|
| 211 |
+
n_expr_probes = len(probe_level_df.index)
|
| 212 |
+
n_map_probes = mapping_df['ID'].nunique()
|
| 213 |
+
n_overlap_probes = len(set(probe_level_df.index).intersection(set(mapping_df['ID'])))
|
| 214 |
+
print(f"Probes in expression data: {n_expr_probes}")
|
| 215 |
+
print(f"Unique probes in mapping table: {n_map_probes}")
|
| 216 |
+
print(f"Overlapping probes (mappable): {n_overlap_probes}")
|
| 217 |
+
|
| 218 |
+
# Apply mapping to convert probe-level data to gene-level data
|
| 219 |
+
gene_mapped_df = apply_gene_mapping(expression_df=probe_level_df, mapping_df=mapping_df)
|
| 220 |
+
|
| 221 |
+
if gene_mapped_df.shape[0] == 0 or n_overlap_probes == 0:
|
| 222 |
+
raise RuntimeError("Gene mapping yielded no results. Please verify the platform annotation and gene symbol column.")
|
| 223 |
+
else:
|
| 224 |
+
print(f"Mapped to {gene_mapped_df.shape[0]} genes.")
|
| 225 |
+
gene_data = gene_mapped_df
|
| 226 |
+
|
| 227 |
+
# Step 7: Gene Identifier Mapping
|
| 228 |
+
import os
|
| 229 |
+
from typing import Optional, Tuple
|
| 230 |
+
|
| 231 |
+
# Reuse probe-level data from previous steps
|
| 232 |
+
probe_level_df = gene_data.copy()
|
| 233 |
+
|
| 234 |
+
def find_best_annotation_and_gene_col(probe_ids, primary_soft: str, search_root: str) -> Tuple[Optional[str], Optional[str]]:
|
| 235 |
+
# Collect candidate GPL SOFT files (.soft.gz) prioritizing the one in-cohort, then others under trait dir
|
| 236 |
+
candidates = []
|
| 237 |
+
if primary_soft and os.path.basename(primary_soft).lower().endswith(".soft.gz"):
|
| 238 |
+
candidates.append(primary_soft)
|
| 239 |
+
|
| 240 |
+
for root, _, files in os.walk(search_root):
|
| 241 |
+
for f in files:
|
| 242 |
+
fname = f.lower()
|
| 243 |
+
if ("gpl" in fname) and fname.endswith(".soft.gz"):
|
| 244 |
+
full = os.path.join(root, f)
|
| 245 |
+
if full != primary_soft:
|
| 246 |
+
candidates.append(full)
|
| 247 |
+
|
| 248 |
+
best = (None, None, -1.0) # (file, gene_col, score)
|
| 249 |
+
|
| 250 |
+
# Exclude known non-gene columns
|
| 251 |
+
exclude_cols_lower = {
|
| 252 |
+
'range_strand', 'range_start', 'range_end', 'total_probes',
|
| 253 |
+
'spot_id', 'range_gb', 'gb_acc'
|
| 254 |
+
}
|
| 255 |
+
priority_names = [
|
| 256 |
+
'Gene Symbol', 'GENE_SYMBOL', 'Symbol', 'SYMBOL', 'Gene symbol',
|
| 257 |
+
'GENE', 'GeneSymbol', 'GENE_SYMBOLS', 'Gene Symbols', 'GENE SYMBOL',
|
| 258 |
+
'gene_assignment', 'GENEASSIGNMENT', 'Gene Title', 'GENE_NAME', 'GENE NAME',
|
| 259 |
+
'DESCRIPTION', 'Definition', 'Annot', 'GENENAME'
|
| 260 |
+
]
|
| 261 |
+
|
| 262 |
+
expr_probe_set = set(map(str, probe_ids))
|
| 263 |
+
|
| 264 |
+
for cand in candidates:
|
| 265 |
+
try:
|
| 266 |
+
ann = get_gene_annotation(cand)
|
| 267 |
+
except Exception as e:
|
| 268 |
+
print(f"Skipping {cand}: failed to read annotation ({e})")
|
| 269 |
+
continue
|
| 270 |
+
|
| 271 |
+
if 'ID' not in ann.columns:
|
| 272 |
+
print(f"Skipping {cand}: no 'ID' column in annotation.")
|
| 273 |
+
continue
|
| 274 |
+
|
| 275 |
+
# Check probe ID overlap
|
| 276 |
+
ann_ids = set(ann['ID'].astype(str))
|
| 277 |
+
overlap_ids = ann_ids.intersection(expr_probe_set)
|
| 278 |
+
if not overlap_ids:
|
| 279 |
+
print(f"Skipping {cand}: no probe ID overlap with expression data.")
|
| 280 |
+
continue
|
| 281 |
+
overlap_ratio = len(overlap_ids) / len(expr_probe_set)
|
| 282 |
+
|
| 283 |
+
# Prepare candidate gene symbol columns
|
| 284 |
+
lower_to_orig = {str(c).lower(): c for c in ann.columns}
|
| 285 |
+
gene_cols_ordered = []
|
| 286 |
+
for nm in priority_names:
|
| 287 |
+
key = nm.lower()
|
| 288 |
+
if key in lower_to_orig and key not in exclude_cols_lower:
|
| 289 |
+
gene_cols_ordered.append(lower_to_orig[key])
|
| 290 |
+
|
| 291 |
+
for c in ann.columns:
|
| 292 |
+
lc = str(c).lower()
|
| 293 |
+
if (any(tok in lc for tok in ['symbol', 'gene', 'title', 'name', 'assign', 'desc'])) and (lc not in exclude_cols_lower):
|
| 294 |
+
if c not in gene_cols_ordered:
|
| 295 |
+
gene_cols_ordered.append(c)
|
| 296 |
+
|
| 297 |
+
if not gene_cols_ordered:
|
| 298 |
+
# Fallback: all non-ID columns except excluded
|
| 299 |
+
gene_cols_ordered = [c for c in ann.columns if c != 'ID' and str(c).lower() not in exclude_cols_lower]
|
| 300 |
+
|
| 301 |
+
if not gene_cols_ordered:
|
| 302 |
+
print(f"Skipping {cand}: no candidate gene columns found.")
|
| 303 |
+
continue
|
| 304 |
+
|
| 305 |
+
# Evaluate each candidate column by coverage of extractable symbols and ID overlap
|
| 306 |
+
best_local_col, best_local_score = None, -1.0
|
| 307 |
+
for gc in gene_cols_ordered:
|
| 308 |
+
try:
|
| 309 |
+
sample_series = ann[gc].astype(str).iloc[:5000]
|
| 310 |
+
symbols_series = sample_series.apply(extract_human_gene_symbols)
|
| 311 |
+
coverage = (symbols_series.apply(lambda lst: len(lst) > 0)).mean()
|
| 312 |
+
score = overlap_ratio * coverage
|
| 313 |
+
if score > best_local_score:
|
| 314 |
+
best_local_score = score
|
| 315 |
+
best_local_col = gc
|
| 316 |
+
except Exception:
|
| 317 |
+
continue
|
| 318 |
+
|
| 319 |
+
if best_local_col is not None and best_local_score > best[2]:
|
| 320 |
+
best = (cand, best_local_col, best_local_score)
|
| 321 |
+
|
| 322 |
+
return best[0], best[1]
|
| 323 |
+
|
| 324 |
+
# 1) Locate the best platform annotation and gene symbol column
|
| 325 |
+
files = os.listdir(in_cohort_dir)
|
| 326 |
+
gpl_softs = [f for f in files if ('soft' in f.lower()) and ('gpl' in f.lower())]
|
| 327 |
+
platform_soft_file = os.path.join(in_cohort_dir, gpl_softs[0]) if gpl_softs else soft_file
|
| 328 |
+
|
| 329 |
+
best_file, gene_col = find_best_annotation_and_gene_col(
|
| 330 |
+
probe_ids=probe_level_df.index,
|
| 331 |
+
primary_soft=platform_soft_file,
|
| 332 |
+
search_root=in_trait_dir
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
mapped_successfully = False
|
| 336 |
+
if best_file is None or gene_col is None:
|
| 337 |
+
print("WARNING: Unable to identify a gene-symbol column in available GPL annotations. Proceeding with probe-level data.")
|
| 338 |
+
gene_data = probe_level_df
|
| 339 |
+
else:
|
| 340 |
+
print(f"Using annotation file: {best_file}")
|
| 341 |
+
print(f"Identifier column: ID")
|
| 342 |
+
print(f"Selected annotation column for gene symbols: {gene_col}")
|
| 343 |
+
|
| 344 |
+
gene_annotation = get_gene_annotation(best_file)
|
| 345 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col=gene_col)
|
| 346 |
+
|
| 347 |
+
n_expr_probes = len(probe_level_df.index)
|
| 348 |
+
n_map_probes = mapping_df['ID'].nunique()
|
| 349 |
+
n_overlap_probes = len(set(map(str, probe_level_df.index)).intersection(set(mapping_df['ID'])))
|
| 350 |
+
print(f"Probes in expression data: {n_expr_probes}")
|
| 351 |
+
print(f"Unique probes in mapping table: {n_map_probes}")
|
| 352 |
+
print(f"Overlapping probes (mappable): {n_overlap_probes}")
|
| 353 |
+
|
| 354 |
+
if n_overlap_probes == 0:
|
| 355 |
+
print("WARNING: No overlapping probes between expression data and mapping table. Using probe-level data.")
|
| 356 |
+
gene_data = probe_level_df
|
| 357 |
+
else:
|
| 358 |
+
try:
|
| 359 |
+
gene_mapped_df = apply_gene_mapping(expression_df=probe_level_df, mapping_df=mapping_df)
|
| 360 |
+
if gene_mapped_df.shape[0] > 0:
|
| 361 |
+
print(f"Mapped to {gene_mapped_df.shape[0]} genes.")
|
| 362 |
+
gene_data = gene_mapped_df
|
| 363 |
+
mapped_successfully = True
|
| 364 |
+
else:
|
| 365 |
+
print("WARNING: Gene mapping produced an empty gene table. Using probe-level data.")
|
| 366 |
+
gene_data = probe_level_df
|
| 367 |
+
except Exception as e:
|
| 368 |
+
print(f"WARNING: Gene mapping failed with error: {e}. Using probe-level data.")
|
| 369 |
+
gene_data = probe_level_df
|
| 370 |
+
|
| 371 |
+
# At this point, gene_data is guaranteed to be set (either gene-level or probe-level).
|
| 372 |
+
|
| 373 |
+
# Step 8: Data Normalization and Linking
|
| 374 |
+
import os
|
| 375 |
+
import pandas as pd
|
| 376 |
+
|
| 377 |
+
# 1) Normalize gene symbols if applicable; fallback to probe-level if normalization eliminates most rows
|
| 378 |
+
note_msgs = []
|
| 379 |
+
|
| 380 |
+
# Ensure gene_data exists from previous steps
|
| 381 |
+
gene_df_input = gene_data
|
| 382 |
+
|
| 383 |
+
try:
|
| 384 |
+
norm_df = normalize_gene_symbols_in_index(gene_df_input.copy())
|
| 385 |
+
retained = len(norm_df)
|
| 386 |
+
original = max(1, len(gene_df_input))
|
| 387 |
+
retention_ratio = retained / original
|
| 388 |
+
|
| 389 |
+
# If normalization loses essentially all rows, treat input as probe-level and skip normalization
|
| 390 |
+
if retained == 0 or retention_ratio < 0.01:
|
| 391 |
+
normalized_gene_data = gene_df_input
|
| 392 |
+
note_msgs.append(f"WARNING: Gene symbol normalization skipped; expression index appears to be probe IDs (retention {retention_ratio:.4f}). Using probe-level data.")
|
| 393 |
+
else:
|
| 394 |
+
normalized_gene_data = norm_df
|
| 395 |
+
note_msgs.append(f"INFO: Gene symbols normalized using synonym dictionary; retained {retained} of {original} rows ({retention_ratio:.2%}).")
|
| 396 |
+
except Exception as e:
|
| 397 |
+
# If normalization fails for any reason, fall back to probe-level
|
| 398 |
+
normalized_gene_data = gene_df_input
|
| 399 |
+
note_msgs.append(f"WARNING: Gene symbol normalization failed with error: {e}. Using probe-level data.")
|
| 400 |
+
|
| 401 |
+
# Ensure output directory exists and save gene data
|
| 402 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 403 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 404 |
+
|
| 405 |
+
# 2) Link clinical and genetic data
|
| 406 |
+
# Use the in-memory clinical dataframe if available; otherwise load from saved file.
|
| 407 |
+
try:
|
| 408 |
+
selected_clinical_df # noqa: F401
|
| 409 |
+
clinical_df_to_use = selected_clinical_df
|
| 410 |
+
except NameError:
|
| 411 |
+
clinical_df_to_use = pd.read_csv(out_clinical_data_file, index_col=0)
|
| 412 |
+
|
| 413 |
+
linked_data = geo_link_clinical_genetic_data(clinical_df_to_use, normalized_gene_data)
|
| 414 |
+
|
| 415 |
+
# 3) Handle missing values in the linked data
|
| 416 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 417 |
+
|
| 418 |
+
# 4) Determine bias and remove biased demographic features
|
| 419 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 420 |
+
|
| 421 |
+
# 5) Final validation and save cohort metadata
|
| 422 |
+
is_gene_available_final = (normalized_gene_data.shape[0] > 0) and (normalized_gene_data.shape[1] > 0)
|
| 423 |
+
is_trait_available_final = (trait in unbiased_linked_data.columns) and (len(unbiased_linked_data) > 0)
|
| 424 |
+
|
| 425 |
+
note = " ".join(note_msgs) if note_msgs else "INFO: Processing completed without special notes."
|
| 426 |
+
is_usable = validate_and_save_cohort_info(
|
| 427 |
+
is_final=True,
|
| 428 |
+
cohort=cohort,
|
| 429 |
+
info_path=json_path,
|
| 430 |
+
is_gene_available=is_gene_available_final,
|
| 431 |
+
is_trait_available=is_trait_available_final,
|
| 432 |
+
is_biased=is_trait_biased,
|
| 433 |
+
df=unbiased_linked_data,
|
| 434 |
+
note=note
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
# 6) Save linked data only if usable
|
| 438 |
+
if is_usable:
|
| 439 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 440 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Parkinsons_Disease/code/GSE103099.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE103099"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE103099"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE103099.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE103099.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE103099.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Determine data availability based on provided background and sample characteristics
|
| 40 |
+
# This series is non-human (Microcebus murinus), and trait/age/gender are either non-human or constant.
|
| 41 |
+
is_gene_available = True # Likely mRNA gene expression data in a GEO SuperSeries
|
| 42 |
+
trait_row = None # No human Parkinson's Disease status available
|
| 43 |
+
age_row = None # Non-human and constant ("2 year old")
|
| 44 |
+
gender_row = None # Non-human and constant ("female")
|
| 45 |
+
is_trait_available = trait_row is not None
|
| 46 |
+
|
| 47 |
+
# Converters
|
| 48 |
+
def _extract_value(cell):
|
| 49 |
+
if cell is None:
|
| 50 |
+
return None
|
| 51 |
+
s = str(cell).strip()
|
| 52 |
+
# Get text after the first colon if present
|
| 53 |
+
if ':' in s:
|
| 54 |
+
s = s.split(':', 1)[1].strip()
|
| 55 |
+
return s
|
| 56 |
+
|
| 57 |
+
def convert_trait(cell):
|
| 58 |
+
# Binary: 1 = Parkinson's Disease, 0 = Control
|
| 59 |
+
s = _extract_value(cell)
|
| 60 |
+
if s is None:
|
| 61 |
+
return None
|
| 62 |
+
sl = s.lower()
|
| 63 |
+
# Unknown or missing
|
| 64 |
+
if sl in {'na', 'n/a', 'nan', 'none', 'unknown', ''}:
|
| 65 |
+
return None
|
| 66 |
+
# Heuristics for PD vs Control
|
| 67 |
+
case_kw = ['parkinson', "parkinson's", 'pd', 'parkinson’s']
|
| 68 |
+
ctrl_kw = ['control', 'healthy', 'normal', 'no parkinson', 'non-parkinson', 'non parkinson', 'without parkinson']
|
| 69 |
+
if any(k in sl for k in case_kw):
|
| 70 |
+
# Guard for explicit negatives
|
| 71 |
+
if any(neg in sl for neg in ['no parkinson', 'non-parkinson', 'non parkinson', 'without parkinson']):
|
| 72 |
+
return 0
|
| 73 |
+
return 1
|
| 74 |
+
if any(k in sl for k in ctrl_kw):
|
| 75 |
+
return 0
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
def convert_age(cell):
|
| 79 |
+
# Continuous: age in years (float)
|
| 80 |
+
s = _extract_value(cell)
|
| 81 |
+
if s is None:
|
| 82 |
+
return None
|
| 83 |
+
sl = s.lower()
|
| 84 |
+
if sl in {'na', 'n/a', 'nan', 'none', 'unknown', ''}:
|
| 85 |
+
return None
|
| 86 |
+
import re
|
| 87 |
+
# Extract the first number (int or float)
|
| 88 |
+
m = re.search(r'([-+]?\d*\.?\d+)', sl)
|
| 89 |
+
if not m:
|
| 90 |
+
return None
|
| 91 |
+
val = float(m.group(1))
|
| 92 |
+
# Determine units
|
| 93 |
+
if 'year' in sl or 'yr' in sl or 'y/o' in sl or 'yo' in sl:
|
| 94 |
+
return val
|
| 95 |
+
if 'month' in sl or 'mo' in sl:
|
| 96 |
+
return val / 12.0
|
| 97 |
+
if 'week' in sl or 'wk' in sl or 'wks' in sl:
|
| 98 |
+
return val / 52.0
|
| 99 |
+
if 'day' in sl or 'd ' in sl or sl.endswith('d'):
|
| 100 |
+
return val / 365.0
|
| 101 |
+
# Default assume years if unit unspecified
|
| 102 |
+
return val
|
| 103 |
+
|
| 104 |
+
def convert_gender(cell):
|
| 105 |
+
# Binary: female=0, male=1
|
| 106 |
+
s = _extract_value(cell)
|
| 107 |
+
if s is None:
|
| 108 |
+
return None
|
| 109 |
+
sl = s.lower()
|
| 110 |
+
if sl in {'na', 'n/a', 'nan', 'none', 'unknown', ''}:
|
| 111 |
+
return None
|
| 112 |
+
if sl in {'female', 'f', 'woman', 'girl'}:
|
| 113 |
+
return 0
|
| 114 |
+
if sl in {'male', 'm', 'man', 'boy'}:
|
| 115 |
+
return 1
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
# Save initial metadata (initial filtering)
|
| 119 |
+
_ = validate_and_save_cohort_info(
|
| 120 |
+
is_final=False,
|
| 121 |
+
cohort=cohort,
|
| 122 |
+
info_path=json_path,
|
| 123 |
+
is_gene_available=is_gene_available,
|
| 124 |
+
is_trait_available=is_trait_available
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# Clinical feature extraction is skipped because trait_row is None (no usable human clinical data).
|
| 128 |
+
# If in another scenario trait_row is not None, the following would be used:
|
| 129 |
+
# selected_clinical_df = geo_select_clinical_features(
|
| 130 |
+
# clinical_df=clinical_data,
|
| 131 |
+
# trait=trait,
|
| 132 |
+
# trait_row=trait_row,
|
| 133 |
+
# convert_trait=convert_trait,
|
| 134 |
+
# age_row=age_row,
|
| 135 |
+
# convert_age=convert_age,
|
| 136 |
+
# gender_row=gender_row,
|
| 137 |
+
# convert_gender=convert_gender
|
| 138 |
+
# )
|
| 139 |
+
# _ = preview_df(selected_clinical_df)
|
| 140 |
+
# os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 141 |
+
# selected_clinical_df.to_csv(out_clinical_data_file)
|
output/preprocess/Parkinsons_Disease/code/GSE202665.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE202665"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE202665"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE202665.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE202665.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE202665.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
import re
|
| 41 |
+
|
| 42 |
+
# 1) Gene Expression Data Availability
|
| 43 |
+
is_gene_available = True # mRNA array platform; not miRNA or methylation per series title/summary.
|
| 44 |
+
|
| 45 |
+
# 2) Variable Availability and Data Type Conversion
|
| 46 |
+
|
| 47 |
+
# Identify rows in the Sample Characteristics Dictionary
|
| 48 |
+
trait_row = 0 # 'disease state: Healthy Control' / "disease state: Parkinson's disease"
|
| 49 |
+
age_row = 3 # 'age: <number>'
|
| 50 |
+
gender_row = None # Only 'gender: male' appears -> constant -> not useful
|
| 51 |
+
|
| 52 |
+
def _after_colon(x):
|
| 53 |
+
if x is None:
|
| 54 |
+
return None
|
| 55 |
+
if not isinstance(x, str):
|
| 56 |
+
x = str(x)
|
| 57 |
+
parts = x.split(':', 1)
|
| 58 |
+
return parts[1].strip() if len(parts) > 1 else x.strip()
|
| 59 |
+
|
| 60 |
+
def convert_trait(x):
|
| 61 |
+
v = _after_colon(x)
|
| 62 |
+
if v is None:
|
| 63 |
+
return None
|
| 64 |
+
lv = v.lower()
|
| 65 |
+
if 'parkinson' in lv:
|
| 66 |
+
return 1
|
| 67 |
+
if 'control' in lv:
|
| 68 |
+
return 0
|
| 69 |
+
if lv in {'na', 'n/a', 'unknown', ''}:
|
| 70 |
+
return None
|
| 71 |
+
return None
|
| 72 |
+
|
| 73 |
+
def convert_age(x):
|
| 74 |
+
v = _after_colon(x)
|
| 75 |
+
if v is None:
|
| 76 |
+
return None
|
| 77 |
+
m = re.search(r'\d+(\.\d+)?', v)
|
| 78 |
+
if not m:
|
| 79 |
+
return None
|
| 80 |
+
num = float(m.group(0))
|
| 81 |
+
return int(num) if num.is_integer() else num
|
| 82 |
+
|
| 83 |
+
def convert_gender(x):
|
| 84 |
+
v = _after_colon(x)
|
| 85 |
+
if v is None:
|
| 86 |
+
return None
|
| 87 |
+
lv = v.lower()
|
| 88 |
+
if 'male' in lv:
|
| 89 |
+
return 1
|
| 90 |
+
if 'female' in lv:
|
| 91 |
+
return 0
|
| 92 |
+
if lv in {'na', 'n/a', 'unknown', ''}:
|
| 93 |
+
return None
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
# 3) Save Metadata (initial filtering)
|
| 97 |
+
is_trait_available = trait_row is not None
|
| 98 |
+
_ = validate_and_save_cohort_info(
|
| 99 |
+
is_final=False,
|
| 100 |
+
cohort=cohort,
|
| 101 |
+
info_path=json_path,
|
| 102 |
+
is_gene_available=is_gene_available,
|
| 103 |
+
is_trait_available=is_trait_available
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# 4) Clinical Feature Extraction (only if clinical data is available)
|
| 107 |
+
if trait_row is not None:
|
| 108 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 109 |
+
clinical_df=clinical_data,
|
| 110 |
+
trait=trait,
|
| 111 |
+
trait_row=trait_row,
|
| 112 |
+
convert_trait=convert_trait,
|
| 113 |
+
age_row=age_row,
|
| 114 |
+
convert_age=convert_age,
|
| 115 |
+
gender_row=gender_row,
|
| 116 |
+
convert_gender=convert_gender
|
| 117 |
+
)
|
| 118 |
+
preview = preview_df(selected_clinical_df)
|
| 119 |
+
print(preview)
|
| 120 |
+
|
| 121 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 122 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 123 |
+
|
| 124 |
+
# Step 3: Gene Data Extraction
|
| 125 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 126 |
+
gene_data = get_genetic_data(matrix_file)
|
| 127 |
+
|
| 128 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 129 |
+
print(gene_data.index[:20])
|
| 130 |
+
|
| 131 |
+
# Step 4: Gene Identifier Review
|
| 132 |
+
# The gene identifiers are numeric strings ('1', '2', ...), which are not human gene symbols.
|
| 133 |
+
requires_gene_mapping = True
|
| 134 |
+
print(f"requires_gene_mapping = {requires_gene_mapping}")
|
| 135 |
+
|
| 136 |
+
# Step 5: Gene Annotation
|
| 137 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 138 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 139 |
+
|
| 140 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 141 |
+
print("Gene annotation preview:")
|
| 142 |
+
print(preview_df(gene_annotation))
|
| 143 |
+
|
| 144 |
+
# Step 6: Gene Identifier Mapping
|
| 145 |
+
# Determine the appropriate columns for probe IDs and gene symbols based on the annotation preview
|
| 146 |
+
probe_col = 'ID' # Matches the numeric IDs in the expression data index
|
| 147 |
+
gene_symbol_col = 'GENE_SYMBOL' # Contains human gene symbols
|
| 148 |
+
|
| 149 |
+
# Build the mapping dataframe
|
| 150 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=gene_symbol_col)
|
| 151 |
+
|
| 152 |
+
# Apply the mapping to convert probe-level data to gene-level expression
|
| 153 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 154 |
+
|
| 155 |
+
# Step 7: Data Normalization and Linking
|
| 156 |
+
import os
|
| 157 |
+
import json
|
| 158 |
+
|
| 159 |
+
# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.
|
| 160 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 161 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 162 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 163 |
+
|
| 164 |
+
# 2. Link the clinical and genetic data with the 'geo_link_clinical_genetic_data' function from the library.
|
| 165 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 166 |
+
|
| 167 |
+
# 3. Handle missing values in the linked data
|
| 168 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 169 |
+
|
| 170 |
+
# 4. Determine whether the trait and some demographic features are severely biased, and remove biased features.
|
| 171 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 172 |
+
|
| 173 |
+
# Ensure flags are native Python bools
|
| 174 |
+
is_gene_available_flag = bool((normalized_gene_data.shape[0] > 0) and (normalized_gene_data.shape[1] > 0))
|
| 175 |
+
is_trait_available_flag = bool((trait in selected_clinical_df.index) and bool(selected_clinical_df.loc[trait].notna().any()))
|
| 176 |
+
is_trait_biased = bool(is_trait_biased)
|
| 177 |
+
|
| 178 |
+
note = ("INFO: Gender not included (all male per cohort description); time-course CD4+ T-cell activation "
|
| 179 |
+
"samples linked with disease status and age.")
|
| 180 |
+
|
| 181 |
+
# Pre-sanitize existing cohort_info JSON to avoid numpy/pandas dtypes breaking json.dump
|
| 182 |
+
def _coerce_json_primitives(obj):
|
| 183 |
+
# Convert to JSON-serializable Python primitives
|
| 184 |
+
if obj is None or isinstance(obj, (str, int, float, bool)):
|
| 185 |
+
return obj
|
| 186 |
+
if isinstance(obj, dict):
|
| 187 |
+
return {str(k): _coerce_json_primitives(v) for k, v in obj.items()}
|
| 188 |
+
if isinstance(obj, (list, tuple)):
|
| 189 |
+
return [_coerce_json_primitives(x) for x in obj]
|
| 190 |
+
# Try .item() for numpy scalars
|
| 191 |
+
try:
|
| 192 |
+
return _coerce_json_primitives(obj.item())
|
| 193 |
+
except Exception:
|
| 194 |
+
# Fallback to string representation
|
| 195 |
+
return str(obj)
|
| 196 |
+
|
| 197 |
+
try:
|
| 198 |
+
os.makedirs(os.path.dirname(json_path), exist_ok=True)
|
| 199 |
+
if os.path.exists(json_path):
|
| 200 |
+
with open(json_path, "r") as f:
|
| 201 |
+
existing_records = json.load(f)
|
| 202 |
+
existing_records = _coerce_json_primitives(existing_records)
|
| 203 |
+
with open(json_path, "w") as f:
|
| 204 |
+
json.dump(existing_records, f)
|
| 205 |
+
except Exception as _e:
|
| 206 |
+
# Do not interrupt pipeline if sanitization fails
|
| 207 |
+
pass
|
| 208 |
+
|
| 209 |
+
# 5. Conduct quality check and save the cohort information.
|
| 210 |
+
is_usable = validate_and_save_cohort_info(
|
| 211 |
+
is_final=True,
|
| 212 |
+
cohort=cohort,
|
| 213 |
+
info_path=json_path,
|
| 214 |
+
is_gene_available=is_gene_available_flag,
|
| 215 |
+
is_trait_available=is_trait_available_flag,
|
| 216 |
+
is_biased=is_trait_biased,
|
| 217 |
+
df=unbiased_linked_data,
|
| 218 |
+
note=note
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
# 6. If the linked data is usable, save it as a CSV file to 'out_data_file'.
|
| 222 |
+
if is_usable:
|
| 223 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 224 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Parkinsons_Disease/code/GSE202667.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE202667"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE202667"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE202667.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE202667.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE202667.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import re
|
| 40 |
+
|
| 41 |
+
# 1. Gene Expression Data Availability
|
| 42 |
+
is_gene_available = True # RNA signatures of CD4+ T cells indicate gene expression data
|
| 43 |
+
|
| 44 |
+
# 2. Variable Availability and Data Type Conversion
|
| 45 |
+
|
| 46 |
+
# 2.1 Identify rows
|
| 47 |
+
trait_row = 0 # disease state: Parkinson's disease vs Healthy Control
|
| 48 |
+
age_row = 3 # age: values present
|
| 49 |
+
# Gender appears constant as male only -> not useful
|
| 50 |
+
gender_row = None
|
| 51 |
+
|
| 52 |
+
# 2.2 Converters
|
| 53 |
+
def _after_colon(value):
|
| 54 |
+
if value is None:
|
| 55 |
+
return ""
|
| 56 |
+
s = str(value)
|
| 57 |
+
parts = s.split(":", 1)
|
| 58 |
+
return parts[1].strip() if len(parts) == 2 else s.strip()
|
| 59 |
+
|
| 60 |
+
def convert_trait(value):
|
| 61 |
+
v = _after_colon(value).strip().lower()
|
| 62 |
+
if v in {"", "na", "n/a", "nan", "none", "unknown"}:
|
| 63 |
+
return None
|
| 64 |
+
# Map PD vs control
|
| 65 |
+
if "parkinson" in v or v == "pd":
|
| 66 |
+
return 1
|
| 67 |
+
if "healthy" in v or "control" in v:
|
| 68 |
+
return 0
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
def convert_age(value):
|
| 72 |
+
v = _after_colon(value)
|
| 73 |
+
m = re.search(r'[-+]?\d+(\.\d+)?', v)
|
| 74 |
+
if not m:
|
| 75 |
+
return None
|
| 76 |
+
try:
|
| 77 |
+
return float(m.group(0))
|
| 78 |
+
except Exception:
|
| 79 |
+
return None
|
| 80 |
+
|
| 81 |
+
def convert_gender(value):
|
| 82 |
+
v = _after_colon(value).strip().lower()
|
| 83 |
+
if v in {"", "na", "n/a", "nan", "none", "unknown"}:
|
| 84 |
+
return None
|
| 85 |
+
if "female" in v or v == "f":
|
| 86 |
+
return 0
|
| 87 |
+
if "male" in v or v == "m":
|
| 88 |
+
return 1
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
# 3. Save Metadata (initial filtering)
|
| 92 |
+
is_trait_available = trait_row is not None
|
| 93 |
+
_ = validate_and_save_cohort_info(
|
| 94 |
+
is_final=False,
|
| 95 |
+
cohort=cohort,
|
| 96 |
+
info_path=json_path,
|
| 97 |
+
is_gene_available=is_gene_available,
|
| 98 |
+
is_trait_available=is_trait_available
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# 4. Clinical Feature Extraction
|
| 102 |
+
if trait_row is not None:
|
| 103 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 104 |
+
clinical_df=clinical_data,
|
| 105 |
+
trait=trait,
|
| 106 |
+
trait_row=trait_row,
|
| 107 |
+
convert_trait=convert_trait,
|
| 108 |
+
age_row=age_row,
|
| 109 |
+
convert_age=convert_age,
|
| 110 |
+
gender_row=gender_row,
|
| 111 |
+
convert_gender=convert_gender
|
| 112 |
+
)
|
| 113 |
+
clinical_preview = preview_df(selected_clinical_df)
|
| 114 |
+
print(clinical_preview)
|
| 115 |
+
# Save selected clinical features
|
| 116 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 117 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 118 |
+
|
| 119 |
+
# Step 3: Gene Data Extraction
|
| 120 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 121 |
+
gene_data = get_genetic_data(matrix_file)
|
| 122 |
+
|
| 123 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 124 |
+
print(gene_data.index[:20])
|
| 125 |
+
|
| 126 |
+
# Step 4: Gene Identifier Review
|
| 127 |
+
print("requires_gene_mapping = True")
|
| 128 |
+
|
| 129 |
+
# Step 5: Gene Annotation
|
| 130 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 131 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 132 |
+
|
| 133 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 134 |
+
print("Gene annotation preview:")
|
| 135 |
+
print(preview_df(gene_annotation))
|
| 136 |
+
|
| 137 |
+
# Step 6: Gene Identifier Mapping
|
| 138 |
+
# 1-2. Decide identifier and gene symbol columns and build mapping dataframe
|
| 139 |
+
probe_col = 'ID'
|
| 140 |
+
gene_symbol_col = 'GENE_SYMBOL'
|
| 141 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=gene_symbol_col)
|
| 142 |
+
|
| 143 |
+
# 3. Apply mapping to convert probe-level data to gene-level expression
|
| 144 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 145 |
+
|
| 146 |
+
# Step 7: Data Normalization and Linking
|
| 147 |
+
import os
|
| 148 |
+
import pandas as pd
|
| 149 |
+
|
| 150 |
+
# 1. Normalize gene symbols and save
|
| 151 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 152 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 153 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 154 |
+
|
| 155 |
+
# 2. Link clinical and genetic data
|
| 156 |
+
if 'selected_clinical_df' not in globals():
|
| 157 |
+
selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)
|
| 158 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 159 |
+
|
| 160 |
+
# 3. Handle missing values
|
| 161 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 162 |
+
|
| 163 |
+
# 4. Assess bias and remove biased demographic features
|
| 164 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 165 |
+
|
| 166 |
+
# Ensure columns/index are native Python types to avoid JSON serialization issues in the library
|
| 167 |
+
unbiased_linked_data = unbiased_linked_data.copy()
|
| 168 |
+
unbiased_linked_data.columns = list(unbiased_linked_data.columns)
|
| 169 |
+
unbiased_linked_data.index = unbiased_linked_data.index.astype(str)
|
| 170 |
+
|
| 171 |
+
# 5. Final validation and save cohort info
|
| 172 |
+
is_trait_available = (trait in linked_data.columns) and linked_data[trait].notna().any()
|
| 173 |
+
is_gene_available = (normalized_gene_data.shape[0] > 0 and normalized_gene_data.shape[1] > 0)
|
| 174 |
+
|
| 175 |
+
notes = []
|
| 176 |
+
if 'Gender' not in unbiased_linked_data.columns:
|
| 177 |
+
notes.append("INFO: Gender not available in clinical data.")
|
| 178 |
+
notes.append("INFO: Time-point post activation exists in metadata but was not included as a covariate.")
|
| 179 |
+
|
| 180 |
+
is_usable = validate_and_save_cohort_info(
|
| 181 |
+
is_final=True,
|
| 182 |
+
cohort=cohort,
|
| 183 |
+
info_path=json_path,
|
| 184 |
+
is_gene_available=bool(is_gene_available),
|
| 185 |
+
is_trait_available=bool(is_trait_available),
|
| 186 |
+
is_biased=bool(is_trait_biased),
|
| 187 |
+
df=unbiased_linked_data,
|
| 188 |
+
note=" ".join(notes)
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
# 6. Save linked data if usable
|
| 192 |
+
if is_usable:
|
| 193 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 194 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Parkinsons_Disease/code/GSE30335.py
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE30335"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE30335"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE30335.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE30335.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE30335.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import re
|
| 40 |
+
|
| 41 |
+
# 1) Gene expression data availability
|
| 42 |
+
is_gene_available = True # Whole-blood gene expression microarray study (not miRNA/methylation)
|
| 43 |
+
|
| 44 |
+
# 2) Variable availability and conversion functions
|
| 45 |
+
# From background: no Parkinson's disease status recorded; cohort is all male and age-matched without per-sample ages.
|
| 46 |
+
trait_row = None
|
| 47 |
+
age_row = None
|
| 48 |
+
gender_row = None
|
| 49 |
+
|
| 50 |
+
def _after_colon(x):
|
| 51 |
+
if x is None:
|
| 52 |
+
return None
|
| 53 |
+
s = str(x)
|
| 54 |
+
parts = s.split(":", 1)
|
| 55 |
+
val = parts[1] if len(parts) > 1 else parts[0]
|
| 56 |
+
val = val.strip()
|
| 57 |
+
return val if val else None
|
| 58 |
+
|
| 59 |
+
def convert_trait(x):
|
| 60 |
+
v = _after_colon(x)
|
| 61 |
+
if v is None:
|
| 62 |
+
return None
|
| 63 |
+
vl = v.lower()
|
| 64 |
+
# Parkinson's disease positive indicators
|
| 65 |
+
if "parkinson" in vl or vl in {"pd"}:
|
| 66 |
+
return 1
|
| 67 |
+
if any(tok in vl for tok in ["case", "patient", "affected", "disease", "yes", "present"]):
|
| 68 |
+
return 1
|
| 69 |
+
# Controls/negatives
|
| 70 |
+
if any(tok in vl for tok in ["control", "healthy", "normal", "no", "absent", "unaffected"]):
|
| 71 |
+
return 0
|
| 72 |
+
if vl in {"1", "0"}:
|
| 73 |
+
return int(vl)
|
| 74 |
+
return None
|
| 75 |
+
|
| 76 |
+
def convert_age(x):
|
| 77 |
+
v = _after_colon(x)
|
| 78 |
+
if v is None:
|
| 79 |
+
return None
|
| 80 |
+
m = re.search(r"[-+]?\d*\.?\d+", v)
|
| 81 |
+
if not m:
|
| 82 |
+
return None
|
| 83 |
+
try:
|
| 84 |
+
age = float(m.group())
|
| 85 |
+
except Exception:
|
| 86 |
+
return None
|
| 87 |
+
# Optional sanity check for human age
|
| 88 |
+
if age < 0 or age > 120:
|
| 89 |
+
return None
|
| 90 |
+
return age
|
| 91 |
+
|
| 92 |
+
def convert_gender(x):
|
| 93 |
+
v = _after_colon(x)
|
| 94 |
+
if v is None:
|
| 95 |
+
return None
|
| 96 |
+
vl = v.lower()
|
| 97 |
+
if vl in {"male", "m"}:
|
| 98 |
+
return 1
|
| 99 |
+
if vl in {"female", "f"}:
|
| 100 |
+
return 0
|
| 101 |
+
return None
|
| 102 |
+
|
| 103 |
+
# 3) Save metadata (initial filtering)
|
| 104 |
+
is_trait_available = trait_row is not None
|
| 105 |
+
_ = validate_and_save_cohort_info(
|
| 106 |
+
is_final=False,
|
| 107 |
+
cohort=cohort,
|
| 108 |
+
info_path=json_path,
|
| 109 |
+
is_gene_available=is_gene_available,
|
| 110 |
+
is_trait_available=is_trait_available
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# 4) Clinical feature extraction (skip because trait_row is None)
|
| 114 |
+
# If trait_row becomes available in future, uncomment and use:
|
| 115 |
+
# if trait_row is not None:
|
| 116 |
+
# selected = geo_select_clinical_features(
|
| 117 |
+
# clinical_df=clinical_data,
|
| 118 |
+
# trait=trait,
|
| 119 |
+
# trait_row=trait_row,
|
| 120 |
+
# convert_trait=convert_trait,
|
| 121 |
+
# age_row=age_row,
|
| 122 |
+
# convert_age=convert_age,
|
| 123 |
+
# gender_row=gender_row,
|
| 124 |
+
# convert_gender=convert_gender
|
| 125 |
+
# )
|
| 126 |
+
# preview = preview_df(selected)
|
| 127 |
+
# os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 128 |
+
# selected.to_csv(out_clinical_data_file)
|
| 129 |
+
|
| 130 |
+
# Step 3: Gene Data Extraction
|
| 131 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 132 |
+
gene_data = get_genetic_data(matrix_file)
|
| 133 |
+
|
| 134 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 135 |
+
print(gene_data.index[:20])
|
| 136 |
+
|
| 137 |
+
# Step 4: Gene Identifier Review
|
| 138 |
+
# Affymetrix probe set IDs (e.g., '1007_s_at') are not human gene symbols and require mapping.
|
| 139 |
+
requires_gene_mapping = True
|
| 140 |
+
print(f"requires_gene_mapping = {requires_gene_mapping}")
|
| 141 |
+
|
| 142 |
+
# Step 5: Gene Annotation
|
| 143 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 144 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 145 |
+
|
| 146 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 147 |
+
print("Gene annotation preview:")
|
| 148 |
+
print(preview_df(gene_annotation))
|
| 149 |
+
|
| 150 |
+
# Step 6: Gene Identifier Mapping
|
| 151 |
+
# Decide mapping columns based on previous previews
|
| 152 |
+
probe_col = 'ID' # Affymetrix probe set ID (e.g., '1007_s_at')
|
| 153 |
+
gene_symbol_col = 'Gene Symbol' # Human gene symbols, sometimes multiple per probe separated by delimiters
|
| 154 |
+
|
| 155 |
+
# Build mapping dataframe
|
| 156 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=gene_symbol_col)
|
| 157 |
+
|
| 158 |
+
# Map probe-level expression to gene-level expression
|
| 159 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 160 |
+
|
| 161 |
+
# Step 7: Data Normalization and Linking
|
| 162 |
+
# 1. Normalize gene symbols and save gene expression data
|
| 163 |
+
import os
|
| 164 |
+
|
| 165 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 166 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 167 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 168 |
+
|
| 169 |
+
# 2-6. Link and downstream steps only if clinical data with trait is available; otherwise, record as unavailable
|
| 170 |
+
linked_data = None
|
| 171 |
+
if 'selected_clinical_data' in globals():
|
| 172 |
+
# Proceed only if the trait row exists in the selected clinical data
|
| 173 |
+
try:
|
| 174 |
+
# 2. Link clinical and genetic data
|
| 175 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data)
|
| 176 |
+
|
| 177 |
+
# 3. Handle missing values
|
| 178 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 179 |
+
|
| 180 |
+
# 4. Bias checks
|
| 181 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 182 |
+
|
| 183 |
+
# 5. Final quality validation and metadata save
|
| 184 |
+
is_usable = validate_and_save_cohort_info(
|
| 185 |
+
is_final=True,
|
| 186 |
+
cohort=cohort,
|
| 187 |
+
info_path=json_path,
|
| 188 |
+
is_gene_available=True,
|
| 189 |
+
is_trait_available=True,
|
| 190 |
+
is_biased=is_trait_biased,
|
| 191 |
+
df=unbiased_linked_data,
|
| 192 |
+
note="INFO: Clinical features were provided; completed full preprocessing."
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
# 6. Save linked data if usable
|
| 196 |
+
if is_usable:
|
| 197 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 198 |
+
unbiased_linked_data.to_csv(out_data_file)
|
| 199 |
+
except Exception:
|
| 200 |
+
# If anything goes wrong in linking/processing due to missing trait, record as not available
|
| 201 |
+
_ = validate_and_save_cohort_info(
|
| 202 |
+
is_final=False,
|
| 203 |
+
cohort=cohort,
|
| 204 |
+
info_path=json_path,
|
| 205 |
+
is_gene_available=True,
|
| 206 |
+
is_trait_available=False
|
| 207 |
+
)
|
| 208 |
+
else:
|
| 209 |
+
# No clinical/trait data available; record initial (non-final) metadata
|
| 210 |
+
_ = validate_and_save_cohort_info(
|
| 211 |
+
is_final=False,
|
| 212 |
+
cohort=cohort,
|
| 213 |
+
info_path=json_path,
|
| 214 |
+
is_gene_available=True,
|
| 215 |
+
is_trait_available=False
|
| 216 |
+
)
|
output/preprocess/Parkinsons_Disease/code/GSE49126.py
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE49126"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE49126"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE49126.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE49126.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE49126.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
|
| 41 |
+
# Determine data availability
|
| 42 |
+
is_gene_available = True # Agilent expression microarrays indicate gene expression data
|
| 43 |
+
trait_row = 0 # disease state
|
| 44 |
+
age_row = None
|
| 45 |
+
gender_row = None
|
| 46 |
+
|
| 47 |
+
# Define conversion functions
|
| 48 |
+
def _extract_value(x):
|
| 49 |
+
if x is None:
|
| 50 |
+
return None
|
| 51 |
+
if isinstance(x, (int, float)):
|
| 52 |
+
return x
|
| 53 |
+
if not isinstance(x, str):
|
| 54 |
+
return None
|
| 55 |
+
parts = x.split(':')
|
| 56 |
+
val = parts[-1].strip().strip('"').strip("'").lower()
|
| 57 |
+
return val if val != '' else None
|
| 58 |
+
|
| 59 |
+
def convert_trait(x):
|
| 60 |
+
val = _extract_value(x)
|
| 61 |
+
if val is None:
|
| 62 |
+
return None
|
| 63 |
+
# Map to binary: control -> 0, Parkinson's disease -> 1
|
| 64 |
+
if any(k in val for k in ["parkinson", "parkinson's", "pd"]):
|
| 65 |
+
return 1
|
| 66 |
+
if any(k in val for k in ["control", "healthy", "normal"]):
|
| 67 |
+
return 0
|
| 68 |
+
return None
|
| 69 |
+
|
| 70 |
+
def convert_age(x):
|
| 71 |
+
val = _extract_value(x)
|
| 72 |
+
if val is None:
|
| 73 |
+
return None
|
| 74 |
+
import re
|
| 75 |
+
m = re.search(r'(\d+(\.\d+)?)', val)
|
| 76 |
+
if not m:
|
| 77 |
+
return None
|
| 78 |
+
try:
|
| 79 |
+
return float(m.group(1))
|
| 80 |
+
except Exception:
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
def convert_gender(x):
|
| 84 |
+
val = _extract_value(x)
|
| 85 |
+
if val is None:
|
| 86 |
+
return None
|
| 87 |
+
if val in ['male', 'm', 'man', 'men', 'male sex', 'male gender']:
|
| 88 |
+
return 1
|
| 89 |
+
if val in ['female', 'f', 'woman', 'women', 'female sex', 'female gender']:
|
| 90 |
+
return 0
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
# Trait availability
|
| 94 |
+
is_trait_available = trait_row is not None
|
| 95 |
+
|
| 96 |
+
# Save initial metadata
|
| 97 |
+
_ = validate_and_save_cohort_info(
|
| 98 |
+
is_final=False,
|
| 99 |
+
cohort=cohort,
|
| 100 |
+
info_path=json_path,
|
| 101 |
+
is_gene_available=is_gene_available,
|
| 102 |
+
is_trait_available=is_trait_available
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Clinical feature extraction (only if trait data is available)
|
| 106 |
+
if trait_row is not None:
|
| 107 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 108 |
+
clinical_df=clinical_data,
|
| 109 |
+
trait=trait,
|
| 110 |
+
trait_row=trait_row,
|
| 111 |
+
convert_trait=convert_trait,
|
| 112 |
+
age_row=age_row,
|
| 113 |
+
convert_age=convert_age,
|
| 114 |
+
gender_row=gender_row,
|
| 115 |
+
convert_gender=convert_gender
|
| 116 |
+
)
|
| 117 |
+
# Preview and save
|
| 118 |
+
print(preview_df(selected_clinical_df))
|
| 119 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 120 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 121 |
+
|
| 122 |
+
# Step 3: Gene Data Extraction
|
| 123 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 124 |
+
gene_data = get_genetic_data(matrix_file)
|
| 125 |
+
|
| 126 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 127 |
+
print(gene_data.index[:20])
|
| 128 |
+
|
| 129 |
+
# Step 4: Gene Identifier Review
|
| 130 |
+
print("requires_gene_mapping = True")
|
| 131 |
+
|
| 132 |
+
# Step 5: Gene Annotation
|
| 133 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 134 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 135 |
+
|
| 136 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 137 |
+
print("Gene annotation preview:")
|
| 138 |
+
print(preview_df(gene_annotation))
|
| 139 |
+
|
| 140 |
+
# Step 6: Gene Identifier Mapping
|
| 141 |
+
# Decide which columns to use for probe IDs and gene symbols
|
| 142 |
+
# Based on previous previews, probe IDs are in 'ID' and gene symbols in 'GENE_SYMBOL'
|
| 143 |
+
prob_col = 'ID' if 'ID' in gene_annotation.columns else None
|
| 144 |
+
|
| 145 |
+
gene_symbol_candidates = ['GENE_SYMBOL', 'GENE', 'SYMBOL', 'Gene Symbol', 'GENE_SYMBOLS']
|
| 146 |
+
gene_col = next((c for c in gene_symbol_candidates if c in gene_annotation.columns), None)
|
| 147 |
+
|
| 148 |
+
if prob_col is None or gene_col is None:
|
| 149 |
+
raise ValueError(f"Could not find appropriate columns for mapping. "
|
| 150 |
+
f"Found columns: {list(gene_annotation.columns)}")
|
| 151 |
+
|
| 152 |
+
# Build mapping dataframe
|
| 153 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=prob_col, gene_col=gene_col)
|
| 154 |
+
|
| 155 |
+
# Apply mapping to convert probe-level to gene-level expression
|
| 156 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 157 |
+
|
| 158 |
+
# Step 7: Data Normalization and Linking
|
| 159 |
+
import os
|
| 160 |
+
import pandas as pd
|
| 161 |
+
import json
|
| 162 |
+
|
| 163 |
+
# 1. Normalize gene symbols and save gene data
|
| 164 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 165 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 166 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 167 |
+
|
| 168 |
+
# 2. Link clinical and genetic data
|
| 169 |
+
if 'selected_clinical_df' in globals():
|
| 170 |
+
clinical_df_to_use = selected_clinical_df
|
| 171 |
+
else:
|
| 172 |
+
if os.path.exists(out_clinical_data_file):
|
| 173 |
+
clinical_df_to_use = pd.read_csv(out_clinical_data_file, index_col=0)
|
| 174 |
+
else:
|
| 175 |
+
raise NameError("Clinical data not found: variable 'selected_clinical_df' not in scope and file not found at out_clinical_data_file.")
|
| 176 |
+
|
| 177 |
+
linked_data = geo_link_clinical_genetic_data(clinical_df_to_use, normalized_gene_data)
|
| 178 |
+
|
| 179 |
+
# 3. Handle missing values
|
| 180 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 181 |
+
|
| 182 |
+
# 4. Determine bias and remove biased demographic features
|
| 183 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 184 |
+
|
| 185 |
+
# 5. Final validation and save cohort info
|
| 186 |
+
# Sanitize inputs to Python-native bools to avoid JSON issues
|
| 187 |
+
is_gene_available_final = bool((normalized_gene_data.shape[0] > 0) and (normalized_gene_data.shape[1] > 0))
|
| 188 |
+
is_trait_available_final = bool((trait in linked_data.columns) and bool(linked_data[trait].notna().any()))
|
| 189 |
+
is_trait_biased = bool(is_trait_biased)
|
| 190 |
+
|
| 191 |
+
note = "INFO: Agilent PBMC dataset; probes mapped to gene symbols; no age/gender available in annotation."
|
| 192 |
+
|
| 193 |
+
is_usable = False
|
| 194 |
+
try:
|
| 195 |
+
is_usable = validate_and_save_cohort_info(
|
| 196 |
+
is_final=True,
|
| 197 |
+
cohort=cohort,
|
| 198 |
+
info_path=json_path,
|
| 199 |
+
is_gene_available=is_gene_available_final,
|
| 200 |
+
is_trait_available=is_trait_available_final,
|
| 201 |
+
is_biased=is_trait_biased,
|
| 202 |
+
df=unbiased_linked_data,
|
| 203 |
+
note=note
|
| 204 |
+
)
|
| 205 |
+
except Exception:
|
| 206 |
+
# Fallback: write sanitized record ourselves if library JSON dump fails
|
| 207 |
+
# Mimic library's final validation logic
|
| 208 |
+
df = unbiased_linked_data
|
| 209 |
+
gene_avail = bool(is_gene_available_final)
|
| 210 |
+
trait_avail = bool(is_trait_available_final)
|
| 211 |
+
if (len(df) <= 0) or (len(df.columns) <= 4):
|
| 212 |
+
gene_avail = False
|
| 213 |
+
if len(df) <= 0:
|
| 214 |
+
trait_avail = False
|
| 215 |
+
is_available = bool(gene_avail and trait_avail)
|
| 216 |
+
|
| 217 |
+
record = {
|
| 218 |
+
"is_usable": bool(is_available and (is_trait_biased is False)),
|
| 219 |
+
"is_gene_available": bool(gene_avail),
|
| 220 |
+
"is_trait_available": bool(trait_avail),
|
| 221 |
+
"is_available": bool(is_available),
|
| 222 |
+
"is_biased": (bool(is_trait_biased) if is_available else None),
|
| 223 |
+
"has_age": (bool('Age' in df.columns) if is_available else None),
|
| 224 |
+
"has_gender": (bool('Gender' in df.columns) if is_available else None),
|
| 225 |
+
"sample_size": (int(len(df)) if is_available else None),
|
| 226 |
+
"note": str(note)
|
| 227 |
+
}
|
| 228 |
+
# Write/merge JSON
|
| 229 |
+
os.makedirs(os.path.dirname(json_path), exist_ok=True)
|
| 230 |
+
if not os.path.exists(json_path):
|
| 231 |
+
with open(json_path, 'w') as f:
|
| 232 |
+
json.dump({}, f)
|
| 233 |
+
with open(json_path, 'r') as f:
|
| 234 |
+
try:
|
| 235 |
+
records = json.load(f)
|
| 236 |
+
except Exception:
|
| 237 |
+
records = {}
|
| 238 |
+
records[cohort] = record
|
| 239 |
+
tmp_path = json_path + ".tmp"
|
| 240 |
+
with open(tmp_path, 'w') as f:
|
| 241 |
+
json.dump(records, f)
|
| 242 |
+
os.replace(tmp_path, json_path)
|
| 243 |
+
is_usable = record["is_usable"]
|
| 244 |
+
|
| 245 |
+
# 6. Save linked data if usable
|
| 246 |
+
if is_usable:
|
| 247 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 248 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Parkinsons_Disease/code/GSE57475.py
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE57475"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE57475"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE57475.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE57475.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE57475.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
import re
|
| 41 |
+
|
| 42 |
+
# 1) Gene expression data availability
|
| 43 |
+
is_gene_available = True # Blood transcript levels and expression platforms imply gene expression data
|
| 44 |
+
|
| 45 |
+
# 2) Variable availability and conversion functions
|
| 46 |
+
trait_row = 2 # 'disease state: DAT-confirmed PD' vs 'healthy control'
|
| 47 |
+
age_row = 0 # 'age: ...'
|
| 48 |
+
gender_row = 1 # 'gender: M/F'
|
| 49 |
+
|
| 50 |
+
def _after_colon(value):
|
| 51 |
+
if value is None:
|
| 52 |
+
return None
|
| 53 |
+
s = str(value)
|
| 54 |
+
parts = s.split(":", 1)
|
| 55 |
+
v = parts[1] if len(parts) > 1 else parts[0]
|
| 56 |
+
return v.strip()
|
| 57 |
+
|
| 58 |
+
def convert_trait(value):
|
| 59 |
+
v = _after_colon(value)
|
| 60 |
+
if v is None or v == "":
|
| 61 |
+
return None
|
| 62 |
+
vl = v.lower()
|
| 63 |
+
# Map PD cases to 1, controls to 0
|
| 64 |
+
if "parkinson" in vl or re.search(r"\bpd\b", vl):
|
| 65 |
+
return 1
|
| 66 |
+
if "control" in vl or "healthy" in vl:
|
| 67 |
+
return 0
|
| 68 |
+
if vl in {"case"}:
|
| 69 |
+
return 1
|
| 70 |
+
if vl in {"control", "normal"}:
|
| 71 |
+
return 0
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
def convert_age(value):
|
| 75 |
+
v = _after_colon(value)
|
| 76 |
+
if v is None or v == "":
|
| 77 |
+
return None
|
| 78 |
+
# Extract first integer number as age
|
| 79 |
+
m = re.search(r"(\d+)", v)
|
| 80 |
+
if not m:
|
| 81 |
+
return None
|
| 82 |
+
try:
|
| 83 |
+
age = int(m.group(1))
|
| 84 |
+
# Basic plausibility check for human age
|
| 85 |
+
if 0 < age < 120:
|
| 86 |
+
return age
|
| 87 |
+
return None
|
| 88 |
+
except Exception:
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
def convert_gender(value):
|
| 92 |
+
v = _after_colon(value)
|
| 93 |
+
if v is None or v == "":
|
| 94 |
+
return None
|
| 95 |
+
vl = v.strip().lower()
|
| 96 |
+
if vl in {"m", "male"}:
|
| 97 |
+
return 1
|
| 98 |
+
if vl in {"f", "female", "woman", "women"}:
|
| 99 |
+
return 0
|
| 100 |
+
if vl in {"u", "unknown", "na", "n/a"}:
|
| 101 |
+
return None
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
# 3) Save metadata with initial filtering
|
| 105 |
+
is_trait_available = trait_row is not None
|
| 106 |
+
_ = validate_and_save_cohort_info(
|
| 107 |
+
is_final=False,
|
| 108 |
+
cohort=cohort,
|
| 109 |
+
info_path=json_path,
|
| 110 |
+
is_gene_available=is_gene_available,
|
| 111 |
+
is_trait_available=is_trait_available
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# 4) Clinical Feature Extraction (only if trait_row is available)
|
| 115 |
+
if is_trait_available:
|
| 116 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 117 |
+
clinical_df=clinical_data,
|
| 118 |
+
trait=trait,
|
| 119 |
+
trait_row=trait_row,
|
| 120 |
+
convert_trait=convert_trait,
|
| 121 |
+
age_row=age_row,
|
| 122 |
+
convert_age=convert_age,
|
| 123 |
+
gender_row=gender_row,
|
| 124 |
+
convert_gender=convert_gender
|
| 125 |
+
)
|
| 126 |
+
preview = preview_df(selected_clinical_df)
|
| 127 |
+
print(preview)
|
| 128 |
+
|
| 129 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 130 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 131 |
+
|
| 132 |
+
# Step 3: Gene Data Extraction
|
| 133 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 134 |
+
gene_data = get_genetic_data(matrix_file)
|
| 135 |
+
|
| 136 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 137 |
+
print(gene_data.index[:20])
|
| 138 |
+
|
| 139 |
+
# Step 4: Gene Identifier Review
|
| 140 |
+
# ILMN_* are Illumina probe identifiers, not human gene symbols; mapping is required.
|
| 141 |
+
requires_gene_mapping = True
|
| 142 |
+
print(f"requires_gene_mapping = {requires_gene_mapping}")
|
| 143 |
+
|
| 144 |
+
# Step 5: Gene Annotation
|
| 145 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 146 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 147 |
+
|
| 148 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 149 |
+
print("Gene annotation preview:")
|
| 150 |
+
print(preview_df(gene_annotation))
|
| 151 |
+
|
| 152 |
+
# Step 6: Gene Identifier Mapping
|
| 153 |
+
# Decide mapping columns based on previous previews: probe IDs in 'ID', gene symbols in 'Symbol'
|
| 154 |
+
probe_col = 'ID'
|
| 155 |
+
gene_symbol_col = 'Symbol'
|
| 156 |
+
|
| 157 |
+
# Build the mapping dataframe
|
| 158 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=gene_symbol_col)
|
| 159 |
+
|
| 160 |
+
# Apply the mapping to convert probe-level data to gene-level data
|
| 161 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 162 |
+
|
| 163 |
+
# Step 7: Data Normalization and Linking
|
| 164 |
+
import os
|
| 165 |
+
|
| 166 |
+
# 1. Normalize gene symbols and save normalized gene data
|
| 167 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 168 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 169 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 170 |
+
|
| 171 |
+
# 2. Link clinical and genetic data
|
| 172 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 173 |
+
|
| 174 |
+
# 3. Handle missing values
|
| 175 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 176 |
+
|
| 177 |
+
# 4. Bias check and remove biased demographic features
|
| 178 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 179 |
+
|
| 180 |
+
# 5. Final validation and save cohort info
|
| 181 |
+
# Ensure Python-native bools for JSON serialization
|
| 182 |
+
is_gene_available_final = bool((normalized_gene_data.shape[0] > 0) and (normalized_gene_data.shape[1] > 0))
|
| 183 |
+
is_trait_available_final = bool((trait in linked_data.columns) and bool(linked_data[trait].notna().any()))
|
| 184 |
+
is_trait_biased_py = bool(is_trait_biased)
|
| 185 |
+
|
| 186 |
+
note = ("INFO: ILMN probe IDs mapped to gene symbols via SOFT 'Symbol' column; gene symbols normalized with NCBI "
|
| 187 |
+
"synonyms; missing values handled (genes >20% missing removed; samples >5% missing removed; mean/mode imputation).")
|
| 188 |
+
|
| 189 |
+
is_usable = validate_and_save_cohort_info(
|
| 190 |
+
is_final=True,
|
| 191 |
+
cohort=cohort,
|
| 192 |
+
info_path=json_path,
|
| 193 |
+
is_gene_available=is_gene_available_final,
|
| 194 |
+
is_trait_available=is_trait_available_final,
|
| 195 |
+
is_biased=is_trait_biased_py,
|
| 196 |
+
df=unbiased_linked_data,
|
| 197 |
+
note=note
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# 6. Save linked data if usable
|
| 201 |
+
if is_usable:
|
| 202 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 203 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Parkinsons_Disease/code/GSE71220.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE71220"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE71220"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE71220.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE71220.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE71220.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Step 1: Gene expression data availability
|
| 40 |
+
is_gene_available = True # Affymetrix Human Gene 1.1 ST microarray -> mRNA gene expression data
|
| 41 |
+
|
| 42 |
+
# Step 2: Variable availability and conversion functions
|
| 43 |
+
# Based on the provided Sample Characteristics Dictionary:
|
| 44 |
+
# 0: statin user (y/n)
|
| 45 |
+
# 1: disease: COPD/Control
|
| 46 |
+
# 2: age
|
| 47 |
+
# 3: Sex
|
| 48 |
+
# There is no Parkinson's Disease label; thus, trait_row is None.
|
| 49 |
+
trait_row = None
|
| 50 |
+
age_row = 2
|
| 51 |
+
gender_row = 3
|
| 52 |
+
|
| 53 |
+
def _after_colon(value: str) -> str:
|
| 54 |
+
if value is None:
|
| 55 |
+
return ""
|
| 56 |
+
parts = str(value).split(":", 1)
|
| 57 |
+
return parts[1].strip() if len(parts) == 2 else str(value).strip()
|
| 58 |
+
|
| 59 |
+
# Trait: Parkinsons_Disease - not available in this cohort
|
| 60 |
+
def convert_trait(x):
|
| 61 |
+
return None
|
| 62 |
+
|
| 63 |
+
# Age: continuous
|
| 64 |
+
def convert_age(x):
|
| 65 |
+
v = _after_colon(x)
|
| 66 |
+
if v in {"", "NA", "N/A", "na", "n/a", None}:
|
| 67 |
+
return None
|
| 68 |
+
try:
|
| 69 |
+
f = float(v)
|
| 70 |
+
# Return int if it's an integer value, else float
|
| 71 |
+
return int(f) if f.is_integer() else f
|
| 72 |
+
except Exception:
|
| 73 |
+
return None
|
| 74 |
+
|
| 75 |
+
# Gender: binary female=0, male=1
|
| 76 |
+
def convert_gender(x):
|
| 77 |
+
v = _after_colon(x).lower()
|
| 78 |
+
if v in {"female", "f", "0"}:
|
| 79 |
+
return 0
|
| 80 |
+
if v in {"male", "m", "1"}:
|
| 81 |
+
return 1
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
# Step 3: Save metadata (initial filtering)
|
| 85 |
+
is_trait_available = trait_row is not None
|
| 86 |
+
_ = validate_and_save_cohort_info(
|
| 87 |
+
is_final=False,
|
| 88 |
+
cohort=cohort,
|
| 89 |
+
info_path=json_path,
|
| 90 |
+
is_gene_available=is_gene_available,
|
| 91 |
+
is_trait_available=is_trait_available
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Step 4: Clinical Feature Extraction - skip because trait_row is None
|
| 95 |
+
# If trait_row were available:
|
| 96 |
+
if trait_row is not None:
|
| 97 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 98 |
+
clinical_df=clinical_data,
|
| 99 |
+
trait=trait,
|
| 100 |
+
trait_row=trait_row,
|
| 101 |
+
convert_trait=convert_trait,
|
| 102 |
+
age_row=age_row,
|
| 103 |
+
convert_age=convert_age,
|
| 104 |
+
gender_row=gender_row,
|
| 105 |
+
convert_gender=convert_gender
|
| 106 |
+
)
|
| 107 |
+
preview = preview_df(selected_clinical_df, n=5)
|
| 108 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 109 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
output/preprocess/Parkinsons_Disease/code/GSE72267.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE72267"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE72267"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE72267.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE72267.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE72267.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
import os
|
| 40 |
+
import re
|
| 41 |
+
import pandas as pd
|
| 42 |
+
|
| 43 |
+
# 1) Gene expression availability based on background info (Affymetrix blood transcriptomics)
|
| 44 |
+
is_gene_available = True
|
| 45 |
+
|
| 46 |
+
# 2) Variable availability from Sample Characteristics Dictionary
|
| 47 |
+
# trait_row found at key 0 as "diagnosis: Parkinson's disease" vs "diagnosis: Healthy"
|
| 48 |
+
trait_row = 0
|
| 49 |
+
age_row = None
|
| 50 |
+
gender_row = None
|
| 51 |
+
|
| 52 |
+
# 2.2) Conversion functions
|
| 53 |
+
def _extract_value(x):
|
| 54 |
+
if x is None or (isinstance(x, float) and pd.isna(x)):
|
| 55 |
+
return None
|
| 56 |
+
s = str(x)
|
| 57 |
+
parts = s.split(":", 1)
|
| 58 |
+
val = parts[1] if len(parts) > 1 else parts[0]
|
| 59 |
+
return val.strip()
|
| 60 |
+
|
| 61 |
+
def convert_trait(x):
|
| 62 |
+
val = _extract_value(x)
|
| 63 |
+
if val is None:
|
| 64 |
+
return None
|
| 65 |
+
low = val.lower()
|
| 66 |
+
# Map controls to 0 first
|
| 67 |
+
if "healthy" in low or "control" in low or low in {"hc"}:
|
| 68 |
+
return 0
|
| 69 |
+
# Map PD to 1
|
| 70 |
+
if "parkinson" in low or low in {"pd"}:
|
| 71 |
+
return 1
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
def convert_age(x):
|
| 75 |
+
val = _extract_value(x)
|
| 76 |
+
if val is None:
|
| 77 |
+
return None
|
| 78 |
+
m = re.search(r"[-+]?\d*\.?\d+", val)
|
| 79 |
+
return float(m.group()) if m else None
|
| 80 |
+
|
| 81 |
+
def convert_gender(x):
|
| 82 |
+
val = _extract_value(x)
|
| 83 |
+
if val is None:
|
| 84 |
+
return None
|
| 85 |
+
low = val.lower()
|
| 86 |
+
if low in {"male", "m", "man"}:
|
| 87 |
+
return 1
|
| 88 |
+
if low in {"female", "f", "woman"}:
|
| 89 |
+
return 0
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
# 3) Save metadata using initial filtering
|
| 93 |
+
is_trait_available = trait_row is not None
|
| 94 |
+
_ = validate_and_save_cohort_info(
|
| 95 |
+
is_final=False,
|
| 96 |
+
cohort=cohort,
|
| 97 |
+
info_path=json_path,
|
| 98 |
+
is_gene_available=is_gene_available,
|
| 99 |
+
is_trait_available=is_trait_available
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# 4) Clinical Feature Extraction (since trait_row is available)
|
| 103 |
+
if trait_row is not None:
|
| 104 |
+
selected_clinical_df = geo_select_clinical_features(
|
| 105 |
+
clinical_df=clinical_data,
|
| 106 |
+
trait=trait,
|
| 107 |
+
trait_row=trait_row,
|
| 108 |
+
convert_trait=convert_trait,
|
| 109 |
+
age_row=age_row,
|
| 110 |
+
convert_age=convert_age if age_row is not None else None,
|
| 111 |
+
gender_row=gender_row,
|
| 112 |
+
convert_gender=convert_gender if gender_row is not None else None
|
| 113 |
+
)
|
| 114 |
+
preview = preview_df(selected_clinical_df)
|
| 115 |
+
print(preview)
|
| 116 |
+
|
| 117 |
+
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
|
| 118 |
+
selected_clinical_df.to_csv(out_clinical_data_file)
|
| 119 |
+
|
| 120 |
+
# Step 3: Gene Data Extraction
|
| 121 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 122 |
+
gene_data = get_genetic_data(matrix_file)
|
| 123 |
+
|
| 124 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 125 |
+
print(gene_data.index[:20])
|
| 126 |
+
|
| 127 |
+
# Step 4: Gene Identifier Review
|
| 128 |
+
requires_gene_mapping = True
|
| 129 |
+
print(f"requires_gene_mapping = {requires_gene_mapping}")
|
| 130 |
+
|
| 131 |
+
# Step 5: Gene Annotation
|
| 132 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 133 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 134 |
+
|
| 135 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 136 |
+
print("Gene annotation preview:")
|
| 137 |
+
print(preview_df(gene_annotation))
|
| 138 |
+
|
| 139 |
+
# Step 6: Gene Identifier Mapping
|
| 140 |
+
# Decide columns for mapping based on annotation preview:
|
| 141 |
+
# Probe identifiers are in 'ID' and gene symbols are in 'Gene Symbol'
|
| 142 |
+
id_col = 'ID'
|
| 143 |
+
gene_symbol_col = 'Gene Symbol'
|
| 144 |
+
|
| 145 |
+
# Build mapping dataframe
|
| 146 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=id_col, gene_col=gene_symbol_col)
|
| 147 |
+
|
| 148 |
+
# Apply mapping to convert probe-level data to gene-level data
|
| 149 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 150 |
+
|
| 151 |
+
# Step 7: Data Normalization and Linking
|
| 152 |
+
import os
|
| 153 |
+
|
| 154 |
+
# 1. Normalize gene symbols and save
|
| 155 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 156 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 157 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 158 |
+
|
| 159 |
+
# 2. Link clinical and genetic data
|
| 160 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
|
| 161 |
+
|
| 162 |
+
# 3. Handle missing values in the linked data
|
| 163 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 164 |
+
|
| 165 |
+
# 4. Determine bias and remove biased demographic features
|
| 166 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 167 |
+
|
| 168 |
+
# Sanity prints for debugging
|
| 169 |
+
print(f"Normalized gene data shape: {normalized_gene_data.shape}")
|
| 170 |
+
print(f"Linked data shape after QC and before bias removal: {linked_data.shape}")
|
| 171 |
+
print(f"Final linked data shape (after bias removal): {unbiased_linked_data.shape}")
|
| 172 |
+
|
| 173 |
+
# Compute data availability flags for final validation (cast to native Python bools)
|
| 174 |
+
is_gene_available_final = bool((normalized_gene_data.shape[0] > 0) and (normalized_gene_data.shape[1] > 0))
|
| 175 |
+
is_trait_available_final = bool((trait in linked_data.columns) and bool(linked_data[trait].notna().any()))
|
| 176 |
+
is_trait_biased_py = bool(is_trait_biased)
|
| 177 |
+
|
| 178 |
+
note = "INFO: Age and Gender not available; mapped Affymetrix probes to symbols and normalized via NCBI synonyms."
|
| 179 |
+
|
| 180 |
+
# 5. Final validation and save cohort info
|
| 181 |
+
is_usable = validate_and_save_cohort_info(
|
| 182 |
+
is_final=True,
|
| 183 |
+
cohort=cohort,
|
| 184 |
+
info_path=json_path,
|
| 185 |
+
is_gene_available=is_gene_available_final,
|
| 186 |
+
is_trait_available=is_trait_available_final,
|
| 187 |
+
is_biased=is_trait_biased_py,
|
| 188 |
+
df=unbiased_linked_data,
|
| 189 |
+
note=note
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
# 6. Save linked dataset if usable
|
| 193 |
+
if is_usable:
|
| 194 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 195 |
+
unbiased_linked_data.to_csv(out_data_file)
|
output/preprocess/Parkinsons_Disease/code/GSE80599.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
cohort = "GSE80599"
|
| 7 |
+
|
| 8 |
+
# Input paths
|
| 9 |
+
in_trait_dir = "../DATA/GEO/Parkinsons_Disease"
|
| 10 |
+
in_cohort_dir = "../DATA/GEO/Parkinsons_Disease/GSE80599"
|
| 11 |
+
|
| 12 |
+
# Output paths
|
| 13 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/GSE80599.csv"
|
| 14 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/GSE80599.csv"
|
| 15 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/GSE80599.csv"
|
| 16 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Step 1: Initial Data Loading
|
| 20 |
+
from tools.preprocess import *
|
| 21 |
+
# 1. Identify the paths to the SOFT file and the matrix file
|
| 22 |
+
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
|
| 23 |
+
|
| 24 |
+
# 2. Read the matrix file to obtain background information and sample characteristics data
|
| 25 |
+
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
|
| 26 |
+
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
|
| 27 |
+
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
|
| 28 |
+
|
| 29 |
+
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
|
| 30 |
+
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
|
| 31 |
+
|
| 32 |
+
# 4. Explicitly print out all the background information and the sample characteristics dictionary
|
| 33 |
+
print("Background Information:")
|
| 34 |
+
print(background_info)
|
| 35 |
+
print("Sample Characteristics Dictionary:")
|
| 36 |
+
print(sample_characteristics_dict)
|
| 37 |
+
|
| 38 |
+
# Step 2: Dataset Analysis and Clinical Feature Extraction
|
| 39 |
+
# Step 1: Determine data availability
|
| 40 |
+
is_gene_available = True # Affymetrix Human Genome U219 gene expression platform
|
| 41 |
+
# Trait is Parkinson's Disease; all subjects are PD patients -> no variability for case/control
|
| 42 |
+
trait_row = None
|
| 43 |
+
age_row = 4
|
| 44 |
+
gender_row = 1
|
| 45 |
+
|
| 46 |
+
# Step 2: Define conversion functions
|
| 47 |
+
def _after_colon(x):
|
| 48 |
+
if x is None:
|
| 49 |
+
return None
|
| 50 |
+
try:
|
| 51 |
+
parts = str(x).split(":", 1)
|
| 52 |
+
return parts[1].strip() if len(parts) > 1 else str(x).strip()
|
| 53 |
+
except Exception:
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
+
def convert_trait(x):
|
| 57 |
+
# Heuristic for general PD/control mapping if ever needed
|
| 58 |
+
val = _after_colon(x)
|
| 59 |
+
if val is None or val == "":
|
| 60 |
+
return None
|
| 61 |
+
s = val.lower()
|
| 62 |
+
if "control" in s:
|
| 63 |
+
return 0
|
| 64 |
+
if "parkinson" in s:
|
| 65 |
+
return 1
|
| 66 |
+
# progression-only labels don't reflect PD vs control; return None
|
| 67 |
+
if "rapid" in s or "slow" in s:
|
| 68 |
+
return None
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
def convert_age(x):
|
| 72 |
+
val = _after_colon(x)
|
| 73 |
+
if val is None or val == "":
|
| 74 |
+
return None
|
| 75 |
+
try:
|
| 76 |
+
v = float(val)
|
| 77 |
+
# keep as continuous age; filter out obvious non-sense negative ages
|
| 78 |
+
if v < 0:
|
| 79 |
+
return None
|
| 80 |
+
return v
|
| 81 |
+
except Exception:
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
def convert_gender(x):
|
| 85 |
+
val = _after_colon(x)
|
| 86 |
+
if val is None or val == "":
|
| 87 |
+
return None
|
| 88 |
+
s = val.strip().lower()
|
| 89 |
+
if s in ["female", "f", "woman", "women"]:
|
| 90 |
+
return 0
|
| 91 |
+
if s in ["male", "m", "man", "men"]:
|
| 92 |
+
return 1
|
| 93 |
+
return None
|
| 94 |
+
|
| 95 |
+
# Step 3: Save metadata (initial filtering)
|
| 96 |
+
is_trait_available = trait_row is not None
|
| 97 |
+
_ = validate_and_save_cohort_info(
|
| 98 |
+
is_final=False,
|
| 99 |
+
cohort=cohort,
|
| 100 |
+
info_path=json_path,
|
| 101 |
+
is_gene_available=is_gene_available,
|
| 102 |
+
is_trait_available=is_trait_available
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Step 4: Clinical feature extraction (skip because trait_row is None)
|
| 106 |
+
# If in future a usable trait_row is identified, uncomment the following:
|
| 107 |
+
# if trait_row is not None:
|
| 108 |
+
# selected = geo_select_clinical_features(
|
| 109 |
+
# clinical_df=clinical_data,
|
| 110 |
+
# trait=trait,
|
| 111 |
+
# trait_row=trait_row,
|
| 112 |
+
# convert_trait=convert_trait,
|
| 113 |
+
# age_row=age_row,
|
| 114 |
+
# convert_age=convert_age,
|
| 115 |
+
# gender_row=gender_row,
|
| 116 |
+
# convert_gender=convert_gender
|
| 117 |
+
# )
|
| 118 |
+
# _ = preview_df(selected)
|
| 119 |
+
# selected.to_csv(out_clinical_data_file, index=True)
|
| 120 |
+
|
| 121 |
+
# Step 3: Gene Data Extraction
|
| 122 |
+
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
|
| 123 |
+
gene_data = get_genetic_data(matrix_file)
|
| 124 |
+
|
| 125 |
+
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
|
| 126 |
+
print(gene_data.index[:20])
|
| 127 |
+
|
| 128 |
+
# Step 4: Gene Identifier Review
|
| 129 |
+
import re
|
| 130 |
+
|
| 131 |
+
# Example identifiers provided in the previous step
|
| 132 |
+
ids = ['11715100_at', '11715101_s_at', '11715102_x_at', '11715103_x_at',
|
| 133 |
+
'11715104_s_at', '11715105_at', '11715106_x_at', '11715107_s_at',
|
| 134 |
+
'11715108_x_at', '11715109_at', '11715110_at', '11715111_s_at',
|
| 135 |
+
'11715112_at', '11715113_x_at', '11715114_x_at', '11715115_s_at',
|
| 136 |
+
'11715116_s_at', '11715117_x_at', '11715118_s_at', '11715119_s_at']
|
| 137 |
+
|
| 138 |
+
# Detect typical Affymetrix probe set IDs (not gene symbols)
|
| 139 |
+
affy_pattern = re.compile(r'^\d+_(?:[sx]_)?at$')
|
| 140 |
+
is_affy = sum(bool(affy_pattern.match(x)) for x in ids) / len(ids) > 0.5
|
| 141 |
+
|
| 142 |
+
requires_gene_mapping = bool(is_affy)
|
| 143 |
+
|
| 144 |
+
print(f"requires_gene_mapping = {requires_gene_mapping}")
|
| 145 |
+
|
| 146 |
+
# Step 5: Gene Annotation
|
| 147 |
+
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
|
| 148 |
+
gene_annotation = get_gene_annotation(soft_file)
|
| 149 |
+
|
| 150 |
+
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
|
| 151 |
+
print("Gene annotation preview:")
|
| 152 |
+
print(preview_df(gene_annotation))
|
| 153 |
+
|
| 154 |
+
# Step 6: Gene Identifier Mapping
|
| 155 |
+
# 1. Identify the appropriate columns for mapping based on previous previews
|
| 156 |
+
probe_id_col = 'ID' # Matches probe identifiers like '11715100_at'
|
| 157 |
+
gene_symbol_col = 'Gene Symbol' # Stores human gene symbols
|
| 158 |
+
|
| 159 |
+
# 2. Build the mapping dataframe
|
| 160 |
+
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_id_col, gene_col=gene_symbol_col)
|
| 161 |
+
|
| 162 |
+
# 3. Apply mapping to convert probe-level data to gene-level expression
|
| 163 |
+
# Keep the resulting gene-level dataframe named as gene_data
|
| 164 |
+
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
|
| 165 |
+
|
| 166 |
+
# Step 7: Data Normalization and Linking
|
| 167 |
+
import os
|
| 168 |
+
|
| 169 |
+
# 1. Normalize gene symbols and save gene-level data
|
| 170 |
+
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
|
| 171 |
+
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
|
| 172 |
+
normalized_gene_data.to_csv(out_gene_data_file)
|
| 173 |
+
|
| 174 |
+
# 2-6. Proceed conditionally based on trait availability (trait_row was set in Step 2)
|
| 175 |
+
try:
|
| 176 |
+
is_trait_available = (trait_row is not None)
|
| 177 |
+
except NameError:
|
| 178 |
+
is_trait_available = False
|
| 179 |
+
|
| 180 |
+
if is_trait_available:
|
| 181 |
+
# Link clinical and genetic data
|
| 182 |
+
linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data)
|
| 183 |
+
|
| 184 |
+
# Handle missing values
|
| 185 |
+
linked_data = handle_missing_values(linked_data, trait)
|
| 186 |
+
|
| 187 |
+
# Bias check
|
| 188 |
+
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
|
| 189 |
+
|
| 190 |
+
# Final validation and save cohort info
|
| 191 |
+
note = "INFO: Trait available and linked; performed missing value handling and bias checks."
|
| 192 |
+
is_usable = validate_and_save_cohort_info(
|
| 193 |
+
is_final=True,
|
| 194 |
+
cohort=cohort,
|
| 195 |
+
info_path=json_path,
|
| 196 |
+
is_gene_available=True,
|
| 197 |
+
is_trait_available=True,
|
| 198 |
+
is_biased=is_trait_biased,
|
| 199 |
+
df=unbiased_linked_data,
|
| 200 |
+
note=note
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
# Save linked data only if usable
|
| 204 |
+
if is_usable:
|
| 205 |
+
os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
|
| 206 |
+
unbiased_linked_data.to_csv(out_data_file)
|
| 207 |
+
else:
|
| 208 |
+
# Trait is unavailable (all PD cases; no usable case/control trait)
|
| 209 |
+
# Use a non-empty placeholder df to avoid abnormality override
|
| 210 |
+
placeholder_df = normalized_gene_data.T
|
| 211 |
+
note = ("INFO: Trait data unavailable for association (all subjects are PD cases; "
|
| 212 |
+
"dataset stratified by progression only). Skipping linking and downstream steps.")
|
| 213 |
+
_ = validate_and_save_cohort_info(
|
| 214 |
+
is_final=True,
|
| 215 |
+
cohort=cohort,
|
| 216 |
+
info_path=json_path,
|
| 217 |
+
is_gene_available=True,
|
| 218 |
+
is_trait_available=False,
|
| 219 |
+
is_biased=False,
|
| 220 |
+
df=placeholder_df,
|
| 221 |
+
note=note
|
| 222 |
+
)
|
output/preprocess/Parkinsons_Disease/code/TCGA.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Path Configuration
|
| 2 |
+
from tools.preprocess import *
|
| 3 |
+
|
| 4 |
+
# Processing context
|
| 5 |
+
trait = "Parkinsons_Disease"
|
| 6 |
+
|
| 7 |
+
# Input paths
|
| 8 |
+
tcga_root_dir = "../DATA/TCGA"
|
| 9 |
+
|
| 10 |
+
# Output paths
|
| 11 |
+
out_data_file = "./output/z5/preprocess/Parkinsons_Disease/TCGA.csv"
|
| 12 |
+
out_gene_data_file = "./output/z5/preprocess/Parkinsons_Disease/gene_data/TCGA.csv"
|
| 13 |
+
out_clinical_data_file = "./output/z5/preprocess/Parkinsons_Disease/clinical_data/TCGA.csv"
|
| 14 |
+
json_path = "./output/z5/preprocess/Parkinsons_Disease/cohort_info.json"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Step 1: Initial Data Loading
|
| 18 |
+
import os
|
| 19 |
+
import pandas as pd
|
| 20 |
+
|
| 21 |
+
# Discover available subdirectories
|
| 22 |
+
all_entries = os.listdir(tcga_root_dir)
|
| 23 |
+
subdirs = [d for d in all_entries if os.path.isdir(os.path.join(tcga_root_dir, d))]
|
| 24 |
+
|
| 25 |
+
# Identify cohort directories potentially relevant to Parkinson's Disease
|
| 26 |
+
keywords = {
|
| 27 |
+
"parkinson", "parkinsons", "parkinson's",
|
| 28 |
+
"parkinson disease", "parkinsons disease", "pd"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def best_score(name: str) -> int:
|
| 32 |
+
name_l = name.lower()
|
| 33 |
+
return max((len(k) for k in keywords if k in name_l), default=0)
|
| 34 |
+
|
| 35 |
+
candidates = [d for d in subdirs if any(k in d.lower() for k in keywords)]
|
| 36 |
+
selected_cohort = None
|
| 37 |
+
clinical_df = None
|
| 38 |
+
genetic_df = None
|
| 39 |
+
|
| 40 |
+
if candidates:
|
| 41 |
+
# Choose the most specific match: longest matching keyword, then shorter directory name
|
| 42 |
+
candidates_sorted = sorted(candidates, key=lambda x: (-best_score(x), len(x)))
|
| 43 |
+
selected_cohort = candidates_sorted[0]
|
| 44 |
+
cohort_dir = os.path.join(tcga_root_dir, selected_cohort)
|
| 45 |
+
|
| 46 |
+
# Find clinical and genetic file paths
|
| 47 |
+
clinical_path, genetic_path = tcga_get_relevant_filepaths(cohort_dir)
|
| 48 |
+
|
| 49 |
+
# Load dataframes
|
| 50 |
+
clinical_df = pd.read_csv(clinical_path, sep='\t', index_col=0, low_memory=False)
|
| 51 |
+
genetic_df = pd.read_csv(genetic_path, sep='\t', index_col=0, low_memory=False)
|
| 52 |
+
|
| 53 |
+
# Print clinical column names
|
| 54 |
+
print(list(clinical_df.columns))
|
| 55 |
+
else:
|
| 56 |
+
# No relevant cohort in TCGA for Parkinson's Disease; record and skip
|
| 57 |
+
_ = validate_and_save_cohort_info(
|
| 58 |
+
is_final=False,
|
| 59 |
+
cohort="TCGA",
|
| 60 |
+
info_path=json_path,
|
| 61 |
+
is_gene_available=False,
|
| 62 |
+
is_trait_available=False
|
| 63 |
+
)
|
| 64 |
+
print("No suitable TCGA cohort found for the trait; skipping.")
|
output/preprocess/Parkinsons_Disease/cohort_info.json
CHANGED
|
@@ -1,112 +1 @@
|
|
| 1 |
-
{
|
| 2 |
-
"GSE80599": {
|
| 3 |
-
"is_usable": false,
|
| 4 |
-
"is_gene_available": false,
|
| 5 |
-
"is_trait_available": false,
|
| 6 |
-
"is_available": false,
|
| 7 |
-
"is_biased": null,
|
| 8 |
-
"has_age": null,
|
| 9 |
-
"has_gender": null,
|
| 10 |
-
"sample_size": null
|
| 11 |
-
},
|
| 12 |
-
"GSE72267": {
|
| 13 |
-
"is_usable": false,
|
| 14 |
-
"is_gene_available": true,
|
| 15 |
-
"is_trait_available": true,
|
| 16 |
-
"is_available": true,
|
| 17 |
-
"is_biased": true,
|
| 18 |
-
"has_age": false,
|
| 19 |
-
"has_gender": false,
|
| 20 |
-
"sample_size": 1
|
| 21 |
-
},
|
| 22 |
-
"GSE71220": {
|
| 23 |
-
"is_usable": false,
|
| 24 |
-
"is_gene_available": true,
|
| 25 |
-
"is_trait_available": false,
|
| 26 |
-
"is_available": false,
|
| 27 |
-
"is_biased": null,
|
| 28 |
-
"has_age": null,
|
| 29 |
-
"has_gender": null,
|
| 30 |
-
"sample_size": null
|
| 31 |
-
},
|
| 32 |
-
"GSE57475": {
|
| 33 |
-
"is_usable": true,
|
| 34 |
-
"is_gene_available": true,
|
| 35 |
-
"is_trait_available": true,
|
| 36 |
-
"is_available": true,
|
| 37 |
-
"is_biased": false,
|
| 38 |
-
"has_age": true,
|
| 39 |
-
"has_gender": true,
|
| 40 |
-
"sample_size": 142
|
| 41 |
-
},
|
| 42 |
-
"GSE49126": {
|
| 43 |
-
"is_usable": true,
|
| 44 |
-
"is_gene_available": true,
|
| 45 |
-
"is_trait_available": true,
|
| 46 |
-
"is_available": true,
|
| 47 |
-
"is_biased": false,
|
| 48 |
-
"has_age": false,
|
| 49 |
-
"has_gender": false,
|
| 50 |
-
"sample_size": 50
|
| 51 |
-
},
|
| 52 |
-
"GSE30335": {
|
| 53 |
-
"is_usable": true,
|
| 54 |
-
"is_gene_available": true,
|
| 55 |
-
"is_trait_available": true,
|
| 56 |
-
"is_available": true,
|
| 57 |
-
"is_biased": false,
|
| 58 |
-
"has_age": false,
|
| 59 |
-
"has_gender": false,
|
| 60 |
-
"sample_size": 40
|
| 61 |
-
},
|
| 62 |
-
"GSE202667": {
|
| 63 |
-
"is_usable": false,
|
| 64 |
-
"is_gene_available": false,
|
| 65 |
-
"is_trait_available": true,
|
| 66 |
-
"is_available": false,
|
| 67 |
-
"is_biased": null,
|
| 68 |
-
"has_age": null,
|
| 69 |
-
"has_gender": null,
|
| 70 |
-
"sample_size": null
|
| 71 |
-
},
|
| 72 |
-
"GSE202665": {
|
| 73 |
-
"is_usable": true,
|
| 74 |
-
"is_gene_available": true,
|
| 75 |
-
"is_trait_available": true,
|
| 76 |
-
"is_available": true,
|
| 77 |
-
"is_biased": false,
|
| 78 |
-
"has_age": true,
|
| 79 |
-
"has_gender": false,
|
| 80 |
-
"sample_size": 59
|
| 81 |
-
},
|
| 82 |
-
"GSE103099": {
|
| 83 |
-
"is_usable": true,
|
| 84 |
-
"is_gene_available": true,
|
| 85 |
-
"is_trait_available": true,
|
| 86 |
-
"is_available": true,
|
| 87 |
-
"is_biased": false,
|
| 88 |
-
"has_age": false,
|
| 89 |
-
"has_gender": false,
|
| 90 |
-
"sample_size": 59
|
| 91 |
-
},
|
| 92 |
-
"GSE101534": {
|
| 93 |
-
"is_usable": true,
|
| 94 |
-
"is_gene_available": true,
|
| 95 |
-
"is_trait_available": true,
|
| 96 |
-
"is_available": true,
|
| 97 |
-
"is_biased": false,
|
| 98 |
-
"has_age": false,
|
| 99 |
-
"has_gender": false,
|
| 100 |
-
"sample_size": 51
|
| 101 |
-
},
|
| 102 |
-
"TCGA": {
|
| 103 |
-
"is_usable": false,
|
| 104 |
-
"is_gene_available": false,
|
| 105 |
-
"is_trait_available": false,
|
| 106 |
-
"is_available": false,
|
| 107 |
-
"is_biased": null,
|
| 108 |
-
"has_age": null,
|
| 109 |
-
"has_gender": null,
|
| 110 |
-
"sample_size": null
|
| 111 |
-
}
|
| 112 |
-
}
|
|
|
|
| 1 |
+
{"GSE80599": {"is_usable": false, "is_gene_available": true, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": "INFO: Trait data unavailable for association (all subjects are PD cases; dataset stratified by progression only). Skipping linking and downstream steps."}, "GSE72267": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": false, "has_gender": false, "sample_size": 59, "note": "INFO: Age and Gender not available; mapped Affymetrix probes to symbols and normalized via NCBI synonyms."}, "GSE71220": {"is_usable": false, "is_gene_available": true, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}, "GSE57475": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": true, "has_gender": true, "sample_size": 142, "note": "INFO: ILMN probe IDs mapped to gene symbols via SOFT 'Symbol' column; gene symbols normalized with NCBI synonyms; missing values handled (genes >20% missing removed; samples >5% missing removed; mean/mode imputation)."}, "GSE49126": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": false, "has_gender": false, "sample_size": 50, "note": "INFO: Agilent PBMC dataset; probes mapped to gene symbols; no age/gender available in annotation."}, "GSE30335": {"is_usable": false, "is_gene_available": true, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}, "GSE202667": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": true, "has_gender": false, "sample_size": 60, "note": "INFO: Gender not available in clinical data. INFO: Time-point post activation exists in metadata but was not included as a covariate."}, "GSE202665": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": true, "has_gender": false, "sample_size": 59, "note": "INFO: Gender not included (all male per cohort description); time-course CD4+ T-cell activation samples linked with disease status and age."}, "GSE103099": {"is_usable": false, "is_gene_available": true, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}, "GSE101534": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": false, "has_gender": false, "sample_size": 51, "note": "WARNING: Gene symbol normalization skipped; expression index appears to be probe IDs (retention 0.0000). Using probe-level data."}, "TCGA": {"is_usable": false, "is_gene_available": false, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output/regress/Polycystic_Kidney_Disease/significant_genes_condition_Gender.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/regress/Polycystic_Kidney_Disease/significant_genes_condition_Hypertension.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/regress/Polycystic_Kidney_Disease/significant_genes_condition_None.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/regress/Polycystic_Kidney_Disease/significant_genes_condition_Obesity.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/regress/Polycystic_Ovary_Syndrome/significant_genes_condition_Age.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/regress/Polycystic_Ovary_Syndrome/significant_genes_condition_Atherosclerosis.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/regress/Polycystic_Ovary_Syndrome/significant_genes_condition_Atrial_Fibrillation.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output/regress/Polycystic_Ovary_Syndrome/significant_genes_condition_Bipolar_disorder.json
CHANGED
|
@@ -1,240 +1,423 @@
|
|
| 1 |
{
|
| 2 |
"significant_genes": {
|
| 3 |
"Variable": [
|
| 4 |
-
"AASDH",
|
| 5 |
-
"A1CF",
|
| 6 |
-
"ABCB1",
|
| 7 |
-
"AADAT",
|
| 8 |
"A1BG",
|
| 9 |
-
"
|
| 10 |
-
"
|
| 11 |
-
"
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"AFF3",
|
| 19 |
-
"ADPGK",
|
| 20 |
-
"ABCA9",
|
| 21 |
-
"ABHD14B",
|
| 22 |
-
"ADAMTS1",
|
| 23 |
-
"ACSL5",
|
| 24 |
-
"ALPK1",
|
| 25 |
-
"AOPEP",
|
| 26 |
-
"AA06",
|
| 27 |
-
"ADAMTS6",
|
| 28 |
-
"ADAT2",
|
| 29 |
"ACKR3",
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
-
"ADAM22",
|
| 36 |
-
"ADH4",
|
| 37 |
-
"WDFY2",
|
| 38 |
-
"ABHD12",
|
| 39 |
-
"ADAMTSL4",
|
| 40 |
-
"AKAP13",
|
| 41 |
-
"GABRB3",
|
| 42 |
-
"C6orf163",
|
| 43 |
-
"ADCY5",
|
| 44 |
-
"ABHD4",
|
| 45 |
-
"ASB3",
|
| 46 |
"ABHD15",
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
"ABCC13",
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
"
|
| 68 |
-
"
|
| 69 |
-
"
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
],
|
| 77 |
"Coefficient": [
|
| 78 |
-
-0.
|
| 79 |
-
0.
|
| 80 |
-
|
| 81 |
-
0.
|
| 82 |
-
0.
|
| 83 |
-
0.
|
| 84 |
-
0.
|
| 85 |
-
-0.
|
| 86 |
-
|
| 87 |
-
-0.
|
| 88 |
-
0.
|
| 89 |
-
0.
|
| 90 |
-
0.
|
| 91 |
-
0.
|
| 92 |
-
0.
|
| 93 |
-
0.
|
| 94 |
-
-0.
|
| 95 |
-
0.
|
| 96 |
-
0.
|
| 97 |
-
0.
|
| 98 |
-
0.
|
| 99 |
-
|
| 100 |
-
0.
|
| 101 |
-
-0.
|
| 102 |
-
-0.
|
| 103 |
-
-0.
|
| 104 |
-
0.
|
| 105 |
-
0.
|
| 106 |
-
0.
|
| 107 |
-
0.
|
| 108 |
-
0.
|
| 109 |
-
|
| 110 |
-
-0.
|
| 111 |
-
0.
|
| 112 |
-
0.
|
| 113 |
-
|
| 114 |
-
0.
|
| 115 |
-
0.
|
| 116 |
-
0.
|
| 117 |
-
-0.
|
| 118 |
-
-0.
|
| 119 |
-
0.
|
| 120 |
-
|
| 121 |
-
0.
|
| 122 |
-
-0.
|
| 123 |
-
|
| 124 |
-
0.
|
| 125 |
-
-0.
|
| 126 |
-
0.
|
| 127 |
-
0.
|
| 128 |
-
0.
|
| 129 |
-
0.
|
| 130 |
-
-0.
|
| 131 |
-
0.
|
| 132 |
-
0.
|
| 133 |
-
0.
|
| 134 |
-
0.
|
| 135 |
-
0.
|
| 136 |
-
|
| 137 |
-
0.
|
| 138 |
-
-0.
|
| 139 |
-
0.
|
| 140 |
-
-0.
|
| 141 |
-
0.
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
0.
|
| 145 |
-
0.
|
| 146 |
-
0.
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
],
|
| 151 |
"Absolute Coefficient": [
|
| 152 |
-
0.
|
| 153 |
-
0.
|
| 154 |
-
0.
|
| 155 |
-
0.
|
| 156 |
-
0.
|
| 157 |
-
0.
|
| 158 |
-
0.
|
| 159 |
-
0.
|
| 160 |
-
0.
|
| 161 |
-
0.
|
| 162 |
-
0.
|
| 163 |
-
0.
|
| 164 |
-
0.
|
| 165 |
-
0.
|
| 166 |
-
0.
|
| 167 |
-
0.
|
| 168 |
-
0.
|
| 169 |
-
0.
|
| 170 |
-
0.
|
| 171 |
-
0.
|
| 172 |
-
0.
|
| 173 |
-
0.
|
| 174 |
-
0.
|
| 175 |
-
0.
|
| 176 |
-
0.
|
| 177 |
-
0.
|
| 178 |
-
0.
|
| 179 |
-
0.
|
| 180 |
-
0.
|
| 181 |
-
0.
|
| 182 |
-
0.
|
| 183 |
-
0.
|
| 184 |
-
0.
|
| 185 |
-
0.
|
| 186 |
-
0.
|
| 187 |
-
0.
|
| 188 |
-
0.
|
| 189 |
-
0.
|
| 190 |
-
0.
|
| 191 |
-
0.
|
| 192 |
-
0.
|
| 193 |
-
0.
|
| 194 |
-
0.
|
| 195 |
-
0.
|
| 196 |
-
0.
|
| 197 |
-
0.
|
| 198 |
-
0.
|
| 199 |
-
0.
|
| 200 |
-
0.
|
| 201 |
-
0.
|
| 202 |
-
0.
|
| 203 |
-
0.
|
| 204 |
-
0.
|
| 205 |
-
0.
|
| 206 |
-
0.
|
| 207 |
-
0.
|
| 208 |
-
0.
|
| 209 |
-
0.
|
| 210 |
-
0.
|
| 211 |
-
0.
|
| 212 |
-
0.
|
| 213 |
-
0.
|
| 214 |
-
0.
|
| 215 |
-
0.
|
| 216 |
-
0.
|
| 217 |
-
0.
|
| 218 |
-
0.
|
| 219 |
-
0.
|
| 220 |
-
0.
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
]
|
| 225 |
},
|
| 226 |
"cv_performance": {
|
| 227 |
"prediction": {
|
| 228 |
-
"accuracy":
|
| 229 |
-
"precision":
|
| 230 |
-
"recall":
|
| 231 |
-
"f1":
|
| 232 |
},
|
| 233 |
"selection": {
|
| 234 |
-
"precision":
|
| 235 |
-
"recall":
|
| 236 |
-
"f1":
|
| 237 |
-
"jaccard": 0.
|
| 238 |
}
|
| 239 |
}
|
| 240 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"significant_genes": {
|
| 3 |
"Variable": [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"A1BG",
|
| 5 |
+
"AAMDC",
|
| 6 |
+
"ACTRT2",
|
| 7 |
+
"ACTN2",
|
| 8 |
+
"ABL2",
|
| 9 |
+
"A2M",
|
| 10 |
+
"ACER1",
|
| 11 |
+
"CCDC38",
|
| 12 |
+
"AADAT",
|
| 13 |
+
"ACAA2",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"ACKR3",
|
| 15 |
+
"ADAMTS5",
|
| 16 |
+
"CREB3L2",
|
| 17 |
+
"AARSD1",
|
| 18 |
+
"ACSBG1",
|
| 19 |
+
"ALKBH6",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"ABHD15",
|
| 21 |
+
"AP2A2",
|
| 22 |
+
"ABCF1",
|
| 23 |
+
"ANAPC2",
|
| 24 |
+
"CTRC",
|
| 25 |
+
"AFF3",
|
| 26 |
+
"ACP1",
|
| 27 |
+
"ACBD7",
|
| 28 |
+
"MERTK",
|
| 29 |
+
"GREB1",
|
| 30 |
+
"AASDHPPT",
|
| 31 |
+
"ABI1",
|
| 32 |
+
"BDKRB1",
|
| 33 |
+
"PTK7",
|
| 34 |
+
"ATXN7L2",
|
| 35 |
+
"CASP9",
|
| 36 |
+
"ACOX2",
|
| 37 |
+
"ABCC10",
|
| 38 |
+
"ASB9",
|
| 39 |
+
"ACSS1",
|
| 40 |
+
"AAGAB",
|
| 41 |
+
"AKR7A3",
|
| 42 |
+
"TMPRSS11A",
|
| 43 |
+
"ABCD1",
|
| 44 |
+
"ADARB2",
|
| 45 |
+
"C1D",
|
| 46 |
+
"ACTBL2",
|
| 47 |
+
"C1QC",
|
| 48 |
+
"CPS1",
|
| 49 |
+
"BMP8B",
|
| 50 |
+
"AARS1",
|
| 51 |
+
"ASB7",
|
| 52 |
+
"API5",
|
| 53 |
+
"ACTG1",
|
| 54 |
+
"ABHD2",
|
| 55 |
+
"OR13C8",
|
| 56 |
+
"PSG4",
|
| 57 |
+
"ARHGEF39",
|
| 58 |
+
"ATF6B",
|
| 59 |
+
"ANKRD31",
|
| 60 |
+
"ATG9A",
|
| 61 |
"ABCC13",
|
| 62 |
+
"ACTR3B",
|
| 63 |
+
"ATAD3A",
|
| 64 |
+
"OR52E8",
|
| 65 |
+
"SGSM1",
|
| 66 |
+
"FHDC1",
|
| 67 |
+
"RFXANK",
|
| 68 |
+
"AKAP5",
|
| 69 |
+
"ASB14",
|
| 70 |
+
"MMP3",
|
| 71 |
+
"AFP",
|
| 72 |
+
"ALG12",
|
| 73 |
+
"ACER3",
|
| 74 |
+
"ALX4",
|
| 75 |
+
"BRWD1-AS2",
|
| 76 |
+
"ABHD3",
|
| 77 |
+
"ACRV1",
|
| 78 |
+
"TMEM177",
|
| 79 |
+
"KRTAP10-8",
|
| 80 |
+
"SFMBT2",
|
| 81 |
+
"ACVRL1",
|
| 82 |
+
"ASTL",
|
| 83 |
+
"IFNE",
|
| 84 |
+
"OR52N1",
|
| 85 |
+
"ABCD3",
|
| 86 |
+
"C12orf42",
|
| 87 |
+
"AP2M1",
|
| 88 |
+
"AGBL2",
|
| 89 |
+
"CAPN5",
|
| 90 |
+
"GAL3ST3",
|
| 91 |
+
"ADH4",
|
| 92 |
+
"AMHR2",
|
| 93 |
+
"ABHD12B",
|
| 94 |
+
"ROS1",
|
| 95 |
+
"ST3GAL3",
|
| 96 |
+
"HOMER3",
|
| 97 |
+
"MAT1A",
|
| 98 |
+
"ABCB8",
|
| 99 |
+
"AIMP2",
|
| 100 |
+
"ADISSP",
|
| 101 |
+
"ALKBH4",
|
| 102 |
+
"ASB2",
|
| 103 |
+
"SULT1C2",
|
| 104 |
+
"ADAM3A",
|
| 105 |
+
"CACNG4",
|
| 106 |
+
"ABCD2",
|
| 107 |
+
"ARHGEF2",
|
| 108 |
+
"PAEP",
|
| 109 |
+
"DKK1",
|
| 110 |
+
"TENT5D",
|
| 111 |
+
"C19orf44",
|
| 112 |
+
"GPSM1",
|
| 113 |
+
"LOXL2",
|
| 114 |
+
"ACP4",
|
| 115 |
+
"OR4K13",
|
| 116 |
+
"PHF10",
|
| 117 |
+
"ABT1",
|
| 118 |
+
"ACER2",
|
| 119 |
+
"SCAF1",
|
| 120 |
+
"GINS4",
|
| 121 |
+
"ACBD3",
|
| 122 |
+
"ARHGDIA",
|
| 123 |
+
"ACKR1",
|
| 124 |
+
"ABTB1",
|
| 125 |
+
"C5AR2",
|
| 126 |
+
"LENEP",
|
| 127 |
+
"ADGRD1",
|
| 128 |
+
"ACAP3",
|
| 129 |
+
"ALDH8A1",
|
| 130 |
+
"MS4A12",
|
| 131 |
+
"COL9A1",
|
| 132 |
+
"ACSM1",
|
| 133 |
+
"CAPNS1",
|
| 134 |
+
"CABP4",
|
| 135 |
+
"RPH3AL",
|
| 136 |
+
"EEIG2"
|
| 137 |
],
|
| 138 |
"Coefficient": [
|
| 139 |
+
-0.10464030279304302,
|
| 140 |
+
-0.05365665988648252,
|
| 141 |
+
0.052090573864317306,
|
| 142 |
+
0.048253075602756275,
|
| 143 |
+
0.04523961848352548,
|
| 144 |
+
0.043562495730185315,
|
| 145 |
+
-0.04131615373252105,
|
| 146 |
+
-0.030948335919673654,
|
| 147 |
+
0.028186453929237958,
|
| 148 |
+
-0.02395058450175842,
|
| 149 |
+
-0.023892195119550805,
|
| 150 |
+
0.021749784869354326,
|
| 151 |
+
-0.021529189741655647,
|
| 152 |
+
0.021002900680915263,
|
| 153 |
+
0.020997018923697378,
|
| 154 |
+
-0.01856185128786997,
|
| 155 |
+
-0.017024831424294982,
|
| 156 |
+
-0.01700176258708451,
|
| 157 |
+
0.016653523677761232,
|
| 158 |
+
-0.016550939741132926,
|
| 159 |
+
-0.014020602479100135,
|
| 160 |
+
0.013372331133358607,
|
| 161 |
+
0.013314419729870866,
|
| 162 |
+
-0.012709657372155915,
|
| 163 |
+
-0.012080124942676903,
|
| 164 |
+
-0.011241151159602648,
|
| 165 |
+
0.011023510316253062,
|
| 166 |
+
0.010816402799161611,
|
| 167 |
+
0.010789784949854693,
|
| 168 |
+
-0.01057797919366995,
|
| 169 |
+
-0.010431032304639851,
|
| 170 |
+
0.010391730166880291,
|
| 171 |
+
-0.010188201175614538,
|
| 172 |
+
0.009920435219692936,
|
| 173 |
+
-0.009625231813510436,
|
| 174 |
+
0.009233811145202197,
|
| 175 |
+
0.00893750166773654,
|
| 176 |
+
-0.00880998674952237,
|
| 177 |
+
0.008456493858036322,
|
| 178 |
+
-0.008411114845052341,
|
| 179 |
+
-0.008195817708108774,
|
| 180 |
+
-0.007393079413472402,
|
| 181 |
+
0.007138812905620194,
|
| 182 |
+
-0.006688769933761927,
|
| 183 |
+
-0.006267537762453462,
|
| 184 |
+
0.005986183395650605,
|
| 185 |
+
0.00570549710263503,
|
| 186 |
+
-0.005532561971031495,
|
| 187 |
+
-0.005474012619836727,
|
| 188 |
+
-0.005405463130183713,
|
| 189 |
+
0.005383953793349027,
|
| 190 |
+
-0.005358873938241648,
|
| 191 |
+
-0.005283648294402644,
|
| 192 |
+
0.005252195815033679,
|
| 193 |
+
-0.005084297631364156,
|
| 194 |
+
-0.004835830238163769,
|
| 195 |
+
0.004659306894915272,
|
| 196 |
+
0.0046507557340097795,
|
| 197 |
+
0.0033860785917617445,
|
| 198 |
+
0.0032063994090353056,
|
| 199 |
+
-0.003175095594579186,
|
| 200 |
+
-0.0030960506980886888,
|
| 201 |
+
-0.0029593530784444246,
|
| 202 |
+
0.002889923622085409,
|
| 203 |
+
0.002802365098623947,
|
| 204 |
+
0.002801003350527977,
|
| 205 |
+
-0.002799157139857763,
|
| 206 |
+
-0.0027300427411740364,
|
| 207 |
+
-0.0027195333634302036,
|
| 208 |
+
0.002626747664157729,
|
| 209 |
+
0.0026125212475706563,
|
| 210 |
+
-0.0025693400493038187,
|
| 211 |
+
0.0023933733648502176,
|
| 212 |
+
-0.0022462512863178474,
|
| 213 |
+
0.0022195120776224444,
|
| 214 |
+
-0.002166707995941729,
|
| 215 |
+
-0.0020386100043417593,
|
| 216 |
+
-0.001967873903461149,
|
| 217 |
+
-0.0018986810296323263,
|
| 218 |
+
-0.001847871109693317,
|
| 219 |
+
0.0018317792564494376,
|
| 220 |
+
0.001780938061841259,
|
| 221 |
+
-0.0017036765700258899,
|
| 222 |
+
-0.001545696774654592,
|
| 223 |
+
0.0014572560420009842,
|
| 224 |
+
-0.0014033383056014934,
|
| 225 |
+
-0.0013355633575273778,
|
| 226 |
+
-0.001254199203110189,
|
| 227 |
+
0.0011763474024803582,
|
| 228 |
+
0.001169120924365598,
|
| 229 |
+
-0.0011070955956458232,
|
| 230 |
+
-0.0010707322344254827,
|
| 231 |
+
-0.0010667464699841665,
|
| 232 |
+
-0.0010530150694565618,
|
| 233 |
+
-0.0010275802471353633,
|
| 234 |
+
0.000900955597284388,
|
| 235 |
+
-0.0008594306149872774,
|
| 236 |
+
-0.0008591463650282899,
|
| 237 |
+
0.0007614435644394191,
|
| 238 |
+
-0.0007334860600410629,
|
| 239 |
+
-0.0007112380429965028,
|
| 240 |
+
0.0006727492583371289,
|
| 241 |
+
0.0006530125436399433,
|
| 242 |
+
-0.000652628950685945,
|
| 243 |
+
0.0005689097006576766,
|
| 244 |
+
-0.0004769128977433693,
|
| 245 |
+
0.00037455688138133744,
|
| 246 |
+
-0.0003426958953999745,
|
| 247 |
+
-0.0003403759888381256,
|
| 248 |
+
-0.0003319877988144457,
|
| 249 |
+
-0.0003196439381027939,
|
| 250 |
+
0.000264860736006232,
|
| 251 |
+
0.00024359405817288456,
|
| 252 |
+
0.0002405063620031918,
|
| 253 |
+
0.00022741878507745537,
|
| 254 |
+
0.00022284187456724183,
|
| 255 |
+
-0.00018345045000607376,
|
| 256 |
+
0.00015036870984482168,
|
| 257 |
+
-0.0001486315589694363,
|
| 258 |
+
0.00013386837368196584,
|
| 259 |
+
0.00011865165202445419,
|
| 260 |
+
0.00010078792932494505,
|
| 261 |
+
9.813544266458357e-05,
|
| 262 |
+
-9.429373162113883e-05,
|
| 263 |
+
5.851021634067129e-05,
|
| 264 |
+
5.814829201075462e-05,
|
| 265 |
+
2.761161938700284e-05,
|
| 266 |
+
2.6707529861424095e-05,
|
| 267 |
+
-1.2981447033000547e-05,
|
| 268 |
+
-1.1594396200053842e-05,
|
| 269 |
+
8.240336779261694e-06,
|
| 270 |
+
-1.9670499821430264e-06,
|
| 271 |
+
-1.420772278213226e-07
|
| 272 |
],
|
| 273 |
"Absolute Coefficient": [
|
| 274 |
+
0.10464030279304302,
|
| 275 |
+
0.05365665988648252,
|
| 276 |
+
0.052090573864317306,
|
| 277 |
+
0.048253075602756275,
|
| 278 |
+
0.04523961848352548,
|
| 279 |
+
0.043562495730185315,
|
| 280 |
+
0.04131615373252105,
|
| 281 |
+
0.030948335919673654,
|
| 282 |
+
0.028186453929237958,
|
| 283 |
+
0.02395058450175842,
|
| 284 |
+
0.023892195119550805,
|
| 285 |
+
0.021749784869354326,
|
| 286 |
+
0.021529189741655647,
|
| 287 |
+
0.021002900680915263,
|
| 288 |
+
0.020997018923697378,
|
| 289 |
+
0.01856185128786997,
|
| 290 |
+
0.017024831424294982,
|
| 291 |
+
0.01700176258708451,
|
| 292 |
+
0.016653523677761232,
|
| 293 |
+
0.016550939741132926,
|
| 294 |
+
0.014020602479100135,
|
| 295 |
+
0.013372331133358607,
|
| 296 |
+
0.013314419729870866,
|
| 297 |
+
0.012709657372155915,
|
| 298 |
+
0.012080124942676903,
|
| 299 |
+
0.011241151159602648,
|
| 300 |
+
0.011023510316253062,
|
| 301 |
+
0.010816402799161611,
|
| 302 |
+
0.010789784949854693,
|
| 303 |
+
0.01057797919366995,
|
| 304 |
+
0.010431032304639851,
|
| 305 |
+
0.010391730166880291,
|
| 306 |
+
0.010188201175614538,
|
| 307 |
+
0.009920435219692936,
|
| 308 |
+
0.009625231813510436,
|
| 309 |
+
0.009233811145202197,
|
| 310 |
+
0.00893750166773654,
|
| 311 |
+
0.00880998674952237,
|
| 312 |
+
0.008456493858036322,
|
| 313 |
+
0.008411114845052341,
|
| 314 |
+
0.008195817708108774,
|
| 315 |
+
0.007393079413472402,
|
| 316 |
+
0.007138812905620194,
|
| 317 |
+
0.006688769933761927,
|
| 318 |
+
0.006267537762453462,
|
| 319 |
+
0.005986183395650605,
|
| 320 |
+
0.00570549710263503,
|
| 321 |
+
0.005532561971031495,
|
| 322 |
+
0.005474012619836727,
|
| 323 |
+
0.005405463130183713,
|
| 324 |
+
0.005383953793349027,
|
| 325 |
+
0.005358873938241648,
|
| 326 |
+
0.005283648294402644,
|
| 327 |
+
0.005252195815033679,
|
| 328 |
+
0.005084297631364156,
|
| 329 |
+
0.004835830238163769,
|
| 330 |
+
0.004659306894915272,
|
| 331 |
+
0.0046507557340097795,
|
| 332 |
+
0.0033860785917617445,
|
| 333 |
+
0.0032063994090353056,
|
| 334 |
+
0.003175095594579186,
|
| 335 |
+
0.0030960506980886888,
|
| 336 |
+
0.0029593530784444246,
|
| 337 |
+
0.002889923622085409,
|
| 338 |
+
0.002802365098623947,
|
| 339 |
+
0.002801003350527977,
|
| 340 |
+
0.002799157139857763,
|
| 341 |
+
0.0027300427411740364,
|
| 342 |
+
0.0027195333634302036,
|
| 343 |
+
0.002626747664157729,
|
| 344 |
+
0.0026125212475706563,
|
| 345 |
+
0.0025693400493038187,
|
| 346 |
+
0.0023933733648502176,
|
| 347 |
+
0.0022462512863178474,
|
| 348 |
+
0.0022195120776224444,
|
| 349 |
+
0.002166707995941729,
|
| 350 |
+
0.0020386100043417593,
|
| 351 |
+
0.001967873903461149,
|
| 352 |
+
0.0018986810296323263,
|
| 353 |
+
0.001847871109693317,
|
| 354 |
+
0.0018317792564494376,
|
| 355 |
+
0.001780938061841259,
|
| 356 |
+
0.0017036765700258899,
|
| 357 |
+
0.001545696774654592,
|
| 358 |
+
0.0014572560420009842,
|
| 359 |
+
0.0014033383056014934,
|
| 360 |
+
0.0013355633575273778,
|
| 361 |
+
0.001254199203110189,
|
| 362 |
+
0.0011763474024803582,
|
| 363 |
+
0.001169120924365598,
|
| 364 |
+
0.0011070955956458232,
|
| 365 |
+
0.0010707322344254827,
|
| 366 |
+
0.0010667464699841665,
|
| 367 |
+
0.0010530150694565618,
|
| 368 |
+
0.0010275802471353633,
|
| 369 |
+
0.000900955597284388,
|
| 370 |
+
0.0008594306149872774,
|
| 371 |
+
0.0008591463650282899,
|
| 372 |
+
0.0007614435644394191,
|
| 373 |
+
0.0007334860600410629,
|
| 374 |
+
0.0007112380429965028,
|
| 375 |
+
0.0006727492583371289,
|
| 376 |
+
0.0006530125436399433,
|
| 377 |
+
0.000652628950685945,
|
| 378 |
+
0.0005689097006576766,
|
| 379 |
+
0.0004769128977433693,
|
| 380 |
+
0.00037455688138133744,
|
| 381 |
+
0.0003426958953999745,
|
| 382 |
+
0.0003403759888381256,
|
| 383 |
+
0.0003319877988144457,
|
| 384 |
+
0.0003196439381027939,
|
| 385 |
+
0.000264860736006232,
|
| 386 |
+
0.00024359405817288456,
|
| 387 |
+
0.0002405063620031918,
|
| 388 |
+
0.00022741878507745537,
|
| 389 |
+
0.00022284187456724183,
|
| 390 |
+
0.00018345045000607376,
|
| 391 |
+
0.00015036870984482168,
|
| 392 |
+
0.0001486315589694363,
|
| 393 |
+
0.00013386837368196584,
|
| 394 |
+
0.00011865165202445419,
|
| 395 |
+
0.00010078792932494505,
|
| 396 |
+
9.813544266458357e-05,
|
| 397 |
+
9.429373162113883e-05,
|
| 398 |
+
5.851021634067129e-05,
|
| 399 |
+
5.814829201075462e-05,
|
| 400 |
+
2.761161938700284e-05,
|
| 401 |
+
2.6707529861424095e-05,
|
| 402 |
+
1.2981447033000547e-05,
|
| 403 |
+
1.1594396200053842e-05,
|
| 404 |
+
8.240336779261694e-06,
|
| 405 |
+
1.9670499821430264e-06,
|
| 406 |
+
1.420772278213226e-07
|
| 407 |
]
|
| 408 |
},
|
| 409 |
"cv_performance": {
|
| 410 |
"prediction": {
|
| 411 |
+
"accuracy": 86.66666666666666,
|
| 412 |
+
"precision": 89.33333333333333,
|
| 413 |
+
"recall": 96.0,
|
| 414 |
+
"f1": 92.0
|
| 415 |
},
|
| 416 |
"selection": {
|
| 417 |
+
"precision": 0.17391304347826086,
|
| 418 |
+
"recall": 0.25,
|
| 419 |
+
"f1": 0.20512820512820512,
|
| 420 |
+
"jaccard": 0.10309278350515463
|
| 421 |
}
|
| 422 |
}
|
| 423 |
}
|