> For the complete documentation index, see [llms.txt](https://huang-jason.gitbook.io/complat-software-training-101/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://huang-jason.gitbook.io/complat-software-training-101/rdkit-process.md).

# RDKit - process

```python
for atom in m.GetAtoms():
    print(atom.GetAtomicNum())

m.GetBonds()[0].GetBondType() # SINGLE
```

```python
m.GetAtomWithIdx(0).GetSymbol() # 'C'
m.GetAtomWithIdx(0).GetExplicitValence() # 2

m.GetBondWithIdx(0).GetBeginAtomIdx() # 0
m.GetBondWithIdx(0).GetEndAtomIdx() # 1

m.GetBondBetweenAtoms(0,1).GetBondType() # rdkit.Chem.rdchem.BondType.SINGLE
```

## Ring

```python
m = Chem.MolFromSmiles('OC1C2C1CC2')
m.GetAtomWithIdx(0).IsInRing() # False
m.GetAtomWithIdx(1).IsInRing() # True
m.GetBondWithIdx(1).IsInRing() # True
m.GetAtomWithIdx(1).IsInRingSize(3) # True
```

```python
# the smallest set of smallest rings (SSSR)
Chem.GetSymmSSSR(m)
Chem.GetSSSR(m)
```

## Kekulize

```python
m = Chem.MolFromSmiles('c1ccccc1')

m.GetBondWithIdx(0).GetBondType() # rdkit.Chem.rdchem.BondType.AROMATIC
Chem.Kekulize(m)
m.GetBondWithIdx(0).GetBondType() # rdkit.Chem.rdchem.BondType.DOUBLE
m.GetBondWithIdx(1).GetBondType() # rdkit.Chem.rdchem.BondType.SINGLE
m.GetBondWithIdx(1).GetIsAromatic() # True
```

still aromatic after kekulized, unless the flag is cleared.

```python
Chem.Kekulize(m, clearAromaticFlags=True)
m.GetBondWithIdx(0).GetIsAromatic() # False
```

restore flag

```python
Chem.SanitizeMol(m)
m.GetBondWithIdx(0).GetBondType() # rdkit.Chem.rdchem.BondType.AROMATIC
```

### sanitize <-> kekulize

![](/files/-Lq83ULJWCEMsmjQSq_e)

<https://www.slideshare.net/baoilleach/we-need-to-talk-about-kekulization-aromaticity-and-smiles>

## 2D conformation

coord values: maximize the clarity of the drawing

#### align with template

```python
m = Chem.MolFromSmiles('c1nccc2n1ccc2')
AllChem.Compute2DCoords(m)

template = Chem.MolFromSmiles('c1nccc2n1ccc2')
AllChem.Compute2DCoords(template)
AllChem.GenerateDepictionMatching2DStructure(m,template)
```

![](/files/-Lq83ULLN9IjVMAfMdWe)![](/files/-Lq83ULNcvKFQ9qW3Om7)

## 3D conformation

```python
m = Chem.AddHs(m)
AllChem.EmbedMolecule(m)
```

## Pickleing

```python
m = Chem.MolFromSmiles('c1ccncc1')
import pickle
pkl = pickle.dumps(m)
m2=pickle.loads(pkl)
```

faster to build a molecule from a pickle than from a Mol file or SMILES string

```python
binStr = m.ToBinary()
m2 = Chem.Mol(binStr)
Chem.MolToSmiles(m2)
```

## Drawing

```python
suppl = Chem.SDMolSupplier('data/cdk2.sdf')
ms = [x for x in suppl if x is not None]
for m in ms: tmp=AllChem.Compute2DCoords(m)

from rdkit.Chem import Draw
Draw.MolToFile(ms[0],'images/cdk2_mol1.o.png')
```

## Substructure searching

```python
m = Chem.MolFromSmiles('C1=CC=CC=C1OC')
m.HasSubstructMatch(Chem.MolFromSmiles('COC')) # True
m.HasSubstructMatch(Chem.MolFromSmarts('COC')) # False
m.HasSubstructMatch(Chem.MolFromSmarts('COc')) # True #<- need an aromatic C
```

```python
m = Chem.MolFromSmiles('c1ccccc1O')
patt = Chem.MolFromSmarts('ccO')
m.HasSubstructMatch(patt) # True
m.GetSubstructMatches(patt) # ((0, 5, 6), (4, 5, 6))
```

By default, stereochemistry is not used in substructure searches.

```python
m = Chem.MolFromSmiles('CC[C@H](F)Cl')
m.HasSubstructMatch(Chem.MolFromSmiles('C[C@@H](F)Cl')) # True
m.HasSubstructMatch(Chem.MolFromSmiles('C[C@@H](F)Cl'),useChirality=True) # False
```

## Atom map

```python
qmol = Chem.MolFromSmarts( '[cH0:1][c:2]([cH0])!@[CX3!r:3]=[NX2!r:4]' )
ind_map = {}
for atom in qmol.GetAtoms() :
    map_num = atom.GetAtomMapNum()
    if map_num:
        ind_map[map_num-1] = atom.GetIdx()
ind_map # {0: 0, 1: 1, 2: 3, 3: 4}
map_list = [ind_map[x] for x in sorted(ind_map)]
map_list # [0, 1, 3, 4]

# substructure match
mol = Chem.MolFromSmiles('Cc1cccc(C)c1C(C)=NC')
for match in mol.GetSubstructMatches( qmol ) :
    mas = [match[x] for x in map_list]
    print(mas) # [1, 7, 8, 10]
```
