import matplotlib.pyplot as plt
import numpy as np
def Curve_Fitting(x, y, deg):
parameter = np.polyfit(x, y, deg)
p = np.poly1d(parameter)
aa=''
for i in range(deg+1):
bb=round(parameter[i], 2)
if bb>0:
if i==0:
bb=str(bb)
else:
bb='+'+str(bb)
else:
bb=str(bb)
if deg==i:
aa=aa+bb
else:
aa=aa+bb+'x^'+str(deg-i)
r2 = round(np.corrcoef(y, p(x))[0,1]**2,2)
plt.scatter(x, y, label="Comparison", color='b', marker='o')
plt.plot(x, p(x), color='r')
plt.legend([f"R^2: {r2}", f"Fitting Equation: {aa}"])
plt.xlabel("Old Normalized_TMB Values")
plt.ylabel("New Normalized_TMB Values")
plt.title("Comparison of Normalized_TMB")
plt.savefig('./tmp.jpg')
plt.show()
print('曲线方程为:',aa)
print(' r^2为:', r2)
def plot_scatter(old_list, new_list):
run1_values = old_list
run2_values = new_list
plt.scatter(run1_values, run2_values, label="Comparison", color='b', marker='o')
plt.xlabel("Old Normalized_TMB Values")
plt.ylabel("New Normalized_TMB Values")
plt.title("Comparison of Normalized_TMB")
plt.plot([min(run1_values), max(run1_values)], [min(run1_values), max(run1_values)], linestyle='--', color='r', label="1:1 Line")
plt.legend()
plt.savefig('./tmp.jpg')
plt.show()
normalzed_tmb_compare_file = 'normalzed_tmb.txt'
old_FFPE_list, new_FFPE_list = [], []
old_CF_list, new_CF_list = [], []
with open(normalzed_tmb_compare_file) as f:
next(f)
for line in f:
line_list = line.strip('\n').split('\t')
old, type_, new = line_list
if type_ == 'FFPE':
old_FFPE_list.append(float(line_list[0]))
new_FFPE_list.append(float(line_list[-1]))
if type_ == 'CF':
old_CF_list.append(float(line_list[0]))
new_CF_list.append(float(line_list[-1]))
Curve_Fitting(old_CF_list+old_FFPE_list, new_CF_list+new_FFPE_list, 1)