{"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":"gpu","dataSources":[{"sourceId":87793,"databundleVersionId":11553390,"isSourceIdPinned":false,"sourceType":"competition"}],"dockerImageVersionId":30919,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Embedding, Conv1D, BatchNormalization, Dropout, Dense\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\nfrom sklearn.metrics import mean_squared_error\nimport warnings\nwarnings.simplefilter(action='ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:34:06.390077Z","iopub.execute_input":"2025-04-07T07:34:06.390429Z","iopub.status.idle":"2025-04-07T07:34:21.789930Z","shell.execute_reply.started":"2025-04-07T07:34:06.390389Z","shell.execute_reply":"2025-04-07T07:34:21.788769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_sequence = pd.read_csv('/kaggle/input/stanford-rna-3d-folding/train_sequences.csv')\ntrain_labels = pd.read_csv('/kaggle/input/stanford-rna-3d-folding/train_labels.csv')\nval_sequence = pd.read_csv('/kaggle/input/stanford-rna-3d-folding/validation_sequences.csv')\nval_labels = pd.read_csv('/kaggle/input/stanford-rna-3d-folding/validation_labels.csv')\ntest_sequence = pd.read_csv('/kaggle/input/stanford-rna-3d-folding/test_sequences.csv')\nprint(train_sequence.info())\nprint(train_labels.info())\nprint(val_sequence.info())\nprint(val_labels.info())\nprint(test_sequence.info())\n# Fill missing values\ntrain_labels.fillna(0, inplace=True)\nval_labels.fillna(0, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:34:26.004171Z","iopub.execute_input":"2025-04-07T07:34:26.004822Z","iopub.status.idle":"2025-04-07T07:34:26.475802Z","shell.execute_reply.started":"2025-04-07T07:34:26.004790Z","shell.execute_reply":"2025-04-07T07:34:26.474789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"duplicate_sequences = train_sequence[train_sequence.duplicated('sequence', keep=False)]\nprint(f\"Number of duplicate sequences in train_sequences: {len(duplicate_sequences)}\")\n\n# Check for duplicate target_ids in train_labels\nduplicate_targets = train_labels[train_labels.duplicated('ID', keep=False)]\nprint(f\"Number of duplicate targets in train_labels: {len(duplicate_targets)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:34:30.937911Z","iopub.execute_input":"2025-04-07T07:34:30.938216Z","iopub.status.idle":"2025-04-07T07:34:30.964539Z","shell.execute_reply.started":"2025-04-07T07:34:30.938196Z","shell.execute_reply":"2025-04-07T07:34:30.963605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extract coordinates for a sample target\nsample_target = train_labels\nx = sample_target['x_1'].values\ny = sample_target['y_1'].values\nz = sample_target['z_1'].values\n\n# Plot 3D structure\nfig = plt.figure(figsize=(10, 8))\nax = fig.add_subplot(111, projection='3d')\nax.scatter(x, y, z, c='blue', marker='o')\nax.set_title('3D RNA Structure')\nax.set_xlabel('X')\nax.set_ylabel('Y')\nax.set_zlabel('Z')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:34:38.144803Z","iopub.execute_input":"2025-04-07T07:34:38.145161Z","iopub.status.idle":"2025-04-07T07:34:41.217055Z","shell.execute_reply.started":"2025-04-07T07:34:38.145136Z","shell.execute_reply":"2025-04-07T07:34:41.215922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check for missing values\nprint(\"Missing values in train_sequences:\")\nprint(train_sequence.isnull().sum())\n\nprint(\"\\nMissing values in train_labels:\")\nprint(train_labels.isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:34:41.680742Z","iopub.execute_input":"2025-04-07T07:34:41.681165Z","iopub.status.idle":"2025-04-07T07:34:41.706286Z","shell.execute_reply.started":"2025-04-07T07:34:41.681134Z","shell.execute_reply":"2025-04-07T07:34:41.705058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Encoding Sequence\nseq_cod = {'A': 1,\n           'C': 2,\n           'G': 3, \n           'U': 4\n          }\ndef map_seq(seq):\n    return [seq_cod.get(char, 0) for char in seq]\n\ntrain_sequence['encoded_seq'] = train_sequence['sequence'].apply(map_seq)\ntest_sequence['encoded_seq'] = test_sequence['sequence'].apply(map_seq)\nval_sequence['encoded_seq'] = val_sequence['sequence'].apply(map_seq)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:34:47.175998Z","iopub.execute_input":"2025-04-07T07:34:47.176352Z","iopub.status.idle":"2025-04-07T07:34:47.204911Z","shell.execute_reply.started":"2025-04-07T07:34:47.176329Z","shell.execute_reply":"2025-04-07T07:34:47.203888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def labels_coord_generator(df):\n    result = {}\n    df[\"label\"] = df.ID.str.rsplit('_', n=1, expand=True).iloc[:,0]\n    for _, row in df.iterrows():\n        label = row['label']\n        resid = row['resid']\n        if label not in result:\n            result[label] = []\n        \n\n        if all(col in row for col in ['x_1', 'y_1', 'z_1']):\n            coords = np.array([\n                [row['x_1'], row['y_1'], row['z_1']],\n                [row['x_1'], row['y_1'], row['z_1']], \n                [row['x_1'], row['y_1'], row['z_1']],  \n                [row['x_1'], row['y_1'], row['z_1']],  \n                [row['x_1'], row['y_1'], row['z_1']]   \n            ], dtype=np.float32)\n        else:\n            # If no coordinates are available, put zeros\n            coords = np.zeros((5, 3), dtype=np.float32)\n        \n        result[label].append((resid, coords))\n    \n    for key in result:\n        coords = np.stack([c for r, c in result[key]])\n        result[key] = coords\n    \n    return result\n\ntrain_coords = labels_coord_generator(train_labels)\nval_coords = labels_coord_generator(val_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:34:49.399808Z","iopub.execute_input":"2025-04-07T07:34:49.400162Z","iopub.status.idle":"2025-04-07T07:35:02.965481Z","shell.execute_reply.started":"2025-04-07T07:34:49.400137Z","shell.execute_reply":"2025-04-07T07:35:02.964436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def dataset_generator(seq, stacked_coords):\n    X, y, tids = [], [], []\n    for idx, row in seq.iterrows():\n        tid = row['target_id']\n        if tid in stacked_coords:\n            X.append(row['encoded_seq'])\n            y.append(stacked_coords[tid])\n            tids.append(tid)\n    return X, y, tids\n\ntrain_X, train_y, train_tids = dataset_generator(train_sequence, train_coords)\nval_X, val_y, val_tids = dataset_generator(val_sequence, val_coords)\n\nmax_len = max(len(seq) for seq in train_X)\ntrain_X_pad = pad_sequences(train_X, maxlen=max_len, padding='post', value=0)\nval_X_pad = pad_sequences(val_X, maxlen=max_len, padding='post', value=0)\ntest_X = test_sequence['encoded_seq'].tolist()\ntest_X_pad = pad_sequences(test_X, maxlen=max_len, padding='post', value=0)\n\ndef coord_padding(coords, max_len):\n    L = coords.shape[0]\n    if L < max_len:\n        pad_width = ((0, max_len-L), (0, 0), (0, 0))\n        return np.pad(coords, pad_width, mode='constant', constant_values=0)\n    else:\n        return coords\n\ntrain_y_pad = np.array([coord_padding(y, max_len) for y in train_y])\nval_y_pad = np.array([coord_padding(y, max_len) for y in val_y])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:35:02.966979Z","iopub.execute_input":"2025-04-07T07:35:02.967328Z","iopub.status.idle":"2025-04-07T07:35:03.371211Z","shell.execute_reply.started":"2025-04-07T07:35:02.967295Z","shell.execute_reply":"2025-04-07T07:35:03.370246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"max_len","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:35:08.165501Z","iopub.execute_input":"2025-04-07T07:35:08.165902Z","iopub.status.idle":"2025-04-07T07:35:08.171569Z","shell.execute_reply.started":"2025-04-07T07:35:08.165830Z","shell.execute_reply":"2025-04-07T07:35:08.170790Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow.keras.backend as K\ndef mish(x):\n    return x * K.tanh(K.softplus(x))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:35:27.618102Z","iopub.execute_input":"2025-04-07T07:35:27.618431Z","iopub.status.idle":"2025-04-07T07:35:27.624174Z","shell.execute_reply.started":"2025-04-07T07:35:27.618407Z","shell.execute_reply":"2025-04-07T07:35:27.623200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# CNN Model\n\ninput_seq = Input(shape=(max_len,), name='input_seq')\nx = Embedding(input_dim=5, output_dim=16, mask_zero=False, name='embedding')(input_seq)\nx = Conv1D(filters=64, kernel_size=3, padding='same', activation=mish, name='conv1')(x)\nx = BatchNormalization(name='norm1')(x)\nx = Dropout(0.2, name='drop1')(x)\nx = Conv1D(filters=128, kernel_size=3, padding='same', activation=mish, name='conv2')(x)\nx = BatchNormalization(name='norm2')(x)\nx = Dropout(0.2, name='drop2')(x)\nx = Conv1D(filters=128, kernel_size=3, padding='same', activation=mish, name='conv3')(x)\nx = BatchNormalization(name='norm3')(x)\nx = Dropout(0.2, name='drop3')(x)\nx = Conv1D(filters=256, kernel_size=3, padding='same', activation=mish, name='conv4')(x)\nx = BatchNormalization(name='norm4')(x)\nx = Dropout(0.2, name='drop4')(x)    \n# Output 15 values per residue (5 sets of x, y, z coordinates)\nx = Conv1D(filters=15, kernel_size=1, padding='same', activation='linear', name='predicted_coords')(x)\nmodel = Model(inputs=input_seq, outputs=x)\nmodel.compile(optimizer='adam', loss='mae')\n   ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:35:37.700639Z","iopub.execute_input":"2025-04-07T07:35:37.701029Z","iopub.status.idle":"2025-04-07T07:35:40.191839Z","shell.execute_reply.started":"2025-04-07T07:35:37.701000Z","shell.execute_reply":"2025-04-07T07:35:40.191077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train CNN\nmodel_history = model.fit(\n    train_X_pad, train_y_pad.reshape(train_y_pad.shape[0], train_y_pad.shape[1], -1),\n    validation_data=(val_X_pad, val_y_pad.reshape(val_y_pad.shape[0], val_y_pad.shape[1], -1)),\n    epochs=50, batch_size=32, verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:38:23.047155Z","iopub.execute_input":"2025-04-07T07:38:23.047500Z","iopub.status.idle":"2025-04-07T07:39:28.458136Z","shell.execute_reply.started":"2025-04-07T07:38:23.047475Z","shell.execute_reply":"2025-04-07T07:39:28.457259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:39:46.392268Z","iopub.execute_input":"2025-04-07T07:39:46.392630Z","iopub.status.idle":"2025-04-07T07:39:46.419337Z","shell.execute_reply.started":"2025-04-07T07:39:46.392604Z","shell.execute_reply":"2025-04-07T07:39:46.418626Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def eval_model(model, X, y):\n    preds = model.predict(X)\n    preds = preds.reshape(preds.shape[0], preds.shape[1], 5, 3)  # Reshape to [batch_size, seq_len, 5, 3]\n    rmse = np.sqrt(mean_squared_error(y.reshape(-1), preds.reshape(-1)))\n    return rmse\n\ncnn_rmse = eval_model(model, val_X_pad, val_y_pad)\n\nprint(f\"CNN Validation RMSE: {cnn_rmse}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:39:49.719457Z","iopub.execute_input":"2025-04-07T07:39:49.719796Z","iopub.status.idle":"2025-04-07T07:39:50.289146Z","shell.execute_reply.started":"2025-04-07T07:39:49.719772Z","shell.execute_reply":"2025-04-07T07:39:50.288388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds = model.predict(test_X_pad)\npreds=preds.reshape(preds.shape[0],preds.shape[1],5,3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:39:57.353540Z","iopub.execute_input":"2025-04-07T07:39:57.353934Z","iopub.status.idle":"2025-04-07T07:39:57.425441Z","shell.execute_reply.started":"2025-04-07T07:39:57.353901Z","shell.execute_reply":"2025-04-07T07:39:57.424590Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Creating Submission file\nsubmission_rows = []\nfor idx, row in test_sequence.iterrows():\n    target_id = row['target_id']\n    coords = preds[idx]  \n    seq_length = len(row['encoded_seq'])\n    coords = coords[:seq_length, :, :]  \n    for i in range(seq_length):\n        x_coords = coords[i, :, 0]  # x_1, x_2, x_3, x_4, x_5\n        y_coords = coords[i, :, 1]  # y_1, y_2, y_3, y_4, y_5\n        z_coords = coords[i, :, 2]  # z_1, z_2, z_3, z_4, z_5\n        submission_rows.append({\n            'ID': f\"{target_id}_{i+1}\",\n            'resname': row['sequence'][i],\n            'resid': i+1,\n            'x_1': x_coords[0], 'x_2': x_coords[1], 'x_3': x_coords[2], 'x_4': x_coords[3], 'x_5': x_coords[4],\n            'y_1': y_coords[0], 'y_2': y_coords[1], 'y_3': y_coords[2], 'y_4': y_coords[3], 'y_5': y_coords[4],\n            'z_1': z_coords[0], 'z_2': z_coords[1], 'z_3': z_coords[2], 'z_4': z_coords[3], 'z_5': z_coords[4]})\n\nsubmission = pd.DataFrame(submission_rows)\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"Submission file is created\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T07:41:25.262264Z","iopub.execute_input":"2025-04-07T07:41:25.262639Z","iopub.status.idle":"2025-04-07T07:41:25.343431Z","shell.execute_reply.started":"2025-04-07T07:41:25.262609Z","shell.execute_reply":"2025-04-07T07:41:25.342435Z"}},"outputs":[],"execution_count":null}]}