Upload dataloader
Browse files- app/dataloader.py +90 -0
app/dataloader.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import scanpy as sc
|
| 3 |
+
import numpy as np
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
def getEntrezGeneSymbol(input_data_key,input_data_value):
|
| 10 |
+
BASE_PATH = Path(__file__).parent
|
| 11 |
+
file_path = str(BASE_PATH.parent / "Core data/SSC_all_Healthy_allproteins.csv")
|
| 12 |
+
mapping= pd.read_csv(file_path)
|
| 13 |
+
return mapping[mapping[input_data_key]==input_data_value]['EntrezGeneSymbol'].iloc[0]
|
| 14 |
+
|
| 15 |
+
def load_singlecell_data(single_cell_data_path):
|
| 16 |
+
|
| 17 |
+
return sc.read(f'{single_cell_data_path}/final_combined_simplified.h5ad')
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def load_data(metadata_path, proteins_path):
|
| 21 |
+
"""
|
| 22 |
+
Load metadata and protein data from the provided file paths.
|
| 23 |
+
"""
|
| 24 |
+
try:
|
| 25 |
+
metadata = pd.read_csv(metadata_path)
|
| 26 |
+
proteins = pd.read_csv(proteins_path)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
raise ValueError(f"Error loading files: {e}")
|
| 29 |
+
return metadata, proteins
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def filter_data(proteins, metadata, protein_id, id_type):
|
| 33 |
+
"""
|
| 34 |
+
Filter the proteins data for a specific protein ID based on the ID type
|
| 35 |
+
and retrieve corresponding metadata information.
|
| 36 |
+
"""
|
| 37 |
+
valid_columns = {
|
| 38 |
+
"TargetFullName": "TargetFullName", #SSC all healthy all proteins
|
| 39 |
+
"Target": "Target", #SSC all healthy all proteins
|
| 40 |
+
"EntrezGeneID": "EntrezGeneID",
|
| 41 |
+
"EntrezGeneSymbol": "EntrezGeneSymbol"
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
if id_type not in valid_columns:
|
| 45 |
+
raise ValueError(f"Invalid ID type. Choose from {list(valid_columns.keys())}.")
|
| 46 |
+
|
| 47 |
+
column_name = valid_columns[id_type]
|
| 48 |
+
|
| 49 |
+
if column_name not in proteins.columns:
|
| 50 |
+
raise KeyError(f"Column '{column_name}' not found in proteins data.")
|
| 51 |
+
|
| 52 |
+
# Filter proteins data for the given protein ID
|
| 53 |
+
filtered_data = proteins[proteins[column_name] == protein_id]
|
| 54 |
+
|
| 55 |
+
if filtered_data.empty:
|
| 56 |
+
raise ValueError(f"No data found for {id_type} = {protein_id}.")
|
| 57 |
+
|
| 58 |
+
# Debug: Ensure filtered data has SampleId
|
| 59 |
+
if "SampleId" not in filtered_data.columns:
|
| 60 |
+
raise KeyError("Column 'SampleId' not found in filtered proteins data.")
|
| 61 |
+
print(f"Filtered Data for {protein_id}:")
|
| 62 |
+
print(filtered_data.head())
|
| 63 |
+
|
| 64 |
+
# Match SampleId in proteins with SubjectID in metadata
|
| 65 |
+
sample_ids = filtered_data["SampleId"].unique()
|
| 66 |
+
metadata_info = metadata[metadata["SubjectID"].isin(sample_ids)]
|
| 67 |
+
|
| 68 |
+
if metadata_info.empty:
|
| 69 |
+
raise ValueError(f"No metadata found for Sample IDs: {sample_ids}.")
|
| 70 |
+
|
| 71 |
+
# Debug: Print metadata subset
|
| 72 |
+
print("Metadata Info:")
|
| 73 |
+
print(metadata_info.head())
|
| 74 |
+
|
| 75 |
+
# Merge filtered_data with metadata_info on SampleId
|
| 76 |
+
merged_data = pd.merge(
|
| 77 |
+
filtered_data,
|
| 78 |
+
metadata_info,
|
| 79 |
+
left_on="SampleId",
|
| 80 |
+
right_on="SubjectID",
|
| 81 |
+
how="inner"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Debug: Print merged data
|
| 85 |
+
print("Merged Data:")
|
| 86 |
+
print(merged_data.head())
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
return merged_data
|
| 90 |
+
|