@@ -150,43 +150,3 @@ remove_unlabelled <- function(adata) {
150150 c(" unlabelled" , " unlabeled" )
151151 adata [! is_unlabelled , ]
152152}
153-
154- # ' Subsets the anndata object in a stratified manner
155- # ' with 'cell type' and 'sample' as strata.
156- # '
157- # ' @param adata AnnData object
158- # ' @param frac numeric, fraction of cells to keep for each cell type
159- # ' @param seed numeric, seed for reproducibility
160- # ' @param anndatar logical, whether the input is anndataR object or not
161- # ' @return AnnData object with only the markers to correct
162- subset_by_celltype <- function (adata , frac = 0.5 , seed = 1 , anndatar = TRUE ) {
163- set.seed(seed )
164-
165- obs <- adata $ obs
166- obs $ cell_id <- rownames(obs )
167- obs $ .row <- seq_len(nrow(obs )) # original order
168-
169- keep_ids <- obs %> %
170- group_by(cell_type , sample ) %> %
171- slice_sample(prop = frac ) %> %
172- ungroup() %> %
173- arrange(.row ) %> % # restore original order
174- pull(cell_id )
175-
176- if (anndatar == TRUE ){
177- keep_idx <- match(keep_ids , adata $ obs_names )
178-
179- adata_sub <- anndataR :: AnnData(
180- X = NULL ,
181- obs = adata $ obs [keep_idx , , drop = FALSE ],
182- var = adata $ var ,
183- uns = adata $ uns ,
184- layers = list (
185- " integrated" = adata $ layers $ integrated [keep_idx , , drop = FALSE ]
186- )
187- )
188- } else {
189- adata_sub <- adata [keep_ids , ]
190- }
191- }
192-
0 commit comments