Example #1
0
    def testPathCounts(self):
        """ FIX: this should be in some other file

    """
        data = [
            ('CCCCCC', (6, 5, 4, 3, 2, 1)),
            ('CCC(C)CC', (6, 5, 5, 4, 1, 0)),
            ('CC(C)CCC', (6, 5, 5, 3, 2, 0)),
            ('CC(C)C(C)C', (6, 5, 6, 4, 0, 0)),
            ('CC(C)(C)CC', (6, 5, 7, 3, 0, 0)),
            ('CCCCCO', (6, 5, 4, 3, 2, 1)),
            ('CCC(O)CC', (6, 5, 5, 4, 1, 0)),
            ('CC(O)(C)CC', (6, 5, 7, 3, 0, 0)),
            ('c1ccccc1O', (7, 7, 8, 8, 8, 8)),
        ]
        for smi, res in data:
            m = Chem.MolFromSmiles(smi)
            for i in range(1, 6):
                cnt = len(Chem.FindAllPathsOfLengthN(m, i, useBonds=1))
                assert cnt == res[i], (smi, i, cnt, res[i],
                                       Chem.FindAllPathsOfLengthN(m,
                                                                  i,
                                                                  useBonds=1))
                cnt = len(Chem.FindAllPathsOfLengthN(m, i + 1, useBonds=0))
                assert cnt == res[i], (smi, i, cnt, res[i],
                                       Chem.FindAllPathsOfLengthN(m,
                                                                  i + 1,
                                                                  useBonds=1))
Example #2
0
    def calculate(self):
        L = 0
        pi = 0

        self._gen_bonds()

        for path in Chem.FindAllPathsOfLengthN(self.mol, self._order):
            aids = set()
            before = None
            w = 1

            for i in self._bond_ids_to_atom_ids(path):
                if i in aids:
                    break

                aids.add(i)

                if before is not None:
                    bond = self.mol.GetBondBetweenAtoms(before, i)
                    w *= bond.GetBondTypeAsDouble()

                before = i

            else:
                L += 1
                pi += w

        return L, pi
Example #3
0
def CalculateKappa2(mol):
    """
    #################################################################
    Calculation of molecular shape index for two bonded fragment
    
    ---->kappa2

    Usage:
        
        result=CalculateKappa2(mol)
        
        Input: mol is a molecule object.
        
        Output: result is a numeric value.
    #################################################################
    """
    P2=len(Chem.FindAllPathsOfLengthN(mol,2))
    A=mol.GetNumHeavyAtoms()

    denom=P2+0.0
    if denom:
        kappa=(A-1)*(A-2)**2/denom**2
    else:
        kappa=0.0
    return round(kappa,3)
Example #4
0
def CalculateKappaAlapha2(mol):
    """
    #################################################################
    Calculation of molecular shape index for two bonded fragment

    with Alapha

    ---->kappam2

    Usage:

        result=CalculateKappaAlapha2(mol)

        Input: mol is a molecule object.

        Output: result is a numeric value.
    #################################################################
    """
    P2 = len(Chem.FindAllPathsOfLengthN(mol, 2))
    A = mol.GetNumAtoms(onlyHeavy=1)
    alpha = _HallKierAlpha(mol)
    denom = P2 + alpha
    if denom:
        kappa = (A + alpha - 1) * (A + alpha - 2)**2 / denom**2
    else:
        kappa = 0.0
    return round(kappa, 3)
Example #5
0
def CalculateKappaAlapha3(mol):
    """
    #################################################################
    Calculation of molecular shape index for three bonded fragment 
    
    with Alapha
    
    ---->kappam3
    
    Usage:
        
        result=CalculateKappaAlapha3(mol)
        
        Input: mol is a molecule object.
        
        Output: result is a numeric value.
    #################################################################
    """
    P3=len(Chem.FindAllPathsOfLengthN(mol,3))
    A=mol.GetNumHeavyAtoms()
    alpha=_HallKierAlpha(mol)
    denom=P3+alpha
    if denom:
        if A % 2 == 1:
            kappa=(A+alpha-1)*(A+alpha-3)**2/denom**2
        else:
            kappa=(A+alpha-3)*(A+alpha-2)**2/denom**2
    else:
        kappa=0.0
    return round(kappa,3)
Example #6
0
def CalculateKappa3(mol):
    """
    #################################################################
    Calculation of molecular shape index for three bonded fragment

    ---->kappa3

    Usage:

        result=CalculateKappa3(mol)

        Input: mol is a molecule object.

        Output: result is a numeric value.
    #################################################################
    """
    P3 = len(Chem.FindAllPathsOfLengthN(mol, 3))
    A = mol.GetNumAtoms(onlyHeavy=1)

    denom = P3 + 0.0
    if denom:
        if A % 2 == 1:
            kappa = (A - 1) * (A - 3)**2 / denom**2
        else:
            kappa = (A - 3) * (A - 2)**2 / denom**2
    else:
        kappa = 0.0
    return round(kappa, 3)
Example #7
0
def _CalculateBasakICn(mol, NumPath=1):
    """
    #################################################################
    **internal used only**
    
    Obtain the information content with order n proposed by Basak
    #################################################################
    """
    Hmol = Chem.AddHs(mol)
    nAtoms = Hmol.GetNumAtoms()
    TotalPath = Chem.FindAllPathsOfLengthN(Hmol, NumPath, useBonds=0, useHs=1)
    if len(TotalPath) == 0:
        BasakIC = 0.0
    else:
        IC = {}
        for i in range(nAtoms):
            temp = []
            at = Hmol.GetAtomWithIdx(i)
            temp.append(at.GetAtomicNum())
            for index in TotalPath:
                if i == index[0]:
                    temp.extend([
                        Hmol.GetAtomWithIdx(kk).GetAtomicNum()
                        for kk in index[1:]
                    ])
                if i == index[-1]:
                    cds = list(index)
                    cds.reverse()
                    temp.extend([
                        Hmol.GetAtomWithIdx(kk).GetAtomicNum()
                        for kk in cds[1:]
                    ])
            # print temp

            IC[str(i)] = temp
        cds = []
        for value in IC.values():
            value.sort()
            cds.append(value)
        kkk = list(range(len(cds)))
        aaa = copy.deepcopy(kkk)
        res = []
        for i in aaa:
            if i in kkk:
                jishu = 0
                kong = []
                temp1 = cds[i]
                for j in aaa:
                    if cds[j] == temp1:
                        jishu = jishu + 1
                        kong.append(j)
                for ks in kong:
                    kkk.remove(ks)
                res.append(jishu)

                # print res_CalculateBasakICn
        BasakIC = _CalculateEntropy(numpy.array(res, numpy.float) / sum(res))

    return BasakIC
def _CalculateBasakICn(mol,NumPath=1):

    """
    #################################################################
    **internal used only**

    Obtain the information content with order n proposed by Basak
    #################################################################
    """
    Hmol=Chem.AddHs(mol)
    nAtoms=Hmol.GetNumAtoms()
    TotalPath=Chem.FindAllPathsOfLengthN(Hmol,NumPath,useBonds=0,useHs=1)
    if len(TotalPath)==0:
        BasakIC=0.0
    else:
        IC={}
        for i in range(nAtoms):
            temp=[]
            at=Hmol.GetAtomWithIdx(i)
            temp.append(at.GetAtomicNum())
            for index in TotalPath:
                if i==index[0]:
                    temp.append([Hmol.GetAtomWithIdx(kk).GetAtomicNum() for kk in index[1:]])
                if i==index[-1]:
                    cds=list(index)
                    cds.reverse()
                    temp.append([Hmol.GetAtomWithIdx(kk).GetAtomicNum() for kk in cds[1:]])
            #print temp

            IC[str(i)]=temp
        cds=[]
        for value in IC.values():
            #print(value) #this line added for debugging
            bla = value[0] #added
            value = [int(x[0]) for x in value[1:]] #added by Peter Sarvari
            value = [bla] + value #append to the front of the vale list, added
            #print(value) #added for debugging
            value.sort()
            cds.append(value)
        kkk=list(range(len(cds))) #because in Python 3 range does not return a list, typecasted by Peter
        aaa=copy.deepcopy(kkk)
        res=[]
        for i in aaa:
            if i in kkk:
                jishu=0
                kong=[]
                temp1=cds[i]
                for j in aaa:
                    if cds[j]==temp1:
                        jishu=jishu+1
                        kong.append(j)
                for ks in kong:
                    kkk.remove(ks)
                res.append(jishu)

        #print res
        BasakIC=_CalculateEntropy(numpy.array(res,numpy.float)/sum(res))

    return BasakIC
Example #9
0
def CalculateKappa2(mol: Chem.Mol) -> float:
    """Calculate molecular shape index for two bonded fragments."""
    P2 = len(Chem.FindAllPathsOfLengthN(mol, 2))
    A = mol.GetNumHeavyAtoms()
    denom = P2 + 0.0
    if denom:
        kappa = (A - 1) * (A - 2)**2 / denom**2
    else:
        kappa = 0.0
    return round(kappa, 3)
Example #10
0
def _CalculateChivnp(mol, NumPath=1):
    accum = 0.0
    deltas = _HKDeltas(mol, skipHs=0)
    for path in Chem.FindAllPathsOfLengthN(mol, NumPath + 1, useBonds=0):
        cAccum = 1.0
        for idx in path:
            cAccum *= deltas[idx]
        if cAccum:
            accum += 1. / numpy.sqrt(cAccum)
    return accum
Example #11
0
def _CalculateChinp(mol, NumPath=2):
    accum = 0.0
    deltas = [x.GetDegree() for x in mol.GetAtoms()]
    for path in Chem.FindAllPathsOfLengthN(mol, NumPath + 1, useBonds=0):
        cAccum = 1.0
        for idx in path:
            cAccum *= deltas[idx]
        if cAccum:
            accum += 1. / numpy.sqrt(cAccum)
    return accum
Example #12
0
def _CalculateChinp(mol: Chem.Mol, NumPath: int = 2) -> float:
    """Calculate molecular connectivity chi index for path order 2."""
    accum = 0.0
    deltas = [x.GetDegree() for x in mol.GetAtoms()]
    for path in Chem.FindAllPathsOfLengthN(mol, NumPath + 1, useBonds=0):
        cAccum = 1.0
        for idx in path:
            cAccum *= deltas[idx]
        if cAccum:
            accum += 1. / numpy.sqrt(cAccum)
    return accum
Example #13
0
def _CalculateChivnp(mol: Chem.Mol, NumPath: int = 1) -> float:
    """Calculate valence molecular connectivity chi index for path order 1."""
    accum = 0.0
    deltas = _HallKierDeltas(mol, skipHs=0)
    for path in Chem.FindAllPathsOfLengthN(mol, NumPath + 1, useBonds=0):
        cAccum = 1.0
        for idx in path:
            cAccum *= deltas[idx]
        if cAccum:
            accum += 1. / numpy.sqrt(cAccum)
    return accum
Example #14
0
def CalculateKappaAlapha2(mol):
    """
    Calculation of molecular shape index for two bonded fragment
    """
    P2 = len(Chem.FindAllPathsOfLengthN(mol, 2))
    A = mol.GetNumHeavyAtoms()
    alpha = _HallKierAlpha(mol)
    denom = P2 + alpha
    if denom:
        kappa = (A + alpha - 1) * (A + alpha - 2)**2 / denom**2
    else:
        kappa = 0.0
    return round(kappa, 3)
Example #15
0
def CalculateKappa3(mol: Chem.Mol) -> float:
    """Calculate molecular shape index for three bonded fragments."""
    P3 = len(Chem.FindAllPathsOfLengthN(mol, 3))
    A = mol.GetNumHeavyAtoms()
    denom = P3 + 0.0
    if denom:
        if A % 2 == 1:
            kappa = (A - 1) * (A - 3)**2 / denom**2
        else:
            kappa = (A - 3) * (A - 2)**2 / denom**2
    else:
        kappa = 0.0
    return round(kappa, 3)
Example #16
0
def getChivnp(mol, NumPath=1):
    """#################################################################
    Calculation of valence molecular connectivity chi index for path order 1
    #################################################################
    """
    accum = 0.0
    deltas = getHKDeltas(mol, skipHs=0)
    for path in Chem.FindAllPathsOfLengthN(mol, NumPath + 1, useBonds=0):
        cAccum = 1.0
        for idx in path:
            cAccum *= deltas[idx]
        if cAccum:
            accum += 1. / numpy.sqrt(cAccum)
    return accum
Example #17
0
 def _validate_sidechain(self):
     connection_atom = utils.get_atom_with_map_num(
         self.reacting_mol, self.SIDECHAIN_OLIGOMERIZATION_MAP_NUM)
     paths = Chem.FindAllPathsOfLengthN(
         self.reacting_mol,
         3,
         useBonds=False,
         rootedAtAtom=self.reacting_atom.GetIdx())
     atoms = set().union([atom for path in paths for atom in path])
     if connection_atom.GetIdx() not in atoms - set(
             atom.GetIdx() for atom in connection_atom.GetNeighbors()):
         raise InvalidMolecule(
             'The attachment point of the reacting sidechain must be two atoms away from the reacting atom in a pictet spangler reaction!'
         )
Example #18
0
def _CalculateChinp(mol, NumPath=2):
    """
    **Internal used only**
    Calculation of molecular connectivity chi index for path order 2
    """
    accum = 0.0
    deltas = [x.GetDegree() for x in mol.GetAtoms()]
    for path in Chem.FindAllPathsOfLengthN(mol, NumPath + 1, useBonds=0):
        cAccum = 1.0
        for idx in path:
            cAccum *= deltas[idx]
        if cAccum:
            accum += 1. / numpy.sqrt(cAccum)
    return accum
Example #19
0
def _pyKappa2(mol):
    """  Hall-Kier Kappa2 value

   From equations (58) and (60) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
    P2 = len(Chem.FindAllPathsOfLengthN(mol, 2))
    A = mol.GetNumHeavyAtoms()
    alpha = HallKierAlpha(mol)
    denom = (P2 + alpha)**2
    if denom:
        kappa = (A + alpha - 1) * (A + alpha - 2)**2 / denom
    else:
        kappa = 0
    return kappa
Example #20
0
def _pyChiNv_(mol,order=2):
  """  From equations (5),(15) and (16) of Rev. Comp. Chem. vol 2, 367-422, (1991)
   
  **NOTE**: because the current path finding code does, by design,
  detect rings as paths (e.g. in C1CC1 there is *1* atom path of
  length 3), values of ChiNv with N >= 3 may give results that differ
  from those provided by the old code in molecules that have rings of
  size 3.
  
  """
  deltas = numpy.array([(1. / numpy.sqrt(hkd) if hkd!=0.0 else 0.0) for hkd in _hkDeltas(mol, skipHs=0)])
  accum = 0.0
  for path in Chem.FindAllPathsOfLengthN(mol, order + 1, useBonds=0):
    accum += numpy.prod(deltas[numpy.array(path)])
  return accum
Example #21
0
def _pyChiNn_(mol, order=2):
    """  Similar to Hall Kier ChiNv, but uses nVal instead of valence
  This makes a big difference after we get out of the first row.

  **NOTE**: because the current path finding code does, by design,
  detect rings as paths (e.g. in C1CC1 there is *1* atom path of
  length 3), values of ChiNn with N >= 3 may give results that differ
  from those provided by the old code in molecules that have rings of
  size 3.

  """
    nval = [_nVal(x) for x in mol.GetAtoms()]
    deltas = numpy.array([(1. / numpy.sqrt(x) if x else 0.0) for x in nval])
    accum = 0.0
    for path in Chem.FindAllPathsOfLengthN(mol, order + 1, useBonds=0):
        accum += numpy.prod(deltas[numpy.array(path)])
    return accum
Example #22
0
 def _validate_monomer(self):
     try:
         n_term_atom = utils.get_atom_with_map_num(
             self.reacting_mol, self.BACKBONE_NITROGEN_MAP_NUM)
         paths = Chem.FindAllPathsOfLengthN(
             self.reacting_mol,
             5,
             useBonds=False,
             rootedAtAtom=self.reacting_atom.GetIdx())
         atoms = set().union([atom for path in paths for atom in path])
         if n_term_atom.GetIdx() not in atoms - set(
                 atom.GetIdx() for atom in n_term_atom.GetNeighbors()):
             raise InvalidMolecule(
                 'The reacting atom in the monomer must be 4 atoms away from the N-terminus!'
             )
     except (AttributeError, RuntimeError):
         raise InvalidMolecule
Example #23
0
def _pyKappa3(mol):
    """  Hall-Kier Kappa3 value

   From equations (58), (61) and (62) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
    P3 = len(Chem.FindAllPathsOfLengthN(mol, 3))
    A = mol.GetNumHeavyAtoms()
    alpha = HallKierAlpha(mol)
    denom = (P3 + alpha)**2
    if denom:
        if A % 2 == 1:
            kappa = (A + alpha - 1) * (A + alpha - 3)**2 / denom
        else:
            kappa = (A + alpha - 2) * (A + alpha - 3)**2 / denom
    else:
        kappa = 0
    return kappa
Example #24
0
def _CalculatePathN(mol, PathLength=2):
    """
    #################################################################
    *Internal Use Only*

    Calculation of the counts of path length N for a molecule

    ---->PC1-PC6

    Usage:

        result=CalculateMolWeight(mol)

        Input: mol is a molecule object.

        Output: result is a numeric value.
    #################################################################
    """
    return len(Chem.FindAllPathsOfLengthN(mol, PathLength, useBonds=1))
Example #25
0
def _CalculatePathN(mol, PathLength=2):
    """
    *Internal Use Only*
    Calculation of the counts of path length N for a molecule
    """
    return len(Chem.FindAllPathsOfLengthN(mol, PathLength, useBonds=1))
Example #26
0
def getPathMolecule(mol, PathLength=2):
    return len(Chem.FindAllPathsOfLengthN(mol, PathLength, useBonds=1))
Example #27
0
def _CalculatePathN(mol: Chem.Mol, PathLength=2) -> float:
    """Calculate number of path of length N."""
    return len(Chem.FindAllPathsOfLengthN(mol, PathLength, useBonds=1))
Example #28
0
def CalculatePath6(mol):
    return len(Chem.FindAllPathsOfLengthN(mol,6))
Example #29
0
def _CalculatePathN(mol,PathLength=2):
    return len(Chem.FindAllPathsOfLengthN(mol,PathLength,useBonds=1))