Пример #1
0
def parse_ins(fpath_ins):
    """Parses .ins file and returns information relevant for creation
    of .inflip file"""

    def dispatcher(list_matched_objects):
        """fills parsing_dict with key-value pairs"""
        parsing_dict = dict()
        for line in list_matched_objects:
            match_obj = line.pop()
            if match_obj.group('tag') in parsing_dict:
                parsing_dict[match_obj.group('tag')] += \
                        "\n{}".format(match_obj.group('value'))
            else:
                parsing_dict[match_obj.group('tag')] = match_obj.group('value')
        return parsing_dict

    tags = ['CELL', 'SYMM', 'LATT', 'LIST']
    re_compile_objs = map(lambda tag: \
            re.compile("(?P<tag>{})\\s+(?P<value>\\S+.*)".format(tag)), tags)
    filenames = generators.gen_find(os.path.basename(fpath_ins),
                                    os.path.dirname(fpath_ins))
    fobjects = generators.gen_open(filenames)
    lines = generators.gen_cat(fobjects)
    list_matched_objects = generators.gen_grep(lines, re_compile_objs,
                                                yield_line = False)
    parsing_dict  = dispatcher(list_matched_objects)
    return parsing_dict
Пример #2
0
    def symops_from_cif():
        yield "loop_\n" 
        yield "_symmetry_equiv_pos_as_xyz\n"
        filenames = generators.gen_find(
                os.path.basename(fpath_res).rsplit(".res")[0] + ".cif",
                os.path.dirname(fpath_res))
        fobjects = generators.gen_open(filenames)
        lines = generators.gen_cat(fobjects)

        
        patt = patt =re.compile(r"('[^,'()]+,[^,'()]+,[^,'()]+')")
        for line in lines:
            mobj = re.search(patt, line)
            if mobj:
                yield line
Пример #3
0
def difference_peaks(fpath_res):
    """Saves peaks from difference Fourier(liested in .res) into peaks.cif"""

    def cell():
        cell_str = "_cell_length_a              {}\n"\
                   "_cell_length_b              {}\n"\
                   "_cell_length_c              {}\n"\
                   "_cell_angle_alpha           {}\n"\
                   "_cell_angle_beta            {}\n"\
                   "_cell_angle_gamma           {}".format(
                                *parsing_dict["CELL"].strip().split()[1:])
        for line in cell_str.split("\n"):
            yield line

    #def group_HM():
    #    yield "_symmetry_space_group_name_H-M    '{}'\n".format(
    #                                                sgnr)
    #                                                    #groupsHM.table[sgnr])
    #    #yield "_symmetry_Int_Tables_number        {}\n".format(sgnr)
    def symops_from_cif():
        yield "loop_\n" 
        yield "_symmetry_equiv_pos_as_xyz\n"
        filenames = generators.gen_find(
                os.path.basename(fpath_res).rsplit(".res")[0] + ".cif",
                os.path.dirname(fpath_res))
        fobjects = generators.gen_open(filenames)
        lines = generators.gen_cat(fobjects)

        
        patt = patt =re.compile(r"('[^,'()]+,[^,'()]+,[^,'()]+')")
        for line in lines:
            mobj = re.search(patt, line)
            if mobj:
                yield line


    def loop_atom():
        yield "loop_\n"
        yield "_atom_site_label\n"
        yield "_atom_site_type_symbol\n"
        yield "_atom_site_fract_x\n"
        yield "_atom_site_fract_y\n"
        yield "_atom_site_fract_z\n"

    def density_peaks():
        for matchobj in parsing_dict["PEAKS"]:
            yield " {}  {}  {}  {}  {}\n".format(
                                                 matchobj.group(1),
                                                 'He',
                                                 matchobj.group(3),
                                                 matchobj.group(4),
                                                 matchobj.group(5),
                                                 matchobj.group(6))

    re_compile_objs = (
                        re.compile("\s*(CELL)(.*)"),
                        re.compile(
                          r'\s*(Q\d+)\s+(\d+)'\
                          r'\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)'\
                          r'\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s*'))


    
    parsing_dict = dict()

    filenames = generators.gen_find(os.path.basename(fpath_res),
                                       os.path.dirname(fpath_res))
    fobjects = generators.gen_open(filenames)
    lines = generators.gen_cat(fobjects)
    list_matched_objects = generators.gen_grep(lines, re_compile_objs,
                                                yield_line = False)
    poped_objs = generators.pop_it_fromlist(list_matched_objects)
    
    for obj in poped_objs:
        if obj.group(1) == "CELL":
            parsing_dict["CELL"] = obj.group(2)
        else:
            parsing_dict.setdefault("PEAKS", []).append(obj)

    peaks_fpath = os.path.join(os.path.dirname(fpath_res), "peaks.cif")

    with open(peaks_fpath, 'w') as f:
        f.write("data_{}\n".format(os.path.basename(fpath_res)))
        for line in cell():
            f.write(line + "\n")
        for line in symops_from_cif():
            f.write(line)
        for line in loop_atom():
            f.write(line)
        for line in density_peaks():
            f.write(line)
Пример #4
0
def gen_inflip_from_ins(fpath_ins, fpath_inflip, purpose, voxel = 'AUTO'):
    """generates .inflip file from .ins file"""
    def commands_inflip():
        inflip_inp = "{fname}\noutputfile {fname}_{job}.xplor\n"\
                     "outputformat xplor\nexpandedlog yes\n"\
                     "#coverage no\nperform fourier\ncell {cell}\n"\
                     "dimension 3\nrealdimension 3\nvoxel {voxel}\ncenters\n"\
                     "{centers}\nendcenters\nsymmetry\n{symmetry}\n"\
                     "endsymmetry\n#Keywords for charge flipping\n"\
                     "#delta AUTO\n#weakratio 0.000\n#Biso 0.000\n"\
                     "#polish yes 200\n#randomseed AUTO\n#derivesymmetry no\n"\
                     "#searchsymmetry average\n"\
                     "dataformat amplitude phase\n".format(**inflip_dict)
        for line in inflip_inp.split("\n"):
            yield line

    def reflections_inflip():
        yield "fbegin\n"
        for line in purposes[purpose](fpath_fcf):
            yield line
        yield "endf\n"
    
    def centers():
        return  centering_vecs(abs(eval(parsing_dict['LATT'])))

    def symmetry():
        """Generates centers and symmetry for inflip"""
        hasinversion = lambda parsing_dict: eval(parsing_dict['LATT']) > 0
        symmops = 'x,y,z\n' + parsing_dict["SYMM"]

        #this adds inverted symmetry operations in case LATT is less than 0        
        if hasinversion(parsing_dict):
            symmops += "\n{}".format(
                "\n".join(map(symmath.invert_symop, symmops.split("\n"))))

        return symmops

    def cell():
        return "{} {} {} {} {} {}".format(
                                *parsing_dict["CELL"].strip().split()[1:]
                                )

    def dispatcher(list_matched_objects):
        """fills parsed_dict with key-value pairs"""
        parsing_dict = dict()
        for line in list_matched_objects:
            match_obj = line.pop()
            if match_obj.group('tag') in parsing_dict:
                parsing_dict[match_obj.group('tag')] += \
                        "\n{}".format(match_obj.group('value'))
            else:
                parsing_dict[match_obj.group('tag')] = match_obj.group('value')
        return parsing_dict

    def check_list():
        return parsing_dict['LIST'] == 5

        
    inflip_dict = dict()

    purposes = {
                'Fo': gen_fo4sflip,
                'Fc': gen_fc4sflip,
                'dF': gen_df_4sflip
            }
    assert purpose in purposes.keys()
    
    fpath_fcf = fpath_ins.rsplit(".ins")[0] + ".fcf"
    tags = ['CELL', 'SYMM', 'LATT', 'LIST']
    re_compile_objs = map(lambda tag: \
            re.compile("(?P<tag>{})\\s+(?P<value>\\S+.*)".format(tag)), tags)
    filenames = generators.gen_find(os.path.basename(fpath_ins),
                                    os.path.dirname(fpath_ins))
    fobjects = generators.gen_open(filenames)
    lines = generators.gen_cat(fobjects)
    list_matched_objects = generators.gen_grep(lines, re_compile_objs,
                                                yield_line = False)
    parsing_dict  = dispatcher(list_matched_objects)
    
    inflip_dict['job'] = purpose
    inflip_dict['fname'] = os.path.basename(fpath_ins).rsplit(".ins")[0]
    inflip_dict['cell'] = cell()
    inflip_dict['centers'] = centers()
    inflip_dict['symmetry'] = symmetry()
    inflip_dict['voxel'] = voxel

    with open(fpath_inflip, 'w') as f:
        for line in commands_inflip():
            f.write(line + "\n")
        for line in reflections_inflip():
            f.write(line)