File size: 6,931 Bytes
2d12bc4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
#!/usr/bin/python
# coding: utf-8
# Author: LE YUAN
import os
import csv
import math
import xlrd
import pickle
import numpy as np
import pandas as pd
from rdkit import Chem
from Bio import SeqIO
from collections import defaultdict
from scipy import stats
from scipy.stats import ranksums
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from matplotlib import rc
def plot_spe_gen() :
with open('../../../BayesianApproach/Results/kcat_gen_spe.txt') as infile :
alllines = infile.readlines()[1:]
# all_clades = ['Outgroup', 'Lipomycetaceae', 'Trigonopsidaceae', 'Dipodascaceae/Trichomonascaceae', 'Alloascoideaceae', 'Sporopachydermia clade',
# 'Pichiaceae', 'CUG-Ala', 'CUG-Ser1', 'CUG-Ser2', 'Phaffomycetaceae', 'Saccharomycodaceae', 'Saccharomycetaceae']
alldata = dict()
alldata['type'] = list()
alldata['clade'] = list()
alldata['Kcat_value'] = list()
for line in alllines :
kcatValue = float(line.strip().split('\t')[0])
data_type = line.strip().split('\t')[1]
clade_order = int(line.strip().split('\t')[2])
if data_type == 'gen' :
alldata['type'].append('Generalist')
alldata['clade'].append(clade_order)
alldata['Kcat_value'].append(kcatValue)
for line in alllines :
kcatValue = float(line.strip().split('\t')[0])
data_type = line.strip().split('\t')[1]
clade_order = int(line.strip().split('\t')[2])
if data_type == 'spe' :
alldata['type'].append('Specialist')
alldata['clade'].append(clade_order)
alldata['Kcat_value'].append(kcatValue)
allData = pd.DataFrame(alldata)
# print(type(allData))
for clade in range(1,14) :
print('This is the clade:', clade)
cluster_1 = list()
cluster_2 = list()
# types = allData.iloc[:,1]
# print(len(types))
# print(types[:3])
# for clade_type in types :
# if clade_type == clade :
for row_index, row in allData.iterrows() :
if row['clade'] == clade and row['type'] == 'Specialist' :
# print(row['Kcat_value'])
cluster_1.append(row['Kcat_value'])
if row['clade'] == clade and row['type'] == 'Generalist' :
# print(row['Kcat_value'])
cluster_2.append(row['Kcat_value'])
stat, p_value = ranksums(cluster_1,cluster_2)
print('The P_value between the two clusters is:', p_value)
# Results :
# This is the clade: 1
# The P_value between the two clusters is: 1.7089850523355335e-33
# This is the clade: 2
# The P_value between the two clusters is: 5.4226066209879584e-24
# This is the clade: 3
# The P_value between the two clusters is: 9.280288602757499e-20
# This is the clade: 4
# The P_value between the two clusters is: 7.655748855812308e-177
# This is the clade: 5
# The P_value between the two clusters is: 1.2522285545479606e-08
# This is the clade: 6
# The P_value between the two clusters is: 2.2092138972003236e-10
# This is the clade: 7
# The P_value between the two clusters is: 6.9148612803985e-238
# This is the clade: 8
# The P_value between the two clusters is: 4.720528655096346e-15
# This is the clade: 9
# The P_value between the two clusters is: 0.0
# This is the clade: 10
# The P_value between the two clusters is: 3.7033670928277406e-22
# This is the clade: 11
# The P_value between the two clusters is: 1.2089161920329716e-127
# This is the clade: 12
# The P_value between the two clusters is: 4.4365830029574404e-39
# This is the clade: 13
# The P_value between the two clusters is: 0.0
plt.figure(figsize=(2.5, 2.0))
# To solve the 'Helvetica' font cannot be used in PDF file
# https://stackoverflow.com/questions/59845568/the-pdf-backend-does-not-currently-support-the-selected-font
rc('font',**{'family':'serif','serif':['Helvetica']})
plt.rcParams['pdf.fonttype'] = 42
plt.axes([0.12,0.12,0.83,0.83])
plt.tick_params(direction='in')
plt.tick_params(which='major',length=1.5)
plt.tick_params(which='major',width=0.4)
plt.tick_params(which='major',width=0.4)
palette = {"Specialist": '#b2182b', "Generalist": '#2166ac'}
ax = sns.boxplot(data=alldata, x="clade", y="Kcat_value", hue="type",
palette=palette, showfliers=False, linewidth=0.5)
# https://stackoverflow.com/questions/58476654/how-to-remove-or-hide-x-axis-label-from-seaborn-boxplot
# plt.xlabel(None) will remove the Label, but not the ticks.
ax.set(xlabel=None)
# ax.set(xticks=None)
for patch in ax.artists:
r, g, b, a = patch.get_facecolor()
patch.set_facecolor((r, g, b, 0.3))
# print(ax.artists)
# print(ax.lines)
# print(len(ax.lines))
# https://cduvallet.github.io/posts/2018/03/boxplots-in-python
for i, artist in enumerate(ax.artists):
# print(i)
if i % 2 == 0:
col = '#2166ac'
else:
col = '#b2182b'
# if i % 2 == 0:
# col = '#b2182b'
# else:
# col = '#2166ac'
# This sets the color for the main box
artist.set_edgecolor(col)
# Each box has 5 associated Line2D objects (to make the whiskers, fliers, etc.)
# Loop over them here, and use the same colour as above
for j in range(i*5,i*5+5):
# print(j)
line = ax.lines[j]
line.set_color(col)
line.set_mfc(col)
line.set_mec(col)
handles = [ax.artists[0], ax.artists[1]]
# for tick in ax.get_xticklabels() :
# tick.set_rotation(30)
plt.rcParams['font.family'] = 'Helvetica'
for i in range(13) :
plt.text(i-0.3, 2.95, '***', fontweight ="normal", fontsize=6)
plt.ylabel("$k$$_\mathregular{cat}$ value", fontname='Helvetica', fontsize=7)
plt.xticks(rotation=30,ha='right')
plt.ylim(-2,5)
plt.yticks([-2,-1,0,1,2,3,4,5])
plt.xticks(fontsize=7)
plt.yticks(fontsize=6)
ax.spines['bottom'].set_linewidth(0.5)
ax.spines['left'].set_linewidth(0.5)
ax.spines['top'].set_linewidth(0.5)
ax.spines['right'].set_linewidth(0.5)
ax = plt.gca()
# handles,labels = ax.get_legend_handles_labels()
labels = ax.get_legend_handles_labels()[1]
# print(handles)
# print(labels)
# specify just one legend
lgd = plt.legend(handles[0:2], labels[0:2], loc=1, frameon=False, prop={'size': 6})
# https://blog.csdn.net/weixin_38314865/article/details/88633880
plt.savefig("../../Results/figures/SuppleFig8a.pdf", dpi=400, bbox_inches = 'tight')
if __name__ == '__main__' :
plot_spe_gen()
|