Skip to content

Commit

Permalink
Issue #73: Extract and revise the particle_metadata module tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
bongjinkoo committed May 3, 2022
1 parent f4dd01a commit 7936529
Showing 1 changed file with 274 additions and 0 deletions.
274 changes: 274 additions & 0 deletions notebooks/particle_metadata.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"# Particle Metadata\n",
"``ioSPI`` library provides functionalities to work with cryo-EM data. To store the data, ``ioSPI`` uses the STAR (Self-defining Text Archiving and Retrieval) format (Hall, Allen and Brown, 1991) which is used by RELION for the storage of label-value pairs for all kinds of input and output metadata. In ``ioSPI``, the module `particle_metatdata` is used to create a STAR file `.star`. This module formats and writes particle metadata as `.star` files, following RELION conventions. This tutorial shows you how to create a `.star` file using `particle_detadata`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"import warnings\n",
"\n",
"sys.path.append(os.path.dirname(os.getcwd()))\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"In order to create a `.star` file, it is necessary to provide information about the experiment, such as the image pixel size and image center shift. This information is passed in the form of a list and a `Config` object."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"from ioSPI import particle_metadata\n",
"\n",
"class Config:\n",
" \"\"\"Class to instantiate the config object.\"\"\"\n",
" def __init__(self, ctf, shift):\n",
" self.ctf = ctf\n",
" self.shift = shift"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"data_list = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]]\n",
"config = Config(ctf=True, shift=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"The names of the metadata for the ``.star`` file (in RELION conventions) can be accessed using the function `get_starfile_metadata_names` passing a `Config` object."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['__rlnImageName', '__rlnAngleRot', '__rlnAngleTilt', '__rlnAnglePsi', '__rlnOriginX', '__rlnOriginY', '__rlnDefocusU', '__rlnDefocusV', '__rlnDefocusAngle', '__rlnVoltage', '__rlnImagePixelSize', '__rlnSphericalAberration', '__rlnAmplitudeContrast', '__rlnCtfBfactor']\n"
]
}
],
"source": [
"variable_names = particle_metadata.get_starfile_metadata_names(config)\n",
"print(variable_names)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"Using the list of values and the `Config` object, we can format the data using `format_metadata_for_writing_cryoem_convention` function, which creates a dataframe with the data."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"text/plain": " __rlnImageName __rlnAngleRot __rlnAngleTilt __rlnAnglePsi __rlnOriginX \\\n0 1 2 3 4 5 \n\n __rlnOriginY __rlnDefocusU __rlnDefocusV __rlnDefocusAngle \\\n0 6 7 8 9 \n\n __rlnVoltage __rlnImagePixelSize __rlnSphericalAberration \\\n0 10 11 12 \n\n __rlnAmplitudeContrast __rlnCtfBfactor \n0 13 14 ",
"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>__rlnImageName</th>\n <th>__rlnAngleRot</th>\n <th>__rlnAngleTilt</th>\n <th>__rlnAnglePsi</th>\n <th>__rlnOriginX</th>\n <th>__rlnOriginY</th>\n <th>__rlnDefocusU</th>\n <th>__rlnDefocusV</th>\n <th>__rlnDefocusAngle</th>\n <th>__rlnVoltage</th>\n <th>__rlnImagePixelSize</th>\n <th>__rlnSphericalAberration</th>\n <th>__rlnAmplitudeContrast</th>\n <th>__rlnCtfBfactor</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1</td>\n <td>2</td>\n <td>3</td>\n <td>4</td>\n <td>5</td>\n <td>6</td>\n <td>7</td>\n <td>8</td>\n <td>9</td>\n <td>10</td>\n <td>11</td>\n <td>12</td>\n <td>13</td>\n <td>14</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"metadata_df = particle_metadata.format_metadata_for_writing_cryoem_convention(data_list=data_list, config=config)\n",
"metadata_df"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"After formatting the data, we can use the function `write_metadata_to_starfile` providing the path, dataframe and name of the star file, to save the `.star` file."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"metadata_path = os.path.join(os.getcwd(), \"data\")\n",
"filename = \"metadata.star\"\n",
"particle_metadata.write_metadata_to_starfile(path=metadata_path, metadata=metadata_df, filename=filename)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"Finally, we check whether a `.star` file with the name `metadata.star` was created or not, using the function `check_star_file` function which will raise an exception if the file is not found."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"particle_metadata.check_star_file(os.path.join(metadata_path, filename))"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"The file was successfully created as there is no exception raised. Finally, let's print out the content of the `.star` file we created to verify!"
]
},
{
"cell_type": "code",
"execution_count": 11,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# Created by the starfile Python package (version 0.4.11) at 17:31:20 on 02/05/2022\n",
"\n",
"data_\n",
"\n",
"loop_\n",
"___rlnImageName #1\n",
"___rlnAngleRot #2\n",
"___rlnAngleTilt #3\n",
"___rlnAnglePsi #4\n",
"___rlnOriginX #5\n",
"___rlnOriginY #6\n",
"___rlnDefocusU #7\n",
"___rlnDefocusV #8\n",
"___rlnDefocusAngle #9\n",
"___rlnVoltage #10\n",
"___rlnImagePixelSize #11\n",
"___rlnSphericalAberration #12\n",
"___rlnAmplitudeContrast #13\n",
"___rlnCtfBfactor #14\n",
"1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\t13\t14\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"with open(os.path.join(metadata_path, filename)) as star_file:\n",
" print(star_file.read())\n",
" star_file.close()"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "venv_iospi",
"language": "python",
"name": "venv_iospi"
},
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 7936529

Please sign in to comment.