<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Chemical similarity using PubChem fingerprints
Step1:  First we'll get some compounds. Here we just use PubChem CIDs to retrieve, but you could search (e.g. using name, SMILES, SDF, etc.).
Step2:  The similarity between two molecules is typically calculated using molecular fingerprints that encode structural information about the molecule as a series of bits (0 or 1). These bits represent the presence or absence of particular patterns or substructures — two molecules that contain more of the same patterns will have more bits in common, indicating that they are more similar.
The PubChem CACTVS fingerprint is available on each compound using the fingerprint method. This is returned as a hex-encoded string
Step3:  We can decode this from hexadecimal and then display as a binary string as follows
Step4:  There is more information about the PubChem fingerprints at ftp
Step5:  Let's try it out
<ASSISTANT_TASK:>
Python Code:
import pubchempy as pcp
from IPython.display import Image
Explanation: Chemical similarity using PubChem fingerprints
End of explanation
coumarin = pcp.Compound.from_cid(323)
Image(url='https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?cid=323&t=l')
coumarin_314 = pcp.Compound.from_cid(72653)
Image(url='https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?cid=72653&t=l')
coumarin_343 = pcp.Compound.from_cid(108770)
Image(url='https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?cid=108770&t=l')
aspirin = pcp.Compound.from_cid(2244)
Image(url='https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?cid=2244&t=l')
Explanation: First we'll get some compounds. Here we just use PubChem CIDs to retrieve, but you could search (e.g. using name, SMILES, SDF, etc.).
End of explanation
coumarin.fingerprint
Explanation: The similarity between two molecules is typically calculated using molecular fingerprints that encode structural information about the molecule as a series of bits (0 or 1). These bits represent the presence or absence of particular patterns or substructures — two molecules that contain more of the same patterns will have more bits in common, indicating that they are more similar.
The PubChem CACTVS fingerprint is available on each compound using the fingerprint method. This is returned as a hex-encoded string:
End of explanation
bin(int(coumarin.fingerprint, 16))
Explanation: We can decode this from hexadecimal and then display as a binary string as follows:
End of explanation
def tanimoto(compound1, compound2):
    fp1 = int(compound1.fingerprint, 16)
    fp2 = int(compound2.fingerprint, 16)
    fp1_count = bin(fp1).count('1')
    fp2_count = bin(fp2).count('1')
    both_count = bin(fp1 & fp2).count('1')
    return float(both_count) / (fp1_count + fp2_count - both_count)
Explanation: There is more information about the PubChem fingerprints at ftp://ftp.ncbi.nlm.nih.gov/pubchem/specifications/pubchem_fingerprints.txt
The most commonly used measure for quantifying the similarity of two fingerprints is the Tanimoto Coefficient, given by:
$$ T = \frac{N_{ab}}{N_{a} + N_{b} - N_{ab}} $$
where $N_{a}$ and $N_{b}$ are the number of 1-bits (i.e corresponding to the presence of a pattern) in the fingerprints of molecule $a$ and molecule $b$ respectively. $N_{ab}$ is the number of 1-bits common to the fingerprints of both molecule $a$ and $b$. The Tanimoto coefficient ranges from 0 when the fingerprints have no bits in common, to 1 when the fingerprints are identical.
Here's a simple way to calculate the Tanimoto coefficient between two compounds in python:
End of explanation
tanimoto(coumarin, coumarin)
tanimoto(coumarin, coumarin_314)
tanimoto(coumarin, coumarin_343)
tanimoto(coumarin_314, coumarin_343)
tanimoto(coumarin, aspirin)
tanimoto(coumarin_343, aspirin)
Explanation: Let's try it out:
End of explanation
<END_TASK>