File: readImgData.R

package info (click to toggle)
r-bioc-spatialexperiment 1.16.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,648 kB
  • sloc: makefile: 2
file content (81 lines) | stat: -rw-r--r-- 2,715 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#' @rdname readImgData
#' @title Read images & scale factors for 10x Genomics Visium
#'
#' @description
#' Function to read in images and scale factors for 10x Genomics Visium data,
#' and return as a valid \code{\link{imgData}} \code{DataFrame}.
#'
#' @param path a path where to find one or more images
#' @param sample_id the \code{sample_id} for the \code{\link{SpatialExperiment}}
#'   object
#' @param imageSources the images source path(s)
#' @param scaleFactors the .json file where to find the scale factors
#' @param load logical; should the image(s) be loaded into memory
#'   as a \code{grob}? If FALSE, will store the path/URL instead.
#'
#' @return a \code{\link{DataFrame}}
#'
#' @author Helena L. Crowell
#'
#' @examples
#' dir <- system.file(
#'   file.path("extdata", "10xVisium", "section1", "outs", "spatial"),
#'   package = "SpatialExperiment")
#'
#' # base directory contains
#' # - scale factors (scalefactors_json.json)
#' # - one image (tissue_lowres_image.png)
#' list.files(dir)
#'
#' # read in images & scale factors
#' # as valid 'imgData' 'DFrame'
#' readImgData(dir, sample_id = "foo")
#'
#' @importFrom rjson fromJSON
#' @importFrom magick image_read image_info
#' @export

# TODO: change 'as' to 'load = TRUE/FALSE' &
# support images to be supplied as path or URL

readImgData <- function(path=".", sample_id=names(path),
    imageSources=file.path(path, "tissue_lowres_image.png"),
    scaleFactors=file.path(path, "scalefactors_json.json"),
    load=TRUE) {

    # get sample identifiers
    if (is.null(sample_id))
        stop("'sample_id' mustn't be NULL")
    stopifnot(
        is.character(sample_id),
        length(unique(sample_id)) == length(path))
    names(path) <- names(scaleFactors) <- sample_id

    # put images into list with one element per sample
    images <- lapply(path, function(.)
        grep(., imageSources, value=TRUE, fixed=TRUE))

    dfs <- lapply(sample_id, function(sid)
    {
        sfs <- fromJSON(file=scaleFactors[sid])
        dfs <- lapply(images[[sid]], function(img)
        {
            # get image identifier
            img_nm <- gsub("\\..*", "", basename(img))
            iid <- switch(img_nm,
                tissue_lowres_image="lowres",
                tissue_hires_image="hires",
                detected_tissue_image="detected",
                aligned_fiducials="aligned")
            # get scale factor
            sf_nm <- switch(iid,
                lowres="tissue_lowres_scalef",
                "tissue_hires_scalef")
            sf <- sfs[[grep(sf_nm, names(sfs))]]
            # get 'imgData'
            .get_imgData(img, sf, sid, iid, load)
        })
        do.call(rbind, dfs)
    })
    do.call(rbind, dfs)
}