Question

I'm trying to read coordinates from a file and find distance between each two respective atoms in the file. I want to have the calculated distance in one column and the respective atom names in the other.

1 Br1 0 Br x,y,z 1.195 7.005 10.004
2 Br2 0 Br x,y,z 1.432 5.040 6.816
3 Br3 0 Br x,y,z -0.407 8.433 6.863
4 Br4 0 Br x,y,z 3.344 8.375 7.107
5 Fe1 0 Fe x,y,z 1.412 7.214 7.682
6 Br5 0 Br x,y,z 2.813 12.506 12.949
7 Br6 0 Br x,y,z 4.778 14.123 10.091
8 Br7 0 Br x,y,z 6.563 12.765 13.175
9 Br8 0 Br x,y,z 4.387 15.965 13.344

For instance:

Br1-Br2 1.5

I encounter two problems:

1-I have no idea how to command the program to calculate euclidean distance between each two pairs of atoms, I used split() to split coordinates from other data, but I still don't know how to calculate the distance.I found some info about the same problem, but the answers didn't work for me as they were for perl.

atom_positions= open('Atoms.txt','r')
revised_atom_positions = open('Atoms_atoms.txt','w')
aline = atom_positions.readline()#for getting rid of the first line data which is general info

for aline in atom_positions:
     Values = aline.split()
     dataline = values[1]+' , '+ values[5] + ' , ' + values[6] +' , '+values[7]
     revised_atom_positions.write(dataline + '\n')


atom_positions.close()
revised_atom_positions.close()

When I try to change it into an array and use pdist I get the error:

TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

2- I can store my new data in another file with new order, But i have no idea how to call them for distance calculations! there were some guidelines about pdist but it didn't work for me either.

are there any solutions to this problem?

Was it helpful?

Solution

You can use index:

atom_positions= open('Atoms.txt','r')
revised_atom_positions = open('Atoms_atoms.txt','w')

lines = atom_positions.readlines()

for i in range(0, len(lines)):
    line = lines[i]
    values = line.split()
    dataline = values[1]+' , '+ values[5] + ' , ' + values[6] +' , '+values[7]
    # You can access to next line with lines[i + 1]
    revised_atom_positions.write(dataline + '\n')


atom_positions.close()
revised_atom_positions.close()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top