<jupyter_start><jupyter_text>**`neutralise`**: remove charges by adding/removing protons  IntroductionThis document describes the operation of the **`neutralize`** module, which attempts to balance charges _via_ the removal and/or addition of protons.Recall that bonds to Group I & II metals are broken before neutralization is attempted, and that a further round of neutralization is carried out after the application of the standardization rules.Note that charge-neutralization rules _could_ provide an alternative to the proton-addition/removal strategy employed by the **`neutralize`** module; however, the latter is used here as it allows finer control, which is particularly useful when more complex systems such as zwitterions are encountered.  Examples<jupyter_code>mol = Chem.MolFromSmiles("C(C(=O)[O-])(Cc1n[n-]nn1)(C[NH3+])(C[N+](=O)[O-])")
mol
neutralise.apply(mol)<jupyter_output>[2016/Mar/24 16:01:26 DEBUG   ] 1 positive/H, 0 positive/quat and 2 negative (of which 2 are acid) charges identified
[2016/Mar/24 16:01:26 DEBUG   ] Overall H balance: +1; formal charge: 0<jupyter_text>ZwitterionsZwitterions can be problematic: cases where inappropriate protonation has been performed are not uncommon in chemical database.Here, protonation of negative charges is *not* attempted unless there is a 'surplus' of negative charges above the (non-neutralizable) 'quat' positive charges)...<jupyter_code>mol1 = Chem.MolFromSmiles("n(C)1cc[n+]2cccc([O-])c12")

mol2 = neutralise.apply(mol1)

Draw.MolsToGridImage([mol1, mol2])<jupyter_output>[2016/Mar/24 16:01:26 DEBUG   ] 0 positive/H, 1 positive/quat and 1 negative (of which 0 are acid) charges identified
[2016/Mar/24 16:01:26 DEBUG   ] Overall H balance: 0; formal charge: 0<jupyter_text>If there *are* 'surplus' negative charges, neutralization is attempted, but only using recognized 'acidic' groups...<jupyter_code>mol1 = Chem.MolFromSmiles("n(CC(=O)[O-])1cc[n+]2cccc([O-])c12")
                          
mol2 = neutralise.apply(mol1)

Draw.MolsToGridImage([mol1, mol2])<jupyter_output>[2016/Mar/24 16:01:26 DEBUG   ] 0 positive/H, 1 positive/quat and 2 negative (of which 1 are acid) charges identified
[2016/Mar/24 16:01:26 WARNING ] zwitterion with more negative charges than quaternary positive centres detected
[2016/Mar/24 16:01:26 DEBUG   ] Overall H balance: +1; formal charge: 0<jupyter_text>The idea is to avoid protonating negative charges inappropriately: in the example above, it would be clearly wrong to protonate the phenolic O instead of the carboxylate O.However, this means that nothing is attempted if no such 'obvious' acidic groups are present, even in cases where inspection makes it clear what _ought_ to be done ...<jupyter_code>mol1 = Chem.MolFromSmiles("n(CC[O-])1cc[n+]2cccc([O-])c12")
                          
mol2 = neutralise.apply(mol1)

Draw.MolsToGridImage([mol1, mol2])<jupyter_output>[2016/Mar/24 16:01:26 DEBUG   ] 0 positive/H, 1 positive/quat and 2 negative (of which 0 are acid) charges identified
[2016/Mar/24 16:01:26 DEBUG   ] Overall H balance: 0; formal charge: -1<jupyter_text>Here, the sensible thing to do would be to protonate the alkoxy group and leave the phenoxy group alone.Whilst it would be possible to code up rules for deciding which atom to protonate in the above case, it has not yet been attempted; whilst cases such as that above do occur, in practice they are fairly rare.  Futher neutralizationsAs mentioned above, over-zealous neutralizations sometimes mean acidic groups in zwitterions are protonated, leaving a 'parent' molecule with an overall positive charge.An experimental option to treat simple cases where this has happened has been introduced...<jupyter_code>mol1 = Chem.MolFromSmiles("n(CC(=O)O)1cc[n+]2ccccc12")
                          
mol2 = neutralise.apply(mol1, balance_quat_surplus=True)

Draw.MolsToGridImage([mol1, mol2])<jupyter_output>[2016/Mar/24 16:01:26 DEBUG   ] 0 positive/H, 1 positive/quat and 0 negative (of which 0 are acid) charges identified
[2016/Mar/24 16:01:26 WARNING ] Surplus of quat positive charges but with uncharged acids detected
[2016/Mar/24 16:01:26 DEBUG   ] Overall H balance: -1; formal charge: 0<jupyter_text>Note that this will only deprotonate obvious acid groups. If, say, a phenolic anion in a zwitterion has been inappropriately protonated, no attempt is made to fix this at present...<jupyter_code>mol1 = Chem.MolFromSmiles("n(C)1cc[n+]2cccc(O)c12")
                          
mol2 = neutralise.apply(mol1)

Draw.MolsToGridImage([mol1, mol2])<jupyter_output>[2016/Mar/24 16:01:26 DEBUG   ] 0 positive/H, 1 positive/quat and 0 negative (of which 0 are acid) charges identified
[2016/Mar/24 16:01:26 DEBUG   ] Overall H balance: 0; formal charge: 1
{'fraction_non_alphanumeric': 0.09294465568229827, 'fraction_numerical': 0.053231939163498096, 'mean_word_length': 4.9113607990012484, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '658896', 'license': 'Apache-2.0', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1432, 'n_words': 536, 'path': 'standardiser2/docs/02_neutralise.ipynb', 'repo_name': 'EMVGaron/standardiser_2', 'size': '4734'}