Esempio n. 1
0
def generate_symmetry_mates(pdbin, pdbout, sgno, cell):
    """Takes the input pdb and generates the unit cell from the point group symmetry"""

    if not pdbout.endswith('.pdb'):
        pdbout = pdbout + '.pdb'
    if not os.path.exists(pdbin):
        raise IOError('pdbin does not exist! {!s}'.format(pdbin))
    if os.path.exists(pdbout):
        raise Exception('pdbout already exists! {!s}'.format(pdbout))

    assert isinstance(sgno,
                      int), 'SPACEGROUP MUST BE AN INTEGER! {!s}'.format(sgno)
    assert isinstance(cell, list), 'CELL MUST BE A LIST! {!s}'.format(cell)

    # Initialise Commander
    PDBSET = CommandManager('pdbset')
    # Set Command Arguments
    PDBSET.add_command_line_arguments('XYZIN', os.path.abspath(pdbin),
                                      'XYZOUT', os.path.abspath(pdbout))
    # Set inputs
    PDBSET.add_standard_input([
        'SYMGEN {!s}'.format(sgno),
        'CELL {!s}'.format(' '.join(map(str, cell))), 'END'
    ])
    # run!
    PDBSET.run()

    if not os.path.exists(pdbout):
        raise ExternalProgramError(
            'PDBSET has failed to generate SYMMETRY mates. {!s}\nCOM: {!s}\nOUT: {!s}\nERR: {!s}'
            .format(pdbin, PDBSET.command, PDBSET.out, PDBSET.err))

    return pdbout
Esempio n. 2
0
def create_cryst_line(pdbin, pdbout, sg, cell):
    """Adds a cryst line to pdbin"""

    if not pdbout.endswith('.pdb'):
        pdbout = pdbout + '.pdb'
    if not os.path.exists(pdbin):
        raise IOError('pdbin does not exist! {!s}'.format(pdbin))
    if os.path.exists(pdbout):
        raise Exception('pdbout already exists! {!s}'.format(pdbout))

    # Initialise Commander
    PDBSET = CommandManager('pdbset')
    # Set Command Arguments
    PDBSET.add_command_line_arguments('XYZIN', os.path.abspath(pdbin),
                                      'XYZOUT', os.path.abspath(pdbout))
    # Set Stdin
    PDBSET.add_standard_input([
        'SPACEGROUP {!s}'.format(sg),
        'CELL {!s}'.format(' '.join(map(str, cell)))
    ])
    # run!
    PDBSET.run()

    if not os.path.exists(pdbout):
        raise ExternalProgramError(
            'PDBSET has failed to create cryst line for {!s}\nOUT: {!s}\nERR: {!s}'
            .format(pdbin, PDBSET.out, PDBSET.err))

    return PDBSET
Esempio n. 3
0
def extract_bfactor_statistics(pdb_file):
    """Analyse the b-factors in a pdb file and return a dictionary with all of the averages, rms deviations and Z-scores for the b-factors in a structure"""

    # Create a temporary file for the output summary table and pdb_file
    temp_handle, temp_path = tempfile.mkstemp(suffix='.table',
                                              prefix='baverage_')
    temp_handle2, temp_path2 = tempfile.mkstemp(suffix='.pdb',
                                                prefix='baverage_')

    BAVERAGE = CommandManager('baverage')
    BAVERAGE.add_command_line_arguments('XYZIN', pdb_file, 'RMSTAB', temp_path,
                                        'XYZOUT', temp_path2)
    BAVERAGE.add_standard_input(['END'])
    BAVERAGE.run()

    if not os.path.exists(temp_path):
        raise ExternalProgramError(
            'BAVERAGE has failed to calculate b-factor summary statistics for {!s}'
            .format(pdb_file))

    # Process Table and Standard Out
    table_contents = open(temp_path, 'r').read().split('\n')

    if not table_contents:
        raise ExternalProgramError(
            'BAVERAGE has failed to calculate b-factor summary statistics for {!s}'
            .format(pdb_file))
    else:
        os.remove(temp_path)
        os.remove(temp_path2)

    return BAVERAGE, table_contents
Esempio n. 4
0
def create_alpha_carbon_backbone(pdbin, pdbout):
    """Takes a pdb files and removes eveything except for the alpha carbons"""

    if not pdbout.endswith('.pdb'):
        pdbout = pdbout + '.pdb'
    if not os.path.exists(pdbin):
        raise IOError('pdbin does not exist! {!s}'.format(pdbin))
    if os.path.exists(pdbout):
        raise Exception('pdbout already exists! {!s}'.format(pdbout))

    # Initialise Commander
    PDBCUR = CommandManager('pdbcur')
    # Set Command Arguments
    PDBCUR.add_command_line_arguments('XYZIN', pdbin, 'XYZOUT', pdbout)
    # Set inputs
    PDBCUR.add_standard_input(['lvatom "CA[C]:*"', 'END'])
    # run!
    PDBCUR.run()

    if not os.path.exists(pdbout):
        raise ExternalProgramError(
            'PDBCUR has failed to create carbon backbone. {!s}\nOUT: {!s}\nERR: {!s}'
            .format(pdbin, PDBCUR.out, PDBCUR.err))

    return pdbout
Esempio n. 5
0
def apply_rfree_set(refmtz, mtzin, mtzout):
    """Takes an input mtz and a reference mtz and transplants the rfree flags"""

    tempmtz = mtzin.replace('.mtz', '.temp.mtz')

    refobj = MtzSummary(refmtz)
    newobj = MtzSummary(mtzin)

    # Copy data columns from new mtz file
    F1, SIGF1 = newobj.label.f, newobj.label.sigf
    if not (F1 and SIGF1):
        raise LabelError('No Amplitudes found in {!s}'.format(mtzin))

    # Get the spacegroup and the rfree from the reference mtz
    sgno = refobj.data.spacegroupno
    RFree2 = refobj.label.free
    if not sgno:
        raise LabelError(
            'No Spacegroup information found in {!s}'.format(refmtz))
    if not RFree2:
        raise LabelError('No RFreeFlags found in {!s}'.format(refmtz))

    # Initialise Commander
    CAD = CommandManager('cad')
    # Set command arguments
    CAD.add_command_line_arguments('hklin1', mtzin, 'hklin2', refmtz, 'hklout',
                                   tempmtz)
    # Set inputs
    CAD.add_standard_input(['symmetry {!s}'.format(sgno),'labin file_number 1 E1={!s} E2={!s}'.format(F1, SIGF1), \
                                               'labout file_number 1 E1={!s} E2={!s}'.format(F1, SIGF1), \
                                               'labin file_number 2 E1={!s}'.format(RFree2), \
                                               'labout file_number 2 E1={!s}'.format(RFree2),'END'])
    # Run!
    CAD.run()

    if not os.path.exists(tempmtz):
        raise ExternalProgramError(
            'CAD has failed to transplant RFree Flags. {!s}\nOUT: {!s}\nERR: {!s}'
            .format(mtzin, CAD.out, CAD.err))

    # Now complete the free r set

    # Initialise Commander
    FREE = CommandManager('freerflag')
    # Set command arguments
    FREE.add_command_line_arguments('hklin', tempmtz, 'hklout', mtzout)
    # Set inputs
    FREE.add_standard_input(['COMPLETE FREE={!s}'.format(RFree2), 'END'])
    # Run!
    FREE.run()

    if not os.path.exists(mtzout):
        raise ExternalProgramError(
            'freerflag has failed to complete the RFree Flag set. {!s}\nOUT: {!s}\nERR: {s}'
            .format(tempmtz, CAD.out, CAD.err))

    os.remove(tempmtz)

    return CAD, FREE
Esempio n. 6
0
def isolate_residue(inpdb, outpdb, resname):
    """Extract the residues identified by resname using pdbcur - i.e. 'UNL'"""

    PDBCUR = CommandManager('pdbcur')
    PDBCUR.add_command_line_arguments('XYZIN', inpdb, 'XYZOUT', outpdb)
    PDBCUR.add_standard_input(['lvresidue /*/*/({!s})'.format(resname), 'END'])
    PDBCUR.run()

    return PDBCUR
Esempio n. 7
0
def merge_pdb_files(pdb1, pdb2, pdbout):
    """Merge two PDB Files using CCP4s pdb_merge"""

    # Initialise Commander
    MERGER = CommandManager('pdb_merge')
    # Set command arguments
    MERGER.add_command_line_arguments('xyzin1', pdb1, 'xyzin2', pdb2, 'xyzout',
                                      pdbout)
    # Set inputs
    MERGER.add_standard_input('END')
    # run!
    MERGER.run()

    return MERGER
Esempio n. 8
0
def reindex_mtz_to_reference(in_mtz, out_mtz, reference_mtz, tolerance):
    """Reindex the data in one mtz to a reference mtz"""

    print '**************************'
    print '*** Running reindexing ***'
    print '**************************'

    cmd = CommandManager('pointless')
    cmd.add_command_line_arguments(
        ['hklin', in_mtz, 'hklref', reference_mtz, 'hklout', out_mtz])
    cmd.add_standard_input(['tolerance {}'.format(tolerance)])
    print_run_and_raise_error_maybe(cmd)
    if not os.path.exists(out_mtz):
        raise Failure(
            'reindexing has failed -- {} does not exist'.format(out_mtz))
Esempio n. 9
0
def transfer_rfree_flags(in_mtz,
                         out_mtz,
                         reference_mtz,
                         input_free_r_flag,
                         output_free_r_flag,
                         delete_tmp_files=True):
    """Copy R-free flags from reference mtz"""

    print '*********************************'
    print '*** Transferring R-free flags ***'
    print '*********************************'

    tmp_mtz = splice_ext(path=out_mtz, new='step1-transfer')

    # Stage 1 - transfer R-free from reference
    cmd = CommandManager('cad')
    cmd.add_command_line_arguments(
        ['hklin1', in_mtz, 'hklin2', reference_mtz, 'hklout', tmp_mtz])
    cmd.add_standard_input([
        'labin file_number 1 ALL',
        'labin file_number 2 E1={}'.format(input_free_r_flag),
        'labout file_number 2 E1={}'.format(output_free_r_flag), 'END'
    ])
    print_run_and_raise_error_maybe(cmd)
    if not os.path.exists(tmp_mtz):
        raise Failure(
            'transfer of R-free flags has failed -- {} does not exist'.format(
                tmp_mtz))

    print '*******************************'
    print '*** Completing R-free flags ***'
    print '*******************************'

    # Stage 2 - populate missing R-free values
    cmd = CommandManager('freerflag')
    cmd.add_command_line_arguments(['hklin', tmp_mtz, 'hklout', out_mtz])
    cmd.add_standard_input(
        ['COMPLETE FREE={}'.format(output_free_r_flag), 'END'])
    print_run_and_raise_error_maybe(cmd)
    if not os.path.exists(out_mtz):
        raise Failure(
            'expanding of R-free flags has failed -- {} does not exist'.format(
                out_mtz))

    if delete_tmp_files:
        os.remove(tmp_mtz)
Esempio n. 10
0
def mask_map(mapin, maskpdb, mapout, border=1):
    """Takes mapin and masks around atoms in maskpdb"""

    # Masking object
    masker = CommandManager('mapmask')
    # Set input files
    masker.add_command_line_arguments(
        ['mapin', mapin, 'mapout', mapout, 'xyzin', maskpdb])
    # Set stdin
    masker.add_standard_input(['BORDER {!s}'.format(border), 'END'])
    # Run!
    masker.run()
    # Report errors
    if masker.process.returncode != 0:
        raise RuntimeError('mapmask failed to mask map {!s}'.format(mapin))

    # Return Command Managers for flexible handling of out & err
    return masker
Esempio n. 11
0
def create_asu_map(mapin, mapout):
    """Takes mapin and masks to the asymmetric unit"""

    # Masking object
    masker = CommandManager('mapmask')
    # Set input files
    masker.add_command_line_arguments(['mapin', mapin, 'mapout', mapout])
    # Set stdin
    masker.add_standard_input(['XYZLIM ASU', 'END'])
    # Run!
    masker.run()
    # Report errors
    if masker.process.returncode != 0:
        raise RuntimeError(
            'mapmask failed to create asu map from {!s}'.format(mapin))

    # Return Command Managers for flexible handling of out & err
    return masker
Esempio n. 12
0
def reset_pdb_file(pdbin, pdbout):
    """Resets the B-Factors in a file and removes anisotropy"""

    if not pdbout.endswith('.pdb'):
        pdbout = pdbout + '.pdb'
    if not os.path.exists(pdbin):
        raise IOError('pdbin does not exist! {!s}'.format(pdbin))
    if os.path.exists(pdbout):
        raise Exception('pdbout already exists! {!s}'.format(pdbout))

    pdbtemp = pdbout.replace('.pdb', '.temp.pdb')

    # Initialise Commander
    PDBCUR = CommandManager('pdbcur')
    # Set Command Arguments
    PDBCUR.add_command_line_arguments('XYZIN', pdbin, 'XYZOUT', pdbtemp)
    # Set inputs
    PDBCUR.add_standard_input(['NOANISOU', 'DELSOLVENT', 'END'])
    # run!
    PDBCUR.run()

    if not os.path.exists(pdbtemp):
        raise ExternalProgramError(
            'PDBCUR has failed to remove anisotropy and delete solvent. {!s}\nOUT: {!s}\nERR: {!s}'
            .format(pdbin, PDBCUR.out, PDBCUR.err))

    # Initialise Commander
    PDBSET = CommandManager('pdbset')
    # Set Command Arguments
    PDBSET.add_command_line_arguments('XYZIN', pdbtemp, 'XYZOUT', pdbout)
    # Set inputs
    PDBSET.add_standard_input(['BFACTOR', 'END'])
    # run!
    PDBSET.run()

    if not os.path.exists(pdbout):
        raise ExternalProgramError(
            'PDBSET has failed to reset B-factors. {!s}\nOUT: {!s}\nERR: {!s}'.
            format(pdbtemp, PDBSET.out, PDBSET.err))
    else:
        os.remove(pdbtemp)

    return pdbout
Esempio n. 13
0
def isolate_residue_by_res_id(inpdb,
                              outpdb,
                              chain,
                              resnum,
                              model='*',
                              inscode=''):
    """Isolate the residues identified by residue ids using pdbcur - i.e. '0/A/54.A/'"""

    if inscode:
        selection = '/{!s}/{!s}/{!s}.{!s}'.format(model, chain, resnum,
                                                  inscode)
    else:
        selection = '/{!s}/{!s}/{!s}'.format(model, chain, resnum)

    PDBCUR = CommandManager('pdbcur')
    PDBCUR.add_command_line_arguments('XYZIN', inpdb, 'XYZOUT', outpdb)
    PDBCUR.add_standard_input(['lvresidue {!s}'.format(selection), 'END'])
    PDBCUR.run()

    return PDBCUR
Esempio n. 14
0
def fft_mtz_to_map(mtz_file, map_file, cols):
    """Converts an MTZ Format File to a MAP File (using fft as default)"""

    # Initialise
    writer = CommandManager('fft')
    # Set Program Arguments
    writer.add_command_line_arguments('hklin', mtz_file, 'mapout', map_file)
    # Set Program Input
    writer.add_standard_input(
        ['LABIN F1={!s} PHI={!s}'.format(cols['F'], cols['P']), 'END'])
    # RUN!
    writer.run()
    # Check Output
    if writer.process.returncode != 0:
        print('\nOUT\n\n' + writer.out)
        print('\nERR\n\n' + writer.err)
        raise RuntimeError(
            'fft failed to generate map from {!s}'.format(mtz_file))

    return writer
Esempio n. 15
0
def merge_cif_libraries(incifs, outcif):
    """Take a list of cifs and merge into one cif"""

    assert isinstance(incifs, list), "'incifs' is not a list!"
    assert len(incifs) > 1, "'incifs' must be two or more files!"

    current = incifs.pop(0)

    to_be_deleted = []

    for additional in incifs:

        # Create a file handle and path for the output
        temp_handle, temp_path = tempfile.mkstemp(suffix='.lib',
                                                  prefix='libcheck_')
        to_be_deleted.append(temp_path)

        # Merge current and additional to temp_path
        LIBCHK = CommandManager('libcheck')
        LIBCHK.add_standard_input(
            '_DOC N', '_FILE_L {!s}'.format(current),
            '_FILE_L2 {!s}'.format(additional),
            '_FILE_O {!s}'.format(temp_path.replace('.lib', '')), '_END')
        LIBCHK.run()

        current = temp_path

    shutil.copy(current, outcif)

    for file in to_be_deleted:
        os.remove(file)

    assert os.path.exists(outcif), 'OUTPUT CIF DOES NOT EXIST! {!s}'.format(
        outcif)

    return outcif
Esempio n. 16
0
def remove_residue_by_res_id(inpdb,
                             outpdb,
                             chain,
                             resnum,
                             model='*',
                             inscode='',
                             removeSolvent=False):
    """Remove the residues identified by res info using pdbcur - i.e. 'UNL'"""

    if inscode:
        selection = '/{!s}/{!s}/{!s}.{!s}'.format(model, chain, resnum,
                                                  inscode)
    else:
        selection = '/{!s}/{!s}/{!s}'.format(model, chain, resnum)

    std_input = ['delresidue {!s}'.format(selection)
                 ] + (removeSolvent) * ['delsolvent'] + ['END']

    PDBCUR = CommandManager('pdbcur')
    PDBCUR.add_command_line_arguments('XYZIN', inpdb, 'XYZOUT', outpdb)
    PDBCUR.add_standard_input(std_input)
    PDBCUR.run()

    return PDBCUR
Esempio n. 17
0
class BuilderObject(object):
    """Template Object for creating an Object to generate ligand restraints"""
    def __init__(self, time=True, verbose=True):

        self.allowed_args = allowed_builder_args
        self.ligname = DEFAULT_LIGAND_NAMES[0]
        # Settings
        self.time = time
        self.verbose = verbose
        self.runtime = -1.0
        self.timeout = 1800
        # Custom Init
        self._custom_init()

    def generate_ligand(self, ligsmile, outdir, outfile, flags=[]):
        """Generate ligand PDB and CIF from smile string"""

        # Prepare standard settings
        self._prepare_to_generate_ligand(ligsmile, outdir, outfile, flags)
        # Check to see if already run, and if not, run the custom part of the object - different for each program
        if os.path.exists(self.outpdb) and os.path.exists(
                self.outcif) and os.path.exists(self.outlog):
            if self.verbose:
                print('\tLigand already generated - DOING NOTHING')
        else:
            cmd_line_args, std_inpt_args = self._create_program_arguments(
                ligsmile, outdir, outfile, flags)
            # Initialise CommandManager
            self.builder = CommandManager(self.program)
            # Set Command-Line Args
            if cmd_line_args:
                self.builder.add_command_line_arguments(cmd_line_args)
            # Set Standard Input
            if std_inpt_args: self.builder.add_standard_input(std_inpt_args)
            # Set Parameters
            self.builder.set_timeout(timeout=self.timeout)
            # RUN
            self.builder.run()
            # Get runtime
            self.runtime = self.builder.runtime

            try:
                # Could add postprocess here
                pass

            finally:
                self.write_log_file()

        return self.outpdb, self.outcif, self.outlog

    def _prepare_to_generate_ligand(self, ligsmile, outdir, outfile, flags):
        """Set up the generic file names"""

        # Process outputfile
        if '/' in outfile:
            raise ValueError('outfile must be a file, not a path')
        # Record Template and Outdir
        self.outtemplate = os.path.join(outdir, outfile)
        self.outdir = outdir
        # Record Filenames
        self.outpdb = self.outtemplate + '.pdb'
        self.outcif = self.outtemplate + '.cif'
        self.outlog = self.outtemplate + '.log'

    def write_log_file(self):
        """Write the log file"""

        with open(self.outlog, 'w') as logfile:
            # Write out the input command
            logfile.write('\nCOMMAND\n\n')
            logfile.write('\n'.join(self.builder.cmd_line_args) + '\n')
            logfile.write('\nINPUT\n\n')
            logfile.write('\n'.join(self.builder.std_inp_lines) + '\n')
            # Write out & err
            logfile.write('\nSTDOUT\n\n')
            logfile.write(self.builder.output)
            logfile.write('\nSTDERR\n\n')
            logfile.write(self.builder.error)
Esempio n. 18
0
def run(params):

    # Identify any existing output directories
    current_dirs = sorted(glob.glob(params.output.dir_prefix + '*'))
    if not current_dirs:
        next_int = 1
    else:
        current_nums = [
            s.replace(params.output.dir_prefix, '') for s in current_dirs
        ]
        next_int = sorted(map(int, current_nums))[-1] + 1

    # Create output directory name from int
    out_dir = params.output.dir_prefix + '{:04}'.format(next_int)
    # Create output directory
    os.mkdir(out_dir)

    # Create log object
    log = Log(log_file=os.path.join(
        out_dir, params.output.out_prefix + '.quick-refine.log'),
              verbose=params.settings.verbose)

    # Report
    if current_dirs:
        log('Found existing refinement directories: \n\t{}'.format(
            '\n\t'.join(current_dirs)))
        log('')
    log('Creating new output directory: {}'.format(out_dir))

    # Validate input parameters
    log.subheading('Validating input parameters')
    assert params.input.pdb is not None, 'No PDB given for refinement'
    assert params.input.mtz is not None, 'No MTZ given for refinement'

    if os.path.islink(params.input.mtz):
        log('Converting mtz path to real path:')
        log('{} -> {}'.format(params.input.mtz,
                              os.path.realpath(params.input.mtz)))
        params.input.mtz = os.path.realpath(params.input.mtz)

    # Link input
    log('Copying/linking files to refinement folder')
    shutil.copy(params.input.pdb,
                os.path.abspath(os.path.join(out_dir, 'input.pdb')))
    rel_symlink(params.input.mtz,
                os.path.abspath(os.path.join(out_dir, 'input.mtz')))
    # Copy parameter file to output folder
    if params.input.params:
        shutil.copy(params.input.params,
                    os.path.abspath(os.path.join(out_dir, 'input.params')))

    # Create output prefixes
    output_prefix = os.path.join(out_dir, params.output.out_prefix)
    log('Real output file path prefixes: {}'.format(output_prefix))
    log('Link output file path prefixes: {}'.format(params.output.link_prefix))

    # Create command objects
    log.subheading('Preparing command line input for refinement program')

    # PHENIX
    if params.options.program == 'phenix':
        cm = CommandManager('phenix.refine')
        # Command line args
        cm.add_command_line_arguments([params.input.pdb, params.input.mtz])
        cm.add_command_line_arguments(
            ['output.prefix={}'.format(output_prefix)])
        if params.input.cif:
            cm.add_command_line_arguments(params.input.cif)
        if params.input.params and os.path.exists(params.input.params):
            cm.add_command_line_arguments([params.input.params])

    # REFMAC
    elif params.options.program == 'refmac':
        cm = CommandManager('refmac5')
        # Command line args
        cm.add_command_line_arguments(
            ['xyzin', params.input.pdb, 'hklin', params.input.mtz])

        cm.add_command_line_arguments([
            'xyzout', output_prefix + '.pdb', 'hklout', output_prefix + '.mtz'
        ])
        if params.input.cif:
            for cif in params.input.cif:
                cm.add_command_line_arguments(['libin', cif])
        # Standard input
        if params.input.params:
            cm.add_standard_input(open(params.input.params).read().split('\n'))

        cm.add_standard_input(['END'])

    elif params.options.program == "buster":
        cm = CommandManager('refine')
        # Command line arguments
        # inputs
        cm.add_command_line_arguments(
            ['-p', params.input.pdb, '-m', params.input.mtz, '-d', out_dir])

        if params.input.cif:
            for cif in params.input.cif:
                cm.add_command_line_arguments(['-l', cif])

        if params.input.params:
            cm.add_command_line_arguments(['-Gelly', params.input.params])

    # Pass additional command line arguments?
    if params.input.args:
        cm.add_command_line_arguments(params.input.args)

    # Report
    log(str(cm))

    log.bar()
    log('running refinement... ({})'.format(cm.program[0]))
    out = cm.run()

    log.subheading('Refinement output')
    if not log.verbose:
        log('output written to log file ({} lines)'.format(
            cm.output.count('\n')))

    log('\n' + cm.output, show=False)

    if out != 0:
        log.subheading('Refinement Errors')
        log(cm.error)

    log.subheading('Post-processing output files')

    if params.options.program == "buster":
        log.subheading('Renaming buster output files')

        shutil.move(src=os.path.join(out_dir, 'refine.pdb'),
                    dst=output_prefix + '.pdb')

        shutil.move(src=os.path.join(out_dir, 'refine.mtz'),
                    dst=output_prefix + '.mtz')

    # Find output files
    try:
        real_pdb = glob.glob(output_prefix + '*.pdb')[0]
        real_mtz = glob.glob(output_prefix + '*.mtz')[0]
    except:
        log('Refinement has failed - output files do not exist')
        log('{}: {}'.format(output_prefix + '*.pdb',
                            glob.glob(output_prefix + '*.pdb')))
        log('{}: {}'.format(output_prefix + '*.mtz',
                            glob.glob(output_prefix + '*.mtz')))
        raise

    # List of links to make at the end of the run
    link_file_pairs = [(real_pdb, params.output.link_prefix + '.pdb'),
                       (real_mtz, params.output.link_prefix + '.mtz')]

    # Split conformations
    if params.options.split_conformations:
        params.split_conformations.settings.verbose = params.settings.verbose
        log.subheading('Splitting refined structure conformations')
        # Running split conformations
        out_files = split_conformations.split_conformations(
            filename=real_pdb, params=params.split_conformations, log=log)
        # Link output files to top
        for real_file in out_files:
            link_file = params.output.link_prefix + os.path.basename(
                real_file.replace(os.path.splitext(real_pdb)[0], ''))
            link_file_pairs.append([real_file, link_file])

    # Link output files
    log.subheading('linking output files')
    for real_file, link_file in link_file_pairs:
        log('Linking {} -> {}'.format(link_file, real_file))
        if not os.path.exists(real_file):
            log('file does not exist: {}'.format(real_file))
            continue
        if os.path.exists(link_file) and os.path.islink(link_file):
            log('removing existing link: {}'.format(link_file))
            os.unlink(link_file)
        if not os.path.exists(link_file):
            rel_symlink(real_file, link_file)

    log.heading('finished - refinement')
Esempio n. 19
0
def run(params):

    in_mtzs = params.input.mtz
    assert len(params.input.mtz) > 1, 'Need to provide at least one mtz files'
    #    assert len(params.input.mtz) < 3, 'Need to provide at most two mtz files'

    out_mtz = params.output.mtz
    out_log = params.output.log

    ###########################################
    # COMMAND LINE COMMANDS
    ###########################################
    cm = CommandManager('cad')

    ###########################################
    # ITERATE THROUGH INPUT MTZS
    ###########################################
    for i_mtz, mtz in enumerate(in_mtzs):
        # Use numbering from 1
        n_mtz = i_mtz + 1
        # Create an mtz suffix
        if params.options.label_suffix == 'incremental':
            suffix = '-{}'.format(n_mtz)
        elif params.options.label_suffix == 'filename':
            suffix = '-{}'.format(os.path.splitext(os.path.basename(mtz))[0])
        elif params.options.label_suffix == 'foldername':
            suffix = '-{}'.format(os.path.basename(os.path.dirname(mtz)))
        else:
            raise Exception('Not yet implemented, sorry')
        # Add to the command line
        cm.add_command_line_arguments(['hklin{}'.format(n_mtz), mtz])
        # Select column labels
        cm.add_standard_input('labin file_number {0} {1}'.format(
            n_mtz, ' '.join([
                'E{0}={1}'.format(i + 1, c)
                for i, c in enumerate(params.options.column.label)
            ])))
        cm.add_standard_input('labout file_number {0} {1}'.format(
            n_mtz, ' '.join([
                'E{0}={1}'.format(i + 1, c + suffix)
                for i, c in enumerate(params.options.column.label)
            ])))

    ###########################################
    # OUTPUT FILES
    ###########################################
    cm.add_command_line_arguments(['hklout', out_mtz])

    ###########################################
    # RUN
    ###########################################
    print 'Running CAD:'
    cm.print_settings()
    ret_code = cm.run()
    cm.write_output(log_file=out_log)
    if ret_code != 0:
        print '============================>'
        print 'Refmac returned with an error'
        print '============================>'
        print cm.output
        print '============================>'
        print cm.error
        print '============================>'
Esempio n. 20
0
def run(params):
    # Identify any existing output directories
    current_dirs = sorted(glob.glob(params.output.dir_prefix + "*"))
    if not current_dirs:
        next_int = 1
    else:
        current_nums = [
            s.replace(params.output.dir_prefix, "") for s in current_dirs
        ]
        next_int = sorted(map(int, current_nums))[-1] + 1

    # Create output directory name from int
    out_dir = params.output.dir_prefix + "{:04}".format(next_int)
    # Create output directory
    os.mkdir(out_dir)

    # Create log object
    log = Log(
        log_file=os.path.join(out_dir,
                              params.output.out_prefix + ".quick-refine.log"),
        verbose=params.settings.verbose,
    )

    # Report
    if current_dirs:
        log("Found existing refinement directories: \n\t{}".format(
            "\n\t".join(current_dirs)))
        log("")
    log("Creating new output directory: {}".format(out_dir))

    # Validate input parameters
    log.subheading("Validating input parameters")
    assert params.input.pdb is not None, "No PDB given for refinement"
    assert params.input.mtz is not None, "No MTZ given for refinement"

    if os.path.islink(params.input.mtz):
        log("Converting mtz path to real path:")
        log("{} -> {}".format(params.input.mtz,
                              os.path.realpath(params.input.mtz)))
        params.input.mtz = os.path.realpath(params.input.mtz)

    # Link input
    log("Copying/linking files to refinement folder")
    shutil.copy(params.input.pdb,
                os.path.abspath(os.path.join(out_dir, "input.pdb")))
    rel_symlink(params.input.mtz,
                os.path.abspath(os.path.join(out_dir, "input.mtz")))
    # Copy parameter file to output folder
    if params.input.params:
        shutil.copy(params.input.params,
                    os.path.abspath(os.path.join(out_dir, "input.params")))

    # Create output prefixes

    output_prefix = out_dir

    log("Real output file path prefixes: {}".format(output_prefix))
    log("Link output file path prefixes: {}".format(params.output.link_prefix))

    # Create command objects
    log.subheading("Preparing command line input for refinement program")

    # PHENIX
    if params.options.program == "phenix":
        cm = CommandManager("phenix.refine")
        # Command line args
        cm.add_command_line_arguments([params.input.pdb, params.input.mtz])
        cm.add_command_line_arguments(
            ["output.prefix={}".format(output_prefix)])
        if params.input.cif:
            cm.add_command_line_arguments(params.input.cif)
        if params.input.params and os.path.exists(params.input.params):
            cm.add_command_line_arguments([params.input.params])

    # REFMAC
    elif params.options.program == "refmac":
        cm = CommandManager("refmac5")
        # Command line args
        cm.add_command_line_arguments(
            ["xyzin", params.input.pdb, "hklin", params.input.mtz])
        cm.add_command_line_arguments([
            "xyzout", output_prefix + ".pdb", "hklout", output_prefix + ".mtz"
        ])
        if params.input.cif:
            for cif in params.input.cif:
                cm.add_command_line_arguments(["libin", cif])
        # Standard input
        if params.input.params:
            cm.add_standard_input(open(params.input.params).read().split("\n"))

        cm.add_standard_input(["END"])

    # Pass additional command line arguments?
    if params.input.args:
        cm.add_command_line_arguments(params.input.args)

    # Report
    log(str(cm))

    log.bar()
    log("running refinement... ({})".format(cm.program[0]))
    out = cm.run()

    log.subheading("Refinement output")
    if not log.verbose:
        log("output written to log file ({} lines)".format(
            cm.output.count("\n")))

    log("\n" + cm.output, show=False)

    if out != 0:
        log.subheading("Refinement Errors")
        log(cm.error)

    log.subheading("Post-processing output files")

    # Find output files
    try:
        real_pdb = os.path.join(output_prefix,
                                params.output.out_prefix + ".pdb")
        real_mtz = os.path.join(output_prefix,
                                params.output.out_prefix + ".mtz")

        print(real_pdb, "\n", real_mtz)

    except:
        log("Refinement has failed - output files do not exist")
        log("{}: {}".format(output_prefix + "*.pdb",
                            glob.glob(output_prefix + "*.pdb")))
        log("{}: {}".format(output_prefix + "*.mtz",
                            glob.glob(output_prefix + "*.mtz")))
        raise

    # List of links to make at the end of the run
    link_file_pairs = [
        (real_pdb, params.output.link_prefix + ".pdb"),
        (real_mtz, params.output.link_prefix + ".mtz"),
    ]

    print(link_file_pairs)

    # Split conformations
    if params.options.split_conformations:
        params.split_conformations.settings.verbose = params.settings.verbose
        log.subheading("Splitting refined structure conformations")
        # Running split conformations
        out_files = split_conformations.split_conformations(
            filename=real_pdb, params=params.split_conformations, log=log)
        # Link output files to top
        for real_file in out_files:
            link_file = params.output.link_prefix + os.path.basename(
                real_file.replace(os.path.splitext(real_pdb)[0], ""))
            link_file_pairs.append([real_file, link_file])

    # Link output files
    log.subheading("linking output files")
    for real_file, link_file in link_file_pairs:
        log("Linking {} -> {}".format(link_file, real_file))
        if not os.path.exists(real_file):
            log("file does not exist: {}".format(real_file))
            continue
        if os.path.exists(link_file) and os.path.islink(link_file):
            log("removing existing link: {}".format(link_file))
            os.unlink(link_file)
        if not os.path.exists(link_file):
            rel_symlink(real_file, link_file)

    log.heading("finished - refinement")
Esempio n. 21
0
def fill_missing_reflections(in_mtz,
                             out_mtz,
                             fill_resolution_low,
                             fill_resolution_high,
                             delete_tmp_files=True):
    """Complete the set of miller indices in an MTZ file"""

    print '***************************'
    print '*** Filling reflections ***'
    print '***************************'

    tmp_mtz_1 = splice_ext(path=out_mtz, new='step1-truncate')
    tmp_mtz_2 = splice_ext(path=out_mtz, new='step2-uniquify')
    tmp_mtz_3 = splice_ext(path=out_mtz, new='step3-remerged')

    # Stage 1 - truncate dataset, fill missing reflections, change column name
    cmd = CommandManager('cad')
    cmd.add_command_line_arguments(['hklin1', in_mtz, 'hklout', tmp_mtz_1])
    cmd.add_standard_input([
        'monitor BRIEF', 'labin file_number 1 ALL',
        'resolution file 1 {} {}'.format(fill_resolution_low,
                                         fill_resolution_high)
    ])
    print_run_and_raise_error_maybe(cmd)
    if not os.path.exists(tmp_mtz_1):
        raise Failure(
            'filling of missing reflections has failed -- {} does not exist'.
            format(tmp_mtz_1))

    print '-------------------'

    # Stage 2 - Uniqueify the file
    cmd = CommandManager('uniqueify')
    cmd.add_command_line_arguments(['-p', '0.05', tmp_mtz_1, tmp_mtz_2])
    print_run_and_raise_error_maybe(cmd)
    if not os.path.exists(tmp_mtz_2):
        raise Failure(
            'filling of missing reflections has failed -- {} does not exist'.
            format(tmp_mtz_2))

    print '-------------------'

    # Stage 3 - remerge the two files
    cmd = CommandManager('cad')
    cmd.add_command_line_arguments(
        ['hklin1', in_mtz, 'hklin2', tmp_mtz_2, 'hklout', tmp_mtz_3])
    cmd.add_standard_input([
        'monitor BRIEF', 'labin file_number 1 ALL',
        'labin file_number 2 E1=FreeR_flag', 'labout file_number 2 E1=dummy'
    ])
    print_run_and_raise_error_maybe(cmd)
    if not os.path.exists(tmp_mtz_3):
        raise Failure(
            'filling of missing reflections has failed -- {} does not exist'.
            format(tmp_mtz_3))

    print '-------------------'

    # Stage 4 - remove the dummy column
    cmd = CommandManager('mtzutils')
    cmd.add_command_line_arguments(['hklin1', tmp_mtz_3, 'hklout', out_mtz])
    cmd.add_standard_input(
        ['HEADER BRIEF', 'EXCLUDE 1 dummy', 'ONEFILE', 'END'])
    print_run_and_raise_error_maybe(cmd)
    if not os.path.exists(tmp_mtz_3):
        raise Failure(
            'filling of missing reflections has failed -- {} does not exist'.
            format(out_mtz))

    if delete_tmp_files:
        os.remove(tmp_mtz_1)
        os.remove(tmp_mtz_2)
        os.remove(tmp_mtz_3)
Esempio n. 22
0
def run(params):

    in_pdb = params.input.pdb
    assert os.path.exists(in_pdb), 'PDB does not exist: {}'.format(in_pdb)
    in_mtz = params.input.mtz
    if in_mtz:
        assert os.path.exists(in_mtz), 'MTZ does not exist: {}'.format(in_mtz)

    if params.output.pdb: out_pdb = params.output.pdb
    else: out_pdb = os.path.splitext(in_pdb)[0] + params.output.suffix + '.pdb'

    if params.output.mtz: out_mtz = params.output.mtz
    elif in_mtz:
        out_mtz = os.path.splitext(in_mtz)[0] + params.output.suffix + '.mtz'
    else:
        out_mtz = None

    if params.output.log: out_log = params.output.log
    else: out_log = os.path.splitext(in_pdb)[0] + params.output.suffix + '.log'

    ###########################################
    # COMMAND LINE COMMANDS
    ###########################################
    cm = CommandManager('refmac5')
    if in_pdb: cm.add_command_line_arguments(['xyzin', in_pdb])
    if out_pdb: cm.add_command_line_arguments(['xyzout', out_pdb])
    if in_mtz: cm.add_command_line_arguments(['hklin', in_mtz])
    if out_mtz: cm.add_command_line_arguments(['hklout', out_mtz])

    ###########################################
    # MAIN PARAMETERS
    ###########################################
    cm.add_standard_input('MAKE NCYC {}'.format(params.options.n_cycles))

    ###########################################
    # HYDROGENS
    ###########################################
    if params.options.add_hydrogens:
        cm.add_standard_input('MAKE HYDR A')
    else:
        cm.add_standard_input('MAKE HYDR Y')

    if params.options.output_hydrogens:
        cm.add_standard_input('MAKE HOUT Y')
    else:
        cm.add_standard_input('MAKE HOUT N')

    ###########################################
    # MISSING ATOMS
    ###########################################
    if params.options.build_absent_atoms:
        cm.add_standard_input('MAKE BUIL Y')
    else:
        cm.add_standard_input('MAKE BUIL N')

    ###########################################
    # LASTLY
    ###########################################
    cm.add_standard_input('END')

    ###########################################
    # RUN
    ###########################################
    print 'Running Refmac:'
    cm.print_settings()
    ret_code = cm.run()
    cm.write_output(log_file=out_log)
    if ret_code != 0:
        print '============================>'
        print 'Refmac returned with an error'
        print '============================>'
        print cm.output
        print '============================>'
        print cm.error
        print '============================>'