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;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.regex.Matcher;
026import java.util.regex.Pattern;
027
028import org.biojava.nbio.structure.align.util.AtomCache;
029
030public class BioAssemblyIdentifier implements StructureIdentifier {
031
032        private static final long serialVersionUID = -356206725119993449L;
033
034        private PdbId pdbId;
035        private int biolNr;
036
037        public static final Pattern BIO_NAME_PATTERN = Pattern.compile("^(?:BIO:)([0-9][a-z0-9]{3})(?::([0-9]+))?$", Pattern.CASE_INSENSITIVE);
038
039        public BioAssemblyIdentifier(String name) {
040                Matcher match = BIO_NAME_PATTERN.matcher(name);
041                if(! match.matches() ) {
042                        throw new IllegalArgumentException("Invalid BIO identifier");
043                }
044                pdbId = new PdbId(match.group(1));
045                if(match.group(2) != null) {
046                        biolNr = Integer.parseInt(match.group(2));
047                } else {
048                        biolNr = 1;
049                }
050        }
051
052        /**
053         * @param pdbCode
054         * @param biolNr
055         */
056        public BioAssemblyIdentifier(String pdbCode, int biolNr) {
057                this(new PdbId(pdbCode), biolNr);
058        }
059
060        /**
061         * @param pdbCode
062         * @param biolNr
063         */
064        public BioAssemblyIdentifier(PdbId pdbId, int biolNr) {
065                this.pdbId = pdbId;
066                this.biolNr = biolNr;
067        }
068
069        @Override
070        public String getIdentifier() {
071                if( biolNr < 0) {
072                        return "BIO:"+pdbId.getId();
073                } else {
074                        return String.format("BIO:%s:%d",pdbId.getId(),biolNr);
075                }
076        }
077        @Override
078        public String toString() {
079                return getIdentifier();
080        }
081
082        @Override
083        public Structure loadStructure(AtomCache cache) throws StructureException,
084                        IOException {
085                return cache.getBiologicalAssembly(pdbId, biolNr, AtomCache.DEFAULT_BIOASSEMBLY_STYLE);
086        }
087
088        @Override
089        public SubstructureIdentifier toCanonical() throws StructureException {
090                return new SubstructureIdentifier(pdbId, new ArrayList<ResidueRange>());
091        }
092
093        @Override
094        public Structure reduce(Structure input) throws StructureException {
095                // Should be the full structure
096                return input;
097        }
098
099}