Exemplo n.º 1
0
    def POST(self, request):
        form = UploadSDFForm(request.POST, request.FILES)

        if form.is_valid():
            upload = request.FILES['sdf_file']
            tf = tempfile.NamedTemporaryFile()
            for chunk in upload.chunks():
                tf.write(chunk)

            tf.flush()
            tf.seek(0)
            molecules = SDMolSupplier(tf.name)

            tag, created = ChemicalTag.objects.get_or_create(
                name=form.cleaned_data['tag'])

            loaded = 0
            skipped = 0
            for mol in molecules:
                result = load_mol(mol, tag)

                if result == -1:
                    skipped += 1
                else:
                    loaded += 1

            return HttpResponseRedirect(reverse('tag-index'))

        else:
            return render(request, 'cspace/upload-sdf.html', {'form': form})
Exemplo n.º 2
0
    def handle(self, *args, **options):
        loaded = 0
        skipped = 0

        tag, created = ChemicalTag.objects.get_or_create(
            name=options['tag_name'])

        molecules = SDMolSupplier(options['path'])

        for mol in molecules:
            result = utils.load_mol(mol)

            if result == -1:
                self.stderr.write(
                    self.style.WARNING(
                        ('Chemical with SMILES of "%s" already exist in '
                         'database. Only adding tag.') % smiles))
                skipped += 1
                continue

            else:
                loaded += 1

        self.stdout.write(
            self.style.SUCCESS('Loaded %d chemicals and skipped %d' %
                               (loaded, skipped)))
Exemplo n.º 3
0
    def _initPatterns(self):
        """

    >>> remover = SaltRemover()
    >>> len(remover.salts)>0
    True

    Default input format is SMARTS
    >>> remover = SaltRemover(defnData="[Cl,Br]")
    >>> len(remover.salts)
    1

    >>> remover = SaltRemover(defnData="[Na+]\\nCC(=O)O", defnFormat=InputFormat.SMILES)
    >>> len(remover.salts)
    2

    >>> from rdkit import RDLogger
    >>> RDLogger.DisableLog('rdApp.error')
    >>> remover = SaltRemover(defnData="[Cl,fail]")
    Traceback (most recent call last):
      ...
    ValueError: [Cl,fail]

    >>> RDLogger.EnableLog('rdApp.error')
    """
        if self.defnData:
            from rdkit.six.moves import cStringIO as StringIO
            inF = StringIO(self.defnData)
            with closing(inF):
                self.salts = []
                for line in inF:
                    if line:
                        if self.defnFormat == InputFormat.SMARTS:
                            salt = _smartsFromSmartsLine(line)
                        elif self.defnFormat == InputFormat.SMILES:
                            salt = Chem.MolFromSmiles(line)
                        else:
                            raise ValueError(
                                'Unsupported format for supplier.')
                        if salt is None:
                            raise ValueError(line)
                        self.salts.append(salt)
        else:
            if self.defnFormat == InputFormat.SMARTS:
                self.salts = [
                    mol for mol in _getSmartsSaltsFromFile(self.defnFilename)
                ]
            elif self.defnFormat == InputFormat.MOL:
                self.salts = [mol for mol in SDMolSupplier(self.defnFilename)]
            elif self.defnFormat == InputFormat.SMILES:
                self.salts = [
                    mol for mol in SmilesMolSupplier(self.defnFilename)
                ]
            else:
                raise ValueError('Unsupported format for supplier.')
Exemplo n.º 4
0
def mol_supplier(filename, ext):
    """
    Based on the file extension, use the appropriate RDKit function to
    load a chemical data file (SMILES or SDF) containing multiple molecules
    and return a list of RDKit Mol objects
    """
    if ext == 'sdf':
        return [n for n in SDMolSupplier(filename)]
    with open(filename) as f:
        mols = f.read().split('\n')
    if ext == 'smi' or ext == 'inchi':
        return [
            Chem.MolFromSmiles(mol, sanitize=True) for mol in mols if mol != ''
        ]
Exemplo n.º 5
0
Arquivo: ops.py Projeto: flyuuu/core
def get_decoys(pdb_file, mol_file, num_atoms, init='get_decoys_init'):
    """For each binding ligand, gets a list of decoy ligands. We filter by number
	of atoms and maximum common substructure (MCS). Then we generate conformers
	for each decoy and save them to the decoy_ligands folder"""

    init = eval(init)

    reader = SDMolSupplier(mol_file)
    mol = reader[0]
    output = []

    iterator = range(len(init.all_mols))
    random.shuffle(iterator)
    for i in iterator:
        if (init.all_mol_files[i] == mol_file or \
         abs(init.all_num_atoms[i] - num_atoms) > init.max_atom_dif):
            continue
        mcs = MCS.FindMCS([init.all_mols[i], mol],
                          minNumAtoms=init.max_substruct,
                          ringMatchesRingOnly=True,
                          completeRingsOnly=True,
                          timeout=1)
        if mcs.numAtoms == -1:
            #save the mol object as a PDB file in the decoys folder
            decoy_file = pdb_file.replace('/binding_ligands/',
                                          '/decoy_ligands/').replace(
                                              '.pdb',
                                              str(len(output)) + '.pdb')
            pdb_writer = PDBWriter(decoy_file)
            # generate the decoy and its conformers
            decoy2 = Chem.AddHs(init.all_mols[i])
            conf_ids = AllChem.EmbedMultipleConfs(decoy2, init.num_conformers)
            for cid in conf_ids:
                AllChem.MMFFOptimizeMolecule(decoy2, confId=cid)
                decoy = Chem.RemoveHs(decoy2)
                pdb_writer.write(decoy)

            pdb_writer.close()
            output.append([init.all_pdb_files[i], decoy_file])

        if len(output) >= init.max_num_decoys:
            break

    print 'Got the decoys for one ligand'
    return output
Exemplo n.º 6
0
def get_decoys(pdb_file, mol_file, num_atoms, init='get_decoys_init'):
    """
    For each binding ligand, get a list of decoy ligands. We filter by number of atoms and maximum common
    substructure (MCS). Returns filepaths to all binding ligand - decoy pair.

    :param pdb_file: pdb format ligand
    :param mol_file: mol format ligand
    :param num_atoms: ligand's atom number
    :param init:
    :return:
    nested list [[pdb_file, decoy_files]]
    """

    init = eval(init)
    reader = SDMolSupplier(mol_file)
    mol = reader[0]
    output = ""
    counter = 0

    # Shuffle which ligands we sample to avoid biases in decoy ligands
    iterator = range(len(init.all_mols))
    random.shuffle(iterator)
    for i in iterator:
        if (init.all_mol_files[i] == mol_file
                or abs(init.all_num_atoms[i] - num_atoms) >
                init.max_atom_dif):  # FIXME O2 time
            continue  # FIXME
        mcs = MCS.FindMCS([init.all_mols[i], mol],
                          minNumAtoms=init.max_substruct,
                          ringMatchesRingOnly=True,
                          completeRingsOnly=True,
                          timeout=1)
        if mcs.numAtoms == -1:
            if counter == init.max_num_decoys - 1:
                output += init.all_pdb_files[i]
                counter += 1
                break  # FIXME
        output += init.all_pdb_files[i] + ','
        counter += 1
    # Check to make sure there are enough decoys
    if counter < init.max_num_decoys:
        raise Exception("Not enough decoys for ligand " + pdb_file)
    print 'Got the decoys for one ligand'
    return [[pdb_file, output]]
Exemplo n.º 7
0
 def __init__(self, file_name=None):
     if file_name == None and self.file_name == None:
         return None
     self.file_name = file_name
     self.index = 0
     SDMolSupplier.__init__(self, file_name)
Exemplo n.º 8
0
def sdf2fragmentsdb_run(sdffns, fragmentsdb):
    frags = FragmentsDb(fragmentsdb)
    for sdffn in sdffns:
        logging.warning('Parsing {}'.format(sdffn))
        suppl = SDMolSupplier(sdffn)
        frags.add_molecules(suppl)