<jupyter_start><jupyter_text>Description It uses the PhenomeXcan traits to EFO mapping files to group traits that end up having the same EFO label. This only combines the fastENLOC results (RCPs) by taking the maximum value across all traits with the same EFO label.  Modules loading<jupyter_code>%load_ext autoreload
%autoreload 2
from IPython.display import display
import numpy as np
import pandas as pd

import conf
from data.cache import read_data
from entity import Trait<jupyter_output><empty_output><jupyter_text>Load fastENLOC<jupyter_code>fastenloc_rcps = read_data(conf.PHENOMEXCAN["FASTENLOC_TORUS_RCP_FILE"])
fastenloc_rcps.shape
fastenloc_rcps.head()<jupyter_output><empty_output><jupyter_text>Load S-MultiXcan z-scores (EFO-mapped)<jupyter_code>smultixcan_zscores_combined = read_data(
    conf.PHENOMEXCAN["SMULTIXCAN_EFO_PARTIAL_MASHR_ZSCORES_FILE"]
)
smultixcan_zscores_combined.shape
smultixcan_zscores_combined.head()<jupyter_output><empty_output><jupyter_text>Get PhenomeXcan traits<jupyter_code>phenomexcan_fullcode_to_traits = {
    (trait_obj := Trait.get_trait(full_code=trait_name)).full_code: trait_obj
    for trait_name in fastenloc_rcps.columns
}
len(phenomexcan_fullcode_to_traits)
assert len(phenomexcan_fullcode_to_traits) == fastenloc_rcps.columns.shape[0]<jupyter_output><empty_output><jupyter_text>Change/combine traits in S-MultiXcan results  Get a list of EFO labels for PhenomeXcan traits<jupyter_code>traits_efo_labels = [
    t.get_efo_info().label
    if (t := phenomexcan_fullcode_to_traits[c]).get_efo_info() is not None
    else t.full_code
    for c in fastenloc_rcps.columns
]
len(traits_efo_labels)
traits_efo_labels[:10]<jupyter_output><empty_output><jupyter_text>Get `max(rcp)` for same EFO labels<jupyter_code>fastenloc_rcps_combined = fastenloc_rcps.groupby(traits_efo_labels, axis=1).max()
fastenloc_rcps_combined.shape
fastenloc_rcps_combined.head()<jupyter_output><empty_output><jupyter_text>Keep same order traits as in the S-MultiXcan results<jupyter_code>assert set(fastenloc_rcps_combined.columns) == set(smultixcan_zscores_combined.columns)
fastenloc_rcps_combined = fastenloc_rcps_combined.loc[
    :, smultixcan_zscores_combined.columns
]
fastenloc_rcps_combined.shape<jupyter_output><empty_output><jupyter_text>Do we have NaN values?<jupyter_code>fastenloc_rcps_combined.isna().any().any()
fastenloc_rcps_combined.isna().sum()<jupyter_output><empty_output><jupyter_text>Testing  Stats<jupyter_code>_stats = fastenloc_rcps_combined.stack().describe()
display(_stats.apply(str))
assert _stats["min"] >= 0.0
assert _stats["max"] <= 2.5<jupyter_output><empty_output><jupyter_text>Same traits as in z-scores version<jupyter_code>assert fastenloc_rcps_combined.columns.equals(smultixcan_zscores_combined.columns)<jupyter_output><empty_output><jupyter_text>EFO label (asthma) which combined three PhenomeXcan traits.<jupyter_code>_asthma_traits = [
    "22127-Doctor_diagnosed_asthma",
    "20002_1111-Noncancer_illness_code_selfreported_asthma",
    "J45-Diagnoses_main_ICD10_J45_Asthma",
]
fastenloc_rcps[_asthma_traits]
fastenloc_rcps[_asthma_traits].isna().sum(axis=1).sort_values(ascending=False).head()
_trait = "asthma"

_gene = "ENSG00000000419"
assert fastenloc_rcps_combined.loc[_gene, _trait].round(3) == 0.001

_gene = "ENSG00000000457"
assert fastenloc_rcps_combined.loc[_gene, _trait].round(3) == 0.005

_gene = "ENSG00000284543"
assert fastenloc_rcps_combined.loc[_gene, _trait].round(4) == 0.0008

_gene = "ENSG00000253892"
assert pd.isnull(fastenloc_rcps_combined.loc[_gene, _trait])<jupyter_output><empty_output><jupyter_text>PhenomeXcan trait which has no EFO label.<jupyter_code>_trait = "100001_raw-Food_weight"
fastenloc_rcps[_trait].sort_values(ascending=False)
_gene = "ENSG00000280319"
assert fastenloc_rcps_combined.loc[_gene, _trait].round(3) == 0.360

_gene = "ENSG00000196071"
assert fastenloc_rcps_combined.loc[_gene, _trait].round(3) == 0.015

_gene = "ENSG00000284552"
assert pd.isnull(fastenloc_rcps_combined.loc[_gene, _trait])<jupyter_output><empty_output><jupyter_text>Save full (all traits, some with EFO, some not)<jupyter_code>fastenloc_rcps_combined.shape
fastenloc_rcps_combined.head()<jupyter_output><empty_output><jupyter_text>Pickle (binary)<jupyter_code>output_file = conf.PHENOMEXCAN["FASTENLOC_EFO_PARTIAL_TORUS_RCP_FILE"]
display(output_file)
fastenloc_rcps_combined.to_pickle(output_file)<jupyter_output><empty_output><jupyter_text>TSV (text)<jupyter_code># tsv format
output_text_file = output_file.with_suffix(".tsv.gz")
display(output_text_file)
fastenloc_rcps_combined.to_csv(
    output_text_file, sep="\t", index=True, float_format="%.5e"
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10147404400769067, 'fraction_numerical': 0.02990813928647725, 'mean_word_length': 9.738532110091743, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '220205', 'license': 'BSD-2-Clause-Patent', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1828, 'n_words': 250, 'path': 'nbs/01_preprocessing/010_02-fastenloc_rcp_to_efo.ipynb', 'repo_name': 'greenelab/phenoplier', 'size': '4681'}