@@ -35,7 +35,9 @@ def filter_cells(
35
35
max_n_genes_by_counts = None ,
36
36
pct_counts_mt = None ,
37
37
cell_list = None ,
38
- inplace = True ):
38
+ excluded = False ,
39
+ inplace = True ,
40
+ ):
39
41
"""
40
42
filter cells based on numbers of genes expressed.
41
43
@@ -46,6 +48,7 @@ def filter_cells(
46
48
:param max_n_genes_by_counts: Maximum number of n_genes_by_counts for a cell pass filtering.
47
49
:param pct_counts_mt: Maximum number of pct_counts_mt for a cell pass filtering.
48
50
:param cell_list: the list of cells which will be filtered.
51
+ :param excluded: set it to True to exclude the cells which are specified by parameter `cell_list` while False to include.
49
52
:param inplace: whether inplace the original data or return a new data.
50
53
51
54
:return: StereoExpData object.
@@ -67,7 +70,10 @@ def filter_cells(
67
70
if pct_counts_mt :
68
71
cell_subset &= data .cells .pct_counts_mt <= pct_counts_mt
69
72
if cell_list is not None :
70
- cell_subset &= np .isin (data .cells .cell_name , cell_list )
73
+ if excluded :
74
+ cell_subset &= ~ np .isin (data .cells .cell_name , cell_list )
75
+ else :
76
+ cell_subset &= np .isin (data .cells .cell_name , cell_list )
71
77
data .sub_by_index (cell_index = cell_subset )
72
78
return data
73
79
@@ -80,6 +86,8 @@ def filter_genes(
80
86
max_count = None ,
81
87
gene_list = None ,
82
88
mean_umi_gt = None ,
89
+ excluded = False ,
90
+ filter_mt_genes = False ,
83
91
inplace = True
84
92
):
85
93
"""
@@ -90,14 +98,16 @@ def filter_genes(
90
98
:param max_cell: Maximun number of cells for a gene pass filtering.
91
99
:param mean_umi_gt: Filter genes whose mean umi greater than this value.
92
100
:param gene_list: the list of genes which will be filtered.
101
+ :param excluded: set it to True to exclude the genes which are specified by parameter `gene_list` while False to include.
93
102
:param inplace: whether inplace the original data or return a new data.
94
103
95
104
:return: StereoExpData object.
96
105
"""
97
106
data = data if inplace else copy .deepcopy (data )
98
- if min_cell is None and max_cell is None \
107
+ if not filter_mt_genes and \
108
+ (min_cell is None and max_cell is None \
99
109
and min_count is None and max_count is None \
100
- and gene_list is None and mean_umi_gt is None :
110
+ and gene_list is None and mean_umi_gt is None ) :
101
111
raise ValueError ('please set any of `min_cell`, `max_cell`, `min_count`, `max_count`, `gene_list` and `mean_umi_gt`' )
102
112
cal_genes_indicators (data )
103
113
gene_subset = np .ones (data .genes .size , dtype = np .bool8 )
@@ -110,9 +120,14 @@ def filter_genes(
110
120
if max_count :
111
121
gene_subset &= data .genes .n_counts <= max_count
112
122
if gene_list is not None :
113
- gene_subset &= np .isin (data .gene_names , gene_list )
123
+ if excluded :
124
+ gene_subset &= ~ np .isin (data .gene_names , gene_list )
125
+ else :
126
+ gene_subset &= np .isin (data .gene_names , gene_list )
114
127
if mean_umi_gt is not None :
115
128
gene_subset &= data .genes .mean_umi > mean_umi_gt
129
+ if filter_mt_genes :
130
+ gene_subset &= ~ np .char .startswith (np .char .lower (data .gene_names ), 'mt-' )
116
131
data .sub_by_index (gene_index = gene_subset )
117
132
return data
118
133
0 commit comments