import sys
import numpy as np
from itertools import permutations as perm
rmsd_list=[1000]
final_atom_names=[]
def rmsd(V, W):
"""
Calculate Root-mean-square deviation from two sets of vectors V and W.
Parameters
----------
V : array
(N,D) matrix, where N is points and D is dimension.
W : array
(N,D) matrix, where N is points and D is dimension.
Returns
-------
rmsd : float
Root-mean-square deviation
"""
D = len(V[0])
N = len(V)
rmsd = 0.0
for v, w in zip(V, W):
rmsd += sum([(v[i] - w[i])**2.0 for i in range(D)])
return np.sqrt(rmsd/N)
def calculator(initial_file, final_file):
p_atoms, p_all = "xyz", initial_file
q_atoms, q_all = "xyz", final_file
normal_rmsd =rmsd(p_all[:], q_all[:])
if normal_rmsd < rmsd_list[0]:
rmsd_list[0] = normal_rmsd
return normal_rmsd
def inread(file_xyz, list_coordinates, list_names, initial):
filename = file_xyz
datafile = open(filename)
lines= list(datafile.readlines())
if initial == True:
molecule_length.append(int(lines.pop(0).rstrip()))
elif initial == False:
lines.pop(0)
else:
print("Fourth statement should be True or False")
for line in lines:
list_coordinates.append(map(str, line.split()))
list_coordinates.pop(0)
for atoms in list_coordinates:
list_names.append(atoms.pop(0).rstrip())
for atoms in list_coordinates:
for i, coordinates in enumerate(atoms):
atoms[i]= float(coordinates)
def permutate_and_calculate(molecule_length, initial_molecule_coordinates ,final_molecule_coordinates, final_atom_names):
permutated_final_molecule=[]
permutated_atom_names=[]
counter = 0
for i in perm(range(molecule_length)):
for j in i:
permutated_final_molecule.append(final_molecule_coordinates[j])
permutated_atom_names.append(final_atom_names[j])
permutated_final_molecule= np.asarray(permutated_final_molecule)
if calculator(initial_molecule_coordinates, permutated_final_molecule) != None:
better_list[0] = permutated_atom_names
better_list[1] = permutated_final_molecule
permutated_final_molecule=[]
permutated_atom_names=[]
counter += 1
print ("Hij heeft {} loops gerunt".format(counter))
molecule_length=[]
initial_molecule = []
atom_names = []
inread(sys.argv[1], initial_molecule, atom_names, True)
final_molecule= []
atom_names_final = []
inread(sys.argv[2], final_molecule, atom_names_final, False)
initial_molecule= np.asarray(initial_molecule)
better_list=[0,0]
permutate_and_calculate(molecule_length[0],initial_molecule, final_molecule, atom_names_final)
print (better_list[0])
print (better_list[1])