annotate main.R @ 1:a62b0c62270e draft default tip

"planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 86b0cc490d7a4fe44c9c75f857a0609bd3fa82d3-dirty"
author perssond
date Tue, 06 Apr 2021 21:12:31 +0000
parents 1fb6181c2c64
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
1 #!/usr/bin/env Rscript
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
2
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
3 suppressMessages( library(tidyverse) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
4 library( optparse )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
5 library( naivestates )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
6
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
7 ## Identify directory of the script
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
8 wd <- commandArgs( trailingOnly=FALSE ) %>%
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
9 keep( ~grepl("--file=", .x) ) %>%
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
10 str_replace( "--file=", "" ) %>% dirname()
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
11 cat( "Running the script from", wd, "\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
12
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
13 ## Parse command-line arugments
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
14 option_list <- list(
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
15 make_option(c("-i", "--in"), type="character", help="Input file"),
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
16 make_option(c("-o", "--out"), type="character", default="/data",
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
17 help="Output directory"),
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
18 make_option(c("-m", "--markers"), type="character", default="auto",
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
19 help="Markers to model"),
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
20 make_option(c("-p", "--plots"), type="character", default="off",
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
21 help="Generate plots showing the fit"),
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
22 make_option("--mct", type="character", default="",
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
23 help="Marker -> cell type map in .csv format"),
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
24 make_option("--id", type="character", default="CellID",
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
25 help="Column containing cell IDs"),
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
26 make_option("--log", type="character", default="auto",
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
27 help="Whether to apply a log transform <yes|no|auto>"),
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
28 make_option("--sfx", type="character", default="",
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
29 help="Common suffix on marker columns (e.g., _cellMask)"),
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
30 make_option("--umap", action="store_true", default=FALSE,
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
31 help="Generate UMAP plots")
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
32 )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
33 opt <- parse_args(OptionParser(option_list=option_list))
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
34
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
35 ## Argument verification
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
36 if( !("in" %in% names(opt)) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
37 stop( "Please provide an input file name with -i" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
38 if( !(opt$log %in% c("yes","no","auto")) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
39 stop( "--log must be one of <yes|no|auto>" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
40 if( !(opt$plots %in% c("off", "pdf", "png")) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
41 stop( "--plots must be one of <off|pdf|png>" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
42
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
43 ## Identify the sample name
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
44 sn <- basename( opt$`in` ) %>% str_split( "\\." ) %>%
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
45 pluck( 1, 1 )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
46 cat( "Inferred sample name:", sn, "\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
47
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
48 ## Read the data matrix
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
49 X <- read_csv( opt$`in`, col_types=cols() )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
50 cat( "Read", nrow(X), "entries\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
51
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
52 ## Fix potential capitalization mismatch of --id
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
53 if( !(opt$id %in% colnames(X)) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
54 {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
55 ## Attempt to find a singular case-insensitive match
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
56 i <- grep( tolower(opt$id), tolower(colnames(X)) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
57 if( length(i) == 1 )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
58 {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
59 warning( " No such column ", opt$id,
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
60 "; using ", colnames(X)[i], " instead" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
61 opt$id <- colnames(X)[i]
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
62 }
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
63 else stop( "No such column ", opt$id,
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
64 "; use --id to specify which column contains cell IDs" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
65 }
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
66
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
67 ## Identify markers in the matrix
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
68 mrkv <- findMarkers(setdiff(colnames(X), opt$id), opt$markers,
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
69 opt$sfx, TRUE, TRUE)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
70
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
71 ## Handle log transformation of the data
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
72 if( opt$log == "yes" ||
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
73 (opt$log == "auto" && max(X[mrkv], na.rm=TRUE) > 1000) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
74 {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
75 cat( "Applying a log10 transform\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
76 X <- X %>% mutate_at( unname(mrkv), ~log10(.x+1) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
77 }
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
78
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
79 ## Fit Gaussian mixture models
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
80 GMM <- GMMfit(X, opt$id, !!!mrkv)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
81 fnMdl <- file.path( opt$out, str_c(sn, "-models.csv") )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
82 cat( "Saving models to", fnMdl, "\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
83 GMMmodels(GMM) %>% write_csv( fnMdl )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
84
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
85 ## Reshape the matrix back to cells-by-marker format
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
86 Y <- GMMreshape(GMM)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
87
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
88 cat( "------\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
89
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
90 ## Find the default cell type map
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
91 if( opt$mct != "" ) {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
92
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
93 ## Load marker -> cell type associations
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
94 cat( "Loading cell type map from", opt$mct, "\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
95 mct <- read_csv( opt$mct, col_types=cols() ) %>%
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
96 distinct() %>% filter(Marker %in% colnames(Y))
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
97
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
98 if( nrow(mct) == 0 ) {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
99 warning( "No usable marker -> cell type mappings detected" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
100 Y <- findDominant(Y, opt$id)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
101 } else {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
102 cat( "Using the following marker -> cell type map:\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
103 walk2( mct$Marker, mct$State, ~cat(.x, "->", .y, "\n") )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
104 Y <- callStates(Y, opt$id, mct)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
105 }
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
106 } else {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
107 cat( "No marker -> cell type mapping provided\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
108 Y <- findDominant(Y, opt$id)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
109 }
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
110
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
111 cat( "------\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
112
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
113 ## Identify the output location(s)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
114 fnOut <- file.path( opt$out, str_c(sn, "-states.csv") )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
115 cat( "Saving probabilities and calls to", fnOut, "\n")
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
116 Y %>% write_csv( fnOut )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
117
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
118 ## Generates plots as necessary
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
119 if( opt$plots != "off" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
120 {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
121 ## Create a separate directory for plots
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
122 dirPlot <- file.path( opt$out, "plots", sn )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
123 dir.create(dirPlot, recursive=TRUE, showWarnings=FALSE)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
124
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
125 ## Fit overview
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
126 fn <- file.path( file.path(opt$out, "plots"), str_c(sn, "-allfits.", opt$plots) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
127 ggf <- plotFitOverview(GMM)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
128 suppressMessages(ggsave( fn, ggf, width=12, height=8 ))
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
129
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
130 ## Compute a UMAP projection
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
131 if( opt$umap ) {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
132 cat( "Computing a UMAP projection...\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
133 U <- umap( Y, c(opt$id, "State", "Dominant") )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
134
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
135 ## Generate and write a summary plot
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
136 gg <- plotSummary( U )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
137 fn <- file.path( file.path(opt$out, "plots"), str_c(sn, "-summary.", opt$plots) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
138 suppressMessages(ggsave( fn, gg, width=9, height=7 ))
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
139 cat( "Plotted summary to", fn, "\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
140
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
141 ## Generate and write faceted probabilities plot
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
142 gg <- plotProbs( U, c(opt$id, "State", "Dominant") )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
143 fn <- file.path( file.path(opt$out, "plots"), str_c(sn, "-probs.", opt$plots) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
144 suppressMessages(ggsave( fn, gg, width=9, height=7 ))
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
145 cat( "Plotted probabilities to", fn, "\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
146 }
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
147
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
148 ## Generate and write out plots for individual marker fits
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
149 for( i in names(mrkv) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
150 {
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
151 gg <- plotMarker(GMM, i)
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
152 fn <- file.path( dirPlot, str_c(i,".",opt$plots) )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
153 suppressMessages(ggsave( fn, gg ))
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
154 cat( "Wrote", fn, "\n" )
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
155 }
1fb6181c2c64 "planemo upload for repository https://github.com/ohsu-comp-bio/naivestates commit 392f57d212a7499bf1d3e421112a32a56635bc67-dirty"
perssond
parents:
diff changeset
156 }