Team Ai
Apppublic

onconpc/onconpc-visualization

sourceHugging Faceupdated 8mo agoView on Hugging Face
2likes
utils.cpython-38.pyc293 linesDownload Raw Back to __pycache__
1U

22f�u�
@s6ddlZddlmZmZmZmZmZddlZddlZddl	Z	ddl3mZddlm
Z
mZddlmZddlZddlZddlZddlmZddlmZddlmZddlZddlmZddl4mZejj ej!ee"ej!d�d	d5�Z#ej$j%ej!ee"ej!d�dd�Z&ejj ej!ej'd
�dd�Z(ej$j%ej!ej'd
�dd�Z)ee"d�dd�Z*e"ee"ee"fe"d�dd�Z+dFej!ej!e,e,e"e"e"e"e-e.d�6dd�Z/dGej!ej0ee"ee"e-ee"ee"effd �d!d"�Z1dHee"ej!ej2ej!ee"e"e"e.eee"efd#�	d$d%�Z3dIej!ej!ej!ej!e"e"e"eej!ej!fd)�d*d+�Z4dJej!e"e"e"e"e"ee"ej!d-�d.d/�Z5dKej!e"ej!d1�d2d3�Z6dLej!ej!ej!ej!eee"e"e"eej!ej!fd6�d7d8�Z7dMej!ej!ej!ej!ee"eee"e"eej!ej!ej!fd:�d;d<�Z8ej!ee"ej!d=�d>d?�Z9ej!ee"ej!d@�dAdB�Z:ee"ee"dC�dDdE�Z;dS)N�N)�List�Mapping�Optional�Any�Tuple��Patch)�7conversion�default_converter)�	pandas2ri)�localconverter)�adjust_text)�	xgb_model�features_test_df�cancer_types�returncs`|�|j�}|jdd�}dd�t||�D�}tj||j�d�}||d<�fdd�|D�|d<|S)	�Returns cancer type predictions for test set using XGBoost model.8	9	Args:10		xgb_model: XGBoost model.11		features_test_df: Test set features.12		cancer_types: List of cancer types.13	Returns:14		pd.DataFrame containing cancer type predictions and prediction probabilities.15	���axiscSsg|]\}}||�qS�r��.0Z	pred_dist�max_idxrr�:/Users/jenniferzhou/onconpc-visualization/./codes/utils.py�16<listcomp>'sz1get_xgboost_cancer_type_preds.<locals>.<listcomp>��index�columns�
max_posteriorcsg|]}�|�qSrr�rr�rrrr-s�cancer_type)Z
predict_proba�values�argmax�zip�pd�	DataFramer)rrr�ckp_test_pred_probs�ckp_test_preds�max_posteriors�ckp_test_preds_dfrr!r�get_xgboost_cancer_type_predss��r,cs�t�|j�}|j|dd�}t�|�}||jddd�}|jdd�}dd�t||�D�}t	j17||j�d�}||d	<�fd18d�|D�|d<|S)rT)Z
output_marginr)rZkeepdimsrcSsg|]\}}||�qSrrrrrrrBsz8get_xgboost_latest_cancer_type_preds.<locals>.<listcomp>rrcsg|]}�|�qSrrr r!rrrHsr")�xgbZDMatrixr#Zpredict�npZexp�sumr$r%r&r'r)rrrZdtestr(r)r*r+rr!r�$get_xgboost_latest_cancer_type_preds0s19��r0)�model�datarcs@|��}|��dd��d�fdd�	}||_t�|�}|�|�S)��Returns SHAP values for predictions based on data.20	21	Args:22		model: XGBoost model.23		data: Data to obtain SHAP values for.24	Returns:25		Numpy array containing SHAP values.26	�Ncs�S�Nr)�self�Zmodel_bytearrayrr�in_bytearrayXsz(obtain_shap_values.<locals>.in_bytearray)N)Zget_boosterZsave_raw�shap�
TreeExplainer�shap_values)r1r2Z	myboosterr8�shap_exrr7r�obtain_shap_valuesKs27r=cCst�|�}|�|�S)r3)r9r:r;)r1r2r<rrr�&obtain_shap_values_with_latest_xgboost^s28r>)�fature_namescCs�t�t�}i}|D]z}d|kr6|d�|�d||<q|dkrV|d�|�d||<qd|krv|d�|�d||<q|d�|�d||<q||fS)z�Partitions feature names into groups.29	30	Args:31		feature_names: List of feature names.32	Returns:33		Dictionary mapping feature groups to feature names and vice versa.34	�SBS�	signature)�Age�Sex�clinical�CNA�cna�mutation)�collections�defaultdict�list�append)r?�feature_group_to_features_dict�feature_to_feature_group_dict�featrrr� partition_feature_names_by_groupls3536373839rO)�feature_name�feature_group_dictrcCs8ddddd�}|��D]\}}||kr||SqdS)a'40    Determines the color for a given feature based on its group.41 42    Args:43        feature_name: Name of the feature.44        feature_group_dict: Dictionary mapping feature groups to feature names.45 46    Returns:47        A string representing the color associated with the feature's group.48    �#984ea3�#f781bf�#4daf4a�#377eb8)rGrFrArDZblack)�items)rPrQZ
color_mapping�group�featuresrrr�	get_color�s49rY�./others_prediction_explanation�$./data/combined_cohort_age_stats.pkl�50F)51�shap_pred_sample_df�feature_sample_dfrLrM�sample_info�filename�filepath�cohort_age_stats_path�top_feature_num�	save_plotc52	svtj�ddd��t��\}53}|jd�d�|jd�d�t���g�}t|�fdd�d	d54�}
t|
d|��fdd�dd55�}t	�56|�j||j|�fd
d�|D�d��}|j|d|d|dd�|�
d�|�|�|�g�t|d��}t�|�}W5QRX|��ddd}tt|d|d��D]�\}\}}||dk�rVt|��n|d�}|dk�rz|dk�rvdnd}|d k�r�t||d!|d"�}|d#d�d$k�r�t|��}|j|||�d%|��dd&d'd(��q2|j||d)dd&d'd(�d*d�td+d,d-d.gd/d0d1d2g�D�}|j|d3d4�t��|	�r^t�|�d5|�d6��t�|�d5|�d7��t��|�d5|�d6�S)8a(57    Dynamic version of the function for individual prediction interpretation for a given tumor sample.58 59    Args:60        shap_pred_sample_df: DataFrame containing SHAP values for a given tumor sample.61        feature_sample_df: DataFrame containing feature values for a given tumor sample.62        feature_group_to_features_dict: Dictionary mapping feature groups to feature names.63        sample_info: Sample information to be displayed.64        filename: Filename to save the figure.65        top_feature_num: Number of top features to display.66    �ZArial)z	font.sizezfont.family�rightF�topcst�j|�Sr5)�abs�loc��x�r]rr�<lambda>��z4get_individual_pred_interpretation.<locals>.<lambda>T)�key�reverseNcs67�j|Sr5)rirjrlrrrm�rncsg|]}t|���qSr)rY)rrN)rLrrr�sz6get_individual_pred_interpretation.<locals>.<listcomp>)�	feat_name�SHAP_val�feat_val�colorrqrrrt)rtzSHAP Values�rbrg�������?皙�����?rsrGz.2frC��?�Male�FemalerB�Std_mean�Age_mean�����rEz: �centerr\)ZhaZvaZfontsizezfeature: valuecSsg|]\}}t||d��qS))Z	facecolor�labelr)rr~rtrrrr�szSomatic Mut.z68CNA eventsz
Mutation Sig.zAge/SexrRrSrTrUzFeature Groups)Zhandles�title�/z.svgz.pdf)�pltZrcParams�updateZsubplotsZspinesZset_visibler/r#�sortedr&r'riZbarhZ69set_xlabelZ	set_titleZ70set_yticks�open�pickle�loadZget_xlim�	enumerater%�int�textZlegendZtight_layoutZsavefigZshow)r]r^rLrMr_r`rarbrcrdZfigZaxZall_featuresZsorted_featuresZtop_featuresZtop_feats_df�fp�combined_cohort_age_statsZleft_margin�i�name�valueZ71value_textZlegend_elementsr)rLr]r�"get_individual_pred_interpretation�sL�727374" 757677$$r��)�preds_df�shapsrXr�nrcCs�||}i}|��D]�\}}|�|�jdd�}	|j�|�}78g}|	jD]4}|�|�}
tj||
|79||gd�j}|�|�qDt�	|�}|	�80�|d�||<q|S)aX81	Get top n predictions for each sample in the given DataFrame.82	Args:83		preds_df: DataFrame containing predictions.84		shaps: Array of SHAP values.85		features: List of features.86		n: Number of top predictions to return.87		cancer_types: List of cancer types.88	Returns:89		Dictionary containing top n predictions and SHAP values for each sample.90	F)Z	ascendingr)Ztop_n_predictionsZshap_values_df)Ziterrows�nlargestZsort_valuesrZget_locr&r'�TrK�concatZto_dict)r�r�rXrr�Z	df_chosenZ$sample_id_to_top_n_pred_and_shaps_df�	sample_idZrowZsorted_top_3Zrow_idxZshap_dfs�pred_cancer�pred_cancer_idx�shap_pred_cancer_dfZ
shap_total_dfrrr�get_top_n_pred_and_shap�s$9192�93�r�)	�	query_idsr�r��df_features_genie�cancer_types_to_considerrarbrdrc
Cs�i}|D]�}	|j|	df}94|j|	df}|�|�}|j|	}
tj|||j|jd�}|j|	}d|	�d|�d|95d��}t|j�\}}t||
|||t|	�|d||d	�96}|97||d98�||	<q|S)a999	Get OncoNPC predictions and generate SHAP-based explanation plots for multiple query IDs.100 101	Args:102		query_ids: List of IDs of the tumor samples to query.103		preds_df: DataFrame containing predictions.104		shaps: Array of SHAP values.105		df_features_genie: DataFrame containing features.106		cancer_types_to_consider: List of cancer types considered in the prediction.107		filepath: Path to save the explanation plots.108		save_plot: Whether to save the explanation plots.109	Returns:110		List of dictionaries containing prediction details and explanation plots for each query ID.111	rr"rzSAMPLE_ID: z
112Prediction: z113Prediction probability: z.3fr\)r_r`rarcrbrd)�	pred_probr�Zexplanation_plot)	�atrrir&r'rrOr��str)r�r�r�r�r�rarbrdZresults_dictZquery_idr�r�r�r^r�r]r_rLrMZ
full_filenamerrr�#get_onconpc_prediction_explanationss:114115116�117��r��data/features_onconpc.pkl�"data/combined_cohort_age_stats.pkl�)data/mutation_signatures/sigProfiler*.csv)�df_patients_chosen�df_samples_chosen�df_mutations_chosen�
df_cna_chosen�features_onconpc_path�combined_cohort_age_stats_path�mut_sig_weights_filepathrc	Cs�|�dg�j��}tj||ddd�}t|ddddd	d118d�}	t|	|d�}119t|||120|�\}}t|d
��}
t	�121|
�}W5QRXt||�}t|j
�|_
t|d
��}
t	�122|
�}W5QRX|d|d|d|d<||fS)a\123	Process raw GENIE data for OncoNPC prediction inference.124 125	Args:126		df_patients_chosen: DataFrame containing chosen patient data.127		df_samples_chosen: DataFrame containing chosen sample data.128		df_mutations_chosen: DataFrame containing chosen mutation data.129		df_cna_chosen: DataFrame containing chosen copy number alteration data.130		features_onconpc_path: Path to OncoNPC features file.131		combined_cohort_age_stats_path: Path to combined cohort age statistics file.132		weights_filepath: Path to mutation signatures weights file.133 134	Returns:135		Processed DataFrame ready for OncoNPC prediction inference.136	�Hugo_SymbolrfZ137PATIENT_ID)�howZon�Tumor_Sample_BarcodeZ138ChromosomeZStart_PositionZReference_AlleleZTumor_Seq_Allele2�genie)�
sample_id_col�chromosome_col�
start_pos_col�ref_allele_col�alt_allele_col�config)�weights_filepathrurBr{rz)�	set_indexr��copyr&�merge�get_snv_in_trinuc_context�obtain_mutation_signatures�pre_process_features_genier�r�r��zero_pad_missing_features�standardize_feat_namesr)r�r�r�r�r�r�r�Zdf_cna_reshapedZdf_patients_mergedZdf_trinuc_feats_genieZdf_mut_sigs_genier�Zdf_labels_genier�Zfeatures_onconpcZdf_features_genie_finalr�rrr�"get_onconpc_features_from_raw_dataIs0�	�139r�r�)�df_mutationsr�r�r�r�r�r�rc
Cs|||||g}||��}|dkr8d||�t�||<|||j�d�}||�d�||<d}	t�t��8ttjtj	��tj�140|�}141W5QRXt�|	�}W5QRXt�t��<ttjtj	�� tj�||142|||||��}W5QRXW5QRX|dk�r|j
�d�|_
|S)a�143	Processes the given mutations DataFrame to get SNV in trinucleotide context.144 145	Args:146		df_mutations: DataFrame containing mutation data.147		sample_id_col: Column name for sample IDs.148		chromosome_col: Column name for chromosome.149		start_pos_col: Column name for start position.150		ref_allele_col: Column name for reference allele.151		alt_allele_col: Column name for alternative allele.152	Returns:153		DataFrame with SNV in trinucleotide context.154	r��chrzGL|chrMTr�a~155	function(mutationData, sample_id, chr, pos, ref, alt) {156		library(deconstructSigs)157		sigs.input <- mut.to.sigs.input(mut.ref = mutationData, 158										sample.id = sample_id,159										chr = chr, 160										pos = pos, 161										ref = ref, 162										alt = alt)163 164		# Filter samples with low mutations165		sigs.input <- sigs.input[rowSums(sigs.input) >= 1,]166		return(sigs.input)167	}168	Zprofile_dfci�float)r�Zastyper��containsr	rr169�robjectsrZ	converterZpy2rpy�rZrpy2pyr)
r�r�r�r�r�r�r�Zcolumns_chosenZdf_mutations_chosen_trinucZr_codeZr_df_mutationsZ170r_function�df_trinuc_featsrrrr��s.171�172r��+./data/mutation_signatures/sigProfiler*.csv)r�r�rcCs�t�|�}t|�dkrtd��dd�|D�}i}tt|��D�]}t�||�}|jdd�g}t|jj	|j173j	�D].\}}	|	dd|d|	d	}174|�|175�qr||d176<|jd177dd�d�
|j�}d|kr�|||d
j	|d<n*d|k�r|||dj	|d<ntd��t�|jj	�}t|d�dk�r6td��||||<q<tjg|jd�}
|��D]\\}}tt|j�t|j�@�}t|�dk�r�td��t�||j	|j|jj	�}||
|<�q\|
S)z�Transforms tri-nucleotide features into mutation signature based features.178 179	Args:180		df_trinuc_feats: DataFrame containing tri-nucleotide features.181	Returns:182		mut_sig_based_df: DataFrame containing mutation signature based features.183	rzNo mutation signatures data.cSs"g|]}|�d�ddd��qS)�_�����N�����)�split)r�	file_namerrrr�sz.obtain_mutation_signatures.<locals>.<listcomp>T��inplace�[�]�ZType_new�ZGRCh38Z_GRCh38�	values_oiZGRCh37Z_GRCh37z$No human-based experimental results.rwrvzValues do not sum up to 1.�r�`z6Number of substitution features does not sum up to 96.)�glob�len�184ValueError�ranger&Zread_csvZdropnar%ZTyper#ZSubtyperKr��joinrr.r/r�rhr'rrVrJ�set�matmulri)r�r�Z185file_namesZfile_names_keyZsbs_conversion_df_dictr��dfZ
new_name_listZchangeZsubtype�new_nameZmerged_colum_namesZsum_valsZmut_sig_based_dfroZmut_dfZcommon_substitute_featsZmut_sig_valsrrrr��sD186187�r��	SAMPLE_ID�CANCER_TYPE)r��df_cna�df_mutation_signatures�df_patientsr�	id_column�cancer_type_columnrcs�||j}|j|j�|�}t�|jj�}	tjd|	|d�}188|ddgjD]:}|d}|d}
|
|189j	krH|190j191||
fd|192j193||
f<qHdd�|	D�|194_	|jtt|j
�t|�@�}|jddd	�|�d195�}|ddg��}g}g}g}t|j
|j�D]�\}\}}|d
k�r|�d�n |dk�r*|�d�n196|�d�t�|��rV|�|�|�d�q�dt|�k�r||�t|dd���q�dt|�k�r�|�t|dd���q�|�t|��q�||d<||d<ddg|_	tt|197j
�t|j
�@t|j
�@t|j
�@�}|198|||g}t||�}|jtt|j
�t|��}|j|j
|}�dk	�rztj�fdd�|jD�dg|j
d�}|j|d<||fS||fSdS)aPre-process genetics data to create feature df for GENIE.199	200	Args:201		df_mutations: DataFrame containing mutation data.202		df_cna: DataFrame containing CNA data.203		df_mutation_signatures: DataFrame containing mutation signature data.204		df_patients: DataFrame containing patient data.205		cancer_types: List of cancer types.206		id_column: Column name for sample IDs.207		cancer_type_column: Column name for cancer types.208	Returns:209		df_features_merged_final: DataFrame containing merged features.210		df_labels_final: DataFrame containing labels.211	r�rrr�r�rcSsg|]}|d�qS��_mutr�r�generrrrsz.pre_process_features_genie.<locals>.<listcomp>Tr�r�ZSEXZAGE_AT_SEQ_REPORTrxryr�z>89Nz<18rCrBcsg|]}��|��qSrr��r�valr!rrrAs�cancer_labelr")r#rir��isinr.�uniquer�r&r'rr�rJr�r�fillnar�r�r%rKZisnullr�r��	merge_dfs)r�r�r�r�rr�r��212sample_idsr��mutation_gene_names�df_mutation_feature�entryr�r�r��df_patients_id_indexed�df_sex_age_feature�sex_listZage_nan_indicesZage_list�sex�age�213common_ids�dfs_list�df_features_mergedZdf_features_merged_final�	df_labels�df_labels_finalrr!rr��sb214215216217218219220221�222223�224r��UNIQUE_SAMPLE_ID)r�r�r�r�r�cup_samples_idsr�rcsf||j}|j||�|�}t�|jj�}	tjd|	|d�}225|ddgjD]:}|d}|d}
|
|226jkrJ|227j	||
fd|228j	||
f<qJdd�|	D�|229_|jt230t|j�t|�@�}|j
ddd	�|�|�}|d231dg��}g}t|j|j�D]B\}\}}|dk�r|�d�q�|d
k�r$|�d�q�|�d�q�||d232<d233dg|_t234t|235j�t|j�@t|j�@t|j�@�}|236|||g}t||�}i}d|d<d|d<d|d<d|d<d|d<d|d<d|d<d|d<d|d <d!|d"<|j|dd#�|jt237t|j�t|��}|jt238t|j�t|�@�}|j|jd$}tj�fd%d�|jD�d&g|jd�}|j|d'<|||fS)(a�Pre-process genetics data to create feature df for DFCI data.239	240	Args:241		df_mutations: DataFrame containing mutation data.242		df_cna: DataFrame containing CNA data.243		df_mutation_signatures: DataFrame containing mutation signature data.244		df_patients: DataFrame containing patient data.245		cancer_types: List of cancer types.246		cup_samples_ids: List of CUP sample IDs.247		id_column: Column name for sample IDs.248	Returns:249		df_features_merged_ckps: DataFrame containing merged features for CKP samples.250		df_labels_final: DataFrame containing labels for CKP samples.251		df_features_merged_cups: DataFrame containing merged features for CUP samples.252	rr�r�CANONICAL_GENErcSsg|]}|d�qSr�rr�rrrrjsz-pre_process_features_dfci.<locals>.<listcomp>Tr�rCrBZMALEZFEMALEr�ZFAAP100ZC17ORF70ZFAAP100_mutZC17orf70_mutZFAAP24ZC19ORF40Z253FAAP24_mutZC19orf40_mutZBMS1P20_mutZLOC96610_mutzMEF2BNB-MEF2B_mutzLOC729991-MEF2B_mutZ254FAAP20_mutZC1orf86_mutZFAAP20ZC1ORF86Z	RACK1_mutZ255GNB2L1_mutZRACK1ZGNB2L1)rr�r�csg|]}��|��qSrr�r�r!rrr�sr�r")r#rir�r.r�rr&r'rr�rJr�rr�r�r�r%rKr��rename)r�r�r�r�rrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�Zprofile_old_new_gene_mappingZdf_features_merged_ckpsZdf_features_merged_cupsr�rrr!r�pre_process_features_dfciHsr256257258259260261��262����263r)r��feature_listrcs8�fdd�|D�}tj�tj|�jd�gdd���|S)z�264	Zero pad missing features.265	Args:266		df: DataFrame containing features.267		feature_list: List of features to include.268	Returns:269		df: DataFrame containing zero-padded features.270	cs&i|]}|�jkr|dgt���qS)r)rr�)rZfeature�r�rr�271<dictcomp>�s272z-zero_pad_missing_features.<locals>.<dictcomp>r�rr)r&r�r'r)r�rZnew_columnsrrrr��sr�)�df_list�273ids_commonrcCsDt|�D]6\}}|dkr$|j|}qtj||j|dddd�}q|S)z�Merges a list of dataframes using common sample IDs.274	275	Args:276		df_list: List of dataframes to merge.277		ids_common: Common sample IDs.278	Returns:279		df_merged_to_return: Merged dataframe.280	r�leftT)r�Z281left_indexZright_index)r�rir&r�)r	r282Zidxr�Zdf_merged_to_returnrrrr��s283	r�)�curr_feat_namesrcCs�g}|D]z}d|kr(|�|�dd��qd|ks8d|krD|�d�qd|ksTd|kr`|�d�qd|krt|�|�q|�|d�q|S)	z�284	Standardize feature names.285	Args:286		curr_feat_names: list of feature names287	Returns:288		new_feat_names: list of standardized feature names289	r�r�ZAGErBZGENDERrCr@z CNA)rK�replace)rZnew_feat_namesrNrrrr��sr�)NNrZr[r\F)r�)rZr[F)r�r�r�)r�)r�)Nr�r�)Nr)<r��typingrrrrrrH�osr�Zmatplotlib.patchesrZ
rpy2.robjectsr	r290Zmatplotlib.pyplotZpyplotr�Znumpyr.Zpandasr&r9r�rZrpy2.robjects.conversionrZxgboostr-Z291adjustTextr
ZsklearnZ
XGBClassifierr'r�r,�coreZBoosterr0Zndarrayr=r>rOrY�dictr��boolr�Zasarrayr�Zarrayr�r�r�r�r�rr�r�r�rrrr�<module>s������T��.��>��>��E��7�292�U�293�Q�