OK using PDBx Python Parser and .cif of 1ffk , 1w2b , 3cxc and 1ffM.cif
that is modded 1fkk where first row reads:
data_1FFM
#
_entry.id 1FFM
and modded _entity_name_com
loop :
loop_
_entity_name_com.entity_id
_entity_name_com.name
3 '50S RIBOSOMAL PROTEIN L2P, HMAL2, HL4'
4 '50S RIBOSOMAL PROTEIN L3P, HMAL3, HL1'
5 '50S RIBOSOMAL PROTEIN L4E, HMAL4, HL6'
6 '50S RIBOSOMAL PROTEIN L5P, HMAL5, HL13'
7 '30S RIBOSOMAL PROTEIN HS6'
9 '50S RIBOSOMAL PROTEIN L13P, HMAL13'
10 '50S RIBOSOMAL PROTEIN L14P, HMAL14, HL27'
12 '50S RIBOSOMAL PROTEIN L15P, HMAL15, HL9'
13 '50S RIBOSOMAL PROTEIN L18P, HMAL18, HL12'
14 '50S RIBOSOMAL PROTEIN L18E, HL29, L19'
15 '50S RIBOSOMAL PROTEIN L19E, HMAL19, HL24'
16 '50S RIBOSOMAL PROTEIN L21E, HLXX'
17 '50S RIBOSOMAL PROTEIN L22P, HMALXX, HL23'
18 '50S RIBOSOMAL PROTEIN L23P, HMAL23, HL25, L21'
19 '50S RIBOSOMAL PROTEIN L24P, HMAL24, HL16, HL15'
20 '50S RIBOSOMAL PROTEIN L24E, HL21/HL22'
21 '50S RIBOSOMAL PROTEIN L29P, HMAL29, HL33'
22 '50S RIBOSOMAL PROTEIN L30P, HMAL30, HL20, HL16'
23 '50S RIBOSOMAL PROTEIN L31E, L34, HL30'
24 '50S RIBOSOMAL PROTEIN L32E, HL5'
26 '50S RIBOSOMAL PROTEIN L37E, L35E'
27 '50S RIBOSOMAL PROTEINS L39E, HL39E, HL46E'
28 '50S RIBOSOMAL PROTEIN L44E, LA, HLA'
29 '50S RIBOSOMAL PROTEIN L6P, HMAL6, HL10'
#
where I changen protein name with X:
16 '50S RIBOSOMAL PROTEIN L21E, HLXX'
17 '50S RIBOSOMAL PROTEIN L22P, HMALXX, HL23'
Using code:
import pdbx
from pdbx.reader.PdbxReader import PdbxReader
from pdbx.reader.PdbxContainers import *
print('PDBx version : ', pdbx.__version__)
import pandas as pd
# import xlsxwriter
pdbs = ['1ffk', '1w2b', '3cxc', '1ffM']
pdbs_dict = {}
for pdb in pdbs:
try:
with open( './' + pdb + '.cif') as open_cif:
cif_parser = PdbxReader(open_cif)
cif_data = []
try :
cif_parser.read(cif_data)
except Exception as exc:
print('\n\nERROR !!! while PDBxReader reads data of :', pdb,' ', exc)
for block in cif_data:
if block.getName() == pdb.upper():
print('\n\n', block.getObj('entry').getValue('id'),('\n\n_____________________'))
print(block)
entities = [l[0] for l in block.getObj('entity_poly').getRowList() if l[1] == 'polypeptide(L)']
print('entities :\n', entities)
try :
entities_names = []
for i in entities :
print('\n\n', i , type(i),'\n')
# print(block.getObj('entity_name_com').getRowList())
name = [l[1] for l in block.getObj('entity_name_com').getRowList() if l[0] == i]
print('1n1nname :\n', name, type(name))
if name :
entities_names.append(name[0])
pdbs_dict[pdb] = [len(entities_names), entities_names]
except Exception as exc:
print("\n\nERROR !!! while reetriving 'entity_name_com' of :", pdb,' ', exc)
else:
print("\n\nERROR !!! while reetriving block.getName() of :", pdb)
except :
print('something went wrong with instatiating PdbxReader for : ' , pdb)
print('\n\npdbs_dict : \n', pdbs_dict)
for i, value in pdbs_dict.items():
print(i , value[0], len(value[1]))
header = ['PDB Accession No.'] + [i for i, value in pdbs_dict.items()]
first_row = ['No. of unique proteins'] + [value[0] for i, value in pdbs_dict.items()]
## correct 2 linee
# unique_proteins_unflatten = [value[1] for i, value in pdbs_dict.items()]
# unique_proteins = [[x for xs in unique_proteins_unflatten for x in xs]]
## correct 1 linea
# unique_proteins = [x for xs in [value[1] for i, value in pdbs_dict.items()] for x in xs]
# unique_proteins_set = set(unique_proteins)
# unique_proteins_list = list(unique_proteins_set)
unique_proteins_list = list(set([x for xs in [value[1] for i, value in pdbs_dict.items()] for x in xs]))
print('\n\nheader : ' , header)
print('\n\nfirst_row :', first_row)
# print('\n\nunique_proteins', unique_proteins , len(unique_proteins))
# print('\n\nunique_proteins_set', unique_proteins_set, len(unique_proteins_set))
print('\n\nunique_proteins_list', unique_proteins_list, len(unique_proteins_list))
other_rows = []
for i in unique_proteins_list :
row = [i]
for pdb, value in pdbs_dict.items() :
if i in value[1] :
row.append('X')
else:
row.append('/')
other_rows.append(row)
print('\n\n-------------------------')
for row in other_rows:
print(row)
data = [first_row] + other_rows
print('\n\ndata : ', data , len(data))
df = pd.DataFrame(data, columns = header)
writer = pd.ExcelWriter('file.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index = False)
#https://stackoverflow.com/questions/69263078/pandas-dataframe-to-excel-cell-alignment
def align_center(x):
return ['text-align: center' for x in x]
df.style.apply(align_center, axis=0).to_excel(
writer,
index=False,
header=True
)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
## https://stackoverflow.com/questions/17326973/is-there-a-way-to-auto-adjust-excel-column-widths-with-pandas-excelwriter
#Iterate through each column and set the width == the max length in that column. A padding length of 2 is also added.
for i, col in enumerate(df.columns):
# find length of column i
column_len = df[col].astype(str).str.len().max()
# Setting the length if the column header is larger
# than the max column value length
column_len = max(column_len, len(col)) + 2
# set the column length
worksheet.set_column(i, i, column_len)
writer.save()
Sorry for the number of prints, but needed to debug, I get the table below
just check it to be sure it is what you where looking for
NOTES :
In the table No. of unique proteins means that I kept count of the names of proteins, not the amount of time the same protein is present in a complex (that I think would be difficult to parse, think you'll have to count it from the number of chains in which the proteis is present, check the dictionary : Dictionary Index mmcif_pdbx.dic).
I just used the first big complexes I got from a fast search on RCSB Protein Data Bank (RCSB PDB).
Countercheck my code, was the first time I tried this kinf of stuff, probably there are faster/better/cleaniest eway to get the risult, just use it as a blueprint.
Could be that Parser I am using is ostrokach/pdbx its on pypi too pdbx-mmcif 0.0.2 , but to be used with latest Python had to change :
PdbxReader.py
line 360 to :
# Tokenizer loop begins here ---
while True:
try:
line = next(fileIter)
self.__curLineNumber += 1
except:
return
not sure if it is because of PEP 479.
3CXC is skipped by my code because _entity_name_com.name
loop_ is missing in its .cif
file, not sure how the RCSB Protein Data Bank (RCSB PDB) is mantained so probably you'll have to keep it in mind or user other Categories.
I am not sure about univocity of protein names in RCSB Protein Data Bank (RCSB PDB) as shown by 1W2B.cis
complex that is still a 50S ribosomal subunit of H. marismortui
, see table results.