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.template.AbstractSequence; 030import org.biojava.nbio.core.sequence.template.Compound; 031 032import java.util.ArrayList; 033import org.biojava.nbio.core.sequence.DataSource; 034 035public class GenericGenbankHeaderParser<S extends AbstractSequence<C>, C extends Compound> implements SequenceHeaderParserInterface<S,C> { 036 037 private String accession = null; 038 private String identifier = null; 039 private String name = null; 040 @SuppressWarnings("unused") 041 private int version; 042 private boolean versionSeen; 043 private ArrayList<String> comments = new ArrayList<String>(); 044 045 /** 046 * Parse the header and set the values in the sequence 047 * @param header 048 * @param sequence 049 */ 050 @Override 051 public void parseHeader(String header, S sequence) { 052 sequence.setOriginalHeader(header); 053 sequence.setAccession(new AccessionID(accession, DataSource.GENBANK, version, identifier)); 054 sequence.setDescription(description); 055 } 056 057 /** 058 * Sets the sequence info back to default values, ie. in order to start 059 * constructing a new sequence from scratch. 060 */ 061 @SuppressWarnings("unused") 062 private void reset() { 063 this.version = 0; 064 this.versionSeen = false; 065 this.accession = null; 066 this.description = null; 067 this.identifier = null; 068 this.name = null; 069 this.comments.clear(); 070 } 071 072 /** 073 * {@inheritDoc} 074 */ 075 public void setVersion(int version) throws ParserException { 076 if (this.versionSeen) throw new ParserException("Current BioEntry already has a version"); 077 else { 078 try { 079 this.version = version; 080 this.versionSeen = true; 081 } catch (NumberFormatException e) { 082 throw new ParserException("Could not parse version as an integer"); 083 } 084 } 085 } 086 087 088 /** 089 * {@inheritDoc} 090 * The last accession passed to this routine will always be the one used. 091 */ 092 public void setAccession(String accession) throws ParserException { 093 if (accession==null) throw new ParserException("Accession cannot be null"); 094 this.accession = accession; 095 } 096 097 /** 098 * {@inheritDoc} 099 */ 100 public void setDescription(String description) throws ParserException { 101 if (this.description!=null) throw new ParserException("Current BioEntry already has a description"); 102 this.description = description; 103 } 104 private String description; 105 106 /** 107 * {@inheritDoc} 108 */ 109 public void setIdentifier(String identifier) throws ParserException { 110 if (identifier==null) throw new ParserException("Identifier cannot be null"); 111 if (this.identifier!=null) throw new ParserException("Current BioEntry already has a identifier"); 112 this.identifier = identifier; 113 } 114 115 /** 116 * {@inheritDoc} 117 */ 118 public void setName(String name) throws ParserException { 119 if (name==null) throw new ParserException("Name cannot be null"); 120 if (this.name!=null) throw new ParserException("Current BioEntry already has a name"); 121 this.name = name; 122 } 123 124 /** 125 * {@inheritDoc} 126 */ 127 public void setComment(String comment) throws ParserException { 128 if (comment==null) throw new ParserException("Comment cannot be null"); 129 this.comments.add(comment); 130 } 131}