Skip to content

Commit

Permalink
added error checking to return smoothed anndatas
Browse files Browse the repository at this point in the history
  • Loading branch information
Gibbsdavidl committed Jan 4, 2024
1 parent 69491dd commit 51be2a4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions gssnng/score_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def run_gssnng(

samp_neighbors = None
error_checking(mat, samp_neighbors, recompute_neighbors,
gs_obj, score_method, ranked, method_params)
gs_obj, score_method, ranked, method_params, 0)

if method_params == None:
method_params = dict()
Expand Down Expand Up @@ -154,7 +154,7 @@ def with_gene_sets(

samp_neighbors = None
error_checking(adata, samp_neighbors, recompute_neighbors,
gs_obj, score_method, ranked, method_params)
gs_obj, score_method, ranked, method_params, 0)

if method_params == None:
method_params = dict()
Expand Down
10 changes: 6 additions & 4 deletions gssnng/smooth_anndatas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import anndata
from gssnng.score_cells import _proc_data
#from gssnng.util import error_checking
from gssnng.util import error_checking
from typing import Union

def smooth_anndata(
Expand Down Expand Up @@ -34,16 +34,18 @@ def smooth_anndata(

return_data = 1
noise_trials = 0 ### not used currently
samp_neighbors = None
samp_neighbors = None ### also not used
just_smoothing=1

#error_checking2(adata, recompute_neighbors, method_params) # UPDATE
error_checking(adata, samp_neighbors, recompute_neighbors,
None, None, None, method_params, just_smoothing)

if method_params == None:
method_params = dict()

# score each cell with the list of gene sets
data_list = _proc_data(adata, None, groupby, smooth_mode, recompute_neighbors,
None, method_params, None,
None, method_params, samp_neighbors,
noise_trials, None, cores, return_data)

print("**done**")
Expand Down
34 changes: 19 additions & 15 deletions gssnng/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def error_checking(
gs_obj,
score_method,
ranked,
method_params
method_params,
just_smoothing
):
"""
QC on the adata. Need to make sure there's enough neighbors available given the sampling size.
Expand All @@ -23,30 +24,33 @@ def error_checking(
:param samp_neighbors: integer, number of neighbors to sample
"""

if type(method_params) != type(dict()):
raise Exception('ERROR: please use a dictionary to pass method params')

if any([xi in adata.obs.columns for xi in gs_obj.get_gs_names()]):
#raise Exception('ERROR: gene set names in columns of adata.obs, please drop.')
print("Warning! Dropping gene set names from obs!")
genesetlist = [x.name for x in gs_obj.set_list]
for gsi in genesetlist:
print('dropping: ' + gsi)
adata.obs.drop(columns=[gsi], inplace=True)

if 'gssnng_groupby' in adata.obs.columns:
adata.obs.drop(columns='gssnng_groupby', inplace=True)
#raise Exception("Error: please drop 'gssnng_groupby' as a column name.")
print('... and dropping gssnng_groupby column...')

if ranked == False and score_method == 'singscore':
raise Exception('ERROR: singscore requires ranked data, set ranked parameter to True')

if (recompute_neighbors == None) or (recompute_neighbors == 0):
n_neighbors = adata.uns['neighbors']['params']['n_neighbors'] #[0]# in older AnnData versions need this??
else:
n_neighbors = recompute_neighbors

if just_smoothing == 0:
# then do all other checks
if type(method_params) != type(dict()):
raise Exception('ERROR: please use a dictionary to pass method params')

if any([xi in adata.obs.columns for xi in gs_obj.get_gs_names()]):
#raise Exception('ERROR: gene set names in columns of adata.obs, please drop.')
print("Warning! Dropping gene set names from obs!")
genesetlist = [x.name for x in gs_obj.set_list]
for gsi in genesetlist:
print('dropping: ' + gsi)
adata.obs.drop(columns=[gsi], inplace=True)

if ranked == False and score_method == 'singscore':
raise Exception('ERROR: singscore requires ranked data, set ranked parameter to True')


#if n_neighbors < samp_neighbors:
# print('*******')
# print('WARNING: Number of neighbors too low for sampling parameter!')
Expand Down

0 comments on commit 51be2a4

Please sign in to comment.