Example #1
0
def xds2mtz(xds_file, dir_name, hklout=None, run_xtriage=False, run_ctruncate=False, dmin=None, dmax=None, force_anomalous=False):
    if hklout is None:
        hklout = os.path.splitext(os.path.basename(xds_file))[0] + ".mtz"

    # if output file already exists, exit.
    if os.path.isfile(os.path.join(dir_name, hklout)):
        raise Exception(os.path.join(dir_name, hklout), "already exists.")

    # read header
    header = {}
    for l in open(xds_file):
        if l.startswith("!END_OF_HEADER"):
            break

        headers = re_xds_kwd.findall(l[l.index("!")+1:])
        for k, v in headers:
            if k == "FRIEDEL'S_LAW":
                header["FRIEDEL'S_LAW"] = v.strip()
            if k == "SPACE_GROUP_NUMBER":
                header["SPACE_GROUP_NUMBER"] = v.strip()
            if k == "X-RAY_WAVELENGTH":
                header["X-RAY_WAVELENGTH"] = v.strip() # XXX could be wrong if XSCALE result

    if force_anomalous:
        header["FRIEDEL'S_LAW"] = "FALSE"

    # make output directory
    if not os.path.isdir(dir_name):
        os.makedirs(dir_name)

    logout = open(os.path.join(dir_name, "xds2mtz.log"), "w")
    print >>logout, "xds2mtz.py running in %s" % os.getcwd()
    print >>logout, "output directory: %s" % dir_name
    print >>logout, "original file: %s" % xds_file

    print "Header information read from", xds_file

    for k in header:
        print k, "=", header[k]

    print

    ##
    # convert to MTZ
    print >>logout, "FRIEDEL'S_LAW= %s" % header["FRIEDEL'S_LAW"]
    print >>logout, ""
    logout.flush()

    if header["FRIEDEL'S_LAW"] == "TRUE":
        print xds_file, "is not anomalous dataset."
        print

        xds2mtz_normal(xds_file,
                       mtzout=os.path.join(dir_name, hklout),
                       sg=header["SPACE_GROUP_NUMBER"],
                       wavelen=header.get("X-RAY_WAVELENGTH","0"),
                       logout=logout,
                       use_ctruncate=run_ctruncate,
                       dmin=dmin, dmax=dmax)
        if run_xtriage:
            print "Running xtriage.."
            run_xtriage_in_module_if_possible(args=[hklout], wdir=dir_name)

    else:
        print xds_file, "is anomalous dataset."
        print
        xds2mtz_anom(xds_file,
                     mtzout=os.path.join(dir_name, hklout),
                     sg=header["SPACE_GROUP_NUMBER"],
                     wavelen=header.get("X-RAY_WAVELENGTH","0"),
                     logout=logout,
                     use_ctruncate=run_ctruncate,
                     dmin=dmin, dmax=dmax)
        if run_xtriage:
            print "Running xtriage.."
            run_xtriage_in_module_if_possible(args=[hklout, 'input.xray_data.obs_labels="I(+),SIGI(+),I(-),SIGI(-)"'],
                        wdir=dir_name)
Example #2
0
    def read_header(self):
        re_item = re.compile("!ITEM_([^=]+)=([0-9]+)")

        colindex = {} # {"H":1, "K":2, "L":3, ...}
        nitemfound = 0
        flag_data_start = False
        num_hkl = 0

        headers = []

        for line in open(self._filein):
            if flag_data_start:
                if line.startswith("!END_OF_DATA"):
                    break
                num_hkl += 1
                continue

            if line.startswith('!END_OF_HEADER'):
                flag_data_start = True
                continue

            if line.startswith("! ISET="):
                pars = dict(re_xds_kwd.findall(line))
                iset = int(pars["ISET"])
                if iset not in self.input_files: self.input_files[iset] = [None, None]
                if "INPUT_FILE" in pars:
                    self.input_files[iset][0] = pars["INPUT_FILE"]
                elif "X-RAY_WAVELENGTH" in pars:
                    tmp = pars["X-RAY_WAVELENGTH"]
                    if " (" in tmp: tmp = tmp[:tmp.index(" (")]
                    self.input_files[iset][1] = tmp
            else:
                headers.extend(re_xds_kwd.findall(line[line.index("!")+1:]))

        self.nx, self.ny, self.anomalous, self.zmin, self.zmax = (None,)*5


        for key, val in headers:
            if key == "NUMBER_OF_ITEMS_IN_EACH_DATA_RECORD":
                nitem = int(val.strip())
                print >>self._log, 'number of items according to header is', nitem
            elif key == "UNIT_CELL_CONSTANTS":
                a, b, c, al, be, ga = map(lambda x:float(x), val.strip().split())
            elif key == "UNIT_CELL_A-AXIS":
                self.a_axis = tuple(map(float, val.split()))
            elif key == "UNIT_CELL_B-AXIS":
                self.b_axis = tuple(map(float, val.split()))
            elif key == "UNIT_CELL_C-AXIS":
                self.c_axis = tuple(map(float, val.split()))
            elif key.startswith("ITEM_"):
                item, ind = key[len("ITEM_"):], int(val)
                colindex[item] = ind - 1
                nitemfound += 1
            elif key == "NX":
                self.nx = int(val)
            elif key == "NY":
                self.ny = int(val)
            elif key == "QX":
                self.qx = float(val)
            elif key == "QY":
                self.qy = float(val)
            elif key == "ORGX":
                self.orgx = float(val)
            elif key == "ORGY":
                self.orgy = float(val)
            elif key == "DATA_RANGE":
                self.zmin, self.zmax = map(lambda x:int(x), val.strip().split())
            elif key == "SPACE_GROUP_NUMBER":
                ispgrp = int(val.strip())
            elif key == "FRIEDEL'S_LAW":
                assert val.strip() in ("TRUE", "FALSE")
                self.anomalous = val.strip() == "FALSE"
            elif key == "DETECTOR_DISTANCE":
                self.distance = float(val)
            elif key == "X-RAY_WAVELENGTH":
                self.wavelength = float(val.split()[0])
            elif key == "INCIDENT_BEAM_DIRECTION":
                self.incident_axis = tuple(map(float, val.split()))
            elif key == "ROTATION_AXIS":
                self.rotation_axis = tuple(map(float, val.split()))
            elif key == "VARIANCE_MODEL":
                self.variance_model = tuple(map(float, val.split()))

        assert nitem == len(colindex)

        self._colindex = colindex
        self._num_hkl = num_hkl
        self.symm = crystal.symmetry(unit_cell=(a, b, c, al, be, ga),
                                     space_group=ispgrp)

        self.symm.show_summary(self._log)
        print >>self._log, 'data_range=', self.zmin, self.zmax
Example #3
0
def xds2mtz(xds_file,
            dir_name,
            hklout=None,
            run_xtriage=False,
            run_ctruncate=False,
            dmin=None,
            dmax=None,
            force_anomalous=False,
            with_multiplicity=False,
            space_group=None,
            flag_source=None,
            add_flag=False):
    if hklout is None:
        hklout = os.path.splitext(os.path.basename(xds_file))[0] + ".mtz"

    # if output file already exists, exit.
    if os.path.isfile(os.path.join(dir_name, hklout)):
        raise Exception(os.path.join(dir_name, hklout), "already exists.")

    # read header
    header = {}
    for l in open(xds_file):
        if l.startswith("!END_OF_HEADER"):
            break

        headers = re_xds_kwd.findall(l[l.index("!") + 1:])
        for k, v in headers:
            if k == "FRIEDEL'S_LAW":
                header["FRIEDEL'S_LAW"] = v.strip()
            if k == "SPACE_GROUP_NUMBER":
                header["SPACE_GROUP_NUMBER"] = v.strip()
            if k == "X-RAY_WAVELENGTH":
                header["X-RAY_WAVELENGTH"] = v.strip(
                )  # XXX could be wrong if XSCALE result

    anom_flag = header["FRIEDEL'S_LAW"] == "FALSE"
    if force_anomalous: anom_flag = True

    sginfo_org = sgtbx.space_group_info(header["SPACE_GROUP_NUMBER"])

    if space_group:
        sginfo = sgtbx.space_group_info(space_group)
    else:
        sginfo = sginfo_org

    # make output directory
    if not os.path.isdir(dir_name):
        os.makedirs(dir_name)

    print "Header information read from", xds_file
    for k in header:
        print k, "=", header[k]
    print

    logout = open(os.path.join(dir_name, "xds2mtz.log"), "w")
    print >> logout, "xds2mtz.py running in %s" % os.getcwd()
    print >> logout, "output directory: %s" % dir_name
    print >> logout, "original file: %s" % xds_file
    print >> logout, "flag_source: %s" % flag_source
    print >> logout, "space group: %s (SPACE_GROUP_NUMBER=%s; requested space_group=%s)" % (
        sginfo, header.get("SPACE_GROUP_NUMBER", ""), space_group)
    if sginfo_org.group().build_derived_reflection_intensity_group(
            False) != sginfo.group().build_derived_reflection_intensity_group(
                False):
        print >> logout, "  WARNING!! specified space group is incompatible with original file (%s)." % sginfo_org
    print >> logout, "anomalous: %s (FRIEDEL'S_LAW=%s force_anomalous=%s)" % (
        anom_flag, header["FRIEDEL'S_LAW"], force_anomalous)
    print >> logout, ""
    logout.flush()

    ##
    # convert to MTZ
    xds2mtz_work(xds_file,
                 mtzout=os.path.join(dir_name, hklout),
                 sg=str(sginfo).replace(" ", ""),
                 wavelen=header.get("X-RAY_WAVELENGTH", "0"),
                 logout=logout,
                 anom_flag=anom_flag,
                 use_ctruncate=run_ctruncate,
                 dmin=dmin,
                 dmax=dmax)

    if run_xtriage:
        print "Running xtriage.."
        args = [hklout]
        if anom_flag:
            args.append(
                'input.xray_data.obs_labels="I(+),SIGI(+),I(-),SIGI(-)"')
        run_xtriage_in_module_if_possible(args=args, wdir=dir_name)

    if with_multiplicity:
        add_multi(xds_file,
                  os.path.join(dir_name, hklout),
                  dmin=dmin,
                  dmax=dmax,
                  force_anomalous=anom_flag)

    if flag_source is not None:
        copy_test_flag(os.path.join(dir_name, hklout),
                       flag_source,
                       log_out=logout)
    elif add_flag:
        add_test_flag(os.path.join(dir_name, hklout), log_out=logout)
Example #4
0
    def read_header(self):
        re_item = re.compile("!ITEM_([^=]+)=([0-9]+)")

        colindex = {}  # {"H":1, "K":2, "L":3, ...}
        nitemfound = 0
        flag_data_start = False
        num_hkl = 0

        headers = []

        for line in open(self._filein):
            if flag_data_start:
                if line.startswith("!END_OF_DATA"):
                    break
                num_hkl += 1
                continue

            if line.startswith('!END_OF_HEADER'):
                flag_data_start = True
                continue

            if line.startswith("!Generated by dials"):
                self.by_dials = True
                continue

            if line.startswith("! ISET="):
                pars = dict(re_xds_kwd.findall(line))
                iset = int(pars["ISET"])
                if iset not in self.input_files:
                    self.input_files[iset] = [None, None, None]
                if "INPUT_FILE" in pars:
                    self.input_files[iset][0] = pars["INPUT_FILE"]
                elif "X-RAY_WAVELENGTH" in pars:
                    tmp = pars["X-RAY_WAVELENGTH"]
                    if " (" in tmp: tmp = tmp[:tmp.index(" (")]
                    self.input_files[iset][1] = tmp
                elif "UNIT_CELL_CONSTANTS" in pars:
                    tmp = pars["UNIT_CELL_CONSTANTS"]
                    self.input_files[iset][2] = tmp

            else:
                headers.extend(re_xds_kwd.findall(line[line.index("!") + 1:]))

        self.nx, self.ny, self.anomalous, self.zmin, self.zmax = (None, ) * 5

        for key, val in headers:
            if key == "NUMBER_OF_ITEMS_IN_EACH_DATA_RECORD":
                nitem = int(val.strip())
                print >> self._log, 'number of items according to header is', nitem
            elif key == "UNIT_CELL_CONSTANTS":
                a, b, c, al, be, ga = map(lambda x: float(x),
                                          val.strip().split())
            elif key == "UNIT_CELL_A-AXIS":
                self.a_axis = tuple(map(float, val.split()))
            elif key == "UNIT_CELL_B-AXIS":
                self.b_axis = tuple(map(float, val.split()))
            elif key == "UNIT_CELL_C-AXIS":
                self.c_axis = tuple(map(float, val.split()))
            elif key.startswith("ITEM_"):
                item, ind = key[len("ITEM_"):], int(val)
                colindex[item] = ind - 1
                nitemfound += 1
            elif key == "NX":
                self.nx = int(val)
            elif key == "NY":
                self.ny = int(val)
            elif key == "QX":
                self.qx = float(val)
            elif key == "QY":
                self.qy = float(val)
            elif key == "ORGX":
                self.orgx = float(val)
            elif key == "ORGY":
                self.orgy = float(val)
            elif key == "DATA_RANGE":
                self.zmin, self.zmax = map(lambda x: int(x),
                                           val.strip().split())
            elif key == "SPACE_GROUP_NUMBER":
                ispgrp = int(val.strip())
            elif key == "FRIEDEL'S_LAW":
                assert val.strip() in ("TRUE", "FALSE")
                self.anomalous = val.strip() == "FALSE"
            elif key == "DETECTOR_DISTANCE":
                self.distance = float(val)
            elif key == "X-RAY_WAVELENGTH":
                self.wavelength = float(val.split()[0])
            elif key == "INCIDENT_BEAM_DIRECTION":
                self.incident_axis = tuple(map(float, val.split()))
            elif key == "ROTATION_AXIS":
                self.rotation_axis = tuple(map(float, val.split()))
            elif key == "OSCILLATION_RANGE":
                self.osc_range = float(val.split()[0])
            elif key == "VARIANCE_MODEL":
                self.variance_model = tuple(map(float, val.split()))

        assert nitem == len(colindex)

        self._colindex = colindex
        self._num_hkl = num_hkl
        self.symm = crystal.symmetry(unit_cell=(a, b, c, al, be, ga),
                                     space_group=ispgrp)

        self.symm.show_summary(self._log)
        print >> self._log, 'data_range=', self.zmin, self.zmax
Example #5
0
def xds2mtz(xds_file,
            dir_name,
            hklout=None,
            run_xtriage=False,
            run_ctruncate=False,
            dmin=None,
            dmax=None,
            force_anomalous=False,
            with_multiplicity=False,
            sgnum=None):
    if hklout is None:
        hklout = os.path.splitext(os.path.basename(xds_file))[0] + ".mtz"

    # if output file already exists, exit.
    if os.path.isfile(os.path.join(dir_name, hklout)):
        raise Exception(os.path.join(dir_name, hklout), "already exists.")

    # read header
    header = {}
    for l in open(xds_file):
        if l.startswith("!END_OF_HEADER"):
            break

        headers = re_xds_kwd.findall(l[l.index("!") + 1:])
        for k, v in headers:
            if k == "FRIEDEL'S_LAW":
                header["FRIEDEL'S_LAW"] = v.strip()
            if k == "SPACE_GROUP_NUMBER":
                header["SPACE_GROUP_NUMBER"] = v.strip()
            if k == "X-RAY_WAVELENGTH":
                header["X-RAY_WAVELENGTH"] = v.strip(
                )  # XXX could be wrong if XSCALE result

    if force_anomalous:
        header["FRIEDEL'S_LAW"] = "FALSE"

    if sgnum is not None:
        header["SPACE_GROUP_NUMBER"] = str(sgnum)

    # make output directory
    if not os.path.isdir(dir_name):
        os.makedirs(dir_name)

    logout = open(os.path.join(dir_name, "xds2mtz.log"), "w")
    print >> logout, "xds2mtz.py running in %s" % os.getcwd()
    print >> logout, "output directory: %s" % dir_name
    print >> logout, "original file: %s" % xds_file

    print "Header information read from", xds_file

    for k in header:
        print k, "=", header[k]

    print

    ##
    # convert to MTZ
    print >> logout, "FRIEDEL'S_LAW= %s" % header["FRIEDEL'S_LAW"]
    print >> logout, ""
    logout.flush()

    if header["FRIEDEL'S_LAW"] == "TRUE":
        print xds_file, "is not anomalous dataset."
        print

        xds2mtz_normal(xds_file,
                       mtzout=os.path.join(dir_name, hklout),
                       sg=header["SPACE_GROUP_NUMBER"],
                       wavelen=header.get("X-RAY_WAVELENGTH", "0"),
                       logout=logout,
                       use_ctruncate=run_ctruncate,
                       dmin=dmin,
                       dmax=dmax)
        if run_xtriage:
            print "Running xtriage.."
            run_xtriage_in_module_if_possible(args=[hklout], wdir=dir_name)

        if with_multiplicity:
            add_multi(xds_file,
                      os.path.join(dir_name, hklout),
                      dmin=dmin,
                      dmax=dmax,
                      force_anomalous=False)

    else:
        print xds_file, "is anomalous dataset."
        print
        xds2mtz_anom(xds_file,
                     mtzout=os.path.join(dir_name, hklout),
                     sg=header["SPACE_GROUP_NUMBER"],
                     wavelen=header.get("X-RAY_WAVELENGTH", "0"),
                     logout=logout,
                     use_ctruncate=run_ctruncate,
                     dmin=dmin,
                     dmax=dmax)
        if run_xtriage:
            print "Running xtriage.."
            run_xtriage_in_module_if_possible(args=[
                hklout,
                'input.xray_data.obs_labels="I(+),SIGI(+),I(-),SIGI(-)"'
            ],
                                              wdir=dir_name)

        if with_multiplicity:
            add_multi(xds_file,
                      os.path.join(dir_name, hklout),
                      dmin=dmin,
                      dmax=dmax,
                      force_anomalous=True)