<jupyter_start><jupyter_text>Convert pdb using ProDy (failed..)<jupyter_code>from prody import parseMMCIF, parsePDB, writePDB
import numpy as np
def mmcif2pdb(pdb_id: str, chain: str, output_path: str = None):
    mol = parseMMCIF(pdb_id)
    chids = mol.getChids()
    sel_mol = mol.select('chain {}'.format(chain[: 2]))
    assert sel_mol is not None
    if output_path is None:
        output_path = pdb_id + '_' + chain + '.pdb'
    if len(chain) > 1:
        sel_mol.setChids(chain[0])
    writePDB(output_path, sel_mol)
    with open(output_path, 'r') as f:
        lines = f.readlines()
    with open(output_path, 'w') as f:
        def del_res_prefix(line):
            if line[: 4] == 'ATOM' or line[: 6] == 'HETATM':
                line_str_list = list(line)
                line_str_list[16] = ' '
                line = ''.join(line_str_list)
            return line

        lines = list(map(lambda line: del_res_prefix(line), lines))
        f.writelines(lines)
    return sel_mol, parsePDB(output_path)
# チェイン名が1文字の場合
pdb_id = '3j7p'
chain = 'A'
mmcif2pdb(pdb_id, chain)
# チェイン名が1文字の場合
pdb_id = '3j7p'
chain = 'S'
mmcif2pdb(pdb_id, chain)
mol = parseMMCIF('3j7p')
mol.select('chain 5')
# チェイン名が1文字の場合 かつ　数字の場合　バグがある
pdb_id = '3j7p'
chain = '5'
mmcif2pdb(pdb_id, chain)
# チェイン名が3文字だが，2文字までしかロードされていない．
mol = parseMMCIF('6zzr')
mol.getChids()
pdb_id = '6zzr'
chain = 'AAA'
sel_mol, pdb_mol = mmcif2pdb(pdb_id, chain)
pdb_id = '1vvj'
chain = 'QQ'
sel_mol, pdb_mol = mmcif2pdb(pdb_id, chain)
# チェイン名が2文字の場合
# チェイン名のせいで出力pdbの列がずれてしまうため，parseできない(修正済み)
mmcif2pdb('4v52', 'BC')
mmcif2pdb('3jai', 'SS')<jupyter_output>@> 226453 atoms and 1 coordinate set(s) were parsed in 2.16s.
@> 1139 atoms and 1 coordinate set(s) were parsed in 0.01s.<jupyter_text>Convert pdb using biopython<jupyter_code>from Bio.PDB import MMCIFParser, PDBParser, PDBIO, Select
parser = MMCIFParser()
structure = parser.get_structure('6zzr', '6zzr.cif')
structure.header
io = PDBIO()
io.set_structure(structure)
class ChainSelect(Select):
    def __init__(self, chain_name):
        self.chain_name = chain_name
    def accept_chain(self, chain):
        print(type(chain))
        if chain.get_id() == self.chain_name:
            return 1
        else:
            return 0
structure.get_chains()
io.save('6zzr_A_bio.pdb', ChainSelect('AAA'))
list(structure.get_chains())
import urllib

rcsb_download_url = 'https://files.rcsb.org/download/'

def download_file_from_url(url: str, output_path: str):
    urllib.request.urlretrieve(url, output_path)

def download_pdb(pdb_id: str, output_path: str = None):
    pdb_url = rcsb_download_url + pdb_id.upper() + '.pdb'
    if output_path is None:
        output_path = pdb_id.upper() + '.pdb'
    download_file_from_url(pdb_url, output_path)

def download_mmcif(pdb_id: str, output_path: str = None):
    mmcif_url = rcsb_download_url + pdb_id.upper() + '.cif.gz'
    if output_path is None:
        output_path = pdb_id.upper() + '.cif.gz'
    download_file_from_url(mmcif_url, output_path)
import urllib
import gzip
rcsb_download_url = 'https://files.rcsb.org/download/'

def download_and_decompress_file_from_url(url: str, output_path: str):
    urllib.request.urlretrieve(url, output_path)
    with gzip.open(output_path, 'rt') as f:
        lines = f.readlines()
    with open(output_path, 'w') as f:
        f.writelines(lines)

def download_mmcif(pdb_id,output_path = None):
    mmcif_url = rcsb_download_url + pdb_id.upper() + '.cif.gz'
    if output_path is None:
        output_path = pdb_id.upper() + '.cif'
    download_and_decompress_file_from_url(mmcif_url, output_path)
from Bio.PDB import MMCIFParser, PDBIO, Select, PDBExceptions
from prody import parsePDB
import warnings
warnings.simplefilter('ignore', PDBExceptions.PDBConstructionWarning)

class ChainSelect(Select):
    def __init__(self, chain_name):
        self.chain_name = chain_name

    def accept_chain(self, chain):
        print(type(chain))
        if chain.get_id() == self.chain_name:
            return 1
        else:
            return 0

import copy

def biopython_mmcif2pdb(pdb_id, chain):
    out_path = 'bio_' + pdb_id + '_' + chain + '.pdb'
    parser = MMCIFParser()
    structure = parser.get_structure("", pdb_id + '.cif')
    sel_structure = structure[0][chain]
    if len(chain) > 1:
        if chain[0] in [chain.id for chain in structure.get_chains()]:
            structure[0][chain[0]].id = None
        sel_structure.id = chain[0]
    io = PDBIO()
    io.set_structure(sel_structure)
    io.save(out_path)
    return parsePDB(out_path)
type(structure)
type(structure[0])
parser = MMCIFParser()
structure = parser.get_structure('', '5MYJ.cif')
download_mmcif('5MYJ')
biopython_mmcif2pdb('5MYJ', 'AQ')
download_mmcif('6V39')
biopython_mmcif2pdb('6V39', 'q')
mol = biopython_mmcif2pdb('1VVJ', 'QQ')
path = 'bio_1VVJ_QQ.pdb'
parsePDB(path, chain='Q')
def biopython_parseMMCIF(pdb_id):
    parser = MMCIFParser()
    structure = parser.get_structure(pdb_id, pdb_id + '.cif')
    return structure
structure = biopython_parseMMCIF('6zzr')
sel_structure = structure[0]['AAA']
biopython_mmcif2pdb('6zzr', 'AAA')
structure = biopython_parseMMCIF('3j7p')
biopython_mmcif2pdb('3j7p', 'S')
biopython_mmcif2pdb('3j7p', '5')<jupyter_output>/Users/TAKEI/miniforge3/envs/mypython/lib/python3.9/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain 5 is discontinuous at line 221516.
  warnings.warn(
/Users/TAKEI/miniforge3/envs/mypython/lib/python3.9/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain 7 is discontinuous at line 221634.
  warnings.warn(
/Users/TAKEI/miniforge3/envs/mypython/lib/python3.9/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain 8 is discontinuous at line 221639.
  warnings.warn(
/Users/TAKEI/miniforge3/envs/mypython/lib/python3.9/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain P is discontinuous at line 221643.
  warnings.warn(
/Users/TAKEI/miniforge3/envs/mypython/lib/python3.9/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain V is discontinuous at line 221644.
  warnings.warn(
/Users/TAKEI/miniforge3/envs/mypython/lib/python3.9/sit[...]
{'fraction_non_alphanumeric': 0.12649736463823671, 'fraction_numerical': 0.02539530426449449, 'mean_word_length': 4.192371475953565, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '562922', 'license': 'MIT', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2455, 'n_words': 463, 'path': 'src/test/mmcif2pdb/mmcif2pdb.ipynb', 'repo_name': 'yutake27/HMDM', 'size': '6261'}