annotate pca_cosine.py @ 3:ad135cf42274 draft

planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
author chemteam
date Wed, 03 Apr 2019 15:47:56 -0400
parents
children e7d0075052c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
1 #!/usr/bin/env python
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
2
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
3 import argparse
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
4 import csv
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
5 import sys
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
6
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
7 import MDAnalysis as mda
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
8 import MDAnalysis.analysis.pca as pca
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
9
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
10 import numpy as np
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
11
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
12
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
13 def parse_command_line(argv):
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
14 parser = argparse.ArgumentParser()
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
15 parser.add_argument('--idcd', help='input dcd')
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
16 parser.add_argument('--ipdb', help='input pdb')
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
17 parser.add_argument('--icomponents', help='number of principle components')
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
18 parser.add_argument('--iindex', help='index of the PC')
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
19 parser.add_argument('--output', help='output')
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
20 parser.add_argument('--cosout', help='cosine output')
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
21 return parser.parse_args()
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
22
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
23
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
24 args = parse_command_line(sys.argv)
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
25
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
26 u = mda.Universe(args.ipdb, args.idcd, topology_format="PDB", format="DCD")
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
27
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
28 components = int(args.icomponents)
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
29 pca_index = int(args.iindex)
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
30
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
31 PSF_pca = pca.PCA(u, select='backbone')
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
32 PSF_pca.run()
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
33 n_pcs = np.where(PSF_pca.cumulated_variance > 0.95)[0][0]
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
34 atomgroup = u.select_atoms('backbone')
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
35
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
36 pca_space = PSF_pca.transform(atomgroup, n_components=components)
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
37 cosine = mda.analysis.pca.cosine_content(pca_space, pca_index)
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
38
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
39 PCA = list(pca_space)
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
40
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
41 with open(args.output, 'w') as f:
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
42 writer = csv.writer(f, delimiter='\t')
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
43 writer.writerows(PCA)
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
44
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
45 with open(args.cosout, 'w') as f1:
ad135cf42274 planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/ commit 3ff06e3182c3a1546ea0a3b29e0d4383e12169e1
chemteam
parents:
diff changeset
46 f1.write(str(cosine))