|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Multiple_sclerosis" |
|
cohort = "GSE193442" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Multiple_sclerosis" |
|
in_cohort_dir = "../DATA/GEO/Multiple_sclerosis/GSE193442" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Multiple_sclerosis/GSE193442.csv" |
|
out_gene_data_file = "./output/preprocess/3/Multiple_sclerosis/gene_data/GSE193442.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Multiple_sclerosis/clinical_data/GSE193442.csv" |
|
json_path = "./output/preprocess/3/Multiple_sclerosis/cohort_info.json" |
|
|
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
background_info, clinical_data = get_background_and_clinical_data(matrix_file) |
|
|
|
|
|
sample_characteristics = get_unique_values_by_row(clinical_data) |
|
|
|
|
|
print("Dataset Background Information:") |
|
print(f"{background_info}\n") |
|
|
|
|
|
print("Sample Characteristics:") |
|
for feature, values in sample_characteristics.items(): |
|
print(f"Feature: {feature}") |
|
print(f"Values: {values}\n") |
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
trait_row = None |
|
age_row = None |
|
gender_row = None |
|
|
|
|
|
def convert_trait(value): |
|
if not isinstance(value, str): |
|
return None |
|
value = value.split(": ")[-1].strip().lower() |
|
if value == "ms" or value == "multiple sclerosis": |
|
return 1 |
|
elif value == "control" or value == "healthy" or value == "hc": |
|
return 0 |
|
return None |
|
|
|
def convert_age(value): |
|
if not isinstance(value, str): |
|
return None |
|
try: |
|
age = float(value.split(": ")[-1].strip()) |
|
return age |
|
except: |
|
return None |
|
|
|
def convert_gender(value): |
|
if not isinstance(value, str): |
|
return None |
|
value = value.split(": ")[-1].strip().lower() |
|
if value in ["f", "female"]: |
|
return 0 |
|
elif value in ["m", "male"]: |
|
return 1 |
|
return None |
|
|
|
|
|
|
|
validate_and_save_cohort_info(is_final=False, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=is_gene_available, |
|
is_trait_available=False) |
|
|
|
|
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
print("First few lines of matrix file:") |
|
with gzip.open(matrix_file, 'rt') as f: |
|
for i, line in enumerate(f): |
|
print(line.strip()) |
|
if i >= 10: |
|
break |
|
|
|
|
|
with gzip.open(matrix_file, 'rt') as f: |
|
for i, line in enumerate(f): |
|
if "series_matrix_table_end" in line: |
|
end_marker = i |
|
if "series_matrix_table_begin" in line: |
|
begin_marker = i |
|
header = next(f).strip() |
|
print("\nFound data table at line", i) |
|
print("Header line:", header) |
|
break |
|
|
|
|
|
gene_data = get_genetic_data(matrix_file) |
|
|
|
|
|
print("\nShape of gene expression data:", gene_data.shape) |
|
print("\nFirst few rows of data:") |
|
print(gene_data.head()) |
|
|
|
|
|
print("\nFirst 20 gene/probe identifiers:") |
|
print(gene_data.head(20).index) |
|
requires_gene_mapping = True |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
print("Files in directory:") |
|
print(os.listdir(in_cohort_dir)) |
|
|
|
|
|
print("\nFirst few lines of matrix file:") |
|
with gzip.open(matrix_file, 'rt') as f: |
|
for i, line in enumerate(f): |
|
if i < 15 or ('!platform_table_begin' in line.lower() and i < 30): |
|
print(line.strip()) |
|
if i >= 30: |
|
break |
|
|
|
|
|
platform_lines = [] |
|
capturing = False |
|
with gzip.open(matrix_file, 'rt') as f: |
|
for line in f: |
|
if '!platform_table_begin' in line.lower(): |
|
capturing = True |
|
continue |
|
if '!platform_table_end' in line.lower(): |
|
capturing = False |
|
break |
|
if capturing and line.strip(): |
|
platform_lines.append(line) |
|
|
|
|
|
if platform_lines: |
|
try: |
|
gene_annotation = pd.read_csv(io.StringIO(''.join(platform_lines)), |
|
delimiter='\t', low_memory=False) |
|
print("\nGene Annotation Preview:") |
|
print("Column names:", gene_annotation.columns.tolist()) |
|
print("\nFirst few rows as dictionary:") |
|
print(preview_df(gene_annotation)) |
|
except Exception as e: |
|
print(f"\nError reading platform annotation: {str(e)}") |
|
else: |
|
print("\nNo platform annotation data found in matrix file") |
|
|
|
|
|
|
|
|
|
|
|
gene_data = pd.DataFrame() |