To modify the provided code to extract all interface residues and their pairs from a PDB file and then write these data to a CSV file, you can follow the below steps. Note, you need to have pdb2sql installed, which you seem to have already. Additionally, this example makes use of the csv module from Python's standard library to write data into a CSV file. Make sure you have the '1a02.pdb' file accessible in the same directory or provide an absolute path to it.
from pdb2sql import pdb2sql, interface
import csv
# Create an interface object from the pdb file
pdb_db = pdb2sql('1a02.pdb')
db = interface(pdb_db)
# Get interface residues and interface residues pairs
itf_residue = db.get_contact_residues(cutoff=3)
itf_residue_pair = db.get_contact_residues(cutoff=3, return_contact_pairs=True)
print("interface residue:\n", itf_residue)
print("interface residue pairs:\n", itf_residue_pair)
# Create and write data to a csv file
with open('interface_data.csv', mode='w', newline='') as file:
writer = csv.writer(file)
# Writing header
writer.writerow(["Residue ID 1", "Chain ID 1", "Residue Name 1", "Residue ID 2", "Chain ID 2", "Residue Name 2"])
# Writing residue data
for res_pair in itf_residue_pair:
res1 = res_pair[0]
res2 = res_pair[1]
writer.writerow([
res1[-1], # Residue ID of the first residue
res1[1], # Chain ID of the first residue
res1[0], # Residue Name of the first residue
res2[-1], # Residue ID of the second residue
res2[1], # Chain ID of the second residue
res2[0] # Residue Name of the second residue
])
print("Data has been written to 'interface_data.csv'")
Explanation:
- The
pdb2sql and interface classes from pdb2sql are used to interface with the PDB file.
- Function
get_contact_residues() with return_contact_pairs=True fetches the pairs of interface residues that are within the specified cutoff distance.
- In the example above, for the CSV writing:
- The CSV header includes columns for both residues in a pair: their IDs, chain IDs, and residue names.
- Loop through
itf_residue_pair to write each pair of residues into the CSV file.
- CSV file
'interface_data.csv' is created and populated with the interface residue data.
Please replace '1a02.pdb' with the correct path to your PDB file if it's not located in the current working directory. This code snippet assumes that the 'pdb2sql' module functions correctly and that the desired residue and residue pair information is obtainable through the mentioned methods.