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 * @author Karl Nicholas <github:karlnicholas>
015 *
016 * For more information on the BioJava project and its aims,
017 * or to join the biojava-l mailing list, visit the home page
018 * at:
019 *
020 *      http://www.biojava.org/
021 *
022 * Created on 01-21-2010
023 */
024package org.biojava.nbio.core.sequence.io;
025
026import org.biojava.nbio.core.exceptions.ParserException;
027import org.biojava.nbio.core.sequence.AccessionID;
028import org.biojava.nbio.core.sequence.io.template.SequenceHeaderParserInterface;
029import org.biojava.nbio.core.sequence.reference.AbstractReference;
030import org.biojava.nbio.core.sequence.template.AbstractSequence;
031import org.biojava.nbio.core.sequence.template.Compound;
032
033import java.util.ArrayList;
034import java.util.List;
035
036import org.biojava.nbio.core.sequence.DataSource;
037
038public class GenericGenbankHeaderParser<S extends AbstractSequence<C>, C extends Compound> implements SequenceHeaderParserInterface<S,C> {
039
040        private String accession = null;
041        private String identifier = null;
042        private String name = null;
043        @SuppressWarnings("unused")
044        private int version;
045        private boolean versionSeen;
046        private ArrayList<String> comments = new ArrayList<>();
047        private List<AbstractReference> references = new ArrayList<>();
048
049        /**
050         * Parse the header and set the values in the sequence
051         * @param header
052         * @param sequence
053         */
054        @Override
055        public void parseHeader(String header, S sequence) {
056                sequence.setOriginalHeader(header);
057                sequence.setAccession(new AccessionID(accession, DataSource.GENBANK, version, identifier));
058                sequence.setDescription(description);
059                sequence.setComments(comments);
060                sequence.setReferences(references);
061        }
062
063        /**
064         * Sets the sequence info back to default values, ie. in order to start
065         * constructing a new sequence from scratch.
066         */
067        @SuppressWarnings("unused")
068        private void reset() {
069                this.version = 0;
070                this.versionSeen = false;
071                this.accession = null;
072                this.description = null;
073                this.identifier = null;
074                this.name = null;
075                this.comments.clear();
076        }
077
078        /**
079         * {@inheritDoc}
080         */
081        public void setVersion(int version) throws ParserException {
082                if (this.versionSeen) throw new ParserException("Current BioEntry already has a version");
083                else {
084                        try {
085                                this.version = version;
086                                this.versionSeen = true;
087                        } catch (NumberFormatException e) {
088                                throw new ParserException("Could not parse version as an integer");
089                        }
090                }
091        }
092
093
094        /**
095         * {@inheritDoc}
096         * The last accession passed to this routine will always be the one used.
097         */
098        public void setAccession(String accession) throws ParserException {
099                if (accession==null) throw new ParserException("Accession cannot be null");
100                this.accession = accession;
101        }
102
103        /**
104         * {@inheritDoc}
105         */
106        public void setDescription(String description) throws ParserException {
107                if (this.description!=null) throw new ParserException("Current BioEntry already has a description");
108                this.description = description;
109        }
110        private String description;
111
112        /**
113         * {@inheritDoc}
114         */
115        public void setIdentifier(String identifier) throws ParserException {
116                if (identifier==null) throw new ParserException("Identifier cannot be null");
117                if (this.identifier!=null) throw new ParserException("Current BioEntry already has a identifier");
118                this.identifier = identifier;
119        }
120
121        /**
122         * {@inheritDoc}
123         */
124        public void setName(String name) throws ParserException {
125                if (name==null) throw new ParserException("Name cannot be null");
126                if (this.name!=null) throw new ParserException("Current BioEntry already has a name");
127                this.name = name;
128        }
129
130        /**
131         * {@inheritDoc}
132         */
133        public void setComment(String comment) throws ParserException {
134                if (comment==null) throw new ParserException("Comment cannot be null");
135                this.comments.add(comment);
136        }
137
138        public void addReference(AbstractReference abstractReference){
139            this.references.add(abstractReference);
140    }
141}