This is how I believe this can be achieved (minus the rdkit thing – that was just something I wanted to use some time ago)
rdkit