{"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":"markdown","source":"# Using prep notebook zip PDBs and train with sequence ids with PDBs","metadata":{}},{"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)\n\n\nimport os\nimport sys\nimport shutil\nimport glob\nfrom shutil import copyfile\nimport gc\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-02T10:06:53.145321Z","iopub.execute_input":"2023-11-02T10:06:53.147994Z","iopub.status.idle":"2023-11-02T10:06:53.631086Z","shell.execute_reply.started":"2023-11-02T10:06:53.147937Z","shell.execute_reply":"2023-11-02T10:06:53.630167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### make a directory in /tmp to work with","metadata":{}},{"cell_type":"code","source":"!mkdir /tmp/ribonanza","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:06:56.717995Z","iopub.execute_input":"2023-11-02T10:06:56.718490Z","iopub.status.idle":"2023-11-02T10:06:57.819136Z","shell.execute_reply.started":"2023-11-02T10:06:56.718457Z","shell.execute_reply":"2023-11-02T10:06:57.817898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### use shutil to extract ","metadata":{}},{"cell_type":"code","source":"shutil.unpack_archive('/kaggle/input/ribonanza-3d-coords-prep/PDBs.zip', '/tmp/ribonanza/')\nprint('unpack archive complete!')","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:07:26.804010Z","iopub.execute_input":"2023-11-02T10:07:26.804434Z","iopub.status.idle":"2023-11-02T10:14:24.222031Z","shell.execute_reply.started":"2023-11-02T10:07:26.804400Z","shell.execute_reply":"2023-11-02T10:14:24.219813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### /tmp/ribonanza now has directories for each sequence id '/tmp/ribonanza/' + df_train_PDBs['sequence_id']","metadata":{}},{"cell_type":"code","source":"seqIDs = []\nfor dirname, _, filenames in os.walk('/tmp/ribonanza/'):\n    seqIDs.append(dirname)\n    \nlen(seqIDs)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:18:33.518764Z","iopub.execute_input":"2023-11-02T10:18:33.519224Z","iopub.status.idle":"2023-11-02T10:18:41.899448Z","shell.execute_reply.started":"2023-11-02T10:18:33.519191Z","shell.execute_reply":"2023-11-02T10:18:41.898268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seqIDs[:5],seqIDs[-5:]","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:18:53.100889Z","iopub.execute_input":"2023-11-02T10:18:53.102063Z","iopub.status.idle":"2023-11-02T10:18:53.123717Z","shell.execute_reply.started":"2023-11-02T10:18:53.101972Z","shell.execute_reply":"2023-11-02T10:18:53.120463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_PDBs  = pd.read_csv('/kaggle/input/ribonanza-3d-coords-prep/df_train_PDBs.csv')\ndf_train_PDBs.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:17:31.178174Z","iopub.execute_input":"2023-11-02T10:17:31.178819Z","iopub.status.idle":"2023-11-02T10:17:32.570873Z","shell.execute_reply.started":"2023-11-02T10:17:31.178779Z","shell.execute_reply":"2023-11-02T10:17:32.569706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### add a column for the PDBs folder for the sequence id if desired","metadata":{}},{"cell_type":"code","source":"df_train_PDBs['PDBs_folder'] = '/tmp/ribonanza/' + df_train_PDBs['sequence_id']","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:23:54.858509Z","iopub.execute_input":"2023-11-02T10:23:54.858938Z","iopub.status.idle":"2023-11-02T10:23:54.910129Z","shell.execute_reply.started":"2023-11-02T10:23:54.858906Z","shell.execute_reply":"2023-11-02T10:23:54.908510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'/tmp/ribonanza/' + df_train_PDBs['sequence_id'][0]","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:24:04.927195Z","iopub.execute_input":"2023-11-02T10:24:04.927718Z","iopub.status.idle":"2023-11-02T10:24:04.946679Z","shell.execute_reply.started":"2023-11-02T10:24:04.927662Z","shell.execute_reply":"2023-11-02T10:24:04.945387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### View an example with py3Dmol - each folder has unrelaxed_model.pdb which will be used\n","metadata":{}},{"cell_type":"code","source":"PDB2view = df_train_PDBs.PDBs_folder[0] + '/unrelaxed_model.pdb'  # folder + unrelaxed_model.pdb\nPDB2view","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:41:22.549112Z","iopub.execute_input":"2023-11-02T10:41:22.549545Z","iopub.status.idle":"2023-11-02T10:41:22.558899Z","shell.execute_reply.started":"2023-11-02T10:41:22.549511Z","shell.execute_reply":"2023-11-02T10:41:22.557229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### reference for using py3Dmol - see  \n\nhttps://www.kaggle.com/code/rhijudas/interactive-rhofold\n","metadata":{}},{"cell_type":"code","source":"!pip install py3Dmol\n#!wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=1us10sgCvCAgqL2XVW_Df2gXX8US_qlyz' -O unrelaxed_model_PymolOUT.pdb\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T11:41:43.164652Z","iopub.execute_input":"2023-11-02T11:41:43.165345Z","iopub.status.idle":"2023-11-02T11:41:57.820452Z","shell.execute_reply.started":"2023-11-02T11:41:43.165286Z","shell.execute_reply":"2023-11-02T11:41:57.818879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py3Dmol","metadata":{"execution":{"iopub.status.busy":"2023-11-02T11:42:45.394019Z","iopub.execute_input":"2023-11-02T11:42:45.394503Z","iopub.status.idle":"2023-11-02T11:42:45.445683Z","shell.execute_reply.started":"2023-11-02T11:42:45.394453Z","shell.execute_reply":"2023-11-02T11:42:45.444479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef reorder( resi_lines):\n    '''Do backbone atom reordering for a block of lines corresponding to one residue'''\n    #if len(resi_lines) > 0: print( resi_lines[0][19])  # ACGU\n    #backbone_atoms_for_nt = {'A':['P',\"C5'\",\"O5'\",\"C4'\",\"O4'\",\"C3'\",\"O3'\",\"C2'\",\"O2'\",\"C1'\"]}\n    backbone_atoms = ['P',\"C5'\",\"O5'\",\"C4'\",\"O4'\",\"C3'\",\"O3'\",\"C2'\",\"O2'\",\"C1'\",'OP1','OP2']\n    other_lines = []\n    resi_line_dict = {}\n    for resi_line in resi_lines:\n        if len(resi_line)>25: \n            atom_name = resi_line[11:16].strip()\n            if atom_name in backbone_atoms:\n                resi_line_dict[atom_name] = resi_line\n            else:\n                other_lines.append(resi_line)\n        else: other_lines.append(resi_line)\n    resi_lines_reorder = [resi_line_dict[x] for x in backbone_atoms if x in resi_line_dict]\n    resi_lines_reorder += other_lines\n    return resi_lines_reorder\n    \ndef reorder_atom_numbers( lines ):\n    ''' Atom numbers should go from 1,2,.... '''\n    atom_number = 0\n    lines_new = []\n    for line in lines:\n        line_new = line\n        if len(line) > 11:\n            atom_number += 1\n            line_new = '%s%6d%s' % (line[:5],atom_number,line[11:])\n            assert( len(line_new) == len(line))\n        lines_new.append( line_new )\n    return lines_new\n\ndef sanitize( lines ):\n    ''' sanitize RNA residue lines from RhoFold to be in \"correct\" order expected by py3Dmol.'''\n    lines_is_string = False\n    if isinstance(lines,str): \n        lines = lines.split('\\n')\n        lines_is_string = True\n        \n    lines_reorder = []\n    resi_lines = []\n    chain = ''\n    resi = ''\n    count = 0\n    for (i,line) in enumerate(lines):\n        if len(line)>25 and (line[21] != chain or resi != line[22:26]):\n            #print(len(resi_lines),chain,resi)\n            chain = line[21]\n            resi = line[22:26]\n            resi_lines_reorder = reorder(resi_lines)\n            lines_reorder = lines_reorder + reorder(resi_lines)\n            # get next block ready\n            resi_lines = [line]\n            count += 1\n        else:\n            resi_lines.append(line)\n    lines_reorder = lines_reorder + reorder(resi_lines)\n    lines_reorder = reorder_atom_numbers( lines_reorder )\n    if lines_is_string: lines_reorder = '\\n'.join(lines_reorder)\n    return lines_reorder\n\ndef get_pdb_lines(filename):\n    ''' Currently only reads in ATOM, not HETATM '''\n    lines = ''.join([(line) for line in open(filename).readlines() if line.find(\"ATOM\")==0])\n    return sanitize(lines)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-11-02T11:47:03.032104Z","iopub.execute_input":"2023-11-02T11:47:03.032631Z","iopub.status.idle":"2023-11-02T11:47:03.051640Z","shell.execute_reply.started":"2023-11-02T11:47:03.032559Z","shell.execute_reply":"2023-11-02T11:47:03.050306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_pdb(PDB2view_path):\n    view = py3Dmol.view(width=400, height=400)\n    view.addModel(get_pdb_lines(PDB2view_path),'pdb')\n    view.setStyle( {\"cartoon\": {'color': 'spectrum'}})       \n    view.zoomTo()\n    view.update()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T11:59:23.586562Z","iopub.execute_input":"2023-11-02T11:59:23.586995Z","iopub.status.idle":"2023-11-02T11:59:23.593986Z","shell.execute_reply.started":"2023-11-02T11:59:23.586963Z","shell.execute_reply":"2023-11-02T11:59:23.592803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_pdb(PDB2view)\nprint(PDB2view)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T11:59:25.919056Z","iopub.execute_input":"2023-11-02T11:59:25.919465Z","iopub.status.idle":"2023-11-02T11:59:25.956821Z","shell.execute_reply.started":"2023-11-02T11:59:25.919422Z","shell.execute_reply":"2023-11-02T11:59:25.955730Z"},"trusted":true},"execution_count":null,"outputs":[]}]}