# Filtering out a subset of genes in adata

**URL:** <https://discourse.scverse.org/t/filtering-out-a-subset-of-genes-in-adata/2419>\
**Category:** anndata\
**Created:** [July 30, 2024, 8:18pm UTC](https://discourse.scverse.org/t/filtering-out-a-subset-of-genes-in-adata/2419 "2024-07-30T20:18:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlejandraRodelaRo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/alejandrarodelaro/32/1113_2.png) [@AlejandraRodelaRo](https://discourse.scverse.org/u/AlejandraRodelaRo)\
**Post date:** [July 30, 2024, 8:18pm UTC](https://discourse.scverse.org/t/filtering-out-a-subset-of-genes-in-adata/2419/1 "2024-07-30T20:18:09Z")

</div>

Hello,

I am trying to filter out a particular set of genes from a Anndata object.  
So far I tried:

```auto
# Assuming 'adata' is your AnnData object
# List of genes to remove
genes_to_remove

# Convert genes_to_remove to a set for efficient lookup
genes_to_remove_set = set(genes_to_remove)

# Check which genes are in the AnnData object
genes_in_adata = set(adata.var_names)

# Find the intersection of genes_to_remove and genes_in_adata
common_genes_to_remove = genes_to_remove_set.intersection(genes_in_adata)

# Print the genes that will be removed
print("Genes to be removed:", common_genes_to_remove)

# Create a boolean mask to keep only the genes not in 'common_genes_to_remove'
mask = [gene not in common_genes_to_remove for gene in adata.var_names]

# Subset the AnnData object to exclude the genes in 'common_genes_to_remove'
adata_filtered = adata[:, mask].copy()

# Verify that the genes have been removed
print("Remaining genes:", adata_filtered .var_names)

```

This seems to initially work, so if I run :

```auto
for gene in genes_to_remove:
    print(gene)
    # Ensure the gene exists in the data
    if gene not in adata_filtered .var_names:
        print(f"Gene {gene} not found in the dataset.")

```

it does indeed state that my list of genes is gone. However, if I run a umap for one of the genes in particular or if I run a wilcoxon analysis these genes seem to still be there…  
Any thoughts?

---

<div class="post-metadata">

**Author:** ![gtca](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/gtca/32/1199_2.png) [@gtca](https://discourse.scverse.org/u/gtca)\
**Post date:** [July 30, 2024, 9:57pm UTC](https://discourse.scverse.org/t/filtering-out-a-subset-of-genes-in-adata/2419/2 "2024-07-30T21:57:31Z")

</div>

Welcome @AlejandraRodelaRo!

Do you have more details on what `these genes seem to still be there` means in the context of UMAPs?  
I am also not sure what `run a umap for one of the genes in particular` means, do you have code examples for those steps?

---

<div class="post-metadata">

**Author:** ![AlejandraRodelaRo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/alejandrarodelaro/32/1113_2.png) [@AlejandraRodelaRo](https://discourse.scverse.org/u/AlejandraRodelaRo)\
**Post date:** [July 31, 2024, 3:13pm UTC](https://discourse.scverse.org/t/filtering-out-a-subset-of-genes-in-adata/2419/3 "2024-07-31T15:13:48Z")

</div>

Thank you @gtca for your quick reply!

Here’s the code I use to check the gene in the context of umaps:  
`sc.pl.umap(adata_filtered , color="PAX3") #where PAX3 is a gene on my list to be filtered out`

Similarly, when I compute DEG with wilcoxon I can still see some of the genes (e.g. PAX3) differentially expressed even after filtering them out:

```auto
sc.tl.rank_genes_groups(adata_filtered , 'sample', reference="WT",method='wilcoxon', key_added = "wilcoxon")
sc.pl.rank_genes_groups(adata_filtered , n_genes=20, sharey=False, key="wilcoxon")

```

---

<div class="post-metadata">

**Author:** ![AlejandraRodelaRo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/alejandrarodelaro/32/1113_2.png) [@AlejandraRodelaRo](https://discourse.scverse.org/u/AlejandraRodelaRo)\
**Post date:** [July 31, 2024, 10:54pm UTC](https://discourse.scverse.org/t/filtering-out-a-subset-of-genes-in-adata/2419/4 "2024-07-31T22:54:32Z")

</div>

Hello again,

Just realized what was the issue and might be of use to somebody.  
When you exclude some genes either by the methods I mentioned earlier or by using:

```auto
adata_filtered = adata[:, ~adata.var_names.isin(list_tobeexcluded)].copy()

```

This will erase them on adata, but not on adata.raw.  
sc.pl.umap uses use\_raw= True by default, so that’s why I was still able to detect them. Similar thing would happen when doing wilcoxon analysis…

I guess this is as simple as to run:

`adata_filtered.raw = adata_filtered.copy()`

after the exclusion, or use use\_raw=False, when running umap.
