|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Anxiety_disorder" |
|
cohort = "GSE61672" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Anxiety_disorder" |
|
in_cohort_dir = "../DATA/GEO/Anxiety_disorder/GSE61672" |
|
|
|
|
|
out_data_file = "./output/preprocess/1/Anxiety_disorder/GSE61672.csv" |
|
out_gene_data_file = "./output/preprocess/1/Anxiety_disorder/gene_data/GSE61672.csv" |
|
out_clinical_data_file = "./output/preprocess/1/Anxiety_disorder/clinical_data/GSE61672.csv" |
|
json_path = "./output/preprocess/1/Anxiety_disorder/cohort_info.json" |
|
|
|
|
|
|
|
from tools.preprocess import * |
|
|
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design'] |
|
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1'] |
|
background_info, clinical_data = get_background_and_clinical_data( |
|
matrix_file, |
|
background_prefixes, |
|
clinical_prefixes |
|
) |
|
|
|
|
|
sample_characteristics_dict = get_unique_values_by_row(clinical_data) |
|
|
|
|
|
print("Background Information:") |
|
print(background_info) |
|
print("\nSample Characteristics Dictionary:") |
|
print(sample_characteristics_dict) |
|
import pandas as pd |
|
import os |
|
import json |
|
from typing import Optional, Callable |
|
|
|
|
|
sample_dict = { |
|
0: ['age: 44', 'age: 59', 'age: 39', 'age: 64', 'age: 58', 'age: 45', 'age: 37', 'age: 40', |
|
'age: 57', 'age: 52', 'age: 62', 'age: 55', 'age: 53', 'age: 47', 'age: 48', 'age: 49', |
|
'age: 35', 'age: 46', 'age: 54', 'age: 67', 'age: 51', 'age: 34', 'age: 60', 'age: 41', |
|
'age: 38', 'age: 73', 'age: 28', 'age: 56', 'age: 71', 'age: 50'], |
|
1: ['Sex: F', 'Sex: M', 'body mass index: 25.1', 'body mass index: 31.1', 'body mass index: 29.4', |
|
'body mass index: 27.6', 'body mass index: 24.6', 'body mass index: 28', 'body mass index: 33.9', |
|
'body mass index: 35', 'body mass index: 18.1', 'body mass index: 19.2', 'body mass index: 39.2', |
|
'body mass index: 26.8', 'body mass index: 21.3', 'body mass index: 36.5', 'body mass index: 19.5', |
|
'body mass index: 24.4', 'body mass index: 26.4', 'body mass index: 26.2', 'body mass index: 23.8', |
|
'body mass index: 19.7', 'body mass index: 30.6', 'body mass index: 22.8', 'body mass index: 22.1', |
|
'body mass index: 33.4', 'body mass index: 26.6', 'body mass index: 21.8', 'body mass index: 24.3', |
|
'body mass index: 27'], |
|
2: ['body mass index: 22.2', 'body mass index: 33.1', 'body mass index: 22.4', 'body mass index: 20.6', |
|
'body mass index: 27.5', 'body mass index: 21.9', 'body mass index: 26.1', 'body mass index: 34.8', |
|
'body mass index: 20.8', 'body mass index: 23.3', 'body mass index: 22.7', 'body mass index: 26.4', |
|
'body mass index: 32.5', 'body mass index: 21.6', 'body mass index: 27.6', 'body mass index: 25.7', |
|
'body mass index: 33.3', 'body mass index: 31.6', 'body mass index: 28', 'body mass index: 41.1', |
|
'body mass index: 19.7', 'body mass index: 22.1', 'body mass index: 20.7', 'body mass index: 30.9', |
|
'body mass index: 17.8', 'body mass index: 22.5', 'body mass index: 40.6', 'body mass index: 28.9', |
|
'body mass index: 26', 'body mass index: 22'], |
|
3: ['ethnicity: CAU', 'ethnicity: AFR', 'ethnicity: ASN', 'ethnicity: AMI', 'ethnicity: CAH', |
|
'gad7 score: 6', 'gad7 score: 1', 'gad7 score: 0', 'gad7 score: 2', 'gad7 score: 3', 'gad7 score: 5', |
|
'gad7 score: 4', 'gad7 score: 9', 'gad7 score: 7', 'gad7 score: 8', 'hybridization batch: C', |
|
'gad7 score: .', 'gad7 score: 16', 'gad7 score: 12', 'gad7 score: 11', 'gad7 score: 21', |
|
'gad7 score: 18', 'gad7 score: 14'], |
|
4: ['gad7 score: 2', 'gad7 score: 0', 'gad7 score: 3', 'gad7 score: 7', 'gad7 score: 4', |
|
'gad7 score: 9', 'gad7 score: 1', 'gad7 score: 10', 'gad7 score: 5', 'gad7 score: 17', 'gad7 score: 6', |
|
'gad7 score: 8', 'gad7 score: 12', 'gad7 score: 11', 'gad7 score: 14', 'gad7 score: .', |
|
'hybridization batch: Z', 'gad7 score: 18', 'hybridization batch: O', 'gad7 score: 13', |
|
'gad7 score: 15', 'gad7 score: 20', 'gad7 score: 21', 'gad7 score: 19', 'anxiety case/control: case', |
|
'anxiety case/control: control', 'hybridization batch: B', None, 'hybridization batch: C', |
|
'hybridization batch: D'], |
|
5: ['hybridization batch: Z', 'anxiety case/control: control', 'anxiety case/control: case', |
|
'rin: 8.4', 'hybridization batch: A', 'hybridization batch: O', 'rin: 6', None, |
|
'hybridization batch: B', 'rin: 9.5', 'rin: 9.1', 'rin: 9.3', 'rin: 9.7', 'rin: 9.6', |
|
'rin: 8.7', 'hybridization batch: C', 'rin: 8.6', 'rin: 7.9', 'rin: 7.3', 'rin: 7.1', |
|
'rin: 8.9', 'rin: 9.8', 'rin: 9.4', 'rin: 9.2', 'rin: 8.8', 'rin: 10', 'rin: 9', 'rin: 9.9', |
|
'hybridization batch: D'], |
|
6: ['rin: 8.1', 'hybridization batch: Z', 'rin: 7.9', 'rin: 6.6', 'rin: 7.3', 'rin: 6.9', |
|
'rin: 6.8', 'rin: 7.5', 'rin: 6.7', 'rin: 6.5', 'rin: 7.8', 'rin: 7.6', 'rin: 8', 'rin: 7.4', |
|
'rin: 8.4', 'rin: 8.7', 'rin: 8.8', 'rin: 7.7', 'rin: 8.3', 'rin: 7', 'rin: 9', 'rin: 9.3', |
|
'rin: 8.9', None, 'rin: 8.2', 'rin: 9.2', 'rin: 7.2', 'rin: 7.1', 'hybridization batch: A', |
|
'rin: 9.8'], |
|
7: [None, 'rin: 7.8', 'rin: 8.1', 'rin: 6.6', 'rin: 6.5', 'rin: 6.7', 'rin: 7.2', 'rin: 7.7', |
|
'rin: 7.1', 'rin: 7', 'rin: 7.3', 'rin: 7.5', 'rin: 7.9', 'rin: 8.2', 'rin: 7.4', 'rin: 7.6', |
|
'rin: 6.8', 'rin: 9.4', 'rin: 8.6', 'rin: 8.3', 'rin: 8.8', 'rin: 8', 'rin: 8.4', 'rin: 8.7', |
|
'rin: 9', 'rin: 9.1', 'rin: 9.2', 'rin: 9.3', 'rin: 8.5', 'rin: 6.9'] |
|
} |
|
|
|
|
|
clinical_data = pd.DataFrame.from_dict(sample_dict, orient='index') |
|
|
|
|
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
|
|
age_row = 0 |
|
|
|
|
|
trait_row = 4 |
|
|
|
|
|
gender_row = 1 |
|
|
|
def convert_trait(value: str) -> Optional[int]: |
|
""" |
|
Convert values referring to 'anxiety case/control': |
|
'case' -> 1 |
|
'control' -> 0 |
|
Everything else -> None |
|
""" |
|
parts = value.split(":", 1) |
|
if len(parts) == 2: |
|
header = parts[0].strip().lower() |
|
val = parts[1].strip().lower() |
|
if header == "anxiety case/control": |
|
if val == "case": |
|
return 1 |
|
elif val == "control": |
|
return 0 |
|
return None |
|
|
|
def convert_age(value: str) -> Optional[float]: |
|
""" |
|
Convert values of the form 'age: 44' -> 44.0 |
|
Otherwise -> None |
|
""" |
|
parts = value.split(":", 1) |
|
if len(parts) == 2: |
|
header = parts[0].strip().lower() |
|
val = parts[1].strip() |
|
if header == "age": |
|
try: |
|
return float(val) |
|
except ValueError: |
|
return None |
|
return None |
|
|
|
def convert_gender(value: str) -> Optional[int]: |
|
""" |
|
For gender, convert: |
|
'Sex: F' -> 0 |
|
'Sex: M' -> 1 |
|
Otherwise -> None |
|
""" |
|
parts = value.split(":", 1) |
|
if len(parts) == 2: |
|
header = parts[0].strip().lower() |
|
val = parts[1].strip().lower() |
|
if header == "sex": |
|
if val == "f": |
|
return 0 |
|
elif val == "m": |
|
return 1 |
|
return None |
|
|
|
|
|
is_trait_available = (trait_row is not None) |
|
|
|
|
|
from tools.preprocess import validate_and_save_cohort_info, geo_select_clinical_features, preview_df |
|
|
|
is_usable = validate_and_save_cohort_info( |
|
is_final=False, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=is_gene_available, |
|
is_trait_available=is_trait_available |
|
) |
|
|
|
|
|
if trait_row is not None: |
|
selected_clinical_df = geo_select_clinical_features( |
|
clinical_df=clinical_data, |
|
trait=trait, |
|
trait_row=trait_row, |
|
convert_trait=convert_trait, |
|
age_row=age_row, |
|
convert_age=convert_age, |
|
gender_row=gender_row, |
|
convert_gender=convert_gender |
|
) |
|
preview = preview_df(selected_clinical_df) |
|
print("Clinical dataframe preview:", preview) |
|
|
|
|
|
selected_clinical_df.to_csv(out_clinical_data_file, index=False) |
|
import re |
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
trait_row = None |
|
age_row = None |
|
gender_row = None |
|
|
|
|
|
|
|
def convert_trait(val_str: str): |
|
"""Convert trait value to binary (0 for control, 1 for anxiety), None if unknown.""" |
|
if val_str is None: |
|
return None |
|
|
|
parts = val_str.split(":") |
|
val_str = parts[-1].strip() if len(parts) > 1 else val_str.strip() |
|
val_lower = val_str.lower() |
|
if "anxiety" in val_lower: |
|
return 1 |
|
elif "control" in val_lower: |
|
return 0 |
|
return None |
|
|
|
def convert_age(val_str: str): |
|
"""Convert age to a continuous number, None if parsing fails.""" |
|
if val_str is None: |
|
return None |
|
|
|
parts = val_str.split(":") |
|
val_str = parts[-1].strip() if len(parts) > 1 else val_str.strip() |
|
|
|
match = re.search(r"(\d+(\.\d+)?)", val_str) |
|
if match: |
|
try: |
|
return float(match.group(1)) |
|
except ValueError: |
|
return None |
|
return None |
|
|
|
def convert_gender(val_str: str): |
|
"""Convert gender to binary (0 for female, 1 for male), None if unknown.""" |
|
if val_str is None: |
|
return None |
|
|
|
parts = val_str.split(":") |
|
val_str = parts[-1].strip() if len(parts) > 1 else val_str.strip() |
|
val_lower = val_str.lower() |
|
if "female" in val_lower: |
|
return 0 |
|
elif "male" in val_lower: |
|
return 1 |
|
return None |
|
|
|
|
|
is_trait_available = (trait_row is not None) |
|
|
|
|
|
is_usable = validate_and_save_cohort_info( |
|
is_final=False, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=is_gene_available, |
|
is_trait_available=is_trait_available |
|
) |
|
|
|
|
|
|
|
|
|
|
|
gene_data = get_genetic_data(matrix_file) |
|
|
|
|
|
print(gene_data.index[:20]) |
|
|
|
|
|
|
|
print("requires_gene_mapping = True") |
|
|
|
|
|
gene_annotation = get_gene_annotation(soft_file) |
|
|
|
|
|
print("Gene annotation preview:") |
|
print(preview_df(gene_annotation)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol') |
|
|
|
|
|
gene_data = apply_gene_mapping(gene_data, mapping_df) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
normalized_gene_data = normalize_gene_symbols_in_index(gene_data) |
|
normalized_gene_data.to_csv(out_gene_data_file) |
|
print(f"Saved normalized gene data to {out_gene_data_file}") |
|
|
|
|
|
print("Trait is not available (trait_row = None). Skipping linking and missing-value handling steps.") |
|
|
|
|
|
is_usable = validate_and_save_cohort_info( |
|
is_final=True, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=True, |
|
is_trait_available=False, |
|
is_biased=None, |
|
df=pd.DataFrame(), |
|
note="No trait data available, thus not usable for trait-based analysis." |
|
) |
|
|
|
|
|
|
|
if is_usable: |
|
|
|
out_data_file_placeholder = out_data_file.replace(".csv", "_linked_placeholder.csv") |
|
pd.DataFrame().to_csv(out_data_file_placeholder, index=False) |
|
print(f"Saved a placeholder linked dataset to {out_data_file_placeholder}") |
|
else: |
|
print("The dataset is not usable for trait-based association. Skipping final output of linked data.") |