The 3.0 release of Arthor brings many new features with it including an overhauled Python interface.
This new interface has been written using Cython bindings to the C++ core of Arthor,
with the goals of native speed for performing chemical searches
as well as effortless interoperability with the existing Python data science ecosystem, i.e. numpy
and pandas
.
Loading and searching databases¶
Databases can be opened using either the arthor.SubDb
or arthor.SimDb
classes for substructure and similarity searches respectively.
Once opened, these classes have a .search()
method which takes a SMILES or SMARTS string respectively and returns a ResultSet
object.
Here we load a similarity database (pubchem.smi.atfp
) indexing the PubChem database,
and also provide the corresponding smi file so we can later cross reference against it.
import arthor
simdb = arthor.SimDb('/nvme/arthor/pubchem.smi.atfp',
smifile='/nvme/arthor/pubchem.smi')
simdb.set_num_processors(16)
print(simdb.get_num_records())
We can then search for the top 100 most similar hits for a particular compound. Here we’re searching for a candidate molecule suggested in the recent Open Source Malaria Series 4 meeting.
The returned arthor.ResultSet
object acts as a cursor over the hits found.
%%time
rs = simdb.search('C24=NN=C(C1=CC=C(OC(C)(C)C)C=C1)N2C(OCC(C3=CC(F)=C(F)C=C3)O)=CN=C4', limit=100)
Exporting to pandas¶
This ResultSet
object can be iterated and sliced etc, but a more interesting option is that it has both a .read(nbytes)
and .readline()
method, allowing it to behave like an open file handle onto the results.
A use of this is to pass the arthor.ResultSet
object directly into pandas.read_csv
,
allowing the results of the search to be efficiently slurped into a pandas.DataFrame
:
import pandas as pd
df = pd.read_csv(rs, sep='\s+', names=['smiles', 'id', 'similarity'])
print(df.head())
Creating on the fly databases¶
Another new feature with the 3.0 release of Arthor is the ability to create substructure or similarity databases in memory.
This is exposed in Python via .from_smiles
classmethods which take an iterable of smiles (i.e. list, array or pandas series) to create a searchable chemical Database.
Here we create a substructure database of our previous results, and search for hits which feature the double fluoro substituted phenyl ring.
The result set is then directly converted (with the to_array()
method) into a numpy.array
allowing it to index the original dataframe directly and pull out the rows which feature this substructure.
%%time
subdb = arthor.SubDb.from_smiles(df['smiles'])
filtered = df.iloc[subdb.search('c1(F)c(F)cccc1').to_array()]
print(len(filtered))
To quickly visualise this, we can drop the results into rdkit
:
from rdkit import Chem
Chem.Draw.MolsToGridImage(
filtered.iloc[:6]['smiles'].map(Chem.MolFromSmiles),
legends=list(filtered.iloc[:6]['similarity'].map(lambda x: str(x)[:4]))
)
Scalability of in-memory databases¶
The example databases in this notebook are toy examples to give an idea of the possibilities with the new API.
For those who are curious, these are the times for creating (currently this method is only single threaded) similarity and substructure databases of the entire PubChem datbase (currently 102 million molecules) within a notebook:
%%time
pubchem = pd.read_csv('/nvme/arthor/pubchem.smi', sep='\t', names=['smiles', 'id'])
%%time
pb_simdb = arthor.SimDb.from_smiles(pubchem['smiles'])
%%time
pb_subdb = arthor.SubDb.from_smiles(pubchem['smiles'])
Bioactivity databases¶
For more reasonably sized databases, for example Chembl 25, the times to create a database are much more reasonable for interactive use:
%%time
chembl = pd.read_csv('/nvme/arthor/chembl_25.smi', sep='\t', names=['smiles', 'id'])
print("Chembl 25 with {} rows".format(len(chembl)))
%%time
chembl_simdb = arthor.SimDb.from_smiles(chembl['smiles'])
%%time
chembl_subdb = arthor.SubDb.from_smiles(chembl['smiles'])
Conclusion¶
This isn’t the end of the story by far, but is just a first pass of improvements to the Python API of NextMove’s tools. We’ve got plenty of more ideas for bringing these usability and productivity enhancements to Arthor, and our other products We’d love to hear what you think!