comparison ramachandran_plots.py @ 3:554f60da9c8f draft

planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
author chemteam
date Wed, 03 Apr 2019 15:47:36 -0400
parents
children d8178fe9aaff
comparison
equal deleted inserted replaced
2:2fff8653412f 3:554f60da9c8f
1 #!/usr/bin/env python
2
3 import argparse
4 import csv
5 import sys
6 from collections import namedtuple
7
8 import MDAnalysis as mda
9 from MDAnalysis.lib.distances import calc_dihedrals
10
11 import matplotlib
12 matplotlib.use('Agg') # noqa
13 import matplotlib.pyplot as plt
14
15 import numpy as np
16
17 import seaborn as sns
18
19
20 def parse_command_line(argv):
21 parser = argparse.ArgumentParser()
22 parser.add_argument('--idcd', help='input dcd')
23 parser.add_argument('--ipdb', help='input pdb')
24 parser.add_argument('--isegid1', help='segid 1')
25 parser.add_argument('--iresid1', help='resid 1')
26 parser.add_argument('--iname1', help='name 1')
27 parser.add_argument('--isegid2', help='segid 2')
28 parser.add_argument('--iresid2', help='resid 2')
29 parser.add_argument('--iname2', help='name 2')
30 parser.add_argument('--isegid3', help='segid 3')
31 parser.add_argument('--iresid3', help='resid 3')
32 parser.add_argument('--iname3', help='name 3')
33 parser.add_argument('--isegid4', help='segid 4')
34 parser.add_argument('--iresid4', help='resid 4')
35 parser.add_argument('--iname4', help='name 4')
36 parser.add_argument('--isegid5', help='segid 1')
37 parser.add_argument('--iresid5', help='resid 1')
38 parser.add_argument('--iname5', help='name 1')
39 parser.add_argument('--isegid6', help='segid 2')
40 parser.add_argument('--iresid6', help='resid 2')
41 parser.add_argument('--iname6', help='name 2')
42 parser.add_argument('--isegid7', help='segid 3')
43 parser.add_argument('--iresid7', help='resid 3')
44 parser.add_argument('--iname7', help='name 3')
45 parser.add_argument('--isegid8', help='segid 4')
46 parser.add_argument('--iresid8', help='resid 4')
47 parser.add_argument('--iname8', help='name 4')
48 parser.add_argument('--output', help='output')
49 parser.add_argument('--oramachandran_plot', help='dihedral plot')
50 return parser.parse_args()
51
52
53 args = parse_command_line(sys.argv)
54
55 Dihedral = namedtuple(
56 'Dihedral', ['atom1', 'atom2', 'atom3', 'atom4'])
57
58 # order of dihedral atom is the crystallographic definition
59 # (see glycanstructure.org)
60
61 # phi
62 atom1 = "(segid %s and resid %s and name %s)" % \
63 (args.isegid1, args.iresid1, args.iname1)
64 atom2 = "(segid %s and resid %s and name %s)" % \
65 (args.isegid2, args.iresid2, args.iname2)
66 atom3 = "(segid %s and resid %s and name %s)" % \
67 (args.isegid3, args.iresid3, args.iname3)
68 atom4 = "(segid %s and resid %s and name %s)" % \
69 (args.isegid4, args.iresid4, args.iname4)
70
71 dihe_phi = Dihedral(atom1, atom2, atom3, atom4)
72
73 # psi
74 atom1 = "(segid %s and resid %s and name %s)" % \
75 (args.isegid5, args.iresid5, args.iname5)
76 atom2 = "(segid %s and resid %s and name %s)" % \
77 (args.isegid6, args.iresid6, args.iname6)
78 atom3 = "(segid %s and resid %s and name %s)" % \
79 (args.isegid7, args.iresid7, args.iname7)
80 atom4 = "(segid %s and resid %s and name %s)" % \
81 (args.isegid8, args.iresid8, args.iname8)
82
83 dihe_psi = Dihedral(atom1, atom2, atom3, atom4)
84
85
86 def calc_torsion(dihedral):
87 """atom 1 -4 are valid atom selections. torsion in degrees is returned"""
88 A = u.select_atoms(dihedral.atom1).positions
89 B = u.select_atoms(dihedral.atom2).positions
90 C = u.select_atoms(dihedral.atom3).positions
91 D = u.select_atoms(dihedral.atom4).positions
92
93 dihe = calc_dihedrals(A, B, C, D)
94 return np.rad2deg(dihe)
95
96
97 u = mda.Universe(args.ipdb, args.idcd, topology_format="PDB", format="DCD")
98
99 phi_trajdata = np.array(
100 [(u.trajectory.frame, calc_torsion(dihe_phi)) for ts in u.trajectory])
101 psi_trajdata = np.array(
102 [(u.trajectory.frame, calc_torsion(dihe_psi)) for ts in u.trajectory])
103
104 phi_frame, phi_series = phi_trajdata.T
105 psi_frame, psi_series = psi_trajdata.T
106
107 phi_series = np.concatenate(phi_series, axis=0)
108 psi_series = np.concatenate(psi_series, axis=0)
109
110 zip(phi_frame, phi_series, psi_series)
111
112 with open(args.output, 'w') as f:
113 writer = csv.writer(f, delimiter='\t')
114 writer.writerows(zip(phi_frame, phi_series, psi_series))
115
116 with sns.axes_style("white"):
117 h = sns.jointplot(x=phi_series, y=psi_series, kind="kde", legend=True)
118 h.set_axis_labels(r'$\Phi$ (degrees)', r'$\Psi$ (degrees)')
119 h.ax_joint.set_xlim(-180, 180)
120 h.ax_joint.set_ylim(-180, 180)
121 plt.savefig(args.oramachandran_plot, format='png')