Skip to content

Commit

Permalink
cif2pdb fancy way
Browse files Browse the repository at this point in the history
  • Loading branch information
mmagnus committed Sep 26, 2024
1 parent 34f3b6f commit faf6373
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions rna_tools/rna_pdb_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,12 +1219,24 @@ def get_parser():
structure = parser.get_structure("structure_id", cif_file)
pdb_file = cif_file.replace('.cif', '_fCIF.pdb')

remarks = []

try:
# Save to PDB format
io = PDBIO()
io.set_structure(structure)
io.save(pdb_file)

print(f'saved: {pdb_file}')
# open a file add remarks
new_file = ''
with open(pdb_file, 'r') as f:
if not args.no_hr:
new_file += add_header(version) + '\n'
new_file += f.read()

with open(pdb_file, 'w') as f:
f.write(new_file)

except TypeError as e:
print('Warning: some of the chains in this mmCIF file has chain names with more char than 1, e.g. AB, and the PDB format needs single-letter code, e.g. A.')

Expand Down Expand Up @@ -1262,43 +1274,46 @@ def has_high_rna_content(chain, threshold=0.8):
import string
letters = list(string.ascii_uppercase)

# New structure
new_structure = Structure.Structure("new_structure")
new_model = Model.Model(0) # Create a new model
new_structure.add(new_model) # Add the new model to the new structure

for model in structure:
for chain in model:
if has_high_rna_content(chain):
# New structure
new_structure = Structure.Structure("new_structure")
new_model = Model.Model(0) # Create a new model
new_structure.add(new_model) # Add the new model to the new structure

chain_id_new = letters.pop(0)
chain_id = chain.get_id()

atom_count = 0
for residue in chain:
for atom in residue:
atom_count += 1
print(f'rna chain {chain.id} -> {chain_id_new} # of atoms: {atom_count}')

remarks = []
remarks.append(f'REMARK rna chain {chain.id} -> {chain_id_new}')

chain.id = chain_id_new
new_model.add(chain)

io = PDBIO()
io.set_structure(new_structure)
io.save(pdb_file)

print(f'saved: {pdb_file}')

# open a file add remarks
new_file = ''
with open(pdb_file, 'r') as f:
if not args.no_hr:
new_file += add_header(version) + '\n'
if remarks:
new_file += '\n'.join(remarks) + '\n'
new_file += f.read()

with open(pdb_file, 'w') as f:
f.write(new_file)
pdb_file = cif_file.replace('.cif', f'_{chain_id}_n{chain_id_new}_fCIF.pdb')

io = PDBIO()
io.set_structure(new_structure)

io.save(pdb_file)
print(f'rna chain {chain.id} -> {chain_id_new} # of atoms: {atom_count} {pdb_file}')
# open a file add remarks
new_file = ''
with open(pdb_file, 'r') as f:
if not args.no_hr:
new_file += add_header(version) + '\n'
if remarks:
new_file += '\n'.join(remarks) + '\n'
new_file += f.read()

with open(pdb_file, 'w') as f:
f.write(new_file)

if args.pdb2cif:
try:
Expand Down

0 comments on commit faf6373

Please sign in to comment.