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.io.mmcif.ChemCompGroupFactory;
032import org.biojava.nbio.structure.io.mmcif.model.ChemComp;
033import org.biojava.nbio.structure.io.mmcif.model.ChemCompAtom;
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 *
041 */
042public class ChargeAdder {
043
044        private static final Logger logger = LoggerFactory.getLogger(ChargeAdder.class);
045
046        /**
047         * Function to add the charges to a given structure.
048         */
049        public static void addCharges(Structure structure) {
050                // Loop through the models
051                for(int i=0; i<structure.nrModels(); i++){
052                        for(Chain c: structure.getChains(i)){
053                                for(Group g: c.getAtomGroups()){
054                                        ChemComp thisChemComp = ChemCompGroupFactory.getChemComp(g.getPDBName());
055                                        List<ChemCompAtom> chemAtoms = thisChemComp.getAtoms();
056                                        for(ChemCompAtom chemCompAtom : chemAtoms) {
057                                                Atom atom = g.getAtom(chemCompAtom.getAtom_id());
058                                                String stringCharge = chemCompAtom.getCharge();
059                                                short shortCharge = 0;
060                                                if (stringCharge!=null){
061                                                        if(!stringCharge.equals("?")){
062                                                                try{
063                                                                        shortCharge = Short.parseShort(stringCharge);
064                                                                }
065                                                                catch(NumberFormatException e){
066                                                                        logger.warn("Number format exception. Parsing '"+stringCharge+"' to short");
067                                                                }
068                                                        }
069                                                        else{
070                                                                logger.warn("? charge on atom "+chemCompAtom.getAtom_id()+" in group "+thisChemComp.getId());
071                                                        }
072                                                }
073                                                else{
074                                                        logger.warn("Null charge on atom "+chemCompAtom.getAtom_id()+" in group "+thisChemComp.getId());
075                                                }
076                                                if(atom!=null){
077                                                        atom.setCharge(shortCharge);
078                                                }
079                                                // Now do the same for alt locs
080                                                for (Group altLoc : g.getAltLocs()) {
081                                                        Atom altAtom = altLoc.getAtom(chemCompAtom.getAtom_id());
082                                                        if(altAtom!=null){
083                                                                altAtom.setCharge(shortCharge);
084                                                        }
085                                                }
086                                        }
087                                }
088
089                        }
090                }
091        }
092}