The API allows to write all the files the command line tools can. This includes the outputs of PBassign. The functions to handle several file formats are available in the pbxplore.io
module.
In [1]:
from pprint import pprint
import urllib.request
import os
# print date & versions
import datetime
print("Date & time:",datetime.datetime.now())
import sys
print("Python version:", sys.version)
In [2]:
import pbxplore as pbx
print("PBxplore version:", pbx.__version__)
PBxplore allows two ways to write fasta files. The sequences can be written either all at once or one at a time. To write a batch of sequences at once, we need a list of sequences and a list of the corresponding sequence names. The writing function here is pbxplore.io.write_fasta()
.
In [3]:
names = []
pb_sequences = []
pdb_name, _ = urllib.request.urlretrieve('https://files.rcsb.org/view/2LFU.pdb', '2LFU.pdb')
for chain_name, chain in pbx.chains_from_files([pdb_name]):
dihedrals = chain.get_phi_psi_angles()
pb_seq = pbx.assign(dihedrals)
names.append(chain_name)
pb_sequences.append(pb_seq)
pprint(names)
pprint(pb_sequences)
with open('output.fasta', 'w') as outfile:
pbx.io.write_fasta(outfile, pb_sequences, names)
In [4]:
!cat output.fasta
!rm output.fasta
Sequences can be written once at a time using the pbxplore.io.write_fasta_entry()
function.
In [5]:
pdb_name, _ = urllib.request.urlretrieve('https://files.rcsb.org/view/2LFU.pdb', '2LFU.pdb')
with open('output.fasta', 'w') as outfile:
for chain_name, chain in pbx.chains_from_files([pdb_name]):
dihedrals = chain.get_phi_psi_angles()
pb_seq = pbx.assign(dihedrals)
pbx.io.write_fasta_entry(outfile, pb_seq, chain_name)
In [6]:
!cat output.fasta
!rm output.fasta
By default, the lines in fasta files are wrapped at 60 caracters as defined in pbxplore.io.fasta.FASTA_WIDTH
. Both pbxplore.io.write_fasta()
and pbxplore.io.write_fasta_entry()
have a width
optionnal argument that allows to control the wrapping.
In [7]:
print(pb_sequences[0])
In [8]:
with open('output.fasta', 'w') as outfile:
for width in (60, 70, 80):
pbx.io.write_fasta_entry(outfile, pb_sequences[0],
'width={} blocks'.format(width),
width=width)
In [9]:
!cat output.fasta
!rm output.fasta
In [10]:
pdb_name, _ = urllib.request.urlretrieve('https://files.rcsb.org/view/2LFU.pdb', '2LFU.pdb')
with open('output.phipsi', 'w') as outfile:
for chain_name, chain in pbx.chains_from_files([pdb_name]):
dihedral = chain.get_phi_psi_angles()
for res in sorted(dihedral):
phi = "{:8.2f}".format(dihedral[res]["phi"]) if dihedral[res]["phi"] else " None"
psi = "{:8.2f}".format(dihedral[res]["psi"]) if dihedral[res]["psi"] else " None"
print("{} {:6d} {} {} ".format(chain_name, res, phi, psi), file=outfile)
Note it's better to write the dihedral for each PDB/frame due to the high memory cost to store all of them in a list.
The output is formated with one line per residue. The first columns repeat the name given for the chain, then is the residue id followed by the phi and the psi angle. If an angle is not defined, 'None' is written instead.
In [11]:
!head output.phipsi
!tail output.phipsi
!rm output.phipsi
In [12]:
def pdb_to_fasta_pb(pdb_path, fasta_path):
"""
Write a fasta file with all the PB sequences from a PDB
"""
with open(fasta_path, 'w') as outfile:
for chain_name, chain in pbx.chains_from_files([pdb_path]):
dihedrals = chain.get_phi_psi_angles()
pb_seq = pbx.assign(dihedrals)
pbx.io.write_fasta_entry(outfile, pb_seq, chain_name)
# Write a fasta file
pdb_name, _ = urllib.request.urlretrieve('https://files.rcsb.org/view/2LFU.pdb', '2LFU.pdb')
pdb_to_fasta_pb(pdb_name, 'output.fasta')
# Read a list of headers and a list of sequences from a fasta file
names, sequences = pbx.io.read_fasta('output.fasta')
print('names:')
pprint(names)
print('sequences:')
pprint(sequences)
!rm output.fasta
If the sequences we want to read are spread amongst several fasta files, then we can use the pbxplore.io.read_several_fasta()
function that takes a list of fasta file path as argument instead of a single path.
In [13]:
# Write several fasta files
pdbname, _ = urllib.request.urlretrieve('https://files.rcsb.org/view/1BTA.pdb', '1BTA.pdb')
pdb_to_fasta_pb(pdbname, '1BTA.fasta')
pdbname, _ = urllib.request.urlretrieve('https://files.rcsb.org/view/2LFU.pdb', '2LFU.pdb')
pdb_to_fasta_pb(pdb_name, '2FLU.fasta')
# Read the fasta files
names, sequences = pbx.io.read_several_fasta(['1BTA.fasta', '2FLU.fasta'])
# Print the first entries
print('names:')
pprint(names[:5])
print('sequences:')
pprint(sequences[:5])
!rm 1BTA.fasta 2FLU.fasta