Reputation: 733
I have a gromac file to extract specific details from it. File format is follows. Only i need values in each column.
Generated by trjconv : a bunch of waters t= 0.00000
3000
1SOL OW 1 1.5040 2.7580 0.6820
1SOL HW1 2 1.4788 2.7853 0.7702
1SOL HW2 3 1.4640 2.8230 0.6243
2SOL OW 4 1.5210 0.9510 2.2050
2SOL HW1 5 1.5960 0.9780 2.1520
2SOL HW2 6 1.4460 0.9940 2.1640
1000SOL OW 2998 1.5310 1.7952 2.1981
1000SOL HW1 2999 1.4560 1.7375 2.1836
1000SOL HW2 3000 1.6006 1.7369 2.2286
3.12736 3.12736 3.12736
Generated by trjconv : a bunch of waters t= 9000.00000
3000
1SOL OW 1 1.1579 0.4255 2.1329
1SOL HW1 2 1.0743 0.3793 2.1385
1SOL HW2 3 1.1514 0.4772 2.0526
2SOL OW 4 2.2976 2.5403 0.1574
2SOL HW1 5 2.2161 2.4981 0.1847
2SOL HW2 6 2.3118 2.6092 0.2223
1000SOL OW 2998 2.5927 1.0233 0.4695
1000SOL HW1 2999 2.5927 0.9869 0.3810
1000SOL HW2 3000 2.5365 0.9642 0.5197
3.10941 3.10941 3.10941
I have tried following codes to read it.
F = open('Data.gro', 'r')
for line in F:
P = line.split('\t')
I want to extract data as below in to float variables.
My idea was after split the data called as P[3], P[4], P[6] But I failed. When i am reading file i want to skip information after ever 3000 lines. There are information given in three line after every 3000 line. and also I want to skip first two lines as well. The .gro file has specific format.
http://manual.gromacs.org/current/online/gro.html
Out put should be as below. i am going to do some calculations using those values.
First Set
X = [1.5040, 1.4788, 14640,... , 1.6006]
Y = [2.7580, 2.7853, , 1.7369]
Z = [0.6820, 0.7702, , 2.2286]
Second Set
X = [1.1579, 1.0743, ,... , 2.5365]
Y = [0.4255, 0.3793, ,... , 0.9642]
Z = [2.1329, 2.1329, ,... , 0.5197]
Please read my question care fully. I need some guidance to write above program. I am new one to python. I have done research but I couldn't find any help to extract those numbers.
Thank you
Upvotes: 3
Views: 1817
Reputation: 733
Finally I was able to written full program to read my gromac file. This might help some one if want to read data from grmomac file and extract specific values from it. This code adjusted to read 10 rows after specific place find. I used this to read 3000 rows. This is working 100% successfully.
F = open('Data.gro', 'r')
A = open('XYZ.txt', 'w')
XO = []
YO = []
ZO = []
XHW1 = []
YHW1 = []
ZHW1 = []
XHW2 = []
YHW2 = []
ZHW2 = []
I = range(1, 10)
with open('Data.gro') as F:
for line in F:
if line.split()[0] == '3000':
A.write('Frame' + '\n')
for R in I:
line = next(F)
P = line.split()
if P[1] == 'OW':
x = float(P[3])
y = float(P[4])
z = float(P[5])
XO.append(x)
YO.append(y)
ZO.append(z)
else:
if P[1] == 'HW1':
x = float(P[3])
y = float(P[4])
z = float(P[5])
XHW1.append(x)
YHW1.append(y)
ZHW1.append(z)
else:
if P[1] == 'HW2':
x = float(P[3])
y = float(P[4])
z = float(P[5])
XHW2.append(x)
YHW2.append(y)
ZHW2.append(z)
else:
pass
if line.split()[2] == '3000':
A.write('Oxygen atoms XYZ coordinates:' + '\n')
A.write("%s\n" % (XO))
A.write("%s\n" % (YO))
A.write("%s\n\n" % (ZO))
A.write('Hydrogen 1 atoms XYZ coordinates:' + '\n')
A.write("%s\n" % (XHW1))
A.write("%s\n" % (YHW1))
A.write("%s\n\n" % (ZHW1))
A.write('Hydrogen 2 atoms XYZ coordinates:' + '\n')
A.write("%s\n" % (XHW2))
A.write("%s\n" % (YHW2))
A.write("%s\n\n" % (ZHW2))
XO = []
YO = []
ZO = []
XHW1 = []
YHW1 = []
ZHW1 = []
XHW2 = []
YHW2 = []
ZHW2 = []
R +=1
else:
pass
else:
pass
A.close()
Thanx some people help me to solve specific points. Thanx goes to them also. This is a really nice website. I like this. This help lot to learn things.
Upvotes: 3