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 May 27, 2010
021 * Author: Jianjiong Gao
022 *
023 */
024
025package org.biojava.nbio.protmod;
026
027import java.util.Collection;
028import java.util.LinkedHashSet;
029import java.util.Set;
030
031/**
032 * This class contains information about a specific protein
033 * modification.
034 *
035 * @author Jianjiong Gao
036 * @since 3.0
037 */
038public class ProteinModificationImpl
039implements ProteinModification , Comparable<ProteinModification> {
040
041        private final String id;
042        private final ModificationCondition condition;
043        private final ModificationCategory category;
044        private final ModificationOccurrenceType occurrenceType;
045
046        private final String pdbccId;
047        private final String pdbccName;
048        private final String residId;
049        private final String residName;
050        private final String psimodId;
051        private final String psimodName;
052        private final String sysName;
053        private final String formula;
054
055        private final Set<String> keywords;
056
057        @Override
058        public String getId() {
059                return id;
060        }
061
062        @Override
063        public String getPdbccId() {
064                return pdbccId;
065        }
066
067        @Override
068        public String getPdbccName() {
069                return pdbccName;
070        }
071
072        @Override
073        public String getResidId() {
074                return residId;
075        }
076
077        @Override
078        public String getResidName() {
079                return residName;
080        }
081
082        @Override
083        public String getPsimodId() {
084                return psimodId;
085        }
086
087        @Override
088        public String getPsimodName() {
089                return psimodName;
090        }
091
092        @Override
093        public String getSystematicName() {
094                return sysName;
095        }
096
097        @Override
098        public String getDescription() {
099                return toString();
100                //return description;
101        }
102
103        @Override
104        public Set<String> getKeywords() {
105                return keywords;
106        }
107
108        @Override
109        public ModificationCondition getCondition() {
110                return condition;
111        }
112
113        @Override
114        public String getFormula() {
115                return formula;
116        }
117
118        @Override
119        public ModificationCategory getCategory() {
120                return category;
121        }
122
123        @Override
124        public ModificationOccurrenceType getOccurrenceType() {
125                return occurrenceType;
126        }
127
128        @Override
129        public String toString() {
130                return "ProteinModificationImpl [id=" + id + ", condition=" + condition
131                                + ", category=" + category + ", occurrenceType="
132                                + occurrenceType + ", pdbccId=" + pdbccId + ", pdbccName="
133                                + pdbccName + ", residId=" + residId + ", residName="
134                                + residName + ", psimodId=" + psimodId + ", psimodName="
135                                + psimodName + ", sysName=" + sysName + ", formula=" + formula
136                                + ", keywords=" + keywords + "]";
137        }
138
139        /*
140        private String printModification(ProteinModificationImpl mod) {
141                StringBuilder sb = new StringBuilder();
142
143                String name = getBestPossibleName(mod);
144                boolean hasName = true;
145                if (  name.equals(""))
146                        hasName = false;
147                sb.append(name);
148
149                Set<String> keywords = mod.getKeywords();
150                if (keywords!=null && !keywords.isEmpty()) {
151                        if ( hasName)
152                                sb.append(" (");
153                        for (String keyword : keywords) {
154
155                                sb.append(keyword);
156                                sb.append(", ");
157                        }
158                        sb.delete(sb.length()-2,sb.length());
159                }
160                if ( hasName)
161                        sb.append(")");
162                return sb.toString();
163        }
164
165
166        private String getBestPossibleName(ProteinModificationImpl mod) {
167
168                //System.out.println(mod.getResidName() + " : " + mod.getPsimodName() + " : " + mod.getPdbccName() + " : " + mod.getSystematicName());
169
170                // first: get resid
171                String resid = mod.getResidId();
172                if (resid != null) {
173                        String residname = mod.getResidName();
174                        if (residname != null) {
175                                return residname;
176                        }
177                }
178
179                // 2nd: PSI-MOD
180
181                String name = mod.getPsimodName();
182                if ( name != null) {
183                        //System.out.println("PSI_MOD name:" + name);
184                        return name;
185                }
186
187                // 3rd PDB-CC
188
189                String pdbcc = mod.getPdbccName();
190                if ( pdbcc != null ) {
191                        //System.out.println("PDBCC name: " + pdbcc);
192                        return pdbcc;
193                }
194
195
196                // no public name know, use the systematic name
197
198                String systematic = mod.getSystematicName();
199                if ( systematic != null) {
200                        //System.out.println("SYSTEMATIC NAME: " + mod.getSystematicName());
201                        return systematic;
202                }
203
204
205                return "";
206
207        }
208        */
209
210        @Override
211        public int hashCode() {
212                int ret = id.hashCode();
213                ret = ret * 31 + category.hashCode();
214                return ret;
215        }
216
217        @Override
218        public boolean equals(Object obj) {
219                if (!(obj instanceof ProteinModification))
220                        return false;
221
222                ProteinModification mod = (ProteinModification)obj;
223                if (!id.equals(mod.getId()))
224                        return false;
225
226                if (category != mod.getCategory())
227                        return false;
228
229                return true;
230        }
231
232
233
234
235        /**
236         * Uses Builder pattern to build a ProteinModification.
237         */
238        public static class Builder {
239                private final String id;
240
241                private ModificationCondition condition;
242                private ModificationCategory category;
243                private ModificationOccurrenceType occurrenceType;
244
245                private String pdbccId = null;
246                private String pdbccName = null;
247                private String residId = null;
248                private String residName = null;
249                private String psimodId = null;
250                private String psimodName = null;
251                private String sysName = null;
252                private String formula = null;
253
254                private Set<String> keywords = new LinkedHashSet<String>();
255
256                /**
257                 *
258                 * @param id
259                 * @param cat
260                 * @param occType
261                 * @param condition
262                 */
263                public Builder(final String id, final ModificationCategory cat,
264                                final ModificationOccurrenceType occType,
265                                final ModificationCondition condition) {
266                        if ( id == null) throw new IllegalArgumentException("id == null!");
267                        if ( cat == null) throw new IllegalArgumentException("cat == null!");
268                        if ( occType == null) throw new IllegalArgumentException("occType == null!");
269                        if ( condition == null) throw new IllegalArgumentException("condition == null!");
270
271                        this.id = id;
272                        this.category = cat;
273                        this.occurrenceType = occType;
274                        this.condition = condition;
275                }
276
277                /**
278                 * Create a Builder from an existing ProteinModification.
279                 * @param copyFrom the ProteinModification to be copied from.
280                 */
281                public Builder(final ProteinModification copyFrom) {
282                        this(copyFrom.getId(), copyFrom.getCategory(), copyFrom.getOccurrenceType(), copyFrom.getCondition());
283                        this.pdbccId = copyFrom.getPdbccId();
284                        this.pdbccName = copyFrom.getPdbccName();
285                        this.residId = copyFrom.getResidId();
286                        this.residName = copyFrom.getResidName();
287                        this.psimodId = copyFrom.getPsimodId();
288                        this.psimodName = copyFrom.getPsimodName();
289                        this.sysName = copyFrom.getSystematicName();
290                        this.formula = copyFrom.getFormula();
291
292                        this.keywords = new LinkedHashSet<String>(copyFrom.getKeywords());
293                }
294
295                public Builder setCategory(final ModificationCategory cat) {
296                        if (cat == null) throw new IllegalArgumentException("cat == null!");
297                        this.category = cat;
298                        return this;
299                }
300
301                public Builder setOccurrenceType(final ModificationOccurrenceType occType) {
302                        if (occType == null) throw new IllegalArgumentException("occType == null!");
303                        this.occurrenceType =occType;
304                        return this;
305                }
306
307                public Builder setCondition(final ModificationCondition condition) {
308                        if (condition == null) throw new IllegalArgumentException("condition == null!");
309                        this.condition = condition;
310                        return this;
311                }
312
313                /**
314                 * Set the Protein Data Bank Chemical Component ID.
315                 * @param pdbccId Protein Data Bank Chemical Component ID.
316                 * @return the same Builder object so you can chain setters.
317                 */
318                public Builder setPdbccId(final String pdbccId) {
319                        this.pdbccId = pdbccId;
320                        return this;
321                }
322
323                /**
324                 * Set the Protein Data Bank Chemical Component name.
325                 * @param pdbccName Protein Data Bank Chemical Component name.
326                 * @return the same Builder object so you can chain setters.
327                 */
328                public Builder setPdbccName(final String pdbccName) {
329                        this.pdbccName = pdbccName;
330                        return this;
331                }
332
333                /**
334                 * Set the RESID ID.
335                 * @param residId RESID ID.
336                 * @return the same Builder object so you can chain setters.
337                 */
338                public Builder setResidId(final String residId) {
339                        this.residId = residId;
340                        return this;
341                }
342
343                /**
344                 * Set the RESID name.
345                 * @param residName RESID name.
346                 * @return the same Builder object so you can chain setters.
347                 */
348                public Builder setResidName(final String residName) {
349                        this.residName = residName;
350                        return this;
351                }
352
353                /**
354                 * Set the PSI-MOD ID.
355                 * @param psimodId PSI-MOD ID.
356                 * @return the same Builder object so you can chain setters.
357                 */
358                public Builder setPsimodId(final String psimodId) {
359                        this.psimodId = psimodId;
360                        return this;
361                }
362
363                /**
364                 * Set the PSI-MOD name.
365                 * @param psimodName PSI-MOD name.
366                 * @return the same Builder object so you can chain setters.
367                 */
368                public Builder setPsimodName(final String psimodName) {
369                        this.psimodName = psimodName;
370                        return this;
371                }
372
373                /**
374                 * Set the systematic name.
375                 * @param sysName systematic name.
376                 * @return the same Builder object so you can chain setters.
377                 */
378                public Builder setSystematicName(final String sysName) {
379                        this.sysName = sysName;
380                        return this;
381                }
382
383                /**
384                 *
385                 * @param description description of the modification.
386                 * @return the same Builder object so you can chain setters.
387                 */
388                public Builder setDescription(final String description) {
389                        // description is created on the fly in getDescription
390                        return this;
391                }
392
393                /**
394                 * Add a keyword associate with the PTM.
395                 * @param keyword a keyword.
396                 * @return the same Builder object so you can chain setters.
397                 * @throws IllegalArgumentException if the keyword is null.
398                 */
399                public Builder addKeyword(String keyword) {
400                        if (keyword == null) throw new IllegalArgumentException("Keyword cannot be null.");
401                        keywords.add(keyword);
402                        return this;
403                }
404
405                public Builder addKeywords(Collection<String> keywords) {
406                        if (keywords==null)     throw new IllegalArgumentException("Keywords cannot be null.");
407
408                        for (String keyword : keywords) {
409                                addKeyword(keyword);
410                        }
411
412                        return this;
413                }
414
415                /**
416                 * Set the residue formula.
417                 * @param formula residue formula.
418                 * @return the same Builder object so you can chain setters.
419                 */
420                public Builder setFormula(final String formula) {
421                        this.formula = formula;
422                        return this;
423                }
424
425                /**
426                 *
427                 * @return build ProteinModification.
428                 */
429                public ProteinModificationImpl build() {
430                        return new ProteinModificationImpl(this);
431                }
432        }
433
434        /**
435         *
436         */
437        private ProteinModificationImpl(Builder builder) {
438                this.id = builder.id;
439                this.category = builder.category;
440                this.occurrenceType = builder.occurrenceType;
441                this.condition = builder.condition;
442                this.pdbccId = builder.pdbccId;
443                this.pdbccName = builder.pdbccName;
444                this.residId = builder.residId;
445                this.residName = builder.residName;
446                this.psimodId = builder.psimodId;
447                this.psimodName = builder.psimodName;
448                this.sysName = builder.sysName;
449                this.formula = builder.formula;
450
451                this.keywords = new LinkedHashSet<String>(builder.keywords);
452        }
453
454        @Override
455        public int compareTo(ProteinModification arg0) {
456                if ( this.equals(arg0))
457                        return 0;
458
459                return this.toString().compareTo(arg0.toString());
460        }
461}