args <- commandArgs(trailingOnly=TRUE) root <- if (length(args)) args[[1]] else "build/figures" script_arg <- grep("^--file=", commandArgs(), value=TRUE) script_dir <- dirname(normalizePath(sub("^--file=", "", script_arg[[1]]))) input <- if (length(args) >= 2) args[[2]] else "data" dir.create(root, recursive=TRUE, showWarnings=FALSE) png_device <- if (.Platform$OS.type == "windows") "windows" else "cairo" read <- function(name) read.csv(file.path(input, name), stringsAsFactors=FALSE) t <- read("tissue.csv") te <- subset(read("tissue_effects.csv"), feature=="CIP2A") g <- read("chronos.csv") r <- subset(read("rnai.csv"), is.finite(dependency)) p <- read("pairs.csv") p <- p[tolower(p$eligible_25)=="true",] s <- read("cip2a_stability_grid.csv") arms <- read("gse281740_cnv_arms.csv") chan <- read("chan_confirmed_primary.csv") ext <- subset(read("gse281740_cnv_effects.csv"), stratum=="all") stopifnot(nrow(t)==122,nrow(g)==70,nrow(r)==11,nrow(subset(p,stratum=="all"))==4,nrow(s)==120) pal <- c("#16736D","#A03951") accent <- "#453C78" emit <- function(name, panel, n=3) { for (fmt in c("pdf","png")) { file <- file.path(root,paste0(name,".",fmt)) if (fmt=="pdf") pdf(file,width=3.35*n,height=3.25,useDingbats=FALSE) else png(file,width=3.35*n,height=3.25,units="in",res=300,type=png_device) par(mfrow=c(1,floor(n)),mar=c(4.5,4.5,2.1,1.2),mgp=c(2.7,.75,0),las=1, bty="l",cex=.83,font.main=2,col.axis="#222222") set.seed(45) panel() dev.off() } } ttl <- function(x) title(main=x,adj=0,line=.6,cex.main=1) strip <- function(values, labs, y, zero=NULL) { plot(NA,xlim=c(.55,2.45),ylim=extendrange(unlist(values),f=.08),xaxt="n",xlab="",ylab=y) axis(1,1:2,labs) if (!is.null(zero)) abline(h=zero,lty=2,col="grey55") for (i in 1:2) { v <- values[[i]] points(i+runif(length(v),-.15,.15),v,pch=19,cex=.75,col=adjustcolor(pal[i],.7)) segments(i-.22,median(v),i+.22,median(v),lwd=2) } } emit("FigureS6",function(){ for(state in c("all","low_cycle")){ z <- s[s$stratum==state,] z$setting <- match(z$seed,unique(s$seed))+5*(match(z$quantile,c(.95,.975,.99))-1)+15*(z$min_arms-2) z$eligible <- z$tumour_cells>=25 & z$epithelial_cells>=25 cap <- if(state=="all") 45 else 1.5 ramp <- colorRampPalette(c("#356999","#FFFFFF","#A03951"))(101) col <- ramp[1+round(50*(1+pmax(-1,pmin(1,z$delta_cpm/cap))))] col[!z$eligible] <- "#C6CDD3" y <- match(z$sample,c("ru1449a","ru1334")) stopifnot(nrow(z)==60,!anyDuplicated(data.frame(y,z$setting))) plot(NA,xlim=c(.5,30.5),ylim=c(.15,2.65),xaxt="n",yaxt="n",xlab="",ylab="",bty="n") rect(z$setting-.5,y-.26,z$setting+.5,y+.26,col=col,border="white") axis(2,1:2,c("RU1449A","RU1334"),las=2,cex.axis=.72) text(seq(3,28,5),2.39,rep(c("95%","97.5%","99%"),2),cex=.7) text(c(8,23),.57,c("2 arms","3 arms"),cex=.78) abline(v=15.5,col="grey60",lty=2) k <- seq(2,20,length.out=101) rect(k,.28,k+.19,.41,col=ramp,border=NA) text(c(2,11,20),.14,c(-cap,0,cap),cex=.65,xpd=NA) rect(23,.28,24.4,.41,col="#C6CDD3",border=NA) text(27,.34,"Excluded",cex=.64) mtext("Tumor minus epithelial (CPM)",side=1,line=2.15,cex=.76) ttl(if(state=="all") "A All cells" else "B Low-cycle marker stratum") } z <- subset(s,tumour_cells>=25 & epithelial_cells>=25 & matched_cells>=25) vals <- split(z$matched_positive,z$stratum) plot(NA,xlim=c(.55,2.45),ylim=c(0,1.05),xaxt="n",xlab="",ylab="Fraction of matching draws > 0") axis(1,1:2,c("All cells","Low-cycle markers")) for(i in 1:2) for(j in 1:2){ q <- z[z$stratum==names(vals)[i]&z$sample==c("ru1334","ru1449a")[j],"matched_positive"] points(i+(j-1.5)*.25+runif(length(q),-.08,.08),q,pch=19,col=adjustcolor(pal[j],.68),cex=.75) } legend("bottomleft",c("RU1334","RU1449A"),pch=19,col=pal,bty="n",cex=.7) ttl("C Repeated depth matching") }) emit("FigureS1",function(){ for(id in c("ru1334","ru1449a")){ z <- subset(arms,sample==id) x <- xtabs(median_tumour~arm+reference,z) plot(x[,1],x[,2],pch=19,col=pal[1],xlab="Immune reference arm score",ylab="Stromal reference arm score") abline(h=0,v=0,col="grey60",lty=2) ttl(paste(if(id=="ru1334") "A" else "B",toupper(id))) } },n=2) emit("FigureS2",function(){ classes <- c("malignant","nonmalignant_epithelial","immune","Fibroblast","Endothelial","other") cols <- c(pal[2],pal[1],"#527CB0","#C18A49","#745D91","#9A9A9A") make <- function(variable){ z <- xtabs(reformulate(c("group","patient"),response=variable),chan)[classes,] sweep(z,2,colSums(z),"/")*100 } for (v in c("cells","CIP2A")){ barplot(make(v),col=cols,border=NA,ylim=c(0,100),names.arg=sub("HTA8_","",sort(unique(chan$patient))), ylab=if(v=="cells") "Fraction of cells (%)" else "Fraction of CIP2A UMI (%)") ttl(if(v=="cells") "A Sample composition" else "B Transcript contribution") } par(mar=rep(.5,4)); plot.new() legend("center",c("Tumor group","Nonmalignant epithelium","Immune","Fibroblast","Endothelial","Other"), fill=cols,border=NA,bty="n") }) emit("FigureS3",function(){ par(mar=c(.4,.6,1.2,.6)) flow <- function(lines){ plot.new(); plot.window(xlim=c(0,1),ylim=c(0,1)) y <- seq(.89,.11,length.out=length(lines)) h <- .082 for(i in seq_along(lines)){ if(i>1) arrows(.5,y[i-1]-h,.5,y[i]+h,length=.08,col="grey45") rect(.05,y[i]-h,.95,y[i]+h,col=if(i==length(lines)) "#DDEDEB" else "#F1F3F5",border="#8593A0") text(.5,y[i],lines[i],cex=.92) } } flow(c("Chan atlas\n9 patients with lung SCLC specimens", "4 patients with corroborating\nprimary lung labels", "Published cell annotations\nAt least 25 cells per group", "2 eligible patient pairs\n2 had only 4 or 5 epithelial cells")) flow(c("GSE281740\n19 specimens, 5 labeled primary lung", "3 overlapping patients removed\n2 independent candidates retained", "Marker and chromosome-arm assignment\nAt least 25 cells per group", "2 eligible patient pairs\nComputational cell labels")) },n=2) emit("FigureS4",function(){ raw <- subset(read("gse281740_cnv_effects.csv"),stratum=="low_cycle_depth_matched") stopifnot(nrow(raw)==4) for(id in c("ru1334","ru1449a")){ z <- raw[raw$sample==id,] z <- z[match(c("CNV_low_epithelial","CNV_supported_tumour"),z$group),] plot(NA,xlim=c(.65,2.35),ylim=c(-.18,2.45),xaxt="n",yaxt="n",xlab="",ylab="Observed CIP2A UMI") axis(1,1:2,c("Lung epithelium","Tumor group")) axis(2,0:2) segments(1:2,0,1:2,z$cip2a,col=pal,lwd=2) points(1:2,z$cip2a,pch=19,col=pal,cex=1.3) text(1:2,z$cip2a,labels=z$cip2a,pos=3) mtext(paste(z$cells,"cells per group")[1],side=3,line=-1.2,adj=.5,cex=.72) mtext(paste("Total UMI:",paste(format(z$umi,big.mark=",",trim=TRUE),collapse=" / ")),side=1,line=2.6,cex=.72) ttl(paste(if(id=="ru1334") "A" else "B",toupper(id))) } },n=2) emit("FigureS5",function(){ par(mar=c(1.2,7.3,2.1,2.3)) z <- r[order(r$dependency),] vals <- as.matrix(z[c("chronos","dependency")]) ranges <- apply(vals,2,range) ramp <- colorRampPalette(c("#6C2469","#F1D6E7","#FAF8FA"))(101) plot(NA,xlim=c(.5,2.5),ylim=c(.3,12.3),xaxt="n",yaxt="n",xlab="",ylab="",bty="n") for(k in 1:2){ y <- rev(seq_len(nrow(z)))+1 pos <- (vals[,k]-ranges[1,k])/diff(ranges[,k]) rect(k-.39,y-.44,k+.39,y+.44,col=ramp[1+round(pos*100)],border="white") text(k,y,sprintf("%.3f",vals[,k]),col=ifelse(pos<.26,"white","#262332"),cex=.84) text(k,.88,sprintf("%.2f to %.2f",ranges[1,k],ranges[2,k]),cex=.64) } text(c(1,2),12.76,c("CRISPR Chronos","RNAi DEMETER2"),xpd=NA,font=2,cex=.87) text(.56,y,z$model_name,adj=1,xpd=NA,cex=.78, font=ifelse(z$model_name %in% c("NCI-H211","NCI-H1836","DMS 53"),2,1)) mtext("Darker cells indicate more negative effects within each platform",side=1,line=.05,cex=.7) },n=1.75) emit("FigureS7",function(){ hi <- r$model_name %in% c("NCI-H211","DMS 53","NCI-H1836") plot(r$chronos,r$dependency,pch=19,col=ifelse(hi,pal[2],"grey55"), xlim=c(-.9,.2),ylim=c(-1,.16),xlab="CRISPR Chronos",ylab="RNAi DEMETER2") text(r$chronos[hi],r$dependency[hi],r$model_name[hi],pos=3,cex=.69) ttl("Matched screen effects") },n=1.65) source(file.path(script_dir,"plate_layouts.R"),encoding="UTF-8")