{"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":87793,"databundleVersionId":11403143,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 【question】\n\n# If the PDB is the same, would the sequence be the same?","metadata":{}},{"cell_type":"code","source":"!pip install pymsaviz","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:42:26.896200Z","iopub.execute_input":"2025-03-14T15:42:26.896534Z","iopub.status.idle":"2025-03-14T15:42:31.307632Z","shell.execute_reply.started":"2025-03-14T15:42:26.896509Z","shell.execute_reply":"2025-03-14T15:42:31.306315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport re\n\ntrain_labels = pd.read_csv('/kaggle/input/stanford-rna-3d-folding/train_labels.csv')\ntrain_sequences = pd.read_csv('/kaggle/input/stanford-rna-3d-folding/train_sequences.csv')\nprint(train_labels.shape), print(train_sequences.shape)\ntrain_sequences","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:42:31.310077Z","iopub.execute_input":"2025-03-14T15:42:31.310416Z","iopub.status.idle":"2025-03-14T15:42:31.568717Z","shell.execute_reply.started":"2025-03-14T15:42:31.310388Z","shell.execute_reply":"2025-03-14T15:42:31.567799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def extract_resid_resname(df):\n    df = df.copy()  \n    df['protein'] = None  \n    df['chain'] = None\n\n    for index, entry in df['all_sequences'].dropna().items():  \n        if isinstance(entry, str):  \n            split_data_pro = entry.split('|')[0] \n            split_data_pro = entry.split('>')[1]\n            pro = split_data_pro.split('_')[0]  \n            \n            match = re.search(r\"\\|Chain (\\w)\\|\", entry) \n            chain = match.group(1) if match else None\n\n            df.at[index, 'protein'] = pro  \n            df.at[index, 'chain'] = chain\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:42:31.571085Z","iopub.execute_input":"2025-03-14T15:42:31.571456Z","iopub.status.idle":"2025-03-14T15:42:31.579024Z","shell.execute_reply.started":"2025-03-14T15:42:31.571426Z","shell.execute_reply":"2025-03-14T15:42:31.577427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_sequences = extract_resid_resname(train_sequences)\ntrain_sequences","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:42:31.581054Z","iopub.execute_input":"2025-03-14T15:42:31.581476Z","iopub.status.idle":"2025-03-14T15:42:31.641381Z","shell.execute_reply.started":"2025-03-14T15:42:31.581437Z","shell.execute_reply":"2025-03-14T15:42:31.640106Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Usage pyMSAviz","metadata":{}},{"cell_type":"code","source":"!cat /kaggle/input/stanford-rna-3d-folding/MSA/17RA_A.MSA.fasta /kaggle/input/stanford-rna-3d-folding/MSA/1A1T_B.MSA.fasta /kaggle/input/stanford-rna-3d-folding/MSA/1A4T_A.MSA.fasta > MSA.fasta","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:42:31.642539Z","iopub.execute_input":"2025-03-14T15:42:31.642954Z","iopub.status.idle":"2025-03-14T15:42:31.774442Z","shell.execute_reply.started":"2025-03-14T15:42:31.642878Z","shell.execute_reply":"2025-03-14T15:42:31.772640Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from Bio import SeqIO\n\nmsa_file = \"MSA.fasta\"\noutput_file = \"MSA_fixed.fasta\"\n\n# 最大の配列長を取得\nmax_length = max(len(record.seq) for record in SeqIO.parse(msa_file, \"fasta\"))\n\n# 配列を揃える\nwith open(output_file, \"w\") as out_f:\n    for record in SeqIO.parse(msa_file, \"fasta\"):\n        fixed_seq = str(record.seq).ljust(max_length, \"-\")  # 修正: str() に変換\n        out_f.write(f\">{record.id}\\n{fixed_seq}\\n\")\n\nprint(f\"✅ 修正済みの MSA ファイルを保存しました: {output_file}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:42:31.776107Z","iopub.execute_input":"2025-03-14T15:42:31.776573Z","iopub.status.idle":"2025-03-14T15:42:31.785818Z","shell.execute_reply.started":"2025-03-14T15:42:31.776522Z","shell.execute_reply":"2025-03-14T15:42:31.784612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from pymsaviz import MsaViz, get_msa_testdata\nimport matplotlib.pyplot as plt\nimport IPython.display as display\n\n\n#msa_file = \"/kaggle/input/stanford-rna-3d-folding/MSA/17RA_A.MSA.fasta\"\nmsa_file = \"MSA_fixed.fasta\"\nmv = MsaViz(msa_file, color_scheme=\"Taylor\", wrap_length=50, show_grid=True, show_consensus=True)\nmv.savefig(\"api_example01.png\")\n\ndisplay.display(display.Image(\"api_example01.png\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:42:31.786849Z","iopub.execute_input":"2025-03-14T15:42:31.787225Z","iopub.status.idle":"2025-03-14T15:42:32.295107Z","shell.execute_reply.started":"2025-03-14T15:42:31.787187Z","shell.execute_reply":"2025-03-14T15:42:32.293827Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# For test sequences","metadata":{}},{"cell_type":"code","source":"test_sequences = pd.read_csv('/kaggle/input/stanford-rna-3d-folding/test_sequences.csv')\ntest_sequences = extract_resid_resname(test_sequences)\ntest_sequences","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:42:32.297636Z","iopub.execute_input":"2025-03-14T15:42:32.298022Z","iopub.status.idle":"2025-03-14T15:42:32.315221Z","shell.execute_reply.started":"2025-03-14T15:42:32.297992Z","shell.execute_reply":"2025-03-14T15:42:32.314248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_merged = test_sequences.merge(train_sequences, on=\"protein\", how=\"left\")\ndf_merged","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:42:42.561385Z","iopub.execute_input":"2025-03-14T15:42:42.561773Z","iopub.status.idle":"2025-03-14T15:42:42.583453Z","shell.execute_reply.started":"2025-03-14T15:42:42.561738Z","shell.execute_reply":"2025-03-14T15:42:42.582370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_8TVZ = df_merged[df_merged['protein']=='8TVZ']['sequence_x']\ntrain_8TVZ = df_merged[df_merged['protein']=='8TVZ']['sequence_y']\n\ntest_8BTZ = df_merged[df_merged['protein']=='8BTZ']['sequence_x']\ntrain_8BTZ = df_merged[df_merged['protein']=='8BTZ']['sequence_x']\n\ntest_8UYS = df_merged[df_merged['protein']=='8UYS']['sequence_x']\ntrain_8UYS = df_merged[df_merged['protein']=='8UYS']['sequence_x']\n\ntest_7YR7 = df_merged[df_merged['protein']=='7YR7']['sequence_x']\ntrain_7YR7 = df_merged[df_merged['protein']=='7YR7']['sequence_x']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:48:57.588717Z","iopub.execute_input":"2025-03-14T15:48:57.589156Z","iopub.status.idle":"2025-03-14T15:48:57.601040Z","shell.execute_reply.started":"2025-03-14T15:48:57.589124Z","shell.execute_reply":"2025-03-14T15:48:57.599811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# FASTA ファイルの作成\nfasta_filename = \"sequences_8TVZ.fasta\"\n\nwith open(fasta_filename, \"w\") as fasta_file:\n    for i, seq in enumerate(test_8TVZ, 1):\n        fasta_file.write(f\">test_8TVZ_{i}\\n{seq}\\n\")\n    \n    for i, seq in enumerate(train_8TVZ, 1):\n        fasta_file.write(f\">train_8TVZ_{i}\\n{seq}\\n\")\n\nprint(f\"✅ FASTA ファイルが作成されました: {fasta_filename}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:50:43.381580Z","iopub.execute_input":"2025-03-14T15:50:43.381996Z","iopub.status.idle":"2025-03-14T15:50:43.390946Z","shell.execute_reply.started":"2025-03-14T15:50:43.381966Z","shell.execute_reply":"2025-03-14T15:50:43.389598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"msa_file = \"sequences_8TVZ.fasta\"\nmv = MsaViz(msa_file, color_scheme=\"Taylor\", wrap_length=50, show_grid=True, show_consensus=True)\nmv.savefig(\"sequences_8TVZ.png\")\n\ndisplay.display(display.Image(\"sequences_8TVZ.png\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:53:27.301164Z","iopub.execute_input":"2025-03-14T15:53:27.301520Z","iopub.status.idle":"2025-03-14T15:53:34.555904Z","shell.execute_reply.started":"2025-03-14T15:53:27.301495Z","shell.execute_reply":"2025-03-14T15:53:34.554744Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# FASTA ファイルの作成\nfasta_filename = \"sequences_8BTZ.fasta\"\n\nwith open(fasta_filename, \"w\") as fasta_file:\n    for i, seq in enumerate(test_8BTZ, 1):\n        fasta_file.write(f\">test_8BTZ_{i}\\n{seq}\\n\")\n    \n    for i, seq in enumerate(train_8BTZ, 1):\n        fasta_file.write(f\">train_8BTZ_{i}\\n{seq}\\n\")\n\nprint(f\"✅ FASTA ファイルが作成されました: {fasta_filename}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:51:24.308349Z","iopub.execute_input":"2025-03-14T15:51:24.308759Z","iopub.status.idle":"2025-03-14T15:51:24.316676Z","shell.execute_reply.started":"2025-03-14T15:51:24.308726Z","shell.execute_reply":"2025-03-14T15:51:24.315211Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"msa_file = \"sequences_8BTZ.fasta\"\nmv = MsaViz(msa_file, color_scheme=\"Taylor\", wrap_length=50, show_grid=True, show_consensus=True)\nmv.savefig(\"sequences_8BTZ.png\")\n\ndisplay.display(display.Image(\"sequences_8BTZ.png\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:54:20.961045Z","iopub.execute_input":"2025-03-14T15:54:20.961417Z","iopub.status.idle":"2025-03-14T15:54:25.659912Z","shell.execute_reply.started":"2025-03-14T15:54:20.961390Z","shell.execute_reply":"2025-03-14T15:54:25.658258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# FASTA ファイルの作成\nfasta_filename = \"sequences_8UYS.fasta\"\n\nwith open(fasta_filename, \"w\") as fasta_file:\n    for i, seq in enumerate(test_8UYS, 1):\n        fasta_file.write(f\">test_8UYS_{i}\\n{seq}\\n\")\n    \n    for i, seq in enumerate(train_8UYS, 1):\n        fasta_file.write(f\">train_8UYS_{i}\\n{seq}\\n\")\n\nprint(f\"✅ FASTA ファイルが作成されました: {fasta_filename}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:51:48.564972Z","iopub.execute_input":"2025-03-14T15:51:48.565353Z","iopub.status.idle":"2025-03-14T15:51:48.573776Z","shell.execute_reply.started":"2025-03-14T15:51:48.565324Z","shell.execute_reply":"2025-03-14T15:51:48.572526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"msa_file = \"sequences_8UYS.fasta\"\nmv = MsaViz(msa_file, color_scheme=\"Taylor\", wrap_length=50, show_grid=True, show_consensus=True)\nmv.savefig(\"sequences_8UYS.png\")\n\ndisplay.display(display.Image(\"sequences_8UYS.png\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:55:31.558026Z","iopub.execute_input":"2025-03-14T15:55:31.558471Z","iopub.status.idle":"2025-03-14T15:55:34.039349Z","shell.execute_reply.started":"2025-03-14T15:55:31.558437Z","shell.execute_reply":"2025-03-14T15:55:34.038255Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# FASTA ファイルの作成\nfasta_filename = \"sequences_7YR7.fasta\"\n\nwith open(fasta_filename, \"w\") as fasta_file:\n    for i, seq in enumerate(test_7YR7, 1):\n        fasta_file.write(f\">test_7YR7_{i}\\n{seq}\\n\")\n    \n    for i, seq in enumerate(train_7YR7, 1):\n        fasta_file.write(f\">train_7YR7_{i}\\n{seq}\\n\")\n\nprint(f\"✅ FASTA ファイルが作成されました: {fasta_filename}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:52:09.517419Z","iopub.execute_input":"2025-03-14T15:52:09.517800Z","iopub.status.idle":"2025-03-14T15:52:09.524995Z","shell.execute_reply.started":"2025-03-14T15:52:09.517770Z","shell.execute_reply":"2025-03-14T15:52:09.523826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"msa_file = \"sequences_7YR7.fasta\"\nmv = MsaViz(msa_file, color_scheme=\"Taylor\", wrap_length=50, show_grid=True, show_consensus=True)\nmv.savefig(\"sequences_7YR7.png\")\n\ndisplay.display(display.Image(\"sequences_7YR7.png\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T15:55:58.590308Z","iopub.execute_input":"2025-03-14T15:55:58.590657Z","iopub.status.idle":"2025-03-14T15:56:00.986286Z","shell.execute_reply.started":"2025-03-14T15:55:58.590630Z","shell.execute_reply":"2025-03-14T15:56:00.985235Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# If the PDB is the same, it seems to be considered the same sequence.\n\n# There is no difference in mutation between train and test.","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}