swp2.py 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. import matplotlib.pyplot as plt
  2. import os
  3. import numpy as np
  4. import math
  5. import json
  6. def log_facto(k):
  7. """
  8. Using the Stirling's approximation
  9. """
  10. k = int(k)
  11. if k > 1e6:
  12. return k * np.log(k) - k + np.log(2*math.pi*k)/2
  13. val = 0
  14. for i in range(2, k+1):
  15. val += np.log(i)
  16. return val
  17. def parse_stwp_theta_file(stwp_theta_file, breaks, mu, tgen, relative_theta_scale = False):
  18. with open(stwp_theta_file, "r") as swp_file:
  19. # Read the first line
  20. line = swp_file.readline()
  21. L = float(line.split()[2])
  22. rands = swp_file.readline()
  23. line = swp_file.readline()
  24. # skip empty lines before SFS
  25. while line == "\n":
  26. line = swp_file.readline()
  27. sfs = np.array(line.split()).astype(float)
  28. # Process lines until the end of the file
  29. while line:
  30. # check at each line
  31. if line.startswith("dim") :
  32. dim = int(line.split()[1])
  33. if dim == breaks+1:
  34. likelihood = line.split()[5]
  35. groups = line.split()[6:6+dim]
  36. theta_site = line.split()[6+dim:6+dim+1+dim]
  37. elif dim < breaks+1:
  38. line = swp_file.readline()
  39. continue
  40. elif dim > breaks+1:
  41. break
  42. #return 0,0,0
  43. # Read the next line
  44. line = swp_file.readline()
  45. #### END of parsing
  46. # quit this file if the number of dimensions is incorrect
  47. if dim < breaks+1:
  48. return 0,0,0,0,0,0
  49. # get n, the last bin of the last group
  50. # revert the list of groups as the most recent times correspond
  51. # to the closest and last leafs of the coal. tree.
  52. groups = groups[::-1]
  53. theta_site = theta_site[::-1]
  54. # store thetas for later use
  55. grps = groups.copy()
  56. thetas = {}
  57. for i in range(len(groups)):
  58. grps[i] = grps[i].split(',')
  59. thetas[i] = [float(theta_site[i]), grps[i], likelihood]
  60. # initiate the dict of times
  61. t = {}
  62. # list of thetas
  63. theta_L = []
  64. sum_t = 0
  65. for group_nb, group in enumerate(groups):
  66. ###print(group_nb, group, theta_site[group_nb], len(theta_site))
  67. # store all the thetas one by one, with one theta per group
  68. theta_L.append(float(theta_site[group_nb]))
  69. # if the group is of size 1
  70. if len(group.split(',')) == 1:
  71. i = int(group)
  72. # if the group size is >1, take the first elem of the group
  73. # i is the first bin of each group, straight after a breakpoint
  74. else:
  75. i = int(group.split(",")[0])
  76. j = int(group.split(",")[-1])
  77. t[i] = 0
  78. #t =
  79. if len(group.split(',')) == 1:
  80. k = i
  81. if relative_theta_scale:
  82. t[i] += ((theta_L[group_nb] ) / (k*(k-1)))
  83. else:
  84. t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu
  85. else:
  86. for k in range(j, i-1, -1 ):
  87. if relative_theta_scale:
  88. t[i] += ((theta_L[group_nb] ) / (k*(k-1)))
  89. else:
  90. t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu
  91. # we add the cumulative times at the end
  92. t[i] += sum_t
  93. sum_t = t[i]
  94. # build the y axis (sizes)
  95. y = []
  96. for theta in theta_L:
  97. if relative_theta_scale:
  98. size = theta
  99. else:
  100. # with size N = theta/4mu
  101. size = theta / (4*mu)
  102. y.append(size)
  103. y.append(size)
  104. # build the time x axis
  105. x = [0]
  106. for time in range(0, len(t.values())-1):
  107. x.append(list(t.values())[time])
  108. x.append(list(t.values())[time])
  109. x.append(list(t.values())[len(t.values())-1])
  110. return x,y,likelihood,thetas,sfs,L
  111. def plot_straight_x_y(x,y):
  112. x_1 = [x[0]]
  113. y_1 = []
  114. for i in range(0, len(y)-1):
  115. x_1.append(x[i])
  116. x_1.append(x[i])
  117. y_1.append(y[i])
  118. y_1.append(y[i])
  119. y_1 = y_1+[y[-1],y[-1]]
  120. x_1.append(x[-1])
  121. return x_1, y_1
  122. def plot_all_epochs_thetafolder(full_dict, mu, tgen, title = "Title",
  123. theta_scale = True, ax = None, input = None, output = None):
  124. my_dpi = 300
  125. if ax is None:
  126. # intialize figure
  127. my_dpi = 300
  128. fnt_size = 18
  129. # plt.rcParams['font.size'] = fnt_size
  130. fig, ax1 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  131. else:
  132. fnt_size = 12
  133. # plt.rcParams['font.size'] = fnt_size
  134. ax1 = ax[1][0,0]
  135. ax1.set_yscale('log')
  136. ax1.set_xscale('log')
  137. ax1.grid(True,which="both", linestyle='--', alpha = 0.3)
  138. plot_handles = []
  139. best_plot = full_dict['all_epochs']['best']
  140. p0, = ax1.plot(best_plot[0], best_plot[1], 'o', linestyle = "-",
  141. alpha=1, lw=2, label = str(best_plot[2])+' brks | Lik='+best_plot[3])
  142. plot_handles.append(p0)
  143. for k, plot_Lk in enumerate(full_dict['all_epochs']['plots']):
  144. plot_Lk = str(full_dict['all_epochs']['plots'][k][3])
  145. # plt.rcParams['font.size'] = fnt_size
  146. p, = ax1.plot(full_dict['all_epochs']['plots'][k][0], full_dict['all_epochs']['plots'][k][1], 'o', linestyle = "--",
  147. alpha=1/(k+1), lw=1.5, label = str(full_dict['all_epochs']['plots'][k][2])+' brks | Lik='+plot_Lk)
  148. plot_handles.append(p)
  149. if theta_scale:
  150. ax1.set_xlabel("Coal. time", fontsize=fnt_size)
  151. ax1.set_ylabel("Pop. size scaled by N0", fontsize=fnt_size)
  152. # recent_scale_lower_bound = 0.01
  153. # recent_scale_upper_bound = 0.1
  154. # ax1.axvline(x=recent_scale_lower_bound)
  155. # ax1.axvline(x=recent_scale_upper_bound)
  156. else:
  157. # years
  158. plt.set_xlabel("Time (years)", fontsize=fnt_size)
  159. plt.set_ylabel("Individuals (N)", fontsize=fnt_size)
  160. # plt.rcParams['font.size'] = fnt_size
  161. # print(fnt_size, "rcParam font.size=", plt.rcParams['font.size'])
  162. ax1.legend(handles = plot_handles, loc='best', fontsize = fnt_size*0.5)
  163. ax1.set_title(title)
  164. if ax is None:
  165. plt.savefig(title+'_b'+str(breaks)+'.pdf')
  166. # plot likelihood against nb of breakpoints
  167. if ax is None:
  168. fig, ax2 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  169. # plt.rcParams['font.size'] = fnt_size
  170. else:
  171. #plt.rcParams['font.size'] = fnt_size
  172. ax2 = ax[0][0,1]
  173. ax2.plot(full_dict['Ln_Brks'][0], full_dict['Ln_Brks'][1], 'o', linestyle = "dotted", lw=2)
  174. ax2.axhline(y=full_dict['best_Ln'], linestyle = "-.", color = "red", label = "$-\log\mathcal{L}$ = "+str(round(full_dict['best_Ln'], 2)))
  175. ax2.set_yscale('log')
  176. ax2.set_xlabel("# breakpoints", fontsize=fnt_size)
  177. ax2.set_ylabel("$-\log\mathcal{L}$", fontsize=fnt_size)
  178. ax2.legend(loc='best', fontsize = fnt_size*0.5)
  179. ax2.set_title(title+" Likelihood gain from # breakpoints")
  180. if ax is None:
  181. plt.savefig(title+'_Breakpts_Likelihood.pdf')
  182. # AIC
  183. if ax is None:
  184. fig, ax3 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  185. # plt.rcParams['font.size'] = '18'
  186. else:
  187. #plt.rcParams['font.size'] = fnt_size
  188. ax3 = ax[1][0,1]
  189. AIC = full_dict['AIC_Brks']
  190. ax3.plot(AIC[0], AIC[1], 'o', linestyle = "dotted", lw=2)
  191. ax3.axhline(y=full_dict['best_AIC'], linestyle = "-.", color = "red",
  192. label = "Min. AIC = "+str(round(full_dict['best_AIC'], 2)))
  193. ax3.set_yscale('log')
  194. ax3.set_xlabel("# breakpoints", fontsize=fnt_size)
  195. ax3.set_ylabel("AIC")
  196. ax3.legend(loc='best', fontsize = fnt_size*0.5)
  197. ax3.set_title(title+" AIC")
  198. if ax is None:
  199. plt.savefig(title+'_Breakpts_Likelihood_AIC.pdf')
  200. # return plots
  201. return ax[0], ax[1]
  202. def save_all_epochs_thetafolder(folder_path, mu, tgen, title = "Title", theta_scale = True, input = None, output = None):
  203. #scenari = {}
  204. cpt = 0
  205. epochs = {}
  206. plots = {}
  207. # store ['best'], and [0] for epoch 0 etc...
  208. for file_name in os.listdir(folder_path):
  209. breaks = 0
  210. cpt +=1
  211. if os.path.isfile(os.path.join(folder_path, file_name)):
  212. x, y, likelihood, theta, sfs, L = parse_stwp_theta_file(folder_path+file_name, breaks = breaks,
  213. tgen = tgen,
  214. mu = mu, relative_theta_scale = theta_scale)
  215. SFS_stored = sfs
  216. L_stored = L
  217. while not (x == 0 and y == 0):
  218. if breaks not in epochs.keys():
  219. epochs[breaks] = {}
  220. epochs[breaks][likelihood] = x,y
  221. breaks += 1
  222. x,y,likelihood,theta,sfs,L = parse_stwp_theta_file(folder_path+file_name, breaks = breaks,
  223. tgen = tgen,
  224. mu = mu, relative_theta_scale = theta_scale)
  225. if x == 0:
  226. # last break did not work, then breaks = breaks-1
  227. breaks -= 1
  228. print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(breaks)+"\n*******\n")
  229. print(cpt, "theta file(s) have been scanned.")
  230. brkpt_lik = []
  231. top_plots = {}
  232. for epoch, scenari in epochs.items():
  233. # sort starting by the smallest -log(Likelihood)
  234. best10_scenari = (sorted(list(scenari.keys())))[:10]
  235. greatest_likelihood = best10_scenari[0]
  236. # store the tuple breakpoints and likelihood for later plot
  237. brkpt_lik.append((epoch, greatest_likelihood))
  238. x, y = scenari[greatest_likelihood]
  239. #without breakpoint
  240. if epoch == 0:
  241. # do something with the theta without bp and skip the plotting
  242. N0 = y[0]
  243. #continue
  244. for i in range(len(y)):
  245. # divide by N0
  246. y[i] = y[i]/N0
  247. x[i] = x[i]/N0
  248. top_plots[greatest_likelihood] = x,y,epoch
  249. plots_likelihoods = list(top_plots.keys())
  250. for i in range(len(plots_likelihoods)):
  251. plots_likelihoods[i] = float(plots_likelihoods[i])
  252. best10_plots = sorted(plots_likelihoods)[:10]
  253. top_plot_lik = str(best10_plots[0])
  254. # store x,y,brks,likelihood
  255. plots['best'] = (top_plots[top_plot_lik][0], top_plots[top_plot_lik][1], str(top_plots[top_plot_lik][2]), top_plot_lik)
  256. plots['plots'] = []
  257. for k, plot_Lk in enumerate(best10_plots[1:]):
  258. plot_Lk = str(plot_Lk)
  259. plots['plots'].append([top_plots[plot_Lk][0], top_plots[plot_Lk][1], str(top_plots[plot_Lk][2]), plot_Lk])
  260. # plot likelihood against nb of breakpoints
  261. # best possible likelihood from SFS
  262. # Segregating sites
  263. S = sum(SFS_stored)
  264. # Number of kept sites from which the SFS is computed
  265. L = L_stored
  266. # number of monomorphic sites
  267. S0 = L-S
  268. # print("SFS", SFS_stored)
  269. # print("S", S, "L", L, "S0=", S0)
  270. # compute Ln
  271. Ln = log_facto(S+S0) - log_facto(S0) + np.log(float(S0)/(S+S0)) * S0
  272. for xi in range(0, len(SFS_stored)):
  273. p_i = SFS_stored[xi] / float(S+S0)
  274. Ln += np.log(p_i) * SFS_stored[xi] - log_facto(SFS_stored[xi])
  275. # basic plot likelihood
  276. Ln_Brks = [list(np.array(brkpt_lik)[:, 0]), list(np.array(brkpt_lik)[:, 1].astype(float))]
  277. best_Ln = -Ln
  278. AIC = []
  279. for brk in np.array(brkpt_lik)[:, 0]:
  280. brk = int(brk)
  281. AIC.append((2*brk+1)+2*np.array(brkpt_lik)[brk, 1].astype(float))
  282. AIC_Brks = [list(np.array(brkpt_lik)[:, 0]), AIC]
  283. # AIC = 2*k - 2ln(L) ; where k is the number of parameters, here brks+1
  284. AIC_ln = 2*(len(brkpt_lik)+1) - 2*Ln
  285. best_AIC = AIC_ln
  286. selected_brks_nb = AIC.index(min(AIC))
  287. # to return : plots ; Ln_Brks ; AIC_Brks ; best_Ln ; best_AIC
  288. # 'plots' dict keys: 'best', {epochs}('0', '1',...)
  289. if input == None:
  290. saved_plots = {"S":S, "S0":S0, "L":L, "all_epochs":plots, "Ln_Brks":Ln_Brks,
  291. "AIC_Brks":AIC_Brks, "best_Ln":best_Ln,
  292. "best_AIC":best_AIC, "best_epoch_by_AIC":selected_brks_nb}
  293. else:
  294. # if the dict has to be loaded from input
  295. with open(input, 'r') as json_file:
  296. saved_plots = json.load(json_file)
  297. saved_plots["S"] = S
  298. saved_plots["S0"] = S0
  299. saved_plots["L"] = L
  300. saved_plots["all_epochs"] = plots
  301. saved_plots["Ln_Brks"] = Ln_Brks
  302. saved_plots["AIC_Brks"] = AIC_Brks
  303. saved_plots["best_Ln"] = best_Ln
  304. saved_plots["best_AIC"] = best_AIC
  305. saved_plots["best_epoch_by_AIC"] = selected_brks_nb
  306. if output == None:
  307. output = title+"_plotdata.json"
  308. with open(output, 'w') as json_file:
  309. json.dump(saved_plots, json_file)
  310. return saved_plots
  311. def save_k_theta(folder_path, mu, tgen, title = "Title", theta_scale = True,
  312. breaks_max = 10, input = None, output = None):
  313. """
  314. Save theta values as is to do basic plots.
  315. """
  316. cpt = 0
  317. epochs = {}
  318. len_sfs = 0
  319. for file_name in os.listdir(folder_path):
  320. cpt +=1
  321. if os.path.isfile(os.path.join(folder_path, file_name)):
  322. for k in range(breaks_max):
  323. x,y,likelihood,thetas,sfs,L = parse_stwp_theta_file(folder_path+file_name, breaks = k,
  324. tgen = tgen,
  325. mu = mu, relative_theta_scale = theta_scale)
  326. if thetas == 0:
  327. continue
  328. if len(thetas)-1 != k:
  329. continue
  330. if k not in epochs.keys():
  331. epochs[k] = {}
  332. likelihood = str(eval(thetas[k][2]))
  333. epochs[k][likelihood] = thetas
  334. #epochs[k] = thetas
  335. print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(k)+"\n*******\n")
  336. print(cpt, "theta file(s) have been scanned.")
  337. plots = []
  338. best_epochs = {}
  339. for epoch in epochs:
  340. likelihoods = []
  341. for key in epochs[epoch].keys():
  342. likelihoods.append(key)
  343. likelihoods.sort()
  344. minLogLn = str(likelihoods[0])
  345. best_epochs[epoch] = epochs[epoch][minLogLn]
  346. for epoch, theta in best_epochs.items():
  347. groups = np.array(list(theta.values()), dtype=object)[:, 1].tolist()
  348. x = []
  349. y = []
  350. thetas = np.array(list(theta.values()), dtype=object)[:, 0]
  351. for i,group in enumerate(groups):
  352. x += group[::-1]
  353. y += list(np.repeat(thetas[i], len(group)))
  354. if epoch == 0:
  355. N0 = y[0]
  356. # compute the proportion of information used at each bin of the SFS
  357. sum_theta_i = 0
  358. for i in range(2, len(y)+2):
  359. sum_theta_i+=y[i-2] / (i-1)
  360. prop = []
  361. for k in range(2, len(y)+2):
  362. prop.append(y[k-2] / (k - 1) / sum_theta_i)
  363. prop = prop[::-1]
  364. # normalise to N0 (N0 of epoch1)
  365. for i in range(len(y)):
  366. y[i] = y[i]/N0
  367. # x_plot, y_plot = plot_straight_x_y(x, y)
  368. p = x, y
  369. # add plot to the list of all plots to superimpose
  370. plots.append(p)
  371. cumul = 0
  372. prop_cumul = []
  373. for val in prop:
  374. prop_cumul.append(val+cumul)
  375. cumul = val+cumul
  376. prop = prop_cumul
  377. lines_fig2 = []
  378. for epoch, theta in best_epochs.items():
  379. groups = np.array(list(theta.values()), dtype=object)[:, 1].tolist()
  380. x = []
  381. y = []
  382. thetas = np.array(list(theta.values()), dtype=object)[:, 0]
  383. for i,group in enumerate(groups):
  384. x += group[::-1]
  385. y += list(np.repeat(thetas[i], len(group)))
  386. if epoch == 0:
  387. N0 = y[0]
  388. for i in range(len(y)):
  389. y[i] = y[i]/N0
  390. x_2 = []
  391. T = 0
  392. for i in range(len(x)):
  393. x[i] = int(x[i])
  394. # compute the times as: theta_k / (k*(k-1))
  395. for i in range(0, len(x)):
  396. T += y[i] / (x[i]*(x[i]-1))
  397. x_2.append(T)
  398. # Save plotting (fig 2)
  399. x_2 = [0]+x_2
  400. y = [y[0]]+y
  401. # x2_plot, y2_plot = plot_straight_x_y(x_2, y)
  402. p2 = x_2, y
  403. lines_fig2.append(p2)
  404. if input == None:
  405. saved_plots = {"raw_stairs":plots, "scaled_stairs":lines_fig2,
  406. "prop":prop}
  407. else:
  408. # if the dict has to be loaded from input
  409. with open(input, 'r') as json_file:
  410. saved_plots = json.load(json_file)
  411. saved_plots["raw_stairs"] = plots
  412. saved_plots["scaled_stairs"] = lines_fig2
  413. saved_plots["prop"] = prop
  414. if output == None:
  415. output = title+"_plotdata.json"
  416. with open(output, 'w') as json_file:
  417. json.dump(saved_plots, json_file)
  418. return saved_plots
  419. def plot_scaled_theta(plot_lines, prop, title, ax = None, n_ticks = 10, subset = None):
  420. # fig 2 & 3
  421. if ax is None:
  422. my_dpi = 300
  423. fnt_size = 18
  424. fig2, ax2 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  425. fig3, ax3 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  426. else:
  427. # plt.rcParams['font.size'] = fnt_size
  428. fnt_size = 12
  429. # place of plots on the grid
  430. ax2 = ax[1,0]
  431. ax3 = ax[1,1]
  432. lines_fig2 = []
  433. lines_fig3 = []
  434. #plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  435. nb_breaks = len(plot_lines)
  436. for breaks, plot in enumerate(plot_lines):
  437. if subset is not None:
  438. if breaks not in subset :
  439. # skip if not in subset
  440. if max(subset) > nb_breaks and breaks == nb_breaks-1:
  441. pass
  442. else:
  443. continue
  444. x,y=plot
  445. x2_plot, y2_plot = plot_straight_x_y(x,y)
  446. p2, = ax2.plot(x2_plot, y2_plot, 'o', linestyle="-", alpha=0.75, lw=2, label = str(breaks)+' brks')
  447. lines_fig2.append(p2)
  448. # Plotting (fig 3) which is the same but log scale for x
  449. p3, = ax3.plot(x2_plot, y2_plot, 'o', linestyle="-", alpha=0.75, lw=2, label = str(breaks)+' brks')
  450. lines_fig3.append(p3)
  451. ax2.set_xlabel("Relative scale", fontsize=fnt_size)
  452. ax2.set_ylabel("theta", fontsize=fnt_size)
  453. ax2.set_title(title, fontsize=fnt_size)
  454. ax2.legend(handles=lines_fig2, loc='best', fontsize = fnt_size*0.5)
  455. if ax is None:
  456. # nb of plot_lines represent the number of epochs stored (len(plot_lines) = #breaks+1)
  457. plt.savefig(title+'_plot2_'+str(len(plot_lines))+'.pdf')
  458. # close fig2 to save memory
  459. plt.close(fig2)
  460. ax3.set_xscale('log')
  461. ax3.set_yscale('log')
  462. ax3.set_xlabel("log Relative scale", fontsize=fnt_size)
  463. ax3.set_ylabel("theta", fontsize=fnt_size)
  464. ax3.set_title(title, fontsize=fnt_size)
  465. ax3.legend(handles=lines_fig3, loc='best', fontsize = fnt_size*0.5)
  466. if ax is None:
  467. # nb of plot_lines represent the number of epochs stored (len(plot_lines) = #breaks+1)
  468. plt.savefig(title+'_plot3_'+str(len(plot_lines))+'_log.pdf')
  469. # close fig3 to save memory
  470. plt.close(fig3)
  471. return ax
  472. def plot_raw_stairs(plot_lines, prop, title, ax = None, n_ticks = 10):
  473. # multiple fig
  474. if ax is None:
  475. # intialize figure 1
  476. my_dpi = 300
  477. fnt_size = 18
  478. # plt.rcParams['font.size'] = fnt_size
  479. fig, ax1 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  480. else:
  481. fnt_size = 12
  482. # plt.rcParams['font.size'] = fnt_size
  483. ax1 = ax[0, 0]
  484. plt.subplots_adjust(wspace=0.3, hspace=0.3)
  485. plots = []
  486. for epoch, plot in enumerate(plot_lines):
  487. x,y = plot
  488. x_plot, y_plot = plot_straight_x_y(x,y)
  489. p, = ax1.plot(x_plot, y_plot, 'o', linestyle="-", alpha=0.75, lw=2, label = str(epoch)+' brks')
  490. # add plot to the list of all plots to superimpose
  491. plots.append(p)
  492. x_ticks = x
  493. # print(x_ticks)
  494. #print(prop, "\n", sum(prop))
  495. #ax.legend(handles=[p0]+plots)
  496. ax1.set_xlabel("# bin & cumul. prop. of sites", fontsize=fnt_size)
  497. # Set the x-axis locator to reduce the number of ticks to 10
  498. ax1.set_ylabel("theta", fontsize=fnt_size)
  499. ax1.set_title(title, fontsize=fnt_size)
  500. ax1.legend(handles=plots, loc='best', fontsize = fnt_size*0.5)
  501. ax1.set_xticks(x_ticks)
  502. step = len(x_ticks)//(n_ticks-1)
  503. values = x_ticks[::step]
  504. new_prop = []
  505. for val in values:
  506. new_prop.append(prop[int(val)-2])
  507. new_prop = new_prop[::-1]
  508. ax1.set_xticks(values)
  509. ax1.set_xticklabels([f'{values[k]}\n{val:.2f}' for k, val in enumerate(new_prop)], fontsize = fnt_size*0.8)
  510. if ax is None:
  511. # nb of plot_lines represent the number of epochs stored (len(plot_lines) = #breaks+1)
  512. plt.savefig(title+'_raw'+str(len(plot_lines))+'.pdf')
  513. plt.close(fig)
  514. # return plots
  515. return ax
  516. def combined_plot(folder_path, mu, tgen, breaks, title = "Title", theta_scale = True, selected_breaks = []):
  517. my_dpi = 300
  518. save_k_theta(folder_path, mu, tgen, title, theta_scale, breaks_max = breaks, output = title+"_plotdata.json")
  519. save_all_epochs_thetafolder(folder_path, mu, tgen, title, theta_scale, input = title+"_plotdata.json", output = title+"_plotdata.json")
  520. with open(title+"_plotdata.json", 'r') as json_file:
  521. loaded_data = json.load(json_file)
  522. # plot page 1 of summary
  523. fig1, ax1 = plt.subplots(2, 2, figsize=(5000/my_dpi, 2970/my_dpi), dpi=my_dpi)
  524. # fig1.tight_layout()
  525. # Adjust absolute space between the top and bottom rows
  526. fig1.subplots_adjust(hspace=0.35) # Adjust this value based on your requirement
  527. # plot page 2 of summary
  528. fig2, ax2 = plt.subplots(2, 2, figsize=(5000/my_dpi, 2970/my_dpi), dpi=my_dpi)
  529. # fig2.tight_layout()
  530. ax1 = plot_raw_stairs(plot_lines = loaded_data['raw_stairs'],
  531. prop = loaded_data['prop'], title = title, ax = ax1)
  532. ax1 = plot_scaled_theta(plot_lines = loaded_data['scaled_stairs'],
  533. prop = loaded_data['prop'], title = title, ax = ax1, subset=[loaded_data['best_epoch_by_AIC']]+selected_breaks)
  534. ax2 = plot_scaled_theta(plot_lines = loaded_data['scaled_stairs'],
  535. prop = loaded_data['prop'], title = title, ax = ax2)
  536. ax1, ax2 = plot_all_epochs_thetafolder(loaded_data, mu, tgen, title, theta_scale, ax = [ax1, ax2])
  537. fig1.savefig(title+'_combined_p1.pdf')
  538. print("Wrote", title+'_combined_p1.pdf')
  539. fig2.savefig(title+'_combined_p2.pdf')
  540. print("Wrote", title+'_combined_p2.pdf')
  541. plot_raw_stairs(plot_lines = loaded_data['raw_stairs'],
  542. prop = loaded_data['prop'], title = title, ax = None)
  543. plot_scaled_theta(plot_lines = loaded_data['scaled_stairs'],
  544. prop = loaded_data['prop'], title = title, ax = None)
  545. plt.close(fig1)
  546. plt.close(fig2)
  547. if __name__ == "__main__":
  548. if len(sys.argv) != 4:
  549. print("Need 3 args: ThetaFolder MutationRate GenerationTime")
  550. exit(0)
  551. folder_path = sys.argv[1]
  552. mu = sys.argv[2]
  553. tgen = sys.argv[3]
  554. plot_all_epochs_thetafolder(folder_path, mu, tgen)