Skip to content

Instantly share code, notes, and snippets.

@cdiener
Created June 10, 2021 18:18
Show Gist options
  • Save cdiener/3f989faf8d7f3b920f901ea5e793690a to your computer and use it in GitHub Desktop.
Save cdiener/3f989faf8d7f3b920f901ea5e793690a to your computer and use it in GitHub Desktop.
MICOM per sample diets
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Per-sample media/diets in q2-micom\n",
"\n",
"Under the hood micom and q2-micom do have support for sample-specific media. In fact, that is micom only knows sample-specific media. If a media is not sample-speciic it is internally copied for each sample in the data set. To get an understanding of how to create a per-sample medium for q2-micom let's first have a look at the actual format. Media/diets in micom are just DataFrames with at least the columns `metabolite`, `reaction` and `flux`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>flux</th>\n",
" <th>dilution</th>\n",
" <th>metabolite</th>\n",
" <th>reaction</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>fru_m</td>\n",
" <td>EX_fru_m</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>glc_m</td>\n",
" <td>EX_glc_m</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>gal_m</td>\n",
" <td>EX_gal_m</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>man_m</td>\n",
" <td>EX_man_m</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>mnl_m</td>\n",
" <td>EX_mnl_m</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" flux dilution metabolite reaction\n",
"0 0.014899 0.1 fru_m EX_fru_m\n",
"1 0.014899 0.1 glc_m EX_glc_m\n",
"2 0.014899 0.1 gal_m EX_gal_m\n",
"3 0.014899 0.1 man_m EX_man_m\n",
"4 0.014899 0.1 mnl_m EX_mnl_m"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import qiime2 as q2\n",
"import pandas as pd\n",
"\n",
"arti = q2.Artifact.load(\"western_diet_gut.qza\")\n",
"medium = arti.view(pd.DataFrame)\n",
"medium.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you see you may also add additional columns to annotate your data. This is a global medium:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"MicomMedium[Global]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arti.type"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For a per sample medium you will need an additional column called `sample_id`. There should be a row for each metabolite that is consumed in each sample. So for instance let's assume we want to convert the current medium into sample-specific media for a normal and a diabetic sample that has higher glucose availability. For this we duplicate the medium, modify the glucose flux bound, add the sample IDs and concatenate the media."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>flux</th>\n",
" <th>dilution</th>\n",
" <th>metabolite</th>\n",
" <th>reaction</th>\n",
" <th>sample_id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>fru_m</td>\n",
" <td>EX_fru_m</td>\n",
" <td>normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>glc_m</td>\n",
" <td>EX_glc_m</td>\n",
" <td>normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>gal_m</td>\n",
" <td>EX_gal_m</td>\n",
" <td>normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>man_m</td>\n",
" <td>EX_man_m</td>\n",
" <td>normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.014899</td>\n",
" <td>0.1</td>\n",
" <td>mnl_m</td>\n",
" <td>EX_mnl_m</td>\n",
" <td>normal</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" flux dilution metabolite reaction sample_id\n",
"0 0.014899 0.1 fru_m EX_fru_m normal\n",
"1 0.014899 0.1 glc_m EX_glc_m normal\n",
"2 0.014899 0.1 gal_m EX_gal_m normal\n",
"3 0.014899 0.1 man_m EX_man_m normal\n",
"4 0.014899 0.1 mnl_m EX_mnl_m normal"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"diabetic = medium.copy()\n",
"diabetic.loc[diabetic.metabolite == \"glc_m\", \"flux\"] = 0.1\n",
"diabetic[\"sample_id\"] = \"diabetic\"\n",
"medium[\"sample_id\"] = \"normal\"\n",
"\n",
"per_sample_medium = pd.concat([medium, diabetic])\n",
"per_sample_medium.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Not that the sample IDs have to correspond to your sample names used in your OTU table paased to `qiime micom build`.\n",
"\n",
"Now we can save the medium as a per=sample q2-micom medium:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'my_media.qza'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_arti = q2.Artifact.import_data(\"MicomMedium[PerSample]\", per_sample_medium)\n",
"new_arti.save(\"my_media.qza\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And that's it 🎉 If you use this saved media with `qiime micom grow` it will automatically apply the correct medium for each sample."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment