|
| 1 | +#!/usr/bin/env Rscript |
| 2 | + |
| 3 | +# This script is used to create a JSON file of annotations for a single library |
| 4 | +# JSON file will include barcodes, annotation column, ontology column (if provided), |
| 5 | +# openscpca-nf version, data release data, and module name |
| 6 | + |
| 7 | +library(optparse) |
| 8 | + |
| 9 | +option_list <- list( |
| 10 | + make_option( |
| 11 | + opt_str = c("--annotations_tsv_file"), |
| 12 | + type = "character", |
| 13 | + help = "Path to TSV file with cell type annotations" |
| 14 | + ), |
| 15 | + make_option( |
| 16 | + opt_str = c("--annotation_column"), |
| 17 | + type = "character", |
| 18 | + help = "Name of the column containing the cell type annotations to use for openscpca_celltype_annotation" |
| 19 | + ), |
| 20 | + make_option( |
| 21 | + opt_str = c("--ontology_column"), |
| 22 | + default = "", |
| 23 | + type = "character", |
| 24 | + help = "Name of the column containing the cell type ontology IDs to use for openscpca_celltype_ontology" |
| 25 | + ), |
| 26 | + make_option( |
| 27 | + opt_str = c("--module_name"), |
| 28 | + type = "character", |
| 29 | + help = "Name of original module in OpenScPCA-analysis" |
| 30 | + ), |
| 31 | + make_option( |
| 32 | + opt_str = c("--release_date"), |
| 33 | + type = "character", |
| 34 | + help = "Release date of data used when generating annotations" |
| 35 | + ), |
| 36 | + make_option( |
| 37 | + opt_str = c("--openscpca_nf_version"), |
| 38 | + type = "character", |
| 39 | + help = "Version of OpenScPCA-nf workflow" |
| 40 | + ), |
| 41 | + make_option( |
| 42 | + opt_str = "--output_json_file", |
| 43 | + type = "character", |
| 44 | + help = "Path to JSON file to save cell type annotations" |
| 45 | + ) |
| 46 | +) |
| 47 | + |
| 48 | +# Parse options |
| 49 | +opt <- parse_args(OptionParser(option_list = option_list)) |
| 50 | + |
| 51 | +# Set up ----------------------------------------------------------------------- |
| 52 | + |
| 53 | +# make sure input/output exist |
| 54 | +stopifnot( |
| 55 | + "annotations TSV file does not exist" = file.exists(opt$annotations_tsv_file), |
| 56 | + "annotation column must be provided" = !is.null(opt$annotation_column), |
| 57 | + "module name must be provided" = !is.null(opt$module_name), |
| 58 | + "release date must be provided" = !is.null(opt$release_date), |
| 59 | + "openscpca-nf version must be provided" = !is.null(opt$openscpca_nf_version), |
| 60 | + "output json file must end in .json" = stringr::str_ends(opt$output_json_file, "\\.json") |
| 61 | +) |
| 62 | + |
| 63 | +# read in annotations |
| 64 | +annotations_df <- readr::read_tsv(opt$annotations_tsv_file) |
| 65 | + |
| 66 | +# check that barcodes and annotation column exist |
| 67 | +stopifnot( |
| 68 | + "barcodes column must be present in provided TSV file" = "barcodes" %in% colnames(annotations_df), |
| 69 | + "annotation column is not present in provided TSV file" = opt$annotation_column %in% colnames(annotations_df) |
| 70 | +) |
| 71 | + |
| 72 | +# check for ontology ids if provided |
| 73 | +if (!is.null(opt$ontology_column)) { |
| 74 | + stopifnot( |
| 75 | + "ontology column is not present in provided TSV file" = opt$ontology_column %in% colnames(annotations_df) |
| 76 | + ) |
| 77 | + ontology_ids <- annotations_df[[opt$ontology_column]] |
| 78 | +} else { |
| 79 | + ontology_ids <- NA |
| 80 | +} |
| 81 | + |
| 82 | +# build json contents |
| 83 | +json_contents <- list( |
| 84 | + module_name = opt$module_name, |
| 85 | + openscpca_nf_version = opt$openscpca_nf_version, |
| 86 | + release_date = opt$release_date, |
| 87 | + barcodes = annotations_df$barcodes, |
| 88 | + openscpca_celltype_annotation = annotations_df[[opt$annotation_column]], |
| 89 | + openscpca_celltype_ontology = ontology_ids |
| 90 | +) |
| 91 | + |
| 92 | +# export json file |
| 93 | +jsonlite::write_json( |
| 94 | + json_contents, |
| 95 | + path = opt$output_json_file, |
| 96 | + simplifyVector = TRUE, |
| 97 | + auto_unbox = TRUE, |
| 98 | + pretty = TRUE |
| 99 | +) |
0 commit comments