Skip to content

Commit 9704759

Browse files
committed
account for non-unique var_names in concatenate
1 parent 366a370 commit 9704759

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

anndata/base.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -1310,19 +1310,19 @@ def concatenate(self, *adatas, join='inner', batch_key='batch', batch_categories
13101310
adatas : :class:`~anndata.AnnData`
13111311
AnnData matrices to concatenate with.
13121312
join: `str` (default: 'inner')
1313-
Use intersection (``'inner'``) or union (``'outer'``) of variables?
1313+
Use intersection (``'inner'``) or union (``'outer'``) of variables.
13141314
batch_key : `str` (default: 'batch')
13151315
Add the batch annotation to `.obs` using this key.
13161316
batch_categories : list, optional (default: `range(len(adatas)+1)`)
13171317
Use these as categories for the batch annotation.
1318-
index_unique : `str` or `None`, optional (default: '-')
1319-
Make the index unique by joining the previous index name with the
1320-
batch category. Provide `None` to keep previous indices.
1318+
index_unique : `str` or `None`, optional (default: None)
1319+
Make the index unique by joining the existing index names with the
1320+
batch category. Provide `None` to keep existing indices.
13211321
13221322
Returns
13231323
-------
13241324
adata : :class:`~anndata.AnnData`
1325-
The concatenated AnnData, where `adata.obs['batch']` stores a
1325+
The concatenated AnnData, where `adata.obs[batch_key]` stores a
13261326
categorical variable labeling the batch.
13271327
13281328
Examples
@@ -1360,8 +1360,18 @@ def concatenate(self, *adatas, join='inner', batch_key='batch', batch_categories
13601360
return self
13611361
elif len(adatas) == 1 and not isinstance(adatas[0], AnnData):
13621362
adatas = adatas[0] # backwards compatibility
1363-
all_adatas = (self,) + adatas
1363+
all_adatas = (self,) + tuple(adatas)
13641364

1365+
# for controlled behavior, make all variable names unique
1366+
printed_info = False
1367+
for i, ad in enumerate(all_adatas):
1368+
if not ad.var_names.is_unique:
1369+
ad.var_names = utils.make_index_unique(ad.var_names)
1370+
if not printed_info:
1371+
logg.info(
1372+
'Making variable names unique for controlled concatenation.')
1373+
printed_info = True
1374+
13651375
mergers = dict(inner=set.intersection, outer=set.union)
13661376
var_names = pd.Index(reduce(mergers[join], (set(ad.var_names) for ad in all_adatas)))
13671377

anndata/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ def warn_names_duplicates(string, df):
4242
names = 'Observation' if string == 'obs' else 'Variable'
4343
logg.info(
4444
'{} names are not unique. '
45-
'To make them unique, call `.{}_names_make_unique()`.\n')
45+
'To make them unique, call `.{}_names_make_unique()`.'
46+
.format(names, string))

0 commit comments

Comments
 (0)