2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.phylogeny.data;
28 import java.io.IOException;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.SortedSet;
33 import java.util.TreeSet;
35 import org.forester.io.parsers.nhx.NHXtags;
36 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
37 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
38 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
39 import org.forester.io.writers.PhylogenyWriter;
40 import org.forester.util.ForesterUtil;
42 public class Sequence implements PhylogenyData, MultipleUris {
44 private String _mol_sequence;
45 private boolean _mol_sequence_is_aligned;
47 private String _source_id;
48 private Accession _accession;
49 private String _symbol;
50 private String _location;
52 private SortedSet<Annotation> _annotations;
53 private DomainArchitecture _da;
54 private List<Uri> _uris;
55 private List<SequenceRelation> _seq_relations;
61 public boolean isEmpty() {
62 return ( getAccession() == null ) && ForesterUtil.isEmpty( getName() ) && ForesterUtil.isEmpty( getSymbol() )
63 && ForesterUtil.isEmpty( getType() ) && ForesterUtil.isEmpty( getLocation() )
64 && ForesterUtil.isEmpty( getSourceId() ) && ForesterUtil.isEmpty( getMolecularSequence() )
65 && ( getDomainArchitecture() == null ) && ForesterUtil.isEmpty( _annotations )
66 && ForesterUtil.isEmpty( _uris ) && ForesterUtil.isEmpty( _seq_relations );
69 public void addAnnotation( final Annotation annotation ) {
70 getAnnotations().add( annotation );
74 public void addUri( final Uri uri ) {
75 if ( getUris() == null ) {
76 setUris( new ArrayList<Uri>() );
81 public void addSequenceRelation( final SequenceRelation sr ) {
82 _seq_relations.add( sr );
86 public StringBuffer asSimpleText() {
87 final StringBuffer sb = new StringBuffer();
88 if ( getAccession() != null ) {
90 sb.append( getAccession() );
93 if ( !ForesterUtil.isEmpty( getName() ) ) {
94 sb.append( getName() );
97 if ( !ForesterUtil.isEmpty( getLocation() ) ) {
98 sb.append( getLocation() );
104 public StringBuffer asText() {
105 return asSimpleText();
113 public PhylogenyData copy() {
114 final Sequence seq = new Sequence();
115 seq.setAnnotations( getAnnotations() );
116 seq.setName( getName() );
118 seq.setSymbol( getSymbol() );
120 catch ( final PhyloXmlDataFormatException e ) {
123 seq.setMolecularSequence( getMolecularSequence() );
124 seq.setMolecularSequenceAligned( isMolecularSequenceAligned() );
125 seq.setLocation( getLocation() );
126 if ( getAccession() != null ) {
127 seq.setAccession( ( Accession ) getAccession().copy() );
130 seq.setAccession( null );
133 seq.setType( getType() );
135 catch ( final PhyloXmlDataFormatException e ) {
138 if ( getUris() != null ) {
139 seq.setUris( new ArrayList<Uri>() );
140 for( final Uri uri : getUris() ) {
142 seq.getUris().add( uri );
146 if ( getDomainArchitecture() != null ) {
147 seq.setDomainArchitecture( ( DomainArchitecture ) getDomainArchitecture().copy() );
150 seq.setDomainArchitecture( null );
156 public boolean equals( final Object o ) {
160 else if ( o == null ) {
163 else if ( o.getClass() != this.getClass() ) {
164 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
165 + o.getClass() + "]" );
168 return isEqual( ( Sequence ) o );
172 public Accession getAccession() {
176 public Annotation getAnnotation( final int i ) {
177 return ( Annotation ) getAnnotations().toArray()[ i ];
180 public SortedSet<Annotation> getAnnotations() {
181 if ( _annotations == null ) {
182 _annotations = new TreeSet<Annotation>();
187 public DomainArchitecture getDomainArchitecture() {
191 public String getLocation() {
195 public String getMolecularSequence() {
196 return _mol_sequence;
199 public boolean isMolecularSequenceAligned() {
200 return _mol_sequence_is_aligned;
203 public String getName() {
207 public List<SequenceRelation> getSequenceRelations() {
208 if ( _seq_relations == null ) {
209 _seq_relations = new ArrayList<SequenceRelation>();
211 return _seq_relations;
214 private void setSequenceRelations( final List<SequenceRelation> seq_relations ) {
215 _seq_relations = seq_relations;
218 public String getSourceId() {
222 public String getSymbol() {
226 public String getType() {
231 public List<Uri> getUris() {
236 public Uri getUri( final int index ) {
237 return getUris().get( index );
241 public int hashCode() {
242 if ( getAccession() != null ) {
243 return getAccession().hashCode();
245 int result = getSymbol().hashCode();
246 if ( getName().length() > 0 ) {
247 result ^= getName().hashCode();
249 if ( getMolecularSequence().length() > 0 ) {
250 result ^= getMolecularSequence().hashCode();
255 public boolean hasSequenceRelations() {
256 return _seq_relations.size() > 0;
260 setAnnotations( null );
262 setMolecularSequence( "" );
263 setMolecularSequenceAligned( false );
265 setAccession( null );
269 catch ( final PhyloXmlDataFormatException e ) {
275 catch ( final PhyloXmlDataFormatException e ) {
278 setDomainArchitecture( null );
280 setSequenceRelations( null );
285 public boolean isEqual( final PhylogenyData data ) {
286 if ( this == data ) {
289 final Sequence s = ( Sequence ) data;
290 if ( ( getAccession() != null ) && ( s.getAccession() != null ) ) {
291 return getAccession().isEqual( s.getAccession() );
293 return s.getMolecularSequence().equals( getMolecularSequence() ) && s.getName().equals( getName() )
294 && s.getSymbol().equals( getSymbol() );
297 public void setAccession( final Accession accession ) {
298 _accession = accession;
301 private void setAnnotations( final SortedSet<Annotation> annotations ) {
302 _annotations = annotations;
305 public void setDomainArchitecture( final DomainArchitecture ds ) {
309 public void setLocation( final String description ) {
310 _location = description;
313 public void setMolecularSequence( final String mol_sequence ) {
314 _mol_sequence = mol_sequence;
317 public void setMolecularSequenceAligned( final boolean aligned ) {
318 _mol_sequence_is_aligned = aligned;
321 public void setName( final String name ) {
325 public void setSourceId( final String source_id ) {
326 _source_id = source_id;
329 public void setSymbol( final String symbol ) throws PhyloXmlDataFormatException {
330 if ( !ForesterUtil.isEmpty( symbol ) && !PhyloXmlUtil.SEQUENCE_SYMBOL_PATTERN.matcher( symbol ).matches() ) {
331 throw new PhyloXmlDataFormatException( "illegal sequence symbol: [" + symbol + "]" );
336 public void setType( final String type ) throws PhyloXmlDataFormatException {
337 if ( !ForesterUtil.isEmpty( type ) && !PhyloXmlUtil.SEQUENCE_TYPES.contains( type ) ) {
338 throw new PhyloXmlDataFormatException( "illegal sequence type: [" + type + "]" );
344 public void setUris( final List<Uri> uris ) {
349 public StringBuffer toNHX() {
350 final StringBuffer sb = new StringBuffer();
351 if ( getName().length() > 0 ) {
353 sb.append( NHXtags.GENE_NAME );
354 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getName() ) );
356 if ( getAccession() != null ) {
357 getAccession().toNHX();
359 if ( getDomainArchitecture() != null ) {
360 sb.append( getDomainArchitecture().toNHX() );
366 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
370 final String my_ind = indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE;
371 writer.write( ForesterUtil.LINE_SEPARATOR );
372 writer.write( indentation );
373 PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.SEQUENCE, PhyloXmlMapping.SEQUENCE_TYPE, getType() );
374 if ( !ForesterUtil.isEmpty( getSymbol() ) ) {
375 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.SEQUENCE_SYMBOL, getSymbol(), indentation );
377 if ( ( getAccession() != null ) && !ForesterUtil.isEmpty( getAccession().getValue() ) ) {
378 getAccession().toPhyloXML( writer, level, indentation );
380 if ( !ForesterUtil.isEmpty( getName() ) ) {
381 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.SEQUENCE_NAME, getName(), indentation );
383 if ( !ForesterUtil.isEmpty( getLocation() ) ) {
384 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.SEQUENCE_LOCATION, getLocation(), indentation );
386 if ( !ForesterUtil.isEmpty( getMolecularSequence() ) ) {
387 PhylogenyDataUtil.appendElement( writer,
388 PhyloXmlMapping.SEQUENCE_MOL_SEQ,
389 getMolecularSequence(),
390 PhyloXmlMapping.SEQUENCE_MOL_SEQ_ALIGNED_ATTR,
391 String.valueOf( isMolecularSequenceAligned() ),
394 if ( getUris() != null ) {
395 for( final Uri uri : getUris() ) {
397 uri.toPhyloXML( writer, level, indentation );
401 if ( _annotations != null ) {
402 for( final PhylogenyData annotation : getAnnotations() ) {
403 annotation.toPhyloXML( writer, level, my_ind );
406 if ( getDomainArchitecture() != null ) {
407 getDomainArchitecture().toPhyloXML( writer, level, my_ind );
409 writer.write( ForesterUtil.LINE_SEPARATOR );
410 writer.write( indentation );
411 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.SEQUENCE );
415 public String toString() {
416 return asText().toString();