Esempio n. 1
0
def run(args=None, l=None):
    '''
    Check additivity of motion in reciprocal space, subtract mtz files
    '''

    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    if l == None:
        l = Log(log_name='B_noise_log.txt')
    l.title("exp.B_noise module")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = B_noise_phil.phil_parse(
        args=args, log=l)  # use phil to process input
    p = working_params.B_noise

    # Read PDB
    pdb_f = PDBClass(
        fname=p.input.pdb_in,
        selection="not (resname HOH) and not (resname CL) and not (resname NA)"
    )
    # Loop over all atoms
    for atom in pdb_f.hierarchy.models()[0].atoms():
        atom.b = random.uniform(0.0, 0.1)
        # Write PDB to file
    pdb_f.write_hierarchy_to_pdb(output_hierarchy=pdb_f.hierarchy,
                                 out_name='{}_B_noise.pdb'.format(
                                     p.input.pdb_in[:-4]))
Esempio n. 2
0
def run(args=None, l=None):
    '''
    Check additivity of motion in reciprocal space, subtract mtz files
    '''

    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    if l == None:
        l = Log(log_name='additivity_log.txt')
    l.title("exp.additivity module")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = additivity_phil.phil_parse(
        args=args, log=l)  # use phil to process input
    p = working_params.additivity

    ###########################################################################
    #                           Read mtz_files                                #
    ###########################################################################

    l.process_message('Reading mtz_1...')
    mtz_1 = MTZClass(fname=p.input.mtz_1, array_name='IDFF')

    l.process_message('Reading mtz_2...')
    mtz_2 = MTZClass(fname=p.input.mtz_2, array_name='IDFF')

    #    l.process_message('Calculating R-value and CC...')
    #    mtz_correlation(mtz_1 = mtz_1,
    #                    mtz_2= mtz_2,
    #                    super_1 = 1,
    #                    super_2 = 1,
    #                    l = l)

    temp_array = mtz_1.miller_array.deep_copy()
    t = temp_array.data() - mtz_2.miller_array.data()
    temp_miller = mtz_1.miller_array.customized_copy(data=t)

    mtz_1.write_array_as_mtz(array=temp_miller,
                             label='IDFF',
                             mtz_out_name=p.output.mtz_out)

    ###########################################################################
    #                            Close Log File                               #
    ###########################################################################

    return l
Esempio n. 3
0
def run(args=None, l=None):
    '''
    Check additivity of motion in reciprocal space, subtract mtz files
    '''

    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    if l == None:
        l = Log(log_name='cypa_helix_log.txt')
    l.title("exp.cypa_helix module")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = cypa_helix_phil.phil_parse(
        args=args, log=l)  # use phil to process input
    p = working_params.cypa_helix

    ###########################################################################
    #                           Read PDB file                                 #
    ###########################################################################

    pdb_object = PDBClass(
        fname=p.input.pdb_in,
        selection="not (resname HOH) and not (resname CL) and not (resname NA)"
    )

    # HELIX residues: 30-42
    res_list = range(30, 43)
    t_list = np.arange(-.50, 0.52, 0.01)
    final_hierarchy = iotbx.pdb.hierarchy.root()
    for t in t_list:
        new_model = pdb_object.hierarchy.models()[0].detached_copy()
        for resi in res_list:
            for at in new_model.chains()[0].residues()[resi].atoms():
                at.xyz = tuple(np.array(at.xyz) + np.array([0, 0, t]))
        final_hierarchy.append_model(new_model)

    final_hierarchy.write_pdb_file(crystal_symmetry=pdb_object.symmetry,
                                   file_name=p.input.pdb_in[:-4] +
                                   '_helix.pdb')
Esempio n. 4
0
def run(args=None, l=None):
    '''
    First FFT structure
    Then calculate rvalues between output and other MTZ
    while splitting Itot and Idiff miller indices
    '''

    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    if l == None:
        l = Log(log_name='Rfacts_log.txt')
    l.title("exp.Rfacts module")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = Rfacts_phil.phil_parse(args=args,
                                            log=l)  # use phil to process input
    p = working_params.Rfacts

    ###########################################################################
    #                         FFT structure                                   #
    ###########################################################################

    # If input structure and data are to be compared FFT input structure first
    if p.input.er_mtz == None:
        pdb_1 = PDBClass(
            fname=p.input.pdb_1,
            selection=
            "not (resname HOH) and not (resname CL) and not (resname NA)")
        # create xray structure from model 1 in pdb_1
        xray_structure = pdb_1.hierarchy.extract_xray_structure(
            crystal_symmetry=pdb_1.symmetry)
        # Get default fmodel parameters
        params = mmtbx.command_line.fmodel.\
                 fmodel_from_xray_structure_master_params.extract()
        # Input high resolution
        params.high_resolution = 2.0
        mtz_name = '{}.mtz'.format(p.input.pdb_1[:-4])
        # Convert structure to structurefactors
        mmtbx.utils.fmodel_from_xray_structure(xray_structure=xray_structure,
                                               f_obs=None,
                                               add_sigmas=False,
                                               params=params,
                                               twin_law=None,
                                               twin_fraction=None,
                                               out=sys.stdout).write_to_file(
                                                   file_name=mtz_name,
                                                   obs_type=complex)
        l.process_message('Structure FFTed and written to file')

        ###########################################################################
        #                   Read mtz's and calc R-factors                         #
        ###########################################################################

        # Read miller array 1
        mtz_1 = MTZClass('{}.mtz'.format(p.input.pdb_1[:-4]), p.params.array_1)
        # Read miller array 2
        mtz_2 = MTZClass(p.input.mtz_2, p.params.array_2)

    if p.input.er_mtz != None:
        array_1 = 'F-obs-filtered_xray'
        array_2 = 'F-model_xray'
        # Read er_mtz twice (2 columns needed)
        mtz_1 = MTZClass(p.input.er_mtz, array_1)
        mtz_2 = MTZClass(p.input.er_mtz, array_2)

    l.process_message('MTZ files read...')
    l.process_message('Splitting data in IDFF and IBRG...')

    # Get miller array
    ma_1 = mtz_1.miller_array
    ma_2 = mtz_2.miller_array

    # Calculate scale factor between 2 columns
    sfactor = sf(ma_1=ma_1, ma_2=ma_2, l=l)
    # Mask Diffuse
    ma_1_indices = np.array(ma_1.indices())
    m_brg = flex.bool(((ma_1_indices % 2).sum(axis=1) == 0))

    # Save R-values in this
    final_r = []
    # Calc R-Bragg
    nr = calculate_R(ma_1=ma_1,
                     ma_2=ma_2,
                     mask=m_brg,
                     name='Bragg',
                     sf=sfactor,
                     l=l)
    final_r.append(nr)

    # Mask Bragg
    m_dff = flex.bool(((ma_1_indices % 2).sum(axis=1) != 0))

    nr = calculate_R(ma_1=ma_1,
                     ma_2=ma_2,
                     mask=m_dff,
                     name='IDFF',
                     sf=sfactor,
                     l=l)
    final_r.append(nr)

    # Write to File
    if p.input.er_mtz != None:
        nm = '{}_ER_Rval.txt'.format(p.input.er_mtz[:-4])
    else:
        nm = '{}_input_Rval'.format(p.input.mtz_2[:-4])
    with open(nm, 'w') as f:
        for i in final_r:
            print >> f, i

    return l
Esempio n. 5
0
def run(args=None, l=None):
    '''
    jan, 2018
    Additivity check

    TODO:
    - 2 PDF files:
      - Single structre
      - Ensemble, internal motion only
    - Input sigma for translation is similar
    - Create 2 disorder models from same translation sigma's:
      - Translation only
      - Internal + Translation
    - Plot B-factor from ensembles:
      - Internal only
      - Internal + Translation
      - Translation only
    - Calculate diffuse scattering for 3 models
      - Internal only
      - Translation only
      - Internal + Translation
    - Slice IDFF 0kl,h0l,hk0 from mtz, save as np.array
    - Plots
    - Subtract Translation only array from Internal+Translation
    - Plot difference array
    '''

    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    if l == None:
        l = Log(log_name='addLoes_log.txt')
    l.title("exp.addLoes module")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = addLoes_phil.phil_parse(
        args=args, log=l)  # use phil to process input
    p = working_params.addLoes

    ###########################################################################
    #                           Input files                                   #
    ###########################################################################

    # If translation models should be created:
    if p.params.do_rb == True:
        l.process_message('Applying rigid body operation')
        # Read files
        single_pdb = PDBClass(
            fname=p.input.single_pdb,
            selection=
            "not (resname HOH) and not (resname CL) and not (resname NA)")
        internal_pdb = PDBClass(
            fname=p.input.internal_pdb,
            selection=
            "not (resname HOH) and not (resname CL) and not (resname NA)")
        l.process_message('Input models read...')
        # Input params
        sigma = p.params.trans_sigma
        nr_out = p.params.nr_models
        l.show_info('Tanslation sigma: {}'.format(sigma))

        ###########################################################################
        #                           Create Models                                 #
        ###########################################################################

        # Translate single_pdb:
        single_pdb.model_list = single_pdb.create_random_model_list(
            l=l, ensemble_size=nr_out)
        single_pdb.set_B_zero()
        # Set occupancy to 1
        single_pdb.set_occ()
        center_of_mass = single_pdb.calculate_center_of_mass()
        # Create final ensemble, ER result + added btls motion
        single_pdb.make_rot_trans_ens(trans_only=True,
                                      rot_only=False,
                                      trans_sigma=sigma,
                                      rot_sigma=0.0,
                                      center_of_mass=center_of_mass,
                                      l=l)
        # Write final ensemble
        single_pdb.write_hierarchy_to_pdb(out_name=p.input.translation_pdb)
        l.process_message('Translation only done...')
        l.show_info('Written to: {}'.format(p.input.translation_pdb))

        # Translate internal internal_pdb
        internal_pdb.model_list = internal_pdb.create_random_model_list(
            l=l, ensemble_size=nr_out)
        internal_pdb.set_B_zero()
        # Set occupancy to 1
        internal_pdb.set_occ()
        center_of_mass = internal_pdb.calculate_center_of_mass()
        # Create final ensemble, ER result + added btls motion
        internal_pdb.make_rot_trans_ens(trans_only=True,
                                        rot_only=False,
                                        trans_sigma=sigma,
                                        rot_sigma=0.0,
                                        center_of_mass=center_of_mass,
                                        l=l)
        # Write final ensemble
        internal_pdb.write_hierarchy_to_pdb(
            out_name=p.input.int_translation_pdb)
        l.process_message('Translation + Internal motion done...')
        l.show_info('Written to: {}'.format(p.input.int_translation_pdb))

###########################################################################
#                           Plot B-factors                                #
###########################################################################

# If true, run ens2b on the created ensembles
    if p.params.do_bfactor == True:
        fls = [
            p.input.translation_pdb, p.input.internal_pdb,
            p.input.int_translation_pdb
        ]
        for f in fls:
            args = [
                'pdb_in={}'.format(f), 'plot_dat={}'.format(f[:-4] + '.json')
            ]
            ens2b.run(args=args, l=l)

###########################################################################
#                           Calculate IDFF                                #
###########################################################################

# If diffuse scattering should be calculated from models
    if p.params.do_diff_calc == True:
        l.process_message('Performing diffuse scattering calculations...')
        # Parameters for diffuse scattering calculation
        supercell_num = 100
        size_h = p.params.size_h
        size_k = p.params.size_k
        size_l = p.params.size_l
        Ncpu = 10

        # Loop over three models
        for pdb in [
                p.input.internal_pdb, p.input.translation_pdb,
                p.input.int_translation_pdb
        ]:
            l.show_info('PDB in: {}'.format(pdb))
            # Easy method to calculate diffuse scattering
            ex = EXPClass()
            ex.calc_diffuse_and_map(pdb=pdb,
                                    supercell_num=supercell_num,
                                    size_h=size_h,
                                    size_k=size_k,
                                    size_l=size_l,
                                    Ncpu=Ncpu,
                                    l=l)
        l.process_message('Diffuse scattering calculated...')

###########################################################################
#                           Slice MTZ files                               #
###########################################################################

# If true, slice IDFF mtz files, save 3 npy arrays
    if p.params.do_slice == True:
        l.process_message('Slicing mtz files and writing .npy arrays')
        # Input parameters
        fls = [
            p.input.translation_pdb[:-4] + '.mtz',
            p.input.int_translation_pdb[:-4] + '.mtz',
            p.input.internal_pdb[:-4] + '.mtz'
        ]
        # Plotting parameters
        vmin = p.params.vmin
        vmax = p.params.vmax

        # Loop over mtz files
        for i, n in enumerate(fls):
            if i == 2:
                vmin = p.params.vmin_internal
                vmax = p.params.vmax_internal
            l.show_info('mtz: {}'.format(n))
            args = [
                'mtz_in={}'.format(n), 'vmin={}'.format(vmin),
                'vmax={}'.format(vmax), 'array=IDFF', 'write_slices=True'
            ]
            slice_mtz.run(args=args, l=l)

###########################################################################
#                           Subtractions                                  #
###########################################################################

    import matplotlib.pyplot as plt
    # Perform actual experiment
    if p.params.do_subtractions == True:
        l.process_message('Subtracting Arrays')
        vmin = p.params.vmin_sub
        vmax = p.params.vmax_sub
        for ind in ['h', 'k', 'l']:
            total = np.load(p.input.int_translation_pdb[:-4] +
                            '_{}.npy'.format(ind))
            s_1 = np.load(p.input.translation_pdb[:-4] + '_{}.npy'.format(ind))
            sub = total - s_1
            thing = ((sub > 0.0).astype(float) * 2) - 1
            print thing[0, 0]
            fig = plt.figure(figsize=(9, 9))
            fig.subplots_adjust(left=0.0,
                                bottom=0.0,
                                right=1.0,
                                top=1.0,
                                wspace=0.0,
                                hspace=0.0)
            ax1 = fig.add_subplot(111)
            # Turn axes (x,y) off
            ax1.set_axis_off()
            # Start plot
            img = ax1.imshow(np.rot90(thing), vmin=-1, vmax=1)
            img.set_cmap('binary_r')
            # Force square plot
            ax1.set_aspect('equal')
            # Add colorbar
            colorbar = False
            fig.savefig('sum_binary_' + ind + '.eps',
                        format='eps',
                        transparent=True,
                        bbox_inches='tight')
            for ar in [total, s_1, sub]:
                print 'mean: {:12.2f}, min: {:12.2f}, max: {:12.2}'.format(
                    np.nanmean(ar), np.nanmin(ar), np.nanmax(ar))

            trans = np.load(p.input.translation_pdb[:-4] +
                            '_{}.npy'.format(ind))
            internal = np.load('internal' + '_{}.npy'.format(ind))
            add = trans + internal
            pt = Plot()
            pt.contour2D(add,
                         plotfile_name='trans_int_sum_' + ind + '.eps',
                         vmin=1000,
                         vmax=75000000)
Esempio n. 6
0
def run(args=None, l=None):
    '''
    * read in a supercell structure
    * fractionalize the structure based on the 'orginial' unit cell
    * Brute force find the best super cell translation and symmetry 
      operation per molecule to place it back in the asu
    '''
    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    if l == None:
        l = Log(log_name='er.sc_reduce.txt')
    l.title("er.sc_reduce module")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = sc_reduce_phil.phil_parse(
        args=args, log=l)  # use phil to process input
    p = working_params.sc_reduce

    # Read input super cell PDB
    sc_pdb = PDBClass(
        fname=p.input.pdb_in,
        selection="not (resname HOH) and not (resname CL) and not (resname NA)"
    )
    # Read unit cell (super cell)
    sc_unit_cell = sc_pdb.symmetry.unit_cell()

    # Read reference PDB
    ref_pdb = PDBClass(
        fname=p.input.ref_pdb,
        selection="not (resname HOH) and not (resname CL) and not (resname NA)"
    )
    ref_pdb.hierarchy.remove_alt_confs(always_keep_one_conformer=True)

    # Read reference unit cell
    ref_unit_cell = ref_pdb.symmetry.unit_cell()

    ###########################################################################
    #             Find inverse symmetry operation and ref coordinates         #
    ###########################################################################

    l.process_message('Listing inverse symmetry operations...')

    #### Generating sym ops based on space group ####

    # List to save in
    inv_sym_op_ls = []

    # Loop over symmetry operation
    for sym_op in ref_pdb.symmetry.space_group():

        # Extract rotation and translation
        rot = sym_op.r().as_double()
        trans = sym_op.t().as_double()

        # convert to numpy
        rot = np.array(rot)
        trans = np.array(trans)

        # Print info
        l.show_info('t: {}'.format(trans))
        l.show_info('r: {}'.format(rot[0:3]))
        l.show_info('r: {}'.format(rot[3:6]))
        l.show_info('r: {}'.format(rot[6:9]))
        l.show_info('\n')

        # Invert symmetry operations
        inv_rot = tuple(
            np.transpose(np.array([rot[0:3], rot[3:6], rot[6:9]])).flatten())
        inv_trans = tuple(trans * -1)

        # Save to list
        inv_sym_op_ls.append([inv_rot, inv_trans])

    #### Extract reference coordinates ####

    # get model:
    ref_model = ref_pdb.detached_model_copy(n=0)

    # fractionalize coordinates:
    ref_model.atoms().set_xyz(
        ref_unit_cell.fractionalize(ref_model.atoms().extract_xyz()))

    #### Save reference coordinates (ONE CHAIN) ####

    # reference coordinates:
    ref_coords = ref_model.chains()[0].atoms().extract_xyz()

    ###########################################################################
    #              Brute force reduce super cell to ASU                       #
    ###########################################################################

    #### Create final hierarchy ####

    final_hierarchy = iotbx.pdb.hierarchy.root()

    # Loop over all models in the super cell PDB
    for m, model in enumerate(sc_pdb.hierarchy.models()):

        # Detach model
        det_model = sc_pdb.detached_model_copy(n=m)

        # Fractionalize based on ref unit cell:
        det_model.atoms().set_xyz(
            ref_unit_cell.fractionalize(det_model.atoms().extract_xyz()))

        # Now per chain within model try all sc translations and symemtry operations
        for i, chain in enumerate(det_model.chains()):

            # Hacky to obtain an empty model object
            new_model = model.detached_copy()
            for c in range(len(new_model.chains())):
                new_model.remove_chain(0)

            # Possible super cell translations
            sc_trans_ls = sc_trans_list(p.input.supercell_size)

            #### Try all translations + inverse symmetry operations ####

            # Save coordinates belonging to (smallest) score
            final_coords = None
            score = None

            # Loop over all possible supercell translations
            for sc_trans in sc_trans_ls:

                # Loop over all symmetry ops
                for inv_sym_op in inv_sym_op_ls:

                    #### Generate possible answer ####

                    # Deep copy chain
                    tst_chain = chain.detached_copy().atoms()

                    # Apply super cell translation
                    tst_chain.set_xyz(tst_chain.extract_xyz() + sc_trans)

                    # Apply inverse symmetry operation
                    tst_chain.set_xyz(
                        (tst_chain.extract_xyz() + inv_sym_op[1]) *
                        inv_sym_op[0])

                    #### Score possible solution ####

                    # Calculate score
                    tst_score = calc_score(tst_chain, ref_coords)

                    # Get initial values
                    if score == None:
                        score = tst_score
                        final_coords = tst_chain.extract_xyz().deep_copy()
                    elif score != None:
                        # If sore is better:
                        if tst_score < score:
                            score = tst_score
                            final_coords = tst_chain.extract_xyz().deep_copy()
                        elif tst_score > score:
                            continue
                        elif tst_score == score:
                            continue
                        else:
                            raise Exception('error!!')
                    else:
                        raise Exception('Score is not correct type')

            l.show_info('model: {}, chain: {}'.format(m, i))

            #### Prep output ####

            # rename new chain (A because 1 chain per model)
            chain.id = 'A'

            # Convert chain atoms back to cartesian
            chain.atoms().set_xyz(ref_unit_cell.orthogonalize(final_coords))

            # Append chain to empty model
            new_model.append_chain(chain.detached_copy())
            # Append model to final hierarchy
            final_hierarchy.append_model(new_model)
            gc.collect()
            gc.collect()
            gc.collect()


###########################################################################
#                               Write to file                             #
###########################################################################

# New space group / unit cell based on ref pdb (FOR NOW IT IS IN P1!!)
    new_sym = crystal.symmetry(ref_unit_cell.parameters(), space_group='P1')

    # write to file:
    final_hierarchy.write_pdb_file(crystal_symmetry=new_sym,
                                   file_name=p.input.pdb_in[:-4] + '_asu.pdb')

    return l
Esempio n. 7
0
def run(args=None):
    '''
    Extract l=0 slice from mtz file, export as new .mtz, .npy and plot
    '''
    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    l = Log(log_name='slice_mtz_log.txt')
    l.title("exp.check_2d_detail")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = slice_phil.phil_parse(args=args,
                                           log=l)  # use phil to process input
    p = working_params.slice_mtz

    ###########################################################################
    #                         Reading .mtz file                               #
    ###########################################################################

    if p.input.mtz_in != None:
        l.process_message('Reading mtz file...')
        mtz_obj = MTZClass(p.input.mtz_in)

        ###########################################################################
        #                        Slicing .mtz file                                #
        ###########################################################################

        # Select which array to slice
        l.process_message('Slicing mtz file and writing to {}...'.format(
            p.output.mtz_out))
        ar = p.params.array
        if ar == 'Ibrg':
            ar_num = 0
            l.warning('Array name: Ibrg')
        if ar == 'Itot':
            ar_num = 1
            l.warning('Array name: Itot')
        if ar == 'Idff':
            ar_num = 2
            l.warning('Array name: Idff')
        slice_2D = mtz_obj.slice_mtz(mtz_out_name=p.output.mtz_out,
                                     array_num=ar_num)
        l.process_message('Saving 2D slice as .npy file: {}...'.format(
            p.output.npy_out))
        np.save(p.output.npy_out, slice_2D)

###########################################################################
#                          Reading .npy Slice                             #
###########################################################################

    if p.input.npy_in != None:
        l.process_message('Loading 2D array from npy file...')
        slice_2D = np.load(p.input.npy_in)

###########################################################################
#                          Cut 2D map!                                    #
###########################################################################

    sp = np.shape(slice_2D)[0] + 1
    delta = int(round(sp / 10))
    delta_y = int(round(delta / 2))
    center = int((sp / 2) - 1)

    print sp, delta, delta_y, center

    slice_2D = slice_2D[-delta:, center - delta_y:center + delta_y]

    print np.shape(slice_2D)

    ###########################################################################
    #                             Plotting Slice                              #
    ###########################################################################

    l.process_message('Plotting 2D slice and saving to: {}...'.format(
        p.output.plt_out))
    l.show_info('Minimum of slice: {:.2f}'.format(np.nanmin(slice_2D)))
    l.show_info('Maximum of slice: {:.2f}'.format(np.nanmax(slice_2D)))
    if p.params.vmin == None and p.params.vmax == None:
        l.warning('Automatic determination of minimum and maximum values')
    else:
        l.show_info('Minimum value for plotting: {:.2f}'.format(p.params.vmin))
        l.show_info('Maximum value for plotting: {:.2f}'.format(p.params.vmax))
    pt = Plot()
    pt.contour2D(slice_2D,
                 plotfile_name=p.output.plt_out,
                 vmin=p.params.vmin,
                 vmax=p.params.vmax)

    ###########################################################################
    #                            Close Log File                               #
    ###########################################################################

    l.close_log()
Esempio n. 8
0
def run(args=None, l=None):
    '''
    Create 4 PDB files, 2 P1, 2 SC, perfect or containing an offset
    Calculate (diffuse) scattering from all 4
    Run ensemble refinement for 8 combo's (4 per "size" SC or P1)
    
    '''
    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    if l == None:
        l = Log(log_name='sc_er_setup_log.txt')
    l.title("er.sc_er_setup module")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = sc_er_setup_phil.phil_parse(
        args=args, log=l)  # use phil to process input
    p = working_params.sc_er_setup

    single_pdb_name = 'single_pdb.pdb'
    mtz_list, pdb_list = [], []

    ###########################################################################
    #                      Create simple dynamic model                        #
    ###########################################################################

    # Rigid Body motion simple test:
    if p.params.make_rb:
        l.process_message('RB module...')
        rigid_body_args = [
            'pdb_in={}'.format(p.input.rb_pdb_in),
            'rb_type={}'.format(p.params.rb_type),
            'pdb_out={}'.format(p.output.rb_pdb_out)
        ]
        rigid_body.run(args=rigid_body_args, l=l)
        os.system("mv trans_rb.pdb rb_pdb_out.pdb")

###########################################################################
#                          Prepare single PDB                             #
###########################################################################

# Single PDB will be used for rb setups
    pdb_list.append(single_pdb_name)
    if p.params.prep_single:
        l.process_message('Prepping single_pdb')
        single_pdb = PDBClass(
            fname=p.input.single_pdb_in,
            selection=
            "not (resname HOH) and not (resname CL) and not (resname NA)")
        # Set B-factors
        single_pdb.set_B_zero()
        # Set Occupancy
        single_pdb.set_occ()
        # Write PDB
        single_pdb.write_hierarchy_to_pdb(
            out_name=single_pdb_name, output_hierarchy=single_pdb.hierarchy)

###########################################################################
#                Calculate perfect scattering from single_pdb             #
###########################################################################

    if p.params.single_sc:
        l.process_message('single_pdb supercell operation...')
        # use EXP class to calculate diffuse scattering
        ex = EXPClass()
        ex.calc_diffuse_and_map(pdb=single_pdb_name,
                                supercell_num=2,
                                size_h=p.params.size_h,
                                size_k=p.params.size_k,
                                size_l=p.params.size_l,
                                Ncpu=p.params.Ncpu,
                                write_pdb=True,
                                l=l)
        mtz_name = 'single_sc.mtz'
        pdb_name = 'single_sc.pdb'
        os.system('rm supercell_out_1.pdb')
        os.system('mv supercell_out_0.pdb {}'.format(pdb_name))
        os.system('mv single_pdb.mtz {}'.format(mtz_name))
        os.system('mv single_pdb_IDFF.map single_sc_IDFF.map')
        mtz_list.append(mtz_name)
        pdb_list.append(pdb_name)

###########################################################################
#            Calculate perfect scattering from single_pdb in P1           #
###########################################################################

    if p.params.single_P1:
        l.process_message('single_pdb P1 operation...')
        # use EXP class to calculate diffuse scattering
        ex = EXPClass()
        ex.calc_diffuse_and_map(pdb=single_pdb_name,
                                supercell_num=2,
                                size_h=1,
                                size_k=1,
                                size_l=1,
                                Ncpu=p.params.Ncpu,
                                write_pdb=True,
                                l=l)
        mtz_name = 'single_P1.mtz'
        pdb_name = 'single_P1.pdb'
        os.system('rm supercell_out_1.pdb')
        os.system('mv supercell_out_0.pdb {}'.format(pdb_name))
        os.system('mv single_pdb.mtz {}'.format(mtz_name))
        os.system('mv single_pdb_IDFF.map single_P1_IDFF.map')
        mtz_list.append(mtz_name)
        pdb_list.append(pdb_name)

###########################################################################
#                Calculate diffuse scattering from rb_pdb                 #
###########################################################################

    if p.params.rb_sc:
        l.process_message('rb_pdb supercell operation...')
        # use EXP class to calculate diffuse scattering
        ex = EXPClass()
        ex.calc_diffuse_and_map(pdb=p.output.rb_pdb_out,
                                supercell_num=100,
                                size_h=p.params.size_h,
                                size_k=p.params.size_k,
                                size_l=p.params.size_l,
                                Ncpu=p.params.Ncpu,
                                write_pdb=True,
                                l=l)
        mtz_name = 'rb_sc.mtz'
        pdb_name = 'rb_sc.pdb'
        os.system('mv supercell_out_0.pdb {}'.format(pdb_name))
        os.system('rm supercell_out_*.pdb')
        os.system('mv rb_pdb_out.mtz {}'.format(mtz_name))
        os.system('mv rb_pdb_out_IDFF.map rb_sc_IDFF.map')
        mtz_list.append(mtz_name)
        pdb_list.append(pdb_name)

###########################################################################
#            Calculate diffuse scattering from rb_pdb in P1               #
###########################################################################

    if p.params.rb_P1:
        l.process_message('rb_pdb P1 operation...')
        # use EXP class to calculate diffuse scattering
        ex = EXPClass()
        ex.calc_diffuse_and_map(pdb=p.output.rb_pdb_out,
                                supercell_num=100,
                                size_h=1,
                                size_k=1,
                                size_l=1,
                                Ncpu=p.params.Ncpu,
                                write_pdb=True,
                                l=l)
        mtz_name = 'rb_P1.mtz'
        pdb_name = 'rb_P1.pdb'
        os.system('mv supercell_out_0.pdb {}'.format(pdb_name))
        os.system('rm supercell_out_*.pdb')
        os.system('mv rb_pdb_out.mtz {}'.format(mtz_name))
        os.system('mv rb_pdb_out_IDFF.map rb_P1_IDFF.map')
        mtz_list.append(mtz_name)
        pdb_list.append(pdb_name)

###########################################################################
#                      Prep intensity files for er                        #
###########################################################################

    if p.params.add_sigma:
        # Modify .mtz files by adding a SIGI column! This will be sqrt(I)
        l.process_message('Adding sigmas (sqrt(I) to mtz files...')
        if len(mtz_list) == 0:
            # Dev option for when fft steps are skipped
            mtz_list = [
                'single_sc.mtz', 'single_P1.mtz', 'rb_sc.mtz', 'rb_P1.mtz'
            ]
        for mtz_file in mtz_list:
            l.show_info('Processing mtz file: {}'.format(mtz_file))
            # Read mtz, create mtz_object
            mtz_object = mtz.object(mtz_file)
            # Extract miller arrays from mtz_object
            miller_arrays = mtz_object.as_miller_arrays()
            # create new miller array (IBRG) with added sigma's (sqrt(IBRG))
            ibrg_new = miller_arrays[0].customized_copy(
                data=miller_arrays[0].data(),
                sigmas=flex.sqrt(miller_arrays[0].data()))
            # Create new mtz dataset
            mtz_dataset = ibrg_new.as_mtz_dataset(column_root_label="IBRG")
            # create new miller array (ITOT) with added sigma's (sqrt(ITOT))
            itot_new = miller_arrays[1].customized_copy(
                data=miller_arrays[1].data(),
                sigmas=flex.sqrt(miller_arrays[1].data()))
            mtz_dataset.add_miller_array(itot_new, column_root_label="ITOT")
            # create new miller array (IDFF) with added sigma's (sqrt(IDFF))
            idff_new = miller_arrays[1].customized_copy(
                data=miller_arrays[2].data(),
                sigmas=flex.sqrt(miller_arrays[2].data()))
            mtz_dataset.add_miller_array(idff_new, column_root_label="IDFF")
            # Write new MTZ to file
            mtz_dataset.mtz_object().write("{}_SIG.mtz".format(mtz_file[:-4]))

###########################################################################
#                For all PDB's add B-fact noise                           #
###########################################################################

# Add noise (0-0.1) in B-factor column, this allows for TLS fitting without
# influencing the ensemble refinement
    if p.params.b_fact_noise:
        l.process_message('Adding noise to B-factor column')
        # Dev-option:
        if len(pdb_list) == 1:
            pdb_list = [
                'single_sc.pdb', 'single_P1.pdb', 'rb_sc.pdb', 'rb_P1.pdb'
            ]
        # Loop over all PDB's
        for pdb_file in pdb_list:
            l.show_info('Adding noise to {}'.format(pdb_file))
            # Read PDB
            pdb_f = PDBClass(
                fname=pdb_file,
                selection=
                "not (resname HOH) and not (resname CL) and not (resname NA)")
            # Loop over all atoms
            for atom in pdb_f.hierarchy.models()[0].atoms():
                atom.b = random.uniform(0.0, 0.1)
            # Write PDB to file
            pdb_f.write_hierarchy_to_pdb(output_hierarchy=pdb_f.hierarchy,
                                         out_name='{}_B_noise.pdb'.format(
                                             pdb_file[:-4]))

###########################################################################
#                Prep input for ensemble refinement                       #
###########################################################################

# P1
    single_P1_vs_single_P1 = 'phenix.ensemble_refinement single_P1_B_noise.pdb single_P1_SIG.mtz output_file_prefix=single_P1_vs_single_P1  params'
    single_P1_vs_rb_P1 = 'phenix.ensemble_refinement single_P1_B_noise.pdb rb_P1_SIG.mtz output_file_prefix=single_P1_vs_rb_P1 params'
    rb_P1_vs_single_P1 = 'phenix.ensemble_refinement rb_P1_B_noise.pdb single_P1_SIG.mtz output_file_prefix=rb_P1_vs_rb_P1 params'
    rb_P1_vs_rb_P1 = 'phenix.ensemble_refinement rb_P1_B_noise.pdb rb_P1_SIG.mtz output_file_prefix=rb_P1_vs_single_P1 params'

    # SC
    single_sc_vs_single_sc = 'phenix.ensemble_refinement single_sc_B_noise.pdb single_sc_SIG.mtz output_file_prefix=single_sc_vs_single_sc params'
    single_sc_vs_rb_sc = 'phenix.ensemble_refinement single_sc_B_noise.pdb rb_sc_SIG.mtz output_file_prefix=single_sc_vs_rb_sc params'
    rb_sc_vs_single_sc = 'phenix.ensemble_refinement rb_sc_B_noise.pdb single_sc_SIG.mtz output_file_prefix=rb_sc_vs_single_sc params'
    rb_sc_vs_rb_sc = 'phenix.ensemble_refinement rb_sc_B_noise.pdb rb_sc_SIG.mtz output_file_prefix=rb_sc_vs_rb_sc params'

    # Running parameters (right now for TESTING!!!!!) adjust tx for real runs!!!
    params_commands = '''
ensemble_refinement {
  max_ptls_cycles=1
  tls_group_selections = all
  ptls = 0.0
  tx = 1.0
  equilibrium_n_tx = 2
  acquisition_block_n_tx = 4
  number_of_aquisition_periods = 5
  cartesian_dynamics.stop_cm_motion = False
  ordered_solvent_update = False
  ensemble_reduction = False
  output_running_kinetic_energy_in_occupancy_column = True
}
input.xray_data.labels = ITOT,SIGITOT
input.xray_data.r_free_flags.generate=True
 '''

    # Write parameter file
    with open('params', 'w') as f:
        print >> f, params_commands

###########################################################################
#                   Start simulations (parallel and screened)             #
###########################################################################

# P1
    com = 'screen -dmSL {} {}'.format('single_P1_vs_single_P1',
                                      single_P1_vs_single_P1)
    os.system(com)
    com = 'screen -dmSL {} {}'.format('single_P1_vs_rb_P1', single_P1_vs_rb_P1)
    os.system(com)
    com = 'screen -dmSL {} {}'.format('rb_P1_vs_single_P1', rb_P1_vs_single_P1)
    os.system(com)
    com = 'screen -dmSL {} {}'.format('rb_P1_vs_rb_P1', rb_P1_vs_rb_P1)
    os.system(com)
    # SC
    com = 'screen -dmSL {} {}'.format('single_sc_vs_single_sc',
                                      single_sc_vs_single_sc)
    os.system(com)
    com = 'screen -dmSL {} {}'.format('single_sc_vs_rb_sc', single_sc_vs_rb_sc)
    os.system(com)
    com = 'screen -dmSL {} {}'.format('rb_sc_vs_single_sc', rb_sc_vs_single_sc)
    os.system(com)
    com = 'screen -dmSL {} {}'.format('rb_sc_vs_rb_sc', rb_sc_vs_rb_sc)
    os.system(com)

    return l
Esempio n. 9
0
def run(args=None):
    '''
    Check additivity of motion in reciprocal space, subtract mtz files
    '''

    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    l = Log(log_name='additivity_all_log.txt')
    l.title("exp.additivity_all module")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = additivity_all_phil.phil_parse(
        args=args, log=l)  # use phil to process input
    p = working_params.additivity_all

    ###########################################################################
    #                        Generate ensembles                               #
    ###########################################################################

    ens_list = [
        'rbTrans', 'rbRot', 'rbMix', 'ens', 'rbTransEns', 'rbRotEns',
        'rbMixEns'
    ]
    ens_command_list = [
        [
            True,
            [
                'pdb_in={}'.format(p.input.single_pdb),
                'pdb_out={}.pdb'.format(ens_list[0]), 'translation_only=True'
            ]
        ],
        [
            True,
            [
                'pdb_in={}'.format(p.input.single_pdb),
                'pdb_out={}.pdb'.format(ens_list[1]), 'rotation_only=True'
            ]
        ],
        [
            True,
            [
                'pdb_in={}'.format(p.input.single_pdb),
                'pdb_out={}.pdb'.format(ens_list[2])
            ]
        ],
        [False, [p.input.ensemble_pdb]],  # Only ensemble no rbMotion
        [
            True,
            [
                'pdb_in={}'.format(p.input.ensemble_pdb),
                'pdb_out={}.pdb'.format(ens_list[4]), 'translation_only=True'
            ]
        ],
        [
            True,
            [
                'pdb_in={}'.format(p.input.ensemble_pdb),
                'pdb_out={}.pdb'.format(ens_list[5]), 'rotation_only=True'
            ]
        ],
        [
            True,
            [
                'pdb_in={}'.format(p.input.ensemble_pdb),
                'pdb_out={}.pdb'.format(ens_list[6])
            ]
        ]
    ]

    if p.params.calc_diff:
        batch_diffuse_calc(p=p,
                           ens_list=ens_list,
                           ens_command_list=ens_command_list,
                           l=l)
###########################################################################
#                       Create big slice image                            #
###########################################################################

    if p.params.merge_slices: merge_slices(ens_list=ens_list, l=l)

    ###########################################################################
    #                         Plot all B-Factors                              #
    ###########################################################################

    if p.params.plot_all_B_factors: plot_all_B_factors(ens_list=ens_list, l=l)

    ###########################################################################
    #                   Subtract all scattering stuff                         #
    ###########################################################################

    # All sums, per element [0] - [1] = [2]
    sum_list = [['rbMix', 'rbRot', 'rbTrans'], ['rbMix', 'rbTrans', 'rbRot'],
                ['rbTransEns', 'rbTrans', 'ens'],
                ['rbTransEns', 'ens', 'rbTrans'], ['rbRotEns', 'rbRot', 'ens'],
                ['rbRotEns', 'ens', 'rbRot'], ['rbMixEns', 'rbMix', 'ens'],
                ['rbMixEns', 'ens', 'rbMix']]
    # special sum: [0] - [1] - [2] = [3]
    special = ['rbMixEns', 'rbTrans', 'rbRot', 'ens']

    # List to be appended to ens_list for all R and CC value calculations
    outname_list = []
    # Loop over all summations
    for sum in sum_list:
        # File name
        outname = '{}_{}'.format(sum[0], sum[1])
        outname_list.append(outname)
        # Subtract sum[1] from sum[0]
        a = [
            'mtz_1={}_diffuse.mtz'.format(sum[0]),
            'mtz_2={}_diffuse.mtz'.format(sum[1]),
            'mtz_out={}_diffuse.mtz'.format(outname)
        ]
        additivity.run(args=a, l=l)
        # Create 2D slice and map from new mtz
        slice_and_make_map(nm=outname, l=l)
        # Combine slices to final png file
        os.system(
            'convert {}_slice.eps {}_slice.eps {}_slice.eps {}_slice.eps  +append {}_slice.png'
            .format(sum[0], sum[1], outname, sum[2], outname))


###########################################################################
#                   Calculate all R and CC values                         #
###########################################################################

# use mtz.correlation to calculate values
    l.close_log()
Esempio n. 10
0
def run(args=None):
    '''
    - Create a large amount of dynamic models, 
    - Calculate diffuse scattering using supercells
    - Convert IDFF column to maps
    - In depth analysis between all maps and (possibly) data
    - Return CC-table
    - Return difference projection table
    '''

    ###########################################################################
    #                         Start log file                                  #
    ###########################################################################
    # init log file
    l = Log(log_name='diffuse_all_log.txt')
    l.title("scud.exp.diffuse_all module")

    ###########################################################################
    #                      Process input Params                               #
    ###########################################################################

    l.process_message('Processing input...\n')
    working_params = diffuse_all_phil.phil_parse(
        args=args, log=l)  # use phil to process input
    p = working_params.diffuse_all

    ###########################################################################
    #                           Create models                                 #
    ###########################################################################

    model_names = [
        'trans.pdb', 'rot.pdb', 'mix.pdb', 'int.pdb', 'intTrans.pdb',
        'intRot.pdb', 'intMix.pdb'
    ]
    # PDB stuff
    single = p.input.single_pdb
    l.process_message('Cleaning up ensemble PDB...')
    ensemble = 'int.pdb'
    # clean up ensemble PDB
    ens_tmp = PDBClass(
        fname=p.input.ensemble_pdb,
        selection="not (resname HOH) and not (resname CL) and not (resname NA)"
    )
    ens_tmp.set_occ()
    ens_tmp.set_B_zero()
    ens_tmp.write_hierarchy_to_pdb(output_hierarchy=ens_tmp.hierarchy,
                                   out_name=ensemble)
    if p.params.create_models:
        # Use scud.pdb.rigid_body to create models from single structure
        # and ensemble
        # Loat single PDB to find average B-factor needed for model generation
        l.process_message(
            'Creating dynamic models (pdbs), as input for diffuse scattering calculation...'
        )
        single_pdb = PDBClass(
            fname=single,
            selection=
            "not (resname HOH) and not (resname CL) and not (resname NA)")
        average_B = single_pdb.average_B()
        # Rigid Body commands
        wilson_B = 20.18
        rigid_body_commands = [[
            'pdb_in={}'.format(single), 'translation_only=True',
            'pdb_out=trans.pdb', 'b_ext={}'.format(wilson_B)
        ],
                               [
                                   'pdb_in={}'.format(single),
                                   'rotation_only=True', 'pdb_out=rot.pdb',
                                   'b_ref={}'.format(single)
                               ],
                               [
                                   'pdb_in={}'.format(single),
                                   'pdb_out=mix.pdb', 'b_ref={}'.format(single)
                               ],
                               [
                                   'pdb_in={}'.format(ensemble),
                                   'pdb_out=intTrans.pdb',
                                   'b_ext={}'.format(wilson_B)
                               ],
                               [
                                   'pdb_in={}'.format(ensemble),
                                   'rotation_only=True', 'pdb_out=intRot.pdb',
                                   'b_ext={}'.format(wilson_B)
                               ],
                               [
                                   'pdb_in={}'.format(ensemble),
                                   'pdb_out=intMix.pdb',
                                   'b_ext={}'.format(wilson_B)
                               ]]
        l.process_message('Creating models...')
        for a in rigid_body_commands:
            rigid_body.run(args=a, l=l)

###########################################################################
#                           B-Factor Plot                                 #
###########################################################################

    if p.params.B_factor_plots:
        l.process_message(
            'Calculating and plotting B-Factors from all models...')
        # Create a B-Factor plot of all models and save all as a json file
        # Classic B-factor
        b_argument = ['pdb_in={}'.format(single), 'plot_dat=single.json']
        b.run(b_argument)
        # Ensemble B-factor
        ens2b_argument = ['pdb_in={}'.format(ensemble), 'plot_dat=int.json']
        ens2b.run(ens2b_argument)
        # New models B-Factor
        for n in model_names:
            ens2b_argument = [
                'pdb_in={}'.format(n), 'plot_dat={}.json'.format(n[:-4])
            ]
            ens2b.run(ens2b_argument)
        plt_list = ['dat={}.json'.format(n[:-4]) for n in model_names]
        plt_list = ['dat=single.json', 'dat=int.json'] + plt_list + [
            'plot_out=all_b_factors.eps', 'show=False'
        ]
        lines.run(plt_list)
        l.process_message('All B-factors plotted in all_b_factors.eps...')

###########################################################################
#                   Calculate Diffuse scattering                          #
###########################################################################

# Calculate ITOT,IBRG and IDFF for each model in supercell space and
# convert to IDFF maps
    ex = EXPClass()
    if p.params.calc_diff:
        l.process_message('Calculating diffuse scattering for all models...')
        for pdb in model_names:
            ex.calc_diffuse_and_map(pdb=pdb,
                                    supercell_num=p.params.supercell_num,
                                    size_h=p.params.size_h,
                                    size_k=p.params.size_k,
                                    size_l=p.params.size_l,
                                    Ncpu=p.params.Ncpu,
                                    l=l)

###########################################################################
#                           Create CC table                               #
###########################################################################

# If a data map is present append this to the map list
# If not just calculate all possible CC values and save to a big file
    map_list = ['{}_IDFF.map'.format(i[:-4]) for i in model_names]
    if p.params.calc_CC:
        l.process_message(
            'Calculating CC values between all maps and / or data...')
        if p.input.data_map != None:
            map_list.append(p.input.data_map)
            # List with all map names
        # ADD FOR LOOPS
        # List where CC values will be stored in
        CC_list = np.zeros((len(map_list), len(map_list)))
        CC_brg_list = np.zeros((len(map_list), len(map_list)))
        CC_aniso_list = np.zeros((len(map_list), len(map_list)))
        for i in range(len(map_list)):
            CC_list[i, i] = 1.0
        for i in range(len(map_list)):
            CC_brg_list[i, i] = 1.0
        for i in range(len(map_list)):
            CC_aniso_list[i, i] = 1.0
        print CC_list
        print CC_brg_list
        print CC_aniso_list
        # Create double loop, map2map for every map-map combination
        l.process_message('looping over all maps...')
        for i_map1, map1 in enumerate(map_list):
            for i_map2, map2 in enumerate(map_list):
                # Do not do double calculations
                if i_map1 <= i_map2: continue
                l.show_info('{} vs. {}'.format(map1, map2))
                # Check for experimental map
                m1_dat, m2_dat = False, False
                if map1 == p.input.data_map: m1_dat = True
                if map2 == p.input.data_map: m2_dat = True
                # Arguments for map2map
                map2map_args = [
                    'map_1={}'.format(map1),
                    'map_2={}'.format(map2),
                    'map_1_exp={}'.format(m1_dat),
                    'map_2_exp={}'.format(m2_dat),
                    'size_h={}'.format(p.params.size_h),
                    'size_k={}'.format(p.params.size_k),
                    'size_l={}'.format(p.params.size_l),
                ]
                l = map2map.run(args=map2map_args, l=l)
                # ADD FOR LOOPS
                # Find back CC in map2map output and append to table
                CC_fname = 'map2map_{}_{}/CC.txt'.format(map1[:-4], map2[:-4])
                with open(CC_fname, 'r') as f:
                    CC = float(f.readlines()[0])
                CC_list[i_map1, i_map2] = CC

                # Find back CC_bragg in map2map output and append to table
                CC_fname = 'map2map_{}_{}/CC_brg.txt'.format(
                    map1[:-4], map2[:-4])
                with open(CC_fname, 'r') as f:
                    CC = float(f.readlines()[0])
                CC_brg_list[i_map1, i_map2] = CC

                # Find back CC_inter_bragg in map2map output and append to table
                CC_fname = 'map2map_{}_{}/CC_aniso.txt'.format(
                    map1[:-4], map2[:-4])
                with open(CC_fname, 'r') as f:
                    CC = float(f.readlines()[0])
                CC_aniso_list[i_map1, i_map2] = CC

###########################################################################
#                      Writing CC's to table                              #
###########################################################################

# ADD FOR LOOPS

# Convert file names
        map_names = [i[:-4] for i in map_list]
        cc_lines = []
        # Make column header line
        column_header = [''] + map_names
        ch = ''
        for i in column_header:
            ch += '{:>15s}'.format(i)
        cc_lines.append(ch)
        # Make other lines
        for i, li in enumerate(CC_list):
            # Create first column (name)
            line = '{:15s}'.format(map_names[i])
            # add CC values to line
            for c in li:
                line += '{:15.2f}'.format(c)
            cc_lines.append(line)
        # Write to file
        with open('all_CC.txt', 'w') as f:
            for line in cc_lines:
                print >> f, line

        # Convert file names
        map_names = [i[:-4] for i in map_list]
        cc_lines = []
        # Make column header line
        column_header = [''] + map_names
        ch = ''
        for i in column_header:
            ch += '{:>15s}'.format(i)
        cc_lines.append(ch)
        # Make other lines
        for i, li in enumerate(CC_brg_list):
            # Create first column (name)
            line = '{:15s}'.format(map_names[i])
            # add CC values to line
            for c in li:
                line += '{:15.2f}'.format(c)
            cc_lines.append(line)
        # Write to file
        with open('all_CC_brg.txt', 'w') as f:
            for line in cc_lines:
                print >> f, line

        # Convert file names
        map_names = [i[:-4] for i in map_list]
        cc_lines = []
        # Make column header line
        column_header = [''] + map_names
        ch = ''
        for i in column_header:
            ch += '{:>15s}'.format(i)
        cc_lines.append(ch)
        # Make other lines
        for i, li in enumerate(CC_aniso_list):
            # Create first column (name)
            line = '{:15s}'.format(map_names[i])
            # add CC values to line
            for c in li:
                line += '{:15.2f}'.format(c)
            cc_lines.append(line)
        # Write to file
        with open('all_CC_aniso.txt', 'w') as f:
            for line in cc_lines:
                print >> f, line

###########################################################################
#                Make a difference projection table                       #
###########################################################################

# All difference projections into one big 'image' sorted same as CC table
    l.process_message('Generating difference projection table...')
    # Create stings with image names per row
    s_list = []
    for i_map1, map1 in enumerate(map_list):
        s = ''
        for i_map2, map2 in enumerate(map_list):
            if i_map1 <= i_map2: continue
            projection_fname = 'map2map_{}_{}/projection_difference_map.eps'.format(
                map1[:-4], map2[:-4])
            s += ' ' + projection_fname
        s_list.append(s)

    # Create rows
    f_list = []
    for i, s in enumerate(s_list):
        f = '{}_tmp.png'.format(i)
        os.system('convert {} +append {}'.format(s, f))
        f_list.append(f)

    # Create final image
    f_string = ''
    for f in f_list:
        f_string += ' ' + f
    os.system(
        'convert {} -append all_projection_differences.png'.format(f_string))