
org.bitbucket.kienerj.indigoutils.ChargeNeutralizer Maven / Gradle / Ivy
Show all versions of indigo-utils Show documentation
/*
* Copyright (C) 2013 Joos Kiener
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.bitbucket.kienerj.indigoutils;
import com.ggasoftware.indigo.IndigoObject;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
/**
* Neutralizes charged molecules.
*
* A simple single atom ion like Na+ or Cl- are having their charge set to 0
* and a neutral atom Na or Cl will be returned.
For more complex molecules
* the charged atoms charge will be set to 0 and the valence and hydrogen count
* will be adjusted accordingly by indigo toolkit.
*
*
* @author Joos Kiener
*/
public class ChargeNeutralizer {
private static final XLogger logger = XLoggerFactory.getXLogger("ChargeNeutralizer");
/**
* All molecules that need to be neutralized must have been created by
* the Indigo instance
* indigio
.
*
* @param indigo the indigo instance
*/
public ChargeNeutralizer() {
}
/**
* Neutralizes mol
in a chemical correct manner.
*
* See {@link #neutralizeCharges(IndigoObject, boolean) neutralizeCharges(IndigoObject, boolean)}
* for further info. This method behaves like neutralizeCharges(mol, true)
.
*
* @param mol the molecule to neutralize
* @return
*/
public IndigoObject neutralizeCharges(IndigoObject mol) {
return this.neutralizeCharges(mol, true);
}
/**
* Neutralizes
* mol
in a chemical correct manner.
*
* In case of single atoms like Na+ the charge is set to 0. In case of
* more complex molecules the charge in all charged atoms is set to 0 and
* hydrogen count is adjusted accordingly.
*
* Due to how Indigo Toolkit works, Na+ is neutralized to NaH. However it
* is assumed that the user actually wanted Na as reply. if you set
* stripHydrogen
to true, Na will be returned, else NaH. The
* same applies for any other similar case.
*
* The method does not change mol
. It returns a changed copy.
*
* @param mol the molecule to neutralize
* @return the neutralized molecule if any pattern matched or an unchanged
* copy of mol
*/
public IndigoObject neutralizeCharges(IndigoObject mol, boolean stripHydrogen) {
logger.entry(mol);
logger.debug("Neutralizing charges in {}", mol.canonicalSmiles());
IndigoObject result = mol.clone();
if (mol.countHeavyAtoms() == 1
&& mol.countHydrogens() == 0) { // eg. for all single atom ions like Na+, Cl-,...
result.getAtom(0).setCharge(0);
if (stripHydrogen && result.countImplicitHydrogens() > 0) {
// bug in indigo: Na+ -> NaH instead of Na without below line
result.getAtom(0).setImplicitHCount(0);
}
} else {
for (IndigoObject atom : result.iterateAtoms()) {
if (atom.charge() != 0) {
atom.setCharge(0);
}
}
}
logger.debug("Neutralized molecule {} to {}", mol.canonicalSmiles(), result.canonicalSmiles());
logger.exit(result);
return result;
}
}