# Not able to load TCR data

**URL:** <https://discourse.scverse.org/t/not-able-to-load-tcr-data/771>\
**Category:** scirpy\
**Created:** [September 23, 2022, 12:54am UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771 "2022-09-23T00:54:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![france](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@france](https://discourse.scverse.org/u/france)\
**Post date:** [September 23, 2022, 12:54am UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/1 "2022-09-23T00:54:10Z")

</div>

Hello!  
I am trying to analyze my TCR data by using scirpy but I am running into an error. Quick note, I analyze my sc data using Seurat and then converted Seurat in AnnData. This worked to run scvelo but I think I am doing something wrong in loading the filtered\_contig\_annotations.

Here is my code:

```auto
adata = scv.read("sobjscv.h5ad")

filenames = ['p219_base', 'p229_base', 'p264_base', 'pV03_base', 'pV09_base', 'p219_post', 'p229_post', 'p264_post', 'pV03_post', 'pV09_post']
tcrs = [ir.io.read_10x_vdj(filename) for filename in filenames]
for tcr in range(len(tcrs)):
    tcrs[tcr].obs_names = filenames[tcr] + "_" + tcrs[tcr].obs_names
    print(tcrs[tcr].obs_names)
tcr = tcrs[0].concatenate(tcrs[1:])
adata.obs_names= [c.split("-")[0] for c in adata.obs_names]
tcr.obs_names = [d.split("-")[0] for d in tcr.obs_names]
ir.pp.merge_with_ir(adata, tcr)

```

p219\_base, p229\_base, p264\_base, pV03\_base, pV09\_base, p219\_post, p229\_post, p264\_post, pV03\_post, pV09\_post are the names of my csv contigs.

I tried to make adata.obs\_names and tcr.obs\_names the same:

```auto
adata.obs_names
Index(['p229_base_AAACCTGAGACGCACA', 'p229_base_AAACCTGCACCGAAAG',
       'p229_base_AAACCTGGTCTACCTC', 'p229_base_AAACCTGGTGGTAACG',
       'p229_base_AAACCTGTCAGCAACT', 'p229_base_AAACCTGTCCCTAATT',
       'p229_base_AAACCTGTCTTGCATT', 'p229_base_AAACGGGAGCAATATG',
       'p229_base_AAACGGGGTTATTCTC', 'p229_base_AAACGGGTCTAACCGA',
       ...
       'pV03_post_TTTGGTTTCAAGATCC', 'pV03_post_TTTGGTTTCATCGCTC',
       'pV03_post_TTTGTCAAGAAGAAGC', 'pV03_post_TTTGTCAAGCCCTAAT',
       'pV03_post_TTTGTCAAGTGTACCT', 'pV03_post_TTTGTCACAAAGAATC',
       'pV03_post_TTTGTCACAGTGAGTG', 'pV03_post_TTTGTCACATTTCACT',
       'pV03_post_TTTGTCATCCCTCAGT', 'pV03_post_TTTGTCATCGCTGATA'],
      dtype='object', length=50808)
tcr.obs_names
Index(['p219_base_AAACCTGAGAGCTGCA', 'p219_base_AAACCTGAGGGATCTG',
       'p219_base_AAACCTGAGTGGCACA', 'p219_base_AAACCTGCAAACGCGA',
       'p219_base_AAACCTGCAGCCTTTC', 'p219_base_AAACCTGCATTGCGGC',
       'p219_base_AAACCTGGTCCAAGTT', 'p219_base_AAACCTGGTGGTCTCG',
       'p219_base_AAACCTGTCACCCGAG', 'p219_base_AAACCTGTCACTGGGC',
       ...
       'pV09_post_TTTGTCACAAGAGGCT', 'pV09_post_TTTGTCACAGACAAAT',
       'pV09_post_TTTGTCACAGGTCTCG', 'pV09_post_TTTGTCAGTAGGACAC',
       'pV09_post_TTTGTCAGTCATATGC', 'pV09_post_TTTGTCAGTCGACTAT',
       'pV09_post_TTTGTCAGTCTTCTCG', 'pV09_post_TTTGTCAGTGCCTGTG',
       'pV09_post_TTTGTCATCCATTCTA', 'pV09_post_TTTGTCATCGGAATCT'],
      dtype='object', length=36086)

```

Then if I run

```auto
ax = ir.pl.group_abundance(adata, groupby="receptor_subtype", target_col="group_id")
Traceback (most recent call last):
  File "/home/francesco/Documents/AML_project/scRNA_AMLproj/python_scRNAseq/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3629, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'receptor_subtype'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/home/francesco/Documents/AML_project/scRNA_AMLproj/python_scRNAseq/lib/python3.8/site-packages/scirpy/io/_util.py", line 71, in check_wrapper
    return f(*args, **kwargs)
  File "/home/francesco/Documents/AML_project/scRNA_AMLproj/python_scRNAseq/lib/python3.8/site-packages/scirpy/pl/_group_abundance.py", line 62, in group_abundance
    abundance = tl.group_abundance(
  File "/home/francesco/Documents/AML_project/scRNA_AMLproj/python_scRNAseq/lib/python3.8/site-packages/scirpy/io/_util.py", line 71, in check_wrapper
    return f(*args, **kwargs)
  File "/home/francesco/Documents/AML_project/scRNA_AMLproj/python_scRNAseq/lib/python3.8/site-packages/scirpy/tl/_group_abundance.py", line 110, in group_abundance
    return _group_abundance(
  File "/home/francesco/Documents/AML_project/scRNA_AMLproj/python_scRNAseq/lib/python3.8/site-packages/scirpy/tl/_group_abundance.py", line 20, in _group_abundance
    na_mask = _is_na(ir_obs[groupby]) | _is_na(ir_obs[target_col])
  File "/home/francesco/Documents/AML_project/scRNA_AMLproj/python_scRNAseq/lib/python3.8/site-packages/pandas/core/frame.py", line 3505, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/home/francesco/Documents/AML_project/scRNA_AMLproj/python_scRNAseq/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3631, in get_loc
    raise KeyError(key) from err
KeyError: 'receptor_subtype'
/snap/pycharm-community/298/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_resolver.py:178: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  attr = getattr(var, n)

```

Could you help me?

Francesco

---

<div class="post-metadata">

**Author:** ![grst](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/grst/32/17_2.png) [@grst](https://discourse.scverse.org/u/grst)\
**Post date:** [September 23, 2022, 6:11am UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/2 "2022-09-23T06:11:31Z")

</div>

> `KeyError: 'receptor_subtype'`

This sounds as if you didn’t run `ir.tl.chain_qc` before plotting… any chance you didn’t do this?

* * *

The rest of your code snippet looks fine, however

```python
for tcr in range(len(tcrs)):
    tcrs[tcr]

```

could be simplified to

```python
for tcr in tcrs:
   tcr

```

---

<div class="post-metadata">

**Author:** ![france](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@france](https://discourse.scverse.org/u/france)\
**Post date:** [September 23, 2022, 4:05pm UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/3 "2022-09-23T16:05:08Z")

</div>

Thanks for your reply. I did forget to paste `ir.tl.chain_qc` in the snippet.  
Sorry for that. However, even running the missing line I get the following  
 ![group_abundance](https://canada1.discourse-cdn.com/flex035/uploads/forum11/original/1X/afbf52ddb182b9a524e3c4c70de92fa0fb58f993.png)

---

<div class="post-metadata">

**Author:** ![grst](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/grst/32/17_2.png) [@grst](https://discourse.scverse.org/u/grst)\
**Post date:** [September 23, 2022, 5:52pm UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/4 "2022-09-23T17:52:50Z")

</div>

ok, could you please take a look at

```python
tcrs[0].obs.loc[:, ["IR_VJ_1_locus", "IR_VJ_1_junction_aa", "IR_VDJ_1_locus", "IR_VDJ_1_junction_aa"]]

```

and

```auto
adata.obs.loc[:, ["IR_VJ_1_locus", "IR_VJ_1_junction_aa", "IR_VDJ_1_locus", "IR_VDJ_1_junction_aa"]]

```

and post the results here?

---

<div class="post-metadata">

**Author:** ![france](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@france](https://discourse.scverse.org/u/france)\
**Post date:** [September 23, 2022, 9:09pm UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/5 "2022-09-23T21:09:27Z")

</div>

It seems you found the issue. How can I fix it?

```auto
tcrs[0].obs.loc[:, ["IR_VJ_1_locus", "IR_VJ_1_junction_aa", "IR_VDJ_1_locus", "IR_VDJ_1_junction_aa"]]
                             IR_VJ_1_locus ... IR_VDJ_1_junction_aa
cell_id ...                     
p219_base_AAACCTGAGAGCTGCA-1 TRA ... CASSLQGGNTEAFF
p219_base_AAACCTGAGGGATCTG-1 TRA ... CASSLGQGAGQPQHF
p219_base_AAACCTGAGTGGCACA-1 TRA ... CASRDPGQRHGYTF
p219_base_AAACCTGCAAACGCGA-1 TRA ... CASSLRGGFYEQYF
p219_base_AAACCTGCAGCCTTTC-1 TRA ... CASSLGQGPGAEAFF
                                    ... ... ...
p219_base_TTTGTCAGTAGCTCCG-1 TRA ... CASSLSDRDNSPLHF
p219_base_TTTGTCAGTGCAGGTA-1 TRA ... CSALTGEIVEKLFF
p219_base_TTTGTCAGTTCAGTAC-1 TRA ... CSARDYRAVSPLHF
p219_base_TTTGTCATCATGCATG-1 TRA ... NaN
p219_base_TTTGTCATCGTCTGCT-1 TRA ... CASSEEGYYEQYF
[5016 rows x 4 columns]
adata.obs.loc[:, ["IR_VJ_1_locus", "IR_VJ_1_junction_aa", "IR_VDJ_1_locus", "IR_VDJ_1_junction_aa"]]
                           IR_VJ_1_locus ... IR_VDJ_1_junction_aa
p229_base_AAACCTGAGACGCACA NaN ... NaN
p229_base_AAAGATGCAAAGTGCG NaN ... NaN
p229_base_AAAGCAAAGTGGACGT NaN ... NaN
p229_base_AAATGCCCATTCTCAT NaN ... NaN
p229_base_AACACGTCACAACTGT NaN ... NaN
                                  ... ... ...
pV03_post_TTGGCAAAGTGTGAAT NaN ... NaN
pV03_post_TTTACTGGTAGAAGGA NaN ... NaN
pV03_post_TTTATGCCACTTCTGC NaN ... NaN
pV03_post_TTTGCGCCAGGGTATG NaN ... NaN
pV03_post_TTTGTCACATTTCACT NaN ... NaN
[2978 rows x 4 columns]

```

---

<div class="post-metadata">

**Author:** ![grst](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/grst/32/17_2.png) [@grst](https://discourse.scverse.org/u/grst)\
**Post date:** [September 24, 2022, 12:19pm UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/6 "2022-09-24T12:19:46Z")

</div>

It’s still not entirely clear to me what’s causing the problem.

First, could you please also check

```python
tcr.obs.loc[:, ["IR_VJ_1_locus", "IR_VJ_1_junction_aa", "IR_VDJ_1_locus", "IR_VDJ_1_junction_aa"]]

```

to rule out that the problem already arises during concatenation?

Second, do you happen to have a `batch` column in `adata.obs`, i.e. does one of the following exist?

```python
tcr.obs["batch"]
adata.obs["batch"]

```

If it exists, the batch column is implicitly included in the join of `merge_with_ir`, so the batch column would need to be consistent between the two objects, too.

---

<div class="post-metadata">

**Author:** ![france](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@france](https://discourse.scverse.org/u/france)\
**Post date:** [September 24, 2022, 2:56pm UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/7 "2022-09-24T14:56:30Z")

</div>

This:

```auto
tcr.obs["batch"]
adata.obs["batch"]

```

was the problem. I removed the batch variable from adata.obs and it is working.  
Only one question. How should I interpret the no IR receptor\_subtype?

 ![abund](https://canada1.discourse-cdn.com/flex035/uploads/forum11/original/1X/7277e967347d8801053aa570d3162d6329312324.png)

Is this the portion of cells without an identified TCR?

Thanks!

---

<div class="post-metadata">

**Author:** ![grst](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/grst/32/17_2.png) [@grst](https://discourse.scverse.org/u/grst)\
**Post date:** [September 24, 2022, 3:48pm UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/8 "2022-09-24T15:48:20Z")

</div>

> Is this the portion of cells without an identified TCR?

exactly. Cells with only non-productive TCRs would also fall in this category, though.

---

<div class="post-metadata">

**Author:** ![france](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@france](https://discourse.scverse.org/u/france)\
**Post date:** [September 26, 2022, 7:10pm UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/9 "2022-09-26T19:10:24Z")

</div>

Hello grst,  
Thank you again for your reply.  
How can I remove then the no IR subtype?

One last question about the Query epitope databases section. How can one plot only for example the CMV antigen.specie? Also, what would be the way to plot the antigen.species onto the 2D UMAP as density instead of dots (if possible) and also as boxplots (showing the proportion of cells CMV specific in a specific subset)?

Thanks!

---

<div class="post-metadata">

**Author:** ![grst](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/grst/32/17_2.png) [@grst](https://discourse.scverse.org/u/grst)\
**Post date:** [September 26, 2022, 8:03pm UTC](https://discourse.scverse.org/t/not-able-to-load-tcr-data/771/10 "2022-09-26T20:03:52Z")

</div>

> How can I remove then the no IR subtype?

You can simply subset the AnnData object to exclude a certain category, e.g.

```python
adata_filtered = adata[adata.obs["receptor_subtype"] != "no IR", :]

```

> How can one plot only for example the CMV antigen.specie?

Assuming you mean on a UMAP plot, you can specify the `groups` keyword (see [umap docs](https://scanpy.readthedocs.io/en/stable/generated/scanpy.pl.umap.html)).

> Also, what would be the way to plot the antigen.species onto the 2D UMAP as density instead of dots (if possible)

I never tried this, but I think this would be possible with scanpy’s [`embedding_density`](https://scanpy.readthedocs.io/en/stable/generated/scanpy.pl.embedding_density.html?highlight=density#scanpy.pl.embedding_density) function.

> and also as boxplots (showing the proportion of cells CMV specific in a specific subset)?

This is beyond the scope of scverse functions, but you can easily use the dataframe in `adata.obs` and use it with a plotting library of your choice, e.g. seaborn or altair. Here’s an example with seaborn using the `adata` object with annotated epitopes from the end of the scirpy tutorial. It generates a boxplot of cell-fractions split by sample origin (Blood vs. normal adjacent vs. Tumor) colored by antigen species.

```python
import matplotlib.pyplot as plt
import seaborn as sns

antigen_per_patient = (
    adata.obs.groupby(["source", "patient"])
    .apply(
        lambda x: x["antigen.species"]
        .value_counts(dropna=False, normalize=True)
        .rename_axis("antigen.species")
    )
    .reset_index(name="fraction")
)

sns.boxplot(data=antigen_per_patient, hue="antigen.species", x="source", y="fraction")
plt.legend(bbox_to_anchor=(1.04, 1))

```

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/forum11/original/1X/551346c8353f2b0dcd960096da0093c47e696c17.png)

You can also use the `antigen_per_patient` dataframe to perform statistics with e.g. [statsmodels](https://www.statsmodels.org/stable/index.html) or [pingouin](https://pingouin-stats.org/), or simply export it to R if you are more familiar with it.
