Exemplo n.º 1
0
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
Exemplo 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 = {}
Exemplo n.º 3
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
Exemplo n.º 4
0
def calculate_map ( mtzin = "",
                    colin_fo = "F,SIGF",
                    colin_dano = "",
                    resol = None,
                    callback = callbacks.interactive_flush ) :
    """Reads EM map, sets origin to 0, pads cell and computes finely-sampled structure factors
    
       Parameters:
         mtzin -- a string path to an MTZ file containing measured amplitudes
         colin-fo -- configurable column name, defaults to F,SIGF
         colin-dano -- empty by default; if specified, patterson will be calculated on anomalous differences
         resol -- resolution cutoff (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"""


    # create log string so console-based apps get some feedback
    log_string = "\n  >> clipper_tools: xray.patterson.calculate_map"
    log_string += "\n    mtzin: %s" % mtzin
    log_string += "\n    colin_fo: %s" % colin_fo
    log_string += "\n    colin_dano: %s" % colin_dano


    # create XML tree, to be merged in a global structured results file
    xml_root = etree.Element('structure_factors')
    xml_root.attrib['mtzin'] = mtzin
    xml_root.attrib['colin_fo'] = colin_fo
    xml_root.attrib['colin_dano'] = colin_dano

    from clipper_tools.io.structure_factors import read_from_mtz
    log_sub, xml_sub, hkl_info, hkl_data = read_from_mtz ( mtzin, colin_fo )

    if resol is not None :
        resolution_cutoff = clipper.Resolution(resol)
    else :
        resolution_cutoff = hkl_info.resolution()

    log_string += "\n    resol: %.2f" % ( resolution_cutoff.limit() )
    xml_root.attrib['resol'] = "%.2f" % ( resolution_cutoff.limit() )
    callback( log_string, xml_root  )

    p_spgr = clipper.Spacegroup ( clipper.Spgr_descr (hkl_info.spacegroup().generator_ops().patterson_ops()) )
    p_hklinfo = clipper.HKL_info ( p_spgr, hkl_info.cell(), resolution_cutoff, True )
    f_phi = clipper.HKL_data_F_phi_float (p_hklinfo) # map coefficients to be calculated later

    numpy_data = numpy.zeros ((hkl_data.data_size() * len ( hkl_data )), numpy.float)
    numpy_coef = numpy.zeros ((hkl_data.data_size() * len ( hkl_data )), numpy.float)

    print "data array size is %i" % (hkl_data.data_size() * len ( hkl_data ))

    n_data = hkl_data.export_numpy ( numpy_data )

    numpy_data = numpy_data.reshape ( ( -1, 2) ) # so we have column 0 = F, column 1 = SIGF
    print "len(f_phi)=%i   len(original_data)= %i    len(numpy_f_phi)=%i and n_data is %i" % (len(f_phi), len(hkl_data), len(numpy_data), n_data)
    print numpy_data



    numpy_data[:,1] = 0 # we are going to re-use the F,SIGF numpy array as F,PHI
                        # so we need to set PHI = 0 for the Patterson map

    numpy_data[:,0] = numpy.power ( numpy_data[:,0], 2 ) # square the amplitudes

    numpy_data = numpy_data.reshape ( -1 ) # need to put it back in a 1-D array for export
    f_phi.import_numpy ( numpy_data )

    from clipper_tools.io.map_coefficients import write_to_mtz
    log_sub, xml_sub = write_to_mtz ( f_phi, "patterson.mtz" )
    callback (log_string, xml_root )

    return
Exemplo n.º 5
0
    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" ):
  mtzin.import_crystal( cxtl, ipcolf+".F_sigF.F" );
if ( ipcola != "NONE" ):
Exemplo n.º 6
0
print stats.range_double().max()
print stats.range_double().min()
print stats.range_double().contains(0)
print stats.range_double().contains(100)

c1 = clipper.Coord_orth(1, 2, 3)
c2 = clipper.Coord_orth(10, 10, 10)

c = c1 + c2
cm = -c

print c.x(), c.y(), c.z()
print cm.x(), cm.y(), cm.z()

cif = clipper.CIFfile()
mydata = clipper.HKL_info()

if len(sys.argv) > 2:
    cif.open_read(sys.argv[2])
    cif.import_hkl_info(mydata)
    print dir(mydata)
    sg, cell = mydata.spacegroup(), mydata.cell()
    print sg.symbol_laue()
    print sg.symbol_hall()
    print cell
    print cell.a(), cell.b(), cell.c(), cell.alpha(), cell.beta(), cell.gamma()
    myfsigf = clipper.HKL_data_F_sigF_float(mydata)
    status = clipper.HKL_data_Flag(mydata)
    cif.import_hkl_data(myfsigf)
    cif.import_hkl_data(status)
    cif.close_read()
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
    def test_clipper_numpy(self, verbose=False):
        '''
        Test numpy clipper-python bindings and CIF reading.
        '''

        cif = clipper.CIFfile()
        mydata = clipper.HKL_info()
        cif_in_path = os.path.join(self.test_data_path, '4x1v.cif')
        assert os.path.exists(cif_in_path)
        cif.open_read(cif_in_path)
        cif.import_hkl_info(mydata)
        sg, cell = mydata.spacegroup(), mydata.cell()
        self.assertAlmostEquals(cell.a(), 44.59, places=3)
        self.assertAlmostEquals(cell.b(), 44.59, places=3)
        self.assertAlmostEquals(cell.c(), 32.94, places=3)
        self.assertAlmostEquals(cell.alpha(), 1.570796, places=6)
        self.assertAlmostEquals(cell.beta(), 1.570796, places=6)
        self.assertAlmostEquals(cell.gamma(), 1.570796, places=6)
        myfsigf = clipper.HKL_data_F_sigF_float(mydata)
        status = clipper.HKL_data_Flag(mydata)
        cif.import_hkl_data(myfsigf)
        cif.import_hkl_data(status)
        cif.close_read()
        fsigf_numpy = numpy.zeros((myfsigf.data_size() * len(myfsigf)),
                                  numpy.float)
        myfsigf.getDataNumpy(fsigf_numpy)
        i = 0
        test_f_sig_f_data = [
            5.54820013046, 1.37300002575, 3.76679992676, 1.04229998589,
            217.498001099, 2.99740004539, 4.34929990768, 1.16820001602,
            5.93720006943, 1.57710003853, 3.85949993134, 1.11129999161,
            157.050003052, 2.50629997253, 6.6061000824, 2.30500006676,
            11.4969997406, 2.55789995193, 5.8341999054, 1.54460000992,
            180.908996582, 3.49650001526, 6.76160001755, 2.36299991608,
            7.12340021133, 2.56310009956, 5.70559978485, 1.66779994965,
            80.7269973755, 2.87069988251, 8.69659996033, 3.13499999046,
            15.4949998856, 3.25819993019, 111.319999695, 1.08910000324,
            105.82900238, 1.37000000477, 193.830993652, 1.60860002041,
            51.813999176, 0.959100008011, 102.278999329, 1.15299999714,
            97.1070022583, 1.23099994659, 261.845001221, 1.74699997902,
            156.783004761, 1.9959000349, 71.9779968262, 1.62220001221,
            52.9140014648, 1.18400001526, 73.2170028687, 1.23230004311,
            89.4909973145, 1.25510001183, 38.7389984131, 1.13900005817,
            69.7870025635, 1.2460000515, 36.9379997253, 1.25059998035,
            54.6240005493, 1.60409998894, 29.7430000305, 1.81970000267,
            44.4739990234, 1.54340004921, 14.9239997864, 1.64839994907,
            10.498000145, 2.10789990425, 62.6800003052, 1.29030001163,
            152.975006104, 1.29550004005, 48.2270011902, 0.990499973297,
            363.785003662, 2.64770007133, 9.69950008392, 1.07669997215,
            98.0699996948, 1.13880002499, 42.6469993591, 1.12839996815,
            174.261001587, 1.52559995651, 262.213989258, 2.44720005989
        ]
        j = 0
        f_list = []
        for i in range(20):
            n = fsigf_numpy[i]
            if not numpy.isnan(fsigf_numpy[i]):
                self.assertAlmostEquals(fsigf_numpy[i],
                                        test_f_sig_f_data[j],
                                        places=6)
                j += 1
            if i % 2 == 0:
                f_list.append(n)

        # N.B. getDataNumpy returns 1D array:
        #        [F, sigF, F, sigF...]
        # Convert to 2D array with columns F and sigF
        fsigf_numpy = numpy.reshape(fsigf_numpy, (-1, 2))
        fsigf_numpy = numpy.transpose(fsigf_numpy)
        print(fsigf_numpy.shape)
        for i in range(10):
            if not numpy.isnan(fsigf_numpy[0][i]):
                assert fsigf_numpy[0][i] == f_list[i]
Exemplo n.º 9
0
def cut_by_model(mapin="",
                 pdbin="",
                 ipradius=1.5,
                 ipresol=8.0,
                 ipbfact=0.0,
                 callback=callbacks.interactive_flush):

    xmap = clipper.Xmap_double()
    sg = clipper.Spacegroup.p1()
    resolution = clipper.Resolution(ipresol)

    # create log string so console-based apps get some feedback
    log_string = "\n  >> clipper_tools: em.cut_density.cut_by_model"
    log_string += "\n    mapin: %s" % mapin
    log_string += "\n    pdbin: %s" % pdbin
    log_string += "\n    bfact: %s" % ipbfact
    log_string += "\n    resol: %s" % ipresol
    log_string += "\n    radius: %s" % ipradius

    # create XML tree, to be merged in a global structured results file
    xml_root = etree.Element('program')
    xml_root.attrib['name'] = 'cut_by_model'
    xml_root.attrib['user'] = getpass.getuser()
    xml_root.attrib['date'] = time.strftime("%c")

    params = etree.SubElement(xml_root, 'parameters')
    params.attrib['mapin'] = mapin
    params.attrib['pdbin'] = pdbin
    params.attrib['b_factor'] = str(ipbfact)
    params.attrib['resolution'] = str(ipresol)
    params.attrib['mask_radius'] = str(ipradius)
    callback(log_string, xml_root)

    # nothing in, nothing out
    if mapin == "" or pdbin == "":
        return log_string, xml_root, None

    # read the input atomic model
    from clipper_tools.io.molecules import read_pdb
    log_string_sub, xml_sub, mmol = read_pdb(pdbin)
    log_string += log_string_sub
    xml_root.append(xml_sub)

    callback(log_string, xml_root)

    # read the cryoEM map into xmap to get cell dimensions, etc.
    from clipper_tools.io.maps import read_xmap
    log_sub, xml_sub, xmap = read_xmap(mapin)

    log_string += log_sub
    xml_root.append(xml_sub)
    callback(log_string, xml_root)

    grid_sampling = clipper.Grid_sampling(xmap.grid_asu().nu(),
                                          xmap.grid_asu().nv(),
                                          xmap.grid_asu().nw())

    log_string += "\n  >> cell parameters: %s" % xmap.cell().format()
    callback(log_string, xml_root)

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

    atom_list = mmol.model().atom_list()

    mask = clipper.Xmap_float(xmap.spacegroup(), xmap.cell(), grid_sampling)

    masker = clipper.EDcalc_mask_float(ipradius)
    masker.compute(mask, atom_list)

    mask_matrix = numpy.zeros(
        (xmap.grid_asu().nu(), xmap.grid_asu().nv(), xmap.grid_asu().nw()),
        dtype='double')
    mask_points = mask.export_numpy(mask_matrix)

    log_string += "\n  >> the original map has %i points and the computed mask has %i points" % (
        data_points, mask_points)
    callback(log_string, xml_root)

    masked_array = map_numpy * mask_matrix

    log_string += "\n  >> non-zero values: original= %i ; mask=%i ; product=%i" % (
        numpy.count_nonzero(map_numpy), numpy.count_nonzero(mask_matrix),
        numpy.count_nonzero(masked_array))

    xmap.import_numpy(masked_array)

    # create HKL_info using user-supplied resolution parameter
    hkl_info = clipper.HKL_info(xmap.spacegroup(), xmap.cell(), resolution,
                                True)

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

    xmap.fft_to(f_phi)
    log_string += "\n  >> writing map coefficients to MTZ file mapout_cut_density.mtz"
    callback(log_string, xml_root)

    if ipbfact != 0.0:
        f_phi.compute_scale_u_iso_fphi(1.0, clipper.Util.b2u(-ipbfact), f_phi)
        log_string += "\n  >> and applying B factor correction - using %3.2f\n" % ipbfact

    # setup an MTZ file so we can export our map coefficients
    from clipper_tools.io.map_coefficients import write_to_mtz
    log_sub, xml_sub = write_to_mtz(f_phi, "mapout_cut_density.mtz")

    log_string += log_sub
    xml_root.append(xml_sub)

    log_string += "\n  >> all done"
    xml_root.attrib['ok'] = 'yes'
    callback(log_string, xml_root)

    from clipper_tools.callbacks import offline_flush
    offline_flush(log_string, xml_root)