Skip to content

Commit

Permalink
Merge pull request #7 from DongzeHE/master
Browse files Browse the repository at this point in the history
update loadFry function
  • Loading branch information
mikelove authored Jul 23, 2021
2 parents 30691a1 + f0d9445 commit 19124e4
Showing 1 changed file with 54 additions and 48 deletions.
102 changes: 54 additions & 48 deletions R/loadFry.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,57 +44,63 @@
#'

loadFry <- function(fry.dir, which_counts = c('S', 'A'), verbose = FALSE) {

# read in metadata
meta_info <- fromJSON(file.path(fry.dir, "meta_info.json"))
ng <- meta_info$num_genes
usa_mode <- meta_info$usa_mode

if (usa_mode) {
if (length(which_counts) == 0) {
stop("Please at least provide one status in 'U' 'S' 'A' ")
# in alevin-fry 0.4.1, meta_info.json is changed to quant.json, so check both
# read in metadata
qfile <- file.path(fry.dir, "quant.json")
if (!file.exists(qfile)) {
qfile <- file.path(fry.dir, "meta_info.json")
}
if (verbose) {
message("processing input in USA mode, will return ", paste(which_counts, collapse = '+'))

# read in metadata
meta_info <- fromJSON(file = qfile)
ng <- meta_info$num_genes
usa_mode <- meta_info$usa_mode

if (usa_mode) {
if (length(which_counts) == 0) {
stop("Please at least provide one status in 'U' 'S' 'A' ")
}
if (verbose) {
message("processing input in USA mode, will return ", paste(which_counts, collapse = '+'))
}
} else if (verbose) {
message("processing input in standard mode, will return spliced count")
}
} else if (verbose) {
message("processing input in standard mode, will return spliced count")
}

# read in count matrix
af_raw <- readMM(file = file.path(fry.dir, "alevin", "quants_mat.mtx"))
# if usa mode, each gene gets 3 rows, so the actual number of genes is ng/3
if (usa_mode) {
if (ng %% 3 != 0) {
stop("The number of quantified targets is not a multiple of 3")

# read in count matrix
af_raw <- readMM(file = file.path(fry.dir, "alevin", "quants_mat.mtx"))
# if usa mode, each gene gets 3 rows, so the actual number of genes is ng/3
if (usa_mode) {
if (ng %% 3 != 0) {
stop("The number of quantified targets is not a multiple of 3")
}
ng <- as.integer(ng/3)
}
ng <- as.integer(ng/3)
}

# read in gene name file and cell barcode file
afg <- read.table(file.path(fry.dir, "alevin", "quants_mat_cols.txt"),
strip.white = TRUE, header = FALSE, nrows = ng,
col.names = c("gene_ids"), row.names = 1)
afc <- read.table(file.path(fry.dir, "alevin", "quants_mat_rows.txt"),
strip.white = TRUE, header = FALSE,
col.names = c("barcodes"), row.names = 1)

# if in usa_mode, sum up counts in different status according to which_counts
if (usa_mode) {
rd <- list("S" = seq(1, ng), "U" = seq(ng + 1, 2 * ng),
"A" = seq(2 * ng + 1, 3 * ng))
o <- af_raw[, rd[[which_counts[1]]], drop = FALSE]
for (wc in which_counts[-1]) {
o <- o + af_raw[, rd[[wc]], drop = FALSE]

# read in gene name file and cell barcode file
afg <- read.table(file.path(fry.dir, "alevin", "quants_mat_cols.txt"),
strip.white = TRUE, header = FALSE, nrows = ng,
col.names = c("gene_ids"), row.names = 1)
afc <- read.table(file.path(fry.dir, "alevin", "quants_mat_rows.txt"),
strip.white = TRUE, header = FALSE,
col.names = c("barcodes"), row.names = 1)

# if in usa_mode, sum up counts in different status according to which_counts
if (usa_mode) {
rd <- list("S" = seq(1, ng), "U" = seq(ng + 1, 2 * ng),
"A" = seq(2 * ng + 1, 3 * ng))
o <- af_raw[, rd[[which_counts[1]]], drop = FALSE]
for (wc in which_counts[-1]) {
o <- o + af_raw[, rd[[wc]], drop = FALSE]
}
} else {
o <- af_raw
}
} else {
o <- af_raw
}

# create SingleCellExperiment object
sce <- SingleCellExperiment(list(counts = t(o)),
colData = afc,
rowData = afg
)
sce
# create SingleCellExperiment object
sce <- SingleCellExperiment(list(counts = t(o)),
colData = afc,
rowData = afg
)
sce
}

0 comments on commit 19124e4

Please sign in to comment.