# RDKit - read/write

## Read a molecule

```python
from rdkit import Chem

m = Chem.MolFromSmiles('Cc1ccccc1')
m = Chem.MolFromMolFile('data/input.mol')
mol_str=open('data/input.mol','r').read()
m = Chem.MolFromMolBlock(mol_str)
```

Read molecule will return `<rdkit.Chem.rdchem.Mol object at 0x...>` or `None`

## Read molecules

```python
mols = Chem.SDMolSupplier('data/5ht3ligs.sdf')

f = open('data/5ht3ligs.sdf','rb')
mols = Chem.ForwardSDMolSupplier(f)


for mol in mols:
    print(mol.GetNumAtoms())
```

## Write molecule

```python
m = Chem.MolFromMolFile('data/chiral.mol')
Chem.MolToSmiles(m) # 'C[C@H](O)c1ccccc1'
Chem.MolToSmiles(m,isomericSmiles=False) # 'CC(O)c1ccccc1'

Chem.MolToMolBlock(m2)
```

* generic SMILES = no chiral or isotopic info
* isomeric SMILES = with chiral & isotopic info
* unique SMILES = canonicalized generic SMILES
* absolute SMILES = unique isomeric SMILES

***Output SMILES is canonical.***

## Molfile with coord

```python
# 2D coordinates
from rdkit.Chem import AllChem

AllChem.Compute2DCoords(m)

#  4  4  0  0  0  0  0  0  0  0999 V2000
#    1.0607   -0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
#   -0.0000   -1.0607    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
#   -1.0607    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
#    0.0000    1.0607    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
```

```python
# 3D coordinates
m = Chem.AddHs(m) # To get good 3D conformations, it’s almost always a good idea to add hydrogens to the molecule first
AllChem.EmbedMolecule(m)
m = Chem.RemoveHs(m)

#  4  4  0  0  0  0  0  0  0  0999 V2000
#    0.3497    0.9755   -0.2202 C   0  0  0  0  0  0  0  0  0  0  0  0
#    0.9814   -0.3380    0.2534 C   0  0  0  0  0  0  0  0  0  0  0  0
#   -0.3384   -1.0009   -0.1474 C   0  0  0  0  0  0  0  0  0  0  0  0
#   -0.9992    0.3532    0.1458 C   0  0  0  0  0  0  0  0  0  0  0  0
```

## Chem vs AllChem

speed startup and lower import times

`rdkit.Chem` = basic func (reading/writing, substructure searching, cleanup, etc)

`rdkit.Chem.AllChem` = advanced


---

# 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.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.
