{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":118765,"databundleVersionId":15231210}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Imports**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom collections import Counter\nfrom mpl_toolkits.mplot3d import Axes3D\n\nsns.set_style(\"whitegrid\")\n\nDATA = \"/kaggle/input/competitions/stanford-rna-3d-folding-2\"\n\ntrain_seqs   = pd.read_csv(f\"{DATA}/train_sequences.csv\")\ntest_seqs    = pd.read_csv(f\"{DATA}/test_sequences.csv\")\ntrain_labels = pd.read_csv(f\"{DATA}/train_labels.csv\", low_memory=False)\nval_labels   = pd.read_csv(f\"{DATA}/validation_labels.csv\", low_memory=False)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:03:08.365573Z","iopub.execute_input":"2026-03-05T18:03:08.366218Z","iopub.status.idle":"2026-03-05T18:03:21.175615Z","shell.execute_reply.started":"2026-03-05T18:03:08.366185Z","shell.execute_reply":"2026-03-05T18:03:21.174439Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Dataset Overview**","metadata":{}},{"cell_type":"code","source":"print(\"Train sequences:\", train_seqs.shape)\nprint(\"Test sequences:\", test_seqs.shape)\nprint(\"Train labels:\", train_labels.shape)\nprint(\"Validation labels:\", val_labels.shape)\n\ntrain_seqs.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:03:21.177482Z","iopub.execute_input":"2026-03-05T18:03:21.177847Z","iopub.status.idle":"2026-03-05T18:03:21.220777Z","shell.execute_reply.started":"2026-03-05T18:03:21.17781Z","shell.execute_reply":"2026-03-05T18:03:21.219742Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Sequence Length Distribution**","metadata":{}},{"cell_type":"code","source":"lengths = train_seqs['sequence'].apply(len)\n\nplt.figure(figsize=(8,5))\nsns.histplot(lengths[lengths < 1000], bins=50, color='skyblue')\n\nplt.title(\"RNA Sequence Lengths (<1000 nt)\")\nplt.xlabel(\"Sequence Length (nt)\")\nplt.ylabel(\"Count\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:06:35.135568Z","iopub.execute_input":"2026-03-05T18:06:35.136005Z","iopub.status.idle":"2026-03-05T18:06:35.340816Z","shell.execute_reply.started":"2026-03-05T18:06:35.135976Z","shell.execute_reply":"2026-03-05T18:06:35.339585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\nsns.histplot(lengths, bins=50, log_scale=(True, False), color='skyblue')\n\nplt.title(\"RNA Sequence Length Distribution (log scale)\")\nplt.xlabel(\"Sequence Length (nt)\")\nplt.ylabel(\"Count\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:06:48.115577Z","iopub.execute_input":"2026-03-05T18:06:48.11687Z","iopub.status.idle":"2026-03-05T18:06:48.554242Z","shell.execute_reply.started":"2026-03-05T18:06:48.116834Z","shell.execute_reply":"2026-03-05T18:06:48.553154Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Nucleotide Composition**","metadata":{}},{"cell_type":"code","source":"all_seq = \"\".join(train_seqs['sequence'].tolist())\ncounts = Counter(all_seq)\n\ndf_counts = pd.DataFrame.from_dict(counts, orient='index', columns=['count'])\n\ndf_counts.plot(kind='bar', figsize=(6,4), legend=False)\nplt.title(\"Nucleotide Frequency\")\nplt.ylabel(\"Count\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:03:21.571695Z","iopub.execute_input":"2026-03-05T18:03:21.572165Z","iopub.status.idle":"2026-03-05T18:03:22.320925Z","shell.execute_reply.started":"2026-03-05T18:03:21.572137Z","shell.execute_reply":"2026-03-05T18:03:22.320011Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Target Structure Size**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\nresidue_counts = train_labels.groupby('target').size()\nsns.histplot(residue_counts, bins=40)\n\nplt.xscale(\"log\")\n\nplt.title(\"Residues per RNA Structure (log scale)\")\nplt.xlabel(\"Number of Residues\")\nplt.ylabel(\"Count\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:10:33.792634Z","iopub.execute_input":"2026-03-05T18:10:33.793007Z","iopub.status.idle":"2026-03-05T18:10:34.160925Z","shell.execute_reply.started":"2026-03-05T18:10:33.792979Z","shell.execute_reply":"2026-03-05T18:10:34.159563Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Backbone Distance Distribution**","metadata":{}},{"cell_type":"code","source":"def backbone_distances(target):\n    \n    df = train_labels[train_labels['ID'].str.startswith(target)]\n    df = df.sort_values('resid')\n    \n    xyz = df[['x_1','y_1','z_1']].values\n    d = np.linalg.norm(xyz[1:] - xyz[:-1], axis=1)\n    \n    return d\n\nsample_target = train_seqs['target_id'].iloc[0]\ndists = backbone_distances(sample_target)\n\nplt.figure(figsize=(6,4))\nsns.histplot(dists, bins=30)\nplt.axvline(5.95, color='red', linestyle='--')\nplt.title(\"Backbone C1'-C1' Distances\")\nplt.xlabel(\"Distance (Å)\")\nplt.show()\n\nprint(\"Mean:\", dists.mean())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:03:34.237944Z","iopub.execute_input":"2026-03-05T18:03:34.238341Z","iopub.status.idle":"2026-03-05T18:03:36.392032Z","shell.execute_reply.started":"2026-03-05T18:03:34.2383Z","shell.execute_reply":"2026-03-05T18:03:36.39115Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**RNA 3D Structure Visualization**","metadata":{}},{"cell_type":"code","source":"def plot_structure(target_id):\n\n    coords = train_labels[train_labels['ID'].str.startswith(target_id)]\n    coords = coords.sort_values('resid')\n\n    fig = plt.figure(figsize=(10,8))\n    ax = fig.add_subplot(111, projection='3d')\n\n    colors = {'A':'cyan','C':'green','G':'orange','U':'magenta'}\n    c = [colors.get(r,'gray') for r in coords['resname']]\n\n    ax.scatter(coords['x_1'], coords['y_1'], coords['z_1'],\n               c=c, s=20)\n\n    ax.plot(coords['x_1'], coords['y_1'], coords['z_1'],\n            color='gray', alpha=0.4)\n\n    ax.set_title(target_id)\n\n    plt.show()\n\nplot_structure(train_seqs['target_id'].iloc[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:03:36.393208Z","iopub.execute_input":"2026-03-05T18:03:36.393641Z","iopub.status.idle":"2026-03-05T18:03:38.520861Z","shell.execute_reply.started":"2026-03-05T18:03:36.393612Z","shell.execute_reply":"2026-03-05T18:03:38.519706Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Coordinate Range**","metadata":{}},{"cell_type":"code","source":"coords = train_labels[['x_1','y_1','z_1']]\n\nprint(\"Coordinate ranges\")\nprint(coords.describe())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:03:38.522101Z","iopub.execute_input":"2026-03-05T18:03:38.522502Z","iopub.status.idle":"2026-03-05T18:03:39.386262Z","shell.execute_reply.started":"2026-03-05T18:03:38.522467Z","shell.execute_reply":"2026-03-05T18:03:39.385544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig,axs = plt.subplots(1,3,figsize=(12,4))\n\nsns.histplot(coords['x_1'], ax=axs[0])\nsns.histplot(coords['y_1'], ax=axs[1])\nsns.histplot(coords['z_1'], ax=axs[2])\n\naxs[0].set_title(\"X\")\naxs[1].set_title(\"Y\")\naxs[2].set_title(\"Z\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:03:39.387752Z","iopub.execute_input":"2026-03-05T18:03:39.388083Z","iopub.status.idle":"2026-03-05T18:04:04.19763Z","shell.execute_reply.started":"2026-03-05T18:03:39.388058Z","shell.execute_reply":"2026-03-05T18:04:04.196535Z"}},"outputs":[],"execution_count":null}]}