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 String pdbCode;
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                pdbCode = 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        public BioAssemblyIdentifier(String pdbCode, int biolNr) {
053                this.pdbCode = pdbCode;
054                this.biolNr = biolNr;
055        }
056
057        @Override
058        public String getIdentifier() {
059                if( biolNr < 0) {
060                        return "BIO:"+pdbCode;
061                } else {
062                        return String.format("BIO:%s:%d",pdbCode,biolNr);
063                }
064        }
065        @Override
066        public String toString() {
067                return getIdentifier();
068        }
069
070        @Override
071        public Structure loadStructure(AtomCache cache) throws StructureException,
072                        IOException {
073                return cache.getBiologicalAssembly(pdbCode, biolNr, AtomCache.DEFAULT_BIOASSEMBLY_STYLE);
074        }
075
076        @Override
077        public SubstructureIdentifier toCanonical() throws StructureException {
078                return new SubstructureIdentifier(pdbCode, new ArrayList<ResidueRange>());
079        }
080
081        @Override
082        public Structure reduce(Structure input) throws StructureException {
083                // Should be the full structure
084                return input;
085        }
086
087}