计算化学公社

 找回密码 Forget password
 注册 Register
Views: 8565|回复 Reply: 1
打印 Print 上一主题 Last thread 下一主题 Next thread

[GROMACS] 求助 Gromacs 伞形采样脚本运行错误

[复制链接 Copy URL]

99

帖子

0

威望

1303

eV
积分
1402

Level 4 (黑子)

伞形采样脚本是从官方下载的,是连续自动运行伞形采样计算的,我用python3  运行出现如下错误:

Traceback (most recent call last):
  File "./setup-umbrella-script/setupUmbrella.py", line 183, in <module>
    out = main()
  File "./setup-umbrella-script/setupUmbrella.py", line 151, in main
    distance_table = readDistanceFile(distance_file)
  File "./setup-umbrella-script/setupUmbrella.py", line 49, in readDistanceFile
    value = float(columns[1])
IndexError: list index out of range



不知如何修改,我用红色标出了第49,151和183行,非常感谢。



脚本如下:

#!/usr/bin/env python
__description__ = \
"""
Take a file containing frame number vs. distance, then identify frames that
sample those distances at approximately sample_interval.  Optionally takes an
arbitrary number of template files.  The program searches the contents of these
files for a search string (by default XXX), replaces the search string with the
frame number, then writes out each file with a unique, frame-specific filename.
This last feature means that this script can be used to automatically generate
input files to run each umbrella sampling simulation.

v. 0.1: fixed bug where the script would choke on distance_files that went by
        frame intervals besides steps of 1.
"""
__author__ = "Michael J. Harms [harmsm@gmail.com]"
__date__ = "120314"
__usage__ = "setupUmbrella.py distance_file sample_interval [template_file1 template_file2 ...]"
__version__ = "0.1"

import sys, os, re

def readDistanceFile(distance_file):
    """
    Read a distance file that correlates file number with center-of-mass
    distance between pulling groups.  Format is:

    0 com0
    1 com1
    2 com2
    ...

    If this data was generated using distances.pl (Justin Lemkul), it could
    have multiple com calculations in the same file.  This function starts from
    the bottom of the file and only takes the last output.
    """

    # Read file
    f = open(distance_file,'r')
    lines = f.readlines()
    f.close()

    # Read the data from the bottom
    out_dict = {}
    for i in range(len(lines)-1,-1,-1):

        # Split on white-space; grab frame/distance
        columns = lines.split()
        key = int(columns[0])
        value = float(columns[1])                       #第49行

        # If we've already seen this key, we're done reading the unique data
        # from the file.
        if key in out_dict:
            break     
        else:
           out_dict[key] = value


    # Now put the values into a simple list, sorting to make sure that they are
    # in the correct order.
    keys = out_dict.keys()
    keys.sort()
    out = [(k,out_dict[k]) for k in keys]

    return out


def sampleDistances(distance_table,sample_interval):
    """
    Go through the distances list and sample frames at sample_interval.  
    Appropriate samples are identified by looking forward through the
    distances to find the one that is closest to current_distance +
    sample_interval.  
    """

    distances = [d[1] for d in distance_table]

    current_index = 0
    sampled_indexes = [current_index]
    while current_index < len(distances):

        target_distance = distances[current_index] + sample_interval

        # Walk through the rest of the distances list and find the distance
        # that is closest to the target distance
        onward = [abs(target_distance-d) for d in distances[current_index:]]
        next_index = onward.index(min(onward)) + current_index

        # If we run out of distances to compare, our next_index will be the
        # current index.  This means we're done.  Otherwise, record that we
        # found a new index.
        if current_index == next_index:
            break
        else:
            sampled_indexes.append(next_index)
            current_index = next_index

    return sampled_indexes


def createOutputFile(template_file,frame_number,search_string="XXX"):
    """
    Look for instances of the search string in a template file and replace with
    the frame number in a new file.
    """

    out_file = "frame-%i_%s" % (frame_number,template_file)

    # Prompt the user before wiping out an existing file
    if os.path.exists(out_file):
        answer = raw_input("%s exists!  Overwrite (y|n)?" % out_file)
        answer = answer[0].lower()
        if answer != "y":
            return None

    # Read the contents of the template file
    f = open(template_file,'r')
    file_contents = f.read()
    f.close()

    # Write out the template file contents, replacing all instances of
    # the search string with the frame number
    f = open(out_file,'w')
    f.write(re.sub(search_string,"%i" % frame_number,file_contents))
    f.close()   


def main(argv=None):
    """
    Parse command line, etc.
    """

    if argv == None:
        argv = sys.argv[1:]

    # Parse required command line arguments
    try:
        distance_file = argv[0]
        sample_interval = float(argv[1])
    except (IndexError,ValueError):
        err = "Incorrect command line arguments!\n\n%s\n\n" % __usage__
        raise IOError(err)

    # See if a template file has been specified
    try:
        template_files = argv[2:]
    except IndexError:
        template_files = []

    # Figure out which frames to use
    distance_table = readDistanceFile(distance_file)                               #第151行
    sampled_indexes = sampleDistances(distance_table,sample_interval)

    # If any template files were specified, use them to make frame-specific
    # output
    if len(template_files) != 0:
        print ("Creating frame-specific output for files:")
        print ("\n").join(template_files)
        for t in template_files:
            for i in sampled_indexes:
                frame = distance_table[0]
                createOutputFile(t,frame,search_string="XXX")

    # Print out summary of the frames we identified      
    out = ["%10s%10s%10s\n" % ("frame","dist","d_dist")]
    for i in range(len(sampled_indexes)):

        frame = distance_table[sampled_indexes][0]
        dist = distance_table[sampled_indexes][1]
        if i == 0:
            delta_dist = "%10s" % "NA"
        else:
            prev_dist = distance_table[sampled_indexes[i-1]][1]
            delta_dist = "%10.3f" % (dist - prev_dist)

        out.append("%10i%10.3f%s\n" % (frame,dist,delta_dist))

    return out


# If called from the command line...
if __name__ == "__main__":
    out = main()                                           # 第183行
    print ("").join(out)

1

帖子

0

威望

45

eV
积分
46

Level 2 能力者

2#
发表于 Post on 2022-8-25 14:19:46 | 只看该作者 Only view this author
这个应该是用python2系列写的脚本,你用Python2 加上脚本给出的readme的命令应该就可以正常进行这个自动化过程

本版积分规则 Credits rule

手机版 Mobile version|北京科音自然科学研究中心 Beijing Kein Research Center for Natural Sciences|京公网安备 11010502035419号|计算化学公社 — 北京科音旗下高水平计算化学交流论坛 ( 京ICP备14038949号-1 )|网站地图

GMT+8, 2026-2-22 13:02 , Processed in 0.232209 second(s), 26 queries , Gzip On.

快速回复 返回顶部 返回列表 Return to list