# Metaphlan: sgb\_to\_gtdb\_profile.py fails with KeyError

**URL:** <https://forum.biobakery.org/t/metaphlan-sgb-to-gtdb-profile-py-fails-with-keyerror/8099>\
**Category:** MetaPhlAn\
**Created:** [June 2, 2025, 3:41pm UTC](https://forum.biobakery.org/t/metaphlan-sgb-to-gtdb-profile-py-fails-with-keyerror/8099 "2025-06-02T15:41:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtrachsel](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@jtrachsel](https://forum.biobakery.org/u/jtrachsel)\
**Post date:** [June 2, 2025, 3:41pm UTC](https://forum.biobakery.org/t/metaphlan-sgb-to-gtdb-profile-py-fails-with-keyerror/8099/1 "2025-06-02T15:41:55Z")

</div>

MetaPhlAn version 4.1.1 (11 Mar 2024)  
(installed from bioconda)

I profiled my metagenome and successfully created the metaphlan profile file, but got this warning message in the process:

> WARNING: The metagenome profile contains clades that represent multiple species merged into a single representant.

I then tried to convert to gtdb taxonomy (for a comparison) but received this error message:

> Mon Jun 2 15:36:05 2025: Start execution  
> Traceback (most recent call last):  
> File “/home/ubuntu/miniforge3/envs/tax\_class/bin/sgb\_to\_gtdb\_profile.py”, line 10, in   
> sys.exit(main())  
> ^^^^^^  
> File “/home/ubuntu/miniforge3/envs/tax\_class/lib/python3.12/site-packages/metaphlan/utils/sgb\_to\_gtdb\_profile.py”, line 97, in main  
> get\_gtdb\_profile(args.input, args.output, database\_controller.get\_database\_name())  
> File “/home/ubuntu/miniforge3/envs/tax\_class/lib/python3.12/site-packages/metaphlan/utils/sgb\_to\_gtdb\_profile.py”, line 72, in get\_gtdb\_profile  
> gtdb\_tax = sgb2gtdb[line[0].split(‘|’)[-1][3:]]  
> ~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
> KeyError: ‘SGB10139\_group’

I tried re-installing the database and I think I have the most recent version:  
mpa\_vJan25\_CHOCOPhlAnSGB\_202503…

How can I convert the taxonomy to gtdb?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Claudia\_Mengoni](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.biobakery.org/claudia_mengoni/32/2871_2.png) [@Claudia\_Mengoni](https://forum.biobakery.org/u/Claudia_Mengoni)\
**Post date:** [June 10, 2025, 1:43pm UTC](https://forum.biobakery.org/t/metaphlan-sgb-to-gtdb-profile-py-fails-with-keyerror/8099/2 "2025-06-10T13:43:50Z")

</div>

Hi @jtrachsel  
Can you check that the database specified in the sgb\_to\_gtdb\_profile.py corresponds to the database you used to run MetaPhlAn?  
For the sgb\_to\_gtdb\_profile.py you should find this info at the beginning of the file (defined as variable GTDB\_ASSIGNMENT\_FILE), while for the metaphlan profiles it’s in the header of the profile.

---

<div class="post-metadata">

**Author:** ![fconstancias](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.biobakery.org/fconstancias/32/190_2.png) [@fconstancias](https://forum.biobakery.org/u/fconstancias)\
**Post date:** [October 21, 2025, 6:47pm UTC](https://forum.biobakery.org/t/metaphlan-sgb-to-gtdb-profile-py-fails-with-keyerror/8099/3 "2025-10-21T18:47:59Z")

</div>

Hi @Claudia_Mengoni,and biobakers,

i biobakers,

I got this error when running:

```bash
sgb_to_gtdb_profile.py -i S_10_profiled_metagenome.txt -o S_10_profiled_metagenome_gtdb.txt

```

**Output:**

```auto
Tue Oct 21 20:41:17 2025: Start execution
Traceback (most recent call last):
  File "/work/FAC/FBM/DBC/slehtine/stool_sampling/tools/mpa4.2.2/bin/sgb_to_gtdb_profile.py", line 10, in <module>
    sys.exit(main())
             ^^^^^^
  File "/work/FAC/FBM/DBC/slehtine/stool_sampling/tools/mpa4.2.2/lib/python3.12/site-packages/metaphlan/utils/sgb_to_gtdb_profile.py", line 94, in main
    get_gtdb_profile(args.input, args.output)
  File "/work/FAC/FBM/DBC/slehtine/stool_sampling/tools/mpa4.2.2/lib/python3.12/site-packages/metaphlan/utils/sgb_to_gtdb_profile.py", line 71, in get_gtdb_profile
    gtdb_tax = sgb2gtdb[line[0].split('|')[-1][3:]]
               ~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'SGB8007_group'

```

**Output file preview:**

I am using the latest metphlan4 humann4 compatible database…

```bash
(/work/FAC/FBM/DBC/slehtine/stool_sampling/tools/mpa4.2.2)
head S_10_profiled_metagenome_gtdb.txt
#mpa_vOct22_CHOCOPhlAnSGB_202403
#clade_name relative_abundance
UNCLASSIFIED 15.33515

```

**MetaPhlAn version:**

```bash
metaphlan --version
MetaPhlAn version 4.2.2 (4 Jun 2025)

```

**Environment variable:**

```bash
echo $GTDB_ASSIGNMENT_FILE

```

Thanks for the support !

---

<div class="post-metadata">

**Author:** ![Claudia\_Mengoni](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.biobakery.org/claudia_mengoni/32/2871_2.png) [@Claudia\_Mengoni](https://forum.biobakery.org/u/Claudia_Mengoni)\
**Post date:** [October 27, 2025, 1:39pm UTC](https://forum.biobakery.org/t/metaphlan-sgb-to-gtdb-profile-py-fails-with-keyerror/8099/4 "2025-10-27T13:39:01Z")

</div>

Hi @fconstancias

Since you’re using the v4.2.2 you will see in the sgb\_to\_gtdb\_profile.py that the version hardcoded to work is the vJan25 (see first lines of the file `/work/FAC/FBM/DBC/slehtine/stool_sampling/tools/mpa4.2.2/lib/python3.12/site-packages/metaphlan/utils/sgb_to_gtdb_profile.py`) We should modify the script for it to be able to detect the version from the profile, meanwhile a quick fix would be to modify the variable in the script yourself (specifically with `GTDB_ASSIGNMENT_FILE = os.path.join(os.path.dirname(os.path.abspath(`  
`file)), “mpa_vOct22_CHOCOPhlAnSGB_202212_SGB2GTDB.tsv”)` )
