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 Sep 12, 2007
021 *
022 */
023package org.biojava.nbio.structure.io;
024
025import org.biojava.nbio.structure.*;
026
027import java.util.ArrayList;
028import java.util.List;
029
030/** Converts full atom representations to Calpha only ones.
031 *
032 * @author Andreas Prlic
033 * @version %I% %G%
034 */
035public class CAConverter {
036
037
038        /** Convert a List of chain objects to another List of chains, containing C-alpha atoms only.
039         *
040         * @param chains list of chains
041         * @return a list of chains
042         * @deprecated Use the more generic {@link #getRepresentativeAtomsOnly(List)} instead
043         */
044        @Deprecated
045        public static List<Chain> getCAOnly(List<Chain> chains){
046                List<Chain> newChains = new ArrayList<Chain>();
047
048                for (Chain chain : chains){
049                        Chain newChain = getCAOnly(chain);
050                        newChains.add(newChain);
051
052                }
053
054                return newChains;
055
056
057
058        }
059
060
061        /** Convert a List of chain objects to another List of chains, containing Representative atoms only.
062         *
063         * @param chains list of chains
064         * @return a list of chains
065         * @since Biojava 4.1.0
066         */
067        public static List<Chain> getRepresentativeAtomsOnly(List<Chain> chains){
068                List<Chain> newChains = new ArrayList<Chain>();
069
070                for (Chain chain : chains){
071                        Chain newChain = getRepresentativeAtomsOnly(chain);
072                        newChains.add(newChain);
073                }
074
075                return newChains;
076        }
077
078        /** Convert a Chain to a new Chain containing C-alpha atoms only.
079         *
080         * @param chain to convert
081         * @return a new chain containing Amino acids with C-alpha only.
082         * @deprecated Use the more generic {@link #getRepresentativeAtomsOnly(Chain)} instead
083         */
084        @Deprecated
085        public static Chain getCAOnly(Chain chain){
086
087                Chain newChain = new ChainImpl();
088                newChain.setChainID(chain.getChainID());
089                newChain.setCompound(chain.getCompound());
090                newChain.setSwissprotId(chain.getSwissprotId());
091
092                List<Group> groups = chain.getAtomGroups();
093
094                grouploop:
095                        for (Group g: groups){
096                                List<Atom> atoms = g.getAtoms();
097
098                                if ( ! (g instanceof AminoAcid))
099                                        continue;
100
101                                for (Atom a : atoms){
102
103                                        if ( a.getName().equals(StructureTools.CA_ATOM_NAME) && a.getElement()==Element.C){
104                                                // we got a CA atom in this group!
105                                                AminoAcid n = new AminoAcidImpl();
106                                                n.setPDBName(g.getPDBName());
107                                                n.setResidueNumber(g.getResidueNumber());
108                                                n.addAtom(a);
109                                                newChain.addGroup(n);
110                                                continue grouploop;
111
112                                        }
113                                }
114
115                        }
116                return newChain;
117        }
118
119
120        /** Convert a Chain to a new Chain containing C-alpha atoms only.
121         *
122         * @param chain to convert
123         * @return a new chain containing Amino acids with C-alpha only.
124         * @since Biojava 4.1.0
125         */
126        public static Chain getRepresentativeAtomsOnly(Chain chain){
127
128                Chain newChain = new ChainImpl();
129                newChain.setChainID(chain.getChainID());
130                newChain.setCompound(chain.getCompound());
131                newChain.setSwissprotId(chain.getSwissprotId());
132
133                List<Group> groups = chain.getAtomGroups();
134
135                grouploop:
136                        for (Group g: groups){
137                                List<Atom> atoms = g.getAtoms();
138
139                                if ( ! (g instanceof AminoAcid))
140                                        continue;
141
142                                for (Atom a : atoms){
143
144                                        if ( a.getName().equals(StructureTools.CA_ATOM_NAME) && a.getElement()==Element.C){
145                                                // we got a CA atom in this group!
146                                                AminoAcid n = new AminoAcidImpl();
147                                                n.setPDBName(g.getPDBName());
148                                                n.setResidueNumber(g.getResidueNumber());
149                                                n.addAtom(a);
150                                                newChain.addGroup(n);
151                                                continue grouploop;
152
153                                        }
154                                }
155
156                        }
157                return newChain;
158        }
159}