• 【生物信息学】基因差异分析Deg(数据读取、数据处理、差异分析、结果可视化)


    目录

    一、实验介绍

    二、实验环境

    1. 配置虚拟环境

    2. 库版本介绍

    3. IDE

    三、实验内容

    0. 导入必要的工具包

    1. 定义一些阈值和参数

    2. 读取数据

    normal_data.csv部分展示

    tumor_data.csv部分展示

    3. 绘制箱型图

    4. 删除表达量低于阈值的基因

    5. 计算差异显著的基因

    6. 初始化结果数据表格

    7. 进行秩和检验和差异倍数计算

    8. 可视化分析

    9. 代码整合


    一、实验介绍

            本实验完成了基因差异分析,包括数据读取、数据处理( 绘制箱型图、删除表达量低于阈值的基因、计算差异显著的基因)、差异分析(进行秩和检验和差异倍数计算)等,成功识别出在正常样本与肿瘤样本之间显著表达差异的基因,并对其进行了进一步的可视化分析(箱型图、差异倍数fold分布图、热力图和散点图)。

            基因差异分析是研究不同条件下基因表达差异的重要手段,能够帮助我们理解生物体内基因调控的变化及其与表型特征的关联。本实验旨在探索正常样本与肿瘤样本之间基因表达的差异,并识别差异显著的基因。

    二、实验环境

        本系列实验使用了PyTorch深度学习框架,相关操作如下(基于深度学习系列文章的环境):

    1. 配置虚拟环境

    深度学习系列文章的环境

    conda create -n DL python=3.7 
    conda activate DL
    pip install torch==1.8.1+cu102 torchvision==0.9.1+cu102 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
    
    conda install matplotlib
    conda install scikit-learn

    新增加

    conda install pandas
    conda install seaborn
    conda install networkx
    conda install statsmodels
    pip install pyHSICLasso

    注:本人的实验环境按照上述顺序安装各种库,若想尝试一起安装(天知道会不会出问题)

    2. 库版本介绍

    软件包本实验版本目前最新版
    matplotlib3.5.33.8.0
    numpy1.21.61.26.0
    python3.7.16
    scikit-learn0.22.11.3.0
    torch1.8.1+cu1022.0.1
    torchaudio0.8.12.0.2
    torchvision0.9.1+cu1020.15.2

    新增

    networkx2.6.33.1
    pandas1.2.32.1.1
    pyHSICLasso1.4.21.4.2
    seaborn0.12.20.13.0
    statsmodels0.13.50.14.0

    3. IDE

            建议使用Pycharm(其中,pyHSICLasso库在VScode出错,尚未找到解决办法……)

    win11 安装 Anaconda(2022.10)+pycharm(2022.3/2023.1.4)+配置虚拟环境_QomolangmaH的博客-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/128693741

    三、实验内容

    0. 导入必要的工具包

    1. import numpy as np
    2. import pandas as pd
    3. import scipy
    4. import matplotlib.pyplot as plt
    5. import seaborn as sns
    6. from scipy.stats import zscore
    7. from scipy.stats import ranksums
    8. from pyHSICLasso import HSICLasso
    9. from sklearn.preprocessing import LabelEncoder

    1. 定义一些阈值和参数

    1. p_cutoff = 0.001
    2. FC_cutoff = 3
    3. num_cutoff = 10
    • p_cutoff 是判断差异显著性的 p 值阈值。
    • FC_cutoff 是判断差异的倍数阈值。
    • num_cutoff 是基因表达量筛选的阈值。

    2. 读取数据

    1. ndata = pd.read_csv("normal_data.csv", index_col=0, header=0)
    2. tdata = pd.read_csv("tumor_data.csv", index_col=0, header=0)

            从文件 "normal_data.csv" 和 "tumor_data.csv" 中读取正常样本和肿瘤样本的基因表达数据。

    normal_data.csv部分展示

    TCGA-BL-A13J-11A-13R-A10U-07TCGA-BT-A20N-11A-11R-A14Y-07TCGA-BT-A20Q-11A-11R-A14Y-07TCGA-BT-A20R-11A-11R-A16R-07TCGA-BT-A20U-11A-11R-A14Y-07TCGA-BT-A20W-11A-11R-A14Y-07TCGA-BT-A2LA-11A-11R-A18C-07TCGA-BT-A2LB-11A-11R-A18C-07TCGA-CU-A0YN-11A-11R-A10U-07TCGA-CU-A0YR-11A-13R-A10U-07TCGA-GC-A3BM-11A-11R-A22U-07TCGA-GC-A3WC-11A-11R-A22U-07TCGA-GC-A6I3-11A-11R-A31N-07TCGA-GD-A2C5-11A-11R-A180-07TCGA-GD-A3OP-11A-11R-A220-07TCGA-GD-A3OQ-11A-21R-A220-07TCGA-K4-A3WV-11A-21R-A22U-07TCGA-K4-A54R-11A-11R-A26T-07TCGA-K4-A5RI-11A-11R-A28M-07
    ?10000000000000000000
    ?22.55213.89335.459416.458334.42645.221911.34638.2564.30548.747121.00843.183706.012817.762140.84528.308813.72161.5099
    ?38.51195.730510.44177.68119.0621.35343.421925.75141.48346.54719.65152.64725.312110.733719.36419.13819.527318.536414.4093
    ?4117.4995103.9108148.8251100.9537162.7054128.9808118.3924124.8423180.5137131.3118127.6985145.632795.2855119.1427149.7972128.4473142.8571128.644139.1199
    ?5619.5833738.4077679.3286786.70361132.5581032.8771158.651143.321687.4096690.5882697.5129697.37611551.793556.22011018.907646.4851895.4832903.8232836.9674
    ?60000000000000000000
    ?7128.3422115.4856119.258120.6965120.930254.794582.700491.372966.5702153.5294259.3317272.8863291.500794.4976250.6016253.2622313.5504270.6093187.172
    ?80.7376000.39570.77520.547900.463800000000.3332000
    ?900000000000000.398700000

    tumor_data.csv部分展示

    TCGA-BT-A42F-01A-11R-A23W-07TCGA-C4-A0EZ-01A-21R-A24X-07TCGA-C4-A0F0-01A-12R-A10U-07TCGA-C4-A0F1-01A-11R-A034-07TCGA-C4-A0F6-01A-11R-A10U-07TCGA-C4-A0F7-01A-11R-A084-07TCGA-CF-A1HR-01A-11R-A13Y-07TCGA-CF-A1HS-01A-11R-A13Y-07TCGA-CF-A27C-01A-11R-A16R-07TCGA-CF-A3MF-01A-12R-A21D-07TCGA-CF-A3MG-01A-11R-A20F-07TCGA-CF-A3MH-01A-11R-A20F-07TCGA-CF-A3MI-01A-11R-A20F-07TCGA-CF-A47S-01A-11R-A23W-07TCGA-CF-A47T-01A-11R-A23W-07TCGA-CF-A47V-01A-11R-A23W-07TCGA-CF-A47W-01A-11R-A23W-07TCGA-CF-A47X-01A-31R-A23W-07TCGA-CF-A47Y-01A-11R-A23W-07TCGA-CF-A5U8-01A-11R-A28M-07
    ?100.367100000000002.1101000000.57370
    ?23.76137.328105.35982.80936.477713.93723.568440.37629.09149.08534.74093.9742.39047.021111.2489019.57226.34546.4641
    ?32.89595.52180.663604.5452.89226.81445.397541.06653.62386.080612.13348.68657.80335.667717.39535.188717.411218.324713.092
    ?4117.7889443.1501144.144692.6276136.3486238.55791.604211.2791100.480927.5497101.9194105.91878.537120.6983212.084670.1299106.1415150.7324103.2415149.5243
    ?51754.6361546.3974391.827868.8195685.42011752.1671229.951456.665966.06551691.1261220.8531279.2292167.0481399.5921939.577978.75961754.7171110.2251262.7651477.273
    ?600000000000000000000
    ?7192.1065306.195515.926866.9972157.381932.169973.471749.6115426.9924745.960348.8152402.5713229.9982266.8196120.8459472.8729135.8491640.3191553.0694511.0994
    ?802.9371000.7354000.59771.16351.059600.803500.50970.60420.485501.45034.58980.5285
    ?900000000000000000000

    3. 绘制箱型图

    1. ndata.iloc[1000:1010, ].transpose().plot(kind='box', title='Normal Sample Gene Boxplot', rot=30)
    2. plt.tick_params(labelsize=10)
    3. plt.savefig('Normal_Sample_Gene_box_plot', bbox_inches='tight')
    4. plt.show()

            绘制正常样本中部分基因的箱型图,并保存为图片文件。

    4. 删除表达量低于阈值的基因

    1. ndata = ndata.iloc[29:, :]
    2. tdata = tdata.iloc[29:, :]

            删除正常样本和肿瘤样本中表达量低于阈值的基因。

    5. 计算差异显著的基因

    1. gene_sig = ndata[ndata.mean(axis=1) > num_cutoff].index.intersection(tdata[tdata.mean(axis=1) > num_cutoff].index)
    2. Ndata = ndata.loc[gene_sig]
    3. Tdata = tdata.loc[gene_sig]

            选择正常样本和肿瘤样本中表达量高于阈值的基因,并保存为新的数据。

    6. 初始化结果数据表格

    1. p_value = [1.] * len(Ndata)
    2. log2FoldChange = []
    3. label = ['0'] * len(Ndata)
    4. result = pd.DataFrame([p_value, log2FoldChange, label])
    5. result = result.transpose()
    6. result.columns = ['p_value', 'log2FC', 'label']
    7. result.index = Ndata.index.tolist()
    8. p = []

            创建一个结果数据表格,包含 p 值、log2 fold change 和标签,并初始化为默认值。

    7. 进行秩和检验和差异倍数计算

    1. for i in Ndata.index:
    2. p1 = ranksums(Tdata.loc[i], Ndata.loc[i], alternative='greater')[1]
    3. p2 = ranksums(Tdata.loc[i], Ndata.loc[i], alternative='less')[1]
    4. p3 = ranksums(Tdata.loc[i], Ndata.loc[i])[1]
    5. result.loc[i, 'log2FC'] = np.log2(np.average(Tdata.loc[i]) / np.average(Ndata.loc[i]))
    6. p.append(p3)
    7. if (p1 <= p_cutoff):
    8. result.loc[i, 'p_value'] = p1
    9. if result.loc[i, 'log2FC'] > np.log2(FC_cutoff):
    10. result.loc[i, 'label'] = '1'
    11. if (p2 <= p_cutoff):
    12. result.loc[i, 'p_value'] = p2
    13. if result.loc[i, 'log2FC'] < -np.log2(FC_cutoff):
    14. result.loc[i, 'label'] = '-1'

            对每个基因进行秩和检验,计算 p 值和差异倍数,并根据设定的阈值确定差异显著的基因,并给予标签。

    8. 可视化分析

    1. print('finished')
    2. plt.hist(result['log2FC'], bins=10, color='blue', alpha=0.6, edgecolor='black')
    3. plt.title("fold change")
    4. plt.show()

    9. 代码整合

    1. import numpy as np
    2. import pandas as pd
    3. import scipy
    4. import matplotlib.pyplot as plt
    5. import seaborn as sns
    6. from scipy.stats import zscore
    7. from scipy.stats import ranksums
    8. from pyHSICLasso import HSICLasso
    9. from sklearn.preprocessing import LabelEncoder
    10. p_cutoff = 0.001
    11. FC_cutoff = 3
    12. num_cutoff = 10
    13. # 读取数据ndata normal data,tdata tumor data
    14. ndata = pd.read_csv("normal_data.csv", index_col=0, header=0)
    15. tdata = pd.read_csv("tumor_data.csv", index_col=0, header=0)
    16. # 箱型图
    17. # 中位数
    18. # QL称为下四分位数,表示全部观察值中有四分之一的数据取值比它小;
    19. # QU称为上四分位数,表示全部观察值中有四分之一的数据取值比它大;
    20. # IQR称为四分位数间距,是上四分位数QU与下四分位数QL之差,其间包含了全部观察值的一半。
    21. # 异常值通常被定义为小于QL-1.5IQR或大于QU+1.5IQR的值。
    22. ndata.iloc[1000:1010, ].transpose().plot(kind='box', title='Normal Sample Gene Boxplot', rot=30)
    23. plt.tick_params(labelsize=10)
    24. plt.savefig('Normal_Sample_Gene_box_plot', bbox_inches='tight')
    25. plt.show()
    26. # delete gene expression < num
    27. ndata = ndata.iloc[29:, :]
    28. tdata = tdata.iloc[29:, :]
    29. gene_sig = ndata[ndata.mean(axis=1) > num_cutoff].index.intersection(tdata[tdata.mean(axis=1) > num_cutoff].index)
    30. Ndata = ndata.loc[gene_sig]
    31. Tdata = tdata.loc[gene_sig]
    32. p_value = [1.] * len(Ndata)
    33. # log2 fold change的意思是取log2,这样可以可以让差异特别大的和差异比较小的数值缩小之间的差距
    34. log2FoldChange = []
    35. label = ['0'] * len(Ndata)
    36. result = pd.DataFrame([p_value, log2FoldChange, label])
    37. result = result.transpose()
    38. result.columns = ['p_value', 'log2FC', 'label']
    39. result.index = Ndata.index.tolist()
    40. p = []
    41. # 秩和检验(也叫Mann-Whitney U-test),检验两组数据是否来自具有相同中位数的连续分布,检验它们是否有显著差异。
    42. # ’less‘ 基于 x 的分布小于基于 y 的分布
    43. # ‘greater’ x 的分布大于 y 的分布。
    44. for i in Ndata.index:
    45. p1 = ranksums(Tdata.loc[i], Ndata.loc[i], alternative='greater')[1]
    46. p2 = ranksums(Tdata.loc[i], Ndata.loc[i], alternative='less')[1]
    47. p3 = ranksums(Tdata.loc[i], Ndata.loc[i])[1]
    48. result.loc[i, 'log2FC'] = np.log2(np.average(Tdata.loc[i]) / np.average(Ndata.loc[i]))
    49. p.append(p3)
    50. if (p1 <= p_cutoff):
    51. result.loc[i, 'p_value'] = p1
    52. if result.loc[i, 'log2FC'] > np.log2(FC_cutoff):
    53. result.loc[i, 'label'] = '1'
    54. if (p2 <= p_cutoff):
    55. result.loc[i, 'p_value'] = p2
    56. if result.loc[i, 'log2FC'] < -np.log2(FC_cutoff):
    57. result.loc[i, 'label'] = '-1'
    58. print('finished')
    59. # 查看差异倍数fold分布
    60. plt.hist(result['log2FC'], bins=10, color='blue', alpha=0.6, edgecolor='black')
    61. plt.title("fold change")
    62. plt.show()
    63. # result.to_csv('result.csv')
    64. up_data_n = Ndata.loc[result.query("(label == '1')").index]
    65. up_data_t = Tdata.loc[result.query("(label == '1')").index]
    66. down_data_n = Ndata.loc[result.query("(label == '-1')").index]
    67. down_data_t = Tdata.loc[result.query("(label == '-1')").index]
    68. data = pd.concat([pd.concat([up_data_n, up_data_t], axis=1), pd.concat([down_data_n, down_data_t], axis=1)], axis=0)
    69. deg_gene = pd.DataFrame(data.index.tolist())
    70. deg_gene.to_csv('deg_gene.csv')
    71. z1 = zscore(np.log2(data+1), axis=0)
    72. sns.heatmap(data=z1, cmap="coolwarm", xticklabels=False)
    73. plt.savefig('heatmap_plot', bbox_inches='tight')
    74. plt.show()
    75. p = -np.log10(p)
    76. ax = sns.scatterplot(x="log2FC", y=p,
    77. hue='label',
    78. hue_order=('-1', '0', '1'),
    79. palette=("#377EB8","grey","#E41A1C"),
    80. data=result)
    81. ax.set_ylabel('-log(pvalue)', fontweight='bold')
    82. ax.set_xlabel('FoldChange', fontweight='bold')
    83. plt.savefig('deg_p_fc_plot', bbox_inches='tight')
    84. plt.show()


     

  • 相关阅读:
    Day 46 | 139.单词拆分 & 多重背包理论基础 & 背包问题总结
    小学生python游戏编程arcade----基本知识5 自动行走的敌人
    开发npm第三方库的实战经验
    商城接口文档
    合规性管理如何帮助产品团队按时交付?
    maven本地仓库配置
    程序员连拿3份offer,平均薪资高达25K,他是如何做到的?
    【Proteus仿真】【51单片机】水质监测报警系统设计
    MySQL INTERVAL 关键字指南
    【开源】SpringBoot框架开发公司货物订单管理系统
  • 原文地址:https://blog.csdn.net/m0_63834988/article/details/133443138