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.align.client;
022
023import java.util.Objects;
024
025import org.biojava.nbio.structure.PdbId;
026import org.biojava.nbio.structure.StructureException;
027
028/**
029 * A pair for structure alignment.
030 * a pair is considered equal to another pair if their two respective tuple poles are equal either in their original or reversed order.
031 * i.e. both <code>new PdbPair("1abc", "2abc").equals(new PdbPair("1abc", "2abc"))</code> and 
032 * <code>new PdbPair("1abc", "2abc").equals(new PdbPair("2abc", "1abc"))</code> are <code>true</code>.
033 * @author Andreas Prlic
034 *
035 */
036public class PdbPair implements Comparable<PdbPair> {
037
038        private StructureName name1;
039        private StructureName name2;
040
041        public PdbPair(String name1, String name2) {
042                this(new StructureName(Objects.requireNonNull(name1)),
043                                new StructureName(Objects.requireNonNull(name1)));
044        }
045
046        public PdbPair(StructureName name1, StructureName name2) {
047                this.name1 = Objects.requireNonNull(name1);
048                this.name2 = Objects.requireNonNull(name2);
049        }
050
051        public String getName1() {
052                return name1.getIdentifier();
053        }
054        public void setName1(String name1) {
055                this.name1 = new StructureName(Objects.requireNonNull(name1));
056        }
057        public String getName2() {
058                return name2.getIdentifier();
059        }
060        public void setName2(String name2) {
061                this.name2 = new StructureName(Objects.requireNonNull(name2));
062        }
063
064        @Override
065        public String toString() {
066                return "PdbPair [name1=" + name1 + ", name2=" + name2 + "]";
067        }
068
069        @Override
070        public int hashCode() {
071                return Objects.hashCode(name1) + Objects.hashCode(name2);
072        }
073
074        @Override
075        public boolean equals(Object obj) {
076                if (this == obj)
077                        return true;
078                if (obj == null)
079                        return false;
080                if (getClass() != obj.getClass())
081                        return false;
082                PdbPair other = (PdbPair) obj;
083                return (this.name1.equals(other.name1) && this.name2.equals(other.name2)) ||
084                                (this.name1.equals(other.name2) && this.name2.equals(other.name1));
085        }
086
087        @Override
088        public int compareTo(PdbPair o) {
089                //make sure they are not just reverse.
090                if ( this.equals(o))
091                        return 0;
092
093                // Use StructureName's compareTo method
094                int c = name1.compareTo(o.name1);
095                if ( c != 0 )
096                        return c;
097                return name2.compareTo(o.name2);
098        }
099
100        /**
101         * @deprecated use {@link #getPDBCode1()} instead
102         * @return
103         * @throws StructureException
104         */
105        public String getPDBCode1() throws StructureException {
106                PdbId pdbId = name1.getPdbId();
107                return pdbId == null? null: pdbId.getId();
108        }
109
110        /**
111         * @deprecated use {@link #getPDBCode2()} instead
112         * @return
113         * @throws StructureException
114         */
115        @Deprecated
116        public String getPDBCode2() throws StructureException{
117                PdbId pdbId = name2.getPdbId();
118                return pdbId == null? null: pdbId.getId();
119        }
120        
121        /**
122         * @since 6.0.0
123         * @return
124         * @throws StructureException
125         */
126        public PdbId getPdbId1() throws StructureException{
127                return name1.getPdbId();
128        }
129
130        /**
131         * @since 6.0.0
132         * @return
133         * @throws StructureException
134         */
135        public PdbId getPdbId2() throws StructureException{
136                return name2.getPdbId();
137        }
138        
139        public String getChainId1(){
140                return  name1.getChainId();
141        }
142
143        public String getChainId2(){
144                return name2.getChainId();
145        }
146
147        public PdbPair getReverse() {
148                return new PdbPair(name2, name1);
149        }
150}