Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimum number of cells (3) for merge #801

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bin/merge_sces.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ read_trim_sce <- function(sce_file) {
# get list of sces
sce_list <- purrr::map(input_sce_files, read_trim_sce)

# filter out libraries with fewer than 3 cells (causes errors with PCA)
n_cells <- sce_list |> purrr::map_int(ncol)
included_libs <- names(sce_list)[which(n_cells >= 3)]
lib_diff <- setdiff(names(sce_list), included_libs)
if (length(lib_diff) > 0) {
message(
"The following libraries have fewer than 3 cells and will be excluded from the merged object: ",
paste(lib_diff, collapse = ", ")
)
}
sce_list <- sce_list[included_libs]


# Add cell type annotation columns where needed -------------------------------

# check for present cell type annotations
Expand Down
4 changes: 4 additions & 0 deletions lib/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class Utils {
* @return A value from the metadata
*/
static def getMetaVal(file, key) {
if (!file.exists()) {
return(null)
}

def obj = new JsonSlurper().parse(file)
def value = obj[key]

Expand Down
22 changes: 16 additions & 6 deletions merge.nf
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,32 @@ workflow {
filtered_libraries_ch.single_sample
.map{[
it.library_id,
file("${params.results_dir}/${it.project_id}/${it.sample_id}/${it.library_id}_processed.rds")
file("${params.results_dir}/${it.project_id}/${it.sample_id}/${it.library_id}_processed.rds"),
file("${params.results_dir}/${it.project_id}/${it.sample_id}/${it.library_id}_metadata.json")
]}
.filter{!(it[1].exists() && it[1].size() > 0)}
.subscribe{
log.warn("Processed files do not exist for ${it[0]}. This library will not be included in the merged object.")
if(!(it[1].exists() && it[1].size() > 0)){
log.warn("Processed files do not exist for ${it[0]}. This library will not be included in the merged object.")
}
else if(!(it[2].exists() && it[2].size() > 0)){
log.warn("Metadata file does not exist for ${it[0]}. This library will not be included in the merged object.")
}
else if (Utils.getMetaVal(it[2], "processed_cells") < 3){
log.warn("Library ${it[0]} has fewer than 3 cells. This library will not be included in the merged object.")
}
}

grouped_libraries_ch = filtered_libraries_ch.single_sample
// create tuple of [project id, library_id, processed_sce_file]
.map{[
it.project_id,
it.library_id,
file("${params.results_dir}/${it.project_id}/${it.sample_id}/${it.library_id}_processed.rds")
file("${params.results_dir}/${it.project_id}/${it.sample_id}/${it.library_id}_processed.rds"),
file("${params.results_dir}/${it.project_id}/${it.sample_id}/${it.library_id}_metadata.json")
]}
// only include libraries that have been processed through scpca-nf and aren't empty
.filter{it[2].exists() && it[2].size() > 0}
// only include libraries that have been processed through scpca-nf and have at least 3 cells
.filter{it[2].exists() && it[2].size() > 0 && Utils.getMetaVal(it[3], "processed_cells") >= 3}
.map{it[0..2]} // remove metadata file from tuple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gather 0..2 syntax is the groovy equivalent of R's 0:2 (...ok, 1:3 what with indexing from 1 😄 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. I could have done a number of things to get rid of the last element. Probably the best is it.dropRight(1) to remove the last element, but this seemed fine here.

// only one row per library ID, this removes all the duplicates that may be present due to CITE/hashing
.unique()
// group tuple by project id: [project_id, [library_id1, library_id2, ...], [sce_file1, sce_file2, ...]]
Expand Down
Loading