需要帮助在 Python 中读取 gromac 文件 [英] Need help to read gromac file in Python

查看:67
本文介绍了需要帮助在 Python 中读取 gromac 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 gromac 文件可以从中提取特定的细节.文件格式如下.只有我需要每一列中的值.

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.

我的想法是将数据拆分为 P[3]、P[4]、P[6]但我失败了.当我阅读文件时,我想跳过 3000 行后的信息.每 3000 行后有三行给出的信息.而且我也想跳过前两行..gro 文件具有特定格式.

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.

第一组

X = [1.5040, 1.4788, 14640,... , 1.6006]
Y = [2.7580, 2.7853,           , 1.7369]
Z = [0.6820, 0.7702,           , 2.2286]

第二套

X = [1.1579, 1.0743, ,... , 2.5365]
Y = [0.4255, 0.3793, ,... , 0.9642]
Z = [2.1329, 2.1329, ,... , 0.5197]

请仔细阅读我的问题.我需要一些指导来编写上述程序.我是python的新手.我做了研究,但找不到任何帮助来提取这些数字.

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.

谢谢

推荐答案

最后,我能够编写完整的程序来读取我的 gromac 文件.如果想要从 grmomac 文件中读取数据并从中提取特定值,这可能会有所帮助.此代码调整为在特定位置查找后读取 10 行.我用它来读取 3000 行.这 100% 成功.

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 也去找他们.这是一个非常好的网站.我喜欢这个.这对学习东西很有帮助.

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.

这篇关于需要帮助在 Python 中读取 gromac 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆