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:

PyMOL GUI showing two protein structures and their sequences

For two protein PDBs, one can be aligned to the other with the "align" menu or align command:

PyMOL GUI showing the two protein structures superimposed and their sequences aligned position by position (and with gaps)

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):



             .****......  ..*.*..*.*..**..*....****.***.*..***.**........ ..

             ...*...*..**..**.*..*  .              **....    ........**..*..

             ..*..*....                              * .*.*.***....*...*.***

             *.......*   *   .  .....*.*.**..*..**..**..*..*..**         

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.


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
  • 1
    $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – Community Bot
    Commented May 23, 2022 at 17:56
  • $\begingroup$ Please provide more details to help us in answering your question. For example, showing the alignment that you would like to see from Chimera or PyMOL. Showing the first few lines of any input files is also helpful (or, at least, providing two demonstrative PDB IDs that you are interested in). $\endgroup$
    – gringer
    Commented May 24, 2022 at 3:58
  • $\begingroup$ This was asked before here —I know because I answered it. You want the primary sequence alignment based on PyMOL's superimposition of tertiary structure. $\endgroup$ Commented May 24, 2022 at 9:06
  • $\begingroup$ Ignore my comment. I saw your Q was just updated... $\endgroup$ Commented May 24, 2022 at 9:07
  • $\begingroup$ "align" performs a sequence alignment followed by a structural superposition, and then carries out zero or more cycles of refinement in order to reject structural outliers found during the fit. "align" does a good job on proteins with decent sequence similarity (identity >30%). For comparing proteins with lower sequence identity, the "super" and "cealign" commands perform better. $\endgroup$
    – pippo1980
    Commented Jun 26, 2023 at 9:48

1 Answer 1


PyMOL in itself can be used as python package. The GUI app has its own Python, but in a conda environment you can install it as a package. Any PyMOL app command can be used as a cmd command. In PyMOL Wiki you see both. They follow a similar pattern and actually have more functionality.

import pymol2
with pymol2.PYMOL() as pymol:
    pymol.cmd.load('foo.pdb', 'foo')
    pymol.cmd.load('bar.pdb', 'bar')
    pymol.cmd.align('foo', 'bar')

For more discussion of how the alignment output object is used see Assessing PyMol sequence alignment object

  • $\begingroup$ Hi! Thanks so much for your answer. Sorry it took me so long to say anything, I was testing all the options. Also, I'm sorry I don't have enough reputation to upvote your answer. Is there any way I could save the alignment in a Python text stream, as in an io.StringIO() stream? So that I don't save any file in the process. I made some attempts but I couldn't hack it and I didn't see anything related in your previous answer that you linked. Thanks in advance! $\endgroup$ Commented Jun 27, 2022 at 10:40
  • $\begingroup$ Nah, the important thing is that marked as solved, thus helping the healthiness of the site, which struggled to leave beta because of high unsolved cases —so thanks! I don't know off the top of my head, but docs.python.org/3/library/tempfile.html may be of use. There is likely a function that is not imported by the top of cmd module, but is present in one of the submodules. Maybe one of the searches in the open-source PyMOL repo might help github.com/schrodinger/pymol-open-source/search?q=.aln (sorry I'm tight for time atm) $\endgroup$ Commented Jun 27, 2022 at 14:21
  • $\begingroup$ Thanks for the pointers! I finally found out how to retrieve the alignment string, I'm going to edit my question to put the final answer in it. $\endgroup$ Commented Jun 27, 2022 at 14:41
  • 1
    $\begingroup$ Hi @pippo1980 maybe a separate question? Generally not advised, you could use venv and pip, but historically there were compatibility issues between conda and pip (probably resolved) $\endgroup$
    – M__
    Commented Jun 27, 2022 at 16:16
  • 1
    $\begingroup$ @pippo1980 The classic solution was installing the OS pymol —which is not installed via conda but with wizardry and the deprecated python setup.py install (not pip install .) github.com/schrodinger/pymol-open-source/blob/master/INSTALL —alternatively, there's always the nightmarish über-hack of copypasting of site-packages folders from a conda installation to a vanilla Python... But that's a big no no due to irreproducibility. $\endgroup$ Commented Jun 27, 2022 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.