{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":91249,"databundleVersionId":11294684,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Published on March 05, 2025. By Prata, Marília (mpwolke).","metadata":{}},{"cell_type":"markdown","source":"### Bacterial Flagellar Motor\n\nCitation: Tan, J., Zhang, L., Zhou, X. et al. Structural basis of the bacterial flagellar motor rotational switching. Cell Res 34, 788–801 (2024). https://doi.org/10.1038/s41422-024-01017-z\n\n**Structural basis of the bacterial flagellar motor rotational switching**\n\n\"The bacterial flagellar motor is a huge bidirectional rotary nanomachine that drives rotation of the flagellum for bacterial motility. The cytoplasmic C ring of the flagellar motor functions as the switch complex for the rotational direction switching from counterclockwise to clockwise. However, the structural basis of the rotational switching and how the C ring is assembled have long remained elusive.\"\n\n\"There, the authors presented two high-resolution cryo-electron microscopy structures of the C ring-containing flagellar basal body–hook complex from Salmonella Typhimurium, which are in the default counterclockwise state and in a constitutively active CheY mutant-induced clockwise state, respectively. In both complexes, the C ring consists of four subrings, but is in two different conformations.\"\n\n\"The CheY proteins are bound into an open groove between two adjacent protomers on the surface of the middle subring of the C ring and interact with the FliG and FliM subunits. The binding of the CheY protein induces a significant upward shift of the C ring towards the MS ring and inward movements of its protomers towards the motor center, which eventually remodels the structures of the FliG subunits and reverses the orientations and surface electrostatic potential of the αtorque helices to trigger the counterclockwise-to-clockwise rotational switching.\"\n\n\"The conformational changes of the FliG subunits reveal that the stator units on the motor require a relocation process in the inner membrane during the rotational switching. This study provides unprecedented molecular insights into the rotational switching mechanism and a detailed overall structural view of the bacterial flagellar motors.\"\n\n\"All final models were validated using **MolProbity**.56 Root mean square deviation (RMSD) values and the electrostatic distributions were calculated using **PyMol**. The model resolutions were estimated by phenix.mtriage using the model-based noise-free and experimental maps with an FSC criterion of 0.5. Angle and distance measurements were performed in **ChimeraX**.\"\n\nhttps://www.nature.com/articles/s41422-024-01017-z#Bib1","metadata":{}},{"cell_type":"markdown","source":"## This MOTOR Lives INSIDE YOU!\n\n<iframe width=\"928\" height=\"522\" src=\"https://www.youtube.com/embed/9qPJueHMVMQ\" title=\"This MOTOR Lives INSIDE YOU!\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen></iframe>\n\nhttps://www.youtube.com/watch?v=9qPJueHMVMQ","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n#Two lines Required to Plot Plotly\nimport plotly.io as pio\npio.renderers.default = 'iframe'\n\nimport plotly.graph_objs as go\nimport plotly.offline as py\nimport plotly.express as px\n\nimport plotly.io as pio\npio.renderers.default = 'iframe'\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T02:08:12.012904Z","iopub.execute_input":"2025-03-06T02:08:12.013301Z","iopub.status.idle":"2025-03-06T02:08:12.021203Z","shell.execute_reply.started":"2025-03-06T02:08:12.013271Z","shell.execute_reply":"2025-03-06T02:08:12.020105Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Competition Citation: \n\n@misc{byu-locating-bacterial-flagellar-motors-2025,\n\n    author = {Andrew Darley and Braxton Owens and Bryan Morse and Eben Lonsdale and Gus Hart and Jackson Pond and Joshua Blaser and Matias Gomez Paz and Matthew Ward and Rachel Webb and Andrew Crowther and Nathan Smith and Grant J. Jensen and TJ Hart and Maggie Demkin and Walter Reade and Elizabeth Park},","metadata":{}},{"cell_type":"markdown","source":"## Load train_labels csv file","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/byu-locating-bacterial-flagellar-motors-2025/train_labels.csv')\ntrain.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T00:27:15.172382Z","iopub.execute_input":"2025-03-06T00:27:15.172802Z","iopub.status.idle":"2025-03-06T00:27:15.214245Z","shell.execute_reply.started":"2025-03-06T00:27:15.172768Z","shell.execute_reply":"2025-03-06T00:27:15.213259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/sample_submission.csv')\nsub.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T00:27:49.557767Z","iopub.execute_input":"2025-03-06T00:27:49.558167Z","iopub.status.idle":"2025-03-06T00:27:49.579492Z","shell.execute_reply.started":"2025-03-06T00:27:49.558110Z","shell.execute_reply":"2025-03-06T00:27:49.578545Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## train_labels info()","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T00:28:41.584624Z","iopub.execute_input":"2025-03-06T00:28:41.585038Z","iopub.status.idle":"2025-03-06T00:28:41.612543Z","shell.execute_reply.started":"2025-03-06T00:28:41.585004Z","shell.execute_reply":"2025-03-06T00:28:41.611332Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Motor axis 3D - How to interpret it?\n\n\"Data points that tend to rise together suggest a positive correlation.\"\n\n\"Data points that tend to rise as other data points tend to decline suggests a negative correlation.\"\n\n\"Outliers fall far from the main group of data points.\"\n\n\"Each row in the data table is represented by a marker whose position depends on its values in the columns set on the X, Y, and Z axes.\"\n\n\nWith that amount of points below, it isn't easy to say anything about the 3D. Mostly, for a non-coder, non-nothing like me.","metadata":{}},{"cell_type":"code","source":"#Code by Anmorgul https://www.kaggle.com/anmorgul/strange-pattern-cottonwood-willow\n#https://www.kaggle.com/code/mpwolke/roosevelt-forest-of-northern-colorado-charts\n\nfor i in range(4,5):\n    fig = px.scatter_3d(train, x='Motor axis 0', y='Motor axis 1', z='Motor axis 2',\n                  color='Number of motors', size_max=6, width=800, height=600, opacity=0.9, template=\"plotly_dark\")\n    fig.update_layout(\n        font_size=8,\n        legend_font_size=16,)\n    fig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T00:46:04.695340Z","iopub.execute_input":"2025-03-06T00:46:04.695712Z","iopub.status.idle":"2025-03-06T00:46:04.809885Z","shell.execute_reply.started":"2025-03-06T00:46:04.695666Z","shell.execute_reply":"2025-03-06T00:46:04.808814Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Array shape (axis ) 3D chart","metadata":{}},{"cell_type":"code","source":"for i in range(4,5):\n    fig = px.scatter_3d(train, x='Array shape (axis 0)', y='Array shape (axis 1)', z='Array shape (axis 2)',\n                  color='Number of motors', size_max=6, width=800, height=600, opacity=0.9, template=\"ggplot2\")\n    fig.update_layout(\n        font_size=8,\n        legend_font_size=16,)\n    fig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T00:45:03.346436Z","iopub.execute_input":"2025-03-06T00:45:03.346828Z","iopub.status.idle":"2025-03-06T00:45:03.455908Z","shell.execute_reply.started":"2025-03-06T00:45:03.346796Z","shell.execute_reply":"2025-03-06T00:45:03.454824Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### describe() method","metadata":{}},{"cell_type":"code","source":"train.describe().loc[['mean','min','max']].T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T01:00:25.475801Z","iopub.execute_input":"2025-03-06T01:00:25.476203Z","iopub.status.idle":"2025-03-06T01:00:25.510733Z","shell.execute_reply.started":"2025-03-06T01:00:25.476170Z","shell.execute_reply":"2025-03-06T01:00:25.509853Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Checking distributions. train_labels histograms.","metadata":{}},{"cell_type":"code","source":"#https://stackoverflow.com/questions/64791405/log-scale-for-multiple-subplot-histograms-in-pandas\n\n# no need to initiate `fig,ax` to avoid the warning\naxes = train.hist(bins=25, figsize=(8,6), layout=(-1, 4), edgecolor=\"black\")\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T01:04:18.995338Z","iopub.execute_input":"2025-03-06T01:04:18.995675Z","iopub.status.idle":"2025-03-06T01:04:20.995413Z","shell.execute_reply.started":"2025-03-06T01:04:18.995650Z","shell.execute_reply":"2025-03-06T01:04:20.994371Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Heatmap\n\nIt seems that, Motor axis 1 is correlated with Motor axis 2.\n\nAnd, Array shape (axis 1) correlated with Array shape (axis 2).","metadata":{}},{"cell_type":"code","source":"#Lucas Dat Artist https://www.kaggle.com/code/lucasdataartist/eda-prediction-of-obesity-risk\n\n# correlation matrix\nplt.figure(figsize = (8, 4), facecolor = \"white\")\n\n# plotting\nsns.heatmap(\n    data = train.corr(numeric_only = True),\n    cmap = \"summer\",\n    vmin = -1, vmax = 1,\n    linecolor = \"white\", linewidth = 0.5,\n    annot = True,\n    fmt = \".2f\"\n)\n\nplt.title('Correlation Heatmap')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T01:13:07.178210Z","iopub.execute_input":"2025-03-06T01:13:07.178537Z","iopub.status.idle":"2025-03-06T01:13:07.836322Z","shell.execute_reply.started":"2025-03-06T01:13:07.178512Z","shell.execute_reply":"2025-03-06T01:13:07.835249Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%matplotlib inline\nfrom PIL import Image\nfrom glob import glob\nimport cv2\n\nimport math\nimport random","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T01:36:57.935195Z","iopub.execute_input":"2025-03-06T01:36:57.935537Z","iopub.status.idle":"2025-03-06T01:36:57.941461Z","shell.execute_reply.started":"2025-03-06T01:36:57.935513Z","shell.execute_reply":"2025-03-06T01:36:57.940298Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Bacterial Flagellar Motor train images","metadata":{}},{"cell_type":"code","source":"def plotImages(motor,directory):\n    print(motor)\n    multipleImages = glob(directory)\n    plt.rcParams['figure.figsize'] = (15, 15)\n    plt.subplots_adjust(wspace=0, hspace=0)\n    i_ = 0\n    for l in multipleImages[:25]:\n        im = cv2.imread(l)\n        im = cv2.resize(im, (128, 128)) \n        plt.subplot(5, 5, i_+1) #.set_title(l)\n        plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)); plt.axis('off')\n        i_ += 1\n        \n        \nplotImages(\"Bacterial Flagellar Motors train images\",\"../input/byu-locating-bacterial-flagellar-motors-2025/train/***/**\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T01:43:46.451429Z","iopub.execute_input":"2025-03-06T01:43:46.451820Z","iopub.status.idle":"2025-03-06T01:44:00.012249Z","shell.execute_reply.started":"2025-03-06T01:43:46.451791Z","shell.execute_reply":"2025-03-06T01:44:00.010807Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Bacterial Flagellar Motor test images","metadata":{}},{"cell_type":"code","source":"def plotImages(motor,directory):\n    print(motor)\n    multipleImages = glob(directory)\n    plt.rcParams['figure.figsize'] = (15, 15)\n    plt.subplots_adjust(wspace=0, hspace=0)\n    i_ = 0\n    for l in multipleImages[:25]:\n        im = cv2.imread(l)\n        im = cv2.resize(im, (128, 128)) \n        plt.subplot(5, 5, i_+1) #.set_title(l)\n        plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)); plt.axis('off')\n        i_ += 1\n        \n        \nplotImages(\"Bacterial Flagellar Motors test images\",\"../input/byu-locating-bacterial-flagellar-motors-2025/test/***/**\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T01:44:42.873662Z","iopub.execute_input":"2025-03-06T01:44:42.874094Z","iopub.status.idle":"2025-03-06T01:44:44.992560Z","shell.execute_reply.started":"2025-03-06T01:44:42.874061Z","shell.execute_reply":"2025-03-06T01:44:44.991041Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#By Yaroslav Isaienkov https://www.kaggle.com/ihelon/monet-eda-and-visualization-techniques\n\ndef visualize_images(path, n_images, is_random=True, figsize=(16, 16)):\n    plt.figure(figsize=figsize)\n    w = int(n_images ** .5)\n    h = math.ceil(n_images / w)\n    \n    all_names = os.listdir(path)\n    image_names = all_names[:n_images]   \n    if is_random:\n        image_names = random.sample(all_names, n_images)\n            \n    for ind, image_name in enumerate(image_names):\n        img = cv2.imread(os.path.join(path, image_name))\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n        plt.subplot(h, w, ind + 1)\n        plt.imshow(img)\n        plt.xticks([])\n        plt.yticks([])\n    \n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T02:08:33.184476Z","iopub.execute_input":"2025-03-06T02:08:33.184860Z","iopub.status.idle":"2025-03-06T02:08:33.192231Z","shell.execute_reply.started":"2025-03-06T02:08:33.184828Z","shell.execute_reply":"2025-03-06T02:08:33.190792Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tomo08bf73_JPG_PATH = '../input/byu-locating-bacterial-flagellar-motors-2025/train/tomo_08bf73'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T02:08:38.200266Z","iopub.execute_input":"2025-03-06T02:08:38.200616Z","iopub.status.idle":"2025-03-06T02:08:38.205202Z","shell.execute_reply.started":"2025-03-06T02:08:38.200590Z","shell.execute_reply":"2025-03-06T02:08:38.203662Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### tomo_08bf73 visualizations","metadata":{}},{"cell_type":"code","source":"visualize_images(tomo08bf73_JPG_PATH, 9)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T02:08:42.640677Z","iopub.execute_input":"2025-03-06T02:08:42.641111Z","iopub.status.idle":"2025-03-06T02:08:44.888678Z","shell.execute_reply.started":"2025-03-06T02:08:42.641078Z","shell.execute_reply":"2025-03-06T02:08:44.887165Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tomo05f919_JPG_PATH = '../input/byu-locating-bacterial-flagellar-motors-2025/train/tomo_05f919'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T02:12:35.526713Z","iopub.execute_input":"2025-03-06T02:12:35.527150Z","iopub.status.idle":"2025-03-06T02:12:35.531626Z","shell.execute_reply.started":"2025-03-06T02:12:35.527118Z","shell.execute_reply":"2025-03-06T02:12:35.530399Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### tomo 08bf73 visualizations","metadata":{}},{"cell_type":"code","source":"visualize_images(tomo05f919_JPG_PATH, 9)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T02:13:13.368561Z","iopub.execute_input":"2025-03-06T02:13:13.368948Z","iopub.status.idle":"2025-03-06T02:13:15.499447Z","shell.execute_reply.started":"2025-03-06T02:13:13.368920Z","shell.execute_reply":"2025-03-06T02:13:15.498279Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Draft Session: 1h49m","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgements:\n\nYaroslav Isaienkov https://www.kaggle.com/ihelon/monet-eda-and-visualization-techniques\n\nLucas Dat Artist https://www.kaggle.com/code/lucasdataartist/eda-prediction-of-obesity-risk","metadata":{}}]}