001/*
002 *                  BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Jan. 22, 2016
021 *
022 */
023package org.biojava.nbio.structure.io;
024
025import java.util.List;
026
027import org.biojava.nbio.structure.Atom;
028import org.biojava.nbio.structure.Chain;
029import org.biojava.nbio.structure.Group;
030import org.biojava.nbio.structure.Structure;
031import org.biojava.nbio.structure.chem.ChemComp;
032import org.biojava.nbio.structure.chem.ChemCompAtom;
033import org.biojava.nbio.structure.chem.ChemCompGroupFactory;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * A class to add appropriate charge information to a structure.
039 * @author Anthony Bradley
040 */
041public class ChargeAdder {
042        private static final Logger logger = LoggerFactory.getLogger(ChargeAdder.class);
043
044        /**
045         * Function to add the charges to a given structure.
046         */
047        public static void addCharges(Structure structure) {
048                // Loop through the models
049                for (int i = 0; i < structure.nrModels(); i++) {
050                        for (Chain c : structure.getChains(i)) {
051                                for (Group g : c.getAtomGroups()) {
052                                        ChemComp thisChemComp = ChemCompGroupFactory.getChemComp(g.getPDBName());
053                                        List<ChemCompAtom> chemAtoms = thisChemComp.getAtoms();
054                                        for (ChemCompAtom chemCompAtom : chemAtoms) {
055                                                Atom atom = g.getAtom(chemCompAtom.getAtomId());
056                                                short shortCharge = (short) chemCompAtom.getCharge();
057                                                if (atom != null) {
058                                                        atom.setCharge(shortCharge);
059                                                }
060                                                // Now do the same for alt locs
061                                                for (Group altLoc : g.getAltLocs()) {
062                                                        Atom altAtom = altLoc.getAtom(chemCompAtom.getAtomId());
063                                                        if (altAtom != null) {
064                                                                altAtom.setCharge(shortCharge);
065                                                        }
066                                                }
067                                        }
068                                }
069                        }
070                }
071        }
072}