<commit_before>#!/usr/bin/env python
#-*- coding:utf-8 -*-
#
#       find_decoys.py is part of Decoy Finder
#
#       Copyright 2011 <NAME> <<EMAIL>>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

#Condicions per al decoy respecte el lligand actiu:
# - Tanimoto MACCS < 0,9
# - HBA 0 (+-1)
# - ClogP  +-1 (+- 1.5)
# - HBD 0 (+-1)
# - Molecular weight +/-40 Da
# - exact same number of rotational bonds

import pybel, os, urllib2, tempfile, random,  sys
from decimal import Decimal
#Decimal() serveix per fer operacions exactes amb decimals, ja que el built-in float és imprecís

##### Aquesta part és necessària per a poder fer servir MACCS fingerprinting des de pybel en versions antiquades d'openbabel
if "MACCS" not in pybel.fps:
    pybel.fps.append("MACCS")
    pybel._fingerprinters = pybel._getplugins(pybel.ob.OBFingerprint.FindFingerprint, pybel.fps)
#####
informats = ''
for format in pybel.informats.iterkeys():
    informats += "*.%s " %format
    if os.name != 'nt':
        for compression in ('gz', 'tar',  'bz',  'bz2',  'tar.gz',  'tar.bz',  'tar.bz2'):
            informats += "*.%s.%s " % (format,  compression)


#Alguns valors per defecte:

HBA_t = 0 #1
HBD_t = 0#1
ClogP_t = Decimal(1)#1.5
tanimoto_t = Decimal('0.9')
tanimoto_d = Decimal('0.9')
MW_t = 40
RB_t = 0#1

#Creem dos filtres per aconseguir els HBD i HBA:
HBA = pybel.Smarts("[#7,#8]")
HBD = pybel.Smarts("[#7,#8;!H0]")

ZINC_subsets = {"lead-like":"1","fragment-like":"2","drug-like":"3","all-purchasable":"6","everything":"10","clean-leads":"11","clean-fragments":"12","clean-drug-like":"13","all-clean":"16","leads-now":"21","frags-now":"22","drugs-now":"23","all-now":"26","sarah":"37","Stan":"94"}

class ComparableMol():
    """
    """
    def __init__(self, mol):
        self.mol = mol
        self.fp = mol.calcfp("MACCS")
        self.hba = len(HBA.findall(mol))
        self.hbd = len(HBD.findall(mol))
        self.clogp = Decimal(str(mol.calcdesc(['logP'])['logP']))
        self.mw = mol.molwt
        self.rot = mol.OBMol.NumRotors()
        self.title = mol.title
    def __str__(self):
        return "Title: %s; HBA: %s; HBD: %s; CLogP: %s; MW:%s \n" % (self.title, self.hba, self.hbd, self.clogp, self.mw)

def get_zinc_slice(slicename, subset = '10', cachedir = tempfile.gettempdir(),  keepcache = False):
    """
    returns an iterable list of files from  online ZINC slices
    """
    if slicename in ('all', 'single', 'usual', 'metals'):
        script = "http://zinc.docking.org/subset1/%s/%s.sdf.csh" % (subset,slicename)
        handler = urllib2.urlopen(script)
        print "Reading ZINC data..."
        scriptcontent = handler.read().split('\n')
        handler.close()
        filelist = scriptcontent[1:-2]
        #print slicename
        yield len(filelist)
        random.shuffle(filelist)
        #print filelist
        #print "Provant si va o no"
        parenturl = scriptcontent[0].split()[1].split('=')[1]
        for file in filelist:
            #print "treballant amb %s" % file

            outfilename = os.path.join(cachedir, file)
            if not (keepcache and os.path.isfile(outfilename)):
                print 'File not cached or cache disabled; downloading %s' % parenturl + file
                dbhandler = urllib2.urlopen(parenturl + file)
                outfile = open(outfilename, "wb")
                outfile.write(dbhandler.read())
                dbhandler.close()
                outfile.close()
            else:
                print "Loading cached file: %s" % outfilename
            yield str(outfilename)
            #print outfilename
            if not keepcache:
                try:
                    os.remove(outfilename)
                except Exception,  e:
                    print "Unable to remove %s" % (outfilename)
                    print unicode(e)
    else:
        raise Exception,  u"Unknown slice"

def get_fileformat(file):
    """
    """
    index = -1
    ext = file.split(".")[index].lower()
    while ext in ('gz', 'tar',  'bz',  'bz2'):
        index -= 1
        ext = file.split(".")[index].lower()
    if ext in pybel.informats.keys():
        #print ext
        return ext
    else:
       print "%s: unknown format"  % file<commit_msg>Check that filesize matches current ZINC files before deciding not to download<commit_after>#!/usr/bin/env python
#-*- coding:utf-8 -*-
#
#       find_decoys.py is part of Decoy Finder
#
#       Copyright 2011 <NAME> <<EMAIL>>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

#Condicions per al decoy respecte el lligand actiu:
# - Tanimoto MACCS < 0,9
# - HBA 0 (+-1)
# - ClogP  +-1 (+- 1.5)
# - HBD 0 (+-1)
# - Molecular weight +/-40 Da
# - exact same number of rotational bonds

import pybel, os, urllib2, tempfile, random,  sys
from decimal import Decimal
#Decimal() serveix per fer operacions exactes amb decimals, ja que el built-in float és imprecís

##### Aquesta part és necessària per a poder fer servir MACCS fingerprinting des de pybel en versions antiquades d'openbabel
if "MACCS" not in pybel.fps:
    pybel.fps.append("MACCS")
    pybel._fingerprinters = pybel._getplugins(pybel.ob.OBFingerprint.FindFingerprint, pybel.fps)
#####
informats = ''
for format in pybel.informats.iterkeys():
    informats += "*.%s " %format
    if os.name != 'nt':
        for compression in ('gz', 'tar',  'bz',  'bz2',  'tar.gz',  'tar.bz',  'tar.bz2'):
            informats += "*.%s.%s " % (format,  compression)


#Alguns valors per defecte:

HBA_t = 0 #1
HBD_t = 0#1
ClogP_t = Decimal(1)#1.5
tanimoto_t = Decimal('0.9')
tanimoto_d = Decimal('0.9')
MW_t = 40
RB_t = 0#1

#Creem dos filtres per aconseguir els HBD i HBA:
HBA = pybel.Smarts("[#7,#8]")
HBD = pybel.Smarts("[#7,#8;!H0]")

ZINC_subsets = {"lead-like":"1","fragment-like":"2","drug-like":"3","all-purchasable":"6","everything":"10","clean-leads":"11","clean-fragments":"12","clean-drug-like":"13","all-clean":"16","leads-now":"21","frags-now":"22","drugs-now":"23","all-now":"26","sarah":"37","Stan":"94"}

class ComparableMol():
    """
    """
    def __init__(self, mol):
        self.mol = mol
        self.fp = mol.calcfp("MACCS")
        self.hba = len(HBA.findall(mol))
        self.hbd = len(HBD.findall(mol))
        self.clogp = Decimal(str(mol.calcdesc(['logP'])['logP']))
        self.mw = mol.molwt
        self.rot = mol.OBMol.NumRotors()
        self.title = mol.title
    def __str__(self):
        return "Title: %s; HBA: %s; HBD: %s; CLogP: %s; MW:%s \n" % (self.title, self.hba, self.hbd, self.clogp, self.mw)

def get_zinc_slice(slicename = 'all', subset = '10', cachedir = tempfile.gettempdir(),  keepcache = False):
    """
    returns an iterable list of files from  online ZINC slices
    """
    if slicename in ('all', 'single', 'usual', 'metals'):
        script = "http://zinc.docking.org/subset1/%s/%s.sdf.csh" % (subset,slicename)
        handler = urllib2.urlopen(script)
        print "Reading ZINC data..."
        scriptcontent = handler.read().split('\n')
        handler.close()
        filelist = scriptcontent[1:-2]
        #print slicename
        yield len(filelist)
        random.shuffle(filelist)
        parenturl = scriptcontent[0].split()[1].split('=')[1]
        for file in filelist:
            dbhandler = urllib2.urlopen(parenturl + file)
            outfilename = os.path.join(cachedir, file)
            download_needed = True
            if keepcache:
                filesize = dbhandler.info().get('Content-Length')
                if filesize:
                    filesize = int(filesize)

                if os.path.isfile(outfilename):
                    localsize = os.path.getsize(outfilename)
                    download_needed = localsize != filesize
                    if download_needed:
                        print "Local file outdated or incomplete"

            if download_needed:
                print 'Downloading %s' % parenturl + file
                outfile = open(outfilename, "wb")
                outfile.write(dbhandler.read())
                outfile.close()
            else:
                print "Loading cached file: %s" % outfilename
            dbhandler.close()
            yield str(outfilename)
            #print outfilename
            if not keepcache:
                try:
                    os.remove(outfilename)
                except Exception,  e:
                    print "Unable to remove %s" % (outfilename)
                    print unicode(e)
    else:
        raise Exception,  u"Unknown slice"

def get_fileformat(file):
    """
    """
    index = -1
    ext = file.split(".")[index].lower()
    while ext in ('gz', 'tar',  'bz',  'bz2'):
        index -= 1
        ext = file.split(".")[index].lower()
    if ext in pybel.informats.keys():
        #print ext
        return ext
    else:
       print "%s: unknown format"  % file
{'fraction_non_alphanumeric': 0.1377536161537715, 'fraction_numerical': 0.01747403164741287, 'mean_word_length': 2.454728370221328, 'pattern_counts': {'":': 30, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '453480', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3283, 'n_words': 936}