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.symmetry.core;
022
023import org.biojava.nbio.structure.Atom;
024import org.biojava.nbio.structure.Group;
025
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.List;
029
030/**
031 * Bean for a single sequence. These are intended to be unique sequences (100% id)
032 * as an imput to clustering.
033 *
034 */
035public class UniqueSequenceList implements Cloneable {
036        private String sequenceString = "";
037        private String seqResSequence = "";
038        private List<Integer> alignment1 = null;
039        private List<Integer> alignment2 = null;
040        private Atom[] caAtoms = null;
041        private String chainId = null;
042        private int modelNumber = -1;
043        private int structureId = -1;
044
045        public UniqueSequenceList(Atom[] cAlphaAtoms, String chainId, int modelNumber, int structureId, String seqResSequence) {
046                this.caAtoms = cAlphaAtoms;
047                this.chainId = chainId;
048                this.modelNumber = modelNumber;
049                this.structureId = structureId;
050                this.seqResSequence = seqResSequence;
051                this.sequenceString =  getSequenceString(cAlphaAtoms);
052                this.alignment1 = new ArrayList<Integer>(cAlphaAtoms.length);
053                this.alignment2 = new ArrayList<Integer>(cAlphaAtoms.length);
054                for (int i = 0; i < cAlphaAtoms.length; i++) {
055                        this.alignment1.add(i);
056                        this.alignment2.add(i);
057                }
058        }
059
060        /**
061         * Return true is the sequence and residues numbers of the passed in array of
062         * atoms matches those of this unique sequence list
063         *
064         * @param caAlphaAtoms
065         * @return
066         */
067        public boolean isMatch(Atom[] caAlphaAtoms) {
068                return sequenceString.equals(getSequenceString(caAlphaAtoms));
069        }
070
071        public String getChainId() {
072                return chainId;
073        }
074
075        public int getModelNumber() {
076                return modelNumber;
077        }
078
079        public int getStructureId() {
080                return structureId;
081        }
082
083        public Atom[] getCalphaAtoms() {
084                return caAtoms;
085        }
086
087        public String getSeqResSequence() {
088                return seqResSequence;
089        }
090
091        /**
092         * @param sequenceString the sequenceString to set
093         */
094        public void setSequenceString(String sequenceString) {
095                this.sequenceString = sequenceString;
096        }
097        /**
098         * @return the alignment1
099         */
100        public List<Integer> getAlignment1() {
101                return alignment1;
102        }
103        /**
104         * @param alignment1 the alignment1 to set
105         */
106        public void setAlignment1(List<Integer> alignment1) {
107                this.alignment1 = alignment1;
108        }
109        /**
110         * @return the alignment2
111         */
112        public List<Integer> getAlignment2() {
113                return alignment2;
114        }
115        /**
116         * @param alignment2 the alignment2 to set
117         */
118        public void setAlignment2(List<Integer> alignment2) {
119                this.alignment2 = alignment2;
120        }
121
122        @Override
123        public Object clone() {
124                UniqueSequenceList copy = null;
125                try {
126                        copy = (UniqueSequenceList) super.clone();
127                } catch (CloneNotSupportedException e) {
128                        // TODO Auto-generated catch block
129                        e.printStackTrace();
130                }
131                // deep copy lists and arrays
132                copy.alignment1 = new ArrayList<Integer>(this.alignment1);
133                copy.alignment2 = new ArrayList<Integer>(this.alignment2);
134                copy.caAtoms = Arrays.copyOf(this.caAtoms, this.caAtoms.length); // note, that atoms in this array will be identical (this is intended)
135                return copy;
136        }
137
138        public static String getSequenceString(Atom[] caAlphaAtoms) {
139                StringBuilder builder = new StringBuilder();
140
141                for (Atom a:  caAlphaAtoms) {
142                        Group g = a.getGroup();
143                        // TODO is the check for UNK required? UNK should have been filtered already in ChainClusterer?
144                        if (! g.getPDBName().equals("UNK")) {
145                                builder.append(g.getResidueNumber());
146                                builder.append(g.getPDBName());
147                        }
148                }
149
150//              System.out.println("getSequenceString: " + builder.toString());
151                return builder.toString();
152        }
153
154        @Override
155        public String toString() {
156                StringBuilder builder = new StringBuilder();
157                builder.append("length: ");
158                builder.append(caAtoms.length);
159                builder.append(" seq: ");
160                builder.append(sequenceString);
161                builder.append("\n");
162                builder.append(alignment1);
163                builder.append("\n");
164                builder.append(alignment2);
165                return builder.toString();
166        }
167
168}