<jupyter_start><jupyter_text>肿瘤个体化诊疗基因检测统计  样本选取条件<jupyter_code>try:
    print(cdx.filter_description())
    print(f'样本总量为{cdx.sample_size()}例。')
except Exception as e:
        print(e)<jupyter_output><empty_output><jupyter_text>送检样本量<jupyter_code>try:
    scdx=cdx
    chosen_cancer_types='肾癌/胃肠道间质瘤/胰腺癌/食管癌/神经内分泌肿瘤/非小细胞肺癌/甲状腺癌/睾丸癌/胃癌/卵巢癌/膀胱癌/恶性胸膜间皮瘤/乳腺癌/阴茎癌/黑色素瘤/胆管癌/胆囊癌/肝细胞癌/软组织肉瘤/胸腺癌/胸腺瘤/骨癌/子宫肿瘤/结直肠癌/中枢神经系统肿瘤/非黑色素瘤皮肤癌/小细胞肺癌/宫颈癌/头颈癌/前列腺癌/外阴癌/肛门癌/小肠腺癌/默克尔细胞癌/不分癌种'.split('/')

    value_counts=scdx.sample_size('CANCER_TYPE').reindex(chosen_cancer_types).fillna(0).sort_values(ascending=False)

    fig= px.pie(values=value_counts,names=value_counts.index)
    fig.update_traces( texttemplate = "%{label}: %{value} <br>%{percent:.2%}")
    #fig.update_layout(showlegend=False)
    fig.update_traces(textposition='inside')
    fig.update_layout(
                uniformtext_minsize=10,  # 文本信息最小值
                uniformtext_mode='hide')
    fig.show()
except:
    print('Data selected don`t support this calculation.')<jupyter_output><empty_output><jupyter_text>药物检测阳性率<jupyter_code>#判断数据集是否支持药物阳性率的统计
try:
    scdx=cdx
    support_for_drug_sensitivity=False
    if 'GENETIC_TEST_RESULT' in scdx.cli.columns:
        support_for_drug_sensitivity=True


    if support_for_drug_sensitivity:
        pr=pypeta.positive_rate(scdx.cli.GENETIC_TEST_RESULT,['阳性'])

        print(f'总例数为{pr[0]}，其中有效{pr[1]}例，阳性率为{pr[2]:8.2%}。')
    else:
        print("数据不支持该选项。")
except:
    print('Data selected don`t support this calculation.')
f = io.StringIO()
with redirect_stderr(f):
    cli=scdx.cli.copy()
    try:
        x=cli['GENETIC_TEST_RESULT'].groupby(cli['CANCER_TYPE']).apply(lambda x: pypeta.positive_rate(x,['阳性'])[2]).sort_values(ascending=False)

        xdf=pd.DataFrame(x).reset_index()


        print('各个癌种的药物阳性率为：')
        fig = px.bar(xdf, x='CANCER_TYPE', y='GENETIC_TEST_RESULT',text='GENETIC_TEST_RESULT',labels={
                             'CANCER_TYPE':'癌种',
                             'GENETIC_TEST_RESULT':'药物检出阳性率',
                         })
        #fig.update_traces(texttemplate='%{text:%.2f%%}', textposition='outside',)
        fig.update_traces(texttemplate='%{text:.2%}', textposition='outside',)
        fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
        fig.update_layout()
        fig.show()
    except:
        print('Data selected don`t support this calculation.')<jupyter_output><empty_output><jupyter_text>基因检出率<jupyter_code>try:
    ser=cdx.test_positive_rate(groupby_genes=True)
    mut_freq_per_gene_df=ser.sort_values(ascending=False).reset_index()

    mut_freq_per_gene_df.columns=pd.Index(['基因','频率'])
    print('各基因的检出率为：')
    fig = px.bar(mut_freq_per_gene_df, x='基因', y='频率',text='频率')
    #fig.update_traces(texttemplate='%{text:%.2f%%}', textposition='outside',)
    fig.update_traces(texttemplate='%{text:.2%}', textposition='outside',)
    fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
    fig.show()
except:
    print('Data selected don`t support this calculation.')<jupyter_output><empty_output><jupyter_text>基因突变类型检出率<jupyter_code>try:
    mut_freq_per_gene_df=cdx.test_positive_rate(groupby_variant_type=True).reset_index()

    mut_freq_per_gene_df.columns=pd.Index(['类型','频率'])
    print('各类型的检出率为：')
    fig = px.bar(mut_freq_per_gene_df, x='类型', y='频率',text='频率')
    #fig.update_traces(texttemplate='%{text:%.2f%%}', textposition='outside',)
    fig.update_traces(texttemplate='%{text:.2%}', textposition='outside',)
    fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
    fig.show()
except:
    print('Data selected don`t support this calculation.')<jupyter_output><empty_output><jupyter_text>TMB分布<jupyter_code>try:
    cdx_tmb=cdx

    chosen_cancer_types='肾癌/胃肠道间质瘤/胰腺癌/食管癌/神经内分泌肿瘤/非小细胞肺癌/甲状腺癌/睾丸癌/胃癌/卵巢癌/膀胱癌/恶性胸膜间皮瘤/乳腺癌/阴茎癌/黑色素瘤/胆管癌/胆囊癌/肝细胞癌/软组织肉瘤/胸腺癌/胸腺瘤/骨癌/子宫肿瘤/结直肠癌/中枢神经系统肿瘤/非黑色素瘤皮肤癌/小细胞肺癌/宫颈癌/头颈癌/前列腺癌/外阴癌/肛门癌/小肠腺癌/默克尔细胞癌/不分癌种'.split('/')

    cli=cdx_tmb.cli[cdx_tmb.cli.TMB.map(lambda x: pypeta.is_float(x))].copy()

    cli=cli[cli.CANCER_TYPE.isin(chosen_cancer_types)].copy()

    cli.TMB=cli.TMB.astype('float')

    display(cli.TMB.groupby(cli.CANCER_TYPE).describe())
    
    # fig1
    fig=px.histogram( x=cli.TMB,labels={'x':'TMB','y':'百分比'},histnorm='probability density',marginal="rug",)
    fig.show()
    
    fig=px.violin(cli,x='CANCER_TYPE', y="TMB", box=True, # draw box plot inside the violin
                    points='all', # can be 'outliers', or False
                   )
    fig.show()
    
except Exception as e:
    print(e)<jupyter_output><empty_output><jupyter_text>MSI分布<jupyter_code>try:    
    cdx_tmb=cdx

    chosen_cancer_types='肾癌/胃肠道间质瘤/胰腺癌/食管癌/神经内分泌肿瘤/非小细胞肺癌/甲状腺癌/睾丸癌/胃癌/卵巢癌/膀胱癌/恶性胸膜间皮瘤/乳腺癌/阴茎癌/黑色素瘤/胆管癌/胆囊癌/肝细胞癌/软组织肉瘤/胸腺癌/胸腺瘤/骨癌/子宫肿瘤/结直肠癌/中枢神经系统肿瘤/非黑色素瘤皮肤癌/小细胞肺癌/宫颈癌/头颈癌/前列腺癌/外阴癌/肛门癌/小肠腺癌/默克尔细胞癌/不分癌种'.split('/')

    cli=cli[cli.CANCER_TYPE.isin(chosen_cancer_types)].copy()


    

    cli=cli.dropna(subset=['MSI_STATUS'])
    cli=cli[cli.MSI_STATUS.isin(['MSI-L','MSI-H','MSS'])]

    display(cli.MSI_STATUS.groupby(cli.CANCER_TYPE).value_counts().unstack(level=1).fillna(0))

    vc=cli.MSI_STATUS.value_counts()
    tdf=vc.reindex(set(vc.index).union(set(['MSI-L','MSI-H','MSS']))).fillna(0).reset_index()
    fig= px.pie(tdf, values='MSI_STATUS', names='index')
    fig.update_traces( texttemplate = "%{label}: %{value} <br>%{percent:.2%}")
    fig.update_layout(showlegend=False)
    fig.show()

    tmp=cli.MSI_STATUS.groupby(cli.CANCER_TYPE).value_counts().reset_index('CANCER_TYPE')

    tmp=tmp.rename(columns={'MSI_STATUS':'COUNT'})

    tmp=tmp.reset_index()

    print('分癌种的MSI分布如下：')
    fig=px.sunburst(tmp,path=['CANCER_TYPE','MSI_STATUS'],values='COUNT')
    fig.update_traces( texttemplate = "%{label}: %{value} ")
    #fig.update_layout(showlegend=False)
    fig.show()

    msi_tmp_df=cli[['CANCER_TYPE','MSI_STATUS']]
    msi_tmp_df.columns=pd.Index(['癌症类型','MSI状态'])

    fig = px.parallel_categories(msi_tmp_df,)
    fig.show()
except Exception as e:
    print('Data selected don`t support this calculation.')
    print(e)<jupyter_output><empty_output><jupyter_text>基因融合详情<jupyter_code>try:
    pd.set_option('display.max_rows', None)
    
    display(cdx.sv)
except:
    print('Data selected don`t support this calculation.')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16742722265932336, 'fraction_numerical': 0.0037765538945712038, 'mean_word_length': 4.609885260370698, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '667276', 'license': 'MIT', 'n_tokens_mistral': 3425, 'n_tokens_neox': 3285, 'n_words': 219, 'path': 'PETA_report_template__Python__Product_statistics.ipynb', 'repo_name': 'JaylanLiu/PETA_report_template__Python__Product_statistics', 'size': '6355'}