1+ from pymol import cmd
2+ from io import StringIO
3+
4+ try :
5+ from Bio .SeqUtils .ProtParam import ProteinAnalysis
6+ from Bio import SeqIO
7+ from Bio .Seq import Seq
8+ except ModuleNotFoundError :
9+ # Note that Bio package might be missing from Pymol 2 installation!
10+ print ("Oops! Protparam: Biopython is missing!\n If you want to install it, run protparam_dependencies_install command" )
11+
12+
13+ @cmd .extend
14+ def protparam (selection = 'enabled' , bychain = 0 ):
15+ '''
16+ DESCRIPTION:
17+ Given selection, calculates common protein properties, like Mw, pI, length and aminoacid content.
18+ By default, combines all chains of each object into the single sequence.
19+
20+ USAGE:
21+ protparam selection, [bychain]
22+
23+ DEPENDENCIES:
24+ biopython
25+ '''
26+ #TODO: add pretty output suitable for copy-pasting
27+ for entry in cmd .get_object_list (selection ):
28+ sequence_obj = cmd .get_fastastr (f"({ selection } ) and { entry } " )
29+ fasta_io = StringIO (sequence_obj )
30+ sequences = list (SeqIO .parse (fasta_io , "fasta" ))
31+ sequences = [s .seq for s in sequences ]
32+ if not bychain :
33+ #by default combine all chains into single sequence
34+ sequences = [Seq ('' ).join (sequences )]
35+ for sequence in sequences :
36+ sequence = str (sequence ).replace ('?' ,'' ).strip ()
37+ analysis = ProteinAnalysis (sequence )
38+ counts_aa = analysis .count_amino_acids () #Dict is useful when only specific residues should be reported
39+ print (f"Protein name: { entry } " )
40+ print (f"Sequence: { sequence } " )
41+ print (f"\n Protein length: { analysis .length } aa" )
42+ print (f"Molecular Weight: { analysis .molecular_weight ():.1f} Da" )
43+ print (f"Isoelectric point: { analysis .isoelectric_point ():.2f} " )
44+ print (f"Count of aminoacids: { counts_aa } \n \n " )
45+
46+ @cmd .extend
47+ def protparam_dependencies_install ():
48+ import sys
49+ import subprocess
50+ try :
51+ subprocess .check_call ([sys .executable , "-m" , "pip" , "install" , 'biopython' ])
52+ print (f"Successfully installed biopython! Reload Protparam plugin or restart PyMOL." )
53+ except subprocess .CalledProcessError as e :
54+ print (f"Failed to install biopython: { e } " )
0 commit comments