I am looking for a Python package that performs pairwise structural alignment of protein structures (i.e., PDB files) and returns a sequence alignment. PyMOL is able to do this through the GUI, for example:
For two protein PDBs, one can be aligned to the other with the "align" menu or align
command:
With it, the sequences appear aligned in the sequence viewer according to the structural alignment, and an "alignment" object is created, which can be saved with the command:
save A_aligned_with_B.aln, aln_structure_1_to_structure_0
which produces the A_aligned_with_B.aln
alignment file in Clustal format (nevermind the ?s, which correspond to special HIS resnames):
CLUSTAL
structure_0 YIYQDSISLPWKVLLVMLLALITLATTLSNAFVIATVYRTRKL?TPANYLIASLAVTDLLVSI
structure_1 EEQGNKL?WAALLILMVIIPTIG-----GNTLVILAVSLEKKLQYATNYFLMSLAVADLLVGL
.*..**..*....**....**...****.****..
structure_0 LVMPISTMYTVT-GRWTLGQVVCDFWLSSDITCCTASIL?LCVIALDRYWAITDAVEYSAKRT
structure_1 FVMPIALLTIMFEAMWPLPLVLCPAWLFLDVLFSTASIM?LCAISVDRYIAIKKPIQANQYNS
.****...... ..*.*..*.*..**..*....****.***.*..***.**........ ..
structure_0 PKRAAVMIALVWVFSISISLPP-FFWRQAKAEEEVSE-CVVNTD?IL-YTVYSTVGAFYFPTL
structure_1 RATAFIKITVVWLISIGIAIPVPIKGIETDVDNPNNITCVLTKERFGDFMLFGSLAAFFTPLA
...*...*..**..**.*..* . **.... ........**..*..
structure_0 LLIALYGRIYVEARSRILKQTPNRTGKLLEKKKLMAAR--ERKATKTLGIILGAFIVCWLPFF
structure_1 IMIVTYFLTI?ALQKKAYLVKNKPPQRLTIGKKSVQTISNEQRASKVLGIVFFLFLLMWCPFF
..*..*.... * .*.*.***....*...*.***
structure_0 IISLVMPICKDACW--F?LAIFDFFTWLGYLNSLINPIIYTMSNEDFKQAF?KLIRFK--
structure_1 ITNITLVLCDS-CNQTTLQMLLEIFVWIGYVSSGVNPLVYTLFNKTFRDAFGRYITCNYR
*.......* * . .....*.*.**..*..**..**..*..*..**
I need a Python package that is easily installable/distributable that accepts two PDBs or protein structures and returns the structural alignment of the sequences somehow. All within a Python script, and not creating intermediate files, e.g. for the alignment.
There are plenty of Python tools that allow structural superimposition and RMSD calculation (e.g., Biopython), but I haven't found any that are able to return a sequence alignment from the structural alignment.
Edit:
Possible answer, thanks to the answer and comments from Matteo Ferla.
import pymol2
with pymol2.PyMOL() as pymol:
pymol.cmd.load('foo.pdb', 'foo')
pymol.cmd.load('bar.pdb', 'bar')
pymol.cmd.align('foo', 'bar', object='aln')
aln = pymol2.cmd2.pymol.exporting.get_alnstr('aln', _self=pymol.cmd) # I don't know if this line could be cleaner
# aln is itself a string