|
| 1 | +# plot PSMC plots with bootstraps, using output from psmc_plot.pl |
| 2 | +#Created by P. Morin. Modified by K. Hernandez May 2025. |
| 3 | + |
| 4 | +# step 1 (create the .txt files from .psmc files using psmc_plot.pl) - run in terminal |
| 5 | +# step 2 (input .txt files from step 1 to create final plot) - run in R |
| 6 | + |
| 7 | +### Set working directory and get list of all input files including bootstrap replicates (these are the output text files generated from the utils/psmc_plot.pl -R command) |
| 8 | +# setwd("main_with_boot/plot_mu1.4e-08_g10") |
| 9 | +allfiles=list.files(pattern="txt") |
| 10 | + |
| 11 | +# get species names based on species name patterns from the filenames |
| 12 | +pop1<-"Pcra_z0018462" |
| 13 | +pop2<-"Pcra_z0045928" |
| 14 | +pop3<-"hPSMC" |
| 15 | +#sp4<-"" |
| 16 | +#sp5<-"" |
| 17 | +#sp6<-"" |
| 18 | +#sp7<-"" |
| 19 | +#sp8<-"" |
| 20 | +#sp9<-"" |
| 21 | + |
| 22 | +leg1<-c("ETP","MHI", "hPSMC") # for legend on plot |
| 23 | + |
| 24 | + |
| 25 | +# Save as PNG using png() and dev.off() |
| 26 | +png(paste0("Pseudorca","_psmc_plot.png"),width = 1500, height = 900, units = "px", res = 300) |
| 27 | +#converted the 5x3 original in inches based on 300 dpi recommended by JOH |
| 28 | +### Set min and max values for plot axes |
| 29 | +xmin=1.2e4 |
| 30 | +xmax=1e7 |
| 31 | +ymin=0 |
| 32 | +ymax=18 #original was 15 for Berardius |
| 33 | + |
| 34 | +### Set line colors (first, pick rgb values for each sample, then set main and transparent colors for plot lines) |
| 35 | +# color1 (aquamarine) |
| 36 | +c1=c(127,255,212)/255 |
| 37 | + |
| 38 | +# color (purple) |
| 39 | +c2=c(102, 102, 255)/255 |
| 40 | +# color (orange) |
| 41 | +c3=c(240, 105, 20)/255 |
| 42 | + |
| 43 | +# Main colors (no transparency) |
| 44 | +mycols1=c( |
| 45 | + rgb(c1[1], c1[2], c1[3], alpha=1), |
| 46 | + rgb(c2[1], c2[2], c2[3], alpha=1), |
| 47 | + rgb(c3[1], c3[2], c3[3], alpha=1) |
| 48 | +) |
| 49 | + |
| 50 | +# Bootstrap replicate colors (with transparency) |
| 51 | +transp=0.05 # 0.05; 0 if no bootstraps needed. |
| 52 | +mycols2=c( |
| 53 | + rgb(c1[1], c1[2], c1[3], alpha=transp), |
| 54 | + rgb(c2[1], c2[2], c2[3], alpha=transp), |
| 55 | + rgb(c3[1], c3[2], c3[3], alpha=0) |
| 56 | +) |
| 57 | + |
| 58 | +### Generate an empty plot with labeled axes |
| 59 | +par(mar=c(3.5,3.75,0.5,0.5)) |
| 60 | +op <- par(cex = 0.75) # font size |
| 61 | + |
| 62 | +plot(1, 1, type="n", log="x", axes=F, xlim=c(xmin, xmax), ylim=c(ymin, ymax), xlab="", ylab="") |
| 63 | + |
| 64 | +title(xlab="Years before present", line=2) |
| 65 | +title(ylab=expression("Effective population size (x10"^4*")"), line=2.25) |
| 66 | + |
| 67 | +axis(side=2, line=0, labels=F) |
| 68 | +axis(side=2, line=-.25, labels=T, tick=F) |
| 69 | + |
| 70 | +at.x=outer(1:9, 10^(3:8)) |
| 71 | +lab.x=NULL |
| 72 | +for (i in 1:length(at.x)){ |
| 73 | + p=log10(at.x[i]) |
| 74 | + if (p %% 1 == 0) {lab.x[i]=as.expression(bquote(10^ .(p)))} |
| 75 | + else {lab.x[i]=""} |
| 76 | +} |
| 77 | +axis(1, at=at.x, labels=lab.x, las=1) |
| 78 | + |
| 79 | +legend("topright", lwd=3, col=mycols1[c(1,2,3,4,5,6,7,8,9)], legend=leg1, |
| 80 | + bty="n") |
| 81 | + |
| 82 | +box() |
| 83 | + |
| 84 | +### Function to add plot lines for each sample |
| 85 | +psmc_plot_fill=function(){ |
| 86 | + # Get list of input files using "samplename" as the search term |
| 87 | + dfiles=allfiles[grep(pattern=samplename, allfiles)] |
| 88 | + # Loop through the bootstrap reps (first bootstrap file = the second file from the list above) |
| 89 | + for (i in 2:length(dfiles)){ |
| 90 | + bb=read.table(dfiles[i]) |
| 91 | + # Plot lines for each bootstrap file using the transparency colors |
| 92 | + lines(bb$V1, bb$V2, type="s", col=mycols2[nn], lwd=1) |
| 93 | + } |
| 94 | + # Read in the first file from the list (this one is for adding the main solid line |
| 95 | + # on top of the bootstrap lines, given as boot.out.0.txt from the psmc_plot.pl script) |
| 96 | + aa=read.table(dfiles[1]) |
| 97 | + # Plot the line using the main color (no transparency) |
| 98 | + lines(aa$V1, aa$V2, type="s", col=mycols1[nn], lwd=2) |
| 99 | +} |
| 100 | + |
| 101 | +### Plot each sample (nn is the numerical index for the sample - only needs to |
| 102 | +# correspond to the order of samples in the color lists above) |
| 103 | +# comment out "psmc_plot_fill()" for unused samplenames. |
| 104 | + |
| 105 | +# samples to plot |
| 106 | +# 1 |
| 107 | +samplename=pop1 |
| 108 | +nn=1 |
| 109 | +psmc_plot_fill() |
| 110 | +# 2 |
| 111 | +samplename=pop2 |
| 112 | +nn=2 |
| 113 | +psmc_plot_fill() |
| 114 | +# 3 #can't use PSMC function b/c no bootstrap replicates |
| 115 | +samplename=pop3 |
| 116 | +nn=3 |
| 117 | +lines(hPSMC_z0018462_z0045928_9.10E10_msy_t15_psmc.out.0$V1, |
| 118 | + hPSMC_z0018462_z0045928_9.10E10_msy_t15_psmc.out.0$V2, type="s", col=mycols1[nn], lwd=2) |
| 119 | + |
| 120 | +# Close the PNG device |
| 121 | +dev.off() |
| 122 | + |
0 commit comments