annotate distance_multiple.py @ 3:8bd0e29927da draft

"planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 7a25958195ccc8f448dd64ddcc36e8f5e5979d8b"
author chemteam
date Wed, 20 May 2020 13:03:30 -0400
parents b348dfa55e0a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
1 import argparse
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
2 import sys
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
3
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
4 import MDAnalysis as mda
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
5 from MDAnalysis.analysis import distances
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
6
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
7 import numpy as np
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
8
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
9
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
10 def parse_command_line(argv):
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
11 parser = argparse.ArgumentParser()
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
12 parser.add_argument('--itraj', help='input traj')
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
13 parser.add_argument('--istr', help='input str')
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
14 parser.add_argument('--itrajext', help='input traj ext')
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
15 parser.add_argument('--istrext', help='input str ext')
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
16 parser.add_argument('--list1', help='list 2')
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
17 parser.add_argument('--list2', help='list 2')
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
18 parser.add_argument('--output', help='output')
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
19 parser.add_argument('--header', dest='header', action='store_true')
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
20 return parser.parse_args()
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
21
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
22
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
23 args = parse_command_line(sys.argv)
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
24
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
25 u = mda.Universe(args.istr, args.itraj,
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
26 topology_format=args.istrext, format=args.itrajext)
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
27
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
28 list1 = np.loadtxt(args.list1, dtype=str, delimiter="\t", ndmin=1)
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
29 list2 = np.loadtxt(args.list2, dtype=str, delimiter="\t", ndmin=1)
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
30
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
31 sel1 = [u.select_atoms(selection) for selection in list1]
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
32 sel2 = [u.select_atoms(selection) for selection in list2]
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
33
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
34 d = np.empty((u.trajectory.n_frames, list1.shape[0], list2.shape[0]),)
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
35
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
36 for ts in u.trajectory:
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
37 c_o_m1 = np.array([selection.center_of_mass() for selection in sel1])
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
38 c_o_m2 = np.array([selection.center_of_mass() for selection in sel2])
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
39 distances.distance_array(c_o_m1, c_o_m2, result=d[ts.frame])
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
40
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
41 d = np.hstack((
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
42 np.array(np.reshape(np.arange(
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
43 0, d.shape[0]), (d.shape[0], 1)), dtype=int), # add column w frame
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
44 np.reshape(d, (d.shape[0], d.shape[1] * d.shape[2]))
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
45 ))
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
46
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
47 if args.header:
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
48 header = 'Frame\t' + '\t'.join(
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
49 ['-'.join(pair) for pair in zip(
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
50 sum([[n, ] * len(list2) for n in list1], []),
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
51 list(list2) * len(list1),)]).replace(' ', '_')
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
52 else:
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
53 header = ''
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
54
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
55 np.savetxt(args.output, d, header=header, comments='',
b348dfa55e0a "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 12823af06b7926cc56aaf9b59cfea9f16d342b8c"
chemteam
parents:
diff changeset
56 fmt=['%d'] + ['%f'] * (d.shape[1] - 1), delimiter='\t')