swp2.py 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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 = 500
  125. L = full_dict["L"]
  126. if ax is None:
  127. # intialize figure
  128. #my_dpi = 300
  129. fnt_size = 18
  130. # plt.rcParams['font.size'] = fnt_size
  131. fig, ax1 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  132. else:
  133. fnt_size = 12
  134. # plt.rcParams['font.size'] = fnt_size
  135. ax1 = ax[1][0,0]
  136. ax1.set_yscale('log')
  137. ax1.set_xscale('log')
  138. plot_handles = []
  139. best_plot = full_dict['all_epochs']['best']
  140. p0, = ax1.plot(best_plot[0], best_plot[1], linestyle = "-",
  141. alpha=1, lw=2, label = str(best_plot[2])+' brks | Lik='+best_plot[3])
  142. plot_handles.append(p0)
  143. #ax1.grid(True,which="both", linestyle='--', alpha = 0.3)
  144. for k, plot_Lk in enumerate(full_dict['all_epochs']['plots']):
  145. plot_Lk = str(full_dict['all_epochs']['plots'][k][3])
  146. # plt.rcParams['font.size'] = fnt_size
  147. p, = ax1.plot(full_dict['all_epochs']['plots'][k][0], full_dict['all_epochs']['plots'][k][1], linestyle = "-",
  148. alpha=1/(k+1), lw=1.5, label = str(full_dict['all_epochs']['plots'][k][2])+' brks | Lik='+plot_Lk)
  149. plot_handles.append(p)
  150. if theta_scale:
  151. ax1.set_xlabel("Coal. time", fontsize=fnt_size)
  152. ax1.set_ylabel("Pop. size scaled by N0", fontsize=fnt_size)
  153. # recent_scale_lower_bound = 0.01
  154. # recent_scale_upper_bound = 0.1
  155. # ax1.axvline(x=recent_scale_lower_bound)
  156. # ax1.axvline(x=recent_scale_upper_bound)
  157. else:
  158. # years
  159. if ax is not None:
  160. plt.set_xlabel("Time (years)", fontsize=fnt_size)
  161. plt.set_ylabel("Effective pop. size (Ne)", fontsize=fnt_size)
  162. else:
  163. plt.xlabel("Time (years)", fontsize=fnt_size)
  164. plt.ylabel("Effective pop. size (Ne)", fontsize=fnt_size)
  165. # x_ticks = ax1.get_xticks()
  166. # ax1.set_xticklabels([f'{k:.0e}\n{k/(mu):.0e}\n{k/(mu)*tgen:.0e}' for k in x_ticks], fontsize = fnt_size*0.5)
  167. # ax1.set_xticklabels([f'{k}\n{k/(mu)}\n{k/(mu)*tgen}' for k in x_ticks], fontsize = fnt_size*0.8)
  168. # plt.rcParams['font.size'] = fnt_size
  169. # print(fnt_size, "rcParam font.size=", plt.rcParams['font.size'])
  170. ax1.legend(handles = plot_handles, loc='best', fontsize = fnt_size*0.5)
  171. ax1.set_title(title)
  172. breaks = len(full_dict['all_epochs']['plots'])
  173. if ax is None:
  174. plt.savefig(title+'_'+str(breaks+1)+'_epochs.pdf')
  175. # plot likelihood against nb of breakpoints
  176. if ax is None:
  177. fig, ax2 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  178. # plt.rcParams['font.size'] = fnt_size
  179. else:
  180. #plt.rcParams['font.size'] = fnt_size
  181. ax2 = ax[0][0,1]
  182. # Retrieve the default color cycle from rcParams
  183. default_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
  184. # Create an array of colors from the default color cycle
  185. colors = [default_colors[i % len(default_colors)] for i in range(len(full_dict['Ln_Brks'][0]))]
  186. ax2.plot(full_dict['Ln_Brks'][0], full_dict['Ln_Brks'][1], "--", lw=1, color="black", zorder=1)
  187. ax2.scatter(full_dict['Ln_Brks'][0], full_dict['Ln_Brks'][1], s=50, c=colors, marker='o', zorder=2)
  188. ax2.axhline(y=full_dict['best_Ln'], linestyle = "-.", color = "red", label = "$-\log\mathcal{L}$ = "+str(round(full_dict['best_Ln'], 2)))
  189. ax2.set_yscale('log')
  190. ax2.set_xlabel("# breakpoints", fontsize=fnt_size)
  191. ax2.set_ylabel("$-\log\mathcal{L}$", fontsize=fnt_size)
  192. ax2.legend(loc='best', fontsize = fnt_size*0.5)
  193. ax2.set_title(title+" Likelihood gain from # breakpoints")
  194. if ax is None:
  195. plt.savefig(title+'_Breakpts_Likelihood.pdf')
  196. # AIC
  197. if ax is None:
  198. fig, ax3 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  199. # plt.rcParams['font.size'] = '18'
  200. else:
  201. #plt.rcParams['font.size'] = fnt_size
  202. ax3 = ax[1][0,1]
  203. AIC = full_dict['AIC_Brks']
  204. # ax3.plot(AIC[0], AIC[1], 'o', linestyle = "dotted", lw=2)
  205. ax3.plot(AIC[0], AIC[1], "--", lw=1, color="black", zorder=1)
  206. ax3.scatter(AIC[0], AIC[1], s=50, c=colors, marker='o', zorder=2)
  207. ax3.axhline(y=full_dict['best_AIC'], linestyle = "-.", color = "red",
  208. label = "Min. AIC = "+str(round(full_dict['best_AIC'], 2)))
  209. ax3.set_yscale('log')
  210. ax3.set_xlabel("# breakpoints", fontsize=fnt_size)
  211. ax3.set_ylabel("AIC")
  212. ax3.legend(loc='best', fontsize = fnt_size*0.5)
  213. ax3.set_title(title+" AIC")
  214. if ax is None:
  215. plt.savefig(title+'_Breakpts_Likelihood_AIC.pdf')
  216. else:
  217. # return plots
  218. return ax[0], ax[1]
  219. def save_all_epochs_thetafolder(folder_path, mu, tgen, title = "Title", theta_scale = True, input = None, output = None):
  220. #scenari = {}
  221. cpt = 0
  222. epochs = {}
  223. plots = {}
  224. # store ['best'], and [0] for epoch 0 etc...
  225. for file_name in os.listdir(folder_path):
  226. breaks = 0
  227. cpt +=1
  228. if os.path.isfile(os.path.join(folder_path, file_name)):
  229. x, y, likelihood, theta, sfs, L = parse_stwp_theta_file(folder_path+file_name, breaks = breaks,
  230. tgen = tgen,
  231. mu = mu, relative_theta_scale = theta_scale)
  232. SFS_stored = sfs
  233. L_stored = L
  234. while not (x == 0 and y == 0):
  235. if breaks not in epochs.keys():
  236. epochs[breaks] = {}
  237. epochs[breaks][likelihood] = x,y
  238. breaks += 1
  239. x,y,likelihood,theta,sfs,L = parse_stwp_theta_file(folder_path+file_name, breaks = breaks,
  240. tgen = tgen,
  241. mu = mu, relative_theta_scale = theta_scale)
  242. if x == 0:
  243. # last break did not work, then breaks = breaks-1
  244. breaks -= 1
  245. print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(breaks)+"\n*******\n")
  246. print(cpt, "theta file(s) have been scanned.")
  247. brkpt_lik = []
  248. top_plots = {}
  249. best_scenario_for_epoch = {}
  250. for epoch, scenari in epochs.items():
  251. # sort starting by the smallest -log(Likelihood)
  252. best10_scenari = (sorted(list(scenari.keys())))[:10]
  253. greatest_likelihood = best10_scenari[0]
  254. # store the tuple breakpoints and likelihood for later plot
  255. brkpt_lik.append((epoch, greatest_likelihood))
  256. x, y = scenari[greatest_likelihood]
  257. #without breakpoint
  258. if epoch == 0:
  259. # do something with the theta without bp and skip the plotting
  260. N0 = y[0]
  261. #continue
  262. if theta_scale:
  263. for i in range(len(y)):
  264. # divide by N0
  265. y[i] = y[i]/N0
  266. x[i] = x[i]/N0
  267. top_plots[greatest_likelihood] = x,y,epoch
  268. best_scenario_for_epoch[epoch] = x,y,greatest_likelihood
  269. plots_likelihoods = list(top_plots.keys())
  270. for i in range(len(plots_likelihoods)):
  271. plots_likelihoods[i] = float(plots_likelihoods[i])
  272. best10_plots = sorted(plots_likelihoods)[:10]
  273. top_plot_lik = str(best10_plots[0])
  274. # store x,y,brks,likelihood
  275. plots['best'] = (top_plots[top_plot_lik][0], top_plots[top_plot_lik][1], str(top_plots[top_plot_lik][2]), top_plot_lik)
  276. plots['plots'] = []
  277. for k, epoch in enumerate(best_scenario_for_epoch.keys()):
  278. plot_Lk = str(best_scenario_for_epoch[epoch][2])
  279. x,y = best_scenario_for_epoch[epoch][0], best_scenario_for_epoch[epoch][1]
  280. plots['plots'].append([x, y, str(epoch), plot_Lk])
  281. plots['plots'] = sorted(plots['plots'], key=lambda x: float(x[3]))
  282. plots['plots'] = plots['plots'][1:]
  283. # Previous version. Was this correct????
  284. # for k, plot_Lk in enumerate(best10_plots[1:]):
  285. # plot_Lk = str(plot_Lk)
  286. # plots['plots'].append([top_plots[plot_Lk][0], top_plots[plot_Lk][1], str(top_plots[plot_Lk][2]), plot_Lk])
  287. # plot likelihood against nb of breakpoints
  288. # best possible likelihood from SFS
  289. # Segregating sites
  290. S = sum(SFS_stored)
  291. # Number of kept sites from which the SFS is computed
  292. L = L_stored
  293. # number of monomorphic sites
  294. S0 = L-S
  295. # print("SFS", SFS_stored)
  296. # print("S", S, "L", L, "S0=", S0)
  297. # compute Ln
  298. Ln = log_facto(S+S0) - log_facto(S0) + np.log(float(S0)/(S+S0)) * S0
  299. for xi in range(0, len(SFS_stored)):
  300. p_i = SFS_stored[xi] / float(S+S0)
  301. Ln += np.log(p_i) * SFS_stored[xi] - log_facto(SFS_stored[xi])
  302. # basic plot likelihood
  303. Ln_Brks = [list(np.array(brkpt_lik)[:, 0]), list(np.array(brkpt_lik)[:, 1].astype(float))]
  304. best_Ln = -Ln
  305. AIC = []
  306. for brk in np.array(brkpt_lik)[:, 0]:
  307. brk = int(brk)
  308. AIC.append((2*brk+1)+2*np.array(brkpt_lik)[brk, 1].astype(float))
  309. AIC_Brks = [list(np.array(brkpt_lik)[:, 0]), AIC]
  310. # AIC = 2*k - 2ln(L) ; where k is the number of parameters, here brks+1
  311. AIC_ln = 2*(len(brkpt_lik)+1) - 2*Ln
  312. best_AIC = AIC_ln
  313. selected_brks_nb = AIC.index(min(AIC))
  314. # to return : plots ; Ln_Brks ; AIC_Brks ; best_Ln ; best_AIC
  315. # 'plots' dict keys: 'best', {epochs}('0', '1',...)
  316. if input == None:
  317. saved_plots = {"S":S, "S0":S0, "L":L, "mu":mu, "tgen":tgen,
  318. "all_epochs":plots, "Ln_Brks":Ln_Brks,
  319. "AIC_Brks":AIC_Brks, "best_Ln":best_Ln,
  320. "best_AIC":best_AIC, "best_epoch_by_AIC":selected_brks_nb}
  321. else:
  322. # if the dict has to be loaded from input
  323. with open(input, 'r') as json_file:
  324. saved_plots = json.load(json_file)
  325. saved_plots["S"] = S
  326. saved_plots["S0"] = S0
  327. saved_plots["L"] = L
  328. saved_plots["mu"] = mu
  329. saved_plots["tgen"] = tgen
  330. saved_plots["all_epochs"] = plots
  331. saved_plots["Ln_Brks"] = Ln_Brks
  332. saved_plots["AIC_Brks"] = AIC_Brks
  333. saved_plots["best_Ln"] = best_Ln
  334. saved_plots["best_AIC"] = best_AIC
  335. saved_plots["best_epoch_by_AIC"] = selected_brks_nb
  336. if output == None:
  337. output = title+"_plotdata.json"
  338. with open(output, 'w') as json_file:
  339. json.dump(saved_plots, json_file)
  340. return saved_plots
  341. def save_k_theta(folder_path, mu, tgen, title = "Title", theta_scale = True,
  342. breaks_max = 10, input = None, output = None):
  343. """
  344. Save theta values as is to do basic plots.
  345. """
  346. cpt = 0
  347. epochs = {}
  348. len_sfs = 0
  349. for file_name in os.listdir(folder_path):
  350. cpt +=1
  351. if os.path.isfile(os.path.join(folder_path, file_name)):
  352. for k in range(breaks_max+1):
  353. x,y,likelihood,thetas,sfs,L = parse_stwp_theta_file(folder_path+file_name, breaks = k,
  354. tgen = tgen,
  355. mu = mu, relative_theta_scale = theta_scale)
  356. if thetas == 0:
  357. continue
  358. if len(thetas)-1 != k:
  359. continue
  360. if k not in epochs.keys():
  361. epochs[k] = {}
  362. likelihood = str(eval(thetas[k][2]))
  363. epochs[k][likelihood] = thetas
  364. #epochs[k] = thetas
  365. print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(k)+"\n*******\n")
  366. print(cpt, "theta file(s) have been scanned.")
  367. plots = []
  368. best_epochs = {}
  369. for epoch in epochs:
  370. likelihoods = []
  371. for key in epochs[epoch].keys():
  372. likelihoods.append(key)
  373. likelihoods.sort()
  374. minLogLn = str(likelihoods[0])
  375. best_epochs[epoch] = epochs[epoch][minLogLn]
  376. for epoch, theta in best_epochs.items():
  377. groups = np.array(list(theta.values()), dtype=object)[:, 1].tolist()
  378. x = []
  379. y = []
  380. thetas = np.array(list(theta.values()), dtype=object)[:, 0]
  381. for i,group in enumerate(groups):
  382. x += group[::-1]
  383. y += list(np.repeat(thetas[i], len(group)))
  384. if epoch == 0:
  385. N0 = y[0]
  386. # compute the proportion of information used at each bin of the SFS
  387. sum_theta_i = 0
  388. for i in range(2, len(y)+2):
  389. sum_theta_i+=y[i-2] / (i-1)
  390. prop = []
  391. for k in range(2, len(y)+2):
  392. prop.append(y[k-2] / (k - 1) / sum_theta_i)
  393. prop = prop[::-1]
  394. if theta_scale :
  395. # normalise to N0 (N0 of epoch1)
  396. for i in range(len(y)):
  397. y[i] = y[i]/N0
  398. # x_plot, y_plot = plot_straight_x_y(x, y)
  399. p = x, y
  400. # add plot to the list of all plots to superimpose
  401. plots.append(p)
  402. cumul = 0
  403. prop_cumul = []
  404. for val in prop:
  405. prop_cumul.append(val+cumul)
  406. cumul = val+cumul
  407. prop = prop_cumul
  408. lines_fig2 = []
  409. for epoch, theta in best_epochs.items():
  410. groups = np.array(list(theta.values()), dtype=object)[:, 1].tolist()
  411. x = []
  412. y = []
  413. thetas = np.array(list(theta.values()), dtype=object)[:, 0]
  414. for i,group in enumerate(groups):
  415. x += group[::-1]
  416. y += list(np.repeat(thetas[i], len(group)))
  417. if epoch == 0:
  418. N0 = y[0]
  419. if theta_scale :
  420. for i in range(len(y)):
  421. y[i] = y[i]/N0
  422. x_2 = []
  423. T = 0
  424. for i in range(len(x)):
  425. x[i] = int(x[i])
  426. # compute the times as: theta_k / (k*(k-1))
  427. for i in range(0, len(x)):
  428. T += y[i] / (x[i]*(x[i]-1))
  429. x_2.append(T)
  430. # Save plotting (fig 2)
  431. x_2 = [0]+x_2
  432. y = [y[0]]+y
  433. # x2_plot, y2_plot = plot_straight_x_y(x_2, y)
  434. p2 = x_2, y
  435. lines_fig2.append(p2)
  436. if input == None:
  437. saved_plots = {"raw_stairs":plots, "scaled_stairs":lines_fig2,
  438. "prop":prop}
  439. else:
  440. # if the dict has to be loaded from input
  441. with open(input, 'r') as json_file:
  442. saved_plots = json.load(json_file)
  443. saved_plots["raw_stairs"] = plots
  444. saved_plots["scaled_stairs"] = lines_fig2
  445. saved_plots["prop"] = prop
  446. if output == None:
  447. output = title+"_plotdata.json"
  448. with open(output, 'w') as json_file:
  449. json.dump(saved_plots, json_file)
  450. return saved_plots
  451. def plot_scaled_theta(plot_lines, prop, title, mu, tgen, swp2_lines = None, ax = None, n_ticks = 10, subset = None, theta_scale = False):
  452. recent_limit_years = 500
  453. # recent limit in coal. time
  454. recent_limit = recent_limit_years/tgen*mu
  455. # nb of plot_lines represent the number of epochs stored (len(plot_lines) = #breaks+1)
  456. nb_epochs = len(plot_lines)
  457. # fig 2 & 3
  458. if ax is None:
  459. my_dpi = 500
  460. fnt_size = 18
  461. fig2, ax2 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  462. fig3, ax3 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  463. else:
  464. # plt.rcParams['font.size'] = fnt_size
  465. fnt_size = 12
  466. # place of plots on the grid
  467. ax2 = ax[1,0]
  468. ax3 = ax[1,1]
  469. lines_fig2 = []
  470. lines_fig3 = []
  471. #plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  472. if swp2_lines:
  473. for k in range(len(swp2_lines[0])):
  474. swp2_lines[0][k] = swp2_lines[0][k]/tgen*mu
  475. for k in range(len(swp2_lines[1])):
  476. swp2_lines[1][k] = swp2_lines[1][k]*4*mu
  477. # x2_plot, y2_plot = plot_straight_x_y(swp2_lines[0],swp2_lines[1])
  478. x2_plot, y2_plot = swp2_lines[0], swp2_lines[1]
  479. p2, = ax2.plot(x2_plot, y2_plot, linestyle="-", alpha=0.75, lw=2, label = 'swp2', color="black")
  480. lines_fig2.append(p2)
  481. # Plotting (fig 3) which is the same but log scale for x
  482. p3, = ax3.plot(x2_plot, y2_plot, linestyle="-", alpha=0.75, lw=2, label = 'swp2', color="black")
  483. lines_fig3.append(p3)
  484. min_x = 1
  485. min_y = 1
  486. max_x = 0
  487. max_y = 0
  488. for breaks, plot in enumerate(plot_lines):
  489. x,y=plot
  490. x2_plot, y2_plot = plot_straight_x_y(x,y)
  491. if subset is not None:
  492. if breaks in subset:
  493. masking_alpha = 0.75
  494. autoscale = True
  495. min_x = min(min_x, min(x2_plot))
  496. min_y = min(min_y, min(y2_plot))
  497. max_x = max(max_x, max(x2_plot))
  498. max_y = max(max_y, max(y2_plot))
  499. # skip the base 0 points x_plot[0:3]
  500. t_max_below_limit = 0
  501. t_min_below_limit = 1
  502. recent_change = False
  503. for t in x[1:]:
  504. if t <= recent_limit:
  505. recent_change = True
  506. t_max_below_limit = max(t_max_below_limit, t)
  507. t_min_below_limit = min(t_min_below_limit, t)
  508. Ne_max_below_limit = y[x.index(t_max_below_limit)]
  509. Ne_min_below_limit = y[x.index(t_min_below_limit)]
  510. if recent_change:
  511. print(f"\n{breaks} breaks ; This is below the recent limit of {recent_limit_years} years:\n",
  512. f"t_min (most recent time point under the limit) : {t_min_below_limit/mu*tgen:.1f} t_max (most ancient time point under the limit) : {t_max_below_limit/mu*tgen:.1f}",
  513. f"\nNe_min (effective size at t_min) : {Ne_min_below_limit/(4*mu):.1f} Ne_max (effective size at t_max) : {Ne_max_below_limit/(4*mu):.1f}",
  514. f"\nNe_min/Ne_max = {(Ne_min_below_limit/(4*mu)) / (Ne_max_below_limit/(4*mu)):.1f}",
  515. f"\nEvolution: {((Ne_min_below_limit/(4*mu)) - (Ne_max_below_limit/(4*mu)))/((Ne_max_below_limit/(4*mu)))*100:.1f}%")
  516. else:
  517. print(f"Recent event under {recent_limit_years} years: NA")
  518. # need to compute the last change and when it occured
  519. tmin = x[1]
  520. tmin_plus_1 = x[2]
  521. Ne_min = y[1]
  522. Ne_min_plus_1 = y[2]
  523. print(f"Last was {tmin/mu*tgen:.1f} years ago. And was of {((Ne_min/(4*mu)) - (Ne_min_plus_1/(4*mu)))/(Ne_min_plus_1/(4*mu))*100:.1f}%")
  524. else:
  525. masking_alpha = 0
  526. autoscale = False
  527. ax2.set_autoscale_on(autoscale)
  528. ax3.set_autoscale_on(autoscale)
  529. p2, = ax2.plot(x2_plot, y2_plot, 'o', linestyle="-", alpha=masking_alpha, lw=2, label = str(breaks)+' brks')
  530. # Plotting (fig 3) which is the same but log scale for x
  531. p3, = ax3.plot(x2_plot, y2_plot, 'o', linestyle="-", alpha=masking_alpha, lw=2, label = str(breaks)+' brks')
  532. if subset is not None and breaks in subset:
  533. # store for legend
  534. lines_fig2.append(p2)
  535. lines_fig3.append(p3)
  536. # put the vertical line of the "recent" time limit
  537. ax3.axvline(x=recent_limit, linestyle="--")
  538. if theta_scale:
  539. xlabel = "Theta scaled by N0"
  540. ylabel = "Theta scaled by N0"
  541. else:
  542. xlabel = "time"
  543. ylabel = "Effective pop. size (Ne)"
  544. if ax is None:
  545. # if not ax, then use the plt syntax, not ax...
  546. plt.xlabel(xlabel, fontsize=fnt_size)
  547. plt.ylabel(ylabel, fontsize=fnt_size)
  548. #plt.xlim(left=0)
  549. #xlim_val = plt.gca().get_xlim()
  550. #x_ticks = list(plt.xticks())[0]
  551. plt.xlim(min(min_x,min(swp2_lines[0])), max(max(swp2_lines[0]), max_x))
  552. x_ticks = list(plt.gca().get_xticks())
  553. plt.gca().set_xticks(x_ticks)
  554. # plt.xticks(x_ticks)
  555. # plt.gca().set_xlim(xlim_val)
  556. plt.gca().set_xticklabels([f'{k:.0e}\n{k/(mu):.0e}\n{k/(mu)*tgen:.0e}' for k in x_ticks], fontsize = fnt_size*0.5)
  557. # rescale y to effective pop size
  558. # ylim_val = plt.gca().get_ylim()
  559. plt.ylim(min(min_y,min(swp2_lines[1])), max(max_y+(max_y*0.05), max(swp2_lines[1])+(max(swp2_lines[1])*0.05)))
  560. y_ticks = list(plt.yticks())[0]
  561. plt.gca().set_yticks(y_ticks)
  562. # plt.gca().set_ylim(ylim_val)
  563. plt.yticks(y_ticks)
  564. plt.gca().set_yticklabels([f'{k/(4*mu):.0e}' for k in y_ticks], fontsize = fnt_size*0.5)
  565. plt.title(title, fontsize=fnt_size)
  566. plt.legend(handles=lines_fig2, loc='best', fontsize = fnt_size*0.5)
  567. plt.text(-0.13, -0.135, 'Coal. time\nGen. time\nYears', ha='left', va='bottom', transform=ax3.transAxes)
  568. plt.subplots_adjust(bottom=0.2) # Adjust the value as needed
  569. plt.savefig(title+'_plotB_'+str(nb_epochs)+'_epochs.pdf')
  570. # close fig2 to save memory
  571. plt.close(fig2)
  572. else:
  573. # when ax subplotting is used
  574. ax2.set_xlabel(xlabel, fontsize=fnt_size)
  575. ax2.set_ylabel(ylabel, fontsize=fnt_size)
  576. ax2.set_title(title, fontsize=fnt_size)
  577. ax2.legend(handles=lines_fig2, loc='best', fontsize = fnt_size*0.5)
  578. ax3.set_xlabel(xlabel, fontsize=fnt_size)
  579. ax3.set_ylabel(ylabel, fontsize=fnt_size)
  580. ax3.set_title(title, fontsize=fnt_size)
  581. ax3.legend(handles=lines_fig3, loc='best', fontsize = fnt_size*0.5)
  582. ax3.set_xscale('log')
  583. ax3.set_yscale('log')
  584. # Scale the x-axis
  585. x_ticks = list(ax3.get_xticks())
  586. ax3.set_xticks(x_ticks)
  587. ax3.set_xlim(min(min(x_ticks), min(swp2_lines[0])), max(max_x, max(swp2_lines[0])))
  588. ax3.set_xticklabels([f'{k:.0e}\n{k/(mu):.0e}\n{k/(mu)*tgen:.0e}' for k in x_ticks], fontsize = fnt_size*0.5)
  589. # rescale y to effective pop size
  590. y_ticks = list(ax3.get_yticks())
  591. ax3.set_yticks(y_ticks)
  592. ax3.set_ylim(min(min(y_ticks), min(swp2_lines[1])), max(max_y+(max_y*0.5), max(swp2_lines[1])+(max(swp2_lines[1])*0.5)))
  593. ax3.set_yticklabels([f'{k/(4*mu):.0e}' for k in y_ticks], fontsize = fnt_size*0.5)
  594. plt.text(-0.13, -0.135, 'Coal. time\nGen. time\nYears', ha='left', va='bottom', transform=ax3.transAxes)
  595. plt.subplots_adjust(bottom=0.2) # Adjust the value as needed
  596. if ax is None:
  597. # nb of plot_lines represent the number of epochs stored (len(plot_lines) = #breaks+1)
  598. plt.savefig(title+'_plotC_'+str(nb_epochs)+'_epochs_log.pdf')
  599. # close fig3 to save memory
  600. plt.close(fig3)
  601. return ax
  602. def plot_raw_stairs(plot_lines, prop, title, ax = None, n_ticks = 10, rescale = False, subset = None, max_breaks = None):
  603. if max_breaks:
  604. nb_breaks = max_breaks
  605. else:
  606. nb_breaks = len(plot_lines)+1
  607. # multiple fig
  608. if ax is None:
  609. # intialize figure 1
  610. my_dpi = 500
  611. fnt_size = 18
  612. # plt.rcParams['font.size'] = fnt_size
  613. fig, ax1 = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
  614. plt.subplots_adjust(bottom=0.2) # Adjust the value as needed
  615. else:
  616. fnt_size = 12
  617. # plt.rcParams['font.size'] = fnt_size
  618. ax1 = ax[0, 0]
  619. plt.subplots_adjust(wspace=0.3, hspace=0.3)
  620. plots = []
  621. for breaks, plot in enumerate(plot_lines):
  622. if max_breaks and breaks > max_breaks:
  623. # stop plotting if it exceeds the limit
  624. continue
  625. x,y = plot
  626. x_plot, y_plot = plot_straight_x_y(x,y)
  627. p, = ax1.plot(x_plot, y_plot, 'o', linestyle="-", alpha=0.75, lw=2, label = str(breaks)+' brks')
  628. # add plot to the list of all plots to superimpose
  629. plots.append(p)
  630. x_ticks = x
  631. # print(x_ticks)
  632. #print(prop, "\n", sum(prop))
  633. #ax.legend(handles=[p0]+plots)
  634. ax1.set_xlabel("# bin & cumul. prop. of sites", fontsize=fnt_size)
  635. # Set the x-axis locator to reduce the number of ticks to 10
  636. ax1.set_ylabel(r'$\theta_k$', fontsize=fnt_size, rotation = 90)
  637. ax1.set_title(title, fontsize=fnt_size)
  638. ax1.legend(handles=plots, loc='best', fontsize = fnt_size*0.5)
  639. ax1.set_xticks(x_ticks)
  640. step = len(x_ticks)//(n_ticks-1)
  641. values = x_ticks[::step]
  642. new_prop = []
  643. for val in values:
  644. new_prop.append(prop[int(val)-2])
  645. new_prop = new_prop[::-1]
  646. ax1.set_xticks(values)
  647. ax1.set_xticklabels([f'{values[k]}\n{val:.2f}' for k, val in enumerate(new_prop)], fontsize = fnt_size*0.8)
  648. if ax is None:
  649. # nb of plot_lines represent the number of epochs stored (len(plot_lines) = #breaks+1)
  650. plt.savefig(title+'_raw_'+str(nb_breaks)+'_breaks.pdf')
  651. plt.close(fig)
  652. # return plots
  653. return ax
  654. def combined_plot(folder_path, mu, tgen, breaks, title = "Title", theta_scale = False, selected_breaks = []):
  655. my_dpi = 300
  656. saved_plots_dict = save_all_epochs_thetafolder(folder_path, mu, tgen, title, theta_scale, output = title+"_plotdata.json")
  657. nb_of_epochs = len(saved_plots_dict["all_epochs"]["plots"])
  658. best_epoch = saved_plots_dict["best_epoch_by_AIC"]
  659. print("Best epoch based on AIC =", best_epoch)
  660. save_k_theta(folder_path, mu, tgen, title, theta_scale, breaks_max = nb_of_epochs, input = title+"_plotdata.json", output = title+"_plotdata.json")
  661. with open(title+"_plotdata.json", 'r') as json_file:
  662. loaded_data = json.load(json_file)
  663. # START OF COMBINED PLOT CODE
  664. # # plot page 1 of summary
  665. # fig1, ax1 = plt.subplots(2, 2, figsize=(5000/my_dpi, 2970/my_dpi), dpi=my_dpi)
  666. # # fig1.tight_layout()
  667. # # Adjust absolute space between the top and bottom rows
  668. # fig1.subplots_adjust(hspace=0.35) # Adjust this value based on your requirement
  669. # # plot page 2 of summary
  670. # fig2, ax2 = plt.subplots(2, 2, figsize=(5000/my_dpi, 2970/my_dpi), dpi=my_dpi)
  671. # # fig2.tight_layout()
  672. # ax1 = plot_raw_stairs(plot_lines = loaded_data['raw_stairs'],
  673. # prop = loaded_data['prop'], title = title, ax = ax1)
  674. # ax1 = plot_scaled_theta(plot_lines = loaded_data['scaled_stairs'],
  675. # prop = loaded_data['prop'], title = title, ax = ax1, subset=[loaded_data['best_epoch_by_AIC']]+selected_breaks)
  676. # ax2 = plot_scaled_theta(plot_lines = loaded_data['scaled_stairs'],
  677. # prop = loaded_data['prop'], title = title, ax = ax2)
  678. # ax1, ax2 = plot_all_epochs_thetafolder(loaded_data, mu, tgen, title, theta_scale, ax = [ax1, ax2])
  679. # fig1.savefig(title+'_combined_p1.pdf')
  680. # print("Wrote", title+'_combined_p1.pdf')
  681. # fig2.savefig(title+'_combined_p2.pdf')
  682. # print("Wrote", title+'_combined_p2.pdf')
  683. # END OF COMBINED PLOT CODE
  684. # Start of Parsing real swp2 output
  685. folder_splitted = folder_path.split("/")
  686. swp2_summary = "/".join(folder_splitted[:-2])+'/'+folder_splitted[-3]+".final.summary"
  687. swp2_vals = parse_stairwayplot_output_summary(stwplt_out = swp2_summary)
  688. swp2_x, swp2_y = swp2_vals[0], swp2_vals[1]
  689. remove_back_and_forth_points(swp2_x, swp2_y)
  690. # End of Parsing real swp2 output
  691. plot_raw_stairs(plot_lines = loaded_data['raw_stairs'],
  692. prop = loaded_data['prop'], title = title, ax = None, max_breaks = breaks)
  693. plot_scaled_theta(plot_lines = loaded_data['scaled_stairs'], mu = mu, tgen = tgen, subset=[loaded_data['best_epoch_by_AIC']]+selected_breaks,
  694. # plot_scaled_theta(plot_lines = loaded_data['scaled_stairs'], subset=list(range(0,3))+[loaded_data['best_epoch_by_AIC']]+selected_breaks,
  695. prop = loaded_data['prop'], title = title, swp2_lines = [swp2_x, swp2_y], ax = None)
  696. plot_all_epochs_thetafolder(loaded_data, mu, tgen, title, theta_scale, ax = None)
  697. # plt.close(fig1)
  698. # plt.close(fig2)
  699. def remove_back_and_forth_points(x_values, y_values):
  700. # to deal with some weirdness of plotting that occur sometimes with the swp2 output
  701. # sometimes the line is going back and forth as x_k > x_(k+1), which is normally not possible
  702. i = 0
  703. while i < len(x_values) - 1:
  704. if x_values[i] >= x_values[i+1]:
  705. del x_values[i]
  706. del y_values[i]
  707. else:
  708. i += 1
  709. def parse_stairwayplot_output_summary(stwplt_out, xlim = None, ylim = None, title = "default title", plot = False):
  710. #col 5
  711. year = []
  712. # col 6
  713. ne_median = []
  714. ne_2_5 = []
  715. ne_97_5 = []
  716. ne_12_5 = []
  717. # col 10
  718. ne_87_5 = []
  719. with open(stwplt_out, "r") as stwplt_stream:
  720. for line in stwplt_stream:
  721. ## Line format
  722. # mutation_per_site n_estimation theta_per_site_median theta_per_site_2.5% theta_per_site_97.5% year Ne_median Ne_2.5% Ne_97.5% Ne_12.5% Ne_87.5%
  723. if not line.startswith("mutation_per_site"):
  724. #not header
  725. values = line.strip().split()
  726. year.append(float(values[5]))
  727. ne_median.append(float(values[6]))
  728. ne_2_5.append(float(values[7]))
  729. ne_97_5.append(float(values[8]))
  730. ne_12_5.append(float(values[9]))
  731. ne_87_5.append(float(values[10]))
  732. vals = [year, ne_median, ne_2_5, ne_97_5, ne_12_5, ne_87_5]
  733. if plot :
  734. # plot parsed data
  735. label = ["Ne median", "Ne 2.5%", "Ne 97.5%", "Ne 12.5%", "Ne 87.5%"]
  736. for i in range(1, 5):
  737. fig, = plt.plot(year, vals[i], '--', alpha = 0.4)
  738. fig.set_label(label[i])
  739. # # last plot is median
  740. fig, = plt.plot(year, ne_median, 'r-', lw=2)
  741. fig.set_label(label[0])
  742. plt.legend()
  743. plt.ylabel("Individuals (Ne)")
  744. plt.xlabel("Time (years)")
  745. if xlim:
  746. plt.xlim(xlim)
  747. if ylim:
  748. plt.ylim(ylim)
  749. plt.title(title)
  750. plt.show()
  751. plt.close()
  752. return vals
  753. if __name__ == "__main__":
  754. if len(sys.argv) != 4:
  755. print("Need 3 args: ThetaFolder MutationRate GenerationTime")
  756. exit(0)
  757. folder_path = sys.argv[1]
  758. mu = sys.argv[2]
  759. tgen = sys.argv[3]
  760. plot_all_epochs_thetafolder(folder_path, mu, tgen)