Repository navigation
Expand file tree
/
Copy pathdimensionality_reduction_functions.py
More file actions
1725 lines (1330 loc) · 79.8 KB
/
Copy pathdimensionality_reduction_functions.py
File metadata and controls
1725 lines (1330 loc) · 79.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import calculate_rmsd as rmsd
import pandas as pd
import math
import glob
import os
import sys
import ntpath
import plotting_functions
from periodictable import *
from sklearn import *
from sympy import solve, Symbol
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
def read_xyz_file(path):
""" Reads in an xyz file from path as a DataFrame. This DataFrame is then turned into a 3D array such that the
dimensions are (number of points) X (number of atoms) X 3 (Cartesian coordinates). The system name (based on the
filename), list of atoms in the system, and Cartesian coordinates are output.
:param path: path to xyz file to be read
:return extensionless_system_name: str
atom_list: numpy array
cartesians: numpy array
"""
system_name = path_leaf(path)
print("File being read is: %s" % system_name)
extensionless_system_name = os.path.splitext(system_name)[0]
data = pd.read_csv(path, header=None, delim_whitespace=True, names=['atom', 'X', 'Y', 'Z'])
n_atoms = int(data.loc[0][0])
n_lines_per_frame = int(n_atoms + 2)
data_array = np.array(data)
data_reshape = np.reshape(data_array, (int(data_array.shape[0]/n_lines_per_frame), n_lines_per_frame,
data_array.shape[1]))
cartesians = data_reshape[:, 2::, 1::].astype(np.float)
atom_list = data_reshape[0, 2::, 0]
return extensionless_system_name, atom_list, cartesians
def remove_atoms_by_type(atom_types_to_remove, atom_list, cartesians):
"""
Removes specific atoms if they are not wanted for PCA
:param atom_list: list of atoms in the structure
:param cartesians: cartesian coordinates of each frame
:return: cartesian coordinates of each frame with specific atom types removed
"""
matches_indexes = [i for i, x in enumerate(atom_list) if x in atom_types_to_remove]
cartesians_sans_atoms = np.delete(cartesians, list(matches_indexes), axis=1)
atom_list_sans_atoms = np.delete(atom_list, list(matches_indexes), axis=0)
return atom_list_sans_atoms, cartesians_sans_atoms
def calculate_velocities(cartesians, timestep=1):
"""
Calculate velocities at each timestep given Cartesian coordinates. Velocities at the first and last point are
extrapolated.
:param cartesians: Cartesian coordinates along trajectory
:param timestep: time step between frames in units of fs, default=1
:return: velocities
"""
velocities = []
for i in range(0, len(cartesians)):
if i == 0:
velocity = (cartesians[i + 1] - cartesians[i]) / timestep
elif i == len(cartesians)-1:
velocity = (cartesians[i] - cartesians[i - 1]) / timestep
else:
velocity = (cartesians[i + 1] - cartesians[i - 1])/2*timestep
velocities.append(velocity)
return velocities
def calculate_momenta(velocities, atoms):
"""
:param cartesians: Cartesian coordinates along trajectory
:param timestep: time step between frames in units of fs, default=1
:return: velocities
"""
velocities = np.array(velocities)
atoms = np.array(atoms)
atom_masses = np.array([formula(atom).mass for atom in atoms])
momenta = velocities * atom_masses[np.newaxis, :, np.newaxis]
return momenta
def set_atom_one_to_origin(coordinates):
coordinates_shifted = coordinates - coordinates[:, np.newaxis, 0]
return coordinates_shifted
def mass_weighting(atoms, cartesians):
cartesians = np.array(cartesians)
atoms = np.array(atoms)
atom_masses = [formula(atom).mass for atom in atoms]
weighting = np.sqrt(atom_masses)
mass_weighted_cartesians = cartesians * weighting[np.newaxis, :, np.newaxis]
return mass_weighted_cartesians
def remove_mass_weighting(atoms, coordinates):
coordinates = np.array(coordinates)
atoms = np.array(atoms)
atom_masses = [formula(atom).mass for atom in atoms]
weighting = np.sqrt(atom_masses)
unmass_weighted_coords = coordinates / weighting[np.newaxis, :, np.newaxis]
return unmass_weighted_coords
def generate_distance_matrices(coordinates):
""" Generates distance matrices for each structure.
"""
coordinates = np.array(coordinates)
d2 = np.sum((coordinates[:, :, None] - coordinates[:, None, :]) ** 2, axis=3)
return d2
def generate_dihedral_matrices(coordinates):
return coordinates
def generate_and_reshape_ds_big_structures(coordinates):
""" Generates matrix of pairwise distances, which includes pairwise distances for each structure.
:param coordinates:
"""
coordinates = np.array(coordinates)
atoms = int(coordinates.shape[1])
d_re = np.zeros((coordinates.shape[0], int(atoms*(atoms-1)/2)))
for i in range(coordinates.shape[0]):
d2 = np.square(metrics.pairwise.euclidean_distances(coordinates[i]))
x = d2[0].shape[0]
dint_re = d2[np.triu_indices(x, k=1)]
d_re[i] = dint_re
return d_re
def reshape_ds(d):
""" Takes only the upper triangle of the distance matrices and reshapes them into 1D arrays.
"""
d_re = []
x = d[0][0].shape[0]
for dint in d:
dint_re = dint[np.triu_indices(x, k=1)]
d_re.append(dint_re)
d_re = np.asarray(d_re)
return d_re
def vector_to_matrix(v):
""" Converts a representation from 1D vector to 2D square matrix. Slightly altered from rmsd package to disregard
zeroes along diagonal of matrix.
:param v: 1D input representation.
:type v: numpy array
:return: Square matrix representation.
:rtype: numpy array
"""
if not (np.sqrt(8 * v.shape[0] + 1) == int(np.sqrt(8 * v.shape[0] + 1))):
print("ERROR: Can not make a square matrix.")
exit(1)
n = v.shape[0]
w = ((-1 + int(np.sqrt(8 * n + 1))) // 2) + 1
m = np.zeros((w, w))
index = 0
for i in range(w):
for j in range(w):
if i > j - 1:
continue
m[i, j] = v[index]
m[j, i] = m[i, j]
index += 1
return m
def distance_matrix_to_coords(v):
""" Converts a (2D square) distance matrix representation of a structure to Cartesian coordinates (first 3 columns
correspond to 3D xyz coordinates) via a Gram matrix.
:param v: 1D vector, numpy array
:return: 3D Cartesian coordinates, numpy array
"""
d = vector_to_matrix(v)
d_one = np.reshape(d[:, 0], (d.shape[0], 1))
m = (-0.5) * (d - np.matmul(np.ones((d.shape[0], 1)), np.transpose(d_one)) - np.matmul(d_one,
np.ones((1, d.shape[0]))))
values, vectors = np.linalg.eig(m)
idx = values.argsort()[::-1]
values = values[idx]
vectors = vectors[:, idx]
assert np.allclose(np.dot(m, vectors), values * vectors)
coords = np.dot(vectors, np.diag(np.sqrt(values)))
# Only taking first three columns as Cartesian (xyz) coordinates
coords = np.asarray(coords[:, 0:3])
return coords
def pca_dr(matrix):
"""
Does PCA on input matrix with specified number of dimensions. Outputs information used to later generate xyz files
in the reduced dimensional space and also for the function that filters out distances between key atoms and their
neighbors.
:param matrix: array
:return: matrix_pca: input data (in matrix) projected onto covariance matrix eigenvectors (PCs)
matrix_pca_fit: fit used to transform new data into reduced dimensional space
pca.components_: eigenvectors of covariance matrix
pca.mean_: mean of the original dataset (in matrix)
pca.explained_variance_: amount of variance described by each PC
"""
matrix = pd.DataFrame(matrix)
pca = decomposition.PCA()
matrix_pca_fit = pca.fit(matrix)
matrix_pca = pca.transform(matrix)
return matrix_pca, matrix_pca_fit, pca.components_, pca.mean_, pca.explained_variance_
#TODO: Add function that is able to do LDA on data rather than PCA
def lda_dr(matrix, data_labels):
"""
Does LDA (Linear Discriminant Analysis) on input matrix with specified number of dimensions. Outputs information
used to later generate xyz files in the reduced dimensional space and also for the function that filters out
distances between key atoms and their neighbors.
:param matrix: array
:param data_labels: array, separates data into classes to differentiate
:return: matrix_lda: input data (in matrix) projected onto covariance matrix eigenvectors (PCs)
matrix_lda_fit: fit used to transform new data into reduced dimensional space
lda.coef_: weight vectors
lda.means_: means of each class of the original dataset
lda.explained_variance_ratio_: amount of variance described by each PC
"""
matrix = pd.DataFrame(matrix)
lda = discriminant_analysis.LinearDiscriminantAnalysis()
matrix_lda_fit = lda.fit(matrix, data_labels)
matrix_lda = lda.transform(matrix)
return matrix_lda, matrix_lda_fit, lda.coef_, lda.means_, lda.explained_variance_ratio_
def calc_mean_distance_vector(d2_matrix):
return np.mean(d2_matrix, axis=0)
def filter_important_distances(upper_tri_d2_matrices, num_dists=75000):
num_points = upper_tri_d2_matrices.shape[0]
vec_length = upper_tri_d2_matrices.shape[1]
num_atoms = calc_num_atoms(vec_length)
variances = []
atom_indexes = {}
for k in range(vec_length):
variances.append(np.var(upper_tri_d2_matrices[:, k]))
atom1, atom2 = calc_ij(k, num_atoms)
atom_indexes[k] = atom1, atom2
important_distances_matrix = np.zeros((num_points, num_dists))
top_vars_indexes = top_values_indexes(variances, num_dists)
i = 0
selected_dist_atom_indexes = {}
for index in top_vars_indexes:
important_distances_matrix[:, i] = upper_tri_d2_matrices[:, index]
selected_dist_atom_indexes[i] = atom_indexes[index], index
i += 1
# print(selected_dist_atom_indexes)
return important_distances_matrix, selected_dist_atom_indexes
def calc_num_atoms(vec_length):
"""
Calculates number of atoms in a system based on the length of the vector generated by flattening the upper triangle
of its interatomic distance matrix.
:param vec_length: length of interatomic distance matrix vector
:return: num_atoms: int, number of atoms in the system
"""
n = Symbol('n', positive=True)
answers = solve(n * (n - 1) / 2 - vec_length, n)
num_atoms = int(answers[0])
return num_atoms
def set_unimportant_distance_weights_to_zero(components, selected_dist_atom_indexes, num_atoms):
num_dists = int((num_atoms*(num_atoms-1))/2)
num_points = components.shape[0]
components_all_distances = np.zeros((num_points, num_dists))
distance_vector_indexes = list(pd.DataFrame(list(selected_dist_atom_indexes.values()))[1])
for i in range(len(distance_vector_indexes)):
components_all_distances[:, distance_vector_indexes[i]] = components[:, i]
return components_all_distances
def generate_PC_matrices_selected_distances(n_dim, matrix_reduced, components, mean, selected_dist_atom_indexes,
num_atoms):
num_points = matrix_reduced.shape[0]
num_dists = int((num_atoms*(num_atoms-1))/2)
PCs_separate = []
for i in range(0, n_dim):
PCi = np.zeros((num_points, num_dists))
PCi_selected = np.dot(matrix_reduced[:, i, None], components[None, i, :]) + mean
for j in range(len(selected_dist_atom_indexes)):
distance_location = selected_dist_atom_indexes[j][1]
PCi[:, distance_location] = PCi_selected[:, j]
PCs_separate.append(PCi)
PCs_combined = np.zeros((num_points, num_dists))
PCs_combined_selected = np.dot(matrix_reduced, components) + mean
for j in range(len(selected_dist_atom_indexes)):
distance_location = selected_dist_atom_indexes[j][1]
PCs_combined[:, distance_location] = PCs_combined_selected[:, j]
PCs_separate = np.array(PCs_separate)
PCs_combined = np.array(PCs_combined)
return PCs_separate, PCs_combined
def inverse_transform_of_pcs(n_dim, matrix_reduced, components, mean):
"""
Calculates the inverse transform of the PCs to see what the PCs correspond to in terms of geometric changes.
Different than inverse_transform_of_pcs_as_normal_modes function because this function shows only structures that
are spanned by the input data itself (i.e., uses the PC scores of each structure).
:param n_dim: int, number of principal components specified to define reduced dimensional space
:param matrix_reduced: array, PCs in reduced dimensional space
:param components: array, eigenvectors of the covariance matrix of the input data
:param mean: mean structure of the input data
:return: PCs_separate, PCs_combined as arrays
"""
PCs_separate = []
for i in range(0, n_dim):
PCi = np.dot(matrix_reduced[:, i, None], components[None, i, :]) + mean
PCs_separate.append(PCi)
PCs_combined = np.dot(matrix_reduced[:, 0:n_dim], components[0:n_dim, :]) + mean
PCs_separate = np.array(PCs_separate)
PCs_combined = np.array(PCs_combined)
return PCs_separate, PCs_combined
def inverse_transform_of_pcs_as_normal_modes(n_dim, matrix_reduced, components, mean, alpha=0.40):
"""
Adds incremental amounts of each eigenvector to the mean structure to show the effect of individual eigenvectors
on molecular structure. Different than the inverse_transform_of_pcs function as this function does NOT take into
account the space spanned by the original input data, but rather distorts the geometry of the mean structure in a
linear fashion (i.e., how visualization of a normal mode appears in GaussView).
:param n_dim: int, number of principal components specified to define reduced dimensional space
:param components: array, eigenvectors of the covariance matrix of the input data
:param mean: mean structure of the input data
:param alpha: the multiple of each eigenvector to add to the mean structure
:return: PCs_separate, PCs_combined as arrays
"""
PCs_separate = []
for i in range(0, n_dim):
PCi = np.dot(alpha * (np.arange(-20, 21))[:, None], components[None, i, :]) + mean
PCs_separate.append(PCi)
multiplier = np.zeros((len(np.arange(-20, 21)), n_dim))
for i in range(n_dim):
multiplier[:, i] = np.arange(-20, 21)
PCs_combined = np.dot(alpha * multiplier, components[0:n_dim, :]) + mean
PCs_separate = np.array(PCs_separate)
PCs_combined = np.array(PCs_combined)
return PCs_separate, PCs_combined
def calc_ij(k, n):
"""
Calculate indexes i and j of a square symmetric matrix given upper triangle vector index k and matrix side length n.
:param k: vector index
:param n: side length of resultant matrix M
:return: i, j as ints
"""
i = n - 2 - math.floor((np.sqrt(-8 * k + 4 * n * (n - 1) - 7) / 2) - 0.5)
j = k + i + 1 - (n * (n - 1) / 2) + ((n - i) * ((n - i) - 1) / 2)
return int(i), int(j)
def top_values_indexes(a, n):
"""
Determine indexes of n top values of matrix a
:param a: matrix
:param n: integer, number of top values desired
:return: sorted list of indexes of n top values of a
"""
return np.argsort(a)[::-1][:n]
def kabsch(coordinates):
"""Kabsch algorithm to get orientation of axes that minimizes RMSD. All structures will be aligned to the first
structure in the trajectory.
:param coordinates: coordinates along trajectory to be aligned, list or array
"""
coordinates = np.array(coordinates)
coordinates[0] -= rmsd.centroid(coordinates[0])
coords_kabsch = []
for i in range(len(coordinates)):
coordinates[i] -= rmsd.centroid(coordinates[i])
coords_kabschi = rmsd.kabsch_rotate(coordinates[i], coordinates[0])
coords_kabsch.append(coords_kabschi)
return np.array(coords_kabsch)
def align_to_original_traj(coords, original_traj_coords):
"""Kabsch algorithm to get orientation of axes that minimizes RMSD (to avoid rotations in visualization). All
structures will be aligned to the first structure in the original trajectory.
:param coords: coordinates along trajectory to be aligned, list or array
:param original_traj_coords: coordinates along original trajectory
"""
coords = np.array(coords)
coords_aligned = []
original_traj_coords[0] -= rmsd.centroid(original_traj_coords[0])
for i in range(len(coords)):
coords[i] -= rmsd.centroid(coords[i])
coords_i = rmsd.kabsch_rotate(coords[i], original_traj_coords[0])
coords_aligned.append(coords_i)
return np.array(coords_aligned)
def chirality_test(coords, stereo_atoms):
""" Determines chirality of structure so it is consistent throughout the generated reduced dimensional
IRC/trajectory.
:param coords: xyz coordinates along IRC or trajectory
:param stereo_atoms: list of 4 atom numbers that represent groups around a chiral center
:type coords: numpy array
:type stereo_atoms: list
"""
a1 = stereo_atoms[0]
a2 = stereo_atoms[1]
a3 = stereo_atoms[2]
a4 = stereo_atoms[3]
signs = []
for i in range(len(coords)):
m = np.ones((4, 4))
m[0, 0:3] = coords[i][a1 - 1]
m[1, 0:3] = coords[i][a2 - 1]
m[2, 0:3] = coords[i][a3 - 1]
m[3, 0:3] = coords[i][a4 - 1]
if np.linalg.det(m) < 0:
signs.append(-1)
elif np.linalg.det(m) > 0:
signs.append(1)
elif np.linalg.det(m) == 0:
signs.append(0)
negs = np.where(np.array(signs) < 0)
poss = np.where(np.array(signs) > 0)
zeros = np.where(np.array(signs) == 0)
return negs, poss, zeros, signs
def chirality_changes(reconstructed_coordinates, stereo_atoms, original_structure_signs):
""" Determines chirality of structure along original trajectory and reconstructed reduced dimensional trajectory
and switches inconsistencies along reduced dimensional IRC/trajectory.
:param reconstructed_coordinates: coordinates of trajectory in the reduced dimensional space
:param stereo_atoms: list of 4 indexes of atoms surrounding stereogenic center
:param original_structure_signs: signs (positive or negative) that represent chirality at given point along original
trajectory, numpy array
:return: correct_chirality_coordinates: coordinates with the chirality of each structure consistent with the
original coordinates (based on original_structure_signs), array
"""
pos, neg, zero, signs_reconstructed = chirality_test(reconstructed_coordinates, stereo_atoms)
correct_chirality_coordinates = reconstructed_coordinates
for i in range(len(original_structure_signs)):
if original_structure_signs[i] == 0:
# If molecule begins planar but reconstruction of PCs are not, keep chirality consistent along reconstructed
# trajectory
if i > 0 and signs_reconstructed[i] != signs_reconstructed[0]:
correct_chirality_coordinates[i] = -correct_chirality_coordinates[i]
elif signs_reconstructed[i] != original_structure_signs[i]:
correct_chirality_coordinates[i] = -correct_chirality_coordinates[i]
return correct_chirality_coordinates
def chirality_changes_normal_modes(reconstructed_coordinates, stereo_atoms, original_structure_signs):
""" Determines chirality of structure along original trajectory and reconstructed reduced dimensional trajectory
and switches inconsistencies along reduced dimensional IRC/trajectory.
:param reconstructed_coordinates: coordinates of trajectory in the reduced dimensional space
:param stereo_atoms: list of 4 indexes of atoms surrounding stereogenic center
:param original_structure_signs: signs (positive or negative) that represent chirality at given point along original
trajectory, numpy array
:return: correct_chirality_coordinates: coordinates with the chirality of each structure consistent with the
original coordinates (based on original_structure_signs), array
"""
pos, neg, zero, signs_reconstructed = chirality_test(reconstructed_coordinates, stereo_atoms)
correct_chirality_coordinates = reconstructed_coordinates
for i in range(len(reconstructed_coordinates)):
if original_structure_signs[0] == 0:
if i > 0 and signs_reconstructed[i] != signs_reconstructed[0]:
correct_chirality_coordinates[i] = -correct_chirality_coordinates[i]
elif signs_reconstructed[i] != original_structure_signs[0]:
correct_chirality_coordinates[i] = -correct_chirality_coordinates[i]
return correct_chirality_coordinates
def make_pc_xyz_files(output_directory, title, atoms, coordinates):
""" Save principal coordinates as xyz files PC[n].xyz to output directory.
:param output_directory: output directory to store xyz files, str
:param atoms: atoms in input trajectory, list
:param title: name of the input system, str
:param coordinates: xyz coordinates of structures along PCi, list or numpy array
:return: None
"""
for k in range(np.array(coordinates).shape[0]):
if np.array(coordinates).shape[0] == 1:
f = open(os.path.join(output_directory, '%s_all_PCs.xyz' % title), 'w')
else:
f = open(os.path.join(output_directory, '%s_PC%s.xyz' % (title, k + 1)), 'w')
for i in range(len(coordinates[k])):
a = coordinates[k][i]
a = a.tolist()
b = []
for j in range(len(a)):
a[j] = ['%.5f' % x for x in a[j]]
a[j].insert(0, atoms[j])
b.append(a[j])
f.write('%d' % len(atoms) + '\n')
f.write('%s point %i' % (title, i + 1) + '\n')
f.write('%s' % str(np.asarray(b)).replace("[", "").replace("]", "").replace("'", "") + '\n')
f.close()
def print_prop_of_var_to_txt(values, system_name, directory):
"""
Print list of proportions of variance explained by each principal component to a text file.
:param values: array or list, proportions of variance in descending order
:param system_name: name of the system, used for the text file name
:param directory: output directory to put the output text file
:return: None
"""
normalized_values = values / np.sum(values)
df = pd.DataFrame({'Principal Component': pd.Series([i+1 for i in range(len(values))]),
'Singular Value': values,
'Prop. of Variance': normalized_values,
'Cumul. Prop. of Var.': np.cumsum(normalized_values)})
pd.set_option('display.expand_frame_repr', False)
print(df.head())
df.to_csv(os.path.join(directory, system_name + '_prop_of_var.txt'), sep='\t', index=None)
def print_distance_weights_to_files(directory, n_dim, system_name, pca_components, num_atoms, selected_atom_indexes=None):
for n in range(n_dim):
if selected_atom_indexes:
distance_vector_indexes = list(pd.DataFrame(list(selected_atom_indexes.values()))[1])
else:
distance_vector_indexes = range(len(pca_components[n]))
d = []
for k, l in zip(distance_vector_indexes, range(len(pca_components[n]))):
i, j = calc_ij(k, num_atoms)
coeff = pca_components[n][l]
d.append({'atom 1': i, 'atom 2': j, 'Coefficient of Distance': coeff})
d_df = pd.DataFrame(d)
sorted_d = d_df.reindex(d_df['Coefficient of Distance'].abs().sort_values(ascending=False).index)
sorted_d.to_csv(os.path.join(directory, system_name + '_PC%s_components.txt' % (n + 1)), sep='\t', index=None)
def print_distance_weights_to_files_select_atom_indexes(atom_indexes, n_dim, pca_components, system_name, directory):
for n in range(n_dim):
d = []
for k in range(len(pca_components[n])):
coeff = pca_components[n][k]
d.append({'atom 1': atom_indexes[k][0], 'atom 2': atom_indexes[k][1], 'Coefficient of Distance': coeff})
d_df = pd.DataFrame(d)
sorted_d = d_df.reindex(d_df['Coefficient of Distance'].abs().sort_values(ascending=False).index)
sorted_d.to_csv(os.path.join(directory, system_name + '_PC%s_components.txt' % (n + 1)), sep='\t', index=None)
def print_distance_weights_to_files_weighted(directory, n_dim, system_name, pca_components, pca_values, num_atoms,
display=False):
for n in range(n_dim):
d = []
for k in range(len(pca_components[n])):
i, j = calc_ij(k, num_atoms)
coeff = (pca_values[n]/sum(pca_values))*pca_components[n][k]
d.append({'atom 1': i, 'atom 2': j, 'Coefficient of Distance': coeff})
d_df = pd.DataFrame(d)
sorted_d = d_df.reindex(d_df['Coefficient of Distance'].abs().sort_values(ascending=False).index)
sorted_d.to_csv(os.path.join(directory, system_name + '_PC%s_components_weighted.txt' % (n + 1)), sep='\t',
index=None)
if display:
print("PC%s" % (n+1))
print(sorted_d)
def transform_new_data(new_xyz_file_path, output_directory, n_dim, pca_fit, pca_components, pca_mean,
original_traj_coords, input_type, stereo_atoms=[1, 2, 3, 4], mw=False, remove_atom_types=None,
selected_atom_indexes=None):
if input_type=="Cartesians":
new_system_name, components_df = transform_new_data_cartesians(new_xyz_file_path, output_directory, n_dim,
pca_fit, pca_components, pca_mean,
original_traj_coords, mw=mw,
remove_atom_types=remove_atom_types)
elif input_type=="Distances":
if selected_atom_indexes:
new_system_name, components_df = transform_new_data_only_top_distances(new_xyz_file_path, output_directory, n_dim,
pca_fit, pca_components, pca_mean,
selected_atom_indexes=selected_atom_indexes,
stereo_atoms=stereo_atoms, mw=mw,
remove_atom_types=remove_atom_types)
else:
new_system_name, components_df = transform_new_data_distances(new_xyz_file_path, output_directory, n_dim,
pca_fit, pca_components, pca_mean,
stereo_atoms=stereo_atoms, mw=mw,
remove_atom_types=remove_atom_types)
else:
print("ERROR: Please specify input_type=\"Cartesians\" or \"Distances\"")
return new_system_name, components_df
def transform_new_data_cartesians(new_xyz_file_path, output_directory, n_dim, pca_fit, pca_components, pca_mean,
original_traj_coords, mw=False, remove_atom_types=None):
"""
Takes as input a new trajectory (xyz file) for a given system for which dimensionality reduction has already been
conducted and transforms this new data into the reduced dimensional space. Generates a plot, with the new data atop
the "trained" data, and generates xyz files for the new trajectories represented by the principal components.
:param new_xyz_file_path: new input to dimensionality reduction (xyz file location), str
:param output_directory: output directory, str
:param n_dim: number of dimensions of the reduced dimensional space, int
:param pca_fit: fit from PCA on training data
:param pca_components: components from PCA on training data, array
:param pca_mean: mean of input data to PCA (mean structure as coords or distances), array
:param original_traj_coords: coordinates of the trajectory that the reduced dimensional space was trained on
:param MW: whether coordinates should be mass weighted prior to PCA, bool
"""
print("\nTransforming %s into reduced dimensional representation..." % new_xyz_file_path)
new_system_name, atoms, coordinates = read_xyz_file(new_xyz_file_path)
if remove_atom_types is not None:
atoms, coordinates = remove_atoms_by_type(remove_atom_types, atoms, coordinates)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
print("\nResults for %s input will be stored in %s" % (new_xyz_file_path, output_directory))
# Determining names of output directories/files
file_name_end = "_Cartesians"
# Align structures using Kabsch algorithm so rotations don't affect PCs
aligned_original_traj_coords = kabsch(original_traj_coords)
coords_for_analysis = align_to_original_traj(coordinates, aligned_original_traj_coords)
if mw is True:
file_name_end = file_name_end + "_MW"
mass_weighted_coords = mass_weighting(atoms, coords_for_analysis)
coords_for_analysis = mass_weighted_coords
else:
file_name_end = file_name_end + "_noMW"
coords_for_analysis = coords_for_analysis
coords_for_analysis = np.reshape(coords_for_analysis, (coords_for_analysis.shape[0],
coords_for_analysis.shape[1] *
coords_for_analysis.shape[2]))
components = pca_fit.transform(coords_for_analysis)
components_df = pd.DataFrame(components)
PCs_separate = []
for i in range(0, n_dim):
PCi = np.dot(components[:, i, None], pca_components[None, i, :]) + pca_mean
PCs_separate.append(PCi)
PCs_combined = np.dot(components, pca_components) + pca_mean
PCs_separate = np.array(PCs_separate)
PCs_combined = np.array(PCs_combined)
# Reshape n x 3N x 1 arrays into n x N x 3 arrays
PCs_separate = np.reshape(PCs_separate, (PCs_separate.shape[0], PCs_separate.shape[1],
int(PCs_separate.shape[2] / 3), 3))
PCs_combined = np.reshape(PCs_combined, (1, PCs_combined.shape[0], int(PCs_combined.shape[1] / 3), 3))
if mw is True:
# Remove mass-weighting of coordinates
no_mass_weighting_PCs_separate = [remove_mass_weighting(atoms, PCs_separate[i])
for i in range(n_dim)]
no_mass_weighting_PCs_combined = remove_mass_weighting(atoms, PCs_combined)
else:
no_mass_weighting_PCs_separate = PCs_separate
no_mass_weighting_PCs_combined = PCs_combined
aligned_PCs_separate = no_mass_weighting_PCs_separate
aligned_PCs_combined = no_mass_weighting_PCs_combined
make_pc_xyz_files(output_directory, new_system_name + file_name_end, atoms, aligned_PCs_separate)
make_pc_xyz_files(output_directory, new_system_name + file_name_end, atoms, aligned_PCs_combined)
return new_system_name, components_df
def transform_new_data_distances(new_xyz_file_path, output_directory, n_dim, pca_fit, pca_components, pca_mean,
stereo_atoms=[1, 2, 3, 4], mw=False, remove_atom_types=None):
"""
Takes as input a new trajectory (xyz file) for a given system for which dimensionality reduction has already been
conducted and transforms this new data into the reduced dimensional space. Generates a plot, with the new data atop
the "trained" data, and generates xyz files for the new trajectories represented by the principal components.
:param new_xyz_file_path: new input to dimensionality reduction (xyz file location), str
:param output_directory: output directory, str
:param n_dim: number of dimensions of the reduced dimensional space, int
:param pca_fit: fit from PCA on training data
:param pca_components: components from PCA on training data, array
:param pca_mean: mean of input data to PCA (mean structure as coords or distances), array
:param original_traj_coords: coordinates of the trajectory that the reduced dimensional space was trained on
:param stereo_atoms: indexes of 4 atoms surrounding stereogenic center, list of ints
:param MW: whether coordinates should be mass weighted prior to PCA, bool
"""
print("\nTransforming %s into reduced dimensional representation..." % new_xyz_file_path)
new_system_name, atoms, coordinates = read_xyz_file(new_xyz_file_path)
if remove_atom_types is not None:
atoms, coordinates = remove_atoms_by_type(remove_atom_types, atoms, coordinates)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
print("\nResults for %s input will be stored in %s" % (new_xyz_file_path, output_directory))
# Determining names of output directories/files
file_name_end = "_Distances"
if mw is True:
file_name_end = file_name_end + "_MW"
coordinates_shifted = set_atom_one_to_origin(coordinates)
mass_weighted_coords = mass_weighting(atoms, coordinates_shifted)
coords_for_analysis = mass_weighted_coords
else:
file_name_end = file_name_end + "_noMW"
coords_for_analysis = coordinates
negatives, positives, zeroes, all_signs = chirality_test(coordinates, stereo_atoms)
d2 = generate_distance_matrices(coords_for_analysis)
coords_for_analysis = reshape_ds(d2)
components = pca_fit.transform(coords_for_analysis)
components_df = pd.DataFrame(components)
PCs_separate = []
for i in range(0, n_dim):
PCi = np.dot(components[:, i, None], pca_components[None, i, :]) + pca_mean
PCs_separate.append(PCi)
PCs_combined = np.dot(components, pca_components) + pca_mean
PCs_separate = np.array(PCs_separate)
PCs_combined = np.array(PCs_combined)
# Turning distance matrix representations of structures back into Cartesian coordinates
PCs_separate = [[distance_matrix_to_coords(PCs_separate[i][k])
for k in range(PCs_separate.shape[1])] for i in range(PCs_separate.shape[0])]
PCs_combined = [distance_matrix_to_coords(PCs_combined[i])
for i in range(np.array(PCs_combined).shape[0])]
PCs_separate = np.real(PCs_separate)
PCs_combined = np.real(PCs_combined)
if mw is True:
# Remove mass-weighting of coordinates
no_mass_weighting_PCs_separate = [remove_mass_weighting(atoms, PCs_separate[i])
for i in range(n_dim)]
no_mass_weighting_PCs_combined = remove_mass_weighting(atoms, PCs_combined)
else:
no_mass_weighting_PCs_separate = PCs_separate
no_mass_weighting_PCs_combined = PCs_combined
# Reorient coordinates so they are in a consistent orientation
aligned_PCs_separate = [kabsch(chirality_changes(no_mass_weighting_PCs_separate[i], stereo_atoms,
all_signs)) for i in range(n_dim)]
aligned_PCs_combined = kabsch(chirality_changes(no_mass_weighting_PCs_combined, stereo_atoms, all_signs))
aligned_PCs_combined = np.reshape(aligned_PCs_combined, (1, aligned_PCs_combined.shape[0],
aligned_PCs_combined.shape[1],
aligned_PCs_combined.shape[2]))
make_pc_xyz_files(output_directory, new_system_name + file_name_end, atoms, aligned_PCs_separate)
make_pc_xyz_files(output_directory, new_system_name + file_name_end, atoms, aligned_PCs_combined)
return new_system_name, components_df
def transform_new_data_only_top_distances(new_xyz_file_path, output_directory, n_dim, pca_fit, pca_components, pca_mean,
selected_atom_indexes, stereo_atoms=[1, 2, 3, 4], mw=False, remove_atom_types=None):
"""
Takes as input a new trajectory (xyz file) for a given system for which dimensionality reduction has already been
conducted and transforms this new data into the reduced dimensional space. Generates a plot, with the new data atop
the "trained" data, and generates xyz files for the new trajectories represented by the principal components.
:param new_xyz_file_path: new input to dimensionality reduction (xyz file location), str
:param output_directory: output directory, str
:param n_dim: number of dimensions of the reduced dimensional space, int
:param pca_fit: fit from PCA on training data
:param pca_components: components from PCA on training data, array
:param pca_mean: mean of input data to PCA (mean structure as coords or distances), array
:param original_traj_coords: coordinates of the trajectory that the reduced dimensional space was trained on
:param stereo_atoms: indexes of 4 atoms surrounding stereogenic center, list of ints
:param MW: whether coordinates should be mass weighted prior to PCA, bool
"""
print("\nTransforming %s into reduced dimensional representation..." % new_xyz_file_path)
new_system_name, atoms, coordinates = read_xyz_file(new_xyz_file_path)
if remove_atom_types is not None:
atoms, coordinates = remove_atoms_by_type(remove_atom_types, atoms, coordinates)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
print("\nResults for %s input will be stored in %s" % (new_xyz_file_path, output_directory))
if mw is True:
coordinates_shifted = set_atom_one_to_origin(coordinates)
mass_weighted_coords = mass_weighting(atoms, coordinates_shifted)
coords_for_analysis = mass_weighted_coords
else:
coords_for_analysis = coordinates
d2_vector_matrix_all = generate_and_reshape_ds_big_structures(coords_for_analysis)
print('Starting new bit')
num_dists = len(list(selected_atom_indexes.keys()))
num_points = d2_vector_matrix_all.shape[0]
important_distances_matrix = np.zeros((num_points, num_dists))
distance_vector_indexes = list(pd.DataFrame(list(selected_atom_indexes.values()))[1])
for i in range(len(distance_vector_indexes)):
important_distances_matrix[:, i] = d2_vector_matrix_all[:, distance_vector_indexes[i]]
components = pca_fit.transform(important_distances_matrix)
components_df = pd.DataFrame(components)
return new_system_name, components_df
def pathreducer(xyz_file_path, n_dim, stereo_atoms=[1, 2, 3, 4], input_type="Cartesians", mw=False, reconstruct=True,
normal_modes=False, remove_atom_types=None, num_dists=None):
"""
Workhorse function for doing dimensionality reduction on xyz files. Dimensionality reduction can be done on the
structures represented as Cartesian coordinates (easy/faster) or the structures represented as distances matrices
(slower, but potentially more useful for certain systems that vary non-linearly with respect to Cartesian space,
e.g., torsions).
:param xyz_file_path: xyz file or directory filled with xyz files that will be used to generate the reduced
dimensional space, str
:param n_dim: number of dimensions to reduce system to using PCA, int
:param stereo_atoms: list of 4 atom indexes surrounding stereogenic center, ints
:param input_type: input type to PCA, either "Cartesians" or "Distances", str
:return: name, directory, pca, pca_fit, components, mean, values, lengths
"""
# Make sure even large matrices are printed out in their entirety (for the generation of xyz files)
np.set_printoptions(threshold=sys.maxsize)
# Check if input is directory (containing input files) or a single input file itself
assert os.path.isfile(xyz_file_path) or os.path.isdir(xyz_file_path), "No such file or directory."
if os.path.isfile(xyz_file_path) is True:
if input_type == "Cartesians":
system_name, output_directory, pca, pca_fit, components, mean, values, aligned_coords = \
pathreducer_cartesians_one_file(xyz_file_path, n_dim, mw=mw, reconstruct=reconstruct,
normal_modes=normal_modes, remove_atom_types=remove_atom_types)
elif input_type == "Distances":
if num_dists:
system_name, output_directory, pca, pca_fit, components, mean, values, aligned_coords, selected_dist_atom_indexes = \
pathreducer_distances_one_file(xyz_file_path, n_dim, stereo_atoms=stereo_atoms, mw=mw,
reconstruct=reconstruct, normal_modes=normal_modes,
remove_atom_types=remove_atom_types, num_dists=num_dists)
lengths = aligned_coords.shape[0]
return system_name, output_directory, pca, pca_fit, components, mean, values, lengths, aligned_coords, \
selected_dist_atom_indexes
else:
system_name, output_directory, pca, pca_fit, components, mean, values, aligned_coords = \
pathreducer_distances_one_file(xyz_file_path, n_dim, stereo_atoms=stereo_atoms, mw=mw,
reconstruct=reconstruct, normal_modes=normal_modes,
remove_atom_types=remove_atom_types, num_dists=num_dists)
lengths = aligned_coords.shape[0]
return system_name, output_directory, pca, pca_fit, components, mean, values, lengths, aligned_coords
elif os.path.isdir(xyz_file_path) is True:
if input_type == "Cartesians":
system_name, output_directory, pca, pca_fit, components, mean, values, lengths, aligned_coords = \
pathreducer_cartesians_directory_of_files(xyz_file_path, n_dim, mw=mw, reconstruct=reconstruct,
normal_modes=normal_modes, remove_atom_types=remove_atom_types)
return system_name, output_directory, pca, pca_fit, components, mean, values, lengths, aligned_coords
elif input_type == "Distances":
if num_dists:
system_name, output_directory, pca, pca_fit, components, mean, values, lengths, aligned_coords, \
selected_dist_atom_indexes = pathreducer_distances_directory_of_files(xyz_file_path, n_dim,
stereo_atoms=stereo_atoms, mw=mw,
reconstruct=reconstruct, normal_modes=normal_modes,
num_dists=num_dists, remove_atom_types=remove_atom_types)
return system_name, output_directory, pca, pca_fit, components, mean, values, lengths, aligned_coords, \
selected_dist_atom_indexes
else:
system_name, output_directory, pca, pca_fit, components, mean, values, lengths, aligned_coords, \
selected_dist_atom_indexes = pathreducer_distances_directory_of_files(xyz_file_path, n_dim,
stereo_atoms=stereo_atoms, mw=mw,
reconstruct=reconstruct, normal_modes=normal_modes,
num_dists=num_dists, remove_atom_types=remove_atom_types)
return system_name, output_directory, pca, pca_fit, components, mean, values, lengths, aligned_coords