def read_from_mtz(mtzin="", colin="F,SIGF"):
    """Reads reflection and sigmas, returns a HKL_data_F_sigF_float object
    
       Parameters:
         mtzin -- a string path to an MTZ file containing measured reflections
         colin -- configurable column name, defaults to F,SIGF, could also be DANO,SIGDANO
         callback -- a function that takes care of log string and xml flushing
       
       Returns:
         a plain text log string, an XML etree and a clipper.HKL_data_F_sigf object"""

    log_string = "\n  >> clipper_tools: io.structure_factors.read_from_mtz"
    log_string += "\n     mtzin: %s" % mtzin

    xml_root = etree.Element('input_file')
    xml_root.attrib['name'] = mtzin
    xml_root.attrib['type'] = 'mini MTZ'

    hkl_data = clipper.HKL_data_F_sigF_float()
    hkl_info = clipper.HKL_info()

    if mtzin is not "":
        mtzfilein = clipper.CCP4MTZfile()
        mtzfilein.open_read(mtzin)
        mtzfilein.import_hkl_info(hkl_info, True)
        mtzfilein.import_hkl_data(hkl_data, "*/*/[" + colin + "]")
    else:
        return log_string, xml_root, hkl_data, hkl_info

    print(dir(hkl_data))

    log_string += "\n  << read_from_mtz has finished\n"
    xml_root.attrib['ok'] = 'yes'

    return log_string, xml_root, hkl_info, hkl_data
Esempio n. 2
0
 def __init__(self, mtz_in_path=None):
     self.mtz = clipper.CCP4MTZfile()
     self.hkl_info = clipper.HKL_info()
     self.mtz_in_path = mtz_in_path
     if self.mtz_in_path is not None:
         assert os.path.exists(self.mtz_in_path)
     self.spacegroup = None
     self.cell = None
     self.column_data = {}
def write_to_mtz(dataout=None, mtzout=''):
    log_string = '\n  >> clipper_tools: io.map_coefficients.write_to_mtz'
    log_string += '\n     mtzout: %s' % mtzout

    xml_root = etree.Element('output_file')
    xml_root.attrib['name'] = mtzout
    xml_root.attrib['type'] = 'mini MTZ'

    if dataout is not None and mtzout is not '':
        mtzfileout = clipper.CCP4MTZfile()
        mtzfileout.open_write(mtzout)
        mtzfileout.export_hkl_info(dataout.hkl_info())
        mtzfileout.export_hkl_data(dataout, '*/*/[F, PHI]')
        mtzfileout.close_write()
        xml_root.attrib['ok'] = 'yes'
    else:
        xml_root.attrib['ok'] = 'no'

    log_string += '\n  << write_to_mtz has finished\n'

    return log_string, xml_root
def write_to_mtz(dataout=None, mtzout=""):

    log_string = "\n  >> clipper_tools: io.map_coefficients.write_to_mtz"
    log_string += "\n     mtzout: %s" % mtzout

    xml_root = etree.Element('output_file')
    xml_root.attrib['name'] = mtzout
    xml_root.attrib['type'] = 'mini MTZ'

    if dataout is not None and mtzout is not "":
        mtzfileout = clipper.CCP4MTZfile()
        mtzfileout.open_write(mtzout)
        mtzfileout.export_hkl_info(dataout.hkl_info())
        mtzfileout.export_hkl_data(dataout, "*/*/[F, PHI]")
        mtzfileout.close_write()

        xml_root.attrib['ok'] = "yes"

    else:
        xml_root.attrib['ok'] = "no"

    log_string += "\n  << write_to_mtz has finished\n"

    return log_string, xml_root
Esempio n. 5
0
def prepare_map(mapin='', resol=8.0, callback=_callbacks.interactive_flush):
    """Reads EM map, sets origin to 0, pads cell and computes finely-sampled structure factors
    
       Parameters:
         mapin -- a string path to a map that will be read into a clipper.NXmap_float object
         resol -- estimated resolution (float)
         callback -- a function that takes care of log string and xml flushing
       
       Returns:
         a plain text log string, an XML etree and a clipper.HKL_data_F_phi_float object"""
    def determine_extent(numpy_in, tolerance):
        """Reads numpy array, determines the extent of the electron density
        
        Parameters:
          numpy_in -- a numpy array containing grid points
          tolerance -- number of points in a plane with value greater than 1 sigma
        
        Returns:
          a vector of grid indices: (min_u, max_u, min_v, max_v, min_w, max_w)"""

        log_string = ''
        min = clipper.Coord_orth()
        max = clipper.Coord_orth()

        map_mean = numpy.mean(map_numpy)
        map_std = numpy.std(map_numpy)

        mask = map_numpy > map_mean + map_std

        sum_u = sum(sum(mask))
        sum_w = sum(sum(numpy.transpose(mask)))
        sum_v = sum(numpy.transpose(sum(mask)))

        log_string += '\n  >> dumping 1D summaries of the map\'s content:\n\n  >> U:\n %s\n' % sum_u
        log_string += '\n  >> V:\n %s\n' % sum_v
        log_string += '\n  >> W:\n %s\n' % sum_w

        point_list = []

        for idx_u, val_u in enumerate(sum_u):
            if val_u > tolerance:
                point_list.append(idx_u)

        min_u = point_list[0]
        max_u = point_list[-1]

        log_string += '\n  >> First meaningful U: %i ; Last meaningful U: %i' % (
            min_u, max_u)

        point_list = []

        for idx_v, val_v in enumerate(sum_v):
            if val_v > tolerance:
                point_list.append(idx_v)

        min_v = point_list[0]
        max_v = point_list[-1]

        log_string += '\n  >> First meaningful V: %i ; Last meaningful V: %i' % (
            min_v, max_v)

        point_list = []

        for idx_w, val_w in enumerate(sum_w):
            if val_w > tolerance:
                point_list.append(idx_w)

        min_w = point_list[0]
        max_w = point_list[-1]

        log_string += '\n  >> First meaningful W: %i ; Last meaningful W: %i\n' % (
            min_w, max_w)

        extent = [min_u, max_u, min_v, max_v, min_w, max_w]

        return extent, log_string

        ################# end determine_extent ################

    ############### main function ################

    # create log string so console-based apps get some feedback
    log_string = '\n  >> clipper_tools: mr_from_em.prepare_map'
    log_string += '\n    mapin: %s' % mapin
    log_string += '\n    resol: %s' % resol

    # create XML tree, to be merged in a global structured results file
    xml_root = etree.Element('structure_factors')
    xml_root.attrib['mapin'] = mapin
    xml_root.attrib['resol'] = str(resol)
    callback(log_string, xml_root)

    phaser_params = {}
    nxmap = clipper.NXmap_double()
    xmap = clipper.Xmap_double()
    map_file = clipper.CCP4MAPfile()
    sg = clipper.Spacegroup.p1()
    resol *= 0.9
    resolution = clipper.Resolution(resol)

    # nothing in, nothing out
    if mapin == '':
        return log_string, xml_root, None

    # read the cryoEM map into nxmap, get map data irrespective of origin
    map_file.open_read(mapin)
    map_file.import_nxmap_double(nxmap)
    map_file.close_read()
    log_string += '\n  >> file %s has been read as nxmap' % mapin
    callback(log_string, xml_root)

    # read the cryoEM map into xmap to get cell dimensions, etc.
    map_file.open_read(mapin)
    map_file.import_xmap_double(xmap)
    map_file.close_read()
    log_string += '\n  >> file %s has been read as xmap' % mapin
    callback(log_string, xml_root)
    log_string += '\n  >> cell parameters: %s' % xmap.cell().format()
    log_string += '\n     original translation: %s' % nxmap.operator_orth_grid(
    ).trn()

    # put map content in a numpy data structure
    map_numpy = numpy.zeros(
        (nxmap.grid().nu(), nxmap.grid().nv(), nxmap.grid().nw()),
        dtype='double')
    log_string += '\n  >> exporting a numpy array of %i x %i x %i grid points' \
               % (nxmap.grid().nu(), nxmap.grid().nv(), nxmap.grid().nw())
    callback(log_string, xml_root)

    data_points = nxmap.export_numpy(map_numpy)
    log_string += '\n  >> %i data points have been exported' % data_points
    callback(log_string, xml_root)
    map_mean = numpy.mean(map_numpy)
    map_stdv = numpy.std(map_numpy)

    log_string += '\n  >> map mean (stdev): %.4f (%.4f)' % (map_mean, map_stdv)

    # compute the extent
    extent, temp_log = determine_extent(map_numpy, 30)
    log_string += temp_log
    extent_list = [
        extent[1] - extent[0], extent[3] - extent[2], extent[5] - extent[4]
    ]
    max_extent = max(extent_list)

    # create padded xmap and import numpy array
    origin_trans = clipper.vec3_double(
        extent[0] + ((extent[1] - extent[0]) / 2),
        extent[2] + ((extent[3] - extent[2]) / 2),
        extent[4] + ((extent[5] - extent[4]) / 2))

    large_a = (xmap.cell().a() *
               (max_extent + xmap.grid_asu().nu())) / xmap.grid_asu().nu()
    large_b = (xmap.cell().b() *
               (max_extent + xmap.grid_asu().nv())) / xmap.grid_asu().nv()
    large_c = (xmap.cell().c() *
               (max_extent + xmap.grid_asu().nw())) / xmap.grid_asu().nw()

    cell_desc = clipper.Cell_descr(large_a, large_b, large_c, \
                  xmap.cell().alpha(), xmap.cell().beta(), xmap.cell().gamma())

    large_p1_cell = clipper.Cell(cell_desc)
    large_grid_sampling = clipper.Grid_sampling(
        max_extent + xmap.grid_asu().nu(), max_extent + xmap.grid_asu().nv(),
        max_extent + xmap.grid_asu().nw())

    large_xmap = clipper.Xmap_double(sg, large_p1_cell, large_grid_sampling)

    log_string += '\n  >> new grid: nu=%i nv=%i nw=%i' % (large_xmap.grid_asu(
    ).nu(), large_xmap.grid_asu().nv(), large_xmap.grid_asu().nw())

    log_string += '\n  >> putting map into a large p1 cell...'
    log_string += '\n  >> new cell parameters: %s' % large_p1_cell.format()
    callback(log_string, xml_root)

    large_xmap.import_numpy(map_numpy)

    # dump map to disk
    map_file = clipper.CCP4MAPfile()
    map_file.open_write('mapout_padded.mrc')
    map_file.export_xmap_double(large_xmap)
    map_file.close_write()
    log_string += '\n  >> map file mapout_padded.mrc written to disk'
    callback(log_string, xml_root)

    # import it back to nxmap so we can trivially shift the origin
    map_file.open_read('mapout_padded.mrc')
    map_file.import_nxmap_double(nxmap)
    map_file.close_read()
    log_string += '\n  >> file mapout_padded.mrc has been read back as nxmap'
    callback(log_string, xml_root)

    # now shift the origin
    rtop_zero = clipper.RTop_double(nxmap.operator_orth_grid().rot(),
                                    origin_trans)

    log_string += '\n  >> moving origin...'
    log_string += '\n     original translation: %s  new origin: %s' % (
        nxmap.operator_orth_grid().trn(), rtop_zero.trn())
    callback(log_string, xml_root)

    nxmap_zero = clipper.NXmap_double(nxmap.grid(), rtop_zero)
    nxmap_zero.import_numpy(map_numpy)

    # dump map to disk
    map_file.open_write('mapout_padded_zero.mrc')
    map_file.export_nxmap_double(nxmap_zero)
    map_file.close_write()
    log_string += '\n  >> map file mapout_padded_zero.mrc written to disk'
    callback(log_string, xml_root)

    # read it back to an xmap so we can fft-it
    new_xmap = clipper.Xmap_double()
    map_file.open_read('mapout_padded_zero.mrc')
    map_file.import_xmap_double(new_xmap)
    map_file.close_read()
    log_string += '\n  >> map file mapout_padded_zero.mrc read back as xmap'
    callback(log_string, xml_root)

    # create HKL_info using user-supplied resolution parameter
    hkl_info = clipper.HKL_info(sg, large_p1_cell, resolution, True)

    # fft the map
    f_phi = clipper.HKL_data_F_phi_float(hkl_info, large_p1_cell)
    log_string += '\n  >> now computing map coefficients to %0.1f A resolution...' % resol
    callback(log_string, xml_root)

    new_xmap.fft_to(f_phi)
    log_string += '\n  >> writing map coefficients to MTZ file mapout_padded_zero.mtz'
    callback(log_string, xml_root)

    # setup an MTZ file so we can export our map coefficients
    mtzout = clipper.CCP4MTZfile()
    mtzout.open_write('mapout_padded_zero.mtz')
    mtzout.export_hkl_info(f_phi.hkl_info())
    mtzout.export_hkl_data(f_phi, '*/*/[F, PHI]')
    mtzout.close_write()
    log_string += '\n  >> all done'
    callback(log_string, xml_root)

    return log_string, xml_root, f_phi, phaser_params
      arg += 1
      limit_e = float(sys.argv[arg])
    elif ( sys.argv[arg] == "-e-weight" ):
      arg += 1
      weight_e = float(sys.argv[arg])
    elif ( sys.argv[arg] == "-anomalous" ):
      adiff = True;
    elif ( sys.argv[arg] == "-origin-removal" ):
      oremv = True;

if len(sys.argv) <= 1:
    print "Usage: cpatterson\n\t-mtzin <filename>\n\t-mapout <filename>\n\t-colin-fo <colpath>\n\t-colin-fano <colpath>\n\t-colin-fdiff <colpath>\n\t-resolution <reso>\n\t-grid <nu>,<nv>,<nw>\n\t-e-limit <limit>\n\t-e-weight <weight>\n\t-anomalous\n\t-origin-removal\nCalculate Patterson from Fobs or Fano.\nFor E-Patterson <weight> = 1, for F-Patterson <weight> = 0, and other values.\n<limit> can reject reflections or differences by E-value.\n";
    sys.exit(1);

# make data objects
mtzin =  clipper.CCP4MTZfile()
cxtl =clipper.MTZcrystal()
hkls = clipper.HKL_info()
hklp = clipper.HKL_info()
#  typedef clipper::HKL_data_base::HKL_reference_index HRI; ??
mtzin.set_column_label_mode( clipper.CCP4MTZfile.Legacy );

# open file
mtzin.open_read( ipfile );
spgr = mtzin.spacegroup();

print spgr.symbol_hm()
cell = mtzin.cell();
print cell.a(),cell.b(),cell.c(),cell.alpha(),cell.beta(),cell.gamma()

if ( ipcolf != "NONE" ):
def smartSplitMTZ(inputFilePath=None,
                  inputColumnPath=None,
                  objectPath=None,
                  intoDirectory=None):
    if inputFilePath is None:
        raise Exception("smartSplitMTZ Exception:",
                        "Must provide an input file")
    if not os.path.isfile(inputFilePath):
        raise Exception("smartSplitMTZ Exception:",
                        "inputFile must exist" + str(inputFilePath))
    if inputColumnPath is None:
        raise Exception(
            "smartSplitMTZ Exception:",
            "Must provide an input columnPath e.g. '/*/*/[F,SIGFP]'")
    if objectPath is not None and intoDirectory is not None:
        raise Exception(
            "smartSplitMTZ Exception:",
            "Provide either full output path for file, or name of directory where file should be placed"
        )
    if objectPath is None and intoDirectory is None:
        raise Exception(
            "smartSplitMTZ Exception:",
            "Provide either full output path for file, or name of directory where file should be placed"
        )

    mtz_file = clipper.CCP4MTZfile()
    hkl_info = clipper.HKL_info()
    mtz_file.open_read(inputFilePath)
    mtz_file.import_hkl_info(hkl_info)
    xtal = clipper.MTZcrystal()
    mtz_file.import_crystal(xtal, inputColumnPath)
    dataset = clipper.MTZdataset()
    mtz_file.import_dataset(dataset, inputColumnPath)
    providedColumnPaths = mtz_file.column_paths()

    selectedColumnLabelsExp = re.compile(
        r"^/(?P<XtalName>[A-Za-z0-9_. -+\*,]+)/(?P<DatasetName>[A-Za-z0-9_. -+\*,]+)/\[(?P<Columns>[A-Za-z0-9_. -+\*,]+)\]"
    )
    columnsMatch = selectedColumnLabelsExp.search(inputColumnPath)
    selectedColumnLabelExp = re.compile(
        r"^/(?P<XtalName>[A-Za-z0-9_. -+\*,]+)/(?P<DatasetName>[A-Za-z0-9_. -+\*,]+)/(?P<Column>[A-Za-z0-9_. -+\*,]+)"
    )
    columnMatch = selectedColumnLabelExp.search(inputColumnPath)
    if columnsMatch is not None:
        selectedColumnPaths = [
            "/{}/{}/{}".format(columnsMatch.group("XtalName"),
                               columnsMatch.group("DatasetName"), column)
            for column in columnsMatch.group("Columns").split(",")
        ]
    elif columnMatch is not None:
        selectedColumnPaths = [
            "/{}/{}/{}".format(columnMatch.group("XtalName"),
                               columnMatch.group("DatasetName"),
                               columnMatch.group("Column"))
        ]

    typeSignature = ""
    for selectedColumnPath in selectedColumnPaths:
        selectedColumnMatch = selectedColumnLabelExp.search(selectedColumnPath)
        for providedColumnPath in providedColumnPaths:
            #Generating clipper String and then calling str to deal with
            #Known unpredictable bug in clipper-python
            try:
                columnName, columnType = str(
                    clipper.String(providedColumnPath)).split(" ")
            except NotImplementedError as err:
                columnName, columnType = str(providedColumnPath).split(" ")
            parsedColumnMatch = selectedColumnLabelExp.search(columnName)
            if ((selectedColumnMatch.group("XtalName") == "*"
                 or selectedColumnMatch.group("XtalName")
                 == parsedColumnMatch.group("XtalName"))
                    and (selectedColumnMatch.group("DatasetName") == "*"
                         or selectedColumnMatch.group("DatasetName")
                         == parsedColumnMatch.group("DatasetName"))
                    and selectedColumnMatch.group("Column")
                    == parsedColumnMatch.group("Column")):
                typeSignature += columnType
                break

    if typeSignature == "FQ":
        extractedData = clipper.HKL_data_F_sigF_float(hkl_info)
        cls = CCP4XtalData.CObsDataFile
        contentType = 4
    if typeSignature == "JQ":
        extractedData = clipper.HKL_data_I_sigI_float(hkl_info)
        cls = CCP4XtalData.CObsDataFile
        contentType = 3
    if typeSignature == "GLGL" or typeSignature == "FQFQ":
        extractedData = clipper.HKL_data_F_sigF_ano_float(hkl_info)
        cls = CCP4XtalData.CObsDataFile
        contentType = 2
    if typeSignature == "KMKM" or typeSignature == "JQJQ":
        extractedData = clipper.HKL_data_I_sigI_ano_float(hkl_info)
        cls = CCP4XtalData.CObsDataFile
        contentType = 1
    elif typeSignature == "AAAA":
        extractedData = clipper.HKL_data_ABCD_float(hkl_info)
        cls = CCP4XtalData.CPhsDataFile
        contentType = 1
    elif typeSignature == "PW":
        extractedData = clipper.HKL_data_Phi_fom_float(hkl_info)
        cls = CCP4XtalData.CPhsDataFile
        contentType = 2
    elif typeSignature == "I":
        extractedData = clipper.HKL_data_Flag(hkl_info)
        cls = CCP4XtalData.CFreeRDataFile
        contentType = 1
    outputColumnPath = "[{}]".format(','.join(
        getattr(cls, "CONTENT_SIGNATURE_LIST")[contentType - 1]))

    mtz_file.import_hkl_data(extractedData, inputColumnPath)
    mtz_file.close_read()

    if intoDirectory is not None:
        firstGuess = os.path.join(
            intoDirectory,
            typeSignature + '_ColumnsFrom_' + os.path.split(inputFilePath)[1])
        objectPath = availableNameBasedOn(firstGuess)

    mtzout = clipper.CCP4MTZfile()
    mtzout.open_write(objectPath)
    mtzout.export_hkl_info(hkl_info)
    outputColumnPath = "/{}/{}/{}".format(str(xtal.crystal_name()),
                                          str(dataset.dataset_name()),
                                          outputColumnPath)
    mtzout.export_crystal(xtal, outputColumnPath)
    mtzout.export_dataset(dataset, outputColumnPath)
    mtzout.export_hkl_data(extractedData, outputColumnPath)
    mtzout.close_write()

    return objectPath