Example #1
0
def generate_ligand_with_acedrg(smiles,
                                name='LIG',
                                prefix='ligand',
                                verbose=False):
    """Generate pdb and cif files from smiles string"""

    assert len(name) == 3

    smiles = smiles.replace('CL', 'Cl')
    smiles = smiles.replace('BR', 'Br')

    out_pdb = prefix + '.pdb'
    out_cif = prefix + '.cif'

    if os.path.exists(out_pdb):
        raise Exception('Output PDB file already exists')
    if os.path.exists(out_cif):
        raise Exception('Output CIF file already exists')

    # Run acedrg
    acedrg = CommandManager('acedrg')
    acedrg.add_command_line_arguments(['--smi={}'.format(smiles)])
    acedrg.add_command_line_arguments(['-r', name])
    acedrg.add_command_line_arguments(['-o', prefix])
    if verbose: acedrg.print_settings()
    acedrg.run()

    if not (os.path.exists(out_pdb) and os.path.exists(out_cif)):
        e = Exception('acedrg failed during ligand generation')
        e.command = acedrg
        raise e

    return out_pdb, out_cif
Example #2
0
class _refiner(object):

    program = None
    auto = True

    def __init__(self,
                 pdb_file,
                 mtz_file=None,
                 cif_file=None,
                 out_prefix=None,
                 **kw_args):

        # Set defaults if not given
        if mtz_file is None:
            mtz_file = pdb_file.replace('.pdb', '.mtz')
        if out_prefix is None:
            out_prefix = os.path.splitext(pdb_file)[0] + '-refined'

        # Main files
        self.pdb_file = pdb_file
        self.mtz_file = mtz_file
        self.cif_file = cif_file

        # Eventual output prefix
        self.out_prefix = out_prefix
        self.out_pdb_file = self.out_prefix + '.pdb'
        self.out_mtz_file = self.out_prefix + '.mtz'
        self.out_log_file = self.out_prefix + '.log'

        # Validate input
        assert os.path.exists(self.pdb_file)
        assert os.path.exists(self.mtz_file)
        assert not os.path.exists(
            self.out_pdb_file), 'Output file already exists: {}'.format(
                self.out_pdb_file)
        assert not os.path.exists(
            self.out_mtz_file), 'Output file already exists: {}'.format(
                self.out_mtz_file)

        # Create temporary folder for refinement
        self.tmp_dir = tempfile.mkdtemp(prefix='refine-model-')
        self.tmp_pre = os.path.join(self.tmp_dir, 'refine')

        # Command object for refinement
        self.cmd = CommandManager(self.program)
        self.kw_args = kw_args

        # Setup, refine and post_process
        if self.auto is True:
            self.run()

    def run(self):
        """...run refinement amd export files"""
        self.setup()
        ret = self.refine()
        self.export()
        return ret

    def setup(self):
        raise Exception('Dummy class -- not implemented')

    def refine(self):
        """...run the refinement"""
        self.cmd.print_settings()
        return self.cmd.run()

    def export(self):
        """Copy files to output destination"""
        # Find pdb file in the output folder
        tmp_pdb = glob.glob(self.tmp_pre + '*.pdb')
        assert tmp_pdb, 'No refined files found: {}'.format(self.tmp_dir)
        tmp_pdb = tmp_pdb[0]
        tmp_mtz = tmp_pdb.replace('.pdb', '.mtz')
        assert os.path.exists(tmp_pdb)
        assert os.path.exists(tmp_mtz)
        # Copy to output folder
        shutil.copy(tmp_pdb, self.out_pdb_file)
        shutil.copy(tmp_mtz, self.out_mtz_file)
        assert os.path.exists(self.out_pdb_file)
        assert os.path.exists(self.out_mtz_file)
        # Write the log to the output log file
        self.cmd.write_output(self.out_log_file)
        # Delete temporary directory
        shutil.rmtree(self.tmp_dir)
Example #3
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 '============================>'
Example #4
0
def run(params):

    assert params.input.pdb and os.path.exists(
        params.input.pdb), 'Need to provide an pdb file'
    assert params.input.mtz and os.path.exists(
        params.input.mtz), 'Need to provide an mtz file'

    out_mtz = os.path.splitext(params.input.mtz)[0] + params.output.suffix
    assert not os.path.exists(out_mtz)
    out_log = out_mtz.replace('.mtz', '.log')

    ###########################################
    # Load diffraction data
    ###########################################
    diff_data = iotbx.mtz.object(params.input.mtz)

    ###########################################
    # COMMAND LINE COMMANDS
    ###########################################
    cm = CommandManager('phenix.f000')
    cm.add_command_line_arguments(params.input.pdb)

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

    f000_regex = re.compile(
        'Estimate of F\(0,0,0\)=(.*?) given mean bulk-solvent density (.*?) and fraction (.*?)\n'
    )
    f000_details = f000_regex.findall(cm.output)

    assert len(
        f000_details) == 1, 'Error extracting f000 from output of phenix.f000'
    f000, solvent_density, solvent_fraction = map(float, f000_details[0])
    print 'F000 is {}'.format(f000)

    ###########################################
    # Append to diffraction data
    ###########################################

    col_labs = params.options.column.label.split(',')
    print 'Extracting column labels: {}'.format(col_labs)

    miller_dict = diff_data.as_miller_arrays_dict()
    ampl_data = [d for l, d in miller_dict.items() if l[2] in col_labs]
    assert ampl_data, 'Matching AMPLITUDE DATA not in mtz data: {}'.format(
        ampl)
    assert len(ampl_data) == 2
    ampl_data = ampl_data[0]
    assert ampl_data.info().labels == col_labs

    # Create new objects
    new_data = ampl_data.customized_copy()
    # Append the F000 term
    new_data.indices().append((0, 0, 0))
    new_data.data().append(f000)
    # Sort for kicks
    new_data = new_data.sort('packed_indices')

    #    from IPython import embed; embed(); assert 0;

    # Create new "dataset" from the miller array of the amplitudes
    mtz_dataset = new_data.as_mtz_dataset(column_root_label="2FOFCWT-ABS")
    # Convert "dataset" to an "object"
    mtz_object = mtz_dataset.mtz_object()
    mtz_object.write(file_name=out_mtz)
Example #5
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 '============================>'