{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# !pip install arnie","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:22:43.904657Z","iopub.execute_input":"2023-10-26T18:22:43.905181Z","iopub.status.idle":"2023-10-26T18:22:43.944947Z","shell.execute_reply.started":"2023-10-26T18:22:43.905129Z","shell.execute_reply":"2023-10-26T18:22:43.943744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Install Eternafold\n# !conda config --set auto_update_conda false\n# !conda install -c bioconda eternafold --yes","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:22:43.946764Z","iopub.execute_input":"2023-10-26T18:22:43.947717Z","iopub.status.idle":"2023-10-26T18:22:43.954130Z","shell.execute_reply.started":"2023-10-26T18:22:43.947670Z","shell.execute_reply":"2023-10-26T18:22:43.952247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %env ETERNAFOLD_PATH=/opt/conda/bin/eternafold-bin\n# %env ETERNAFOLD_PARAMETERS=/opt/conda/lib/eternafold-lib/parameters/EternaFoldParams.v1","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:22:43.955626Z","iopub.execute_input":"2023-10-26T18:22:43.956590Z","iopub.status.idle":"2023-10-26T18:22:43.970094Z","shell.execute_reply.started":"2023-10-26T18:22:43.956548Z","shell.execute_reply":"2023-10-26T18:22:43.968436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-26T18:22:43.972216Z","iopub.execute_input":"2023-10-26T18:22:43.973742Z","iopub.status.idle":"2023-10-26T18:22:44.888674Z","shell.execute_reply.started":"2023-10-26T18:22:43.973667Z","shell.execute_reply":"2023-10-26T18:22:44.887494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OrdinalEncoder\nfrom sklearn.preprocessing import OneHotEncoder\n\nfrom lightgbm import LGBMRegressor\nfrom xgboost import XGBRegressor\nfrom sklearn.multioutput import MultiOutputRegressor\nfrom sklearn.svm import SVR\nfrom sklearn.ensemble import RandomForestRegressor\n\nfrom sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:22:44.890470Z","iopub.execute_input":"2023-10-26T18:22:44.891101Z","iopub.status.idle":"2023-10-26T18:23:00.472636Z","shell.execute_reply.started":"2023-10-26T18:22:44.891068Z","shell.execute_reply":"2023-10-26T18:23:00.471343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nimport polars as pl\ntrain_dataset = (pl.scan_csv('/kaggle/input/stanford-ribonanza-rna-folding/train_data.csv')\n                 .collect()\n                 .to_pandas())\ntest_dataset = pd.read_csv('/kaggle/input/stanford-ribonanza-rna-folding/test_sequences.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:00.473985Z","iopub.execute_input":"2023-10-26T18:23:00.474340Z","iopub.status.idle":"2023-10-26T18:23:32.755647Z","shell.execute_reply.started":"2023-10-26T18:23:00.474309Z","shell.execute_reply":"2023-10-26T18:23:32.754488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.760926Z","iopub.execute_input":"2023-10-26T18:23:32.761320Z","iopub.status.idle":"2023-10-26T18:23:32.769958Z","shell.execute_reply.started":"2023-10-26T18:23:32.761285Z","shell.execute_reply":"2023-10-26T18:23:32.768179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.771999Z","iopub.execute_input":"2023-10-26T18:23:32.772521Z","iopub.status.idle":"2023-10-26T18:23:32.817704Z","shell.execute_reply.started":"2023-10-26T18:23:32.772479Z","shell.execute_reply":"2023-10-26T18:23:32.816233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.820720Z","iopub.execute_input":"2023-10-26T18:23:32.821156Z","iopub.status.idle":"2023-10-26T18:23:32.837650Z","shell.execute_reply.started":"2023-10-26T18:23:32.821117Z","shell.execute_reply":"2023-10-26T18:23:32.834911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.839471Z","iopub.execute_input":"2023-10-26T18:23:32.840044Z","iopub.status.idle":"2023-10-26T18:23:32.849118Z","shell.execute_reply.started":"2023-10-26T18:23:32.840000Z","shell.execute_reply":"2023-10-26T18:23:32.847969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_dataset.apply(lambda x: x.unique())","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.851038Z","iopub.execute_input":"2023-10-26T18:23:32.852236Z","iopub.status.idle":"2023-10-26T18:23:32.860171Z","shell.execute_reply.started":"2023-10-26T18:23:32.852170Z","shell.execute_reply":"2023-10-26T18:23:32.858490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Explore the data","metadata":{}},{"cell_type":"code","source":"# train_dataset.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.861748Z","iopub.execute_input":"2023-10-26T18:23:32.862079Z","iopub.status.idle":"2023-10-26T18:23:32.875096Z","shell.execute_reply.started":"2023-10-26T18:23:32.862047Z","shell.execute_reply":"2023-10-26T18:23:32.873528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset_drop = train_dataset[['sequence_id', 'sequence', 'experiment_type', 'dataset_name', 'reads', 'signal_to_noise', 'SN_filter']]\n# train_dataset_drop.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.879762Z","iopub.execute_input":"2023-10-26T18:23:32.880100Z","iopub.status.idle":"2023-10-26T18:23:32.890009Z","shell.execute_reply.started":"2023-10-26T18:23:32.880071Z","shell.execute_reply":"2023-10-26T18:23:32.888246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset_drop.apply(lambda x: x.unique())","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.894701Z","iopub.execute_input":"2023-10-26T18:23:32.896805Z","iopub.status.idle":"2023-10-26T18:23:32.906850Z","shell.execute_reply.started":"2023-10-26T18:23:32.896746Z","shell.execute_reply":"2023-10-26T18:23:32.905777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset_drop.apply(lambda x: len(x.unique()))","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.907911Z","iopub.execute_input":"2023-10-26T18:23:32.908240Z","iopub.status.idle":"2023-10-26T18:23:32.921136Z","shell.execute_reply.started":"2023-10-26T18:23:32.908210Z","shell.execute_reply":"2023-10-26T18:23:32.920179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(8,3))\n# plt.title('signal_to_noise')\n# sns.histplot(x=train_dataset_drop.signal_to_noise, hue=train_dataset_drop.experiment_type, multiple='dodge')\n# plt.yscale('log')","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.922691Z","iopub.execute_input":"2023-10-26T18:23:32.923021Z","iopub.status.idle":"2023-10-26T18:23:32.936885Z","shell.execute_reply.started":"2023-10-26T18:23:32.922993Z","shell.execute_reply":"2023-10-26T18:23:32.934877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(8,3))\n# plt.title('SN filter')\n# sns.histplot(x=train_dataset_drop.SN_filter, hue=train_dataset_drop.experiment_type, multiple='dodge', shrink=20)","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.938706Z","iopub.execute_input":"2023-10-26T18:23:32.939086Z","iopub.status.idle":"2023-10-26T18:23:32.950419Z","shell.execute_reply.started":"2023-10-26T18:23:32.939059Z","shell.execute_reply":"2023-10-26T18:23:32.949231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reactivity columns","metadata":{}},{"cell_type":"code","source":"reactivity_error_col = [col for col in train_dataset if col.startswith('reactivity_error')]","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.951471Z","iopub.execute_input":"2023-10-26T18:23:32.951829Z","iopub.status.idle":"2023-10-26T18:23:32.970666Z","shell.execute_reply.started":"2023-10-26T18:23:32.951797Z","shell.execute_reply":"2023-10-26T18:23:32.968848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Expand the dataset","metadata":{}},{"cell_type":"code","source":"train_dataset_2A3 = train_dataset[train_dataset.experiment_type == '2A3_MaP'].drop(columns=reactivity_error_col)\ntrain_dataset_2A3 = train_dataset_2A3.drop(columns=['sequence_id', 'experiment_type', 'dataset_name', 'reads', 'signal_to_noise', 'SN_filter'])\ntrain_dataset_2A3.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:32.972497Z","iopub.execute_input":"2023-10-26T18:23:32.972907Z","iopub.status.idle":"2023-10-26T18:23:38.685754Z","shell.execute_reply.started":"2023-10-26T18:23:32.972871Z","shell.execute_reply":"2023-10-26T18:23:38.683950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset_DMS = train_dataset[train_dataset.experiment_type == 'DMS_MaP'].drop(columns=reactivity_error_col)\ntrain_dataset_DMS = train_dataset_DMS.drop(columns=['sequence_id', 'experiment_type', 'dataset_name', 'reads', 'signal_to_noise', 'SN_filter'])\ntrain_dataset_DMS.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:38.687864Z","iopub.execute_input":"2023-10-26T18:23:38.688384Z","iopub.status.idle":"2023-10-26T18:23:44.374807Z","shell.execute_reply.started":"2023-10-26T18:23:38.688337Z","shell.execute_reply":"2023-10-26T18:23:44.372968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_2A3 = train_dataset_DMS[['sequence']]\ny_2A3 = train_dataset_DMS.drop(columns='sequence').fillna(0).astype(float)","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:23:44.377567Z","iopub.execute_input":"2023-10-26T18:23:44.378105Z","iopub.status.idle":"2023-10-26T18:24:00.091562Z","shell.execute_reply.started":"2023-10-26T18:23:44.378066Z","shell.execute_reply":"2023-10-26T18:24:00.089553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_2A3_expanded = pd.DataFrame(list(X_2A3.sequence.apply(lambda x: list(x))))","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:24:00.093878Z","iopub.execute_input":"2023-10-26T18:24:00.094397Z","iopub.status.idle":"2023-10-26T18:24:14.716976Z","shell.execute_reply.started":"2023-10-26T18:24:00.094349Z","shell.execute_reply":"2023-10-26T18:24:14.714786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoding_dict = {'A':1, 'C': 2, 'G': 3, 'U': 4, 0:0}","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:24:14.719790Z","iopub.execute_input":"2023-10-26T18:24:14.720645Z","iopub.status.idle":"2023-10-26T18:24:14.731857Z","shell.execute_reply.started":"2023-10-26T18:24:14.720564Z","shell.execute_reply":"2023-10-26T18:24:14.729284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_2A3_expanded = X_2A3_expanded.apply(lambda x: x.map(encoding_dict)).fillna(0).astype(float)\nX_2A3_expanded","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:24:14.736036Z","iopub.execute_input":"2023-10-26T18:24:14.736932Z","iopub.status.idle":"2023-10-26T18:24:25.042485Z","shell.execute_reply.started":"2023-10-26T18:24:14.736850Z","shell.execute_reply":"2023-10-26T18:24:25.040642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# encoding_dict = {'A':1, 'C': 2, 'G': 3, 'U': 4}\n# max_len = 206 \n# X_2A3_encoded = []\n# for seq in X_2A3.sequence:\n#     X_2A3_encoded.append(\n#         np.concatenate([np.asarray([encoding_dict[x] for x in seq]), np.zeros((max_len - len(seq)))]).astype(np.float32))","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:24:25.044233Z","iopub.execute_input":"2023-10-26T18:24:25.044634Z","iopub.status.idle":"2023-10-26T18:24:25.050505Z","shell.execute_reply.started":"2023-10-26T18:24:25.044599Z","shell.execute_reply":"2023-10-26T18:24:25.049066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_ds = tf.data.Dataset.from_tensor_slices((X_2A3_encoded, np.array(y_2A3)))\n# batch_size = 256\n# train_ds = train_ds.padded_batch(batch_size, padding_values=(0.0), padded_shapes=([max_len]), drop_remainder=False)\n# train_ds = train_ds.prefetch(tf.data.AUTOTUNE)\n# batch = next(iter(train_ds))\n# batch.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:24:25.052664Z","iopub.execute_input":"2023-10-26T18:24:25.053089Z","iopub.status.idle":"2023-10-26T18:24:25.061339Z","shell.execute_reply.started":"2023-10-26T18:24:25.053034Z","shell.execute_reply":"2023-10-26T18:24:25.060470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n#     vectorize_layer = tf.keras.layers.TextVectorization()\n#     vectorize_layer.adapt(X_2A3)\n    \n    model = tf.keras.models.Sequential()\n    model.add(tf.keras.layers.BatchNormalization(input_shape=(206, )))\n#     model.add(vectorize_layer)\n#     model.add(tf.keras.layers.Embedding(1000, 64))\n    model.add(tf.keras.layers.Dense(64, activation='relu'))\n    model.add(tf.keras.layers.Dense(206))\n    \n    model.compile(optimizer=tf.keras.optimizers.Adam(),\n                  loss=tf.keras.losses.MeanSquaredError(),\n                  metrics=['mae'])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:33:37.389655Z","iopub.execute_input":"2023-10-26T18:33:37.390070Z","iopub.status.idle":"2023-10-26T18:33:37.431635Z","shell.execute_reply.started":"2023-10-26T18:33:37.390037Z","shell.execute_reply":"2023-10-26T18:33:37.430517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:24:25.076375Z","iopub.execute_input":"2023-10-26T18:24:25.077549Z","iopub.status.idle":"2023-10-26T18:24:25.597129Z","shell.execute_reply.started":"2023-10-26T18:24:25.077500Z","shell.execute_reply":"2023-10-26T18:24:25.595788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_2A3_expanded, y_2A3,\n          epochs=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:24:25.598947Z","iopub.execute_input":"2023-10-26T18:24:25.599469Z","iopub.status.idle":"2023-10-26T18:30:39.611245Z","shell.execute_reply.started":"2023-10-26T18:24:25.599402Z","shell.execute_reply":"2023-10-26T18:30:39.609554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create test dataset for predictions","metadata":{}},{"cell_type":"code","source":"test_dataset_expanded = pd.DataFrame(list(test_dataset.sequence.apply(lambda x: list(x))))","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:30:49.033405Z","iopub.execute_input":"2023-10-26T18:30:49.033863Z","iopub.status.idle":"2023-10-26T18:31:53.740427Z","shell.execute_reply.started":"2023-10-26T18:30:49.033827Z","shell.execute_reply":"2023-10-26T18:31:53.738738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset_expanded = test_dataset_expanded.apply(lambda x: x.map(encoding_dict)).fillna(0).astype(float)\ntest_dataset_expanded","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:31:53.742961Z","iopub.execute_input":"2023-10-26T18:31:53.743561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.predict(test_dataset_expanded.loc[:1000])","metadata":{"execution":{"iopub.status.busy":"2023-10-26T18:30:39.617592Z","iopub.status.idle":"2023-10-26T18:30:39.617969Z","shell.execute_reply.started":"2023-10-26T18:30:39.617804Z","shell.execute_reply":"2023-10-26T18:30:39.617820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}