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.io.parsers.phyloxml.data;
28 import java.util.HashMap;
31 import org.forester.io.parsers.phyloxml.PhyloXmlHandler;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.io.parsers.phyloxml.XmlElement;
34 import org.forester.io.parsers.util.PhylogenyParserException;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.data.Confidence;
37 import org.forester.phylogeny.data.Sequence;
38 import org.forester.phylogeny.data.SequenceRelation;
40 public class SequenceRelationParser implements PhylogenyDataPhyloXmlParser {
42 private static final Map<Phylogeny, SequenceRelationParser> _instances = new HashMap<Phylogeny, SequenceRelationParser>();
43 private Phylogeny _phylogeny;
45 private SequenceRelationParser() {
49 public SequenceRelation parse( final XmlElement element ) throws PhylogenyParserException {
50 final SequenceRelation seqRelation = new SequenceRelation();
51 if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_RELATION_TYPE ) ) {
52 final String sType = element.getAttribute( PhyloXmlMapping.SEQUENCE_RELATION_TYPE );
53 seqRelation.setType( SequenceRelation.SEQUENCE_RELATION_TYPE.valueOf( sType ) );
55 if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_RELATION_ID_REF0 ) && ( _phylogeny != null ) ) {
56 final Sequence ref = PhyloXmlHandler.getSequenceMapByIdForPhylogeny( _phylogeny ).get( element
57 .getAttribute( PhyloXmlMapping.SEQUENCE_RELATION_ID_REF0 ) );
59 seqRelation.setRef0( ref );
62 if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_RELATION_ID_REF1 ) && ( _phylogeny != null ) ) {
63 final Sequence ref = PhyloXmlHandler.getSequenceMapByIdForPhylogeny( _phylogeny ).get( element
64 .getAttribute( PhyloXmlMapping.SEQUENCE_RELATION_ID_REF1 ) );
66 seqRelation.setRef1( ref );
69 if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_RELATION_DISTANCE ) ) {
70 seqRelation.setDistance( Double
71 .valueOf( element.getAttribute( PhyloXmlMapping.SEQUENCE_RELATION_DISTANCE ) ) );
73 for( int i = 0; i < element.getNumberOfChildElements(); ++i ) {
74 final XmlElement child_element = element.getChildElement( i );
75 if ( child_element.getQualifiedName().equals( PhyloXmlMapping.CONFIDENCE ) ) {
76 seqRelation.setConfidence( ( Confidence ) ConfidenceParser.getInstance().parse( child_element ) );
82 public static PhylogenyDataPhyloXmlParser getInstance( final Phylogeny phylogeny ) {
83 SequenceRelationParser instance = _instances.get( phylogeny );
84 if ( instance == null ) {
85 instance = new SequenceRelationParser();
86 instance._phylogeny = phylogeny;
87 _instances.put( phylogeny, instance );