# 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

![](https://437555650-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lq83PytCJFhVoqGvu-1%2F-Lq83RGMs1nIq67HB7g9%2F-Lq83ULJWCEMsmjQSq_e%2Fkekulize.png?generation=1569962720189496\&alt=media)

<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)
```

![](https://437555650-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lq83PytCJFhVoqGvu-1%2F-Lq83RGMs1nIq67HB7g9%2F-Lq83ULLN9IjVMAfMdWe%2Fmatch2d.png?generation=1569962720060841\&alt=media)![](https://437555650-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lq83PytCJFhVoqGvu-1%2F-Lq83RGMs1nIq67HB7g9%2F-Lq83ULNcvKFQ9qW3Om7%2Fmatch2d2.png?generation=1569962720042894\&alt=media)

## 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]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huang-jason.gitbook.io/complat-software-training-101/rdkit-process.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
