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.secstruc;
022
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Map;
026import java.util.TreeMap;
027
028import org.biojava.nbio.structure.Group;
029import org.biojava.nbio.structure.GroupIterator;
030import org.biojava.nbio.structure.ResidueNumber;
031import org.biojava.nbio.structure.Structure;
032
033/**
034 * This class contains methods for obtaining and converting secondary structure
035 * information from BioJava {@link Structure}s.
036 *
037 * @author Aleix Lafita
038 * @since 4.1.1
039 *
040 */
041public class SecStrucTools {
042
043        /**
044         * Obtain the List of secondary structure information (SecStrucInfo) of a
045         * Structure.
046         *
047         * @param s
048         *            Structure with SS assignments
049         * @return List of SecStrucInfo objects
050         */
051        public static List<SecStrucInfo> getSecStrucInfo(Structure s) {
052
053                List<SecStrucInfo> listSSI = new ArrayList<>();
054                GroupIterator iter = new GroupIterator(s);
055
056                while (iter.hasNext()) {
057                        Group g = iter.next();
058                        if (g.hasAminoAtoms()) {
059                                Object p = g.getProperty(Group.SEC_STRUC);
060                                if (!(p == null)) {
061                                        SecStrucInfo ss = (SecStrucInfo) p;
062                                        listSSI.add(ss);
063                                }
064                        }
065                }
066
067                return listSSI;
068        }
069
070
071        public static void assignSecStruc( Structure s,List<SecStrucInfo> listSSI){
072
073                for ( SecStrucInfo ssi : listSSI){
074
075                        ssi.getGroup().setProperty(Group.SEC_STRUC,ssi);
076
077                }
078
079        }
080
081        /**
082         * Obtain the List of secondary structure elements (SecStrucElement) of a
083         * Structure.
084         *
085         * @param s
086         *            Structure with SS assignments
087         * @return List of SecStrucElement objects
088         */
089        public static List<SecStrucElement> getSecStrucElements(Structure s) {
090
091                List<SecStrucElement> listSSE = new ArrayList<>();
092                GroupIterator iter = new GroupIterator(s);
093
094                // SecStruc information - initialize
095                SecStrucType type = SecStrucType.coil;
096                ResidueNumber previous = new ResidueNumber();
097                ResidueNumber start = new ResidueNumber();
098                String chainId = "";
099                int count = 0; // counts the number of residues in SSE
100
101                // Create a map for the IDs of the SSE in the structure
102                Map<SecStrucType, Integer> ids = new TreeMap<SecStrucType, Integer>();
103                for (SecStrucType t : SecStrucType.values())
104                        ids.put(t, 1);
105
106                while (iter.hasNext()) {
107                        Group g = iter.next();
108
109                        if (g.hasAminoAtoms()) {
110                                Object p = g.getProperty(Group.SEC_STRUC);
111                                if (p == null)
112                                        continue;
113                                SecStrucInfo ss = (SecStrucInfo) p;
114
115                                if (count > 0) {
116                                        // If chain and type are equal increment counter
117                                        if (ss.type == type && chainId.equals(g.getChainId())) {
118                                                previous = g.getResidueNumber();
119                                                count++;
120                                                continue;
121                                        } else {
122                                                // Save the current SSE if chain or type change
123                                                SecStrucElement sse = new SecStrucElement(type, start,
124                                                                previous, count, ids.get(type), chainId);
125                                                listSSE.add(sse);
126                                                ids.put(type, ids.get(type) + 1);
127                                                count = 0;
128
129                                                // Initialize a new SSE one
130                                                if (ss.type != SecStrucType.coil) {
131                                                        type = ss.type;
132                                                        start = g.getResidueNumber();
133                                                        previous = start;
134                                                        chainId = g.getChainId();
135                                                        count = 1;
136                                                }
137                                        }
138                                } else {
139                                        // This is for the first residue only
140                                        if (ss.type != SecStrucType.coil) {
141                                                type = ss.type;
142                                                start = g.getResidueNumber();
143                                                previous = start;
144                                                chainId = g.getChainId();
145                                                count = 1;
146                                        }
147                                }
148                        }
149                }
150                return listSSE;
151        }
152
153        /**
154         * Obtain the List of secondary structure elements (SecStrucElement) of a
155         * List of Groups (assumed to be sequential, this is, connected in the
156         * original Structure).
157         *
158         * @param groups
159         *            Structure with SS assignments
160         * @return List of SecStrucElement objects
161         */
162        public static List<SecStrucElement> getSecStrucElements(List<Group> groups) {
163
164                List<SecStrucElement> listSSE = new ArrayList<>();
165
166                // SecStruc information - initialize
167                SecStrucType type = SecStrucType.coil;
168                ResidueNumber previous = new ResidueNumber();
169                ResidueNumber start = new ResidueNumber();
170                String chainId = "";
171                int count = 0; // counts the number of residues in SSE
172
173                // Create a map for the IDs of the SSE in the structure
174                Map<SecStrucType, Integer> ids = new TreeMap<>();
175                for (SecStrucType t : SecStrucType.values())
176                        ids.put(t, 1);
177
178                for (Group g : groups) {
179
180                        if (g.hasAminoAtoms()) {
181                                Object p = g.getProperty(Group.SEC_STRUC);
182                                if (p == null)
183                                        continue;
184                                SecStrucInfo ss = (SecStrucInfo) p;
185
186                                if (count > 0) {
187                                        // If chain and type are equal increment counter
188                                        if (ss.type == type && chainId.equals(g.getChainId())) {
189                                                previous = g.getResidueNumber();
190                                                count++;
191                                                continue;
192                                        } else {
193                                                // Save the current SSE if chain or type change
194                                                SecStrucElement sse = new SecStrucElement(type, start,
195                                                                previous, count, ids.get(type), chainId);
196                                                listSSE.add(sse);
197                                                ids.put(type, ids.get(type) + 1);
198                                                count = 0;
199
200                                                // Initialize a new SSE one
201                                                if (ss.type != SecStrucType.coil) {
202                                                        type = ss.type;
203                                                        start = g.getResidueNumber();
204                                                        previous = start;
205                                                        chainId = g.getChainId();
206                                                        count = 1;
207                                                }
208                                        }
209                                } else {
210                                        // This is for the first residue only
211                                        if (ss.type != SecStrucType.coil) {
212                                                type = ss.type;
213                                                start = g.getResidueNumber();
214                                                previous = start;
215                                                chainId = g.getChainId();
216                                                count = 1;
217                                        }
218                                }
219                        }
220                }
221                return listSSE;
222        }
223}