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 */
021package org.biojava.nbio.structure;
022
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * An internal utility class for StructureImpl to make it easier to manage poly and nonpoly chains.
032 * Not to exposed to users through API.
033 *
034 * Created by andreas on 5/3/16.
035 * @author Andreas Prlic
036 * @since 5.0
037 */
038public class Model implements Serializable {
039
040        private static final long serialVersionUID = 5320613424668781882L;
041
042        private static final Logger logger = LoggerFactory.getLogger(Model.class);
043
044        private List<Chain> polyChains;
045        private List<Chain> nonPolyChains;
046        private List<Chain> waterChains;
047
048        public Model(){
049                polyChains = new ArrayList<>();
050                nonPolyChains = new ArrayList<>();
051                waterChains = new ArrayList<>();
052        }
053
054        public List<Chain> getPolyChains() {
055                return polyChains;
056        }
057
058        public List<Chain> getNonPolyChains() {
059                return nonPolyChains;
060        }
061
062        public List<Chain> getWaterChains() {
063                return waterChains;
064        }
065
066        /**
067         * Get all chains: polymeric, non-polymeric and water
068         * @return
069         */
070        public List<Chain> getChains(){
071                ArrayList<Chain> chains = new ArrayList<>();
072
073                chains.addAll(polyChains);
074                chains.addAll(nonPolyChains);
075                chains.addAll(waterChains);
076
077                chains.trimToSize();
078
079                return chains;
080        }
081
082        public void setChains(List<Chain> modelChains) {
083
084                polyChains.clear();
085                nonPolyChains.clear();
086                waterChains.clear();
087
088                for (Chain c : modelChains){
089                        addChain(c);
090                }
091        }
092
093        public void addChain(Chain c) {
094                EntityInfo info = c.getEntityInfo();
095
096                if ( info == null || info.getType() == null) {
097                        logger.info("No entity info could be found while adding chain with asym id {} (author id {}). Will consider it a polymer chain.", c.getId(), c.getName());
098                        polyChains.add(c);
099
100                } else if ( info.getType() == EntityType.POLYMER) {
101                        polyChains.add(c);
102
103                } else if (info.getType() == EntityType.NONPOLYMER) {
104                        nonPolyChains.add(c);
105
106                } else if (info.getType() == EntityType.WATER) {
107                        waterChains.add(c);
108
109                } else if (info.getType() == EntityType.MACROLIDE) {
110                        logger.warn("Chain with asym id {} (author id {}) has entity type 'macrolide', considering it non-polymeric", c.getId(), c.getName());
111                        nonPolyChains.add(c);
112
113                } else {
114                        logger.warn("Chain with asym id {} (author id {}) has unsupported entity type '{}'. Will not add it to the Structure.", c.getId(), c.getName(), info.getType().toString());
115                        // ignore it
116
117                }
118        }
119
120        /**
121         * Returns the total number of chains in this model: polymeric, non-polymeric and water
122         * @return
123         */
124        public int size() {
125                return polyChains.size() + nonPolyChains.size() + waterChains.size();
126        }
127
128        @Override
129        public String toString() {
130                return "["+polyChains.size()+" poly chains, "+nonPolyChains.size()+" non-poly chains, "+waterChains.size()+" water chains]";
131        }
132}